| 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[] = |
| 37 "#version 110\n" |
| 38 "#define ATTRIBUTE attribute\n" |
| 39 "#define VARYING varying\n"; |
| 36 | 40 |
| 37 const char kTextureRectangleRequired[] = | 41 const char kVertexHeaderCoreProfile[] = |
| 38 "#extension GL_ARB_texture_rectangle : require"; | 42 "#version 150\n" |
| 43 "#define ATTRIBUTE in\n" |
| 44 "#define VARYING out\n"; |
| 45 |
| 46 const char kFragmentHeaderCompatiblityProfile[] = |
| 47 "#version 110\n" |
| 48 "#extension GL_ARB_texture_rectangle : require\n" |
| 49 "#define VARYING varying\n" |
| 50 "#define FRAGCOLOR gl_FragColor\n" |
| 51 "#define TEX texture2DRect\n"; |
| 52 |
| 53 const char kFragmentHeaderCoreProfile[] = |
| 54 "#version 150\n" |
| 55 "#define VARYING in\n" |
| 56 "#define TEX texture\n" |
| 57 "#define FRAGCOLOR frag_color\n" |
| 58 "out vec4 FRAGCOLOR;\n"; |
| 39 | 59 |
| 40 // clang-format off | 60 // clang-format off |
| 41 const char kVertexShader[] = | 61 const char kVertexShader[] = |
| 42 STRINGIZE( | 62 STRINGIZE( |
| 43 attribute vec2 a_position; | 63 ATTRIBUTE vec2 a_position; |
| 44 uniform vec2 a_texScale; | 64 uniform vec2 a_texScale; |
| 45 varying vec2 v_texCoord; | 65 VARYING vec2 v_texCoord; |
| 46 void main() { | 66 void main() { |
| 47 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | 67 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; | 68 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5 * a_texScale; |
| 49 } | 69 } |
| 50 ); | 70 ); |
| 51 | 71 |
| 52 const char kFragmentShader[] = | 72 const char kFragmentShader[] = |
| 53 STRINGIZE( | 73 STRINGIZE( |
| 54 uniform sampler2DRect a_y_texture; | 74 uniform sampler2DRect a_y_texture; |
| 55 uniform sampler2DRect a_uv_texture; | 75 uniform sampler2DRect a_uv_texture; |
| 56 varying vec2 v_texCoord; | 76 VARYING vec2 v_texCoord; |
| 57 void main() { | 77 void main() { |
| 58 vec3 yuv_adj = vec3(-0.0625, -0.5, -0.5); | 78 vec3 yuv_adj = vec3(-0.0625, -0.5, -0.5); |
| 59 mat3 yuv_matrix = mat3(vec3(1.164, 1.164, 1.164), | 79 mat3 yuv_matrix = mat3(vec3(1.164, 1.164, 1.164), |
| 60 vec3(0.0, -.391, 2.018), | 80 vec3(0.0, -.391, 2.018), |
| 61 vec3(1.596, -.813, 0.0)); | 81 vec3(1.596, -.813, 0.0)); |
| 62 vec3 yuv = vec3( | 82 vec3 yuv = vec3( |
| 63 texture2DRect(a_y_texture, v_texCoord).r, | 83 TEX(a_y_texture, v_texCoord).r, |
| 64 texture2DRect(a_uv_texture, v_texCoord * 0.5).rg); | 84 TEX(a_uv_texture, v_texCoord * 0.5).rg); |
| 65 gl_FragColor = vec4(yuv_matrix * (yuv + yuv_adj), 1.0); | 85 FRAGCOLOR = vec4(yuv_matrix * (yuv + yuv_adj), 1.0); |
| 66 } | 86 } |
| 67 ); | 87 ); |
| 68 // clang-format on | 88 // clang-format on |
| 69 | 89 |
| 70 bool ValidInternalFormat(unsigned internalformat) { | 90 bool ValidInternalFormat(unsigned internalformat) { |
| 71 switch (internalformat) { | 91 switch (internalformat) { |
| 72 case GL_RED: | 92 case GL_RED: |
| 73 case GL_BGRA_EXT: | 93 case GL_BGRA_EXT: |
| 74 case GL_RGB: | 94 case GL_RGB: |
| 75 case GL_RGB_YCBCR_420V_CHROMIUM: | 95 case GL_RGB_YCBCR_420V_CHROMIUM: |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 DCHECK(current_context); | 248 DCHECK(current_context); |
| 229 DCHECK(g_rgb_converters_thread_checker.Get().CalledOnValidThread()); | 249 DCHECK(g_rgb_converters_thread_checker.Get().CalledOnValidThread()); |
| 230 auto found = g_rgb_converters.Get().find(current_context); | 250 auto found = g_rgb_converters.Get().find(current_context); |
| 231 if (found != g_rgb_converters.Get().end()) | 251 if (found != g_rgb_converters.Get().end()) |
| 232 return make_scoped_refptr(found->second); | 252 return make_scoped_refptr(found->second); |
| 233 return make_scoped_refptr(new RGBConverter(current_context)); | 253 return make_scoped_refptr(new RGBConverter(current_context)); |
| 234 } | 254 } |
| 235 | 255 |
| 236 GLImageIOSurface::RGBConverter::RGBConverter(CGLContextObj cgl_context) | 256 GLImageIOSurface::RGBConverter::RGBConverter(CGLContextObj cgl_context) |
| 237 : cgl_context_(cgl_context, base::scoped_policy::RETAIN) { | 257 : cgl_context_(cgl_context, base::scoped_policy::RETAIN) { |
| 258 bool use_core_profile = |
| 259 gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGLCoreProfile; |
| 238 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; | 260 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; |
| 239 glGenFramebuffersEXT(1, &framebuffer_); | 261 glGenFramebuffersEXT(1, &framebuffer_); |
| 240 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | 262 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); |
| 241 vertex_shader_ = gfx::GLHelper::LoadShader( | 263 vertex_shader_ = gfx::GLHelper::LoadShader( |
| 242 GL_VERTEX_SHADER, | 264 GL_VERTEX_SHADER, |
| 243 base::StringPrintf("%s\n%s", kGLSLVersion, kVertexShader).c_str()); | 265 base::StringPrintf("%s\n%s", |
| 266 use_core_profile ? kVertexHeaderCoreProfile |
| 267 : kVertexHeaderCompatiblityProfile, |
| 268 kVertexShader).c_str()); |
| 244 fragment_shader_ = gfx::GLHelper::LoadShader( | 269 fragment_shader_ = gfx::GLHelper::LoadShader( |
| 245 GL_FRAGMENT_SHADER, | 270 GL_FRAGMENT_SHADER, |
| 246 base::StringPrintf("%s\n%s\n%s", kGLSLVersion, kTextureRectangleRequired, | 271 base::StringPrintf("%s\n%s", |
| 247 kFragmentShader) | 272 use_core_profile ? kFragmentHeaderCoreProfile |
| 248 .c_str()); | 273 : kFragmentHeaderCompatiblityProfile, |
| 274 kFragmentShader).c_str()); |
| 249 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | 275 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); |
| 250 | 276 |
| 251 gfx::ScopedUseProgram use_program(program_); | 277 gfx::ScopedUseProgram use_program(program_); |
| 252 size_location_ = glGetUniformLocation(program_, "a_texScale"); | 278 size_location_ = glGetUniformLocation(program_, "a_texScale"); |
| 253 DCHECK_NE(-1, size_location_); | 279 DCHECK_NE(-1, size_location_); |
| 254 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); | 280 int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); |
| 255 DCHECK_NE(-1, y_sampler_location); | 281 DCHECK_NE(-1, y_sampler_location); |
| 256 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); | 282 int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); |
| 257 DCHECK_NE(-1, uv_sampler_location); | 283 DCHECK_NE(-1, uv_sampler_location); |
| 258 | 284 |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 return cv_pixel_buffer_; | 533 return cv_pixel_buffer_; |
| 508 } | 534 } |
| 509 | 535 |
| 510 // static | 536 // static |
| 511 unsigned GLImageIOSurface::GetInternalFormatForTesting( | 537 unsigned GLImageIOSurface::GetInternalFormatForTesting( |
| 512 gfx::BufferFormat format) { | 538 gfx::BufferFormat format) { |
| 513 DCHECK(ValidFormat(format)); | 539 DCHECK(ValidFormat(format)); |
| 514 return TextureFormat(format); | 540 return TextureFormat(format); |
| 515 } | 541 } |
| 516 } // namespace gl | 542 } // namespace gl |
| OLD | NEW |