OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/compositor/compositor_gl.h" | 5 #include "ui/gfx/compositor/compositor_gl.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // Store locations of program inputs. | 201 // Store locations of program inputs. |
202 a_pos_loc_ = glGetAttribLocation(program_, "a_position"); | 202 a_pos_loc_ = glGetAttribLocation(program_, "a_position"); |
203 a_tex_loc_ = glGetAttribLocation(program_, "a_texCoord"); | 203 a_tex_loc_ = glGetAttribLocation(program_, "a_texCoord"); |
204 u_alpha_loc_ = glGetUniformLocation(program_, "u_alpha"); | 204 u_alpha_loc_ = glGetUniformLocation(program_, "u_alpha"); |
205 u_tex_loc_ = glGetUniformLocation(program_, "u_tex"); | 205 u_tex_loc_ = glGetUniformLocation(program_, "u_tex"); |
206 u_mat_loc_ = glGetUniformLocation(program_, "u_matViewProjection"); | 206 u_mat_loc_ = glGetUniformLocation(program_, "u_matViewProjection"); |
207 | 207 |
208 return true; | 208 return true; |
209 } | 209 } |
210 | 210 |
211 SharedResources::SharedResources() : initialized_(false) { | 211 SharedResourcesGL::SharedResourcesGL() : initialized_(false) { |
212 } | 212 } |
213 | 213 |
214 | 214 |
215 SharedResources::~SharedResources() { | 215 SharedResourcesGL::~SharedResourcesGL() { |
216 } | 216 } |
217 | 217 |
218 // static | 218 // static |
219 SharedResources* SharedResources::GetInstance() { | 219 SharedResourcesGL* SharedResourcesGL::GetInstance() { |
220 // We use LeakySingletonTraits so that we don't race with | 220 // We use LeakySingletonTraits so that we don't race with |
221 // the tear down of the gl_bindings. | 221 // the tear down of the gl_bindings. |
222 SharedResources* instance = Singleton<SharedResources, | 222 SharedResourcesGL* instance = Singleton<SharedResourcesGL, |
223 LeakySingletonTraits<SharedResources> >::get(); | 223 LeakySingletonTraits<SharedResourcesGL> >::get(); |
224 if (instance->Initialize()) { | 224 if (instance->Initialize()) { |
225 return instance; | 225 return instance; |
226 } else { | 226 } else { |
227 instance->Destroy(); | 227 instance->Destroy(); |
228 return NULL; | 228 return NULL; |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 bool SharedResources::Initialize() { | 232 bool SharedResourcesGL::Initialize() { |
233 if (initialized_) | 233 if (initialized_) |
234 return true; | 234 return true; |
235 | 235 |
236 { | 236 { |
237 // The following line of code exists soley to disable IO restrictions | 237 // The following line of code exists soley to disable IO restrictions |
238 // on this thread long enough to perform the GL bindings. | 238 // on this thread long enough to perform the GL bindings. |
239 // TODO(wjmaclean) Remove this when GL initialisation cleaned up. | 239 // TODO(wjmaclean) Remove this when GL initialisation cleaned up. |
240 base::ThreadRestrictions::ScopedAllowIO allow_io; | 240 base::ThreadRestrictions::ScopedAllowIO allow_io; |
241 if (!gfx::GLSurface::InitializeOneOff() || | 241 if (!gfx::GLSurface::InitializeOneOff() || |
242 gfx::GetGLImplementation() == gfx::kGLImplementationNone) { | 242 gfx::GetGLImplementation() == gfx::kGLImplementationNone) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 return false; | 279 return false; |
280 } | 280 } |
281 | 281 |
282 program_no_swizzle_.swap(temp_program_no_swizzle); | 282 program_no_swizzle_.swap(temp_program_no_swizzle); |
283 program_swizzle_.swap(temp_program_swizzle); | 283 program_swizzle_.swap(temp_program_swizzle); |
284 | 284 |
285 initialized_ = true; | 285 initialized_ = true; |
286 return true; | 286 return true; |
287 } | 287 } |
288 | 288 |
289 void SharedResources::Destroy() { | 289 void SharedResourcesGL::Destroy() { |
290 program_swizzle_.reset(); | 290 program_swizzle_.reset(); |
291 program_no_swizzle_.reset(); | 291 program_no_swizzle_.reset(); |
292 | 292 |
293 context_ = NULL; | 293 context_ = NULL; |
294 surface_ = NULL; | 294 surface_ = NULL; |
295 | 295 |
296 initialized_ = false; | 296 initialized_ = false; |
297 } | 297 } |
298 | 298 |
299 bool SharedResources::MakeSharedContextCurrent() { | 299 bool SharedResourcesGL::MakeSharedContextCurrent() { |
300 if (!initialized_) | 300 if (!initialized_) |
301 return false; | 301 return false; |
302 else | 302 else |
303 return context_->MakeCurrent(surface_.get()); | 303 return context_->MakeCurrent(surface_.get()); |
304 } | 304 } |
305 | 305 |
306 scoped_refptr<gfx::GLContext> SharedResources::CreateContext( | 306 scoped_refptr<gfx::GLContext> SharedResourcesGL::CreateContext( |
307 gfx::GLSurface* surface) { | 307 gfx::GLSurface* surface) { |
308 if (initialized_) | 308 if (initialized_) |
309 return gfx::GLContext::CreateGLContext( | 309 return gfx::GLContext::CreateGLContext( |
310 context_->share_group(), | 310 context_->share_group(), |
311 surface, | 311 surface, |
312 gfx::PreferIntegratedGpu); | 312 gfx::PreferIntegratedGpu); |
313 else | 313 else |
314 return NULL; | 314 return NULL; |
315 } | 315 } |
316 | 316 |
317 TextureGL::TextureGL() : texture_id_(0) { | 317 TextureGL::TextureGL() : texture_id_(0) { |
318 } | 318 } |
319 | 319 |
320 TextureGL::TextureGL(const gfx::Size& size) : texture_id_(0), size_(size) { | 320 TextureGL::TextureGL(const gfx::Size& size) : texture_id_(0), size_(size) { |
321 } | 321 } |
322 | 322 |
323 TextureGL::~TextureGL() { | 323 TextureGL::~TextureGL() { |
324 if (texture_id_) { | 324 if (texture_id_) { |
325 SharedResources* instance = SharedResources::GetInstance(); | 325 SharedResourcesGL* instance = SharedResourcesGL::GetInstance(); |
326 DCHECK(instance); | 326 DCHECK(instance); |
327 instance->MakeSharedContextCurrent(); | 327 instance->MakeSharedContextCurrent(); |
328 glDeleteTextures(1, &texture_id_); | 328 glDeleteTextures(1, &texture_id_); |
329 } | 329 } |
330 } | 330 } |
331 | 331 |
332 void TextureGL::SetCanvas(const SkCanvas& canvas, | 332 void TextureGL::SetCanvas(const SkCanvas& canvas, |
333 const gfx::Point& origin, | 333 const gfx::Point& origin, |
334 const gfx::Size& overall_size) { | 334 const gfx::Size& overall_size) { |
335 const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false); | 335 const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false); |
(...skipping 27 matching lines...) Expand all Loading... |
363 } else { // Uploading partial texture. | 363 } else { // Uploading partial texture. |
364 glBindTexture(GL_TEXTURE_2D, texture_id_); | 364 glBindTexture(GL_TEXTURE_2D, texture_id_); |
365 glTexSubImage2D(GL_TEXTURE_2D, 0, origin.x(), origin.y(), | 365 glTexSubImage2D(GL_TEXTURE_2D, 0, origin.x(), origin.y(), |
366 bitmap.width(), bitmap.height(), | 366 bitmap.width(), bitmap.height(), |
367 GL_RGBA, GL_UNSIGNED_BYTE, pixels); | 367 GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 void TextureGL::Draw(const ui::TextureDrawParams& params, | 371 void TextureGL::Draw(const ui::TextureDrawParams& params, |
372 const gfx::Rect& clip_bounds_in_texture) { | 372 const gfx::Rect& clip_bounds_in_texture) { |
373 SharedResources* instance = SharedResources::GetInstance(); | 373 SharedResourcesGL* instance = SharedResourcesGL::GetInstance(); |
374 DCHECK(instance); | 374 DCHECK(instance); |
375 DrawInternal(*instance->program_swizzle(), | 375 DrawInternal(*instance->program_swizzle(), |
376 params, | 376 params, |
377 clip_bounds_in_texture); | 377 clip_bounds_in_texture); |
378 } | 378 } |
379 | 379 |
380 void TextureGL::DrawInternal(const ui::TextureProgramGL& program, | 380 void TextureGL::DrawInternal(const ui::TextureProgramGL& program, |
381 const ui::TextureDrawParams& params, | 381 const ui::TextureDrawParams& params, |
382 const gfx::Rect& clip_bounds_in_texture) { | 382 const gfx::Rect& clip_bounds_in_texture) { |
383 // Clip clip_bounds_in_texture to size of texture. | 383 // Clip clip_bounds_in_texture to size of texture. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 | 479 |
480 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 480 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
481 } | 481 } |
482 | 482 |
483 CompositorGL::CompositorGL(CompositorDelegate* delegate, | 483 CompositorGL::CompositorGL(CompositorDelegate* delegate, |
484 gfx::AcceleratedWidget widget, | 484 gfx::AcceleratedWidget widget, |
485 const gfx::Size& size) | 485 const gfx::Size& size) |
486 : Compositor(delegate, size), | 486 : Compositor(delegate, size), |
487 started_(false) { | 487 started_(false) { |
488 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(false, widget); | 488 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(false, widget); |
489 gl_context_ = SharedResources::GetInstance()-> | 489 gl_context_ = SharedResourcesGL::GetInstance()-> |
490 CreateContext(gl_surface_.get()); | 490 CreateContext(gl_surface_.get()); |
491 gl_context_->MakeCurrent(gl_surface_.get()); | 491 gl_context_->MakeCurrent(gl_surface_.get()); |
492 gl_context_->SetSwapInterval(1); | 492 gl_context_->SetSwapInterval(1); |
493 glColorMask(true, true, true, true); | 493 glColorMask(true, true, true, true); |
494 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 494 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
495 } | 495 } |
496 | 496 |
497 CompositorGL::~CompositorGL() { | 497 CompositorGL::~CompositorGL() { |
498 gl_context_ = NULL; | 498 gl_context_ = NULL; |
499 } | 499 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 } | 537 } |
538 | 538 |
539 void CompositorGL::Blur(const gfx::Rect& bounds) { | 539 void CompositorGL::Blur(const gfx::Rect& bounds) { |
540 NOTIMPLEMENTED(); | 540 NOTIMPLEMENTED(); |
541 } | 541 } |
542 | 542 |
543 // static | 543 // static |
544 Compositor* Compositor::Create(CompositorDelegate* owner, | 544 Compositor* Compositor::Create(CompositorDelegate* owner, |
545 gfx::AcceleratedWidget widget, | 545 gfx::AcceleratedWidget widget, |
546 const gfx::Size& size) { | 546 const gfx::Size& size) { |
547 if (SharedResources::GetInstance() == NULL) | 547 if (SharedResourcesGL::GetInstance() == NULL) |
548 return NULL; | 548 return NULL; |
549 else | 549 else |
550 return new CompositorGL(owner, widget, size); | 550 return new CompositorGL(owner, widget, size); |
551 } | 551 } |
552 | 552 |
553 } // namespace ui | 553 } // namespace ui |
OLD | NEW |