| 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/WebKit/chromium/public/platform/WebContentLa
yer.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebContentLa
yer.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebExternalT
extureLayer.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebExternalT
extureLayer.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin
t.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin
t.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect
.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect
.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSolidColo
rLayer.h" |
| 18 #include "ui/base/animation/animation.h" | 19 #include "ui/base/animation/animation.h" |
| 19 #include "ui/gfx/canvas_skia.h" | 20 #include "ui/gfx/canvas_skia.h" |
| 20 #include "ui/gfx/compositor/compositor_switches.h" | 21 #include "ui/gfx/compositor/compositor_switches.h" |
| 21 #include "ui/gfx/compositor/layer_animator.h" | 22 #include "ui/gfx/compositor/layer_animator.h" |
| 22 #include "ui/gfx/interpolated_transform.h" | 23 #include "ui/gfx/interpolated_transform.h" |
| 23 #include "ui/gfx/point3.h" | 24 #include "ui/gfx/point3.h" |
| 24 | 25 |
| 25 #include "ui/gfx/compositor/compositor_cc.h" | 26 #include "ui/gfx/compositor/compositor_cc.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const float EPSILON = 1e-3f; | 30 const float EPSILON = 1e-3f; |
| 30 | 31 |
| 31 bool IsApproximateMultilpleOf(float value, float base) { | 32 bool IsApproximateMultipleOf(float value, float base) { |
| 32 float remainder = fmod(fabs(value), base); | 33 float remainder = fmod(fabs(value), base); |
| 33 return remainder < EPSILON || base - remainder < EPSILON; | 34 return remainder < EPSILON || base - remainder < EPSILON; |
| 34 } | 35 } |
| 35 | 36 |
| 36 const ui::Layer* GetRoot(const ui::Layer* layer) { | 37 const ui::Layer* GetRoot(const ui::Layer* layer) { |
| 37 return layer->parent() ? GetRoot(layer->parent()) : layer; | 38 return layer->parent() ? GetRoot(layer->parent()) : layer; |
| 38 } | 39 } |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 namespace ui { | 43 namespace ui { |
| 43 | 44 |
| 44 Layer::Layer() | 45 Layer::Layer() |
| 45 : type_(LAYER_HAS_TEXTURE), | 46 : type_(LAYER_TEXTURED), |
| 46 compositor_(NULL), | 47 compositor_(NULL), |
| 47 parent_(NULL), | 48 parent_(NULL), |
| 48 visible_(true), | 49 visible_(true), |
| 49 fills_bounds_opaquely_(true), | 50 fills_bounds_opaquely_(true), |
| 50 layer_updated_externally_(false), | 51 layer_updated_externally_(false), |
| 51 opacity_(1.0f), | 52 opacity_(1.0f), |
| 52 delegate_(NULL) { | 53 delegate_(NULL) { |
| 53 CreateWebLayer(); | 54 CreateWebLayer(); |
| 54 } | 55 } |
| 55 | 56 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 187 } |
| 187 | 188 |
| 188 bool Layer::IsDrawn() const { | 189 bool Layer::IsDrawn() const { |
| 189 const Layer* layer = this; | 190 const Layer* layer = this; |
| 190 while (layer && layer->visible_) | 191 while (layer && layer->visible_) |
| 191 layer = layer->parent_; | 192 layer = layer->parent_; |
| 192 return layer == NULL; | 193 return layer == NULL; |
| 193 } | 194 } |
| 194 | 195 |
| 195 bool Layer::ShouldDraw() const { | 196 bool Layer::ShouldDraw() const { |
| 196 return type_ == LAYER_HAS_TEXTURE && GetCombinedOpacity() > 0.0f; | 197 return type_ != LAYER_NOT_DRAWN && GetCombinedOpacity() > 0.0f; |
| 197 } | 198 } |
| 198 | 199 |
| 199 // static | 200 // static |
| 200 void Layer::ConvertPointToLayer(const Layer* source, | 201 void Layer::ConvertPointToLayer(const Layer* source, |
| 201 const Layer* target, | 202 const Layer* target, |
| 202 gfx::Point* point) { | 203 gfx::Point* point) { |
| 203 if (source == target) | 204 if (source == target) |
| 204 return; | 205 return; |
| 205 | 206 |
| 206 const Layer* root_layer = GetRoot(source); | 207 const Layer* root_layer = GetRoot(source); |
| 207 CHECK_EQ(root_layer, GetRoot(target)); | 208 CHECK_EQ(root_layer, GetRoot(target)); |
| 208 | 209 |
| 209 if (source != root_layer) | 210 if (source != root_layer) |
| 210 source->ConvertPointForAncestor(root_layer, point); | 211 source->ConvertPointForAncestor(root_layer, point); |
| 211 if (target != root_layer) | 212 if (target != root_layer) |
| 212 target->ConvertPointFromAncestor(root_layer, point); | 213 target->ConvertPointFromAncestor(root_layer, point); |
| 213 } | 214 } |
| 214 | 215 |
| 215 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 216 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
| 216 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 217 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
| 217 return; | 218 return; |
| 218 | 219 |
| 219 fills_bounds_opaquely_ = fills_bounds_opaquely; | 220 fills_bounds_opaquely_ = fills_bounds_opaquely; |
| 220 | 221 |
| 221 web_layer_.setOpaque(fills_bounds_opaquely); | 222 web_layer_.setOpaque(fills_bounds_opaquely); |
| 222 RecomputeDebugBorderColor(); | 223 RecomputeDebugBorderColor(); |
| 223 } | 224 } |
| 224 | 225 |
| 225 void Layer::SetExternalTexture(ui::Texture* texture) { | 226 void Layer::SetExternalTexture(ui::Texture* texture) { |
| 227 DCHECK_EQ(type_, LAYER_TEXTURED); |
| 226 layer_updated_externally_ = !!texture; | 228 layer_updated_externally_ = !!texture; |
| 227 texture_ = texture; | 229 texture_ = texture; |
| 228 if (web_layer_is_accelerated_ != layer_updated_externally_) { | 230 if (web_layer_is_accelerated_ != layer_updated_externally_) { |
| 229 // Switch to a different type of layer. | 231 // Switch to a different type of layer. |
| 230 web_layer_.removeAllChildren(); | 232 web_layer_.removeAllChildren(); |
| 231 WebKit::WebLayer new_layer; | 233 WebKit::WebLayer new_layer; |
| 232 if (layer_updated_externally_) | 234 if (layer_updated_externally_) |
| 233 new_layer = WebKit::WebExternalTextureLayer::create(); | 235 new_layer = WebKit::WebExternalTextureLayer::create(); |
| 234 else | 236 else |
| 235 new_layer = WebKit::WebContentLayer::create(this); | 237 new_layer = WebKit::WebContentLayer::create(this); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 257 web_layer_.to<WebKit::WebExternalTextureLayer>(); | 259 web_layer_.to<WebKit::WebExternalTextureLayer>(); |
| 258 texture_layer.setFlipped(texture_cc->flipped()); | 260 texture_layer.setFlipped(texture_cc->flipped()); |
| 259 } | 261 } |
| 260 RecomputeDrawsContentAndUVRect(); | 262 RecomputeDrawsContentAndUVRect(); |
| 261 } | 263 } |
| 262 | 264 |
| 263 void Layer::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) { | 265 void Layer::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) { |
| 264 NOTREACHED(); | 266 NOTREACHED(); |
| 265 } | 267 } |
| 266 | 268 |
| 269 void Layer::SetColor(SkColor color) { |
| 270 DCHECK_EQ(type_, LAYER_SOLID_COLOR); |
| 271 // WebColor is equivalent to SkColor, per WebColor.h. |
| 272 web_layer_.to<WebKit::WebSolidColorLayer>().setBackgroundColor( |
| 273 static_cast<WebKit::WebColor>(color)); |
| 274 SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); |
| 275 } |
| 276 |
| 267 void Layer::SchedulePaint(const gfx::Rect& invalid_rect) { | 277 void Layer::SchedulePaint(const gfx::Rect& invalid_rect) { |
| 278 if (type_ == LAYER_SOLID_COLOR) |
| 279 return; |
| 280 |
| 268 WebKit::WebFloatRect web_rect( | 281 WebKit::WebFloatRect web_rect( |
| 269 invalid_rect.x(), | 282 invalid_rect.x(), |
| 270 invalid_rect.y(), | 283 invalid_rect.y(), |
| 271 invalid_rect.width(), | 284 invalid_rect.width(), |
| 272 invalid_rect.height()); | 285 invalid_rect.height()); |
| 273 if (!web_layer_is_accelerated_) | 286 if (!web_layer_is_accelerated_) |
| 274 web_layer_.to<WebKit::WebContentLayer>().invalidateRect(web_rect); | 287 web_layer_.to<WebKit::WebContentLayer>().invalidateRect(web_rect); |
| 275 else | 288 else |
| 276 web_layer_.to<WebKit::WebExternalTextureLayer>().invalidateRect(web_rect); | 289 web_layer_.to<WebKit::WebExternalTextureLayer>().invalidateRect(web_rect); |
| 277 } | 290 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 return; | 342 return; |
| 330 | 343 |
| 331 ui::Transform current_transform; | 344 ui::Transform current_transform; |
| 332 if (transform().HasChange()) | 345 if (transform().HasChange()) |
| 333 current_transform.ConcatTransform(transform()); | 346 current_transform.ConcatTransform(transform()); |
| 334 current_transform.ConcatTranslate( | 347 current_transform.ConcatTranslate( |
| 335 static_cast<float>(bounds().x()), | 348 static_cast<float>(bounds().x()), |
| 336 static_cast<float>(bounds().y())); | 349 static_cast<float>(bounds().y())); |
| 337 current_transform.ConcatTransform(parent_transform); | 350 current_transform.ConcatTransform(parent_transform); |
| 338 | 351 |
| 339 if (fills_bounds_opaquely_ && type_ == LAYER_HAS_TEXTURE) { | 352 if (fills_bounds_opaquely_ && type_ != LAYER_NOT_DRAWN) { |
| 340 LayerProperties properties; | 353 LayerProperties properties; |
| 341 properties.layer = this; | 354 properties.layer = this; |
| 342 properties.transform_relative_to_root = current_transform; | 355 properties.transform_relative_to_root = current_transform; |
| 343 traversal->push_back(properties); | 356 traversal->push_back(properties); |
| 344 } | 357 } |
| 345 | 358 |
| 346 for (size_t i = 0; i < children_.size(); i++) | 359 for (size_t i = 0; i < children_.size(); i++) |
| 347 children_[i]->GetLayerProperties(current_transform, traversal); | 360 children_[i]->GetLayerProperties(current_transform, traversal); |
| 348 } | 361 } |
| 349 | 362 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 448 |
| 436 const Transform& Layer::GetTransformForAnimation() const { | 449 const Transform& Layer::GetTransformForAnimation() const { |
| 437 return transform(); | 450 return transform(); |
| 438 } | 451 } |
| 439 | 452 |
| 440 float Layer::GetOpacityForAnimation() const { | 453 float Layer::GetOpacityForAnimation() const { |
| 441 return opacity(); | 454 return opacity(); |
| 442 } | 455 } |
| 443 | 456 |
| 444 void Layer::CreateWebLayer() { | 457 void Layer::CreateWebLayer() { |
| 445 web_layer_ = WebKit::WebContentLayer::create(this); | 458 if (type_ == LAYER_SOLID_COLOR) |
| 459 web_layer_ = WebKit::WebSolidColorLayer::create(); |
| 460 else |
| 461 web_layer_ = WebKit::WebContentLayer::create(this); |
| 446 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); | 462 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); |
| 447 web_layer_.setOpaque(true); | 463 web_layer_.setOpaque(true); |
| 448 web_layer_is_accelerated_ = false; | 464 web_layer_is_accelerated_ = false; |
| 449 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( | 465 show_debug_borders_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 450 switches::kUIShowLayerBorders); | 466 switches::kUIShowLayerBorders); |
| 451 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); | 467 web_layer_.setDebugBorderWidth(show_debug_borders_ ? 2 : 0); |
| 452 RecomputeDrawsContentAndUVRect(); | 468 RecomputeDrawsContentAndUVRect(); |
| 453 RecomputeDebugBorderColor(); | 469 RecomputeDebugBorderColor(); |
| 454 } | 470 } |
| 455 | 471 |
| 456 void Layer::RecomputeTransform() { | 472 void Layer::RecomputeTransform() { |
| 457 ui::Transform transform = transform_; | 473 ui::Transform transform = transform_; |
| 458 transform.ConcatTranslate(bounds_.x(), bounds_.y()); | 474 transform.ConcatTranslate(bounds_.x(), bounds_.y()); |
| 459 web_layer_.setTransform(transform.matrix()); | 475 web_layer_.setTransform(transform.matrix()); |
| 460 } | 476 } |
| 461 | 477 |
| 462 void Layer::RecomputeDrawsContentAndUVRect() { | 478 void Layer::RecomputeDrawsContentAndUVRect() { |
| 463 DCHECK(!web_layer_.isNull()); | 479 DCHECK(!web_layer_.isNull()); |
| 464 bool should_draw = type_ == LAYER_HAS_TEXTURE; | 480 bool should_draw = type_ != LAYER_NOT_DRAWN; |
| 465 if (!web_layer_is_accelerated_) { | 481 if (!web_layer_is_accelerated_) { |
| 466 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); | 482 if (type_ != LAYER_SOLID_COLOR) |
| 483 web_layer_.to<WebKit::WebContentLayer>().setDrawsContent(should_draw); |
| 467 web_layer_.setBounds(bounds_.size()); | 484 web_layer_.setBounds(bounds_.size()); |
| 468 } else { | 485 } else { |
| 469 DCHECK(texture_); | 486 DCHECK(texture_); |
| 470 TextureCC* texture_cc = static_cast<TextureCC*>(texture_.get()); | 487 TextureCC* texture_cc = static_cast<TextureCC*>(texture_.get()); |
| 471 unsigned int texture_id = texture_cc->texture_id(); | 488 unsigned int texture_id = texture_cc->texture_id(); |
| 472 WebKit::WebExternalTextureLayer texture_layer = | 489 WebKit::WebExternalTextureLayer texture_layer = |
| 473 web_layer_.to<WebKit::WebExternalTextureLayer>(); | 490 web_layer_.to<WebKit::WebExternalTextureLayer>(); |
| 474 texture_layer.setTextureId(should_draw ? texture_id : 0); | 491 texture_layer.setTextureId(should_draw ? texture_id : 0); |
| 475 gfx::Size size(std::min(bounds_.width(), texture_cc->size().width()), | 492 gfx::Size size(std::min(bounds_.width(), texture_cc->size().width()), |
| 476 std::min(bounds_.height(), texture_cc->size().height())); | 493 std::min(bounds_.height(), texture_cc->size().height())); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 489 return; | 506 return; |
| 490 unsigned int color = 0xFF000000; | 507 unsigned int color = 0xFF000000; |
| 491 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; | 508 color |= web_layer_is_accelerated_ ? 0x0000FF00 : 0x00FF0000; |
| 492 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); | 509 bool opaque = fills_bounds_opaquely_ && (GetCombinedOpacity() == 1.f); |
| 493 if (!opaque) | 510 if (!opaque) |
| 494 color |= 0xFF; | 511 color |= 0xFF; |
| 495 web_layer_.setDebugBorderColor(color); | 512 web_layer_.setDebugBorderColor(color); |
| 496 } | 513 } |
| 497 | 514 |
| 498 } // namespace ui | 515 } // namespace ui |
| OLD | NEW |