Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gl / gl_image_ozone_native_pixmap.cc
blob413c0abe460a3fc4f418c674907f0bda6aa958e8
1 // Copyright 2015 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_ozone_native_pixmap.h"
7 #define FOURCC(a, b, c, d) \
8 ((static_cast<uint32>(a)) | (static_cast<uint32>(b) << 8) | \
9 (static_cast<uint32>(c) << 16) | (static_cast<uint32>(d) << 24))
11 #define DRM_FORMAT_ARGB8888 FOURCC('A', 'R', '2', '4')
12 #define DRM_FORMAT_XRGB8888 FOURCC('X', 'R', '2', '4')
14 namespace gfx {
15 namespace {
17 bool ValidInternalFormat(unsigned internalformat) {
18 switch (internalformat) {
19 case GL_RGB:
20 case GL_BGRA_EXT:
21 return true;
22 default:
23 return false;
27 bool ValidFormat(gfx::BufferFormat format) {
28 switch (format) {
29 case BufferFormat::BGRA_8888:
30 case BufferFormat::BGRX_8888:
31 return true;
32 case BufferFormat::ATC:
33 case BufferFormat::ATCIA:
34 case BufferFormat::DXT1:
35 case BufferFormat::DXT5:
36 case BufferFormat::ETC1:
37 case BufferFormat::R_8:
38 case BufferFormat::RGBA_4444:
39 case BufferFormat::RGBA_8888:
40 case BufferFormat::YUV_420:
41 case BufferFormat::YUV_420_BIPLANAR:
42 case BufferFormat::UYVY_422:
43 return false;
46 NOTREACHED();
47 return false;
50 EGLint FourCC(gfx::BufferFormat format) {
51 switch (format) {
52 case BufferFormat::BGRA_8888:
53 return DRM_FORMAT_ARGB8888;
54 case BufferFormat::BGRX_8888:
55 return DRM_FORMAT_XRGB8888;
56 case BufferFormat::ATC:
57 case BufferFormat::ATCIA:
58 case BufferFormat::DXT1:
59 case BufferFormat::DXT5:
60 case BufferFormat::ETC1:
61 case BufferFormat::R_8:
62 case BufferFormat::RGBA_4444:
63 case BufferFormat::RGBA_8888:
64 case BufferFormat::YUV_420:
65 case BufferFormat::YUV_420_BIPLANAR:
66 case BufferFormat::UYVY_422:
67 NOTREACHED();
68 return 0;
71 NOTREACHED();
72 return 0;
75 } // namespace
77 GLImageOzoneNativePixmap::GLImageOzoneNativePixmap(const Size& size,
78 unsigned internalformat)
79 : GLImageEGL(size), internalformat_(internalformat) {}
81 GLImageOzoneNativePixmap::~GLImageOzoneNativePixmap() {
84 bool GLImageOzoneNativePixmap::Initialize(ui::NativePixmap* pixmap,
85 BufferFormat format) {
86 DCHECK(!pixmap_);
88 bool result = true;
89 if (pixmap->GetEGLClientBuffer()) {
90 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
91 result = GLImageEGL::Initialize(EGL_NATIVE_PIXMAP_KHR,
92 pixmap->GetEGLClientBuffer(), attrs);
93 } else if (pixmap->GetDmaBufFd() >= 0) {
94 if (!ValidInternalFormat(internalformat_)) {
95 LOG(ERROR) << "Invalid internalformat: " << internalformat_;
96 return false;
99 if (!ValidFormat(format)) {
100 LOG(ERROR) << "Invalid format: " << static_cast<int>(format);
101 return false;
104 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
105 // target, the EGL will take a reference to the dma_buf.
106 EGLint attrs[] = {EGL_WIDTH,
107 size_.width(),
108 EGL_HEIGHT,
109 size_.height(),
110 EGL_LINUX_DRM_FOURCC_EXT,
111 FourCC(format),
112 EGL_DMA_BUF_PLANE0_FD_EXT,
113 pixmap->GetDmaBufFd(),
114 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
116 EGL_DMA_BUF_PLANE0_PITCH_EXT,
117 pixmap->GetDmaBufPitch(),
118 EGL_NONE};
119 result = GLImageEGL::Initialize(
120 EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(nullptr), attrs);
123 if (result)
124 pixmap_ = pixmap;
125 return result;
128 unsigned GLImageOzoneNativePixmap::GetInternalFormat() {
129 return internalformat_;
132 void GLImageOzoneNativePixmap::Destroy(bool have_context) {
133 GLImageEGL::Destroy(have_context);
136 bool GLImageOzoneNativePixmap::ScheduleOverlayPlane(AcceleratedWidget widget,
137 int z_order,
138 OverlayTransform transform,
139 const Rect& bounds_rect,
140 const RectF& crop_rect) {
141 DCHECK(pixmap_);
142 return pixmap_->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect,
143 crop_rect);
146 void GLImageOzoneNativePixmap::OnMemoryDump(
147 base::trace_event::ProcessMemoryDump* pmd,
148 uint64_t process_tracing_id,
149 const std::string& dump_name) {
150 // TODO(ericrk): Implement GLImage OnMemoryDump. crbug.com/514914
153 } // namespace gfx