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 ValidFormat(unsigned internalformat
, gfx::GpuMemoryBuffer::Format format
) {
20 switch (internalformat
) {
23 case gfx::GpuMemoryBuffer::RGBX_8888
:
25 case gfx::GpuMemoryBuffer::RGBA_8888
:
26 case gfx::GpuMemoryBuffer::BGRA_8888
:
33 case gfx::GpuMemoryBuffer::BGRA_8888
:
35 case gfx::GpuMemoryBuffer::RGBX_8888
:
36 case gfx::GpuMemoryBuffer::RGBA_8888
:
46 EGLint
FourCC(gfx::GpuMemoryBuffer::Format format
) {
48 case gfx::GpuMemoryBuffer::BGRA_8888
:
49 return DRM_FORMAT_ARGB8888
;
50 case gfx::GpuMemoryBuffer::RGBX_8888
:
51 return DRM_FORMAT_XRGB8888
;
52 case gfx::GpuMemoryBuffer::RGBA_8888
:
61 bool IsHandleValid(const base::FileDescriptor
& handle
) {
62 return handle
.fd
>= 0;
67 GLImageLinuxDMABuffer::GLImageLinuxDMABuffer(const gfx::Size
& size
,
68 unsigned internalformat
)
69 : GLImageEGL(size
), internalformat_(internalformat
) {
72 GLImageLinuxDMABuffer::~GLImageLinuxDMABuffer() {
75 bool GLImageLinuxDMABuffer::Initialize(const base::FileDescriptor
& handle
,
76 gfx::GpuMemoryBuffer::Format format
,
78 if (!ValidFormat(internalformat_
, format
)) {
79 LOG(ERROR
) << "Invalid format: " << internalformat_
;
83 if (!IsHandleValid(handle
)) {
84 LOG(ERROR
) << "Invalid file descriptor: " << handle
.fd
;
88 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
89 // target, the EGL will take a reference to the dma_buf.
90 EGLint attrs
[] = {EGL_WIDTH
,
94 EGL_LINUX_DRM_FOURCC_EXT
,
96 EGL_DMA_BUF_PLANE0_FD_EXT
,
98 EGL_DMA_BUF_PLANE0_OFFSET_EXT
,
100 EGL_DMA_BUF_PLANE0_PITCH_EXT
,
103 return GLImageEGL::Initialize(
104 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(NULL
), attrs
);