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
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkAutoPixmapStorage.h"
|
|
|
|
#include "src/core/SkSpecialImage.h"
|
|
|
|
#include "src/core/SkSpecialSurface.h"
|
2020-01-28 22:02:49 +00:00
|
|
|
#include "src/gpu/GrBitmapTextureMaker.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrSurfaceProxy.h"
|
|
|
|
#include "src/gpu/GrTextureProxy.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/SkGr.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "tests/Test.h"
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
// 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() {
|
2019-09-20 16:40:55 +00:00
|
|
|
SkImageInfo ii = SkImageInfo::Make(kFullSize, kFullSize, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
|
2016-02-04 18:52:42 +00:00
|
|
|
SkBitmap bm;
|
2019-09-20 16:40:55 +00:00
|
|
|
bm.allocPixels(ii);
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
SkCanvas temp(bm);
|
|
|
|
|
|
|
|
temp.clear(SK_ColorGREEN);
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(false);
|
|
|
|
|
|
|
|
temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
|
2016-03-29 16:03:52 +00:00
|
|
|
SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
|
2016-02-04 18:52:42 +00:00
|
|
|
p);
|
|
|
|
|
2020-01-28 22:02:49 +00:00
|
|
|
bm.setImmutable();
|
2016-02-04 18:52:42 +00:00
|
|
|
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,
|
2020-07-23 17:54:35 +00:00
|
|
|
GrRecordingContext* rContext, bool isGPUBacked) {
|
2016-04-20 18:48:36 +00:00
|
|
|
const SkIRect subset = img->subset();
|
2019-06-21 20:01:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, kPad == subset.left());
|
|
|
|
REPORTER_ASSERT(reporter, kPad == subset.top());
|
2016-02-04 18:52:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
|
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
|
|
|
|
|
|
|
|
//--------------
|
2017-02-21 15:19:29 +00:00
|
|
|
// Test that isTextureBacked reports the correct backing type
|
2017-01-31 16:31:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
|
2016-04-08 19:10:42 +00:00
|
|
|
|
|
|
|
//--------------
|
2020-02-05 15:37:27 +00:00
|
|
|
// Test view - as long as there is a context this should succeed
|
2020-07-23 17:54:35 +00:00
|
|
|
if (rContext) {
|
|
|
|
GrSurfaceProxyView view = img->view(rContext);
|
2020-01-29 16:11:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, view.asTextureProxy());
|
2016-04-08 19:10:42 +00:00
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
//--------------
|
2020-06-22 18:51:22 +00:00
|
|
|
// Test getROPixels - this only works for raster-backed special images
|
|
|
|
if (!img->isTextureBacked()) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
|
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
|
|
|
|
REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
|
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
//--------------
|
2016-03-21 20:44:18 +00:00
|
|
|
// Test that draw restricts itself to the subset
|
2019-08-14 18:35:15 +00:00
|
|
|
sk_sp<SkSpecialSurface> surf(img->makeSurface(kN32_SkColorType, img->getColorSpace(),
|
|
|
|
SkISize::Make(kFullSize, kFullSize),
|
2017-01-17 15:48:53 +00:00
|
|
|
kPremul_SkAlphaType));
|
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;
|
2017-01-17 15:48:53 +00:00
|
|
|
bm.allocN32Pixels(kFullSize, kFullSize, false);
|
2016-02-04 18:52:42 +00:00
|
|
|
|
|
|
|
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));
|
2016-03-21 20:44:18 +00:00
|
|
|
|
|
|
|
//--------------
|
2017-02-18 21:58:09 +00:00
|
|
|
// Test that asImage & makeTightSurface return appropriately sized objects
|
2016-03-21 20:44:18 +00:00
|
|
|
// of the correct backing type
|
|
|
|
SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
|
|
|
|
{
|
2017-02-18 21:58:09 +00:00
|
|
|
sk_sp<SkImage> tightImg(img->asImage(&newSubset));
|
2016-03-21 20:44:18 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
|
|
|
|
REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
|
2017-03-21 20:22:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked());
|
2016-03-21 20:44:18 +00:00
|
|
|
SkPixmap tmpPixmap;
|
2017-01-31 16:31:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap));
|
2016-03-21 20:44:18 +00:00
|
|
|
}
|
|
|
|
{
|
2019-08-14 18:35:15 +00:00
|
|
|
sk_sp<SkSurface> tightSurf(img->makeTightSurface(kN32_SkColorType, img->getColorSpace(),
|
|
|
|
subset.size()));
|
2016-03-21 20:44:18 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
|
|
|
|
REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
|
2018-04-05 13:30:38 +00:00
|
|
|
GrBackendTexture backendTex = tightSurf->getBackendTexture(
|
|
|
|
SkSurface::kDiscardWrite_BackendHandleAccess);
|
|
|
|
REPORTER_ASSERT(reporter, isGPUBacked == backendTex.isValid());
|
2016-03-21 20:44:18 +00:00
|
|
|
SkPixmap tmpPixmap;
|
2017-01-31 16:31:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap));
|
2016-03-21 20:44:18 +00:00
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
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-04-20 18:48:36 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg1, reporter, nullptr, false);
|
2016-03-17 13:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg2, reporter, nullptr, false);
|
2016-03-17 13:58:39 +00:00
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:01:22 +00:00
|
|
|
static void test_specialimage_image(skiatest::Reporter* reporter) {
|
2016-02-04 18:52:42 +00:00
|
|
|
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(
|
2019-01-10 21:34:22 +00:00
|
|
|
nullptr,
|
2016-03-17 13:58:39 +00:00
|
|
|
SkIRect::MakeWH(kFullSize, kFullSize),
|
2018-10-12 16:01:22 +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
|
|
|
{
|
2019-01-10 21:34:22 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset, fullImage));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg1, reporter, nullptr, false);
|
2016-03-17 13:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 21:31:39 +00:00
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg2, reporter, nullptr, false);
|
2016-03-17 13:58:39 +00:00
|
|
|
}
|
2016-02-04 18:52:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:28:24 +00:00
|
|
|
DEF_TEST(SpecialImage_Image_Legacy, reporter) {
|
2018-10-12 16:01:22 +00:00
|
|
|
test_specialimage_image(reporter);
|
2016-11-18 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:59:58 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
2016-02-04 18:52:42 +00:00
|
|
|
SkBitmap bm = create_bm();
|
2020-03-18 14:06:13 +00:00
|
|
|
GrBitmapTextureMaker maker(context, bm, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
|
2020-07-21 13:27:25 +00:00
|
|
|
auto view = maker.view(GrMipmapped::kNo);
|
2020-02-03 19:17:08 +00:00
|
|
|
if (!view.proxy()) {
|
2016-02-04 18:52:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:07:32 +00:00
|
|
|
sk_sp<SkSpecialImage> fullSImg(
|
|
|
|
SkSpecialImage::MakeDeferredFromGpu(context,
|
|
|
|
SkIRect::MakeWH(kFullSize, kFullSize),
|
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
|
|
|
view,
|
|
|
|
maker.colorType(),
|
|
|
|
nullptr));
|
2016-11-01 21:28:40 +00:00
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
|
2020-02-28 23:07:32 +00:00
|
|
|
context,
|
|
|
|
subset,
|
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
|
|
|
std::move(view),
|
|
|
|
maker.colorType(),
|
|
|
|
nullptr));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg1, reporter, context, true);
|
2016-11-01 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
|
2019-06-21 20:01:42 +00:00
|
|
|
test_image(subSImg2, reporter, context, true);
|
2016-11-01 21:28:40 +00:00
|
|
|
}
|
|
|
|
}
|