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
) {
22 return format
== gfx::GpuMemoryBuffer::ATC
;
23 case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD
:
24 return format
== gfx::GpuMemoryBuffer::ATCIA
;
25 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
26 return format
== gfx::GpuMemoryBuffer::DXT1
;
27 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
28 return format
== gfx::GpuMemoryBuffer::DXT5
;
29 case GL_ETC1_RGB8_OES
:
30 return format
== gfx::GpuMemoryBuffer::ETC1
;
33 case gfx::GpuMemoryBuffer::RGBX_8888
:
35 case gfx::GpuMemoryBuffer::ATC
:
36 case gfx::GpuMemoryBuffer::ATCIA
:
37 case gfx::GpuMemoryBuffer::DXT1
:
38 case gfx::GpuMemoryBuffer::DXT5
:
39 case gfx::GpuMemoryBuffer::ETC1
:
40 case gfx::GpuMemoryBuffer::RGBA_8888
:
41 case gfx::GpuMemoryBuffer::BGRA_8888
:
48 case gfx::GpuMemoryBuffer::BGRA_8888
:
50 case gfx::GpuMemoryBuffer::ATC
:
51 case gfx::GpuMemoryBuffer::ATCIA
:
52 case gfx::GpuMemoryBuffer::DXT1
:
53 case gfx::GpuMemoryBuffer::DXT5
:
54 case gfx::GpuMemoryBuffer::ETC1
:
55 case gfx::GpuMemoryBuffer::RGBX_8888
:
56 case gfx::GpuMemoryBuffer::RGBA_8888
:
66 EGLint
FourCC(gfx::GpuMemoryBuffer::Format format
) {
68 case gfx::GpuMemoryBuffer::BGRA_8888
:
69 return DRM_FORMAT_ARGB8888
;
70 case gfx::GpuMemoryBuffer::RGBX_8888
:
71 return DRM_FORMAT_XRGB8888
;
72 case gfx::GpuMemoryBuffer::ATC
:
73 case gfx::GpuMemoryBuffer::ATCIA
:
74 case gfx::GpuMemoryBuffer::DXT1
:
75 case gfx::GpuMemoryBuffer::DXT5
:
76 case gfx::GpuMemoryBuffer::ETC1
:
77 case gfx::GpuMemoryBuffer::RGBA_8888
:
86 bool IsHandleValid(const base::FileDescriptor
& handle
) {
87 return handle
.fd
>= 0;
92 GLImageLinuxDMABuffer::GLImageLinuxDMABuffer(const gfx::Size
& size
,
93 unsigned internalformat
)
94 : GLImageEGL(size
), internalformat_(internalformat
) {
97 GLImageLinuxDMABuffer::~GLImageLinuxDMABuffer() {
100 bool GLImageLinuxDMABuffer::Initialize(const base::FileDescriptor
& handle
,
101 gfx::GpuMemoryBuffer::Format format
,
103 if (!ValidFormat(internalformat_
, format
)) {
104 LOG(ERROR
) << "Invalid format: " << internalformat_
;
108 if (!IsHandleValid(handle
)) {
109 LOG(ERROR
) << "Invalid file descriptor: " << handle
.fd
;
113 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
114 // target, the EGL will take a reference to the dma_buf.
115 EGLint attrs
[] = {EGL_WIDTH
,
119 EGL_LINUX_DRM_FOURCC_EXT
,
121 EGL_DMA_BUF_PLANE0_FD_EXT
,
123 EGL_DMA_BUF_PLANE0_OFFSET_EXT
,
125 EGL_DMA_BUF_PLANE0_PITCH_EXT
,
128 return GLImageEGL::Initialize(
129 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(NULL
), attrs
);