OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "webkit/compositor_bindings/web_layer_impl_fixed_bounds.h" |
| 6 |
| 7 #include "cc/layer.h" |
| 8 #include "third_party/WebKit/Source/Platform/chromium/public/WebFloatPoint.h" |
| 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
| 10 #include "third_party/WebKit/Source/Platform/chromium/public/WebTransformationMa
trix.h" |
| 11 #include "third_party/skia/include/utils/SkMatrix44.h" |
| 12 #include "webkit/compositor_bindings/web_transformation_matrix_util.h" |
| 13 |
| 14 using cc::Layer; |
| 15 using webkit::WebTransformationMatrixUtil; |
| 16 |
| 17 namespace WebKit { |
| 18 |
| 19 WebLayerImplFixedBounds::WebLayerImplFixedBounds() |
| 20 { |
| 21 } |
| 22 |
| 23 WebLayerImplFixedBounds::WebLayerImplFixedBounds(scoped_refptr<Layer> layer) |
| 24 : WebLayerImpl(layer) |
| 25 { |
| 26 } |
| 27 |
| 28 |
| 29 WebLayerImplFixedBounds::~WebLayerImplFixedBounds() |
| 30 { |
| 31 } |
| 32 |
| 33 void WebLayerImplFixedBounds::invalidateRect(const WebFloatRect& rect) |
| 34 { |
| 35 // Partial invalidations seldom occur for such layers. |
| 36 // Simply invalidate the whole layer to avoid transformation of coordinates. |
| 37 invalidate(); |
| 38 } |
| 39 |
| 40 void WebLayerImplFixedBounds::setAnchorPoint(const WebFloatPoint& anchorPoint) |
| 41 { |
| 42 if (anchorPoint != this->anchorPoint()) { |
| 43 m_layer->setAnchorPoint(anchorPoint); |
| 44 updateBoundsScaleTransform(); |
| 45 updateLayerBoundsAndTransform(); |
| 46 } |
| 47 } |
| 48 |
| 49 void WebLayerImplFixedBounds::setBounds(const WebSize& bounds) |
| 50 { |
| 51 if (m_originalBounds != gfx::Size(bounds)) { |
| 52 m_originalBounds = bounds; |
| 53 updateBoundsScaleTransform(); |
| 54 updateLayerBoundsAndTransform(); |
| 55 } |
| 56 } |
| 57 |
| 58 WebSize WebLayerImplFixedBounds::bounds() const |
| 59 { |
| 60 return m_originalBounds; |
| 61 } |
| 62 |
| 63 void WebLayerImplFixedBounds::setSublayerTransform(const SkMatrix44& matrix) |
| 64 { |
| 65 gfx::Transform transform; |
| 66 transform.matrix() = matrix; |
| 67 setSublayerTransformInternal(transform); |
| 68 } |
| 69 |
| 70 void WebLayerImplFixedBounds::setSublayerTransform(const WebTransformationMatrix
& matrix) |
| 71 { |
| 72 setSublayerTransformInternal(WebTransformationMatrixUtil::ToTransform(matrix
)); |
| 73 } |
| 74 |
| 75 SkMatrix44 WebLayerImplFixedBounds::sublayerTransform() const |
| 76 { |
| 77 return m_originalSublayerTransform.matrix(); |
| 78 } |
| 79 |
| 80 void WebLayerImplFixedBounds::setTransform(const SkMatrix44& matrix) |
| 81 { |
| 82 gfx::Transform transform; |
| 83 transform.matrix() = matrix; |
| 84 setTransformInternal(transform); |
| 85 } |
| 86 |
| 87 void WebLayerImplFixedBounds::setTransform(const WebTransformationMatrix& matrix
) |
| 88 { |
| 89 setTransformInternal(WebTransformationMatrixUtil::ToTransform(matrix)); |
| 90 } |
| 91 |
| 92 SkMatrix44 WebLayerImplFixedBounds::transform() const |
| 93 { |
| 94 return m_originalTransform.matrix(); |
| 95 } |
| 96 |
| 97 void WebLayerImplFixedBounds::setFixedBounds(const gfx::Size& fixedBounds) |
| 98 { |
| 99 if (m_fixedBounds != fixedBounds) { |
| 100 m_fixedBounds = fixedBounds; |
| 101 updateBoundsScaleTransform(); |
| 102 updateLayerBoundsAndTransform(); |
| 103 } |
| 104 } |
| 105 |
| 106 void WebLayerImplFixedBounds::setSublayerTransformInternal(const gfx::Transform&
transform) |
| 107 { |
| 108 if (m_originalSublayerTransform != transform) { |
| 109 m_originalSublayerTransform = transform; |
| 110 updateLayerBoundsAndTransform(); |
| 111 } |
| 112 } |
| 113 |
| 114 void WebLayerImplFixedBounds::setTransformInternal(const gfx::Transform& transfo
rm) |
| 115 { |
| 116 if (m_originalTransform != transform) { |
| 117 m_originalTransform = transform; |
| 118 updateLayerBoundsAndTransform(); |
| 119 } |
| 120 } |
| 121 |
| 122 void WebLayerImplFixedBounds::updateLayerBoundsAndTransform() |
| 123 { |
| 124 m_layer->setBounds(m_fixedBounds); |
| 125 |
| 126 // Apply the bounds scale on original transform. |
| 127 m_layer->setTransform(m_originalTransform * m_boundsScaleTransform); |
| 128 |
| 129 // Undo the bounds scale for sublayer transform. |
| 130 gfx::Transform inverseTransform; |
| 131 if (m_boundsScaleTransform.GetInverse(&inverseTransform)) |
| 132 m_layer->setSublayerTransform(m_originalSublayerTransform * inverseTrans
form); |
| 133 else |
| 134 NOTREACHED() << "Bounds scale is not invertible" << m_boundsScaleTransfo
rm.ToString(); |
| 135 } |
| 136 |
| 137 void WebLayerImplFixedBounds::updateBoundsScaleTransform() |
| 138 { |
| 139 m_boundsScaleTransform.MakeIdentity(); |
| 140 |
| 141 if (m_fixedBounds.IsEmpty() || m_originalBounds.IsEmpty() || m_fixedBounds =
= m_originalBounds) |
| 142 return; |
| 143 |
| 144 // Scale caused by bounds change always scales from the top-left of the laye
r |
| 145 // despite the anchor point, so exclude the anchor point from boundsScale. |
| 146 gfx::PointF anchorPoint = m_layer->anchorPoint(); |
| 147 float anchorOffsetX = anchorPoint.x() * m_fixedBounds.width(); |
| 148 float anchorOffsetY = anchorPoint.y() * m_fixedBounds.height(); |
| 149 m_boundsScaleTransform.Translate(-anchorOffsetX, -anchorOffsetY); |
| 150 m_boundsScaleTransform.Scale( |
| 151 static_cast<float>(m_originalBounds.width()) / m_fixedBounds.width(), |
| 152 static_cast<float>(m_originalBounds.height()) / m_fixedBounds.height()); |
| 153 m_boundsScaleTransform.Translate(anchorOffsetX, anchorOffsetY); |
| 154 } |
| 155 |
| 156 } // namespace WebKit |
OLD | NEW |