Add ICU message format support
[chromium-blink-merge.git] / ui / gl / gl_image_ozone_native_pixmap.cc
blob65f3982f0363e891249eb468dc22bd01dca6244c
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::RGBX_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 return false;
44 NOTREACHED();
45 return false;
48 EGLint FourCC(gfx::BufferFormat format) {
49 switch (format) {
50 case BufferFormat::BGRA_8888:
51 return DRM_FORMAT_ARGB8888;
52 case BufferFormat::RGBX_8888:
53 return DRM_FORMAT_XRGB8888;
54 case BufferFormat::ATC:
55 case BufferFormat::ATCIA:
56 case BufferFormat::DXT1:
57 case BufferFormat::DXT5:
58 case BufferFormat::ETC1:
59 case BufferFormat::R_8:
60 case BufferFormat::RGBA_4444:
61 case BufferFormat::RGBA_8888:
62 case BufferFormat::YUV_420:
63 NOTREACHED();
64 return 0;
67 NOTREACHED();
68 return 0;
71 } // namespace
73 GLImageOzoneNativePixmap::GLImageOzoneNativePixmap(const Size& size,
74 unsigned internalformat)
75 : GLImageEGL(size), internalformat_(internalformat) {}
77 GLImageOzoneNativePixmap::~GLImageOzoneNativePixmap() {
80 bool GLImageOzoneNativePixmap::Initialize(ui::NativePixmap* pixmap,
81 BufferFormat format) {
82 DCHECK(!pixmap_);
84 bool result = true;
85 if (pixmap->GetEGLClientBuffer()) {
86 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
87 result = GLImageEGL::Initialize(EGL_NATIVE_PIXMAP_KHR,
88 pixmap->GetEGLClientBuffer(), attrs);
89 } else if (pixmap->GetDmaBufFd() >= 0) {
90 if (!ValidInternalFormat(internalformat_)) {
91 LOG(ERROR) << "Invalid internalformat: " << internalformat_;
92 return false;
95 if (!ValidFormat(format)) {
96 LOG(ERROR) << "Invalid format: " << static_cast<int>(format);
97 return false;
100 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
101 // target, the EGL will take a reference to the dma_buf.
102 EGLint attrs[] = {EGL_WIDTH,
103 size_.width(),
104 EGL_HEIGHT,
105 size_.height(),
106 EGL_LINUX_DRM_FOURCC_EXT,
107 FourCC(format),
108 EGL_DMA_BUF_PLANE0_FD_EXT,
109 pixmap->GetDmaBufFd(),
110 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
112 EGL_DMA_BUF_PLANE0_PITCH_EXT,
113 pixmap->GetDmaBufPitch(),
114 EGL_NONE};
115 result = GLImageEGL::Initialize(
116 EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(nullptr), attrs);
119 if (result)
120 pixmap_ = pixmap;
121 return result;
124 unsigned GLImageOzoneNativePixmap::GetInternalFormat() {
125 return internalformat_;
128 void GLImageOzoneNativePixmap::Destroy(bool have_context) {
129 GLImageEGL::Destroy(have_context);
132 bool GLImageOzoneNativePixmap::ScheduleOverlayPlane(AcceleratedWidget widget,
133 int z_order,
134 OverlayTransform transform,
135 const Rect& bounds_rect,
136 const RectF& crop_rect) {
137 DCHECK(pixmap_);
138 return pixmap_->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect,
139 crop_rect);
142 } // namespace gfx