OLD | NEW |
---|---|
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 "ui/gfx/compositor/layer.h" | 5 #include "ui/gfx/compositor/layer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayer.h" | 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayer.h" |
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebExternalTextureL ayer.h" | 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebExternalTextureL ayer.h" |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation. h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation. h" |
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations .h" | 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations .h" |
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebFloatPoint.h" | 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebFloatPoint.h" |
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebFloatRect.h" | 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebFloatRect.h" |
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | 19 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
20 #include "third_party/WebKit/Source/Platform/chromium/public/WebSolidColorLayer. h" | 20 #include "third_party/WebKit/Source/Platform/chromium/public/WebSolidColorLayer. h" |
21 #include "ui/base/animation/animation.h" | 21 #include "ui/base/animation/animation.h" |
22 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
23 #include "ui/gfx/compositor/compositor_switches.h" | 23 #include "ui/gfx/compositor/compositor_switches.h" |
24 #include "ui/gfx/compositor/dip_util.h" | |
24 #include "ui/gfx/compositor/layer_animator.h" | 25 #include "ui/gfx/compositor/layer_animator.h" |
25 #include "ui/gfx/interpolated_transform.h" | 26 #include "ui/gfx/interpolated_transform.h" |
26 #include "ui/gfx/point3.h" | 27 #include "ui/gfx/point3.h" |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 const float EPSILON = 1e-3f; | 31 const float EPSILON = 1e-3f; |
31 | 32 |
32 bool IsApproximateMultipleOf(float value, float base) { | 33 bool IsApproximateMultipleOf(float value, float base) { |
33 float remainder = fmod(fabs(value), base); | 34 float remainder = fmod(fabs(value), base); |
34 return remainder < EPSILON || base - remainder < EPSILON; | 35 return remainder < EPSILON || base - remainder < EPSILON; |
35 } | 36 } |
36 | 37 |
37 const ui::Layer* GetRoot(const ui::Layer* layer) { | 38 const ui::Layer* GetRoot(const ui::Layer* layer) { |
38 return layer->parent() ? GetRoot(layer->parent()) : layer; | 39 return layer->parent() ? GetRoot(layer->parent()) : layer; |
39 } | 40 } |
40 | 41 |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 namespace ui { | 44 namespace ui { |
44 | 45 |
45 Layer::Layer() | 46 Layer::Layer() |
46 : type_(LAYER_TEXTURED), | 47 : type_(LAYER_TEXTURED), |
47 compositor_(NULL), | 48 compositor_(NULL), |
48 parent_(NULL), | 49 parent_(NULL), |
49 visible_(true), | 50 visible_(true), |
50 fills_bounds_opaquely_(true), | 51 fills_bounds_opaquely_(true), |
51 layer_updated_externally_(false), | 52 layer_updated_externally_(false), |
52 opacity_(1.0f), | 53 opacity_(1.0f), |
53 delegate_(NULL) { | 54 delegate_(NULL), |
55 scale_canvas_(true) { | |
54 CreateWebLayer(); | 56 CreateWebLayer(); |
55 } | 57 } |
56 | 58 |
57 Layer::Layer(LayerType type) | 59 Layer::Layer(LayerType type) |
58 : type_(type), | 60 : type_(type), |
59 compositor_(NULL), | 61 compositor_(NULL), |
60 parent_(NULL), | 62 parent_(NULL), |
61 visible_(true), | 63 visible_(true), |
62 fills_bounds_opaquely_(true), | 64 fills_bounds_opaquely_(true), |
63 layer_updated_externally_(false), | 65 layer_updated_externally_(false), |
64 opacity_(1.0f), | 66 opacity_(1.0f), |
65 delegate_(NULL) { | 67 delegate_(NULL), |
68 scale_canvas_(true) { | |
66 CreateWebLayer(); | 69 CreateWebLayer(); |
67 } | 70 } |
68 | 71 |
69 Layer::~Layer() { | 72 Layer::~Layer() { |
70 // Destroying the animator may cause observers to use the layer (and | 73 // Destroying the animator may cause observers to use the layer (and |
71 // indirectly the WebLayer). Destroy the animator first so that the WebLayer | 74 // indirectly the WebLayer). Destroy the animator first so that the WebLayer |
72 // is still around. | 75 // is still around. |
73 animator_.reset(); | 76 animator_.reset(); |
74 if (compositor_) | 77 if (compositor_) |
75 compositor_->SetRootLayer(NULL); | 78 compositor_->SetRootLayer(NULL); |
(...skipping 17 matching lines...) Expand all Loading... | |
93 compositor_ = compositor; | 96 compositor_ = compositor; |
94 } | 97 } |
95 | 98 |
96 void Layer::Add(Layer* child) { | 99 void Layer::Add(Layer* child) { |
97 DCHECK(!child->compositor_); | 100 DCHECK(!child->compositor_); |
98 if (child->parent_) | 101 if (child->parent_) |
99 child->parent_->Remove(child); | 102 child->parent_->Remove(child); |
100 child->parent_ = this; | 103 child->parent_ = this; |
101 children_.push_back(child); | 104 children_.push_back(child); |
102 web_layer_.addChild(child->web_layer_); | 105 web_layer_.addChild(child->web_layer_); |
106 child->UpdateLayerSize(); | |
103 } | 107 } |
104 | 108 |
105 void Layer::Remove(Layer* child) { | 109 void Layer::Remove(Layer* child) { |
106 std::vector<Layer*>::iterator i = | 110 std::vector<Layer*>::iterator i = |
107 std::find(children_.begin(), children_.end(), child); | 111 std::find(children_.begin(), children_.end(), child); |
108 DCHECK(i != children_.end()); | 112 DCHECK(i != children_.end()); |
109 children_.erase(i); | 113 children_.erase(i); |
110 child->parent_ = NULL; | 114 child->parent_ = NULL; |
111 child->web_layer_.removeFromParent(); | 115 child->web_layer_.removeFromParent(); |
112 } | 116 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 if (animator_.get() && animator_->IsAnimatingProperty( | 163 if (animator_.get() && animator_->IsAnimatingProperty( |
160 LayerAnimationElement::TRANSFORM)) | 164 LayerAnimationElement::TRANSFORM)) |
161 return animator_->GetTargetTransform(); | 165 return animator_->GetTargetTransform(); |
162 return transform_; | 166 return transform_; |
163 } | 167 } |
164 | 168 |
165 void Layer::SetBounds(const gfx::Rect& bounds) { | 169 void Layer::SetBounds(const gfx::Rect& bounds) { |
166 GetAnimator()->SetBounds(bounds); | 170 GetAnimator()->SetBounds(bounds); |
167 } | 171 } |
168 | 172 |
173 gfx::Rect Layer::GetBoundsInPixel() const { | |
174 return ConvertRectToPixel(this, bounds()); | |
175 } | |
176 | |
177 void Layer::SetBoundsInPixel(const gfx::Rect& bounds_in_pixel) { | |
178 SetBounds(ConvertRectToDIP(this, bounds_in_pixel)); | |
179 } | |
180 | |
169 gfx::Rect Layer::GetTargetBounds() const { | 181 gfx::Rect Layer::GetTargetBounds() const { |
170 if (animator_.get() && animator_->IsAnimatingProperty( | 182 if (animator_.get() && animator_->IsAnimatingProperty( |
171 LayerAnimationElement::BOUNDS)) | 183 LayerAnimationElement::BOUNDS)) |
172 return animator_->GetTargetBounds(); | 184 return animator_->GetTargetBounds(); |
173 return bounds_; | 185 return bounds_; |
174 } | 186 } |
175 | 187 |
176 void Layer::SetMasksToBounds(bool masks_to_bounds) { | 188 void Layer::SetMasksToBounds(bool masks_to_bounds) { |
177 web_layer_.setMasksToBounds(masks_to_bounds); | 189 web_layer_.setMasksToBounds(masks_to_bounds); |
178 } | 190 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 // static | 239 // static |
228 void Layer::ConvertPointToLayer(const Layer* source, | 240 void Layer::ConvertPointToLayer(const Layer* source, |
229 const Layer* target, | 241 const Layer* target, |
230 gfx::Point* point) { | 242 gfx::Point* point) { |
231 if (source == target) | 243 if (source == target) |
232 return; | 244 return; |
233 | 245 |
234 const Layer* root_layer = GetRoot(source); | 246 const Layer* root_layer = GetRoot(source); |
235 CHECK_EQ(root_layer, GetRoot(target)); | 247 CHECK_EQ(root_layer, GetRoot(target)); |
236 | 248 |
249 // TODO(oshima): We probably need to handle source's root != target's root | |
250 // case under multi monitor environment. | |
piman
2012/05/04 18:46:06
FYI, I think the plan is to have a separate root f
| |
237 if (source != root_layer) | 251 if (source != root_layer) |
238 source->ConvertPointForAncestor(root_layer, point); | 252 source->ConvertPointForAncestor(root_layer, point); |
239 if (target != root_layer) | 253 if (target != root_layer) |
240 target->ConvertPointFromAncestor(root_layer, point); | 254 target->ConvertPointFromAncestor(root_layer, point); |
241 } | 255 } |
242 | 256 |
243 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 257 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
244 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 258 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
245 return; | 259 return; |
246 | 260 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 DCHECK_EQ(type_, LAYER_SOLID_COLOR); | 304 DCHECK_EQ(type_, LAYER_SOLID_COLOR); |
291 // WebColor is equivalent to SkColor, per WebColor.h. | 305 // WebColor is equivalent to SkColor, per WebColor.h. |
292 web_layer_.to<WebKit::WebSolidColorLayer>().setBackgroundColor( | 306 web_layer_.to<WebKit::WebSolidColorLayer>().setBackgroundColor( |
293 static_cast<WebKit::WebColor>(color)); | 307 static_cast<WebKit::WebColor>(color)); |
294 SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); | 308 SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); |
295 } | 309 } |
296 | 310 |
297 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { | 311 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { |
298 if (type_ == LAYER_SOLID_COLOR || !delegate_) | 312 if (type_ == LAYER_SOLID_COLOR || !delegate_) |
299 return false; | 313 return false; |
300 damaged_region_.op(invalid_rect.x(), | 314 gfx::Rect invalid_rect_in_pixel = ConvertRectToPixel(this, invalid_rect); |
301 invalid_rect.y(), | 315 damaged_region_.op(invalid_rect_in_pixel.x(), |
302 invalid_rect.right(), | 316 invalid_rect_in_pixel.y(), |
303 invalid_rect.bottom(), | 317 invalid_rect_in_pixel.right(), |
318 invalid_rect_in_pixel.bottom(), | |
304 SkRegion::kUnion_Op); | 319 SkRegion::kUnion_Op); |
305 ScheduleDraw(); | 320 ScheduleDraw(); |
306 return true; | 321 return true; |
307 } | 322 } |
308 | 323 |
309 void Layer::ScheduleDraw() { | 324 void Layer::ScheduleDraw() { |
310 Compositor* compositor = GetCompositor(); | 325 Compositor* compositor = GetCompositor(); |
311 if (compositor) | 326 if (compositor) |
312 compositor->ScheduleDraw(); | 327 compositor->ScheduleDraw(); |
313 } | 328 } |
(...skipping 21 matching lines...) Expand all Loading... | |
335 } | 350 } |
336 | 351 |
337 void Layer::SuppressPaint() { | 352 void Layer::SuppressPaint() { |
338 if (!delegate_) | 353 if (!delegate_) |
339 return; | 354 return; |
340 delegate_ = NULL; | 355 delegate_ = NULL; |
341 for (size_t i = 0; i < children_.size(); ++i) | 356 for (size_t i = 0; i < children_.size(); ++i) |
342 children_[i]->SuppressPaint(); | 357 children_[i]->SuppressPaint(); |
343 } | 358 } |
344 | 359 |
360 void Layer::UpdateLayerSize() { | |
361 if (IsDrawn()) | |
362 ScheduleDraw(); | |
piman
2012/05/04 18:46:06
Let's not force this, except maybe if we explicitl
| |
363 RecomputeTransform(); | |
364 RecomputeDrawsContentAndUVRect(); | |
365 for (size_t i = 0; i < children_.size(); ++i) | |
366 children_[i]->UpdateLayerSize(); | |
367 } | |
368 | |
345 void Layer::paintContents(WebKit::WebCanvas* web_canvas, | 369 void Layer::paintContents(WebKit::WebCanvas* web_canvas, |
346 const WebKit::WebRect& clip) { | 370 const WebKit::WebRect& clip) { |
347 TRACE_EVENT0("ui", "Layer::paintContents"); | 371 TRACE_EVENT0("ui", "Layer::paintContents"); |
348 gfx::Canvas canvas(web_canvas); | 372 gfx::Canvas canvas(web_canvas); |
373 bool scale_canvas = IsDIPEnabled() && scale_canvas_; | |
374 if (scale_canvas) { | |
375 float scale = GetDeviceScaleFactor(this); | |
376 canvas.sk_canvas()->scale(SkFloatToScalar(scale), SkFloatToScalar(scale)); | |
377 } | |
349 if (delegate_) | 378 if (delegate_) |
350 delegate_->OnPaintLayer(&canvas); | 379 delegate_->OnPaintLayer(&canvas); |
380 if (scale_canvas) | |
381 canvas.Restore(); | |
351 } | 382 } |
352 | 383 |
353 float Layer::GetCombinedOpacity() const { | 384 float Layer::GetCombinedOpacity() const { |
354 float opacity = opacity_; | 385 float opacity = opacity_; |
355 Layer* current = this->parent_; | 386 Layer* current = this->parent_; |
356 while (current) { | 387 while (current) { |
357 opacity *= current->opacity_; | 388 opacity *= current->opacity_; |
358 current = current->parent_; | 389 current = current->parent_; |
359 } | 390 } |
360 return opacity; | 391 return opacity; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
503 web_layer_.setOpaque(true); | 534 web_layer_.setOpaque(true); |
504 web_layer_is_accelerated_ = false; | 535 web_layer_is_accelerated_ = false; |
505 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( | 536 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( |
506 switches::kUIShowLayerBorders); | 537 switches::kUIShowLayerBorders); |
507 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); | 538 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); |
508 RecomputeDrawsContentAndUVRect(); | 539 RecomputeDrawsContentAndUVRect(); |
509 RecomputeDebugBorderColor(); | 540 RecomputeDebugBorderColor(); |
510 } | 541 } |
511 | 542 |
512 void Layer::RecomputeTransform() { | 543 void Layer::RecomputeTransform() { |
513 ui::Transform transform = transform_; | 544 Compositor* compositor = GetCompositor(); |
piman
2012/05/04 18:46:06
Is there a way we could avoid walking the layer tr
oshima
2012/05/04 21:08:06
Given that layer already have compositor_ and I ha
| |
514 transform.ConcatTranslate(bounds_.x(), bounds_.y()); | 545 Transform t; |
515 web_layer_.setTransform(transform.matrix()); | 546 if (compositor && IsDIPEnabled()) { |
547 t = compositor->GetTranslateTransform(transform_, bounds_.origin()); | |
548 } else { | |
549 t = transform_; | |
550 t.ConcatTranslate(bounds_.x(), bounds_.y()); | |
551 } | |
552 web_layer_.setTransform(t.matrix()); | |
516 } | 553 } |
517 | 554 |
518 void Layer::RecomputeDrawsContentAndUVRect() { | 555 void Layer::RecomputeDrawsContentAndUVRect() { |
519 DCHECK(!web_layer_.isNull()); | 556 DCHECK(!web_layer_.isNull()); |
520 bool should_draw = type_ != LAYER_NOT_DRAWN; | 557 bool should_draw = type_ != LAYER_NOT_DRAWN; |
521 if (!web_layer_is_accelerated_) { | 558 if (!web_layer_is_accelerated_) { |
522 if (type_ != LAYER_SOLID_COLOR) | 559 if (type_ != LAYER_SOLID_COLOR) |
523 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); | 560 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); |
524 web_layer_.setBounds(bounds_.size()); | 561 web_layer_.setBounds(ConvertSizeToPixel(this, bounds_.size())); |
piman
2012/05/04 18:46:06
This, like above, will have to walk the layer tree
| |
525 } else { | 562 } else { |
526 DCHECK(texture_); | 563 DCHECK(texture_); |
527 unsigned int texture_id = texture_->texture_id(); | 564 unsigned int texture_id = texture_->texture_id(); |
528 WebKit::WebExternalTextureLayer texture_layer = | 565 WebKit::WebExternalTextureLayer texture_layer = |
529 web_layer_.to<WebKit::WebExternalTextureLayer>(); | 566 web_layer_.to<WebKit::WebExternalTextureLayer>(); |
530 texture_layer.setTextureId(should_draw ? texture_id : 0); | 567 texture_layer.setTextureId(should_draw ? texture_id : 0); |
568 gfx::Rect bounds_in_pixel = GetBoundsInPixel(); | |
piman
2012/05/04 18:46:06
Same here
| |
531 gfx::Size texture_size = texture_->size(); | 569 gfx::Size texture_size = texture_->size(); |
532 gfx::Size size(std::min(bounds_.width(), texture_size.width()), | 570 gfx::Size size(std::min(bounds_in_pixel.width(), texture_size.width()), |
533 std::min(bounds_.height(), texture_size.height())); | 571 std::min(bounds_in_pixel.height(), texture_size.height())); |
534 WebKit::WebFloatRect rect( | 572 WebKit::WebFloatRect rect( |
535 0, | 573 0, |
536 0, | 574 0, |
537 static_cast<float>(size.width())/texture_size.width(), | 575 static_cast<float>(size.width())/texture_size.width(), |
538 static_cast<float>(size.height())/texture_size.height()); | 576 static_cast<float>(size.height())/texture_size.height()); |
539 texture_layer.setUVRect(rect); | 577 texture_layer.setUVRect(rect); |
540 web_layer_.setBounds(size); | 578 web_layer_.setBounds(size); |
541 } | 579 } |
542 } | 580 } |
543 | 581 |
544 void Layer::RecomputeDebugBorderColor() { | 582 void Layer::RecomputeDebugBorderColor() { |
545 if (!show_debug_borders_) | 583 if (!show_debug_borders_) |
546 return; | 584 return; |
547 unsigned int color = 0xFF000000; | 585 unsigned int color = 0xFF000000; |
548 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; | 586 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; |
549 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); | 587 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); |
550 if (!opaque) | 588 if (!opaque) |
551 color |= 0xFF; | 589 color |= 0xFF; |
552 web_layer_.setDebugBorderColor(color); | 590 web_layer_.setDebugBorderColor(color); |
553 } | 591 } |
554 | 592 |
555 } // namespace ui | 593 } // namespace ui |
OLD | NEW |