michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkLayer.h" michael@0: #include "SkCanvas.h" michael@0: michael@0: //#define DEBUG_DRAW_LAYER_BOUNDS michael@0: //#define DEBUG_TRACK_NEW_DELETE michael@0: michael@0: #ifdef DEBUG_TRACK_NEW_DELETE michael@0: static int gLayerAllocCount; michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkLayer::SkLayer() { michael@0: fParent = NULL; michael@0: m_opacity = SK_Scalar1; michael@0: m_size.set(0, 0); michael@0: m_position.set(0, 0); michael@0: m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf); michael@0: michael@0: fMatrix.reset(); michael@0: fChildrenMatrix.reset(); michael@0: fFlags = 0; michael@0: michael@0: #ifdef DEBUG_TRACK_NEW_DELETE michael@0: gLayerAllocCount += 1; michael@0: SkDebugf("SkLayer new: %d\n", gLayerAllocCount); michael@0: #endif michael@0: } michael@0: michael@0: SkLayer::SkLayer(const SkLayer& src) : INHERITED() { michael@0: fParent = NULL; michael@0: m_opacity = src.m_opacity; michael@0: m_size = src.m_size; michael@0: m_position = src.m_position; michael@0: m_anchorPoint = src.m_anchorPoint; michael@0: michael@0: fMatrix = src.fMatrix; michael@0: fChildrenMatrix = src.fChildrenMatrix; michael@0: fFlags = src.fFlags; michael@0: michael@0: #ifdef DEBUG_TRACK_NEW_DELETE michael@0: gLayerAllocCount += 1; michael@0: SkDebugf("SkLayer copy: %d\n", gLayerAllocCount); michael@0: #endif michael@0: } michael@0: michael@0: SkLayer::~SkLayer() { michael@0: this->removeChildren(); michael@0: michael@0: #ifdef DEBUG_TRACK_NEW_DELETE michael@0: gLayerAllocCount -= 1; michael@0: SkDebugf("SkLayer delete: %d\n", gLayerAllocCount); michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkLayer::isInheritFromRootTransform() const { michael@0: return (fFlags & kInheritFromRootTransform_Flag) != 0; michael@0: } michael@0: michael@0: void SkLayer::setInheritFromRootTransform(bool doInherit) { michael@0: if (doInherit) { michael@0: fFlags |= kInheritFromRootTransform_Flag; michael@0: } else { michael@0: fFlags &= ~kInheritFromRootTransform_Flag; michael@0: } michael@0: } michael@0: michael@0: void SkLayer::setMatrix(const SkMatrix& matrix) { michael@0: fMatrix = matrix; michael@0: } michael@0: michael@0: void SkLayer::setChildrenMatrix(const SkMatrix& matrix) { michael@0: fChildrenMatrix = matrix; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: int SkLayer::countChildren() const { michael@0: return m_children.count(); michael@0: } michael@0: michael@0: SkLayer* SkLayer::getChild(int index) const { michael@0: if ((unsigned)index < (unsigned)m_children.count()) { michael@0: SkASSERT(m_children[index]->fParent == this); michael@0: return m_children[index]; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: SkLayer* SkLayer::addChild(SkLayer* child) { michael@0: SkASSERT(this != child); michael@0: child->ref(); michael@0: child->detachFromParent(); michael@0: SkASSERT(child->fParent == NULL); michael@0: child->fParent = this; michael@0: michael@0: *m_children.append() = child; michael@0: return child; michael@0: } michael@0: michael@0: void SkLayer::detachFromParent() { michael@0: if (fParent) { michael@0: int index = fParent->m_children.find(this); michael@0: SkASSERT(index >= 0); michael@0: fParent->m_children.remove(index); michael@0: fParent = NULL; michael@0: this->unref(); // this call might delete us michael@0: } michael@0: } michael@0: michael@0: void SkLayer::removeChildren() { michael@0: int count = m_children.count(); michael@0: for (int i = 0; i < count; i++) { michael@0: SkLayer* child = m_children[i]; michael@0: SkASSERT(child->fParent == this); michael@0: child->fParent = NULL; // in case it has more than one owner michael@0: child->unref(); michael@0: } michael@0: m_children.reset(); michael@0: } michael@0: michael@0: SkLayer* SkLayer::getRootLayer() const { michael@0: const SkLayer* root = this; michael@0: while (root->fParent != NULL) { michael@0: root = root->fParent; michael@0: } michael@0: return const_cast(root); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkLayer::getLocalTransform(SkMatrix* matrix) const { michael@0: matrix->setTranslate(m_position.fX, m_position.fY); michael@0: michael@0: SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width()); michael@0: SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height()); michael@0: matrix->preTranslate(tx, ty); michael@0: matrix->preConcat(this->getMatrix()); michael@0: matrix->preTranslate(-tx, -ty); michael@0: } michael@0: michael@0: void SkLayer::localToGlobal(SkMatrix* matrix) const { michael@0: this->getLocalTransform(matrix); michael@0: michael@0: if (this->isInheritFromRootTransform()) { michael@0: matrix->postConcat(this->getRootLayer()->getMatrix()); michael@0: return; michael@0: } michael@0: michael@0: const SkLayer* layer = this; michael@0: while (layer->fParent != NULL) { michael@0: layer = layer->fParent; michael@0: michael@0: SkMatrix tmp; michael@0: layer->getLocalTransform(&tmp); michael@0: tmp.preConcat(layer->getChildrenMatrix()); michael@0: matrix->postConcat(tmp); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkLayer::onDraw(SkCanvas*, SkScalar opacity) { michael@0: // SkDebugf("----- no onDraw for %p\n", this); michael@0: } michael@0: michael@0: #include "SkString.h" michael@0: michael@0: void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) { michael@0: #if 0 michael@0: SkString str1, str2; michael@0: // this->getMatrix().toDumpString(&str1); michael@0: // this->getChildrenMatrix().toDumpString(&str2); michael@0: SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n", michael@0: this, opacity * this->getOpacity(), m_size.width(), m_size.height(), michael@0: m_position.fX, m_position.fY, str1.c_str(), str2.c_str()); michael@0: #endif michael@0: michael@0: opacity = SkScalarMul(opacity, this->getOpacity()); michael@0: if (opacity <= 0) { michael@0: // SkDebugf("---- abort drawing %p opacity %g\n", this, opacity); michael@0: return; michael@0: } michael@0: michael@0: SkAutoCanvasRestore acr(canvas, true); michael@0: michael@0: // apply our local transform michael@0: { michael@0: SkMatrix tmp; michael@0: this->getLocalTransform(&tmp); michael@0: if (this->isInheritFromRootTransform()) { michael@0: // should we also apply the root's childrenMatrix? michael@0: canvas->setMatrix(getRootLayer()->getMatrix()); michael@0: } michael@0: canvas->concat(tmp); michael@0: } michael@0: michael@0: this->onDraw(canvas, opacity); michael@0: michael@0: #ifdef DEBUG_DRAW_LAYER_BOUNDS michael@0: { michael@0: SkRect r = SkRect::MakeSize(this->getSize()); michael@0: SkPaint p; michael@0: p.setAntiAlias(true); michael@0: p.setStyle(SkPaint::kStroke_Style); michael@0: p.setStrokeWidth(SkIntToScalar(2)); michael@0: p.setColor(0xFFFF44DD); michael@0: canvas->drawRect(r, p); michael@0: canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); michael@0: canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); michael@0: } michael@0: #endif michael@0: michael@0: int count = this->countChildren(); michael@0: if (count > 0) { michael@0: canvas->concat(this->getChildrenMatrix()); michael@0: for (int i = 0; i < count; i++) { michael@0: this->getChild(i)->draw(canvas, opacity); michael@0: } michael@0: } michael@0: }