OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/compositor/compositor_gl.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/command_line.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/debug/trace_event.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/threading/thread_restrictions.h" | |
15 #include "third_party/skia/include/core/SkBitmap.h" | |
16 #include "third_party/skia/include/core/SkDevice.h" | |
17 #include "third_party/skia/include/core/SkMatrix.h" | |
18 #include "third_party/skia/include/core/SkPoint.h" | |
19 #include "third_party/skia/include/core/SkRect.h" | |
20 #include "third_party/skia/include/core/SkScalar.h" | |
21 #include "ui/gfx/compositor/compositor_switches.h" | |
22 #include "ui/gfx/rect.h" | |
23 #include "ui/gfx/transform.h" | |
24 #include "ui/gfx/gl/gl_bindings.h" | |
25 #include "ui/gfx/gl/gl_context.h" | |
26 #include "ui/gfx/gl/gl_implementation.h" | |
27 #include "ui/gfx/gl/gl_surface.h" | |
28 | |
29 // Wraps a simple GL program for drawing textures to the screen. | |
30 // Need the declaration before the subclasses in the anonymous namespace below. | |
31 class ui::TextureProgramGL { | |
32 public: | |
33 TextureProgramGL(); | |
34 virtual ~TextureProgramGL() {} | |
35 | |
36 // Returns false if it was unable to initialize properly. | |
37 // | |
38 // Host GL context must be current when this is called. | |
39 virtual bool Initialize() = 0; | |
40 | |
41 // Make the program active in the current GL context. | |
42 void Use() const { glUseProgram(program_); } | |
43 | |
44 // Location of vertex position attribute in vertex shader. | |
45 GLuint a_pos_loc() const { return a_pos_loc_; } | |
46 | |
47 // Location of texture co-ordinate attribute in vertex shader. | |
48 GLuint a_tex_loc() const { return a_tex_loc_; } | |
49 | |
50 // Location of the alpha multiplier uniform in the vertex shader. | |
51 GLuint u_alpha_loc() const { return u_alpha_loc_; } | |
52 | |
53 // Location of transformation matrix uniform in vertex shader. | |
54 GLuint u_mat_loc() const { return u_mat_loc_; } | |
55 | |
56 // Location of texture unit uniform that we texture map from | |
57 // in the fragment shader. | |
58 GLuint u_tex_loc() const { return u_tex_loc_; } | |
59 | |
60 protected: | |
61 // Only the fragment shaders differ. This handles the initialization | |
62 // of all the other fields. | |
63 bool InitializeCommon(); | |
64 | |
65 GLuint frag_shader_; | |
66 | |
67 private: | |
68 GLuint program_; | |
69 GLuint vertex_shader_; | |
70 | |
71 GLuint a_pos_loc_; | |
72 GLuint a_tex_loc_; | |
73 GLuint u_alpha_loc_; | |
74 GLuint u_tex_loc_; | |
75 GLuint u_mat_loc_; | |
76 }; | |
77 | |
78 namespace { | |
79 | |
80 class TextureProgramNoSwizzleGL : public ui::TextureProgramGL { | |
81 public: | |
82 TextureProgramNoSwizzleGL() {} | |
83 virtual bool Initialize(); | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(TextureProgramNoSwizzleGL); | |
86 }; | |
87 | |
88 class TextureProgramSwizzleGL : public ui::TextureProgramGL { | |
89 public: | |
90 TextureProgramSwizzleGL() {} | |
91 virtual bool Initialize(); | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(TextureProgramSwizzleGL); | |
94 }; | |
95 | |
96 GLuint CompileShader(GLenum type, const GLchar* source) { | |
97 GLuint shader = glCreateShader(type); | |
98 if (!shader) | |
99 return 0; | |
100 | |
101 glShaderSource(shader, 1, &source, 0); | |
102 glCompileShader(shader); | |
103 | |
104 GLint compiled; | |
105 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
106 if (!compiled) { | |
107 GLint info_len = 0; | |
108 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len); | |
109 | |
110 if (info_len > 0) { | |
111 scoped_array<char> info_log(new char[info_len]); | |
112 glGetShaderInfoLog(shader, info_len, NULL, info_log.get()); | |
113 LOG(ERROR) << "Compile error: " << info_log.get(); | |
114 return 0; | |
115 } | |
116 } | |
117 return shader; | |
118 } | |
119 | |
120 bool TextureProgramNoSwizzleGL::Initialize() { | |
121 const bool debug_overdraw = CommandLine::ForCurrentProcess()->HasSwitch( | |
122 switches::kEnableCompositorOverdrawDebugging); | |
123 | |
124 if (debug_overdraw) { | |
125 const GLchar* frag_shader_source = | |
126 "#ifdef GL_ES\n" | |
127 "precision mediump float;\n" | |
128 "#endif\n" | |
129 "uniform sampler2D u_tex;" | |
130 "varying vec2 v_texCoord;" | |
131 "void main()" | |
132 "{" | |
133 " gl_FragColor = texture2D(u_tex, v_texCoord);" | |
134 " gl_FragColor.a = 1.0;" | |
135 " gl_FragColor = gl_FragColor * 0.25;" | |
136 " gl_FragColor = gl_FragColor + vec4(0.75, 0.75, 0.75, 0.75);" | |
137 " gl_FragColor = gl_FragColor * 0.3333333;" | |
138 "}"; | |
139 frag_shader_ = CompileShader(GL_FRAGMENT_SHADER, frag_shader_source); | |
140 } else { | |
141 const GLchar* frag_shader_source = | |
142 "#ifdef GL_ES\n" | |
143 "precision mediump float;\n" | |
144 "#endif\n" | |
145 "uniform float u_alpha;" | |
146 "uniform sampler2D u_tex;" | |
147 "varying vec2 v_texCoord;" | |
148 "void main()" | |
149 "{" | |
150 " gl_FragColor = texture2D(u_tex, v_texCoord);" | |
151 " if (u_alpha > 0.0)" | |
152 " gl_FragColor.a = u_alpha;" | |
153 " else" | |
154 " gl_FragColor.a = gl_FragColor.a * -u_alpha;" | |
155 "}"; | |
156 frag_shader_ = CompileShader(GL_FRAGMENT_SHADER, frag_shader_source); | |
157 } | |
158 | |
159 if (!frag_shader_) | |
160 return false; | |
161 | |
162 return InitializeCommon(); | |
163 } | |
164 | |
165 bool TextureProgramSwizzleGL::Initialize() { | |
166 const bool debug_overdraw = CommandLine::ForCurrentProcess()->HasSwitch( | |
167 switches::kEnableCompositorOverdrawDebugging); | |
168 | |
169 if (debug_overdraw) { | |
170 const GLchar* frag_shader_source = | |
171 "#ifdef GL_ES\n" | |
172 "precision mediump float;\n" | |
173 "#endif\n" | |
174 "uniform sampler2D u_tex;" | |
175 "varying vec2 v_texCoord;" | |
176 "void main()" | |
177 "{" | |
178 " gl_FragColor = texture2D(u_tex, v_texCoord).zyxw;" | |
179 " gl_FragColor.a = 1.0;" | |
180 " gl_FragColor = gl_FragColor * 0.25;" | |
181 " gl_FragColor = gl_FragColor + vec4(0.75, 0.75, 0.75, 0.75);" | |
182 " gl_FragColor = gl_FragColor * 0.3333333;" | |
183 "}"; | |
184 frag_shader_ = CompileShader(GL_FRAGMENT_SHADER, frag_shader_source); | |
185 } else { | |
186 const GLchar* frag_shader_source = | |
187 "#ifdef GL_ES\n" | |
188 "precision mediump float;\n" | |
189 "#endif\n" | |
190 "uniform float u_alpha;" | |
191 "uniform sampler2D u_tex;" | |
192 "varying vec2 v_texCoord;" | |
193 "void main()" | |
194 "{" | |
195 " gl_FragColor = texture2D(u_tex, v_texCoord).zyxw;" | |
196 " if (u_alpha > 0.0)" | |
197 " gl_FragColor.a = u_alpha;" | |
198 " else" | |
199 " gl_FragColor.a = gl_FragColor.a * -u_alpha;" | |
200 "}"; | |
201 frag_shader_ = CompileShader(GL_FRAGMENT_SHADER, frag_shader_source); | |
202 } | |
203 | |
204 if (!frag_shader_) | |
205 return false; | |
206 | |
207 return InitializeCommon(); | |
208 } | |
209 | |
210 } // namespace | |
211 | |
212 namespace ui { | |
213 | |
214 TextureProgramGL::TextureProgramGL() | |
215 : program_(0), | |
216 a_pos_loc_(0), | |
217 a_tex_loc_(0), | |
218 u_alpha_loc_(0), | |
219 u_tex_loc_(0), | |
220 u_mat_loc_(0) { | |
221 } | |
222 | |
223 bool TextureProgramGL::InitializeCommon() { | |
224 const GLchar* vertex_shader_source = | |
225 "attribute vec4 a_position;" | |
226 "attribute vec2 a_texCoord;" | |
227 "uniform mat4 u_matViewProjection;" | |
228 "varying vec2 v_texCoord;" | |
229 "void main()" | |
230 "{" | |
231 " gl_Position = u_matViewProjection * a_position;" | |
232 " v_texCoord = a_texCoord;" | |
233 "}"; | |
234 | |
235 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, vertex_shader_source); | |
236 if (!vertex_shader_) | |
237 return false; | |
238 | |
239 program_ = glCreateProgram(); | |
240 glAttachShader(program_, vertex_shader_); | |
241 glAttachShader(program_, frag_shader_); | |
242 glLinkProgram(program_); | |
243 | |
244 if (glGetError() != GL_NO_ERROR) | |
245 return false; | |
246 | |
247 // Store locations of program inputs. | |
248 a_pos_loc_ = glGetAttribLocation(program_, "a_position"); | |
249 a_tex_loc_ = glGetAttribLocation(program_, "a_texCoord"); | |
250 u_alpha_loc_ = glGetUniformLocation(program_, "u_alpha"); | |
251 u_tex_loc_ = glGetUniformLocation(program_, "u_tex"); | |
252 u_mat_loc_ = glGetUniformLocation(program_, "u_matViewProjection"); | |
253 | |
254 return true; | |
255 } | |
256 | |
257 SharedResourcesGL::SharedResourcesGL() : initialized_(false) { | |
258 } | |
259 | |
260 | |
261 SharedResourcesGL::~SharedResourcesGL() { | |
262 } | |
263 | |
264 // static | |
265 SharedResourcesGL* SharedResourcesGL::GetInstance() { | |
266 // We use LeakySingletonTraits so that we don't race with | |
267 // the tear down of the gl_bindings. | |
268 SharedResourcesGL* instance = Singleton<SharedResourcesGL, | |
269 LeakySingletonTraits<SharedResourcesGL> >::get(); | |
270 if (instance->Initialize()) { | |
271 return instance; | |
272 } else { | |
273 instance->Destroy(); | |
274 return NULL; | |
275 } | |
276 } | |
277 | |
278 bool SharedResourcesGL::Initialize() { | |
279 if (initialized_) | |
280 return true; | |
281 | |
282 { | |
283 // The following line of code exists soley to disable IO restrictions | |
284 // on this thread long enough to perform the GL bindings. | |
285 // TODO(wjmaclean) Remove this when GL initialisation cleaned up. | |
286 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
287 if (!gfx::GLSurface::InitializeOneOff() || | |
288 gfx::GetGLImplementation() == gfx::kGLImplementationNone) { | |
289 LOG(ERROR) << "Could not load the GL bindings"; | |
290 return false; | |
291 } | |
292 } | |
293 | |
294 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)); | |
295 if (!surface_.get()) { | |
296 LOG(ERROR) << "Unable to create offscreen GL surface."; | |
297 return false; | |
298 } | |
299 | |
300 context_ = gfx::GLContext::CreateGLContext( | |
301 NULL, | |
302 surface_.get(), | |
303 gfx::PreferIntegratedGpu); | |
304 if (!context_.get()) { | |
305 LOG(ERROR) << "Unable to create GL context."; | |
306 return false; | |
307 } | |
308 | |
309 program_no_swizzle_.reset(); | |
310 program_swizzle_.reset(); | |
311 | |
312 context_->MakeCurrent(surface_.get()); | |
313 | |
314 scoped_ptr<ui::TextureProgramGL> temp_program_no_swizzle( | |
315 new TextureProgramNoSwizzleGL()); | |
316 if (!temp_program_no_swizzle->Initialize()) { | |
317 LOG(ERROR) << "Unable to initialize shader."; | |
318 return false; | |
319 } | |
320 | |
321 scoped_ptr<ui::TextureProgramGL> temp_program_swizzle( | |
322 new TextureProgramSwizzleGL()); | |
323 if (!temp_program_swizzle->Initialize()) { | |
324 LOG(ERROR) << "Unable to initialize shader."; | |
325 return false; | |
326 } | |
327 | |
328 program_no_swizzle_.swap(temp_program_no_swizzle); | |
329 program_swizzle_.swap(temp_program_swizzle); | |
330 | |
331 initialized_ = true; | |
332 return true; | |
333 } | |
334 | |
335 void SharedResourcesGL::Destroy() { | |
336 program_swizzle_.reset(); | |
337 program_no_swizzle_.reset(); | |
338 | |
339 context_ = NULL; | |
340 surface_ = NULL; | |
341 | |
342 initialized_ = false; | |
343 } | |
344 | |
345 gfx::ScopedMakeCurrent* SharedResourcesGL::GetScopedMakeCurrent() { | |
346 DCHECK(initialized_); | |
347 if (initialized_) | |
348 return new gfx::ScopedMakeCurrent(context_.get(), surface_.get()); | |
349 else | |
350 return NULL; | |
351 } | |
352 | |
353 scoped_refptr<gfx::GLContext> SharedResourcesGL::CreateContext( | |
354 gfx::GLSurface* surface) { | |
355 if (initialized_) | |
356 return gfx::GLContext::CreateGLContext( | |
357 context_->share_group(), | |
358 surface, | |
359 gfx::PreferIntegratedGpu); | |
360 else | |
361 return NULL; | |
362 } | |
363 | |
364 void* SharedResourcesGL::GetDisplay() { | |
365 return surface_->GetDisplay(); | |
366 } | |
367 | |
368 TextureGL::TextureGL() : texture_id_(0) { | |
369 } | |
370 | |
371 TextureGL::TextureGL(const gfx::Size& size) : texture_id_(0), size_(size) { | |
372 } | |
373 | |
374 TextureGL::~TextureGL() { | |
375 if (texture_id_) { | |
376 SharedResourcesGL* instance = SharedResourcesGL::GetInstance(); | |
377 DCHECK(instance); | |
378 scoped_ptr<gfx::ScopedMakeCurrent> bind(instance->GetScopedMakeCurrent()); | |
379 glDeleteTextures(1, &texture_id_); | |
380 } | |
381 } | |
382 | |
383 void TextureGL::SetCanvas(const SkCanvas& canvas, | |
384 const gfx::Point& origin, | |
385 const gfx::Size& overall_size) { | |
386 TRACE_EVENT0("ui", "TextureGL::SetCanvas"); | |
387 const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false); | |
388 // Verify bitmap pixels are contiguous. | |
389 DCHECK_EQ(bitmap.rowBytes(), | |
390 SkBitmap::ComputeRowBytes(bitmap.config(), bitmap.width())); | |
391 SkAutoLockPixels lock(bitmap); | |
392 void* pixels = bitmap.getPixels(); | |
393 | |
394 if (!texture_id_) { | |
395 // Texture needs to be created. We assume the first call is for | |
396 // a full-sized canvas. | |
397 size_ = overall_size; | |
398 | |
399 glGenTextures(1, &texture_id_); | |
400 glBindTexture(GL_TEXTURE_2D, texture_id_); | |
401 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | |
402 size_.width(), size_.height(), 0, | |
403 GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
404 | |
405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
409 } else if (size_ != overall_size) { // Size has changed. | |
410 size_ = overall_size; | |
411 glBindTexture(GL_TEXTURE_2D, texture_id_); | |
412 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | |
413 size_.width(), size_.height(), 0, | |
414 GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
415 } else { | |
416 glBindTexture(GL_TEXTURE_2D, texture_id_); | |
417 } | |
418 glTexSubImage2D(GL_TEXTURE_2D, 0, origin.x(), origin.y(), | |
419 bitmap.width(), bitmap.height(), | |
420 GL_RGBA, GL_UNSIGNED_BYTE, pixels); | |
421 } | |
422 | |
423 void TextureGL::Draw(const ui::TextureDrawParams& params, | |
424 const gfx::Rect& clip_bounds_in_texture) { | |
425 TRACE_EVENT0("ui", "TextureGL::Draw"); | |
426 SharedResourcesGL* instance = SharedResourcesGL::GetInstance(); | |
427 DCHECK(instance); | |
428 DrawInternal(*instance->program_swizzle(), | |
429 params, | |
430 clip_bounds_in_texture); | |
431 } | |
432 | |
433 void TextureGL::DrawInternal(const ui::TextureProgramGL& program, | |
434 const ui::TextureDrawParams& params, | |
435 const gfx::Rect& clip_bounds_in_texture) { | |
436 // Clip clip_bounds_in_texture to size of texture. | |
437 gfx::Rect clip_bounds = clip_bounds_in_texture.Intersect( | |
438 gfx::Rect(gfx::Point(0, 0), size_)); | |
439 | |
440 // Verify that compositor_size has been set. | |
441 DCHECK(params.compositor_size != gfx::Size(0, 0)); | |
442 | |
443 if (params.blend) | |
444 glEnable(GL_BLEND); | |
445 else | |
446 glDisable(GL_BLEND); | |
447 | |
448 program.Use(); | |
449 | |
450 glActiveTexture(GL_TEXTURE0); | |
451 glUniform1i(program.u_tex_loc(), 0); | |
452 glBindTexture(GL_TEXTURE_2D, texture_id_); | |
453 | |
454 ui::Transform t; | |
455 t.ConcatTranslate(1, 1); | |
456 t.ConcatScale(size_.width()/2.0f, size_.height()/2.0f); | |
457 t.ConcatTranslate(0, -size_.height()); | |
458 t.ConcatScale(1, -1); | |
459 | |
460 t.ConcatTransform(params.transform); // Add view transform. | |
461 | |
462 t.ConcatTranslate(0, -params.compositor_size.height()); | |
463 t.ConcatScale(1, -1); | |
464 t.ConcatTranslate(-params.compositor_size.width() / 2.0f, | |
465 -params.compositor_size.height() / 2.0f); | |
466 t.ConcatScale(2.0f / params.compositor_size.width(), | |
467 2.0f / params.compositor_size.height()); | |
468 | |
469 GLfloat m[16]; | |
470 t.matrix().asColMajorf(m); | |
471 | |
472 SkPoint texture_points[4]; | |
473 texture_points[0] = SkPoint::Make(clip_bounds.x(), | |
474 clip_bounds.y() + clip_bounds.height()); | |
475 texture_points[1] = SkPoint::Make(clip_bounds.x() + clip_bounds.width(), | |
476 clip_bounds.y() + clip_bounds.height()); | |
477 texture_points[2] = SkPoint::Make(clip_bounds.x() + clip_bounds.width(), | |
478 clip_bounds.y()); | |
479 texture_points[3] = SkPoint::Make(clip_bounds.x(), clip_bounds.y()); | |
480 | |
481 ui::Transform texture_rect_transform; | |
482 texture_rect_transform.ConcatScale(1.0f / size_.width(), | |
483 1.0f / size_.height()); | |
484 if (params.vertically_flipped) { | |
485 ui::Transform vertical_flip; | |
486 vertical_flip.SetScaleY(-1.0); | |
487 vertical_flip.SetTranslateY(1.0); | |
488 texture_rect_transform.ConcatTransform(vertical_flip); | |
489 } | |
490 SkMatrix texture_transform_matrix = texture_rect_transform.matrix(); | |
491 texture_transform_matrix.mapPoints(texture_points, 4); | |
492 | |
493 SkRect clip_rect = SkRect::MakeXYWH( | |
494 clip_bounds.x(), | |
495 clip_bounds.y(), | |
496 clip_bounds.width(), | |
497 clip_bounds.height()); | |
498 | |
499 ui::Transform clip_rect_transform; | |
500 clip_rect_transform.ConcatScale(2.0f / size_.width(), | |
501 2.0f / size_.height()); | |
502 clip_rect_transform.ConcatScale(1, -1); | |
503 clip_rect_transform.ConcatTranslate(-1.0f, 1.0f); | |
504 SkMatrix clip_transform_matrix = clip_rect_transform.matrix(); | |
505 clip_transform_matrix.mapRect(&clip_rect); | |
506 | |
507 GLfloat clip_vertices[] = { clip_rect.left(), clip_rect.top(), +0., | |
508 clip_rect.right(), clip_rect.top(), +0., | |
509 clip_rect.right(), clip_rect.bottom(), +0., | |
510 clip_rect.left(), clip_rect.bottom(), +0.}; | |
511 | |
512 GLfloat texture_vertices[] = { texture_points[0].x(), texture_points[0].y(), | |
513 texture_points[1].x(), texture_points[1].y(), | |
514 texture_points[2].x(), texture_points[2].y(), | |
515 texture_points[3].x(), texture_points[3].y()}; | |
516 | |
517 glVertexAttribPointer(program.a_pos_loc(), 3, GL_FLOAT, | |
518 GL_FALSE, 3 * sizeof(GLfloat), clip_vertices); | |
519 glVertexAttribPointer(program.a_tex_loc(), 2, GL_FLOAT, | |
520 GL_FALSE, 2 * sizeof(GLfloat), texture_vertices); | |
521 glEnableVertexAttribArray(program.a_pos_loc()); | |
522 glEnableVertexAttribArray(program.a_tex_loc()); | |
523 | |
524 glUniformMatrix4fv(program.u_mat_loc(), 1, GL_FALSE, m); | |
525 | |
526 // negative means multiply, positive means clobber. | |
527 float alpha = params.has_valid_alpha_channel | |
528 ? -params.opacity | |
529 : params.opacity; | |
530 | |
531 glUniform1fv(program.u_alpha_loc(), 1, &alpha); | |
532 | |
533 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | |
534 } | |
535 | |
536 CompositorGL::CompositorGL(CompositorDelegate* delegate, | |
537 gfx::AcceleratedWidget widget, | |
538 const gfx::Size& size) | |
539 : Compositor(delegate, size), | |
540 started_(false) { | |
541 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(false, widget); | |
542 gl_context_ = SharedResourcesGL::GetInstance()-> | |
543 CreateContext(gl_surface_.get()); | |
544 gl_context_->MakeCurrent(gl_surface_.get()); | |
545 | |
546 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
547 | |
548 if (!command_line->HasSwitch(switches::kDisableUIVsync)) | |
549 gl_context_->SetSwapInterval(1); | |
550 | |
551 glColorMask(true, true, true, true); | |
552 | |
553 const bool debug_overdraw = command_line->HasSwitch( | |
554 switches::kEnableCompositorOverdrawDebugging); | |
555 | |
556 if (debug_overdraw) | |
557 glBlendFunc(GL_ONE, GL_ONE); | |
558 else | |
559 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
560 } | |
561 | |
562 CompositorGL::~CompositorGL() { | |
563 gl_context_ = NULL; | |
564 } | |
565 | |
566 bool CompositorGL::ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds) { | |
567 MakeCurrent(); | |
568 | |
569 if (bounds.right() > size().width() || bounds.bottom() > size().height()) | |
570 return false; | |
571 | |
572 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
573 bounds.width(), | |
574 bounds.height()); | |
575 bitmap->allocPixels(); | |
576 SkAutoLockPixels lock(*bitmap); | |
577 unsigned char* pixels = static_cast<unsigned char*>(bitmap->getPixels()); | |
578 | |
579 // Check that it's a tight pixel packing | |
580 DCHECK_EQ(bitmap->rowBytes(), | |
581 SkBitmap::ComputeRowBytes(bitmap->config(), bitmap->width())); | |
582 | |
583 GLint current_alignment = 0; | |
584 glGetIntegerv(GL_PACK_ALIGNMENT, ¤t_alignment); | |
585 glPixelStorei(GL_PACK_ALIGNMENT, 4); | |
586 | |
587 // Flip vertically to convert to OpenGL coordinates. | |
588 glReadPixels(bounds.x(), | |
589 size().height() - bounds.y() - bounds.height(), | |
590 bounds.width(), | |
591 bounds.height(), | |
592 GL_RGBA, | |
593 GL_UNSIGNED_BYTE, | |
594 pixels); | |
595 glPixelStorei(GL_PACK_ALIGNMENT, current_alignment); | |
596 | |
597 SwizzleRGBAToBGRAAndFlip(pixels, bounds.size()); | |
598 return true; | |
599 } | |
600 | |
601 void CompositorGL::MakeCurrent() { | |
602 gl_context_->MakeCurrent(gl_surface_.get()); | |
603 } | |
604 | |
605 void CompositorGL::OnWidgetSizeChanged() { | |
606 } | |
607 | |
608 Texture* CompositorGL::CreateTexture() { | |
609 Texture* texture = new TextureGL(); | |
610 return texture; | |
611 } | |
612 | |
613 void CompositorGL::OnNotifyStart(bool clear) { | |
614 TRACE_EVENT0("ui", "CompositorGL::OnNotifyStart"); | |
615 started_ = true; | |
616 gl_context_->MakeCurrent(gl_surface_.get()); | |
617 glViewport(0, 0, size().width(), size().height()); | |
618 glColorMask(true, true, true, true); | |
619 | |
620 if (clear) { | |
621 glClearColor(0, 0, 0, 0); | |
622 glClear(GL_COLOR_BUFFER_BIT); | |
623 } | |
624 #if !defined(NDEBUG) | |
625 else { | |
626 // In debug mode, when we're not forcing a clear, clear to 'psychedelic' | |
627 // purple to make it easy to spot un-rendered regions. | |
628 glClearColor(223.0 / 255, 0, 1, 1); | |
629 glClear(GL_COLOR_BUFFER_BIT); | |
630 } | |
631 #endif | |
632 } | |
633 | |
634 void CompositorGL::OnNotifyEnd() { | |
635 TRACE_EVENT0("ui", "CompositorGL::OnNotifyEnd"); | |
636 DCHECK(started_); | |
637 gl_surface_->SwapBuffers(); | |
638 started_ = false; | |
639 } | |
640 | |
641 void CompositorGL::Blur(const gfx::Rect& bounds) { | |
642 NOTIMPLEMENTED(); | |
643 } | |
644 | |
645 // static | |
646 Compositor* Compositor::Create(CompositorDelegate* owner, | |
647 gfx::AcceleratedWidget widget, | |
648 const gfx::Size& size) { | |
649 if (SharedResourcesGL::GetInstance() == NULL) | |
650 return NULL; | |
651 else | |
652 return new CompositorGL(owner, widget, size); | |
653 } | |
654 | |
655 } // namespace ui | |
OLD | NEW |