epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 8 | #include "SkLayer.h" |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 10 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 11 | //#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.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 19 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 20 | SkLayer::SkLayer() { |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 21 | fParent = NULL; |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 22 | m_opacity = SK_Scalar1; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 23 | m_size.set(0, 0); |
| 24 | m_position.set(0, 0); |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 25 | m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 26 | |
| 27 | fMatrix.reset(); |
| 28 | fChildrenMatrix.reset(); |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 29 | fFlags = 0; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef DEBUG_TRACK_NEW_DELETE |
| 32 | gLayerAllocCount += 1; |
| 33 | SkDebugf("SkLayer new: %d\n", gLayerAllocCount); |
| 34 | #endif |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 35 | } |
| 36 | |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 37 | SkLayer::SkLayer(const SkLayer& src) : INHERITED() { |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 38 | fParent = NULL; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 39 | m_opacity = src.m_opacity; |
| 40 | m_size = src.m_size; |
| 41 | m_position = src.m_position; |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 42 | m_anchorPoint = src.m_anchorPoint; |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 43 | |
| 44 | fMatrix = src.fMatrix; |
| 45 | fChildrenMatrix = src.fChildrenMatrix; |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 46 | fFlags = src.fFlags; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 47 | |
| 48 | #ifdef DEBUG_TRACK_NEW_DELETE |
| 49 | gLayerAllocCount += 1; |
| 50 | SkDebugf("SkLayer copy: %d\n", gLayerAllocCount); |
| 51 | #endif |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | SkLayer::~SkLayer() { |
| 55 | this->removeChildren(); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef DEBUG_TRACK_NEW_DELETE |
| 58 | gLayerAllocCount -= 1; |
| 59 | SkDebugf("SkLayer delete: %d\n", gLayerAllocCount); |
| 60 | #endif |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 61 | } |
| 62 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 65 | bool SkLayer::isInheritFromRootTransform() const { |
| 66 | return (fFlags & kInheritFromRootTransform_Flag) != 0; |
| 67 | } |
| 68 | |
| 69 | void SkLayer::setInheritFromRootTransform(bool doInherit) { |
| 70 | if (doInherit) { |
| 71 | fFlags |= kInheritFromRootTransform_Flag; |
| 72 | } else { |
| 73 | fFlags &= ~kInheritFromRootTransform_Flag; |
| 74 | } |
| 75 | } |
| 76 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 77 | void SkLayer::setMatrix(const SkMatrix& matrix) { |
| 78 | fMatrix = matrix; |
| 79 | } |
| 80 | |
| 81 | void SkLayer::setChildrenMatrix(const SkMatrix& matrix) { |
| 82 | fChildrenMatrix = matrix; |
| 83 | } |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 87 | int SkLayer::countChildren() const { |
| 88 | return m_children.count(); |
| 89 | } |
| 90 | |
| 91 | SkLayer* SkLayer::getChild(int index) const { |
| 92 | if ((unsigned)index < (unsigned)m_children.count()) { |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 93 | SkASSERT(m_children[index]->fParent == this); |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 94 | return m_children[index]; |
| 95 | } |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | SkLayer* SkLayer::addChild(SkLayer* child) { |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 100 | SkASSERT(this != child); |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 101 | child->ref(); |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 102 | child->detachFromParent(); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 103 | SkASSERT(child->fParent == NULL); |
| 104 | child->fParent = this; |
| 105 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 106 | *m_children.append() = child; |
| 107 | return child; |
| 108 | } |
| 109 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 110 | void 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.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 117 | } |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 118 | } |
| 119 | |
reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 120 | void SkLayer::removeChildren() { |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 121 | 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.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame] | 128 | m_children.reset(); |
| 129 | } |
| 130 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 131 | SkLayer* 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.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 137 | } |
| 138 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | void SkLayer::getLocalTransform(SkMatrix* matrix) const { |
| 142 | matrix->setTranslate(m_position.fX, m_position.fY); |
reed@android.com | 940584d | 2010-02-26 22:04:15 +0000 | [diff] [blame] | 143 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 144 | 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 | |
| 151 | void SkLayer::localToGlobal(SkMatrix* matrix) const { |
| 152 | this->getLocalTransform(matrix); |
| 153 | |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 154 | if (this->isInheritFromRootTransform()) { |
| 155 | matrix->postConcat(this->getRootLayer()->getMatrix()); |
| 156 | return; |
| 157 | } |
| 158 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 159 | 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.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /////////////////////////////////////////////////////////////////////////////// |
| 171 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 172 | void SkLayer::onDraw(SkCanvas*, SkScalar opacity) { |
| 173 | // SkDebugf("----- no onDraw for %p\n", this); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 176 | #include "SkString.h" |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 177 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 178 | void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) { |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 179 | #if 0 |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 180 | 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.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
| 188 | opacity = SkScalarMul(opacity, this->getOpacity()); |
reed@android.com | 940584d | 2010-02-26 22:04:15 +0000 | [diff] [blame] | 189 | if (opacity <= 0) { |
| 190 | // SkDebugf("---- abort drawing %p opacity %g\n", this, opacity); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 194 | SkAutoCanvasRestore acr(canvas, true); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 195 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 196 | // apply our local transform |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 197 | { |
reed@android.com | 8381e00 | 2010-03-23 20:10:46 +0000 | [diff] [blame] | 198 | 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.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | this->onDraw(canvas, opacity); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 208 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 209 | #ifdef DEBUG_DRAW_LAYER_BOUNDS |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 210 | { |
| 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.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 223 | int count = this->countChildren(); |
| 224 | if (count > 0) { |
| 225 | canvas->concat(this->getChildrenMatrix()); |
| 226 | for (int i = 0; i < count; i++) { |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 227 | this->getChild(i)->draw(canvas, opacity); |
reed@android.com | 81dc331 | 2010-02-18 19:32:03 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |