blob: 1e973338a3d0cf88b3685b540052fff0397be148 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkDrawLooper.h"
11#include "include/core/SkPoint3.h"
12#include "include/core/SkTypes.h"
13#include "include/effects/SkLightingImageFilter.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040014#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
reed@google.com3d608122011-11-21 15:16:16 +000016
17/*
18 * Subclass of looper that just draws once, with an offset in X.
19 */
20class TestLooper : public SkDrawLooper {
21public:
reed@google.com3d608122011-11-21 15:16:16 +000022
Herb Derby73fe7b02017-02-08 15:12:19 -050023 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
24 return alloc->make<TestDrawLooperContext>();
reed@google.com3d608122011-11-21 15:16:16 +000025 }
26
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000027private:
Mike Klein4fee3232018-10-18 17:27:16 -040028 SK_FLATTENABLE_HOOKS(TestLooper)
29
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000030 class TestDrawLooperContext : public SkDrawLooper::Context {
31 public:
32 TestDrawLooperContext() : fOnce(true) {}
Brian Salomond3b65972017-03-22 12:05:03 -040033 ~TestDrawLooperContext() override {}
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000034
mtklein36352bf2015-03-25 18:17:31 -070035 bool next(SkCanvas* canvas, SkPaint*) override {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000036 if (fOnce) {
37 fOnce = false;
38 canvas->translate(SkIntToScalar(10), 0);
39 return true;
40 }
41 return false;
42 }
Matt Sarettcdc651d2017-03-30 12:41:48 -040043
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000044 private:
45 bool fOnce;
46 };
reed@google.com3d608122011-11-21 15:16:16 +000047};
48
reed60c9b582016-04-03 09:11:13 -070049sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
mtklein7e44bb12015-01-07 09:06:08 -080050
reed@google.com3d608122011-11-21 15:16:16 +000051static void test_drawBitmap(skiatest::Reporter* reporter) {
52 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000053 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000054 src.eraseColor(SK_ColorWHITE);
55
56 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000057 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000058 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000059
60 SkCanvas canvas(dst);
61 SkPaint paint;
62
63 // we are initially transparent
64 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
65
66 // we see the bitmap drawn
67 canvas.drawBitmap(src, 0, 0, &paint);
68 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
69
70 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000071 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000072 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
73
74 // if the bitmap is clipped out, we don't draw it
75 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
76 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
77
Mike Reedb7dad442019-07-22 14:51:10 -040078#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
reed@google.com3d608122011-11-21 15:16:16 +000079 // now install our looper, which will draw, since it internally translates
80 // to the left. The test is to ensure that canvas' quickReject machinary
81 // allows us through, even though sans-looper we would look like we should
82 // be clipped out.
reed7b380d02016-03-21 13:25:16 -070083 paint.setLooper(sk_make_sp<TestLooper>());
reed@google.com3d608122011-11-21 15:16:16 +000084 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
85 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
Mike Reedb7dad442019-07-22 14:51:10 -040086#endif
reed@google.com3d608122011-11-21 15:16:16 +000087}
88
reed9b3aa542015-03-11 08:47:12 -070089static void test_layers(skiatest::Reporter* reporter) {
90 SkCanvas canvas(100, 100);
91
92 SkRect r = SkRect::MakeWH(10, 10);
93 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
94
95 r.offset(300, 300);
96 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
97
98 // Test that saveLayer updates quickReject
99 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
halcanary96fcdcc2015-08-27 07:41:13 -0700100 canvas.saveLayer(&bounds, nullptr);
reed9b3aa542015-03-11 08:47:12 -0700101 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
102 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
103}
104
msarettfbfa2582016-08-12 08:29:08 -0700105static void test_quick_reject(skiatest::Reporter* reporter) {
106 SkCanvas canvas(100, 100);
107 SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
108 SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
109 SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
110 SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
111 SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
112 SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
113 SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
114 SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
115 SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
116 SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
117 SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
118 REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
119 REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
120 REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
121 REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
122 REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
123 REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
124 REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
125 REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
126 REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
127 REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
128 REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
129
130 SkMatrix m = SkMatrix::MakeScale(2.0f);
131 m.setTranslateX(10.0f);
132 m.setTranslateY(10.0f);
133 canvas.setMatrix(m);
134 SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
135 SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
136 SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
137 REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
138 REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
139 REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
140}
141
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000142DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +0000143 test_drawBitmap(reporter);
reed9b3aa542015-03-11 08:47:12 -0700144 test_layers(reporter);
msarettfbfa2582016-08-12 08:29:08 -0700145 test_quick_reject(reporter);
reed@google.com3d608122011-11-21 15:16:16 +0000146}
msarett9da5a5a2016-08-19 08:38:36 -0700147
148// Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
149// It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
150// that code path.
151DEF_TEST(QuickReject_MatrixState, reporter) {
152 SkCanvas canvas(100, 100);
153
154 SkMatrix matrix;
155 matrix.setRotate(45.0f);
156 canvas.setMatrix(matrix);
157
158 SkPaint paint;
159 sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
160 SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
161 REPORTER_ASSERT(reporter, filter);
162 paint.setImageFilter(filter);
163 SkCanvas::SaveLayerRec rec;
164 rec.fPaint = &paint;
165 canvas.saveLayer(rec);
166
167 // quickReject() will assert if the matrix is out of sync.
168 canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
169}
Mike Reed59af19f2018-04-12 17:26:40 -0400170
Mike Reedb7dad442019-07-22 14:51:10 -0400171#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500172#include "include/core/SkSurface.h"
173#include "include/effects/SkLayerDrawLooper.h"
Mike Reed59af19f2018-04-12 17:26:40 -0400174DEF_TEST(looper_nothingtodraw, reporter) {
175 auto surf = SkSurface::MakeRasterN32Premul(20, 20);
176
177 SkPaint paint;
178 paint.setColor(0);
179 REPORTER_ASSERT(reporter, paint.nothingToDraw());
180
181 SkLayerDrawLooper::Builder builder;
182 builder.addLayer();
183 paint.setDrawLooper(builder.detach());
184 // the presence of the looper fools this predicate, so we think it might draw
185 REPORTER_ASSERT(reporter, !paint.nothingToDraw());
186
187 // Before fixing, this would assert in ~AutoDrawLooper() in SkCanvas.cpp as it checked for
188 // a balance in the save/restore count after handling the looper. Before the fix, this
189 // code would call nothingToDraw() and since it now clears the looper, that predicate will
190 // return true, aborting the sequence prematurely, and not finishing the iterator on the looper
191 // which handles the final "restore". This was a bug -- we *must* call the looper's iterator
192 // until it returns done to keep the canvas balanced. The fix was to remove this early-exit
193 // in the autodrawlooper. Now this call won't assert.
194 // See https://skia-review.googlesource.com/c/skia/+/121220
195 surf->getCanvas()->drawRect({1, 1, 10, 10}, paint);
196}
Mike Reedb7dad442019-07-22 14:51:10 -0400197#endif