2018-03-09 17:05:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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 "tests/Test.h"
|
2018-03-09 17:05:04 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkPromiseImageTexture.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/gpu/GrTexture.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrGpu.h"
|
|
|
|
#include "src/image/SkImage_Gpu.h"
|
2018-03-09 17:05:04 +00:00
|
|
|
|
|
|
|
using namespace sk_gpu_test;
|
|
|
|
|
|
|
|
struct PromiseTextureChecker {
|
2019-01-24 17:18:33 +00:00
|
|
|
// shared indicates whether the backend texture is used to fulfill more than one promise
|
|
|
|
// image.
|
|
|
|
explicit PromiseTextureChecker(const GrBackendTexture& tex, skiatest::Reporter* reporter,
|
|
|
|
bool shared)
|
2019-01-11 21:03:19 +00:00
|
|
|
: fTexture(SkPromiseImageTexture::Make(tex))
|
2019-01-24 17:18:33 +00:00
|
|
|
, fReporter(reporter)
|
|
|
|
, fShared(shared)
|
2018-03-09 17:05:04 +00:00
|
|
|
, fFulfillCount(0)
|
2018-03-16 18:57:21 +00:00
|
|
|
, fReleaseCount(0)
|
|
|
|
, fDoneCount(0) {}
|
2019-01-11 21:03:19 +00:00
|
|
|
sk_sp<SkPromiseImageTexture> fTexture;
|
2019-01-24 17:18:33 +00:00
|
|
|
skiatest::Reporter* fReporter;
|
|
|
|
bool fShared;
|
2018-03-09 17:05:04 +00:00
|
|
|
int fFulfillCount;
|
|
|
|
int fReleaseCount;
|
2018-03-16 18:57:21 +00:00
|
|
|
int fDoneCount;
|
2019-01-10 17:09:52 +00:00
|
|
|
|
|
|
|
/**
|
2019-02-28 15:03:03 +00:00
|
|
|
* Releases the SkPromiseImageTexture. Used to test that cached GrTexture representations
|
|
|
|
* in the cache are freed.
|
2019-01-10 17:09:52 +00:00
|
|
|
*/
|
2019-02-28 15:03:03 +00:00
|
|
|
void releaseTexture() { fTexture.reset(); }
|
2019-01-10 17:09:52 +00:00
|
|
|
|
|
|
|
SkTArray<GrUniqueKey> uniqueKeys() const {
|
2019-01-11 21:03:19 +00:00
|
|
|
return fTexture->testingOnly_uniqueKeysToInvalidate();
|
2018-03-09 17:05:04 +00:00
|
|
|
}
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
static sk_sp<SkPromiseImageTexture> Fulfill(void* self) {
|
2019-01-10 17:09:52 +00:00
|
|
|
auto checker = static_cast<PromiseTextureChecker*>(self);
|
|
|
|
checker->fFulfillCount++;
|
2019-01-11 21:03:19 +00:00
|
|
|
return checker->fTexture;
|
2018-03-09 17:05:04 +00:00
|
|
|
}
|
2019-01-24 17:18:33 +00:00
|
|
|
static void Release(void* self) {
|
|
|
|
auto checker = static_cast<PromiseTextureChecker*>(self);
|
|
|
|
checker->fReleaseCount++;
|
|
|
|
if (!checker->fShared) {
|
|
|
|
// This is only used in a single threaded fashion with a single promise image. So
|
|
|
|
// every fulfill should be balanced by a release before the next fulfill.
|
|
|
|
REPORTER_ASSERT(checker->fReporter, checker->fReleaseCount == checker->fFulfillCount);
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 18:57:21 +00:00
|
|
|
static void Done(void* self) {
|
|
|
|
static_cast<PromiseTextureChecker*>(self)->fDoneCount++;
|
|
|
|
}
|
2018-03-09 17:05:04 +00:00
|
|
|
};
|
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
enum class ReleaseBalanceExpectation {
|
2019-01-30 22:28:20 +00:00
|
|
|
kBalanced,
|
2019-03-08 18:28:46 +00:00
|
|
|
kAllUnbalanced,
|
|
|
|
kUnbalancedByOne,
|
2019-01-30 22:28:20 +00:00
|
|
|
};
|
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
enum class DoneBalanceExpectation {
|
|
|
|
kBalanced,
|
|
|
|
kAllUnbalanced,
|
|
|
|
kUnknown,
|
|
|
|
kUnbalancedByOne,
|
|
|
|
kBalancedOrOffByOne,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void check_fulfill_and_release_cnts(skiatest::Reporter* reporter,
|
|
|
|
const PromiseTextureChecker& promiseChecker,
|
2018-03-09 17:05:04 +00:00
|
|
|
int expectedFulfillCnt,
|
2019-03-08 18:28:46 +00:00
|
|
|
ReleaseBalanceExpectation releaseBalanceExpecation,
|
|
|
|
DoneBalanceExpectation doneBalanceExpecation) {
|
|
|
|
REPORTER_ASSERT(reporter, promiseChecker.fFulfillCount == expectedFulfillCnt);
|
|
|
|
if (!expectedFulfillCnt) {
|
|
|
|
// Release and Done should only ever be called after Fulfill.
|
|
|
|
REPORTER_ASSERT(reporter, !promiseChecker.fReleaseCount);
|
|
|
|
REPORTER_ASSERT(reporter, !promiseChecker.fDoneCount);
|
|
|
|
return;
|
2018-03-09 17:05:04 +00:00
|
|
|
}
|
2019-03-08 18:28:46 +00:00
|
|
|
int releaseDiff = promiseChecker.fFulfillCount - promiseChecker.fReleaseCount;
|
|
|
|
switch (releaseBalanceExpecation) {
|
|
|
|
case ReleaseBalanceExpectation::kBalanced:
|
|
|
|
REPORTER_ASSERT(reporter, !releaseDiff);
|
|
|
|
break;
|
|
|
|
case ReleaseBalanceExpectation::kAllUnbalanced:
|
|
|
|
REPORTER_ASSERT(reporter, releaseDiff == promiseChecker.fFulfillCount);
|
|
|
|
break;
|
|
|
|
case ReleaseBalanceExpectation::kUnbalancedByOne:
|
|
|
|
REPORTER_ASSERT(reporter, releaseDiff == 1);
|
|
|
|
break;
|
2019-03-07 15:23:30 +00:00
|
|
|
}
|
2019-03-08 18:28:46 +00:00
|
|
|
int doneDiff = promiseChecker.fFulfillCount - promiseChecker.fDoneCount;
|
|
|
|
switch (doneBalanceExpecation) {
|
|
|
|
case DoneBalanceExpectation::kBalanced:
|
|
|
|
REPORTER_ASSERT(reporter, !doneDiff);
|
|
|
|
break;
|
|
|
|
case DoneBalanceExpectation::kAllUnbalanced:
|
|
|
|
REPORTER_ASSERT(reporter, doneDiff == promiseChecker.fFulfillCount);
|
|
|
|
break;
|
|
|
|
case DoneBalanceExpectation::kUnknown:
|
|
|
|
REPORTER_ASSERT(reporter, doneDiff >= 0 && doneDiff <= promiseChecker.fFulfillCount);
|
|
|
|
break;
|
|
|
|
case DoneBalanceExpectation::kUnbalancedByOne:
|
|
|
|
REPORTER_ASSERT(reporter, doneDiff == 1);
|
|
|
|
break;
|
|
|
|
case DoneBalanceExpectation::kBalancedOrOffByOne:
|
|
|
|
REPORTER_ASSERT(reporter, doneDiff == 0 || doneDiff == 1);
|
|
|
|
break;
|
2019-03-07 15:23:30 +00:00
|
|
|
}
|
2019-03-08 18:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void check_unfulfilled(const PromiseTextureChecker& promiseChecker,
|
|
|
|
skiatest::Reporter* reporter) {
|
|
|
|
check_fulfill_and_release_cnts(reporter, promiseChecker, 0,
|
|
|
|
ReleaseBalanceExpectation::kBalanced,
|
|
|
|
DoneBalanceExpectation::kBalanced);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_only_fulfilled(skiatest::Reporter* reporter,
|
|
|
|
const PromiseTextureChecker& promiseChecker,
|
|
|
|
int expectedFulfillCnt = 1) {
|
|
|
|
check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt,
|
|
|
|
ReleaseBalanceExpectation::kAllUnbalanced,
|
|
|
|
DoneBalanceExpectation::kAllUnbalanced);
|
|
|
|
}
|
2019-03-06 18:51:09 +00:00
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
static void check_all_flushed_but_not_synced(skiatest::Reporter* reporter,
|
|
|
|
const PromiseTextureChecker& promiseChecker,
|
|
|
|
GrBackendApi api,
|
|
|
|
int expectedFulfillCnt = 1) {
|
|
|
|
DoneBalanceExpectation doneBalanceExpectation = DoneBalanceExpectation::kBalanced;
|
|
|
|
// On Vulkan Done isn't guaranteed to be called until a sync has occurred.
|
|
|
|
if (api == GrBackendApi::kVulkan) {
|
|
|
|
doneBalanceExpectation = expectedFulfillCnt == 1
|
|
|
|
? DoneBalanceExpectation::kBalancedOrOffByOne
|
|
|
|
: DoneBalanceExpectation::kUnknown;
|
2018-03-16 18:57:21 +00:00
|
|
|
}
|
2019-03-08 18:28:46 +00:00
|
|
|
check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt,
|
|
|
|
ReleaseBalanceExpectation::kBalanced, doneBalanceExpectation);
|
|
|
|
}
|
2018-03-16 18:57:21 +00:00
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
static void check_all_done(skiatest::Reporter* reporter,
|
|
|
|
const PromiseTextureChecker& promiseChecker,
|
|
|
|
int expectedFulfillCnt = 1) {
|
|
|
|
check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt,
|
|
|
|
ReleaseBalanceExpectation::kBalanced,
|
|
|
|
DoneBalanceExpectation::kBalanced);
|
2018-03-09 17:05:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageTest, reporter, ctxInfo) {
|
2019-01-30 22:28:20 +00:00
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
|
|
|
|
GrContext* ctx = ctxInfo.grContext();
|
2019-02-04 18:26:26 +00:00
|
|
|
GrGpu* gpu = ctx->priv().getGpu();
|
2019-01-30 22:28:20 +00:00
|
|
|
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kRGBA_8888_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
|
2019-01-30 22:28:20 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex.isValid());
|
|
|
|
|
|
|
|
GrBackendFormat backendFormat = backendTex.getBackendFormat();
|
|
|
|
REPORTER_ASSERT(reporter, backendFormat.isValid());
|
|
|
|
|
|
|
|
PromiseTextureChecker promiseChecker(backendTex, reporter, false);
|
|
|
|
GrSurfaceOrigin texOrigin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
sk_sp<SkImage> refImg(
|
|
|
|
SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendFormat, kWidth, kHeight,
|
|
|
|
GrMipMapped::kNo, texOrigin,
|
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType,
|
|
|
|
nullptr,
|
|
|
|
PromiseTextureChecker::Fulfill,
|
|
|
|
PromiseTextureChecker::Release,
|
|
|
|
PromiseTextureChecker::Done,
|
2019-03-08 18:28:46 +00:00
|
|
|
&promiseChecker,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2019-03-07 15:23:30 +00:00
|
|
|
canvas->drawImage(refImg, 0, 0);
|
2019-03-08 18:28:46 +00:00
|
|
|
check_unfulfilled(promiseChecker, reporter);
|
|
|
|
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-03-08 18:28:46 +00:00
|
|
|
// We still own the image so we should not have called Release or Done.
|
|
|
|
check_only_fulfilled(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
gpu->testingOnly_flushGpuAndSync();
|
2019-03-08 18:28:46 +00:00
|
|
|
check_only_fulfilled(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
canvas->drawImage(refImg, 0, 0);
|
|
|
|
canvas->drawImage(refImg, 0, 0);
|
|
|
|
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
gpu->testingOnly_flushGpuAndSync();
|
2019-03-08 18:28:46 +00:00
|
|
|
// Image should still be fulfilled from the first time we drew/flushed it.
|
|
|
|
check_only_fulfilled(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
canvas->drawImage(refImg, 0, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-03-08 18:28:46 +00:00
|
|
|
check_only_fulfilled(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
|
|
|
canvas->drawImage(refImg, 0, 0);
|
|
|
|
refImg.reset();
|
2019-03-08 18:28:46 +00:00
|
|
|
// We no longer own the image but the last draw is still unflushed.
|
|
|
|
check_only_fulfilled(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-03-08 18:28:46 +00:00
|
|
|
// Flushing should have called Release. Depending on the backend and timing it may have called
|
|
|
|
// done.
|
|
|
|
check_all_flushed_but_not_synced(reporter, promiseChecker, ctx->backend());
|
2019-01-30 22:28:20 +00:00
|
|
|
gpu->testingOnly_flushGpuAndSync();
|
2019-03-08 18:28:46 +00:00
|
|
|
// Now Done should definitely have been called.
|
|
|
|
check_all_done(reporter, promiseChecker);
|
2019-01-30 22:28:20 +00:00
|
|
|
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex);
|
2019-01-30 22:28:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageTextureReuseDifferentConfig, reporter, ctxInfo) {
|
|
|
|
// Try making two promise SkImages backed by the same texture but with different configs.
|
|
|
|
// This will only be testable on backends where a single texture format (8bit red unorm) can
|
|
|
|
// be used for alpha and gray image color types.
|
|
|
|
|
2019-01-10 17:09:52 +00:00
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
|
|
|
|
GrContext* ctx = ctxInfo.grContext();
|
2019-02-04 18:26:26 +00:00
|
|
|
GrGpu* gpu = ctx->priv().getGpu();
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex1 = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kGray_8_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
|
2019-01-10 17:09:52 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex1.isValid());
|
|
|
|
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex2 = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kAlpha_8_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
|
2019-02-28 15:03:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex2.isValid());
|
|
|
|
if (backendTex1.getBackendFormat() != backendTex2.getBackendFormat()) {
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex1);
|
2019-02-28 15:03:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We only needed this texture to check that alpha and gray color types use the same format.
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex2);
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-01-16 18:51:35 +00:00
|
|
|
SkImageInfo info =
|
|
|
|
SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
2019-01-10 17:09:52 +00:00
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
PromiseTextureChecker promiseChecker(backendTex1, reporter, true);
|
|
|
|
sk_sp<SkImage> alphaImg(SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendTex1.getBackendFormat(), kWidth, kHeight, GrMipMapped::kNo,
|
|
|
|
kTopLeft_GrSurfaceOrigin, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr,
|
|
|
|
PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release,
|
2019-03-08 18:28:46 +00:00
|
|
|
PromiseTextureChecker::Done, &promiseChecker,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
2019-02-28 15:03:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, alphaImg);
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
sk_sp<SkImage> grayImg(SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendTex1.getBackendFormat(), kWidth, kHeight, GrMipMapped::kNo,
|
|
|
|
kBottomLeft_GrSurfaceOrigin, kGray_8_SkColorType, kOpaque_SkAlphaType, nullptr,
|
|
|
|
PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release,
|
2019-03-08 18:28:46 +00:00
|
|
|
PromiseTextureChecker::Done, &promiseChecker,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
2019-02-28 15:03:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, grayImg);
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
canvas->drawImage(alphaImg, 0, 0);
|
|
|
|
canvas->drawImage(grayImg, 1, 1);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-10 17:09:52 +00:00
|
|
|
gpu->testingOnly_flushGpuAndSync();
|
2019-03-08 18:28:46 +00:00
|
|
|
check_only_fulfilled(reporter, promiseChecker, 2);
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
// Because they use different configs, each image should have created a different GrTexture
|
|
|
|
// and they both should still be cached.
|
2019-02-04 18:26:26 +00:00
|
|
|
ctx->priv().getResourceCache()->purgeAsNeeded();
|
2019-01-10 17:09:52 +00:00
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
auto keys = promiseChecker.uniqueKeys();
|
|
|
|
REPORTER_ASSERT(reporter, keys.count() == 2);
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
auto surf = ctx->priv().resourceProvider()->findByUniqueKey<GrSurface>(key);
|
|
|
|
REPORTER_ASSERT(reporter, surf && surf->asTexture());
|
|
|
|
if (surf && surf->asTexture()) {
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
!GrBackendTexture::TestingOnly_Equals(
|
|
|
|
backendTex1, surf->asTexture()->getBackendTexture()));
|
|
|
|
}
|
2019-01-10 17:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
// Invalidate the backing texture, this should invalidate the keys.
|
2019-02-28 15:03:03 +00:00
|
|
|
promiseChecker.releaseTexture();
|
2019-02-04 18:26:26 +00:00
|
|
|
ctx->priv().getResourceCache()->purgeAsNeeded();
|
2019-01-16 18:51:35 +00:00
|
|
|
|
2019-02-28 15:03:03 +00:00
|
|
|
for (const auto& key : keys) {
|
|
|
|
auto surf = ctx->priv().resourceProvider()->findByUniqueKey<GrSurface>(key);
|
|
|
|
REPORTER_ASSERT(reporter, !surf);
|
2019-01-16 18:51:35 +00:00
|
|
|
}
|
2019-03-08 16:12:14 +00:00
|
|
|
alphaImg.reset();
|
2019-03-08 18:28:46 +00:00
|
|
|
ctx->flush(); // We do this to pick up any unref messages that are sent by unref'ing the image.
|
|
|
|
check_fulfill_and_release_cnts(reporter, promiseChecker, 2,
|
|
|
|
ReleaseBalanceExpectation::kUnbalancedByOne,
|
|
|
|
DoneBalanceExpectation::kUnbalancedByOne);
|
2019-03-08 16:12:14 +00:00
|
|
|
grayImg.reset();
|
2019-03-08 18:28:46 +00:00
|
|
|
ctx->flush(); // We do this to pick up any unref messages that are sent by unref'ing the image.
|
|
|
|
check_all_done(reporter, promiseChecker, 2);
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex1);
|
2019-01-16 18:51:35 +00:00
|
|
|
}
|
2019-01-24 17:18:33 +00:00
|
|
|
|
|
|
|
DEF_GPUTEST(PromiseImageTextureShutdown, reporter, ctxInfo) {
|
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
|
|
|
|
// Different ways of killing contexts.
|
|
|
|
using DeathFn = std::function<void(sk_gpu_test::GrContextFactory*, GrContext*)>;
|
|
|
|
DeathFn destroy = [](sk_gpu_test::GrContextFactory* factory, GrContext* context) {
|
|
|
|
factory->destroyContexts();
|
|
|
|
};
|
|
|
|
DeathFn abandon = [](sk_gpu_test::GrContextFactory* factory, GrContext* context) {
|
|
|
|
context->abandonContext();
|
|
|
|
};
|
|
|
|
DeathFn releaseResourcesAndAbandon = [](sk_gpu_test::GrContextFactory* factory,
|
|
|
|
GrContext* context) {
|
|
|
|
context->releaseResourcesAndAbandonContext();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
|
|
|
|
auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
|
|
|
|
// These tests are difficult to get working with Vulkan. See http://skbug.com/8705
|
|
|
|
// and http://skbug.com/8275
|
|
|
|
GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
|
|
|
|
if (api == GrBackendApi::kVulkan) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DeathFn contextKillers[] = {destroy, abandon, releaseResourcesAndAbandon};
|
|
|
|
for (auto contextDeath : contextKillers) {
|
|
|
|
sk_gpu_test::GrContextFactory factory;
|
|
|
|
auto ctx = factory.get(contextType);
|
|
|
|
if (!ctx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kAlpha_8_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
|
2019-01-24 17:18:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex.isValid());
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
PromiseTextureChecker promiseChecker(backendTex, reporter, false);
|
|
|
|
sk_sp<SkImage> image(SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendTex.getBackendFormat(), kWidth, kHeight, GrMipMapped::kNo,
|
2019-01-30 22:28:20 +00:00
|
|
|
kTopLeft_GrSurfaceOrigin, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr,
|
|
|
|
PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release,
|
2019-03-08 18:28:46 +00:00
|
|
|
PromiseTextureChecker::Done, &promiseChecker,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
2019-01-24 17:18:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, image);
|
|
|
|
|
|
|
|
canvas->drawImage(image, 0, 0);
|
|
|
|
image.reset();
|
|
|
|
// If the surface still holds a ref to the context then the factory will not be able
|
|
|
|
// to destroy the context (and instead will release-all-and-abandon).
|
|
|
|
surface.reset();
|
|
|
|
|
|
|
|
ctx->flush();
|
|
|
|
contextDeath(&factory, ctx);
|
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
check_all_done(reporter, promiseChecker);
|
2019-01-24 17:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageTextureFullCache, reporter, ctxInfo) {
|
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
|
|
|
|
GrContext* ctx = ctxInfo.grContext();
|
|
|
|
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kAlpha_8_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
|
2019-01-24 17:18:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex.isValid());
|
|
|
|
|
|
|
|
SkImageInfo info =
|
|
|
|
SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
PromiseTextureChecker promiseChecker(backendTex, reporter, false);
|
|
|
|
sk_sp<SkImage> image(SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendTex.getBackendFormat(), kWidth, kHeight, GrMipMapped::kNo,
|
|
|
|
kTopLeft_GrSurfaceOrigin, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr,
|
|
|
|
PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release,
|
2019-03-08 18:28:46 +00:00
|
|
|
PromiseTextureChecker::Done, &promiseChecker,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
2019-01-24 17:18:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, image);
|
|
|
|
|
|
|
|
// Make the cache full. This tests that we don't preemptively purge cached textures for
|
|
|
|
// fulfillment due to cache pressure.
|
|
|
|
static constexpr int kMaxResources = 10;
|
|
|
|
static constexpr int kMaxBytes = 100;
|
|
|
|
ctx->setResourceCacheLimits(kMaxResources, kMaxBytes);
|
|
|
|
sk_sp<GrTexture> textures[2 * kMaxResources];
|
|
|
|
for (int i = 0; i < 2 * kMaxResources; ++i) {
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
desc.fWidth = desc.fHeight = 100;
|
2019-04-09 22:41:27 +00:00
|
|
|
textures[i] = ctx->priv().resourceProvider()->createTexture(
|
2019-07-19 18:24:36 +00:00
|
|
|
desc, GrRenderable::kNo, SkBudgeted::kYes, GrProtected::kNo,
|
|
|
|
GrResourceProvider::Flags::kNoPendingIO);
|
2019-01-24 17:18:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, textures[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relying on the asserts in the promiseImageChecker to ensure that fulfills and releases are
|
|
|
|
// properly ordered.
|
|
|
|
canvas->drawImage(image, 0, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-24 17:18:33 +00:00
|
|
|
canvas->drawImage(image, 1, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-24 17:18:33 +00:00
|
|
|
canvas->drawImage(image, 2, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-24 17:18:33 +00:00
|
|
|
canvas->drawImage(image, 3, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-24 17:18:33 +00:00
|
|
|
canvas->drawImage(image, 4, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-01-24 17:18:33 +00:00
|
|
|
canvas->drawImage(image, 5, 0);
|
2019-03-04 16:00:10 +00:00
|
|
|
surface->flush();
|
2019-03-08 16:12:14 +00:00
|
|
|
// Must call these to ensure that all callbacks are performed before the checker is destroyed.
|
|
|
|
image.reset();
|
|
|
|
ctx->flush();
|
2019-05-17 14:01:21 +00:00
|
|
|
ctx->priv().getGpu()->testingOnly_flushGpuAndSync();
|
2019-03-08 16:12:14 +00:00
|
|
|
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex);
|
2019-01-24 17:18:33 +00:00
|
|
|
}
|
2019-04-04 16:18:17 +00:00
|
|
|
|
|
|
|
// Test case where promise image fulfill returns nullptr.
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageNullFulfill, reporter, ctxInfo) {
|
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 10;
|
|
|
|
|
|
|
|
GrContext* ctx = ctxInfo.grContext();
|
|
|
|
|
|
|
|
// Do all this just to get a valid backend format for the image.
|
2019-06-04 15:03:06 +00:00
|
|
|
GrBackendTexture backendTex = ctx->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
kWidth, kHeight, kRGBA_8888_SkColorType,
|
2019-07-01 19:04:06 +00:00
|
|
|
SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
|
2019-04-04 16:18:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, backendTex.isValid());
|
|
|
|
GrBackendFormat backendFormat = backendTex.getBackendFormat();
|
|
|
|
REPORTER_ASSERT(reporter, backendFormat.isValid());
|
2019-05-20 12:38:07 +00:00
|
|
|
ctx->deleteBackendTexture(backendTex);
|
2019-04-04 16:18:17 +00:00
|
|
|
|
|
|
|
struct Counts {
|
|
|
|
int fFulfillCount = 0;
|
|
|
|
int fReleaseCount = 0;
|
|
|
|
int fDoneCount = 0;
|
|
|
|
} counts;
|
|
|
|
auto fulfill = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) {
|
|
|
|
++static_cast<Counts*>(ctx)->fFulfillCount;
|
|
|
|
return sk_sp<SkPromiseImageTexture>();
|
|
|
|
};
|
|
|
|
auto release = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) {
|
|
|
|
++static_cast<Counts*>(ctx)->fReleaseCount;
|
|
|
|
};
|
|
|
|
auto done = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) {
|
|
|
|
++static_cast<Counts*>(ctx)->fDoneCount;
|
|
|
|
};
|
|
|
|
GrSurfaceOrigin texOrigin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
sk_sp<SkImage> refImg(SkImage_Gpu::MakePromiseTexture(
|
|
|
|
ctx, backendFormat, kWidth, kHeight, GrMipMapped::kNo, texOrigin,
|
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, fulfill, release, done, &counts,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew));
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
// Draw the image a few different ways.
|
|
|
|
canvas->drawImage(refImg, 0, 0);
|
|
|
|
SkPaint paint;
|
2019-04-08 20:23:20 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::LinearToSRGBGamma());
|
2019-04-04 16:18:17 +00:00
|
|
|
canvas->drawImage(refImg, 0, 0, &paint);
|
|
|
|
auto shader = refImg->makeShader(SkTileMode::kClamp, SkTileMode::kClamp);
|
|
|
|
REPORTER_ASSERT(reporter, shader);
|
|
|
|
paint.setShader(std::move(shader));
|
|
|
|
canvas->drawRect(SkRect::MakeWH(1,1), paint);
|
|
|
|
paint.setShader(nullptr);
|
|
|
|
refImg.reset();
|
|
|
|
surface->flush();
|
|
|
|
// We should only call each callback once and we should have made all the calls by this point.
|
|
|
|
REPORTER_ASSERT(reporter, counts.fFulfillCount == 1);
|
|
|
|
REPORTER_ASSERT(reporter, counts.fReleaseCount == 1);
|
|
|
|
REPORTER_ASSERT(reporter, counts.fDoneCount == 1);
|
|
|
|
}
|