base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / gl / gl_image_linux_dma_buffer.cc
blob1dec94209763c95020441fdb35d1989c48d41cb3
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_RGB:
22 switch (format) {
23 case gfx::GpuMemoryBuffer::RGBX_8888:
24 return true;
25 case gfx::GpuMemoryBuffer::RGBA_8888:
26 case gfx::GpuMemoryBuffer::BGRA_8888:
27 return false;
29 NOTREACHED();
30 return false;
31 case GL_RGBA:
32 switch (format) {
33 case gfx::GpuMemoryBuffer::BGRA_8888:
34 return true;
35 case gfx::GpuMemoryBuffer::RGBX_8888:
36 case gfx::GpuMemoryBuffer::RGBA_8888:
37 return false;
39 NOTREACHED();
40 return false;
41 default:
42 return false;
46 EGLint FourCC(gfx::GpuMemoryBuffer::Format format) {
47 switch (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:
53 NOTREACHED();
54 return 0;
57 NOTREACHED();
58 return 0;
61 bool IsHandleValid(const base::FileDescriptor& handle) {
62 return handle.fd >= 0;
65 } // namespace
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,
77 int pitch) {
78 if (!ValidFormat(internalformat_, format)) {
79 LOG(ERROR) << "Invalid format: " << internalformat_;
80 return false;
83 if (!IsHandleValid(handle)) {
84 LOG(ERROR) << "Invalid file descriptor: " << handle.fd;
85 return false;
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,
91 size_.width(),
92 EGL_HEIGHT,
93 size_.height(),
94 EGL_LINUX_DRM_FOURCC_EXT,
95 FourCC(format),
96 EGL_DMA_BUF_PLANE0_FD_EXT,
97 handle.fd,
98 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
100 EGL_DMA_BUF_PLANE0_PITCH_EXT,
101 pitch,
102 EGL_NONE};
103 return GLImageEGL::Initialize(
104 EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(NULL), attrs);
107 } // namespace gfx