1 /*
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 
8 #include "SkArenaAlloc.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkColorSpaceXformer.h"
12 #include "SkDrawLooper.h"
13 #include "SkLightingImageFilter.h"
14 #include "SkPoint3.h"
15 #include "SkTypes.h"
16 #include "Test.h"
17 
18 /*
19  *  Subclass of looper that just draws once, with an offset in X.
20  */
21 class TestLooper : public SkDrawLooper {
22 public:
23 
makeContext(SkCanvas *,SkArenaAlloc * alloc) const24     SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
25         return alloc->make<TestDrawLooperContext>();
26     }
27 
onMakeColorSpace(SkColorSpaceXformer *) const28     sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override {
29         return nullptr;
30     }
31 
32 private:
33     SK_FLATTENABLE_HOOKS(TestLooper)
34 
35     class TestDrawLooperContext : public SkDrawLooper::Context {
36     public:
TestDrawLooperContext()37         TestDrawLooperContext() : fOnce(true) {}
~TestDrawLooperContext()38         ~TestDrawLooperContext() override {}
39 
next(SkCanvas * canvas,SkPaint *)40         bool next(SkCanvas* canvas, SkPaint*) override {
41             if (fOnce) {
42                 fOnce = false;
43                 canvas->translate(SkIntToScalar(10), 0);
44                 return true;
45             }
46             return false;
47         }
48 
49     private:
50         bool fOnce;
51     };
52 };
53 
CreateProc(SkReadBuffer &)54 sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
55 
test_drawBitmap(skiatest::Reporter * reporter)56 static void test_drawBitmap(skiatest::Reporter* reporter) {
57     SkBitmap src;
58     src.allocN32Pixels(10, 10);
59     src.eraseColor(SK_ColorWHITE);
60 
61     SkBitmap dst;
62     dst.allocN32Pixels(10, 10);
63     dst.eraseColor(SK_ColorTRANSPARENT);
64 
65     SkCanvas canvas(dst);
66     SkPaint  paint;
67 
68     // we are initially transparent
69     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
70 
71     // we see the bitmap drawn
72     canvas.drawBitmap(src, 0, 0, &paint);
73     REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
74 
75     // reverify we are clear again
76     dst.eraseColor(SK_ColorTRANSPARENT);
77     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
78 
79     // if the bitmap is clipped out, we don't draw it
80     canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
81     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
82 
83     // now install our looper, which will draw, since it internally translates
84     // to the left. The test is to ensure that canvas' quickReject machinary
85     // allows us through, even though sans-looper we would look like we should
86     // be clipped out.
87     paint.setLooper(sk_make_sp<TestLooper>());
88     canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
89     REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
90 }
91 
test_layers(skiatest::Reporter * reporter)92 static void test_layers(skiatest::Reporter* reporter) {
93     SkCanvas canvas(100, 100);
94 
95     SkRect r = SkRect::MakeWH(10, 10);
96     REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
97 
98     r.offset(300, 300);
99     REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
100 
101     // Test that saveLayer updates quickReject
102     SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
103     canvas.saveLayer(&bounds, nullptr);
104     REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
105     REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
106 }
107 
test_quick_reject(skiatest::Reporter * reporter)108 static void test_quick_reject(skiatest::Reporter* reporter) {
109     SkCanvas canvas(100, 100);
110     SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
111     SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
112     SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
113     SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
114     SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
115     SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
116     SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
117     SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
118     SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
119     SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
120     SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
121     REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
122     REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
123     REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
124     REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
125     REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
126     REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
127     REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
128     REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
129     REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
130     REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
131     REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
132 
133     SkMatrix m = SkMatrix::MakeScale(2.0f);
134     m.setTranslateX(10.0f);
135     m.setTranslateY(10.0f);
136     canvas.setMatrix(m);
137     SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
138     SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
139     SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
140     REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
141     REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
142     REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
143 }
144 
DEF_TEST(QuickReject,reporter)145 DEF_TEST(QuickReject, reporter) {
146     test_drawBitmap(reporter);
147     test_layers(reporter);
148     test_quick_reject(reporter);
149 }
150 
151 // Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
152 // It is possible to set a new matrix on the canvas without calling setMatrix().  This tests
153 // that code path.
DEF_TEST(QuickReject_MatrixState,reporter)154 DEF_TEST(QuickReject_MatrixState, reporter) {
155     SkCanvas canvas(100, 100);
156 
157     SkMatrix matrix;
158     matrix.setRotate(45.0f);
159     canvas.setMatrix(matrix);
160 
161     SkPaint paint;
162     sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
163             SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
164     REPORTER_ASSERT(reporter, filter);
165     paint.setImageFilter(filter);
166     SkCanvas::SaveLayerRec rec;
167     rec.fPaint = &paint;
168     canvas.saveLayer(rec);
169 
170     // quickReject() will assert if the matrix is out of sync.
171     canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
172 }
173 
174 #include "SkLayerDrawLooper.h"
175 #include "SkSurface.h"
DEF_TEST(looper_nothingtodraw,reporter)176 DEF_TEST(looper_nothingtodraw, reporter) {
177     auto surf = SkSurface::MakeRasterN32Premul(20, 20);
178 
179     SkPaint paint;
180     paint.setColor(0);
181     REPORTER_ASSERT(reporter, paint.nothingToDraw());
182 
183     SkLayerDrawLooper::Builder builder;
184     builder.addLayer();
185     paint.setDrawLooper(builder.detach());
186     // the presence of the looper fools this predicate, so we think it might draw
187     REPORTER_ASSERT(reporter, !paint.nothingToDraw());
188 
189     // Before fixing, this would assert in ~AutoDrawLooper() in SkCanvas.cpp as it checked for
190     // a balance in the save/restore count after handling the looper. Before the fix, this
191     // code would call nothingToDraw() and since it now clears the looper, that predicate will
192     // return true, aborting the sequence prematurely, and not finishing the iterator on the looper
193     // which handles the final "restore". This was a bug -- we *must* call the looper's iterator
194     // until it returns done to keep the canvas balanced. The fix was to remove this early-exit
195     // in the autodrawlooper. Now this call won't assert.
196     // See https://skia-review.googlesource.com/c/skia/+/121220
197     surf->getCanvas()->drawRect({1, 1, 10, 10}, paint);
198 }
199