reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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 Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 14 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tests/Test.h" |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Subclass of looper that just draws once, with an offset in X. |
| 19 | */ |
| 20 | class TestLooper : public SkDrawLooper { |
| 21 | public: |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 22 | |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 23 | SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override { |
| 24 | return alloc->make<TestDrawLooperContext>(); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 25 | } |
| 26 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 27 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 28 | SK_FLATTENABLE_HOOKS(TestLooper) |
| 29 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 30 | class TestDrawLooperContext : public SkDrawLooper::Context { |
| 31 | public: |
| 32 | TestDrawLooperContext() : fOnce(true) {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 33 | ~TestDrawLooperContext() override {} |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | bool next(SkCanvas* canvas, SkPaint*) override { |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 36 | if (fOnce) { |
| 37 | fOnce = false; |
| 38 | canvas->translate(SkIntToScalar(10), 0); |
| 39 | return true; |
| 40 | } |
| 41 | return false; |
| 42 | } |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 44 | private: |
| 45 | bool fOnce; |
| 46 | }; |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 49 | sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); } |
mtklein | 7e44bb1 | 2015-01-07 09:06:08 -0800 | [diff] [blame] | 50 | |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 51 | static void test_drawBitmap(skiatest::Reporter* reporter) { |
| 52 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 53 | src.allocN32Pixels(10, 10); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 54 | src.eraseColor(SK_ColorWHITE); |
| 55 | |
| 56 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 57 | dst.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 58 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 59 | |
| 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.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 71 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 72 | 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 | |
| 78 | // now install our looper, which will draw, since it internally translates |
| 79 | // to the left. The test is to ensure that canvas' quickReject machinary |
| 80 | // allows us through, even though sans-looper we would look like we should |
| 81 | // be clipped out. |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 82 | paint.setLooper(sk_make_sp<TestLooper>()); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 83 | canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint); |
| 84 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5)); |
| 85 | } |
| 86 | |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 87 | static void test_layers(skiatest::Reporter* reporter) { |
| 88 | SkCanvas canvas(100, 100); |
| 89 | |
| 90 | SkRect r = SkRect::MakeWH(10, 10); |
| 91 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r)); |
| 92 | |
| 93 | r.offset(300, 300); |
| 94 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r)); |
| 95 | |
| 96 | // Test that saveLayer updates quickReject |
| 97 | SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | canvas.saveLayer(&bounds, nullptr); |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10))); |
| 100 | REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60))); |
| 101 | } |
| 102 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 103 | static void test_quick_reject(skiatest::Reporter* reporter) { |
| 104 | SkCanvas canvas(100, 100); |
| 105 | SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f); |
| 106 | SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f); |
| 107 | SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f); |
| 108 | SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f); |
| 109 | SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f); |
| 110 | SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f); |
| 111 | SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f); |
| 112 | SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f); |
| 113 | SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f); |
| 114 | SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f); |
| 115 | SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN); |
| 116 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r0)); |
| 117 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r1)); |
| 118 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r2)); |
| 119 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r3)); |
| 120 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r4)); |
| 121 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r5)); |
| 122 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r6)); |
| 123 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r7)); |
| 124 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r8)); |
| 125 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r9)); |
| 126 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r10)); |
| 127 | |
| 128 | SkMatrix m = SkMatrix::MakeScale(2.0f); |
| 129 | m.setTranslateX(10.0f); |
| 130 | m.setTranslateY(10.0f); |
| 131 | canvas.setMatrix(m); |
| 132 | SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f); |
| 133 | SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f); |
| 134 | SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f); |
| 135 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r11)); |
| 136 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r12)); |
| 137 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r13)); |
| 138 | } |
| 139 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 140 | DEF_TEST(QuickReject, reporter) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 141 | test_drawBitmap(reporter); |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 142 | test_layers(reporter); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 143 | test_quick_reject(reporter); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 144 | } |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 145 | |
| 146 | // Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas. |
| 147 | // It is possible to set a new matrix on the canvas without calling setMatrix(). This tests |
| 148 | // that code path. |
| 149 | DEF_TEST(QuickReject_MatrixState, reporter) { |
| 150 | SkCanvas canvas(100, 100); |
| 151 | |
| 152 | SkMatrix matrix; |
| 153 | matrix.setRotate(45.0f); |
| 154 | canvas.setMatrix(matrix); |
| 155 | |
| 156 | SkPaint paint; |
| 157 | sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse( |
| 158 | SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr); |
| 159 | REPORTER_ASSERT(reporter, filter); |
| 160 | paint.setImageFilter(filter); |
| 161 | SkCanvas::SaveLayerRec rec; |
| 162 | rec.fPaint = &paint; |
| 163 | canvas.saveLayer(rec); |
| 164 | |
| 165 | // quickReject() will assert if the matrix is out of sync. |
| 166 | canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f)); |
| 167 | } |
Mike Reed | 59af19f | 2018-04-12 17:26:40 -0400 | [diff] [blame] | 168 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 169 | #include "include/core/SkSurface.h" |
| 170 | #include "include/effects/SkLayerDrawLooper.h" |
Mike Reed | 59af19f | 2018-04-12 17:26:40 -0400 | [diff] [blame] | 171 | DEF_TEST(looper_nothingtodraw, reporter) { |
| 172 | auto surf = SkSurface::MakeRasterN32Premul(20, 20); |
| 173 | |
| 174 | SkPaint paint; |
| 175 | paint.setColor(0); |
| 176 | REPORTER_ASSERT(reporter, paint.nothingToDraw()); |
| 177 | |
| 178 | SkLayerDrawLooper::Builder builder; |
| 179 | builder.addLayer(); |
| 180 | paint.setDrawLooper(builder.detach()); |
| 181 | // the presence of the looper fools this predicate, so we think it might draw |
| 182 | REPORTER_ASSERT(reporter, !paint.nothingToDraw()); |
| 183 | |
| 184 | // Before fixing, this would assert in ~AutoDrawLooper() in SkCanvas.cpp as it checked for |
| 185 | // a balance in the save/restore count after handling the looper. Before the fix, this |
| 186 | // code would call nothingToDraw() and since it now clears the looper, that predicate will |
| 187 | // return true, aborting the sequence prematurely, and not finishing the iterator on the looper |
| 188 | // which handles the final "restore". This was a bug -- we *must* call the looper's iterator |
| 189 | // until it returns done to keep the canvas balanced. The fix was to remove this early-exit |
| 190 | // in the autodrawlooper. Now this call won't assert. |
| 191 | // See https://skia-review.googlesource.com/c/skia/+/121220 |
| 192 | surf->getCanvas()->drawRect({1, 1, 10, 10}, paint); |
| 193 | } |