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
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/effects/SkLayerDrawLooper.h"
|
2019-05-17 20:29:34 +00:00
|
|
|
#include "src/core/SkArenaAlloc.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkBitmapDevice.h"
|
|
|
|
#include "src/core/SkDraw.h"
|
|
|
|
#include "tests/Test.h"
|
2013-05-22 12:35:50 +00:00
|
|
|
|
2014-02-16 00:59:25 +00:00
|
|
|
static SkBitmap make_bm(int w, int h) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(w, h);
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2015-06-22 16:46:59 +00:00
|
|
|
// TODO: can this be derived from SkBaseDevice?
|
2013-08-29 11:54:56 +00:00
|
|
|
class FakeDevice : public SkBitmapDevice {
|
2013-05-22 12:35:50 +00:00
|
|
|
public:
|
2018-04-25 17:04:05 +00:00
|
|
|
FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry),
|
|
|
|
nullptr, nullptr) {
|
2015-06-22 16:46:59 +00:00
|
|
|
}
|
2013-05-22 12:35:50 +00:00
|
|
|
|
2017-03-07 14:37:29 +00:00
|
|
|
void drawRect(const SkRect& r, const SkPaint& paint) override {
|
|
|
|
fLastMatrix = this->ctm();
|
|
|
|
this->INHERITED::drawRect(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) {
|
2014-02-14 10:06:42 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-05-22 12:35:50 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo layerInfo;
|
|
|
|
|
|
|
|
// Add the front layer, with the defaults.
|
2014-02-14 10:06:42 +00:00
|
|
|
(void)looperBuilder.addLayer(layerInfo);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkPaint* layerPaint = looperBuilder.addLayer(layerInfo);
|
2016-10-06 00:33:02 +00:00
|
|
|
layerPaint->setBlendMode(SkBlendMode::kSrc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
FakeDevice device;
|
2018-05-01 21:06:20 +00:00
|
|
|
SkCanvas canvas(sk_ref_sp(&device));
|
2013-05-22 12:35:50 +00:00
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
auto looper(looperBuilder.detach());
|
2017-02-08 20:12:19 +00:00
|
|
|
SkArenaAlloc alloc{48};
|
|
|
|
SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// The back layer should come first.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_backToFront(skiatest::Reporter* reporter) {
|
2014-02-14 10:06:42 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-05-22 12:35:50 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo layerInfo;
|
|
|
|
|
|
|
|
// Add the back layer, with the defaults.
|
2014-02-14 10:06:42 +00:00
|
|
|
(void)looperBuilder.addLayerOnTop(layerInfo);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
|
2016-10-06 00:33:02 +00:00
|
|
|
layerPaint->setBlendMode(SkBlendMode::kSrc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
FakeDevice device;
|
2018-05-01 21:06:20 +00:00
|
|
|
SkCanvas canvas(sk_ref_sp(&device));
|
2013-05-22 12:35:50 +00:00
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
auto looper(looperBuilder.detach());
|
2017-02-08 20:12:19 +00:00
|
|
|
SkArenaAlloc alloc{48};
|
|
|
|
SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// The back layer should come first.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_mixed(skiatest::Reporter* reporter) {
|
2014-02-14 10:06:42 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-05-22 12:35:50 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo layerInfo;
|
|
|
|
|
|
|
|
// Add the back layer, with the defaults.
|
2014-02-14 10:06:42 +00:00
|
|
|
(void)looperBuilder.addLayer(layerInfo);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
|
2016-10-06 00:33:02 +00:00
|
|
|
layerPaint->setBlendMode(SkBlendMode::kSrc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
FakeDevice device;
|
2018-05-01 21:06:20 +00:00
|
|
|
SkCanvas canvas(sk_ref_sp(&device));
|
2013-05-22 12:35:50 +00:00
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
sk_sp<SkDrawLooper> looper(looperBuilder.detach());
|
2017-02-08 20:12:19 +00:00
|
|
|
SkArenaAlloc alloc{48};
|
|
|
|
SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
|
2013-05-22 12:35:50 +00:00
|
|
|
|
|
|
|
// The back layer should come first.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2016-10-06 00:33:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|