blob: ffcbc23d5645f3aa512679c99b9e4ee888fcddcf [file] [log] [blame]
junov@chromium.org1f9767c2012-02-07 16:27:57 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00007
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00008#include "../src/image/SkImagePriv.h"
9#include "../src/image/SkSurface_Base.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000010#include "SkBitmap.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
junov@chromium.orgce65f382012-10-17 19:36:09 +000012#include "SkBitmapProcShader.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000013#include "SkDeferredCanvas.h"
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000014#include "SkGradientShader.h"
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000015#include "SkShader.h"
reed@google.com28183b42014-02-04 15:34:10 +000016#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018#include "sk_tool_utils.h"
19
junov@chromium.org67d74222013-04-12 13:33:01 +000020#if SK_SUPPORT_GPU
21#include "GrContextFactory.h"
22#else
23class GrContextFactory;
24#endif
junov@chromium.org1f9767c2012-02-07 16:27:57 +000025
junov@chromium.org1f9767c2012-02-07 16:27:57 +000026static const int gWidth = 2;
27static const int gHeight = 2;
28
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000029static void create(SkBitmap* bm, SkColor color) {
30 bm->allocN32Pixels(gWidth, gHeight);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000031 bm->eraseColor(color);
32}
33
reed@google.com28183b42014-02-04 15:34:10 +000034static SkSurface* createSurface(SkColor color) {
35 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight);
36 surface->getCanvas()->clear(color);
37 return surface;
38}
39
40static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
41 SkPMColor pixel = 0;
42 SkBitmap bitmap;
43 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4, NULL, NULL);
44 SkCanvas canvas(bitmap);
45
46 SkPaint paint;
47 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@google.comd52a9972014-02-04 16:14:58 +000048 surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
reed@google.com28183b42014-02-04 15:34:10 +000049 return pixel;
50}
51
junov@chromium.org1f9767c2012-02-07 16:27:57 +000052static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
53 SkBitmap store;
54
reed@google.com28183b42014-02-04 15:34:10 +000055 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
56 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +000057
junov@chromium.org66070a52013-05-28 17:39:08 +000058 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000059
reed@google.com28183b42014-02-04 15:34:10 +000060 // verify that the clear() was deferred
61 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
62
junov@chromium.org66070a52013-05-28 17:39:08 +000063 SkBitmap accessed = canvas->getDevice()->accessBitmap(false);
reed@google.com28183b42014-02-04 15:34:10 +000064
65 // verify that clear was executed
66 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +000067}
68
junov@chromium.org44324fa2013-08-02 15:36:02 +000069class MockSurface : public SkSurface_Base {
70public:
71 MockSurface(int width, int height) : SkSurface_Base(width, height) {
72 clearCounts();
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000073 fBitmap.allocN32Pixels(width, height);
junov@chromium.org44324fa2013-08-02 15:36:02 +000074 }
75
76 virtual SkCanvas* onNewCanvas() SK_OVERRIDE {
77 return SkNEW_ARGS(SkCanvas, (fBitmap));
78 }
79
reed@google.com2bd8b812013-11-01 13:46:54 +000080 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000081 return NULL;
82 }
83
84 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE {
85 return SkNewImageFromBitmap(fBitmap, true);
86 }
87
88 virtual void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
89 if (mode == SkSurface::kDiscard_ContentChangeMode) {
90 fDiscardCount++;
91 } else {
92 fRetainCount++;
93 }
94 }
95
96 void clearCounts() {
97 fDiscardCount = 0;
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +000098 fRetainCount = 0;
junov@chromium.org44324fa2013-08-02 15:36:02 +000099 }
100
101 int fDiscardCount, fRetainCount;
102 SkBitmap fBitmap;
103};
104
105static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
106 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000107 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org44324fa2013-08-02 15:36:02 +0000108
109 SkBitmap srcBitmap;
reed@google.com7111d462014-03-25 16:20:24 +0000110 srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
junov@chromium.org44324fa2013-08-02 15:36:02 +0000111 srcBitmap.eraseColor(SK_ColorGREEN);
112 // Tests below depend on this bitmap being recognized as opaque
113
114 // Preliminary sanity check: no copy on write if no active snapshot
115 surface->clearCounts();
116 canvas->clear(SK_ColorWHITE);
117 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
118 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
119
120 surface->clearCounts();
121 canvas->flush();
122 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
123 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
124
125 // Case 1: Discard notification happens upon flushing
126 // with an Image attached.
127 surface->clearCounts();
128 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
129 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
130 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
131
132 surface->clearCounts();
133 canvas->clear(SK_ColorWHITE);
134 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
135 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
136
137 surface->clearCounts();
138 canvas->flush();
139 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
140 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
141
142 // Case 2: Opaque writePixels
143 surface->clearCounts();
144 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
145 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
146 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
147
junov@chromium.org44324fa2013-08-02 15:36:02 +0000148 // Case 3: writePixels that partially covers the canvas
149 surface->clearCounts();
150 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
151 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
152 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
153
junov@chromium.org44324fa2013-08-02 15:36:02 +0000154 // Case 4: unpremultiplied opaque writePixels that entirely
155 // covers the canvas
156 surface->clearCounts();
157 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
158 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
159 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
160
161 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000162 canvas->writePixels(srcBitmap, 0, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000163 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
164 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
165
166 surface->clearCounts();
167 canvas->flush();
168 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
169 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
170
171 // Case 5: unpremultiplied opaque writePixels that partially
172 // covers the canvas
173 surface->clearCounts();
174 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
175 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
176 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
177
178 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000179 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000180 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
181 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
182
183 surface->clearCounts();
184 canvas->flush();
185 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
186 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
187
188 // Case 6: unpremultiplied opaque writePixels that entirely
189 // covers the canvas, preceded by clear
190 surface->clearCounts();
191 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
192 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
193 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
194
195 surface->clearCounts();
196 canvas->clear(SK_ColorWHITE);
197 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
198 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
199
200 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000201 canvas->writePixels(srcBitmap, 0, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000202 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
203 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
204
205 surface->clearCounts();
206 canvas->flush();
207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
209
210 // Case 7: unpremultiplied opaque writePixels that partially
211 // covers the canvas, preceeded by a clear
212 surface->clearCounts();
213 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
214 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
215 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
216
217 surface->clearCounts();
218 canvas->clear(SK_ColorWHITE);
219 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
220 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
221
222 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000223 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000224 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
225 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
226
227 surface->clearCounts();
228 canvas->flush();
229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
231
232 // Case 8: unpremultiplied opaque writePixels that partially
233 // covers the canvas, preceeded by a drawREct that partially
234 // covers the canvas
235 surface->clearCounts();
236 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
237 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
238 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
239
240 surface->clearCounts();
241 SkPaint paint;
242 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
243 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
244 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
245
246 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000247 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000248 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
249 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
250
251 surface->clearCounts();
252 canvas->flush();
253 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
254 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
255}
256
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000257static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000258 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
259 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000260
junov@chromium.org66070a52013-05-28 17:39:08 +0000261 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000262
reed@google.com28183b42014-02-04 15:34:10 +0000263 // verify that clear was deferred
264 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
265
junov@chromium.org66070a52013-05-28 17:39:08 +0000266 canvas->flush();
reed@google.com28183b42014-02-04 15:34:10 +0000267
268 // verify that clear was executed
269 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000270}
271
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000272static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000273 SkRect fullRect;
274 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
275 SkIntToScalar(gHeight));
276 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000277 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
278 SkIntToScalar(1), SkIntToScalar(1));
reed@google.com28183b42014-02-04 15:34:10 +0000279
280 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
281 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000282
283 // verify that frame is intially fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000284 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000285 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000286 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000287
288 // Verify that clear triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000289 canvas->clear(0x00000000);
290 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000291
292 // Verify that clear with saved state triggers a fresh frame
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000293 canvas->save();
junov@chromium.org66070a52013-05-28 17:39:08 +0000294 canvas->clear(0x00000000);
295 canvas->restore();
296 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000297
298 // Verify that clear within a layer does NOT trigger a fresh frame
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000299 canvas->saveLayer(NULL, NULL);
junov@chromium.org66070a52013-05-28 17:39:08 +0000300 canvas->clear(0x00000000);
301 canvas->restore();
302 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000303
304 // Verify that a clear with clipping triggers a fresh frame
305 // (clear is not affected by clipping)
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000306 canvas->save();
junov@chromium.org66070a52013-05-28 17:39:08 +0000307 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
308 canvas->clear(0x00000000);
309 canvas->restore();
310 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000311
312 // Verify that full frame rects with different forms of opaque paint
313 // trigger frames to be marked as fresh
314 {
315 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000316 paint.setStyle(SkPaint::kFill_Style);
317 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000318 canvas->drawRect(fullRect, paint);
319 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000320 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000321 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000322 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000323 paint.setStyle(SkPaint::kFill_Style);
324 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000325 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000326 canvas->drawRect(fullRect, paint);
327 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000328 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000329 {
330 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000331 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000332 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000333 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000334 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000335 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000336 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
337 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000338 canvas->drawRect(fullRect, paint);
339 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000340 }
341
342 // Verify that full frame rects with different forms of non-opaque paint
343 // do not trigger frames to be marked as fresh
344 {
345 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000346 paint.setStyle(SkPaint::kFill_Style);
347 paint.setAlpha(254);
junov@chromium.org66070a52013-05-28 17:39:08 +0000348 canvas->drawRect(fullRect, paint);
349 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000350 }
351 {
352 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000353 paint.setStyle(SkPaint::kFill_Style);
354 // Defining a cone that partially overlaps the canvas
355 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
356 const SkScalar r1 = SkIntToScalar(1);
357 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
358 const SkScalar r2 = SkIntToScalar(5);
359 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
360 const SkScalar pos[2] = {0, SK_Scalar1};
361 SkShader* shader = SkGradientShader::CreateTwoPointConical(
362 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
363 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000364 canvas->drawRect(fullRect, paint);
365 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000366 }
367 {
368 SkPaint paint;
369 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000370 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000371 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000372 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000374 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
375 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000376 canvas->drawRect(fullRect, paint);
377 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000378 }
379
380 // Verify that incomplete coverage does not trigger a fresh frame
381 {
382 SkPaint paint;
383 paint.setStyle(SkPaint::kFill_Style);
384 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000385 canvas->drawRect(partialRect, paint);
386 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000387 }
388
389 // Verify that incomplete coverage due to clipping does not trigger a fresh
390 // frame
391 {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000392 canvas->save();
junov@chromium.org66070a52013-05-28 17:39:08 +0000393 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000394 SkPaint paint;
395 paint.setStyle(SkPaint::kFill_Style);
396 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000397 canvas->drawRect(fullRect, paint);
398 canvas->restore();
399 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000400 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000401 {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000402 canvas->save();
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000403 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000404 paint.setStyle(SkPaint::kFill_Style);
405 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000406 SkPath path;
407 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
junov@chromium.org66070a52013-05-28 17:39:08 +0000408 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
409 canvas->drawRect(fullRect, paint);
410 canvas->restore();
411 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000412 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000413
414 // Verify that stroked rect does not trigger a fresh frame
415 {
416 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000417 paint.setStyle(SkPaint::kStroke_Style);
418 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000419 canvas->drawRect(fullRect, paint);
420 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000421 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000422
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000423 // Verify kSrcMode triggers a fresh frame even with transparent color
424 {
425 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000426 paint.setStyle(SkPaint::kFill_Style);
427 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000428 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000429 canvas->drawRect(fullRect, paint);
430 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000431 }
432}
433
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000434class MockDevice : public SkBitmapDevice {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000435public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000436 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000437 fDrawBitmapCallCount = 0;
438 }
439 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000440 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000441 fDrawBitmapCallCount++;
442 }
443
444 int fDrawBitmapCallCount;
445};
446
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000447class NotificationCounter : public SkDeferredCanvas::NotificationClient {
448public:
449 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000450 fPrepareForDrawCount = fStorageAllocatedChangedCount =
451 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000452 }
453
454 virtual void prepareForDraw() SK_OVERRIDE {
455 fPrepareForDrawCount++;
456 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000457 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000458 fStorageAllocatedChangedCount++;
459 }
460 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000461 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000462 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000463 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
464 fSkippedPendingDrawCommandsCount++;
465 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000466
467 int fPrepareForDrawCount;
468 int fStorageAllocatedChangedCount;
469 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000470 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000471
472private:
473 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000474};
475
reed@google.com28183b42014-02-04 15:34:10 +0000476// Verifies that the deferred canvas triggers a flush when its memory
477// limit is exceeded
478static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
479 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
480 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000481
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000482 NotificationCounter notificationCounter;
reed@google.com28183b42014-02-04 15:34:10 +0000483 canvas->setNotificationClient(&notificationCounter);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000484
reed@google.com28183b42014-02-04 15:34:10 +0000485 canvas->setMaxRecordingStorage(160000);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000486
reed@google.com28183b42014-02-04 15:34:10 +0000487 SkBitmap sourceImage;
488 // 100 by 100 image, takes 40,000 bytes in memory
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000489 sourceImage.allocN32Pixels(100, 100);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000490
reed@google.com28183b42014-02-04 15:34:10 +0000491 for (int i = 0; i < 5; i++) {
492 sourceImage.notifyPixelsChanged(); // to force re-serialization
493 canvas->drawBitmap(sourceImage, 0, 0, NULL);
494 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000495
reed@google.com28183b42014-02-04 15:34:10 +0000496 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
497}
498
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000499static void TestDeferredCanvasSilentFlush(skiatest::Reporter* reporter) {
500 SkAutoTUnref<SkSurface> surface(createSurface(0));
501 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
502
503 NotificationCounter notificationCounter;
504 canvas->setNotificationClient(&notificationCounter);
505
506 canvas->silentFlush(); // will skip the initial clear that was recorded in createSurface
507
508 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
509 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
510}
511
reed@google.com28183b42014-02-04 15:34:10 +0000512static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
513 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
514 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
515
516 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000517 canvas->setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000518
519 const int imageCount = 2;
520 SkBitmap sourceImages[imageCount];
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000521 for (int i = 0; i < imageCount; i++) {
522 sourceImages[i].allocN32Pixels(100, 100);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000523 }
524
525 size_t bitmapSize = sourceImages[0].getSize();
526
junov@chromium.org66070a52013-05-28 17:39:08 +0000527 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000528 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000529 // stored bitmap + drawBitmap command
junov@chromium.org66070a52013-05-28 17:39:08 +0000530 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000531
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000532 // verify that nothing can be freed at this point
junov@chromium.org66070a52013-05-28 17:39:08 +0000533 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000534
535 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000536 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
537 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000538 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000539 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
540 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000541 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000542
543 // verify that after a flush, cached image can be freed
junov@chromium.org66070a52013-05-28 17:39:08 +0000544 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000545
546 // Verify that caching works for avoiding multiple copies of the same bitmap
junov@chromium.org66070a52013-05-28 17:39:08 +0000547 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000548 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000549 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000550 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
551 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000552 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000553
554 // Verify partial eviction based on bytesToFree
junov@chromium.org66070a52013-05-28 17:39:08 +0000555 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000556 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000557 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000558 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000559 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
560 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000561 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000562 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
563 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
564
rmistry@google.comd6176b02012-08-23 18:14:13 +0000565 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000566 // a pending draw, while image 1 is locked-in.
junov@chromium.org66070a52013-05-28 17:39:08 +0000567 canvas->freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000568 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000569 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
570 canvas->flush();
571 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
572 bytesFreed = canvas->freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000573 // only one bitmap should have been freed.
574 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
575 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
576 // Clear for next test
junov@chromium.org66070a52013-05-28 17:39:08 +0000577 canvas->flush();
578 canvas->freeMemoryIfPossible(~0U);
579 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000580
581 // Verify the image cache is sensitive to genID bumps
junov@chromium.org66070a52013-05-28 17:39:08 +0000582 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000583 sourceImages[1].notifyPixelsChanged();
junov@chromium.org66070a52013-05-28 17:39:08 +0000584 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
585 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000586
587 // Verify that nothing in this test caused commands to be skipped
588 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
589}
590
591static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000592 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
593 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
594
junov@google.com52a00ca2012-10-01 15:27:14 +0000595 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000596 canvas->setNotificationClient(&notificationCounter);
597 canvas->clear(0x0);
junov@google.com52a00ca2012-10-01 15:27:14 +0000598 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
599 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000600 canvas->flush();
junov@google.com52a00ca2012-10-01 15:27:14 +0000601 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
602 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
603
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000604}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000605
junov@chromium.orgce65f382012-10-17 19:36:09 +0000606static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
607 // This is a regression test for crbug.com/155875
608 // This test covers a code path that inserts bitmaps into the bitmap heap through the
609 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
610 // the flattening and unflattening of the shader.
reed@google.com28183b42014-02-04 15:34:10 +0000611 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
612 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.orgce65f382012-10-17 19:36:09 +0000613 // test will fail if nbIterations is not in sync with
614 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
615 const int nbIterations = 5;
616 size_t bytesAllocated = 0;
617 for(int pass = 0; pass < 2; ++pass) {
618 for(int i = 0; i < nbIterations; ++i) {
619 SkPaint paint;
620 SkBitmap paintPattern;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000621 paintPattern.allocN32Pixels(10, 10);
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000622 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000623 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000624 canvas->drawPaint(paint);
625 canvas->flush();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000626
627 // In the first pass, memory allocation should be monotonically increasing as
628 // the bitmap heap slots fill up. In the second pass memory allocation should be
629 // stable as bitmap heap slots get recycled.
junov@chromium.org66070a52013-05-28 17:39:08 +0000630 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000631 if (pass == 0) {
632 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
633 bytesAllocated = newBytesAllocated;
634 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000635 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000636 }
637 }
638 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000639 // All cached resources should be evictable since last canvas call was flush()
junov@chromium.org66070a52013-05-28 17:39:08 +0000640 canvas->freeMemoryIfPossible(~0U);
641 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000642}
643
sugoi@google.com7775fd52012-11-21 15:47:04 +0000644static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000645 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000646
sugoi@google.com7775fd52012-11-21 15:47:04 +0000647 SkBitmap sourceImage;
648 // 100 by 100 image, takes 40,000 bytes in memory
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000649 sourceImage.allocN32Pixels(100, 100);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000650
651 // 1 under : should not store the image
652 {
reed@google.com28183b42014-02-04 15:34:10 +0000653 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000654 canvas->setBitmapSizeThreshold(39999);
655 canvas->drawBitmap(sourceImage, 0, 0, NULL);
656 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000657 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
658 }
659
660 // exact value : should store the image
661 {
reed@google.com28183b42014-02-04 15:34:10 +0000662 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000663 canvas->setBitmapSizeThreshold(40000);
664 canvas->drawBitmap(sourceImage, 0, 0, NULL);
665 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000666 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
667 }
668
669 // 1 over : should still store the image
670 {
reed@google.com28183b42014-02-04 15:34:10 +0000671 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000672 canvas->setBitmapSizeThreshold(40001);
673 canvas->drawBitmap(sourceImage, 0, 0, NULL);
674 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000675 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
676 }
677}
678
junov@chromium.org67d74222013-04-12 13:33:01 +0000679
680typedef void* PixelPtr;
681// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
682// buffer. Used to test pointer equality do determine whether a surface points
683// to the same pixel data storage as before.
junov@chromium.org3c5ec8d2013-04-12 13:34:47 +0000684static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000685 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() :
686 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels();
687}
688
689static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000690 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
junov@chromium.org67d74222013-04-12 13:33:01 +0000691 SkSurface* surface;
692 bool useGpu = NULL != factory;
693#if SK_SUPPORT_GPU
694 if (useGpu) {
695 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com5fa42d12013-11-12 17:33:02 +0000696 if (NULL == context) {
697 return;
698 }
699
junov@chromium.org67d74222013-04-12 13:33:01 +0000700 surface = SkSurface::NewRenderTarget(context, imageSpec);
701 } else {
702 surface = SkSurface::NewRaster(imageSpec);
703 }
704#else
705 SkASSERT(!useGpu);
706 surface = SkSurface::NewRaster(imageSpec);
707#endif
708 SkASSERT(NULL != surface);
709 SkAutoTUnref<SkSurface> aur(surface);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000710 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org67d74222013-04-12 13:33:01 +0000711
junov@chromium.org66070a52013-05-28 17:39:08 +0000712 SkImage* image1 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000713 SkAutoTUnref<SkImage> aur_i1(image1);
714 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
715 // The following clear would normally trigger a copy on write, but
716 // it won't because rendering is deferred.
junov@chromium.org66070a52013-05-28 17:39:08 +0000717 canvas->clear(SK_ColorBLACK);
junov@chromium.org67d74222013-04-12 13:33:01 +0000718 // Obtaining a snapshot directly from the surface (as opposed to the
719 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
720 // and will therefore return the same image as the previous snapshot.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000721 SkImage* image2 = surface->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000722 SkAutoTUnref<SkImage> aur_i2(image2);
723 // Images identical because of deferral
724 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
725 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
726 // Because there is a pending clear, this will generate a different image.
junov@chromium.org66070a52013-05-28 17:39:08 +0000727 SkImage* image3 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000728 SkAutoTUnref<SkImage> aur_i3(image3);
729 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
730 // Verify that backing store is now a different buffer because of copy on
731 // write
732 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu);
733 REPORTER_ASSERT(reporter, pixels1 != pixels2);
junov@chromium.org9becf002013-04-15 18:15:23 +0000734 // Verify copy-on write with a draw operation that gets deferred by
735 // the in order draw buffer.
736 SkPaint paint;
junov@chromium.org66070a52013-05-28 17:39:08 +0000737 canvas->drawPaint(paint);
738 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
junov@chromium.org9becf002013-04-15 18:15:23 +0000739 SkAutoTUnref<SkImage> aur_i4(image4);
740 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
junov@chromium.org67d74222013-04-12 13:33:01 +0000741 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000742 REPORTER_ASSERT(reporter, pixels2 != pixels3);
junov@chromium.org67d74222013-04-12 13:33:01 +0000743 // Verify that a direct canvas flush with a pending draw does not trigger
744 // a copy on write when the surface is not sharing its buffer with an
745 // SkImage.
junov@chromium.org66070a52013-05-28 17:39:08 +0000746 canvas->clear(SK_ColorWHITE);
747 canvas->flush();
junov@chromium.org67d74222013-04-12 13:33:01 +0000748 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org66070a52013-05-28 17:39:08 +0000749 canvas->drawPaint(paint);
750 canvas->flush();
junov@chromium.org9becf002013-04-15 18:15:23 +0000751 PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu);
752 REPORTER_ASSERT(reporter, pixels4 == pixels5);
junov@chromium.org67d74222013-04-12 13:33:01 +0000753}
754
junov@chromium.org7070f762013-05-24 17:13:00 +0000755static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000756 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
junov@chromium.org7070f762013-05-24 17:13:00 +0000757 SkSurface* surface;
758 SkSurface* alternateSurface;
759 bool useGpu = NULL != factory;
760#if SK_SUPPORT_GPU
761 if (useGpu) {
762 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.come2930052013-11-13 14:26:52 +0000763 if (NULL == context) {
764 return;
765 }
junov@chromium.org7070f762013-05-24 17:13:00 +0000766 surface = SkSurface::NewRenderTarget(context, imageSpec);
767 alternateSurface = SkSurface::NewRenderTarget(context, imageSpec);
768 } else {
769 surface = SkSurface::NewRaster(imageSpec);
770 alternateSurface = SkSurface::NewRaster(imageSpec);
771 }
772#else
773 SkASSERT(!useGpu);
774 surface = SkSurface::NewRaster(imageSpec);
775 alternateSurface = SkSurface::NewRaster(imageSpec);
776#endif
777 SkASSERT(NULL != surface);
778 SkASSERT(NULL != alternateSurface);
779 SkAutoTUnref<SkSurface> aur1(surface);
780 SkAutoTUnref<SkSurface> aur2(alternateSurface);
781 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
782 PixelPtr pixels2 = getSurfacePixelPtr(alternateSurface, useGpu);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000783 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org66070a52013-05-28 17:39:08 +0000784 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
785 canvas->setSurface(alternateSurface);
786 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
junov@chromium.org7070f762013-05-24 17:13:00 +0000787 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
788 // Verify that none of the above operations triggered a surface copy on write.
789 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
790 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) == pixels2);
791 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
junov@chromium.org66070a52013-05-28 17:39:08 +0000792 canvas->clear(SK_ColorWHITE);
793 canvas->flush();
junov@chromium.org7070f762013-05-24 17:13:00 +0000794 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
795 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) != pixels2);
796}
797
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000798static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000799 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
800 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
801
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000802 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000803 canvas->setNotificationClient(&notificationCounter);
reed@google.com28183b42014-02-04 15:34:10 +0000804
reed@google.com76f10a32014-02-05 15:32:21 +0000805 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
806 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info));
807
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000808 SkRect rect = SkRect::MakeWH(5, 5);
809 SkPaint paint;
810 // After spawning a compatible canvas:
811 // 1) Verify that secondary canvas is usable and does not report to the notification client.
reed@google.com76f10a32014-02-05 15:32:21 +0000812 surface->getCanvas()->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000813 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
814 // 2) Verify that original canvas is usable and still reports to the notification client.
junov@chromium.org66070a52013-05-28 17:39:08 +0000815 canvas->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000816 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
817}
818
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000819DEF_GPUTEST(DeferredCanvas, reporter, factory) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000820 TestDeferredCanvasBitmapAccess(reporter);
821 TestDeferredCanvasFlush(reporter);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000822 TestDeferredCanvasSilentFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000823 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000824 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000825 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000826 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000827 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000828 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000829 TestDeferredCanvasCreateCompatibleDevice(reporter);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000830 TestDeferredCanvasWritePixelsToSurface(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000831 TestDeferredCanvasSurface(reporter, NULL);
junov@chromium.org7070f762013-05-24 17:13:00 +0000832 TestDeferredCanvasSetSurface(reporter, NULL);
junov@chromium.org67d74222013-04-12 13:33:01 +0000833 if (NULL != factory) {
834 TestDeferredCanvasSurface(reporter, factory);
junov@chromium.org7070f762013-05-24 17:13:00 +0000835 TestDeferredCanvasSetSurface(reporter, factory);
junov@chromium.org67d74222013-04-12 13:33:01 +0000836 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000837}