blob: ffa012ba3517a53418bb1e38b8632f9ad1fc1e81 [file] [log] [blame]
reed@google.com3d608122011-11-21 15:16:16 +00001/*
2 * Copyright 2011 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 */
7
Herb Derby73fe7b02017-02-08 15:12:19 -05008#include "SkArenaAlloc.h"
Florin Malita39e08552017-07-06 14:16:18 -04009#include "SkBitmap.h"
reed@google.com3d608122011-11-21 15:16:16 +000010#include "SkCanvas.h"
Matt Sarettcdc651d2017-03-30 12:41:48 -040011#include "SkColorSpaceXformer.h"
reed@google.com3d608122011-11-21 15:16:16 +000012#include "SkDrawLooper.h"
Cary Clark4dc5a452018-05-21 11:56:57 -040013#include "SkFlattenablePriv.h"
msarett9da5a5a2016-08-19 08:38:36 -070014#include "SkLightingImageFilter.h"
Mike Reed54518ac2017-07-22 08:29:48 -040015#include "SkPoint3.h"
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000016#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
reed@google.com3d608122011-11-21 15:16:16 +000018
19/*
20 * Subclass of looper that just draws once, with an offset in X.
21 */
22class TestLooper : public SkDrawLooper {
23public:
reed@google.com3d608122011-11-21 15:16:16 +000024
Herb Derby73fe7b02017-02-08 15:12:19 -050025 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
26 return alloc->make<TestDrawLooperContext>();
reed@google.com3d608122011-11-21 15:16:16 +000027 }
28
Matt Sarettcdc651d2017-03-30 12:41:48 -040029 sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override {
30 return nullptr;
31 }
32
Mike Kleinfc6c37b2016-09-27 09:34:10 -040033 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper)
mtklein7e44bb12015-01-07 09:06:08 -080034
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000035private:
36 class TestDrawLooperContext : public SkDrawLooper::Context {
37 public:
38 TestDrawLooperContext() : fOnce(true) {}
Brian Salomond3b65972017-03-22 12:05:03 -040039 ~TestDrawLooperContext() override {}
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 bool next(SkCanvas* canvas, SkPaint*) override {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000042 if (fOnce) {
43 fOnce = false;
44 canvas->translate(SkIntToScalar(10), 0);
45 return true;
46 }
47 return false;
48 }
Matt Sarettcdc651d2017-03-30 12:41:48 -040049
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000050 private:
51 bool fOnce;
52 };
reed@google.com3d608122011-11-21 15:16:16 +000053};
54
reed60c9b582016-04-03 09:11:13 -070055sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
mtklein7e44bb12015-01-07 09:06:08 -080056
reed@google.com3d608122011-11-21 15:16:16 +000057static void test_drawBitmap(skiatest::Reporter* reporter) {
58 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000059 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000060 src.eraseColor(SK_ColorWHITE);
61
62 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000063 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000064 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000065
66 SkCanvas canvas(dst);
67 SkPaint paint;
68
69 // we are initially transparent
70 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
71
72 // we see the bitmap drawn
73 canvas.drawBitmap(src, 0, 0, &paint);
74 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
75
76 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000077 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000078 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
79
80 // if the bitmap is clipped out, we don't draw it
81 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
82 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
83
84 // now install our looper, which will draw, since it internally translates
85 // to the left. The test is to ensure that canvas' quickReject machinary
86 // allows us through, even though sans-looper we would look like we should
87 // be clipped out.
reed7b380d02016-03-21 13:25:16 -070088 paint.setLooper(sk_make_sp<TestLooper>());
reed@google.com3d608122011-11-21 15:16:16 +000089 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
90 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
91}
92
reed9b3aa542015-03-11 08:47:12 -070093static void test_layers(skiatest::Reporter* reporter) {
94 SkCanvas canvas(100, 100);
95
96 SkRect r = SkRect::MakeWH(10, 10);
97 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
98
99 r.offset(300, 300);
100 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
101
102 // Test that saveLayer updates quickReject
103 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
halcanary96fcdcc2015-08-27 07:41:13 -0700104 canvas.saveLayer(&bounds, nullptr);
reed9b3aa542015-03-11 08:47:12 -0700105 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
106 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
107}
108
msarettfbfa2582016-08-12 08:29:08 -0700109static void test_quick_reject(skiatest::Reporter* reporter) {
110 SkCanvas canvas(100, 100);
111 SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
112 SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
113 SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
114 SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
115 SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
116 SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
117 SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
118 SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
119 SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
120 SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
121 SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
122 REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
123 REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
124 REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
125 REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
126 REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
127 REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
128 REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
129 REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
130 REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
131 REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
132 REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
133
134 SkMatrix m = SkMatrix::MakeScale(2.0f);
135 m.setTranslateX(10.0f);
136 m.setTranslateY(10.0f);
137 canvas.setMatrix(m);
138 SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
139 SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
140 SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
141 REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
142 REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
143 REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
144}
145
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000146DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +0000147 test_drawBitmap(reporter);
reed9b3aa542015-03-11 08:47:12 -0700148 test_layers(reporter);
msarettfbfa2582016-08-12 08:29:08 -0700149 test_quick_reject(reporter);
reed@google.com3d608122011-11-21 15:16:16 +0000150}
msarett9da5a5a2016-08-19 08:38:36 -0700151
152// Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
153// It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
154// that code path.
155DEF_TEST(QuickReject_MatrixState, reporter) {
156 SkCanvas canvas(100, 100);
157
158 SkMatrix matrix;
159 matrix.setRotate(45.0f);
160 canvas.setMatrix(matrix);
161
162 SkPaint paint;
163 sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
164 SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
165 REPORTER_ASSERT(reporter, filter);
166 paint.setImageFilter(filter);
167 SkCanvas::SaveLayerRec rec;
168 rec.fPaint = &paint;
169 canvas.saveLayer(rec);
170
171 // quickReject() will assert if the matrix is out of sync.
172 canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
173}
Mike Reed59af19f2018-04-12 17:26:40 -0400174
175#include "SkLayerDrawLooper.h"
176#include "SkSurface.h"
177DEF_TEST(looper_nothingtodraw, reporter) {
178 auto surf = SkSurface::MakeRasterN32Premul(20, 20);
179
180 SkPaint paint;
181 paint.setColor(0);
182 REPORTER_ASSERT(reporter, paint.nothingToDraw());
183
184 SkLayerDrawLooper::Builder builder;
185 builder.addLayer();
186 paint.setDrawLooper(builder.detach());
187 // the presence of the looper fools this predicate, so we think it might draw
188 REPORTER_ASSERT(reporter, !paint.nothingToDraw());
189
190 // Before fixing, this would assert in ~AutoDrawLooper() in SkCanvas.cpp as it checked for
191 // a balance in the save/restore count after handling the looper. Before the fix, this
192 // code would call nothingToDraw() and since it now clears the looper, that predicate will
193 // return true, aborting the sequence prematurely, and not finishing the iterator on the looper
194 // which handles the final "restore". This was a bug -- we *must* call the looper's iterator
195 // until it returns done to keep the canvas balanced. The fix was to remove this early-exit
196 // in the autodrawlooper. Now this call won't assert.
197 // See https://skia-review.googlesource.com/c/skia/+/121220
198 surf->getCanvas()->drawRect({1, 1, 10, 10}, paint);
199}