blob: 65a34947913ab32b697b3d6309be9e8f866c791e [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 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000131 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000132
133 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
134 virtual const char* name() const = 0;
135
136 const char* assertMessage() {
137 fAssertMessage.printf(fAssertMessageFormat, name());
138 return fAssertMessage.c_str();
139 }
140
141 void setAssertMessageFormat(const char* format) {
142 fAssertMessageFormat = format;
143 }
144
145private:
146 SkString fAssertMessage;
147 const char* fAssertMessageFormat;
148};
149
150///////////////////////////////////////////////////////////////////////////////
151// Constants used by test steps
152
153const SkRect kTestRect =
154 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
155 SkIntToScalar(2), SkIntToScalar(1));
156static SkMatrix testMatrix() {
157 SkMatrix matrix;
158 matrix.reset();
159 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
160 return matrix;
161}
162const SkMatrix kTestMatrix = testMatrix();
163static SkPath testPath() {
164 SkPath path;
165 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
166 SkIntToScalar(2), SkIntToScalar(1)));
167 return path;
168}
169const SkPath kTestPath = testPath();
170static SkRegion testRegion() {
171 SkRegion region;
172 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
173 region.setRect(rect);
174 return region;
175}
176const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
177const SkRegion kTestRegion = testRegion();
178const SkColor kTestColor = 0x01020304;
179const SkPaint kTestPaint;
180const SkPoint kTestPoints[3] = {
181 {SkIntToScalar(0), SkIntToScalar(0)},
182 {SkIntToScalar(2), SkIntToScalar(1)},
183 {SkIntToScalar(0), SkIntToScalar(2)}
184};
185const size_t kTestPointCount = 3;
186static SkBitmap testBitmap() {
187 SkBitmap bitmap;
188 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
189 return bitmap;
190}
191SkBitmap kTestBitmap; // cannot be created during static init
192SkString kTestText("Hello World");
193SkPoint kTestPoint = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(1));
194
195///////////////////////////////////////////////////////////////////////////////
196// Macros for defining test steps
197
198#define TEST_STEP(NAME, FUNCTION) \
199class NAME##_TestStep : public CanvasTestStep{ \
200public: \
201 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
202 FUNCTION (canvas, reporter, this); \
203 } \
204 virtual const char* name() const {return #NAME ;} \
205}; \
206static NAME##_TestStep NAME##_TestStepInstance;
207
208#define SIMPLE_TEST_STEP(NAME, CALL) \
209static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
210 CanvasTestStep*) { \
211 canvas-> CALL ; \
212} \
213TEST_STEP(NAME, NAME##TestStep )
214
215#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
216static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
217 CanvasTestStep* testStep) { \
218 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
219 testStep->assertMessage()); \
220} \
221TEST_STEP(NAME, NAME##TestStep )
222
223
224///////////////////////////////////////////////////////////////////////////////
225// Basic test steps for most virtual methods in SkCanvas that draw or affect
226// the state of the canvas.
227
junov@chromium.orga907ac32012-02-24 21:54:07 +0000228SIMPLE_TEST_STEP(SaveMatrix, save(SkCanvas::kMatrix_SaveFlag));
229SIMPLE_TEST_STEP(SaveClip, save(SkCanvas::kClip_SaveFlag));
230SIMPLE_TEST_STEP(SaveMatrixClip, save(SkCanvas::kMatrixClip_SaveFlag));
231SIMPLE_TEST_STEP(SaveLayer, saveLayer(NULL, NULL));
232SIMPLE_TEST_STEP(BoundedSaveLayer, saveLayer(&kTestRect, NULL));
233SIMPLE_TEST_STEP(PaintSaveLayer, saveLayer(NULL, &kTestPaint));
234SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
235 translate(SkIntToScalar(1), SkIntToScalar(2)));
236SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
237 scale(SkIntToScalar(1), SkIntToScalar(2)));
238SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
239SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
240 skew(SkIntToScalar(1), SkIntToScalar(2)));
241SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
242SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
243SIMPLE_TEST_STEP_WITH_ASSERT(ClipRect, clipRect(kTestRect));
244SIMPLE_TEST_STEP_WITH_ASSERT(ClipPath, clipPath(kTestPath));
245SIMPLE_TEST_STEP_WITH_ASSERT(ClipRegion,
246 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247SIMPLE_TEST_STEP(Clear, clear(kTestColor));
248SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
249SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
250 kTestPointCount, kTestPoints, kTestPaint));
251SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
252 kTestPointCount, kTestPoints, kTestPaint));
253SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
254 kTestPointCount, kTestPoints, kTestPaint));
255SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
256SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000257SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000258SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
259SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
260 NULL));
261SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
262 &kTestIRect, kTestRect, NULL));
263SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
264 kTestRect, &kTestPaint));
265SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
266 NULL));
267SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
268 kTestMatrix, &kTestPaint));
269SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
270 kTestRect, NULL));
271SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
272 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000273SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000274SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
275SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
276 0, 1, kTestPaint));
277SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
278 kTestText.size(), &kTestPoint, kTestPaint));
279SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
280 kTestText.size(), kTestPath, NULL, kTestPaint));
281SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
282 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
283SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
284SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
285
286///////////////////////////////////////////////////////////////////////////////
287// Complex test steps
288
epoger@google.com94fa43c2012-04-11 17:51:01 +0000289// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
290// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
291static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
292 skiatest::Reporter* reporter,
293 CanvasTestStep* testStep) {
294 SkPaint paint;
295 paint.setStrokeWidth(SkIntToScalar(1));
296 paint.setStyle(SkPaint::kStroke_Style);
297
298 SkPath path;
299 SkPoint pt1 = { 0, 0 };
300 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
301 SkPoint pt3 = { SkIntToScalar(1), 0 };
302 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
303 path.moveTo(pt1);
304 path.lineTo(pt2);
305 path.lineTo(pt3);
306 path.lineTo(pt4);
307
308 canvas->drawPath(path, paint);
309}
310TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
311
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000312static void DrawVerticesShaderTestStep(SkCanvas* canvas,
313 skiatest::Reporter* reporter,
314 CanvasTestStep* testStep) {
315 SkPoint pts[4];
316 pts[0].set(0, 0);
317 pts[1].set(SkIntToScalar(kWidth), 0);
318 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
319 pts[3].set(0, SkIntToScalar(kHeight));
320 SkPaint paint;
321 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
322 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
323 paint.setShader(shader)->unref();
324 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
325 NULL, NULL, NULL, 0, paint);
326}
327TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
328
329static void DrawPictureTestStep(SkCanvas* canvas,
330 skiatest::Reporter* reporter,
331 CanvasTestStep* testStep) {
332 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
333 SkAutoUnref aup(testPicture);
334 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
335 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
336 testCanvas->clipRect(kTestRect);
337 testCanvas->drawRect(kTestRect, kTestPaint);
338 canvas->drawPicture(*testPicture);
339}
340TEST_STEP(DrawPicture, DrawPictureTestStep);
341
342static void SaveRestoreTestStep(SkCanvas* canvas,
343 skiatest::Reporter* reporter,
344 CanvasTestStep* testStep) {
345 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
346 testStep->assertMessage());
347 size_t n = canvas->save();
348 REPORTER_ASSERT_MESSAGE(reporter, 1 == n, testStep->assertMessage());
349 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
350 testStep->assertMessage());
351 canvas->save();
352 canvas->save();
353 REPORTER_ASSERT_MESSAGE(reporter, 4 == canvas->getSaveCount(),
354 testStep->assertMessage());
355 canvas->restoreToCount(2);
356 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
357 testStep->assertMessage());
358
359 // should this pin to 1, or be a no-op, or crash?
360 canvas->restoreToCount(0);
361 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
362 testStep->assertMessage());
363}
364TEST_STEP(SaveRestore, SaveRestoreTestStep);
365
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000366static void DrawLayerTestStep(SkCanvas* canvas,
367 skiatest::Reporter* reporter,
368 CanvasTestStep* testStep) {
369 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
370 testStep->assertMessage());
371 canvas->save();
372 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
373 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000374
375 const SkRect* bounds = NULL; // null means include entire bounds
376 const SkPaint* paint = NULL;
377
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000378 canvas->saveLayer(bounds, paint);
379 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
380 testStep->assertMessage());
381 canvas->restore();
382 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
383 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000384
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000385 canvas->saveLayer(bounds, paint);
386 canvas->saveLayer(bounds, paint);
387 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
388 testStep->assertMessage());
389 canvas->restore();
390 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
391 testStep->assertMessage());
392 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000393 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000394 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
395 testStep->assertMessage());
396}
397TEST_STEP(DrawLayer, DrawLayerTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000398
399static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
400 const SkCanvas* canvas1,
401 const SkCanvas* canvas2,
402 CanvasTestStep* testStep) {
403 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
404 canvas2->getDeviceSize(), testStep->assertMessage());
405 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
406 canvas2->getSaveCount(), testStep->assertMessage());
407 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
408 canvas2->isDrawingToLayer(), testStep->assertMessage());
409 SkRect bounds1, bounds2;
410 REPORTER_ASSERT_MESSAGE(reporter,
411 canvas1->getClipBounds(&bounds1, SkCanvas::kAA_EdgeType) ==
412 canvas2->getClipBounds(&bounds2, SkCanvas::kAA_EdgeType),
413 testStep->assertMessage());
414 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
415 testStep->assertMessage());
416 REPORTER_ASSERT_MESSAGE(reporter,
417 canvas1->getClipBounds(&bounds1, SkCanvas::kBW_EdgeType) ==
418 canvas2->getClipBounds(&bounds2, SkCanvas::kBW_EdgeType),
419 testStep->assertMessage());
420 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
421 testStep->assertMessage());
422 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
423 canvas2->getDrawFilter(), testStep->assertMessage());
424 SkIRect deviceBounds1, deviceBounds2;
425 REPORTER_ASSERT_MESSAGE(reporter,
426 canvas1->getClipDeviceBounds(&deviceBounds1) ==
427 canvas2->getClipDeviceBounds(&deviceBounds2),
428 testStep->assertMessage());
429 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
430 testStep->assertMessage());
431 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
432 canvas2->getBounder(), testStep->assertMessage());
433 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
434 canvas2->getTotalMatrix(), testStep->assertMessage());
435 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
436 canvas2->getClipType(), testStep->assertMessage());
437 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
438 canvas2->getTotalClip(), testStep->assertMessage());
439 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClipStack() ==
440 canvas2->getTotalClipStack(), testStep->assertMessage());
441
442 // The following test code is commented out because the test fails when
443 // the canvas is an SkPictureRecord or SkDeferredCanvas
444 // Issue: http://code.google.com/p/skia/issues/detail?id=498
445 // Also, creating a LayerIter on an SkProxyCanvas crashes
446 // Issue: http://code.google.com/p/skia/issues/detail?id=499
447 /*
448 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
449 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
450 while (!layerIter1.done() && !layerIter2.done()) {
451 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
452 layerIter2.matrix(), testStep->assertMessage());
453 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
454 layerIter2.clip(), testStep->assertMessage());
455 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
456 layerIter2.paint(), testStep->assertMessage());
457 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
458 layerIter2.x(), testStep->assertMessage());
459 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
460 layerIter2.y(), testStep->assertMessage());
461 layerIter1.next();
462 layerIter2.next();
463 }
464 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
465 testStep->assertMessage());
466 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
467 testStep->assertMessage());
468 */
469}
470
471// The following class groups static functions that need to access
472// the privates members of SkPictureRecord
473class SkPictureTester {
474private:
475 static void AssertFlattenedObjectsEqual(
476 SkPictureRecord* referenceRecord,
477 SkPictureRecord* testRecord,
478 skiatest::Reporter* reporter,
479 CanvasTestStep* testStep) {
480
481 REPORTER_ASSERT_MESSAGE(reporter,
482 referenceRecord->fBitmaps.count() ==
483 testRecord->fBitmaps.count(), testStep->assertMessage());
484 for (int i = 0; i < referenceRecord->fBitmaps.count(); ++i) {
485 REPORTER_ASSERT_MESSAGE(reporter,
486 SkFlatData::Compare(referenceRecord->fBitmaps[i],
487 testRecord->fBitmaps[i]) == 0, testStep->assertMessage());
488 }
489 REPORTER_ASSERT_MESSAGE(reporter,
490 referenceRecord->fMatrices.count() ==
491 testRecord->fMatrices.count(), testStep->assertMessage());
492 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
493 REPORTER_ASSERT_MESSAGE(reporter,
494 SkFlatData::Compare(referenceRecord->fMatrices[i],
495 testRecord->fMatrices[i]) == 0,
496 testStep->assertMessage());
497 }
498 REPORTER_ASSERT_MESSAGE(reporter,
499 referenceRecord->fPaints.count() ==
500 testRecord->fPaints.count(), testStep->assertMessage());
501 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
502 REPORTER_ASSERT_MESSAGE(reporter,
503 SkFlatData::Compare(referenceRecord->fPaints[i],
504 testRecord->fPaints[i]) == 0, testStep->assertMessage());
505 }
506 REPORTER_ASSERT_MESSAGE(reporter,
507 referenceRecord->fRegions.count() ==
508 testRecord->fRegions.count(), testStep->assertMessage());
509 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
510 REPORTER_ASSERT_MESSAGE(reporter,
511 SkFlatData::Compare(referenceRecord->fRegions[i],
512 testRecord->fRegions[i]) == 0, testStep->assertMessage());
513 }
514 REPORTER_ASSERT_MESSAGE(reporter,
515 !referenceRecord->fPathHeap ==
516 !testRecord->fPathHeap,
517 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000518 // The following tests are commented out because they currently
519 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
520 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521 if (referenceRecord->fPathHeap) {
522 REPORTER_ASSERT_MESSAGE(reporter,
523 referenceRecord->fPathHeap->count() ==
524 testRecord->fPathHeap->count(),
525 testStep->assertMessage());
526 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
527 REPORTER_ASSERT_MESSAGE(reporter,
528 (*referenceRecord->fPathHeap)[i] ==
529 (*testRecord->fPathHeap)[i], testStep->assertMessage());
530 }
531 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000532 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533
534 }
535
536public:
537
538 static void TestPictureSerializationRoundTrip(skiatest::Reporter* reporter,
539 CanvasTestStep* testStep) {
540 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
541 SkPicture referencePicture;
542 testStep->draw(referencePicture.beginRecording(kWidth, kHeight),
543 reporter);
544 SkPicture initialPicture;
545 testStep->draw(initialPicture.beginRecording(kWidth, kHeight),
546 reporter);
547 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
548 SkPicture roundTripPicture;
549 initialPicture.draw(roundTripPicture.beginRecording(kWidth, kHeight));
550
551 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
552 referencePicture.getRecordingCanvas());
553 SkPictureRecord* roundTripRecord = static_cast<SkPictureRecord*>(
554 roundTripPicture.getRecordingCanvas());
555
556 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
557
558 // Verify that deserialization-serialization round trip conserves all
559 // data by comparing referenceRecord to roundTripRecord
560 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fBitmapIndex ==
561 roundTripRecord->fBitmapIndex, testStep->assertMessage());
562 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fMatrixIndex ==
563 roundTripRecord->fMatrixIndex, testStep->assertMessage());
564 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fPaintIndex ==
565 roundTripRecord->fPaintIndex, testStep->assertMessage());
566 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRegionIndex ==
567 roundTripRecord->fRegionIndex, testStep->assertMessage());
568 char referenceBuffer[kMaxPictureBufferSize];
569 SkMemoryWStream referenceStream(referenceBuffer,
570 kMaxPictureBufferSize);
571 referenceRecord->fWriter.writeToStream(&referenceStream);
572 char roundTripBuffer[kMaxPictureBufferSize];
573 SkMemoryWStream roundTripStream(roundTripBuffer,
574 kMaxPictureBufferSize);
575 roundTripRecord->fWriter.writeToStream(&roundTripStream);
576 REPORTER_ASSERT_MESSAGE(reporter,
577 roundTripStream.bytesWritten() == referenceStream.bytesWritten(),
578 testStep->assertMessage());
579 REPORTER_ASSERT_MESSAGE(reporter, 0 == memcmp(referenceBuffer,
580 roundTripBuffer, roundTripStream.bytesWritten()),
581 testStep->assertMessage());
582 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRecordFlags ==
583 roundTripRecord->fRecordFlags, testStep->assertMessage());
584 REPORTER_ASSERT_MESSAGE(reporter,
585 referenceRecord->fRestoreOffsetStack ==
586 roundTripRecord->fRestoreOffsetStack,
587 testStep->assertMessage());
588 AssertFlattenedObjectsEqual(referenceRecord, roundTripRecord,
589 reporter, testStep);
590 AssertCanvasStatesEqual(reporter, referenceRecord, roundTripRecord,
591 testStep);
592 }
593
594 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
595 CanvasTestStep* testStep) {
596 // Verify that when a test step is executed twice, no extra resources
597 // are flattened during the second execution
598 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
599 SkPicture referencePicture;
600 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
601 kHeight);
602 testStep->draw(referenceCanvas, reporter);
603 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000604 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000605 kHeight);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000606 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000607 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000608 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000609
610 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
611 referenceCanvas);
612 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
613 testCanvas);
614 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
615 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000616 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000617 }
618};
619
620static void TestPictureStateConsistency(skiatest::Reporter* reporter,
621 CanvasTestStep* testStep,
622 const SkCanvas& referenceCanvas) {
623 // Verify that the recording canvas's state is consistent
624 // with that of a regular canvas
625 SkPicture testPicture;
626 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
627 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
628 testStep->draw(pictureCanvas, reporter);
629 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
630 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
631 testStep);
632
633 SkBitmap playbackStore;
634 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
635 SkDevice playbackDevice(playbackStore);
636 SkCanvas playbackCanvas(&playbackDevice);
637 testPicture.draw(&playbackCanvas);
638 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
639 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
640 testStep);
641
642 // The following test code is commented out because SkPicture is not
643 // currently expected to preserve state when restarting recording.
644 /*
645 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight);
646 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
647 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
648 testStep);
649 */
650}
651
652static void TestDeferredCanvasStateConsistency(
653 skiatest::Reporter* reporter,
654 CanvasTestStep* testStep,
655 const SkCanvas& referenceCanvas) {
656
657 SkBitmap deferredStore;
658 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
659 SkDevice deferredDevice(deferredStore);
660 SkDeferredCanvas deferredCanvas(&deferredDevice);
661 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
662 testStep->draw(&deferredCanvas, reporter);
663 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
664 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
665 testStep);
666
667 // Verified that deferred canvas state is not affected by flushing
668 // pending draw operations
669
670 // The following test code is commented out because it currently fails.
671 // Issue: http://code.google.com/p/skia/issues/detail?id=496
672 /*
673 deferredCanvas.flush();
674 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
675 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
676 testStep);
677 */
678}
679
680static void TestProxyCanvasStateConsistency(
681 skiatest::Reporter* reporter,
682 CanvasTestStep* testStep,
683 const SkCanvas& referenceCanvas) {
684
685 SkBitmap indirectStore;
686 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
687 SkDevice indirectDevice(indirectStore);
688 SkCanvas indirectCanvas(&indirectDevice);
689 SkProxyCanvas proxyCanvas(&indirectCanvas);
690 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
691 testStep->draw(&proxyCanvas, reporter);
692 // Verify that the SkProxyCanvas reports consitent state
693 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
694 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
695 testStep);
696 // Verify that the indirect canvas reports consitent state
697 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
698 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
699 testStep);
700}
701
702static void TestNWayCanvasStateConsistency(
703 skiatest::Reporter* reporter,
704 CanvasTestStep* testStep,
705 const SkCanvas& referenceCanvas) {
706
707 SkBitmap indirectStore1;
708 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
709 SkDevice indirectDevice1(indirectStore1);
710 SkCanvas indirectCanvas1(&indirectDevice1);
711
712 SkBitmap indirectStore2;
713 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
714 SkDevice indirectDevice2(indirectStore2);
715 SkCanvas indirectCanvas2(&indirectDevice2);
716
717 SkNWayCanvas nWayCanvas;
718 nWayCanvas.addCanvas(&indirectCanvas1);
719 nWayCanvas.addCanvas(&indirectCanvas2);
720
721 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
722 testStep->draw(&nWayCanvas, reporter);
723 // Verify that the SkProxyCanvas reports consitent state
724 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
725 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
726 testStep);
727 // Verify that the indirect canvases report consitent state
728 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
729 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
730 testStep);
731 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
732 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
733 testStep);
734}
735
736/*
737 * This sub-test verifies that the test step passes when executed
738 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
739 * that the all canvas derivatives report the same state as an SkCanvas
740 * after having executed the test step.
741 */
742static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
743 CanvasTestStep* testStep) {
744 SkBitmap referenceStore;
745 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
746 SkDevice referenceDevice(referenceStore);
747 SkCanvas referenceCanvas(&referenceDevice);
748 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
749 testStep->draw(&referenceCanvas, reporter);
750
751 TestPictureStateConsistency(reporter, testStep, referenceCanvas);
752 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
753
754 // The following test code is commented out because SkProxyCanvas is
755 // missing a lot of virtual overrides on get* methods, which are used
756 // to verify canvas state.
757 // Issue: http://code.google.com/p/skia/issues/detail?id=500
758
759 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
760
761 // The following test code is commented out because SkNWayCanvas does not
762 // report correct clipping and device bounds information
763 // Issue: http://code.google.com/p/skia/issues/detail?id=501
764
765 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000766}
reed@google.com37f3ae02011-11-28 16:06:04 +0000767
768static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000769 // Init global here because bitmap pixels cannot be alocated during
770 // static initialization
771 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000772
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000773 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
774 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
775 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
776 testStepArray()[testStep]);
777 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
778 testStepArray()[testStep]);
779 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000780}
781
782#include "TestClassDef.h"
783DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)