OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_image_memory.h" | 5 #include "ui/gl/gl_image_memory.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
10 #include "ui/gl/scoped_binders.h" | 10 #include "ui/gl/scoped_binders.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 return true; | 23 return true; |
24 default: | 24 default: |
25 return false; | 25 return false; |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 bool ValidFormat(gfx::GpuMemoryBuffer::Format format) { | 29 bool ValidFormat(gfx::GpuMemoryBuffer::Format format) { |
30 switch (format) { | 30 switch (format) { |
31 case gfx::GpuMemoryBuffer::RGBA_8888: | 31 case gfx::GpuMemoryBuffer::RGBA_8888: |
32 case gfx::GpuMemoryBuffer::BGRA_8888: | 32 case gfx::GpuMemoryBuffer::BGRA_8888: |
| 33 case gfx::GpuMemoryBuffer::ATC: |
| 34 case gfx::GpuMemoryBuffer::ATCIA: |
| 35 case gfx::GpuMemoryBuffer::DXT1: |
| 36 case gfx::GpuMemoryBuffer::DXT5: |
| 37 case gfx::GpuMemoryBuffer::ETC1: |
33 return true; | 38 return true; |
34 case gfx::GpuMemoryBuffer::RGBX_8888: | 39 case gfx::GpuMemoryBuffer::RGBX_8888: |
35 return false; | 40 return false; |
36 } | 41 } |
37 | 42 |
38 NOTREACHED(); | 43 NOTREACHED(); |
39 return false; | 44 return false; |
40 } | 45 } |
41 | 46 |
| 47 bool CompressedFormat(gfx::GpuMemoryBuffer::Format format) { |
| 48 switch (format) { |
| 49 case gfx::GpuMemoryBuffer::ATC: |
| 50 case gfx::GpuMemoryBuffer::ATCIA: |
| 51 case gfx::GpuMemoryBuffer::DXT1: |
| 52 case gfx::GpuMemoryBuffer::DXT5: |
| 53 case gfx::GpuMemoryBuffer::ETC1: |
| 54 return true; |
| 55 case gfx::GpuMemoryBuffer::RGBA_8888: |
| 56 case gfx::GpuMemoryBuffer::BGRA_8888: |
| 57 case gfx::GpuMemoryBuffer::RGBX_8888: |
| 58 return false; |
| 59 } |
| 60 |
| 61 NOTREACHED(); |
| 62 return false; |
| 63 } |
| 64 |
42 GLenum TextureFormat(gfx::GpuMemoryBuffer::Format format) { | 65 GLenum TextureFormat(gfx::GpuMemoryBuffer::Format format) { |
43 switch (format) { | 66 switch (format) { |
44 case gfx::GpuMemoryBuffer::RGBA_8888: | 67 case gfx::GpuMemoryBuffer::RGBA_8888: |
45 return GL_RGBA; | 68 return GL_RGBA; |
46 case gfx::GpuMemoryBuffer::BGRA_8888: | 69 case gfx::GpuMemoryBuffer::BGRA_8888: |
47 return GL_BGRA_EXT; | 70 return GL_BGRA_EXT; |
48 case gfx::GpuMemoryBuffer::RGBX_8888: | 71 case gfx::GpuMemoryBuffer::RGBX_8888: |
49 NOTREACHED(); | 72 NOTREACHED(); |
50 return 0; | 73 return 0; |
| 74 case gfx::GpuMemoryBuffer::ATC: |
| 75 return GL_ATC_RGB_AMD; |
| 76 case gfx::GpuMemoryBuffer::ATCIA: |
| 77 return GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD; |
| 78 case gfx::GpuMemoryBuffer::DXT1: |
| 79 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
| 80 case gfx::GpuMemoryBuffer::DXT5: |
| 81 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 82 case gfx::GpuMemoryBuffer::ETC1: |
| 83 return GL_ETC1_RGB8_OES; |
51 } | 84 } |
52 | 85 |
53 NOTREACHED(); | 86 NOTREACHED(); |
54 return 0; | 87 return 0; |
55 } | 88 } |
56 | 89 |
57 GLenum DataFormat(gfx::GpuMemoryBuffer::Format format) { | 90 GLenum DataFormat(gfx::GpuMemoryBuffer::Format format) { |
58 return TextureFormat(format); | 91 return TextureFormat(format); |
59 } | 92 } |
60 | 93 |
61 GLenum DataType(gfx::GpuMemoryBuffer::Format format) { | 94 GLenum DataType(gfx::GpuMemoryBuffer::Format format) { |
62 switch (format) { | 95 switch (format) { |
63 case gfx::GpuMemoryBuffer::RGBA_8888: | 96 case gfx::GpuMemoryBuffer::RGBA_8888: |
64 case gfx::GpuMemoryBuffer::BGRA_8888: | 97 case gfx::GpuMemoryBuffer::BGRA_8888: |
65 return GL_UNSIGNED_BYTE; | 98 return GL_UNSIGNED_BYTE; |
66 case gfx::GpuMemoryBuffer::RGBX_8888: | 99 case gfx::GpuMemoryBuffer::RGBX_8888: |
| 100 case gfx::GpuMemoryBuffer::ATC: |
| 101 case gfx::GpuMemoryBuffer::ATCIA: |
| 102 case gfx::GpuMemoryBuffer::DXT1: |
| 103 case gfx::GpuMemoryBuffer::DXT5: |
| 104 case gfx::GpuMemoryBuffer::ETC1: |
67 NOTREACHED(); | 105 NOTREACHED(); |
68 return 0; | 106 return 0; |
69 } | 107 } |
70 | 108 |
71 NOTREACHED(); | 109 NOTREACHED(); |
72 return 0; | 110 return 0; |
73 } | 111 } |
74 | 112 |
75 } // namespace | 113 } // namespace |
76 | 114 |
(...skipping 16 matching lines...) Expand all Loading... |
93 | 131 |
94 GLImageMemory::~GLImageMemory() { | 132 GLImageMemory::~GLImageMemory() { |
95 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 133 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
96 defined(USE_OZONE) | 134 defined(USE_OZONE) |
97 DCHECK_EQ(EGL_NO_IMAGE_KHR, egl_image_); | 135 DCHECK_EQ(EGL_NO_IMAGE_KHR, egl_image_); |
98 DCHECK_EQ(0u, egl_texture_id_); | 136 DCHECK_EQ(0u, egl_texture_id_); |
99 #endif | 137 #endif |
100 } | 138 } |
101 | 139 |
102 // static | 140 // static |
103 size_t GLImageMemory::BytesPerPixel(gfx::GpuMemoryBuffer::Format format) { | 141 size_t GLImageMemory::BitsPerPixel(gfx::GpuMemoryBuffer::Format format) { |
104 switch (format) { | 142 switch (format) { |
105 case gfx::GpuMemoryBuffer::RGBA_8888: | 143 case gfx::GpuMemoryBuffer::RGBA_8888: |
106 case gfx::GpuMemoryBuffer::BGRA_8888: | 144 case gfx::GpuMemoryBuffer::BGRA_8888: |
107 return 4; | 145 return 32; |
108 case gfx::GpuMemoryBuffer::RGBX_8888: | 146 case gfx::GpuMemoryBuffer::RGBX_8888: |
109 NOTREACHED(); | 147 NOTREACHED(); |
110 return 0; | 148 return 0; |
| 149 case gfx::GpuMemoryBuffer::ATCIA: |
| 150 case gfx::GpuMemoryBuffer::DXT5: |
| 151 return 8; |
| 152 case gfx::GpuMemoryBuffer::ATC: |
| 153 case gfx::GpuMemoryBuffer::DXT1: |
| 154 case gfx::GpuMemoryBuffer::ETC1: |
| 155 return 4; |
111 } | 156 } |
112 | 157 |
113 NOTREACHED(); | 158 NOTREACHED(); |
114 return 0; | 159 return 0; |
115 } | 160 } |
116 | 161 |
117 bool GLImageMemory::Initialize(const unsigned char* memory, | 162 bool GLImageMemory::Initialize(const unsigned char* memory, |
118 gfx::GpuMemoryBuffer::Format format) { | 163 gfx::GpuMemoryBuffer::Format format) { |
119 if (!ValidInternalFormat(internalformat_)) { | 164 if (!ValidInternalFormat(internalformat_)) { |
120 LOG(ERROR) << "Invalid internalformat: " << internalformat_; | 165 LOG(ERROR) << "Invalid internalformat: " << internalformat_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 217 } |
173 | 218 |
174 bool GLImageMemory::CopyTexImage(unsigned target) { | 219 bool GLImageMemory::CopyTexImage(unsigned target) { |
175 TRACE_EVENT0("gpu", "GLImageMemory::CopyTexImage"); | 220 TRACE_EVENT0("gpu", "GLImageMemory::CopyTexImage"); |
176 | 221 |
177 // GL_TEXTURE_EXTERNAL_OES is not a supported CopyTexImage target. | 222 // GL_TEXTURE_EXTERNAL_OES is not a supported CopyTexImage target. |
178 if (target == GL_TEXTURE_EXTERNAL_OES) | 223 if (target == GL_TEXTURE_EXTERNAL_OES) |
179 return false; | 224 return false; |
180 | 225 |
181 DCHECK(memory_); | 226 DCHECK(memory_); |
182 glTexSubImage2D(target, 0, // level | 227 |
183 0, // x | 228 if (CompressedFormat(format_)) { |
184 0, // y | 229 glCompressedTexSubImage2D(target, |
185 size_.width(), size_.height(), DataFormat(format_), | 230 0, // mip level |
186 DataType(format_), memory_); | 231 0, // x-offset |
| 232 0, // y-offset |
| 233 size_.width(), size_.height(), |
| 234 DataFormat(format_), MemoryBytes(format_), |
| 235 memory_); |
| 236 } else { |
| 237 glTexSubImage2D(target, 0, // level |
| 238 0, // x |
| 239 0, // y |
| 240 size_.width(), size_.height(), DataFormat(format_), |
| 241 DataType(format_), memory_); |
| 242 } |
187 | 243 |
188 return true; | 244 return true; |
189 } | 245 } |
190 | 246 |
191 void GLImageMemory::WillUseTexImage() { | 247 void GLImageMemory::WillUseTexImage() { |
192 DCHECK(!in_use_); | 248 DCHECK(!in_use_); |
193 in_use_ = true; | 249 in_use_ = true; |
194 | 250 |
195 if (!need_do_bind_tex_image_) | 251 if (!need_do_bind_tex_image_) |
196 return; | 252 return; |
(...skipping 28 matching lines...) Expand all Loading... |
225 if (egl_image_ == EGL_NO_IMAGE_KHR) { | 281 if (egl_image_ == EGL_NO_IMAGE_KHR) { |
226 DCHECK_EQ(0u, egl_texture_id_); | 282 DCHECK_EQ(0u, egl_texture_id_); |
227 glGenTextures(1, &egl_texture_id_); | 283 glGenTextures(1, &egl_texture_id_); |
228 | 284 |
229 { | 285 { |
230 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); | 286 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); |
231 | 287 |
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
235 glTexImage2D(GL_TEXTURE_2D, | 291 |
236 0, // mip level | 292 if (CompressedFormat(format_)) { |
237 TextureFormat(format_), | 293 glCompressedTexImage2D(GL_TEXTURE_2D, |
238 size_.width(), | 294 0, // mip level |
239 size_.height(), | 295 TextureFormat(format_), size_.width(), |
240 0, // border | 296 size_.height(), |
241 DataFormat(format_), | 297 0, // border |
242 DataType(format_), | 298 MemoryBytes(format_), memory_); |
243 memory_); | 299 } else { |
| 300 glTexImage2D(GL_TEXTURE_2D, |
| 301 0, // mip level |
| 302 TextureFormat(format_), size_.width(), size_.height(), |
| 303 0, // border |
| 304 DataFormat(format_), DataType(format_), memory_); |
| 305 } |
244 } | 306 } |
245 | 307 |
246 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | 308 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; |
247 // Need to pass current EGL rendering context to eglCreateImageKHR for | 309 // Need to pass current EGL rendering context to eglCreateImageKHR for |
248 // target type EGL_GL_TEXTURE_2D_KHR. | 310 // target type EGL_GL_TEXTURE_2D_KHR. |
249 egl_image_ = | 311 egl_image_ = |
250 eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(), | 312 eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(), |
251 eglGetCurrentContext(), | 313 eglGetCurrentContext(), |
252 EGL_GL_TEXTURE_2D_KHR, | 314 EGL_GL_TEXTURE_2D_KHR, |
253 reinterpret_cast<EGLClientBuffer>(egl_texture_id_), | 315 reinterpret_cast<EGLClientBuffer>(egl_texture_id_), |
254 attrs); | 316 attrs); |
255 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_) | 317 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_) |
256 << "Error creating EGLImage: " << eglGetError(); | 318 << "Error creating EGLImage: " << eglGetError(); |
257 } else { | 319 } else { |
258 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); | 320 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); |
259 | 321 |
260 glTexSubImage2D(GL_TEXTURE_2D, | 322 if (CompressedFormat(format_)) { |
261 0, // mip level | 323 glCompressedTexSubImage2D(GL_TEXTURE_2D, |
262 0, // x-offset | 324 0, // mip level |
263 0, // y-offset | 325 0, // x-offset |
264 size_.width(), | 326 0, // y-offset |
265 size_.height(), | 327 size_.width(), size_.height(), |
266 DataFormat(format_), | 328 DataFormat(format_), MemoryBytes(format_), |
267 DataType(format_), | 329 memory_); |
268 memory_); | 330 } else { |
| 331 glTexSubImage2D(GL_TEXTURE_2D, |
| 332 0, // mip level |
| 333 0, // x-offset |
| 334 0, // y-offset |
| 335 size_.width(), size_.height(), DataFormat(format_), |
| 336 DataType(format_), memory_); |
| 337 } |
269 } | 338 } |
270 | 339 |
271 glEGLImageTargetTexture2DOES(target, egl_image_); | 340 glEGLImageTargetTexture2DOES(target, egl_image_); |
272 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | 341 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); |
273 return; | 342 return; |
274 } | 343 } |
275 #endif | 344 #endif |
276 | 345 |
277 DCHECK_NE(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), target); | 346 DCHECK_NE(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), target); |
278 glTexImage2D(target, | 347 |
279 0, // mip level | 348 if (CompressedFormat(format_)) { |
280 TextureFormat(format_), | 349 glCompressedTexImage2D(target, |
281 size_.width(), | 350 0, // mip level |
282 size_.height(), | 351 TextureFormat(format_), size_.width(), |
283 0, // border | 352 size_.height(), |
284 DataFormat(format_), | 353 0, // border |
285 DataType(format_), | 354 MemoryBytes(format_), memory_); |
286 memory_); | 355 } else { |
| 356 glTexImage2D(target, |
| 357 0, // mip level |
| 358 TextureFormat(format_), size_.width(), size_.height(), |
| 359 0, // border |
| 360 DataFormat(format_), DataType(format_), memory_); |
| 361 } |
| 362 } |
| 363 |
| 364 size_t GLImageMemory::MemoryBytes(gfx::GpuMemoryBuffer::Format format) { |
| 365 return BitsPerPixel(format_) * size_.width() * size_.height() / 8; |
287 } | 366 } |
288 | 367 |
289 } // namespace gfx | 368 } // namespace gfx |
OLD | NEW |