2017-07-18 18:49:38 +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.
|
|
|
|
*/
|
|
|
|
|
2020-04-06 17:57:30 +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"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrGpu.h"
|
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
|
|
|
#include "src/gpu/GrResourceAllocator.h"
|
|
|
|
#include "src/gpu/GrResourceProvider.h"
|
|
|
|
#include "src/gpu/GrSurfaceProxyPriv.h"
|
2020-03-05 19:14:18 +00:00
|
|
|
#include "src/gpu/GrTexture.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrTextureProxy.h"
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "tests/Test.h"
|
2020-10-13 00:45:06 +00:00
|
|
|
#include "tools/gpu/ManagedBackendTexture.h"
|
2018-08-21 17:52:09 +00:00
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
struct ProxyParams {
|
|
|
|
int fSize;
|
2019-07-17 13:59:59 +00:00
|
|
|
GrRenderable fRenderable;
|
2019-07-23 14:27:09 +00:00
|
|
|
GrColorType fColorType;
|
2017-07-21 15:38:13 +00:00
|
|
|
SkBackingFit fFit;
|
|
|
|
int fSampleCnt;
|
2019-03-26 18:50:08 +00:00
|
|
|
SkBudgeted fBudgeted;
|
2017-07-21 15:38:13 +00:00
|
|
|
// TODO: do we care about mipmapping
|
|
|
|
};
|
|
|
|
|
2019-06-11 12:08:08 +00:00
|
|
|
static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps,
|
|
|
|
const ProxyParams& p) {
|
2019-07-30 16:49:10 +00:00
|
|
|
const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
|
2020-03-27 00:37:01 +00:00
|
|
|
return proxyProvider->createProxy(format, {p.fSize, p.fSize}, p.fRenderable, p.fSampleCnt,
|
2020-07-21 13:27:25 +00:00
|
|
|
GrMipmapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo);
|
2017-07-21 15:38:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
static sk_sp<GrSurfaceProxy> make_backend(GrDirectContext* dContext, const ProxyParams& p) {
|
2020-07-20 19:00:36 +00:00
|
|
|
GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
|
2018-01-16 13:06:32 +00:00
|
|
|
|
2019-07-23 14:27:09 +00:00
|
|
|
SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType);
|
|
|
|
SkASSERT(SkColorType::kUnknown_SkColorType != skColorType);
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
|
|
|
|
dContext, p.fSize, p.fSize, skColorType, GrMipmapped::kNo, GrRenderable::kNo);
|
2020-05-06 15:40:03 +00:00
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
if (!mbet) {
|
2018-09-25 13:31:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-12-12 18:01:25 +00:00
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
return proxyProvider->wrapBackendTexture(mbet->texture(),
|
|
|
|
kBorrow_GrWrapOwnership,
|
|
|
|
GrWrapCacheable::kNo,
|
|
|
|
kRead_GrIOType,
|
|
|
|
mbet->refCountedCallback());
|
2017-07-21 15:38:13 +00:00
|
|
|
}
|
2017-07-18 18:49:38 +00:00
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
// Basic test that two proxies with overlapping intervals and compatible descriptors are
|
|
|
|
// assigned different GrSurfaces.
|
|
|
|
static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
|
2019-06-11 12:08:08 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
|
2019-04-02 15:52:03 +00:00
|
|
|
bool expectedResult) {
|
2019-08-30 20:19:42 +00:00
|
|
|
GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
|
2017-07-18 18:49:38 +00:00
|
|
|
|
2021-02-09 17:41:10 +00:00
|
|
|
alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
|
2019-03-26 18:50:08 +00:00
|
|
|
alloc.incOps();
|
2021-02-09 17:41:10 +00:00
|
|
|
alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
|
2019-03-26 18:50:08 +00:00
|
|
|
alloc.incOps();
|
2017-07-18 18:49:38 +00:00
|
|
|
|
2018-01-29 15:34:25 +00:00
|
|
|
GrResourceAllocator::AssignError error;
|
2021-02-11 20:18:17 +00:00
|
|
|
alloc.assign(&error);
|
2018-01-29 15:34:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
|
2017-07-18 18:49:38 +00:00
|
|
|
|
2018-07-31 21:25:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, p1->peekSurface());
|
|
|
|
REPORTER_ASSERT(reporter, p2->peekSurface());
|
2017-07-21 15:38:13 +00:00
|
|
|
bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
|
|
|
|
REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test various cases when two proxies do not have overlapping intervals.
|
|
|
|
// This mainly acts as a test of the ResourceAllocator's free pool.
|
|
|
|
static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
|
2019-06-11 12:08:08 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
|
2017-07-21 15:38:13 +00:00
|
|
|
bool expectedResult) {
|
2019-08-30 20:19:42 +00:00
|
|
|
GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
|
2019-03-26 18:50:08 +00:00
|
|
|
|
|
|
|
alloc.incOps();
|
|
|
|
alloc.incOps();
|
|
|
|
alloc.incOps();
|
|
|
|
alloc.incOps();
|
|
|
|
alloc.incOps();
|
|
|
|
alloc.incOps();
|
2017-07-21 15:38:13 +00:00
|
|
|
|
2021-02-09 17:41:10 +00:00
|
|
|
alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
|
|
|
|
alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
|
2019-04-24 12:49:48 +00:00
|
|
|
|
2018-01-29 15:34:25 +00:00
|
|
|
GrResourceAllocator::AssignError error;
|
2021-02-11 20:18:17 +00:00
|
|
|
alloc.assign(&error);
|
2018-01-29 15:34:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
|
2017-07-21 15:38:13 +00:00
|
|
|
|
2018-07-31 21:25:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, p1->peekSurface());
|
|
|
|
REPORTER_ASSERT(reporter, p2->peekSurface());
|
2017-07-21 15:38:13 +00:00
|
|
|
bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
|
|
|
|
REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
|
2017-07-18 18:49:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto direct = ctxInfo.directContext();
|
|
|
|
const GrCaps* caps = direct->priv().caps();
|
|
|
|
GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
|
|
|
|
GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
|
2017-07-18 18:49:38 +00:00
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
struct TestCase {
|
|
|
|
ProxyParams fP1;
|
|
|
|
ProxyParams fP2;
|
|
|
|
bool fExpectation;
|
|
|
|
};
|
|
|
|
|
2019-07-17 13:59:59 +00:00
|
|
|
constexpr GrRenderable kRT = GrRenderable::kYes;
|
|
|
|
constexpr GrRenderable kNotRT = GrRenderable::kNo;
|
2017-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
constexpr bool kShare = true;
|
|
|
|
constexpr bool kDontShare = false;
|
|
|
|
// Non-RT GrSurfaces are never recycled on some platforms.
|
|
|
|
bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
|
|
|
|
|
2019-07-23 14:27:09 +00:00
|
|
|
const GrColorType kRGBA = GrColorType::kRGBA_8888;
|
2020-01-22 21:53:38 +00:00
|
|
|
const GrColorType kAlpha = GrColorType::kAlpha_8;
|
2017-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
const SkBackingFit kE = SkBackingFit::kExact;
|
|
|
|
const SkBackingFit kA = SkBackingFit::kApprox;
|
|
|
|
|
2019-03-26 18:50:08 +00:00
|
|
|
const SkBudgeted kNotB = SkBudgeted::kNo;
|
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
TestCase gOverlappingTests[] = {
|
2020-02-14 15:47:18 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
// Two proxies with overlapping intervals and compatible descriptors should never share
|
|
|
|
// RT version
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
|
|
|
|
// non-RT version
|
|
|
|
{{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare},
|
2017-07-21 15:38:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto test : gOverlappingTests) {
|
2019-06-11 12:08:08 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
|
|
|
|
sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
|
2019-06-18 21:15:04 +00:00
|
|
|
overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
|
2017-07-21 15:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:52:37 +00:00
|
|
|
auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
|
2020-07-06 14:56:46 +00:00
|
|
|
int k2 = direct->priv().caps()->getRenderTargetSampleCount(2, beFormat);
|
|
|
|
int k4 = direct->priv().caps()->getRenderTargetSampleCount(4, beFormat);
|
2017-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
TestCase gNonOverlappingTests[] = {
|
2020-02-14 15:47:18 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
// Two non-overlapping intervals w/ compatible proxies should share
|
|
|
|
// both same size & approx
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare},
|
|
|
|
{{64, kNotRT, kRGBA, kA, 1, kNotB},
|
|
|
|
{64, kNotRT, kRGBA, kA, 1, kNotB},
|
|
|
|
kConditionallyShare},
|
|
|
|
// diffs sizes but still approx
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {50, kRT, kRGBA, kA, 1, kNotB}, kShare},
|
|
|
|
{{64, kNotRT, kRGBA, kA, 1, kNotB},
|
|
|
|
{50, kNotRT, kRGBA, kA, 1, kNotB},
|
|
|
|
kConditionallyShare},
|
|
|
|
// sames sizes but exact
|
|
|
|
{{64, kRT, kRGBA, kE, 1, kNotB}, {64, kRT, kRGBA, kE, 1, kNotB}, kShare},
|
|
|
|
{{64, kNotRT, kRGBA, kE, 1, kNotB},
|
|
|
|
{64, kNotRT, kRGBA, kE, 1, kNotB},
|
|
|
|
kConditionallyShare},
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
// Two non-overlapping intervals w/ different exact sizes should not share
|
|
|
|
{{56, kRT, kRGBA, kE, 1, kNotB}, {54, kRT, kRGBA, kE, 1, kNotB}, kDontShare},
|
|
|
|
// Two non-overlapping intervals w/ _very different_ approx sizes should not share
|
|
|
|
{{255, kRT, kRGBA, kA, 1, kNotB}, {127, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
|
|
|
|
// Two non-overlapping intervals w/ different MSAA sample counts should not share
|
|
|
|
{{64, kRT, kRGBA, kA, k2, kNotB}, {64, kRT, kRGBA, kA, k4, kNotB}, k2 == k4},
|
|
|
|
// Two non-overlapping intervals w/ different configs should not share
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kAlpha, kA, 1, kNotB}, kDontShare},
|
|
|
|
// Two non-overlapping intervals w/ different RT classifications should never share
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare},
|
|
|
|
{{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
|
|
|
|
// Two non-overlapping intervals w/ different origins should share
|
|
|
|
{{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare},
|
2017-07-21 15:38:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto test : gNonOverlappingTests) {
|
2019-06-11 12:08:08 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
|
|
|
|
sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
|
2018-07-18 17:56:48 +00:00
|
|
|
|
2017-07-21 15:38:13 +00:00
|
|
|
if (!p1 || !p2) {
|
|
|
|
continue; // creation can fail (i.e., for msaa4 on iOS)
|
|
|
|
}
|
2018-07-18 17:56:48 +00:00
|
|
|
|
2019-06-18 21:15:04 +00:00
|
|
|
non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
|
|
|
|
test.fExpectation);
|
2017-07-21 15:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrapped backend textures should never be reused
|
|
|
|
TestCase t[1] = {
|
2020-02-14 15:47:18 +00:00
|
|
|
{{64, kNotRT, kRGBA, kE, 1, kNotB}, {64, kNotRT, kRGBA, kE, 1, kNotB}, kDontShare}};
|
2017-07-21 15:38:13 +00:00
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p1 = make_backend(direct, t[0].fP1);
|
2019-06-11 12:08:08 +00:00
|
|
|
sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
|
2018-07-18 17:56:48 +00:00
|
|
|
|
2019-06-18 21:15:04 +00:00
|
|
|
non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
|
|
|
|
t[0].fExpectation);
|
2017-07-21 15:38:13 +00:00
|
|
|
}
|
2017-07-18 18:49:38 +00:00
|
|
|
}
|
2018-08-21 17:52:09 +00:00
|
|
|
|
2020-07-20 19:00:36 +00:00
|
|
|
static void draw(GrRecordingContext* rContext) {
|
2018-08-21 17:52:09 +00:00
|
|
|
SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
2020-07-20 19:00:36 +00:00
|
|
|
sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes,
|
2018-08-21 17:52:09 +00:00
|
|
|
ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
|
|
|
|
|
|
|
|
SkCanvas* c = s->getCanvas();
|
|
|
|
|
|
|
|
c->clear(SK_ColorBLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
2018-08-21 17:52:09 +00:00
|
|
|
|
2019-09-03 14:29:20 +00:00
|
|
|
size_t maxBytes = context->getResourceCacheLimit();
|
2018-08-21 17:52:09 +00:00
|
|
|
|
2019-09-03 14:29:20 +00:00
|
|
|
context->setResourceCacheLimit(0); // We'll always be overbudget
|
2018-08-21 17:52:09 +00:00
|
|
|
|
|
|
|
draw(context);
|
|
|
|
draw(context);
|
|
|
|
draw(context);
|
|
|
|
draw(context);
|
2020-05-14 19:45:44 +00:00
|
|
|
context->flushAndSubmit();
|
2018-08-21 17:52:09 +00:00
|
|
|
|
2019-09-03 14:29:20 +00:00
|
|
|
context->setResourceCacheLimit(maxBytes);
|
2018-08-21 17:52:09 +00:00
|
|
|
}
|