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::BGRX_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
:
41 case BufferFormat::YUV_420_BIPLANAR
:
42 case BufferFormat::UYVY_422
:
50 EGLint
FourCC(gfx::BufferFormat 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
:
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
) {
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_
;
99 if (!ValidFormat(format
)) {
100 LOG(ERROR
) << "Invalid format: " << static_cast<int>(format
);
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
,
110 EGL_LINUX_DRM_FOURCC_EXT
,
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(),
119 result
= GLImageEGL::Initialize(
120 EGL_LINUX_DMA_BUF_EXT
, static_cast<EGLClientBuffer
>(nullptr), attrs
);
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
,
138 OverlayTransform transform
,
139 const Rect
& bounds_rect
,
140 const RectF
& crop_rect
) {
142 return pixmap_
->ScheduleOverlayPlane(widget
, z_order
, transform
, bounds_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