blob: 7fdc6b2208fa37419614d055a28c1018caacc855 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com86d40082010-02-12 17:17:10 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 The Android Open Source Project
reed@android.com86d40082010-02-12 17:17:10 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com86d40082010-02-12 17:17:10 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com86d40082010-02-12 17:17:10 +000010#ifndef SkLayer_DEFINED
11#define SkLayer_DEFINED
12
13#include "SkRefCnt.h"
14#include "SkTDArray.h"
15#include "SkColor.h"
reed@android.com81dc3312010-02-18 19:32:03 +000016#include "SkMatrix.h"
reed@android.com86d40082010-02-12 17:17:10 +000017#include "SkPoint.h"
18#include "SkRect.h"
19#include "SkSize.h"
20
21class SkCanvas;
reed@android.com86d40082010-02-12 17:17:10 +000022
23class SkLayer : public SkRefCnt {
24
25public:
mtklein2766c002015-06-26 11:45:03 -070026
robertphillips@google.coma22e2112012-08-16 14:58:06 +000027
reed@android.com86d40082010-02-12 17:17:10 +000028 SkLayer();
29 SkLayer(const SkLayer&);
30 virtual ~SkLayer();
31
reed@android.com8381e002010-03-23 20:10:46 +000032 bool isInheritFromRootTransform() const;
reed@android.com81dc3312010-02-18 19:32:03 +000033 SkScalar getOpacity() const { return m_opacity; }
reed@android.comda6fb322010-02-19 21:41:30 +000034 const SkSize& getSize() const { return m_size; }
reed@android.com81dc3312010-02-18 19:32:03 +000035 const SkPoint& getPosition() const { return m_position; }
reed@android.com81dc3312010-02-18 19:32:03 +000036 const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
reed@android.com81dc3312010-02-18 19:32:03 +000037 const SkMatrix& getMatrix() const { return fMatrix; }
reed@android.com81dc3312010-02-18 19:32:03 +000038 const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
reed@android.comda6fb322010-02-19 21:41:30 +000039
40 SkScalar getWidth() const { return m_size.width(); }
41 SkScalar getHeight() const { return m_size.height(); }
42
reed@android.com8381e002010-03-23 20:10:46 +000043 void setInheritFromRootTransform(bool);
reed@android.comda6fb322010-02-19 21:41:30 +000044 void setOpacity(SkScalar opacity) { m_opacity = opacity; }
45 void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
46 void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
47 void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
48 void setMatrix(const SkMatrix&);
reed@android.com81dc3312010-02-18 19:32:03 +000049 void setChildrenMatrix(const SkMatrix&);
reed@android.com86d40082010-02-12 17:17:10 +000050
51 // children
52
reed@android.com745bfbd2010-02-24 17:16:35 +000053 /** Return the number of layers in our child list.
54 */
reed@android.com86d40082010-02-12 17:17:10 +000055 int countChildren() const;
reed@android.com745bfbd2010-02-24 17:16:35 +000056
57 /** Return the child at the specified index (starting at 0). This does not
58 affect the reference count of the child.
59 */
reed@android.com86d40082010-02-12 17:17:10 +000060 SkLayer* getChild(int index) const;
reed@android.com745bfbd2010-02-24 17:16:35 +000061
62 /** Add this layer to our child list at the end (top-most), and ref() it.
63 If it was already in another hierarchy, remove it from that list.
64 Return the new child.
65 */
reed@android.com86d40082010-02-12 17:17:10 +000066 SkLayer* addChild(SkLayer* child);
reed@android.com745bfbd2010-02-24 17:16:35 +000067
reed@android.com8381e002010-03-23 20:10:46 +000068 /** Remove this layer from its parent's list (or do nothing if it has no
69 parent.) If it had a parent, then unref() is called.
reed@android.com745bfbd2010-02-24 17:16:35 +000070 */
reed@android.com8381e002010-03-23 20:10:46 +000071 void detachFromParent();
reed@android.com745bfbd2010-02-24 17:16:35 +000072
73 /** Remove, and unref(), all of the layers in our child list.
74 */
reed@android.com86d40082010-02-12 17:17:10 +000075 void removeChildren();
76
reed@android.com745bfbd2010-02-24 17:16:35 +000077 /** Return our parent layer, or NULL if we have none.
78 */
79 SkLayer* getParent() const { return fParent; }
80
81 /** Return the root layer in this hiearchy. If this layer is the root
82 (i.e. has no parent), then this returns itself.
83 */
84 SkLayer* getRootLayer() const;
85
86 // coordinate system transformations
87
88 /** Return, in matrix, the matix transfomations that are applied locally
89 when this layer draws (i.e. its position and matrix/anchorPoint).
90 This does not include the childrenMatrix, since that is only applied
91 after this layer draws (but before its children draw).
92 */
93 void getLocalTransform(SkMatrix* matrix) const;
94
95 /** Return, in matrix, the concatenation of transforms that are applied
96 from this layer's root parent to the layer itself.
97 This is the matrix that is applied to the layer during drawing.
98 */
99 void localToGlobal(SkMatrix* matrix) const;
100
reed@android.com86d40082010-02-12 17:17:10 +0000101 // paint method
102
reed@android.comda6fb322010-02-19 21:41:30 +0000103 void draw(SkCanvas*, SkScalar opacity);
104 void draw(SkCanvas* canvas) {
105 this->draw(canvas, SK_Scalar1);
106 }
reed@android.com81dc3312010-02-18 19:32:03 +0000107
108protected:
reed@android.comda6fb322010-02-19 21:41:30 +0000109 virtual void onDraw(SkCanvas*, SkScalar opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000110
111private:
reed@android.com8381e002010-03-23 20:10:46 +0000112 enum Flags {
113 kInheritFromRootTransform_Flag = 0x01
114 };
115
reed@android.com745bfbd2010-02-24 17:16:35 +0000116 SkLayer* fParent;
reed@android.comda6fb322010-02-19 21:41:30 +0000117 SkScalar m_opacity;
118 SkSize m_size;
119 SkPoint m_position;
120 SkPoint m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +0000121 SkMatrix fMatrix;
122 SkMatrix fChildrenMatrix;
reed@android.com8381e002010-03-23 20:10:46 +0000123 uint32_t fFlags;
reed@android.com86d40082010-02-12 17:17:10 +0000124
reed@android.com86d40082010-02-12 17:17:10 +0000125 SkTDArray<SkLayer*> m_children;
reed@google.comc9218432011-01-25 19:05:12 +0000126
127 typedef SkRefCnt INHERITED;
reed@android.com86d40082010-02-12 17:17:10 +0000128};
129
130#endif