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), | |
56 device_scale_factor_(0.0f) { | |
54 CreateWebLayer(); | 57 CreateWebLayer(); |
55 } | 58 } |
56 | 59 |
57 Layer::Layer(LayerType type) | 60 Layer::Layer(LayerType type) |
58 : type_(type), | 61 : type_(type), |
59 compositor_(NULL), | 62 compositor_(NULL), |
60 parent_(NULL), | 63 parent_(NULL), |
61 visible_(true), | 64 visible_(true), |
62 fills_bounds_opaquely_(true), | 65 fills_bounds_opaquely_(true), |
63 layer_updated_externally_(false), | 66 layer_updated_externally_(false), |
64 opacity_(1.0f), | 67 opacity_(1.0f), |
65 delegate_(NULL) { | 68 delegate_(NULL), |
69 scale_canvas_(true), | |
70 device_scale_factor_(0.0f) { | |
66 CreateWebLayer(); | 71 CreateWebLayer(); |
67 } | 72 } |
68 | 73 |
69 Layer::~Layer() { | 74 Layer::~Layer() { |
70 // Destroying the animator may cause observers to use the layer (and | 75 // Destroying the animator may cause observers to use the layer (and |
71 // indirectly the WebLayer). Destroy the animator first so that the WebLayer | 76 // indirectly the WebLayer). Destroy the animator first so that the WebLayer |
72 // is still around. | 77 // is still around. |
73 animator_.reset(); | 78 animator_.reset(); |
74 if (compositor_) | 79 if (compositor_) |
75 compositor_->SetRootLayer(NULL); | 80 compositor_->SetRootLayer(NULL); |
76 if (parent_) | 81 if (parent_) |
77 parent_->Remove(this); | 82 parent_->Remove(this); |
78 for (size_t i = 0; i < children_.size(); ++i) | 83 for (size_t i = 0; i < children_.size(); ++i) |
79 children_[i]->parent_ = NULL; | 84 children_[i]->parent_ = NULL; |
80 web_layer_.removeFromParent(); | 85 web_layer_.removeFromParent(); |
81 } | 86 } |
82 | 87 |
83 Compositor* Layer::GetCompositor() { | 88 Compositor* Layer::GetCompositor() { |
84 return GetRoot(this)->compositor_; | 89 return GetRoot(this)->compositor_; |
85 } | 90 } |
86 | 91 |
87 void Layer::SetCompositor(Compositor* compositor) { | 92 void Layer::SetCompositor(Compositor* compositor) { |
88 // This function must only be called to set the compositor on the root layer, | 93 // This function must only be called to set the compositor on the root layer, |
89 // or to reset it. | 94 // or to reset it. |
90 DCHECK(!compositor || !compositor_); | 95 DCHECK(!compositor || !compositor_); |
91 DCHECK(!compositor || compositor->root_layer() == this); | 96 DCHECK(!compositor || compositor->root_layer() == this); |
92 DCHECK(!parent_); | 97 DCHECK(!parent_); |
93 compositor_ = compositor; | 98 compositor_ = compositor; |
99 if (IsDIPEnabled() && | |
100 compositor && device_scale_factor_ != compositor->device_scale_factor()) { | |
piman
2012/05/08 00:28:46
nit: you may not need the device_scale_factor_ !=
| |
101 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); | |
102 } | |
94 } | 103 } |
95 | 104 |
96 void Layer::Add(Layer* child) { | 105 void Layer::Add(Layer* child) { |
97 DCHECK(!child->compositor_); | 106 DCHECK(!child->compositor_); |
98 if (child->parent_) | 107 if (child->parent_) |
99 child->parent_->Remove(child); | 108 child->parent_->Remove(child); |
100 child->parent_ = this; | 109 child->parent_ = this; |
101 children_.push_back(child); | 110 children_.push_back(child); |
102 web_layer_.addChild(child->web_layer_); | 111 web_layer_.addChild(child->web_layer_); |
112 if (IsDIPEnabled() && device_scale_factor_ != child->device_scale_factor_) | |
piman
2012/05/08 00:28:46
nit: Same here, there's already an early out in On
| |
113 child->OnDeviceScaleFactorChanged(device_scale_factor_); | |
103 } | 114 } |
104 | 115 |
105 void Layer::Remove(Layer* child) { | 116 void Layer::Remove(Layer* child) { |
106 std::vector<Layer*>::iterator i = | 117 std::vector<Layer*>::iterator i = |
107 std::find(children_.begin(), children_.end(), child); | 118 std::find(children_.begin(), children_.end(), child); |
108 DCHECK(i != children_.end()); | 119 DCHECK(i != children_.end()); |
109 children_.erase(i); | 120 children_.erase(i); |
110 child->parent_ = NULL; | 121 child->parent_ = NULL; |
111 child->web_layer_.removeFromParent(); | 122 child->web_layer_.removeFromParent(); |
112 } | 123 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 // static | 238 // static |
228 void Layer::ConvertPointToLayer(const Layer* source, | 239 void Layer::ConvertPointToLayer(const Layer* source, |
229 const Layer* target, | 240 const Layer* target, |
230 gfx::Point* point) { | 241 gfx::Point* point) { |
231 if (source == target) | 242 if (source == target) |
232 return; | 243 return; |
233 | 244 |
234 const Layer* root_layer = GetRoot(source); | 245 const Layer* root_layer = GetRoot(source); |
235 CHECK_EQ(root_layer, GetRoot(target)); | 246 CHECK_EQ(root_layer, GetRoot(target)); |
236 | 247 |
248 // TODO(oshima): We probably need to handle source's root != target's root | |
249 // case under multi monitor environment. | |
237 if (source != root_layer) | 250 if (source != root_layer) |
238 source->ConvertPointForAncestor(root_layer, point); | 251 source->ConvertPointForAncestor(root_layer, point); |
239 if (target != root_layer) | 252 if (target != root_layer) |
240 target->ConvertPointFromAncestor(root_layer, point); | 253 target->ConvertPointFromAncestor(root_layer, point); |
241 } | 254 } |
242 | 255 |
243 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 256 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
244 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 257 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
245 return; | 258 return; |
246 | 259 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 DCHECK_EQ(type_, LAYER_SOLID_COLOR); | 303 DCHECK_EQ(type_, LAYER_SOLID_COLOR); |
291 // WebColor is equivalent to SkColor, per WebColor.h. | 304 // WebColor is equivalent to SkColor, per WebColor.h. |
292 web_layer_.to<WebKit::WebSolidColorLayer>().setBackgroundColor( | 305 web_layer_.to<WebKit::WebSolidColorLayer>().setBackgroundColor( |
293 static_cast<WebKit::WebColor>(color)); | 306 static_cast<WebKit::WebColor>(color)); |
294 SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); | 307 SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); |
295 } | 308 } |
296 | 309 |
297 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { | 310 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { |
298 if (type_ == LAYER_SOLID_COLOR || !delegate_) | 311 if (type_ == LAYER_SOLID_COLOR || !delegate_) |
299 return false; | 312 return false; |
300 damaged_region_.op(invalid_rect.x(), | 313 gfx::Rect invalid_rect_in_pixel = ConvertRectToPixel(this, invalid_rect); |
301 invalid_rect.y(), | 314 damaged_region_.op(invalid_rect_in_pixel.x(), |
302 invalid_rect.right(), | 315 invalid_rect_in_pixel.y(), |
303 invalid_rect.bottom(), | 316 invalid_rect_in_pixel.right(), |
317 invalid_rect_in_pixel.bottom(), | |
304 SkRegion::kUnion_Op); | 318 SkRegion::kUnion_Op); |
305 ScheduleDraw(); | 319 ScheduleDraw(); |
306 return true; | 320 return true; |
307 } | 321 } |
308 | 322 |
309 void Layer::ScheduleDraw() { | 323 void Layer::ScheduleDraw() { |
310 Compositor* compositor = GetCompositor(); | 324 Compositor* compositor = GetCompositor(); |
311 if (compositor) | 325 if (compositor) |
312 compositor->ScheduleDraw(); | 326 compositor->ScheduleDraw(); |
313 } | 327 } |
(...skipping 21 matching lines...) Expand all Loading... | |
335 } | 349 } |
336 | 350 |
337 void Layer::SuppressPaint() { | 351 void Layer::SuppressPaint() { |
338 if (!delegate_) | 352 if (!delegate_) |
339 return; | 353 return; |
340 delegate_ = NULL; | 354 delegate_ = NULL; |
341 for (size_t i = 0; i < children_.size(); ++i) | 355 for (size_t i = 0; i < children_.size(); ++i) |
342 children_[i]->SuppressPaint(); | 356 children_[i]->SuppressPaint(); |
343 } | 357 } |
344 | 358 |
359 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { | |
360 CHECK(IsDIPEnabled()); | |
361 if (device_scale_factor_ == device_scale_factor) | |
362 return; | |
363 device_scale_factor_ = device_scale_factor; | |
364 RecomputeTransform(); | |
365 RecomputeDrawsContentAndUVRect(); | |
366 for (size_t i = 0; i < children_.size(); ++i) | |
367 children_[i]->OnDeviceScaleFactorChanged(device_scale_factor); | |
368 } | |
369 | |
345 void Layer::paintContents(WebKit::WebCanvas* web_canvas, | 370 void Layer::paintContents(WebKit::WebCanvas* web_canvas, |
346 const WebKit::WebRect& clip) { | 371 const WebKit::WebRect& clip) { |
347 TRACE_EVENT0("ui", "Layer::paintContents"); | 372 TRACE_EVENT0("ui", "Layer::paintContents"); |
348 gfx::Canvas canvas(web_canvas); | 373 gfx::Canvas canvas(web_canvas); |
374 bool scale_canvas = IsDIPEnabled() && scale_canvas_; | |
375 if (scale_canvas) { | |
376 canvas.sk_canvas()->scale(SkFloatToScalar(device_scale_factor_), | |
377 SkFloatToScalar(device_scale_factor_)); | |
378 } | |
349 if (delegate_) | 379 if (delegate_) |
350 delegate_->OnPaintLayer(&canvas); | 380 delegate_->OnPaintLayer(&canvas); |
381 if (scale_canvas) | |
382 canvas.Restore(); | |
351 } | 383 } |
352 | 384 |
353 float Layer::GetCombinedOpacity() const { | 385 float Layer::GetCombinedOpacity() const { |
354 float opacity = opacity_; | 386 float opacity = opacity_; |
355 Layer* current = this->parent_; | 387 Layer* current = this->parent_; |
356 while (current) { | 388 while (current) { |
357 opacity *= current->opacity_; | 389 opacity *= current->opacity_; |
358 current = current->parent_; | 390 current = current->parent_; |
359 } | 391 } |
360 return opacity; | 392 return opacity; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 if (type_ == LAYER_SOLID_COLOR) | 530 if (type_ == LAYER_SOLID_COLOR) |
499 web_layer_ = WebKit::WebSolidColorLayer::create(); | 531 web_layer_ = WebKit::WebSolidColorLayer::create(); |
500 else | 532 else |
501 web_layer_ = WebKit::WebContentLayer::create(this); | 533 web_layer_ = WebKit::WebContentLayer::create(this); |
502 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); | 534 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); |
503 web_layer_.setOpaque(true); | 535 web_layer_.setOpaque(true); |
504 web_layer_is_accelerated_ = false; | 536 web_layer_is_accelerated_ = false; |
505 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( | 537 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( |
506 switches::kUIShowLayerBorders); | 538 switches::kUIShowLayerBorders); |
507 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); | 539 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); |
508 RecomputeDrawsContentAndUVRect(); | |
509 RecomputeDebugBorderColor(); | |
510 } | 540 } |
511 | 541 |
512 void Layer::RecomputeTransform() { | 542 void Layer::RecomputeTransform() { |
513 ui::Transform transform = transform_; | 543 if (IsDIPEnabled()) { |
514 transform.ConcatTranslate(bounds_.x(), bounds_.y()); | 544 ui::Transform scale_translate; |
515 web_layer_.setTransform(transform.matrix()); | 545 scale_translate.matrix().set3x3(device_scale_factor_, 0, 0, |
546 0, device_scale_factor_, 0, | |
547 0, 0, 1); | |
548 // Start with the inverse matrix of above. | |
549 Transform transform; | |
550 transform.matrix().set3x3(1.0f / device_scale_factor_, 0, 0, | |
551 0, 1.0f / device_scale_factor_, 0, | |
552 0, 0, 1); | |
553 transform.ConcatTransform(transform_); | |
554 transform.ConcatTranslate(bounds_.x(), bounds_.y()); | |
555 transform.ConcatTransform(scale_translate); | |
556 web_layer_.setTransform(transform.matrix()); | |
557 } else { | |
558 Transform t = transform_; | |
559 t.ConcatTranslate(bounds_.x(), bounds_.y()); | |
560 web_layer_.setTransform(t.matrix()); | |
561 } | |
516 } | 562 } |
517 | 563 |
518 void Layer::RecomputeDrawsContentAndUVRect() { | 564 void Layer::RecomputeDrawsContentAndUVRect() { |
519 DCHECK(!web_layer_.isNull()); | 565 DCHECK(!web_layer_.isNull()); |
520 bool should_draw = type_ != LAYER_NOT_DRAWN; | 566 bool should_draw = type_ != LAYER_NOT_DRAWN; |
521 if (!web_layer_is_accelerated_) { | 567 if (!web_layer_is_accelerated_) { |
522 if (type_ != LAYER_SOLID_COLOR) | 568 if (type_ != LAYER_SOLID_COLOR) |
523 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); | 569 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); |
524 web_layer_.setBounds(bounds_.size()); | 570 web_layer_.setBounds(ConvertSizeToPixel(this, bounds_.size())); |
525 } else { | 571 } else { |
526 DCHECK(texture_); | 572 DCHECK(texture_); |
527 unsigned int texture_id = texture_->texture_id(); | 573 unsigned int texture_id = texture_->texture_id(); |
528 WebKit::WebExternalTextureLayer texture_layer = | 574 WebKit::WebExternalTextureLayer texture_layer = |
529 web_layer_.to<WebKit::WebExternalTextureLayer>(); | 575 web_layer_.to<WebKit::WebExternalTextureLayer>(); |
530 texture_layer.setTextureId(should_draw ? texture_id : 0); | 576 texture_layer.setTextureId(should_draw ? texture_id : 0); |
577 gfx::Rect bounds_in_pixel = ConvertRectToPixel(this, bounds()); | |
531 gfx::Size texture_size = texture_->size(); | 578 gfx::Size texture_size = texture_->size(); |
532 gfx::Size size(std::min(bounds_.width(), texture_size.width()), | 579 gfx::Size size(std::min(bounds_in_pixel.width(), texture_size.width()), |
533 std::min(bounds_.height(), texture_size.height())); | 580 std::min(bounds_in_pixel.height(), texture_size.height())); |
534 WebKit::WebFloatRect rect( | 581 WebKit::WebFloatRect rect( |
535 0, | 582 0, |
536 0, | 583 0, |
537 static_cast<float>(size.width())/texture_size.width(), | 584 static_cast<float>(size.width())/texture_size.width(), |
538 static_cast<float>(size.height())/texture_size.height()); | 585 static_cast<float>(size.height())/texture_size.height()); |
539 texture_layer.setUVRect(rect); | 586 texture_layer.setUVRect(rect); |
540 web_layer_.setBounds(size); | 587 web_layer_.setBounds(size); |
541 } | 588 } |
542 } | 589 } |
543 | 590 |
544 void Layer::RecomputeDebugBorderColor() { | 591 void Layer::RecomputeDebugBorderColor() { |
545 if (!show_debug_borders_) | 592 if (!show_debug_borders_) |
546 return; | 593 return; |
547 unsigned int color = 0xFF000000; | 594 unsigned int color = 0xFF000000; |
548 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; | 595 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; |
549 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); | 596 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); |
550 if (!opaque) | 597 if (!opaque) |
551 color |= 0xFF; | 598 color |= 0xFF; |
552 web_layer_.setDebugBorderColor(color); | 599 web_layer_.setDebugBorderColor(color); |
553 } | 600 } |
554 | 601 |
555 } // namespace ui | 602 } // namespace ui |
OLD | NEW |