blob: 0293731d7f0c42367d28d628768c410c77eba375 [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
Robert Phillipse19babf2020-04-06 13:57:30 -04008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrGpu.h"
12#include "src/gpu/GrProxyProvider.h"
13#include "src/gpu/GrResourceAllocator.h"
14#include "src/gpu/GrResourceProvider.h"
15#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000016#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrTextureProxy.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040018#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040019#include "tests/TestUtils.h"
Robert Phillips1734dd32018-08-21 13:52:09 -040020
Robert Phillips57aa3672017-07-21 11:38:13 -040021struct ProxyParams {
22 int fSize;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040023 GrRenderable fRenderable;
Robert Phillipsc80b0e92019-07-23 10:27:09 -040024 GrColorType fColorType;
Robert Phillips57aa3672017-07-21 11:38:13 -040025 SkBackingFit fFit;
26 int fSampleCnt;
Robert Phillipsc476e5d2019-03-26 14:50:08 -040027 SkBudgeted fBudgeted;
Robert Phillips57aa3672017-07-21 11:38:13 -040028 // TODO: do we care about mipmapping
29};
30
Robert Phillips3d4cac52019-06-11 08:08:08 -040031static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps,
32 const ProxyParams& p) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040033 const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Brian Salomondf1bd6d2020-03-26 20:37:01 -040034 return proxyProvider->createProxy(format, {p.fSize, p.fSize}, p.fRenderable, p.fSampleCnt,
35 GrMipMapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040036}
37
Robert Phillips3d4cac52019-06-11 08:08:08 -040038static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
39 GrBackendTexture* backendTex) {
Robert Phillips9da87e02019-02-04 13:26:26 -050040 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050041
Robert Phillipsc80b0e92019-07-23 10:27:09 -040042 SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType);
43 SkASSERT(SkColorType::kUnknown_SkColorType != skColorType);
44
Greg Danielc1ad77c2020-05-06 11:40:03 -040045 CreateBackendTexture(context, backendTex, p.fSize, p.fSize, skColorType,
46 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo);
47
Robert Phillips646f6372018-09-25 09:31:10 -040048 if (!backendTex->isValid()) {
49 return nullptr;
50 }
Robert Phillips57aa3672017-07-21 11:38:13 -040051
Brian Salomon8a78e9c2020-03-27 10:42:15 -040052 return proxyProvider->wrapBackendTexture(*backendTex, kBorrow_GrWrapOwnership,
Greg Daniel3a365112020-02-14 10:47:18 -050053 GrWrapCacheable::kNo, kRead_GrIOType);
Robert Phillips57aa3672017-07-21 11:38:13 -040054}
55
Brian Salomon26102cb2018-03-09 09:33:19 -050056static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040057 context->deleteBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040058}
59
Robert Phillips5af44de2017-07-18 14:49:38 -040060// Basic test that two proxies with overlapping intervals and compatible descriptors are
61// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040062static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -040063 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Brian Salomon2c791fc2019-04-02 11:52:03 -040064 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040065 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040066
Robert Phillips3d4cac52019-06-11 08:08:08 -040067 alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040068 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -040069 alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040070 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -040071 alloc.markEndOfOpsTask(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040072
Robert Phillipsc73666f2019-04-24 08:49:48 -040073 alloc.determineRecyclability();
74
Robert Phillipseafd48a2017-11-16 07:52:08 -050075 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050076 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -050077 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050078 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040079
Brian Salomonfd98c2c2018-07-31 17:25:29 -040080 REPORTER_ASSERT(reporter, p1->peekSurface());
81 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040082 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
83 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040084}
85
Robert Phillips57aa3672017-07-21 11:38:13 -040086// Test various cases when two proxies do not have overlapping intervals.
87// This mainly acts as a test of the ResourceAllocator's free pool.
88static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -040089 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Robert Phillips57aa3672017-07-21 11:38:13 -040090 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040091 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillipsc476e5d2019-03-26 14:50:08 -040092
93 alloc.incOps();
94 alloc.incOps();
95 alloc.incOps();
96 alloc.incOps();
97 alloc.incOps();
98 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -040099
Robert Phillips3d4cac52019-06-11 08:08:08 -0400100 alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
101 alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400102 alloc.markEndOfOpsTask(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400103
Robert Phillipsc73666f2019-04-24 08:49:48 -0400104 alloc.determineRecyclability();
105
Robert Phillipseafd48a2017-11-16 07:52:08 -0500106 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500107 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500108 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500109 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400110
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400111 REPORTER_ASSERT(reporter, p1->peekSurface());
112 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400113 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
114 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
115}
116
117DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500118 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
119 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
120 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400121
Robert Phillips57aa3672017-07-21 11:38:13 -0400122 struct TestCase {
123 ProxyParams fP1;
124 ProxyParams fP2;
125 bool fExpectation;
126 };
127
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400128 constexpr GrRenderable kRT = GrRenderable::kYes;
129 constexpr GrRenderable kNotRT = GrRenderable::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400130
131 constexpr bool kShare = true;
132 constexpr bool kDontShare = false;
133 // Non-RT GrSurfaces are never recycled on some platforms.
134 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
135
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400136 const GrColorType kRGBA = GrColorType::kRGBA_8888;
Greg Danield51fa2f2020-01-22 16:53:38 -0500137 const GrColorType kAlpha = GrColorType::kAlpha_8;
Robert Phillips57aa3672017-07-21 11:38:13 -0400138
139 const SkBackingFit kE = SkBackingFit::kExact;
140 const SkBackingFit kA = SkBackingFit::kApprox;
141
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400142 const SkBudgeted kNotB = SkBudgeted::kNo;
143
Robert Phillips57aa3672017-07-21 11:38:13 -0400144 //--------------------------------------------------------------------------------------------
145 TestCase gOverlappingTests[] = {
Greg Daniel3a365112020-02-14 10:47:18 -0500146 //----------------------------------------------------------------------------------------
147 // Two proxies with overlapping intervals and compatible descriptors should never share
148 // RT version
149 {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
150 // non-RT version
151 {{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare},
Robert Phillips57aa3672017-07-21 11:38:13 -0400152 };
153
154 for (auto test : gOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400155 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
156 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillipse5f73282019-06-18 17:15:04 -0400157 overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400158 }
159
Greg Daniel6fa62e22019-08-07 15:52:37 -0400160 auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
161 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(2, beFormat);
162 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(4, beFormat);
Robert Phillips57aa3672017-07-21 11:38:13 -0400163
164 //--------------------------------------------------------------------------------------------
165 TestCase gNonOverlappingTests[] = {
Greg Daniel3a365112020-02-14 10:47:18 -0500166 //----------------------------------------------------------------------------------------
167 // Two non-overlapping intervals w/ compatible proxies should share
168 // both same size & approx
169 {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare},
170 {{64, kNotRT, kRGBA, kA, 1, kNotB},
171 {64, kNotRT, kRGBA, kA, 1, kNotB},
172 kConditionallyShare},
173 // diffs sizes but still approx
174 {{64, kRT, kRGBA, kA, 1, kNotB}, {50, kRT, kRGBA, kA, 1, kNotB}, kShare},
175 {{64, kNotRT, kRGBA, kA, 1, kNotB},
176 {50, kNotRT, kRGBA, kA, 1, kNotB},
177 kConditionallyShare},
178 // sames sizes but exact
179 {{64, kRT, kRGBA, kE, 1, kNotB}, {64, kRT, kRGBA, kE, 1, kNotB}, kShare},
180 {{64, kNotRT, kRGBA, kE, 1, kNotB},
181 {64, kNotRT, kRGBA, kE, 1, kNotB},
182 kConditionallyShare},
183 //----------------------------------------------------------------------------------------
184 // Two non-overlapping intervals w/ different exact sizes should not share
185 {{56, kRT, kRGBA, kE, 1, kNotB}, {54, kRT, kRGBA, kE, 1, kNotB}, kDontShare},
186 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
187 {{255, kRT, kRGBA, kA, 1, kNotB}, {127, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
188 // Two non-overlapping intervals w/ different MSAA sample counts should not share
189 {{64, kRT, kRGBA, kA, k2, kNotB}, {64, kRT, kRGBA, kA, k4, kNotB}, k2 == k4},
190 // Two non-overlapping intervals w/ different configs should not share
191 {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kAlpha, kA, 1, kNotB}, kDontShare},
192 // Two non-overlapping intervals w/ different RT classifications should never share
193 {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kNotRT, kRGBA, kA, 1, kNotB}, kDontShare},
194 {{64, kNotRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kDontShare},
195 // Two non-overlapping intervals w/ different origins should share
196 {{64, kRT, kRGBA, kA, 1, kNotB}, {64, kRT, kRGBA, kA, 1, kNotB}, kShare},
Robert Phillips57aa3672017-07-21 11:38:13 -0400197 };
198
199 for (auto test : gNonOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400200 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
201 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400202
Robert Phillips57aa3672017-07-21 11:38:13 -0400203 if (!p1 || !p2) {
204 continue; // creation can fail (i.e., for msaa4 on iOS)
205 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400206
Robert Phillipse5f73282019-06-18 17:15:04 -0400207 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
208 test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400209 }
210
211 {
212 // Wrapped backend textures should never be reused
213 TestCase t[1] = {
Greg Daniel3a365112020-02-14 10:47:18 -0500214 {{64, kNotRT, kRGBA, kE, 1, kNotB}, {64, kNotRT, kRGBA, kE, 1, kNotB}, kDontShare}};
Robert Phillips57aa3672017-07-21 11:38:13 -0400215
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500216 GrBackendTexture backEndTex;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400217 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
218 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400219
Robert Phillipse5f73282019-06-18 17:15:04 -0400220 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
221 t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400222
Brian Salomon26102cb2018-03-09 09:33:19 -0500223 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400224 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400225}
Robert Phillips1734dd32018-08-21 13:52:09 -0400226
227static void draw(GrContext* context) {
228 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
229
230 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
231 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
232
233 SkCanvas* c = s->getCanvas();
234
235 c->clear(SK_ColorBLACK);
236}
237
238
239DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
240 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400241
Robert Phillipscf39f372019-09-03 10:29:20 -0400242 size_t maxBytes = context->getResourceCacheLimit();
Robert Phillips1734dd32018-08-21 13:52:09 -0400243
Robert Phillipscf39f372019-09-03 10:29:20 -0400244 context->setResourceCacheLimit(0); // We'll always be overbudget
Robert Phillips1734dd32018-08-21 13:52:09 -0400245
246 draw(context);
247 draw(context);
248 draw(context);
249 draw(context);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400250 context->flushAndSubmit();
Robert Phillips1734dd32018-08-21 13:52:09 -0400251
Robert Phillipscf39f372019-09-03 10:29:20 -0400252 context->setResourceCacheLimit(maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400253}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500254
255sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400256 const ProxyParams& p) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400257 const auto format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500258
Brian Salomon63410e92020-03-23 18:32:50 -0400259 auto callback = [](GrResourceProvider* resourceProvider,
260 const GrSurfaceProxy::LazySurfaceDesc& desc) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400261 sk_sp<GrTexture> texture;
Brian Salomon63410e92020-03-23 18:32:50 -0400262 if (desc.fFit == SkBackingFit::kApprox) {
263 texture = resourceProvider->createApproxTexture(desc.fDimensions, desc.fFormat,
264 desc.fRenderable, desc.fSampleCnt,
265 desc.fProtected);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500266 } else {
Brian Salomon63410e92020-03-23 18:32:50 -0400267 texture = resourceProvider->createTexture(
268 desc.fDimensions, desc.fFormat, desc.fRenderable, desc.fSampleCnt,
269 desc.fMipMapped, desc.fBudgeted, desc.fProtected);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500270 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400271 return GrSurfaceProxy::LazyCallbackResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500272 };
Brian Salomon577aa0f2018-11-30 13:32:23 -0500273 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Brian Salomon63410e92020-03-23 18:32:50 -0400274 SkISize dims = {p.fSize, p.fSize};
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400275 return proxyProvider->createLazyProxy(callback, format, dims, p.fRenderable, p.fSampleCnt,
276 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, flags,
277 p.fFit, p.fBudgeted, GrProtected::kNo,
278 GrSurfaceProxy::UseAllocator::kYes);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500279}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400280
Greg Danielf41b2bd2019-08-22 16:19:24 -0400281// Set up so there are two opsTasks that need to be flushed but the resource allocator thinks
282// it is over budget. The two opsTasks should be flushed separately and the opsTask indices
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400283// returned from assign should be correct.
284DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
285 GrContext* context = ctxInfo.grContext();
286 const GrCaps* caps = context->priv().caps();
287 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
288 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
289
Robert Phillipscf39f372019-09-03 10:29:20 -0400290 size_t origMaxBytes = context->getResourceCacheLimit();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400291
292 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400293 context->setResourceCacheLimit(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400294
Greg Daniel3a365112020-02-14 10:47:18 -0500295 const ProxyParams params = {
296 64, GrRenderable::kNo, GrColorType::kRGBA_8888, SkBackingFit::kExact,
297 1, SkBudgeted::kYes};
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400298
299 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400300 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params);
301 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params);
302 sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params);
303 sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400304
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 2));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400306
Robert Phillips3d4cac52019-06-11 08:08:08 -0400307 alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400308 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400309 alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400310 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400311 alloc.markEndOfOpsTask(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400312
Robert Phillips3d4cac52019-06-11 08:08:08 -0400313 alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400314 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400315 alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400316 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400317 alloc.markEndOfOpsTask(1);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400318
319 int startIndex, stopIndex;
320 GrResourceAllocator::AssignError error;
321
Robert Phillipsc73666f2019-04-24 08:49:48 -0400322 alloc.determineRecyclability();
323
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400324 alloc.assign(&startIndex, &stopIndex, &error);
325 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
326 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
327
328 alloc.assign(&startIndex, &stopIndex, &error);
329 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
330 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400331 }
332
Robert Phillipscf39f372019-09-03 10:29:20 -0400333 context->setResourceCacheLimit(origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400334}
Greg Danield72dd4d2019-08-29 14:37:46 -0400335
336// This test is used to make sure we are tracking the current task index during the assign call in
337// the GrResourceAllocator. Specifically we can fall behind if we have intervals that don't
338// use the allocator. In this case we need to possibly increment the fCurOpsTaskIndex multiple times
339// to get in back in sync. We had a bug where we'd only every increment the index by one,
340// http://crbug.com/996610.
341DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorCurOpsTaskIndexTest,
342 reporter, ctxInfo) {
343 GrContext* context = ctxInfo.grContext();
344 const GrCaps* caps = context->priv().caps();
345 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
346 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
347
Robert Phillipscf39f372019-09-03 10:29:20 -0400348 size_t origMaxBytes = context->getResourceCacheLimit();
Greg Danield72dd4d2019-08-29 14:37:46 -0400349
350 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400351 context->setResourceCacheLimit(0);
Greg Danield72dd4d2019-08-29 14:37:46 -0400352
353 ProxyParams params;
354 params.fFit = SkBackingFit::kExact;
Greg Danield72dd4d2019-08-29 14:37:46 -0400355 params.fColorType = GrColorType::kRGBA_8888;
356 params.fRenderable = GrRenderable::kYes;
357 params.fSampleCnt = 1;
358 params.fSize = 100;
359 params.fBudgeted = SkBudgeted::kYes;
360
361 sk_sp<GrSurfaceProxy> proxy1 = make_deferred(proxyProvider, caps, params);
362 if (!proxy1) {
363 return;
364 }
365 sk_sp<GrSurfaceProxy> proxy2 = make_deferred(proxyProvider, caps, params);
366 if (!proxy2) {
367 return;
368 }
369
370 // Wrapped proxy that will be ignored by the resourceAllocator. We use this to try and get the
371 // resource allocator fCurOpsTaskIndex to fall behind what it really should be.
372 GrBackendTexture backEndTex;
373 sk_sp<GrSurfaceProxy> proxyWrapped = make_backend(ctxInfo.grContext(), params,
374 &backEndTex);
375 if (!proxyWrapped) {
376 return;
377 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400378
379 // Same as above, but we actually need to have at least two intervals that don't go through the
380 // resource allocator to expose the index bug.
381 GrBackendTexture backEndTex2;
382 sk_sp<GrSurfaceProxy> proxyWrapped2 = make_backend(ctxInfo.grContext(), params,
383 &backEndTex2);
384 if (!proxyWrapped2) {
385 cleanup_backend(ctxInfo.grContext(), backEndTex);
386 return;
387 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400388
Brian Salomonbeb7f522019-08-30 16:19:42 -0400389 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 4));
Greg Danield72dd4d2019-08-29 14:37:46 -0400390
391 alloc.addInterval(proxyWrapped.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
392 alloc.incOps();
393 alloc.markEndOfOpsTask(0);
394
395 alloc.addInterval(proxyWrapped2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
396 alloc.incOps();
397 alloc.markEndOfOpsTask(1);
398
399 alloc.addInterval(proxy1.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
400 alloc.incOps();
401 alloc.markEndOfOpsTask(2);
402
403 // We want to force the resource allocator to do a intermediateFlush for the previous interval.
404 // But if it is the resource allocator is at the of its list of intervals it skips the
405 // intermediate flush call, so we add another interval here so it is not skipped.
406 alloc.addInterval(proxy2.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
407 alloc.incOps();
408 alloc.markEndOfOpsTask(3);
409
410 int startIndex, stopIndex;
411 GrResourceAllocator::AssignError error;
412
413 alloc.determineRecyclability();
414
415 alloc.assign(&startIndex, &stopIndex, &error);
416 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
417 // The original bug in the allocator here would return a stopIndex of 2 since it would have only
418 // incremented its fCurOpsTaskIndex once instead of the needed two times to skip the first two
419 // unused intervals.
420 REPORTER_ASSERT(reporter, 0 == startIndex && 3 == stopIndex);
421
422 alloc.assign(&startIndex, &stopIndex, &error);
423 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
424 REPORTER_ASSERT(reporter, 3 == startIndex && 4 == stopIndex);
425
426 cleanup_backend(ctxInfo.grContext(), backEndTex);
427 cleanup_backend(ctxInfo.grContext(), backEndTex2);
428
Robert Phillipscf39f372019-09-03 10:29:20 -0400429 context->setResourceCacheLimit(origMaxBytes);
Greg Danield72dd4d2019-08-29 14:37:46 -0400430}
431