2016-02-04 18:52:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file
|
|
|
|
*/
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkAutoPixmapStorage.h"
|
2016-02-04 18:52:42 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImage.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkPixmap.h"
|
2016-02-04 18:52:42 +00:00
|
|
|
#include "SkSpecialImage.h"
|
|
|
|
#include "SkSpecialSurface.h"
|
|
|
|
#include "Test.h"
|
2016-03-07 20:45:14 +00:00
|
|
|
#include "TestingSpecialImageAccess.h"
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// This test creates backing resources exactly sized to [kFullSize x kFullSize].
|
|
|
|
// It then wraps them in an SkSpecialImage with only the center (red) region being active.
|
|
|
|
// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
|
|
|
|
// of the inactive (green) region leaked out.
|
|
|
|
|
|
|
|
static const int kSmallerSize = 10;
|
|
|
|
static const int kPad = 3;
|
|
|
|
static const int kFullSize = kSmallerSize + 2 * kPad;
|
|
|
|
|
|
|
|
// Create a bitmap with red in the center and green around it
|
|
|
|
static SkBitmap create_bm() {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(kFullSize, kFullSize, true);
|
|
|
|
|
|
|
|
SkCanvas temp(bm);
|
|
|
|
|
|
|
|
temp.clear(SK_ColorGREEN);
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(false);
|
|
|
|
|
|
|
|
temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
|
|
|
|
SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
|
|
|
|
p);
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
|
2016-03-17 21:31:39 +00:00
|
|
|
static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
|
2016-03-17 13:58:39 +00:00
|
|
|
bool peekPixelsSucceeds, bool peekTextureSucceeds,
|
|
|
|
int offset, int size) {
|
2016-03-17 21:31:39 +00:00
|
|
|
const SkIRect subset = TestingSpecialImageAccess::Subset(img.get());
|
2016-03-17 13:58:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, offset == subset.left());
|
|
|
|
REPORTER_ASSERT(reporter, offset == subset.top());
|
2016-02-04 18:52:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
|
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
|
|
|
|
|
|
|
|
//--------------
|
2016-03-17 21:31:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, peekTextureSucceeds ==
|
|
|
|
!!TestingSpecialImageAccess::PeekTexture(img.get()));
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
//--------------
|
|
|
|
SkPixmap pixmap;
|
|
|
|
REPORTER_ASSERT(reporter, peekPixelsSucceeds ==
|
2016-03-17 21:31:39 +00:00
|
|
|
!!TestingSpecialImageAccess::PeekPixels(img.get(), &pixmap));
|
2016-02-04 18:52:42 +00:00
|
|
|
if (peekPixelsSucceeds) {
|
2016-03-17 13:58:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, size == pixmap.width());
|
|
|
|
REPORTER_ASSERT(reporter, size == pixmap.height());
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
|
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialSurface> surf(img->makeSurface(info));
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
SkCanvas* canvas = surf->getCanvas();
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorBLUE);
|
2016-02-16 20:09:36 +00:00
|
|
|
img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(kFullSize, kFullSize, true);
|
|
|
|
|
|
|
|
bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
|
|
|
|
SkASSERT_RELEASE(result);
|
|
|
|
|
|
|
|
// Only the center (red) portion should've been drawn into the canvas
|
|
|
|
REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
|
|
|
|
REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
|
|
|
|
REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
|
|
|
|
kSmallerSize+kPad-1));
|
|
|
|
REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
|
|
|
|
kSmallerSize+kPad));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SpecialImage_Raster, reporter) {
|
|
|
|
SkBitmap bm = create_bm();
|
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr,
|
|
|
|
SkIRect::MakeWH(kFullSize, kFullSize),
|
|
|
|
bm));
|
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, subset, bm));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(subSImg1, reporter, true, false, kPad, kFullSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(subSImg2, reporter, true, false, 0, kSmallerSize);
|
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SpecialImage_Image, reporter) {
|
|
|
|
SkBitmap bm = create_bm();
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
|
2016-02-04 18:52:42 +00:00
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr,
|
|
|
|
SkIRect::MakeWH(kFullSize, kFullSize),
|
2016-03-17 21:31:39 +00:00
|
|
|
fullImage));
|
2016-03-17 13:58:39 +00:00
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset,
|
|
|
|
fullImage));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(subSImg1, reporter, true, false, kPad, kFullSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(subSImg2, reporter, true, false, 0, kSmallerSize);
|
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
DEF_TEST(SpecialImage_Pixmap, reporter) {
|
|
|
|
SkAutoPixmapStorage pixmap;
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
|
|
|
|
pixmap.alloc(info);
|
|
|
|
pixmap.erase(SK_ColorGREEN);
|
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
|
|
|
pixmap.erase(SK_ColorRED, subset);
|
|
|
|
|
|
|
|
{
|
|
|
|
// The SkAutoPixmapStorage keeps hold of the memory
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap,
|
|
|
|
nullptr, nullptr));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(img, reporter, true, false, kPad, kFullSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// The image takes ownership of the memory
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr, subset, pixmap,
|
2016-03-17 21:31:39 +00:00
|
|
|
[] (void* addr, void*) -> void {
|
|
|
|
sk_free(addr);
|
|
|
|
},
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr));
|
|
|
|
pixmap.release();
|
|
|
|
test_image(img, reporter, true, false, kPad, kFullSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-03-18 15:14:27 +00:00
|
|
|
|
|
|
|
static void test_texture_backed(skiatest::Reporter* reporter,
|
|
|
|
const sk_sp<SkSpecialImage>& orig,
|
|
|
|
const sk_sp<SkSpecialImage>& gpuBacked) {
|
|
|
|
REPORTER_ASSERT(reporter, gpuBacked);
|
|
|
|
REPORTER_ASSERT(reporter, gpuBacked->peekTexture());
|
|
|
|
REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
|
|
|
|
REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
|
|
|
|
gpuBacked->subset().height() == orig->subset().height());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test out the SkSpecialImage::makeTextureImage entry point
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, context) {
|
|
|
|
SkBitmap bm = create_bm();
|
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
|
|
|
{
|
|
|
|
// raster
|
|
|
|
sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
|
|
|
|
nullptr,
|
|
|
|
SkIRect::MakeWH(kFullSize,
|
|
|
|
kFullSize),
|
|
|
|
bm));
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
|
|
|
|
test_texture_backed(reporter, rasterImage, fromRaster);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
|
|
|
|
|
|
|
|
sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
|
|
|
|
test_texture_backed(reporter, subRasterImage, fromSubRaster);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// gpu
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fFlags = kNone_GrSurfaceFlags;
|
|
|
|
desc.fWidth = kFullSize;
|
|
|
|
desc.fHeight = kFullSize;
|
|
|
|
|
|
|
|
SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
|
|
|
|
SkBudgeted::kNo,
|
|
|
|
bm.getPixels(),
|
|
|
|
0));
|
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
|
|
|
|
nullptr,
|
|
|
|
SkIRect::MakeWH(kFullSize,
|
|
|
|
kFullSize),
|
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
|
|
|
texture));
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
|
|
|
|
test_texture_backed(reporter, gpuImage, fromGPU);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
|
|
|
|
|
|
|
|
sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
|
|
|
|
test_texture_backed(reporter, subGPUImage, fromSubGPU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
|
|
|
|
SkBitmap bm = create_bm();
|
|
|
|
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fFlags = kNone_GrSurfaceFlags;
|
|
|
|
desc.fWidth = kFullSize;
|
|
|
|
desc.fHeight = kFullSize;
|
|
|
|
|
2016-03-18 15:14:27 +00:00
|
|
|
SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
|
|
|
|
SkBudgeted::kNo,
|
2016-02-04 18:52:42 +00:00
|
|
|
bm.getPixels(), 0));
|
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr,
|
|
|
|
SkIRect::MakeWH(kFullSize, kFullSize),
|
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
|
|
|
texture));
|
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
|
2016-03-17 13:58:39 +00:00
|
|
|
nullptr, subset,
|
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
|
|
|
texture));
|
|
|
|
test_image(subSImg1, reporter, false, true, kPad, kFullSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
|
2016-03-17 13:58:39 +00:00
|
|
|
test_image(subSImg2, reporter, false, true, kPad, kFullSize);
|
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|