1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gl/gl_image_linux_dma_buffer.h"
9 #define FOURCC(a, b, c, d) \
10 ((static_cast<uint32>(a)) | (static_cast<uint32>(b) << 8) | \
11 (static_cast<uint32>(c) << 16) | (static_cast<uint32>(d) << 24))
13 #define DRM_FORMAT_ARGB8888 FOURCC('A', 'R', '2', '4')
14 #define DRM_FORMAT_XRGB8888 FOURCC('X', 'R', '2', '4')
19 bool ValidInternalFormat(unsigned internalformat
) {
20 switch (internalformat
) {
29 bool ValidFormat(gfx::GpuMemoryBuffer::Format format
) {
31 case GpuMemoryBuffer::BGRA_8888
:
32 case GpuMemoryBuffer::RGBX_8888
:
34 case GpuMemoryBuffer::ATC
:
35 case GpuMemoryBuffer::ATCIA
:
36 case GpuMemoryBuffer::DXT1
:
37 case GpuMemoryBuffer::DXT5
:
38 case GpuMemoryBuffer::ETC1
:
39 case GpuMemoryBuffer::R_8
:
40 case GpuMemoryBuffer::RGBA_4444
:
41 case GpuMemoryBuffer::RGBA_8888
:
42 case GpuMemoryBuffer::YUV_420
:
50 EGLint
FourCC(gfx::GpuMemoryBuffer::Format format
) {
52 case GpuMemoryBuffer::BGRA_8888
:
53 return DRM_FORMAT_ARGB8888
;
54 case GpuMemoryBuffer::RGBX_8888
:
55 return DRM_FORMAT_XRGB8888
;
56 case GpuMemoryBuffer::ATC
:
57 case GpuMemoryBuffer::ATCIA
:
58 case GpuMemoryBuffer::DXT1
:
59 case GpuMemoryBuffer::DXT5
:
60 case GpuMemoryBuffer::ETC1
:
61 case GpuMemoryBuffer::R_8
:
62 case GpuMemoryBuffer::RGBA_4444
:
63 case GpuMemoryBuffer::RGBA_8888
:
64 case GpuMemoryBuffer::YUV_420
:
73 bool IsHandleValid(const base::FileDescriptor
& handle
) {
74 return handle
.fd
>= 0;
79 GLImageLinuxDMABuffer::GLImageLinuxDMABuffer(const Size
& size
,
80 unsigned internalformat
)
81 : GLImageEGL(size
), internalformat_(internalformat
) {
84 GLImageLinuxDMABuffer::~GLImageLinuxDMABuffer() {
87 bool GLImageLinuxDMABuffer::Initialize(const base::FileDescriptor
& handle
,
88 GpuMemoryBuffer::Format format
,
90 if (!ValidInternalFormat(internalformat_
)) {
91 LOG(ERROR
) << "Invalid internalformat: " << internalformat_
;
95 if (!ValidFormat(format
)) {
96 LOG(ERROR
) << "Invalid format: " << format
;
100 if (!IsHandleValid(handle
)) {
101 LOG(ERROR
) << "Invalid file descriptor: " << handle
.fd
;
105 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
106 // target, the EGL will take a reference to the dma_buf.
107 EGLint attrs
[] = {EGL_WIDTH
,
111 EGL_LINUX_DRM_FOURCC_EXT
,
113 EGL_DMA_BUF_PLANE0_FD_EXT
,
115 EGL_DMA_BUF_PLANE0_OFFSET_EXT
,
117 EGL_DMA_BUF_PLANE0_PITCH_EXT
,
120 return GLImageEGL::Initialize(
121 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(NULL
), attrs
);
124 unsigned GLImageLinuxDMABuffer::GetInternalFormat() { return internalformat_
; }