blob: 525917ff66d211afcdcbd2a268f79a30462cac6b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com86d40082010-02-12 17:17:10 +00008#include "SkLayer.h"
reed@android.com81dc3312010-02-18 19:32:03 +00009#include "SkCanvas.h"
reed@android.com86d40082010-02-12 17:17:10 +000010
reed@android.com745bfbd2010-02-24 17:16:35 +000011//#define DEBUG_DRAW_LAYER_BOUNDS
12//#define DEBUG_TRACK_NEW_DELETE
13
14#ifdef DEBUG_TRACK_NEW_DELETE
15 static int gLayerAllocCount;
16#endif
17
18///////////////////////////////////////////////////////////////////////////////
reed@android.com2bd703b2010-02-24 02:01:23 +000019
reed@android.com86d40082010-02-12 17:17:10 +000020SkLayer::SkLayer() {
reed@android.com745bfbd2010-02-24 17:16:35 +000021 fParent = NULL;
reed@android.com2bd703b2010-02-24 02:01:23 +000022 m_opacity = SK_Scalar1;
reed@android.com86d40082010-02-12 17:17:10 +000023 m_size.set(0, 0);
24 m_position.set(0, 0);
reed@android.com2bd703b2010-02-24 02:01:23 +000025 m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf);
reed@android.com81dc3312010-02-18 19:32:03 +000026
27 fMatrix.reset();
28 fChildrenMatrix.reset();
reed@android.com8381e002010-03-23 20:10:46 +000029 fFlags = 0;
reed@android.com745bfbd2010-02-24 17:16:35 +000030
31#ifdef DEBUG_TRACK_NEW_DELETE
32 gLayerAllocCount += 1;
33 SkDebugf("SkLayer new: %d\n", gLayerAllocCount);
34#endif
reed@android.com86d40082010-02-12 17:17:10 +000035}
36
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000037SkLayer::SkLayer(const SkLayer& src) : INHERITED() {
reed@android.com745bfbd2010-02-24 17:16:35 +000038 fParent = NULL;
reed@android.com86d40082010-02-12 17:17:10 +000039 m_opacity = src.m_opacity;
40 m_size = src.m_size;
41 m_position = src.m_position;
reed@android.com86d40082010-02-12 17:17:10 +000042 m_anchorPoint = src.m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +000043
44 fMatrix = src.fMatrix;
45 fChildrenMatrix = src.fChildrenMatrix;
reed@android.com8381e002010-03-23 20:10:46 +000046 fFlags = src.fFlags;
reed@android.com745bfbd2010-02-24 17:16:35 +000047
48#ifdef DEBUG_TRACK_NEW_DELETE
49 gLayerAllocCount += 1;
50 SkDebugf("SkLayer copy: %d\n", gLayerAllocCount);
51#endif
reed@android.com86d40082010-02-12 17:17:10 +000052}
53
54SkLayer::~SkLayer() {
55 this->removeChildren();
reed@android.com745bfbd2010-02-24 17:16:35 +000056
57#ifdef DEBUG_TRACK_NEW_DELETE
58 gLayerAllocCount -= 1;
59 SkDebugf("SkLayer delete: %d\n", gLayerAllocCount);
60#endif
reed@android.com86d40082010-02-12 17:17:10 +000061}
62
reed@android.com745bfbd2010-02-24 17:16:35 +000063///////////////////////////////////////////////////////////////////////////////
64
reed@android.com8381e002010-03-23 20:10:46 +000065bool SkLayer::isInheritFromRootTransform() const {
66 return (fFlags & kInheritFromRootTransform_Flag) != 0;
67}
68
69void SkLayer::setInheritFromRootTransform(bool doInherit) {
70 if (doInherit) {
71 fFlags |= kInheritFromRootTransform_Flag;
72 } else {
73 fFlags &= ~kInheritFromRootTransform_Flag;
74 }
75}
76
reed@android.com745bfbd2010-02-24 17:16:35 +000077void SkLayer::setMatrix(const SkMatrix& matrix) {
78 fMatrix = matrix;
79}
80
81void SkLayer::setChildrenMatrix(const SkMatrix& matrix) {
82 fChildrenMatrix = matrix;
83}
84
85///////////////////////////////////////////////////////////////////////////////
86
reed@android.com86d40082010-02-12 17:17:10 +000087int SkLayer::countChildren() const {
88 return m_children.count();
89}
90
91SkLayer* SkLayer::getChild(int index) const {
92 if ((unsigned)index < (unsigned)m_children.count()) {
reed@android.com8381e002010-03-23 20:10:46 +000093 SkASSERT(m_children[index]->fParent == this);
reed@android.com86d40082010-02-12 17:17:10 +000094 return m_children[index];
95 }
96 return NULL;
97}
98
99SkLayer* SkLayer::addChild(SkLayer* child) {
reed@android.com8381e002010-03-23 20:10:46 +0000100 SkASSERT(this != child);
reed@android.com86d40082010-02-12 17:17:10 +0000101 child->ref();
reed@android.com8381e002010-03-23 20:10:46 +0000102 child->detachFromParent();
reed@android.com745bfbd2010-02-24 17:16:35 +0000103 SkASSERT(child->fParent == NULL);
104 child->fParent = this;
105
reed@android.com86d40082010-02-12 17:17:10 +0000106 *m_children.append() = child;
107 return child;
108}
109
reed@android.com8381e002010-03-23 20:10:46 +0000110void SkLayer::detachFromParent() {
111 if (fParent) {
112 int index = fParent->m_children.find(this);
113 SkASSERT(index >= 0);
114 fParent->m_children.remove(index);
115 fParent = NULL;
116 this->unref(); // this call might delete us
reed@android.com745bfbd2010-02-24 17:16:35 +0000117 }
reed@android.com745bfbd2010-02-24 17:16:35 +0000118}
119
reed@android.com86d40082010-02-12 17:17:10 +0000120void SkLayer::removeChildren() {
reed@android.com8381e002010-03-23 20:10:46 +0000121 int count = m_children.count();
122 for (int i = 0; i < count; i++) {
123 SkLayer* child = m_children[i];
124 SkASSERT(child->fParent == this);
125 child->fParent = NULL; // in case it has more than one owner
126 child->unref();
127 }
reed@android.com86d40082010-02-12 17:17:10 +0000128 m_children.reset();
129}
130
reed@android.com745bfbd2010-02-24 17:16:35 +0000131SkLayer* SkLayer::getRootLayer() const {
132 const SkLayer* root = this;
133 while (root->fParent != NULL) {
134 root = root->fParent;
135 }
136 return const_cast<SkLayer*>(root);
reed@android.com81dc3312010-02-18 19:32:03 +0000137}
138
reed@android.com745bfbd2010-02-24 17:16:35 +0000139///////////////////////////////////////////////////////////////////////////////
140
141void SkLayer::getLocalTransform(SkMatrix* matrix) const {
142 matrix->setTranslate(m_position.fX, m_position.fY);
reed@android.com940584d2010-02-26 22:04:15 +0000143
reed@android.com745bfbd2010-02-24 17:16:35 +0000144 SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width());
145 SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height());
146 matrix->preTranslate(tx, ty);
147 matrix->preConcat(this->getMatrix());
148 matrix->preTranslate(-tx, -ty);
149}
150
151void SkLayer::localToGlobal(SkMatrix* matrix) const {
152 this->getLocalTransform(matrix);
153
reed@android.com8381e002010-03-23 20:10:46 +0000154 if (this->isInheritFromRootTransform()) {
155 matrix->postConcat(this->getRootLayer()->getMatrix());
156 return;
157 }
158
reed@android.com745bfbd2010-02-24 17:16:35 +0000159 const SkLayer* layer = this;
160 while (layer->fParent != NULL) {
161 layer = layer->fParent;
162
163 SkMatrix tmp;
164 layer->getLocalTransform(&tmp);
165 tmp.preConcat(layer->getChildrenMatrix());
166 matrix->postConcat(tmp);
167 }
reed@android.com81dc3312010-02-18 19:32:03 +0000168}
169
170///////////////////////////////////////////////////////////////////////////////
171
reed@android.comda6fb322010-02-19 21:41:30 +0000172void SkLayer::onDraw(SkCanvas*, SkScalar opacity) {
173// SkDebugf("----- no onDraw for %p\n", this);
reed@android.com81dc3312010-02-18 19:32:03 +0000174}
175
reed@android.comda6fb322010-02-19 21:41:30 +0000176#include "SkString.h"
reed@android.com81dc3312010-02-18 19:32:03 +0000177
reed@android.comda6fb322010-02-19 21:41:30 +0000178void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) {
reed@android.com81dc3312010-02-18 19:32:03 +0000179#if 0
reed@android.comda6fb322010-02-19 21:41:30 +0000180 SkString str1, str2;
181 // this->getMatrix().toDumpString(&str1);
182 // this->getChildrenMatrix().toDumpString(&str2);
183 SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n",
184 this, opacity * this->getOpacity(), m_size.width(), m_size.height(),
185 m_position.fX, m_position.fY, str1.c_str(), str2.c_str());
reed@android.com81dc3312010-02-18 19:32:03 +0000186#endif
187
188 opacity = SkScalarMul(opacity, this->getOpacity());
reed@android.com940584d2010-02-26 22:04:15 +0000189 if (opacity <= 0) {
190// SkDebugf("---- abort drawing %p opacity %g\n", this, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000191 return;
192 }
193
reed@android.comda6fb322010-02-19 21:41:30 +0000194 SkAutoCanvasRestore acr(canvas, true);
reed@android.com81dc3312010-02-18 19:32:03 +0000195
reed@android.com745bfbd2010-02-24 17:16:35 +0000196 // apply our local transform
reed@android.comda6fb322010-02-19 21:41:30 +0000197 {
reed@android.com8381e002010-03-23 20:10:46 +0000198 SkMatrix tmp;
199 this->getLocalTransform(&tmp);
200 if (this->isInheritFromRootTransform()) {
201 // should we also apply the root's childrenMatrix?
202 canvas->setMatrix(getRootLayer()->getMatrix());
203 }
204 canvas->concat(tmp);
reed@android.comda6fb322010-02-19 21:41:30 +0000205 }
206
207 this->onDraw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000208
reed@android.com745bfbd2010-02-24 17:16:35 +0000209#ifdef DEBUG_DRAW_LAYER_BOUNDS
reed@android.com2bd703b2010-02-24 02:01:23 +0000210 {
211 SkRect r = SkRect::MakeSize(this->getSize());
212 SkPaint p;
213 p.setAntiAlias(true);
214 p.setStyle(SkPaint::kStroke_Style);
215 p.setStrokeWidth(SkIntToScalar(2));
216 p.setColor(0xFFFF44DD);
217 canvas->drawRect(r, p);
218 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
219 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
220 }
221#endif
222
reed@android.com81dc3312010-02-18 19:32:03 +0000223 int count = this->countChildren();
224 if (count > 0) {
225 canvas->concat(this->getChildrenMatrix());
226 for (int i = 0; i < count; i++) {
reed@android.comda6fb322010-02-19 21:41:30 +0000227 this->getChild(i)->draw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000228 }
229 }
230}