blob: 6426538c64b809bae9c9f3f17275cb9a5c6093f0 [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"
msarett9da5a5a2016-08-19 08:38:36 -070013#include "SkLightingImageFilter.h"
Mike Reed54518ac2017-07-22 08:29:48 -040014#include "SkPoint3.h"
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000015#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "Test.h"
reed@google.com3d608122011-11-21 15:16:16 +000017
18/*
19 * Subclass of looper that just draws once, with an offset in X.
20 */
21class TestLooper : public SkDrawLooper {
22public:
reed@google.com3d608122011-11-21 15:16:16 +000023
Herb Derby73fe7b02017-02-08 15:12:19 -050024 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
25 return alloc->make<TestDrawLooperContext>();
reed@google.com3d608122011-11-21 15:16:16 +000026 }
27
Matt Sarettcdc651d2017-03-30 12:41:48 -040028 sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override {
29 return nullptr;
30 }
31
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000032#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -070033 void toString(SkString* str) const override {
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000034 str->append("TestLooper:");
35 }
36#endif
37
Mike Kleinfc6c37b2016-09-27 09:34:10 -040038 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper)
mtklein7e44bb12015-01-07 09:06:08 -080039
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000040private:
41 class TestDrawLooperContext : public SkDrawLooper::Context {
42 public:
43 TestDrawLooperContext() : fOnce(true) {}
Brian Salomond3b65972017-03-22 12:05:03 -040044 ~TestDrawLooperContext() override {}
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000045
mtklein36352bf2015-03-25 18:17:31 -070046 bool next(SkCanvas* canvas, SkPaint*) override {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000047 if (fOnce) {
48 fOnce = false;
49 canvas->translate(SkIntToScalar(10), 0);
50 return true;
51 }
52 return false;
53 }
Matt Sarettcdc651d2017-03-30 12:41:48 -040054
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000055 private:
56 bool fOnce;
57 };
reed@google.com3d608122011-11-21 15:16:16 +000058};
59
reed60c9b582016-04-03 09:11:13 -070060sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
mtklein7e44bb12015-01-07 09:06:08 -080061
reed@google.com3d608122011-11-21 15:16:16 +000062static void test_drawBitmap(skiatest::Reporter* reporter) {
63 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000064 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000065 src.eraseColor(SK_ColorWHITE);
66
67 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000068 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000069 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000070
71 SkCanvas canvas(dst);
72 SkPaint paint;
73
74 // we are initially transparent
75 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
76
77 // we see the bitmap drawn
78 canvas.drawBitmap(src, 0, 0, &paint);
79 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
80
81 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000082 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000083 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
84
85 // if the bitmap is clipped out, we don't draw it
86 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
87 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
88
89 // now install our looper, which will draw, since it internally translates
90 // to the left. The test is to ensure that canvas' quickReject machinary
91 // allows us through, even though sans-looper we would look like we should
92 // be clipped out.
reed7b380d02016-03-21 13:25:16 -070093 paint.setLooper(sk_make_sp<TestLooper>());
reed@google.com3d608122011-11-21 15:16:16 +000094 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
95 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
96}
97
reed9b3aa542015-03-11 08:47:12 -070098static void test_layers(skiatest::Reporter* reporter) {
99 SkCanvas canvas(100, 100);
100
101 SkRect r = SkRect::MakeWH(10, 10);
102 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
103
104 r.offset(300, 300);
105 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
106
107 // Test that saveLayer updates quickReject
108 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 canvas.saveLayer(&bounds, nullptr);
reed9b3aa542015-03-11 08:47:12 -0700110 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
111 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
112}
113
msarettfbfa2582016-08-12 08:29:08 -0700114static void test_quick_reject(skiatest::Reporter* reporter) {
115 SkCanvas canvas(100, 100);
116 SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
117 SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
118 SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
119 SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
120 SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
121 SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
122 SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
123 SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
124 SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
125 SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
126 SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
127 REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
128 REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
129 REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
130 REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
131 REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
132 REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
133 REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
134 REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
135 REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
136 REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
137 REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
138
139 SkMatrix m = SkMatrix::MakeScale(2.0f);
140 m.setTranslateX(10.0f);
141 m.setTranslateY(10.0f);
142 canvas.setMatrix(m);
143 SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
144 SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
145 SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
146 REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
147 REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
148 REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
149}
150
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000151DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +0000152 test_drawBitmap(reporter);
reed9b3aa542015-03-11 08:47:12 -0700153 test_layers(reporter);
msarettfbfa2582016-08-12 08:29:08 -0700154 test_quick_reject(reporter);
reed@google.com3d608122011-11-21 15:16:16 +0000155}
msarett9da5a5a2016-08-19 08:38:36 -0700156
157// Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
158// It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
159// that code path.
160DEF_TEST(QuickReject_MatrixState, reporter) {
161 SkCanvas canvas(100, 100);
162
163 SkMatrix matrix;
164 matrix.setRotate(45.0f);
165 canvas.setMatrix(matrix);
166
167 SkPaint paint;
168 sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
169 SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
170 REPORTER_ASSERT(reporter, filter);
171 paint.setImageFilter(filter);
172 SkCanvas::SaveLayerRec rec;
173 rec.fPaint = &paint;
174 canvas.saveLayer(rec);
175
176 // quickReject() will assert if the matrix is out of sync.
177 canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
178}