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