blob: 0199606a3f2ee053b9954a81b133364f3989293d [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());
504 if (referenceRecord->fPathHeap) {
505 REPORTER_ASSERT_MESSAGE(reporter,
506 referenceRecord->fPathHeap->count() ==
507 testRecord->fPathHeap->count(),
508 testStep->assertMessage());
509 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
510 REPORTER_ASSERT_MESSAGE(reporter,
511 (*referenceRecord->fPathHeap)[i] ==
512 (*testRecord->fPathHeap)[i], testStep->assertMessage());
513 }
514 }
515
516 }
517
518public:
519
520 static void TestPictureSerializationRoundTrip(skiatest::Reporter* reporter,
521 CanvasTestStep* testStep) {
522 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
523 SkPicture referencePicture;
524 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
525 reporter);
526 SkPicture initialPicture;
527 testStep->draw(initialPicture.beginRecording(kWidth, kHeight),
528 reporter);
529 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
530 SkPicture roundTripPicture;
531 initialPicture.draw(roundTripPicture.beginRecording(kWidth, kHeight));
532
533 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
534 referencePicture.getRecordingCanvas());
535 SkPictureRecord* roundTripRecord = static_cast<SkPictureRecord*>(
536 roundTripPicture.getRecordingCanvas());
537
538 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
539
540 // Verify that deserialization-serialization round trip conserves all
541 // data by comparing referenceRecord to roundTripRecord
542 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fBitmapIndex ==
543 roundTripRecord->fBitmapIndex, testStep->assertMessage());
544 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fMatrixIndex ==
545 roundTripRecord->fMatrixIndex, testStep->assertMessage());
546 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fPaintIndex ==
547 roundTripRecord->fPaintIndex, testStep->assertMessage());
548 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRegionIndex ==
549 roundTripRecord->fRegionIndex, testStep->assertMessage());
550 char referenceBuffer[kMaxPictureBufferSize];
551 SkMemoryWStream referenceStream(referenceBuffer,
552 kMaxPictureBufferSize);
553 referenceRecord->fWriter.writeToStream(&referenceStream);
554 char roundTripBuffer[kMaxPictureBufferSize];
555 SkMemoryWStream roundTripStream(roundTripBuffer,
556 kMaxPictureBufferSize);
557 roundTripRecord->fWriter.writeToStream(&roundTripStream);
558 REPORTER_ASSERT_MESSAGE(reporter,
559 roundTripStream.bytesWritten() == referenceStream.bytesWritten(),
560 testStep->assertMessage());
561 REPORTER_ASSERT_MESSAGE(reporter, 0 == memcmp(referenceBuffer,
562 roundTripBuffer, roundTripStream.bytesWritten()),
563 testStep->assertMessage());
564 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRecordFlags ==
565 roundTripRecord->fRecordFlags, testStep->assertMessage());
566 REPORTER_ASSERT_MESSAGE(reporter,
567 referenceRecord->fRestoreOffsetStack ==
568 roundTripRecord->fRestoreOffsetStack,
569 testStep->assertMessage());
570 AssertFlattenedObjectsEqual(referenceRecord, roundTripRecord,
571 reporter, testStep);
572 AssertCanvasStatesEqual(reporter, referenceRecord, roundTripRecord,
573 testStep);
574 }
575
576 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
577 CanvasTestStep* testStep) {
578 // Verify that when a test step is executed twice, no extra resources
579 // are flattened during the second execution
580 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
581 SkPicture referencePicture;
582 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
583 kHeight);
584 testStep->draw(referenceCanvas, reporter);
585 SkPicture testPicture;
586 SkCanvas* testCanvas = referencePicture.beginRecording(kWidth,
587 kHeight);
588 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
589 reporter);
590 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
591 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
592 reporter);
593
594 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
595 referenceCanvas);
596 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
597 testCanvas);
598 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
599 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
600 reporter, testStep);
601 }
602};
603
604static void TestPictureStateConsistency(skiatest::Reporter* reporter,
605 CanvasTestStep* testStep,
606 const SkCanvas& referenceCanvas) {
607 // Verify that the recording canvas's state is consistent
608 // with that of a regular canvas
609 SkPicture testPicture;
610 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
611 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
612 testStep->draw(pictureCanvas, reporter);
613 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
614 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
615 testStep);
616
617 SkBitmap playbackStore;
618 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
619 SkDevice playbackDevice(playbackStore);
620 SkCanvas playbackCanvas(&playbackDevice);
621 testPicture.draw(&playbackCanvas);
622 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
623 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
624 testStep);
625
626 // The following test code is commented out because SkPicture is not
627 // currently expected to preserve state when restarting recording.
628 /*
629 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
630 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
631 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
632 testStep);
633 */
634}
635
636static void TestDeferredCanvasStateConsistency(
637 skiatest::Reporter* reporter,
638 CanvasTestStep* testStep,
639 const SkCanvas& referenceCanvas) {
640
641 SkBitmap deferredStore;
642 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
643 SkDevice deferredDevice(deferredStore);
644 SkDeferredCanvas deferredCanvas(&deferredDevice);
645 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
646 testStep->draw(&deferredCanvas, reporter);
647 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
648 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
649 testStep);
650
651 // Verified that deferred canvas state is not affected by flushing
652 // pending draw operations
653
654 // The following test code is commented out because it currently fails.
655 // Issue: http://code.google.com/p/skia/issues/detail?id=496
656 /*
657 deferredCanvas.flush();
658 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
659 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
660 testStep);
661 */
662}
663
664static void TestProxyCanvasStateConsistency(
665 skiatest::Reporter* reporter,
666 CanvasTestStep* testStep,
667 const SkCanvas& referenceCanvas) {
668
669 SkBitmap indirectStore;
670 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
671 SkDevice indirectDevice(indirectStore);
672 SkCanvas indirectCanvas(&indirectDevice);
673 SkProxyCanvas proxyCanvas(&indirectCanvas);
674 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
675 testStep->draw(&proxyCanvas, reporter);
676 // Verify that the SkProxyCanvas reports consitent state
677 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
678 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
679 testStep);
680 // Verify that the indirect canvas reports consitent state
681 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
682 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
683 testStep);
684}
685
686static void TestNWayCanvasStateConsistency(
687 skiatest::Reporter* reporter,
688 CanvasTestStep* testStep,
689 const SkCanvas& referenceCanvas) {
690
691 SkBitmap indirectStore1;
692 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
693 SkDevice indirectDevice1(indirectStore1);
694 SkCanvas indirectCanvas1(&indirectDevice1);
695
696 SkBitmap indirectStore2;
697 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
698 SkDevice indirectDevice2(indirectStore2);
699 SkCanvas indirectCanvas2(&indirectDevice2);
700
701 SkNWayCanvas nWayCanvas;
702 nWayCanvas.addCanvas(&indirectCanvas1);
703 nWayCanvas.addCanvas(&indirectCanvas2);
704
705 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
706 testStep->draw(&nWayCanvas, reporter);
707 // Verify that the SkProxyCanvas reports consitent state
708 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
709 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
710 testStep);
711 // Verify that the indirect canvases report consitent state
712 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
713 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
714 testStep);
715 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
716 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
717 testStep);
718}
719
720/*
721 * This sub-test verifies that the test step passes when executed
722 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
723 * that the all canvas derivatives report the same state as an SkCanvas
724 * after having executed the test step.
725 */
726static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
727 CanvasTestStep* testStep) {
728 SkBitmap referenceStore;
729 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
730 SkDevice referenceDevice(referenceStore);
731 SkCanvas referenceCanvas(&referenceDevice);
732 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
733 testStep->draw(&referenceCanvas, reporter);
734
735 TestPictureStateConsistency(reporter, testStep, referenceCanvas);
736 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
737
738 // The following test code is commented out because SkProxyCanvas is
739 // missing a lot of virtual overrides on get* methods, which are used
740 // to verify canvas state.
741 // Issue: http://code.google.com/p/skia/issues/detail?id=500
742
743 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
744
745 // The following test code is commented out because SkNWayCanvas does not
746 // report correct clipping and device bounds information
747 // Issue: http://code.google.com/p/skia/issues/detail?id=501
748
749 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000750}
reed@google.com37f3ae02011-11-28 16:06:04 +0000751
752static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000753 // Init global here because bitmap pixels cannot be alocated during
754 // static initialization
755 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000756
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000757 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
758 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
759 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
760 testStepArray()[testStep]);
761 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
762 testStepArray()[testStep]);
763 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000764}
765
766#include "TestClassDef.h"
767DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)