Chromium Code Reviews| Index: content/common/gpu/media/gles2_external_texture_copier.cc |
| diff --git a/content/common/gpu/media/gles2_external_texture_copier.cc b/content/common/gpu/media/gles2_external_texture_copier.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b4b4f2a2a928d66af59b77403b5a9a543e96c5d8 |
| --- /dev/null |
| +++ b/content/common/gpu/media/gles2_external_texture_copier.cc |
| @@ -0,0 +1,283 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/common/gpu/media/gles2_external_texture_copier.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace content { |
| + |
| +static void CheckGlError(const char* op) { |
| + for (GLint error = glGetError(); error; error = glGetError()) { |
| + LOG(ERROR) << "after " << op <<" glError (0x" << std::hex << error << ")"; |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +static const char g_vertex_shader[] = |
| + "uniform mat4 uMVPMatrix;\n" |
| + "uniform mat4 uSTMatrix;\n" |
| + "attribute vec4 aPosition;\n" |
| + "attribute vec4 aTextureCoord;\n" |
| + "varying vec2 vTextureCoord;\n" |
| + "void main() {\n" |
| + " gl_Position = uMVPMatrix * aPosition;\n" |
| + " vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" |
| + "}\n"; |
| + |
| +static const char g_fragment_shader[] = |
| + "#extension GL_OES_EGL_image_external : require\n" |
| + "precision mediump float;\n" |
| + "varying vec2 vTextureCoord;\n" |
| + "uniform samplerExternalOES sTexture;\n" |
| + "void main() {\n" |
| + " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" |
| + "}\n"; |
| + |
| +static const int kVerticeStride = 5 * sizeof(float); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
English: The singular of "vertices" is "vertex" ;)
|
| + |
| +static const GLfloat g_vertices[] = { |
| + -1.0f, -1.0f, 0, 0.f, 0.f, |
| + 1.0f, -1.0f, 0, 1.f, 0.f, |
| + -1.0f, 1.0f, 0, 0.f, 1.f, |
| + 1.0f, 1.0f, 0, 1.f, 1.f, |
| +}; |
| + |
| +// Due to the absence of matrix functions in NDK, a pre-calculated matrix |
| +// is used. g_mvp_matrix = P * L * I |
| +// Matrix.setLookAtM(L, 0, 0, 0, 2, 0, 0, 0, 0, -1, 0); |
| +// Matrix.orthoM(P, 0, -1, 1, -1, 1, 1, 3); |
| +// Matrix.setIdentityM(I, 0); |
| +// Up vector(0, -1, 0) is used instead of (0, 1, 0) to prevent flipped screen. |
| +static const GLfloat g_mvp_matrix[] = { |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
My glFoo is super-weak, but I believe what you hav
|
| + 1.f, 0.f, 0.f, 0.f, |
| + 0.f, -1.f, 0.f, 0.f, |
| + 0.f, 0.f, -1.f, 0.f, |
| + 0.f, 0.f, 0.f, 1.f, |
| +}; |
| + |
| +static GLuint LoadShader(GLenum shaderType, const char* pSource) { |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
here and elsewhere, unix_style, not camelCase for
|
| + GLuint shader = glCreateShader(shaderType); |
| + if (shader) { |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
nit: if you reversed the test you could early-retu
|
| + glShaderSource(shader, 1, &pSource, NULL); |
| + glCompileShader(shader); |
| + GLint compiled = 0; |
| + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
| + if (!compiled) { |
| + GLint infoLen = 0; |
| + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); |
| + if (infoLen) { |
| + char* buf = (char*) malloc(infoLen); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
scoped_ptr
|
| + if (buf) { |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
unnecessary; malloc/new never fail in chromium oth
|
| + glGetShaderInfoLog(shader, infoLen, NULL, buf); |
| + LOG(ERROR) << "Could not compile shader : " << buf; |
| + free(buf); |
| + } |
| + glDeleteShader(shader); |
| + shader = 0; |
| + } |
| + } |
| + } |
| + return shader; |
| +} |
| + |
| +static GLuint CreateProgram( |
| + const char* pVertexSource, const char* pFragmentSource) { |
| + GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, pVertexSource); |
| + if (!vertexShader) |
| + return 0; |
| + |
| + GLuint pixelShader = LoadShader(GL_FRAGMENT_SHADER, pFragmentSource); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
s/pixel/fragment/ ?
(here and below)
|
| + if (!pixelShader) |
| + return 0; |
| + |
| + GLuint program = glCreateProgram(); |
| + if (program) { |
| + glAttachShader(program, vertexShader); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
delete the shaders after attaching them (so progra
|
| + CheckGlError("glAttachShader"); |
| + glAttachShader(program, pixelShader); |
| + CheckGlError("glAttachShader"); |
| + glLinkProgram(program); |
| + GLint linkStatus = GL_FALSE; |
| + glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| + if (linkStatus != GL_TRUE) { |
| + GLint bufLength = 0; |
| + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); |
| + if (bufLength) { |
| + char* buf = (char*) malloc(bufLength); |
| + if (buf) { |
| + glGetProgramInfoLog(program, bufLength, NULL, buf); |
| + LOG(ERROR) << "Could not link program: "<< buf; |
| + free(buf); |
| + } |
| + } |
| + glDeleteProgram(program); |
| + program = 0; |
| + } |
| + } |
| + return program; |
| +} |
| + |
| +Gles2ExternalTextureCopier::Gles2ExternalTextureCopier() |
| + : initialized_(false), |
| + width_(0), |
| + height_(0), |
| + framebuffer_id_(0), |
| + renderbuffer_id_(0), |
| + program_(0), |
| + position_handle_(0), |
| + st_matrix_handle_(0), |
| + mvp_matrix_handle_(0), |
| + texture_handle_(0), |
| + previous_framebuffer_id_(0), |
| + previous_renderbuffer_id_(0), |
| + previous_texture_id_(0) { |
| +} |
| + |
| +Gles2ExternalTextureCopier::~Gles2ExternalTextureCopier() { |
| + if (initialized_) { |
| + glDeleteFramebuffersEXT(1, &framebuffer_id_); |
| + glDeleteRenderbuffersEXT(1, &renderbuffer_id_); |
| + } |
| +} |
| + |
| +bool Gles2ExternalTextureCopier::SetupGraphics() { |
| + program_ = CreateProgram(g_vertex_shader, g_fragment_shader); |
| + if (!program_) { |
| + LOG(ERROR) << "Could not create program."; |
| + return false; |
| + } |
| + position_handle_ = glGetAttribLocation(program_, "aPosition"); |
| + CheckGlError("glGetAttribLocation"); |
| + |
| + texture_handle_ = glGetAttribLocation(program_, "aTextureCoord"); |
| + CheckGlError("glGetAttribLocation aTextureCoord"); |
| + |
| + mvp_matrix_handle_ = glGetUniformLocation(program_, "uMVPMatrix"); |
| + CheckGlError("glGetUniformLocation uMVPMatrix"); |
| + |
| + st_matrix_handle_ = glGetUniformLocation(program_, "uSTMatrix"); |
| + CheckGlError("glGetUniformLocation uSTMatrix"); |
| + |
| + return true; |
| +} |
| + |
| +void Gles2ExternalTextureCopier::RenderFrame( |
| + int w, int h, GLuint texture_id, const float transfrom_matrix[16]) { |
| + glViewport(0, 0, w, h); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
I suspect this can go in a Setup method
|
| + CheckGlError("glViewport"); |
| + |
| + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
why DEPTH?
(IOW, why allocate a depthbuffer later?
|
| + CheckGlError("glClear"); |
| + |
| + glUseProgram(program_); |
| + CheckGlError("glUseProgram"); |
| + |
| + glActiveTexture(GL_TEXTURE0); |
| + glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); |
| + CheckGlError("glBindTexture"); |
| + |
| + glVertexAttribPointer( |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
l.181-195 can probably be done only once, right?
|
| + position_handle_, 3, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices); |
| + CheckGlError("glVertexAttribPointer vPositionHandle"); |
| + glEnableVertexAttribArray(position_handle_); |
| + CheckGlError("glEnableVertexAttribArray vPositionHandle"); |
| + |
| + glVertexAttribPointer( |
| + texture_handle_, 2, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices + 3); |
| + CheckGlError("glVertexAttribPointer aTextureHandle"); |
| + glEnableVertexAttribArray(texture_handle_); |
| + CheckGlError("glEnableVertexAttribArray aTextureHandle"); |
| + |
| + glUniformMatrix4fv(mvp_matrix_handle_, 1, GL_FALSE, g_mvp_matrix); |
| + CheckGlError("glUniformMatrix4fv uMVPMatrixHandle"); |
| + |
| + glUniformMatrix4fv(st_matrix_handle_, 1, GL_FALSE, transfrom_matrix); |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
Part of me wants to just ignore transform_matrix a
|
| + CheckGlError("glUniformMatrix4fv uSTMatrixHandle"); |
| + |
| + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| + CheckGlError("glDrawArrays"); |
| +} |
| + |
| +bool Gles2ExternalTextureCopier::Init(int32 width, int32 height) { |
| + if (initialized_) { |
| + LOG(ERROR) << "Init should not be called twice."; |
|
Ami GONE FROM CHROMIUM
2013/02/17 00:12:44
Since this is the only failure mode of Init(), I t
|
| + return false; |
| + } |
| + |
| + width_ = width; |
| + height_ = height; |
| + |
| + SaveState(); |
| + SetupGraphics(); |
| + SetupFrameBuffer(); |
| + RestoreState(); |
| + |
| + initialized_ = true; |
| + return initialized_; |
| +} |
| + |
| +bool Gles2ExternalTextureCopier::Copy( |
| + GLuint source_texture_id, GLuint destination_texture_id, |
| + const float transfrom_matrix[16]) { |
| + if (!initialized_) |
| + return false; |
| + |
| + SaveState(); |
| + glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_id_); |
| + glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| + destination_texture_id, 0); |
| + GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); |
| + if (status != GL_FRAMEBUFFER_COMPLETE) { |
| + LOG(ERROR) << "Framebuffer is not complete: " << status; |
| + RestoreState(); |
| + return false; |
| + } |
| + CheckGlError("glBindFramebuffer framebuffer_id_"); |
| + RenderFrame(width_, height_, source_texture_id, transfrom_matrix); |
| + RestoreState(); |
| + |
| + return true; |
| +} |
| + |
| + |
| +bool Gles2ExternalTextureCopier::SetupFrameBuffer() { |
| + GLuint framebuffer; |
| + glGenFramebuffersEXT(1, &framebuffer); |
| + glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
| + |
| + GLuint depthbuffer; |
| + glGenRenderbuffersEXT(1, &depthbuffer); |
| + |
| + glBindRenderbufferEXT(GL_RENDERBUFFER, depthbuffer); |
| + glRenderbufferStorageEXT( |
| + GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width_, height_); |
| + glFramebufferRenderbufferEXT( |
| + GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); |
| + CheckGlError("glFramebufferRenderbuffer"); |
| + |
| + framebuffer_id_ = framebuffer; |
| + renderbuffer_id_ = depthbuffer; |
| + return true; |
| +} |
| + |
| +void Gles2ExternalTextureCopier::SaveState() { |
| + glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *)&previous_framebuffer_id_); |
| + CheckGlError("glGetIntegerv previous_framebuffer_id"); |
| + glGetIntegerv(GL_RENDERBUFFER_BINDING, (GLint *)&previous_renderbuffer_id_); |
| + CheckGlError("glGetIntegerv previous_renderbuffer_id_"); |
| + glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&previous_texture_id_); |
| + CheckGlError("glGetIntegerv previous_texture_id_"); |
| +} |
| + |
| +void Gles2ExternalTextureCopier::RestoreState() { |
| + glBindFramebufferEXT(GL_FRAMEBUFFER, previous_framebuffer_id_); |
| + CheckGlError("glBindFramebuffer previous_framebuffer_id"); |
| + glBindRenderbufferEXT(GL_RENDERBUFFER, previous_renderbuffer_id_); |
| + CheckGlError("glBindFramebuffer previous_renderbuffer_id_"); |
| + glBindTexture(GL_TEXTURE_2D, previous_texture_id_); |
| + CheckGlError("glBindTexture previous_texture_id_"); |
| +} |
| + |
| +} // namespace content |