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