2017-09-29 19:10:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This is a GPU-backend specific test.
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2017-09-29 19:10:39 +00:00
|
|
|
|
|
|
|
using namespace sk_gpu_test;
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
2017-09-29 19:10:39 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkImagePriv.h"
|
2017-09-29 19:10:39 +00:00
|
|
|
|
|
|
|
static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(ii);
|
|
|
|
|
|
|
|
surf->readPixels(bm, 0, 0);
|
|
|
|
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = 0; x < bm.width(); ++x) {
|
|
|
|
if (bm.getColor(x, y) != color) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
|
2017-09-29 19:10:39 +00:00
|
|
|
const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(ii);
|
|
|
|
|
|
|
|
SkCanvas bmCanvas(bm);
|
|
|
|
bmCanvas.clear(SK_ColorRED);
|
|
|
|
|
|
|
|
// We start off with the raster image being all red.
|
|
|
|
sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
|
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
|
2017-09-29 19:10:39 +00:00
|
|
|
SkCanvas* canvas = gpuSurface->getCanvas();
|
|
|
|
|
|
|
|
// w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
|
|
|
|
{
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
|
|
|
|
|
|
|
|
bmCanvas.clear(SK_ColorGREEN);
|
|
|
|
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
|
|
|
|
}
|
|
|
|
|
|
|
|
// w/ pinning - the gpu draw is stuck at the pinned state
|
|
|
|
{
|
2020-07-23 13:41:57 +00:00
|
|
|
SkImage_pinAsTexture(img.get(), rContext); // pin at blue
|
2017-09-29 19:10:39 +00:00
|
|
|
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
|
|
|
|
|
|
|
|
bmCanvas.clear(SK_ColorBLUE);
|
|
|
|
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
|
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
SkImage_unpinAsTexture(img.get(), rContext);
|
2017-09-29 19:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// once unpinned local changes will be picked up
|
|
|
|
{
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting the context while there are still pinned images shouldn't result in a crash.
|
|
|
|
static void cleanup_test(skiatest::Reporter* reporter) {
|
|
|
|
|
|
|
|
const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(ii);
|
|
|
|
|
|
|
|
SkCanvas bmCanvas(bm);
|
|
|
|
bmCanvas.clear(SK_ColorRED);
|
|
|
|
|
|
|
|
for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
|
|
|
|
GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<SkImage> img;
|
2020-07-23 17:54:35 +00:00
|
|
|
GrDirectContext* dContext = nullptr;
|
2017-09-29 19:10:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GrContextFactory testFactory;
|
|
|
|
ContextInfo info = testFactory.getContextInfo(ctxType);
|
2020-07-23 17:54:35 +00:00
|
|
|
dContext = info.directContext();
|
|
|
|
if (!dContext) {
|
2017-09-29 19:10:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
|
2020-07-23 17:54:35 +00:00
|
|
|
if (!SkImage_pinAsTexture(img.get(), dContext)) {
|
2017-09-29 19:10:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 17:54:35 +00:00
|
|
|
// The context used to pin the image is gone at this point!
|
2017-09-29 19:10:39 +00:00
|
|
|
// "context" isn't technically used in this call but it can't be null!
|
|
|
|
// We don't really want to support this use case but it currently happens.
|
2020-07-23 17:54:35 +00:00
|
|
|
SkImage_unpinAsTexture(img.get(), dContext);
|
2017-09-29 19:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
basic_test(reporter, ctxInfo.directContext());
|
2017-09-29 19:10:39 +00:00
|
|
|
cleanup_test(reporter);
|
|
|
|
}
|