blob: 132736e5f7eb934873fe3b35f374765ac4929b20 [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
junov@chromium.org1f9767c2012-02-07 16:27:57 +00008#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009#include "TestClassDef.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"
junov@chromium.org44324fa2013-08-02 15:36:02 +000016#include "../src/image/SkSurface_Base.h"
17#include "../src/image/SkImagePriv.h"
junov@chromium.org67d74222013-04-12 13:33:01 +000018#if SK_SUPPORT_GPU
19#include "GrContextFactory.h"
20#else
21class GrContextFactory;
22#endif
junov@chromium.org1f9767c2012-02-07 16:27:57 +000023
junov@chromium.org1f9767c2012-02-07 16:27:57 +000024static const int gWidth = 2;
25static const int gHeight = 2;
26
27static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
28 bm->setConfig(config, gWidth, gHeight);
29 bm->allocPixels();
30 bm->eraseColor(color);
31}
32
33static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
34 SkBitmap store;
35
36 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000037 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000038 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org1f9767c2012-02-07 16:27:57 +000039
junov@chromium.org66070a52013-05-28 17:39:08 +000040 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000041
42 SkAutoLockPixels alp(store);
43 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
junov@chromium.org66070a52013-05-28 17:39:08 +000044 SkBitmap accessed = canvas->getDevice()->accessBitmap(false);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000045 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
46 REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef());
47}
48
junov@chromium.org44324fa2013-08-02 15:36:02 +000049class MockSurface : public SkSurface_Base {
50public:
51 MockSurface(int width, int height) : SkSurface_Base(width, height) {
52 clearCounts();
53 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
54 fBitmap.allocPixels();
55 }
56
57 virtual SkCanvas* onNewCanvas() SK_OVERRIDE {
58 return SkNEW_ARGS(SkCanvas, (fBitmap));
59 }
60
reed@google.com2bd8b812013-11-01 13:46:54 +000061 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000062 return NULL;
63 }
64
65 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE {
66 return SkNewImageFromBitmap(fBitmap, true);
67 }
68
69 virtual void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
70 if (mode == SkSurface::kDiscard_ContentChangeMode) {
71 fDiscardCount++;
72 } else {
73 fRetainCount++;
74 }
75 }
76
77 void clearCounts() {
78 fDiscardCount = 0;
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +000079 fRetainCount = 0;
junov@chromium.org44324fa2013-08-02 15:36:02 +000080 }
81
82 int fDiscardCount, fRetainCount;
83 SkBitmap fBitmap;
84};
85
86static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
87 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000088 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org44324fa2013-08-02 15:36:02 +000089
90 SkBitmap srcBitmap;
91 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
92 srcBitmap.allocPixels();
93 srcBitmap.eraseColor(SK_ColorGREEN);
94 // Tests below depend on this bitmap being recognized as opaque
95
96 // Preliminary sanity check: no copy on write if no active snapshot
97 surface->clearCounts();
98 canvas->clear(SK_ColorWHITE);
99 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
100 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
101
102 surface->clearCounts();
103 canvas->flush();
104 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
105 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
106
107 // Case 1: Discard notification happens upon flushing
108 // with an Image attached.
109 surface->clearCounts();
110 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
111 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
112 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
113
114 surface->clearCounts();
115 canvas->clear(SK_ColorWHITE);
116 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
117 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
118
119 surface->clearCounts();
120 canvas->flush();
121 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
122 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
123
124 // Case 2: Opaque writePixels
125 surface->clearCounts();
126 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
127 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
128 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
129
130 surface->clearCounts();
131 canvas->writePixels(srcBitmap, 0, 0);
132 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
133 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
134
135 surface->clearCounts();
136 canvas->flush();
137 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
138 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
139
140 // Case 3: writePixels that partially covers the canvas
141 surface->clearCounts();
142 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
143 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
144 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
145
146 surface->clearCounts();
147 canvas->writePixels(srcBitmap, 5, 0);
148 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
149 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
150
151 surface->clearCounts();
152 canvas->flush();
153 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
154 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
155
156 // Case 4: unpremultiplied opaque writePixels that entirely
157 // covers the canvas
158 surface->clearCounts();
159 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
160 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
161 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
162
163 surface->clearCounts();
164 canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
165 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
166 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
167
168 surface->clearCounts();
169 canvas->flush();
170 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
171 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
172
173 // Case 5: unpremultiplied opaque writePixels that partially
174 // covers the canvas
175 surface->clearCounts();
176 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
177 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
178 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
179
180 surface->clearCounts();
181 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
182 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
183 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
184
185 surface->clearCounts();
186 canvas->flush();
187 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
188 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
189
190 // Case 6: unpremultiplied opaque writePixels that entirely
191 // covers the canvas, preceded by clear
192 surface->clearCounts();
193 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
194 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
195 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
196
197 surface->clearCounts();
198 canvas->clear(SK_ColorWHITE);
199 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
200 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
201
202 surface->clearCounts();
203 canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
204 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
205 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
206
207 surface->clearCounts();
208 canvas->flush();
209 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
210 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
211
212 // Case 7: unpremultiplied opaque writePixels that partially
213 // covers the canvas, preceeded by a clear
214 surface->clearCounts();
215 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
216 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
217 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
218
219 surface->clearCounts();
220 canvas->clear(SK_ColorWHITE);
221 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
222 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
223
224 surface->clearCounts();
225 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
226 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
227 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
228
229 surface->clearCounts();
230 canvas->flush();
231 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
232 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
233
234 // Case 8: unpremultiplied opaque writePixels that partially
235 // covers the canvas, preceeded by a drawREct that partially
236 // covers the canvas
237 surface->clearCounts();
238 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
239 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
240 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
241
242 surface->clearCounts();
243 SkPaint paint;
244 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
245 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
246 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
247
248 surface->clearCounts();
249 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
250 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
251 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
252
253 surface->clearCounts();
254 canvas->flush();
255 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
256 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
257}
258
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000259static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
260 SkBitmap store;
261
262 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000263 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000264 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000265
junov@chromium.org66070a52013-05-28 17:39:08 +0000266 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000267
268 SkAutoLockPixels alp(store);
269 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
junov@chromium.org66070a52013-05-28 17:39:08 +0000270 canvas->flush();
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000271 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
272}
273
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000274static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
275 SkBitmap store;
276 SkRect fullRect;
277 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
278 SkIntToScalar(gHeight));
279 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000280 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
281 SkIntToScalar(1), SkIntToScalar(1));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000282 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000283 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000284 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000285
286 // verify that frame is intially fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000287 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000288 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000289 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000290
291 // Verify that clear triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000292 canvas->clear(0x00000000);
293 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000294
295 // Verify that clear with saved state triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000296 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
297 canvas->clear(0x00000000);
298 canvas->restore();
299 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000300
301 // Verify that clear within a layer does NOT trigger a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000302 canvas->saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
303 canvas->clear(0x00000000);
304 canvas->restore();
305 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000306
307 // Verify that a clear with clipping triggers a fresh frame
308 // (clear is not affected by clipping)
junov@chromium.org66070a52013-05-28 17:39:08 +0000309 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
310 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
311 canvas->clear(0x00000000);
312 canvas->restore();
313 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000314
315 // Verify that full frame rects with different forms of opaque paint
316 // trigger frames to be marked as fresh
317 {
318 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000319 paint.setStyle(SkPaint::kFill_Style);
320 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000321 canvas->drawRect(fullRect, paint);
322 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000323 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000324 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000325 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000326 paint.setStyle(SkPaint::kFill_Style);
327 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000328 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000329 canvas->drawRect(fullRect, paint);
330 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000331 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000332 {
333 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000334 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000335 SkBitmap bmp;
336 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000337 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000338 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000339 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
340 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000341 canvas->drawRect(fullRect, paint);
342 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000343 }
344
345 // Verify that full frame rects with different forms of non-opaque paint
346 // do not trigger frames to be marked as fresh
347 {
348 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000349 paint.setStyle(SkPaint::kFill_Style);
350 paint.setAlpha(254);
junov@chromium.org66070a52013-05-28 17:39:08 +0000351 canvas->drawRect(fullRect, paint);
352 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000353 }
354 {
355 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000356 paint.setStyle(SkPaint::kFill_Style);
357 // Defining a cone that partially overlaps the canvas
358 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
359 const SkScalar r1 = SkIntToScalar(1);
360 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
361 const SkScalar r2 = SkIntToScalar(5);
362 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
363 const SkScalar pos[2] = {0, SK_Scalar1};
364 SkShader* shader = SkGradientShader::CreateTwoPointConical(
365 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
366 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000367 canvas->drawRect(fullRect, paint);
368 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000369 }
370 {
371 SkPaint paint;
372 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000373 SkBitmap bmp;
374 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000375 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000376 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000377 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
378 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000379 canvas->drawRect(fullRect, paint);
380 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000381 }
382
383 // Verify that incomplete coverage does not trigger a fresh frame
384 {
385 SkPaint paint;
386 paint.setStyle(SkPaint::kFill_Style);
387 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000388 canvas->drawRect(partialRect, paint);
389 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000390 }
391
392 // Verify that incomplete coverage due to clipping does not trigger a fresh
393 // frame
394 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000395 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
396 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000397 SkPaint paint;
398 paint.setStyle(SkPaint::kFill_Style);
399 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000400 canvas->drawRect(fullRect, paint);
401 canvas->restore();
402 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000403 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000404 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000405 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000406 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000407 paint.setStyle(SkPaint::kFill_Style);
408 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000409 SkPath path;
410 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
junov@chromium.org66070a52013-05-28 17:39:08 +0000411 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
412 canvas->drawRect(fullRect, paint);
413 canvas->restore();
414 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000415 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000416
417 // Verify that stroked rect does not trigger a fresh frame
418 {
419 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000420 paint.setStyle(SkPaint::kStroke_Style);
421 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000422 canvas->drawRect(fullRect, paint);
423 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000424 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000425
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000426 // Verify kSrcMode triggers a fresh frame even with transparent color
427 {
428 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000429 paint.setStyle(SkPaint::kFill_Style);
430 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000431 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000432 canvas->drawRect(fullRect, paint);
433 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000434 }
435}
436
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000437class MockDevice : public SkBitmapDevice {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000438public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000439 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000440 fDrawBitmapCallCount = 0;
441 }
442 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000443 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000444 fDrawBitmapCallCount++;
445 }
446
447 int fDrawBitmapCallCount;
448};
449
450// Verifies that the deferred canvas triggers a flush when its memory
451// limit is exceeded
452static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
453 SkBitmap store;
454 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
455 store.allocPixels();
456 MockDevice mockDevice(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000457 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&mockDevice));
junov@chromium.org66070a52013-05-28 17:39:08 +0000458 canvas->setMaxRecordingStorage(160000);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000459
460 SkBitmap sourceImage;
461 // 100 by 100 image, takes 40,000 bytes in memory
462 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
463 sourceImage.allocPixels();
464
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000465 for (int i = 0; i < 5; i++) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000466 sourceImage.notifyPixelsChanged(); // to force re-serialization
junov@chromium.org66070a52013-05-28 17:39:08 +0000467 canvas->drawBitmap(sourceImage, 0, 0, NULL);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000468 }
469
scroggo@google.com15011ee2012-07-26 20:03:32 +0000470 REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000471}
472
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000473class NotificationCounter : public SkDeferredCanvas::NotificationClient {
474public:
475 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000476 fPrepareForDrawCount = fStorageAllocatedChangedCount =
477 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000478 }
479
480 virtual void prepareForDraw() SK_OVERRIDE {
481 fPrepareForDrawCount++;
482 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000483 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000484 fStorageAllocatedChangedCount++;
485 }
486 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000487 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000488 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000489 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
490 fSkippedPendingDrawCommandsCount++;
491 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000492
493 int fPrepareForDrawCount;
494 int fStorageAllocatedChangedCount;
495 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000496 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000497
498private:
499 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000500};
501
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000502static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
503 SkBitmap store;
504 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
505 store.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000506 SkBitmapDevice device(store);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000507 NotificationCounter notificationCounter;
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000508 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000509 canvas->setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000510
511 const int imageCount = 2;
512 SkBitmap sourceImages[imageCount];
513 for (int i = 0; i < imageCount; i++)
514 {
515 sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
516 sourceImages[i].allocPixels();
517 }
518
519 size_t bitmapSize = sourceImages[0].getSize();
520
junov@chromium.org66070a52013-05-28 17:39:08 +0000521 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000522 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000523 // stored bitmap + drawBitmap command
junov@chromium.org66070a52013-05-28 17:39:08 +0000524 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000525
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000526 // verify that nothing can be freed at this point
junov@chromium.org66070a52013-05-28 17:39:08 +0000527 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000528
529 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000530 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
531 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000532 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000533 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
534 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000535 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000536
537 // verify that after a flush, cached image can be freed
junov@chromium.org66070a52013-05-28 17:39:08 +0000538 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000539
540 // Verify that caching works for avoiding multiple copies of the same bitmap
junov@chromium.org66070a52013-05-28 17:39:08 +0000541 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000542 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000543 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000544 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
545 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000546 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000547
548 // Verify partial eviction based on bytesToFree
junov@chromium.org66070a52013-05-28 17:39:08 +0000549 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000550 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000551 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000552 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000553 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
554 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000555 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000556 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
557 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
558
rmistry@google.comd6176b02012-08-23 18:14:13 +0000559 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000560 // a pending draw, while image 1 is locked-in.
junov@chromium.org66070a52013-05-28 17:39:08 +0000561 canvas->freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000562 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000563 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
564 canvas->flush();
565 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
566 bytesFreed = canvas->freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000567 // only one bitmap should have been freed.
568 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
569 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
570 // Clear for next test
junov@chromium.org66070a52013-05-28 17:39:08 +0000571 canvas->flush();
572 canvas->freeMemoryIfPossible(~0U);
573 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000574
575 // Verify the image cache is sensitive to genID bumps
junov@chromium.org66070a52013-05-28 17:39:08 +0000576 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000577 sourceImages[1].notifyPixelsChanged();
junov@chromium.org66070a52013-05-28 17:39:08 +0000578 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
579 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000580
581 // Verify that nothing in this test caused commands to be skipped
582 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
583}
584
585static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
586 SkBitmap store;
587 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
588 store.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000589 SkBitmapDevice device(store);
junov@google.com52a00ca2012-10-01 15:27:14 +0000590 NotificationCounter notificationCounter;
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000591 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000592 canvas->setNotificationClient(&notificationCounter);
593 canvas->clear(0x0);
junov@google.com52a00ca2012-10-01 15:27:14 +0000594 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
595 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000596 canvas->flush();
junov@google.com52a00ca2012-10-01 15:27:14 +0000597 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
598 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
599
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000600}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000601
junov@chromium.orgce65f382012-10-17 19:36:09 +0000602static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
603 // This is a regression test for crbug.com/155875
604 // This test covers a code path that inserts bitmaps into the bitmap heap through the
605 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
606 // the flattening and unflattening of the shader.
607 SkBitmap store;
608 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
609 store.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000610 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000611 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.orgce65f382012-10-17 19:36:09 +0000612 // test will fail if nbIterations is not in sync with
613 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
614 const int nbIterations = 5;
615 size_t bytesAllocated = 0;
616 for(int pass = 0; pass < 2; ++pass) {
617 for(int i = 0; i < nbIterations; ++i) {
618 SkPaint paint;
619 SkBitmap paintPattern;
620 paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
621 paintPattern.allocPixels();
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) {
645 SkBitmap store;
646 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
647 store.allocPixels();
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000648
sugoi@google.com7775fd52012-11-21 15:47:04 +0000649 SkBitmap sourceImage;
650 // 100 by 100 image, takes 40,000 bytes in memory
651 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
652 sourceImage.allocPixels();
653
654 // 1 under : should not store the image
655 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000656 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000657 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000658 canvas->setBitmapSizeThreshold(39999);
659 canvas->drawBitmap(sourceImage, 0, 0, NULL);
660 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000661 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
662 }
663
664 // exact value : should store the image
665 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000666 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000667 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000668 canvas->setBitmapSizeThreshold(40000);
669 canvas->drawBitmap(sourceImage, 0, 0, NULL);
670 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000671 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
672 }
673
674 // 1 over : should still store the image
675 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000676 SkBitmapDevice device(store);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000677 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000678 canvas->setBitmapSizeThreshold(40001);
679 canvas->drawBitmap(sourceImage, 0, 0, NULL);
680 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000681 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
682 }
683}
684
junov@chromium.org67d74222013-04-12 13:33:01 +0000685
686typedef void* PixelPtr;
687// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
688// buffer. Used to test pointer equality do determine whether a surface points
689// to the same pixel data storage as before.
junov@chromium.org3c5ec8d2013-04-12 13:34:47 +0000690static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000691 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() :
692 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels();
693}
694
695static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com2bd8b812013-11-01 13:46:54 +0000696 SkImageInfo imageSpec = {
junov@chromium.org67d74222013-04-12 13:33:01 +0000697 10, // width
698 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +0000699 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +0000700 kPremul_SkAlphaType
junov@chromium.org67d74222013-04-12 13:33:01 +0000701 };
702 SkSurface* surface;
703 bool useGpu = NULL != factory;
704#if SK_SUPPORT_GPU
705 if (useGpu) {
706 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com5fa42d12013-11-12 17:33:02 +0000707 if (NULL == context) {
708 return;
709 }
710
junov@chromium.org67d74222013-04-12 13:33:01 +0000711 surface = SkSurface::NewRenderTarget(context, imageSpec);
712 } else {
713 surface = SkSurface::NewRaster(imageSpec);
714 }
715#else
716 SkASSERT(!useGpu);
717 surface = SkSurface::NewRaster(imageSpec);
718#endif
719 SkASSERT(NULL != surface);
720 SkAutoTUnref<SkSurface> aur(surface);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000721 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org67d74222013-04-12 13:33:01 +0000722
junov@chromium.org66070a52013-05-28 17:39:08 +0000723 SkImage* image1 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000724 SkAutoTUnref<SkImage> aur_i1(image1);
725 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
726 // The following clear would normally trigger a copy on write, but
727 // it won't because rendering is deferred.
junov@chromium.org66070a52013-05-28 17:39:08 +0000728 canvas->clear(SK_ColorBLACK);
junov@chromium.org67d74222013-04-12 13:33:01 +0000729 // Obtaining a snapshot directly from the surface (as opposed to the
730 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
731 // and will therefore return the same image as the previous snapshot.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000732 SkImage* image2 = surface->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000733 SkAutoTUnref<SkImage> aur_i2(image2);
734 // Images identical because of deferral
735 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
736 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
737 // Because there is a pending clear, this will generate a different image.
junov@chromium.org66070a52013-05-28 17:39:08 +0000738 SkImage* image3 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000739 SkAutoTUnref<SkImage> aur_i3(image3);
740 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
741 // Verify that backing store is now a different buffer because of copy on
742 // write
743 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu);
744 REPORTER_ASSERT(reporter, pixels1 != pixels2);
junov@chromium.org9becf002013-04-15 18:15:23 +0000745 // Verify copy-on write with a draw operation that gets deferred by
746 // the in order draw buffer.
747 SkPaint paint;
junov@chromium.org66070a52013-05-28 17:39:08 +0000748 canvas->drawPaint(paint);
749 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
junov@chromium.org9becf002013-04-15 18:15:23 +0000750 SkAutoTUnref<SkImage> aur_i4(image4);
751 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
junov@chromium.org67d74222013-04-12 13:33:01 +0000752 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000753 REPORTER_ASSERT(reporter, pixels2 != pixels3);
junov@chromium.org67d74222013-04-12 13:33:01 +0000754 // Verify that a direct canvas flush with a pending draw does not trigger
755 // a copy on write when the surface is not sharing its buffer with an
756 // SkImage.
junov@chromium.org66070a52013-05-28 17:39:08 +0000757 canvas->clear(SK_ColorWHITE);
758 canvas->flush();
junov@chromium.org67d74222013-04-12 13:33:01 +0000759 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org66070a52013-05-28 17:39:08 +0000760 canvas->drawPaint(paint);
761 canvas->flush();
junov@chromium.org9becf002013-04-15 18:15:23 +0000762 PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu);
763 REPORTER_ASSERT(reporter, pixels4 == pixels5);
junov@chromium.org67d74222013-04-12 13:33:01 +0000764}
765
junov@chromium.org7070f762013-05-24 17:13:00 +0000766static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com2bd8b812013-11-01 13:46:54 +0000767 SkImageInfo imageSpec = {
junov@chromium.org7070f762013-05-24 17:13:00 +0000768 10, // width
769 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +0000770 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +0000771 kPremul_SkAlphaType
junov@chromium.org7070f762013-05-24 17:13:00 +0000772 };
773 SkSurface* surface;
774 SkSurface* alternateSurface;
775 bool useGpu = NULL != factory;
776#if SK_SUPPORT_GPU
777 if (useGpu) {
778 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.come2930052013-11-13 14:26:52 +0000779 if (NULL == context) {
780 return;
781 }
junov@chromium.org7070f762013-05-24 17:13:00 +0000782 surface = SkSurface::NewRenderTarget(context, imageSpec);
783 alternateSurface = SkSurface::NewRenderTarget(context, imageSpec);
784 } else {
785 surface = SkSurface::NewRaster(imageSpec);
786 alternateSurface = SkSurface::NewRaster(imageSpec);
787 }
788#else
789 SkASSERT(!useGpu);
790 surface = SkSurface::NewRaster(imageSpec);
791 alternateSurface = SkSurface::NewRaster(imageSpec);
792#endif
793 SkASSERT(NULL != surface);
794 SkASSERT(NULL != alternateSurface);
795 SkAutoTUnref<SkSurface> aur1(surface);
796 SkAutoTUnref<SkSurface> aur2(alternateSurface);
797 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
798 PixelPtr pixels2 = getSurfacePixelPtr(alternateSurface, useGpu);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000799 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org66070a52013-05-28 17:39:08 +0000800 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
801 canvas->setSurface(alternateSurface);
802 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
junov@chromium.org7070f762013-05-24 17:13:00 +0000803 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
804 // Verify that none of the above operations triggered a surface copy on write.
805 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
806 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) == pixels2);
807 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
junov@chromium.org66070a52013-05-28 17:39:08 +0000808 canvas->clear(SK_ColorWHITE);
809 canvas->flush();
junov@chromium.org7070f762013-05-24 17:13:00 +0000810 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
811 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) != pixels2);
812}
813
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000814static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
815 SkBitmap store;
816 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
817 store.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000818 SkBitmapDevice device(store);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000819 NotificationCounter notificationCounter;
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000820 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device));
junov@chromium.org66070a52013-05-28 17:39:08 +0000821 canvas->setNotificationClient(&notificationCounter);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000822 SkAutoTUnref<SkBaseDevice> secondaryDevice(canvas->createCompatibleDevice(
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000823 SkBitmap::kARGB_8888_Config, 10, 10, device.isOpaque()));
824 SkCanvas secondaryCanvas(secondaryDevice.get());
825 SkRect rect = SkRect::MakeWH(5, 5);
826 SkPaint paint;
827 // After spawning a compatible canvas:
828 // 1) Verify that secondary canvas is usable and does not report to the notification client.
829 secondaryCanvas.drawRect(rect, paint);
830 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
831 // 2) Verify that original canvas is usable and still reports to the notification client.
junov@chromium.org66070a52013-05-28 17:39:08 +0000832 canvas->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000833 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
834}
835
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000836DEF_GPUTEST(DeferredCanvas, reporter, factory) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000837 TestDeferredCanvasBitmapAccess(reporter);
838 TestDeferredCanvasFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000839 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000840 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000841 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000842 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000843 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000844 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000845 TestDeferredCanvasCreateCompatibleDevice(reporter);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000846 TestDeferredCanvasWritePixelsToSurface(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000847 TestDeferredCanvasSurface(reporter, NULL);
junov@chromium.org7070f762013-05-24 17:13:00 +0000848 TestDeferredCanvasSetSurface(reporter, NULL);
junov@chromium.org67d74222013-04-12 13:33:01 +0000849 if (NULL != factory) {
850 TestDeferredCanvasSurface(reporter, factory);
junov@chromium.org7070f762013-05-24 17:13:00 +0000851 TestDeferredCanvasSetSurface(reporter, factory);
junov@chromium.org67d74222013-04-12 13:33:01 +0000852 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000853}