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"
|
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"
|
2014-03-12 09:42:01 +00:00
|
|
|
#include "SkSmallAllocator.h"
|
2013-05-22 12:35:50 +00:00
|
|
|
#include "SkXfermode.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;
|
|
|
|
}
|
|
|
|
|
2013-08-29 11:54:56 +00:00
|
|
|
class FakeDevice : public SkBitmapDevice {
|
2013-05-22 12:35:50 +00:00
|
|
|
public:
|
2014-02-16 00:59:25 +00:00
|
|
|
FakeDevice() : SkBitmapDevice(make_bm(100, 100)) { }
|
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;
|
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);
|
2013-05-22 12:35:50 +00:00
|
|
|
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
|
|
|
|
FakeDevice device;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
|
|
|
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
|
|
|
|
SkDrawLooper::Context* context = 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));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
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);
|
2013-05-22 12:35:50 +00:00
|
|
|
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
|
|
|
|
FakeDevice device;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
|
|
|
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
|
|
|
|
SkDrawLooper::Context* context = 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));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
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);
|
2013-05-22 12:35:50 +00:00
|
|
|
layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
|
|
|
|
FakeDevice device;
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
SkPaint paint;
|
2014-02-14 10:06:42 +00:00
|
|
|
SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
|
2014-03-12 09:42:01 +00:00
|
|
|
SkSmallAllocator<1, 32> allocator;
|
|
|
|
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
|
|
|
|
SkDrawLooper::Context* context = 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));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
|
2013-05-22 12:35:50 +00:00
|
|
|
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.
|
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);
|
|
|
|
}
|