blob: 390abdc892f4f332de4994d88098cc439f86576e [file] [log] [blame]
Robert Phillips5af44de2017-07-18 14:49:38 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Robert Phillips5af44de2017-07-18 14:49:38 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrDeinstantiateProxyTracker.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrResourceAllocator.h"
18#include "src/gpu/GrResourceProvider.h"
19#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrTextureProxy.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040021
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Robert Phillips1734dd32018-08-21 13:52:09 -040023
Robert Phillips57aa3672017-07-21 11:38:13 -040024struct ProxyParams {
25 int fSize;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040026 GrRenderable fRenderable;
Robert Phillips97256382019-07-17 15:26:36 -040027 GrColorType fColorType;
Robert Phillips57aa3672017-07-21 11:38:13 -040028 SkBackingFit fFit;
29 int fSampleCnt;
30 GrSurfaceOrigin fOrigin;
Robert Phillipsc476e5d2019-03-26 14:50:08 -040031 SkBudgeted fBudgeted;
Robert Phillips57aa3672017-07-21 11:38:13 -040032 // TODO: do we care about mipmapping
33};
34
Robert Phillips3d4cac52019-06-11 08:08:08 -040035static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps,
36 const ProxyParams& p) {
Robert Phillips97256382019-07-17 15:26:36 -040037 GrPixelConfig config = GrColorTypeToPixelConfig(p.fColorType);
Robert Phillips646f6372018-09-25 09:31:10 -040038
Robert Phillips57aa3672017-07-21 11:38:13 -040039 GrSurfaceDesc desc;
Robert Phillips57aa3672017-07-21 11:38:13 -040040 desc.fWidth = p.fSize;
41 desc.fHeight = p.fSize;
Robert Phillips646f6372018-09-25 09:31:10 -040042 desc.fConfig = config;
Robert Phillips57aa3672017-07-21 11:38:13 -040043 desc.fSampleCnt = p.fSampleCnt;
44
Robert Phillips97256382019-07-17 15:26:36 -040045 const GrBackendFormat format = caps->getBackendFormatFromColorType(p.fColorType);
Greg Daniel4065d452018-11-16 15:43:41 -050046
Brian Salomonf2c2ba92019-07-17 09:59:59 -040047 return proxyProvider->createProxy(format, desc, p.fRenderable, p.fOrigin, p.fFit, p.fBudgeted);
Robert Phillips57aa3672017-07-21 11:38:13 -040048}
49
Robert Phillips3d4cac52019-06-11 08:08:08 -040050static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
51 GrBackendTexture* backendTex) {
Robert Phillips9da87e02019-02-04 13:26:26 -050052 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050053
Robert Phillips97256382019-07-17 15:26:36 -040054 SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType);
55 SkASSERT(SkColorType::kUnknown_SkColorType != skColorType);
56
57 *backendTex = context->createBackendTexture(p.fSize, p.fSize, skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -040058 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040059 GrMipMapped::kNo, GrRenderable::kNo,
60 GrProtected::kNo);
Robert Phillips646f6372018-09-25 09:31:10 -040061 if (!backendTex->isValid()) {
62 return nullptr;
63 }
Robert Phillips57aa3672017-07-21 11:38:13 -040064
Robert Phillips97256382019-07-17 15:26:36 -040065 return proxyProvider->wrapBackendTexture(*backendTex, p.fColorType, p.fOrigin,
66 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
67 kRead_GrIOType);
Robert Phillips57aa3672017-07-21 11:38:13 -040068}
69
Brian Salomon26102cb2018-03-09 09:33:19 -050070static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040071 context->deleteBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040072}
73
Robert Phillips5af44de2017-07-18 14:49:38 -040074// Basic test that two proxies with overlapping intervals and compatible descriptors are
75// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040076static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -040077 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Brian Salomon2c791fc2019-04-02 11:52:03 -040078 bool expectedResult) {
Robert Phillipse5f73282019-06-18 17:15:04 -040079 GrDeinstantiateProxyTracker deinstantiateTracker;
Robert Phillipsc476e5d2019-03-26 14:50:08 -040080 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040081
Robert Phillips3d4cac52019-06-11 08:08:08 -040082 alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040083 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -040084 alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040085 alloc.incOps();
Robert Phillipseafd48a2017-11-16 07:52:08 -050086 alloc.markEndOfOpList(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040087
Robert Phillipsc73666f2019-04-24 08:49:48 -040088 alloc.determineRecyclability();
89
Robert Phillipseafd48a2017-11-16 07:52:08 -050090 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050091 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -050092 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050093 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040094
Brian Salomonfd98c2c2018-07-31 17:25:29 -040095 REPORTER_ASSERT(reporter, p1->peekSurface());
96 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040097 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
98 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040099}
100
Robert Phillips57aa3672017-07-21 11:38:13 -0400101// Test various cases when two proxies do not have overlapping intervals.
102// This mainly acts as a test of the ResourceAllocator's free pool.
103static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -0400104 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Robert Phillips57aa3672017-07-21 11:38:13 -0400105 bool expectedResult) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400106 GrDeinstantiateProxyTracker deinstantiateTracker;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400107 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
108
109 alloc.incOps();
110 alloc.incOps();
111 alloc.incOps();
112 alloc.incOps();
113 alloc.incOps();
114 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -0400115
Robert Phillips3d4cac52019-06-11 08:08:08 -0400116 alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
117 alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500118 alloc.markEndOfOpList(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400119
Robert Phillipsc73666f2019-04-24 08:49:48 -0400120 alloc.determineRecyclability();
121
Robert Phillipseafd48a2017-11-16 07:52:08 -0500122 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500123 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500124 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500125 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400126
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400127 REPORTER_ASSERT(reporter, p1->peekSurface());
128 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400129 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
130 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
131}
132
133DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500134 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
135 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
136 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400137
Robert Phillips57aa3672017-07-21 11:38:13 -0400138 struct TestCase {
139 ProxyParams fP1;
140 ProxyParams fP2;
141 bool fExpectation;
142 };
143
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400144 constexpr GrRenderable kRT = GrRenderable::kYes;
145 constexpr GrRenderable kNotRT = GrRenderable::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400146
147 constexpr bool kShare = true;
148 constexpr bool kDontShare = false;
149 // Non-RT GrSurfaces are never recycled on some platforms.
150 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
151
Robert Phillips97256382019-07-17 15:26:36 -0400152 const GrColorType kRGBA = GrColorType::kRGBA_8888;
153 const GrColorType kBGRA = GrColorType::kBGRA_8888;
Robert Phillips57aa3672017-07-21 11:38:13 -0400154
155 const SkBackingFit kE = SkBackingFit::kExact;
156 const SkBackingFit kA = SkBackingFit::kApprox;
157
158 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
159 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
160
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400161 const SkBudgeted kNotB = SkBudgeted::kNo;
162
Robert Phillips57aa3672017-07-21 11:38:13 -0400163 //--------------------------------------------------------------------------------------------
164 TestCase gOverlappingTests[] = {
165 //----------------------------------------------------------------------------------------
166 // Two proxies with overlapping intervals and compatible descriptors should never share
167 // RT version
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400168 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400169 // non-RT version
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400170 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400171 };
172
173 for (auto test : gOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400174 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
175 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillipse5f73282019-06-18 17:15:04 -0400176 overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400177 }
178
Robert Phillips9da87e02019-02-04 13:26:26 -0500179 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(
Robert Phillips646f6372018-09-25 09:31:10 -0400180 2, kRGBA_8888_GrPixelConfig);
Robert Phillips9da87e02019-02-04 13:26:26 -0500181 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(
Robert Phillips646f6372018-09-25 09:31:10 -0400182 4, kRGBA_8888_GrPixelConfig);
Robert Phillips57aa3672017-07-21 11:38:13 -0400183
184 //--------------------------------------------------------------------------------------------
185 TestCase gNonOverlappingTests[] = {
186 //----------------------------------------------------------------------------------------
187 // Two non-overlapping intervals w/ compatible proxies should share
188 // both same size & approx
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400189 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kShare },
190 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400191 // diffs sizes but still approx
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400192 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 50, kRT, kRGBA, kA, 0, kTL, kNotB }, kShare },
193 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 50, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400194 // sames sizes but exact
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400195 { { 64, kRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kRT, kRGBA, kE, 0, kTL, kNotB }, kShare },
196 { { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400197 //----------------------------------------------------------------------------------------
198 // Two non-overlapping intervals w/ different exact sizes should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400199 { { 56, kRT, kRGBA, kE, 0, kTL, kNotB }, { 54, kRT, kRGBA, kE, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400200 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400201 { { 255, kRT, kRGBA, kA, 0, kTL, kNotB }, { 127, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400202 // Two non-overlapping intervals w/ different MSAA sample counts should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400203 { { 64, kRT, kRGBA, kA, k2, kTL, kNotB },{ 64, kRT, kRGBA, kA, k4,kTL, kNotB}, k2 == k4 },
Robert Phillips57aa3672017-07-21 11:38:13 -0400204 // Two non-overlapping intervals w/ different configs should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400205 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kBGRA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400206 // Two non-overlapping intervals w/ different RT classifications should never share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400207 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
208 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400209 // Two non-overlapping intervals w/ different origins should share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400210 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kBL, kNotB }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400211 };
212
213 for (auto test : gNonOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400214 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
215 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400216
Robert Phillips57aa3672017-07-21 11:38:13 -0400217 if (!p1 || !p2) {
218 continue; // creation can fail (i.e., for msaa4 on iOS)
219 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400220
Robert Phillipse5f73282019-06-18 17:15:04 -0400221 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
222 test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400223 }
224
225 {
226 // Wrapped backend textures should never be reused
227 TestCase t[1] = {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400228 { { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, kDontShare }
Robert Phillips57aa3672017-07-21 11:38:13 -0400229 };
230
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500231 GrBackendTexture backEndTex;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400232 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
233 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400234
Robert Phillipse5f73282019-06-18 17:15:04 -0400235 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
236 t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400237
Brian Salomon26102cb2018-03-09 09:33:19 -0500238 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400239 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400240}
Robert Phillips1734dd32018-08-21 13:52:09 -0400241
242static void draw(GrContext* context) {
243 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
244
245 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
246 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
247
248 SkCanvas* c = s->getCanvas();
249
250 c->clear(SK_ColorBLACK);
251}
252
253
254DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
255 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400256
257 int maxNum;
258 size_t maxBytes;
259 context->getResourceCacheLimits(&maxNum, &maxBytes);
260
Robert Phillips1734dd32018-08-21 13:52:09 -0400261 context->setResourceCacheLimits(0, 0); // We'll always be overbudget
262
263 draw(context);
264 draw(context);
265 draw(context);
266 draw(context);
267 context->flush();
268
269 context->setResourceCacheLimits(maxNum, maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400270}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500271
272sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomon876a0172019-03-08 11:12:14 -0500273 const ProxyParams& p, bool deinstantiate) {
Robert Phillips97256382019-07-17 15:26:36 -0400274 GrPixelConfig config = GrColorTypeToPixelConfig(p.fColorType);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500275
276 GrSurfaceDesc desc;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500277 desc.fWidth = p.fSize;
278 desc.fHeight = p.fSize;
279 desc.fConfig = config;
280 desc.fSampleCnt = p.fSampleCnt;
281
282 SkBackingFit fit = p.fFit;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400283 auto callback = [fit, desc, renderable = p.fRenderable](GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400284 sk_sp<GrTexture> texture;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500285 if (fit == SkBackingFit::kApprox) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400286 texture = resourceProvider->createApproxTexture(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400287 desc, renderable, GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500288 } else {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400289 texture = resourceProvider->createTexture(desc, renderable, SkBudgeted::kNo,
Robert Phillips9313aa72019-04-09 18:41:27 -0400290 GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500291 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400292 return GrSurfaceProxy::LazyInstantiationResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500293 };
Robert Phillips97256382019-07-17 15:26:36 -0400294 const GrBackendFormat format = caps->getBackendFormatFromColorType(p.fColorType);
Brian Salomon876a0172019-03-08 11:12:14 -0500295 auto lazyType = deinstantiate ? GrSurfaceProxy::LazyInstantiationType ::kDeinstantiate
296 : GrSurfaceProxy::LazyInstantiationType ::kSingleUse;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500297 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400298 return proxyProvider->createLazyProxy(callback, format, desc, p.fRenderable, p.fOrigin,
299 GrMipMapped::kNo, flags, p.fFit, p.fBudgeted, lazyType);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500300}
301
302DEF_GPUTEST_FOR_RENDERING_CONTEXTS(LazyDeinstantiation, reporter, ctxInfo) {
303 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500304 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips9313aa72019-04-09 18:41:27 -0400305 ProxyParams texParams;
306 texParams.fFit = SkBackingFit::kExact;
307 texParams.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips97256382019-07-17 15:26:36 -0400308 texParams.fColorType = GrColorType::kRGBA_8888;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400309 texParams.fRenderable = GrRenderable::kNo;
Robert Phillips9313aa72019-04-09 18:41:27 -0400310 texParams.fSampleCnt = 1;
311 texParams.fSize = 100;
312 texParams.fBudgeted = SkBudgeted::kNo;
Robert Phillips9313aa72019-04-09 18:41:27 -0400313 auto proxyProvider = context->priv().proxyProvider();
314 auto caps = context->priv().caps();
315 auto p0 = make_lazy(proxyProvider, caps, texParams, true);
316 auto p1 = make_lazy(proxyProvider, caps, texParams, false);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400317 ProxyParams rtParams = texParams;
318 rtParams.fRenderable = GrRenderable::kYes;
319 rtParams.fFit = SkBackingFit::kApprox;
Robert Phillips9313aa72019-04-09 18:41:27 -0400320 auto p2 = make_lazy(proxyProvider, caps, rtParams, true);
321 auto p3 = make_lazy(proxyProvider, caps, rtParams, false);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500322
Robert Phillipse5f73282019-06-18 17:15:04 -0400323 GrDeinstantiateProxyTracker deinstantiateTracker;
Robert Phillips9313aa72019-04-09 18:41:27 -0400324 {
325 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
Robert Phillipsc73666f2019-04-24 08:49:48 -0400326 alloc.addInterval(p0.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
327 alloc.addInterval(p1.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
328 alloc.addInterval(p2.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
329 alloc.addInterval(p3.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
Robert Phillips9313aa72019-04-09 18:41:27 -0400330 alloc.incOps();
331 alloc.markEndOfOpList(0);
Robert Phillipsc73666f2019-04-24 08:49:48 -0400332
333 alloc.determineRecyclability();
334
Robert Phillips9313aa72019-04-09 18:41:27 -0400335 int startIndex, stopIndex;
336 GrResourceAllocator::AssignError error;
337 alloc.assign(&startIndex, &stopIndex, &error);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500338 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400339 deinstantiateTracker.deinstantiateAllProxies();
340 REPORTER_ASSERT(reporter, !p0->isInstantiated());
341 REPORTER_ASSERT(reporter, p1->isInstantiated());
342 REPORTER_ASSERT(reporter, !p2->isInstantiated());
343 REPORTER_ASSERT(reporter, p3->isInstantiated());
Brian Salomon577aa0f2018-11-30 13:32:23 -0500344}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400345
346// Set up so there are two opLists that need to be flushed but the resource allocator thinks
347// it is over budget. The two opLists should be flushed separately and the opList indices
348// returned from assign should be correct.
349DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
350 GrContext* context = ctxInfo.grContext();
351 const GrCaps* caps = context->priv().caps();
352 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
353 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
354
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400355 int origMaxNum;
356 size_t origMaxBytes;
357 context->getResourceCacheLimits(&origMaxNum, &origMaxBytes);
358
359 // Force the resource allocator to always believe it is over budget
360 context->setResourceCacheLimits(0, 0);
361
Robert Phillips97256382019-07-17 15:26:36 -0400362 const ProxyParams params = { 64, GrRenderable::kNo, GrColorType::kRGBA_8888,
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400363 SkBackingFit::kExact, 0, kTopLeft_GrSurfaceOrigin,
364 SkBudgeted::kYes };
365
366 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400367 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params);
368 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params);
369 sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params);
370 sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400371
Robert Phillipse5f73282019-06-18 17:15:04 -0400372 GrDeinstantiateProxyTracker deinstantiateTracker;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400373 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 2));
374
Robert Phillips3d4cac52019-06-11 08:08:08 -0400375 alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400376 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400377 alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400378 alloc.incOps();
379 alloc.markEndOfOpList(0);
380
Robert Phillips3d4cac52019-06-11 08:08:08 -0400381 alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400382 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400383 alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400384 alloc.incOps();
385 alloc.markEndOfOpList(1);
386
387 int startIndex, stopIndex;
388 GrResourceAllocator::AssignError error;
389
Robert Phillipsc73666f2019-04-24 08:49:48 -0400390 alloc.determineRecyclability();
391
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400392 alloc.assign(&startIndex, &stopIndex, &error);
393 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
394 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
395
396 alloc.assign(&startIndex, &stopIndex, &error);
397 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
398 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400399 }
400
401 context->setResourceCacheLimits(origMaxNum, origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400402}