blob: 0ab482b7ed0c013a3408ed67ec99deff860a864d [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"
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
reed@google.com28183b42014-02-04 15:34:10 +000033static SkSurface* createSurface(SkColor color) {
34 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight);
35 surface->getCanvas()->clear(color);
36 return surface;
37}
38
39static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
40 SkPMColor pixel = 0;
41 SkBitmap bitmap;
42 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4, NULL, NULL);
43 SkCanvas canvas(bitmap);
44
45 SkPaint paint;
46 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@google.comd52a9972014-02-04 16:14:58 +000047 surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
reed@google.com28183b42014-02-04 15:34:10 +000048 return pixel;
49}
50
junov@chromium.org1f9767c2012-02-07 16:27:57 +000051static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
52 SkBitmap store;
53
reed@google.com28183b42014-02-04 15:34:10 +000054 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
55 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
56
junov@chromium.org66070a52013-05-28 17:39:08 +000057 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000058
reed@google.com28183b42014-02-04 15:34:10 +000059 // verify that the clear() was deferred
60 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
61
junov@chromium.org66070a52013-05-28 17:39:08 +000062 SkBitmap accessed = canvas->getDevice()->accessBitmap(false);
reed@google.com28183b42014-02-04 15:34:10 +000063
64 // verify that clear was executed
65 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +000066}
67
junov@chromium.org44324fa2013-08-02 15:36:02 +000068class MockSurface : public SkSurface_Base {
69public:
70 MockSurface(int width, int height) : SkSurface_Base(width, height) {
71 clearCounts();
72 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
73 fBitmap.allocPixels();
74 }
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;
110 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
111 srcBitmap.allocPixels();
112 srcBitmap.eraseColor(SK_ColorGREEN);
113 // Tests below depend on this bitmap being recognized as opaque
114
115 // Preliminary sanity check: no copy on write if no active snapshot
116 surface->clearCounts();
117 canvas->clear(SK_ColorWHITE);
118 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
119 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
120
121 surface->clearCounts();
122 canvas->flush();
123 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
124 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
125
126 // Case 1: Discard notification happens upon flushing
127 // with an Image attached.
128 surface->clearCounts();
129 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
130 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
131 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
132
133 surface->clearCounts();
134 canvas->clear(SK_ColorWHITE);
135 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
136 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
137
138 surface->clearCounts();
139 canvas->flush();
140 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
141 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
142
143 // Case 2: Opaque writePixels
144 surface->clearCounts();
145 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
146 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
147 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
148
149 surface->clearCounts();
150 canvas->writePixels(srcBitmap, 0, 0);
151 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
152 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
153
154 surface->clearCounts();
155 canvas->flush();
156 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
157 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
158
159 // Case 3: writePixels that partially covers the canvas
160 surface->clearCounts();
161 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
162 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
163 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
164
165 surface->clearCounts();
166 canvas->writePixels(srcBitmap, 5, 0);
167 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
168 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
169
170 surface->clearCounts();
171 canvas->flush();
172 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
173 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
174
175 // Case 4: unpremultiplied opaque writePixels that entirely
176 // covers the canvas
177 surface->clearCounts();
178 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
179 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
180 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
181
182 surface->clearCounts();
183 canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
184 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
185 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
186
187 surface->clearCounts();
188 canvas->flush();
189 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
190 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
191
192 // Case 5: unpremultiplied opaque writePixels that partially
193 // covers the canvas
194 surface->clearCounts();
195 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
196 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
197 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
198
199 surface->clearCounts();
200 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
201 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
202 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
203
204 surface->clearCounts();
205 canvas->flush();
206 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
207 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
208
209 // Case 6: unpremultiplied opaque writePixels that entirely
210 // covers the canvas, preceded by clear
211 surface->clearCounts();
212 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
213 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
214 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
215
216 surface->clearCounts();
217 canvas->clear(SK_ColorWHITE);
218 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
219 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
220
221 surface->clearCounts();
222 canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
223 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
224 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
225
226 surface->clearCounts();
227 canvas->flush();
228 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
229 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
230
231 // Case 7: unpremultiplied opaque writePixels that partially
232 // covers the canvas, preceeded by a clear
233 surface->clearCounts();
234 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
235 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
236 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
237
238 surface->clearCounts();
239 canvas->clear(SK_ColorWHITE);
240 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
241 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
242
243 surface->clearCounts();
244 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
245 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
246 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
247
248 surface->clearCounts();
249 canvas->flush();
250 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
251 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
252
253 // Case 8: unpremultiplied opaque writePixels that partially
254 // covers the canvas, preceeded by a drawREct that partially
255 // covers the canvas
256 surface->clearCounts();
257 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
258 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
259 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
260
261 surface->clearCounts();
262 SkPaint paint;
263 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
264 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
265 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
266
267 surface->clearCounts();
268 canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
269 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
270 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
271
272 surface->clearCounts();
273 canvas->flush();
274 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
275 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
276}
277
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000278static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000279 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
280 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000281
junov@chromium.org66070a52013-05-28 17:39:08 +0000282 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000283
reed@google.com28183b42014-02-04 15:34:10 +0000284 // verify that clear was deferred
285 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
286
junov@chromium.org66070a52013-05-28 17:39:08 +0000287 canvas->flush();
reed@google.com28183b42014-02-04 15:34:10 +0000288
289 // verify that clear was executed
290 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000291}
292
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000293static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000294 SkRect fullRect;
295 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
296 SkIntToScalar(gHeight));
297 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000298 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
299 SkIntToScalar(1), SkIntToScalar(1));
reed@google.com28183b42014-02-04 15:34:10 +0000300
301 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
302 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000303
304 // verify that frame is intially fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000305 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000306 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000307 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000308
309 // Verify that clear triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000310 canvas->clear(0x00000000);
311 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000312
313 // Verify that clear with saved state triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000314 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
315 canvas->clear(0x00000000);
316 canvas->restore();
317 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000318
319 // Verify that clear within a layer does NOT trigger a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000320 canvas->saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
321 canvas->clear(0x00000000);
322 canvas->restore();
323 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000324
325 // Verify that a clear with clipping triggers a fresh frame
326 // (clear is not affected by clipping)
junov@chromium.org66070a52013-05-28 17:39:08 +0000327 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
328 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
329 canvas->clear(0x00000000);
330 canvas->restore();
331 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000332
333 // Verify that full frame rects with different forms of opaque paint
334 // trigger frames to be marked as fresh
335 {
336 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000337 paint.setStyle(SkPaint::kFill_Style);
338 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000339 canvas->drawRect(fullRect, paint);
340 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000341 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000342 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000343 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000344 paint.setStyle(SkPaint::kFill_Style);
345 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000346 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000347 canvas->drawRect(fullRect, paint);
348 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000349 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000350 {
351 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000352 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000353 SkBitmap bmp;
354 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000355 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000357 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
358 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000359 canvas->drawRect(fullRect, paint);
360 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000361 }
362
363 // Verify that full frame rects with different forms of non-opaque paint
364 // do not trigger frames to be marked as fresh
365 {
366 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000367 paint.setStyle(SkPaint::kFill_Style);
368 paint.setAlpha(254);
junov@chromium.org66070a52013-05-28 17:39:08 +0000369 canvas->drawRect(fullRect, paint);
370 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000371 }
372 {
373 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000374 paint.setStyle(SkPaint::kFill_Style);
375 // Defining a cone that partially overlaps the canvas
376 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
377 const SkScalar r1 = SkIntToScalar(1);
378 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
379 const SkScalar r2 = SkIntToScalar(5);
380 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
381 const SkScalar pos[2] = {0, SK_Scalar1};
382 SkShader* shader = SkGradientShader::CreateTwoPointConical(
383 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
384 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000385 canvas->drawRect(fullRect, paint);
386 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000387 }
388 {
389 SkPaint paint;
390 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000391 SkBitmap bmp;
392 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000393 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000394 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000395 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
396 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000397 canvas->drawRect(fullRect, paint);
398 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000399 }
400
401 // Verify that incomplete coverage does not trigger a fresh frame
402 {
403 SkPaint paint;
404 paint.setStyle(SkPaint::kFill_Style);
405 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000406 canvas->drawRect(partialRect, paint);
407 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000408 }
409
410 // Verify that incomplete coverage due to clipping does not trigger a fresh
411 // frame
412 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000413 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
414 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000415 SkPaint paint;
416 paint.setStyle(SkPaint::kFill_Style);
417 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000418 canvas->drawRect(fullRect, paint);
419 canvas->restore();
420 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000421 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000422 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000423 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000424 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000425 paint.setStyle(SkPaint::kFill_Style);
426 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000427 SkPath path;
428 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
junov@chromium.org66070a52013-05-28 17:39:08 +0000429 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
430 canvas->drawRect(fullRect, paint);
431 canvas->restore();
432 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000433 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000434
435 // Verify that stroked rect does not trigger a fresh frame
436 {
437 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000438 paint.setStyle(SkPaint::kStroke_Style);
439 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000440 canvas->drawRect(fullRect, paint);
441 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000442 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000443
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000444 // Verify kSrcMode triggers a fresh frame even with transparent color
445 {
446 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000447 paint.setStyle(SkPaint::kFill_Style);
448 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000449 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000450 canvas->drawRect(fullRect, paint);
451 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000452 }
453}
454
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000455class MockDevice : public SkBitmapDevice {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000456public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000457 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000458 fDrawBitmapCallCount = 0;
459 }
460 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000461 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000462 fDrawBitmapCallCount++;
463 }
464
465 int fDrawBitmapCallCount;
466};
467
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000468class NotificationCounter : public SkDeferredCanvas::NotificationClient {
469public:
470 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000471 fPrepareForDrawCount = fStorageAllocatedChangedCount =
472 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000473 }
474
475 virtual void prepareForDraw() SK_OVERRIDE {
476 fPrepareForDrawCount++;
477 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000478 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000479 fStorageAllocatedChangedCount++;
480 }
481 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000482 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000483 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000484 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
485 fSkippedPendingDrawCommandsCount++;
486 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000487
488 int fPrepareForDrawCount;
489 int fStorageAllocatedChangedCount;
490 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000491 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000492
493private:
494 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000495};
496
reed@google.com28183b42014-02-04 15:34:10 +0000497// Verifies that the deferred canvas triggers a flush when its memory
498// limit is exceeded
499static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
500 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
501 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
502
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000503 NotificationCounter notificationCounter;
reed@google.com28183b42014-02-04 15:34:10 +0000504 canvas->setNotificationClient(&notificationCounter);
505
506 canvas->setMaxRecordingStorage(160000);
507
508 SkBitmap sourceImage;
509 // 100 by 100 image, takes 40,000 bytes in memory
510 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
511 sourceImage.allocPixels();
512
513 for (int i = 0; i < 5; i++) {
514 sourceImage.notifyPixelsChanged(); // to force re-serialization
515 canvas->drawBitmap(sourceImage, 0, 0, NULL);
516 }
517
518 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
519}
520
521static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
522 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
523 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
524
525 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000526 canvas->setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000527
528 const int imageCount = 2;
529 SkBitmap sourceImages[imageCount];
530 for (int i = 0; i < imageCount; i++)
531 {
532 sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
533 sourceImages[i].allocPixels();
534 }
535
536 size_t bitmapSize = sourceImages[0].getSize();
537
junov@chromium.org66070a52013-05-28 17:39:08 +0000538 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000539 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000540 // stored bitmap + drawBitmap command
junov@chromium.org66070a52013-05-28 17:39:08 +0000541 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000542
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000543 // verify that nothing can be freed at this point
junov@chromium.org66070a52013-05-28 17:39:08 +0000544 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000545
546 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000547 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
548 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000549 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000550 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
551 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000552 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000553
554 // verify that after a flush, cached image can be freed
junov@chromium.org66070a52013-05-28 17:39:08 +0000555 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000556
557 // Verify that caching works for avoiding multiple copies of the same bitmap
junov@chromium.org66070a52013-05-28 17:39:08 +0000558 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000559 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000560 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000561 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
562 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000563 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000564
565 // Verify partial eviction based on bytesToFree
junov@chromium.org66070a52013-05-28 17:39:08 +0000566 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000567 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000568 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000569 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000570 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
571 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000572 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000573 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
574 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
575
rmistry@google.comd6176b02012-08-23 18:14:13 +0000576 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000577 // a pending draw, while image 1 is locked-in.
junov@chromium.org66070a52013-05-28 17:39:08 +0000578 canvas->freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000579 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000580 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
581 canvas->flush();
582 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
583 bytesFreed = canvas->freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000584 // only one bitmap should have been freed.
585 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
586 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
587 // Clear for next test
junov@chromium.org66070a52013-05-28 17:39:08 +0000588 canvas->flush();
589 canvas->freeMemoryIfPossible(~0U);
590 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000591
592 // Verify the image cache is sensitive to genID bumps
junov@chromium.org66070a52013-05-28 17:39:08 +0000593 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000594 sourceImages[1].notifyPixelsChanged();
junov@chromium.org66070a52013-05-28 17:39:08 +0000595 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
596 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000597
598 // Verify that nothing in this test caused commands to be skipped
599 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
600}
601
602static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000603 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
604 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
605
junov@google.com52a00ca2012-10-01 15:27:14 +0000606 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000607 canvas->setNotificationClient(&notificationCounter);
608 canvas->clear(0x0);
junov@google.com52a00ca2012-10-01 15:27:14 +0000609 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
610 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000611 canvas->flush();
junov@google.com52a00ca2012-10-01 15:27:14 +0000612 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
613 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
614
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000615}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000616
junov@chromium.orgce65f382012-10-17 19:36:09 +0000617static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
618 // This is a regression test for crbug.com/155875
619 // This test covers a code path that inserts bitmaps into the bitmap heap through the
620 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
621 // the flattening and unflattening of the shader.
reed@google.com28183b42014-02-04 15:34:10 +0000622 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
623 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.orgce65f382012-10-17 19:36:09 +0000624 // test will fail if nbIterations is not in sync with
625 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
626 const int nbIterations = 5;
627 size_t bytesAllocated = 0;
628 for(int pass = 0; pass < 2; ++pass) {
629 for(int i = 0; i < nbIterations; ++i) {
630 SkPaint paint;
631 SkBitmap paintPattern;
632 paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
633 paintPattern.allocPixels();
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000634 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000635 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000636 canvas->drawPaint(paint);
637 canvas->flush();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000638
639 // In the first pass, memory allocation should be monotonically increasing as
640 // the bitmap heap slots fill up. In the second pass memory allocation should be
641 // stable as bitmap heap slots get recycled.
junov@chromium.org66070a52013-05-28 17:39:08 +0000642 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000643 if (pass == 0) {
644 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
645 bytesAllocated = newBytesAllocated;
646 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000647 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000648 }
649 }
650 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000651 // All cached resources should be evictable since last canvas call was flush()
junov@chromium.org66070a52013-05-28 17:39:08 +0000652 canvas->freeMemoryIfPossible(~0U);
653 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000654}
655
sugoi@google.com7775fd52012-11-21 15:47:04 +0000656static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000657 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000658
sugoi@google.com7775fd52012-11-21 15:47:04 +0000659 SkBitmap sourceImage;
660 // 100 by 100 image, takes 40,000 bytes in memory
661 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
662 sourceImage.allocPixels();
663
664 // 1 under : should not store the image
665 {
reed@google.com28183b42014-02-04 15:34:10 +0000666 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000667 canvas->setBitmapSizeThreshold(39999);
668 canvas->drawBitmap(sourceImage, 0, 0, NULL);
669 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000670 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
671 }
672
673 // exact value : should store the image
674 {
reed@google.com28183b42014-02-04 15:34:10 +0000675 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000676 canvas->setBitmapSizeThreshold(40000);
677 canvas->drawBitmap(sourceImage, 0, 0, NULL);
678 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000679 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
680 }
681
682 // 1 over : should still store the image
683 {
reed@google.com28183b42014-02-04 15:34:10 +0000684 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000685 canvas->setBitmapSizeThreshold(40001);
686 canvas->drawBitmap(sourceImage, 0, 0, NULL);
687 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000688 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
689 }
690}
691
junov@chromium.org67d74222013-04-12 13:33:01 +0000692
693typedef void* PixelPtr;
694// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
695// buffer. Used to test pointer equality do determine whether a surface points
696// to the same pixel data storage as before.
junov@chromium.org3c5ec8d2013-04-12 13:34:47 +0000697static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000698 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() :
699 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels();
700}
701
702static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com2bd8b812013-11-01 13:46:54 +0000703 SkImageInfo imageSpec = {
junov@chromium.org67d74222013-04-12 13:33:01 +0000704 10, // width
705 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +0000706 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +0000707 kPremul_SkAlphaType
junov@chromium.org67d74222013-04-12 13:33:01 +0000708 };
709 SkSurface* surface;
710 bool useGpu = NULL != factory;
711#if SK_SUPPORT_GPU
712 if (useGpu) {
713 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com5fa42d12013-11-12 17:33:02 +0000714 if (NULL == context) {
715 return;
716 }
717
junov@chromium.org67d74222013-04-12 13:33:01 +0000718 surface = SkSurface::NewRenderTarget(context, imageSpec);
719 } else {
720 surface = SkSurface::NewRaster(imageSpec);
721 }
722#else
723 SkASSERT(!useGpu);
724 surface = SkSurface::NewRaster(imageSpec);
725#endif
726 SkASSERT(NULL != surface);
727 SkAutoTUnref<SkSurface> aur(surface);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000728 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org67d74222013-04-12 13:33:01 +0000729
junov@chromium.org66070a52013-05-28 17:39:08 +0000730 SkImage* image1 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000731 SkAutoTUnref<SkImage> aur_i1(image1);
732 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
733 // The following clear would normally trigger a copy on write, but
734 // it won't because rendering is deferred.
junov@chromium.org66070a52013-05-28 17:39:08 +0000735 canvas->clear(SK_ColorBLACK);
junov@chromium.org67d74222013-04-12 13:33:01 +0000736 // Obtaining a snapshot directly from the surface (as opposed to the
737 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
738 // and will therefore return the same image as the previous snapshot.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000739 SkImage* image2 = surface->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000740 SkAutoTUnref<SkImage> aur_i2(image2);
741 // Images identical because of deferral
742 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
743 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
744 // Because there is a pending clear, this will generate a different image.
junov@chromium.org66070a52013-05-28 17:39:08 +0000745 SkImage* image3 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000746 SkAutoTUnref<SkImage> aur_i3(image3);
747 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
748 // Verify that backing store is now a different buffer because of copy on
749 // write
750 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu);
751 REPORTER_ASSERT(reporter, pixels1 != pixels2);
junov@chromium.org9becf002013-04-15 18:15:23 +0000752 // Verify copy-on write with a draw operation that gets deferred by
753 // the in order draw buffer.
754 SkPaint paint;
junov@chromium.org66070a52013-05-28 17:39:08 +0000755 canvas->drawPaint(paint);
756 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
junov@chromium.org9becf002013-04-15 18:15:23 +0000757 SkAutoTUnref<SkImage> aur_i4(image4);
758 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
junov@chromium.org67d74222013-04-12 13:33:01 +0000759 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000760 REPORTER_ASSERT(reporter, pixels2 != pixels3);
junov@chromium.org67d74222013-04-12 13:33:01 +0000761 // Verify that a direct canvas flush with a pending draw does not trigger
762 // a copy on write when the surface is not sharing its buffer with an
763 // SkImage.
junov@chromium.org66070a52013-05-28 17:39:08 +0000764 canvas->clear(SK_ColorWHITE);
765 canvas->flush();
junov@chromium.org67d74222013-04-12 13:33:01 +0000766 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org66070a52013-05-28 17:39:08 +0000767 canvas->drawPaint(paint);
768 canvas->flush();
junov@chromium.org9becf002013-04-15 18:15:23 +0000769 PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu);
770 REPORTER_ASSERT(reporter, pixels4 == pixels5);
junov@chromium.org67d74222013-04-12 13:33:01 +0000771}
772
junov@chromium.org7070f762013-05-24 17:13:00 +0000773static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com2bd8b812013-11-01 13:46:54 +0000774 SkImageInfo imageSpec = {
junov@chromium.org7070f762013-05-24 17:13:00 +0000775 10, // width
776 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +0000777 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +0000778 kPremul_SkAlphaType
junov@chromium.org7070f762013-05-24 17:13:00 +0000779 };
780 SkSurface* surface;
781 SkSurface* alternateSurface;
782 bool useGpu = NULL != factory;
783#if SK_SUPPORT_GPU
784 if (useGpu) {
785 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.come2930052013-11-13 14:26:52 +0000786 if (NULL == context) {
787 return;
788 }
junov@chromium.org7070f762013-05-24 17:13:00 +0000789 surface = SkSurface::NewRenderTarget(context, imageSpec);
790 alternateSurface = SkSurface::NewRenderTarget(context, imageSpec);
791 } else {
792 surface = SkSurface::NewRaster(imageSpec);
793 alternateSurface = SkSurface::NewRaster(imageSpec);
794 }
795#else
796 SkASSERT(!useGpu);
797 surface = SkSurface::NewRaster(imageSpec);
798 alternateSurface = SkSurface::NewRaster(imageSpec);
799#endif
800 SkASSERT(NULL != surface);
801 SkASSERT(NULL != alternateSurface);
802 SkAutoTUnref<SkSurface> aur1(surface);
803 SkAutoTUnref<SkSurface> aur2(alternateSurface);
804 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
805 PixelPtr pixels2 = getSurfacePixelPtr(alternateSurface, useGpu);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000806 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org66070a52013-05-28 17:39:08 +0000807 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
808 canvas->setSurface(alternateSurface);
809 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
junov@chromium.org7070f762013-05-24 17:13:00 +0000810 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
811 // Verify that none of the above operations triggered a surface copy on write.
812 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
813 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) == pixels2);
814 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
junov@chromium.org66070a52013-05-28 17:39:08 +0000815 canvas->clear(SK_ColorWHITE);
816 canvas->flush();
junov@chromium.org7070f762013-05-24 17:13:00 +0000817 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
818 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) != pixels2);
819}
820
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000821static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000822 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
823 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
824
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000825 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000826 canvas->setNotificationClient(&notificationCounter);
reed@google.com28183b42014-02-04 15:34:10 +0000827
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000828 SkAutoTUnref<SkBaseDevice> secondaryDevice(canvas->createCompatibleDevice(
reed@google.com28183b42014-02-04 15:34:10 +0000829 SkBitmap::kARGB_8888_Config, 10, 10, false));
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000830 SkCanvas secondaryCanvas(secondaryDevice.get());
831 SkRect rect = SkRect::MakeWH(5, 5);
832 SkPaint paint;
833 // After spawning a compatible canvas:
834 // 1) Verify that secondary canvas is usable and does not report to the notification client.
835 secondaryCanvas.drawRect(rect, paint);
836 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
837 // 2) Verify that original canvas is usable and still reports to the notification client.
junov@chromium.org66070a52013-05-28 17:39:08 +0000838 canvas->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000839 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
840}
841
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000842DEF_GPUTEST(DeferredCanvas, reporter, factory) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000843 TestDeferredCanvasBitmapAccess(reporter);
844 TestDeferredCanvasFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000845 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000846 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000847 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000848 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000849 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000850 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000851 TestDeferredCanvasCreateCompatibleDevice(reporter);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000852 TestDeferredCanvasWritePixelsToSurface(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000853 TestDeferredCanvasSurface(reporter, NULL);
junov@chromium.org7070f762013-05-24 17:13:00 +0000854 TestDeferredCanvasSetSurface(reporter, NULL);
junov@chromium.org67d74222013-04-12 13:33:01 +0000855 if (NULL != factory) {
856 TestDeferredCanvasSurface(reporter, factory);
junov@chromium.org7070f762013-05-24 17:13:00 +0000857 TestDeferredCanvasSetSurface(reporter, factory);
junov@chromium.org67d74222013-04-12 13:33:01 +0000858 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000859}