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 "SkCanvas.h"
10 #include "SkColorSpaceXformer.h"
11 #include "SkDrawLooper.h"
12 #include "SkLightingImageFilter.h"
13 #include "SkTypes.h"
14 #include "Test.h"
15
16 /*
17 * Subclass of looper that just draws once, with an offset in X.
18 */
19 class TestLooper : public SkDrawLooper {
20 public:
21
makeContext(SkCanvas *,SkArenaAlloc * alloc) const22 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
23 return alloc->make<TestDrawLooperContext>();
24 }
25
onMakeColorSpace(SkColorSpaceXformer *) const26 sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override {
27 return nullptr;
28 }
29
30 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const31 void toString(SkString* str) const override {
32 str->append("TestLooper:");
33 }
34 #endif
35
36 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper)
37
38 private:
39 class TestDrawLooperContext : public SkDrawLooper::Context {
40 public:
TestDrawLooperContext()41 TestDrawLooperContext() : fOnce(true) {}
~TestDrawLooperContext()42 ~TestDrawLooperContext() override {}
43
next(SkCanvas * canvas,SkPaint *)44 bool next(SkCanvas* canvas, SkPaint*) override {
45 if (fOnce) {
46 fOnce = false;
47 canvas->translate(SkIntToScalar(10), 0);
48 return true;
49 }
50 return false;
51 }
52
53 private:
54 bool fOnce;
55 };
56 };
57
CreateProc(SkReadBuffer &)58 sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
59
test_drawBitmap(skiatest::Reporter * reporter)60 static void test_drawBitmap(skiatest::Reporter* reporter) {
61 SkBitmap src;
62 src.allocN32Pixels(10, 10);
63 src.eraseColor(SK_ColorWHITE);
64
65 SkBitmap dst;
66 dst.allocN32Pixels(10, 10);
67 dst.eraseColor(SK_ColorTRANSPARENT);
68
69 SkCanvas canvas(dst);
70 SkPaint paint;
71
72 // we are initially transparent
73 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
74
75 // we see the bitmap drawn
76 canvas.drawBitmap(src, 0, 0, &paint);
77 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
78
79 // reverify we are clear again
80 dst.eraseColor(SK_ColorTRANSPARENT);
81 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
82
83 // if the bitmap is clipped out, we don't draw it
84 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
85 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
86
87 // now install our looper, which will draw, since it internally translates
88 // to the left. The test is to ensure that canvas' quickReject machinary
89 // allows us through, even though sans-looper we would look like we should
90 // be clipped out.
91 paint.setLooper(sk_make_sp<TestLooper>());
92 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
93 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
94 }
95
test_layers(skiatest::Reporter * reporter)96 static void test_layers(skiatest::Reporter* reporter) {
97 SkCanvas canvas(100, 100);
98
99 SkRect r = SkRect::MakeWH(10, 10);
100 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
101
102 r.offset(300, 300);
103 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
104
105 // Test that saveLayer updates quickReject
106 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
107 canvas.saveLayer(&bounds, nullptr);
108 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
109 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
110 }
111
test_quick_reject(skiatest::Reporter * reporter)112 static void test_quick_reject(skiatest::Reporter* reporter) {
113 SkCanvas canvas(100, 100);
114 SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
115 SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
116 SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
117 SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
118 SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
119 SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
120 SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
121 SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
122 SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
123 SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
124 SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
125 REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
126 REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
127 REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
128 REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
129 REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
130 REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
131 REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
132 REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
133 REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
134 REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
135 REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
136
137 SkMatrix m = SkMatrix::MakeScale(2.0f);
138 m.setTranslateX(10.0f);
139 m.setTranslateY(10.0f);
140 canvas.setMatrix(m);
141 SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
142 SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
143 SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
144 REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
145 REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
146 REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
147 }
148
DEF_TEST(QuickReject,reporter)149 DEF_TEST(QuickReject, reporter) {
150 test_drawBitmap(reporter);
151 test_layers(reporter);
152 test_quick_reject(reporter);
153 }
154
155 // Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
156 // It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
157 // that code path.
DEF_TEST(QuickReject_MatrixState,reporter)158 DEF_TEST(QuickReject_MatrixState, reporter) {
159 SkCanvas canvas(100, 100);
160
161 SkMatrix matrix;
162 matrix.setRotate(45.0f);
163 canvas.setMatrix(matrix);
164
165 SkPaint paint;
166 sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
167 SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
168 REPORTER_ASSERT(reporter, filter);
169 paint.setImageFilter(filter);
170 SkCanvas::SaveLayerRec rec;
171 rec.fPaint = &paint;
172 canvas.saveLayer(rec);
173
174 // quickReject() will assert if the matrix is out of sync.
175 canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
176 }
177