180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2010 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkLayer_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkLayer_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRefCnt.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkColor.h"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkMatrix.h"
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPoint.h"
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRect.h"
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkSize.h"
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkCanvas;
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkLayer : public SkRefCnt {
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_DECLARE_INST_COUNT(SkLayer)
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer();
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer(const SkLayer&);
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~SkLayer();
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool isInheritFromRootTransform() const;
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar getOpacity() const { return m_opacity; }
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkSize& getSize() const { return m_size; }
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkPoint& getPosition() const { return m_position; }
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkMatrix& getMatrix() const { return fMatrix; }
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar getWidth() const { return m_size.width(); }
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar getHeight() const { return m_size.height(); }
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setInheritFromRootTransform(bool);
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setOpacity(SkScalar opacity) { m_opacity = opacity; }
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setMatrix(const SkMatrix&);
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setChildrenMatrix(const SkMatrix&);
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // children
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return the number of layers in our child list.
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int countChildren() const;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return the child at the specified index (starting at 0). This does not
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        affect the reference count of the child.
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer* getChild(int index) const;
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Add this layer to our child list at the end (top-most), and ref() it.
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        If it was already in another hierarchy, remove it from that list.
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Return the new child.
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer* addChild(SkLayer* child);
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Remove this layer from its parent's list (or do nothing if it has no
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        parent.) If it had a parent, then unref() is called.
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void detachFromParent();
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Remove, and unref(), all of the layers in our child list.
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void removeChildren();
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return our parent layer, or NULL if we have none.
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer* getParent() const { return fParent; }
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return the root layer in this hiearchy. If this layer is the root
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (i.e. has no parent), then this returns itself.
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer* getRootLayer() const;
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // coordinate system transformations
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return, in matrix, the matix transfomations that are applied locally
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        when this layer draws (i.e. its position and matrix/anchorPoint).
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        This does not include the childrenMatrix, since that is only applied
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        after this layer draws (but before its children draw).
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void getLocalTransform(SkMatrix* matrix) const;
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Return, in matrix, the concatenation of transforms that are applied
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        from this layer's root parent to the layer itself.
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        This is the matrix that is applied to the layer during drawing.
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void localToGlobal(SkMatrix* matrix) const;
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // paint method
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void draw(SkCanvas*, SkScalar opacity);
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void draw(SkCanvas* canvas) {
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->draw(canvas, SK_Scalar1);
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas*, SkScalar opacity);
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum Flags {
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kInheritFromRootTransform_Flag = 0x01
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkLayer*    fParent;
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar    m_opacity;
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkSize      m_size;
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkPoint     m_position;
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkPoint     m_anchorPoint;
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMatrix    fMatrix;
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMatrix    fChildrenMatrix;
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t    fFlags;
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDArray<SkLayer*> m_children;
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkRefCnt INHERITED;
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
131