blob: 099146f2ddd033809eeed20921e2ba4b0e6d31f2 [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
227// The following test steps are commented-out because they currently fail
228// Issue: http://code.google.com/p/skia/issues/detail?id=506
229//SIMPLE_TEST_STEP(SaveMatrix, save(SkCanvas::kMatrix_SaveFlag));
230//SIMPLE_TEST_STEP(SaveClip, save(SkCanvas::kClip_SaveFlag));
231//SIMPLE_TEST_STEP(SaveMatrixClip, save(SkCanvas::kMatrixClip_SaveFlag));
232//SIMPLE_TEST_STEP(SaveLayer, saveLayer(NULL, NULL));
233//SIMPLE_TEST_STEP(BoundedSaveLayer, saveLayer(&kTestRect, NULL));
234//SIMPLE_TEST_STEP(PaintSaveLayer, saveLayer(NULL, &kTestPaint));
235//SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
236// translate(SkIntToScalar(1), SkIntToScalar(2)));
237//SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
238// scale(SkIntToScalar(1), SkIntToScalar(2)));
239//SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
240//SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
241// skew(SkIntToScalar(1), SkIntToScalar(2)));
242//SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
243//SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
244//SIMPLE_TEST_STEP_WITH_ASSERT(ClipRect, clipRect(kTestRect));
245//SIMPLE_TEST_STEP_WITH_ASSERT(ClipPath, clipPath(kTestPath));
246//SIMPLE_TEST_STEP_WITH_ASSERT(ClipRegion,
247// clipRegion(kTestRegion, SkRegion::kReplace_Op));
248SIMPLE_TEST_STEP(Clear, clear(kTestColor));
249SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
250SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
251 kTestPointCount, kTestPoints, kTestPaint));
252SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
253 kTestPointCount, kTestPoints, kTestPaint));
254SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
255 kTestPointCount, kTestPoints, kTestPaint));
256SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
257SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
258// The following test step is commented-out because it crashes SkDeferredCanvas
259// Issue: http://code.google.com/p/skia/issues/detail?id=505
260//SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
261SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
262SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
263 NULL));
264SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
265 &kTestIRect, kTestRect, NULL));
266SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
267 kTestRect, &kTestPaint));
268SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
269 NULL));
270SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
271 kTestMatrix, &kTestPaint));
272SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
273 kTestRect, NULL));
274SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
275 kTestRect, &kTestPaint));
276// The following test step is commented-out because it crashes SkDeferredCanvas
277// Issue: http://code.google.com/p/skia/issues/detail?id=505
278//SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
279SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
280SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
281 0, 1, kTestPaint));
282SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
283 kTestText.size(), &kTestPoint, kTestPaint));
284SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
285 kTestText.size(), kTestPath, NULL, kTestPaint));
286SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
287 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
288SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
289SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
290
291///////////////////////////////////////////////////////////////////////////////
292// Complex test steps
293
294static void DrawVerticesShaderTestStep(SkCanvas* canvas,
295 skiatest::Reporter* reporter,
296 CanvasTestStep* testStep) {
297 SkPoint pts[4];
298 pts[0].set(0, 0);
299 pts[1].set(SkIntToScalar(kWidth), 0);
300 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
301 pts[3].set(0, SkIntToScalar(kHeight));
302 SkPaint paint;
303 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
304 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
305 paint.setShader(shader)->unref();
306 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
307 NULL, NULL, NULL, 0, paint);
308}
309TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
310
311static void DrawPictureTestStep(SkCanvas* canvas,
312 skiatest::Reporter* reporter,
313 CanvasTestStep* testStep) {
314 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
315 SkAutoUnref aup(testPicture);
316 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
317 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
318 testCanvas->clipRect(kTestRect);
319 testCanvas->drawRect(kTestRect, kTestPaint);
320 canvas->drawPicture(*testPicture);
321}
322TEST_STEP(DrawPicture, DrawPictureTestStep);
323
324static void SaveRestoreTestStep(SkCanvas* canvas,
325 skiatest::Reporter* reporter,
326 CanvasTestStep* testStep) {
327 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
328 testStep->assertMessage());
329 size_t n = canvas->save();
330 REPORTER_ASSERT_MESSAGE(reporter, 1 == n, testStep->assertMessage());
331 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
332 testStep->assertMessage());
333 canvas->save();
334 canvas->save();
335 REPORTER_ASSERT_MESSAGE(reporter, 4 == canvas->getSaveCount(),
336 testStep->assertMessage());
337 canvas->restoreToCount(2);
338 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
339 testStep->assertMessage());
340
341 // should this pin to 1, or be a no-op, or crash?
342 canvas->restoreToCount(0);
343 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
344 testStep->assertMessage());
345}
346TEST_STEP(SaveRestore, SaveRestoreTestStep);
347
348// The following test step is commented-out because it currently fails
349// Issue: http://code.google.com/p/skia/issues/detail?id=506
350/*
351static void DrawLayerTestStep(SkCanvas* canvas,
352 skiatest::Reporter* reporter,
353 CanvasTestStep* testStep) {
354 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
355 testStep->assertMessage());
356 canvas->save();
357 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
358 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000359
360 const SkRect* bounds = NULL; // null means include entire bounds
361 const SkPaint* paint = NULL;
362
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000363 canvas->saveLayer(bounds, paint);
364 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
365 testStep->assertMessage());
366 canvas->restore();
367 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
368 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000369
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000370 canvas->saveLayer(bounds, paint);
371 canvas->saveLayer(bounds, paint);
372 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
373 testStep->assertMessage());
374 canvas->restore();
375 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
376 testStep->assertMessage());
377 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000378 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000379 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
380 testStep->assertMessage());
381}
382TEST_STEP(DrawLayer, DrawLayerTestStep);
383*/
384
385static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
386 const SkCanvas* canvas1,
387 const SkCanvas* canvas2,
388 CanvasTestStep* testStep) {
389 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
390 canvas2->getDeviceSize(), testStep->assertMessage());
391 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
392 canvas2->getSaveCount(), testStep->assertMessage());
393 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
394 canvas2->isDrawingToLayer(), testStep->assertMessage());
395 SkRect bounds1, bounds2;
396 REPORTER_ASSERT_MESSAGE(reporter,
397 canvas1->getClipBounds(&bounds1, SkCanvas::kAA_EdgeType) ==
398 canvas2->getClipBounds(&bounds2, SkCanvas::kAA_EdgeType),
399 testStep->assertMessage());
400 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
401 testStep->assertMessage());
402 REPORTER_ASSERT_MESSAGE(reporter,
403 canvas1->getClipBounds(&bounds1, SkCanvas::kBW_EdgeType) ==
404 canvas2->getClipBounds(&bounds2, SkCanvas::kBW_EdgeType),
405 testStep->assertMessage());
406 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
407 testStep->assertMessage());
408 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
409 canvas2->getDrawFilter(), testStep->assertMessage());
410 SkIRect deviceBounds1, deviceBounds2;
411 REPORTER_ASSERT_MESSAGE(reporter,
412 canvas1->getClipDeviceBounds(&deviceBounds1) ==
413 canvas2->getClipDeviceBounds(&deviceBounds2),
414 testStep->assertMessage());
415 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
416 testStep->assertMessage());
417 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
418 canvas2->getBounder(), testStep->assertMessage());
419 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
420 canvas2->getTotalMatrix(), testStep->assertMessage());
421 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
422 canvas2->getClipType(), testStep->assertMessage());
423 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
424 canvas2->getTotalClip(), testStep->assertMessage());
425 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClipStack() ==
426 canvas2->getTotalClipStack(), testStep->assertMessage());
427
428 // The following test code is commented out because the test fails when
429 // the canvas is an SkPictureRecord or SkDeferredCanvas
430 // Issue: http://code.google.com/p/skia/issues/detail?id=498
431 // Also, creating a LayerIter on an SkProxyCanvas crashes
432 // Issue: http://code.google.com/p/skia/issues/detail?id=499
433 /*
434 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
435 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
436 while (!layerIter1.done() && !layerIter2.done()) {
437 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
438 layerIter2.matrix(), testStep->assertMessage());
439 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
440 layerIter2.clip(), testStep->assertMessage());
441 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
442 layerIter2.paint(), testStep->assertMessage());
443 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
444 layerIter2.x(), testStep->assertMessage());
445 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
446 layerIter2.y(), testStep->assertMessage());
447 layerIter1.next();
448 layerIter2.next();
449 }
450 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
451 testStep->assertMessage());
452 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
453 testStep->assertMessage());
454 */
455}
456
457// The following class groups static functions that need to access
458// the privates members of SkPictureRecord
459class SkPictureTester {
460private:
461 static void AssertFlattenedObjectsEqual(
462 SkPictureRecord* referenceRecord,
463 SkPictureRecord* testRecord,
464 skiatest::Reporter* reporter,
465 CanvasTestStep* testStep) {
466
467 REPORTER_ASSERT_MESSAGE(reporter,
468 referenceRecord->fBitmaps.count() ==
469 testRecord->fBitmaps.count(), testStep->assertMessage());
470 for (int i = 0; i < referenceRecord->fBitmaps.count(); ++i) {
471 REPORTER_ASSERT_MESSAGE(reporter,
472 SkFlatData::Compare(referenceRecord->fBitmaps[i],
473 testRecord->fBitmaps[i]) == 0, testStep->assertMessage());
474 }
475 REPORTER_ASSERT_MESSAGE(reporter,
476 referenceRecord->fMatrices.count() ==
477 testRecord->fMatrices.count(), testStep->assertMessage());
478 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
479 REPORTER_ASSERT_MESSAGE(reporter,
480 SkFlatData::Compare(referenceRecord->fMatrices[i],
481 testRecord->fMatrices[i]) == 0,
482 testStep->assertMessage());
483 }
484 REPORTER_ASSERT_MESSAGE(reporter,
485 referenceRecord->fPaints.count() ==
486 testRecord->fPaints.count(), testStep->assertMessage());
487 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
488 REPORTER_ASSERT_MESSAGE(reporter,
489 SkFlatData::Compare(referenceRecord->fPaints[i],
490 testRecord->fPaints[i]) == 0, testStep->assertMessage());
491 }
492 REPORTER_ASSERT_MESSAGE(reporter,
493 referenceRecord->fRegions.count() ==
494 testRecord->fRegions.count(), testStep->assertMessage());
495 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
496 REPORTER_ASSERT_MESSAGE(reporter,
497 SkFlatData::Compare(referenceRecord->fRegions[i],
498 testRecord->fRegions[i]) == 0, testStep->assertMessage());
499 }
500 REPORTER_ASSERT_MESSAGE(reporter,
501 !referenceRecord->fPathHeap ==
502 !testRecord->fPathHeap,
503 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000504 // The following tests are commented out because they currently
505 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
506 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000507 if (referenceRecord->fPathHeap) {
508 REPORTER_ASSERT_MESSAGE(reporter,
509 referenceRecord->fPathHeap->count() ==
510 testRecord->fPathHeap->count(),
511 testStep->assertMessage());
512 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
513 REPORTER_ASSERT_MESSAGE(reporter,
514 (*referenceRecord->fPathHeap)[i] ==
515 (*testRecord->fPathHeap)[i], testStep->assertMessage());
516 }
517 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000518 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000519
520 }
521
522public:
523
524 static void TestPictureSerializationRoundTrip(skiatest::Reporter* reporter,
525 CanvasTestStep* testStep) {
526 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
527 SkPicture referencePicture;
528 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
529 reporter);
530 SkPicture initialPicture;
531 testStep->draw(initialPicture.beginRecording(kWidth, kHeight),
532 reporter);
533 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
534 SkPicture roundTripPicture;
535 initialPicture.draw(roundTripPicture.beginRecording(kWidth, kHeight));
536
537 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
538 referencePicture.getRecordingCanvas());
539 SkPictureRecord* roundTripRecord = static_cast<SkPictureRecord*>(
540 roundTripPicture.getRecordingCanvas());
541
542 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
543
544 // Verify that deserialization-serialization round trip conserves all
545 // data by comparing referenceRecord to roundTripRecord
546 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fBitmapIndex ==
547 roundTripRecord->fBitmapIndex, testStep->assertMessage());
548 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fMatrixIndex ==
549 roundTripRecord->fMatrixIndex, testStep->assertMessage());
550 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fPaintIndex ==
551 roundTripRecord->fPaintIndex, testStep->assertMessage());
552 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRegionIndex ==
553 roundTripRecord->fRegionIndex, testStep->assertMessage());
554 char referenceBuffer[kMaxPictureBufferSize];
555 SkMemoryWStream referenceStream(referenceBuffer,
556 kMaxPictureBufferSize);
557 referenceRecord->fWriter.writeToStream(&referenceStream);
558 char roundTripBuffer[kMaxPictureBufferSize];
559 SkMemoryWStream roundTripStream(roundTripBuffer,
560 kMaxPictureBufferSize);
561 roundTripRecord->fWriter.writeToStream(&roundTripStream);
562 REPORTER_ASSERT_MESSAGE(reporter,
563 roundTripStream.bytesWritten() == referenceStream.bytesWritten(),
564 testStep->assertMessage());
565 REPORTER_ASSERT_MESSAGE(reporter, 0 == memcmp(referenceBuffer,
566 roundTripBuffer, roundTripStream.bytesWritten()),
567 testStep->assertMessage());
568 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRecordFlags ==
569 roundTripRecord->fRecordFlags, testStep->assertMessage());
570 REPORTER_ASSERT_MESSAGE(reporter,
571 referenceRecord->fRestoreOffsetStack ==
572 roundTripRecord->fRestoreOffsetStack,
573 testStep->assertMessage());
574 AssertFlattenedObjectsEqual(referenceRecord, roundTripRecord,
575 reporter, testStep);
576 AssertCanvasStatesEqual(reporter, referenceRecord, roundTripRecord,
577 testStep);
578 }
579
580 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
581 CanvasTestStep* testStep) {
582 // Verify that when a test step is executed twice, no extra resources
583 // are flattened during the second execution
584 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
585 SkPicture referencePicture;
586 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
587 kHeight);
588 testStep->draw(referenceCanvas, reporter);
589 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000590 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 kHeight);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000592 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000593 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000594 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000595
596 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
597 referenceCanvas);
598 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
599 testCanvas);
600 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
601 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000602 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603 }
604};
605
606static void TestPictureStateConsistency(skiatest::Reporter* reporter,
607 CanvasTestStep* testStep,
608 const SkCanvas& referenceCanvas) {
609 // Verify that the recording canvas's state is consistent
610 // with that of a regular canvas
611 SkPicture testPicture;
612 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
613 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
614 testStep->draw(pictureCanvas, reporter);
615 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
616 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
617 testStep);
618
619 SkBitmap playbackStore;
620 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
621 SkDevice playbackDevice(playbackStore);
622 SkCanvas playbackCanvas(&playbackDevice);
623 testPicture.draw(&playbackCanvas);
624 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
625 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
626 testStep);
627
628 // The following test code is commented out because SkPicture is not
629 // currently expected to preserve state when restarting recording.
630 /*
631 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
632 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
633 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
634 testStep);
635 */
636}
637
638static void TestDeferredCanvasStateConsistency(
639 skiatest::Reporter* reporter,
640 CanvasTestStep* testStep,
641 const SkCanvas& referenceCanvas) {
642
643 SkBitmap deferredStore;
644 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
645 SkDevice deferredDevice(deferredStore);
646 SkDeferredCanvas deferredCanvas(&deferredDevice);
647 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
648 testStep->draw(&deferredCanvas, reporter);
649 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
650 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
651 testStep);
652
653 // Verified that deferred canvas state is not affected by flushing
654 // pending draw operations
655
656 // The following test code is commented out because it currently fails.
657 // Issue: http://code.google.com/p/skia/issues/detail?id=496
658 /*
659 deferredCanvas.flush();
660 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
661 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
662 testStep);
663 */
664}
665
666static void TestProxyCanvasStateConsistency(
667 skiatest::Reporter* reporter,
668 CanvasTestStep* testStep,
669 const SkCanvas& referenceCanvas) {
670
671 SkBitmap indirectStore;
672 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
673 SkDevice indirectDevice(indirectStore);
674 SkCanvas indirectCanvas(&indirectDevice);
675 SkProxyCanvas proxyCanvas(&indirectCanvas);
676 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
677 testStep->draw(&proxyCanvas, reporter);
678 // Verify that the SkProxyCanvas reports consitent state
679 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
680 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
681 testStep);
682 // Verify that the indirect canvas reports consitent state
683 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
684 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
685 testStep);
686}
687
688static void TestNWayCanvasStateConsistency(
689 skiatest::Reporter* reporter,
690 CanvasTestStep* testStep,
691 const SkCanvas& referenceCanvas) {
692
693 SkBitmap indirectStore1;
694 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
695 SkDevice indirectDevice1(indirectStore1);
696 SkCanvas indirectCanvas1(&indirectDevice1);
697
698 SkBitmap indirectStore2;
699 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
700 SkDevice indirectDevice2(indirectStore2);
701 SkCanvas indirectCanvas2(&indirectDevice2);
702
703 SkNWayCanvas nWayCanvas;
704 nWayCanvas.addCanvas(&indirectCanvas1);
705 nWayCanvas.addCanvas(&indirectCanvas2);
706
707 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
708 testStep->draw(&nWayCanvas, reporter);
709 // Verify that the SkProxyCanvas reports consitent state
710 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
711 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
712 testStep);
713 // Verify that the indirect canvases report consitent state
714 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
715 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
716 testStep);
717 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
718 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
719 testStep);
720}
721
722/*
723 * This sub-test verifies that the test step passes when executed
724 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
725 * that the all canvas derivatives report the same state as an SkCanvas
726 * after having executed the test step.
727 */
728static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
729 CanvasTestStep* testStep) {
730 SkBitmap referenceStore;
731 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
732 SkDevice referenceDevice(referenceStore);
733 SkCanvas referenceCanvas(&referenceDevice);
734 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
735 testStep->draw(&referenceCanvas, reporter);
736
737 TestPictureStateConsistency(reporter, testStep, referenceCanvas);
738 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
739
740 // The following test code is commented out because SkProxyCanvas is
741 // missing a lot of virtual overrides on get* methods, which are used
742 // to verify canvas state.
743 // Issue: http://code.google.com/p/skia/issues/detail?id=500
744
745 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
746
747 // The following test code is commented out because SkNWayCanvas does not
748 // report correct clipping and device bounds information
749 // Issue: http://code.google.com/p/skia/issues/detail?id=501
750
751 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000752}
reed@google.com37f3ae02011-11-28 16:06:04 +0000753
754static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000755 // Init global here because bitmap pixels cannot be alocated during
756 // static initialization
757 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000758
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000759 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
760 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
761 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
762 testStepArray()[testStep]);
763 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
764 testStepArray()[testStep]);
765 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000766}
767
768#include "TestClassDef.h"
769DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)