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