Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.cc

Issue 374193002: gpu: Optimize and cleanup code used for CHROMIUM_copy_texture. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: DoCopyTexture() branchs into fast path Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h" 5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "gpu/command_buffer/service/gl_utils.h" 10 #include "gpu/command_buffer/service/gl_utils.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 return shader_ids[index][SAMPLER_2D]; 160 return shader_ids[index][SAMPLER_2D];
161 case GL_TEXTURE_RECTANGLE_ARB: 161 case GL_TEXTURE_RECTANGLE_ARB:
162 return shader_ids[index][SAMPLER_RECTANGLE_ARB]; 162 return shader_ids[index][SAMPLER_RECTANGLE_ARB];
163 case GL_TEXTURE_EXTERNAL_OES: 163 case GL_TEXTURE_EXTERNAL_OES:
164 return shader_ids[index][SAMPLER_EXTERNAL_OES]; 164 return shader_ids[index][SAMPLER_EXTERNAL_OES];
165 default: 165 default:
166 break; 166 break;
167 } 167 }
168 168
169 NOTREACHED(); 169 NOTREACHED();
170 return shader_ids[index][SAMPLER_2D]; 170 return shader_ids[0][SAMPLER_2D];
171 } 171 }
172 172
173 void CompileShader(GLuint shader, const char* shader_source) { 173 void CompileShader(GLuint shader, const char* shader_source) {
174 glShaderSource(shader, 1, &shader_source, 0); 174 glShaderSource(shader, 1, &shader_source, 0);
175 glCompileShader(shader); 175 glCompileShader(shader);
176 #ifndef NDEBUG 176 #ifndef NDEBUG
177 GLint compile_status; 177 GLint compile_status;
178 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); 178 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
179 if (GL_TRUE != compile_status) 179 if (GL_TRUE != compile_status)
180 DLOG(ERROR) << "CopyTextureCHROMIUM: shader compilation failure."; 180 DLOG(ERROR) << "CopyTextureCHROMIUM: shader compilation failure.";
181 #endif 181 #endif
182 } 182 }
183 183
184 void DeleteShader(GLuint shader) { 184 void DeleteShader(GLuint shader) {
185 if (shader) 185 if (shader)
186 glDeleteShader(shader); 186 glDeleteShader(shader);
187 } 187 }
188 188
189 } // namespace 189 } // namespace
190 190
191 namespace gpu { 191 namespace gpu {
192 192
193 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager() 193 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager()
194 : initialized_(false), 194 : initialized_(false),
195 vertex_shaders_(NUM_VERTEX_SHADERS, 0u), 195 vertex_shaders_(NUM_VERTEX_SHADERS, 0u),
196 fragment_shaders_(NUM_FRAGMENT_SHADERS, 0u), 196 fragment_shaders_(NUM_FRAGMENT_SHADERS, 0u),
197 buffer_id_(0u), 197 buffer_id_(0u),
198 framebuffer_(0u) {} 198 framebuffer_(0u) {}
199 199
200 CopyTextureCHROMIUMResourceManager::~CopyTextureCHROMIUMResourceManager() {} 200 CopyTextureCHROMIUMResourceManager::~CopyTextureCHROMIUMResourceManager() {
201 DCHECK(!buffer_id_ && !framebuffer_);
202 }
201 203
202 void CopyTextureCHROMIUMResourceManager::Initialize( 204 void CopyTextureCHROMIUMResourceManager::Initialize(
203 const gles2::GLES2Decoder* decoder) { 205 const gles2::GLES2Decoder* decoder) {
204 COMPILE_ASSERT( 206 COMPILE_ASSERT(
205 kVertexPositionAttrib == 0u, 207 kVertexPositionAttrib == 0u,
206 Position_attribs_must_be_0); 208 Position_attribs_must_be_0);
209 DCHECK(!buffer_id_ && !framebuffer_ && programs_.empty());
207 210
208 // Initialize all of the GPU resources required to perform the copy. 211 // Initialize all of the GPU resources required to perform the copy.
209 glGenBuffersARB(1, &buffer_id_); 212 glGenBuffersARB(1, &buffer_id_);
210 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); 213 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
211 const GLfloat kQuadVertices[] = {-1.0f, -1.0f, 214 const GLfloat kQuadVertices[] = {-1.0f, -1.0f,
212 1.0f, -1.0f, 215 1.0f, -1.0f,
213 1.0f, 1.0f, 216 1.0f, 1.0f,
214 -1.0f, 1.0f}; 217 -1.0f, 1.0f};
215 glBufferData( 218 glBufferData(
216 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW); 219 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
217 220
218 glGenFramebuffersEXT(1, &framebuffer_); 221 glGenFramebuffersEXT(1, &framebuffer_);
219 222
220 decoder->RestoreBufferBindings(); 223 decoder->RestoreBufferBindings();
221 224
222 initialized_ = true; 225 initialized_ = true;
223 } 226 }
224 227
225 void CopyTextureCHROMIUMResourceManager::Destroy() { 228 void CopyTextureCHROMIUMResourceManager::Destroy() {
226 if (!initialized_) 229 if (!initialized_)
227 return; 230 return;
228 231
229 glDeleteFramebuffersEXT(1, &framebuffer_); 232 glDeleteFramebuffersEXT(1, &framebuffer_);
233 framebuffer_ = 0;
230 234
231 std::for_each(vertex_shaders_.begin(), vertex_shaders_.end(), DeleteShader); 235 std::for_each(vertex_shaders_.begin(), vertex_shaders_.end(), DeleteShader);
232 std::for_each( 236 std::for_each(
233 fragment_shaders_.begin(), fragment_shaders_.end(), DeleteShader); 237 fragment_shaders_.begin(), fragment_shaders_.end(), DeleteShader);
234 238
235 for (ProgramMap::const_iterator it = programs_.begin(); it != programs_.end(); 239 for (ProgramMap::const_iterator it = programs_.begin(); it != programs_.end();
236 ++it) { 240 ++it) {
237 const ProgramInfo& info = it->second; 241 const ProgramInfo& info = it->second;
238 glDeleteProgram(info.program); 242 glDeleteProgram(info.program);
239 } 243 }
240 244
241 glDeleteBuffersARB(1, &buffer_id_); 245 glDeleteBuffersARB(1, &buffer_id_);
246 buffer_id_ = 0;
242 } 247 }
243 248
244 void CopyTextureCHROMIUMResourceManager::DoCopyTexture( 249 void CopyTextureCHROMIUMResourceManager::DoCopyTexture(
245 const gles2::GLES2Decoder* decoder, 250 const gles2::GLES2Decoder* decoder,
246 GLenum source_target, 251 GLenum source_target,
247 GLenum dest_target,
248 GLuint source_id, 252 GLuint source_id,
249 GLuint dest_id, 253 GLuint dest_id,
250 GLint level, 254 GLint level,
255 GLenum internal_format,
251 GLsizei width, 256 GLsizei width,
252 GLsizei height, 257 GLsizei height,
253 bool flip_y, 258 bool flip_y,
254 bool premultiply_alpha, 259 bool premultiply_alpha,
255 bool unpremultiply_alpha) { 260 bool unpremultiply_alpha) {
261 bool just_alpha = (!premultiply_alpha && !unpremultiply_alpha) ||
262 (premultiply_alpha && unpremultiply_alpha);
reveman 2014/07/09 19:45:08 I think it would be cleaner and easier to read if
dshwang 2014/07/10 15:19:46 That's cool. Done.
263 if (source_target == GL_TEXTURE_2D && !flip_y && just_alpha) {
264 DoFastCopyTexture(decoder,
265 source_id,
266 dest_id,
267 level,
268 internal_format,
269 width,
270 height);
reveman 2014/07/09 19:45:08 are you missing a "return" statement here?
dshwang 2014/07/10 15:19:46 Oops, sorry. Done.
271 }
272
256 // Use default transform matrix if no transform passed in. 273 // Use default transform matrix if no transform passed in.
257 const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f, 274 const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
258 0.0f, 1.0f, 0.0f, 0.0f, 275 0.0f, 1.0f, 0.0f, 0.0f,
259 0.0f, 0.0f, 1.0f, 0.0f, 276 0.0f, 0.0f, 1.0f, 0.0f,
260 0.0f, 0.0f, 0.0f, 1.0f}; 277 0.0f, 0.0f, 0.0f, 1.0f};
261 DoCopyTextureWithTransform(decoder, source_target, dest_target, source_id, 278 DoCopyTextureWithTransform(decoder, source_target, source_id, dest_id,
262 dest_id, level, width, height, flip_y, premultiply_alpha, 279 level, internal_format, width, height, flip_y, premultiply_alpha,
263 unpremultiply_alpha, default_matrix); 280 unpremultiply_alpha, default_matrix);
264 } 281 }
265 282
283 void CopyTextureCHROMIUMResourceManager::DoFastCopyTexture(
284 const gles2::GLES2Decoder* decoder,
285 GLuint source_id,
286 GLuint dest_id,
287 GLint level,
288 GLenum internal_format,
289 GLsizei width,
290 GLsizei height) {
291 if (BindFramebufferTexture2D(source_id, 0 /* level */)) {
292 glBindTexture(GL_TEXTURE_2D, dest_id);
293 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
297 glCopyTexImage2D(GL_TEXTURE_2D,
298 level,
299 internal_format,
300 0 /* x */,
301 0 /* y */,
302 width,
303 height,
304 0 /* border */);
305 }
306
307 decoder->RestoreTextureState(source_id);
308 decoder->RestoreTextureState(dest_id);
309 decoder->RestoreTextureUnitBindings(0);
310 decoder->RestoreActiveTexture();
311 decoder->RestoreFramebufferBindings();
312 }
313
314 bool CopyTextureCHROMIUMResourceManager::BindFramebufferTexture2D(
315 GLuint texture_id,
316 GLint level) {
317 glActiveTexture(GL_TEXTURE0);
318 glBindTexture(GL_TEXTURE_2D, texture_id);
319 // NVidia drivers require texture settings to be a certain way
320 // or they won't report FRAMEBUFFER_COMPLETE.
321 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
322 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
323 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
325 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer_);
326 glFramebufferTexture2DEXT(
327 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, level);
328
329 #ifndef NDEBUG
330 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
331 if (GL_FRAMEBUFFER_COMPLETE != fb_status) {
332 DLOG(ERROR) << "CopyTextureCHROMIUM: Incomplete framebuffer.";
333 return false;
334 }
335 #endif
336 return true;
337 }
338
266 void CopyTextureCHROMIUMResourceManager::DoCopyTextureWithTransform( 339 void CopyTextureCHROMIUMResourceManager::DoCopyTextureWithTransform(
267 const gles2::GLES2Decoder* decoder, 340 const gles2::GLES2Decoder* decoder,
268 GLenum source_target, 341 GLenum source_target,
269 GLenum dest_target,
270 GLuint source_id, 342 GLuint source_id,
271 GLuint dest_id, 343 GLuint dest_id,
272 GLint level, 344 GLint level,
345 GLenum internal_format,
273 GLsizei width, 346 GLsizei width,
274 GLsizei height, 347 GLsizei height,
275 bool flip_y, 348 bool flip_y,
276 bool premultiply_alpha, 349 bool premultiply_alpha,
277 bool unpremultiply_alpha, 350 bool unpremultiply_alpha,
278 const GLfloat transform_matrix[16]) { 351 const GLfloat transform_matrix[16]) {
279 DCHECK(source_target == GL_TEXTURE_2D || 352 DCHECK(source_target == GL_TEXTURE_2D ||
280 source_target == GL_TEXTURE_RECTANGLE_ARB || 353 source_target == GL_TEXTURE_RECTANGLE_ARB ||
281 source_target == GL_TEXTURE_EXTERNAL_OES); 354 source_target == GL_TEXTURE_EXTERNAL_OES);
282 if (!initialized_) { 355 if (!initialized_) {
283 DLOG(ERROR) << "CopyTextureCHROMIUM: Uninitialized manager."; 356 DLOG(ERROR) << "CopyTextureCHROMIUM: Uninitialized manager.";
284 return; 357 return;
285 } 358 }
286 359
287 VertexShaderId vertex_shader_id = GetVertexShaderId(flip_y); 360 VertexShaderId vertex_shader_id = GetVertexShaderId(flip_y);
288 DCHECK_LT(static_cast<size_t>(vertex_shader_id), vertex_shaders_.size()); 361 DCHECK_LT(static_cast<size_t>(vertex_shader_id), vertex_shaders_.size());
289 GLuint* vertex_shader = &vertex_shaders_[vertex_shader_id];
290 if (!*vertex_shader) {
291 *vertex_shader = glCreateShader(GL_VERTEX_SHADER);
292 CompileShader(*vertex_shader, vertex_shader_source[vertex_shader_id]);
293 }
reveman 2014/07/09 19:45:08 do you need to move this code?
dshwang 2014/07/10 15:19:46 Not really, but it's good to move, because we can
reveman 2014/07/10 18:13:18 That's fine. I just wanted to know if there was so
294
295 FragmentShaderId fragment_shader_id = GetFragmentShaderId( 362 FragmentShaderId fragment_shader_id = GetFragmentShaderId(
296 premultiply_alpha, unpremultiply_alpha, source_target); 363 premultiply_alpha, unpremultiply_alpha, source_target);
297 DCHECK_LT(static_cast<size_t>(fragment_shader_id), fragment_shaders_.size()); 364 DCHECK_LT(static_cast<size_t>(fragment_shader_id), fragment_shaders_.size());
298 GLuint* fragment_shader = &fragment_shaders_[fragment_shader_id];
299 if (!*fragment_shader) {
300 *fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
301 CompileShader(*fragment_shader, fragment_shader_source[fragment_shader_id]);
302 }
reveman 2014/07/09 19:45:07 do you need to move this code?
dshwang 2014/07/10 15:19:46 Ditto.
303 365
304 ProgramMapKey key(vertex_shader_id, fragment_shader_id); 366 ProgramMapKey key(vertex_shader_id, fragment_shader_id);
305 ProgramInfo* info = &programs_[key]; 367 ProgramInfo* info = &programs_[key];
306 // Create program if necessary. 368 // Create program if necessary.
307 if (!info->program) { 369 if (!info->program) {
308 info->program = glCreateProgram(); 370 info->program = glCreateProgram();
371 GLuint* vertex_shader = &vertex_shaders_[vertex_shader_id];
372 if (!*vertex_shader) {
373 *vertex_shader = glCreateShader(GL_VERTEX_SHADER);
374 CompileShader(*vertex_shader, vertex_shader_source[vertex_shader_id]);
375 }
309 glAttachShader(info->program, *vertex_shader); 376 glAttachShader(info->program, *vertex_shader);
377 GLuint* fragment_shader = &fragment_shaders_[fragment_shader_id];
378 if (!*fragment_shader) {
379 *fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
380 CompileShader(*fragment_shader,
381 fragment_shader_source[fragment_shader_id]);
382 }
310 glAttachShader(info->program, *fragment_shader); 383 glAttachShader(info->program, *fragment_shader);
311 glBindAttribLocation(info->program, kVertexPositionAttrib, "a_position"); 384 glBindAttribLocation(info->program, kVertexPositionAttrib, "a_position");
312 glLinkProgram(info->program); 385 glLinkProgram(info->program);
313 #ifndef NDEBUG 386 #ifndef NDEBUG
314 GLint linked; 387 GLint linked;
315 glGetProgramiv(info->program, GL_LINK_STATUS, &linked); 388 glGetProgramiv(info->program, GL_LINK_STATUS, &linked);
316 if (!linked) 389 if (!linked)
317 DLOG(ERROR) << "CopyTextureCHROMIUM: program link failure."; 390 DLOG(ERROR) << "CopyTextureCHROMIUM: program link failure.";
318 #endif 391 #endif
319 info->matrix_handle = glGetUniformLocation(info->program, "u_matrix"); 392 info->matrix_handle = glGetUniformLocation(info->program, "u_matrix");
(...skipping 10 matching lines...) Expand all
330 DLOG(ERROR) << "CopyTextureCHROMIUM: Invalid shader."; 403 DLOG(ERROR) << "CopyTextureCHROMIUM: Invalid shader.";
331 return; 404 return;
332 } 405 }
333 #endif 406 #endif
334 407
335 glUniformMatrix4fv(info->matrix_handle, 1, GL_FALSE, transform_matrix); 408 glUniformMatrix4fv(info->matrix_handle, 1, GL_FALSE, transform_matrix);
336 if (source_target == GL_TEXTURE_RECTANGLE_ARB) 409 if (source_target == GL_TEXTURE_RECTANGLE_ARB)
337 glUniform2f(info->half_size_handle, width / 2.0f, height / 2.0f); 410 glUniform2f(info->half_size_handle, width / 2.0f, height / 2.0f);
338 else 411 else
339 glUniform2f(info->half_size_handle, 0.5f, 0.5f); 412 glUniform2f(info->half_size_handle, 0.5f, 0.5f);
340 glActiveTexture(GL_TEXTURE0);
341 glBindTexture(GL_TEXTURE_2D, dest_id);
342 // NVidia drivers require texture settings to be a certain way
343 // or they won't report FRAMEBUFFER_COMPLETE.
344 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
345 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
348 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer_);
349 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dest_target,
350 dest_id, level);
351 413
352 #ifndef NDEBUG 414 if (BindFramebufferTexture2D(dest_id, level)) {
353 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
354 if (GL_FRAMEBUFFER_COMPLETE != fb_status) {
355 DLOG(ERROR) << "CopyTextureCHROMIUM: Incomplete framebuffer.";
356 } else
357 #endif
358 {
359 decoder->ClearAllAttributes(); 415 decoder->ClearAllAttributes();
360 glEnableVertexAttribArray(kVertexPositionAttrib); 416 glEnableVertexAttribArray(kVertexPositionAttrib);
361 417
362 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); 418 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
363 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); 419 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
364 420
365 glUniform1i(info->sampler_handle, 0); 421 glUniform1i(info->sampler_handle, 0);
366 422
367 glBindTexture(source_target, source_id); 423 glBindTexture(source_target, source_id);
368 glTexParameterf(source_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 424 glTexParameterf(source_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
(...skipping 18 matching lines...) Expand all
387 decoder->RestoreTextureState(dest_id); 443 decoder->RestoreTextureState(dest_id);
388 decoder->RestoreTextureUnitBindings(0); 444 decoder->RestoreTextureUnitBindings(0);
389 decoder->RestoreActiveTexture(); 445 decoder->RestoreActiveTexture();
390 decoder->RestoreProgramBindings(); 446 decoder->RestoreProgramBindings();
391 decoder->RestoreBufferBindings(); 447 decoder->RestoreBufferBindings();
392 decoder->RestoreFramebufferBindings(); 448 decoder->RestoreFramebufferBindings();
393 decoder->RestoreGlobalState(); 449 decoder->RestoreGlobalState();
394 } 450 }
395 451
396 } // namespace gpu 452 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698