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 "SkBitmap.h"
|
2017-01-13 22:43:16 +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"
|
2014-03-12 09:42:01 +00:00
|
|
|
#include "SkSmallAllocator.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "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:
|
2015-06-22 16:46:59 +00:00
|
|
|
FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry)) {
|
|
|
|
}
|
2013-05-22 12:35:50 +00:00
|
|
|
|
2015-06-22 16:46:59 +00:00
|
|
|
void drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) override {
|
2013-05-22 12:35:50 +00:00
|
|
|
fLastMatrix = *draw.fMatrix;
|
2014-04-13 19:09:42 +00:00
|
|
|
this->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) {
|
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;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
auto looper(looperBuilder.detach());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
2016-11-15 14:26:56 +00:00
|
|
|
SkDrawLooper::Context* context = allocator.createWithIniter(
|
|
|
|
looper->contextSize(),
|
|
|
|
[&](void* buffer) {
|
|
|
|
return looper->createContext(&canvas, buffer);
|
|
|
|
});
|
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;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
auto looper(looperBuilder.detach());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
2016-11-15 14:26:56 +00:00
|
|
|
SkDrawLooper::Context* context = allocator.createWithIniter(
|
|
|
|
looper->contextSize(),
|
|
|
|
[&](void* buffer) {
|
|
|
|
return looper->createContext(&canvas, buffer);
|
|
|
|
});
|
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;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2016-03-21 20:25:16 +00:00
|
|
|
sk_sp<SkDrawLooper> looper(looperBuilder.detach());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
2016-11-15 14:26:56 +00:00
|
|
|
SkDrawLooper::Context* context = allocator.createWithIniter(
|
|
|
|
looper->contextSize(),
|
|
|
|
[&](void* buffer) {
|
|
|
|
return looper->createContext(&canvas, buffer);
|
|
|
|
});
|
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);
|
|
|
|
}
|