2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "../src/image/SkImagePriv.h"
9 #include "../src/image/SkSurface_Base.h"
11 #include "SkBitmapDevice.h"
12 #include "SkBitmapProcShader.h"
13 #include "SkDeferredCanvas.h"
14 #include "SkGradientShader.h"
16 #include "SkSurface.h"
18 #include "sk_tool_utils.h"
21 #include "GrContextFactory.h"
23 class GrContextFactory;
26 static const int gWidth = 2;
27 static const int gHeight = 2;
29 static void create(SkBitmap* bm, SkColor color) {
30 bm->allocN32Pixels(gWidth, gHeight);
31 bm->eraseColor(color);
34 static SkSurface* createSurface(SkColor color) {
35 SkSurface* surface = SkSurface::NewRasterN32Premul(gWidth, gHeight);
36 surface->getCanvas()->clear(color);
40 static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
43 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4);
44 SkCanvas canvas(bitmap);
47 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
48 surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
52 class MockSurface : public SkSurface_Base {
54 MockSurface(int width, int height) : SkSurface_Base(width, height, NULL) {
56 fBitmap.allocN32Pixels(width, height);
59 SkCanvas* onNewCanvas() SK_OVERRIDE {
60 return SkNEW_ARGS(SkCanvas, (fBitmap));
63 SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
67 SkImage* onNewImageSnapshot(Budgeted) SK_OVERRIDE {
68 return SkNewImageFromBitmap(fBitmap, true, &this->props());
71 void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
72 if (mode == SkSurface::kDiscard_ContentChangeMode) {
79 void onDiscard() SK_OVERRIDE {
95 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
96 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
97 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
100 srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
101 srcBitmap.eraseColor(SK_ColorGREEN);
102 // Tests below depend on this bitmap being recognized as opaque
104 // Preliminary sanity check: no copy on write if no active snapshot
105 // Discard notification happens on SkSurface::onDiscard, since no
107 surface->clearCounts();
108 canvas->clear(SK_ColorWHITE);
109 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
110 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
111 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
113 surface->clearCounts();
115 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
116 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
117 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
119 // Case 1: Discard notification happens upon flushing
120 // with an Image attached.
121 surface->clearCounts();
122 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
123 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
124 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
125 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
127 surface->clearCounts();
128 canvas->clear(SK_ColorWHITE);
129 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
130 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
131 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
133 surface->clearCounts();
135 REPORTER_ASSERT(reporter, 1 == surface->fCOWDiscardCount);
136 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
137 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
139 // Case 2: Opaque writePixels
140 surface->clearCounts();
141 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
142 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
143 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
144 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
146 // Case 3: writePixels that partially covers the canvas
147 surface->clearCounts();
148 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
149 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
150 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
151 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
153 // Case 4: unpremultiplied opaque writePixels that entirely
155 surface->clearCounts();
156 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
157 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
158 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
159 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
161 surface->clearCounts();
162 canvas->writePixels(srcBitmap, 0, 0);
163 REPORTER_ASSERT(reporter, 1 == surface->fCOWDiscardCount);
164 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
165 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
167 surface->clearCounts();
169 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
170 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
171 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
173 // Case 5: unpremultiplied opaque writePixels that partially
175 surface->clearCounts();
176 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
177 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
178 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
179 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
181 surface->clearCounts();
182 canvas->writePixels(srcBitmap, 5, 0);
183 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
184 REPORTER_ASSERT(reporter, 1 == surface->fCOWRetainCount);
185 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
187 surface->clearCounts();
189 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
190 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
191 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
193 // Case 6: unpremultiplied opaque writePixels that entirely
194 // covers the canvas, preceded by clear
195 surface->clearCounts();
196 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
197 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
198 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
199 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
201 surface->clearCounts();
202 canvas->clear(SK_ColorWHITE);
203 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
204 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
205 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
207 surface->clearCounts();
208 canvas->writePixels(srcBitmap, 0, 0);
209 REPORTER_ASSERT(reporter, 1 == surface->fCOWDiscardCount);
210 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
211 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
213 surface->clearCounts();
215 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
216 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
217 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
219 // Case 7: unpremultiplied opaque writePixels that partially
220 // covers the canvas, preceeded by a clear
221 surface->clearCounts();
222 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
223 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
224 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
225 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
227 surface->clearCounts();
228 canvas->clear(SK_ColorWHITE);
229 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
230 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
231 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
233 surface->clearCounts();
234 canvas->writePixels(srcBitmap, 5, 0);
235 REPORTER_ASSERT(reporter, 1 == surface->fCOWDiscardCount); // because of the clear
236 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
237 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
239 surface->clearCounts();
241 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
242 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
243 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
245 // Case 8: unpremultiplied opaque writePixels that partially
246 // covers the canvas, preceeded by a drawREct that partially
248 surface->clearCounts();
249 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
250 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
251 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
252 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
254 surface->clearCounts();
256 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
257 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
258 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
259 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
261 surface->clearCounts();
262 canvas->writePixels(srcBitmap, 5, 0);
263 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
264 REPORTER_ASSERT(reporter, 1 == surface->fCOWRetainCount);
265 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
267 surface->clearCounts();
269 REPORTER_ASSERT(reporter, 0 == surface->fCOWDiscardCount);
270 REPORTER_ASSERT(reporter, 0 == surface->fCOWRetainCount);
271 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
274 static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
275 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
276 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
278 canvas->clear(0x00000000);
280 // verify that clear was deferred
281 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
285 // verify that clear was executed
286 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
289 static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
291 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
292 SkIntToScalar(gHeight));
294 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
295 SkIntToScalar(1), SkIntToScalar(1));
297 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
298 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
300 // verify that frame is intially fresh
301 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
302 // no clearing op since last call to isFreshFrame -> not fresh
303 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
305 // Verify that clear triggers a fresh frame
306 canvas->clear(0x00000000);
307 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
309 // Verify that clear with saved state triggers a fresh frame
311 canvas->clear(0x00000000);
313 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
315 // Verify that clear within a layer does NOT trigger a fresh frame
316 canvas->saveLayer(NULL, NULL);
317 canvas->clear(0x00000000);
319 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
321 // Verify that full frame rects with different forms of opaque paint
322 // trigger frames to be marked as fresh
325 paint.setStyle(SkPaint::kFill_Style);
327 canvas->drawRect(fullRect, paint);
328 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
332 paint.setStyle(SkPaint::kFill_Style);
334 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
335 canvas->drawRect(fullRect, paint);
336 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
340 paint.setStyle(SkPaint::kFill_Style);
342 create(&bmp, 0xFFFFFFFF);
343 bmp.setAlphaType(kOpaque_SkAlphaType);
344 SkShader* shader = SkShader::CreateBitmapShader(bmp,
345 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
346 paint.setShader(shader)->unref();
347 canvas->drawRect(fullRect, paint);
348 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
351 // Verify that full frame rects with different forms of non-opaque paint
352 // do not trigger frames to be marked as fresh
355 paint.setStyle(SkPaint::kFill_Style);
357 canvas->drawRect(fullRect, paint);
358 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
362 paint.setStyle(SkPaint::kFill_Style);
363 // Defining a cone that partially overlaps the canvas
364 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
365 const SkScalar r1 = SkIntToScalar(1);
366 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
367 const SkScalar r2 = SkIntToScalar(5);
368 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
369 const SkScalar pos[2] = {0, SK_Scalar1};
370 SkShader* shader = SkGradientShader::CreateTwoPointConical(
371 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode);
372 paint.setShader(shader)->unref();
373 canvas->drawRect(fullRect, paint);
374 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
378 paint.setStyle(SkPaint::kFill_Style);
380 create(&bmp, 0xFFFFFFFF);
381 bmp.setAlphaType(kPremul_SkAlphaType);
382 SkShader* shader = SkShader::CreateBitmapShader(bmp,
383 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
384 paint.setShader(shader)->unref();
385 canvas->drawRect(fullRect, paint);
386 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
389 // Verify that incomplete coverage does not trigger a fresh frame
392 paint.setStyle(SkPaint::kFill_Style);
394 canvas->drawRect(partialRect, paint);
395 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
398 // Verify that incomplete coverage due to clipping does not trigger a fresh
402 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
404 paint.setStyle(SkPaint::kFill_Style);
406 canvas->drawRect(fullRect, paint);
408 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
413 paint.setStyle(SkPaint::kFill_Style);
416 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
417 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
418 canvas->drawRect(fullRect, paint);
420 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
423 // Verify that stroked rect does not trigger a fresh frame
426 paint.setStyle(SkPaint::kStroke_Style);
428 canvas->drawRect(fullRect, paint);
429 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
432 // Verify kSrcMode triggers a fresh frame even with transparent color
435 paint.setStyle(SkPaint::kFill_Style);
437 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
438 canvas->drawRect(fullRect, paint);
439 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
443 class MockDevice : public SkBitmapDevice {
445 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
446 fDrawBitmapCallCount = 0;
448 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
449 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
450 fDrawBitmapCallCount++;
453 int fDrawBitmapCallCount;
456 class NotificationCounter : public SkDeferredCanvas::NotificationClient {
458 NotificationCounter() {
459 fPrepareForDrawCount = fStorageAllocatedChangedCount =
460 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
463 void prepareForDraw() SK_OVERRIDE {
464 fPrepareForDrawCount++;
466 void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
467 fStorageAllocatedChangedCount++;
469 void flushedDrawCommands() SK_OVERRIDE {
470 fFlushedDrawCommandsCount++;
472 void skippedPendingDrawCommands() SK_OVERRIDE {
473 fSkippedPendingDrawCommandsCount++;
476 int fPrepareForDrawCount;
477 int fStorageAllocatedChangedCount;
478 int fFlushedDrawCommandsCount;
479 int fSkippedPendingDrawCommandsCount;
482 typedef SkDeferredCanvas::NotificationClient INHERITED;
485 // Verifies that the deferred canvas triggers a flush when its memory
487 static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
488 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
489 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
491 NotificationCounter notificationCounter;
492 canvas->setNotificationClient(¬ificationCounter);
494 canvas->setMaxRecordingStorage(160000);
496 SkBitmap sourceImage;
497 // 100 by 100 image, takes 40,000 bytes in memory
498 sourceImage.allocN32Pixels(100, 100);
499 sourceImage.eraseColor(SK_ColorGREEN);
501 for (int i = 0; i < 5; i++) {
502 sourceImage.notifyPixelsChanged(); // to force re-serialization
503 canvas->drawBitmap(sourceImage, 0, 0, NULL);
506 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
509 static void TestDeferredCanvasSilentFlush(skiatest::Reporter* reporter) {
510 SkAutoTUnref<SkSurface> surface(createSurface(0));
511 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
513 NotificationCounter notificationCounter;
514 canvas->setNotificationClient(¬ificationCounter);
516 canvas->silentFlush(); // will skip the initial clear that was recorded in createSurface
518 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
519 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
522 static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
523 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
524 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
526 NotificationCounter notificationCounter;
527 canvas->setNotificationClient(¬ificationCounter);
529 const int imageCount = 2;
530 SkBitmap sourceImages[imageCount];
531 for (int i = 0; i < imageCount; i++) {
532 sourceImages[i].allocN32Pixels(100, 100);
533 sourceImages[i].eraseColor(SK_ColorGREEN);
536 size_t bitmapSize = sourceImages[0].getSize();
538 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
539 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
540 // stored bitmap + drawBitmap command
541 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
543 // verify that nothing can be freed at this point
544 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
546 // verify that flush leaves image in cache
547 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
548 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
550 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
551 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
552 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
554 // verify that after a flush, cached image can be freed
555 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
557 // Verify that caching works for avoiding multiple copies of the same bitmap
558 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
559 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
560 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
561 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
562 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
563 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
565 // Verify partial eviction based on bytesToFree
566 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
567 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
569 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
570 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
571 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
572 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
573 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
574 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
576 // Verifiy that partial purge works, image zero is in cache but not reffed by
577 // a pending draw, while image 1 is locked-in.
578 canvas->freeMemoryIfPossible(~0U);
579 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
580 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
582 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
583 bytesFreed = canvas->freeMemoryIfPossible(~0U);
584 // only one bitmap should have been freed.
585 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
586 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
587 // Clear for next test
589 canvas->freeMemoryIfPossible(~0U);
590 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
592 // Verify the image cache is sensitive to genID bumps
593 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
594 sourceImages[1].notifyPixelsChanged();
595 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
596 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
598 // Verify that nothing in this test caused commands to be skipped
599 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
602 static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
603 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
604 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
606 NotificationCounter notificationCounter;
607 canvas->setNotificationClient(¬ificationCounter);
609 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
610 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
612 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
613 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
617 static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
618 // This is a regression test for crbug.com/155875
619 // This test covers a code path that inserts bitmaps into the bitmap heap through the
620 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
621 // the flattening and unflattening of the shader.
622 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
623 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
624 // test will fail if nbIterations is not in sync with
625 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
626 const int nbIterations = 5;
627 size_t bytesAllocated = 0;
628 for(int pass = 0; pass < 2; ++pass) {
629 for(int i = 0; i < nbIterations; ++i) {
631 SkBitmap paintPattern;
632 paintPattern.allocN32Pixels(10, 10);
633 paintPattern.eraseColor(SK_ColorGREEN);
634 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
635 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
636 canvas->drawPaint(paint);
639 // In the first pass, memory allocation should be monotonically increasing as
640 // the bitmap heap slots fill up. In the second pass memory allocation should be
641 // stable as bitmap heap slots get recycled.
642 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
644 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
645 bytesAllocated = newBytesAllocated;
647 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
651 // All cached resources should be evictable since last canvas call was flush()
652 canvas->freeMemoryIfPossible(~0U);
653 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
656 static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
657 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
659 SkBitmap sourceImage;
660 // 100 by 100 image, takes 40,000 bytes in memory
661 sourceImage.allocN32Pixels(100, 100);
662 sourceImage.eraseColor(SK_ColorGREEN);
664 // 1 under : should not store the image
666 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
667 canvas->setBitmapSizeThreshold(39999);
668 canvas->drawBitmap(sourceImage, 0, 0, NULL);
669 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
670 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
673 // exact value : should store the image
675 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
676 canvas->setBitmapSizeThreshold(40000);
677 canvas->drawBitmap(sourceImage, 0, 0, NULL);
678 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
679 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
682 // 1 over : should still store the image
684 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
685 canvas->setBitmapSizeThreshold(40001);
686 canvas->drawBitmap(sourceImage, 0, 0, NULL);
687 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
688 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
693 typedef const void* PixelPtr;
694 // Returns an opaque pointer which, either points to a GrTexture or RAM pixel
695 // buffer. Used to test pointer equality do determine whether a surface points
696 // to the same pixel data storage as before.
697 static PixelPtr get_surface_ptr(SkSurface* surface, bool useGpu) {
700 return surface->getCanvas()->internal_private_accessTopLayerRenderTarget()->asTexture();
704 return surface->peekPixels(NULL, NULL);
708 static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
709 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
710 bool useGpu = SkToBool(factory);
714 cnt = GrContextFactory::kGLContextTypeCnt;
722 for (int i = 0; i < cnt; ++i) {
726 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
727 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
730 GrContext* context = factory->get(glCtxType);
731 if (NULL == context) {
736 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
740 surface = SkSurface::NewRaster(imageSpec);
743 SkAutoTUnref<SkSurface> aur(surface);
744 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
746 SkImage* image1 = canvas->newImageSnapshot();
747 SkAutoTUnref<SkImage> aur_i1(image1);
748 PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
749 // The following clear would normally trigger a copy on write, but
750 // it won't because rendering is deferred.
751 canvas->clear(SK_ColorBLACK);
752 // Obtaining a snapshot directly from the surface (as opposed to the
753 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
754 // and will therefore return the same image as the previous snapshot.
755 SkImage* image2 = surface->newImageSnapshot();
756 SkAutoTUnref<SkImage> aur_i2(image2);
757 // Images identical because of deferral
758 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
759 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
760 // Because there is a pending clear, this will generate a different image.
761 SkImage* image3 = canvas->newImageSnapshot();
762 SkAutoTUnref<SkImage> aur_i3(image3);
763 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
764 // Verify that backing store is now a different buffer because of copy on
766 PixelPtr pixels2 = get_surface_ptr(surface, useGpu);
767 REPORTER_ASSERT(reporter, pixels1 != pixels2);
768 // Verify copy-on write with a draw operation that gets deferred by
769 // the in order draw buffer.
771 canvas->drawPaint(paint);
772 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
773 SkAutoTUnref<SkImage> aur_i4(image4);
774 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
775 PixelPtr pixels3 = get_surface_ptr(surface, useGpu);
776 REPORTER_ASSERT(reporter, pixels2 != pixels3);
777 // Verify that a direct canvas flush with a pending draw does not trigger
778 // a copy on write when the surface is not sharing its buffer with an
780 canvas->clear(SK_ColorWHITE);
782 PixelPtr pixels4 = get_surface_ptr(surface, useGpu);
783 canvas->drawPaint(paint);
785 PixelPtr pixels5 = get_surface_ptr(surface, useGpu);
786 REPORTER_ASSERT(reporter, pixels4 == pixels5);
790 static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
791 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
793 SkSurface* alternateSurface;
794 bool useGpu = SkToBool(factory);
798 cnt = GrContextFactory::kGLContextTypeCnt;
807 for (int i = 0; i < cnt; ++i) {
810 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
811 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
814 GrContext* context = factory->get(glCtxType);
815 if (NULL == context) {
819 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
821 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
825 surface = SkSurface::NewRaster(imageSpec);
826 alternateSurface = SkSurface::NewRaster(imageSpec);
829 SkASSERT(alternateSurface);
830 SkAutoTUnref<SkSurface> aur1(surface);
831 SkAutoTUnref<SkSurface> aur2(alternateSurface);
832 PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
833 PixelPtr pixels2 = get_surface_ptr(alternateSurface, useGpu);
834 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
835 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
836 canvas->setSurface(alternateSurface);
837 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
838 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
839 // Verify that none of the above operations triggered a surface copy on write.
840 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
841 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) == pixels2);
842 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
843 canvas->clear(SK_ColorWHITE);
845 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
846 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) != pixels2);
850 static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
851 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
852 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
854 NotificationCounter notificationCounter;
855 canvas->setNotificationClient(¬ificationCounter);
857 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
858 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info));
860 SkRect rect = SkRect::MakeWH(5, 5);
862 // After spawning a compatible canvas:
863 // 1) Verify that secondary canvas is usable and does not report to the notification client.
864 surface->getCanvas()->drawRect(rect, paint);
865 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
866 // 2) Verify that original canvas is usable and still reports to the notification client.
867 canvas->drawRect(rect, paint);
868 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
871 static void TestDeferredCanvasGetCanvasSize(skiatest::Reporter* reporter) {
873 rect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIntToScalar(gHeight));
875 clip.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToScalar(1));
878 SkISize size = SkISize::Make(gWidth, gHeight);
880 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
881 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
882 SkSurface* newSurface = SkSurface::NewRasterN32Premul(4, 4);
883 SkAutoTUnref<SkSurface> aur(newSurface);
885 for (int i = 0; i < 2; ++i) {
887 canvas->setSurface(newSurface);
888 size = SkISize::Make(4, 4);
891 // verify that canvas size is correctly initialized or set
892 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
894 // Verify that clear, clip and draw the canvas will not change its size
895 canvas->clear(0x00000000);
896 canvas->clipRect(clip, SkRegion::kIntersect_Op, false);
897 canvas->drawRect(rect, paint);
898 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
900 // Verify that flush the canvas will not change its size
902 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
904 // Verify that clear canvas with saved state will not change its size
906 canvas->clear(0xFFFFFFFF);
907 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
909 // Verify that restore canvas state will not change its size
911 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
913 // Verify that clear within a layer will not change canvas size
914 canvas->saveLayer(&clip, &paint);
915 canvas->clear(0x00000000);
916 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
918 // Verify that restore from a layer will not change canvas size
920 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
924 DEF_TEST(DeferredCanvas_CPU, reporter) {
925 TestDeferredCanvasFlush(reporter);
926 TestDeferredCanvasSilentFlush(reporter);
927 TestDeferredCanvasFreshFrame(reporter);
928 TestDeferredCanvasMemoryLimit(reporter);
929 TestDeferredCanvasBitmapCaching(reporter);
930 TestDeferredCanvasSkip(reporter);
931 TestDeferredCanvasBitmapShaderNoLeak(reporter);
932 TestDeferredCanvasBitmapSizeThreshold(reporter);
933 TestDeferredCanvasCreateCompatibleDevice(reporter);
934 TestDeferredCanvasWritePixelsToSurface(reporter);
935 TestDeferredCanvasGetCanvasSize(reporter);
936 TestDeferredCanvasSurface(reporter, NULL);
937 TestDeferredCanvasSetSurface(reporter, NULL);
940 DEF_GPUTEST(DeferredCanvas_GPU, reporter, factory) {
941 if (factory != NULL) {
942 TestDeferredCanvasSurface(reporter, factory);
943 TestDeferredCanvasSetSurface(reporter, factory);