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), internalformat_(internalformat) {} | |
105 | |
106 GLImageOzoneNativePixmap::~GLImageOzoneNativePixmap() { | |
107 } | |
108 | |
109 bool GLImageOzoneNativePixmap::Initialize(ui::NativePixmap* pixmap, | |
110 gfx::BufferFormat format) { | |
111 DCHECK(!pixmap_); | |
112 if (pixmap->GetEGLClientBuffer()) { | |
113 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
114 if (!GLImageEGL::Initialize(EGL_NATIVE_PIXMAP_KHR, | |
115 pixmap->GetEGLClientBuffer(), attrs)) { | |
116 return false; | |
117 } | |
118 } else if (pixmap->AreDmaBufFdsValid()) { | |
119 | |
120 if (!ValidFormat(format)) { | |
121 LOG(ERROR) << "Invalid format: " << static_cast<int>(format); | |
122 return false; | |
123 } | |
124 | |
125 if (!ValidInternalFormat(internalformat_, format)) { | |
126 LOG(ERROR) << "Invalid internalformat: " << internalformat_ | |
127 << " for format: " << static_cast<int>(format); | |
128 return false; | |
129 } | |
130 | |
131 // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT | |
132 // target, the EGL will take a reference to the dma_buf. | |
133 std::vector<EGLint> attrs; | |
134 attrs.push_back(EGL_WIDTH); | |
135 attrs.push_back(size_.width()); | |
136 attrs.push_back(EGL_HEIGHT); | |
137 attrs.push_back(size_.height()); | |
138 attrs.push_back(EGL_LINUX_DRM_FOURCC_EXT); | |
139 attrs.push_back(FourCC(format)); | |
140 | |
141 const EGLint kLinuxDrmModifiers[] = {EGL_LINUX_DRM_PLANE0_MODIFIER0_EXT, | |
142 EGL_LINUX_DRM_PLANE1_MODIFIER0_EXT, | |
143 EGL_LINUX_DRM_PLANE2_MODIFIER0_EXT}; | |
144 bool has_dma_buf_import_modifier = | |
145 GLSurfaceEGL::HasEGLExtension("EGL_EXT_image_dma_buf_import_modifiers"); | |
146 | |
147 for (size_t plane = 0; | |
148 plane < gfx::NumberOfPlanesForBufferFormat(pixmap->GetBufferFormat()); | |
149 ++plane) { | |
150 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3); | |
151 attrs.push_back( | |
152 pixmap->GetDmaBufFd(plane < pixmap->GetDmaBufFdCount() ? plane : 0)); | |
153 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3); | |
154 attrs.push_back(pixmap->GetDmaBufOffset(plane)); | |
155 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3); | |
156 attrs.push_back(pixmap->GetDmaBufPitch(plane)); | |
157 if (has_dma_buf_import_modifier) { | |
158 uint64_t modifier = pixmap->GetDmaBufModifier(plane); | |
159 DCHECK(plane < arraysize(kLinuxDrmModifiers)); | |
160 attrs.push_back(kLinuxDrmModifiers[plane]); | |
161 attrs.push_back(modifier & 0xffffffff); | |
162 attrs.push_back(kLinuxDrmModifiers[plane] + 1); | |
163 attrs.push_back(static_cast<uint32_t>(modifier >> 32)); | |
164 } | |
165 } | |
166 attrs.push_back(EGL_NONE); | |
167 | |
168 if (!GLImageEGL::Initialize(EGL_LINUX_DMA_BUF_EXT, | |
169 static_cast<EGLClientBuffer>(nullptr), | |
170 &attrs[0])) { | |
171 return false; | |
172 } | |
173 } | |
174 | |
175 pixmap_ = pixmap; | |
176 return true; | |
177 } | |
178 | |
179 unsigned GLImageOzoneNativePixmap::GetInternalFormat() { | |
180 return internalformat_; | |
181 } | |
182 | |
183 void GLImageOzoneNativePixmap::Destroy(bool have_context) { | |
184 GLImageEGL::Destroy(have_context); | |
185 } | |
186 | |
187 bool GLImageOzoneNativePixmap::CopyTexImage(unsigned target) { | |
188 if (egl_image_ == EGL_NO_IMAGE_KHR) { | |
189 // Pass-through image type fails to bind and copy; make sure we | |
190 // don't draw with uninitialized texture. | |
191 std::vector<unsigned char> data(size_.width() * size_.height() * 4); | |
192 glTexImage2D(target, 0, GL_RGBA, size_.width(), | |
193 size_.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
194 data.data()); | |
195 return true; | |
196 } | |
197 return GLImageEGL::CopyTexImage(target); | |
198 } | |
199 | |
200 bool GLImageOzoneNativePixmap::ScheduleOverlayPlane( | |
201 gfx::AcceleratedWidget widget, | |
202 int z_order, | |
203 gfx::OverlayTransform transform, | |
204 const gfx::Rect& bounds_rect, | |
205 const gfx::RectF& crop_rect) { | |
206 DCHECK(pixmap_); | |
207 return pixmap_->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect, | |
208 crop_rect); | |
209 } | |
210 | |
211 void GLImageOzoneNativePixmap::OnMemoryDump( | |
212 base::trace_event::ProcessMemoryDump* pmd, | |
213 uint64_t process_tracing_id, | |
214 const std::string& dump_name) { | |
215 // TODO(ericrk): Implement GLImage OnMemoryDump. crbug.com/514914 | |
216 } | |
217 | |
218 // static | |
219 unsigned GLImageOzoneNativePixmap::GetInternalFormatForTesting( | |
220 gfx::BufferFormat format) { | |
221 DCHECK(ValidFormat(format)); | |
222 switch (format) { | |
223 case gfx::BufferFormat::R_8: | |
224 return GL_RED_EXT; | |
225 case gfx::BufferFormat::BGR_565: | |
226 case gfx::BufferFormat::RGBX_8888: | |
227 case gfx::BufferFormat::BGRX_8888: | |
228 return GL_RGB; | |
229 case gfx::BufferFormat::RGBA_8888: | |
230 return GL_RGBA; | |
231 case gfx::BufferFormat::BGRA_8888: | |
232 return GL_BGRA_EXT; | |
233 case gfx::BufferFormat::ATC: | |
234 case gfx::BufferFormat::ATCIA: | |
235 case gfx::BufferFormat::DXT1: | |
236 case gfx::BufferFormat::DXT5: | |
237 case gfx::BufferFormat::ETC1: | |
238 case gfx::BufferFormat::RGBA_4444: | |
239 case gfx::BufferFormat::YVU_420: | |
240 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
241 case gfx::BufferFormat::UYVY_422: | |
242 NOTREACHED(); | |
243 return GL_NONE; | |
244 } | |
245 | |
246 NOTREACHED(); | |
247 return GL_NONE; | |
248 } | |
249 | |
250 } // namespace gl | |
OLD | NEW |