1 // Copyright 2014 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 "cc/blink/web_layer_impl_fixed_bounds.h" 6 7 #include "cc/layers/layer.h" 8 #include "third_party/WebKit/public/platform/WebFloatPoint.h" 9 #include "third_party/WebKit/public/platform/WebSize.h" 10 #include "third_party/skia/include/utils/SkMatrix44.h" 11 12 using cc::Layer; 13 14 namespace cc_blink { 15 16 WebLayerImplFixedBounds::WebLayerImplFixedBounds() { 17 } 18 19 WebLayerImplFixedBounds::WebLayerImplFixedBounds(scoped_refptr<Layer> layer) 20 : WebLayerImpl(layer) { 21 } 22 23 WebLayerImplFixedBounds::~WebLayerImplFixedBounds() { 24 } 25 26 void WebLayerImplFixedBounds::invalidateRect(const blink::WebFloatRect& rect) { 27 // Partial invalidations seldom occur for such layers. 28 // Simply invalidate the whole layer to avoid transformation of coordinates. 29 invalidate(); 30 } 31 32 void WebLayerImplFixedBounds::setTransformOrigin( 33 const blink::WebFloatPoint3D& transform_origin) { 34 if (transform_origin != this->transformOrigin()) { 35 layer_->SetTransformOrigin(transform_origin); 36 UpdateLayerBoundsAndTransform(); 37 } 38 } 39 40 void WebLayerImplFixedBounds::setBounds(const blink::WebSize& bounds) { 41 if (original_bounds_ != gfx::Size(bounds)) { 42 original_bounds_ = bounds; 43 UpdateLayerBoundsAndTransform(); 44 } 45 } 46 47 blink::WebSize WebLayerImplFixedBounds::bounds() const { 48 return original_bounds_; 49 } 50 51 void WebLayerImplFixedBounds::setTransform(const SkMatrix44& matrix) { 52 gfx::Transform transform; 53 transform.matrix() = matrix; 54 SetTransformInternal(transform); 55 } 56 57 SkMatrix44 WebLayerImplFixedBounds::transform() const { 58 return original_transform_.matrix(); 59 } 60 61 void WebLayerImplFixedBounds::SetFixedBounds(gfx::Size fixed_bounds) { 62 if (fixed_bounds_ != fixed_bounds) { 63 fixed_bounds_ = fixed_bounds; 64 UpdateLayerBoundsAndTransform(); 65 } 66 } 67 68 void WebLayerImplFixedBounds::SetTransformInternal( 69 const gfx::Transform& transform) { 70 if (original_transform_ != transform) { 71 original_transform_ = transform; 72 UpdateLayerBoundsAndTransform(); 73 } 74 } 75 76 void WebLayerImplFixedBounds::UpdateLayerBoundsAndTransform() { 77 if (fixed_bounds_.IsEmpty() || original_bounds_.IsEmpty() || 78 fixed_bounds_ == original_bounds_ || 79 // For now fall back to non-fixed bounds for non-zero transform origin. 80 // TODO(wangxianzhu): Support non-zero anchor point for fixed bounds. 81 transformOrigin().x || 82 transformOrigin().y) { 83 layer_->SetBounds(original_bounds_); 84 layer_->SetTransform(original_transform_); 85 return; 86 } 87 88 layer_->SetBounds(fixed_bounds_); 89 90 // Apply bounds scale (bounds/fixed_bounds) over original transform. 91 gfx::Transform transform_with_bounds_scale(original_transform_); 92 float bounds_scale_x = 93 static_cast<float>(original_bounds_.width()) / fixed_bounds_.width(); 94 float bounds_scale_y = 95 static_cast<float>(original_bounds_.height()) / fixed_bounds_.height(); 96 transform_with_bounds_scale.Scale(bounds_scale_x, bounds_scale_y); 97 layer_->SetTransform(transform_with_bounds_scale); 98 } 99 100 } // namespace cc_blink 101