blob: 391db7ce7f2606280b26a06612e9e1cbfbca6e01 [file] [log] [blame]
commit-bot@chromium.org8f838252013-05-22 12:35:50 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +00007
commit-bot@chromium.org8f838252013-05-22 12:35:50 +00008#include "SkBitmap.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00009#include "SkBitmapDevice.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000010#include "SkCanvas.h"
11#include "SkDraw.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000012#include "SkLayerDrawLooper.h"
13#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkRect.h"
16#include "SkRefCnt.h"
17#include "SkScalar.h"
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000018#include "SkSmallAllocator.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000019#include "SkXfermode.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000021
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000022static SkBitmap make_bm(int w, int h) {
23 SkBitmap bm;
24 bm.allocN32Pixels(w, h);
25 return bm;
26}
27
robertphillips9a53fd72015-06-22 09:46:59 -070028// TODO: can this be derived from SkBaseDevice?
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000029class FakeDevice : public SkBitmapDevice {
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000030public:
robertphillips9a53fd72015-06-22 09:46:59 -070031 FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry)) {
32 }
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000033
robertphillips9a53fd72015-06-22 09:46:59 -070034 void drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) override {
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000035 fLastMatrix = *draw.fMatrix;
robertphillips@google.com84b18c72014-04-13 19:09:42 +000036 this->INHERITED::drawRect(draw, r, paint);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000037 }
38
39 SkMatrix fLastMatrix;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000040
41private:
42 typedef SkBitmapDevice INHERITED;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000043};
44
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000045static void test_frontToBack(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000046 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000047 SkLayerDrawLooper::LayerInfo layerInfo;
48
49 // Add the front layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000050 (void)looperBuilder.addLayer(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000051
52 // Add the back layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000053 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000054 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000055 SkPaint* layerPaint = looperBuilder.addLayer(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000056 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
57
58 FakeDevice device;
59 SkCanvas canvas(&device);
60 SkPaint paint;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000061 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000062 SkSmallAllocator<1, 32> allocator;
63 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
64 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000065
66 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000067 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000068 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000069 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
70 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
71 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000072 paint.reset();
73
74 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000075 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000076 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000077 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
78 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
79 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000080
81 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000082 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000083}
84
85static void test_backToFront(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000086 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000087 SkLayerDrawLooper::LayerInfo layerInfo;
88
89 // Add the back layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000090 (void)looperBuilder.addLayerOnTop(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000091
92 // Add the front layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000093 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000094 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000095 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000096 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
97
98 FakeDevice device;
99 SkCanvas canvas(&device);
100 SkPaint paint;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000101 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000102 SkSmallAllocator<1, 32> allocator;
103 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
104 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000105
106 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000107 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000108 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000109 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
110 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
111 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000112 paint.reset();
113
114 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000115 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000116 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000117 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
118 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
119 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000120
121 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000122 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000123}
124
125static void test_mixed(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000126 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000127 SkLayerDrawLooper::LayerInfo layerInfo;
128
129 // Add the back layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000130 (void)looperBuilder.addLayer(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000131
132 // Add the front layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000133 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000134 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000135 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000136 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode);
137
138 FakeDevice device;
139 SkCanvas canvas(&device);
140 SkPaint paint;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000141 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper());
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000142 SkSmallAllocator<1, 32> allocator;
143 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
144 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000145
146 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000147 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000148 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000149 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
150 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
151 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000152 paint.reset();
153
154 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000155 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000156 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000157 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
158 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
159 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000160
161 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000162 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000163}
164
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000165DEF_TEST(LayerDrawLooper, reporter) {
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000166 test_frontToBack(reporter);
167 test_backToFront(reporter);
168 test_mixed(reporter);
169}