blob: d527ea30843678724bfe30f5568ed331772aecb8 [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"
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000013#include "SkShader.h"
junov@chromium.org1f9767c2012-02-07 16:27:57 +000014
junov@chromium.org1f9767c2012-02-07 16:27:57 +000015static const int gWidth = 2;
16static const int gHeight = 2;
17
18static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
19 bm->setConfig(config, gWidth, gHeight);
20 bm->allocPixels();
21 bm->eraseColor(color);
22}
23
24static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
25 SkBitmap store;
26
27 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
28 SkDevice device(store);
29 SkDeferredCanvas canvas(&device);
30
31 canvas.clear(0x00000000);
32
33 SkAutoLockPixels alp(store);
34 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
35 SkBitmap accessed = canvas.getDevice()->accessBitmap(false);
36 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
37 REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef());
38}
39
40static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
41 SkBitmap store;
42
43 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
44 SkDevice device(store);
45 SkDeferredCanvas canvas(&device);
46
47 canvas.clear(0x00000000);
48
49 SkAutoLockPixels alp(store);
50 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
51 canvas.flush();
52 REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
53}
54
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000055static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
56 SkBitmap store;
57 SkRect fullRect;
58 fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
59 SkIntToScalar(gHeight));
60 SkRect partialRect;
junov@chromium.orgb1e218e2012-02-13 22:27:58 +000061 partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
62 SkIntToScalar(1), SkIntToScalar(1));
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000063 create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
64 SkDevice device(store);
65 SkDeferredCanvas canvas(&device);
66
67 // verify that frame is intially fresh
junov@chromium.org88e29142012-08-07 16:48:22 +000068 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000069 // no clearing op since last call to isFreshFrame -> not fresh
junov@chromium.org88e29142012-08-07 16:48:22 +000070 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000071
72 // Verify that clear triggers a fresh frame
73 canvas.clear(0x00000000);
junov@chromium.org88e29142012-08-07 16:48:22 +000074 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000075
76 // Verify that clear with saved state triggers a fresh frame
77 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
78 canvas.clear(0x00000000);
79 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +000080 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000081
82 // Verify that clear within a layer does NOT trigger a fresh frame
83 canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
84 canvas.clear(0x00000000);
85 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +000086 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000087
88 // Verify that a clear with clipping triggers a fresh frame
89 // (clear is not affected by clipping)
90 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
91 canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
92 canvas.clear(0x00000000);
93 canvas.restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +000094 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000095
96 // Verify that full frame rects with different forms of opaque paint
97 // trigger frames to be marked as fresh
98 {
99 SkPaint paint;
100 paint.setStyle( SkPaint::kFill_Style );
101 paint.setAlpha( 255 );
102 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000103 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000104 }
skia.committer@gmail.com5b6f9162012-10-12 02:01:15 +0000105 {
junov@chromium.org8cef67a2012-10-11 20:19:15 +0000106 SkPaint paint;
107 paint.setStyle( SkPaint::kFill_Style );
108 paint.setAlpha( 255 );
109 paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
110 canvas.drawRect(fullRect, paint);
111 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
112 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000113 {
114 SkPaint paint;
115 paint.setStyle( SkPaint::kFill_Style );
116 SkBitmap bmp;
117 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
118 bmp.setIsOpaque(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000120 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
121 paint.setShader(shader)->unref();
122 canvas.drawRect(fullRect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000124 }
125
126 // Verify that full frame rects with different forms of non-opaque paint
127 // do not trigger frames to be marked as fresh
128 {
129 SkPaint paint;
130 paint.setStyle( SkPaint::kFill_Style );
131 paint.setAlpha( 254 );
132 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000133 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000134 }
135 {
136 SkPaint paint;
137 paint.setStyle( SkPaint::kFill_Style );
138 SkBitmap bmp;
139 create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
140 bmp.setIsOpaque(false);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000142 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
143 paint.setShader(shader)->unref();
144 canvas.drawRect(fullRect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000146 }
147
148 // Verify that incomplete coverage does not trigger a fresh frame
149 {
150 SkPaint paint;
151 paint.setStyle(SkPaint::kFill_Style);
152 paint.setAlpha(255);
153 canvas.drawRect(partialRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000154 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000155 }
156
157 // Verify that incomplete coverage due to clipping does not trigger a fresh
158 // frame
159 {
160 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
161 canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
162 SkPaint paint;
163 paint.setStyle(SkPaint::kFill_Style);
164 paint.setAlpha(255);
165 canvas.drawRect(fullRect, paint);
junov@chromium.org41e850f2012-12-10 21:24:38 +0000166 canvas.restore();
junov@chromium.org88e29142012-08-07 16:48:22 +0000167 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000168 }
169
170 // Verify that stroked rect does not trigger a fresh frame
171 {
172 SkPaint paint;
173 paint.setStyle( SkPaint::kStroke_Style );
174 paint.setAlpha( 255 );
175 canvas.drawRect(fullRect, paint);
junov@chromium.org88e29142012-08-07 16:48:22 +0000176 REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000177 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000179 // Verify kSrcMode triggers a fresh frame even with transparent color
180 {
181 SkPaint paint;
182 paint.setStyle( SkPaint::kFill_Style );
183 paint.setAlpha( 100 );
184 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
185 canvas.drawRect(fullRect, paint);
junov@chromium.org41e850f2012-12-10 21:24:38 +0000186 REPORTER_ASSERT(reporter, canvas.isFreshFrame());
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000187 }
188}
189
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000190class MockDevice : public SkDevice {
191public:
192 MockDevice(const SkBitmap& bm) : SkDevice(bm) {
193 fDrawBitmapCallCount = 0;
194 }
195 virtual void drawBitmap(const SkDraw&, const SkBitmap&,
196 const SkIRect*,
197 const SkMatrix&, const SkPaint&) {
198 fDrawBitmapCallCount++;
199 }
200
201 int fDrawBitmapCallCount;
202};
203
204// Verifies that the deferred canvas triggers a flush when its memory
205// limit is exceeded
206static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
207 SkBitmap store;
208 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
209 store.allocPixels();
210 MockDevice mockDevice(store);
211 SkDeferredCanvas canvas(&mockDevice);
212 canvas.setMaxRecordingStorage(160000);
213
214 SkBitmap sourceImage;
215 // 100 by 100 image, takes 40,000 bytes in memory
216 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
217 sourceImage.allocPixels();
218
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000219 for (int i = 0; i < 5; i++) {
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000220 sourceImage.notifyPixelsChanged(); // to force re-serialization
221 canvas.drawBitmap(sourceImage, 0, 0, NULL);
222 }
223
scroggo@google.com15011ee2012-07-26 20:03:32 +0000224 REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000225}
226
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000227class NotificationCounter : public SkDeferredCanvas::NotificationClient {
228public:
229 NotificationCounter() {
junov@google.com52a00ca2012-10-01 15:27:14 +0000230 fPrepareForDrawCount = fStorageAllocatedChangedCount =
231 fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000232 }
233
234 virtual void prepareForDraw() SK_OVERRIDE {
235 fPrepareForDrawCount++;
236 }
237 virtual void storageAllocatedForRecordingChanged(size_t size) SK_OVERRIDE {
238 fStorageAllocatedChangedCount++;
239 }
240 virtual void flushedDrawCommands() SK_OVERRIDE {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241 fFlushedDrawCommandsCount++;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000242 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000243 virtual void skippedPendingDrawCommands() SK_OVERRIDE {
244 fSkippedPendingDrawCommandsCount++;
245 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000246
247 int fPrepareForDrawCount;
248 int fStorageAllocatedChangedCount;
249 int fFlushedDrawCommandsCount;
junov@google.com52a00ca2012-10-01 15:27:14 +0000250 int fSkippedPendingDrawCommandsCount;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000251};
252
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000253static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
254 SkBitmap store;
255 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
256 store.allocPixels();
257 SkDevice device(store);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000258 NotificationCounter notificationCounter;
junov@chromium.orgd433c4e2012-08-17 14:50:16 +0000259 SkDeferredCanvas canvas(&device);
260 canvas.setNotificationClient(&notificationCounter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000261
262 const int imageCount = 2;
263 SkBitmap sourceImages[imageCount];
264 for (int i = 0; i < imageCount; i++)
265 {
266 sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
267 sourceImages[i].allocPixels();
268 }
269
270 size_t bitmapSize = sourceImages[0].getSize();
271
272 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000273 REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000274 // stored bitmap + drawBitmap command
275 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000276
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000277 // verify that nothing can be freed at this point
bsalomon@google.com100abf42012-09-05 17:40:04 +0000278 REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0U));
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000279
280 // verify that flush leaves image in cache
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000281 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
282 REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000283 canvas.flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000284 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
285 REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000286 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize);
287
288 // verify that after a flush, cached image can be freed
bsalomon@google.com100abf42012-09-05 17:40:04 +0000289 REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0U) >= bitmapSize);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000290
291 // Verify that caching works for avoiding multiple copies of the same bitmap
292 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000293 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000294 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000295 REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
296 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000297 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize);
298
299 // Verify partial eviction based on bytesToFree
300 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000301 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000302 canvas.flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000303 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000304 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize);
305 size_t bytesFreed = canvas.freeMemoryIfPossible(1);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000306 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000307 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
308 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
309
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310 // Verifiy that partial purge works, image zero is in cache but not reffed by
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000311 // a pending draw, while image 1 is locked-in.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000312 canvas.freeMemoryIfPossible(~0U);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000313 REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000314 canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
315 canvas.flush();
316 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
bsalomon@google.com100abf42012-09-05 17:40:04 +0000317 bytesFreed = canvas.freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000318 // only one bitmap should have been freed.
319 REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
320 REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
321 // Clear for next test
322 canvas.flush();
bsalomon@google.com100abf42012-09-05 17:40:04 +0000323 canvas.freeMemoryIfPossible(~0U);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000324 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize);
325
326 // Verify the image cache is sensitive to genID bumps
327 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
328 sourceImages[1].notifyPixelsChanged();
329 canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
330 REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize);
junov@google.com52a00ca2012-10-01 15:27:14 +0000331
332 // Verify that nothing in this test caused commands to be skipped
333 REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
334}
335
336static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
337 SkBitmap store;
338 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
339 store.allocPixels();
340 SkDevice device(store);
341 NotificationCounter notificationCounter;
342 SkDeferredCanvas canvas(&device);
343 canvas.setNotificationClient(&notificationCounter);
344 canvas.clear(0x0);
345 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
346 REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
347 canvas.flush();
348 REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
349 REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
350
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000351}
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000352
junov@chromium.orgce65f382012-10-17 19:36:09 +0000353static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
354 // This is a regression test for crbug.com/155875
355 // This test covers a code path that inserts bitmaps into the bitmap heap through the
356 // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
357 // the flattening and unflattening of the shader.
358 SkBitmap store;
359 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
360 store.allocPixels();
361 SkDevice device(store);
362 SkDeferredCanvas canvas(&device);
363 // test will fail if nbIterations is not in sync with
364 // BITMAPS_TO_KEEP in SkGPipeWrite.cpp
365 const int nbIterations = 5;
366 size_t bytesAllocated = 0;
367 for(int pass = 0; pass < 2; ++pass) {
368 for(int i = 0; i < nbIterations; ++i) {
369 SkPaint paint;
370 SkBitmap paintPattern;
371 paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
372 paintPattern.allocPixels();
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000373 paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
junov@chromium.orgce65f382012-10-17 19:36:09 +0000374 (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
375 canvas.drawPaint(paint);
376 canvas.flush();
377
378 // In the first pass, memory allocation should be monotonically increasing as
379 // the bitmap heap slots fill up. In the second pass memory allocation should be
380 // stable as bitmap heap slots get recycled.
381 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
382 if (pass == 0) {
383 REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
384 bytesAllocated = newBytesAllocated;
385 } else {
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000386 REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000387 }
388 }
389 }
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000390 // All cached resources should be evictable since last canvas call was flush()
junov@chromium.orgce65f382012-10-17 19:36:09 +0000391 canvas.freeMemoryIfPossible(~0);
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000392 REPORTER_ASSERT(reporter, 0 == canvas.storageAllocatedForRecording());
junov@chromium.orgce65f382012-10-17 19:36:09 +0000393}
394
sugoi@google.com7775fd52012-11-21 15:47:04 +0000395static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
396 SkBitmap store;
397 store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
398 store.allocPixels();
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000399
sugoi@google.com7775fd52012-11-21 15:47:04 +0000400 SkBitmap sourceImage;
401 // 100 by 100 image, takes 40,000 bytes in memory
402 sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
403 sourceImage.allocPixels();
404
405 // 1 under : should not store the image
406 {
407 SkDevice device(store);
408 SkDeferredCanvas canvas(&device);
409 canvas.setBitmapSizeThreshold(39999);
410 canvas.drawBitmap(sourceImage, 0, 0, NULL);
411 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
412 REPORTER_ASSERT(reporter, newBytesAllocated == 0);
413 }
414
415 // exact value : should store the image
416 {
417 SkDevice device(store);
418 SkDeferredCanvas canvas(&device);
419 canvas.setBitmapSizeThreshold(40000);
420 canvas.drawBitmap(sourceImage, 0, 0, NULL);
421 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
422 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
423 }
424
425 // 1 over : should still store the image
426 {
427 SkDevice device(store);
428 SkDeferredCanvas canvas(&device);
429 canvas.setBitmapSizeThreshold(40001);
430 canvas.drawBitmap(sourceImage, 0, 0, NULL);
431 size_t newBytesAllocated = canvas.storageAllocatedForRecording();
432 REPORTER_ASSERT(reporter, newBytesAllocated > 0);
433 }
434}
435
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000436static void TestDeferredCanvas(skiatest::Reporter* reporter) {
437 TestDeferredCanvasBitmapAccess(reporter);
438 TestDeferredCanvasFlush(reporter);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000439 TestDeferredCanvasFreshFrame(reporter);
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000440 TestDeferredCanvasMemoryLimit(reporter);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000441 TestDeferredCanvasBitmapCaching(reporter);
junov@google.com52a00ca2012-10-01 15:27:14 +0000442 TestDeferredCanvasSkip(reporter);
junov@chromium.orgce65f382012-10-17 19:36:09 +0000443 TestDeferredCanvasBitmapShaderNoLeak(reporter);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000444 TestDeferredCanvasBitmapSizeThreshold(reporter);
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000445}
446
447#include "TestClassDef.h"
448DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas)