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 | |
| 8 | /* Description: |
| 9 | * This test defines a series of elementatry test steps that perform |
| 10 | * a single or a small group of canvas API calls. Each test step is |
| 11 | * used in several test cases that verify that different types of SkCanvas |
| 12 | * flavors and derivatives pass it and yield consistent behavior. The |
| 13 | * test cases analyse results that are queryable through the API. They do |
| 14 | * not look at rendering results. |
| 15 | * |
| 16 | * Adding test stepss: |
| 17 | * The general pattern for creating a new test step is to write a test |
| 18 | * function of the form: |
| 19 | * |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | * static void MyTestStepFunction(SkCanvas* canvas, |
tomhudson | cb3bd18 | 2016-05-18 07:24:16 -0700 | [diff] [blame] | 21 | * const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 22 | * skiatest::Reporter* reporter, |
| 23 | * CanvasTestStep* testStep) |
| 24 | * { |
| 25 | * canvas->someCanvasAPImethod(); |
| 26 | * (...) |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 27 | * REPORTER_ASSERT(reporter, (...), \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 28 | * testStep->assertMessage()); |
| 29 | * } |
| 30 | * |
| 31 | * The definition of the test step function should be followed by an |
| 32 | * invocation of the TEST_STEP macro, which generates a class and |
| 33 | * instance for the test step: |
| 34 | * |
| 35 | * TEST_STEP(MyTestStep, MyTestStepFunction) |
| 36 | * |
| 37 | * There are also short hand macros for defining simple test steps |
| 38 | * in a single line of code. A simple test step is a one that is made |
| 39 | * of a single canvas API call. |
| 40 | * |
| 41 | * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod()); |
| 42 | * |
| 43 | * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that |
| 44 | * works the same way as SIMPLE_TEST_STEP, and additionally verifies |
| 45 | * that the invoked method returns a non-zero value. |
| 46 | */ |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 47 | |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 48 | #include "SkBitmap.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 49 | #include "SkBlendMode.h" |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 50 | #include "SkCanvas.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 51 | #include "SkCanvasStack.h" |
| 52 | #include "SkClipOp.h" |
| 53 | #include "SkClipOpPriv.h" |
| 54 | #include "SkColor.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 55 | #include "SkImageFilter.h" |
| 56 | #include "SkImageInfo.h" |
| 57 | #include "SkMalloc.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 58 | #include "SkMatrix.h" |
| 59 | #include "SkNWayCanvas.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 60 | #include "SkPDFDocument.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 61 | #include "SkPaint.h" |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 62 | #include "SkPaintFilterCanvas.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 63 | #include "SkPath.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 64 | #include "SkPictureRecorder.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 65 | #include "SkPixmap.h" |
| 66 | #include "SkPoint.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 67 | #include "SkRect.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 68 | #include "SkRefCnt.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 69 | #include "SkRegion.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 70 | #include "SkScalar.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 71 | #include "SkShader.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 72 | #include "SkSize.h" |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 73 | #include "SkSpecialImage.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 74 | #include "SkStream.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 75 | #include "SkString.h" |
reed@google.com | 28183b4 | 2014-02-04 15:34:10 +0000 | [diff] [blame] | 76 | #include "SkSurface.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 77 | #include "SkTDArray.h" |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 78 | #include "SkTemplates.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 79 | #include "SkTypes.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 80 | #include "SkVertices.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 81 | #include "Test.h" |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 82 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 83 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 84 | #include "SkColorData.h" |
| 85 | #include "SkColorSpace.h" |
| 86 | #endif |
| 87 | |
| 88 | #include <memory> |
| 89 | #include <utility> |
| 90 | |
| 91 | class SkColorSpaceXformer; |
| 92 | class SkReadBuffer; |
| 93 | template <typename T> class SkTCopyOnFirstWrite; |
| 94 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 95 | DEF_TEST(canvas_clipbounds, reporter) { |
| 96 | SkCanvas canvas(10, 10); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 97 | SkIRect irect, irect2; |
| 98 | SkRect rect, rect2; |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 99 | |
| 100 | irect = canvas.getDeviceClipBounds(); |
| 101 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10)); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2)); |
| 103 | REPORTER_ASSERT(reporter, irect == irect2); |
| 104 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 105 | // local bounds are always too big today -- can we trim them? |
| 106 | rect = canvas.getLocalClipBounds(); |
| 107 | REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10))); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2)); |
| 109 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 110 | |
| 111 | canvas.clipRect(SkRect::MakeEmpty()); |
| 112 | |
| 113 | irect = canvas.getDeviceClipBounds(); |
| 114 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2)); |
| 116 | REPORTER_ASSERT(reporter, irect == irect2); |
| 117 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 118 | rect = canvas.getLocalClipBounds(); |
| 119 | REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2)); |
| 121 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 122 | |
| 123 | // Test for wacky sizes that we (historically) have guarded against |
| 124 | { |
| 125 | SkCanvas c(-10, -20); |
| 126 | REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty()); |
Mike Reed | dc9f0db | 2017-03-10 13:54:16 -0500 | [diff] [blame] | 127 | |
| 128 | SkPictureRecorder().beginRecording({ 5, 5, 4, 4 }); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 129 | } |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 132 | // Will call proc with multiple styles of canvas (recording, raster, pdf) |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 133 | template <typename F> static void multi_canvas_driver(int w, int h, F proc) { |
| 134 | proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h))); |
| 135 | |
| 136 | SkNullWStream stream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 137 | if (auto doc = SkPDF::MakeDocument(&stream)) { |
Hal Canary | 4125b61 | 2018-03-20 14:17:00 -0400 | [diff] [blame] | 138 | proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h))); |
| 139 | } |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 140 | |
| 141 | proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas()); |
| 142 | } |
| 143 | |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 144 | const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 }; |
| 145 | |
| 146 | static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) { |
| 147 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR); |
| 148 | |
| 149 | const SkIRect restrictionR = { 2, 2, 8, 8 }; |
| 150 | canvas->androidFramework_setDeviceClipRestriction(restrictionR); |
| 151 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR); |
| 152 | |
| 153 | const SkIRect clipR = { 4, 4, 6, 6 }; |
| 154 | canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect); |
| 155 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR); |
| 156 | |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 157 | #ifdef SK_SUPPORT_DEPRECATED_CLIPOPS |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 158 | // now test that expanding clipops can't exceed the restriction |
| 159 | const SkClipOp expanders[] = { |
| 160 | SkClipOp::kUnion_deprecated, |
| 161 | SkClipOp::kXOR_deprecated, |
| 162 | SkClipOp::kReverseDifference_deprecated, |
| 163 | SkClipOp::kReplace_deprecated, |
| 164 | }; |
| 165 | |
| 166 | const SkRect expandR = { 0, 0, 5, 9 }; |
| 167 | SkASSERT(!SkRect::Make(restrictionR).contains(expandR)); |
| 168 | |
| 169 | for (SkClipOp op : expanders) { |
| 170 | canvas->save(); |
| 171 | canvas->clipRect(expandR, op); |
| 172 | REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds())); |
| 173 | canvas->restore(); |
| 174 | } |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 175 | #endif |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Clip restriction logic exists in the canvas itself, and in various kinds of devices. |
| 180 | * |
| 181 | * This test explicitly tries to exercise that variety: |
| 182 | * - picture : empty device but exercises canvas itself |
| 183 | * - pdf : uses SkClipStack in its device (as does SVG and GPU) |
| 184 | * - raster : uses SkRasterClip in its device |
| 185 | */ |
| 186 | DEF_TEST(canvas_clip_restriction, reporter) { |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 187 | multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(), |
| 188 | [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); }); |
| 189 | } |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 190 | |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 191 | DEF_TEST(canvas_empty_clip, reporter) { |
| 192 | multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) { |
| 193 | canvas->save(); |
| 194 | canvas->clipRect({0, 0, 20, 40 }); |
| 195 | REPORTER_ASSERT(reporter, !canvas->isClipEmpty()); |
| 196 | canvas->clipRect({30, 0, 50, 40 }); |
| 197 | REPORTER_ASSERT(reporter, canvas->isClipEmpty()); |
| 198 | }); |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 199 | } |
| 200 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 201 | static const int kWidth = 2, kHeight = 2; |
| 202 | |
| 203 | static void createBitmap(SkBitmap* bm, SkColor color) { |
| 204 | bm->allocN32Pixels(kWidth, kHeight); |
| 205 | bm->eraseColor(color); |
| 206 | } |
| 207 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 208 | /////////////////////////////////////////////////////////////////////////////// |
| 209 | // Constants used by test steps |
| 210 | const SkPoint kTestPoints[] = { |
| 211 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 212 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 213 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 214 | }; |
| 215 | const SkPoint kTestPoints2[] = { |
| 216 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 217 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 218 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 219 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 220 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 221 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 222 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 223 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 224 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 225 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 226 | { SkIntToScalar(10), SkIntToScalar(1) } |
| 227 | }; |
| 228 | |
| 229 | struct TestData { |
| 230 | public: |
| 231 | TestData() |
| 232 | : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 233 | SkIntToScalar(2), SkIntToScalar(1))) |
| 234 | , fMatrix(TestMatrix()) |
| 235 | , fPath(TestPath()) |
| 236 | , fNearlyZeroLengthPath(TestNearlyZeroLengthPath()) |
| 237 | , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1)) |
| 238 | , fRegion(TestRegion()) |
| 239 | , fColor(0x01020304) |
| 240 | , fPoints(kTestPoints) |
| 241 | , fPointCount(3) |
| 242 | , fWidth(2) |
| 243 | , fHeight(2) |
| 244 | , fText("Hello World") |
| 245 | , fPoints2(kTestPoints2) |
| 246 | , fBitmap(TestBitmap()) |
| 247 | { } |
| 248 | |
| 249 | SkRect fRect; |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 250 | SkMatrix fMatrix; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 251 | SkPath fPath; |
| 252 | SkPath fNearlyZeroLengthPath; |
| 253 | SkIRect fIRect; |
| 254 | SkRegion fRegion; |
| 255 | SkColor fColor; |
| 256 | SkPaint fPaint; |
| 257 | const SkPoint* fPoints; |
| 258 | size_t fPointCount; |
| 259 | int fWidth; |
| 260 | int fHeight; |
| 261 | SkString fText; |
| 262 | const SkPoint* fPoints2; |
| 263 | SkBitmap fBitmap; |
| 264 | |
| 265 | private: |
| 266 | static SkMatrix TestMatrix() { |
| 267 | SkMatrix matrix; |
| 268 | matrix.reset(); |
| 269 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 270 | |
| 271 | return matrix; |
| 272 | } |
| 273 | static SkPath TestPath() { |
| 274 | SkPath path; |
| 275 | path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 276 | SkIntToScalar(2), SkIntToScalar(1))); |
| 277 | return path; |
| 278 | } |
| 279 | static SkPath TestNearlyZeroLengthPath() { |
| 280 | SkPath path; |
| 281 | SkPoint pt1 = { 0, 0 }; |
| 282 | SkPoint pt2 = { 0, SK_ScalarNearlyZero }; |
| 283 | SkPoint pt3 = { SkIntToScalar(1), 0 }; |
| 284 | SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 }; |
| 285 | path.moveTo(pt1); |
| 286 | path.lineTo(pt2); |
| 287 | path.lineTo(pt3); |
| 288 | path.lineTo(pt4); |
| 289 | return path; |
| 290 | } |
| 291 | static SkRegion TestRegion() { |
| 292 | SkRegion region; |
| 293 | SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 294 | region.setRect(rect); |
| 295 | return region; |
| 296 | } |
| 297 | static SkBitmap TestBitmap() { |
| 298 | SkBitmap bitmap; |
| 299 | createBitmap(&bitmap, 0x05060708); |
| 300 | return bitmap; |
| 301 | } |
| 302 | }; |
| 303 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 304 | // Format strings that describe the test context. The %s token is where |
| 305 | // the name of the test step is inserted. The context is required for |
| 306 | // disambiguating the error in the case of failures that are reported in |
| 307 | // functions that are called multiple times in different contexts (test |
| 308 | // cases and test steps). |
| 309 | static const char* const kDefaultAssertMessageFormat = "%s"; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 310 | static const char* const kCanvasDrawAssertMessageFormat = |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 311 | "Drawing test step %s with SkCanvas"; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 312 | static const char* const kPdfAssertMessageFormat = |
| 313 | "PDF sanity check failed %s"; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 314 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 315 | class CanvasTestStep; |
| 316 | static SkTDArray<CanvasTestStep*>& testStepArray() { |
| 317 | static SkTDArray<CanvasTestStep*> theTests; |
| 318 | return theTests; |
| 319 | } |
| 320 | |
| 321 | class CanvasTestStep { |
| 322 | public: |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 323 | CanvasTestStep(bool fEnablePdfTesting = true) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 324 | *testStepArray().append() = this; |
| 325 | fAssertMessageFormat = kDefaultAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 326 | this->fEnablePdfTesting = fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 327 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 328 | virtual ~CanvasTestStep() { } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 329 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 330 | virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 331 | virtual const char* name() const = 0; |
| 332 | |
| 333 | const char* assertMessage() { |
| 334 | fAssertMessage.printf(fAssertMessageFormat, name()); |
| 335 | return fAssertMessage.c_str(); |
| 336 | } |
| 337 | |
| 338 | void setAssertMessageFormat(const char* format) { |
| 339 | fAssertMessageFormat = format; |
| 340 | } |
| 341 | |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 342 | bool enablePdfTesting() { return fEnablePdfTesting; } |
| 343 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 344 | private: |
| 345 | SkString fAssertMessage; |
| 346 | const char* fAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 347 | bool fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | /////////////////////////////////////////////////////////////////////////////// |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 351 | // Macros for defining test steps |
| 352 | |
| 353 | #define TEST_STEP(NAME, FUNCTION) \ |
| 354 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 355 | public: \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 356 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 357 | skiatest::Reporter* reporter) { \ |
| 358 | FUNCTION (canvas, d, reporter, this); \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 359 | } \ |
| 360 | virtual const char* name() const {return #NAME ;} \ |
| 361 | }; \ |
| 362 | static NAME##_TestStep NAME##_TestStepInstance; |
| 363 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 364 | #define TEST_STEP_NO_PDF(NAME, FUNCTION) \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 365 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 366 | public: \ |
| 367 | NAME##_TestStep() : CanvasTestStep(false) {} \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 368 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 369 | skiatest::Reporter* reporter) { \ |
| 370 | FUNCTION (canvas, d, reporter, this); \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 371 | } \ |
| 372 | virtual const char* name() const {return #NAME ;} \ |
| 373 | }; \ |
| 374 | static NAME##_TestStep NAME##_TestStepInstance; |
| 375 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 376 | #define SIMPLE_TEST_STEP(NAME, CALL) \ |
| 377 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \ |
| 378 | skiatest::Reporter*, CanvasTestStep*) { \ |
| 379 | canvas-> CALL ; \ |
| 380 | } \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 381 | TEST_STEP(NAME, NAME##TestStep ) |
| 382 | |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 383 | #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \ |
| 384 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, skiatest::Reporter*, \ |
| 385 | CanvasTestStep* testStep) { \ |
| 386 | REPORTER_ASSERT(reporter, canvas->CALL, testStep->assertMessage()); \ |
| 387 | } \ |
| 388 | TEST_STEP(NAME, NAME##TestStep) |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 389 | |
| 390 | /////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 391 | // Basic test steps for most virtual methods in SkCanvas that draw or affect |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 392 | // the state of the canvas. |
| 393 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 394 | SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2))); |
| 395 | SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2))); |
| 396 | SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1))); |
| 397 | SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2))); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 398 | SIMPLE_TEST_STEP(Concat, concat(d.fMatrix)); |
| 399 | SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix)); |
| 400 | SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect)); |
| 401 | SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 402 | SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 403 | SIMPLE_TEST_STEP(Clear, clear(d.fColor)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 404 | |
| 405 | /////////////////////////////////////////////////////////////////////////////// |
| 406 | // Complex test steps |
| 407 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 408 | static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d, |
| 409 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 410 | int saveCount = canvas->getSaveCount(); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 411 | canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 412 | canvas->translate(SkIntToScalar(1), SkIntToScalar(2)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 413 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 414 | canvas->restore(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 415 | REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage()); |
| 416 | REPORTER_ASSERT(reporter, canvas->getTotalMatrix().isIdentity(), testStep->assertMessage()); |
| 417 | // REPORTER_ASSERT(reporter, canvas->getTotalClip() != kTestRegion, |
| 418 | // testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 419 | } |
| 420 | TEST_STEP(SaveMatrixClip, SaveMatrixClipStep); |
| 421 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 422 | static void SaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 423 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 424 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 425 | canvas->saveLayer(nullptr, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 426 | canvas->restore(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 427 | REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 428 | } |
| 429 | TEST_STEP(SaveLayer, SaveLayerStep); |
| 430 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 431 | static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 432 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 433 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 434 | canvas->saveLayer(&d.fRect, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 435 | canvas->restore(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 436 | REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 437 | } |
| 438 | TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep); |
| 439 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 440 | static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 441 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 442 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 443 | canvas->saveLayer(nullptr, &d.fPaint); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 444 | canvas->restore(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 445 | REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 446 | } |
| 447 | TEST_STEP(PaintSaveLayer, PaintSaveLayerStep); |
| 448 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 449 | static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d, |
| 450 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 451 | // This test exercises a functionality in SkPicture that leads to the |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 452 | // recording of restore offset placeholders. This test will trigger an |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 453 | // assertion at playback time if the placeholders are not properly |
| 454 | // filled when the recording ends. |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 455 | canvas->clipRect(d.fRect); |
| 456 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 457 | } |
| 458 | TEST_STEP(TwoClipOps, TwoClipOpsStep); |
| 459 | |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 460 | // exercise fix for http://code.google.com/p/skia/issues/detail?id=560 |
| 461 | // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero') |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 462 | static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d, |
| 463 | skiatest::Reporter*, CanvasTestStep*) { |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 464 | SkPaint paint; |
| 465 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 466 | paint.setStyle(SkPaint::kStroke_Style); |
| 467 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 468 | canvas->drawPath(d.fNearlyZeroLengthPath, paint); |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 469 | } |
| 470 | TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep); |
| 471 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 472 | static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d, |
| 473 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 474 | SkPoint pts[4]; |
| 475 | pts[0].set(0, 0); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 476 | pts[1].set(SkIntToScalar(d.fWidth), 0); |
| 477 | pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight)); |
| 478 | pts[3].set(0, SkIntToScalar(d.fHeight)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 479 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 480 | paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode, |
| 481 | SkShader::kClamp_TileMode)); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 482 | canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts, |
| 483 | nullptr), |
| 484 | SkBlendMode::kModulate, paint); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 485 | } |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 486 | // NYI: issue 240. |
| 487 | TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 488 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 489 | static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d, |
| 490 | skiatest::Reporter*, CanvasTestStep*) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 491 | SkPictureRecorder recorder; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 492 | SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 493 | nullptr, 0); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 494 | testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 495 | testCanvas->clipRect(d.fRect); |
| 496 | testCanvas->drawRect(d.fRect, d.fPaint); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 497 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 498 | canvas->drawPicture(recorder.finishRecordingAsPicture()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 499 | } |
| 500 | TEST_STEP(DrawPicture, DrawPictureTestStep); |
| 501 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 502 | static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d, |
| 503 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 504 | int baseSaveCount = canvas->getSaveCount(); |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 505 | int n = canvas->save(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 506 | REPORTER_ASSERT(reporter, baseSaveCount == n, testStep->assertMessage()); |
| 507 | REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
| 508 | testStep->assertMessage()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 509 | canvas->save(); |
| 510 | canvas->save(); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 511 | REPORTER_ASSERT(reporter, baseSaveCount + 3 == canvas->getSaveCount(), |
| 512 | testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 513 | canvas->restoreToCount(baseSaveCount + 1); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 514 | REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
| 515 | testStep->assertMessage()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 516 | |
| 517 | // should this pin to 1, or be a no-op, or crash? |
| 518 | canvas->restoreToCount(0); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 519 | REPORTER_ASSERT(reporter, 1 == canvas->getSaveCount(), testStep->assertMessage()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 520 | } |
| 521 | TEST_STEP(SaveRestore, SaveRestoreTestStep); |
| 522 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 523 | static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d, |
| 524 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 525 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 526 | // test cases because the opaque paint can trigger an optimization |
| 527 | // that discards previously recorded commands. The challenge is to maintain |
| 528 | // correct clip and matrix stack state. |
| 529 | canvas->resetMatrix(); |
| 530 | canvas->rotate(SkIntToScalar(30)); |
| 531 | canvas->save(); |
| 532 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 533 | canvas->save(); |
| 534 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
| 535 | SkPaint paint; |
| 536 | paint.setColor(0xFFFFFFFF); |
| 537 | canvas->drawPaint(paint); |
| 538 | canvas->restore(); |
| 539 | canvas->restore(); |
| 540 | } |
| 541 | TEST_STEP(NestedSaveRestoreWithSolidPaint, \ |
| 542 | NestedSaveRestoreWithSolidPaintTestStep); |
| 543 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 544 | static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d, |
| 545 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 546 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 547 | // test case because the canvas flush on a deferred canvas will |
| 548 | // reset the recording session. The challenge is to maintain correct |
| 549 | // clip and matrix stack state on the playback canvas. |
| 550 | canvas->resetMatrix(); |
| 551 | canvas->rotate(SkIntToScalar(30)); |
| 552 | canvas->save(); |
| 553 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 554 | canvas->save(); |
| 555 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 556 | canvas->drawRect(d.fRect,d.fPaint); |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 557 | canvas->flush(); |
| 558 | canvas->restore(); |
| 559 | canvas->restore(); |
| 560 | } |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 561 | TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 562 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 563 | static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 564 | SkDynamicMemoryWStream outStream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 565 | sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream)); |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 566 | if (!doc) { |
Hal Canary | 4125b61 | 2018-03-20 14:17:00 -0400 | [diff] [blame] | 567 | INFOF(reporter, "PDF disabled; TestPdfDevice test skipped."); |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 568 | return; |
| 569 | } |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 570 | SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth), |
| 571 | SkIntToScalar(d.fHeight)); |
| 572 | REPORTER_ASSERT(reporter, canvas); |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 573 | step->setAssertMessageFormat(kPdfAssertMessageFormat); |
| 574 | step->draw(canvas, d, reporter); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 575 | } |
| 576 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 577 | /* |
| 578 | * This sub-test verifies that the test step passes when executed |
| 579 | * with SkCanvas and with classes derrived from SkCanvas. It also verifies |
| 580 | * that the all canvas derivatives report the same state as an SkCanvas |
| 581 | * after having executed the test step. |
| 582 | */ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 583 | static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 584 | CanvasTestStep* testStep) { |
| 585 | SkBitmap referenceStore; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 586 | createBitmap(&referenceStore, 0xFFFFFFFF); |
reed | 2a8ca93 | 2014-06-26 22:12:09 -0700 | [diff] [blame] | 587 | SkCanvas referenceCanvas(referenceStore); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 588 | testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 589 | testStep->draw(&referenceCanvas, d, reporter); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 590 | } |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 591 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 592 | static void test_newraster(skiatest::Reporter* reporter) { |
| 593 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 594 | const size_t minRowBytes = info.minRowBytes(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 595 | const size_t size = info.computeByteSize(minRowBytes); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 596 | SkAutoTMalloc<SkPMColor> storage(size); |
| 597 | SkPMColor* baseAddr = storage.get(); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 598 | sk_bzero(baseAddr, size); |
| 599 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 600 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 601 | REPORTER_ASSERT(reporter, canvas); |
| 602 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 603 | SkPixmap pmap; |
| 604 | const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 605 | REPORTER_ASSERT(reporter, addr); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 606 | REPORTER_ASSERT(reporter, info == pmap.info()); |
| 607 | REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 608 | for (int y = 0; y < info.height(); ++y) { |
| 609 | for (int x = 0; x < info.width(); ++x) { |
| 610 | REPORTER_ASSERT(reporter, 0 == addr[x]); |
| 611 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 612 | addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 613 | } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 614 | |
| 615 | // now try a deliberately bad info |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 616 | info = info.makeWH(-1, info.height()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 617 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 618 | |
| 619 | // too big |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 620 | info = info.makeWH(1 << 30, 1 << 30); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 621 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
skia.committer@gmail.com | 0e53075 | 2014-02-28 03:02:05 +0000 | [diff] [blame] | 622 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 623 | // not a valid pixel type |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 624 | info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 625 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 626 | |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 627 | // We should not succeed with a zero-sized valid info |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 628 | info = SkImageInfo::MakeN32Premul(0, 0); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 629 | canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 630 | REPORTER_ASSERT(reporter, nullptr == canvas); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 631 | } |
| 632 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 633 | DEF_TEST(Canvas, reporter) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 634 | TestData d; |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 635 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 636 | for (int testStep = 0; testStep < testStepArray().count(); testStep++) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 637 | TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 638 | if (testStepArray()[testStep]->enablePdfTesting()) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 639 | TestPdfDevice(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 640 | } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 641 | } |
junov@chromium.org | cd62ecf | 2012-08-02 17:43:25 +0000 | [diff] [blame] | 642 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 643 | test_newraster(reporter); |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 644 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 645 | |
| 646 | DEF_TEST(Canvas_SaveState, reporter) { |
| 647 | SkCanvas canvas(10, 10); |
| 648 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 649 | |
| 650 | int n = canvas.save(); |
| 651 | REPORTER_ASSERT(reporter, 1 == n); |
| 652 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 653 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 654 | n = canvas.saveLayer(nullptr, nullptr); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 655 | REPORTER_ASSERT(reporter, 2 == n); |
| 656 | REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 657 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 658 | canvas.restore(); |
| 659 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 660 | canvas.restore(); |
| 661 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 662 | } |
reed | c1b11f1 | 2015-03-13 08:48:26 -0700 | [diff] [blame] | 663 | |
| 664 | DEF_TEST(Canvas_ClipEmptyPath, reporter) { |
| 665 | SkCanvas canvas(10, 10); |
| 666 | canvas.save(); |
| 667 | SkPath path; |
| 668 | canvas.clipPath(path); |
| 669 | canvas.restore(); |
| 670 | canvas.save(); |
| 671 | path.moveTo(5, 5); |
| 672 | canvas.clipPath(path); |
| 673 | canvas.restore(); |
| 674 | canvas.save(); |
| 675 | path.moveTo(7, 7); |
| 676 | canvas.clipPath(path); // should not assert here |
| 677 | canvas.restore(); |
| 678 | } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 679 | |
| 680 | namespace { |
| 681 | |
| 682 | class MockFilterCanvas : public SkPaintFilterCanvas { |
| 683 | public: |
| 684 | MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } |
| 685 | |
| 686 | protected: |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 687 | bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 688 | |
| 689 | private: |
| 690 | typedef SkPaintFilterCanvas INHERITED; |
| 691 | }; |
| 692 | |
| 693 | } // anonymous namespace |
| 694 | |
| 695 | // SkPaintFilterCanvas should inherit the initial target canvas state. |
| 696 | DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) { |
| 697 | SkCanvas canvas(100, 100); |
| 698 | canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75)); |
| 699 | canvas.scale(0.5f, 0.75f); |
| 700 | |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 701 | MockFilterCanvas filterCanvas(&canvas); |
| 702 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 703 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds()); |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 704 | |
| 705 | filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100)); |
| 706 | filterCanvas.scale(0.75f, 0.5f); |
| 707 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 708 | REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds())); |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 709 | } |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 710 | |
| 711 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 712 | |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 713 | // Subclass that takes a bool*, which it updates in its construct (true) and destructor (false) |
| 714 | // to allow the caller to know how long the object is alive. |
| 715 | class LifeLineCanvas : public SkCanvas { |
| 716 | bool* fLifeLine; |
| 717 | public: |
| 718 | LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) { |
| 719 | *fLifeLine = true; |
| 720 | } |
| 721 | ~LifeLineCanvas() { |
| 722 | *fLifeLine = false; |
| 723 | } |
| 724 | }; |
| 725 | |
| 726 | // Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases |
| 727 | DEF_TEST(NWayCanvas, r) { |
| 728 | const int w = 10; |
| 729 | const int h = 10; |
| 730 | bool life[2]; |
| 731 | { |
| 732 | LifeLineCanvas c0(w, h, &life[0]); |
| 733 | REPORTER_ASSERT(r, life[0]); |
| 734 | } |
| 735 | REPORTER_ASSERT(r, !life[0]); |
| 736 | |
| 737 | |
| 738 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 739 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 740 | REPORTER_ASSERT(r, life[0]); |
| 741 | REPORTER_ASSERT(r, life[1]); |
| 742 | |
| 743 | { |
| 744 | SkNWayCanvas nway(w, h); |
| 745 | nway.addCanvas(c0.get()); |
| 746 | nway.addCanvas(c1.get()); |
| 747 | REPORTER_ASSERT(r, life[0]); |
| 748 | REPORTER_ASSERT(r, life[1]); |
| 749 | } |
| 750 | // Now assert that the death of the nway has NOT also killed the sub-canvases |
| 751 | REPORTER_ASSERT(r, life[0]); |
| 752 | REPORTER_ASSERT(r, life[1]); |
| 753 | } |
| 754 | |
| 755 | // Check that CanvasStack DOES manage the lifetime of its sub-canvases |
| 756 | DEF_TEST(CanvasStack, r) { |
| 757 | const int w = 10; |
| 758 | const int h = 10; |
| 759 | bool life[2]; |
| 760 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 761 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 762 | REPORTER_ASSERT(r, life[0]); |
| 763 | REPORTER_ASSERT(r, life[1]); |
| 764 | |
| 765 | { |
| 766 | SkCanvasStack stack(w, h); |
| 767 | stack.pushCanvas(std::move(c0), {0,0}); |
| 768 | stack.pushCanvas(std::move(c1), {0,0}); |
| 769 | REPORTER_ASSERT(r, life[0]); |
| 770 | REPORTER_ASSERT(r, life[1]); |
| 771 | } |
| 772 | // Now assert that the death of the canvasstack has also killed the sub-canvases |
| 773 | REPORTER_ASSERT(r, !life[0]); |
| 774 | REPORTER_ASSERT(r, !life[1]); |
| 775 | } |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 776 | |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 777 | static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) { |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 778 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 779 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 780 | |
| 781 | canvas->save(); |
| 782 | canvas->clipRect({0, 0, 0, 0}); |
| 783 | REPORTER_ASSERT(r, canvas->isClipEmpty()); |
| 784 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 785 | canvas->restore(); |
| 786 | |
| 787 | canvas->save(); |
| 788 | canvas->clipRect({2, 2, 6, 6}); |
| 789 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 790 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 791 | canvas->restore(); |
| 792 | |
| 793 | canvas->save(); |
| 794 | canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip |
| 795 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 796 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 797 | canvas->restore(); |
| 798 | |
| 799 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 800 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 801 | } |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 802 | |
| 803 | DEF_TEST(CanvasClipType, r) { |
| 804 | // test rasterclip backend |
| 805 | test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r); |
| 806 | |
| 807 | // test clipstack backend |
| 808 | SkDynamicMemoryWStream stream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 809 | if (auto doc = SkPDF::MakeDocument(&stream)) { |
Hal Canary | 4125b61 | 2018-03-20 14:17:00 -0400 | [diff] [blame] | 810 | test_cliptype(doc->beginPage(100, 100), r); |
| 811 | } |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 812 | } |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 813 | |
| 814 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 815 | DEF_TEST(Canvas_LegacyColorBehavior, r) { |
| 816 | sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 817 | SkColorSpace::kAdobeRGB_Gamut); |
| 818 | |
| 819 | // Make a Adobe RGB bitmap. |
| 820 | SkBitmap bitmap; |
| 821 | bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs)); |
| 822 | bitmap.eraseColor(0xFF000000); |
| 823 | |
| 824 | // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas. |
| 825 | SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy); |
| 826 | REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace()); |
| 827 | SkPaint p; |
| 828 | p.setColor(SK_ColorRED); |
| 829 | canvas.drawIRect(SkIRect::MakeWH(1, 1), p); |
| 830 | REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0))); |
| 831 | } |
| 832 | #endif |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 833 | |
| 834 | namespace { |
| 835 | |
| 836 | class ZeroBoundsImageFilter : public SkImageFilter { |
| 837 | public: |
| 838 | static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); } |
| 839 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 840 | protected: |
| 841 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage*, const Context&, SkIPoint*) const override { |
| 842 | return nullptr; |
| 843 | } |
| 844 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { return nullptr; } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 845 | SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, |
| 846 | MapDirection, const SkIRect* inputRect) const override { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 847 | return SkIRect::MakeEmpty(); |
| 848 | } |
| 849 | |
| 850 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 851 | SK_FLATTENABLE_HOOKS(ZeroBoundsImageFilter) |
| 852 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 853 | ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {} |
| 854 | |
| 855 | typedef SkImageFilter INHERITED; |
| 856 | }; |
| 857 | |
| 858 | sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 859 | SkDEBUGFAIL("Should never get here"); |
| 860 | return nullptr; |
| 861 | } |
| 862 | |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 863 | } // anonymous namespace |
| 864 | |
| 865 | DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) { |
| 866 | SkCanvas canvas(10, 10); |
| 867 | SkPaint p; |
| 868 | p.setImageFilter(ZeroBoundsImageFilter::Make()); |
| 869 | // This should not fail any assert. |
| 870 | canvas.saveLayer(nullptr, &p); |
| 871 | REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty()); |
| 872 | canvas.restore(); |
| 873 | } |
Mike Reed | 490aa59 | 2018-04-13 15:34:16 -0400 | [diff] [blame] | 874 | |
| 875 | #include "SkPaintImageFilter.h" |
| 876 | |
| 877 | // Test that we don't crash/assert when building a canvas with degenerate coordintes |
| 878 | // (esp. big ones, that might invoke tiling). |
| 879 | DEF_TEST(Canvas_degenerate_dimension, reporter) { |
| 880 | // Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the |
| 881 | // raster code further downstream. |
| 882 | SkPaint paint; |
| 883 | paint.setImageFilter(SkPaintImageFilter::Make(SkPaint(), nullptr)); |
| 884 | REPORTER_ASSERT(reporter, !paint.canComputeFastBounds()); |
| 885 | |
| 886 | const int big = 100 * 1024; // big enough to definitely trigger tiling |
| 887 | const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}}; |
| 888 | for (SkISize size : sizes) { |
| 889 | SkBitmap bm; |
| 890 | bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height())); |
| 891 | SkCanvas canvas(bm); |
| 892 | canvas.drawRect({0, 0, 100, 90*1024}, paint); |
| 893 | } |
| 894 | } |
| 895 | |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 896 | #include "SkBlurImageFilter.h" |
| 897 | |
| 898 | DEF_TEST(Canvas_ClippedOutImageFilter, reporter) { |
| 899 | SkCanvas canvas(100, 100); |
| 900 | |
| 901 | SkPaint p; |
| 902 | p.setColor(SK_ColorGREEN); |
| 903 | p.setImageFilter(SkBlurImageFilter::Make(3.0f, 3.0f, nullptr, nullptr)); |
| 904 | |
| 905 | SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30); |
| 906 | |
| 907 | SkMatrix invM; |
| 908 | invM.setRotate(-45); |
| 909 | invM.mapRect(&blurredRect); |
| 910 | |
| 911 | const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50); |
| 912 | |
| 913 | canvas.clipRect(clipRect); |
| 914 | |
| 915 | canvas.rotate(45); |
| 916 | const SkMatrix preCTM = canvas.getTotalMatrix(); |
| 917 | canvas.drawRect(blurredRect, p); |
| 918 | const SkMatrix postCTM = canvas.getTotalMatrix(); |
| 919 | REPORTER_ASSERT(reporter, preCTM == postCTM); |
| 920 | } |
| 921 | |