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')
17 bool ValidInternalFormat(unsigned internalformat
) {
18 switch (internalformat
) {
27 bool ValidFormat(gfx::BufferFormat format
) {
29 case BufferFormat::BGRA_8888
:
30 case BufferFormat::RGBX_8888
:
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
:
48 EGLint
FourCC(gfx::BufferFormat 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
:
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
) {
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_
;
95 if (!ValidFormat(format
)) {
96 LOG(ERROR
) << "Invalid format: " << static_cast<int>(format
);
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
,
106 EGL_LINUX_DRM_FOURCC_EXT
,
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(),
115 result
= GLImageEGL::Initialize(
116 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(nullptr), attrs
);
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
,
134 OverlayTransform transform
,
135 const Rect
& bounds_rect
,
136 const RectF
& crop_rect
) {
138 return pixmap_
->ScheduleOverlayPlane(widget
, z_order
, transform
, bounds_rect
,