Chromium Code Reviews| 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" |
| 11 #include "base/mac/bind_objc_block.h" | 11 #include "base/mac/bind_objc_block.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/strings/stringize_macros.h" | 13 #include "base/strings/stringize_macros.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/trace_event/memory_allocator_dump.h" | 15 #include "base/trace_event/memory_allocator_dump.h" |
| 16 #include "base/trace_event/memory_dump_manager.h" | 16 #include "base/trace_event/memory_dump_manager.h" |
| 17 #include "base/trace_event/process_memory_dump.h" | 17 #include "base/trace_event/process_memory_dump.h" |
| 18 #include "ui/gl/gl_bindings.h" | 18 #include "ui/gl/gl_bindings.h" |
| 19 #include "ui/gl/gl_context.h" | 19 #include "ui/gl/gl_context.h" |
| 20 #include "ui/gl/gl_helper.h" | 20 #include "ui/gl/gl_helper.h" |
| 21 #include "ui/gl/gl_implementation.h" | |
| 21 #include "ui/gl/scoped_api.h" | 22 #include "ui/gl/scoped_api.h" |
| 22 #include "ui/gl/scoped_binders.h" | 23 #include "ui/gl/scoped_binders.h" |
| 23 #include "ui/gl/scoped_cgl.h" | 24 #include "ui/gl/scoped_cgl.h" |
| 24 | 25 |
| 25 // Note that this must be included after gl_bindings.h to avoid conflicts. | 26 // Note that this must be included after gl_bindings.h to avoid conflicts. |
| 26 #include <OpenGL/CGLIOSurface.h> | 27 #include <OpenGL/CGLIOSurface.h> |
| 27 #include <Quartz/Quartz.h> | 28 #include <Quartz/Quartz.h> |
| 28 #include <stddef.h> | 29 #include <stddef.h> |
| 29 | 30 |
| 30 using gfx::BufferFormat; | 31 using gfx::BufferFormat; |
| 31 | 32 |
| 32 namespace gl { | 33 namespace gl { |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 const char kGLSLVersion[] = "#version 110"; | 36 const char kVertexHeaderCompatiblityProfile[] = "#version 110"; |
| 36 | 37 |
| 37 const char kTextureRectangleRequired[] = | 38 const char kVertexHeaderCoreProfile[] = |
| 38 "#extension GL_ARB_texture_rectangle : require"; | 39 "#version 150\n" |
| 40 "#define attribute in\n" | |
| 41 "#define varying out\n"; | |
|
Ken Russell (switch to Gerrit)
2016/04/14 00:01:15
I suggest naming these ATTRIBUTE and VARYING and r
ccameron
2016/04/14 00:25:13
Good point -- done.
| |
| 42 | |
| 43 const char kFragmentHeaderCompatiblityProfile[] = | |
| 44 "#version 110\n" | |
| 45 "#extension GL_ARB_texture_rectangle : require\n" | |
| 46 "#define FRAGCOLOR gl_FragColor\n" | |
| 47 "#define TEX texture2DRect\n"; | |
| 48 | |
| 49 const char kFragmentHeaderCoreProfile[] = | |
| 50 "#version 150\n" | |
| 51 "#define varying in\n" | |
| 52 "#define TEX texture\n" | |
| 53 "#define FRAGCOLOR frag_color\n" | |
| 54 "out vec4 FRAGCOLOR;\n"; | |
| 39 | 55 |
| 40 // clang-format off | 56 // clang-format off |
| 41 const char kVertexShader[] = | 57 const char kVertexShader[] = |
| 42 STRINGIZE( | 58 STRINGIZE( |
| 43 attribute vec2 a_position; | 59 attribute vec2 a_position; |
| 44 uniform vec2 a_texScale; | 60 uniform vec2 a_texScale; |
| 45 varying vec2 v_texCoord; | 61 varying vec2 v_texCoord; |
| 46 void main() { | 62 void main() { |
| 47 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | 63 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); |
| 48 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5 * a_texScale; | 64 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5 * a_texScale; |
| 49 } | 65 } |
| 50 ); | 66 ); |
| 51 | 67 |
| 52 const char kFragmentShader[] = | 68 const char kFragmentShader[] = |
| 53 STRINGIZE( | 69 STRINGIZE( |
| 54 uniform sampler2DRect a_y_texture; | 70 uniform sampler2DRect a_y_texture; |
| 55 uniform sampler2DRect a_uv_texture; | 71 uniform sampler2DRect a_uv_texture; |
| 56 varying vec2 v_texCoord; | 72 varying vec2 v_texCoord; |
| 57 void main() { | 73 void main() { |
| 58 vec3 yuv_adj = vec3(-0.0625, -0.5, -0.5); | 74 vec3 yuv_adj = vec3(-0.0625, -0.5, -0.5); |
| 59 mat3 yuv_matrix = mat3(vec3(1.164, 1.164, 1.164), | 75 mat3 yuv_matrix = mat3(vec3(1.164, 1.164, 1.164), |
| 60 vec3(0.0, -.391, 2.018), | 76 vec3(0.0, -.391, 2.018), |
| 61 vec3(1.596, -.813, 0.0)); | 77 vec3(1.596, -.813, 0.0)); |
| 62 vec3 yuv = vec3( | 78 vec3 yuv = vec3( |
| 63 texture2DRect(a_y_texture, v_texCoord).r, | 79 TEX(a_y_texture, v_texCoord).r, |
| 64 texture2DRect(a_uv_texture, v_texCoord * 0.5).rg); | 80 TEX(a_uv_texture, v_texCoord * 0.5).rg); |
| 65 gl_FragColor = vec4(yuv_matrix * (yuv + yuv_adj), 1.0); | 81 FRAGCOLOR = vec4(yuv_matrix * (yuv + yuv_adj), 1.0); |
| 66 } | 82 } |
| 67 ); | 83 ); |
| 68 // clang-format on | 84 // clang-format on |
| 69 | 85 |
| 70 bool ValidInternalFormat(unsigned internalformat) { | 86 bool ValidInternalFormat(unsigned internalformat) { |
| 71 switch (internalformat) { | 87 switch (internalformat) { |
| 72 case GL_RED: | 88 case GL_RED: |
| 73 case GL_BGRA_EXT: | 89 case GL_BGRA_EXT: |
| 74 case GL_RGB: | 90 case GL_RGB: |
| 75 case GL_RGB_YCBCR_420V_CHROMIUM: | 91 case GL_RGB_YCBCR_420V_CHROMIUM: |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 DCHECK(current_context); | 244 DCHECK(current_context); |
| 229 DCHECK(g_rgb_converters_thread_checker.Get().CalledOnValidThread()); | 245 DCHECK(g_rgb_converters_thread_checker.Get().CalledOnValidThread()); |
| 230 auto found = g_rgb_converters.Get().find(current_context); | 246 auto found = g_rgb_converters.Get().find(current_context); |
| 231 if (found != g_rgb_converters.Get().end()) | 247 if (found != g_rgb_converters.Get().end()) |
| 232 return make_scoped_refptr(found->second); | 248 return make_scoped_refptr(found->second); |
| 233 return make_scoped_refptr(new RGBConverter(current_context)); | 249 return make_scoped_refptr(new RGBConverter(current_context)); |
| 234 } | 250 } |
| 235 | 251 |
| 236 GLImageIOSurface::RGBConverter::RGBConverter(CGLContextObj cgl_context) | 252 GLImageIOSurface::RGBConverter::RGBConverter(CGLContextObj cgl_context) |
| 237 : cgl_context_(cgl_context, base::scoped_policy::RETAIN) { | 253 : cgl_context_(cgl_context, base::scoped_policy::RETAIN) { |
| 254 bool use_core_profile = | |
| 255 gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGLCoreProfile; | |
| 238 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; | 256 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; |
| 239 glGenFramebuffersEXT(1, &framebuffer_); | 257 glGenFramebuffersEXT(1, &framebuffer_); |
| 240 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | 258 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); |
| 241 vertex_shader_ = gfx::GLHelper::LoadShader( | 259 vertex_shader_ = gfx::GLHelper::LoadShader( |
| 242 GL_VERTEX_SHADER, | 260 GL_VERTEX_SHADER, |
| 243 base::StringPrintf("%s\n%s", kGLSLVersion, kVertexShader).c_str()); | 261 base::StringPrintf("%s\n%s", |
| 262 use_core_profile ? kVertexHeaderCoreProfile | |
| 263 : kVertexHeaderCompatiblityProfile, | |
| 264 kVertexShader).c_str()); | |
| 244 fragment_shader_ = gfx::GLHelper::LoadShader( | 265 fragment_shader_ = gfx::GLHelper::LoadShader( |
| 245 GL_FRAGMENT_SHADER, | 266 GL_FRAGMENT_SHADER, |
| 246 base::StringPrintf("%s\n%s\n%s", kGLSLVersion, kTextureRectangleRequired, | 267 base::StringPrintf("%s\n%s", |
| 247 kFragmentShader) | 268 use_core_profile ? kFragmentHeaderCoreProfile |
| 248 .c_str()); | 269 : kFragmentHeaderCompatiblityProfile, |
| 270 kFragmentShader).c_str()); | |
| 249 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | 271 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); |
| 250 | 272 |
| 251 gfx::ScopedUseProgram use_program(program_); | 273 gfx::ScopedUseProgram use_program(program_); |
| 252 size_location_ = glGetUniformLocation(program_, "a_texScale"); | 274 size_location_ = glGetUniformLocation(program_, "a_texScale"); |
| 253 DCHECK_NE(-1, size_location_); | 275 DCHECK_NE(-1, size_location_); |
| 254 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); | 276 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); |
| 255 DCHECK_NE(-1, y_sampler_location); | 277 DCHECK_NE(-1, y_sampler_location); |
| 256 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); | 278 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); |
| 257 DCHECK_NE(-1, uv_sampler_location); | 279 DCHECK_NE(-1, uv_sampler_location); |
| 258 | 280 |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 507 return cv_pixel_buffer_; | 529 return cv_pixel_buffer_; |
| 508 } | 530 } |
| 509 | 531 |
| 510 // static | 532 // static |
| 511 unsigned GLImageIOSurface::GetInternalFormatForTesting( | 533 unsigned GLImageIOSurface::GetInternalFormatForTesting( |
| 512 gfx::BufferFormat format) { | 534 gfx::BufferFormat format) { |
| 513 DCHECK(ValidFormat(format)); | 535 DCHECK(ValidFormat(format)); |
| 514 return TextureFormat(format); | 536 return TextureFormat(format); |
| 515 } | 537 } |
| 516 } // namespace gl | 538 } // namespace gl |
| OLD | NEW |