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