Index: ui/gl/gl_image_io_surface.mm |
diff --git a/ui/gl/gl_image_io_surface.mm b/ui/gl/gl_image_io_surface.mm |
index d6ac250b84bc8849e403ea455980b75842ef5ca5..bf979dcca555dcfeca1a825de454e44c20362720 100644 |
--- a/ui/gl/gl_image_io_surface.mm |
+++ b/ui/gl/gl_image_io_surface.mm |
@@ -6,13 +6,17 @@ |
#include <map> |
+#include "base/callback_helpers.h" |
reveman
2015/11/01 14:19:04
is this used?
Daniele Castagna
2015/11/01 21:55:31
Not anymore. Removed.
|
#include "base/lazy_instance.h" |
#include "base/mac/foundation_util.h" |
+#include "base/strings/stringize_macros.h" |
#include "base/trace_event/memory_allocator_dump.h" |
#include "base/trace_event/memory_dump_manager.h" |
#include "base/trace_event/process_memory_dump.h" |
#include "ui/gl/gl_bindings.h" |
#include "ui/gl/gl_context.h" |
+#include "ui/gl/gl_helper.h" |
+#include "ui/gl/scoped_binders.h" |
// Note that this must be included after gl_bindings.h to avoid conflicts. |
#include <OpenGL/CGLIOSurface.h> |
@@ -26,11 +30,41 @@ namespace { |
using WidgetToLayerMap = std::map<gfx::AcceleratedWidget, CALayer*>; |
base::LazyInstance<WidgetToLayerMap> g_widget_to_layer_map; |
+// clang-format off |
+const char kVertexShader[] = |
+STRINGIZE( |
+ attribute vec2 a_position; |
+ uniform vec2 a_size; |
+ varying vec2 v_texCoord; |
+ void main() { |
+ gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); |
+ v_texCoord = (a_position + vec2(1,1)) * 0.5 * a_size; |
reveman
2015/11/01 14:19:05
s/vec2(1,1)/vec2(1, 1)/ maybe vec2(1.0, 1.0)?
als
Daniele Castagna
2015/11/01 21:55:31
Done.
|
+ } |
+); |
+ |
+const char kFragmentShader[] = |
+STRINGIZE( |
+ uniform sampler2DRect a_y_texture; |
+ uniform sampler2DRect a_uv_texture; |
+ varying vec2 v_texCoord; |
+ void main() { |
+ vec3 yuv_adj = vec3(-0.0625, -0.5, -0.5); |
+ mat3 yuv_matrix = mat3(vec3(1.164, 1.164, 1.164), |
+ vec3(0.0, -.391, 2.018), |
+ vec3(1.596, -.813, 0.0)); |
+ vec3 yuv = vec3(texture2DRect(a_y_texture, v_texCoord).r, |
+ texture2DRect(a_uv_texture, v_texCoord * 0.5).rg); |
reveman
2015/11/01 14:19:04
nit: Horizontally align texture2DRect to have cons
Daniele Castagna
2015/11/01 21:55:31
Done.
|
+ gl_FragColor = vec4( yuv_matrix * (yuv + yuv_adj), 1.0); |
reveman
2015/11/01 14:19:04
nit: s/vec4( yuv_mat.../vec4(yuv_mat.../
Daniele Castagna
2015/11/01 21:55:31
Done.
|
+ } |
+); |
+// clang-format on |
+ |
bool ValidInternalFormat(unsigned internalformat) { |
switch (internalformat) { |
case GL_R8: |
case GL_BGRA_EXT: |
case GL_RGB: |
+ case GL_RGB_YCBCR_420V_CHROMIUM: |
case GL_RGB_YCBCR_422_CHROMIUM: |
case GL_RGBA: |
return true; |
@@ -66,7 +100,6 @@ bool ValidFormat(BufferFormat format) { |
GLenum TextureFormat(BufferFormat format) { |
switch (format) { |
case BufferFormat::R_8: |
- case BufferFormat::YUV_420_BIPLANAR: |
return GL_RED; |
case BufferFormat::BGRA_8888: |
case BufferFormat::RGBA_8888: |
@@ -82,6 +115,7 @@ GLenum TextureFormat(BufferFormat format) { |
case BufferFormat::RGBX_8888: |
case BufferFormat::BGRX_8888: |
case BufferFormat::YUV_420: |
+ case BufferFormat::YUV_420_BIPLANAR: |
NOTREACHED(); |
return 0; |
} |
@@ -93,14 +127,12 @@ GLenum TextureFormat(BufferFormat format) { |
GLenum DataFormat(BufferFormat format) { |
switch (format) { |
case BufferFormat::R_8: |
- case BufferFormat::YUV_420_BIPLANAR: |
return GL_RED; |
case BufferFormat::BGRA_8888: |
case BufferFormat::RGBA_8888: |
return GL_BGRA; |
case BufferFormat::UYVY_422: |
return GL_YCBCR_422_APPLE; |
- break; |
case BufferFormat::ATC: |
case BufferFormat::ATCIA: |
case BufferFormat::DXT1: |
@@ -110,6 +142,7 @@ GLenum DataFormat(BufferFormat format) { |
case BufferFormat::RGBX_8888: |
case BufferFormat::BGRX_8888: |
case BufferFormat::YUV_420: |
+ case BufferFormat::YUV_420_BIPLANAR: |
NOTREACHED(); |
return 0; |
} |
@@ -121,7 +154,6 @@ GLenum DataFormat(BufferFormat format) { |
GLenum DataType(BufferFormat format) { |
switch (format) { |
case BufferFormat::R_8: |
- case BufferFormat::YUV_420_BIPLANAR: |
return GL_UNSIGNED_BYTE; |
case BufferFormat::BGRA_8888: |
case BufferFormat::RGBA_8888: |
@@ -138,6 +170,7 @@ GLenum DataType(BufferFormat format) { |
case BufferFormat::RGBX_8888: |
case BufferFormat::BGRX_8888: |
case BufferFormat::YUV_420: |
+ case BufferFormat::YUV_420_BIPLANAR: |
NOTREACHED(); |
return 0; |
} |
@@ -146,6 +179,18 @@ GLenum DataType(BufferFormat format) { |
return 0; |
} |
+GLuint SetupVertexBuffer() { |
+ GLuint vertex_buffer = 0; |
+ glGenBuffersARB(1, &vertex_buffer); |
+ gfx::ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, vertex_buffer); |
+ |
+ GLfloat data[] = {-1.f, -1.f, 1.f, -1.f, -1.f, 1.f, 1.f, 1.f}; |
+ glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); |
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, 0); |
+ |
+ return vertex_buffer; |
+} |
+ |
} // namespace |
GLImageIOSurface::GLImageIOSurface(const gfx::Size& size, |
@@ -183,6 +228,13 @@ bool GLImageIOSurface::Initialize(IOSurfaceRef io_surface, |
void GLImageIOSurface::Destroy(bool have_context) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (have_context) { |
reveman
2015/11/01 14:19:04
Can you add framebuffer_ to this 'if' clause? I kn
Daniele Castagna
2015/11/01 21:55:30
Done.
|
+ glDeleteProgram(program_); |
+ glDeleteShader(vertex_shader_); |
+ glDeleteShader(fragment_shader_); |
+ glDeleteBuffersARB(1, &vertex_buffer_); |
+ glDeleteFramebuffersEXT(1, &framebuffer_); |
+ } |
io_surface_.reset(); |
} |
@@ -194,6 +246,11 @@ unsigned GLImageIOSurface::GetInternalFormat() { return internalformat_; } |
bool GLImageIOSurface::BindTexImage(unsigned target) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ // YUV_420_BIPLANAR is not supported by BindTexImage. |
reveman
2015/11/01 14:19:04
nit: can you add a blinkline after the DCHECK as o
Daniele Castagna
2015/11/01 21:55:31
Done.
|
+ // Returning false here will result in conversion to RGB in CopyTexImage. |
reveman
2015/11/01 14:19:04
nit: maybe something like "CopyTexImage is support
Daniele Castagna
2015/11/01 21:55:30
Done.
|
+ if (format_ == BufferFormat::YUV_420_BIPLANAR) |
+ return false; |
+ |
if (target != GL_TEXTURE_RECTANGLE_ARB) { |
// This might be supported in the future. For now, perform strict |
// validation so we know what's going on. |
@@ -218,7 +275,99 @@ bool GLImageIOSurface::BindTexImage(unsigned target) { |
} |
bool GLImageIOSurface::CopyTexImage(unsigned target) { |
- return false; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(io_surface_); |
+ if (format_ != BufferFormat::YUV_420_BIPLANAR) |
reveman
2015/11/01 14:19:05
nit: blankline after DCHECKs and maybe move the io
Daniele Castagna
2015/11/01 21:55:31
Done.
|
+ return false; |
+ |
+ if (target != GL_TEXTURE_2D) { |
+ LOG(ERROR) << "YUV_420_BIPLANAR requires TEXTURE_2D target"; |
+ return false; |
+ } |
+ |
+ if (!framebuffer_) { |
+ glGenFramebuffersEXT(1, &framebuffer_); |
+ vertex_buffer_ = SetupVertexBuffer(); |
+ vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); |
+ fragment_shader_ = |
+ gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
+ program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); |
+ gfx::ScopedUseProgram use_program(program_); |
+ |
+ size_location_ = glGetUniformLocation(program_, "a_size"); |
+ DCHECK_NE(-1, size_location_); |
+ int y_sampler_location = glGetUniformLocation(program_, "a_y_texture"); |
+ DCHECK_NE(-1, y_sampler_location); |
+ int uv_sampler_location = glGetUniformLocation(program_, "a_uv_texture"); |
+ DCHECK_NE(-1, uv_sampler_location); |
+ |
+ glUniform1i(y_sampler_location, 0); |
+ glUniform1i(uv_sampler_location, 1); |
+ } |
+ |
+ CGLContextObj cgl_context = |
+ static_cast<CGLContextObj>(gfx::GLContext::GetCurrent()->GetHandle()); |
+ |
+ GLuint yuv_textures[2]; |
reveman
2015/11/01 14:19:04
nit: ..textures[2] = {};
Daniele Castagna
2015/11/01 21:55:30
Done.
|
+ glGenTextures(2, yuv_textures); |
+ DCHECK(yuv_textures[0] && yuv_textures[1]); |
reveman
2015/11/01 14:19:04
nit: one DCHECK for each texture is preferred
Daniele Castagna
2015/11/01 21:55:30
Done.
|
+ |
+ GLint target_texture = 0; |
+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture); |
+ DCHECK(target_texture); |
+ |
+ GLint previous_active_texture = 0; |
+ glGetIntegerv(GL_ACTIVE_TEXTURE, &previous_active_texture); |
reveman
2015/11/01 14:19:04
is this still used?
Daniele Castagna
2015/11/01 21:55:30
Nop. Gone.
|
+ |
+ CGLError cgl_error = kCGLNoError; |
+ { |
+ gfx::ScopedActiveTexture active_texture0(GL_TEXTURE0); |
+ gfx::ScopedTextureBinder texture_y_binder(GL_TEXTURE_RECTANGLE_ARB, |
+ yuv_textures[0]); |
+ cgl_error = CGLTexImageIOSurface2D( |
+ cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RED, size_.width(), |
+ size_.height(), GL_RED, GL_UNSIGNED_BYTE, io_surface_.get(), 0); |
+ if (cgl_error != kCGLNoError) { |
+ LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the Y plane. " |
+ << cgl_error; |
+ glDeleteTextures(2, yuv_textures); |
+ return false; |
+ } |
+ { |
+ gfx::ScopedActiveTexture active_texture1(GL_TEXTURE1); |
+ gfx::ScopedTextureBinder texture_uv_binder(GL_TEXTURE_RECTANGLE_ARB, |
+ yuv_textures[1]); |
+ cgl_error = CGLTexImageIOSurface2D( |
+ cgl_context, GL_TEXTURE_RECTANGLE_ARB, GL_RG, size_.width() / 2, |
+ size_.height() / 2, GL_RG, GL_UNSIGNED_BYTE, io_surface_.get(), 1); |
+ if (cgl_error != kCGLNoError) { |
+ LOG(ERROR) << "Error in CGLTexImageIOSurface2D for the UV plane. " |
+ << cgl_error; |
+ glDeleteTextures(2, yuv_textures); |
+ return false; |
+ } |
+ |
+ gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); |
+ gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); |
+ glViewport(0, 0, size_.width(), size_.height()); |
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
+ GL_TEXTURE_2D, target_texture, 0); |
reveman
2015/11/01 14:19:04
Do we need a glTexImage2D call for the target text
Daniele Castagna
2015/11/01 21:55:31
We do need it. Added it.
|
+ DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
+ glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); |
+ |
+ gfx::ScopedUseProgram use_program(program_); |
+ glUniform2f(size_location_, size_.width(), size_.height()); |
+ |
+ gfx::ScopedEnableVertexAttribArray enable_vertex_attrib_array(0); |
+ gfx::ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, vertex_buffer_); |
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
+ |
+ // Detach the output texture from the fbo. |
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
+ GL_TEXTURE_2D, 0, 0); |
+ } |
+ } |
+ return true; |
} |
bool GLImageIOSurface::CopyTexSubImage(unsigned target, |