blob: f3705b77be2bb71878471519921bf7f3c3eac17c [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
Herb Derby73fe7b02017-02-08 15:12:19 -05008#include "SkArenaAlloc.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +00009#include "SkBitmap.h"
Mike Reed986480a2017-01-13 22:43:16 +000010#include "SkBitmapDevice.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000011#include "SkCanvas.h"
12#include "SkDraw.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000013#include "SkLayerDrawLooper.h"
14#include "SkMatrix.h"
15#include "SkPaint.h"
16#include "SkRect.h"
17#include "SkRefCnt.h"
18#include "SkScalar.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "Test.h"
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000020
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000021static SkBitmap make_bm(int w, int h) {
22 SkBitmap bm;
23 bm.allocN32Pixels(w, h);
24 return bm;
25}
26
robertphillips9a53fd72015-06-22 09:46:59 -070027// TODO: can this be derived from SkBaseDevice?
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000028class FakeDevice : public SkBitmapDevice {
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000029public:
robertphillips9a53fd72015-06-22 09:46:59 -070030 FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry)) {
31 }
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000032
Mike Reeda1361362017-03-07 09:37:29 -050033 void drawRect(const SkRect& r, const SkPaint& paint) override {
34 fLastMatrix = this->ctm();
35 this->INHERITED::drawRect(r, paint);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000036 }
37
38 SkMatrix fLastMatrix;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000039
40private:
41 typedef SkBitmapDevice INHERITED;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000042};
43
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000044static void test_frontToBack(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000045 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000046 SkLayerDrawLooper::LayerInfo layerInfo;
47
48 // Add the front layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000049 (void)looperBuilder.addLayer(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000050
51 // Add the back layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000053 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000054 SkPaint* layerPaint = looperBuilder.addLayer(layerInfo);
reed374772b2016-10-05 17:33:02 -070055 layerPaint->setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000056
57 FakeDevice device;
58 SkCanvas canvas(&device);
59 SkPaint paint;
reed7b380d02016-03-21 13:25:16 -070060 auto looper(looperBuilder.detach());
Herb Derby73fe7b02017-02-08 15:12:19 -050061 SkArenaAlloc alloc{48};
62 SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000063
64 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000065 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -070066 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000067 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
68 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
69 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000070 paint.reset();
71
72 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000073 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -070074 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000075 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
76 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
77 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000078
79 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000080 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000081}
82
83static void test_backToFront(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000084 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000085 SkLayerDrawLooper::LayerInfo layerInfo;
86
87 // Add the back layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000088 (void)looperBuilder.addLayerOnTop(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000089
90 // Add the front layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000091 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000092 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000093 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
reed374772b2016-10-05 17:33:02 -070094 layerPaint->setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000095
96 FakeDevice device;
97 SkCanvas canvas(&device);
98 SkPaint paint;
reed7b380d02016-03-21 13:25:16 -070099 auto looper(looperBuilder.detach());
Herb Derby73fe7b02017-02-08 15:12:19 -0500100 SkArenaAlloc alloc{48};
101 SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000102
103 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000104 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -0700105 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000106 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
107 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
108 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000109 paint.reset();
110
111 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000112 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -0700113 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000114 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
115 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
116 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000117
118 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000119 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000120}
121
122static void test_mixed(skiatest::Reporter* reporter) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000123 SkLayerDrawLooper::Builder looperBuilder;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000124 SkLayerDrawLooper::LayerInfo layerInfo;
125
126 // Add the back layer, with the defaults.
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000127 (void)looperBuilder.addLayer(layerInfo);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000128
129 // Add the front layer, with some layer info set.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000130 layerInfo.fOffset.set(10.0f, 20.0f);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000131 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000132 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
reed374772b2016-10-05 17:33:02 -0700133 layerPaint->setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000134
135 FakeDevice device;
136 SkCanvas canvas(&device);
137 SkPaint paint;
reed7b380d02016-03-21 13:25:16 -0700138 sk_sp<SkDrawLooper> looper(looperBuilder.detach());
Herb Derby73fe7b02017-02-08 15:12:19 -0500139 SkArenaAlloc alloc{48};
140 SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc);
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000141
142 // The back layer should come first.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000143 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -0700144 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000145 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
146 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
147 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000148 paint.reset();
149
150 // Then the front layer.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000151 REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
reed374772b2016-10-05 17:33:02 -0700152 REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000153 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
154 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
155 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000156
157 // Only two layers were added, so that should be the end.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000158 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000159}
160
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000161DEF_TEST(LayerDrawLooper, reporter) {
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000162 test_frontToBack(reporter);
163 test_backToFront(reporter);
164 test_mixed(reporter);
165}