| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SkLayer.h" | 8 #include "SkLayer.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 | 10 |
| 11 //#define DEBUG_DRAW_LAYER_BOUNDS | 11 //#define DEBUG_DRAW_LAYER_BOUNDS |
| 12 //#define DEBUG_TRACK_NEW_DELETE | 12 //#define DEBUG_TRACK_NEW_DELETE |
| 13 | 13 |
| 14 #ifdef DEBUG_TRACK_NEW_DELETE | 14 #ifdef DEBUG_TRACK_NEW_DELETE |
| 15 static int gLayerAllocCount; | 15 static int gLayerAllocCount; |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 /////////////////////////////////////////////////////////////////////////////// | 18 /////////////////////////////////////////////////////////////////////////////// |
| 19 | 19 |
| 20 SkLayer::SkLayer() { | 20 SkLayer::SkLayer() { |
| 21 fParent = NULL; | 21 fParent = nullptr; |
| 22 m_opacity = SK_Scalar1; | 22 m_opacity = SK_Scalar1; |
| 23 m_size.set(0, 0); | 23 m_size.set(0, 0); |
| 24 m_position.set(0, 0); | 24 m_position.set(0, 0); |
| 25 m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf); | 25 m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf); |
| 26 | 26 |
| 27 fMatrix.reset(); | 27 fMatrix.reset(); |
| 28 fChildrenMatrix.reset(); | 28 fChildrenMatrix.reset(); |
| 29 fFlags = 0; | 29 fFlags = 0; |
| 30 | 30 |
| 31 #ifdef DEBUG_TRACK_NEW_DELETE | 31 #ifdef DEBUG_TRACK_NEW_DELETE |
| 32 gLayerAllocCount += 1; | 32 gLayerAllocCount += 1; |
| 33 SkDebugf("SkLayer new: %d\n", gLayerAllocCount); | 33 SkDebugf("SkLayer new: %d\n", gLayerAllocCount); |
| 34 #endif | 34 #endif |
| 35 } | 35 } |
| 36 | 36 |
| 37 SkLayer::SkLayer(const SkLayer& src) : INHERITED() { | 37 SkLayer::SkLayer(const SkLayer& src) : INHERITED() { |
| 38 fParent = NULL; | 38 fParent = nullptr; |
| 39 m_opacity = src.m_opacity; | 39 m_opacity = src.m_opacity; |
| 40 m_size = src.m_size; | 40 m_size = src.m_size; |
| 41 m_position = src.m_position; | 41 m_position = src.m_position; |
| 42 m_anchorPoint = src.m_anchorPoint; | 42 m_anchorPoint = src.m_anchorPoint; |
| 43 | 43 |
| 44 fMatrix = src.fMatrix; | 44 fMatrix = src.fMatrix; |
| 45 fChildrenMatrix = src.fChildrenMatrix; | 45 fChildrenMatrix = src.fChildrenMatrix; |
| 46 fFlags = src.fFlags; | 46 fFlags = src.fFlags; |
| 47 | 47 |
| 48 #ifdef DEBUG_TRACK_NEW_DELETE | 48 #ifdef DEBUG_TRACK_NEW_DELETE |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 int SkLayer::countChildren() const { | 87 int SkLayer::countChildren() const { |
| 88 return m_children.count(); | 88 return m_children.count(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 SkLayer* SkLayer::getChild(int index) const { | 91 SkLayer* SkLayer::getChild(int index) const { |
| 92 if ((unsigned)index < (unsigned)m_children.count()) { | 92 if ((unsigned)index < (unsigned)m_children.count()) { |
| 93 SkASSERT(m_children[index]->fParent == this); | 93 SkASSERT(m_children[index]->fParent == this); |
| 94 return m_children[index]; | 94 return m_children[index]; |
| 95 } | 95 } |
| 96 return NULL; | 96 return nullptr; |
| 97 } | 97 } |
| 98 | 98 |
| 99 SkLayer* SkLayer::addChild(SkLayer* child) { | 99 SkLayer* SkLayer::addChild(SkLayer* child) { |
| 100 SkASSERT(this != child); | 100 SkASSERT(this != child); |
| 101 child->ref(); | 101 child->ref(); |
| 102 child->detachFromParent(); | 102 child->detachFromParent(); |
| 103 SkASSERT(child->fParent == NULL); | 103 SkASSERT(child->fParent == nullptr); |
| 104 child->fParent = this; | 104 child->fParent = this; |
| 105 | 105 |
| 106 *m_children.append() = child; | 106 *m_children.append() = child; |
| 107 return child; | 107 return child; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void SkLayer::detachFromParent() { | 110 void SkLayer::detachFromParent() { |
| 111 if (fParent) { | 111 if (fParent) { |
| 112 int index = fParent->m_children.find(this); | 112 int index = fParent->m_children.find(this); |
| 113 SkASSERT(index >= 0); | 113 SkASSERT(index >= 0); |
| 114 fParent->m_children.remove(index); | 114 fParent->m_children.remove(index); |
| 115 fParent = NULL; | 115 fParent = nullptr; |
| 116 this->unref(); // this call might delete us | 116 this->unref(); // this call might delete us |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 void SkLayer::removeChildren() { | 120 void SkLayer::removeChildren() { |
| 121 int count = m_children.count(); | 121 int count = m_children.count(); |
| 122 for (int i = 0; i < count; i++) { | 122 for (int i = 0; i < count; i++) { |
| 123 SkLayer* child = m_children[i]; | 123 SkLayer* child = m_children[i]; |
| 124 SkASSERT(child->fParent == this); | 124 SkASSERT(child->fParent == this); |
| 125 child->fParent = NULL; // in case it has more than one owner | 125 child->fParent = nullptr; // in case it has more than one owner |
| 126 child->unref(); | 126 child->unref(); |
| 127 } | 127 } |
| 128 m_children.reset(); | 128 m_children.reset(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 SkLayer* SkLayer::getRootLayer() const { | 131 SkLayer* SkLayer::getRootLayer() const { |
| 132 const SkLayer* root = this; | 132 const SkLayer* root = this; |
| 133 while (root->fParent != NULL) { | 133 while (root->fParent != nullptr) { |
| 134 root = root->fParent; | 134 root = root->fParent; |
| 135 } | 135 } |
| 136 return const_cast<SkLayer*>(root); | 136 return const_cast<SkLayer*>(root); |
| 137 } | 137 } |
| 138 | 138 |
| 139 /////////////////////////////////////////////////////////////////////////////// | 139 /////////////////////////////////////////////////////////////////////////////// |
| 140 | 140 |
| 141 void SkLayer::getLocalTransform(SkMatrix* matrix) const { | 141 void SkLayer::getLocalTransform(SkMatrix* matrix) const { |
| 142 matrix->setTranslate(m_position.fX, m_position.fY); | 142 matrix->setTranslate(m_position.fX, m_position.fY); |
| 143 | 143 |
| 144 SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width()); | 144 SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width()); |
| 145 SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height()); | 145 SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height()); |
| 146 matrix->preTranslate(tx, ty); | 146 matrix->preTranslate(tx, ty); |
| 147 matrix->preConcat(this->getMatrix()); | 147 matrix->preConcat(this->getMatrix()); |
| 148 matrix->preTranslate(-tx, -ty); | 148 matrix->preTranslate(-tx, -ty); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void SkLayer::localToGlobal(SkMatrix* matrix) const { | 151 void SkLayer::localToGlobal(SkMatrix* matrix) const { |
| 152 this->getLocalTransform(matrix); | 152 this->getLocalTransform(matrix); |
| 153 | 153 |
| 154 if (this->isInheritFromRootTransform()) { | 154 if (this->isInheritFromRootTransform()) { |
| 155 matrix->postConcat(this->getRootLayer()->getMatrix()); | 155 matrix->postConcat(this->getRootLayer()->getMatrix()); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 | 158 |
| 159 const SkLayer* layer = this; | 159 const SkLayer* layer = this; |
| 160 while (layer->fParent != NULL) { | 160 while (layer->fParent != nullptr) { |
| 161 layer = layer->fParent; | 161 layer = layer->fParent; |
| 162 | 162 |
| 163 SkMatrix tmp; | 163 SkMatrix tmp; |
| 164 layer->getLocalTransform(&tmp); | 164 layer->getLocalTransform(&tmp); |
| 165 tmp.preConcat(layer->getChildrenMatrix()); | 165 tmp.preConcat(layer->getChildrenMatrix()); |
| 166 matrix->postConcat(tmp); | 166 matrix->postConcat(tmp); |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 /////////////////////////////////////////////////////////////////////////////// | 170 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 #endif | 221 #endif |
| 222 | 222 |
| 223 int count = this->countChildren(); | 223 int count = this->countChildren(); |
| 224 if (count > 0) { | 224 if (count > 0) { |
| 225 canvas->concat(this->getChildrenMatrix()); | 225 canvas->concat(this->getChildrenMatrix()); |
| 226 for (int i = 0; i < count; i++) { | 226 for (int i = 0; i < count; i++) { |
| 227 this->getChild(i)->draw(canvas, opacity); | 227 this->getChild(i)->draw(canvas, opacity); |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 } | 230 } |
| OLD | NEW |