Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | #include "SkLayer.h" |
michael@0 | 9 | #include "SkCanvas.h" |
michael@0 | 10 | |
michael@0 | 11 | //#define DEBUG_DRAW_LAYER_BOUNDS |
michael@0 | 12 | //#define DEBUG_TRACK_NEW_DELETE |
michael@0 | 13 | |
michael@0 | 14 | #ifdef DEBUG_TRACK_NEW_DELETE |
michael@0 | 15 | static int gLayerAllocCount; |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 19 | |
michael@0 | 20 | SkLayer::SkLayer() { |
michael@0 | 21 | fParent = NULL; |
michael@0 | 22 | m_opacity = SK_Scalar1; |
michael@0 | 23 | m_size.set(0, 0); |
michael@0 | 24 | m_position.set(0, 0); |
michael@0 | 25 | m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf); |
michael@0 | 26 | |
michael@0 | 27 | fMatrix.reset(); |
michael@0 | 28 | fChildrenMatrix.reset(); |
michael@0 | 29 | fFlags = 0; |
michael@0 | 30 | |
michael@0 | 31 | #ifdef DEBUG_TRACK_NEW_DELETE |
michael@0 | 32 | gLayerAllocCount += 1; |
michael@0 | 33 | SkDebugf("SkLayer new: %d\n", gLayerAllocCount); |
michael@0 | 34 | #endif |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | SkLayer::SkLayer(const SkLayer& src) : INHERITED() { |
michael@0 | 38 | fParent = NULL; |
michael@0 | 39 | m_opacity = src.m_opacity; |
michael@0 | 40 | m_size = src.m_size; |
michael@0 | 41 | m_position = src.m_position; |
michael@0 | 42 | m_anchorPoint = src.m_anchorPoint; |
michael@0 | 43 | |
michael@0 | 44 | fMatrix = src.fMatrix; |
michael@0 | 45 | fChildrenMatrix = src.fChildrenMatrix; |
michael@0 | 46 | fFlags = src.fFlags; |
michael@0 | 47 | |
michael@0 | 48 | #ifdef DEBUG_TRACK_NEW_DELETE |
michael@0 | 49 | gLayerAllocCount += 1; |
michael@0 | 50 | SkDebugf("SkLayer copy: %d\n", gLayerAllocCount); |
michael@0 | 51 | #endif |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | SkLayer::~SkLayer() { |
michael@0 | 55 | this->removeChildren(); |
michael@0 | 56 | |
michael@0 | 57 | #ifdef DEBUG_TRACK_NEW_DELETE |
michael@0 | 58 | gLayerAllocCount -= 1; |
michael@0 | 59 | SkDebugf("SkLayer delete: %d\n", gLayerAllocCount); |
michael@0 | 60 | #endif |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 64 | |
michael@0 | 65 | bool SkLayer::isInheritFromRootTransform() const { |
michael@0 | 66 | return (fFlags & kInheritFromRootTransform_Flag) != 0; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | void SkLayer::setInheritFromRootTransform(bool doInherit) { |
michael@0 | 70 | if (doInherit) { |
michael@0 | 71 | fFlags |= kInheritFromRootTransform_Flag; |
michael@0 | 72 | } else { |
michael@0 | 73 | fFlags &= ~kInheritFromRootTransform_Flag; |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | void SkLayer::setMatrix(const SkMatrix& matrix) { |
michael@0 | 78 | fMatrix = matrix; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | void SkLayer::setChildrenMatrix(const SkMatrix& matrix) { |
michael@0 | 82 | fChildrenMatrix = matrix; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 86 | |
michael@0 | 87 | int SkLayer::countChildren() const { |
michael@0 | 88 | return m_children.count(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | SkLayer* SkLayer::getChild(int index) const { |
michael@0 | 92 | if ((unsigned)index < (unsigned)m_children.count()) { |
michael@0 | 93 | SkASSERT(m_children[index]->fParent == this); |
michael@0 | 94 | return m_children[index]; |
michael@0 | 95 | } |
michael@0 | 96 | return NULL; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | SkLayer* SkLayer::addChild(SkLayer* child) { |
michael@0 | 100 | SkASSERT(this != child); |
michael@0 | 101 | child->ref(); |
michael@0 | 102 | child->detachFromParent(); |
michael@0 | 103 | SkASSERT(child->fParent == NULL); |
michael@0 | 104 | child->fParent = this; |
michael@0 | 105 | |
michael@0 | 106 | *m_children.append() = child; |
michael@0 | 107 | return child; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | void SkLayer::detachFromParent() { |
michael@0 | 111 | if (fParent) { |
michael@0 | 112 | int index = fParent->m_children.find(this); |
michael@0 | 113 | SkASSERT(index >= 0); |
michael@0 | 114 | fParent->m_children.remove(index); |
michael@0 | 115 | fParent = NULL; |
michael@0 | 116 | this->unref(); // this call might delete us |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | void SkLayer::removeChildren() { |
michael@0 | 121 | int count = m_children.count(); |
michael@0 | 122 | for (int i = 0; i < count; i++) { |
michael@0 | 123 | SkLayer* child = m_children[i]; |
michael@0 | 124 | SkASSERT(child->fParent == this); |
michael@0 | 125 | child->fParent = NULL; // in case it has more than one owner |
michael@0 | 126 | child->unref(); |
michael@0 | 127 | } |
michael@0 | 128 | m_children.reset(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | SkLayer* SkLayer::getRootLayer() const { |
michael@0 | 132 | const SkLayer* root = this; |
michael@0 | 133 | while (root->fParent != NULL) { |
michael@0 | 134 | root = root->fParent; |
michael@0 | 135 | } |
michael@0 | 136 | return const_cast<SkLayer*>(root); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 140 | |
michael@0 | 141 | void SkLayer::getLocalTransform(SkMatrix* matrix) const { |
michael@0 | 142 | matrix->setTranslate(m_position.fX, m_position.fY); |
michael@0 | 143 | |
michael@0 | 144 | SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width()); |
michael@0 | 145 | SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height()); |
michael@0 | 146 | matrix->preTranslate(tx, ty); |
michael@0 | 147 | matrix->preConcat(this->getMatrix()); |
michael@0 | 148 | matrix->preTranslate(-tx, -ty); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | void SkLayer::localToGlobal(SkMatrix* matrix) const { |
michael@0 | 152 | this->getLocalTransform(matrix); |
michael@0 | 153 | |
michael@0 | 154 | if (this->isInheritFromRootTransform()) { |
michael@0 | 155 | matrix->postConcat(this->getRootLayer()->getMatrix()); |
michael@0 | 156 | return; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | const SkLayer* layer = this; |
michael@0 | 160 | while (layer->fParent != NULL) { |
michael@0 | 161 | layer = layer->fParent; |
michael@0 | 162 | |
michael@0 | 163 | SkMatrix tmp; |
michael@0 | 164 | layer->getLocalTransform(&tmp); |
michael@0 | 165 | tmp.preConcat(layer->getChildrenMatrix()); |
michael@0 | 166 | matrix->postConcat(tmp); |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 171 | |
michael@0 | 172 | void SkLayer::onDraw(SkCanvas*, SkScalar opacity) { |
michael@0 | 173 | // SkDebugf("----- no onDraw for %p\n", this); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | #include "SkString.h" |
michael@0 | 177 | |
michael@0 | 178 | void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) { |
michael@0 | 179 | #if 0 |
michael@0 | 180 | SkString str1, str2; |
michael@0 | 181 | // this->getMatrix().toDumpString(&str1); |
michael@0 | 182 | // this->getChildrenMatrix().toDumpString(&str2); |
michael@0 | 183 | SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n", |
michael@0 | 184 | this, opacity * this->getOpacity(), m_size.width(), m_size.height(), |
michael@0 | 185 | m_position.fX, m_position.fY, str1.c_str(), str2.c_str()); |
michael@0 | 186 | #endif |
michael@0 | 187 | |
michael@0 | 188 | opacity = SkScalarMul(opacity, this->getOpacity()); |
michael@0 | 189 | if (opacity <= 0) { |
michael@0 | 190 | // SkDebugf("---- abort drawing %p opacity %g\n", this, opacity); |
michael@0 | 191 | return; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | SkAutoCanvasRestore acr(canvas, true); |
michael@0 | 195 | |
michael@0 | 196 | // apply our local transform |
michael@0 | 197 | { |
michael@0 | 198 | SkMatrix tmp; |
michael@0 | 199 | this->getLocalTransform(&tmp); |
michael@0 | 200 | if (this->isInheritFromRootTransform()) { |
michael@0 | 201 | // should we also apply the root's childrenMatrix? |
michael@0 | 202 | canvas->setMatrix(getRootLayer()->getMatrix()); |
michael@0 | 203 | } |
michael@0 | 204 | canvas->concat(tmp); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | this->onDraw(canvas, opacity); |
michael@0 | 208 | |
michael@0 | 209 | #ifdef DEBUG_DRAW_LAYER_BOUNDS |
michael@0 | 210 | { |
michael@0 | 211 | SkRect r = SkRect::MakeSize(this->getSize()); |
michael@0 | 212 | SkPaint p; |
michael@0 | 213 | p.setAntiAlias(true); |
michael@0 | 214 | p.setStyle(SkPaint::kStroke_Style); |
michael@0 | 215 | p.setStrokeWidth(SkIntToScalar(2)); |
michael@0 | 216 | p.setColor(0xFFFF44DD); |
michael@0 | 217 | canvas->drawRect(r, p); |
michael@0 | 218 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); |
michael@0 | 219 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); |
michael@0 | 220 | } |
michael@0 | 221 | #endif |
michael@0 | 222 | |
michael@0 | 223 | int count = this->countChildren(); |
michael@0 | 224 | if (count > 0) { |
michael@0 | 225 | canvas->concat(this->getChildrenMatrix()); |
michael@0 | 226 | for (int i = 0; i < count; i++) { |
michael@0 | 227 | this->getChild(i)->draw(canvas, opacity); |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | } |