OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/gfx/buffer_format_util.h" | |
6 #include "ui/gl/gl_image_ozone_native_pixmap.h" | |
7 #include "ui/gl/gl_surface_egl.h" | |
8 | |
9 #define FOURCC(a, b, c, d) \ | |
10 ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \ | |
11 (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24)) | |
12 | |
13 #define DRM_FORMAT_R8 FOURCC('R', '8', ' ', ' ') | |
14 #define DRM_FORMAT_RGB565 FOURCC('R', 'G', '1', '6') | |
15 #define DRM_FORMAT_ARGB8888 FOURCC('A', 'R', '2', '4') | |
16 #define DRM_FORMAT_ABGR8888 FOURCC('A', 'B', '2', '4') | |
17 #define DRM_FORMAT_XRGB8888 FOURCC('X', 'R', '2', '4') | |
18 #define DRM_FORMAT_XBGR8888 FOURCC('X', 'B', '2', '4') | |
19 #define DRM_FORMAT_YV12 FOURCC('Y', 'V', '1', '2') | |
20 | |
21 namespace gl { | |
22 namespace { | |
23 | |
24 bool ValidInternalFormat(unsigned internalformat, gfx::BufferFormat format) { | |
25 switch (internalformat) { | |
26 case GL_RGB: | |
27 return format == gfx::BufferFormat::BGR_565 || | |
28 format == gfx::BufferFormat::RGBX_8888 || | |
29 format == gfx::BufferFormat::BGRX_8888; | |
30 case GL_RGB_YCRCB_420_CHROMIUM: | |
31 return format == gfx::BufferFormat::YVU_420; | |
32 case GL_RGBA: | |
33 return format == gfx::BufferFormat::RGBA_8888; | |
34 case GL_BGRA_EXT: | |
35 return format == gfx::BufferFormat::BGRA_8888; | |
36 case GL_RED_EXT: | |
37 return format == gfx::BufferFormat::R_8; | |
38 default: | |
39 return false; | |
40 } | |
41 } | |
42 | |
43 bool ValidFormat(gfx::BufferFormat format) { | |
44 switch (format) { | |
45 case gfx::BufferFormat::R_8: | |
46 case gfx::BufferFormat::BGR_565: | |
47 case gfx::BufferFormat::RGBA_8888: | |
48 case gfx::BufferFormat::RGBX_8888: | |
49 case gfx::BufferFormat::BGRA_8888: | |
50 case gfx::BufferFormat::BGRX_8888: | |
51 case gfx::BufferFormat::YVU_420: | |
52 return true; | |
53 case gfx::BufferFormat::ATC: | |
54 case gfx::BufferFormat::ATCIA: | |
55 case gfx::BufferFormat::DXT1: | |
56 case gfx::BufferFormat::DXT5: | |
57 case gfx::BufferFormat::ETC1: | |
58 case gfx::BufferFormat::RGBA_4444: | |
59 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
60 case gfx::BufferFormat::UYVY_422: | |
61 return false; | |
62 } | |
63 | |
64 NOTREACHED(); | |
65 return false; | |
66 } | |
67 | |
68 EGLint FourCC(gfx::BufferFormat format) { | |
69 switch (format) { | |
70 case gfx::BufferFormat::R_8: | |
71 return DRM_FORMAT_R8; | |
72 case gfx::BufferFormat::BGR_565: | |
73 return DRM_FORMAT_RGB565; | |
74 case gfx::BufferFormat::RGBA_8888: | |
75 return DRM_FORMAT_ABGR8888; | |
76 case gfx::BufferFormat::RGBX_8888: | |
77 return DRM_FORMAT_XBGR8888; | |
78 case gfx::BufferFormat::BGRA_8888: | |
79 return DRM_FORMAT_ARGB8888; | |
80 case gfx::BufferFormat::BGRX_8888: | |
81 return DRM_FORMAT_XRGB8888; | |
82 case gfx::BufferFormat::YVU_420: | |
83 return DRM_FORMAT_YV12; | |
84 case gfx::BufferFormat::ATC: | |
85 case gfx::BufferFormat::ATCIA: | |
86 case gfx::BufferFormat::DXT1: | |
87 case gfx::BufferFormat::DXT5: | |
88 case gfx::BufferFormat::ETC1: | |
89 case gfx::BufferFormat::RGBA_4444: | |
90 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
91 case gfx::BufferFormat::UYVY_422: | |
92 NOTREACHED(); | |
93 return 0; | |
94 } | |
95 | |
96 NOTREACHED(); | |
97 return 0; | |
98 } | |
99 | |
100 } // namespace | |
101 | |
102 GLImageOzoneNativePixmap::GLImageOzoneNativePixmap(const gfx::Size& size, | |
103 unsigned internalformat) | |
104 : GLImageEGL(size), | |
105 internalformat_(internalformat), | |
106 has_image_flush_external_( | |
107 GLSurfaceEGL::HasEGLExtension("EGL_EXT_image_flush_external")) {} | |
108 | |
109 GLImageOzoneNativePixmap::~GLImageOzoneNativePixmap() { | |
110 } | |
111 | |
112 bool GLImageOzoneNativePixmap::Initialize(ui::NativePixmap* pixmap, | |
113 gfx::BufferFormat format) { | |
114 DCHECK(!pixmap_); | |
115 if (pixmap->GetEGLClientBuffer()) { | |
116 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
117 if (!GLImageEGL::Initialize(EGL_NATIVE_PIXMAP_KHR, | |
118 pixmap->GetEGLClientBuffer(), attrs)) { | |
119 return false; | |
120 } | |
121 } else if (pixmap->AreDmaBufFdsValid()) { | |
122 | |
123 if (!ValidFormat(format)) { | |
124 LOG(ERROR) << "Invalid format: " << static_cast<int>(format); | |
125 return false; | |
126 } | |
127 | |
128 if (!ValidInternalFormat(internalformat_, format)) { | |
129 LOG(ERROR) << "Invalid internalformat: " << internalformat_ | |
130 << " for format: " << static_cast<int>(format); | |
131 return false; | |
132 } | |
133 | |
134 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT | |
135 // target, the EGL will take a reference to the dma_buf. | |
136 std::vector<EGLint> attrs; | |
137 attrs.push_back(EGL_WIDTH); | |
138 attrs.push_back(size_.width()); | |
139 attrs.push_back(EGL_HEIGHT); | |
140 attrs.push_back(size_.height()); | |
141 attrs.push_back(EGL_LINUX_DRM_FOURCC_EXT); | |
142 attrs.push_back(FourCC(format)); | |
143 | |
144 const EGLint kLinuxDrmModifiers[] = {EGL_LINUX_DRM_PLANE0_MODIFIER0_EXT, | |
145 EGL_LINUX_DRM_PLANE1_MODIFIER0_EXT, | |
146 EGL_LINUX_DRM_PLANE2_MODIFIER0_EXT}; | |
147 bool has_dma_buf_import_modifier = | |
148 GLSurfaceEGL::HasEGLExtension("EGL_EXT_image_dma_buf_import_modifiers"); | |
149 | |
150 for (size_t plane = 0; | |
151 plane < gfx::NumberOfPlanesForBufferFormat(pixmap->GetBufferFormat()); | |
152 ++plane) { | |
153 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3); | |
154 attrs.push_back( | |
155 pixmap->GetDmaBufFd(plane < pixmap->GetDmaBufFdCount() ? plane : 0)); | |
156 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3); | |
157 attrs.push_back(pixmap->GetDmaBufOffset(plane)); | |
158 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3); | |
159 attrs.push_back(pixmap->GetDmaBufPitch(plane)); | |
160 if (has_dma_buf_import_modifier) { | |
161 uint64_t modifier = pixmap->GetDmaBufModifier(plane); | |
162 DCHECK(plane < arraysize(kLinuxDrmModifiers)); | |
163 attrs.push_back(kLinuxDrmModifiers[plane]); | |
164 attrs.push_back(modifier & 0xffffffff); | |
165 attrs.push_back(kLinuxDrmModifiers[plane] + 1); | |
166 attrs.push_back(static_cast<uint32_t>(modifier >> 32)); | |
167 } | |
168 } | |
169 attrs.push_back(EGL_NONE); | |
170 | |
171 if (!GLImageEGL::Initialize(EGL_LINUX_DMA_BUF_EXT, | |
172 static_cast<EGLClientBuffer>(nullptr), | |
173 &attrs[0])) { | |
174 return false; | |
175 } | |
176 } | |
177 | |
178 pixmap_ = pixmap; | |
179 return true; | |
180 } | |
181 | |
182 unsigned GLImageOzoneNativePixmap::GetInternalFormat() { | |
183 return internalformat_; | |
184 } | |
185 | |
186 void GLImageOzoneNativePixmap::Destroy(bool have_context) { | |
187 GLImageEGL::Destroy(have_context); | |
188 } | |
189 | |
190 bool GLImageOzoneNativePixmap::CopyTexImage(unsigned target) { | |
191 if (egl_image_ == EGL_NO_IMAGE_KHR) { | |
192 // Pass-through image type fails to bind and copy; make sure we | |
193 // don't draw with uninitialized texture. | |
194 std::vector<unsigned char> data(size_.width() * size_.height() * 4); | |
195 glTexImage2D(target, 0, GL_RGBA, size_.width(), | |
196 size_.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
197 data.data()); | |
198 return true; | |
199 } | |
200 return GLImageEGL::CopyTexImage(target); | |
201 } | |
202 | |
203 bool GLImageOzoneNativePixmap::ScheduleOverlayPlane( | |
204 gfx::AcceleratedWidget widget, | |
205 int z_order, | |
206 gfx::OverlayTransform transform, | |
207 const gfx::Rect& bounds_rect, | |
208 const gfx::RectF& crop_rect) { | |
209 DCHECK(pixmap_); | |
210 return pixmap_->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect, | |
211 crop_rect); | |
212 } | |
213 | |
214 void GLImageOzoneNativePixmap::Flush() { | |
215 if (!has_image_flush_external_) | |
216 return; | |
217 | |
218 EGLDisplay display = GLSurfaceEGL::GetHardwareDisplay(); | |
219 const EGLAttrib attribs[] = { | |
220 EGL_NONE, | |
221 }; | |
222 if (!eglImageFlushExternalEXT(display, egl_image_, attribs)) { | |
223 LOG(ERROR) << "Failed to flush rendering"; | |
224 return; | |
225 } | |
226 } | |
227 | |
228 void GLImageOzoneNativePixmap::OnMemoryDump( | |
229 base::trace_event::ProcessMemoryDump* pmd, | |
230 uint64_t process_tracing_id, | |
231 const std::string& dump_name) { | |
232 // TODO(ericrk): Implement GLImage OnMemoryDump. crbug.com/514914 | |
233 } | |
234 | |
235 // static | |
236 unsigned GLImageOzoneNativePixmap::GetInternalFormatForTesting( | |
237 gfx::BufferFormat format) { | |
238 DCHECK(ValidFormat(format)); | |
239 switch (format) { | |
240 case gfx::BufferFormat::R_8: | |
241 return GL_RED_EXT; | |
242 case gfx::BufferFormat::BGR_565: | |
243 case gfx::BufferFormat::RGBX_8888: | |
244 case gfx::BufferFormat::BGRX_8888: | |
245 return GL_RGB; | |
246 case gfx::BufferFormat::RGBA_8888: | |
247 return GL_RGBA; | |
248 case gfx::BufferFormat::BGRA_8888: | |
249 return GL_BGRA_EXT; | |
250 case gfx::BufferFormat::ATC: | |
251 case gfx::BufferFormat::ATCIA: | |
252 case gfx::BufferFormat::DXT1: | |
253 case gfx::BufferFormat::DXT5: | |
254 case gfx::BufferFormat::ETC1: | |
255 case gfx::BufferFormat::RGBA_4444: | |
256 case gfx::BufferFormat::YVU_420: | |
257 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
258 case gfx::BufferFormat::UYVY_422: | |
259 NOTREACHED(); | |
260 return GL_NONE; | |
261 } | |
262 | |
263 NOTREACHED(); | |
264 return GL_NONE; | |
265 } | |
266 | |
267 } // namespace gl | |
OLD | NEW |