OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/gl/gl_image_linux_dma_buffer.h" |
| 6 |
| 7 #include <unistd.h> |
| 8 |
| 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)) |
| 12 |
| 13 #define DRM_FORMAT_ARGB8888 FOURCC('A', 'R', '2', '4') |
| 14 #define DRM_FORMAT_XRGB8888 FOURCC('X', 'R', '2', '4') |
| 15 |
| 16 namespace gfx { |
| 17 namespace { |
| 18 |
| 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; |
| 28 } |
| 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; |
| 38 } |
| 39 NOTREACHED(); |
| 40 return false; |
| 41 default: |
| 42 return false; |
| 43 } |
| 44 } |
| 45 |
| 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; |
| 55 } |
| 56 |
| 57 NOTREACHED(); |
| 58 return 0; |
| 59 } |
| 60 |
| 61 bool IsHandleValid(const base::FileDescriptor& handle) { |
| 62 return handle.fd >= 0; |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 GLImageLinuxDMABuffer::GLImageLinuxDMABuffer(const gfx::Size& size, |
| 68 unsigned internalformat) |
| 69 : GLImageEGL(size), internalformat_(internalformat) { |
| 70 } |
| 71 |
| 72 GLImageLinuxDMABuffer::~GLImageLinuxDMABuffer() { |
| 73 } |
| 74 |
| 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; |
| 81 } |
| 82 |
| 83 if (!IsHandleValid(handle)) { |
| 84 LOG(ERROR) << "Invalid file descriptor: " << handle.fd; |
| 85 return false; |
| 86 } |
| 87 |
| 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, |
| 99 0, |
| 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); |
| 105 } |
| 106 |
| 107 } // namespace gfx |
OLD | NEW |