blob: c23c85e588bc9866d6c8ae6db6b6820a94370062 [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"
13#include "include/private/GrTextureProxy.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrDeinstantiateProxyTracker.h"
16#include "src/gpu/GrGpu.h"
17#include "src/gpu/GrProxyProvider.h"
18#include "src/gpu/GrResourceAllocator.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/GrSurfaceProxyPriv.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;
26 bool fIsRT;
Robert Phillips646f6372018-09-25 09:31:10 -040027 SkColorType 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
Greg Daniel4065d452018-11-16 15:43:41 -050035static GrSurfaceProxy* make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps,
36 const ProxyParams& p) {
Robert Phillips646f6372018-09-25 09:31:10 -040037 GrColorType grCT = SkColorTypeToGrColorType(p.fColorType);
38 GrPixelConfig config = GrColorTypeToPixelConfig(grCT, GrSRGBEncoded::kNo);
39
Robert Phillips57aa3672017-07-21 11:38:13 -040040 GrSurfaceDesc desc;
41 desc.fFlags = p.fIsRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillips57aa3672017-07-21 11:38:13 -040042 desc.fWidth = p.fSize;
43 desc.fHeight = p.fSize;
Robert Phillips646f6372018-09-25 09:31:10 -040044 desc.fConfig = config;
Robert Phillips57aa3672017-07-21 11:38:13 -040045 desc.fSampleCnt = p.fSampleCnt;
46
Greg Daniel4065d452018-11-16 15:43:41 -050047 const GrBackendFormat format = caps->getBackendFormatFromColorType(p.fColorType);
48
Robert Phillipsc476e5d2019-03-26 14:50:08 -040049 auto tmp = proxyProvider->createProxy(format, desc, p.fOrigin, p.fFit, p.fBudgeted);
Robert Phillips715d08c2018-07-18 13:56:48 -040050 if (!tmp) {
51 return nullptr;
52 }
53 GrSurfaceProxy* ret = tmp.release();
54
55 // Add a read to keep the proxy around but unref it so its backing surfaces can be recycled
56 ret->addPendingRead();
57 ret->unref();
58 return ret;
Robert Phillips57aa3672017-07-21 11:38:13 -040059}
60
Robert Phillips715d08c2018-07-18 13:56:48 -040061static GrSurfaceProxy* make_backend(GrContext* context, const ProxyParams& p,
62 GrBackendTexture* backendTex) {
Robert Phillips9da87e02019-02-04 13:26:26 -050063 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050064
Robert Phillips4bdd36f2019-06-04 11:03:06 -040065 *backendTex = context->createBackendTexture(p.fSize, p.fSize, p.fColorType,
66 SkColors::kTransparent,
67 GrMipMapped::kNo, GrRenderable::kNo);
Robert Phillips646f6372018-09-25 09:31:10 -040068 if (!backendTex->isValid()) {
69 return nullptr;
70 }
Robert Phillips57aa3672017-07-21 11:38:13 -040071
Brian Salomonc67c31c2018-12-06 10:00:03 -050072 auto tmp = proxyProvider->wrapBackendTexture(*backendTex, p.fOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050073 GrWrapCacheable::kNo, kRead_GrIOType);
Robert Phillips715d08c2018-07-18 13:56:48 -040074 if (!tmp) {
75 return nullptr;
76 }
77 GrSurfaceProxy* ret = tmp.release();
78
79 // Add a read to keep the proxy around but unref it so its backing surfaces can be recycled
80 ret->addPendingRead();
81 ret->unref();
82 return ret;
Robert Phillips57aa3672017-07-21 11:38:13 -040083}
84
Brian Salomon26102cb2018-03-09 09:33:19 -050085static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040086 context->deleteBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040087}
88
Robert Phillips5af44de2017-07-18 14:49:38 -040089// Basic test that two proxies with overlapping intervals and compatible descriptors are
90// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040091static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Brian Salomon2c791fc2019-04-02 11:52:03 -040092 GrResourceCache* resourceCache, GrSurfaceProxy* p1, GrSurfaceProxy* p2,
93 bool expectedResult) {
94 GrDeinstantiateProxyTracker deinstantiateTracker(resourceCache);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040095 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040096
Robert Phillipsc73666f2019-04-24 08:49:48 -040097 alloc.addInterval(p1, 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040098 alloc.incOps();
Robert Phillipsc73666f2019-04-24 08:49:48 -040099 alloc.addInterval(p2, 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400100 alloc.incOps();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500101 alloc.markEndOfOpList(0);
Robert Phillips5af44de2017-07-18 14:49:38 -0400102
Robert Phillipsc73666f2019-04-24 08:49:48 -0400103 alloc.determineRecyclability();
104
Robert Phillipseafd48a2017-11-16 07:52:08 -0500105 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500106 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500107 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500108 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -0400109
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400110 REPORTER_ASSERT(reporter, p1->peekSurface());
111 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400112 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
113 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -0400114}
115
Robert Phillips57aa3672017-07-21 11:38:13 -0400116// Test various cases when two proxies do not have overlapping intervals.
117// This mainly acts as a test of the ResourceAllocator's free pool.
118static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Brian Salomon2c791fc2019-04-02 11:52:03 -0400119 GrResourceCache* resourceCache, GrSurfaceProxy* p1, GrSurfaceProxy* p2,
Robert Phillips57aa3672017-07-21 11:38:13 -0400120 bool expectedResult) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400121 GrDeinstantiateProxyTracker deinstantiateTracker(resourceCache);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400122 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
123
124 alloc.incOps();
125 alloc.incOps();
126 alloc.incOps();
127 alloc.incOps();
128 alloc.incOps();
129 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -0400130
Robert Phillipsc73666f2019-04-24 08:49:48 -0400131 alloc.addInterval(p1, 0, 2, GrResourceAllocator::ActualUse::kYes);
132 alloc.addInterval(p2, 3, 5, GrResourceAllocator::ActualUse::kYes);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500133 alloc.markEndOfOpList(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400134
Robert Phillipsc73666f2019-04-24 08:49:48 -0400135 alloc.determineRecyclability();
136
Robert Phillipseafd48a2017-11-16 07:52:08 -0500137 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500138 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500139 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500140 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400141
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400142 REPORTER_ASSERT(reporter, p1->peekSurface());
143 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400144 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
145 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
146}
147
148DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500149 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
150 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
151 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Brian Salomon2c791fc2019-04-02 11:52:03 -0400152 GrResourceCache* resourceCache = ctxInfo.grContext()->priv().getResourceCache();
Robert Phillips5af44de2017-07-18 14:49:38 -0400153
Robert Phillips57aa3672017-07-21 11:38:13 -0400154 struct TestCase {
155 ProxyParams fP1;
156 ProxyParams fP2;
157 bool fExpectation;
158 };
159
160 constexpr bool kRT = true;
161 constexpr bool kNotRT = false;
162
163 constexpr bool kShare = true;
164 constexpr bool kDontShare = false;
165 // Non-RT GrSurfaces are never recycled on some platforms.
166 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
167
Robert Phillips646f6372018-09-25 09:31:10 -0400168 const SkColorType kRGBA = kRGBA_8888_SkColorType;
169 const SkColorType kBGRA = kBGRA_8888_SkColorType;
Robert Phillips57aa3672017-07-21 11:38:13 -0400170
171 const SkBackingFit kE = SkBackingFit::kExact;
172 const SkBackingFit kA = SkBackingFit::kApprox;
173
174 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
175 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
176
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400177 const SkBudgeted kNotB = SkBudgeted::kNo;
178
Robert Phillips57aa3672017-07-21 11:38:13 -0400179 //--------------------------------------------------------------------------------------------
180 TestCase gOverlappingTests[] = {
181 //----------------------------------------------------------------------------------------
182 // Two proxies with overlapping intervals and compatible descriptors should never share
183 // RT version
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400184 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400185 // non-RT version
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400186 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400187 };
188
189 for (auto test : gOverlappingTests) {
Greg Daniel4065d452018-11-16 15:43:41 -0500190 GrSurfaceProxy* p1 = make_deferred(proxyProvider, caps, test.fP1);
191 GrSurfaceProxy* p2 = make_deferred(proxyProvider, caps, test.fP2);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400192 overlap_test(reporter, resourceProvider, resourceCache, p1, p2, test.fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400193 p1->completedRead();
194 p2->completedRead();
Robert Phillips57aa3672017-07-21 11:38:13 -0400195 }
196
Robert Phillips9da87e02019-02-04 13:26:26 -0500197 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(
Robert Phillips646f6372018-09-25 09:31:10 -0400198 2, kRGBA_8888_GrPixelConfig);
Robert Phillips9da87e02019-02-04 13:26:26 -0500199 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(
Robert Phillips646f6372018-09-25 09:31:10 -0400200 4, kRGBA_8888_GrPixelConfig);
Robert Phillips57aa3672017-07-21 11:38:13 -0400201
202 //--------------------------------------------------------------------------------------------
203 TestCase gNonOverlappingTests[] = {
204 //----------------------------------------------------------------------------------------
205 // Two non-overlapping intervals w/ compatible proxies should share
206 // both same size & approx
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400207 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kShare },
208 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400209 // diffs sizes but still approx
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400210 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 50, kRT, kRGBA, kA, 0, kTL, kNotB }, kShare },
211 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 50, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400212 // sames sizes but exact
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400213 { { 64, kRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kRT, kRGBA, kE, 0, kTL, kNotB }, kShare },
214 { { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400215 //----------------------------------------------------------------------------------------
216 // Two non-overlapping intervals w/ different exact sizes should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400217 { { 56, kRT, kRGBA, kE, 0, kTL, kNotB }, { 54, kRT, kRGBA, kE, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400218 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400219 { { 255, kRT, kRGBA, kA, 0, kTL, kNotB }, { 127, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400220 // Two non-overlapping intervals w/ different MSAA sample counts should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400221 { { 64, kRT, kRGBA, kA, k2, kTL, kNotB },{ 64, kRT, kRGBA, kA, k4,kTL, kNotB}, k2 == k4 },
Robert Phillips57aa3672017-07-21 11:38:13 -0400222 // Two non-overlapping intervals w/ different configs should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400223 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kBGRA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400224 // Two non-overlapping intervals w/ different RT classifications should never share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400225 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
226 { { 64, kNotRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400227 // Two non-overlapping intervals w/ different origins should share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400228 { { 64, kRT, kRGBA, kA, 0, kTL, kNotB }, { 64, kRT, kRGBA, kA, 0, kBL, kNotB }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400229 };
230
231 for (auto test : gNonOverlappingTests) {
Greg Daniel4065d452018-11-16 15:43:41 -0500232 GrSurfaceProxy* p1 = make_deferred(proxyProvider, caps, test.fP1);
233 GrSurfaceProxy* p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400234
Robert Phillips57aa3672017-07-21 11:38:13 -0400235 if (!p1 || !p2) {
236 continue; // creation can fail (i.e., for msaa4 on iOS)
237 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400238
Brian Salomon2c791fc2019-04-02 11:52:03 -0400239 non_overlap_test(reporter, resourceProvider, resourceCache, p1, p2, test.fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400240
241 p1->completedRead();
242 p2->completedRead();
Robert Phillips57aa3672017-07-21 11:38:13 -0400243 }
244
245 {
246 // Wrapped backend textures should never be reused
247 TestCase t[1] = {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400248 { { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 0, kTL, kNotB }, kDontShare }
Robert Phillips57aa3672017-07-21 11:38:13 -0400249 };
250
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500251 GrBackendTexture backEndTex;
Robert Phillips715d08c2018-07-18 13:56:48 -0400252 GrSurfaceProxy* p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
Greg Daniel4065d452018-11-16 15:43:41 -0500253 GrSurfaceProxy* p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400254
Brian Salomon2c791fc2019-04-02 11:52:03 -0400255 non_overlap_test(reporter, resourceProvider, resourceCache, p1, p2, t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400256
257 p1->completedRead();
258 p2->completedRead();
259
Brian Salomon26102cb2018-03-09 09:33:19 -0500260 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400261 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400262}
Robert Phillips1734dd32018-08-21 13:52:09 -0400263
264static void draw(GrContext* context) {
265 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
266
267 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
268 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
269
270 SkCanvas* c = s->getCanvas();
271
272 c->clear(SK_ColorBLACK);
273}
274
275
276DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
277 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400278
279 int maxNum;
280 size_t maxBytes;
281 context->getResourceCacheLimits(&maxNum, &maxBytes);
282
Robert Phillips1734dd32018-08-21 13:52:09 -0400283 context->setResourceCacheLimits(0, 0); // We'll always be overbudget
284
285 draw(context);
286 draw(context);
287 draw(context);
288 draw(context);
289 context->flush();
290
291 context->setResourceCacheLimits(maxNum, maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400292}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500293
294sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomon876a0172019-03-08 11:12:14 -0500295 const ProxyParams& p, bool deinstantiate) {
Brian Salomon577aa0f2018-11-30 13:32:23 -0500296 GrColorType grCT = SkColorTypeToGrColorType(p.fColorType);
297 GrPixelConfig config = GrColorTypeToPixelConfig(grCT, GrSRGBEncoded::kNo);
298
299 GrSurfaceDesc desc;
300 desc.fFlags = p.fIsRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
301 desc.fWidth = p.fSize;
302 desc.fHeight = p.fSize;
303 desc.fConfig = config;
304 desc.fSampleCnt = p.fSampleCnt;
305
306 SkBackingFit fit = p.fFit;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400307 auto callback = [fit, desc](GrResourceProvider* resourceProvider) {
308 sk_sp<GrTexture> texture;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500309 if (fit == SkBackingFit::kApprox) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400310 texture = resourceProvider->createApproxTexture(
311 desc, GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500312 } else {
Robert Phillips9313aa72019-04-09 18:41:27 -0400313 texture = resourceProvider->createTexture(desc, SkBudgeted::kNo,
314 GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500315 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400316 return GrSurfaceProxy::LazyInstantiationResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500317 };
318 const GrBackendFormat format = caps->getBackendFormatFromColorType(p.fColorType);
Brian Salomon876a0172019-03-08 11:12:14 -0500319 auto lazyType = deinstantiate ? GrSurfaceProxy::LazyInstantiationType ::kDeinstantiate
320 : GrSurfaceProxy::LazyInstantiationType ::kSingleUse;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500321 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500322 return proxyProvider->createLazyProxy(callback, format, desc, p.fOrigin, GrMipMapped::kNo,
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400323 flags, p.fFit, p.fBudgeted, lazyType);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500324}
325
326DEF_GPUTEST_FOR_RENDERING_CONTEXTS(LazyDeinstantiation, reporter, ctxInfo) {
327 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500328 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Brian Salomon2c791fc2019-04-02 11:52:03 -0400329 GrResourceCache* resourceCache = ctxInfo.grContext()->priv().getResourceCache();
Robert Phillips9313aa72019-04-09 18:41:27 -0400330 ProxyParams texParams;
331 texParams.fFit = SkBackingFit::kExact;
332 texParams.fOrigin = kTopLeft_GrSurfaceOrigin;
333 texParams.fColorType = kRGBA_8888_SkColorType;
334 texParams.fIsRT = false;
335 texParams.fSampleCnt = 1;
336 texParams.fSize = 100;
337 texParams.fBudgeted = SkBudgeted::kNo;
338 ProxyParams rtParams = texParams;
339 rtParams.fIsRT = true;
340 auto proxyProvider = context->priv().proxyProvider();
341 auto caps = context->priv().caps();
342 auto p0 = make_lazy(proxyProvider, caps, texParams, true);
343 auto p1 = make_lazy(proxyProvider, caps, texParams, false);
344 texParams.fFit = rtParams.fFit = SkBackingFit::kApprox;
345 auto p2 = make_lazy(proxyProvider, caps, rtParams, true);
346 auto p3 = make_lazy(proxyProvider, caps, rtParams, false);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500347
Robert Phillips9313aa72019-04-09 18:41:27 -0400348 GrDeinstantiateProxyTracker deinstantiateTracker(resourceCache);
349 {
350 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 1));
Robert Phillipsc73666f2019-04-24 08:49:48 -0400351 alloc.addInterval(p0.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
352 alloc.addInterval(p1.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
353 alloc.addInterval(p2.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
354 alloc.addInterval(p3.get(), 0, 1, GrResourceAllocator::ActualUse::kNo);
Robert Phillips9313aa72019-04-09 18:41:27 -0400355 alloc.incOps();
356 alloc.markEndOfOpList(0);
Robert Phillipsc73666f2019-04-24 08:49:48 -0400357
358 alloc.determineRecyclability();
359
Robert Phillips9313aa72019-04-09 18:41:27 -0400360 int startIndex, stopIndex;
361 GrResourceAllocator::AssignError error;
362 alloc.assign(&startIndex, &stopIndex, &error);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500363 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400364 deinstantiateTracker.deinstantiateAllProxies();
365 REPORTER_ASSERT(reporter, !p0->isInstantiated());
366 REPORTER_ASSERT(reporter, p1->isInstantiated());
367 REPORTER_ASSERT(reporter, !p2->isInstantiated());
368 REPORTER_ASSERT(reporter, p3->isInstantiated());
Brian Salomon577aa0f2018-11-30 13:32:23 -0500369}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400370
371// Set up so there are two opLists that need to be flushed but the resource allocator thinks
372// it is over budget. The two opLists should be flushed separately and the opList indices
373// returned from assign should be correct.
374DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
375 GrContext* context = ctxInfo.grContext();
376 const GrCaps* caps = context->priv().caps();
377 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
378 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Brian Salomon2c791fc2019-04-02 11:52:03 -0400379 GrResourceCache* resourceCache = context->priv().getResourceCache();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400380
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400381 int origMaxNum;
382 size_t origMaxBytes;
383 context->getResourceCacheLimits(&origMaxNum, &origMaxBytes);
384
385 // Force the resource allocator to always believe it is over budget
386 context->setResourceCacheLimits(0, 0);
387
388 const ProxyParams params = { 64, false, kRGBA_8888_SkColorType,
389 SkBackingFit::kExact, 0, kTopLeft_GrSurfaceOrigin,
390 SkBudgeted::kYes };
391
392 {
393 GrSurfaceProxy* p1 = make_deferred(proxyProvider, caps, params);
394 GrSurfaceProxy* p2 = make_deferred(proxyProvider, caps, params);
395 GrSurfaceProxy* p3 = make_deferred(proxyProvider, caps, params);
396 GrSurfaceProxy* p4 = make_deferred(proxyProvider, caps, params);
397
Brian Salomon2c791fc2019-04-02 11:52:03 -0400398 GrDeinstantiateProxyTracker deinstantiateTracker(resourceCache);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400399 GrResourceAllocator alloc(resourceProvider, &deinstantiateTracker SkDEBUGCODE(, 2));
400
Robert Phillipsc73666f2019-04-24 08:49:48 -0400401 alloc.addInterval(p1, 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400402 alloc.incOps();
Robert Phillipsc73666f2019-04-24 08:49:48 -0400403 alloc.addInterval(p2, 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400404 alloc.incOps();
405 alloc.markEndOfOpList(0);
406
Robert Phillipsc73666f2019-04-24 08:49:48 -0400407 alloc.addInterval(p3, 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400408 alloc.incOps();
Robert Phillipsc73666f2019-04-24 08:49:48 -0400409 alloc.addInterval(p4, 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400410 alloc.incOps();
411 alloc.markEndOfOpList(1);
412
413 int startIndex, stopIndex;
414 GrResourceAllocator::AssignError error;
415
Robert Phillipsc73666f2019-04-24 08:49:48 -0400416 alloc.determineRecyclability();
417
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400418 alloc.assign(&startIndex, &stopIndex, &error);
419 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
420 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
421
422 alloc.assign(&startIndex, &stopIndex, &error);
423 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
424 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
425
426 p1->completedRead();
427 p2->completedRead();
428 p3->completedRead();
429 p4->completedRead();
430 }
431
432 context->setResourceCacheLimits(origMaxNum, origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400433}