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