blob: 3089dc0aad3963c07aefa04bb68b48e50588be41 [file] [log] [blame]
junov@chromium.org1f9767c2012-02-07 16:27:57 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00007
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00008#include "../src/image/SkImagePriv.h"
9#include "../src/image/SkSurface_Base.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000010#include "SkBitmap.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
junov@chromium.orgce65f382012-10-17 19:36:09 +000012#include "SkBitmapProcShader.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000013#include "SkDeferredCanvas.h"
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000014#include "SkGradientShader.h"
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000015#include "SkShader.h"
reed@google.com28183b42014-02-04 15:34:10 +000016#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018#include "sk_tool_utils.h"
19
junov@chromium.org67d74222013-04-12 13:33:01 +000020#if SK_SUPPORT_GPU
21#include "GrContextFactory.h"
22#else
23class GrContextFactory;
24#endif
junov@chromium.org1f9767c2012-02-07 16:27:57 +000025
junov@chromium.org1f9767c2012-02-07 16:27:57 +000026static const int gWidth = 2;
27static const int gHeight = 2;
28
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000029static void create(SkBitmap* bm, SkColor color) {
30 bm->allocN32Pixels(gWidth, gHeight);
junov@chromium.org1f9767c2012-02-07 16:27:57 +000031 bm->eraseColor(color);
32}
33
reed@google.com28183b42014-02-04 15:34:10 +000034static SkSurface* createSurface(SkColor color) {
reed3054be12014-12-10 07:24:28 -080035 SkSurface* surface = SkSurface::NewRasterN32Premul(gWidth, gHeight);
reed@google.com28183b42014-02-04 15:34:10 +000036 surface->getCanvas()->clear(color);
37 return surface;
38}
39
40static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
41 SkPMColor pixel = 0;
42 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000043 bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4);
reed@google.com28183b42014-02-04 15:34:10 +000044 SkCanvas canvas(bitmap);
45
46 SkPaint paint;
47 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@google.comd52a9972014-02-04 16:14:58 +000048 surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
reed@google.com28183b42014-02-04 15:34:10 +000049 return pixel;
50}
51
junov@chromium.org44324fa2013-08-02 15:36:02 +000052class MockSurface : public SkSurface_Base {
53public:
reed4a8126e2014-09-22 07:29:03 -070054 MockSurface(int width, int height) : SkSurface_Base(width, height, NULL) {
junov@chromium.org44324fa2013-08-02 15:36:02 +000055 clearCounts();
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000056 fBitmap.allocN32Pixels(width, height);
junov@chromium.org44324fa2013-08-02 15:36:02 +000057 }
58
mtklein72c9faa2015-01-09 10:06:39 -080059 SkCanvas* onNewCanvas() SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000060 return SkNEW_ARGS(SkCanvas, (fBitmap));
61 }
62
mtklein72c9faa2015-01-09 10:06:39 -080063 SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000064 return NULL;
65 }
66
mtklein72c9faa2015-01-09 10:06:39 -080067 SkImage* onNewImageSnapshot() SK_OVERRIDE {
reed4af267b2014-11-21 08:46:37 -080068 return SkNewImageFromBitmap(fBitmap, true, &this->props());
junov@chromium.org44324fa2013-08-02 15:36:02 +000069 }
70
mtklein72c9faa2015-01-09 10:06:39 -080071 void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
junov@chromium.org44324fa2013-08-02 15:36:02 +000072 if (mode == SkSurface::kDiscard_ContentChangeMode) {
73 fDiscardCount++;
74 } else {
75 fRetainCount++;
76 }
77 }
78
79 void clearCounts() {
80 fDiscardCount = 0;
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +000081 fRetainCount = 0;
junov@chromium.org44324fa2013-08-02 15:36:02 +000082 }
83
84 int fDiscardCount, fRetainCount;
85 SkBitmap fBitmap;
86};
87
88static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
89 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000090 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org44324fa2013-08-02 15:36:02 +000091
92 SkBitmap srcBitmap;
reed@google.com7111d462014-03-25 16:20:24 +000093 srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
junov@chromium.org44324fa2013-08-02 15:36:02 +000094 srcBitmap.eraseColor(SK_ColorGREEN);
95 // Tests below depend on this bitmap being recognized as opaque
96
97 // Preliminary sanity check: no copy on write if no active snapshot
98 surface->clearCounts();
99 canvas->clear(SK_ColorWHITE);
100 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
101 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
102
103 surface->clearCounts();
104 canvas->flush();
105 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
106 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
107
108 // Case 1: Discard notification happens upon flushing
109 // with an Image attached.
110 surface->clearCounts();
111 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
112 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
113 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
114
115 surface->clearCounts();
116 canvas->clear(SK_ColorWHITE);
117 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
118 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
119
120 surface->clearCounts();
121 canvas->flush();
122 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
123 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
124
125 // Case 2: Opaque writePixels
126 surface->clearCounts();
127 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
128 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
129 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
130
junov@chromium.org44324fa2013-08-02 15:36:02 +0000131 // Case 3: writePixels that partially covers the canvas
132 surface->clearCounts();
133 SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
134 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
135 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
136
junov@chromium.org44324fa2013-08-02 15:36:02 +0000137 // Case 4: unpremultiplied opaque writePixels that entirely
138 // covers the canvas
139 surface->clearCounts();
140 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
141 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
142 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
143
144 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000145 canvas->writePixels(srcBitmap, 0, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000146 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
147 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
148
149 surface->clearCounts();
150 canvas->flush();
151 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
152 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
153
154 // Case 5: unpremultiplied opaque writePixels that partially
155 // covers the canvas
156 surface->clearCounts();
157 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
158 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
159 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
160
161 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000162 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000163 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
164 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
165
166 surface->clearCounts();
167 canvas->flush();
168 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
169 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
170
171 // Case 6: unpremultiplied opaque writePixels that entirely
172 // covers the canvas, preceded by clear
173 surface->clearCounts();
174 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
175 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
176 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
177
178 surface->clearCounts();
179 canvas->clear(SK_ColorWHITE);
180 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
181 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
182
183 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000184 canvas->writePixels(srcBitmap, 0, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000185 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
186 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
187
188 surface->clearCounts();
189 canvas->flush();
190 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
191 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
192
193 // Case 7: unpremultiplied opaque writePixels that partially
194 // covers the canvas, preceeded by a clear
195 surface->clearCounts();
196 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
197 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
198 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
199
200 surface->clearCounts();
201 canvas->clear(SK_ColorWHITE);
202 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
203 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
204
205 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000206 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000207 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
209
210 surface->clearCounts();
211 canvas->flush();
212 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
213 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
214
215 // Case 8: unpremultiplied opaque writePixels that partially
216 // covers the canvas, preceeded by a drawREct that partially
217 // covers the canvas
218 surface->clearCounts();
219 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
220 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
221 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
222
223 surface->clearCounts();
224 SkPaint paint;
225 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
226 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
227 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
228
229 surface->clearCounts();
reed@google.com7111d462014-03-25 16:20:24 +0000230 canvas->writePixels(srcBitmap, 5, 0);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000231 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
232 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
233
234 surface->clearCounts();
235 canvas->flush();
236 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
237 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
238}
239
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000240static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
reed@google.com28183b42014-02-04 15:34:10 +0000241 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
242 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000243
junov@chromium.org66070a52013-05-28 17:39:08 +0000244 canvas->clear(0x00000000);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000245
reed@google.com28183b42014-02-04 15:34:10 +0000246 // verify that clear was deferred
247 REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
248
junov@chromium.org66070a52013-05-28 17:39:08 +0000249 canvas->flush();
reed@google.com28183b42014-02-04 15:34:10 +0000250
251 // verify that clear was executed
252 REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000253}
254
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000255static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000256 SkRect fullRect;
257 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
258 SkIntToScalar(gHeight));
259 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000260 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
261 SkIntToScalar(1), SkIntToScalar(1));
reed@google.com28183b42014-02-04 15:34:10 +0000262
263 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
264 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000265
266 // verify that frame is intially fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000267 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000268 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org66070a52013-05-28 17:39:08 +0000269 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000270
271 // Verify that clear triggers a fresh frame
junov@chromium.org66070a52013-05-28 17:39:08 +0000272 canvas->clear(0x00000000);
273 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000274
275 // Verify that clear with saved state triggers a fresh frame
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000276 canvas->save();
junov@chromium.org66070a52013-05-28 17:39:08 +0000277 canvas->clear(0x00000000);
278 canvas->restore();
279 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000280
281 // Verify that clear within a layer does NOT trigger a fresh frame
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000282 canvas->saveLayer(NULL, NULL);
junov@chromium.org66070a52013-05-28 17:39:08 +0000283 canvas->clear(0x00000000);
284 canvas->restore();
285 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000286
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000287 // Verify that full frame rects with different forms of opaque paint
288 // trigger frames to be marked as fresh
289 {
290 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000291 paint.setStyle(SkPaint::kFill_Style);
292 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000293 canvas->drawRect(fullRect, paint);
294 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000295 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000296 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000297 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000298 paint.setStyle(SkPaint::kFill_Style);
299 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000300 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000301 canvas->drawRect(fullRect, paint);
302 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000303 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000304 {
305 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000306 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000307 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000308 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000309 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000311 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
312 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000313 canvas->drawRect(fullRect, paint);
314 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000315 }
316
317 // Verify that full frame rects with different forms of non-opaque paint
318 // do not trigger frames to be marked as fresh
319 {
320 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000321 paint.setStyle(SkPaint::kFill_Style);
322 paint.setAlpha(254);
junov@chromium.org66070a52013-05-28 17:39:08 +0000323 canvas->drawRect(fullRect, paint);
324 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000325 }
326 {
327 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000328 paint.setStyle(SkPaint::kFill_Style);
329 // Defining a cone that partially overlaps the canvas
330 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
331 const SkScalar r1 = SkIntToScalar(1);
332 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
333 const SkScalar r2 = SkIntToScalar(5);
334 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
335 const SkScalar pos[2] = {0, SK_Scalar1};
336 SkShader* shader = SkGradientShader::CreateTwoPointConical(
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000337 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode);
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000338 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000339 canvas->drawRect(fullRect, paint);
340 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000341 }
342 {
343 SkPaint paint;
344 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000345 SkBitmap bmp;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000346 create(&bmp, 0xFFFFFFFF);
reed@google.com383a6972013-10-21 14:00:07 +0000347 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000349 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
350 paint.setShader(shader)->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000351 canvas->drawRect(fullRect, paint);
352 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000353 }
354
355 // Verify that incomplete coverage does not trigger a fresh frame
356 {
357 SkPaint paint;
358 paint.setStyle(SkPaint::kFill_Style);
359 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000360 canvas->drawRect(partialRect, paint);
361 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000362 }
363
364 // Verify that incomplete coverage due to clipping does not trigger a fresh
365 // frame
366 {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000367 canvas->save();
junov@chromium.org66070a52013-05-28 17:39:08 +0000368 canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000369 SkPaint paint;
370 paint.setStyle(SkPaint::kFill_Style);
371 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000372 canvas->drawRect(fullRect, paint);
373 canvas->restore();
374 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000375 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000376 {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000377 canvas->save();
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000378 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000379 paint.setStyle(SkPaint::kFill_Style);
380 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000381 SkPath path;
382 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
junov@chromium.org66070a52013-05-28 17:39:08 +0000383 canvas->clipPath(path, SkRegion::kIntersect_Op, false);
384 canvas->drawRect(fullRect, paint);
385 canvas->restore();
386 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000387 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000388
389 // Verify that stroked rect does not trigger a fresh frame
390 {
391 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000392 paint.setStyle(SkPaint::kStroke_Style);
393 paint.setAlpha(255);
junov@chromium.org66070a52013-05-28 17:39:08 +0000394 canvas->drawRect(fullRect, paint);
395 REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000396 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000397
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000398 // Verify kSrcMode triggers a fresh frame even with transparent color
399 {
400 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000401 paint.setStyle(SkPaint::kFill_Style);
402 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000403 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
junov@chromium.org66070a52013-05-28 17:39:08 +0000404 canvas->drawRect(fullRect, paint);
405 REPORTER_ASSERT(reporter, canvas->isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000406 }
407}
408
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000409class MockDevice : public SkBitmapDevice {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000410public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000411 MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000412 fDrawBitmapCallCount = 0;
413 }
414 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000415 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000416 fDrawBitmapCallCount++;
417 }
418
419 int fDrawBitmapCallCount;
420};
421
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000422class NotificationCounter : public SkDeferredCanvas::NotificationClient {
423public:
424 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000425 fPrepareForDrawCount = fStorageAllocatedChangedCount =
426 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000427 }
428
mtklein72c9faa2015-01-09 10:06:39 -0800429 void prepareForDraw() SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000430 fPrepareForDrawCount++;
431 }
mtklein72c9faa2015-01-09 10:06:39 -0800432 void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000433 fStorageAllocatedChangedCount++;
434 }
mtklein72c9faa2015-01-09 10:06:39 -0800435 void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000437 }
mtklein72c9faa2015-01-09 10:06:39 -0800438 void skippedPendingDrawCommands() SK_OVERRIDE {
junov@google.com52a00ca2012-10-01 15:27:14 +0000439 fSkippedPendingDrawCommandsCount++;
440 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000441
442 int fPrepareForDrawCount;
443 int fStorageAllocatedChangedCount;
444 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000445 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000446
447private:
448 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000449};
450
reed@google.com28183b42014-02-04 15:34:10 +0000451// Verifies that the deferred canvas triggers a flush when its memory
452// limit is exceeded
453static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
reed3054be12014-12-10 07:24:28 -0800454 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
reed@google.com28183b42014-02-04 15:34:10 +0000455 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000456
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000457 NotificationCounter notificationCounter;
reed@google.com28183b42014-02-04 15:34:10 +0000458 canvas->setNotificationClient(&notificationCounter);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000459
reed@google.com28183b42014-02-04 15:34:10 +0000460 canvas->setMaxRecordingStorage(160000);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000461
reed@google.com28183b42014-02-04 15:34:10 +0000462 SkBitmap sourceImage;
463 // 100 by 100 image, takes 40,000 bytes in memory
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000464 sourceImage.allocN32Pixels(100, 100);
henrik.smiding3bb195e2014-06-27 08:03:17 -0700465 sourceImage.eraseColor(SK_ColorGREEN);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000466
reed@google.com28183b42014-02-04 15:34:10 +0000467 for (int i = 0; i < 5; i++) {
468 sourceImage.notifyPixelsChanged(); // to force re-serialization
469 canvas->drawBitmap(sourceImage, 0, 0, NULL);
470 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000471
reed@google.com28183b42014-02-04 15:34:10 +0000472 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
473}
474
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000475static void TestDeferredCanvasSilentFlush(skiatest::Reporter* reporter) {
476 SkAutoTUnref<SkSurface> surface(createSurface(0));
477 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
478
479 NotificationCounter notificationCounter;
480 canvas->setNotificationClient(&notificationCounter);
481
482 canvas->silentFlush(); // will skip the initial clear that was recorded in createSurface
483
484 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
485 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
486}
487
reed@google.com28183b42014-02-04 15:34:10 +0000488static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
reed3054be12014-12-10 07:24:28 -0800489 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
reed@google.com28183b42014-02-04 15:34:10 +0000490 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
491
492 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000493 canvas->setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000494
495 const int imageCount = 2;
496 SkBitmap sourceImages[imageCount];
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000497 for (int i = 0; i < imageCount; i++) {
498 sourceImages[i].allocN32Pixels(100, 100);
henrik.smiding3bb195e2014-06-27 08:03:17 -0700499 sourceImages[i].eraseColor(SK_ColorGREEN);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000500 }
501
502 size_t bitmapSize = sourceImages[0].getSize();
503
junov@chromium.org66070a52013-05-28 17:39:08 +0000504 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000505 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000506 // stored bitmap + drawBitmap command
junov@chromium.org66070a52013-05-28 17:39:08 +0000507 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000508
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000509 // verify that nothing can be freed at this point
junov@chromium.org66070a52013-05-28 17:39:08 +0000510 REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000511
512 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000513 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
514 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000515 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000516 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
517 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000518 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000519
520 // verify that after a flush, cached image can be freed
junov@chromium.org66070a52013-05-28 17:39:08 +0000521 REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000522
523 // Verify that caching works for avoiding multiple copies of the same bitmap
junov@chromium.org66070a52013-05-28 17:39:08 +0000524 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000525 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000526 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
mtklein6d88e6c2014-07-30 09:17:54 -0700527 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000528 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000529 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000530
531 // Verify partial eviction based on bytesToFree
junov@chromium.org66070a52013-05-28 17:39:08 +0000532 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000533 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000534 canvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000535 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000536 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize);
537 size_t bytesFreed = canvas->freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000538 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000539 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
540 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
541
rmistry@google.comd6176b02012-08-23 18:14:13 +0000542 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000543 // a pending draw, while image 1 is locked-in.
junov@chromium.org66070a52013-05-28 17:39:08 +0000544 canvas->freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000545 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000546 canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
547 canvas->flush();
548 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
549 bytesFreed = canvas->freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000550 // only one bitmap should have been freed.
551 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
552 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
553 // Clear for next test
junov@chromium.org66070a52013-05-28 17:39:08 +0000554 canvas->flush();
555 canvas->freeMemoryIfPossible(~0U);
556 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000557
558 // Verify the image cache is sensitive to genID bumps
junov@chromium.org66070a52013-05-28 17:39:08 +0000559 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000560 sourceImages[1].notifyPixelsChanged();
junov@chromium.org66070a52013-05-28 17:39:08 +0000561 canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
562 REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000563
564 // Verify that nothing in this test caused commands to be skipped
565 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
566}
567
568static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
reed3054be12014-12-10 07:24:28 -0800569 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
reed@google.com28183b42014-02-04 15:34:10 +0000570 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
571
junov@google.com52a00ca2012-10-01 15:27:14 +0000572 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000573 canvas->setNotificationClient(&notificationCounter);
574 canvas->clear(0x0);
junov@google.com52a00ca2012-10-01 15:27:14 +0000575 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
576 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org66070a52013-05-28 17:39:08 +0000577 canvas->flush();
junov@google.com52a00ca2012-10-01 15:27:14 +0000578 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
579 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
580
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000581}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000582
junov@chromium.orgce65f382012-10-17 19:36:09 +0000583static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
584 // This is a regression test for crbug.com/155875
585 // This test covers a code path that inserts bitmaps into the bitmap heap through the
586 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
587 // the flattening and unflattening of the shader.
reed3054be12014-12-10 07:24:28 -0800588 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
reed@google.com28183b42014-02-04 15:34:10 +0000589 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.orgce65f382012-10-17 19:36:09 +0000590 // test will fail if nbIterations is not in sync with
591 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
592 const int nbIterations = 5;
593 size_t bytesAllocated = 0;
594 for(int pass = 0; pass < 2; ++pass) {
595 for(int i = 0; i < nbIterations; ++i) {
596 SkPaint paint;
597 SkBitmap paintPattern;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000598 paintPattern.allocN32Pixels(10, 10);
henrik.smiding3bb195e2014-06-27 08:03:17 -0700599 paintPattern.eraseColor(SK_ColorGREEN);
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000600 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000601 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000602 canvas->drawPaint(paint);
603 canvas->flush();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000604
605 // In the first pass, memory allocation should be monotonically increasing as
606 // the bitmap heap slots fill up. In the second pass memory allocation should be
607 // stable as bitmap heap slots get recycled.
junov@chromium.org66070a52013-05-28 17:39:08 +0000608 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
junov@chromium.orgce65f382012-10-17 19:36:09 +0000609 if (pass == 0) {
610 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
611 bytesAllocated = newBytesAllocated;
612 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000613 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000614 }
615 }
616 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000617 // All cached resources should be evictable since last canvas call was flush()
junov@chromium.org66070a52013-05-28 17:39:08 +0000618 canvas->freeMemoryIfPossible(~0U);
619 REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000620}
621
sugoi@google.com7775fd52012-11-21 15:47:04 +0000622static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
reed3054be12014-12-10 07:24:28 -0800623 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000624
sugoi@google.com7775fd52012-11-21 15:47:04 +0000625 SkBitmap sourceImage;
626 // 100 by 100 image, takes 40,000 bytes in memory
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000627 sourceImage.allocN32Pixels(100, 100);
henrik.smiding3bb195e2014-06-27 08:03:17 -0700628 sourceImage.eraseColor(SK_ColorGREEN);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000629
630 // 1 under : should not store the image
631 {
reed@google.com28183b42014-02-04 15:34:10 +0000632 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000633 canvas->setBitmapSizeThreshold(39999);
634 canvas->drawBitmap(sourceImage, 0, 0, NULL);
635 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000636 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
637 }
638
639 // exact value : should store the image
640 {
reed@google.com28183b42014-02-04 15:34:10 +0000641 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000642 canvas->setBitmapSizeThreshold(40000);
643 canvas->drawBitmap(sourceImage, 0, 0, NULL);
644 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000645 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
646 }
647
648 // 1 over : should still store the image
649 {
reed@google.com28183b42014-02-04 15:34:10 +0000650 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
junov@chromium.org66070a52013-05-28 17:39:08 +0000651 canvas->setBitmapSizeThreshold(40001);
652 canvas->drawBitmap(sourceImage, 0, 0, NULL);
653 size_t newBytesAllocated = canvas->storageAllocatedForRecording();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000654 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
655 }
656}
657
junov@chromium.org67d74222013-04-12 13:33:01 +0000658
reed52d9ac62014-06-30 09:05:34 -0700659typedef const void* PixelPtr;
junov@chromium.org67d74222013-04-12 13:33:01 +0000660// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
661// buffer. Used to test pointer equality do determine whether a surface points
662// to the same pixel data storage as before.
reed52d9ac62014-06-30 09:05:34 -0700663static PixelPtr get_surface_ptr(SkSurface* surface, bool useGpu) {
664#if SK_SUPPORT_GPU
665 if (useGpu) {
666 return surface->getCanvas()->internal_private_accessTopLayerRenderTarget()->asTexture();
667 } else
668#endif
669 {
670 return surface->peekPixels(NULL, NULL);
671 }
junov@chromium.org67d74222013-04-12 13:33:01 +0000672}
673
674static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000675 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
bsalomon49f085d2014-09-05 13:34:00 -0700676 bool useGpu = SkToBool(factory);
bsalomone904c092014-07-17 10:50:59 -0700677 int cnt;
junov@chromium.org67d74222013-04-12 13:33:01 +0000678#if SK_SUPPORT_GPU
679 if (useGpu) {
bsalomone904c092014-07-17 10:50:59 -0700680 cnt = GrContextFactory::kGLContextTypeCnt;
junov@chromium.org67d74222013-04-12 13:33:01 +0000681 } else {
bsalomone904c092014-07-17 10:50:59 -0700682 cnt = 1;
junov@chromium.org67d74222013-04-12 13:33:01 +0000683 }
684#else
685 SkASSERT(!useGpu);
bsalomone904c092014-07-17 10:50:59 -0700686 cnt = 1;
junov@chromium.org67d74222013-04-12 13:33:01 +0000687#endif
bsalomone904c092014-07-17 10:50:59 -0700688 for (int i = 0; i < cnt; ++i) {
689 SkSurface* surface;
690#if SK_SUPPORT_GPU
691 if (useGpu) {
692 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
693 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
694 continue;
695 }
696 GrContext* context = factory->get(glCtxType);
697 if (NULL == context) {
698 return;
699 }
junov@chromium.org67d74222013-04-12 13:33:01 +0000700
bsalomonafe30052015-01-16 07:32:33 -0800701 surface =
702 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
bsalomone904c092014-07-17 10:50:59 -0700703 } else
704#endif
705 {
706 surface = SkSurface::NewRaster(imageSpec);
707 }
bsalomon49f085d2014-09-05 13:34:00 -0700708 SkASSERT(surface);
bsalomone904c092014-07-17 10:50:59 -0700709 SkAutoTUnref<SkSurface> aur(surface);
710 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
711
712 SkImage* image1 = canvas->newImageSnapshot();
713 SkAutoTUnref<SkImage> aur_i1(image1);
714 PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
715 // The following clear would normally trigger a copy on write, but
716 // it won't because rendering is deferred.
717 canvas->clear(SK_ColorBLACK);
718 // Obtaining a snapshot directly from the surface (as opposed to the
719 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
720 // and will therefore return the same image as the previous snapshot.
721 SkImage* image2 = surface->newImageSnapshot();
722 SkAutoTUnref<SkImage> aur_i2(image2);
723 // Images identical because of deferral
724 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
725 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
726 // Because there is a pending clear, this will generate a different image.
727 SkImage* image3 = canvas->newImageSnapshot();
728 SkAutoTUnref<SkImage> aur_i3(image3);
729 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
730 // Verify that backing store is now a different buffer because of copy on
731 // write
732 PixelPtr pixels2 = get_surface_ptr(surface, useGpu);
733 REPORTER_ASSERT(reporter, pixels1 != pixels2);
734 // Verify copy-on write with a draw operation that gets deferred by
735 // the in order draw buffer.
736 SkPaint paint;
737 canvas->drawPaint(paint);
738 SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
739 SkAutoTUnref<SkImage> aur_i4(image4);
740 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
741 PixelPtr pixels3 = get_surface_ptr(surface, useGpu);
742 REPORTER_ASSERT(reporter, pixels2 != pixels3);
743 // Verify that a direct canvas flush with a pending draw does not trigger
744 // a copy on write when the surface is not sharing its buffer with an
745 // SkImage.
746 canvas->clear(SK_ColorWHITE);
747 canvas->flush();
748 PixelPtr pixels4 = get_surface_ptr(surface, useGpu);
749 canvas->drawPaint(paint);
750 canvas->flush();
751 PixelPtr pixels5 = get_surface_ptr(surface, useGpu);
752 REPORTER_ASSERT(reporter, pixels4 == pixels5);
753 }
junov@chromium.org67d74222013-04-12 13:33:01 +0000754}
755
junov@chromium.org7070f762013-05-24 17:13:00 +0000756static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
bungeman@google.com2c56cb82014-02-17 17:02:17 +0000757 SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
junov@chromium.org7070f762013-05-24 17:13:00 +0000758 SkSurface* surface;
759 SkSurface* alternateSurface;
bsalomon49f085d2014-09-05 13:34:00 -0700760 bool useGpu = SkToBool(factory);
bsalomone904c092014-07-17 10:50:59 -0700761 int cnt;
junov@chromium.org7070f762013-05-24 17:13:00 +0000762#if SK_SUPPORT_GPU
763 if (useGpu) {
bsalomone904c092014-07-17 10:50:59 -0700764 cnt = GrContextFactory::kGLContextTypeCnt;
junov@chromium.org7070f762013-05-24 17:13:00 +0000765 } else {
bsalomone904c092014-07-17 10:50:59 -0700766 cnt = 1;
junov@chromium.org7070f762013-05-24 17:13:00 +0000767 }
768#else
769 SkASSERT(!useGpu);
bsalomone904c092014-07-17 10:50:59 -0700770 cnt = 1;
junov@chromium.org7070f762013-05-24 17:13:00 +0000771#endif
bsalomone904c092014-07-17 10:50:59 -0700772
773 for (int i = 0; i < cnt; ++i) {
774#if SK_SUPPORT_GPU
775 if (useGpu) {
776 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
777 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
778 continue;
779 }
780 GrContext* context = factory->get(glCtxType);
781 if (NULL == context) {
782 continue;
783 }
bsalomonafe30052015-01-16 07:32:33 -0800784 surface =
785 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
786 alternateSurface =
787 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, imageSpec, 0, NULL);
mtklein6d88e6c2014-07-30 09:17:54 -0700788 } else
bsalomone904c092014-07-17 10:50:59 -0700789#endif
790 {
791 surface = SkSurface::NewRaster(imageSpec);
792 alternateSurface = SkSurface::NewRaster(imageSpec);
793 }
bsalomon49f085d2014-09-05 13:34:00 -0700794 SkASSERT(surface);
795 SkASSERT(alternateSurface);
bsalomone904c092014-07-17 10:50:59 -0700796 SkAutoTUnref<SkSurface> aur1(surface);
797 SkAutoTUnref<SkSurface> aur2(alternateSurface);
798 PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
799 PixelPtr pixels2 = get_surface_ptr(alternateSurface, useGpu);
800 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
801 SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
802 canvas->setSurface(alternateSurface);
803 SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
804 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
805 // Verify that none of the above operations triggered a surface copy on write.
806 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
807 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) == pixels2);
808 // Verify that a flushed draw command will trigger a copy on write on alternateSurface.
809 canvas->clear(SK_ColorWHITE);
810 canvas->flush();
811 REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
812 REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) != pixels2);
813 }
junov@chromium.org7070f762013-05-24 17:13:00 +0000814}
815
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000816static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
reed3054be12014-12-10 07:24:28 -0800817 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100));
reed@google.com28183b42014-02-04 15:34:10 +0000818 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
819
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000820 NotificationCounter notificationCounter;
junov@chromium.org66070a52013-05-28 17:39:08 +0000821 canvas->setNotificationClient(&notificationCounter);
reed@google.com28183b42014-02-04 15:34:10 +0000822
reed@google.com76f10a32014-02-05 15:32:21 +0000823 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
824 SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info));
825
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000826 SkRect rect = SkRect::MakeWH(5, 5);
827 SkPaint paint;
828 // After spawning a compatible canvas:
829 // 1) Verify that secondary canvas is usable and does not report to the notification client.
reed@google.com76f10a32014-02-05 15:32:21 +0000830 surface->getCanvas()->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000831 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
832 // 2) Verify that original canvas is usable and still reports to the notification client.
junov@chromium.org66070a52013-05-28 17:39:08 +0000833 canvas->drawRect(rect, paint);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000834 REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
835}
836
yunchao.he49005bf2014-09-15 22:30:38 -0700837static void TestDeferredCanvasGetCanvasSize(skiatest::Reporter* reporter) {
838 SkRect rect;
839 rect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIntToScalar(gHeight));
840 SkRect clip;
841 clip.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToScalar(1));
842
843 SkPaint paint;
844 SkISize size = SkISize::Make(gWidth, gHeight);
845
846 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
847 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
reed3054be12014-12-10 07:24:28 -0800848 SkSurface* newSurface = SkSurface::NewRasterN32Premul(4, 4);
yunchao.he49005bf2014-09-15 22:30:38 -0700849 SkAutoTUnref<SkSurface> aur(newSurface);
850
851 for (int i = 0; i < 2; ++i) {
852 if (i == 1) {
853 canvas->setSurface(newSurface);
854 size = SkISize::Make(4, 4);
855 }
856
857 // verify that canvas size is correctly initialized or set
858 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
859
860 // Verify that clear, clip and draw the canvas will not change its size
861 canvas->clear(0x00000000);
862 canvas->clipRect(clip, SkRegion::kIntersect_Op, false);
863 canvas->drawRect(rect, paint);
864 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
865
866 // Verify that flush the canvas will not change its size
867 canvas->flush();
868 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
869
870 // Verify that clear canvas with saved state will not change its size
871 canvas->save();
872 canvas->clear(0xFFFFFFFF);
873 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
874
875 // Verify that restore canvas state will not change its size
876 canvas->restore();
877 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
878
879 // Verify that clear within a layer will not change canvas size
880 canvas->saveLayer(&clip, &paint);
881 canvas->clear(0x00000000);
882 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
883
884 // Verify that restore from a layer will not change canvas size
885 canvas->restore();
886 REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
887 }
888}
889
commit-bot@chromium.org8cca4cc2014-05-07 14:33:57 +0000890DEF_TEST(DeferredCanvas_CPU, reporter) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000891 TestDeferredCanvasFlush(reporter);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000892 TestDeferredCanvasSilentFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000893 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000894 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000895 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000896 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000897 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000898 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000899 TestDeferredCanvasCreateCompatibleDevice(reporter);
junov@chromium.org44324fa2013-08-02 15:36:02 +0000900 TestDeferredCanvasWritePixelsToSurface(reporter);
yunchao.he49005bf2014-09-15 22:30:38 -0700901 TestDeferredCanvasGetCanvasSize(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000902 TestDeferredCanvasSurface(reporter, NULL);
junov@chromium.org7070f762013-05-24 17:13:00 +0000903 TestDeferredCanvasSetSurface(reporter, NULL);
commit-bot@chromium.org8cca4cc2014-05-07 14:33:57 +0000904}
905
906DEF_GPUTEST(DeferredCanvas_GPU, reporter, factory) {
907 if (factory != NULL) {
commit-bot@chromium.orgdbf25182014-05-07 12:33:02 +0000908 TestDeferredCanvasSurface(reporter, factory);
909 TestDeferredCanvasSetSurface(reporter, factory);
910 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000911}