OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_io_surface.h" | 5 #include "ui/gl/gl_image_io_surface.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 bool GLImageIOSurface::CopyTexImage(unsigned target) { | 294 bool GLImageIOSurface::CopyTexImage(unsigned target) { |
295 DCHECK(thread_checker_.CalledOnValidThread()); | 295 DCHECK(thread_checker_.CalledOnValidThread()); |
296 | 296 |
297 if (format_ != BufferFormat::YUV_420_BIPLANAR) | 297 if (format_ != BufferFormat::YUV_420_BIPLANAR) |
298 return false; | 298 return false; |
299 if (target != GL_TEXTURE_RECTANGLE_ARB) { | 299 if (target != GL_TEXTURE_RECTANGLE_ARB) { |
300 LOG(ERROR) << "YUV_420_BIPLANAR requires GL_TEXTURE_RECTANGLE_ARB target"; | 300 LOG(ERROR) << "YUV_420_BIPLANAR requires GL_TEXTURE_RECTANGLE_ARB target"; |
301 return false; | 301 return false; |
302 } | 302 } |
303 | 303 |
| 304 GLint target_texture = 0; |
| 305 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &target_texture); |
| 306 DCHECK(target_texture); |
| 307 |
| 308 // Manually re-set the state in the scoped closure runner, to avoid bugs |
| 309 // in using gfx::ScopedActiveTexture and gfx::ScopedTextureBinder together. |
| 310 // https://crbug.com/601729 |
| 311 GLint old_active_texture = -1; |
| 312 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_active_texture); |
| 313 GLint old_texture0_binding = -1; |
| 314 glActiveTexture(GL_TEXTURE0); |
| 315 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_texture0_binding); |
| 316 GLint old_texture1_binding = -1; |
| 317 glActiveTexture(GL_TEXTURE1); |
| 318 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_texture1_binding); |
| 319 glActiveTexture(old_active_texture); |
| 320 |
304 // Ensure that all textures bound to IOSurfaces be destroyed before the | 321 // Ensure that all textures bound to IOSurfaces be destroyed before the |
305 // function exits. If they are not destroyed they may cause deadlocks between | 322 // function exits. If they are not destroyed they may cause deadlocks between |
306 // VTDecompressionSession at CGLContextDestroy. | 323 // VTDecompressionSession at CGLContextDestroy. |
307 // https://crbug.com/598388 | 324 // https://crbug.com/598388 |
308 unsigned y_texture = 0; | 325 unsigned y_texture = 0; |
309 glGenTextures(1, &y_texture); | 326 glGenTextures(1, &y_texture); |
310 unsigned uv_texture = 0; | 327 unsigned uv_texture = 0; |
311 glGenTextures(1, &uv_texture); | 328 glGenTextures(1, &uv_texture); |
312 base::ScopedClosureRunner destroy_resources_runner(base::BindBlock(^{ | 329 base::ScopedClosureRunner restore_bindings_and_destroy_resources( |
313 glDeleteTextures(1, &y_texture); | 330 base::BindBlock(^{ |
314 glDeleteTextures(1, &uv_texture); | 331 glActiveTexture(GL_TEXTURE0); |
315 })); | 332 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, old_texture0_binding); |
| 333 glActiveTexture(GL_TEXTURE1); |
| 334 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, old_texture1_binding); |
| 335 glActiveTexture(old_active_texture); |
| 336 |
| 337 glDeleteTextures(1, &y_texture); |
| 338 glDeleteTextures(1, &uv_texture); |
| 339 })); |
316 | 340 |
317 if (!framebuffer_) { | 341 if (!framebuffer_) { |
318 glGenFramebuffersEXT(1, &framebuffer_); | 342 glGenFramebuffersEXT(1, &framebuffer_); |
319 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | 343 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); |
320 vertex_shader_ = gfx::GLHelper::LoadShader( | 344 vertex_shader_ = gfx::GLHelper::LoadShader( |
321 GL_VERTEX_SHADER, | 345 GL_VERTEX_SHADER, |
322 base::StringPrintf("%s\n%s", kGLSLVersion, kVertexShader).c_str()); | 346 base::StringPrintf("%s\n%s", kGLSLVersion, kVertexShader).c_str()); |
323 fragment_shader_ = gfx::GLHelper::LoadShader( | 347 fragment_shader_ = gfx::GLHelper::LoadShader( |
324 GL_FRAGMENT_SHADER, | 348 GL_FRAGMENT_SHADER, |
325 base::StringPrintf("%s\n%s\n%s", kGLSLVersion, | 349 base::StringPrintf("%s\n%s\n%s", kGLSLVersion, |
326 kTextureRectangleRequired, kFragmentShader) | 350 kTextureRectangleRequired, kFragmentShader) |
327 .c_str()); | 351 .c_str()); |
328 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | 352 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); |
329 gfx::ScopedUseProgram use_program(program_); | 353 gfx::ScopedUseProgram use_program(program_); |
330 | 354 |
331 size_location_ = glGetUniformLocation(program_, "a_texScale"); | 355 size_location_ = glGetUniformLocation(program_, "a_texScale"); |
332 DCHECK_NE(-1, size_location_); | 356 DCHECK_NE(-1, size_location_); |
333 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); | 357 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); |
334 DCHECK_NE(-1, y_sampler_location); | 358 DCHECK_NE(-1, y_sampler_location); |
335 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); | 359 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); |
336 DCHECK_NE(-1, uv_sampler_location); | 360 DCHECK_NE(-1, uv_sampler_location); |
337 | 361 |
338 glUniform1i(y_sampler_location, 0); | 362 glUniform1i(y_sampler_location, 0); |
339 glUniform1i(uv_sampler_location, 1); | 363 glUniform1i(uv_sampler_location, 1); |
340 } | 364 } |
341 | 365 |
342 CGLContextObj cgl_context = | 366 CGLContextObj cgl_context = |
343 static_cast<CGLContextObj>(gfx::GLContext::GetCurrent()->GetHandle()); | 367 static_cast<CGLContextObj>(gfx::GLContext::GetCurrent()->GetHandle()); |
344 | 368 |
345 GLint target_texture = 0; | |
346 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &target_texture); | |
347 DCHECK(target_texture); | |
348 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, size_.width(), | 369 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, size_.width(), |
349 size_.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); | 370 size_.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); |
350 | 371 |
351 CGLError cgl_error = kCGLNoError; | 372 CGLError cgl_error = kCGLNoError; |
352 { | |
353 DCHECK(io_surface_); | 373 DCHECK(io_surface_); |
354 | 374 |
355 gfx::ScopedActiveTexture active_texture0(GL_TEXTURE0); | 375 glActiveTexture(GL_TEXTURE0); |
356 gfx::ScopedTextureBinder texture_y_binder(GL_TEXTURE_RECTANGLE_ARB, | 376 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, y_texture); |
357 y_texture); | |
358 cgl_error = CGLTexImageIOSurface2D( | 377 cgl_error = CGLTexImageIOSurface2D( |
359 cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RED, size_.width(), | 378 cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RED, size_.width(), |
360 size_.height(), GL_RED, GL_UNSIGNED_BYTE, io_surface_.get(), 0); | 379 size_.height(), GL_RED, GL_UNSIGNED_BYTE, io_surface_.get(), 0); |
361 if (cgl_error != kCGLNoError) { | 380 if (cgl_error != kCGLNoError) { |
362 LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the Y plane. " | 381 LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the Y plane. " |
363 << cgl_error; | 382 << cgl_error; |
364 return false; | 383 return false; |
365 } | 384 } |
366 { | |
367 gfx::ScopedActiveTexture active_texture1(GL_TEXTURE1); | |
368 gfx::ScopedTextureBinder texture_uv_binder(GL_TEXTURE_RECTANGLE_ARB, | |
369 uv_texture); | |
370 cgl_error = CGLTexImageIOSurface2D( | |
371 cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RG, size_.width() / 2, | |
372 size_.height() / 2, GL_RG, GL_UNSIGNED_BYTE, io_surface_.get(), 1); | |
373 if (cgl_error != kCGLNoError) { | |
374 LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the UV plane. " | |
375 << cgl_error; | |
376 return false; | |
377 } | |
378 | 385 |
379 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | 386 glActiveTexture(GL_TEXTURE1); |
380 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | 387 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, uv_texture); |
381 glViewport(0, 0, size_.width(), size_.height()); | 388 cgl_error = CGLTexImageIOSurface2D( |
382 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 389 cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RG, size_.width() / 2, |
383 GL_TEXTURE_RECTANGLE_ARB, target_texture, 0); | 390 size_.height() / 2, GL_RG, GL_UNSIGNED_BYTE, io_surface_.get(), 1); |
384 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | 391 if (cgl_error != kCGLNoError) { |
385 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | 392 LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the UV plane. " |
| 393 << cgl_error; |
| 394 return false; |
| 395 } |
386 | 396 |
387 gfx::ScopedUseProgram use_program(program_); | 397 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); |
388 glUniform2f(size_location_, size_.width(), size_.height()); | 398 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); |
| 399 glViewport(0, 0, size_.width(), size_.height()); |
| 400 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 401 GL_TEXTURE_RECTANGLE_ARB, target_texture, 0); |
| 402 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 403 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); |
389 | 404 |
390 gfx::GLHelper::DrawQuad(vertex_buffer_); | 405 gfx::ScopedUseProgram use_program(program_); |
391 // Detach the output texture from the fbo. | 406 glUniform2f(size_location_, size_.width(), size_.height()); |
392 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 407 |
393 GL_TEXTURE_RECTANGLE_ARB, 0, 0); | 408 gfx::GLHelper::DrawQuad(vertex_buffer_); |
394 } | 409 // Detach the output texture from the fbo. |
395 } | 410 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 411 GL_TEXTURE_RECTANGLE_ARB, 0, 0); |
396 return true; | 412 return true; |
397 } | 413 } |
398 | 414 |
399 bool GLImageIOSurface::CopyTexSubImage(unsigned target, | 415 bool GLImageIOSurface::CopyTexSubImage(unsigned target, |
400 const gfx::Point& offset, | 416 const gfx::Point& offset, |
401 const gfx::Rect& rect) { | 417 const gfx::Rect& rect) { |
402 return false; | 418 return false; |
403 } | 419 } |
404 | 420 |
405 bool GLImageIOSurface::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 421 bool GLImageIOSurface::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 return cv_pixel_buffer_; | 454 return cv_pixel_buffer_; |
439 } | 455 } |
440 | 456 |
441 // static | 457 // static |
442 unsigned GLImageIOSurface::GetInternalFormatForTesting( | 458 unsigned GLImageIOSurface::GetInternalFormatForTesting( |
443 gfx::BufferFormat format) { | 459 gfx::BufferFormat format) { |
444 DCHECK(ValidFormat(format)); | 460 DCHECK(ValidFormat(format)); |
445 return TextureFormat(format); | 461 return TextureFormat(format); |
446 } | 462 } |
447 } // namespace gl | 463 } // namespace gl |
OLD | NEW |