Add methods to SkLayerDrawLooper to allow adding layers on top
as well as on the bottom. This is more convenient for some callers who generate layers from a data structure in bottom-to-top, rather than top-to-bottom, order. BUG=242529 R=tomhudson@chromium.org Author: jbroman@chromium.org Review URL: https://chromiumcodereview.appspot.com/15314003 git-svn-id: http://skia.googlecode.com/svn/trunk@9233 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
b148aca07e
commit
8f838259ab
@ -68,6 +68,7 @@
|
||||
'../tests/HashCacheTest.cpp',
|
||||
'../tests/InfRectTest.cpp',
|
||||
'../tests/LListTest.cpp',
|
||||
'../tests/LayerDrawLooperTest.cpp',
|
||||
'../tests/MD5Test.cpp',
|
||||
'../tests/MathTest.cpp',
|
||||
'../tests/MatrixTest.cpp',
|
||||
|
@ -99,6 +99,9 @@ public:
|
||||
*/
|
||||
void addLayer() { this->addLayer(0, 0); }
|
||||
|
||||
/// Similar to addLayer, but adds a layer to the top.
|
||||
SkPaint* addLayerOnTop(const LayerInfo&);
|
||||
|
||||
// overrides from SkDrawLooper
|
||||
virtual void init(SkCanvas*);
|
||||
virtual bool next(SkCanvas*, SkPaint* paint);
|
||||
@ -115,10 +118,9 @@ private:
|
||||
Rec* fNext;
|
||||
SkPaint fPaint;
|
||||
LayerInfo fInfo;
|
||||
|
||||
static Rec* Reverse(Rec*);
|
||||
};
|
||||
Rec* fRecs;
|
||||
Rec* fTopRec;
|
||||
int fCount;
|
||||
|
||||
// state-machine during the init/next cycle
|
||||
|
@ -25,6 +25,7 @@ SkLayerDrawLooper::LayerInfo::LayerInfo() {
|
||||
|
||||
SkLayerDrawLooper::SkLayerDrawLooper()
|
||||
: fRecs(NULL),
|
||||
fTopRec(NULL),
|
||||
fCount(0),
|
||||
fCurrRec(NULL) {
|
||||
}
|
||||
@ -45,6 +46,9 @@ SkPaint* SkLayerDrawLooper::addLayer(const LayerInfo& info) {
|
||||
rec->fNext = fRecs;
|
||||
rec->fInfo = info;
|
||||
fRecs = rec;
|
||||
if (NULL == fTopRec) {
|
||||
fTopRec = rec;
|
||||
}
|
||||
|
||||
return &rec->fPaint;
|
||||
}
|
||||
@ -56,6 +60,23 @@ void SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) {
|
||||
(void)this->addLayer(info);
|
||||
}
|
||||
|
||||
SkPaint* SkLayerDrawLooper::addLayerOnTop(const LayerInfo& info) {
|
||||
fCount += 1;
|
||||
|
||||
Rec* rec = SkNEW(Rec);
|
||||
rec->fNext = NULL;
|
||||
rec->fInfo = info;
|
||||
if (NULL == fRecs) {
|
||||
fRecs = rec;
|
||||
} else {
|
||||
SkASSERT(NULL != fTopRec);
|
||||
fTopRec->fNext = rec;
|
||||
}
|
||||
fTopRec = rec;
|
||||
|
||||
return &rec->fPaint;
|
||||
}
|
||||
|
||||
void SkLayerDrawLooper::init(SkCanvas* canvas) {
|
||||
fCurrRec = fRecs;
|
||||
canvas->save(SkCanvas::kMatrix_SaveFlag);
|
||||
@ -170,18 +191,6 @@ bool SkLayerDrawLooper::next(SkCanvas* canvas, SkPaint* paint) {
|
||||
return true;
|
||||
}
|
||||
|
||||
SkLayerDrawLooper::Rec* SkLayerDrawLooper::Rec::Reverse(Rec* head) {
|
||||
Rec* rec = head;
|
||||
Rec* prev = NULL;
|
||||
while (rec) {
|
||||
Rec* next = rec->fNext;
|
||||
rec->fNext = prev;
|
||||
prev = rec;
|
||||
rec = next;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void SkLayerDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) const {
|
||||
@ -216,6 +225,7 @@ void SkLayerDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) const {
|
||||
SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
|
||||
: INHERITED(buffer),
|
||||
fRecs(NULL),
|
||||
fTopRec(NULL),
|
||||
fCount(0),
|
||||
fCurrRec(NULL) {
|
||||
int count = buffer.readInt();
|
||||
@ -227,13 +237,10 @@ SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
|
||||
info.fColorMode = (SkXfermode::Mode)buffer.readInt();
|
||||
buffer.readPoint(&info.fOffset);
|
||||
info.fPostTranslate = buffer.readBool();
|
||||
buffer.readPaint(this->addLayer(info));
|
||||
buffer.readPaint(this->addLayerOnTop(info));
|
||||
}
|
||||
SkASSERT(count == fCount);
|
||||
|
||||
// we're in reverse order, so fix it now
|
||||
fRecs = Rec::Reverse(fRecs);
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
{
|
||||
Rec* rec = fRecs;
|
||||
|
154
tests/LayerDrawLooperTest.cpp
Normal file
154
tests/LayerDrawLooperTest.cpp
Normal file
@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright 2013 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
#include "Test.h"
|
||||
#include "SkBitmap.h"
|
||||
#include "SkCanvas.h"
|
||||
#include "SkDraw.h"
|
||||
#include "SkDevice.h"
|
||||
#include "SkLayerDrawLooper.h"
|
||||
#include "SkMatrix.h"
|
||||
#include "SkPaint.h"
|
||||
#include "SkRect.h"
|
||||
#include "SkRefCnt.h"
|
||||
#include "SkScalar.h"
|
||||
#include "SkXfermode.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class FakeDevice : public SkDevice {
|
||||
public:
|
||||
FakeDevice() : SkDevice(SkBitmap::kARGB_8888_Config, 100, 100) { }
|
||||
|
||||
virtual void drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) {
|
||||
fLastMatrix = *draw.fMatrix;
|
||||
SkDevice::drawRect(draw, r, paint);
|
||||
}
|
||||
|
||||
SkMatrix fLastMatrix;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
static void test_frontToBack(skiatest::Reporter* reporter) {
|
||||
SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper));
|
||||
SkLayerDrawLooper::LayerInfo layerInfo;
|
||||
|
||||
// Add the front layer, with the defaults.
|
||||
(void)looper->addLayer(layerInfo);
|
||||
|
||||
// Add the back layer, with some layer info set.
|
||||
layerInfo.fOffset.set(SkFloatToScalar(10.0f), SkFloatToScalar(20.0f));
|
||||
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
|
||||
SkPaint* layerPaint = looper->addLayer(layerInfo);
|
||||
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
||||
|
||||
FakeDevice device;
|
||||
SkCanvas canvas(&device);
|
||||
SkPaint paint;
|
||||
looper->init(&canvas);
|
||||
|
||||
// The back layer should come first.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(10.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(20.0f) == device.fLastMatrix.getTranslateY());
|
||||
paint.reset();
|
||||
|
||||
// Then the front layer.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateY());
|
||||
|
||||
// Only two layers were added, so that should be the end.
|
||||
REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint));
|
||||
}
|
||||
|
||||
static void test_backToFront(skiatest::Reporter* reporter) {
|
||||
SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper));
|
||||
SkLayerDrawLooper::LayerInfo layerInfo;
|
||||
|
||||
// Add the back layer, with the defaults.
|
||||
(void)looper->addLayerOnTop(layerInfo);
|
||||
|
||||
// Add the front layer, with some layer info set.
|
||||
layerInfo.fOffset.set(SkFloatToScalar(10.0f), SkFloatToScalar(20.0f));
|
||||
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
|
||||
SkPaint* layerPaint = looper->addLayerOnTop(layerInfo);
|
||||
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
||||
|
||||
FakeDevice device;
|
||||
SkCanvas canvas(&device);
|
||||
SkPaint paint;
|
||||
looper->init(&canvas);
|
||||
|
||||
// The back layer should come first.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateY());
|
||||
paint.reset();
|
||||
|
||||
// Then the front layer.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(10.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(20.0f) == device.fLastMatrix.getTranslateY());
|
||||
|
||||
// Only two layers were added, so that should be the end.
|
||||
REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint));
|
||||
}
|
||||
|
||||
static void test_mixed(skiatest::Reporter* reporter) {
|
||||
SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper));
|
||||
SkLayerDrawLooper::LayerInfo layerInfo;
|
||||
|
||||
// Add the back layer, with the defaults.
|
||||
(void)looper->addLayer(layerInfo);
|
||||
|
||||
// Add the front layer, with some layer info set.
|
||||
layerInfo.fOffset.set(SkFloatToScalar(10.0f), SkFloatToScalar(20.0f));
|
||||
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
|
||||
SkPaint* layerPaint = looper->addLayerOnTop(layerInfo);
|
||||
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
||||
|
||||
FakeDevice device;
|
||||
SkCanvas canvas(&device);
|
||||
SkPaint paint;
|
||||
looper->init(&canvas);
|
||||
|
||||
// The back layer should come first.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(0.0f) == device.fLastMatrix.getTranslateY());
|
||||
paint.reset();
|
||||
|
||||
// Then the front layer.
|
||||
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
||||
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
|
||||
canvas.drawRect(SkRect::MakeWH(SkFloatToScalar(50.0f), SkFloatToScalar(50.0f)), paint);
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(10.0f) == device.fLastMatrix.getTranslateX());
|
||||
REPORTER_ASSERT(reporter, SkFloatToScalar(20.0f) == device.fLastMatrix.getTranslateY());
|
||||
|
||||
// Only two layers were added, so that should be the end.
|
||||
REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint));
|
||||
}
|
||||
|
||||
static void TestLayerDrawLooper(skiatest::Reporter* reporter) {
|
||||
test_frontToBack(reporter);
|
||||
test_backToFront(reporter);
|
||||
test_mixed(reporter);
|
||||
}
|
||||
|
||||
#include "TestClassDef.h"
|
||||
DEFINE_TESTCLASS("LayerDrawLooper", TestLayerDrawLooperClass, TestLayerDrawLooper)
|
Loading…
Reference in New Issue
Block a user