blob: 0c4683e58b6db10285bc25c15653901db1c4897d [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001
2/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00003 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00008
9/* Description:
10 * This test defines a series of elementatry test steps that perform
11 * a single or a small group of canvas API calls. Each test step is
12 * used in several test cases that verify that different types of SkCanvas
13 * flavors and derivatives pass it and yield consistent behavior. The
14 * test cases analyse results that are queryable through the API. They do
15 * not look at rendering results.
16 *
17 * Adding test stepss:
18 * The general pattern for creating a new test step is to write a test
19 * function of the form:
20 *
21 * static void MyTestStepFunction(SkCanvas* canvas,
22 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000049#include "SkDeferredCanvas.h"
50#include "SkDevice.h"
51#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
53#include "SkPaint.h"
54#include "SkPath.h"
55#include "SkPicture.h"
56#include "SkPictureRecord.h"
57#include "SkProxyCanvas.h"
58#include "SkRect.h"
59#include "SkRegion.h"
60#include "SkShader.h"
61#include "SkStream.h"
62#include "SkTDArray.h"
63#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000064
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000065static const int kWidth = 2;
66static const int kHeight = 2;
67// Maximum stream length for picture serialization
68static const size_t kMaxPictureBufferSize = 1024;
reed@google.com7c202932011-12-14 18:48:05 +000069
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000070// Format strings that describe the test context. The %s token is where
71// the name of the test step is inserted. The context is required for
72// disambiguating the error in the case of failures that are reported in
73// functions that are called multiple times in different contexts (test
74// cases and test steps).
75static const char* const kDefaultAssertMessageFormat = "%s";
76static const char* const kCanvasDrawAssertMessageFormat =
77 "Drawing test step %s with SkCanvas";
78static const char* const kPictureDrawAssertMessageFormat =
79 "Drawing test step %s with SkPicture";
80static const char* const kPictureSecondDrawAssertMessageFormat =
81 "Duplicate draw of test step %s with SkPicture";
82static const char* const kPictureReDrawAssertMessageFormat =
83 "Playing back test step %s from an SkPicture to another SkPicture";
84static const char* const kDeferredDrawAssertMessageFormat =
85 "Drawing test step %s with SkDeferredCanvas";
86static const char* const kProxyDrawAssertMessageFormat =
87 "Drawing test step %s with SkProxyCanvas";
88static const char* const kNWayDrawAssertMessageFormat =
89 "Drawing test step %s with SkNWayCanvas";
90static const char* const kRoundTripAssertMessageFormat =
91 "test step %s, SkPicture consistency after round trip";
92static const char* const kPictureRecoringAssertMessageFormat =
93 "test step %s, SkPicture state consistency after recording";
94static const char* const kPicturePlaybackAssertMessageFormat =
95 "test step %s, SkPicture state consistency in playback canvas";
96static const char* const kDeferredPreFlushAssertMessageFormat =
97 "test step %s, SkDeferredCanvas state consistency before flush";
98static const char* const kDeferredPostFlushAssertMessageFormat =
99 "test step %s, SkDeferredCanvas state consistency after flush";
100static const char* const kPictureResourceReuseMessageFormat =
101 "test step %s, SkPicture duplicate flattened object test";
102static const char* const kProxyStateAssertMessageFormat =
103 "test step %s, SkProxyCanvas state consistency";
104static const char* const kProxyIndirectStateAssertMessageFormat =
105 "test step %s, SkProxyCanvas indirect canvas state consistency";
106static const char* const kNWayStateAssertMessageFormat =
107 "test step %s, SkNWayCanvas state consistency";
108static const char* const kNWayIndirect1StateAssertMessageFormat =
109 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
110static const char* const kNWayIndirect2StateAssertMessageFormat =
111 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
112
113static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
114 bm->setConfig(config, kWidth, kHeight);
115 bm->allocPixels();
116 bm->eraseColor(color);
117}
118
119class CanvasTestStep;
120static SkTDArray<CanvasTestStep*>& testStepArray() {
121 static SkTDArray<CanvasTestStep*> theTests;
122 return theTests;
123}
124
125class CanvasTestStep {
126public:
127 CanvasTestStep() {
128 *testStepArray().append() = this;
129 fAssertMessageFormat = kDefaultAssertMessageFormat;
130 }
131
132 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
133 virtual const char* name() const = 0;
134
135 const char* assertMessage() {
136 fAssertMessage.printf(fAssertMessageFormat, name());
137 return fAssertMessage.c_str();
138 }
139
140 void setAssertMessageFormat(const char* format) {
141 fAssertMessageFormat = format;
142 }
143
144private:
145 SkString fAssertMessage;
146 const char* fAssertMessageFormat;
147};
148
149///////////////////////////////////////////////////////////////////////////////
150// Constants used by test steps
151
152const SkRect kTestRect =
153 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
154 SkIntToScalar(2), SkIntToScalar(1));
155static SkMatrix testMatrix() {
156 SkMatrix matrix;
157 matrix.reset();
158 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
159 return matrix;
160}
161const SkMatrix kTestMatrix = testMatrix();
162static SkPath testPath() {
163 SkPath path;
164 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
165 SkIntToScalar(2), SkIntToScalar(1)));
166 return path;
167}
168const SkPath kTestPath = testPath();
169static SkRegion testRegion() {
170 SkRegion region;
171 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
172 region.setRect(rect);
173 return region;
174}
175const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
176const SkRegion kTestRegion = testRegion();
177const SkColor kTestColor = 0x01020304;
178const SkPaint kTestPaint;
179const SkPoint kTestPoints[3] = {
180 {SkIntToScalar(0), SkIntToScalar(0)},
181 {SkIntToScalar(2), SkIntToScalar(1)},
182 {SkIntToScalar(0), SkIntToScalar(2)}
183};
184const size_t kTestPointCount = 3;
185static SkBitmap testBitmap() {
186 SkBitmap bitmap;
187 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
188 return bitmap;
189}
190SkBitmap kTestBitmap; // cannot be created during static init
191SkString kTestText("Hello World");
192SkPoint kTestPoint = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(1));
193
194///////////////////////////////////////////////////////////////////////////////
195// Macros for defining test steps
196
197#define TEST_STEP(NAME, FUNCTION) \
198class NAME##_TestStep : public CanvasTestStep{ \
199public: \
200 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
201 FUNCTION (canvas, reporter, this); \
202 } \
203 virtual const char* name() const {return #NAME ;} \
204}; \
205static NAME##_TestStep NAME##_TestStepInstance;
206
207#define SIMPLE_TEST_STEP(NAME, CALL) \
208static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
209 CanvasTestStep*) { \
210 canvas-> CALL ; \
211} \
212TEST_STEP(NAME, NAME##TestStep )
213
214#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
215static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
216 CanvasTestStep* testStep) { \
217 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
218 testStep->assertMessage()); \
219} \
220TEST_STEP(NAME, NAME##TestStep )
221
222
223///////////////////////////////////////////////////////////////////////////////
224// Basic test steps for most virtual methods in SkCanvas that draw or affect
225// the state of the canvas.
226
junov@chromium.orga907ac32012-02-24 21:54:07 +0000227SIMPLE_TEST_STEP(SaveMatrix, save(SkCanvas::kMatrix_SaveFlag));
228SIMPLE_TEST_STEP(SaveClip, save(SkCanvas::kClip_SaveFlag));
229SIMPLE_TEST_STEP(SaveMatrixClip, save(SkCanvas::kMatrixClip_SaveFlag));
230SIMPLE_TEST_STEP(SaveLayer, saveLayer(NULL, NULL));
231SIMPLE_TEST_STEP(BoundedSaveLayer, saveLayer(&kTestRect, NULL));
232SIMPLE_TEST_STEP(PaintSaveLayer, saveLayer(NULL, &kTestPaint));
233SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
234 translate(SkIntToScalar(1), SkIntToScalar(2)));
235SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
236 scale(SkIntToScalar(1), SkIntToScalar(2)));
237SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
238SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
239 skew(SkIntToScalar(1), SkIntToScalar(2)));
240SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
241SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
242SIMPLE_TEST_STEP_WITH_ASSERT(ClipRect, clipRect(kTestRect));
243SIMPLE_TEST_STEP_WITH_ASSERT(ClipPath, clipPath(kTestPath));
244SIMPLE_TEST_STEP_WITH_ASSERT(ClipRegion,
245 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000246SIMPLE_TEST_STEP(Clear, clear(kTestColor));
247SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
248SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
249 kTestPointCount, kTestPoints, kTestPaint));
250SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
251 kTestPointCount, kTestPoints, kTestPaint));
252SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
253 kTestPointCount, kTestPoints, kTestPaint));
254SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
255SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000256SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000257SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
258SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
259 NULL));
260SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
261 &kTestIRect, kTestRect, NULL));
262SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
263 kTestRect, &kTestPaint));
264SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
265 NULL));
266SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
267 kTestMatrix, &kTestPaint));
268SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
269 kTestRect, NULL));
270SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
271 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000272SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000273SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
274SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
275 0, 1, kTestPaint));
276SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
277 kTestText.size(), &kTestPoint, kTestPaint));
278SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
279 kTestText.size(), kTestPath, NULL, kTestPaint));
280SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
281 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
282SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
283SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
284
285///////////////////////////////////////////////////////////////////////////////
286// Complex test steps
287
288static void DrawVerticesShaderTestStep(SkCanvas* canvas,
289 skiatest::Reporter* reporter,
290 CanvasTestStep* testStep) {
291 SkPoint pts[4];
292 pts[0].set(0, 0);
293 pts[1].set(SkIntToScalar(kWidth), 0);
294 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
295 pts[3].set(0, SkIntToScalar(kHeight));
296 SkPaint paint;
297 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
298 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
299 paint.setShader(shader)->unref();
300 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
301 NULL, NULL, NULL, 0, paint);
302}
303TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
304
305static void DrawPictureTestStep(SkCanvas* canvas,
306 skiatest::Reporter* reporter,
307 CanvasTestStep* testStep) {
308 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
309 SkAutoUnref aup(testPicture);
310 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
311 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
312 testCanvas->clipRect(kTestRect);
313 testCanvas->drawRect(kTestRect, kTestPaint);
314 canvas->drawPicture(*testPicture);
315}
316TEST_STEP(DrawPicture, DrawPictureTestStep);
317
318static void SaveRestoreTestStep(SkCanvas* canvas,
319 skiatest::Reporter* reporter,
320 CanvasTestStep* testStep) {
321 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
322 testStep->assertMessage());
323 size_t n = canvas->save();
324 REPORTER_ASSERT_MESSAGE(reporter, 1 == n, testStep->assertMessage());
325 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
326 testStep->assertMessage());
327 canvas->save();
328 canvas->save();
329 REPORTER_ASSERT_MESSAGE(reporter, 4 == canvas->getSaveCount(),
330 testStep->assertMessage());
331 canvas->restoreToCount(2);
332 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
333 testStep->assertMessage());
334
335 // should this pin to 1, or be a no-op, or crash?
336 canvas->restoreToCount(0);
337 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
338 testStep->assertMessage());
339}
340TEST_STEP(SaveRestore, SaveRestoreTestStep);
341
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000342static void DrawLayerTestStep(SkCanvas* canvas,
343 skiatest::Reporter* reporter,
344 CanvasTestStep* testStep) {
345 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
346 testStep->assertMessage());
347 canvas->save();
348 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
349 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000350
351 const SkRect* bounds = NULL; // null means include entire bounds
352 const SkPaint* paint = NULL;
353
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000354 canvas->saveLayer(bounds, paint);
355 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
356 testStep->assertMessage());
357 canvas->restore();
358 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
359 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000360
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000361 canvas->saveLayer(bounds, paint);
362 canvas->saveLayer(bounds, paint);
363 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
364 testStep->assertMessage());
365 canvas->restore();
366 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
367 testStep->assertMessage());
368 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000369 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000370 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
371 testStep->assertMessage());
372}
373TEST_STEP(DrawLayer, DrawLayerTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000374
375static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
376 const SkCanvas* canvas1,
377 const SkCanvas* canvas2,
378 CanvasTestStep* testStep) {
379 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
380 canvas2->getDeviceSize(), testStep->assertMessage());
381 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
382 canvas2->getSaveCount(), testStep->assertMessage());
383 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
384 canvas2->isDrawingToLayer(), testStep->assertMessage());
385 SkRect bounds1, bounds2;
386 REPORTER_ASSERT_MESSAGE(reporter,
387 canvas1->getClipBounds(&bounds1, SkCanvas::kAA_EdgeType) ==
388 canvas2->getClipBounds(&bounds2, SkCanvas::kAA_EdgeType),
389 testStep->assertMessage());
390 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
391 testStep->assertMessage());
392 REPORTER_ASSERT_MESSAGE(reporter,
393 canvas1->getClipBounds(&bounds1, SkCanvas::kBW_EdgeType) ==
394 canvas2->getClipBounds(&bounds2, SkCanvas::kBW_EdgeType),
395 testStep->assertMessage());
396 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
397 testStep->assertMessage());
398 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
399 canvas2->getDrawFilter(), testStep->assertMessage());
400 SkIRect deviceBounds1, deviceBounds2;
401 REPORTER_ASSERT_MESSAGE(reporter,
402 canvas1->getClipDeviceBounds(&deviceBounds1) ==
403 canvas2->getClipDeviceBounds(&deviceBounds2),
404 testStep->assertMessage());
405 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
406 testStep->assertMessage());
407 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
408 canvas2->getBounder(), testStep->assertMessage());
409 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
410 canvas2->getTotalMatrix(), testStep->assertMessage());
411 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
412 canvas2->getClipType(), testStep->assertMessage());
413 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
414 canvas2->getTotalClip(), testStep->assertMessage());
415 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClipStack() ==
416 canvas2->getTotalClipStack(), testStep->assertMessage());
417
418 // The following test code is commented out because the test fails when
419 // the canvas is an SkPictureRecord or SkDeferredCanvas
420 // Issue: http://code.google.com/p/skia/issues/detail?id=498
421 // Also, creating a LayerIter on an SkProxyCanvas crashes
422 // Issue: http://code.google.com/p/skia/issues/detail?id=499
423 /*
424 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
425 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
426 while (!layerIter1.done() && !layerIter2.done()) {
427 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
428 layerIter2.matrix(), testStep->assertMessage());
429 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
430 layerIter2.clip(), testStep->assertMessage());
431 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
432 layerIter2.paint(), testStep->assertMessage());
433 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
434 layerIter2.x(), testStep->assertMessage());
435 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
436 layerIter2.y(), testStep->assertMessage());
437 layerIter1.next();
438 layerIter2.next();
439 }
440 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
441 testStep->assertMessage());
442 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
443 testStep->assertMessage());
444 */
445}
446
447// The following class groups static functions that need to access
448// the privates members of SkPictureRecord
449class SkPictureTester {
450private:
451 static void AssertFlattenedObjectsEqual(
452 SkPictureRecord* referenceRecord,
453 SkPictureRecord* testRecord,
454 skiatest::Reporter* reporter,
455 CanvasTestStep* testStep) {
456
457 REPORTER_ASSERT_MESSAGE(reporter,
458 referenceRecord->fBitmaps.count() ==
459 testRecord->fBitmaps.count(), testStep->assertMessage());
460 for (int i = 0; i < referenceRecord->fBitmaps.count(); ++i) {
461 REPORTER_ASSERT_MESSAGE(reporter,
462 SkFlatData::Compare(referenceRecord->fBitmaps[i],
463 testRecord->fBitmaps[i]) == 0, testStep->assertMessage());
464 }
465 REPORTER_ASSERT_MESSAGE(reporter,
466 referenceRecord->fMatrices.count() ==
467 testRecord->fMatrices.count(), testStep->assertMessage());
468 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
469 REPORTER_ASSERT_MESSAGE(reporter,
470 SkFlatData::Compare(referenceRecord->fMatrices[i],
471 testRecord->fMatrices[i]) == 0,
472 testStep->assertMessage());
473 }
474 REPORTER_ASSERT_MESSAGE(reporter,
475 referenceRecord->fPaints.count() ==
476 testRecord->fPaints.count(), testStep->assertMessage());
477 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
478 REPORTER_ASSERT_MESSAGE(reporter,
479 SkFlatData::Compare(referenceRecord->fPaints[i],
480 testRecord->fPaints[i]) == 0, testStep->assertMessage());
481 }
482 REPORTER_ASSERT_MESSAGE(reporter,
483 referenceRecord->fRegions.count() ==
484 testRecord->fRegions.count(), testStep->assertMessage());
485 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
486 REPORTER_ASSERT_MESSAGE(reporter,
487 SkFlatData::Compare(referenceRecord->fRegions[i],
488 testRecord->fRegions[i]) == 0, testStep->assertMessage());
489 }
490 REPORTER_ASSERT_MESSAGE(reporter,
491 !referenceRecord->fPathHeap ==
492 !testRecord->fPathHeap,
493 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000494 // The following tests are commented out because they currently
495 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
496 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000497 if (referenceRecord->fPathHeap) {
498 REPORTER_ASSERT_MESSAGE(reporter,
499 referenceRecord->fPathHeap->count() ==
500 testRecord->fPathHeap->count(),
501 testStep->assertMessage());
502 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
503 REPORTER_ASSERT_MESSAGE(reporter,
504 (*referenceRecord->fPathHeap)[i] ==
505 (*testRecord->fPathHeap)[i], testStep->assertMessage());
506 }
507 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000508 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509
510 }
511
512public:
513
514 static void TestPictureSerializationRoundTrip(skiatest::Reporter* reporter,
515 CanvasTestStep* testStep) {
516 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
517 SkPicture referencePicture;
518 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
519 reporter);
520 SkPicture initialPicture;
521 testStep->draw(initialPicture.beginRecording(kWidth, kHeight),
522 reporter);
523 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
524 SkPicture roundTripPicture;
525 initialPicture.draw(roundTripPicture.beginRecording(kWidth, kHeight));
526
527 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
528 referencePicture.getRecordingCanvas());
529 SkPictureRecord* roundTripRecord = static_cast<SkPictureRecord*>(
530 roundTripPicture.getRecordingCanvas());
531
532 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
533
534 // Verify that deserialization-serialization round trip conserves all
535 // data by comparing referenceRecord to roundTripRecord
536 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fBitmapIndex ==
537 roundTripRecord->fBitmapIndex, testStep->assertMessage());
538 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fMatrixIndex ==
539 roundTripRecord->fMatrixIndex, testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fPaintIndex ==
541 roundTripRecord->fPaintIndex, testStep->assertMessage());
542 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRegionIndex ==
543 roundTripRecord->fRegionIndex, testStep->assertMessage());
544 char referenceBuffer[kMaxPictureBufferSize];
545 SkMemoryWStream referenceStream(referenceBuffer,
546 kMaxPictureBufferSize);
547 referenceRecord->fWriter.writeToStream(&referenceStream);
548 char roundTripBuffer[kMaxPictureBufferSize];
549 SkMemoryWStream roundTripStream(roundTripBuffer,
550 kMaxPictureBufferSize);
551 roundTripRecord->fWriter.writeToStream(&roundTripStream);
552 REPORTER_ASSERT_MESSAGE(reporter,
553 roundTripStream.bytesWritten() == referenceStream.bytesWritten(),
554 testStep->assertMessage());
555 REPORTER_ASSERT_MESSAGE(reporter, 0 == memcmp(referenceBuffer,
556 roundTripBuffer, roundTripStream.bytesWritten()),
557 testStep->assertMessage());
558 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRecordFlags ==
559 roundTripRecord->fRecordFlags, testStep->assertMessage());
560 REPORTER_ASSERT_MESSAGE(reporter,
561 referenceRecord->fRestoreOffsetStack ==
562 roundTripRecord->fRestoreOffsetStack,
563 testStep->assertMessage());
564 AssertFlattenedObjectsEqual(referenceRecord, roundTripRecord,
565 reporter, testStep);
566 AssertCanvasStatesEqual(reporter, referenceRecord, roundTripRecord,
567 testStep);
568 }
569
570 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
571 CanvasTestStep* testStep) {
572 // Verify that when a test step is executed twice, no extra resources
573 // are flattened during the second execution
574 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
575 SkPicture referencePicture;
576 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
577 kHeight);
578 testStep->draw(referenceCanvas, reporter);
579 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000580 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000581 kHeight);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000582 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000583 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000584 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000585
586 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
587 referenceCanvas);
588 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
589 testCanvas);
590 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
591 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000592 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000593 }
594};
595
596static void TestPictureStateConsistency(skiatest::Reporter* reporter,
597 CanvasTestStep* testStep,
598 const SkCanvas& referenceCanvas) {
599 // Verify that the recording canvas's state is consistent
600 // with that of a regular canvas
601 SkPicture testPicture;
602 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
603 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
604 testStep->draw(pictureCanvas, reporter);
605 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
606 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
607 testStep);
608
609 SkBitmap playbackStore;
610 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
611 SkDevice playbackDevice(playbackStore);
612 SkCanvas playbackCanvas(&playbackDevice);
613 testPicture.draw(&playbackCanvas);
614 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
615 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
616 testStep);
617
618 // The following test code is commented out because SkPicture is not
619 // currently expected to preserve state when restarting recording.
620 /*
621 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
622 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
623 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
624 testStep);
625 */
626}
627
628static void TestDeferredCanvasStateConsistency(
629 skiatest::Reporter* reporter,
630 CanvasTestStep* testStep,
631 const SkCanvas& referenceCanvas) {
632
633 SkBitmap deferredStore;
634 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
635 SkDevice deferredDevice(deferredStore);
636 SkDeferredCanvas deferredCanvas(&deferredDevice);
637 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
638 testStep->draw(&deferredCanvas, reporter);
639 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
640 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
641 testStep);
642
643 // Verified that deferred canvas state is not affected by flushing
644 // pending draw operations
645
646 // The following test code is commented out because it currently fails.
647 // Issue: http://code.google.com/p/skia/issues/detail?id=496
648 /*
649 deferredCanvas.flush();
650 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
651 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
652 testStep);
653 */
654}
655
656static void TestProxyCanvasStateConsistency(
657 skiatest::Reporter* reporter,
658 CanvasTestStep* testStep,
659 const SkCanvas& referenceCanvas) {
660
661 SkBitmap indirectStore;
662 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
663 SkDevice indirectDevice(indirectStore);
664 SkCanvas indirectCanvas(&indirectDevice);
665 SkProxyCanvas proxyCanvas(&indirectCanvas);
666 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
667 testStep->draw(&proxyCanvas, reporter);
668 // Verify that the SkProxyCanvas reports consitent state
669 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
670 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
671 testStep);
672 // Verify that the indirect canvas reports consitent state
673 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
674 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
675 testStep);
676}
677
678static void TestNWayCanvasStateConsistency(
679 skiatest::Reporter* reporter,
680 CanvasTestStep* testStep,
681 const SkCanvas& referenceCanvas) {
682
683 SkBitmap indirectStore1;
684 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
685 SkDevice indirectDevice1(indirectStore1);
686 SkCanvas indirectCanvas1(&indirectDevice1);
687
688 SkBitmap indirectStore2;
689 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
690 SkDevice indirectDevice2(indirectStore2);
691 SkCanvas indirectCanvas2(&indirectDevice2);
692
693 SkNWayCanvas nWayCanvas;
694 nWayCanvas.addCanvas(&indirectCanvas1);
695 nWayCanvas.addCanvas(&indirectCanvas2);
696
697 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
698 testStep->draw(&nWayCanvas, reporter);
699 // Verify that the SkProxyCanvas reports consitent state
700 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
701 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
702 testStep);
703 // Verify that the indirect canvases report consitent state
704 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
705 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
706 testStep);
707 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
708 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
709 testStep);
710}
711
712/*
713 * This sub-test verifies that the test step passes when executed
714 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
715 * that the all canvas derivatives report the same state as an SkCanvas
716 * after having executed the test step.
717 */
718static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
719 CanvasTestStep* testStep) {
720 SkBitmap referenceStore;
721 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
722 SkDevice referenceDevice(referenceStore);
723 SkCanvas referenceCanvas(&referenceDevice);
724 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
725 testStep->draw(&referenceCanvas, reporter);
726
727 TestPictureStateConsistency(reporter, testStep, referenceCanvas);
728 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
729
730 // The following test code is commented out because SkProxyCanvas is
731 // missing a lot of virtual overrides on get* methods, which are used
732 // to verify canvas state.
733 // Issue: http://code.google.com/p/skia/issues/detail?id=500
734
735 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
736
737 // The following test code is commented out because SkNWayCanvas does not
738 // report correct clipping and device bounds information
739 // Issue: http://code.google.com/p/skia/issues/detail?id=501
740
741 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000742}
reed@google.com37f3ae02011-11-28 16:06:04 +0000743
744static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000745 // Init global here because bitmap pixels cannot be alocated during
746 // static initialization
747 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000748
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000749 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
750 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
751 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
752 testStepArray()[testStep]);
753 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
754 testStepArray()[testStep]);
755 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000756}
757
758#include "TestClassDef.h"
759DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)