blob: 603cdd1eb827afffe3b311b9ef12904934328122 [file] [log] [blame]
reed@android.com86d40082010-02-12 17:17:10 +00001#include "SkLayer.h"
reed@android.com81dc3312010-02-18 19:32:03 +00002#include "SkCanvas.h"
reed@android.com86d40082010-02-12 17:17:10 +00003
4SkLayer::SkLayer() {
reed@android.com86d40082010-02-12 17:17:10 +00005 m_opacity = 1;
reed@android.com86d40082010-02-12 17:17:10 +00006 m_size.set(0, 0);
7 m_position.set(0, 0);
reed@android.com86d40082010-02-12 17:17:10 +00008 m_anchorPoint.set(0.5, 0.5);
reed@android.com81dc3312010-02-18 19:32:03 +00009
10 fMatrix.reset();
11 fChildrenMatrix.reset();
reed@android.com86d40082010-02-12 17:17:10 +000012}
13
14SkLayer::SkLayer(const SkLayer& src) {
reed@android.com86d40082010-02-12 17:17:10 +000015 m_opacity = src.m_opacity;
16 m_size = src.m_size;
17 m_position = src.m_position;
reed@android.com86d40082010-02-12 17:17:10 +000018 m_anchorPoint = src.m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +000019
20 fMatrix = src.fMatrix;
21 fChildrenMatrix = src.fChildrenMatrix;
reed@android.com86d40082010-02-12 17:17:10 +000022}
23
24SkLayer::~SkLayer() {
25 this->removeChildren();
26}
27
28int SkLayer::countChildren() const {
29 return m_children.count();
30}
31
32SkLayer* SkLayer::getChild(int index) const {
33 if ((unsigned)index < (unsigned)m_children.count()) {
34 return m_children[index];
35 }
36 return NULL;
37}
38
39SkLayer* SkLayer::addChild(SkLayer* child) {
40 child->ref();
41 *m_children.append() = child;
42 return child;
43}
44
45void SkLayer::removeChildren() {
46 m_children.unrefAll();
47 m_children.reset();
48}
49
reed@android.com81dc3312010-02-18 19:32:03 +000050///////////////////////////////////////////////////////////////////////////////
51
52void SkLayer::setMatrix(const SkMatrix& matrix) {
53 fMatrix = matrix;
54}
55
56void SkLayer::setChildrenMatrix(const SkMatrix& matrix) {
57 fChildrenMatrix = matrix;
58}
59
60///////////////////////////////////////////////////////////////////////////////
61
reed@android.comda6fb322010-02-19 21:41:30 +000062void SkLayer::onDraw(SkCanvas*, SkScalar opacity) {
63// SkDebugf("----- no onDraw for %p\n", this);
reed@android.com81dc3312010-02-18 19:32:03 +000064}
65
reed@android.comda6fb322010-02-19 21:41:30 +000066#include "SkString.h"
reed@android.com81dc3312010-02-18 19:32:03 +000067
reed@android.comda6fb322010-02-19 21:41:30 +000068void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) {
reed@android.com81dc3312010-02-18 19:32:03 +000069#if 0
reed@android.comda6fb322010-02-19 21:41:30 +000070 SkString str1, str2;
71 // this->getMatrix().toDumpString(&str1);
72 // this->getChildrenMatrix().toDumpString(&str2);
73 SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n",
74 this, opacity * this->getOpacity(), m_size.width(), m_size.height(),
75 m_position.fX, m_position.fY, str1.c_str(), str2.c_str());
reed@android.com81dc3312010-02-18 19:32:03 +000076#endif
77
78 opacity = SkScalarMul(opacity, this->getOpacity());
79 if (opacity <= 0 || this->getSize().isEmpty()) {
reed@android.comda6fb322010-02-19 21:41:30 +000080#if 0
81 SkDebugf("---- abort drawing %p opacity %g size [%g %g]\n",
82 this, opacity, m_size.width(), m_size.height());
83#endif
reed@android.com81dc3312010-02-18 19:32:03 +000084 return;
85 }
86
reed@android.comda6fb322010-02-19 21:41:30 +000087 SkAutoCanvasRestore acr(canvas, true);
reed@android.com81dc3312010-02-18 19:32:03 +000088
reed@android.comda6fb322010-02-19 21:41:30 +000089 // update the matrix
90 {
91 SkScalar tx = m_position.fX;
92 SkScalar ty = m_position.fY;
93 canvas->translate(tx, ty);
94
95 // now apply our matrix about the anchorPoint
96 tx = SkScalarMul(m_anchorPoint.fX, m_size.width());
97 ty = SkScalarMul(m_anchorPoint.fY, m_size.height());
98 canvas->translate(tx, ty);
99 canvas->concat(this->getMatrix());
100 canvas->translate(-tx, -ty);
101 }
102
103 this->onDraw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000104
105 int count = this->countChildren();
106 if (count > 0) {
107 canvas->concat(this->getChildrenMatrix());
108 for (int i = 0; i < count; i++) {
reed@android.comda6fb322010-02-19 21:41:30 +0000109 this->getChild(i)->draw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000110 }
111 }
112}
reed@android.com86d40082010-02-12 17:17:10 +0000113