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
, GpuMemoryBuffer::Format format
) {
20 switch (internalformat
) {
22 return format
== GpuMemoryBuffer::ATC
;
23 case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD
:
24 return format
== GpuMemoryBuffer::ATCIA
;
25 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
26 return format
== GpuMemoryBuffer::DXT1
;
27 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
28 return format
== GpuMemoryBuffer::DXT5
;
29 case GL_ETC1_RGB8_OES
:
30 return format
== GpuMemoryBuffer::ETC1
;
33 case GpuMemoryBuffer::RGBX_8888
:
35 case GpuMemoryBuffer::ATC
:
36 case GpuMemoryBuffer::ATCIA
:
37 case GpuMemoryBuffer::DXT1
:
38 case GpuMemoryBuffer::DXT5
:
39 case GpuMemoryBuffer::ETC1
:
40 case GpuMemoryBuffer::R_8
:
41 case GpuMemoryBuffer::RGBA_8888
:
42 case GpuMemoryBuffer::BGRA_8888
:
43 case GpuMemoryBuffer::YUV_420
:
50 case GpuMemoryBuffer::BGRA_8888
:
52 case GpuMemoryBuffer::ATC
:
53 case GpuMemoryBuffer::ATCIA
:
54 case GpuMemoryBuffer::DXT1
:
55 case GpuMemoryBuffer::DXT5
:
56 case GpuMemoryBuffer::ETC1
:
57 case GpuMemoryBuffer::R_8
:
58 case GpuMemoryBuffer::RGBX_8888
:
59 case GpuMemoryBuffer::RGBA_8888
:
60 case GpuMemoryBuffer::YUV_420
:
72 EGLint
FourCC(GpuMemoryBuffer::Format format
) {
74 case GpuMemoryBuffer::BGRA_8888
:
75 return DRM_FORMAT_ARGB8888
;
76 case GpuMemoryBuffer::RGBX_8888
:
77 return DRM_FORMAT_XRGB8888
;
78 case GpuMemoryBuffer::ATC
:
79 case GpuMemoryBuffer::ATCIA
:
80 case GpuMemoryBuffer::DXT1
:
81 case GpuMemoryBuffer::DXT5
:
82 case GpuMemoryBuffer::ETC1
:
83 case GpuMemoryBuffer::R_8
:
84 case GpuMemoryBuffer::RGBA_8888
:
85 case GpuMemoryBuffer::YUV_420
:
94 bool IsHandleValid(const base::FileDescriptor
& handle
) {
95 return handle
.fd
>= 0;
100 GLImageLinuxDMABuffer::GLImageLinuxDMABuffer(const Size
& size
,
101 unsigned internalformat
)
102 : GLImageEGL(size
), internalformat_(internalformat
) {
105 GLImageLinuxDMABuffer::~GLImageLinuxDMABuffer() {
108 bool GLImageLinuxDMABuffer::Initialize(const base::FileDescriptor
& handle
,
109 GpuMemoryBuffer::Format format
,
111 if (!ValidFormat(internalformat_
, format
)) {
112 LOG(ERROR
) << "Invalid format: " << internalformat_
;
116 if (!IsHandleValid(handle
)) {
117 LOG(ERROR
) << "Invalid file descriptor: " << handle
.fd
;
121 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
122 // target, the EGL will take a reference to the dma_buf.
123 EGLint attrs
[] = {EGL_WIDTH
,
127 EGL_LINUX_DRM_FOURCC_EXT
,
129 EGL_DMA_BUF_PLANE0_FD_EXT
,
131 EGL_DMA_BUF_PLANE0_OFFSET_EXT
,
133 EGL_DMA_BUF_PLANE0_PITCH_EXT
,
136 return GLImageEGL::Initialize(
137 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(NULL
), attrs
);