blob: 54b728b9a00ba4fbcafad019666d7cb09ba409fd [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.org0b5b0ce2012-02-24 20:44:56 +0000227// 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));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000248SIMPLE_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));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000258SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000259SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
260SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
261 NULL));
262SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
263 &kTestIRect, kTestRect, NULL));
264SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
265 kTestRect, &kTestPaint));
266SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
267 NULL));
268SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
269 kTestMatrix, &kTestPaint));
270SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
271 kTestRect, NULL));
272SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
273 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000274SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000275SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
276SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
277 0, 1, kTestPaint));
278SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
279 kTestText.size(), &kTestPoint, kTestPaint));
280SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
281 kTestText.size(), kTestPath, NULL, kTestPaint));
282SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
283 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
284SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
285SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
286
287///////////////////////////////////////////////////////////////////////////////
288// Complex test steps
289
290static void DrawVerticesShaderTestStep(SkCanvas* canvas,
291 skiatest::Reporter* reporter,
292 CanvasTestStep* testStep) {
293 SkPoint pts[4];
294 pts[0].set(0, 0);
295 pts[1].set(SkIntToScalar(kWidth), 0);
296 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
297 pts[3].set(0, SkIntToScalar(kHeight));
298 SkPaint paint;
299 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
300 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
301 paint.setShader(shader)->unref();
302 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
303 NULL, NULL, NULL, 0, paint);
304}
305TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
306
307static void DrawPictureTestStep(SkCanvas* canvas,
308 skiatest::Reporter* reporter,
309 CanvasTestStep* testStep) {
310 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
311 SkAutoUnref aup(testPicture);
312 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
313 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
314 testCanvas->clipRect(kTestRect);
315 testCanvas->drawRect(kTestRect, kTestPaint);
316 canvas->drawPicture(*testPicture);
317}
318TEST_STEP(DrawPicture, DrawPictureTestStep);
319
320static void SaveRestoreTestStep(SkCanvas* canvas,
321 skiatest::Reporter* reporter,
322 CanvasTestStep* testStep) {
323 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
324 testStep->assertMessage());
325 size_t n = canvas->save();
326 REPORTER_ASSERT_MESSAGE(reporter, 1 == n, testStep->assertMessage());
327 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
328 testStep->assertMessage());
329 canvas->save();
330 canvas->save();
331 REPORTER_ASSERT_MESSAGE(reporter, 4 == canvas->getSaveCount(),
332 testStep->assertMessage());
333 canvas->restoreToCount(2);
334 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
335 testStep->assertMessage());
336
337 // should this pin to 1, or be a no-op, or crash?
338 canvas->restoreToCount(0);
339 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
340 testStep->assertMessage());
341}
342TEST_STEP(SaveRestore, SaveRestoreTestStep);
343
junov@chromium.org0b5b0ce2012-02-24 20:44:56 +0000344// The following test step is commented-out because it currently fails
345// Issue: http://code.google.com/p/skia/issues/detail?id=506
346/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000347static void DrawLayerTestStep(SkCanvas* canvas,
348 skiatest::Reporter* reporter,
349 CanvasTestStep* testStep) {
350 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
351 testStep->assertMessage());
352 canvas->save();
353 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
354 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000355
356 const SkRect* bounds = NULL; // null means include entire bounds
357 const SkPaint* paint = NULL;
358
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000359 canvas->saveLayer(bounds, paint);
360 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
361 testStep->assertMessage());
362 canvas->restore();
363 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
364 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000365
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000366 canvas->saveLayer(bounds, paint);
367 canvas->saveLayer(bounds, paint);
368 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
369 testStep->assertMessage());
370 canvas->restore();
371 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
372 testStep->assertMessage());
373 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000374 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000375 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
376 testStep->assertMessage());
377}
378TEST_STEP(DrawLayer, DrawLayerTestStep);
junov@chromium.org0b5b0ce2012-02-24 20:44:56 +0000379*/
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000380
381static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
382 const SkCanvas* canvas1,
383 const SkCanvas* canvas2,
384 CanvasTestStep* testStep) {
385 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
386 canvas2->getDeviceSize(), testStep->assertMessage());
387 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
388 canvas2->getSaveCount(), testStep->assertMessage());
389 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
390 canvas2->isDrawingToLayer(), testStep->assertMessage());
391 SkRect bounds1, bounds2;
392 REPORTER_ASSERT_MESSAGE(reporter,
393 canvas1->getClipBounds(&bounds1, SkCanvas::kAA_EdgeType) ==
394 canvas2->getClipBounds(&bounds2, SkCanvas::kAA_EdgeType),
395 testStep->assertMessage());
396 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
397 testStep->assertMessage());
398 REPORTER_ASSERT_MESSAGE(reporter,
399 canvas1->getClipBounds(&bounds1, SkCanvas::kBW_EdgeType) ==
400 canvas2->getClipBounds(&bounds2, SkCanvas::kBW_EdgeType),
401 testStep->assertMessage());
402 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
403 testStep->assertMessage());
404 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
405 canvas2->getDrawFilter(), testStep->assertMessage());
406 SkIRect deviceBounds1, deviceBounds2;
407 REPORTER_ASSERT_MESSAGE(reporter,
408 canvas1->getClipDeviceBounds(&deviceBounds1) ==
409 canvas2->getClipDeviceBounds(&deviceBounds2),
410 testStep->assertMessage());
411 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
412 testStep->assertMessage());
413 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
414 canvas2->getBounder(), testStep->assertMessage());
415 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
416 canvas2->getTotalMatrix(), testStep->assertMessage());
417 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
418 canvas2->getClipType(), testStep->assertMessage());
419 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
420 canvas2->getTotalClip(), testStep->assertMessage());
421 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClipStack() ==
422 canvas2->getTotalClipStack(), testStep->assertMessage());
423
424 // The following test code is commented out because the test fails when
425 // the canvas is an SkPictureRecord or SkDeferredCanvas
426 // Issue: http://code.google.com/p/skia/issues/detail?id=498
427 // Also, creating a LayerIter on an SkProxyCanvas crashes
428 // Issue: http://code.google.com/p/skia/issues/detail?id=499
429 /*
430 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
431 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
432 while (!layerIter1.done() && !layerIter2.done()) {
433 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
434 layerIter2.matrix(), testStep->assertMessage());
435 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
436 layerIter2.clip(), testStep->assertMessage());
437 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
438 layerIter2.paint(), testStep->assertMessage());
439 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
440 layerIter2.x(), testStep->assertMessage());
441 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
442 layerIter2.y(), testStep->assertMessage());
443 layerIter1.next();
444 layerIter2.next();
445 }
446 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
447 testStep->assertMessage());
448 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
449 testStep->assertMessage());
450 */
451}
452
453// The following class groups static functions that need to access
454// the privates members of SkPictureRecord
455class SkPictureTester {
456private:
457 static void AssertFlattenedObjectsEqual(
458 SkPictureRecord* referenceRecord,
459 SkPictureRecord* testRecord,
460 skiatest::Reporter* reporter,
461 CanvasTestStep* testStep) {
462
463 REPORTER_ASSERT_MESSAGE(reporter,
464 referenceRecord->fBitmaps.count() ==
465 testRecord->fBitmaps.count(), testStep->assertMessage());
466 for (int i = 0; i < referenceRecord->fBitmaps.count(); ++i) {
467 REPORTER_ASSERT_MESSAGE(reporter,
468 SkFlatData::Compare(referenceRecord->fBitmaps[i],
469 testRecord->fBitmaps[i]) == 0, testStep->assertMessage());
470 }
471 REPORTER_ASSERT_MESSAGE(reporter,
472 referenceRecord->fMatrices.count() ==
473 testRecord->fMatrices.count(), testStep->assertMessage());
474 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
475 REPORTER_ASSERT_MESSAGE(reporter,
476 SkFlatData::Compare(referenceRecord->fMatrices[i],
477 testRecord->fMatrices[i]) == 0,
478 testStep->assertMessage());
479 }
480 REPORTER_ASSERT_MESSAGE(reporter,
481 referenceRecord->fPaints.count() ==
482 testRecord->fPaints.count(), testStep->assertMessage());
483 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
484 REPORTER_ASSERT_MESSAGE(reporter,
485 SkFlatData::Compare(referenceRecord->fPaints[i],
486 testRecord->fPaints[i]) == 0, testStep->assertMessage());
487 }
488 REPORTER_ASSERT_MESSAGE(reporter,
489 referenceRecord->fRegions.count() ==
490 testRecord->fRegions.count(), testStep->assertMessage());
491 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
492 REPORTER_ASSERT_MESSAGE(reporter,
493 SkFlatData::Compare(referenceRecord->fRegions[i],
494 testRecord->fRegions[i]) == 0, testStep->assertMessage());
495 }
496 REPORTER_ASSERT_MESSAGE(reporter,
497 !referenceRecord->fPathHeap ==
498 !testRecord->fPathHeap,
499 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000500 // The following tests are commented out because they currently
501 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
502 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000503 if (referenceRecord->fPathHeap) {
504 REPORTER_ASSERT_MESSAGE(reporter,
505 referenceRecord->fPathHeap->count() ==
506 testRecord->fPathHeap->count(),
507 testStep->assertMessage());
508 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
509 REPORTER_ASSERT_MESSAGE(reporter,
510 (*referenceRecord->fPathHeap)[i] ==
511 (*testRecord->fPathHeap)[i], testStep->assertMessage());
512 }
513 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000514 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000515
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;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000586 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000587 kHeight);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000588 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000589 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000590 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591
592 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
593 referenceCanvas);
594 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
595 testCanvas);
596 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
597 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000598 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000599 }
600};
601
602static void TestPictureStateConsistency(skiatest::Reporter* reporter,
603 CanvasTestStep* testStep,
604 const SkCanvas& referenceCanvas) {
605 // Verify that the recording canvas's state is consistent
606 // with that of a regular canvas
607 SkPicture testPicture;
608 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
609 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
610 testStep->draw(pictureCanvas, reporter);
611 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
612 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
613 testStep);
614
615 SkBitmap playbackStore;
616 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
617 SkDevice playbackDevice(playbackStore);
618 SkCanvas playbackCanvas(&playbackDevice);
619 testPicture.draw(&playbackCanvas);
620 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
621 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
622 testStep);
623
624 // The following test code is commented out because SkPicture is not
625 // currently expected to preserve state when restarting recording.
626 /*
627 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
628 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
629 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
630 testStep);
631 */
632}
633
634static void TestDeferredCanvasStateConsistency(
635 skiatest::Reporter* reporter,
636 CanvasTestStep* testStep,
637 const SkCanvas& referenceCanvas) {
638
639 SkBitmap deferredStore;
640 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
641 SkDevice deferredDevice(deferredStore);
642 SkDeferredCanvas deferredCanvas(&deferredDevice);
643 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
644 testStep->draw(&deferredCanvas, reporter);
645 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
646 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
647 testStep);
648
649 // Verified that deferred canvas state is not affected by flushing
650 // pending draw operations
651
652 // The following test code is commented out because it currently fails.
653 // Issue: http://code.google.com/p/skia/issues/detail?id=496
654 /*
655 deferredCanvas.flush();
656 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
657 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
658 testStep);
659 */
660}
661
662static void TestProxyCanvasStateConsistency(
663 skiatest::Reporter* reporter,
664 CanvasTestStep* testStep,
665 const SkCanvas& referenceCanvas) {
666
667 SkBitmap indirectStore;
668 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
669 SkDevice indirectDevice(indirectStore);
670 SkCanvas indirectCanvas(&indirectDevice);
671 SkProxyCanvas proxyCanvas(&indirectCanvas);
672 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
673 testStep->draw(&proxyCanvas, reporter);
674 // Verify that the SkProxyCanvas reports consitent state
675 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
676 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
677 testStep);
678 // Verify that the indirect canvas reports consitent state
679 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
680 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
681 testStep);
682}
683
684static void TestNWayCanvasStateConsistency(
685 skiatest::Reporter* reporter,
686 CanvasTestStep* testStep,
687 const SkCanvas& referenceCanvas) {
688
689 SkBitmap indirectStore1;
690 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
691 SkDevice indirectDevice1(indirectStore1);
692 SkCanvas indirectCanvas1(&indirectDevice1);
693
694 SkBitmap indirectStore2;
695 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
696 SkDevice indirectDevice2(indirectStore2);
697 SkCanvas indirectCanvas2(&indirectDevice2);
698
699 SkNWayCanvas nWayCanvas;
700 nWayCanvas.addCanvas(&indirectCanvas1);
701 nWayCanvas.addCanvas(&indirectCanvas2);
702
703 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
704 testStep->draw(&nWayCanvas, reporter);
705 // Verify that the SkProxyCanvas reports consitent state
706 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
707 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
708 testStep);
709 // Verify that the indirect canvases report consitent state
710 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
711 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
712 testStep);
713 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
714 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
715 testStep);
716}
717
718/*
719 * This sub-test verifies that the test step passes when executed
720 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
721 * that the all canvas derivatives report the same state as an SkCanvas
722 * after having executed the test step.
723 */
724static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
725 CanvasTestStep* testStep) {
726 SkBitmap referenceStore;
727 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
728 SkDevice referenceDevice(referenceStore);
729 SkCanvas referenceCanvas(&referenceDevice);
730 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
731 testStep->draw(&referenceCanvas, reporter);
732
733 TestPictureStateConsistency(reporter, testStep, referenceCanvas);
734 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
735
736 // The following test code is commented out because SkProxyCanvas is
737 // missing a lot of virtual overrides on get* methods, which are used
738 // to verify canvas state.
739 // Issue: http://code.google.com/p/skia/issues/detail?id=500
740
741 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
742
743 // The following test code is commented out because SkNWayCanvas does not
744 // report correct clipping and device bounds information
745 // Issue: http://code.google.com/p/skia/issues/detail?id=501
746
747 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000748}
reed@google.com37f3ae02011-11-28 16:06:04 +0000749
750static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000751 // Init global here because bitmap pixels cannot be alocated during
752 // static initialization
753 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000754
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000755 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
756 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
757 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
758 testStepArray()[testStep]);
759 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
760 testStepArray()[testStep]);
761 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000762}
763
764#include "TestClassDef.h"
765DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)