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