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 static const int 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) return 0; | |
Ami GONE FROM CHROMIUM
2013/02/07 19:40:16
This is counter to the chromium style guide which
dwkang1
2013/02/13 12:57:19
Done.
| |
90 | |
91 GLuint pixelShader = LoadShader(GL_FRAGMENT_SHADER, pFragmentSource); | |
92 if (!pixelShader) return 0; | |
93 | |
94 GLuint program = glCreateProgram(); | |
95 if (program) { | |
96 glAttachShader(program, vertexShader); | |
97 checkGlError("glAttachShader"); | |
98 glAttachShader(program, pixelShader); | |
99 checkGlError("glAttachShader"); | |
100 glLinkProgram(program); | |
101 GLint linkStatus = GL_FALSE; | |
102 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); | |
103 if (linkStatus != GL_TRUE) { | |
104 GLint bufLength = 0; | |
105 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); | |
106 if (bufLength) { | |
107 char* buf = (char*) malloc(bufLength); | |
108 if (buf) { | |
109 glGetProgramInfoLog(program, bufLength, NULL, buf); | |
110 LOG(ERROR) << "Could not link program: "<< buf; | |
111 free(buf); | |
112 } | |
113 } | |
114 glDeleteProgram(program); | |
115 program = 0; | |
116 } | |
117 } | |
118 return program; | |
119 } | |
120 | |
121 Gles2ExternalTextureCopier::Gles2ExternalTextureCopier() | |
122 : initialized_(false), | |
123 width_(0), | |
124 height_(0), | |
125 framebuffer_id_(0), | |
126 renderbuffer_id_(0), | |
127 program_(0), | |
128 position_handle_(0), | |
129 st_matrix_handle_(0), | |
130 mvp_matrix_handle_(0), | |
131 texture_handle_(0) { | |
132 memset(st_matrix_, 0, sizeof(st_matrix_)); | |
133 } | |
134 | |
135 Gles2ExternalTextureCopier::~Gles2ExternalTextureCopier() { | |
136 if (initialized_) { | |
137 glDeleteFramebuffers(1, &framebuffer_id_); | |
138 glDeleteRenderbuffers(1, &renderbuffer_id_); | |
139 } | |
140 } | |
141 | |
142 bool Gles2ExternalTextureCopier::SetupGraphics() { | |
143 program_ = CreateProgram(g_vertex_shader, g_fragment_shader); | |
144 if (!program_) { | |
145 LOG(ERROR) << "Could not create program."; | |
146 return false; | |
147 } | |
148 position_handle_ = glGetAttribLocation(program_, "aPosition"); | |
149 checkGlError("glGetAttribLocation"); | |
150 | |
151 texture_handle_ = glGetAttribLocation(program_, "aTextureCoord"); | |
152 checkGlError("glGetAttribLocation aTextureCoord"); | |
153 | |
154 mvp_matrix_handle_ = glGetUniformLocation(program_, "uMVPMatrix"); | |
155 checkGlError("glGetUniformLocation uMVPMatrix"); | |
156 | |
157 st_matrix_handle_ = glGetUniformLocation(program_, "uSTMatrix"); | |
158 checkGlError("glGetUniformLocation uSTMatrix"); | |
159 | |
160 return true; | |
161 } | |
162 | |
163 void Gles2ExternalTextureCopier::RenderFrame( | |
164 int w, int h, GLuint texture_id, const float transfrom_matrix[16]) { | |
165 glViewport(0, 0, w, h); | |
166 checkGlError("glViewport"); | |
167 | |
168 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | |
169 checkGlError("glClear"); | |
170 | |
171 glUseProgram(program_); | |
172 checkGlError("glUseProgram"); | |
173 | |
174 glActiveTexture(GL_TEXTURE0); | |
175 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); | |
176 checkGlError("glBindTexture"); | |
177 | |
178 glVertexAttribPointer( | |
179 position_handle_, 3, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices); | |
180 checkGlError("glVertexAttribPointer vPositionHandle"); | |
181 glEnableVertexAttribArray(position_handle_); | |
182 checkGlError("glEnableVertexAttribArray vPositionHandle"); | |
183 | |
184 glVertexAttribPointer( | |
185 texture_handle_, 2, GL_FLOAT, GL_FALSE, kVerticeStride, g_vertices + 3); | |
186 checkGlError("glVertexAttribPointer aTextureHandle"); | |
187 glEnableVertexAttribArray(texture_handle_); | |
188 checkGlError("glEnableVertexAttribArray aTextureHandle"); | |
189 | |
190 glUniformMatrix4fv(mvp_matrix_handle_, 1, GL_FALSE, g_mvp_matrix); | |
191 checkGlError("glUniformMatrix4fv uMVPMatrixHandle"); | |
192 | |
193 glUniformMatrix4fv(st_matrix_handle_, 1, GL_FALSE, transfrom_matrix); | |
194 checkGlError("glUniformMatrix4fv uSTMatrixHandle"); | |
195 | |
196 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
197 checkGlError("glDrawArrays"); | |
198 } | |
199 | |
200 bool Gles2ExternalTextureCopier::Init(int32 width, int32 height) { | |
201 if (initialized_) { | |
202 LOG(ERROR) << "Init should not be called twice."; | |
203 return false; | |
204 } | |
205 | |
206 width_ = width; | |
207 height_ = height; | |
208 | |
209 SetupGraphics(); | |
210 if (!SetupFrameBuffer()) { | |
211 LOG(ERROR) << "Failed to create a framebuffer."; | |
212 return false; | |
213 } | |
214 | |
215 initialized_ = true; | |
216 return initialized_; | |
217 } | |
218 | |
219 bool Gles2ExternalTextureCopier::Copy( | |
220 GLuint source_texture_id, GLuint destination_texture_id, | |
221 const float transfrom_matrix[16]) { | |
222 if (!initialized_) return false; | |
223 | |
224 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | |
225 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | |
226 destination_texture_id, 0); | |
227 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); | |
228 if (status != GL_FRAMEBUFFER_COMPLETE) { | |
229 LOG(ERROR) << "Framebuffer is not complete: " << status; | |
230 return false; | |
231 } | |
232 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | |
233 checkGlError("glBindFramebuffer framebuffer_id_"); | |
234 RenderFrame(width_, height_, source_texture_id, transfrom_matrix); | |
235 glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
236 checkGlError("glBindFramebuffer 0"); | |
237 | |
238 return true; | |
239 } | |
240 | |
241 bool Gles2ExternalTextureCopier::SetupFrameBuffer() { | |
242 GLuint framebuffer; | |
243 glGenFramebuffers(1, &framebuffer); | |
244 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
245 | |
246 GLuint depthbuffer; | |
247 glGenRenderbuffers(1, &depthbuffer); | |
248 | |
249 glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer); | |
250 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width_, height_); | |
251 glFramebufferRenderbuffer( | |
252 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); | |
253 checkGlError("glFramebufferRenderbuffer"); | |
254 | |
255 glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
256 framebuffer_id_ = framebuffer; | |
257 renderbuffer_id_ = depthbuffer; | |
258 return true; | |
259 } | |
260 | |
261 } // namespace content | |
OLD | NEW |