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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
9 #include "ui/gfx/buffer_format_util.h" | 9 #include "ui/gfx/buffer_format_util.h" |
10 #include "ui/gl/gl_bindings.h" | 10 #include "ui/gl/gl_bindings.h" |
11 #include "ui/gl/gl_context.h" | 11 #include "ui/gl/gl_context.h" |
12 #include "ui/gl/gl_version_info.h" | 12 #include "ui/gl/gl_version_info.h" |
13 | 13 |
14 namespace gfx { | 14 using gfx::BufferFormat; |
| 15 |
| 16 namespace gl { |
15 namespace { | 17 namespace { |
16 | 18 |
17 bool ValidInternalFormat(unsigned internalformat) { | 19 bool ValidInternalFormat(unsigned internalformat) { |
18 switch (internalformat) { | 20 switch (internalformat) { |
19 case GL_ATC_RGB_AMD: | 21 case GL_ATC_RGB_AMD: |
20 case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD: | 22 case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD: |
21 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: | 23 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
22 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: | 24 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: |
23 case GL_ETC1_RGB8_OES: | 25 case GL_ETC1_RGB8_OES: |
24 case GL_R8: | 26 case GL_R8: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 case BufferFormat::UYVY_422: | 159 case BufferFormat::UYVY_422: |
158 NOTREACHED(); | 160 NOTREACHED(); |
159 return 0; | 161 return 0; |
160 } | 162 } |
161 | 163 |
162 NOTREACHED(); | 164 NOTREACHED(); |
163 return 0; | 165 return 0; |
164 } | 166 } |
165 | 167 |
166 template <typename F> | 168 template <typename F> |
167 scoped_ptr<uint8_t[]> GLES2RGBData(const Size& size, | 169 scoped_ptr<uint8_t[]> GLES2RGBData(const gfx::Size& size, |
168 BufferFormat format, | 170 BufferFormat format, |
169 const uint8_t* data, | 171 const uint8_t* data, |
170 F const& data_to_rgb, | 172 F const& data_to_rgb, |
171 GLenum* data_format, | 173 GLenum* data_format, |
172 GLenum* data_type) { | 174 GLenum* data_type) { |
173 TRACE_EVENT2("gpu", "GLES2RGBData", "width", size.width(), "height", | 175 TRACE_EVENT2("gpu", "GLES2RGBData", "width", size.width(), "height", |
174 size.height()); | 176 size.height()); |
175 | 177 |
176 // Four-byte row alignment as specified by glPixelStorei with argument | 178 // Four-byte row alignment as specified by glPixelStorei with argument |
177 // GL_UNPACK_ALIGNMENT set to 4. | 179 // GL_UNPACK_ALIGNMENT set to 4. |
178 size_t gles2_rgb_data_stride = (size.width() * 3 + 3) & ~3; | 180 size_t gles2_rgb_data_stride = (size.width() * 3 + 3) & ~3; |
179 scoped_ptr<uint8_t[]> gles2_rgb_data( | 181 scoped_ptr<uint8_t[]> gles2_rgb_data( |
180 new uint8_t[gles2_rgb_data_stride * size.height()]); | 182 new uint8_t[gles2_rgb_data_stride * size.height()]); |
181 size_t data_stride = RowSizeForBufferFormat(size.width(), format, 0); | 183 size_t data_stride = RowSizeForBufferFormat(size.width(), format, 0); |
182 | 184 |
183 for (int y = 0; y < size.height(); ++y) { | 185 for (int y = 0; y < size.height(); ++y) { |
184 for (int x = 0; x < size.width(); ++x) { | 186 for (int x = 0; x < size.width(); ++x) { |
185 data_to_rgb(&data[y * data_stride + x * 4], | 187 data_to_rgb(&data[y * data_stride + x * 4], |
186 &gles2_rgb_data[y * gles2_rgb_data_stride + x * 3]); | 188 &gles2_rgb_data[y * gles2_rgb_data_stride + x * 3]); |
187 } | 189 } |
188 } | 190 } |
189 | 191 |
190 *data_format = GL_RGB; | 192 *data_format = GL_RGB; |
191 *data_type = GL_UNSIGNED_BYTE; | 193 *data_type = GL_UNSIGNED_BYTE; |
192 return gles2_rgb_data.Pass(); | 194 return gles2_rgb_data.Pass(); |
193 } | 195 } |
194 | 196 |
195 scoped_ptr<uint8_t[]> GLES2Data(const Size& size, | 197 scoped_ptr<uint8_t[]> GLES2Data(const gfx::Size& size, |
196 BufferFormat format, | 198 BufferFormat format, |
197 const uint8_t* data, | 199 const uint8_t* data, |
198 GLenum* data_format, | 200 GLenum* data_format, |
199 GLenum* data_type) { | 201 GLenum* data_type) { |
200 switch (format) { | 202 switch (format) { |
201 case BufferFormat::RGBX_8888: | 203 case BufferFormat::RGBX_8888: |
202 return GLES2RGBData(size, format, | 204 return GLES2RGBData(size, format, |
203 data, [](const uint8_t* src, uint8_t* dst) { | 205 data, [](const uint8_t* src, uint8_t* dst) { |
204 dst[0] = src[0]; | 206 dst[0] = src[0]; |
205 dst[1] = src[1]; | 207 dst[1] = src[1]; |
(...skipping 21 matching lines...) Expand all Loading... |
227 // No data conversion needed. | 229 // No data conversion needed. |
228 return nullptr; | 230 return nullptr; |
229 } | 231 } |
230 | 232 |
231 NOTREACHED(); | 233 NOTREACHED(); |
232 return 0; | 234 return 0; |
233 } | 235 } |
234 | 236 |
235 } // namespace | 237 } // namespace |
236 | 238 |
237 GLImageMemory::GLImageMemory(const Size& size, unsigned internalformat) | 239 GLImageMemory::GLImageMemory(const gfx::Size& size, unsigned internalformat) |
238 : size_(size), | 240 : size_(size), |
239 internalformat_(internalformat), | 241 internalformat_(internalformat), |
240 memory_(nullptr), | 242 memory_(nullptr), |
241 format_(BufferFormat::RGBA_8888) {} | 243 format_(BufferFormat::RGBA_8888) {} |
242 | 244 |
243 GLImageMemory::~GLImageMemory() { | 245 GLImageMemory::~GLImageMemory() { |
244 DCHECK(!memory_); | 246 DCHECK(!memory_); |
245 } | 247 } |
246 | 248 |
247 bool GLImageMemory::Initialize(const unsigned char* memory, | 249 bool GLImageMemory::Initialize(const unsigned char* memory, |
(...skipping 14 matching lines...) Expand all Loading... |
262 DCHECK_IMPLIES(IsCompressedFormat(format), size_.height() % 4 == 0); | 264 DCHECK_IMPLIES(IsCompressedFormat(format), size_.height() % 4 == 0); |
263 memory_ = memory; | 265 memory_ = memory; |
264 format_ = format; | 266 format_ = format; |
265 return true; | 267 return true; |
266 } | 268 } |
267 | 269 |
268 void GLImageMemory::Destroy(bool have_context) { | 270 void GLImageMemory::Destroy(bool have_context) { |
269 memory_ = nullptr; | 271 memory_ = nullptr; |
270 } | 272 } |
271 | 273 |
272 Size GLImageMemory::GetSize() { | 274 gfx::Size GLImageMemory::GetSize() { |
273 return size_; | 275 return size_; |
274 } | 276 } |
275 | 277 |
276 unsigned GLImageMemory::GetInternalFormat() { | 278 unsigned GLImageMemory::GetInternalFormat() { |
277 return internalformat_; | 279 return internalformat_; |
278 } | 280 } |
279 | 281 |
280 bool GLImageMemory::BindTexImage(unsigned target) { | 282 bool GLImageMemory::BindTexImage(unsigned target) { |
281 return false; | 283 return false; |
282 } | 284 } |
283 | 285 |
284 bool GLImageMemory::CopyTexImage(unsigned target) { | 286 bool GLImageMemory::CopyTexImage(unsigned target) { |
285 TRACE_EVENT2("gpu", "GLImageMemory::CopyTexImage", "width", size_.width(), | 287 TRACE_EVENT2("gpu", "GLImageMemory::CopyTexImage", "width", size_.width(), |
286 "height", size_.height()); | 288 "height", size_.height()); |
287 | 289 |
288 // GL_TEXTURE_EXTERNAL_OES is not a supported target. | 290 // GL_TEXTURE_EXTERNAL_OES is not a supported target. |
289 if (target == GL_TEXTURE_EXTERNAL_OES) | 291 if (target == GL_TEXTURE_EXTERNAL_OES) |
290 return false; | 292 return false; |
291 | 293 |
292 if (IsCompressedFormat(format_)) { | 294 if (IsCompressedFormat(format_)) { |
293 glCompressedTexImage2D( | 295 glCompressedTexImage2D( |
294 target, 0, TextureFormat(format_), size_.width(), size_.height(), 0, | 296 target, 0, TextureFormat(format_), size_.width(), size_.height(), 0, |
295 static_cast<GLsizei>(BufferSizeForBufferFormat(size_, format_)), | 297 static_cast<GLsizei>(BufferSizeForBufferFormat(size_, format_)), |
296 memory_); | 298 memory_); |
297 } else { | 299 } else { |
298 scoped_ptr<uint8_t[]> gles2_data; | 300 scoped_ptr<uint8_t[]> gles2_data; |
299 GLenum data_format = DataFormat(format_); | 301 GLenum data_format = DataFormat(format_); |
300 GLenum data_type = DataType(format_); | 302 GLenum data_type = DataType(format_); |
301 | 303 |
302 if (GLContext::GetCurrent()->GetVersionInfo()->is_es) | 304 if (gfx::GLContext::GetCurrent()->GetVersionInfo()->is_es) |
303 gles2_data = GLES2Data(size_, format_, memory_, &data_format, &data_type); | 305 gles2_data = GLES2Data(size_, format_, memory_, &data_format, &data_type); |
304 | 306 |
305 glTexImage2D(target, 0, TextureFormat(format_), size_.width(), | 307 glTexImage2D(target, 0, TextureFormat(format_), size_.width(), |
306 size_.height(), 0, data_format, data_type, | 308 size_.height(), 0, data_format, data_type, |
307 gles2_data ? gles2_data.get() : memory_); | 309 gles2_data ? gles2_data.get() : memory_); |
308 } | 310 } |
309 | 311 |
310 return true; | 312 return true; |
311 } | 313 } |
312 | 314 |
313 bool GLImageMemory::CopyTexSubImage(unsigned target, | 315 bool GLImageMemory::CopyTexSubImage(unsigned target, |
314 const Point& offset, | 316 const gfx::Point& offset, |
315 const Rect& rect) { | 317 const gfx::Rect& rect) { |
316 TRACE_EVENT2("gpu", "GLImageMemory::CopyTexSubImage", "width", rect.width(), | 318 TRACE_EVENT2("gpu", "GLImageMemory::CopyTexSubImage", "width", rect.width(), |
317 "height", rect.height()); | 319 "height", rect.height()); |
318 | 320 |
319 // GL_TEXTURE_EXTERNAL_OES is not a supported target. | 321 // GL_TEXTURE_EXTERNAL_OES is not a supported target. |
320 if (target == GL_TEXTURE_EXTERNAL_OES) | 322 if (target == GL_TEXTURE_EXTERNAL_OES) |
321 return false; | 323 return false; |
322 | 324 |
323 // Sub width is not supported. | 325 // Sub width is not supported. |
324 if (rect.width() != size_.width()) | 326 if (rect.width() != size_.width()) |
325 return false; | 327 return false; |
326 | 328 |
327 // Height must be a multiple of 4 if compressed. | 329 // Height must be a multiple of 4 if compressed. |
328 if (IsCompressedFormat(format_) && rect.height() % 4) | 330 if (IsCompressedFormat(format_) && rect.height() % 4) |
329 return false; | 331 return false; |
330 | 332 |
331 const uint8_t* data = | 333 const uint8_t* data = |
332 memory_ + rect.y() * RowSizeForBufferFormat(size_.width(), format_, 0); | 334 memory_ + rect.y() * RowSizeForBufferFormat(size_.width(), format_, 0); |
333 if (IsCompressedFormat(format_)) { | 335 if (IsCompressedFormat(format_)) { |
334 glCompressedTexSubImage2D( | 336 glCompressedTexSubImage2D( |
335 target, 0, offset.x(), offset.y(), rect.width(), rect.height(), | 337 target, 0, offset.x(), offset.y(), rect.width(), rect.height(), |
336 DataFormat(format_), | 338 DataFormat(format_), |
337 static_cast<GLsizei>(BufferSizeForBufferFormat(rect.size(), format_)), | 339 static_cast<GLsizei>(BufferSizeForBufferFormat(rect.size(), format_)), |
338 data); | 340 data); |
339 } else { | 341 } else { |
340 GLenum data_format = DataFormat(format_); | 342 GLenum data_format = DataFormat(format_); |
341 GLenum data_type = DataType(format_); | 343 GLenum data_type = DataType(format_); |
342 scoped_ptr<uint8_t[]> gles2_data; | 344 scoped_ptr<uint8_t[]> gles2_data; |
343 | 345 |
344 if (GLContext::GetCurrent()->GetVersionInfo()->is_es) { | 346 if (gfx::GLContext::GetCurrent()->GetVersionInfo()->is_es) { |
345 gles2_data = | 347 gles2_data = |
346 GLES2Data(rect.size(), format_, data, &data_format, &data_type); | 348 GLES2Data(rect.size(), format_, data, &data_format, &data_type); |
347 } | 349 } |
348 | 350 |
349 glTexSubImage2D(target, 0, offset.x(), offset.y(), rect.width(), | 351 glTexSubImage2D(target, 0, offset.x(), offset.y(), rect.width(), |
350 rect.height(), data_format, data_type, | 352 rect.height(), data_format, data_type, |
351 gles2_data ? gles2_data.get() : data); | 353 gles2_data ? gles2_data.get() : data); |
352 } | 354 } |
353 | 355 |
354 return true; | 356 return true; |
355 } | 357 } |
356 | 358 |
357 bool GLImageMemory::ScheduleOverlayPlane(AcceleratedWidget widget, | 359 bool GLImageMemory::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
358 int z_order, | 360 int z_order, |
359 OverlayTransform transform, | 361 gfx::OverlayTransform transform, |
360 const Rect& bounds_rect, | 362 const gfx::Rect& bounds_rect, |
361 const RectF& crop_rect) { | 363 const gfx::RectF& crop_rect) { |
362 return false; | 364 return false; |
363 } | 365 } |
364 | 366 |
365 // static | 367 // static |
366 unsigned GLImageMemory::GetInternalFormatForTesting(BufferFormat format) { | 368 unsigned GLImageMemory::GetInternalFormatForTesting(BufferFormat format) { |
367 DCHECK(ValidFormat(format)); | 369 DCHECK(ValidFormat(format)); |
368 return TextureFormat(format); | 370 return TextureFormat(format); |
369 } | 371 } |
370 | 372 |
371 } // namespace gfx | 373 } // namespace gl |
OLD | NEW |