Roll src/third_party/WebKit 327fcc4:20c53c8 (svn 191269:191273)
[chromium-blink-merge.git] / ui / gl / gl_image_linux_dma_buffer.cc
blob04ad06bd438433b270c60b804509a088d08e0a44
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"
7 #include <unistd.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')
16 namespace gfx {
17 namespace {
19 bool ValidFormat(unsigned internalformat, gfx::GpuMemoryBuffer::Format format) {
20 switch (internalformat) {
21 case GL_ATC_RGB_AMD:
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;
31 case GL_RGB:
32 switch (format) {
33 case gfx::GpuMemoryBuffer::RGBX_8888:
34 return true;
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:
42 return false;
44 NOTREACHED();
45 return false;
46 case GL_RGBA:
47 switch (format) {
48 case gfx::GpuMemoryBuffer::BGRA_8888:
49 return true;
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:
57 return false;
59 NOTREACHED();
60 return false;
61 default:
62 return false;
66 EGLint FourCC(gfx::GpuMemoryBuffer::Format format) {
67 switch (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:
78 NOTREACHED();
79 return 0;
82 NOTREACHED();
83 return 0;
86 bool IsHandleValid(const base::FileDescriptor& handle) {
87 return handle.fd >= 0;
90 } // namespace
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,
102 int pitch) {
103 if (!ValidFormat(internalformat_, format)) {
104 LOG(ERROR) << "Invalid format: " << internalformat_;
105 return false;
108 if (!IsHandleValid(handle)) {
109 LOG(ERROR) << "Invalid file descriptor: " << handle.fd;
110 return false;
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,
116 size_.width(),
117 EGL_HEIGHT,
118 size_.height(),
119 EGL_LINUX_DRM_FOURCC_EXT,
120 FourCC(format),
121 EGL_DMA_BUF_PLANE0_FD_EXT,
122 handle.fd,
123 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
125 EGL_DMA_BUF_PLANE0_PITCH_EXT,
126 pitch,
127 EGL_NONE};
128 return GLImageEGL::Initialize(
129 EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(NULL), attrs);
132 } // namespace gfx