OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/gpu/media/gles2_external_texture_copier.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace content { |
| 10 |
| 11 static void checkGlError(const char* op) { |
| 12 for (GLint error = glGetError(); error; error = glGetError()) { |
| 13 LOG(ERROR) << "after " << op <<" glError (0x" << std::hex << error << ")"; |
| 14 NOTREACHED(); |
| 15 } |
| 16 } |
| 17 |
| 18 static const char g_vertex_shader[] = |
| 19 "uniform mat4 uMVPMatrix;\n" |
| 20 "uniform mat4 uSTMatrix;\n" |
| 21 "attribute vec4 aPosition;\n" |
| 22 "attribute vec4 aTextureCoord;\n" |
| 23 "varying vec2 vTextureCoord;\n" |
| 24 "void main() {\n" |
| 25 " gl_Position = uMVPMatrix * aPosition;\n" |
| 26 " vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" |
| 27 "}\n"; |
| 28 |
| 29 static const char g_fragment_shader[] = |
| 30 "#extension GL_OES_EGL_image_external : require\n" |
| 31 "precision mediump float;\n" |
| 32 "varying vec2 vTextureCoord;\n" |
| 33 "uniform samplerExternalOES sTexture;\n" |
| 34 "void main() {\n" |
| 35 " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" |
| 36 "}\n"; |
| 37 |
| 38 enum { kVerticeStride = 5 * sizeof(float) }; |
| 39 |
| 40 static const GLfloat g_vertices[] = { |
| 41 -1.0f, -1.0f, 0, 0.f, 0.f, |
| 42 1.0f, -1.0f, 0, 1.f, 0.f, |
| 43 -1.0f, 1.0f, 0, 0.f, 1.f, |
| 44 1.0f, 1.0f, 0, 1.f, 1.f, |
| 45 }; |
| 46 |
| 47 // Due to the absence of matrix functions in NDK, a pre-calculated matrix |
| 48 // is used. g_mvp_matrix = P * L * I |
| 49 // Matrix.setLookAtM(L, 0, 0, 0, 2, 0, 0, 0, 0, -1, 0); |
| 50 // Matrix.orthoM(P, 0, -1, 1, -1, 1, 1, 3); |
| 51 // Matrix.setIdentityM(I, 0); |
| 52 // XXX(dwkang): currently, (0, -1, 0) is used for the up vector. |
| 53 // figure out why up vector (0, 1, 0) generates flipped screen. |
| 54 static const GLfloat g_mvp_matrix[] = { |
| 55 1.f, 0.f, 0.f, 0.f, |
| 56 0.f, -1.f, 0.f, 0.f, |
| 57 0.f, 0.f, -1.f, 0.f, |
| 58 0.f, 0.f, 0.f, 1.f, |
| 59 }; |
| 60 |
| 61 static GLuint LoadShader(GLenum shaderType, const char* pSource) { |
| 62 GLuint shader = glCreateShader(shaderType); |
| 63 if (shader) { |
| 64 glShaderSource(shader, 1, &pSource, NULL); |
| 65 glCompileShader(shader); |
| 66 GLint compiled = 0; |
| 67 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
| 68 if (!compiled) { |
| 69 GLint infoLen = 0; |
| 70 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); |
| 71 if (infoLen) { |
| 72 char* buf = (char*) malloc(infoLen); |
| 73 if (buf) { |
| 74 glGetShaderInfoLog(shader, infoLen, NULL, buf); |
| 75 LOG(ERROR) << "Could not compile shader : " << buf; |
| 76 free(buf); |
| 77 } |
| 78 glDeleteShader(shader); |
| 79 shader = 0; |
| 80 } |
| 81 } |
| 82 } |
| 83 return shader; |
| 84 } |
| 85 |
| 86 static GLuint CreateProgram( |
| 87 const char* pVertexSource, const char* pFragmentSource) { |
| 88 GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, pVertexSource); |
| 89 if (!vertexShader) { |
| 90 return 0; |
| 91 } |
| 92 |
| 93 GLuint pixelShader = LoadShader(GL_FRAGMENT_SHADER, pFragmentSource); |
| 94 if (!pixelShader) { |
| 95 return 0; |
| 96 } |
| 97 |
| 98 GLuint program = glCreateProgram(); |
| 99 if (program) { |
| 100 glAttachShader(program, vertexShader); |
| 101 checkGlError("glAttachShader"); |
| 102 glAttachShader(program, pixelShader); |
| 103 checkGlError("glAttachShader"); |
| 104 glLinkProgram(program); |
| 105 GLint linkStatus = GL_FALSE; |
| 106 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 107 if (linkStatus != GL_TRUE) { |
| 108 GLint bufLength = 0; |
| 109 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); |
| 110 if (bufLength) { |
| 111 char* buf = (char*) malloc(bufLength); |
| 112 if (buf) { |
| 113 glGetProgramInfoLog(program, bufLength, NULL, buf); |
| 114 LOG(ERROR) << "Could not link program: "<< buf; |
| 115 free(buf); |
| 116 } |
| 117 } |
| 118 glDeleteProgram(program); |
| 119 program = 0; |
| 120 } |
| 121 } |
| 122 return program; |
| 123 } |
| 124 |
| 125 Gles2ExternalTextureCopier::Gles2ExternalTextureCopier() |
| 126 : initialized_(false), |
| 127 width_(0), |
| 128 height_(0), |
| 129 framebuffer_id_(0), |
| 130 renderbuffer_id_(0), |
| 131 program_(0), |
| 132 position_handle_(0), |
| 133 st_matrix_handle_(0), |
| 134 mvp_matrix_handle_(0), |
| 135 texture_handle_(0) { |
| 136 memset(st_matrix_, 0, sizeof(st_matrix_)); |
| 137 } |
| 138 |
| 139 Gles2ExternalTextureCopier::~Gles2ExternalTextureCopier() { |
| 140 if (initialized_) { |
| 141 glDeleteFramebuffers(1, &framebuffer_id_); |
| 142 glDeleteRenderbuffers(1, &renderbuffer_id_); |
| 143 } |
| 144 } |
| 145 |
| 146 bool Gles2ExternalTextureCopier::SetupGraphics() { |
| 147 program_ = CreateProgram(g_vertex_shader, g_fragment_shader); |
| 148 if (!program_) { |
| 149 LOG(ERROR) << "Could not create program."; |
| 150 return false; |
| 151 } |
| 152 position_handle_ = glGetAttribLocation(program_, "aPosition"); |
| 153 checkGlError("glGetAttribLocation"); |
| 154 |
| 155 texture_handle_ = glGetAttribLocation(program_, "aTextureCoord"); |
| 156 checkGlError("glGetAttribLocation aTextureCoord"); |
| 157 |
| 158 mvp_matrix_handle_ = glGetUniformLocation(program_, "uMVPMatrix"); |
| 159 checkGlError("glGetUniformLocation uMVPMatrix"); |
| 160 |
| 161 st_matrix_handle_ = glGetUniformLocation(program_, "uSTMatrix"); |
| 162 checkGlError("glGetUniformLocation uSTMatrix"); |
| 163 |
| 164 return true; |
| 165 } |
| 166 |
| 167 void Gles2ExternalTextureCopier::RenderFrame( |
| 168 int w, int h, GLuint texture_id, const float transfrom_matrix[16]) { |
| 169 glViewport(0, 0, w, h); |
| 170 checkGlError("glViewport"); |
| 171 |
| 172 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 173 checkGlError("glClear"); |
| 174 |
| 175 glUseProgram(program_); |
| 176 checkGlError("glUseProgram"); |
| 177 |
| 178 glActiveTexture(GL_TEXTURE0); |
| 179 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); |
| 180 checkGlError("glBindTexture"); |
| 181 |
| 182 glVertexAttribPointer( |
| 183 position_handle_, 3, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices); |
| 184 checkGlError("glVertexAttribPointer vPositionHandle"); |
| 185 glEnableVertexAttribArray(position_handle_); |
| 186 checkGlError("glEnableVertexAttribArray vPositionHandle"); |
| 187 |
| 188 glVertexAttribPointer( |
| 189 texture_handle_, 2, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices + 3); |
| 190 checkGlError("glVertexAttribPointer aTextureHandle"); |
| 191 glEnableVertexAttribArray(texture_handle_); |
| 192 checkGlError("glEnableVertexAttribArray aTextureHandle"); |
| 193 |
| 194 glUniformMatrix4fv(mvp_matrix_handle_, 1, GL_FALSE, g_mvp_matrix); |
| 195 checkGlError("glUniformMatrix4fv uMVPMatrixHandle"); |
| 196 |
| 197 glUniformMatrix4fv(st_matrix_handle_, 1, GL_FALSE, transfrom_matrix); |
| 198 checkGlError("glUniformMatrix4fv uSTMatrixHandle"); |
| 199 |
| 200 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 201 checkGlError("glDrawArrays"); |
| 202 } |
| 203 |
| 204 bool Gles2ExternalTextureCopier::Init(int32 width, int32 height) { |
| 205 if (initialized_) { |
| 206 LOG(ERROR) << "Init should not be called twice."; |
| 207 return false; |
| 208 } |
| 209 |
| 210 width_ = width; |
| 211 height_ = height; |
| 212 |
| 213 SetupGraphics(); |
| 214 if (!SetupFrameBuffer()) { |
| 215 LOG(ERROR) << "Failed to create a framebuffer."; |
| 216 return false; |
| 217 } |
| 218 |
| 219 initialized_ = true; |
| 220 return initialized_; |
| 221 } |
| 222 |
| 223 bool Gles2ExternalTextureCopier::Copy( |
| 224 GLuint source_texture_id, GLuint destination_texture_id, |
| 225 const float transfrom_matrix[16]) { |
| 226 if (!initialized_) { |
| 227 return false; |
| 228 } |
| 229 |
| 230 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); |
| 231 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 232 destination_texture_id, 0); |
| 233 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 234 if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 235 LOG(ERROR) << "Framebuffer is not complete: " << status; |
| 236 return false; |
| 237 } |
| 238 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); |
| 239 checkGlError("glBindFramebuffer framebuffer_id_"); |
| 240 RenderFrame(width_, height_, source_texture_id, transfrom_matrix); |
| 241 glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 242 checkGlError("glBindFramebuffer 0"); |
| 243 |
| 244 return true; |
| 245 } |
| 246 |
| 247 bool Gles2ExternalTextureCopier::SetupFrameBuffer() { |
| 248 GLuint framebuffer; |
| 249 glGenFramebuffers(1, &framebuffer); |
| 250 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 251 |
| 252 GLuint depthbuffer; |
| 253 glGenRenderbuffers(1, &depthbuffer); |
| 254 |
| 255 glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer); |
| 256 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width_, height_); |
| 257 glFramebufferRenderbuffer( |
| 258 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); |
| 259 checkGlError("glFramebufferRenderbuffer"); |
| 260 |
| 261 glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 262 framebuffer_id_ = framebuffer; |
| 263 renderbuffer_id_ = depthbuffer; |
| 264 return true; |
| 265 } |
| 266 |
| 267 } // namespace content |
OLD | NEW |