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