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 fe34240f737f2ec3489d2f10c027f7605a87612b..e83bd8d4b0529445df76baaa4242aa3d0249e9f8 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" |
#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> |
@@ -29,6 +33,7 @@ bool ValidInternalFormat(unsigned internalformat) { |
case GL_R8: |
case GL_BGRA_EXT: |
case GL_RGB: |
+ case GL_RGB_YCBCR_420V_CHROMIUM: |
case GL_RGB_YCBCR_422_CHROMIUM: |
return true; |
default: |
@@ -63,12 +68,10 @@ 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: |
return GL_RGBA; |
case BufferFormat::UYVY_422: |
- return GL_RGB; |
case BufferFormat::ATC: |
case BufferFormat::ATCIA: |
case BufferFormat::DXT1: |
@@ -79,6 +82,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; |
} |
@@ -90,13 +94,11 @@ 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: |
return GL_BGRA; |
case BufferFormat::UYVY_422: |
return GL_YCBCR_422_APPLE; |
- break; |
case BufferFormat::ATC: |
case BufferFormat::ATCIA: |
case BufferFormat::DXT1: |
@@ -107,6 +109,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; |
} |
@@ -118,7 +121,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: |
return GL_UNSIGNED_INT_8_8_8_8_REV; |
@@ -135,6 +137,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; |
} |
@@ -145,6 +148,158 @@ GLenum DataType(BufferFormat format) { |
} // namespace |
+class GLImageIOSurface::I420vToRGBA { |
+ public: |
+ bool Initialize(); |
+ bool DrawToTexture(const Size& size, |
+ GLuint yuv_planes[2], |
+ GLuint output_texture, |
+ unsigned target); |
+ void Destroy(); |
+ |
+ private: |
+ static const char kVertexShader[]; |
+ static const char kFragmentShader[]; |
+ GLuint framebuffer_object_ = 0; |
+ GLuint vertex_shader_ = 0; |
+ GLuint fragment_shader_ = 0; |
+ GLuint program_object_ = 0; |
+ GLint y_sampler_location_ = -1; |
+ GLint uv_sampler_location_ = -1; |
+ GLint size_location_ = -1; |
+ GLuint vertex_buffer_ = 0; |
+}; |
+ |
+// clang-format off |
+const char GLImageIOSurface::I420vToRGBA::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; |
+ } |
+); |
+ |
+const char GLImageIOSurface::I420vToRGBA::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); |
+ gl_FragColor = vec4( yuv_matrix * (yuv + yuv_adj), 1.0); |
+ } |
+); |
+// clang-format on |
+ |
+bool GLImageIOSurface::I420vToRGBA::Initialize() { |
+ glGenFramebuffersEXT(1, &framebuffer_object_); |
+ DCHECK(framebuffer_object_); |
+ |
+ glGenBuffersARB(1, &vertex_buffer_); |
+ GLint previous_buffer = 0; |
+ glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &previous_buffer); |
+ glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
+ |
+ // clang-format off |
+ GLfloat data[] = { |
+ -1.f, -1.f, |
+ 1.f, -1.f, |
+ -1.f, 1.f, |
+ 1.f, 1.f}; |
+ // clang-format on |
+ glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); |
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, 0); |
+ glBindBuffer(GL_ARRAY_BUFFER, previous_buffer); |
+ |
+ vertex_shader_ = GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); |
+ fragment_shader_ = GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
+ program_object_ = GLHelper::SetupProgram(vertex_shader_, fragment_shader_); |
+ GLint previous_program = 0; |
+ glGetIntegerv(GL_CURRENT_PROGRAM, &previous_program); |
+ glUseProgram(program_object_); |
+ |
+ size_location_ = glGetUniformLocation(program_object_, "a_size"); |
+ DCHECK_NE(-1, size_location_); |
+ y_sampler_location_ = glGetUniformLocation(program_object_, "a_y_texture"); |
+ DCHECK_NE(-1, y_sampler_location_); |
+ uv_sampler_location_ = glGetUniformLocation(program_object_, "a_uv_texture"); |
+ DCHECK_NE(-1, uv_sampler_location_); |
+ |
+ glUniform1i(y_sampler_location_, 0); |
+ glUniform1i(uv_sampler_location_, 1); |
+ glUseProgram(previous_program); |
+ return true; |
+} |
+ |
+bool GLImageIOSurface::I420vToRGBA::DrawToTexture(const Size& size, |
+ GLuint yuv_planes[2], |
+ GLuint output_texture, |
+ unsigned target) { |
+ ScopedFrameBufferBinder fb(framebuffer_object_); |
+ glViewport(0, 0, size.width(), size.height()); |
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, |
+ output_texture, 0); |
+ DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
+ glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); |
+ |
+ GLint previous_active_texture = 0; |
+ glGetIntegerv(GL_ACTIVE_TEXTURE, &previous_active_texture); |
+ glActiveTexture(GL_TEXTURE0); |
+ GLint previous_texture_0 = 0; |
+ glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &previous_texture_0); |
+ glBindTexture(GL_TEXTURE_RECTANGLE_ARB, yuv_planes[0]); |
+ |
+ glActiveTexture(GL_TEXTURE1); |
+ glBindTexture(GL_TEXTURE_RECTANGLE_ARB, yuv_planes[1]); |
+ GLint previous_texture_1 = 0; |
+ glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &previous_texture_1); |
+ |
+ GLint previous_program = 0; |
+ glGetIntegerv(GL_CURRENT_PROGRAM, &previous_program); |
+ glUseProgram(program_object_); |
+ glUniform2f(size_location_, size.width(), size.height()); |
+ |
+ GLint vertex_array_enabled = 0; |
+ glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &vertex_array_enabled); |
+ glEnableVertexAttribArray(0); |
+ GLint previous_buffer = 0; |
+ glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &previous_buffer); |
+ glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
+ |
+ // Detach the service texture from the fbo. |
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, 0, 0); |
+ // Restore texture bindings and active texture. |
+ glBindTexture(GL_TEXTURE_RECTANGLE_ARB, previous_texture_1); |
+ glActiveTexture(GL_TEXTURE0); |
+ glBindTexture(GL_TEXTURE_RECTANGLE_ARB, previous_texture_0); |
+ glActiveTexture(previous_active_texture); |
+ // Restore program. |
+ glUseProgram(previous_program); |
+ // Restore bound vbuff. |
+ glBindBuffer(GL_ARRAY_BUFFER_BINDING, previous_buffer); |
+ if (!vertex_array_enabled) |
+ glDisableVertexAttribArray(0); |
+ |
+ return true; |
+} |
+ |
+void GLImageIOSurface::I420vToRGBA::Destroy() { |
+ glDeleteProgram(program_object_); |
+ glDeleteShader(vertex_shader_); |
+ glDeleteShader(fragment_shader_); |
+ glDeleteBuffersARB(1, &vertex_buffer_); |
+ glDeleteFramebuffersEXT(1, &framebuffer_object_); |
+} |
+ |
GLImageIOSurface::GLImageIOSurface(const Size& size, unsigned internalformat) |
: size_(size), |
internalformat_(internalformat), |
@@ -179,6 +334,9 @@ bool GLImageIOSurface::Initialize(IOSurfaceRef io_surface, |
void GLImageIOSurface::Destroy(bool have_context) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (have_context && i420v_to_RGBA_) { |
+ i420v_to_RGBA_->Destroy(); |
reveman
2015/10/27 19:31:54
without looking at what this does, it's confusing
Daniele Castagna
2015/10/29 20:09:04
Removed the class as suggested in the comment belo
|
+ } |
io_surface_.reset(); |
} |
@@ -190,6 +348,10 @@ unsigned GLImageIOSurface::GetInternalFormat() { return internalformat_; } |
bool GLImageIOSurface::BindTexImage(unsigned target) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ 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. |
@@ -214,7 +376,67 @@ 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) |
+ return false; |
+ |
+ if (!i420v_to_RGBA_) { |
reveman
2015/10/27 19:31:54
I would prefer to see all this state just part of
Daniele Castagna
2015/10/29 20:09:04
Done.
|
+ i420v_to_RGBA_.reset(new I420vToRGBA()); |
+ if (!i420v_to_RGBA_->Initialize()) { |
+ LOG(ERROR) |
+ << "Can't initialize GL structures for 420v to RGBA conversion"; |
+ return false; |
+ } |
+ } |
+ |
+ DCHECK(target == GL_TEXTURE_2D); |
reveman
2015/10/27 19:31:54
What prevents a client from using a different targ
Daniele Castagna
2015/10/29 20:09:04
Changed that to LOG(ERROR) ... + return false.
|
+ CGLContextObj cgl_context = |
+ static_cast<CGLContextObj>(GLContext::GetCurrent()->GetHandle()); |
+ |
+ GLuint yuv_textures[2]; |
+ glGenTextures(2, yuv_textures); |
+ DCHECK(yuv_textures[0] && yuv_textures[1]); |
+ |
+ GLint service_texture = 0; |
+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &service_texture); |
+ DCHECK(service_texture); |
+ |
+ CGLError cgl_error = kCGLNoError; |
+ { |
+ ScopedTextureBinder b(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; |
+ } |
+ |
+ { |
+ ScopedTextureBinder b(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; |
+ } |
+ |
+ if (!i420v_to_RGBA_->DrawToTexture(size_, yuv_textures, service_texture, |
+ target)) { |
+ LOG(ERROR) << "Can't draw 420v IOSurface to RGBA texture."; |
+ glDeleteTextures(2, yuv_textures); |
+ return false; |
+ } |
+ |
+ return true; |
} |
bool GLImageIOSurface::CopyTexSubImage(unsigned target, |