reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 1 | /* |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 2 | * Copyright 2012 Google Inc. |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkClipOp.h" |
| 12 | #include "include/core/SkColor.h" |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 13 | #include "include/core/SkDocument.h" |
| 14 | #include "include/core/SkFlattenable.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImageFilter.h" |
| 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkMatrix.h" |
| 18 | #include "include/core/SkPaint.h" |
| 19 | #include "include/core/SkPath.h" |
| 20 | #include "include/core/SkPictureRecorder.h" |
| 21 | #include "include/core/SkPixmap.h" |
| 22 | #include "include/core/SkPoint.h" |
| 23 | #include "include/core/SkRect.h" |
| 24 | #include "include/core/SkRefCnt.h" |
| 25 | #include "include/core/SkRegion.h" |
| 26 | #include "include/core/SkScalar.h" |
| 27 | #include "include/core/SkShader.h" |
| 28 | #include "include/core/SkSize.h" |
| 29 | #include "include/core/SkStream.h" |
| 30 | #include "include/core/SkString.h" |
| 31 | #include "include/core/SkSurface.h" |
| 32 | #include "include/core/SkTypes.h" |
| 33 | #include "include/core/SkVertices.h" |
| 34 | #include "include/docs/SkPDFDocument.h" |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 35 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "include/private/SkMalloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "include/private/SkTemplates.h" |
| 38 | #include "include/utils/SkNWayCanvas.h" |
| 39 | #include "include/utils/SkPaintFilterCanvas.h" |
| 40 | #include "src/core/SkClipOpPriv.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 41 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 42 | #include "src/core/SkSpecialImage.h" |
| 43 | #include "src/utils/SkCanvasStack.h" |
| 44 | #include "tests/Test.h" |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 45 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 46 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 47 | #include "include/core/SkColorSpace.h" |
| 48 | #include "include/private/SkColorData.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | #include <memory> |
| 52 | #include <utility> |
| 53 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 54 | class SkReadBuffer; |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 55 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 56 | DEF_TEST(canvas_clipbounds, reporter) { |
| 57 | SkCanvas canvas(10, 10); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 58 | SkIRect irect, irect2; |
| 59 | SkRect rect, rect2; |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 60 | |
| 61 | irect = canvas.getDeviceClipBounds(); |
| 62 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10)); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2)); |
| 64 | REPORTER_ASSERT(reporter, irect == irect2); |
| 65 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 66 | // local bounds are always too big today -- can we trim them? |
| 67 | rect = canvas.getLocalClipBounds(); |
| 68 | REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10))); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2)); |
| 70 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 71 | |
| 72 | canvas.clipRect(SkRect::MakeEmpty()); |
| 73 | |
| 74 | irect = canvas.getDeviceClipBounds(); |
| 75 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2)); |
| 77 | REPORTER_ASSERT(reporter, irect == irect2); |
| 78 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 79 | rect = canvas.getLocalClipBounds(); |
| 80 | REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2)); |
| 82 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 83 | |
| 84 | // Test for wacky sizes that we (historically) have guarded against |
| 85 | { |
| 86 | SkCanvas c(-10, -20); |
| 87 | REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty()); |
Mike Reed | dc9f0db | 2017-03-10 13:54:16 -0500 | [diff] [blame] | 88 | |
| 89 | SkPictureRecorder().beginRecording({ 5, 5, 4, 4 }); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 90 | } |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 93 | // Will call proc with multiple styles of canvas (recording, raster, pdf) |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 94 | template <typename F> static void multi_canvas_driver(int w, int h, F proc) { |
| 95 | proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h))); |
| 96 | |
| 97 | SkNullWStream stream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 98 | if (auto doc = SkPDF::MakeDocument(&stream)) { |
Hal Canary | 4125b61 | 2018-03-20 14:17:00 -0400 | [diff] [blame] | 99 | proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h))); |
| 100 | } |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 101 | |
| 102 | proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas()); |
| 103 | } |
| 104 | |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 105 | const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 }; |
| 106 | |
| 107 | static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) { |
| 108 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR); |
| 109 | |
| 110 | const SkIRect restrictionR = { 2, 2, 8, 8 }; |
| 111 | canvas->androidFramework_setDeviceClipRestriction(restrictionR); |
| 112 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR); |
| 113 | |
| 114 | const SkIRect clipR = { 4, 4, 6, 6 }; |
| 115 | canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect); |
| 116 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR); |
| 117 | |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 118 | #ifdef SK_SUPPORT_DEPRECATED_CLIPOPS |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 119 | // now test that expanding clipops can't exceed the restriction |
| 120 | const SkClipOp expanders[] = { |
| 121 | SkClipOp::kUnion_deprecated, |
| 122 | SkClipOp::kXOR_deprecated, |
| 123 | SkClipOp::kReverseDifference_deprecated, |
| 124 | SkClipOp::kReplace_deprecated, |
| 125 | }; |
| 126 | |
| 127 | const SkRect expandR = { 0, 0, 5, 9 }; |
| 128 | SkASSERT(!SkRect::Make(restrictionR).contains(expandR)); |
| 129 | |
| 130 | for (SkClipOp op : expanders) { |
| 131 | canvas->save(); |
| 132 | canvas->clipRect(expandR, op); |
| 133 | REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds())); |
| 134 | canvas->restore(); |
| 135 | } |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 136 | #endif |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Clip restriction logic exists in the canvas itself, and in various kinds of devices. |
| 141 | * |
| 142 | * This test explicitly tries to exercise that variety: |
| 143 | * - picture : empty device but exercises canvas itself |
| 144 | * - pdf : uses SkClipStack in its device (as does SVG and GPU) |
| 145 | * - raster : uses SkRasterClip in its device |
| 146 | */ |
| 147 | DEF_TEST(canvas_clip_restriction, reporter) { |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 148 | multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(), |
| 149 | [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); }); |
| 150 | } |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 151 | |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 152 | DEF_TEST(canvas_empty_clip, reporter) { |
| 153 | multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) { |
| 154 | canvas->save(); |
| 155 | canvas->clipRect({0, 0, 20, 40 }); |
| 156 | REPORTER_ASSERT(reporter, !canvas->isClipEmpty()); |
| 157 | canvas->clipRect({30, 0, 50, 40 }); |
| 158 | REPORTER_ASSERT(reporter, canvas->isClipEmpty()); |
| 159 | }); |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 162 | DEF_TEST(CanvasNewRasterTest, reporter) { |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 163 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 164 | const size_t minRowBytes = info.minRowBytes(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 165 | const size_t size = info.computeByteSize(minRowBytes); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 166 | SkAutoTMalloc<SkPMColor> storage(size); |
| 167 | SkPMColor* baseAddr = storage.get(); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 168 | sk_bzero(baseAddr, size); |
| 169 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 170 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, canvas); |
| 172 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 173 | SkPixmap pmap; |
| 174 | const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, addr); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 176 | REPORTER_ASSERT(reporter, info == pmap.info()); |
| 177 | REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 178 | for (int y = 0; y < info.height(); ++y) { |
| 179 | for (int x = 0; x < info.width(); ++x) { |
| 180 | REPORTER_ASSERT(reporter, 0 == addr[x]); |
| 181 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 182 | addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 183 | } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 184 | |
Leon Scroggins III | 3eedc97 | 2020-01-22 10:44:00 -0500 | [diff] [blame] | 185 | // unaligned rowBytes |
| 186 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, |
| 187 | minRowBytes + 1)); |
| 188 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 189 | // now try a deliberately bad info |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 190 | info = info.makeWH(-1, info.height()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 192 | |
| 193 | // too big |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 194 | info = info.makeWH(1 << 30, 1 << 30); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
skia.committer@gmail.com | 0e53075 | 2014-02-28 03:02:05 +0000 | [diff] [blame] | 196 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 197 | // not a valid pixel type |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 198 | info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 199 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 200 | |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 201 | // We should not succeed with a zero-sized valid info |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 202 | info = SkImageInfo::MakeN32Premul(0, 0); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 203 | canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 204 | REPORTER_ASSERT(reporter, nullptr == canvas); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 207 | static SkPath make_path_from_rect(SkRect r) { |
| 208 | SkPath path; |
| 209 | path.addRect(r); |
| 210 | return path; |
| 211 | } |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 212 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 213 | static SkRegion make_region_from_irect(SkIRect r) { |
| 214 | SkRegion region; |
| 215 | region.setRect(r); |
| 216 | return region; |
| 217 | } |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 218 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 219 | static SkBitmap make_n32_bitmap(int w, int h, SkColor c = SK_ColorWHITE) { |
| 220 | SkBitmap bm; |
| 221 | bm.allocN32Pixels(w, h); |
| 222 | bm.eraseColor(c); |
| 223 | return bm; |
| 224 | } |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 225 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 226 | // Constants used by test steps |
| 227 | static constexpr SkRect kRect = {0, 0, 2, 1}; |
| 228 | static constexpr SkColor kColor = 0x01020304; |
| 229 | static constexpr int kWidth = 2; |
| 230 | static constexpr int kHeight = 2; |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 231 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 232 | using CanvasTest = void (*)(SkCanvas*, skiatest::Reporter*); |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 233 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 234 | static CanvasTest kCanvasTests[] = { |
| 235 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 236 | c->translate(SkIntToScalar(1), SkIntToScalar(2)); |
| 237 | }, |
| 238 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 239 | c->scale(SkIntToScalar(1), SkIntToScalar(2)); |
| 240 | }, |
| 241 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 242 | c->rotate(SkIntToScalar(1)); |
| 243 | }, |
| 244 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 245 | c->skew(SkIntToScalar(1), SkIntToScalar(2)); |
| 246 | }, |
| 247 | [](SkCanvas* c, skiatest::Reporter* r) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 248 | c->concat(SkMatrix::Scale(2, 3)); |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 249 | }, |
| 250 | [](SkCanvas* c, skiatest::Reporter* r) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 251 | c->setMatrix(SkMatrix::Scale(2, 3)); |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 252 | }, |
| 253 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 254 | c->clipRect(kRect); |
| 255 | }, |
| 256 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 257 | c->clipPath(make_path_from_rect(SkRect{0, 0, 2, 1})); |
| 258 | }, |
| 259 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 260 | c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1}), kReplace_SkClipOp); |
| 261 | }, |
| 262 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 263 | c->clear(kColor); |
| 264 | }, |
| 265 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 266 | int saveCount = c->getSaveCount(); |
| 267 | c->save(); |
| 268 | c->translate(SkIntToScalar(1), SkIntToScalar(2)); |
| 269 | c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1})); |
| 270 | c->restore(); |
| 271 | REPORTER_ASSERT(r, c->getSaveCount() == saveCount); |
| 272 | REPORTER_ASSERT(r, c->getTotalMatrix().isIdentity()); |
| 273 | //REPORTER_ASSERT(reporter, c->getTotalClip() != kTestRegion); |
| 274 | }, |
| 275 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 276 | int saveCount = c->getSaveCount(); |
| 277 | c->saveLayer(nullptr, nullptr); |
| 278 | c->restore(); |
| 279 | REPORTER_ASSERT(r, c->getSaveCount() == saveCount); |
| 280 | }, |
| 281 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 282 | int saveCount = c->getSaveCount(); |
| 283 | c->saveLayer(&kRect, nullptr); |
| 284 | c->restore(); |
| 285 | REPORTER_ASSERT(r, c->getSaveCount() == saveCount); |
| 286 | }, |
| 287 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 288 | int saveCount = c->getSaveCount(); |
| 289 | SkPaint p; |
| 290 | c->saveLayer(nullptr, &p); |
| 291 | c->restore(); |
| 292 | REPORTER_ASSERT(r, c->getSaveCount() == saveCount); |
| 293 | }, |
| 294 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 295 | // This test exercises a functionality in SkPicture that leads to the |
| 296 | // recording of restore offset placeholders. This test will trigger an |
| 297 | // assertion at playback time if the placeholders are not properly |
| 298 | // filled when the recording ends. |
| 299 | c->clipRect(kRect); |
| 300 | c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1})); |
| 301 | }, |
| 302 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 303 | // exercise fix for http://code.google.com/p/skia/issues/detail?id=560 |
| 304 | // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero') |
| 305 | SkPaint paint; |
| 306 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 307 | paint.setStyle(SkPaint::kStroke_Style); |
| 308 | SkPath path; |
| 309 | path.moveTo(SkPoint{ 0, 0 }); |
| 310 | path.lineTo(SkPoint{ 0, SK_ScalarNearlyZero }); |
| 311 | path.lineTo(SkPoint{ SkIntToScalar(1), 0 }); |
| 312 | path.lineTo(SkPoint{ SkIntToScalar(1), SK_ScalarNearlyZero/2 }); |
| 313 | // test nearly zero length path |
| 314 | c->drawPath(path, paint); |
| 315 | }, |
| 316 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 317 | SkPictureRecorder recorder; |
| 318 | SkCanvas* testCanvas = recorder.beginRecording( |
| 319 | SkIntToScalar(kWidth), SkIntToScalar(kHeight), nullptr, 0); |
| 320 | testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1)); |
| 321 | testCanvas->clipRect(kRect); |
| 322 | testCanvas->drawRect(kRect, SkPaint()); |
| 323 | c->drawPicture(recorder.finishRecordingAsPicture()); |
| 324 | }, |
| 325 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 326 | int baseSaveCount = c->getSaveCount(); |
| 327 | int n = c->save(); |
| 328 | REPORTER_ASSERT(r, baseSaveCount == n); |
| 329 | REPORTER_ASSERT(r, baseSaveCount + 1 == c->getSaveCount()); |
| 330 | c->save(); |
| 331 | c->save(); |
| 332 | REPORTER_ASSERT(r, baseSaveCount + 3 == c->getSaveCount()); |
| 333 | c->restoreToCount(baseSaveCount + 1); |
| 334 | REPORTER_ASSERT(r, baseSaveCount + 1 == c->getSaveCount()); |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 335 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 336 | // should this pin to 1, or be a no-op, or crash? |
| 337 | c->restoreToCount(0); |
| 338 | REPORTER_ASSERT(r, 1 == c->getSaveCount()); |
| 339 | }, |
| 340 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 341 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 342 | // test cases because the opaque paint can trigger an optimization |
| 343 | // that discards previously recorded commands. The challenge is to maintain |
| 344 | // correct clip and matrix stack state. |
| 345 | c->resetMatrix(); |
| 346 | c->rotate(SkIntToScalar(30)); |
| 347 | c->save(); |
| 348 | c->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 349 | c->save(); |
| 350 | c->scale(SkIntToScalar(3), SkIntToScalar(3)); |
| 351 | SkPaint paint; |
| 352 | paint.setColor(0xFFFFFFFF); |
| 353 | c->drawPaint(paint); |
| 354 | c->restore(); |
| 355 | c->restore(); |
| 356 | }, |
| 357 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 358 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 359 | // test case because the canvas flush on a deferred canvas will |
| 360 | // reset the recording session. The challenge is to maintain correct |
| 361 | // clip and matrix stack state on the playback canvas. |
| 362 | c->resetMatrix(); |
| 363 | c->rotate(SkIntToScalar(30)); |
| 364 | c->save(); |
| 365 | c->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 366 | c->save(); |
| 367 | c->scale(SkIntToScalar(3), SkIntToScalar(3)); |
| 368 | c->drawRect(kRect, SkPaint()); |
| 369 | c->flush(); |
| 370 | c->restore(); |
| 371 | c->restore(); |
| 372 | }, |
| 373 | [](SkCanvas* c, skiatest::Reporter* r) { |
| 374 | SkPoint pts[4]; |
| 375 | pts[0].set(0, 0); |
| 376 | pts[1].set(SkIntToScalar(kWidth), 0); |
| 377 | pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 378 | pts[3].set(0, SkIntToScalar(kHeight)); |
| 379 | SkPaint paint; |
| 380 | SkBitmap bitmap(make_n32_bitmap(kWidth, kHeight, 0x05060708)); |
| 381 | paint.setShader(bitmap.makeShader()); |
| 382 | c->drawVertices( |
| 383 | SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts, nullptr), |
| 384 | SkBlendMode::kModulate, paint); |
| 385 | } |
| 386 | }; |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 387 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 388 | DEF_TEST(Canvas_bitmap, reporter) { |
| 389 | for (const CanvasTest& test : kCanvasTests) { |
| 390 | SkBitmap referenceStore = make_n32_bitmap(kWidth, kHeight); |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 391 | SkCanvas referenceCanvas(referenceStore); |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 392 | test(&referenceCanvas, reporter); |
| 393 | } |
| 394 | } |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 395 | |
Hal Canary | 5f2807b | 2019-05-06 16:51:06 -0400 | [diff] [blame] | 396 | DEF_TEST(Canvas_pdf, reporter) { |
| 397 | for (const CanvasTest& test : kCanvasTests) { |
| 398 | SkNullWStream outStream; |
| 399 | if (auto doc = SkPDF::MakeDocument(&outStream)) { |
| 400 | SkCanvas* canvas = doc->beginPage(SkIntToScalar(kWidth), |
| 401 | SkIntToScalar(kHeight)); |
| 402 | REPORTER_ASSERT(reporter, canvas); |
| 403 | test(canvas, reporter); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 404 | } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 405 | } |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 406 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 407 | |
| 408 | DEF_TEST(Canvas_SaveState, reporter) { |
| 409 | SkCanvas canvas(10, 10); |
| 410 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 411 | |
| 412 | int n = canvas.save(); |
| 413 | REPORTER_ASSERT(reporter, 1 == n); |
| 414 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 415 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 416 | n = canvas.saveLayer(nullptr, nullptr); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, 2 == n); |
| 418 | REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 419 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 420 | canvas.restore(); |
| 421 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 422 | canvas.restore(); |
| 423 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 424 | } |
reed | c1b11f1 | 2015-03-13 08:48:26 -0700 | [diff] [blame] | 425 | |
| 426 | DEF_TEST(Canvas_ClipEmptyPath, reporter) { |
| 427 | SkCanvas canvas(10, 10); |
| 428 | canvas.save(); |
| 429 | SkPath path; |
| 430 | canvas.clipPath(path); |
| 431 | canvas.restore(); |
| 432 | canvas.save(); |
| 433 | path.moveTo(5, 5); |
| 434 | canvas.clipPath(path); |
| 435 | canvas.restore(); |
| 436 | canvas.save(); |
| 437 | path.moveTo(7, 7); |
| 438 | canvas.clipPath(path); // should not assert here |
| 439 | canvas.restore(); |
| 440 | } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 441 | |
| 442 | namespace { |
| 443 | |
| 444 | class MockFilterCanvas : public SkPaintFilterCanvas { |
| 445 | public: |
| 446 | MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } |
| 447 | |
| 448 | protected: |
Ben Wagner | f55fa0d | 2018-08-27 18:11:57 -0400 | [diff] [blame] | 449 | bool onFilter(SkPaint&) const override { return true; } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 450 | |
| 451 | private: |
| 452 | typedef SkPaintFilterCanvas INHERITED; |
| 453 | }; |
| 454 | |
| 455 | } // anonymous namespace |
| 456 | |
| 457 | // SkPaintFilterCanvas should inherit the initial target canvas state. |
| 458 | DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) { |
| 459 | SkCanvas canvas(100, 100); |
| 460 | canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75)); |
| 461 | canvas.scale(0.5f, 0.75f); |
| 462 | |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 463 | MockFilterCanvas filterCanvas(&canvas); |
| 464 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 465 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds()); |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 466 | |
| 467 | filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100)); |
| 468 | filterCanvas.scale(0.75f, 0.5f); |
| 469 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 470 | REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds())); |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 471 | } |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 472 | |
| 473 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 474 | |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 475 | namespace { |
| 476 | |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 477 | // Subclass that takes a bool*, which it updates in its construct (true) and destructor (false) |
| 478 | // to allow the caller to know how long the object is alive. |
| 479 | class LifeLineCanvas : public SkCanvas { |
| 480 | bool* fLifeLine; |
| 481 | public: |
| 482 | LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) { |
| 483 | *fLifeLine = true; |
| 484 | } |
| 485 | ~LifeLineCanvas() { |
| 486 | *fLifeLine = false; |
| 487 | } |
| 488 | }; |
| 489 | |
Ben Wagner | cbf6d30 | 2019-05-06 15:13:30 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 492 | // Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases |
| 493 | DEF_TEST(NWayCanvas, r) { |
| 494 | const int w = 10; |
| 495 | const int h = 10; |
| 496 | bool life[2]; |
| 497 | { |
| 498 | LifeLineCanvas c0(w, h, &life[0]); |
| 499 | REPORTER_ASSERT(r, life[0]); |
| 500 | } |
| 501 | REPORTER_ASSERT(r, !life[0]); |
| 502 | |
| 503 | |
| 504 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 505 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 506 | REPORTER_ASSERT(r, life[0]); |
| 507 | REPORTER_ASSERT(r, life[1]); |
| 508 | |
| 509 | { |
| 510 | SkNWayCanvas nway(w, h); |
| 511 | nway.addCanvas(c0.get()); |
| 512 | nway.addCanvas(c1.get()); |
| 513 | REPORTER_ASSERT(r, life[0]); |
| 514 | REPORTER_ASSERT(r, life[1]); |
| 515 | } |
| 516 | // Now assert that the death of the nway has NOT also killed the sub-canvases |
| 517 | REPORTER_ASSERT(r, life[0]); |
| 518 | REPORTER_ASSERT(r, life[1]); |
| 519 | } |
| 520 | |
| 521 | // Check that CanvasStack DOES manage the lifetime of its sub-canvases |
| 522 | DEF_TEST(CanvasStack, r) { |
| 523 | const int w = 10; |
| 524 | const int h = 10; |
| 525 | bool life[2]; |
| 526 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 527 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 528 | REPORTER_ASSERT(r, life[0]); |
| 529 | REPORTER_ASSERT(r, life[1]); |
| 530 | |
| 531 | { |
| 532 | SkCanvasStack stack(w, h); |
| 533 | stack.pushCanvas(std::move(c0), {0,0}); |
| 534 | stack.pushCanvas(std::move(c1), {0,0}); |
| 535 | REPORTER_ASSERT(r, life[0]); |
| 536 | REPORTER_ASSERT(r, life[1]); |
| 537 | } |
| 538 | // Now assert that the death of the canvasstack has also killed the sub-canvases |
| 539 | REPORTER_ASSERT(r, !life[0]); |
| 540 | REPORTER_ASSERT(r, !life[1]); |
| 541 | } |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 542 | |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 543 | static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) { |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 544 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 545 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 546 | |
| 547 | canvas->save(); |
| 548 | canvas->clipRect({0, 0, 0, 0}); |
| 549 | REPORTER_ASSERT(r, canvas->isClipEmpty()); |
| 550 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 551 | canvas->restore(); |
| 552 | |
| 553 | canvas->save(); |
| 554 | canvas->clipRect({2, 2, 6, 6}); |
| 555 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 556 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 557 | canvas->restore(); |
| 558 | |
| 559 | canvas->save(); |
| 560 | canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip |
| 561 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 562 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 563 | canvas->restore(); |
| 564 | |
| 565 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 566 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 567 | } |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 568 | |
| 569 | DEF_TEST(CanvasClipType, r) { |
| 570 | // test rasterclip backend |
| 571 | test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r); |
| 572 | |
| 573 | // test clipstack backend |
| 574 | SkDynamicMemoryWStream stream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 575 | if (auto doc = SkPDF::MakeDocument(&stream)) { |
Hal Canary | 4125b61 | 2018-03-20 14:17:00 -0400 | [diff] [blame] | 576 | test_cliptype(doc->beginPage(100, 100), r); |
| 577 | } |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 578 | } |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 579 | |
| 580 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 581 | DEF_TEST(Canvas_LegacyColorBehavior, r) { |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 582 | sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, |
| 583 | SkNamedGamut::kAdobeRGB); |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 584 | |
| 585 | // Make a Adobe RGB bitmap. |
| 586 | SkBitmap bitmap; |
| 587 | bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs)); |
| 588 | bitmap.eraseColor(0xFF000000); |
| 589 | |
| 590 | // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas. |
| 591 | SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy); |
| 592 | REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace()); |
| 593 | SkPaint p; |
| 594 | p.setColor(SK_ColorRED); |
| 595 | canvas.drawIRect(SkIRect::MakeWH(1, 1), p); |
| 596 | REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0))); |
| 597 | } |
| 598 | #endif |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 599 | |
| 600 | namespace { |
| 601 | |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 602 | class ZeroBoundsImageFilter : public SkImageFilter_Base { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 603 | public: |
| 604 | static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); } |
| 605 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 606 | protected: |
Michael Ludwig | e30a485 | 2019-08-14 14:35:42 -0400 | [diff] [blame] | 607 | sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint*) const override { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 608 | return nullptr; |
| 609 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 610 | SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, |
| 611 | MapDirection, const SkIRect* inputRect) const override { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 612 | return SkIRect::MakeEmpty(); |
| 613 | } |
| 614 | |
| 615 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 616 | SK_FLATTENABLE_HOOKS(ZeroBoundsImageFilter) |
| 617 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 618 | ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {} |
| 619 | |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 620 | typedef SkImageFilter_Base INHERITED; |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 624 | SkDEBUGFAIL("Should never get here"); |
| 625 | return nullptr; |
| 626 | } |
| 627 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 628 | } // anonymous namespace |
| 629 | |
| 630 | DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) { |
| 631 | SkCanvas canvas(10, 10); |
| 632 | SkPaint p; |
| 633 | p.setImageFilter(ZeroBoundsImageFilter::Make()); |
| 634 | // This should not fail any assert. |
| 635 | canvas.saveLayer(nullptr, &p); |
| 636 | REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty()); |
| 637 | canvas.restore(); |
| 638 | } |
Mike Reed | 490aa59 | 2018-04-13 15:34:16 -0400 | [diff] [blame] | 639 | |
Mike Reed | 490aa59 | 2018-04-13 15:34:16 -0400 | [diff] [blame] | 640 | // Test that we don't crash/assert when building a canvas with degenerate coordintes |
| 641 | // (esp. big ones, that might invoke tiling). |
| 642 | DEF_TEST(Canvas_degenerate_dimension, reporter) { |
| 643 | // Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the |
| 644 | // raster code further downstream. |
| 645 | SkPaint paint; |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 646 | paint.setImageFilter(SkImageFilters::Paint(SkPaint(), nullptr)); |
Mike Reed | 490aa59 | 2018-04-13 15:34:16 -0400 | [diff] [blame] | 647 | REPORTER_ASSERT(reporter, !paint.canComputeFastBounds()); |
| 648 | |
| 649 | const int big = 100 * 1024; // big enough to definitely trigger tiling |
| 650 | const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}}; |
| 651 | for (SkISize size : sizes) { |
| 652 | SkBitmap bm; |
| 653 | bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height())); |
| 654 | SkCanvas canvas(bm); |
| 655 | canvas.drawRect({0, 0, 100, 90*1024}, paint); |
| 656 | } |
| 657 | } |
| 658 | |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 659 | DEF_TEST(Canvas_ClippedOutImageFilter, reporter) { |
| 660 | SkCanvas canvas(100, 100); |
| 661 | |
| 662 | SkPaint p; |
| 663 | p.setColor(SK_ColorGREEN); |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 664 | p.setImageFilter(SkImageFilters::Blur(3.0f, 3.0f, nullptr, nullptr)); |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 665 | |
| 666 | SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30); |
| 667 | |
| 668 | SkMatrix invM; |
| 669 | invM.setRotate(-45); |
| 670 | invM.mapRect(&blurredRect); |
| 671 | |
| 672 | const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50); |
| 673 | |
| 674 | canvas.clipRect(clipRect); |
| 675 | |
| 676 | canvas.rotate(45); |
| 677 | const SkMatrix preCTM = canvas.getTotalMatrix(); |
| 678 | canvas.drawRect(blurredRect, p); |
| 679 | const SkMatrix postCTM = canvas.getTotalMatrix(); |
| 680 | REPORTER_ASSERT(reporter, preCTM == postCTM); |
| 681 | } |
| 682 | |
Mike Reed | 7fe6ee3 | 2020-04-09 12:35:09 -0400 | [diff] [blame] | 683 | DEF_TEST(canvas_markctm, reporter) { |
| 684 | SkCanvas canvas(10, 10); |
| 685 | |
| 686 | SkM44 m; |
Brian Osman | 548de74 | 2020-04-24 12:02:25 -0400 | [diff] [blame] | 687 | const char* id_a = "a"; |
| 688 | const char* id_b = "b"; |
Mike Reed | 7fe6ee3 | 2020-04-09 12:35:09 -0400 | [diff] [blame] | 689 | |
| 690 | REPORTER_ASSERT(reporter, !canvas.findMarkedCTM(id_a, nullptr)); |
| 691 | REPORTER_ASSERT(reporter, !canvas.findMarkedCTM(id_b, nullptr)); |
| 692 | |
| 693 | // remember the starting state |
| 694 | SkM44 b = canvas.getLocalToDevice(); |
| 695 | canvas.markCTM(id_b); |
| 696 | |
| 697 | // test add |
| 698 | canvas.concat(SkM44::Scale(2, 4, 6)); |
| 699 | SkM44 a = canvas.getLocalToDevice(); |
| 700 | canvas.markCTM(id_a); |
| 701 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a); |
| 702 | |
| 703 | // test replace |
| 704 | canvas.translate(1, 2); |
| 705 | SkM44 a1 = canvas.getLocalToDevice(); |
| 706 | SkASSERT(a != a1); |
| 707 | canvas.markCTM(id_a); |
| 708 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1); |
| 709 | |
| 710 | // test nested |
| 711 | canvas.save(); |
| 712 | // no change |
| 713 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_b, &m) && m == b); |
| 714 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1); |
| 715 | canvas.translate(2, 3); |
| 716 | SkM44 a2 = canvas.getLocalToDevice(); |
| 717 | SkASSERT(a2 != a1); |
| 718 | canvas.markCTM(id_a); |
| 719 | // found the new one |
| 720 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a2); |
| 721 | canvas.restore(); |
| 722 | // found the previous one |
| 723 | REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1); |
| 724 | } |