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