From 81dc331e65ba5a24d515c11890234dd205408c89 Mon Sep 17 00:00:00 2001 From: "reed@android.com" Date: Thu, 18 Feb 2010 19:32:03 +0000 Subject: [PATCH] update layers git-svn-id: http://skia.googlecode.com/svn/trunk@502 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/effects/SkAvoidXfermode.h | 8 ++--- include/utils/SkLayer.h | 55 +++++++++++++++++++--------- src/utils/SkLayer.cpp | 59 +++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 21 deletions(-) diff --git a/include/effects/SkAvoidXfermode.h b/include/effects/SkAvoidXfermode.h index 2803c07ce1..9af4a4b58e 100644 --- a/include/effects/SkAvoidXfermode.h +++ b/include/effects/SkAvoidXfermode.h @@ -38,13 +38,13 @@ public: Avoid: In this mode, drawing is allowed only on destination pixels that are different from the op-color. - Tolerance near 0: avoid anything close to the op-color - Tolerance near 255: avoid only colors very close to the op-color + Tolerance near 0: avoid any colors even remotely similar to the op-color + Tolerance near 255: avoid only colors nearly identical to the op-color Target: In this mode, drawing only occurs on destination pixels that are similar to the op-color - Tolerance near 0: draw on colors that are very close to op-color - Tolerance near 255: draw on colors that to the op-color + Tolerance near 0: draw only on colors that are nearly identical to the op-color + Tolerance near 255: draw on any colors even remotely similar to the op-color */ SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode); diff --git a/include/utils/SkLayer.h b/include/utils/SkLayer.h index c277ddabed..36eccc7873 100644 --- a/include/utils/SkLayer.h +++ b/include/utils/SkLayer.h @@ -20,6 +20,7 @@ #include "SkRefCnt.h" #include "SkTDArray.h" #include "SkColor.h" +#include "SkMatrix.h" #include "SkPoint.h" #include "SkRect.h" #include "SkSize.h" @@ -60,16 +61,36 @@ public: SkLayer(const SkLayer&); virtual ~SkLayer(); - // setters - - void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); } - void setOpacity(SkScalar opacity) { m_opacity = opacity; } + // deprecated void setTranslation(SkScalar x, SkScalar y) { m_translation.set(x, y); } void setRotation(SkScalar a) { m_angleTransform = a; m_doRotation = true; } void setScale(SkScalar x, SkScalar y) { m_scale.set(x, y); } + SkPoint position() const { return m_position; } + SkPoint translation() const { return m_translation; } + SkSize size() const { return m_size; } + SkRect bounds() const { + SkRect rect; + rect.set(m_position.fX, m_position.fY, + m_position.fX + m_size.width(), + m_position.fY + m_size.height()); + rect.offset(m_translation.fX, m_translation.fY); + return rect; + } + + const SkSize& getSize() const { return m_size; } + void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); } + + SkScalar getOpacity() const { return m_opacity; } + void setOpacity(SkScalar opacity) { m_opacity = opacity; } + + const SkPoint& getPosition() const { return m_position; } void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); } + + const SkPoint& getAnchorPoint() const { return m_anchorPoint; } void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); } + virtual void setBackgroundColor(SkColor color) { m_backgroundColor = color; m_backgroundColorSet = true; } + void setFixedPosition(SkLength left, SkLength top, SkLength right, SkLength bottom) { m_fixedLeft = left; m_fixedTop = top; @@ -78,19 +99,11 @@ public: m_isFixed = true; } - // getters + const SkMatrix& getMatrix() const { return fMatrix; } + void setMatrix(const SkMatrix&); - SkPoint position() const { return m_position; } - SkPoint translation() const { return m_translation; } - SkSize size() const { return m_size; } - SkRect bounds() const { - SkRect rect; - rect.set(m_position.fX, m_position.fY, - m_position.fX + m_size.width(), - m_position.fY + m_size.height()); - rect.offset(m_translation.fX, m_translation.fY); - return rect; - } + const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; } + void setChildrenMatrix(const SkMatrix&); // children @@ -101,7 +114,15 @@ public: // paint method - virtual void paintOn(SkPoint offset, SkSize size, SkScalar scale, SkCanvas*) = 0; + virtual void draw(SkCanvas*, SkScalar opacity, const SkRect* viewPort); + +protected: + virtual void onSetupCanvas(SkCanvas*, SkScalar opacity, const SkRect*); + virtual void onDraw(SkCanvas*, SkScalar opacity, const SkRect* viewPort); + +private: + SkMatrix fMatrix; + SkMatrix fChildrenMatrix; public: diff --git a/src/utils/SkLayer.cpp b/src/utils/SkLayer.cpp index c5a6d9a20c..fc1f5e5ccb 100644 --- a/src/utils/SkLayer.cpp +++ b/src/utils/SkLayer.cpp @@ -1,4 +1,5 @@ #include "SkLayer.h" +#include "SkCanvas.h" SkLayer::SkLayer() { m_doRotation = false; @@ -15,6 +16,9 @@ SkLayer::SkLayer() { m_scale.set(1, 1); m_backgroundColor = 0; + + fMatrix.reset(); + fChildrenMatrix.reset(); } SkLayer::SkLayer(const SkLayer& src) { @@ -36,6 +40,9 @@ SkLayer::SkLayer(const SkLayer& src) { m_fixedBottom = src.m_fixedBottom; m_backgroundColor = src.m_backgroundColor; + + fMatrix = src.fMatrix; + fChildrenMatrix = src.fChildrenMatrix; } SkLayer::~SkLayer() { @@ -64,4 +71,56 @@ void SkLayer::removeChildren() { m_children.reset(); } +/////////////////////////////////////////////////////////////////////////////// + +void SkLayer::setMatrix(const SkMatrix& matrix) { + fMatrix = matrix; +} + +void SkLayer::setChildrenMatrix(const SkMatrix& matrix) { + fChildrenMatrix = matrix; +} + +/////////////////////////////////////////////////////////////////////////////// + +void SkLayer::onSetupCanvas(SkCanvas* canvas, SkScalar, const SkRect*) { + SkScalar tx = m_position.fX; + SkScalar ty = m_position.fY; + canvas->translate(tx, ty); + + // now apply our matrix about the anchorPoint + tx = SkScalarMul(m_anchorPoint.fX, m_size.width()); + ty = SkScalarMul(m_anchorPoint.fY, m_size.height()); + canvas->translate(tx, ty); + canvas->concat(this->getMatrix()); + canvas->translate(-tx, -ty); +} + +void SkLayer::onDraw(SkCanvas*, SkScalar opacity, const SkRect* viewport) {} + +void SkLayer::draw(SkCanvas* canvas, SkScalar opacity, const SkRect* viewport) { +#if 0 + SkDebugf("--- drawlayer %p anchor [%g %g] scale [%g %g]\n", this, m_anchorPoint.fX, m_anchorPoint.fY, + m_scale.fX, m_scale.fY); +#endif + + opacity = SkScalarMul(opacity, this->getOpacity()); + if (opacity <= 0 || this->getSize().isEmpty()) { + return; + } + + SkAutoCanvasRestore acr(canvas, false); + canvas->save(SkCanvas::kMatrix_SaveFlag); + + this->onSetupCanvas(canvas, opacity, viewport); + this->onDraw(canvas, opacity, viewport); + + int count = this->countChildren(); + if (count > 0) { + canvas->concat(this->getChildrenMatrix()); + for (int i = 0; i < count; i++) { + this->getChild(i)->draw(canvas, opacity, viewport); + } + } +} -- 2.34.1