1 // Copyright 2013 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.
5 #include "webkit/renderer/compositor_bindings/web_layer_impl_fixed_bounds.h"
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"
16 WebLayerImplFixedBounds::WebLayerImplFixedBounds() {
19 WebLayerImplFixedBounds::WebLayerImplFixedBounds(scoped_refptr<Layer> layer)
20 : WebLayerImpl(layer) {
24 WebLayerImplFixedBounds::~WebLayerImplFixedBounds() {
27 void WebLayerImplFixedBounds::invalidateRect(const blink::WebFloatRect& rect) {
28 // Partial invalidations seldom occur for such layers.
29 // Simply invalidate the whole layer to avoid transformation of coordinates.
33 void WebLayerImplFixedBounds::setAnchorPoint(
34 const blink::WebFloatPoint& anchor_point) {
35 if (anchor_point != this->anchorPoint()) {
36 layer_->SetAnchorPoint(anchor_point);
37 UpdateLayerBoundsAndTransform();
41 void WebLayerImplFixedBounds::setBounds(const blink::WebSize& bounds) {
42 if (original_bounds_ != gfx::Size(bounds)) {
43 original_bounds_ = bounds;
44 UpdateLayerBoundsAndTransform();
48 blink::WebSize WebLayerImplFixedBounds::bounds() const {
49 return original_bounds_;
52 void WebLayerImplFixedBounds::setTransform(const SkMatrix44& matrix) {
53 gfx::Transform transform;
54 transform.matrix() = matrix;
55 SetTransformInternal(transform);
58 SkMatrix44 WebLayerImplFixedBounds::transform() const {
59 return original_transform_.matrix();
62 void WebLayerImplFixedBounds::SetFixedBounds(gfx::Size fixed_bounds) {
63 if (fixed_bounds_ != fixed_bounds) {
64 fixed_bounds_ = fixed_bounds;
65 UpdateLayerBoundsAndTransform();
69 void WebLayerImplFixedBounds::SetTransformInternal(
70 const gfx::Transform& transform) {
71 if (original_transform_ != transform) {
72 original_transform_ = transform;
73 UpdateLayerBoundsAndTransform();
77 void WebLayerImplFixedBounds::UpdateLayerBoundsAndTransform() {
78 if (fixed_bounds_.IsEmpty() || original_bounds_.IsEmpty() ||
79 fixed_bounds_ == original_bounds_ ||
80 // For now fall back to non-fixed bounds for non-zero anchor point.
81 // TODO(wangxianzhu): Support non-zero anchor point for fixed bounds.
82 anchorPoint().x || anchorPoint().y) {
83 layer_->SetBounds(original_bounds_);
84 layer_->SetTransform(original_transform_);
88 layer_->SetBounds(fixed_bounds_);
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);
100 } // namespace webkit