Add unit test for pinned SkImages
Change-Id: I2e14353bc865b5994cc90ad643a6a58604955957 Reviewed-on: https://skia-review.googlesource.com/53500 Reviewed-by: Derek Sollenberger <djsollen@google.com> Commit-Queue: Robert Phillips <robertphillips@google.com>
This commit is contained in:
parent
4faa0408c1
commit
3ec9573ac3
@ -160,6 +160,7 @@ tests_sources = [
|
||||
"$_tests/PictureBBHTest.cpp",
|
||||
"$_tests/PictureShaderTest.cpp",
|
||||
"$_tests/PictureTest.cpp",
|
||||
"$_tests/PinnedImageTest.cpp",
|
||||
"$_tests/PipeTest.cpp",
|
||||
"$_tests/PixelRefTest.cpp",
|
||||
"$_tests/Point3Test.cpp",
|
||||
|
@ -196,12 +196,15 @@ void GrResourceCache::releaseAll() {
|
||||
|
||||
this->processFreedGpuResources();
|
||||
|
||||
// We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
|
||||
// they also have a raw pointer back to this class (which is presumably going away)!
|
||||
UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
|
||||
for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies) ; !iter.done(); ++iter) {
|
||||
for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
|
||||
GrTextureProxy& tmp = *iter;
|
||||
|
||||
this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
|
||||
}
|
||||
SkASSERT(!fUniquelyKeyedProxies.count());
|
||||
|
||||
while(fNonpurgeableResources.count()) {
|
||||
GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
|
||||
|
132
tests/PinnedImageTest.cpp
Normal file
132
tests/PinnedImageTest.cpp
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.
|
||||
|
||||
#include "Test.h"
|
||||
|
||||
#if SK_SUPPORT_GPU
|
||||
|
||||
using namespace sk_gpu_test;
|
||||
|
||||
#include "GrContextFactory.h"
|
||||
|
||||
#include "SkCanvas.h"
|
||||
#include "SkImagePriv.h"
|
||||
#include "SkSurface.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void basic_test(skiatest::Reporter* reporter, GrContext* context) {
|
||||
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);
|
||||
|
||||
sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
|
||||
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
|
||||
{
|
||||
SkImage_pinAsTexture(img.get(), context); // pin at blue
|
||||
|
||||
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));
|
||||
|
||||
SkImage_unpinAsTexture(img.get(), context);
|
||||
}
|
||||
|
||||
// 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;
|
||||
GrContext* context = nullptr;
|
||||
|
||||
{
|
||||
GrContextFactory testFactory;
|
||||
ContextInfo info = testFactory.getContextInfo(ctxType);
|
||||
context = info.grContext();
|
||||
if (!context) {
|
||||
continue;
|
||||
}
|
||||
|
||||
img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
|
||||
if (!SkImage_pinAsTexture(img.get(), context)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// The GrContext used to pin the image is gone at this point!
|
||||
// "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.
|
||||
SkImage_unpinAsTexture(img.get(), context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
|
||||
basic_test(reporter, ctxInfo.grContext());
|
||||
cleanup_test(reporter);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user