delete SkLayer.cpp
Change-Id: I7c0325daef62948ea9f5ef58a06236be502f3ef7 Reviewed-on: https://skia-review.googlesource.com/7783 Commit-Queue: Hal Canary <halcanary@google.com> Commit-Queue: Ben Wagner <bungeman@google.com> Reviewed-by: Ben Wagner <bungeman@google.com>
This commit is contained in:
parent
e4ba1059dd
commit
738bc58988
@ -1,244 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
#include "SampleCode.h"
|
||||
#include "SkCanvas.h"
|
||||
#include "SkPaint.h"
|
||||
#include "SkView.h"
|
||||
#include "SkLayer.h"
|
||||
|
||||
#include "SkMatrix44.h"
|
||||
static void test_inv(const char label[], const SkMatrix44& mat) {
|
||||
SkDebugf("%s\n", label);
|
||||
mat.dump();
|
||||
|
||||
SkMatrix44 inv;
|
||||
if (mat.invert(&inv)) {
|
||||
inv.dump();
|
||||
} else {
|
||||
SkDebugf("--- invert failed\n");
|
||||
}
|
||||
|
||||
SkMatrix44 a, b;
|
||||
a.setConcat(mat, inv);
|
||||
b.setConcat(inv, mat);
|
||||
SkDebugf("concat mat with inverse pre=%d post=%d\n", a.isIdentity(), b.isIdentity());
|
||||
if (!a.isIdentity()) {
|
||||
a.dump();
|
||||
}
|
||||
if (!b.isIdentity()) {
|
||||
b.dump();
|
||||
}
|
||||
SkDebugf("\n");
|
||||
}
|
||||
|
||||
static void test_map(SkScalar x0, SkScalar y0, SkScalar z0,
|
||||
const SkMatrix44& mat,
|
||||
SkScalar x1, SkScalar y1, SkScalar z1) {
|
||||
SkVector4 src, dst;
|
||||
src.set(x0, y0, z0);
|
||||
dst = mat * src;
|
||||
SkDebugf("map: src: %g %g %g dst: %g %g %g (%g) expected: %g %g %g match: %d\n",
|
||||
x0, y0, z0,
|
||||
dst.fData[0], dst.fData[1], dst.fData[2], dst.fData[3],
|
||||
x1, y1, z1,
|
||||
dst.fData[0] == x1 && dst.fData[1] == y1 && dst.fData[2] == z1);
|
||||
}
|
||||
|
||||
static void test_33(const SkMatrix44& mat,
|
||||
SkScalar x0, SkScalar x1, SkScalar x2,
|
||||
SkScalar y0, SkScalar y1, SkScalar y2) {
|
||||
SkMatrix dst = mat;
|
||||
if (dst[0] != x0 || dst[1] != x1 || dst[2] != x2 ||
|
||||
dst[3] != y0 || dst[4] != y1 || dst[5] != y2) {
|
||||
SkString str;
|
||||
dst.toString(&str);
|
||||
SkDebugf("3x3: expected 3x3 [%g %g %g] [%g %g %g] bug got %s\n",
|
||||
x0, x1, x2, y0, y1, y2, str.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static void test44() {
|
||||
SkMatrix44 m0, m1, m2;
|
||||
|
||||
test_inv("identity", m0);
|
||||
m0.setTranslate(2,3,4);
|
||||
test_inv("translate", m0);
|
||||
m0.setScale(2,3,4);
|
||||
test_inv("scale", m0);
|
||||
m0.postTranslate(5, 6, 7);
|
||||
test_inv("postTranslate", m0);
|
||||
m0.setScale(2,3,4);
|
||||
m1.setTranslate(5, 6, 7);
|
||||
m0.setConcat(m0, m1);
|
||||
test_inv("postTranslate2", m0);
|
||||
m0.setScale(2,3,4);
|
||||
m0.preTranslate(5, 6, 7);
|
||||
test_inv("preTranslate", m0);
|
||||
|
||||
m0.setScale(2, 4, 6);
|
||||
m0.postScale(SkDoubleToMScalar(0.5));
|
||||
test_inv("scale/postscale to 1,2,3", m0);
|
||||
|
||||
m0.reset();
|
||||
test_map(1, 0, 0, m0, 1, 0, 0);
|
||||
test_map(0, 1, 0, m0, 0, 1, 0);
|
||||
test_map(0, 0, 1, m0, 0, 0, 1);
|
||||
m0.setScale(2, 3, 4);
|
||||
test_map(1, 0, 0, m0, 2, 0, 0);
|
||||
test_map(0, 1, 0, m0, 0, 3, 0);
|
||||
test_map(0, 0, 1, m0, 0, 0, 4);
|
||||
m0.setTranslate(2, 3, 4);
|
||||
test_map(0, 0, 0, m0, 2, 3, 4);
|
||||
m0.preScale(5, 6, 7);
|
||||
test_map(1, 0, 0, m0, 7, 3, 4);
|
||||
test_map(0, 1, 0, m0, 2, 9, 4);
|
||||
test_map(0, 0, 1, m0, 2, 3, 11);
|
||||
|
||||
SkMScalar deg = 45;
|
||||
m0.setRotateDegreesAbout(0, 0, 1, deg);
|
||||
test_map(1, 0, 0, m0, 0.707106769, -0.707106769, 0);
|
||||
|
||||
m0.reset();
|
||||
test_33(m0, 1, 0, 0, 0, 1, 0);
|
||||
m0.setTranslate(3, 4, 5);
|
||||
test_33(m0, 1, 0, 3, 0, 1, 4);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void dump_layers(const SkLayer* layer, int tab = 0) {
|
||||
SkMatrix matrix;
|
||||
SkString matrixStr;
|
||||
|
||||
layer->getLocalTransform(&matrix);
|
||||
matrix.toString(&matrixStr);
|
||||
|
||||
for (int j = 0; j < tab; j++) {
|
||||
SkDebugf(" ");
|
||||
}
|
||||
SkDebugf("layer=%p parent=%p size=[%g %g] transform=%s\n",
|
||||
layer, layer->getParent(), layer->getWidth(), layer->getHeight(),
|
||||
matrixStr.c_str());
|
||||
for (int i = 0; i < layer->countChildren(); i++) {
|
||||
dump_layers(layer->getChild(i), tab + 4);
|
||||
}
|
||||
}
|
||||
|
||||
class TestLayer : public SkLayer {
|
||||
public:
|
||||
TestLayer(SkColor c) : fColor(c) {}
|
||||
|
||||
protected:
|
||||
virtual void onDraw(SkCanvas* canvas, SkScalar opacity) {
|
||||
SkRect r;
|
||||
r.set(0, 0, this->getWidth(), this->getHeight());
|
||||
|
||||
SkPaint paint;
|
||||
paint.setColor(fColor);
|
||||
paint.setAlpha(SkScalarRoundToInt(opacity * 255));
|
||||
|
||||
canvas->drawRect(r, paint);
|
||||
}
|
||||
|
||||
private:
|
||||
SkColor fColor;
|
||||
};
|
||||
|
||||
class SkLayerView : public SkView {
|
||||
private:
|
||||
SkLayer* fRootLayer;
|
||||
SkLayer* fLastChild;
|
||||
public:
|
||||
SkLayerView() {
|
||||
test44();
|
||||
static const int W = 600;
|
||||
static const int H = 440;
|
||||
static const struct {
|
||||
int fWidth;
|
||||
int fHeight;
|
||||
SkColor fColor;
|
||||
int fPosX;
|
||||
int fPosY;
|
||||
} gData[] = {
|
||||
{ 120, 80, SK_ColorRED, 0, 0 },
|
||||
{ 120, 80, SK_ColorGREEN, W - 120, 0 },
|
||||
{ 120, 80, SK_ColorBLUE, 0, H - 80 },
|
||||
{ 120, 80, SK_ColorMAGENTA, W - 120, H - 80 },
|
||||
};
|
||||
|
||||
fRootLayer = new TestLayer(0xFFDDDDDD);
|
||||
fRootLayer->setSize(W, H);
|
||||
for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
|
||||
SkLayer* child = new TestLayer(gData[i].fColor);
|
||||
child->setSize(gData[i].fWidth, gData[i].fHeight);
|
||||
child->setPosition(gData[i].fPosX, gData[i].fPosY);
|
||||
fRootLayer->addChild(child)->unref();
|
||||
}
|
||||
|
||||
SkLayer* child = new TestLayer(0xFFDD8844);
|
||||
child->setSize(120, 80);
|
||||
child->setPosition(fRootLayer->getWidth()/2 - child->getWidth()/2,
|
||||
fRootLayer->getHeight()/2 - child->getHeight()/2);
|
||||
child->setAnchorPoint(SK_ScalarHalf, SK_ScalarHalf);
|
||||
{
|
||||
SkMatrix m;
|
||||
m.setRotate(SkIntToScalar(30));
|
||||
child->setMatrix(m);
|
||||
}
|
||||
fLastChild = child;
|
||||
fRootLayer->addChild(child)->unref();
|
||||
|
||||
if (false) {
|
||||
SkMatrix matrix;
|
||||
matrix.setScale(0.5, 0.5);
|
||||
fRootLayer->setMatrix(matrix);
|
||||
}
|
||||
|
||||
// dump_layers(fRootLayer);
|
||||
}
|
||||
|
||||
virtual ~SkLayerView() {
|
||||
SkSafeUnref(fRootLayer);
|
||||
}
|
||||
|
||||
protected:
|
||||
// overrides from SkEventSink
|
||||
virtual bool onQuery(SkEvent* evt) {
|
||||
if (SampleCode::TitleQ(*evt)) {
|
||||
SampleCode::TitleR(evt, "SkLayer");
|
||||
return true;
|
||||
}
|
||||
return this->INHERITED::onQuery(evt);
|
||||
}
|
||||
|
||||
virtual void onDraw(SkCanvas* canvas) {
|
||||
canvas->drawColor(SK_ColorWHITE);
|
||||
|
||||
canvas->translate(20, 20);
|
||||
fRootLayer->draw(canvas);
|
||||
|
||||
// visual test of getLocalTransform
|
||||
if (true) {
|
||||
SkMatrix matrix;
|
||||
fLastChild->localToGlobal(&matrix);
|
||||
SkPaint paint;
|
||||
paint.setStyle(SkPaint::kStroke_Style);
|
||||
paint.setStrokeWidth(5);
|
||||
paint.setColor(0x88FF0000);
|
||||
canvas->concat(matrix);
|
||||
canvas->drawRect(SkRect::MakeSize(fLastChild->getSize()), paint);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef SkView INHERITED;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static SkView* MyFactory() { return new SkLayerView; }
|
||||
static SkViewRegister reg(MyFactory);
|
@ -1,229 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
#include "SkLayer.h"
|
||||
#include "SkCanvas.h"
|
||||
|
||||
//#define DEBUG_DRAW_LAYER_BOUNDS
|
||||
//#define DEBUG_TRACK_NEW_DELETE
|
||||
|
||||
#ifdef DEBUG_TRACK_NEW_DELETE
|
||||
static int gLayerAllocCount;
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
SkLayer::SkLayer() {
|
||||
fParent = nullptr;
|
||||
m_opacity = SK_Scalar1;
|
||||
m_size.set(0, 0);
|
||||
m_position.set(0, 0);
|
||||
m_anchorPoint.set(SK_ScalarHalf, SK_ScalarHalf);
|
||||
|
||||
fMatrix.reset();
|
||||
fChildrenMatrix.reset();
|
||||
fFlags = 0;
|
||||
|
||||
#ifdef DEBUG_TRACK_NEW_DELETE
|
||||
gLayerAllocCount += 1;
|
||||
SkDebugf("SkLayer new: %d\n", gLayerAllocCount);
|
||||
#endif
|
||||
}
|
||||
|
||||
SkLayer::SkLayer(const SkLayer& src) : INHERITED() {
|
||||
fParent = nullptr;
|
||||
m_opacity = src.m_opacity;
|
||||
m_size = src.m_size;
|
||||
m_position = src.m_position;
|
||||
m_anchorPoint = src.m_anchorPoint;
|
||||
|
||||
fMatrix = src.fMatrix;
|
||||
fChildrenMatrix = src.fChildrenMatrix;
|
||||
fFlags = src.fFlags;
|
||||
|
||||
#ifdef DEBUG_TRACK_NEW_DELETE
|
||||
gLayerAllocCount += 1;
|
||||
SkDebugf("SkLayer copy: %d\n", gLayerAllocCount);
|
||||
#endif
|
||||
}
|
||||
|
||||
SkLayer::~SkLayer() {
|
||||
this->removeChildren();
|
||||
|
||||
#ifdef DEBUG_TRACK_NEW_DELETE
|
||||
gLayerAllocCount -= 1;
|
||||
SkDebugf("SkLayer delete: %d\n", gLayerAllocCount);
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool SkLayer::isInheritFromRootTransform() const {
|
||||
return (fFlags & kInheritFromRootTransform_Flag) != 0;
|
||||
}
|
||||
|
||||
void SkLayer::setInheritFromRootTransform(bool doInherit) {
|
||||
if (doInherit) {
|
||||
fFlags |= kInheritFromRootTransform_Flag;
|
||||
} else {
|
||||
fFlags &= ~kInheritFromRootTransform_Flag;
|
||||
}
|
||||
}
|
||||
|
||||
void SkLayer::setMatrix(const SkMatrix& matrix) {
|
||||
fMatrix = matrix;
|
||||
}
|
||||
|
||||
void SkLayer::setChildrenMatrix(const SkMatrix& matrix) {
|
||||
fChildrenMatrix = matrix;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int SkLayer::countChildren() const {
|
||||
return m_children.count();
|
||||
}
|
||||
|
||||
SkLayer* SkLayer::getChild(int index) const {
|
||||
if ((unsigned)index < (unsigned)m_children.count()) {
|
||||
SkASSERT(m_children[index]->fParent == this);
|
||||
return m_children[index];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SkLayer* SkLayer::addChild(SkLayer* child) {
|
||||
SkASSERT(this != child);
|
||||
child->ref();
|
||||
child->detachFromParent();
|
||||
SkASSERT(child->fParent == nullptr);
|
||||
child->fParent = this;
|
||||
|
||||
*m_children.append() = child;
|
||||
return child;
|
||||
}
|
||||
|
||||
void SkLayer::detachFromParent() {
|
||||
if (fParent) {
|
||||
int index = fParent->m_children.find(this);
|
||||
SkASSERT(index >= 0);
|
||||
fParent->m_children.remove(index);
|
||||
fParent = nullptr;
|
||||
this->unref(); // this call might delete us
|
||||
}
|
||||
}
|
||||
|
||||
void SkLayer::removeChildren() {
|
||||
int count = m_children.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
SkLayer* child = m_children[i];
|
||||
SkASSERT(child->fParent == this);
|
||||
child->fParent = nullptr; // in case it has more than one owner
|
||||
child->unref();
|
||||
}
|
||||
m_children.reset();
|
||||
}
|
||||
|
||||
SkLayer* SkLayer::getRootLayer() const {
|
||||
const SkLayer* root = this;
|
||||
while (root->fParent != nullptr) {
|
||||
root = root->fParent;
|
||||
}
|
||||
return const_cast<SkLayer*>(root);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void SkLayer::getLocalTransform(SkMatrix* matrix) const {
|
||||
matrix->setTranslate(m_position.fX, m_position.fY);
|
||||
|
||||
SkScalar tx = SkScalarMul(m_anchorPoint.fX, m_size.width());
|
||||
SkScalar ty = SkScalarMul(m_anchorPoint.fY, m_size.height());
|
||||
matrix->preTranslate(tx, ty);
|
||||
matrix->preConcat(this->getMatrix());
|
||||
matrix->preTranslate(-tx, -ty);
|
||||
}
|
||||
|
||||
void SkLayer::localToGlobal(SkMatrix* matrix) const {
|
||||
this->getLocalTransform(matrix);
|
||||
|
||||
if (this->isInheritFromRootTransform()) {
|
||||
matrix->postConcat(this->getRootLayer()->getMatrix());
|
||||
return;
|
||||
}
|
||||
|
||||
const SkLayer* layer = this;
|
||||
while (layer->fParent != nullptr) {
|
||||
layer = layer->fParent;
|
||||
|
||||
SkMatrix tmp;
|
||||
layer->getLocalTransform(&tmp);
|
||||
tmp.preConcat(layer->getChildrenMatrix());
|
||||
matrix->postConcat(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void SkLayer::onDraw(SkCanvas*, SkScalar opacity) {
|
||||
// SkDebugf("----- no onDraw for %p\n", this);
|
||||
}
|
||||
|
||||
#include "SkString.h"
|
||||
|
||||
void SkLayer::draw(SkCanvas* canvas, SkScalar opacity) {
|
||||
#if 0
|
||||
SkString str1, str2;
|
||||
// this->getMatrix().toDumpString(&str1);
|
||||
// this->getChildrenMatrix().toDumpString(&str2);
|
||||
SkDebugf("--- drawlayer %p opacity %g size [%g %g] pos [%g %g] matrix %s children %s\n",
|
||||
this, opacity * this->getOpacity(), m_size.width(), m_size.height(),
|
||||
m_position.fX, m_position.fY, str1.c_str(), str2.c_str());
|
||||
#endif
|
||||
|
||||
opacity = SkScalarMul(opacity, this->getOpacity());
|
||||
if (opacity <= 0) {
|
||||
// SkDebugf("---- abort drawing %p opacity %g\n", this, opacity);
|
||||
return;
|
||||
}
|
||||
|
||||
SkAutoCanvasRestore acr(canvas, true);
|
||||
|
||||
// apply our local transform
|
||||
{
|
||||
SkMatrix tmp;
|
||||
this->getLocalTransform(&tmp);
|
||||
if (this->isInheritFromRootTransform()) {
|
||||
// should we also apply the root's childrenMatrix?
|
||||
canvas->setMatrix(getRootLayer()->getMatrix());
|
||||
}
|
||||
canvas->concat(tmp);
|
||||
}
|
||||
|
||||
this->onDraw(canvas, opacity);
|
||||
|
||||
#ifdef DEBUG_DRAW_LAYER_BOUNDS
|
||||
{
|
||||
SkRect r = SkRect::MakeSize(this->getSize());
|
||||
SkPaint p;
|
||||
p.setAntiAlias(true);
|
||||
p.setStyle(SkPaint::kStroke_Style);
|
||||
p.setStrokeWidth(SkIntToScalar(2));
|
||||
p.setColor(0xFFFF44DD);
|
||||
canvas->drawRect(r, p);
|
||||
canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
|
||||
canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
|
||||
}
|
||||
#endif
|
||||
|
||||
int count = this->countChildren();
|
||||
if (count > 0) {
|
||||
canvas->concat(this->getChildrenMatrix());
|
||||
for (int i = 0; i < count; i++) {
|
||||
this->getChild(i)->draw(canvas, opacity);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user