blob: aa360be4010c7fea3edea10c0239a36f564e8a13 [file] [log] [blame]
junov@chromium.org1f9767c2012-02-07 16:27:57 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00007
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00008#include "../src/image/SkImagePriv.h"
9#include "../src/image/SkSurface_Base.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000010#include "SkBitmap.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
junov@chromium.orgce65f382012-10-17 19:36:09 +000012#include "SkBitmapProcShader.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000013#include "SkDeferredCanvas.h"
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000014#include "SkGradientShader.h"
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000015#include "SkShader.h"
reed@google.com28183b42014-02-04 15:34:10 +000016#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018#include "sk_tool_utils.h"
19
junov@chromium.org67d74222013-04-12 13:33:01 +000020#if SK_SUPPORT_GPU
21#include "GrContextFactory.h"
22#else
23class GrContextFactory;
24#endif
junov@chromium.org1f9767c2012-02-07 16:27:57 +000025
junov@chromium.org1f9767c2012-02-07 16:27:57 +000026static const int gWidth = 2;
27static const int gHeight = 2;
28
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000029static void callWritePixels(SkCanvas* canvas, const SkBitmap& src, int x, int y,
30 SkCanvas::Config8888 config) {
31 SkBitmap bm(src);
32 bm.lockPixels();
33
34 SkImageInfo info = bm.info();
35 sk_tool_utils::config8888_to_imagetypes(config, &info.fColorType, &info.fAlphaType);
36
37 if (src.isOpaque()) {
38 info.fAlphaType = kOpaque_SkAlphaType;
39 }
40
41 canvas->writePixels(info, bm.getPixels(), bm.rowBytes(), x, y);
42}
43
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000044static void create(SkBitmap* bm, SkColor color) {
45 bm->allocN32Pixels(gWidth, gHeight);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000046 bm->eraseColor(color);
47}
48
reed@google.com28183b42014-02-04 15:34:10 +000049static SkSurface* createSurface(SkColor color) {
50 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight);
51 surface->getCanvas()->clear(color);
52 return surface;
53}
54
55static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
56 SkPMColor pixel = 0;
57 SkBitmap bitmap;
58 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4, NULL, NULL);
59 SkCanvas canvas(bitmap);
60
61 SkPaint paint;
62 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@google.comd52a9972014-02-04 16:14:58 +000063 surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
reed@google.com28183b42014-02-04 15:34:10 +000064 return pixel;
65}
66
junov@chromium.org1f9767c2012-02-07 16:27:57 +000067static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
68 SkBitmap store;
69
reed@google.com28183b42014-02-04 15:34:10 +000070 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
71 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +000072
junov@chromium.org66070a52013-05-28 17:39:08 +000073 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000074
reed@google.com28183b42014-02-04 15:34:10 +000075 // verify that the clear() was deferred
76 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
77
junov@chromium.org66070a52013-05-28 17:39:08 +000078 SkBitmap accessed = canvas->getDevice()->accessBitmap(false);
reed@google.com28183b42014-02-04 15:34:10 +000079
80 // verify that clear was executed
81 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +000082}
83
junov@chromium.org44324fa2013-08-02 15:36:02 +000084class MockSurface : public SkSurface_Base {
85public:
86 MockSurface(int width, int height) : SkSurface_Base(width, height) {
87 clearCounts();
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000088 fBitmap.allocN32Pixels(width, height);
junov@chromium.org44324fa2013-08-02 15:36:02 +000089 }
90
91 virtual SkCanvas* onNewCanvas() SK_OVERRIDE {
92 return SkNEW_ARGS(SkCanvas, (fBitmap));
93 }
94
reed@google.com2bd8b812013-11-01 13:46:54 +000095 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000096 return NULL;
97 }
98
99 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE {
100 return SkNewImageFromBitmap(fBitmap, true);
101 }
102
103 virtual void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
104 if (mode == SkSurface::kDiscard_ContentChangeMode) {
105 fDiscardCount++;
106 } else {
107 fRetainCount++;
108 }
109 }
110
111 void clearCounts() {
112 fDiscardCount = 0;
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000113 fRetainCount = 0;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000114 }
115
116 int fDiscardCount, fRetainCount;
117 SkBitmap fBitmap;
118};
119
120static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
121 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000122 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org44324fa2013-08-02 15:36:02 +0000123
124 SkBitmap srcBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000125 srcBitmap.allocN32Pixels(10, 10);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000126 srcBitmap.eraseColor(SK_ColorGREEN);
127 // Tests below depend on this bitmap being recognized as opaque
128
129 // Preliminary sanity check: no copy on write if no active snapshot
130 surface->clearCounts();
131 canvas->clear(SK_ColorWHITE);
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, 0 == surface->fDiscardCount);
138 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
139
140 // Case 1: Discard notification happens upon flushing
141 // with an Image attached.
142 surface->clearCounts();
143 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
144 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
145 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
146
147 surface->clearCounts();
148 canvas->clear(SK_ColorWHITE);
149 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
150 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
151
152 surface->clearCounts();
153 canvas->flush();
154 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
155 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
156
157 // Case 2: Opaque writePixels
158 surface->clearCounts();
159 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
160 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
161 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
162
junov@chromium.org44324fa2013-08-02 15:36:02 +0000163 // Case 3: writePixels that partially covers the canvas
164 surface->clearCounts();
165 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
166 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
167 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
168
junov@chromium.org44324fa2013-08-02 15:36:02 +0000169 // Case 4: unpremultiplied opaque writePixels that entirely
170 // covers the canvas
171 surface->clearCounts();
172 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
173 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
174 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
175
176 surface->clearCounts();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000177 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000178 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
179 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
180
181 surface->clearCounts();
182 canvas->flush();
183 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
184 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
185
186 // Case 5: unpremultiplied opaque writePixels that partially
187 // covers the canvas
188 surface->clearCounts();
189 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
190 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
191 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
192
193 surface->clearCounts();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000194 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000195 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
196 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
197
198 surface->clearCounts();
199 canvas->flush();
200 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
201 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
202
203 // Case 6: unpremultiplied opaque writePixels that entirely
204 // covers the canvas, preceded by clear
205 surface->clearCounts();
206 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
209
210 surface->clearCounts();
211 canvas->clear(SK_ColorWHITE);
212 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
213 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
214
215 surface->clearCounts();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000216 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000217 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
218 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
219
220 surface->clearCounts();
221 canvas->flush();
222 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
223 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
224
225 // Case 7: unpremultiplied opaque writePixels that partially
226 // covers the canvas, preceeded by a clear
227 surface->clearCounts();
228 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
231
232 surface->clearCounts();
233 canvas->clear(SK_ColorWHITE);
234 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
235 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
236
237 surface->clearCounts();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000238 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000239 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
240 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
241
242 surface->clearCounts();
243 canvas->flush();
244 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
245 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
246
247 // Case 8: unpremultiplied opaque writePixels that partially
248 // covers the canvas, preceeded by a drawREct that partially
249 // covers the canvas
250 surface->clearCounts();
251 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
252 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
253 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
254
255 surface->clearCounts();
256 SkPaint paint;
257 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
258 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
259 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
260
261 surface->clearCounts();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000262 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000263 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
264 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
265
266 surface->clearCounts();
267 canvas->flush();
268 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
269 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
270}
271
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000272static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000273 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
274 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000275
junov@chromium.org66070a52013-05-28 17:39:08 +0000276 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000277
reed@google.com28183b42014-02-04 15:34:10 +0000278 // verify that clear was deferred
279 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
280
junov@chromium.org66070a52013-05-28 17:39:08 +0000281 canvas->flush();
reed@google.com28183b42014-02-04 15:34:10 +0000282
283 // verify that clear was executed
284 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000285}
286
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000287static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000288 SkRect fullRect;
289 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
290 SkIntToScalar(gHeight));
291 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000292 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
293 SkIntToScalar(1), SkIntToScalar(1));
reed@google.com28183b42014-02-04 15:34:10 +0000294
295 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
296 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000297
298 // verify that frame is intially fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000299 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000300 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000301 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000302
303 // Verify that clear triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000304 canvas->clear(0x00000000);
305 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000306
307 // Verify that clear with saved state triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000308 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
309 canvas->clear(0x00000000);
310 canvas->restore();
311 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000312
313 // Verify that clear within a layer does NOT trigger a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000314 canvas->saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_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 a clear with clipping triggers a fresh frame
320 // (clear is not affected by clipping)
junov@chromium.org66070a52013-05-28 17:39:08 +0000321 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
322 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
323 canvas->clear(0x00000000);
324 canvas->restore();
325 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000326
327 // Verify that full frame rects with different forms of opaque paint
328 // trigger frames to be marked as fresh
329 {
330 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000331 paint.setStyle(SkPaint::kFill_Style);
332 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000333 canvas->drawRect(fullRect, paint);
334 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000335 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000336 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000337 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000338 paint.setStyle(SkPaint::kFill_Style);
339 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000340 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000341 canvas->drawRect(fullRect, paint);
342 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000343 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000344 {
345 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000346 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000347 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000348 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000349 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000351 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
352 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000353 canvas->drawRect(fullRect, paint);
354 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000355 }
356
357 // Verify that full frame rects with different forms of non-opaque paint
358 // do not trigger frames to be marked as fresh
359 {
360 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000361 paint.setStyle(SkPaint::kFill_Style);
362 paint.setAlpha(254);
junov@chromium.org66070a52013-05-28 17:39:08 +0000363 canvas->drawRect(fullRect, paint);
364 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000365 }
366 {
367 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000368 paint.setStyle(SkPaint::kFill_Style);
369 // Defining a cone that partially overlaps the canvas
370 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
371 const SkScalar r1 = SkIntToScalar(1);
372 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
373 const SkScalar r2 = SkIntToScalar(5);
374 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
375 const SkScalar pos[2] = {0, SK_Scalar1};
376 SkShader* shader = SkGradientShader::CreateTwoPointConical(
377 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
378 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000379 canvas->drawRect(fullRect, paint);
380 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000381 }
382 {
383 SkPaint paint;
384 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000385 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000386 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000387 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000388 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000389 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
390 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000391 canvas->drawRect(fullRect, paint);
392 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000393 }
394
395 // Verify that incomplete coverage does not trigger a fresh frame
396 {
397 SkPaint paint;
398 paint.setStyle(SkPaint::kFill_Style);
399 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000400 canvas->drawRect(partialRect, paint);
401 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000402 }
403
404 // Verify that incomplete coverage due to clipping does not trigger a fresh
405 // frame
406 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000407 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
408 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000409 SkPaint paint;
410 paint.setStyle(SkPaint::kFill_Style);
411 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000412 canvas->drawRect(fullRect, paint);
413 canvas->restore();
414 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000415 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000416 {
junov@chromium.org66070a52013-05-28 17:39:08 +0000417 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000418 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000419 paint.setStyle(SkPaint::kFill_Style);
420 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000421 SkPath path;
422 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
junov@chromium.org66070a52013-05-28 17:39:08 +0000423 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
424 canvas->drawRect(fullRect, paint);
425 canvas->restore();
426 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000427 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000428
429 // Verify that stroked rect does not trigger a fresh frame
430 {
431 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000432 paint.setStyle(SkPaint::kStroke_Style);
433 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000434 canvas->drawRect(fullRect, paint);
435 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000436 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000437
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000438 // Verify kSrcMode triggers a fresh frame even with transparent color
439 {
440 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000441 paint.setStyle(SkPaint::kFill_Style);
442 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000443 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000444 canvas->drawRect(fullRect, paint);
445 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000446 }
447}
448
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000449class MockDevice : public SkBitmapDevice {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000450public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000451 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000452 fDrawBitmapCallCount = 0;
453 }
454 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000455 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000456 fDrawBitmapCallCount++;
457 }
458
459 int fDrawBitmapCallCount;
460};
461
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000462class NotificationCounter : public SkDeferredCanvas::NotificationClient {
463public:
464 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000465 fPrepareForDrawCount = fStorageAllocatedChangedCount =
466 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000467 }
468
469 virtual void prepareForDraw() SK_OVERRIDE {
470 fPrepareForDrawCount++;
471 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000472 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000473 fStorageAllocatedChangedCount++;
474 }
475 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000477 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000478 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
479 fSkippedPendingDrawCommandsCount++;
480 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000481
482 int fPrepareForDrawCount;
483 int fStorageAllocatedChangedCount;
484 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000485 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000486
487private:
488 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000489};
490
reed@google.com28183b42014-02-04 15:34:10 +0000491// Verifies that the deferred canvas triggers a flush when its memory
492// limit is exceeded
493static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
494 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
495 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000496
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000497 NotificationCounter notificationCounter;
reed@google.com28183b42014-02-04 15:34:10 +0000498 canvas->setNotificationClient(&notificationCounter);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000499
reed@google.com28183b42014-02-04 15:34:10 +0000500 canvas->setMaxRecordingStorage(160000);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000501
reed@google.com28183b42014-02-04 15:34:10 +0000502 SkBitmap sourceImage;
503 // 100 by 100 image, takes 40,000 bytes in memory
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000504 sourceImage.allocN32Pixels(100, 100);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000505
reed@google.com28183b42014-02-04 15:34:10 +0000506 for (int i = 0; i < 5; i++) {
507 sourceImage.notifyPixelsChanged(); // to force re-serialization
508 canvas->drawBitmap(sourceImage, 0, 0, NULL);
509 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000510
reed@google.com28183b42014-02-04 15:34:10 +0000511 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
512}
513
514static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
515 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
516 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
517
518 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000519 canvas->setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000520
521 const int imageCount = 2;
522 SkBitmap sourceImages[imageCount];
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000523 for (int i = 0; i < imageCount; i++) {
524 sourceImages[i].allocN32Pixels(100, 100);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000525 }
526
527 size_t bitmapSize = sourceImages[0].getSize();
528
junov@chromium.org66070a52013-05-28 17:39:08 +0000529 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000530 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000531 // stored bitmap + drawBitmap command
junov@chromium.org66070a52013-05-28 17:39:08 +0000532 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000534 // verify that nothing can be freed at this point
junov@chromium.org66070a52013-05-28 17:39:08 +0000535 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000536
537 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000538 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
539 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000540 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000541 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
542 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000543 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000544
545 // verify that after a flush, cached image can be freed
junov@chromium.org66070a52013-05-28 17:39:08 +0000546 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000547
548 // Verify that caching works for avoiding multiple copies of the same bitmap
junov@chromium.org66070a52013-05-28 17:39:08 +0000549 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000550 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000551 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000552 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
553 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000554 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000555
556 // Verify partial eviction based on bytesToFree
junov@chromium.org66070a52013-05-28 17:39:08 +0000557 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000558 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000559 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000560 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000561 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
562 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000563 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000564 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
565 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
566
rmistry@google.comd6176b02012-08-23 18:14:13 +0000567 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000568 // a pending draw, while image 1 is locked-in.
junov@chromium.org66070a52013-05-28 17:39:08 +0000569 canvas->freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000570 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000571 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
572 canvas->flush();
573 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
574 bytesFreed = canvas->freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000575 // only one bitmap should have been freed.
576 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
577 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
578 // Clear for next test
junov@chromium.org66070a52013-05-28 17:39:08 +0000579 canvas->flush();
580 canvas->freeMemoryIfPossible(~0U);
581 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000582
583 // Verify the image cache is sensitive to genID bumps
junov@chromium.org66070a52013-05-28 17:39:08 +0000584 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000585 sourceImages[1].notifyPixelsChanged();
junov@chromium.org66070a52013-05-28 17:39:08 +0000586 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
587 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000588
589 // Verify that nothing in this test caused commands to be skipped
590 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
591}
592
593static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000594 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
595 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
596
junov@google.com52a00ca2012-10-01 15:27:14 +0000597 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000598 canvas->setNotificationClient(&notificationCounter);
599 canvas->clear(0x0);
junov@google.com52a00ca2012-10-01 15:27:14 +0000600 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
601 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000602 canvas->flush();
junov@google.com52a00ca2012-10-01 15:27:14 +0000603 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
604 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
605
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000606}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000607
junov@chromium.orgce65f382012-10-17 19:36:09 +0000608static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
609 // This is a regression test for crbug.com/155875
610 // This test covers a code path that inserts bitmaps into the bitmap heap through the
611 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
612 // the flattening and unflattening of the shader.
reed@google.com28183b42014-02-04 15:34:10 +0000613 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
614 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.orgce65f382012-10-17 19:36:09 +0000615 // test will fail if nbIterations is not in sync with
616 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
617 const int nbIterations = 5;
618 size_t bytesAllocated = 0;
619 for(int pass = 0; pass < 2; ++pass) {
620 for(int i = 0; i < nbIterations; ++i) {
621 SkPaint paint;
622 SkBitmap paintPattern;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000623 paintPattern.allocN32Pixels(10, 10);
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000624 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000625 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000626 canvas->drawPaint(paint);
627 canvas->flush();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000628
629 // In the first pass, memory allocation should be monotonically increasing as
630 // the bitmap heap slots fill up. In the second pass memory allocation should be
631 // stable as bitmap heap slots get recycled.
junov@chromium.org66070a52013-05-28 17:39:08 +0000632 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000633 if (pass == 0) {
634 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
635 bytesAllocated = newBytesAllocated;
636 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000637 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000638 }
639 }
640 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000641 // All cached resources should be evictable since last canvas call was flush()
junov@chromium.org66070a52013-05-28 17:39:08 +0000642 canvas->freeMemoryIfPossible(~0U);
643 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000644}
645
sugoi@google.com7775fd52012-11-21 15:47:04 +0000646static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000647 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
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
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000651 sourceImage.allocN32Pixels(100, 100);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000652
653 // 1 under : should not store the image
654 {
reed@google.com28183b42014-02-04 15:34:10 +0000655 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000656 canvas->setBitmapSizeThreshold(39999);
657 canvas->drawBitmap(sourceImage, 0, 0, NULL);
658 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000659 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
660 }
661
662 // exact value : should store the image
663 {
reed@google.com28183b42014-02-04 15:34:10 +0000664 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000665 canvas->setBitmapSizeThreshold(40000);
666 canvas->drawBitmap(sourceImage, 0, 0, NULL);
667 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000668 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
669 }
670
671 // 1 over : should still store the image
672 {
reed@google.com28183b42014-02-04 15:34:10 +0000673 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000674 canvas->setBitmapSizeThreshold(40001);
675 canvas->drawBitmap(sourceImage, 0, 0, NULL);
676 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000677 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
678 }
679}
680
junov@chromium.org67d74222013-04-12 13:33:01 +0000681
682typedef void* PixelPtr;
683// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
684// buffer. Used to test pointer equality do determine whether a surface points
685// to the same pixel data storage as before.
junov@chromium.org3c5ec8d2013-04-12 13:34:47 +0000686static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000687 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() :
688 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels();
689}
690
691static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000692 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
junov@chromium.org67d74222013-04-12 13:33:01 +0000693 SkSurface* surface;
694 bool useGpu = NULL != factory;
695#if SK_SUPPORT_GPU
696 if (useGpu) {
697 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com5fa42d12013-11-12 17:33:02 +0000698 if (NULL == context) {
699 return;
700 }
701
junov@chromium.org67d74222013-04-12 13:33:01 +0000702 surface = SkSurface::NewRenderTarget(context, imageSpec);
703 } else {
704 surface = SkSurface::NewRaster(imageSpec);
705 }
706#else
707 SkASSERT(!useGpu);
708 surface = SkSurface::NewRaster(imageSpec);
709#endif
710 SkASSERT(NULL != surface);
711 SkAutoTUnref<SkSurface> aur(surface);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000712 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org67d74222013-04-12 13:33:01 +0000713
junov@chromium.org66070a52013-05-28 17:39:08 +0000714 SkImage* image1 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000715 SkAutoTUnref<SkImage> aur_i1(image1);
716 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
717 // The following clear would normally trigger a copy on write, but
718 // it won't because rendering is deferred.
junov@chromium.org66070a52013-05-28 17:39:08 +0000719 canvas->clear(SK_ColorBLACK);
junov@chromium.org67d74222013-04-12 13:33:01 +0000720 // Obtaining a snapshot directly from the surface (as opposed to the
721 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
722 // and will therefore return the same image as the previous snapshot.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000723 SkImage* image2 = surface->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000724 SkAutoTUnref<SkImage> aur_i2(image2);
725 // Images identical because of deferral
726 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
727 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
728 // Because there is a pending clear, this will generate a different image.
junov@chromium.org66070a52013-05-28 17:39:08 +0000729 SkImage* image3 = canvas->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000730 SkAutoTUnref<SkImage> aur_i3(image3);
731 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
732 // Verify that backing store is now a different buffer because of copy on
733 // write
734 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu);
735 REPORTER_ASSERT(reporter, pixels1 != pixels2);
junov@chromium.org9becf002013-04-15 18:15:23 +0000736 // Verify copy-on write with a draw operation that gets deferred by
737 // the in order draw buffer.
738 SkPaint paint;
junov@chromium.org66070a52013-05-28 17:39:08 +0000739 canvas->drawPaint(paint);
740 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
junov@chromium.org9becf002013-04-15 18:15:23 +0000741 SkAutoTUnref<SkImage> aur_i4(image4);
742 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
junov@chromium.org67d74222013-04-12 13:33:01 +0000743 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000744 REPORTER_ASSERT(reporter, pixels2 != pixels3);
junov@chromium.org67d74222013-04-12 13:33:01 +0000745 // Verify that a direct canvas flush with a pending draw does not trigger
746 // a copy on write when the surface is not sharing its buffer with an
747 // SkImage.
junov@chromium.org66070a52013-05-28 17:39:08 +0000748 canvas->clear(SK_ColorWHITE);
749 canvas->flush();
junov@chromium.org67d74222013-04-12 13:33:01 +0000750 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org66070a52013-05-28 17:39:08 +0000751 canvas->drawPaint(paint);
752 canvas->flush();
junov@chromium.org9becf002013-04-15 18:15:23 +0000753 PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu);
754 REPORTER_ASSERT(reporter, pixels4 == pixels5);
junov@chromium.org67d74222013-04-12 13:33:01 +0000755}
756
junov@chromium.org7070f762013-05-24 17:13:00 +0000757static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000758 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
junov@chromium.org7070f762013-05-24 17:13:00 +0000759 SkSurface* surface;
760 SkSurface* alternateSurface;
761 bool useGpu = NULL != factory;
762#if SK_SUPPORT_GPU
763 if (useGpu) {
764 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.come2930052013-11-13 14:26:52 +0000765 if (NULL == context) {
766 return;
767 }
junov@chromium.org7070f762013-05-24 17:13:00 +0000768 surface = SkSurface::NewRenderTarget(context, imageSpec);
769 alternateSurface = SkSurface::NewRenderTarget(context, imageSpec);
770 } else {
771 surface = SkSurface::NewRaster(imageSpec);
772 alternateSurface = SkSurface::NewRaster(imageSpec);
773 }
774#else
775 SkASSERT(!useGpu);
776 surface = SkSurface::NewRaster(imageSpec);
777 alternateSurface = SkSurface::NewRaster(imageSpec);
778#endif
779 SkASSERT(NULL != surface);
780 SkASSERT(NULL != alternateSurface);
781 SkAutoTUnref<SkSurface> aur1(surface);
782 SkAutoTUnref<SkSurface> aur2(alternateSurface);
783 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
784 PixelPtr pixels2 = getSurfacePixelPtr(alternateSurface, useGpu);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000785 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
junov@chromium.org66070a52013-05-28 17:39:08 +0000786 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
787 canvas->setSurface(alternateSurface);
788 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
junov@chromium.org7070f762013-05-24 17:13:00 +0000789 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
790 // Verify that none of the above operations triggered a surface copy on write.
791 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
792 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) == pixels2);
793 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
junov@chromium.org66070a52013-05-28 17:39:08 +0000794 canvas->clear(SK_ColorWHITE);
795 canvas->flush();
junov@chromium.org7070f762013-05-24 17:13:00 +0000796 REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1);
797 REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) != pixels2);
798}
799
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000800static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000801 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
802 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
803
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000804 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000805 canvas->setNotificationClient(&notificationCounter);
reed@google.com28183b42014-02-04 15:34:10 +0000806
reed@google.com76f10a32014-02-05 15:32:21 +0000807 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
808 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info));
809
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000810 SkRect rect = SkRect::MakeWH(5, 5);
811 SkPaint paint;
812 // After spawning a compatible canvas:
813 // 1) Verify that secondary canvas is usable and does not report to the notification client.
reed@google.com76f10a32014-02-05 15:32:21 +0000814 surface->getCanvas()->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000815 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
816 // 2) Verify that original canvas is usable and still reports to the notification client.
junov@chromium.org66070a52013-05-28 17:39:08 +0000817 canvas->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000818 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
819}
820
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000821DEF_GPUTEST(DeferredCanvas, reporter, factory) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000822 TestDeferredCanvasBitmapAccess(reporter);
823 TestDeferredCanvasFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000824 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000825 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000826 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000827 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000828 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000829 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000830 TestDeferredCanvasCreateCompatibleDevice(reporter);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000831 TestDeferredCanvasWritePixelsToSurface(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000832 TestDeferredCanvasSurface(reporter, NULL);
junov@chromium.org7070f762013-05-24 17:13:00 +0000833 TestDeferredCanvasSetSurface(reporter, NULL);
junov@chromium.org67d74222013-04-12 13:33:01 +0000834 if (NULL != factory) {
835 TestDeferredCanvasSurface(reporter, factory);
junov@chromium.org7070f762013-05-24 17:13:00 +0000836 TestDeferredCanvasSetSurface(reporter, factory);
junov@chromium.org67d74222013-04-12 13:33:01 +0000837 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000838}