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