epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2010 The Android Open Source Project |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 10 | #ifndef SkLayer_DEFINED |
| 11 | #define SkLayer_DEFINED |
| 12 | |
| 13 | #include "SkRefCnt.h" |
| 14 | #include "SkTDArray.h" |
| 15 | #include "SkColor.h" |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 16 | #include "SkMatrix.h" |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 17 | #include "SkPoint.h" |
| 18 | #include "SkRect.h" |
| 19 | #include "SkSize.h" |
| 20 | |
| 21 | class SkCanvas; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 22 | |
| 23 | class SkLayer : public SkRefCnt { |
| 24 | |
| 25 | public: |
| 26 | SkLayer(); |
| 27 | SkLayer(const SkLayer&); |
| 28 | virtual ~SkLayer(); |
| 29 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 30 | bool isInheritFromRootTransform() const; |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 31 | SkScalar getOpacity() const { return m_opacity; } |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 32 | const SkSize& getSize() const { return m_size; } |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 33 | const SkPoint& getPosition() const { return m_position; } |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 34 | const SkPoint& getAnchorPoint() const { return m_anchorPoint; } |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 35 | const SkMatrix& getMatrix() const { return fMatrix; } |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 36 | const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; } |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 37 | |
| 38 | SkScalar getWidth() const { return m_size.width(); } |
| 39 | SkScalar getHeight() const { return m_size.height(); } |
| 40 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 41 | void setInheritFromRootTransform(bool); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 42 | void setOpacity(SkScalar opacity) { m_opacity = opacity; } |
| 43 | void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); } |
| 44 | void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); } |
| 45 | void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); } |
| 46 | void setMatrix(const SkMatrix&); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 47 | void setChildrenMatrix(const SkMatrix&); |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 48 | |
| 49 | // children |
| 50 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 51 | /** Return the number of layers in our child list. |
| 52 | */ |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 53 | int countChildren() const; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 54 | |
| 55 | /** Return the child at the specified index (starting at 0). This does not |
| 56 | affect the reference count of the child. |
| 57 | */ |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 58 | SkLayer* getChild(int index) const; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 59 | |
| 60 | /** Add this layer to our child list at the end (top-most), and ref() it. |
| 61 | If it was already in another hierarchy, remove it from that list. |
| 62 | Return the new child. |
| 63 | */ |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 64 | SkLayer* addChild(SkLayer* child); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 65 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 66 | /** Remove this layer from its parent's list (or do nothing if it has no |
| 67 | parent.) If it had a parent, then unref() is called. |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 68 | */ |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 69 | void detachFromParent(); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 70 | |
| 71 | /** Remove, and unref(), all of the layers in our child list. |
| 72 | */ |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 73 | void removeChildren(); |
| 74 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 75 | /** Return our parent layer, or NULL if we have none. |
| 76 | */ |
| 77 | SkLayer* getParent() const { return fParent; } |
| 78 | |
| 79 | /** Return the root layer in this hiearchy. If this layer is the root |
| 80 | (i.e. has no parent), then this returns itself. |
| 81 | */ |
| 82 | SkLayer* getRootLayer() const; |
| 83 | |
| 84 | // coordinate system transformations |
| 85 | |
| 86 | /** Return, in matrix, the matix transfomations that are applied locally |
| 87 | when this layer draws (i.e. its position and matrix/anchorPoint). |
| 88 | This does not include the childrenMatrix, since that is only applied |
| 89 | after this layer draws (but before its children draw). |
| 90 | */ |
| 91 | void getLocalTransform(SkMatrix* matrix) const; |
| 92 | |
| 93 | /** Return, in matrix, the concatenation of transforms that are applied |
| 94 | from this layer's root parent to the layer itself. |
| 95 | This is the matrix that is applied to the layer during drawing. |
| 96 | */ |
| 97 | void localToGlobal(SkMatrix* matrix) const; |
| 98 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 99 | // paint method |
| 100 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 101 | void draw(SkCanvas*, SkScalar opacity); |
| 102 | void draw(SkCanvas* canvas) { |
| 103 | this->draw(canvas, SK_Scalar1); |
| 104 | } |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 105 | |
| 106 | protected: |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 107 | virtual void onDraw(SkCanvas*, SkScalar opacity); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 110 | enum Flags { |
| 111 | kInheritFromRootTransform_Flag = 0x01 |
| 112 | }; |
| 113 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 114 | SkLayer* fParent; |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 115 | SkScalar m_opacity; |
| 116 | SkSize m_size; |
| 117 | SkPoint m_position; |
| 118 | SkPoint m_anchorPoint; |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 119 | SkMatrix fMatrix; |
| 120 | SkMatrix fChildrenMatrix; |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 121 | uint32_t fFlags; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 122 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 123 | SkTDArray<SkLayer*> m_children; |
reed@google.com | c921843 | 2011-01-25 19:05:12 +0000 | [diff] [blame] | 124 | |
| 125 | typedef SkRefCnt INHERITED; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | #endif |