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.
|
|
|
|
*/
|
2014-01-10 22:08:27 +00:00
|
|
|
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "../src/image/SkImagePriv.h"
|
|
|
|
#include "../src/image/SkSurface_Base.h"
|
2012-02-07 16:27:57 +00:00
|
|
|
#include "SkBitmap.h"
|
2013-08-29 11:54:56 +00:00
|
|
|
#include "SkBitmapDevice.h"
|
2012-10-17 19:36:09 +00:00
|
|
|
#include "SkBitmapProcShader.h"
|
2012-02-07 16:27:57 +00:00
|
|
|
#include "SkDeferredCanvas.h"
|
2013-03-20 00:49:57 +00:00
|
|
|
#include "SkGradientShader.h"
|
2012-02-13 21:53:45 +00:00
|
|
|
#include "SkShader.h"
|
2014-02-04 15:34:10 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2014-03-07 03:25:16 +00:00
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
2013-04-12 13:33:01 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContextFactory.h"
|
|
|
|
#else
|
|
|
|
class GrContextFactory;
|
|
|
|
#endif
|
2012-02-07 16:27:57 +00:00
|
|
|
|
|
|
|
static const int gWidth = 2;
|
|
|
|
static const int gHeight = 2;
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
static void create(SkBitmap* bm, SkColor color) {
|
|
|
|
bm->allocN32Pixels(gWidth, gHeight);
|
2012-02-07 16:27:57 +00:00
|
|
|
bm->eraseColor(color);
|
|
|
|
}
|
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
static SkSurface* createSurface(SkColor color) {
|
|
|
|
SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight);
|
|
|
|
surface->getCanvas()->clear(color);
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPMColor read_pixel(SkSurface* surface, int x, int y) {
|
|
|
|
SkPMColor pixel = 0;
|
|
|
|
SkBitmap bitmap;
|
2014-05-29 15:57:20 +00:00
|
|
|
bitmap.installPixels(SkImageInfo::MakeN32Premul(1, 1), &pixel, 4);
|
2014-02-04 15:34:10 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2014-02-04 16:14:58 +00:00
|
|
|
surface->draw(&canvas, -SkIntToScalar(x), -SkIntToScalar(y), &paint);
|
2014-02-04 15:34:10 +00:00
|
|
|
return pixel;
|
|
|
|
}
|
|
|
|
|
2013-08-02 15:36:02 +00:00
|
|
|
class MockSurface : public SkSurface_Base {
|
|
|
|
public:
|
2014-09-22 14:29:03 +00:00
|
|
|
MockSurface(int width, int height) : SkSurface_Base(width, height, NULL) {
|
2013-08-02 15:36:02 +00:00
|
|
|
clearCounts();
|
2014-02-13 22:00:04 +00:00
|
|
|
fBitmap.allocN32Pixels(width, height);
|
2013-08-02 15:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkCanvas* onNewCanvas() SK_OVERRIDE {
|
|
|
|
return SkNEW_ARGS(SkCanvas, (fBitmap));
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:46:54 +00:00
|
|
|
virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE {
|
2013-08-02 15:36:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkImage* onNewImageSnapshot() SK_OVERRIDE {
|
2014-11-21 16:46:37 +00:00
|
|
|
return SkNewImageFromBitmap(fBitmap, true, &this->props());
|
2013-08-02 15:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE {
|
|
|
|
if (mode == SkSurface::kDiscard_ContentChangeMode) {
|
|
|
|
fDiscardCount++;
|
|
|
|
} else {
|
|
|
|
fRetainCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearCounts() {
|
|
|
|
fDiscardCount = 0;
|
2013-08-06 07:01:27 +00:00
|
|
|
fRetainCount = 0;
|
2013-08-02 15:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fDiscardCount, fRetainCount;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10)));
|
2013-08-09 14:24:59 +00:00
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2013-08-02 15:36:02 +00:00
|
|
|
|
|
|
|
SkBitmap srcBitmap;
|
2014-03-25 16:20:24 +00:00
|
|
|
srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
|
2013-08-02 15:36:02 +00:00
|
|
|
srcBitmap.eraseColor(SK_ColorGREEN);
|
|
|
|
// Tests below depend on this bitmap being recognized as opaque
|
|
|
|
|
|
|
|
// Preliminary sanity check: no copy on write if no active snapshot
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 1: Discard notification happens upon flushing
|
|
|
|
// with an Image attached.
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 2: Opaque writePixels
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 3: writePixels that partially covers the canvas
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 4: unpremultiplied opaque writePixels that entirely
|
|
|
|
// covers the canvas
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
2014-03-25 16:20:24 +00:00
|
|
|
canvas->writePixels(srcBitmap, 0, 0);
|
2013-08-02 15:36:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 5: unpremultiplied opaque writePixels that partially
|
|
|
|
// covers the canvas
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
2014-03-25 16:20:24 +00:00
|
|
|
canvas->writePixels(srcBitmap, 5, 0);
|
2013-08-02 15:36:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 6: unpremultiplied opaque writePixels that entirely
|
|
|
|
// covers the canvas, preceded by clear
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
2014-03-25 16:20:24 +00:00
|
|
|
canvas->writePixels(srcBitmap, 0, 0);
|
2013-08-02 15:36:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 7: unpremultiplied opaque writePixels that partially
|
|
|
|
// covers the canvas, preceeded by a clear
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
2014-03-25 16:20:24 +00:00
|
|
|
canvas->writePixels(srcBitmap, 5, 0);
|
2013-08-02 15:36:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
// Case 8: unpremultiplied opaque writePixels that partially
|
|
|
|
// covers the canvas, preceeded by a drawREct that partially
|
|
|
|
// covers the canvas
|
|
|
|
surface->clearCounts();
|
|
|
|
SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
2014-03-25 16:20:24 +00:00
|
|
|
canvas->writePixels(srcBitmap, 5, 0);
|
2013-08-02 15:36:02 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == surface->fRetainCount);
|
|
|
|
|
|
|
|
surface->clearCounts();
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == surface->fRetainCount);
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:27:57 +00:00
|
|
|
static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) {
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2012-02-07 16:27:57 +00:00
|
|
|
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clear(0x00000000);
|
2012-02-07 16:27:57 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
// verify that clear was deferred
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFFFFFFF == read_pixel(surface, 0, 0));
|
|
|
|
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->flush();
|
2014-02-04 15:34:10 +00:00
|
|
|
|
|
|
|
// verify that clear was executed
|
|
|
|
REPORTER_ASSERT(reporter, 0 == read_pixel(surface, 0, 0));
|
2012-02-07 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-02-13 21:53:45 +00:00
|
|
|
static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) {
|
|
|
|
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));
|
2014-02-04 15:34:10 +00:00
|
|
|
|
|
|
|
SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// verify that frame is intially fresh
|
2013-05-28 17:39:08 +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
|
2013-05-28 17:39:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that clear triggers a fresh frame
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clear(0x00000000);
|
|
|
|
REPORTER_ASSERT(reporter, canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that clear with saved state triggers a fresh frame
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clear(0x00000000);
|
|
|
|
canvas->restore();
|
|
|
|
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
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->saveLayer(NULL, NULL);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clear(0x00000000);
|
|
|
|
canvas->restore();
|
|
|
|
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)
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
canvas->restore();
|
|
|
|
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;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
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;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
2012-10-11 20:19:15 +00:00
|
|
|
paint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2012-02-13 21:53:45 +00:00
|
|
|
SkBitmap bmp;
|
2014-02-13 22:00:04 +00:00
|
|
|
create(&bmp, 0xFFFFFFFF);
|
2013-10-21 14:00:07 +00:00
|
|
|
bmp.setAlphaType(kOpaque_SkAlphaType);
|
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();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
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;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(254);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2013-03-20 00:49:57 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
// Defining a cone that partially overlaps the canvas
|
|
|
|
const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
|
|
|
|
const SkScalar r1 = SkIntToScalar(1);
|
|
|
|
const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0));
|
|
|
|
const SkScalar r2 = SkIntToScalar(5);
|
|
|
|
const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE};
|
|
|
|
const SkScalar pos[2] = {0, SK_Scalar1};
|
|
|
|
SkShader* shader = SkGradientShader::CreateTwoPointConical(
|
2014-05-22 12:27:41 +00:00
|
|
|
pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode);
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setShader(shader)->unref();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2012-02-13 21:53:45 +00:00
|
|
|
SkBitmap bmp;
|
2014-02-13 22:00:04 +00:00
|
|
|
create(&bmp, 0xFFFFFFFF);
|
2013-10-21 14:00:07 +00:00
|
|
|
bmp.setAlphaType(kPremul_SkAlphaType);
|
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();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
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);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(partialRect, paint);
|
|
|
|
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
|
|
|
|
{
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false);
|
2012-02-13 21:53:45 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
2012-12-13 16:30:39 +00:00
|
|
|
{
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2012-12-13 16:30:39 +00:00
|
|
|
SkPaint paint;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(255);
|
2012-12-13 16:30:39 +00:00
|
|
|
SkPath path;
|
|
|
|
path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2));
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->clipPath(path, SkRegion::kIntersect_Op, false);
|
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isFreshFrame());
|
2012-12-13 16:30:39 +00:00
|
|
|
}
|
2012-02-13 21:53:45 +00:00
|
|
|
|
|
|
|
// Verify that stroked rect does not trigger a fresh frame
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setAlpha(255);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
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;
|
2013-03-20 00:49:57 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAlpha(100);
|
2012-02-13 21:53:45 +00:00
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(fullRect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, canvas->isFreshFrame());
|
2012-02-13 21:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 11:54:56 +00:00
|
|
|
class MockDevice : public SkBitmapDevice {
|
2012-07-23 13:35:14 +00:00
|
|
|
public:
|
2013-08-29 11:54:56 +00:00
|
|
|
MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) {
|
2012-07-23 13:35:14 +00:00
|
|
|
fDrawBitmapCallCount = 0;
|
|
|
|
}
|
|
|
|
virtual void drawBitmap(const SkDraw&, const SkBitmap&,
|
2013-07-23 11:28:45 +00:00
|
|
|
const SkMatrix&, const SkPaint&) SK_OVERRIDE {
|
2012-07-23 13:35:14 +00:00
|
|
|
fDrawBitmapCallCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fDrawBitmapCallCount;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
// Verifies that the deferred canvas triggers a flush when its memory
|
|
|
|
// limit is exceeded
|
|
|
|
static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
NotificationCounter notificationCounter;
|
|
|
|
canvas->setNotificationClient(¬ificationCounter);
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
canvas->setMaxRecordingStorage(160000);
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
SkBitmap sourceImage;
|
|
|
|
// 100 by 100 image, takes 40,000 bytes in memory
|
2014-02-13 22:00:04 +00:00
|
|
|
sourceImage.allocN32Pixels(100, 100);
|
2014-06-27 15:03:17 +00:00
|
|
|
sourceImage.eraseColor(SK_ColorGREEN);
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
sourceImage.notifyPixelsChanged(); // to force re-serialization
|
|
|
|
canvas->drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
}
|
2014-02-05 03:01:48 +00:00
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
}
|
|
|
|
|
2014-03-27 15:48:52 +00:00
|
|
|
static void TestDeferredCanvasSilentFlush(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<SkSurface> surface(createSurface(0));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
|
|
|
|
|
|
|
NotificationCounter notificationCounter;
|
|
|
|
canvas->setNotificationClient(¬ificationCounter);
|
|
|
|
|
|
|
|
canvas->silentFlush(); // will skip the initial clear that was recorded in createSurface
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
|
|
|
|
}
|
|
|
|
|
2012-08-07 14:26:57 +00:00
|
|
|
static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) {
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
|
|
|
|
2012-08-14 13:36:26 +00:00
|
|
|
NotificationCounter notificationCounter;
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setNotificationClient(¬ificationCounter);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
const int imageCount = 2;
|
|
|
|
SkBitmap sourceImages[imageCount];
|
2014-02-13 22:00:04 +00:00
|
|
|
for (int i = 0; i < imageCount; i++) {
|
|
|
|
sourceImages[i].allocN32Pixels(100, 100);
|
2014-06-27 15:03:17 +00:00
|
|
|
sourceImages[i].eraseColor(SK_ColorGREEN);
|
2012-08-07 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t bitmapSize = sourceImages[0].getSize();
|
|
|
|
|
2013-05-28 17:39:08 +00:00
|
|
|
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
|
2013-05-28 17:39:08 +00:00
|
|
|
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
|
2013-05-28 17:39:08 +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);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->flush();
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
// verify that after a flush, cached image can be freed
|
2013-05-28 17:39:08 +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
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
|
2014-07-30 16:17:54 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
// Verify partial eviction based on bytesToFree
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->flush();
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
|
2013-05-28 17:39:08 +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.
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->freeMemoryIfPossible(~0U);
|
2012-08-14 13:36:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawBitmap(sourceImages[0], 0, 0, NULL);
|
|
|
|
canvas->flush();
|
|
|
|
canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
|
|
|
|
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
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->flush();
|
|
|
|
canvas->freeMemoryIfPossible(~0U);
|
|
|
|
REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize);
|
2012-08-07 14:26:57 +00:00
|
|
|
|
|
|
|
// Verify the image cache is sensitive to genID bumps
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawBitmap(sourceImages[1], 0, 0, NULL);
|
2012-08-07 14:26:57 +00:00
|
|
|
sourceImages[1].notifyPixelsChanged();
|
2013-05-28 17:39:08 +00:00
|
|
|
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) {
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
|
|
|
|
2012-10-01 15:27:14 +00:00
|
|
|
NotificationCounter notificationCounter;
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setNotificationClient(¬ificationCounter);
|
|
|
|
canvas->clear(0x0);
|
2012-10-01 15:27:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount);
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->flush();
|
2012-10-01 15:27:14 +00:00
|
|
|
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.
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2012-10-17 19:36:09 +00:00
|
|
|
// 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;
|
2014-02-13 22:00:04 +00:00
|
|
|
paintPattern.allocN32Pixels(10, 10);
|
2014-06-27 15:03:17 +00:00
|
|
|
paintPattern.eraseColor(SK_ColorGREEN);
|
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();
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->flush();
|
2012-10-17 19:36:09 +00:00
|
|
|
|
|
|
|
// 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.
|
2013-05-28 17:39:08 +00:00
|
|
|
size_t newBytesAllocated = canvas->storageAllocatedForRecording();
|
2012-10-17 19:36:09 +00:00
|
|
|
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-05-28 17:39:08 +00:00
|
|
|
canvas->freeMemoryIfPossible(~0U);
|
|
|
|
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) {
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
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
|
2014-02-13 22:00:04 +00:00
|
|
|
sourceImage.allocN32Pixels(100, 100);
|
2014-06-27 15:03:17 +00:00
|
|
|
sourceImage.eraseColor(SK_ColorGREEN);
|
2012-11-21 15:47:04 +00:00
|
|
|
|
|
|
|
// 1 under : should not store the image
|
|
|
|
{
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setBitmapSizeThreshold(39999);
|
|
|
|
canvas->drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas->storageAllocatedForRecording();
|
2012-11-21 15:47:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// exact value : should store the image
|
|
|
|
{
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setBitmapSizeThreshold(40000);
|
|
|
|
canvas->drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas->storageAllocatedForRecording();
|
2012-11-21 15:47:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1 over : should still store the image
|
|
|
|
{
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setBitmapSizeThreshold(40001);
|
|
|
|
canvas->drawBitmap(sourceImage, 0, 0, NULL);
|
|
|
|
size_t newBytesAllocated = canvas->storageAllocatedForRecording();
|
2012-11-21 15:47:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, newBytesAllocated > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 13:33:01 +00:00
|
|
|
|
2014-06-30 16:05:34 +00:00
|
|
|
typedef const void* PixelPtr;
|
2013-04-12 13:33:01 +00:00
|
|
|
// Returns an opaque pointer which, either points to a GrTexture or RAM pixel
|
|
|
|
// buffer. Used to test pointer equality do determine whether a surface points
|
|
|
|
// to the same pixel data storage as before.
|
2014-06-30 16:05:34 +00:00
|
|
|
static PixelPtr get_surface_ptr(SkSurface* surface, bool useGpu) {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (useGpu) {
|
|
|
|
return surface->getCanvas()->internal_private_accessTopLayerRenderTarget()->asTexture();
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return surface->peekPixels(NULL, NULL);
|
|
|
|
}
|
2013-04-12 13:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
2014-02-17 17:02:17 +00:00
|
|
|
SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
|
2014-09-05 20:34:00 +00:00
|
|
|
bool useGpu = SkToBool(factory);
|
2014-07-17 17:50:59 +00:00
|
|
|
int cnt;
|
2013-04-12 13:33:01 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (useGpu) {
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = GrContextFactory::kGLContextTypeCnt;
|
2013-04-12 13:33:01 +00:00
|
|
|
} else {
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = 1;
|
2013-04-12 13:33:01 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
SkASSERT(!useGpu);
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = 1;
|
2013-04-12 13:33:01 +00:00
|
|
|
#endif
|
2014-07-17 17:50:59 +00:00
|
|
|
for (int i = 0; i < cnt; ++i) {
|
|
|
|
SkSurface* surface;
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (useGpu) {
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glCtxType);
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 14:29:03 +00:00
|
|
|
surface = SkSurface::NewRenderTarget(context, imageSpec, 0, NULL);
|
2014-07-17 17:50:59 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
surface = SkSurface::NewRaster(imageSpec);
|
|
|
|
}
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(surface);
|
2014-07-17 17:50:59 +00:00
|
|
|
SkAutoTUnref<SkSurface> aur(surface);
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
|
|
|
|
|
|
|
|
SkImage* image1 = canvas->newImageSnapshot();
|
|
|
|
SkAutoTUnref<SkImage> aur_i1(image1);
|
|
|
|
PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
|
|
|
|
// The following clear would normally trigger a copy on write, but
|
|
|
|
// it won't because rendering is deferred.
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
// Obtaining a snapshot directly from the surface (as opposed to the
|
|
|
|
// SkDeferredCanvas) will not trigger a flush of deferred draw operations
|
|
|
|
// and will therefore return the same image as the previous snapshot.
|
|
|
|
SkImage* image2 = surface->newImageSnapshot();
|
|
|
|
SkAutoTUnref<SkImage> aur_i2(image2);
|
|
|
|
// Images identical because of deferral
|
|
|
|
REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID());
|
|
|
|
// Now we obtain a snpshot via the deferred canvas, which triggers a flush.
|
|
|
|
// Because there is a pending clear, this will generate a different image.
|
|
|
|
SkImage* image3 = canvas->newImageSnapshot();
|
|
|
|
SkAutoTUnref<SkImage> aur_i3(image3);
|
|
|
|
REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID());
|
|
|
|
// Verify that backing store is now a different buffer because of copy on
|
|
|
|
// write
|
|
|
|
PixelPtr pixels2 = get_surface_ptr(surface, useGpu);
|
|
|
|
REPORTER_ASSERT(reporter, pixels1 != pixels2);
|
|
|
|
// Verify copy-on write with a draw operation that gets deferred by
|
|
|
|
// the in order draw buffer.
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
SkImage* image4 = canvas->newImageSnapshot(); // implicit flush
|
|
|
|
SkAutoTUnref<SkImage> aur_i4(image4);
|
|
|
|
REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID());
|
|
|
|
PixelPtr pixels3 = get_surface_ptr(surface, useGpu);
|
|
|
|
REPORTER_ASSERT(reporter, pixels2 != pixels3);
|
|
|
|
// Verify that a direct canvas flush with a pending draw does not trigger
|
|
|
|
// a copy on write when the surface is not sharing its buffer with an
|
|
|
|
// SkImage.
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
canvas->flush();
|
|
|
|
PixelPtr pixels4 = get_surface_ptr(surface, useGpu);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->flush();
|
|
|
|
PixelPtr pixels5 = get_surface_ptr(surface, useGpu);
|
|
|
|
REPORTER_ASSERT(reporter, pixels4 == pixels5);
|
|
|
|
}
|
2013-04-12 13:33:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 17:13:00 +00:00
|
|
|
static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
2014-02-17 17:02:17 +00:00
|
|
|
SkImageInfo imageSpec = SkImageInfo::MakeN32Premul(10, 10);
|
2013-05-24 17:13:00 +00:00
|
|
|
SkSurface* surface;
|
|
|
|
SkSurface* alternateSurface;
|
2014-09-05 20:34:00 +00:00
|
|
|
bool useGpu = SkToBool(factory);
|
2014-07-17 17:50:59 +00:00
|
|
|
int cnt;
|
2013-05-24 17:13:00 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (useGpu) {
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = GrContextFactory::kGLContextTypeCnt;
|
2013-05-24 17:13:00 +00:00
|
|
|
} else {
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = 1;
|
2013-05-24 17:13:00 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
SkASSERT(!useGpu);
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = 1;
|
2013-05-24 17:13:00 +00:00
|
|
|
#endif
|
2014-07-17 17:50:59 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < cnt; ++i) {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (useGpu) {
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glCtxType);
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-22 14:29:03 +00:00
|
|
|
surface = SkSurface::NewRenderTarget(context, imageSpec, 0, NULL);
|
|
|
|
alternateSurface = SkSurface::NewRenderTarget(context, imageSpec, 0, NULL);
|
2014-07-30 16:17:54 +00:00
|
|
|
} else
|
2014-07-17 17:50:59 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
surface = SkSurface::NewRaster(imageSpec);
|
|
|
|
alternateSurface = SkSurface::NewRaster(imageSpec);
|
|
|
|
}
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(surface);
|
|
|
|
SkASSERT(alternateSurface);
|
2014-07-17 17:50:59 +00:00
|
|
|
SkAutoTUnref<SkSurface> aur1(surface);
|
|
|
|
SkAutoTUnref<SkSurface> aur2(alternateSurface);
|
|
|
|
PixelPtr pixels1 = get_surface_ptr(surface, useGpu);
|
|
|
|
PixelPtr pixels2 = get_surface_ptr(alternateSurface, useGpu);
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface));
|
|
|
|
SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot());
|
|
|
|
canvas->setSurface(alternateSurface);
|
|
|
|
SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
|
|
|
|
// Verify that none of the above operations triggered a surface copy on write.
|
|
|
|
REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
|
|
|
|
REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) == pixels2);
|
|
|
|
// Verify that a flushed draw command will trigger a copy on write on alternateSurface.
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, get_surface_ptr(surface, useGpu) == pixels1);
|
|
|
|
REPORTER_ASSERT(reporter, get_surface_ptr(alternateSurface, useGpu) != pixels2);
|
|
|
|
}
|
2013-05-24 17:13:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 20:16:17 +00:00
|
|
|
static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) {
|
2014-02-04 15:34:10 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(100, 100));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
|
|
|
|
2013-05-21 20:16:17 +00:00
|
|
|
NotificationCounter notificationCounter;
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->setNotificationClient(¬ificationCounter);
|
2014-02-04 15:34:10 +00:00
|
|
|
|
2014-02-05 15:32:21 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
|
|
|
|
SkAutoTUnref<SkSurface> secondarySurface(canvas->newSurface(info));
|
|
|
|
|
2013-05-21 20:16:17 +00:00
|
|
|
SkRect rect = SkRect::MakeWH(5, 5);
|
|
|
|
SkPaint paint;
|
|
|
|
// After spawning a compatible canvas:
|
|
|
|
// 1) Verify that secondary canvas is usable and does not report to the notification client.
|
2014-02-05 15:32:21 +00:00
|
|
|
surface->getCanvas()->drawRect(rect, paint);
|
2013-05-21 20:16:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0);
|
|
|
|
// 2) Verify that original canvas is usable and still reports to the notification client.
|
2013-05-28 17:39:08 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
2013-05-21 20:16:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1);
|
|
|
|
}
|
|
|
|
|
2014-09-16 05:30:38 +00:00
|
|
|
static void TestDeferredCanvasGetCanvasSize(skiatest::Reporter* reporter) {
|
|
|
|
SkRect rect;
|
|
|
|
rect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIntToScalar(gHeight));
|
|
|
|
SkRect clip;
|
|
|
|
clip.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToScalar(1));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkISize size = SkISize::Make(gWidth, gHeight);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
|
|
|
|
SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()));
|
|
|
|
SkSurface* newSurface = SkSurface::NewRasterPMColor(4, 4);
|
|
|
|
SkAutoTUnref<SkSurface> aur(newSurface);
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (i == 1) {
|
|
|
|
canvas->setSurface(newSurface);
|
|
|
|
size = SkISize::Make(4, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that canvas size is correctly initialized or set
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that clear, clip and draw the canvas will not change its size
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
canvas->clipRect(clip, SkRegion::kIntersect_Op, false);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that flush the canvas will not change its size
|
|
|
|
canvas->flush();
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that clear canvas with saved state will not change its size
|
|
|
|
canvas->save();
|
|
|
|
canvas->clear(0xFFFFFFFF);
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that restore canvas state will not change its size
|
|
|
|
canvas->restore();
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that clear within a layer will not change canvas size
|
|
|
|
canvas->saveLayer(&clip, &paint);
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
|
|
|
|
// Verify that restore from a layer will not change canvas size
|
|
|
|
canvas->restore();
|
|
|
|
REPORTER_ASSERT(reporter, size == canvas->getCanvasSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:33:57 +00:00
|
|
|
DEF_TEST(DeferredCanvas_CPU, reporter) {
|
2012-02-07 16:27:57 +00:00
|
|
|
TestDeferredCanvasFlush(reporter);
|
2014-03-27 15:48:52 +00:00
|
|
|
TestDeferredCanvasSilentFlush(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);
|
2013-05-21 20:16:17 +00:00
|
|
|
TestDeferredCanvasCreateCompatibleDevice(reporter);
|
2013-08-02 15:36:02 +00:00
|
|
|
TestDeferredCanvasWritePixelsToSurface(reporter);
|
2014-09-16 05:30:38 +00:00
|
|
|
TestDeferredCanvasGetCanvasSize(reporter);
|
2013-04-12 13:33:01 +00:00
|
|
|
TestDeferredCanvasSurface(reporter, NULL);
|
2013-05-24 17:13:00 +00:00
|
|
|
TestDeferredCanvasSetSurface(reporter, NULL);
|
2014-05-07 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST(DeferredCanvas_GPU, reporter, factory) {
|
|
|
|
if (factory != NULL) {
|
2014-05-07 12:33:02 +00:00
|
|
|
TestDeferredCanvasSurface(reporter, factory);
|
|
|
|
TestDeferredCanvasSetSurface(reporter, factory);
|
|
|
|
}
|
2012-02-07 16:27:57 +00:00
|
|
|
}
|