blob: 80a21376e90c73fb53b170764f6728f061ea063b [file] [log] [blame]
reed@android.com86d40082010-02-12 17:17:10 +00001/*
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 SkLayer_DEFINED
18#define SkLayer_DEFINED
19
20#include "SkRefCnt.h"
21#include "SkTDArray.h"
22#include "SkColor.h"
reed@android.com81dc3312010-02-18 19:32:03 +000023#include "SkMatrix.h"
reed@android.com86d40082010-02-12 17:17:10 +000024#include "SkPoint.h"
25#include "SkRect.h"
26#include "SkSize.h"
27
28class SkCanvas;
reed@android.com86d40082010-02-12 17:17:10 +000029
30class SkLayer : public SkRefCnt {
31
32public:
33 SkLayer();
34 SkLayer(const SkLayer&);
35 virtual ~SkLayer();
36
reed@android.com8381e002010-03-23 20:10:46 +000037 bool isInheritFromRootTransform() const;
reed@android.com81dc3312010-02-18 19:32:03 +000038 SkScalar getOpacity() const { return m_opacity; }
reed@android.comda6fb322010-02-19 21:41:30 +000039 const SkSize& getSize() const { return m_size; }
reed@android.com81dc3312010-02-18 19:32:03 +000040 const SkPoint& getPosition() const { return m_position; }
reed@android.com81dc3312010-02-18 19:32:03 +000041 const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
reed@android.com81dc3312010-02-18 19:32:03 +000042 const SkMatrix& getMatrix() const { return fMatrix; }
reed@android.com81dc3312010-02-18 19:32:03 +000043 const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
reed@android.comda6fb322010-02-19 21:41:30 +000044
45 SkScalar getWidth() const { return m_size.width(); }
46 SkScalar getHeight() const { return m_size.height(); }
47
reed@android.com8381e002010-03-23 20:10:46 +000048 void setInheritFromRootTransform(bool);
reed@android.comda6fb322010-02-19 21:41:30 +000049 void setOpacity(SkScalar opacity) { m_opacity = opacity; }
50 void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
51 void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
52 void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
53 void setMatrix(const SkMatrix&);
reed@android.com81dc3312010-02-18 19:32:03 +000054 void setChildrenMatrix(const SkMatrix&);
reed@android.com86d40082010-02-12 17:17:10 +000055
56 // children
57
reed@android.com745bfbd2010-02-24 17:16:35 +000058 /** Return the number of layers in our child list.
59 */
reed@android.com86d40082010-02-12 17:17:10 +000060 int countChildren() const;
reed@android.com745bfbd2010-02-24 17:16:35 +000061
62 /** Return the child at the specified index (starting at 0). This does not
63 affect the reference count of the child.
64 */
reed@android.com86d40082010-02-12 17:17:10 +000065 SkLayer* getChild(int index) const;
reed@android.com745bfbd2010-02-24 17:16:35 +000066
67 /** Add this layer to our child list at the end (top-most), and ref() it.
68 If it was already in another hierarchy, remove it from that list.
69 Return the new child.
70 */
reed@android.com86d40082010-02-12 17:17:10 +000071 SkLayer* addChild(SkLayer* child);
reed@android.com745bfbd2010-02-24 17:16:35 +000072
reed@android.com8381e002010-03-23 20:10:46 +000073 /** Remove this layer from its parent's list (or do nothing if it has no
74 parent.) If it had a parent, then unref() is called.
reed@android.com745bfbd2010-02-24 17:16:35 +000075 */
reed@android.com8381e002010-03-23 20:10:46 +000076 void detachFromParent();
reed@android.com745bfbd2010-02-24 17:16:35 +000077
78 /** Remove, and unref(), all of the layers in our child list.
79 */
reed@android.com86d40082010-02-12 17:17:10 +000080 void removeChildren();
81
reed@android.com745bfbd2010-02-24 17:16:35 +000082 /** Return our parent layer, or NULL if we have none.
83 */
84 SkLayer* getParent() const { return fParent; }
85
86 /** Return the root layer in this hiearchy. If this layer is the root
87 (i.e. has no parent), then this returns itself.
88 */
89 SkLayer* getRootLayer() const;
90
91 // coordinate system transformations
92
93 /** Return, in matrix, the matix transfomations that are applied locally
94 when this layer draws (i.e. its position and matrix/anchorPoint).
95 This does not include the childrenMatrix, since that is only applied
96 after this layer draws (but before its children draw).
97 */
98 void getLocalTransform(SkMatrix* matrix) const;
99
100 /** Return, in matrix, the concatenation of transforms that are applied
101 from this layer's root parent to the layer itself.
102 This is the matrix that is applied to the layer during drawing.
103 */
104 void localToGlobal(SkMatrix* matrix) const;
105
reed@android.com86d40082010-02-12 17:17:10 +0000106 // paint method
107
reed@android.comda6fb322010-02-19 21:41:30 +0000108 void draw(SkCanvas*, SkScalar opacity);
109 void draw(SkCanvas* canvas) {
110 this->draw(canvas, SK_Scalar1);
111 }
reed@android.com81dc3312010-02-18 19:32:03 +0000112
113protected:
reed@android.comda6fb322010-02-19 21:41:30 +0000114 virtual void onDraw(SkCanvas*, SkScalar opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000115
116private:
reed@android.com8381e002010-03-23 20:10:46 +0000117 enum Flags {
118 kInheritFromRootTransform_Flag = 0x01
119 };
120
reed@android.com745bfbd2010-02-24 17:16:35 +0000121 SkLayer* fParent;
reed@android.comda6fb322010-02-19 21:41:30 +0000122 SkScalar m_opacity;
123 SkSize m_size;
124 SkPoint m_position;
125 SkPoint m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +0000126 SkMatrix fMatrix;
127 SkMatrix fChildrenMatrix;
reed@android.com8381e002010-03-23 20:10:46 +0000128 uint32_t fFlags;
reed@android.com86d40082010-02-12 17:17:10 +0000129
reed@android.com86d40082010-02-12 17:17:10 +0000130 SkTDArray<SkLayer*> m_children;
reed@google.comc9218432011-01-25 19:05:12 +0000131
132 typedef SkRefCnt INHERITED;
reed@android.com86d40082010-02-12 17:17:10 +0000133};
134
135#endif