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 SkLayer_DEFINED
18#define SkLayer_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
28class SkCanvas;
29
30class SkLayer : public SkRefCnt {
31
32public:
33    SkLayer();
34    SkLayer(const SkLayer&);
35    virtual ~SkLayer();
36
37    bool isInheritFromRootTransform() const;
38    SkScalar getOpacity() const { return m_opacity; }
39    const SkSize& getSize() const { return m_size; }
40    const SkPoint& getPosition() const { return m_position; }
41    const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
42    const SkMatrix& getMatrix() const { return fMatrix; }
43    const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
44
45    SkScalar getWidth() const { return m_size.width(); }
46    SkScalar getHeight() const { return m_size.height(); }
47
48    void setInheritFromRootTransform(bool);
49    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&);
54    void setChildrenMatrix(const SkMatrix&);
55
56    // children
57
58    /** Return the number of layers in our child list.
59     */
60    int countChildren() const;
61
62    /** Return the child at the specified index (starting at 0). This does not
63        affect the reference count of the child.
64     */
65    SkLayer* getChild(int index) const;
66
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     */
71    SkLayer* addChild(SkLayer* child);
72
73    /** 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.
75     */
76    void detachFromParent();
77
78    /** Remove, and unref(), all of the layers in our child list.
79     */
80    void removeChildren();
81
82    /** 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
106    // paint method
107
108    void draw(SkCanvas*, SkScalar opacity);
109    void draw(SkCanvas* canvas) {
110        this->draw(canvas, SK_Scalar1);
111    }
112
113protected:
114    virtual void onDraw(SkCanvas*, SkScalar opacity);
115
116private:
117    enum Flags {
118        kInheritFromRootTransform_Flag = 0x01
119    };
120
121    SkLayer*    fParent;
122    SkScalar    m_opacity;
123    SkSize      m_size;
124    SkPoint     m_position;
125    SkPoint     m_anchorPoint;
126    SkMatrix    fMatrix;
127    SkMatrix    fChildrenMatrix;
128    uint32_t    fFlags;
129
130    SkTDArray<SkLayer*> m_children;
131
132    typedef SkRefCnt INHERITED;
133};
134
135#endif
136