Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gl / gl_image_linux_dma_buffer.cc
blob1d2af6eb2258767d94a4fab7feba4949a900284c
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, GpuMemoryBuffer::Format format) {
20 switch (internalformat) {
21 case GL_ATC_RGB_AMD:
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;
31 case GL_RGB:
32 switch (format) {
33 case GpuMemoryBuffer::RGBX_8888:
34 return true;
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:
44 return false;
46 NOTREACHED();
47 return false;
48 case GL_RGBA:
49 switch (format) {
50 case GpuMemoryBuffer::BGRA_8888:
51 return true;
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:
61 return false;
63 NOTREACHED();
64 return false;
65 case GL_R8:
66 return false;
67 default:
68 return false;
72 EGLint FourCC(GpuMemoryBuffer::Format format) {
73 switch (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:
86 NOTREACHED();
87 return 0;
90 NOTREACHED();
91 return 0;
94 bool IsHandleValid(const base::FileDescriptor& handle) {
95 return handle.fd >= 0;
98 } // namespace
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,
110 int pitch) {
111 if (!ValidFormat(internalformat_, format)) {
112 LOG(ERROR) << "Invalid format: " << internalformat_;
113 return false;
116 if (!IsHandleValid(handle)) {
117 LOG(ERROR) << "Invalid file descriptor: " << handle.fd;
118 return false;
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,
124 size_.width(),
125 EGL_HEIGHT,
126 size_.height(),
127 EGL_LINUX_DRM_FOURCC_EXT,
128 FourCC(format),
129 EGL_DMA_BUF_PLANE0_FD_EXT,
130 handle.fd,
131 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
133 EGL_DMA_BUF_PLANE0_PITCH_EXT,
134 pitch,
135 EGL_NONE};
136 return GLImageEGL::Initialize(
137 EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(NULL), attrs);
140 } // namespace gfx