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" |
11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "third_party/skia/include/core/SkMatrix.h" | 13 #include "third_party/skia/include/core/SkMatrix.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 14 #include "third_party/skia/include/core/SkScalar.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
17 #include "ui/gfx/gl/gl_bindings.h" | 17 #include "ui/gfx/gl/gl_bindings.h" |
18 #include "ui/gfx/gl/gl_context.h" | 18 #include "ui/gfx/gl/gl_context.h" |
19 #include "ui/gfx/gl/gl_implementation.h" | 19 #include "ui/gfx/gl/gl_implementation.h" |
20 #include "ui/gfx/gl/gl_surface.h" | 20 #include "ui/gfx/gl/gl_surface.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
| 24 // We pin the first context that the Compositor class creates with ref counting. |
| 25 // This hosts the shaders that are shared between all Compositor contexts. |
| 26 scoped_refptr<gfx::GLContext> g_share_context; |
| 27 |
24 GLuint CompileShader(GLenum type, const GLchar* source) { | 28 GLuint CompileShader(GLenum type, const GLchar* source) { |
25 GLuint shader = glCreateShader(type); | 29 GLuint shader = glCreateShader(type); |
26 if (!shader) | 30 if (!shader) |
27 return 0; | 31 return 0; |
28 | 32 |
29 glShaderSource(shader, 1, &source, 0); | 33 glShaderSource(shader, 1, &source, 0); |
30 glCompileShader(shader); | 34 glCompileShader(shader); |
31 | 35 |
32 GLint compiled; | 36 GLint compiled; |
33 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | 37 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
34 if (!compiled) { | 38 if (!compiled) { |
35 GLint info_len = 0; | 39 GLint info_len = 0; |
36 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len); | 40 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len); |
37 | 41 |
38 if (info_len > 0) { | 42 if (info_len > 0) { |
39 scoped_array<char> info_log(new char[info_len]); | 43 scoped_array<char> info_log(new char[info_len]); |
40 glGetShaderInfoLog(shader, info_len, NULL, info_log.get()); | 44 glGetShaderInfoLog(shader, info_len, NULL, info_log.get()); |
41 LOG(ERROR) << "Compile error: " << info_log.get(); | 45 LOG(ERROR) << "Compile error: " << info_log.get(); |
42 return 0; | 46 return 0; |
43 } | 47 } |
44 } | 48 } |
45 return shader; | 49 return shader; |
46 } | 50 } |
47 | 51 |
48 } // namespace (anonymous) | 52 } // namespace (anonymous) |
49 | 53 |
50 | 54 |
51 namespace ui { | 55 namespace ui { |
52 | 56 |
| 57 TextureProgramGL* CompositorGL::program_swizzle_ = NULL; |
| 58 TextureProgramGL* CompositorGL::program_no_swizzle_ = NULL; |
| 59 |
53 // Wraps a simple GL program for drawing textures to the screen. | 60 // Wraps a simple GL program for drawing textures to the screen. |
54 class TextureProgramGL { | 61 class TextureProgramGL { |
55 public: | 62 public: |
56 TextureProgramGL(); | 63 TextureProgramGL(); |
57 virtual ~TextureProgramGL() {} | 64 virtual ~TextureProgramGL() {} |
58 | 65 |
59 // Returns false if it was unable to initialize properly. | 66 // Returns false if it was unable to initialize properly. |
60 // | 67 // |
61 // Host GL context must be current when this is called. | 68 // Host GL context must be current when this is called. |
62 virtual bool Initialize() = 0; | 69 virtual bool Initialize() = 0; |
(...skipping 22 matching lines...) Expand all Loading... |
85 GLuint frag_shader_; | 92 GLuint frag_shader_; |
86 | 93 |
87 private: | 94 private: |
88 GLuint program_; | 95 GLuint program_; |
89 GLuint vertex_shader_; | 96 GLuint vertex_shader_; |
90 | 97 |
91 GLuint a_pos_loc_; | 98 GLuint a_pos_loc_; |
92 GLuint a_tex_loc_; | 99 GLuint a_tex_loc_; |
93 GLuint u_tex_loc_; | 100 GLuint u_tex_loc_; |
94 GLuint u_mat_loc_; | 101 GLuint u_mat_loc_; |
95 | |
96 }; | 102 }; |
97 | 103 |
98 class TextureProgramNoSwizzleGL : public TextureProgramGL { | 104 class TextureProgramNoSwizzleGL : public TextureProgramGL { |
99 public: | 105 public: |
100 TextureProgramNoSwizzleGL() {} | 106 TextureProgramNoSwizzleGL() {} |
101 virtual bool Initialize(); | 107 virtual bool Initialize(); |
102 private: | 108 private: |
103 DISALLOW_COPY_AND_ASSIGN(TextureProgramNoSwizzleGL); | 109 DISALLOW_COPY_AND_ASSIGN(TextureProgramNoSwizzleGL); |
104 }; | 110 }; |
105 | 111 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 glUniformMatrix4fv(program.u_mat_loc(), 1, GL_FALSE, m); | 319 glUniformMatrix4fv(program.u_mat_loc(), 1, GL_FALSE, m); |
314 | 320 |
315 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 321 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
316 } | 322 } |
317 | 323 |
318 CompositorGL::CompositorGL(gfx::AcceleratedWidget widget, | 324 CompositorGL::CompositorGL(gfx::AcceleratedWidget widget, |
319 const gfx::Size& size) | 325 const gfx::Size& size) |
320 : started_(false), | 326 : started_(false), |
321 size_(size) { | 327 size_(size) { |
322 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(widget); | 328 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(widget); |
323 gl_context_ = gfx::GLContext::CreateGLContext(NULL, gl_surface_.get()); | 329 if (g_share_context.get()) { |
| 330 gl_context_ = gfx::GLContext::CreateGLContext( |
| 331 g_share_context->share_group(), gl_surface_.get()); |
| 332 } else { |
| 333 gl_context_ = gfx::GLContext::CreateGLContext( |
| 334 NULL, gl_surface_.get()); |
| 335 g_share_context = gl_context_; |
| 336 } |
324 gl_context_->MakeCurrent(gl_surface_.get()); | 337 gl_context_->MakeCurrent(gl_surface_.get()); |
325 if (!InitShaders()) | 338 if (!InitShaders()) |
326 LOG(ERROR) << "Unable to initialize shaders (context = " | 339 LOG(ERROR) << "Unable to initialize shaders (context = " |
327 << static_cast<void*>(gl_context_.get()) << ")"; | 340 << static_cast<void*>(gl_context_.get()) << ")"; |
328 glColorMask(true, true, true, true); | 341 glColorMask(true, true, true, true); |
329 glEnable(GL_BLEND); | 342 glEnable(GL_BLEND); |
330 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 343 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
331 } | 344 } |
332 | 345 |
333 CompositorGL::~CompositorGL() { | 346 CompositorGL::~CompositorGL() { |
334 } | 347 } |
335 | 348 |
336 void CompositorGL::MakeCurrent() { | 349 void CompositorGL::MakeCurrent() { |
337 gl_context_->MakeCurrent(gl_surface_.get()); | 350 gl_context_->MakeCurrent(gl_surface_.get()); |
338 } | 351 } |
339 | 352 |
340 gfx::Size CompositorGL::GetSize() { | 353 gfx::Size CompositorGL::GetSize() { |
341 return size_; | 354 return size_; |
342 } | 355 } |
343 | 356 |
344 TextureProgramGL* CompositorGL::program_no_swizzle() { | 357 TextureProgramGL* CompositorGL::program_no_swizzle() { |
345 return program_no_swizzle_.get(); | 358 return program_no_swizzle_; |
346 } | 359 } |
347 | 360 |
348 TextureProgramGL* CompositorGL::program_swizzle() { | 361 TextureProgramGL* CompositorGL::program_swizzle() { |
349 return program_swizzle_.get(); | 362 return program_swizzle_; |
350 } | 363 } |
351 | 364 |
352 Texture* CompositorGL::CreateTexture() { | 365 Texture* CompositorGL::CreateTexture() { |
353 Texture* texture = new TextureGL(this); | 366 Texture* texture = new TextureGL(this); |
354 return texture; | 367 return texture; |
355 } | 368 } |
356 | 369 |
357 void CompositorGL::NotifyStart() { | 370 void CompositorGL::NotifyStart() { |
358 started_ = true; | 371 started_ = true; |
359 gl_context_->MakeCurrent(gl_surface_.get()); | 372 gl_context_->MakeCurrent(gl_surface_.get()); |
(...skipping 25 matching lines...) Expand all Loading... |
385 void CompositorGL::SchedulePaint() { | 398 void CompositorGL::SchedulePaint() { |
386 // TODO: X doesn't provide coalescing of regions, its left to the toolkit. | 399 // TODO: X doesn't provide coalescing of regions, its left to the toolkit. |
387 NOTIMPLEMENTED(); | 400 NOTIMPLEMENTED(); |
388 } | 401 } |
389 | 402 |
390 void CompositorGL::OnWidgetSizeChanged(const gfx::Size& size) { | 403 void CompositorGL::OnWidgetSizeChanged(const gfx::Size& size) { |
391 size_ = size; | 404 size_ = size; |
392 } | 405 } |
393 | 406 |
394 bool CompositorGL::InitShaders() { | 407 bool CompositorGL::InitShaders() { |
395 scoped_ptr<TextureProgramGL> temp_program(new TextureProgramNoSwizzleGL()); | 408 if (!program_no_swizzle_) { |
396 if (!temp_program->Initialize()) | 409 scoped_ptr<TextureProgramGL> temp_program(new TextureProgramNoSwizzleGL()); |
397 return false; | 410 if (!temp_program->Initialize()) |
398 else | 411 return false; |
399 program_no_swizzle_.reset(temp_program.release()); | 412 program_no_swizzle_ = temp_program.release(); |
| 413 } |
400 | 414 |
401 temp_program.reset(new TextureProgramSwizzleGL()); | 415 if (!program_swizzle_) { |
402 if (!temp_program->Initialize()) | 416 scoped_ptr<TextureProgramGL> temp_program(new TextureProgramSwizzleGL()); |
403 return false; | 417 if (!temp_program->Initialize()) |
404 else | 418 return false; |
405 program_swizzle_.reset(temp_program.release()); | 419 program_swizzle_ = temp_program.release(); |
| 420 } |
406 | 421 |
407 return true; | 422 return true; |
408 } | 423 } |
409 | 424 |
410 // static | 425 // static |
411 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, | 426 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, |
412 const gfx::Size& size) { | 427 const gfx::Size& size) { |
413 // The following line of code exists soley to disable IO restrictions | 428 // The following line of code exists soley to disable IO restrictions |
414 // on this thread long enough to perform the GL bindings. | 429 // on this thread long enough to perform the GL bindings. |
415 // TODO(wjmaclean) Remove this when GL initialisation cleaned up. | 430 // TODO(wjmaclean) Remove this when GL initialisation cleaned up. |
416 base::ThreadRestrictions::ScopedAllowIO allow_io; | 431 base::ThreadRestrictions::ScopedAllowIO allow_io; |
417 if (gfx::GLSurface::InitializeOneOff() && | 432 if (gfx::GLSurface::InitializeOneOff() && |
418 gfx::GetGLImplementation() != gfx::kGLImplementationNone) | 433 gfx::GetGLImplementation() != gfx::kGLImplementationNone) |
419 return new CompositorGL(widget, size); | 434 return new CompositorGL(widget, size); |
420 return NULL; | 435 return NULL; |
421 } | 436 } |
422 | 437 |
423 } // namespace ui | 438 } // namespace ui |
OLD | NEW |