blob: c8dea484ddee0df1ca4ab3f49833d0810411ced5 [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.com81dc3312010-02-18 19:32:03 +000037 SkScalar getOpacity() const { return m_opacity; }
reed@android.comda6fb322010-02-19 21:41:30 +000038 const SkSize& getSize() const { return m_size; }
reed@android.com81dc3312010-02-18 19:32:03 +000039 const SkPoint& getPosition() const { return m_position; }
reed@android.com81dc3312010-02-18 19:32:03 +000040 const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
reed@android.com81dc3312010-02-18 19:32:03 +000041 const SkMatrix& getMatrix() const { return fMatrix; }
reed@android.com81dc3312010-02-18 19:32:03 +000042 const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
reed@android.comda6fb322010-02-19 21:41:30 +000043
44 SkScalar getWidth() const { return m_size.width(); }
45 SkScalar getHeight() const { return m_size.height(); }
46
47 void setOpacity(SkScalar opacity) { m_opacity = opacity; }
48 void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
49 void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
50 void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
51 void setMatrix(const SkMatrix&);
reed@android.com81dc3312010-02-18 19:32:03 +000052 void setChildrenMatrix(const SkMatrix&);
reed@android.com86d40082010-02-12 17:17:10 +000053
54 // children
55
reed@android.com745bfbd2010-02-24 17:16:35 +000056 /** Return the number of layers in our child list.
57 */
reed@android.com86d40082010-02-12 17:17:10 +000058 int countChildren() const;
reed@android.com745bfbd2010-02-24 17:16:35 +000059
60 /** Return the child at the specified index (starting at 0). This does not
61 affect the reference count of the child.
62 */
reed@android.com86d40082010-02-12 17:17:10 +000063 SkLayer* getChild(int index) const;
reed@android.com745bfbd2010-02-24 17:16:35 +000064
65 /** Add this layer to our child list at the end (top-most), and ref() it.
66 If it was already in another hierarchy, remove it from that list.
67 Return the new child.
68 */
reed@android.com86d40082010-02-12 17:17:10 +000069 SkLayer* addChild(SkLayer* child);
reed@android.com745bfbd2010-02-24 17:16:35 +000070
71 /** Remove this layer from our child list, and unref() it and return true.
72 If it is not in our child list, do nothing and return false.
73 */
74 bool removeChild(SkLayer* child);
75
76 /** Remove, and unref(), all of the layers in our child list.
77 */
reed@android.com86d40082010-02-12 17:17:10 +000078 void removeChildren();
79
reed@android.com745bfbd2010-02-24 17:16:35 +000080 /** Return our parent layer, or NULL if we have none.
81 */
82 SkLayer* getParent() const { return fParent; }
83
84 /** Return the root layer in this hiearchy. If this layer is the root
85 (i.e. has no parent), then this returns itself.
86 */
87 SkLayer* getRootLayer() const;
88
89 // coordinate system transformations
90
91 /** Return, in matrix, the matix transfomations that are applied locally
92 when this layer draws (i.e. its position and matrix/anchorPoint).
93 This does not include the childrenMatrix, since that is only applied
94 after this layer draws (but before its children draw).
95 */
96 void getLocalTransform(SkMatrix* matrix) const;
97
98 /** Return, in matrix, the concatenation of transforms that are applied
99 from this layer's root parent to the layer itself.
100 This is the matrix that is applied to the layer during drawing.
101 */
102 void localToGlobal(SkMatrix* matrix) const;
103
reed@android.com86d40082010-02-12 17:17:10 +0000104 // paint method
105
reed@android.comda6fb322010-02-19 21:41:30 +0000106 void draw(SkCanvas*, SkScalar opacity);
107 void draw(SkCanvas* canvas) {
108 this->draw(canvas, SK_Scalar1);
109 }
reed@android.com81dc3312010-02-18 19:32:03 +0000110
111protected:
reed@android.comda6fb322010-02-19 21:41:30 +0000112 virtual void onDraw(SkCanvas*, SkScalar opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000113
114private:
reed@android.com745bfbd2010-02-24 17:16:35 +0000115 SkLayer* fParent;
reed@android.comda6fb322010-02-19 21:41:30 +0000116 SkScalar m_opacity;
117 SkSize m_size;
118 SkPoint m_position;
119 SkPoint m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +0000120 SkMatrix fMatrix;
121 SkMatrix fChildrenMatrix;
reed@android.com86d40082010-02-12 17:17:10 +0000122
reed@android.com86d40082010-02-12 17:17:10 +0000123 SkTDArray<SkLayer*> m_children;
124};
125
126#endif