2013-05-22 12:35:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-10-12 17:25:17 +00:00
|
|
|
|
2013-05-22 12:35:50 +00:00
|
|
|
#include "Test.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "TestClassDef.h"
|
2013-05-22 12:35:50 +00:00
|
|
|
#include "SkBitmap.h"
|
2013-08-29 11:54:56 +00:00
|
|
|
#include "SkBitmapDevice.h"
|
2013-05-22 12:35:50 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDraw.h"
|
|
|
|
#include "SkLayerDrawLooper.h"
|
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
2013-08-29 11:54:56 +00:00
|
|
|
class FakeDevice : public SkBitmapDevice {
|
2013-05-22 12:35:50 +00:00
|
|
|
public:
|
2013-08-29 11:54:56 +00:00
|
|
|
FakeDevice() : SkBitmapDevice(SkBitmap::kARGB_8888_Config, 100, 100, false) { }
|
2013-05-22 12:35:50 +00:00
|
|
|
|
2013-07-24 07:01:12 +00:00
|
|
|
virtual void drawRect(const SkDraw& draw, const SkRect& r,
|
2013-07-23 11:28:45 +00:00
|
|
|
const SkPaint& paint) SK_OVERRIDE {
|
2013-05-22 12:35:50 +00:00
|
|
|
fLastMatrix = *draw.fMatrix;
|
2013-08-29 11:54:56 +00:00
|
|
|
INHERITED::drawRect(draw, r, paint);
|
2013-05-22 12:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix fLastMatrix;
|
2013-08-29 11:54:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkBitmapDevice INHERITED;
|
2013-05-22 12:35:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
2013-11-25 19:44:07 +00:00
|
|
|
layerInfo.fOffset.set(10.0f, 20.0f);
|
2013-05-22 12:35:50 +00:00
|
|
|
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));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
paint.reset();
|
|
|
|
|
|
|
|
// Then the front layer.
|
|
|
|
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
|
|
|
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// 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.
|
2013-11-25 19:44:07 +00:00
|
|
|
layerInfo.fOffset.set(10.0f, 20.0f);
|
2013-05-22 12:35:50 +00:00
|
|
|
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));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
paint.reset();
|
|
|
|
|
|
|
|
// Then the front layer.
|
|
|
|
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
|
|
|
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// 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.
|
2013-11-25 19:44:07 +00:00
|
|
|
layerInfo.fOffset.set(10.0f, 20.0f);
|
2013-05-22 12:35:50 +00:00
|
|
|
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));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
paint.reset();
|
|
|
|
|
|
|
|
// Then the front layer.
|
|
|
|
REPORTER_ASSERT(reporter, looper->next(&canvas, &paint));
|
|
|
|
REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
|
|
|
|
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// Only two layers were added, so that should be the end.
|
|
|
|
REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint));
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(LayerDrawLooper, reporter) {
|
2013-05-22 12:35:50 +00:00
|
|
|
test_frontToBack(reporter);
|
|
|
|
test_backToFront(reporter);
|
|
|
|
test_mixed(reporter);
|
|
|
|
}
|