blob: 3e73393d140f67dbafbe8c1a6d638f1396619b2b [file] [log] [blame]
junov@chromium.org1f9767c2012-02-07 16:27:57 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "Test.h"
9#include "SkBitmap.h"
junov@chromium.orgce65f382012-10-17 19:36:09 +000010#include "SkBitmapProcShader.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000011#include "SkDeferredCanvas.h"
junov@chromium.org88e29142012-08-07 16:48:22 +000012#include "SkDevice.h"
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000013#include "SkGradientShader.h"
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000014#include "SkShader.h"
junov@chromium.org67d74222013-04-12 13:33:01 +000015#include "SkSurface.h"
16#if SK_SUPPORT_GPU
17#include "GrContextFactory.h"
18#else
19class GrContextFactory;
20#endif
junov@chromium.org1f9767c2012-02-07 16:27:57 +000021
junov@chromium.org1f9767c2012-02-07 16:27:57 +000022static const int gWidth = 2;
23static const int gHeight = 2;
24
25static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
26 bm->setConfig(config, gWidth, gHeight);
27 bm->allocPixels();
28 bm->eraseColor(color);
29}
30
31static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
32 SkBitmap store;
33
34 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
35 SkDevice device(store);
36 SkDeferredCanvas canvas(&device);
37
38 canvas.clear(0x00000000);
39
40 SkAutoLockPixels alp(store);
41 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
42 SkBitmap accessed = canvas.getDevice()->accessBitmap(false);
43 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
44 REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef());
45}
46
47static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
48 SkBitmap store;
49
50 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
51 SkDevice device(store);
52 SkDeferredCanvas canvas(&device);
53
54 canvas.clear(0x00000000);
55
56 SkAutoLockPixels alp(store);
57 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
58 canvas.flush();
59 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
60}
61
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000062static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
63 SkBitmap store;
64 SkRect fullRect;
65 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
66 SkIntToScalar(gHeight));
67 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +000068 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
69 SkIntToScalar(1), SkIntToScalar(1));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000070 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
71 SkDevice device(store);
72 SkDeferredCanvas canvas(&device);
73
74 // verify that frame is intially fresh
junov@chromium.org88e29142012-08-07 16:48:22 +000075 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000076 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org88e29142012-08-07 16:48:22 +000077 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000078
79 // Verify that clear triggers a fresh frame
80 canvas.clear(0x00000000);
junov@chromium.org88e29142012-08-07 16:48:22 +000081 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000082
83 // Verify that clear with saved state triggers a fresh frame
84 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
85 canvas.clear(0x00000000);
86 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +000087 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000088
89 // Verify that clear within a layer does NOT trigger a fresh frame
90 canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
91 canvas.clear(0x00000000);
92 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +000093 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000094
95 // Verify that a clear with clipping triggers a fresh frame
96 // (clear is not affected by clipping)
97 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
98 canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
99 canvas.clear(0x00000000);
100 canvas.restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000102
103 // Verify that full frame rects with different forms of opaque paint
104 // trigger frames to be marked as fresh
105 {
106 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000107 paint.setStyle(SkPaint::kFill_Style);
108 paint.setAlpha(255);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000109 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000110 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000111 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000112 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000113 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000114 paint.setStyle(SkPaint::kFill_Style);
115 paint.setAlpha(255);
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000116 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
117 canvas.drawRect(fullRect, paint);
118 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
119 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000120 {
121 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000122 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000123 SkBitmap bmp;
124 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
125 bmp.setIsOpaque(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000127 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
128 paint.setShader(shader)->unref();
129 canvas.drawRect(fullRect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000131 }
132
133 // Verify that full frame rects with different forms of non-opaque paint
134 // do not trigger frames to be marked as fresh
135 {
136 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000137 paint.setStyle(SkPaint::kFill_Style);
138 paint.setAlpha(254);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000139 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000140 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000141 }
142 {
143 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000144 paint.setStyle(SkPaint::kFill_Style);
145 // Defining a cone that partially overlaps the canvas
146 const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
147 const SkScalar r1 = SkIntToScalar(1);
148 const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
149 const SkScalar r2 = SkIntToScalar(5);
150 const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
151 const SkScalar pos[2] = {0, SK_Scalar1};
152 SkShader* shader = SkGradientShader::CreateTwoPointConical(
153 pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL);
154 paint.setShader(shader)->unref();
155 canvas.drawRect(fullRect, paint);
156 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
157 }
158 {
159 SkPaint paint;
160 paint.setStyle(SkPaint::kFill_Style);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000161 SkBitmap bmp;
162 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
163 bmp.setIsOpaque(false);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000164 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000165 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
166 paint.setShader(shader)->unref();
167 canvas.drawRect(fullRect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000169 }
170
171 // Verify that incomplete coverage does not trigger a fresh frame
172 {
173 SkPaint paint;
174 paint.setStyle(SkPaint::kFill_Style);
175 paint.setAlpha(255);
176 canvas.drawRect(partialRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000177 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000178 }
179
180 // Verify that incomplete coverage due to clipping does not trigger a fresh
181 // frame
182 {
183 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
184 canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
185 SkPaint paint;
186 paint.setStyle(SkPaint::kFill_Style);
187 paint.setAlpha(255);
188 canvas.drawRect(fullRect, paint);
junov@chromium.org41e850f2012-12-10 21:24:38 +0000189 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +0000190 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000191 }
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000192 {
193 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
194 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000195 paint.setStyle(SkPaint::kFill_Style);
196 paint.setAlpha(255);
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000197 SkPath path;
198 path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
199 canvas.clipPath(path, SkRegion::kIntersect_Op, false);
200 canvas.drawRect(fullRect, paint);
201 canvas.restore();
202 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
203 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000204
205 // Verify that stroked rect does not trigger a fresh frame
206 {
207 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000208 paint.setStyle(SkPaint::kStroke_Style);
209 paint.setAlpha(255);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000210 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000211 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000212 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000213
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000214 // Verify kSrcMode triggers a fresh frame even with transparent color
215 {
216 SkPaint paint;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000217 paint.setStyle(SkPaint::kFill_Style);
218 paint.setAlpha(100);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000219 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
220 canvas.drawRect(fullRect, paint);
junov@chromium.org41e850f2012-12-10 21:24:38 +0000221 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000222 }
223}
224
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000225class MockDevice : public SkDevice {
226public:
227 MockDevice(const SkBitmap& bm) : SkDevice(bm) {
228 fDrawBitmapCallCount = 0;
229 }
230 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
231 const SkIRect*,
232 const SkMatrix&, const SkPaint&) {
233 fDrawBitmapCallCount++;
234 }
235
236 int fDrawBitmapCallCount;
237};
238
239// Verifies that the deferred canvas triggers a flush when its memory
240// limit is exceeded
241static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
242 SkBitmap store;
243 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
244 store.allocPixels();
245 MockDevice mockDevice(store);
246 SkDeferredCanvas canvas(&mockDevice);
247 canvas.setMaxRecordingStorage(160000);
248
249 SkBitmap sourceImage;
250 // 100 by 100 image, takes 40,000 bytes in memory
251 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
252 sourceImage.allocPixels();
253
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000254 for (int i = 0; i < 5; i++) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000255 sourceImage.notifyPixelsChanged(); // to force re-serialization
256 canvas.drawBitmap(sourceImage, 0, 0, NULL);
257 }
258
scroggo@google.com15011ee2012-07-26 20:03:32 +0000259 REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000260}
261
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000262class NotificationCounter : public SkDeferredCanvas::NotificationClient {
263public:
264 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000265 fPrepareForDrawCount = fStorageAllocatedChangedCount =
266 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000267 }
268
269 virtual void prepareForDraw() SK_OVERRIDE {
270 fPrepareForDrawCount++;
271 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000272 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000273 fStorageAllocatedChangedCount++;
274 }
275 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000276 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000277 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000278 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
279 fSkippedPendingDrawCommandsCount++;
280 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000281
282 int fPrepareForDrawCount;
283 int fStorageAllocatedChangedCount;
284 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000285 int fSkippedPendingDrawCommandsCount;
robertphillips@google.com59903972013-02-07 21:02:23 +0000286
287private:
288 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000289};
290
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000291static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
292 SkBitmap store;
293 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
294 store.allocPixels();
295 SkDevice device(store);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000296 NotificationCounter notificationCounter;
junov@chromium.orgd433c4e2012-08-17 14:50:16 +0000297 SkDeferredCanvas canvas(&device);
298 canvas.setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000299
300 const int imageCount = 2;
301 SkBitmap sourceImages[imageCount];
302 for (int i = 0; i < imageCount; i++)
303 {
304 sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
305 sourceImages[i].allocPixels();
306 }
307
308 size_t bitmapSize = sourceImages[0].getSize();
309
310 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000311 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000312 // stored bitmap + drawBitmap command
313 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000314
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000315 // verify that nothing can be freed at this point
bsalomon@google.com100abf42012-09-05 17:40:04 +0000316 REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000317
318 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000319 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
320 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000321 canvas.flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000322 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
323 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000324 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize);
325
326 // verify that after a flush, cached image can be freed
bsalomon@google.com100abf42012-09-05 17:40:04 +0000327 REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000328
329 // Verify that caching works for avoiding multiple copies of the same bitmap
330 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000331 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000332 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000333 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
334 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000335 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize);
336
337 // Verify partial eviction based on bytesToFree
338 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000339 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000340 canvas.flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000341 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000342 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize);
343 size_t bytesFreed = canvas.freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000344 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000345 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
346 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
347
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000349 // a pending draw, while image 1 is locked-in.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000350 canvas.freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000351 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000352 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
353 canvas.flush();
354 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
bsalomon@google.com100abf42012-09-05 17:40:04 +0000355 bytesFreed = canvas.freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000356 // only one bitmap should have been freed.
357 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
358 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
359 // Clear for next test
360 canvas.flush();
bsalomon@google.com100abf42012-09-05 17:40:04 +0000361 canvas.freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000362 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize);
363
364 // Verify the image cache is sensitive to genID bumps
365 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
366 sourceImages[1].notifyPixelsChanged();
367 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
368 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000369
370 // Verify that nothing in this test caused commands to be skipped
371 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
372}
373
374static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
375 SkBitmap store;
376 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
377 store.allocPixels();
378 SkDevice device(store);
379 NotificationCounter notificationCounter;
380 SkDeferredCanvas canvas(&device);
381 canvas.setNotificationClient(&notificationCounter);
382 canvas.clear(0x0);
383 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
384 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
385 canvas.flush();
386 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
387 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
388
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000389}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000390
junov@chromium.orgce65f382012-10-17 19:36:09 +0000391static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
392 // This is a regression test for crbug.com/155875
393 // This test covers a code path that inserts bitmaps into the bitmap heap through the
394 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
395 // the flattening and unflattening of the shader.
396 SkBitmap store;
397 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
398 store.allocPixels();
399 SkDevice device(store);
400 SkDeferredCanvas canvas(&device);
401 // test will fail if nbIterations is not in sync with
402 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
403 const int nbIterations = 5;
404 size_t bytesAllocated = 0;
405 for(int pass = 0; pass < 2; ++pass) {
406 for(int i = 0; i < nbIterations; ++i) {
407 SkPaint paint;
408 SkBitmap paintPattern;
409 paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
410 paintPattern.allocPixels();
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000411 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000412 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
413 canvas.drawPaint(paint);
414 canvas.flush();
415
416 // In the first pass, memory allocation should be monotonically increasing as
417 // the bitmap heap slots fill up. In the second pass memory allocation should be
418 // stable as bitmap heap slots get recycled.
419 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
420 if (pass == 0) {
421 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
422 bytesAllocated = newBytesAllocated;
423 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000424 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000425 }
426 }
427 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000428 // All cached resources should be evictable since last canvas call was flush()
reed@google.com2b57dc62013-01-08 13:23:32 +0000429 canvas.freeMemoryIfPossible(~0U);
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000430 REPORTER_ASSERT(reporter, 0 == canvas.storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000431}
432
sugoi@google.com7775fd52012-11-21 15:47:04 +0000433static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
434 SkBitmap store;
435 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
436 store.allocPixels();
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000437
sugoi@google.com7775fd52012-11-21 15:47:04 +0000438 SkBitmap sourceImage;
439 // 100 by 100 image, takes 40,000 bytes in memory
440 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
441 sourceImage.allocPixels();
442
443 // 1 under : should not store the image
444 {
445 SkDevice device(store);
446 SkDeferredCanvas canvas(&device);
447 canvas.setBitmapSizeThreshold(39999);
448 canvas.drawBitmap(sourceImage, 0, 0, NULL);
449 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
450 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
451 }
452
453 // exact value : should store the image
454 {
455 SkDevice device(store);
456 SkDeferredCanvas canvas(&device);
457 canvas.setBitmapSizeThreshold(40000);
458 canvas.drawBitmap(sourceImage, 0, 0, NULL);
459 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
460 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
461 }
462
463 // 1 over : should still store the image
464 {
465 SkDevice device(store);
466 SkDeferredCanvas canvas(&device);
467 canvas.setBitmapSizeThreshold(40001);
468 canvas.drawBitmap(sourceImage, 0, 0, NULL);
469 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
470 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
471 }
472}
473
junov@chromium.org67d74222013-04-12 13:33:01 +0000474
475typedef void* PixelPtr;
476// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
477// buffer. Used to test pointer equality do determine whether a surface points
478// to the same pixel data storage as before.
junov@chromium.org3c5ec8d2013-04-12 13:34:47 +0000479static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000480 return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() :
481 surface->getCanvas()->getDevice()->accessBitmap(false).getPixels();
482}
483
484static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
485 SkImage::Info imageSpec = {
486 10, // width
487 10, // height
488 SkImage::kPMColor_ColorType,
489 SkImage::kPremul_AlphaType
490 };
491 SkSurface* surface;
492 bool useGpu = NULL != factory;
493#if SK_SUPPORT_GPU
494 if (useGpu) {
495 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
496 surface = SkSurface::NewRenderTarget(context, imageSpec);
497 } else {
498 surface = SkSurface::NewRaster(imageSpec);
499 }
500#else
501 SkASSERT(!useGpu);
502 surface = SkSurface::NewRaster(imageSpec);
503#endif
504 SkASSERT(NULL != surface);
505 SkAutoTUnref<SkSurface> aur(surface);
506 SkDeferredCanvas canvas(surface);
507
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000508 SkImage* image1 = canvas.newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000509 SkAutoTUnref<SkImage> aur_i1(image1);
510 PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu);
511 // The following clear would normally trigger a copy on write, but
512 // it won't because rendering is deferred.
513 canvas.clear(SK_ColorBLACK);
514 // Obtaining a snapshot directly from the surface (as opposed to the
515 // SkDeferredCanvas) will not trigger a flush of deferred draw operations
516 // and will therefore return the same image as the previous snapshot.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000517 SkImage* image2 = surface->newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000518 SkAutoTUnref<SkImage> aur_i2(image2);
519 // Images identical because of deferral
520 REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
521 // Now we obtain a snpshot via the deferred canvas, which triggers a flush.
522 // Because there is a pending clear, this will generate a different image.
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000523 SkImage* image3 = canvas.newImageSnapshot();
junov@chromium.org67d74222013-04-12 13:33:01 +0000524 SkAutoTUnref<SkImage> aur_i3(image3);
525 REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
526 // Verify that backing store is now a different buffer because of copy on
527 // write
528 PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu);
529 REPORTER_ASSERT(reporter, pixels1 != pixels2);
junov@chromium.org9becf002013-04-15 18:15:23 +0000530 // Verify copy-on write with a draw operation that gets deferred by
531 // the in order draw buffer.
532 SkPaint paint;
533 canvas.drawPaint(paint);
534 SkImage* image4 = canvas.newImageSnapshot(); // implicit flush
535 SkAutoTUnref<SkImage> aur_i4(image4);
536 REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
junov@chromium.org67d74222013-04-12 13:33:01 +0000537 PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000538 REPORTER_ASSERT(reporter, pixels2 != pixels3);
junov@chromium.org67d74222013-04-12 13:33:01 +0000539 // Verify that a direct canvas flush with a pending draw does not trigger
540 // a copy on write when the surface is not sharing its buffer with an
541 // SkImage.
junov@chromium.org9becf002013-04-15 18:15:23 +0000542 canvas.clear(SK_ColorWHITE);
junov@chromium.org67d74222013-04-12 13:33:01 +0000543 canvas.flush();
544 PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu);
junov@chromium.org9becf002013-04-15 18:15:23 +0000545 canvas.drawPaint(paint);
546 canvas.flush();
547 PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu);
548 REPORTER_ASSERT(reporter, pixels4 == pixels5);
junov@chromium.org67d74222013-04-12 13:33:01 +0000549}
550
551static void TestDeferredCanvas(skiatest::Reporter* reporter, GrContextFactory* factory) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000552 TestDeferredCanvasBitmapAccess(reporter);
553 TestDeferredCanvasFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000554 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000555 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000556 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000557 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000558 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000559 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.org67d74222013-04-12 13:33:01 +0000560 TestDeferredCanvasSurface(reporter, NULL);
561 if (NULL != factory) {
562 TestDeferredCanvasSurface(reporter, factory);
563 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000564}
565
566#include "TestClassDef.h"
junov@chromium.org67d74222013-04-12 13:33:01 +0000567DEFINE_GPUTESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas)