blob: 54f1840f6f01b02092c78fa3edac32adcf6b0915 [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
robertphillips@google.coma22e2112012-08-16 14:58:06 +000018SK_DEFINE_INST_COUNT(SkLayer)
19
reed@android.com745bfbd2010-02-24 17:16:35 +000020///////////////////////////////////////////////////////////////////////////////
reed@android.com2bd703b2010-02-24 02:01:23 +000021
reed@android.com86d40082010-02-12 17:17:10 +000022SkLayer::SkLayer() {
reed@android.com745bfbd2010-02-24 17:16:35 +000023 fParent = NULL;
reed@android.com2bd703b2010-02-24 02:01:23 +000024 m_opacity = SK_Scalar1;
reed@android.com86d40082010-02-12 17:17:10 +000025 m_size.set(0, 0);
26 m_position.set(0, 0);
reed@android.com2bd703b2010-02-24 02:01:23 +000027 m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf);
reed@android.com81dc3312010-02-18 19:32:03 +000028
29 fMatrix.reset();
30 fChildrenMatrix.reset();
reed@android.com8381e002010-03-23 20:10:46 +000031 fFlags = 0;
reed@android.com745bfbd2010-02-24 17:16:35 +000032
33#ifdef DEBUG_TRACK_NEW_DELETE
34 gLayerAllocCount += 1;
35 SkDebugf("SkLayer new: %d\n", gLayerAllocCount);
36#endif
reed@android.com86d40082010-02-12 17:17:10 +000037}
38
robertphillips@google.com4bdfb8c2012-06-12 21:23:49 +000039SkLayer::SkLayer(const SkLayer& src) {
reed@android.com745bfbd2010-02-24 17:16:35 +000040 fParent = NULL;
reed@android.com86d40082010-02-12 17:17:10 +000041 m_opacity = src.m_opacity;
42 m_size = src.m_size;
43 m_position = src.m_position;
reed@android.com86d40082010-02-12 17:17:10 +000044 m_anchorPoint = src.m_anchorPoint;
reed@android.com81dc3312010-02-18 19:32:03 +000045
46 fMatrix = src.fMatrix;
47 fChildrenMatrix = src.fChildrenMatrix;
reed@android.com8381e002010-03-23 20:10:46 +000048 fFlags = src.fFlags;
reed@android.com745bfbd2010-02-24 17:16:35 +000049
50#ifdef DEBUG_TRACK_NEW_DELETE
51 gLayerAllocCount += 1;
52 SkDebugf("SkLayer copy: %d\n", gLayerAllocCount);
53#endif
reed@android.com86d40082010-02-12 17:17:10 +000054}
55
56SkLayer::~SkLayer() {
57 this->removeChildren();
reed@android.com745bfbd2010-02-24 17:16:35 +000058
59#ifdef DEBUG_TRACK_NEW_DELETE
60 gLayerAllocCount -= 1;
61 SkDebugf("SkLayer delete: %d\n", gLayerAllocCount);
62#endif
reed@android.com86d40082010-02-12 17:17:10 +000063}
64
reed@android.com745bfbd2010-02-24 17:16:35 +000065///////////////////////////////////////////////////////////////////////////////
66
reed@android.com8381e002010-03-23 20:10:46 +000067bool SkLayer::isInheritFromRootTransform() const {
68 return (fFlags & kInheritFromRootTransform_Flag) != 0;
69}
70
71void SkLayer::setInheritFromRootTransform(bool doInherit) {
72 if (doInherit) {
73 fFlags |= kInheritFromRootTransform_Flag;
74 } else {
75 fFlags &= ~kInheritFromRootTransform_Flag;
76 }
77}
78
reed@android.com745bfbd2010-02-24 17:16:35 +000079void SkLayer::setMatrix(const SkMatrix& matrix) {
80 fMatrix = matrix;
81}
82
83void SkLayer::setChildrenMatrix(const SkMatrix& matrix) {
84 fChildrenMatrix = matrix;
85}
86
87///////////////////////////////////////////////////////////////////////////////
88
reed@android.com86d40082010-02-12 17:17:10 +000089int SkLayer::countChildren() const {
90 return m_children.count();
91}
92
93SkLayer* SkLayer::getChild(int index) const {
94 if ((unsigned)index < (unsigned)m_children.count()) {
reed@android.com8381e002010-03-23 20:10:46 +000095 SkASSERT(m_children[index]->fParent == this);
reed@android.com86d40082010-02-12 17:17:10 +000096 return m_children[index];
97 }
98 return NULL;
99}
100
101SkLayer* SkLayer::addChild(SkLayer* child) {
reed@android.com8381e002010-03-23 20:10:46 +0000102 SkASSERT(this != child);
reed@android.com86d40082010-02-12 17:17:10 +0000103 child->ref();
reed@android.com8381e002010-03-23 20:10:46 +0000104 child->detachFromParent();
reed@android.com745bfbd2010-02-24 17:16:35 +0000105 SkASSERT(child->fParent == NULL);
106 child->fParent = this;
107
reed@android.com86d40082010-02-12 17:17:10 +0000108 *m_children.append() = child;
109 return child;
110}
111
reed@android.com8381e002010-03-23 20:10:46 +0000112void SkLayer::detachFromParent() {
113 if (fParent) {
114 int index = fParent->m_children.find(this);
115 SkASSERT(index >= 0);
116 fParent->m_children.remove(index);
117 fParent = NULL;
118 this->unref(); // this call might delete us
reed@android.com745bfbd2010-02-24 17:16:35 +0000119 }
reed@android.com745bfbd2010-02-24 17:16:35 +0000120}
121
reed@android.com86d40082010-02-12 17:17:10 +0000122void SkLayer::removeChildren() {
reed@android.com8381e002010-03-23 20:10:46 +0000123 int count = m_children.count();
124 for (int i = 0; i < count; i++) {
125 SkLayer* child = m_children[i];
126 SkASSERT(child->fParent == this);
127 child->fParent = NULL; // in case it has more than one owner
128 child->unref();
129 }
reed@android.com86d40082010-02-12 17:17:10 +0000130 m_children.reset();
131}
132
reed@android.com745bfbd2010-02-24 17:16:35 +0000133SkLayer* SkLayer::getRootLayer() const {
134 const SkLayer* root = this;
135 while (root->fParent != NULL) {
136 root = root->fParent;
137 }
138 return const_cast<SkLayer*>(root);
reed@android.com81dc3312010-02-18 19:32:03 +0000139}
140
reed@android.com745bfbd2010-02-24 17:16:35 +0000141///////////////////////////////////////////////////////////////////////////////
142
143void SkLayer::getLocalTransform(SkMatrix* matrix) const {
144 matrix->setTranslate(m_position.fX, m_position.fY);
reed@android.com940584d2010-02-26 22:04:15 +0000145
reed@android.com745bfbd2010-02-24 17:16:35 +0000146 SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width());
147 SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height());
148 matrix->preTranslate(tx, ty);
149 matrix->preConcat(this->getMatrix());
150 matrix->preTranslate(-tx, -ty);
151}
152
153void SkLayer::localToGlobal(SkMatrix* matrix) const {
154 this->getLocalTransform(matrix);
155
reed@android.com8381e002010-03-23 20:10:46 +0000156 if (this->isInheritFromRootTransform()) {
157 matrix->postConcat(this->getRootLayer()->getMatrix());
158 return;
159 }
160
reed@android.com745bfbd2010-02-24 17:16:35 +0000161 const SkLayer* layer = this;
162 while (layer->fParent != NULL) {
163 layer = layer->fParent;
164
165 SkMatrix tmp;
166 layer->getLocalTransform(&tmp);
167 tmp.preConcat(layer->getChildrenMatrix());
168 matrix->postConcat(tmp);
169 }
reed@android.com81dc3312010-02-18 19:32:03 +0000170}
171
172///////////////////////////////////////////////////////////////////////////////
173
reed@android.comda6fb322010-02-19 21:41:30 +0000174void SkLayer::onDraw(SkCanvas*, SkScalar opacity) {
175// SkDebugf("----- no onDraw for %p\n", this);
reed@android.com81dc3312010-02-18 19:32:03 +0000176}
177
reed@android.comda6fb322010-02-19 21:41:30 +0000178#include "SkString.h"
reed@android.com81dc3312010-02-18 19:32:03 +0000179
reed@android.comda6fb322010-02-19 21:41:30 +0000180void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) {
reed@android.com81dc3312010-02-18 19:32:03 +0000181#if 0
reed@android.comda6fb322010-02-19 21:41:30 +0000182 SkString str1, str2;
183 // this->getMatrix().toDumpString(&str1);
184 // this->getChildrenMatrix().toDumpString(&str2);
185 SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n",
186 this, opacity * this->getOpacity(), m_size.width(), m_size.height(),
187 m_position.fX, m_position.fY, str1.c_str(), str2.c_str());
reed@android.com81dc3312010-02-18 19:32:03 +0000188#endif
189
190 opacity = SkScalarMul(opacity, this->getOpacity());
reed@android.com940584d2010-02-26 22:04:15 +0000191 if (opacity <= 0) {
192// SkDebugf("---- abort drawing %p opacity %g\n", this, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000193 return;
194 }
195
reed@android.comda6fb322010-02-19 21:41:30 +0000196 SkAutoCanvasRestore acr(canvas, true);
reed@android.com81dc3312010-02-18 19:32:03 +0000197
reed@android.com745bfbd2010-02-24 17:16:35 +0000198 // apply our local transform
reed@android.comda6fb322010-02-19 21:41:30 +0000199 {
reed@android.com8381e002010-03-23 20:10:46 +0000200 SkMatrix tmp;
201 this->getLocalTransform(&tmp);
202 if (this->isInheritFromRootTransform()) {
203 // should we also apply the root's childrenMatrix?
204 canvas->setMatrix(getRootLayer()->getMatrix());
205 }
206 canvas->concat(tmp);
reed@android.comda6fb322010-02-19 21:41:30 +0000207 }
208
209 this->onDraw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000210
reed@android.com745bfbd2010-02-24 17:16:35 +0000211#ifdef DEBUG_DRAW_LAYER_BOUNDS
reed@android.com2bd703b2010-02-24 02:01:23 +0000212 {
213 SkRect r = SkRect::MakeSize(this->getSize());
214 SkPaint p;
215 p.setAntiAlias(true);
216 p.setStyle(SkPaint::kStroke_Style);
217 p.setStrokeWidth(SkIntToScalar(2));
218 p.setColor(0xFFFF44DD);
219 canvas->drawRect(r, p);
220 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
221 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
222 }
223#endif
224
reed@android.com81dc3312010-02-18 19:32:03 +0000225 int count = this->countChildren();
226 if (count > 0) {
227 canvas->concat(this->getChildrenMatrix());
228 for (int i = 0; i < count; i++) {
reed@android.comda6fb322010-02-19 21:41:30 +0000229 this->getChild(i)->draw(canvas, opacity);
reed@android.com81dc3312010-02-18 19:32:03 +0000230 }
231 }
232}
reed@android.com86d40082010-02-12 17:17:10 +0000233