• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef Layer_DEFINED
18 #define Layer_DEFINED
19 
20 #include "SkRefCnt.h"
21 #include "SkTDArray.h"
22 #include "SkColor.h"
23 #include "SkMatrix.h"
24 #include "SkPoint.h"
25 #include "SkRect.h"
26 #include "SkSize.h"
27 
28 class SkCanvas;
29 
30 class Layer : public SkRefCnt {
31 
32 public:
33     Layer();
34     Layer(const Layer&);
35     virtual ~Layer();
36 
37     // Whether the layer should apply its tranform directly onto the root
38     // layer, rather than using the transforms of all ancestor layers. This is
39     // used for fixed position layers.
shouldInheritFromRootTransform()40     bool shouldInheritFromRootTransform() const { return m_shouldInheritFromRootTransform; }
getOpacity()41     SkScalar getOpacity() const { return m_opacity; }
getSize()42     const SkSize& getSize() const { return m_size; }
getPosition()43     const SkPoint& getPosition() const { return m_position; }
getAnchorPoint()44     const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
getMatrix()45     const SkMatrix& getMatrix() const { return m_matrix; }
getChildrenMatrix()46     const SkMatrix& getChildrenMatrix() const { return m_childrenMatrix; }
47 
getWidth()48     SkScalar getWidth() const { return m_size.width(); }
getHeight()49     SkScalar getHeight() const { return m_size.height(); }
50 
setShouldInheritFromRootTransform(bool inherit)51     void setShouldInheritFromRootTransform(bool inherit) { m_shouldInheritFromRootTransform = inherit; }
setOpacity(SkScalar opacity)52     void setOpacity(SkScalar opacity) { m_opacity = opacity; }
setSize(SkScalar w,SkScalar h)53     void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
setPosition(SkScalar x,SkScalar y)54     void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
setAnchorPoint(SkScalar x,SkScalar y)55     void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
setMatrix(const SkMatrix & matrix)56     void setMatrix(const SkMatrix& matrix) { m_matrix = matrix; }
setChildrenMatrix(const SkMatrix & matrix)57     void setChildrenMatrix(const SkMatrix& matrix) { m_childrenMatrix = matrix; }
58 
59     // children
60 
61     /** Return the number of layers in our child list.
62      */
63     int countChildren() const;
64 
65     /** Return the child at the specified index (starting at 0). This does not
66         affect the reference count of the child.
67      */
68     Layer* getChild(int index) const;
69 
70     /** Add this layer to our child list at the end (top-most), and ref() it.
71         If it was already in another hierarchy, remove it from that list.
72         Return the new child.
73      */
74     Layer* addChild(Layer* child);
75 
76     /** Remove this layer from its parent's list (or do nothing if it has no
77         parent.) If it had a parent, then unref() is called.
78      */
79     void detachFromParent();
80 
81     /** Remove, and unref(), all of the layers in our child list.
82      */
83     void removeChildren();
84 
85     /** Return our parent layer, or NULL if we have none.
86      */
getParent()87     Layer* getParent() const { return fParent; }
88 
89     /** Return the root layer in this hiearchy. If this layer is the root
90         (i.e. has no parent), then this returns itself.
91      */
92     Layer* getRootLayer() const;
93 
94     // coordinate system transformations
95 
96     /** Return, in matrix, the matix transfomations that are applied locally
97         when this layer draws (i.e. its position and matrix/anchorPoint).
98         This does not include the childrenMatrix, since that is only applied
99         after this layer draws (but before its children draw).
100      */
101     void getLocalTransform(SkMatrix* matrix) const;
102 
103     /** Return, in matrix, the concatenation of transforms that are applied
104         from this layer's root parent to the layer itself.
105         This is the matrix that is applied to the layer during drawing.
106      */
localToGlobal(SkMatrix * matrix)107     void localToGlobal(SkMatrix* matrix) const { localToAncestor(0, matrix); }
108 
109     /** Return, as a matrix, the transform that converts from this layer's local
110         space to the space of the given ancestor layer. Use NULL for ancestor to
111         represent the root layer. Note that this method must not be called on a
112         fixed position layer with ancestor != NULL.
113 
114         For non-fixed position layers, the following holds (in pseudo-code for
115         brevity) ...
116         SkMatrix localToAncestor = layer->localToAncestor(ancestor);
117         SkMatrix ancestorToGlobal = ancestor->localToAncestor(NULL);
118         SkMatrix localToGlobal = layer->localToGlobal();
119         ASSERT(localToAncestor * ancestorToGlobal == localToGlobal);
120      */
121     void localToAncestor(const Layer* ancestor, SkMatrix* matrix) const;
122 
123     // paint method
124 
125     void draw(SkCanvas*, SkScalar opacity);
draw(SkCanvas * canvas)126     void draw(SkCanvas* canvas) {
127         this->draw(canvas, SK_Scalar1);
128     }
129 
setHasOverflowChildren(bool value)130     void setHasOverflowChildren(bool value) { m_hasOverflowChildren = value; }
131 
contentIsScrollable()132     virtual bool contentIsScrollable() const { return false; }
133 
134 protected:
135     virtual void onDraw(SkCanvas*, SkScalar opacity);
136 
137     bool m_hasOverflowChildren;
138 
139     bool isAncestor(const Layer*) const;
140 
141     Layer* fParent;
142     SkScalar m_opacity;
143     SkSize m_size;
144     // The position of the origin of the layer, relative to the parent layer.
145     SkPoint m_position;
146     // The point in the layer used as the origin for local transformations,
147     // expressed as a fraction of the layer size.
148     SkPoint m_anchorPoint;
149     SkMatrix m_matrix;
150     SkMatrix m_childrenMatrix;
151     bool m_shouldInheritFromRootTransform;
152 
153     SkTDArray<Layer*> m_children;
154 
155     typedef SkRefCnt INHERITED;
156 };
157 
158 #endif
159