2012-02-07 16:27:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "Test.h"
|
|
|
|
#include "SkBitmap.h"
|
2012-10-17 19:36:09 +00:00
|
|
|
#include "SkBitmapProcShader.h"
|
2012-02-07 16:27:57 +00:00
|
|
|
#include "SkDeferredCanvas.h"
|
2012-08-07 16:48:22 +00:00
|
|
|
#include "SkDevice.h"
|
2012-02-13 21:53:45 +00:00
|
|
|
#include "SkShader.h"
|
2012-02-07 16:27:57 +00:00
|
|
|
|
|
|
|
static const int gWidth = 2;
|
|
|
|
static const int gHeight = 2;
|
|
|
|
|
|
|
|
static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
|
|
|
|
bm->setConfig(config, gWidth, gHeight);
|
|
|
|
bm->allocPixels();
|
|
|
|
bm->eraseColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
|
|
|
|
create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
|
|
|
|
SkAutoLockPixels alp(store);
|
|
|
|
REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
|
|
|
|
SkBitmap accessed = canvas.getDevice()->accessBitmap(false);
|
|
|
|
REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
|
|
|
|
REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
|
|
|
|
create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
|
|
|
|
SkAutoLockPixels alp(store);
|
|
|
|
REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred
|
|
|
|
canvas.flush();
|
|
|
|
REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:53:45 +00:00
|
|
|
static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
SkRect fullRect;
|
|
|
|
fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth),
|
|
|
|
SkIntToScalar(gHeight));
|
|
|
|
SkRect partialRect;
|
2012-02-13 22:27:58 +00:00
|
|
|
partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(1), SkIntToScalar(1));
|
2012-02-13 21:53:45 +00:00
|
|
|
create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
|
|
|
|
// verify that frame is intially fresh
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
// no clearing op since last call to isFreshFrame -> not fresh
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that clear triggers a fresh frame
|
|
|
|
canvas.clear(0x00000000);
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that clear with saved state triggers a fresh frame
|
|
|
|
canvas.save(SkCanvas::kMatrixClip_SaveFlag);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
canvas.restore();
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that clear within a layer does NOT trigger a fresh frame
|
|
|
|
canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
canvas.restore();
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that a clear with clipping triggers a fresh frame
|
|
|
|
// (clear is not affected by clipping)
|
|
|
|
canvas.save(SkCanvas::kMatrixClip_SaveFlag);
|
|
|
|
canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
canvas.restore();
|
2012-08-23 18:14:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that full frame rects with different forms of opaque paint
|
|
|
|
// trigger frames to be marked as fresh
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
paint.setAlpha( 255 );
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-10-11 20:19:15 +00:00
|
|
|
}
|
2012-10-12 02:01:15 +00:00
|
|
|
{
|
2012-10-11 20:19:15 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
paint.setAlpha( 255 );
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
|
|
|
|
canvas.drawRect(fullRect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
SkBitmap bmp;
|
|
|
|
create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
|
|
|
|
bmp.setIsOpaque(true);
|
2012-08-23 18:14:13 +00:00
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bmp,
|
2012-02-13 21:53:45 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
paint.setShader(shader)->unref();
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that full frame rects with different forms of non-opaque paint
|
|
|
|
// do not trigger frames to be marked as fresh
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
paint.setAlpha( 254 );
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
SkBitmap bmp;
|
|
|
|
create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
|
|
|
|
bmp.setIsOpaque(false);
|
2012-08-23 18:14:13 +00:00
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bmp,
|
2012-02-13 21:53:45 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
paint.setShader(shader)->unref();
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that incomplete coverage does not trigger a fresh frame
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
|
|
|
canvas.drawRect(partialRect, paint);
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that incomplete coverage due to clipping does not trigger a fresh
|
|
|
|
// frame
|
|
|
|
{
|
|
|
|
canvas.save(SkCanvas::kMatrixClip_SaveFlag);
|
|
|
|
canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-12-10 21:24:38 +00:00
|
|
|
canvas.restore();
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
2012-12-13 16:30:39 +00:00
|
|
|
{
|
|
|
|
canvas.save(SkCanvas::kMatrixClip_SaveFlag);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
paint.setAlpha( 255 );
|
|
|
|
SkPath path;
|
|
|
|
path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
|
|
|
|
canvas.clipPath(path, SkRegion::kIntersect_Op, false);
|
|
|
|
canvas.drawRect(fullRect, paint);
|
|
|
|
canvas.restore();
|
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
|
|
|
}
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that stroked rect does not trigger a fresh frame
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kStroke_Style );
|
|
|
|
paint.setAlpha( 255 );
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-08-07 16:48:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-02-13 21:53:45 +00:00
|
|
|
// Verify kSrcMode triggers a fresh frame even with transparent color
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle( SkPaint::kFill_Style );
|
|
|
|
paint.setAlpha( 100 );
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
canvas.drawRect(fullRect, paint);
|
2012-12-10 21:24:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 13:35:14 +00:00
|
|
|
class MockDevice : public SkDevice {
|
|
|
|
public:
|
|
|
|
MockDevice(const SkBitmap& bm) : SkDevice(bm) {
|
|
|
|
fDrawBitmapCallCount = 0;
|
|
|
|
}
|
|
|
|
virtual void drawBitmap(const SkDraw&, const SkBitmap&,
|
|
|
|
const SkIRect*,
|
|
|
|
const SkMatrix&, const SkPaint&) {
|
|
|
|
fDrawBitmapCallCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fDrawBitmapCallCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Verifies that the deferred canvas triggers a flush when its memory
|
|
|
|
// limit is exceeded
|
|
|
|
static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
store.allocPixels();
|
|
|
|
MockDevice mockDevice(store);
|
|
|
|
SkDeferredCanvas canvas(&mockDevice);
|
|
|
|
canvas.setMaxRecordingStorage(160000);
|
|
|
|
|
|
|
|
SkBitmap sourceImage;
|
|
|
|
// 100 by 100 image, takes 40,000 bytes in memory
|
|
|
|
sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
sourceImage.allocPixels();
|
|
|
|
|
2012-07-25 17:27:13 +00:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2012-07-23 13:35:14 +00:00
|
|
|
sourceImage.notifyPixelsChanged(); // to force re-serialization
|
|
|
|
canvas.drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2012-07-26 20:03:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4);
|
2012-07-23 13:35:14 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 13:36:26 +00:00
|
|
|
class NotificationCounter : public SkDeferredCanvas::NotificationClient {
|
|
|
|
public:
|
|
|
|
NotificationCounter() {
|
2012-10-01 15:27:14 +00:00
|
|
|
fPrepareForDrawCount = fStorageAllocatedChangedCount =
|
|
|
|
fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0;
|
2012-08-14 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepareForDraw() SK_OVERRIDE {
|
|
|
|
fPrepareForDrawCount++;
|
|
|
|
}
|
2013-02-27 19:17:41 +00:00
|
|
|
virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {
|
2012-08-14 13:36:26 +00:00
|
|
|
fStorageAllocatedChangedCount++;
|
|
|
|
}
|
|
|
|
virtual void flushedDrawCommands() SK_OVERRIDE {
|
2012-08-23 18:14:13 +00:00
|
|
|
fFlushedDrawCommandsCount++;
|
2012-08-14 13:36:26 +00:00
|
|
|
}
|
2012-10-01 15:27:14 +00:00
|
|
|
virtual void skippedPendingDrawCommands() SK_OVERRIDE {
|
|
|
|
fSkippedPendingDrawCommandsCount++;
|
|
|
|
}
|
2012-08-14 13:36:26 +00:00
|
|
|
|
|
|
|
int fPrepareForDrawCount;
|
|
|
|
int fStorageAllocatedChangedCount;
|
|
|
|
int fFlushedDrawCommandsCount;
|
2012-10-01 15:27:14 +00:00
|
|
|
int fSkippedPendingDrawCommandsCount;
|
2013-02-07 21:02:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkDeferredCanvas::NotificationClient INHERITED;
|
2012-08-14 13:36:26 +00:00
|
|
|
};
|
|
|
|
|
2012-08-07 14:26:57 +00:00
|
|
|
static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
store.allocPixels();
|
|
|
|
SkDevice device(store);
|
2012-08-14 13:36:26 +00:00
|
|
|
NotificationCounter notificationCounter;
|
2012-08-17 14:50:16 +00:00
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
canvas.setNotificationClient(¬ificationCounter);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
const int imageCount = 2;
|
|
|
|
SkBitmap sourceImages[imageCount];
|
|
|
|
for (int i = 0; i < imageCount; i++)
|
|
|
|
{
|
|
|
|
sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
sourceImages[i].allocPixels();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bitmapSize = sourceImages[0].getSize();
|
|
|
|
|
|
|
|
canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
// stored bitmap + drawBitmap command
|
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-07 14:26:57 +00:00
|
|
|
// verify that nothing can be freed at this point
|
2012-09-05 17:40:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0U));
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
// verify that flush leaves image in cache
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
canvas.flush();
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize);
|
|
|
|
|
|
|
|
// verify that after a flush, cached image can be freed
|
2012-09-05 17:40:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0U) >= bitmapSize);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
// Verify that caching works for avoiding multiple copies of the same bitmap
|
|
|
|
canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize);
|
|
|
|
|
|
|
|
// Verify partial eviction based on bytesToFree
|
|
|
|
canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
canvas.flush();
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize);
|
|
|
|
size_t bytesFreed = canvas.freeMemoryIfPossible(1);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
|
|
|
|
REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
// Verifiy that partial purge works, image zero is in cache but not reffed by
|
2012-08-07 14:26:57 +00:00
|
|
|
// a pending draw, while image 1 is locked-in.
|
2012-09-05 17:40:04 +00:00
|
|
|
canvas.freeMemoryIfPossible(~0U);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
|
2012-08-07 14:26:57 +00:00
|
|
|
canvas.drawBitmap(sourceImages[0], 0, 0, NULL);
|
|
|
|
canvas.flush();
|
|
|
|
canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
|
2012-09-05 17:40:04 +00:00
|
|
|
bytesFreed = canvas.freeMemoryIfPossible(~0U);
|
2012-08-07 14:26:57 +00:00
|
|
|
// only one bitmap should have been freed.
|
|
|
|
REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize);
|
|
|
|
REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize);
|
|
|
|
// Clear for next test
|
|
|
|
canvas.flush();
|
2012-09-05 17:40:04 +00:00
|
|
|
canvas.freeMemoryIfPossible(~0U);
|
2012-08-07 14:26:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize);
|
|
|
|
|
|
|
|
// Verify the image cache is sensitive to genID bumps
|
|
|
|
canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
|
|
|
|
sourceImages[1].notifyPixelsChanged();
|
|
|
|
canvas.drawBitmap(sourceImages[1], 0, 0, NULL);
|
|
|
|
REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize);
|
2012-10-01 15:27:14 +00:00
|
|
|
|
|
|
|
// Verify that nothing in this test caused commands to be skipped
|
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
store.allocPixels();
|
|
|
|
SkDevice device(store);
|
|
|
|
NotificationCounter notificationCounter;
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
canvas.setNotificationClient(¬ificationCounter);
|
|
|
|
canvas.clear(0x0);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
canvas.flush();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
|
2012-08-07 14:26:57 +00:00
|
|
|
}
|
2012-07-23 13:35:14 +00:00
|
|
|
|
2012-10-17 19:36:09 +00:00
|
|
|
static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) {
|
|
|
|
// This is a regression test for crbug.com/155875
|
|
|
|
// This test covers a code path that inserts bitmaps into the bitmap heap through the
|
|
|
|
// flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through
|
|
|
|
// the flattening and unflattening of the shader.
|
|
|
|
SkBitmap store;
|
|
|
|
store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
store.allocPixels();
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
// test will fail if nbIterations is not in sync with
|
|
|
|
// BITMAPS_TO_KEEP in SkGPipeWrite.cpp
|
|
|
|
const int nbIterations = 5;
|
|
|
|
size_t bytesAllocated = 0;
|
|
|
|
for(int pass = 0; pass < 2; ++pass) {
|
|
|
|
for(int i = 0; i < nbIterations; ++i) {
|
|
|
|
SkPaint paint;
|
|
|
|
SkBitmap paintPattern;
|
|
|
|
paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
|
|
|
|
paintPattern.allocPixels();
|
2012-10-18 02:01:23 +00:00
|
|
|
paint.setShader(SkNEW_ARGS(SkBitmapProcShader,
|
2012-10-17 19:36:09 +00:00
|
|
|
(paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref();
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
canvas.flush();
|
|
|
|
|
|
|
|
// In the first pass, memory allocation should be monotonically increasing as
|
|
|
|
// the bitmap heap slots fill up. In the second pass memory allocation should be
|
|
|
|
// stable as bitmap heap slots get recycled.
|
|
|
|
size_t newBytesAllocated = canvas.storageAllocatedForRecording();
|
|
|
|
if (pass == 0) {
|
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated);
|
|
|
|
bytesAllocated = newBytesAllocated;
|
|
|
|
} else {
|
2012-10-18 02:01:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated);
|
2012-10-17 19:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 02:01:23 +00:00
|
|
|
// All cached resources should be evictable since last canvas call was flush()
|
2013-01-08 13:23:32 +00:00
|
|
|
canvas.freeMemoryIfPossible(~0U);
|
2012-10-18 02:01:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == canvas.storageAllocatedForRecording());
|
2012-10-17 19:36:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 15:47:04 +00:00
|
|
|
static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap store;
|
|
|
|
store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
store.allocPixels();
|
2012-11-22 02:02:41 +00:00
|
|
|
|
2012-11-21 15:47:04 +00:00
|
|
|
SkBitmap sourceImage;
|
|
|
|
// 100 by 100 image, takes 40,000 bytes in memory
|
|
|
|
sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
|
|
|
|
sourceImage.allocPixels();
|
|
|
|
|
|
|
|
// 1 under : should not store the image
|
|
|
|
{
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
canvas.setBitmapSizeThreshold(39999);
|
|
|
|
canvas.drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas.storageAllocatedForRecording();
|
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// exact value : should store the image
|
|
|
|
{
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
canvas.setBitmapSizeThreshold(40000);
|
|
|
|
canvas.drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas.storageAllocatedForRecording();
|
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1 over : should still store the image
|
|
|
|
{
|
|
|
|
SkDevice device(store);
|
|
|
|
SkDeferredCanvas canvas(&device);
|
|
|
|
canvas.setBitmapSizeThreshold(40001);
|
|
|
|
canvas.drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas.storageAllocatedForRecording();
|
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:27:57 +00:00
|
|
|
static void TestDeferredCanvas(skiatest::Reporter* reporter) {
|
|
|
|
TestDeferredCanvasBitmapAccess(reporter);
|
|
|
|
TestDeferredCanvasFlush(reporter);
|
2012-02-13 21:53:45 +00:00
|
|
|
TestDeferredCanvasFreshFrame(reporter);
|
2012-07-23 13:35:14 +00:00
|
|
|
TestDeferredCanvasMemoryLimit(reporter);
|
2012-08-07 14:26:57 +00:00
|
|
|
TestDeferredCanvasBitmapCaching(reporter);
|
2012-10-01 15:27:14 +00:00
|
|
|
TestDeferredCanvasSkip(reporter);
|
2012-10-17 19:36:09 +00:00
|
|
|
TestDeferredCanvasBitmapShaderNoLeak(reporter);
|
2012-11-21 15:47:04 +00:00
|
|
|
TestDeferredCanvasBitmapSizeThreshold(reporter);
|
2012-02-07 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas)
|