blob: 95d603f66161ddf2dfbd73243c21ce3b0a693f3b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrResourceAllocator.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrTextureProxy.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040020
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkSurface.h"
Robert Phillips1734dd32018-08-21 13:52:09 -040022
Robert Phillips57aa3672017-07-21 11:38:13 -040023struct ProxyParams {
24 int fSize;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040025 GrRenderable fRenderable;
Robert Phillipsc80b0e92019-07-23 10:27:09 -040026 GrColorType fColorType;
Robert Phillips57aa3672017-07-21 11:38:13 -040027 SkBackingFit fFit;
28 int fSampleCnt;
29 GrSurfaceOrigin fOrigin;
Robert Phillipsc476e5d2019-03-26 14:50:08 -040030 SkBudgeted fBudgeted;
Robert Phillips57aa3672017-07-21 11:38:13 -040031 // TODO: do we care about mipmapping
32};
33
Robert Phillips3d4cac52019-06-11 08:08:08 -040034static sk_sp<GrSurfaceProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps,
35 const ProxyParams& p) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -040036 GrPixelConfig config = GrColorTypeToPixelConfig(p.fColorType);
Robert Phillips646f6372018-09-25 09:31:10 -040037
Robert Phillips57aa3672017-07-21 11:38:13 -040038 GrSurfaceDesc desc;
Robert Phillips57aa3672017-07-21 11:38:13 -040039 desc.fWidth = p.fSize;
40 desc.fHeight = p.fSize;
Robert Phillips646f6372018-09-25 09:31:10 -040041 desc.fConfig = config;
Robert Phillips57aa3672017-07-21 11:38:13 -040042
Robert Phillips0a15cc62019-07-30 12:49:10 -040043 const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Greg Daniel47c20e82020-01-21 14:29:57 -050044 GrSwizzle swizzle = caps->getReadSwizzle(format, p.fColorType);
Greg Daniel4065d452018-11-16 15:43:41 -050045
Greg Daniel47c20e82020-01-21 14:29:57 -050046 return proxyProvider->createProxy(format, desc, swizzle, p.fRenderable, p.fSampleCnt, p.fOrigin,
Brian Salomonbeb7f522019-08-30 16:19:42 -040047 GrMipMapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo);
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 Phillipsc80b0e92019-07-23 10:27:09 -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 Phillipsc80b0e92019-07-23 10:27:09 -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) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040079 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040080
Robert Phillips3d4cac52019-06-11 08:08:08 -040081 alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040082 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -040083 alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040084 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -040085 alloc.markEndOfOpsTask(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040086
Robert Phillipsc73666f2019-04-24 08:49:48 -040087 alloc.determineRecyclability();
88
Robert Phillipseafd48a2017-11-16 07:52:08 -050089 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050090 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -050091 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050092 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040093
Brian Salomonfd98c2c2018-07-31 17:25:29 -040094 REPORTER_ASSERT(reporter, p1->peekSurface());
95 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040096 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
97 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040098}
99
Robert Phillips57aa3672017-07-21 11:38:13 -0400100// Test various cases when two proxies do not have overlapping intervals.
101// This mainly acts as a test of the ResourceAllocator's free pool.
102static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -0400103 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Robert Phillips57aa3672017-07-21 11:38:13 -0400104 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400105 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400106
107 alloc.incOps();
108 alloc.incOps();
109 alloc.incOps();
110 alloc.incOps();
111 alloc.incOps();
112 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -0400113
Robert Phillips3d4cac52019-06-11 08:08:08 -0400114 alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
115 alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116 alloc.markEndOfOpsTask(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400117
Robert Phillipsc73666f2019-04-24 08:49:48 -0400118 alloc.determineRecyclability();
119
Robert Phillipseafd48a2017-11-16 07:52:08 -0500120 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500121 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500122 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500123 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400124
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400125 REPORTER_ASSERT(reporter, p1->peekSurface());
126 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400127 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
128 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
129}
130
131DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500132 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
133 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
134 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400135
Robert Phillips57aa3672017-07-21 11:38:13 -0400136 struct TestCase {
137 ProxyParams fP1;
138 ProxyParams fP2;
139 bool fExpectation;
140 };
141
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400142 constexpr GrRenderable kRT = GrRenderable::kYes;
143 constexpr GrRenderable kNotRT = GrRenderable::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400144
145 constexpr bool kShare = true;
146 constexpr bool kDontShare = false;
147 // Non-RT GrSurfaces are never recycled on some platforms.
148 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
149
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400150 const GrColorType kRGBA = GrColorType::kRGBA_8888;
151 const GrColorType kBGRA = GrColorType::kBGRA_8888;
Robert Phillips57aa3672017-07-21 11:38:13 -0400152
153 const SkBackingFit kE = SkBackingFit::kExact;
154 const SkBackingFit kA = SkBackingFit::kApprox;
155
156 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
157 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
158
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400159 const SkBudgeted kNotB = SkBudgeted::kNo;
160
Robert Phillips57aa3672017-07-21 11:38:13 -0400161 //--------------------------------------------------------------------------------------------
162 TestCase gOverlappingTests[] = {
163 //----------------------------------------------------------------------------------------
164 // Two proxies with overlapping intervals and compatible descriptors should never share
165 // RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400166 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400167 // non-RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400168 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400169 };
170
171 for (auto test : gOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400172 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
173 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillipse5f73282019-06-18 17:15:04 -0400174 overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400175 }
176
Greg Daniel6fa62e22019-08-07 15:52:37 -0400177 auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
178 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(2, beFormat);
179 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(4, beFormat);
Robert Phillips57aa3672017-07-21 11:38:13 -0400180
181 //--------------------------------------------------------------------------------------------
182 TestCase gNonOverlappingTests[] = {
183 //----------------------------------------------------------------------------------------
184 // Two non-overlapping intervals w/ compatible proxies should share
185 // both same size & approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400186 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
187 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400188 // diffs sizes but still approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400189 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
190 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400191 // sames sizes but exact
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400192 { { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, kShare },
193 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400194 //----------------------------------------------------------------------------------------
195 // Two non-overlapping intervals w/ different exact sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400196 { { 56, kRT, kRGBA, kE, 1, kTL, kNotB }, { 54, kRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400197 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400198 { { 255, kRT, kRGBA, kA, 1, kTL, kNotB }, { 127, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400199 // Two non-overlapping intervals w/ different MSAA sample counts should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400200 { { 64, kRT, kRGBA, kA, k2, kTL, kNotB },{ 64, kRT, kRGBA, kA, k4,kTL, kNotB}, k2 == k4 },
Robert Phillips57aa3672017-07-21 11:38:13 -0400201 // Two non-overlapping intervals w/ different configs should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400202 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kBGRA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400203 // Two non-overlapping intervals w/ different RT classifications should never share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400204 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
205 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400206 // Two non-overlapping intervals w/ different origins should share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400207 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kBL, kNotB }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400208 };
209
210 for (auto test : gNonOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400211 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
212 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400213
Robert Phillips57aa3672017-07-21 11:38:13 -0400214 if (!p1 || !p2) {
215 continue; // creation can fail (i.e., for msaa4 on iOS)
216 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400217
Robert Phillipse5f73282019-06-18 17:15:04 -0400218 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
219 test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400220 }
221
222 {
223 // Wrapped backend textures should never be reused
224 TestCase t[1] = {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400225 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare }
Robert Phillips57aa3672017-07-21 11:38:13 -0400226 };
227
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 GrBackendTexture backEndTex;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400229 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
230 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400231
Robert Phillipse5f73282019-06-18 17:15:04 -0400232 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
233 t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400234
Brian Salomon26102cb2018-03-09 09:33:19 -0500235 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400236 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400237}
Robert Phillips1734dd32018-08-21 13:52:09 -0400238
239static void draw(GrContext* context) {
240 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
241
242 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
243 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
244
245 SkCanvas* c = s->getCanvas();
246
247 c->clear(SK_ColorBLACK);
248}
249
250
251DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
252 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400253
Robert Phillipscf39f372019-09-03 10:29:20 -0400254 size_t maxBytes = context->getResourceCacheLimit();
Robert Phillips1734dd32018-08-21 13:52:09 -0400255
Robert Phillipscf39f372019-09-03 10:29:20 -0400256 context->setResourceCacheLimit(0); // We'll always be overbudget
Robert Phillips1734dd32018-08-21 13:52:09 -0400257
258 draw(context);
259 draw(context);
260 draw(context);
261 draw(context);
262 context->flush();
263
Robert Phillipscf39f372019-09-03 10:29:20 -0400264 context->setResourceCacheLimit(maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400265}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500266
267sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400268 const ProxyParams& p) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400269 GrPixelConfig config = GrColorTypeToPixelConfig(p.fColorType);
Brian Salomon4eb38b72019-08-05 12:58:39 -0400270 const auto format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500271
272 GrSurfaceDesc desc;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500273 desc.fWidth = p.fSize;
274 desc.fHeight = p.fSize;
275 desc.fConfig = config;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500276
277 SkBackingFit fit = p.fFit;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400278 auto callback = [fit, desc, format, p](GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400279 sk_sp<GrTexture> texture;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500280 if (fit == SkBackingFit::kApprox) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400281 texture = resourceProvider->createApproxTexture(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400282 desc, format, p.fRenderable, p.fSampleCnt, GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500283 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400284 texture = resourceProvider->createTexture(desc, format, p.fRenderable, p.fSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400285 GrMipMapped::kNo, SkBudgeted::kNo,
286 GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500287 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400288 return GrSurfaceProxy::LazyCallbackResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500289 };
Brian Salomon577aa0f2018-11-30 13:32:23 -0500290 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600291 return proxyProvider->createLazyProxy(
292 callback, format, desc, p.fRenderable, p.fSampleCnt, p.fOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400293 GrMipMapsStatus::kNotAllocated, flags, p.fFit, p.fBudgeted, GrProtected::kNo,
294 GrSurfaceProxy::UseAllocator::kYes);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500295}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400296
Greg Danielf41b2bd2019-08-22 16:19:24 -0400297// Set up so there are two opsTasks that need to be flushed but the resource allocator thinks
298// it is over budget. The two opsTasks should be flushed separately and the opsTask indices
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400299// returned from assign should be correct.
300DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
301 GrContext* context = ctxInfo.grContext();
302 const GrCaps* caps = context->priv().caps();
303 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
304 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
305
Robert Phillipscf39f372019-09-03 10:29:20 -0400306 size_t origMaxBytes = context->getResourceCacheLimit();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400307
308 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400309 context->setResourceCacheLimit(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400310
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400311 const ProxyParams params = { 64, GrRenderable::kNo, GrColorType::kRGBA_8888,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400312 SkBackingFit::kExact, 1, kTopLeft_GrSurfaceOrigin,
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400313 SkBudgeted::kYes };
314
315 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400316 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params);
317 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params);
318 sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params);
319 sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400320
Brian Salomonbeb7f522019-08-30 16:19:42 -0400321 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 2));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400322
Robert Phillips3d4cac52019-06-11 08:08:08 -0400323 alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400324 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400325 alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400326 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400327 alloc.markEndOfOpsTask(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400328
Robert Phillips3d4cac52019-06-11 08:08:08 -0400329 alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400330 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400331 alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400332 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400333 alloc.markEndOfOpsTask(1);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400334
335 int startIndex, stopIndex;
336 GrResourceAllocator::AssignError error;
337
Robert Phillipsc73666f2019-04-24 08:49:48 -0400338 alloc.determineRecyclability();
339
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400340 alloc.assign(&startIndex, &stopIndex, &error);
341 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
342 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
343
344 alloc.assign(&startIndex, &stopIndex, &error);
345 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
346 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400347 }
348
Robert Phillipscf39f372019-09-03 10:29:20 -0400349 context->setResourceCacheLimit(origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400350}
Greg Danield72dd4d2019-08-29 14:37:46 -0400351
352// This test is used to make sure we are tracking the current task index during the assign call in
353// the GrResourceAllocator. Specifically we can fall behind if we have intervals that don't
354// use the allocator. In this case we need to possibly increment the fCurOpsTaskIndex multiple times
355// to get in back in sync. We had a bug where we'd only every increment the index by one,
356// http://crbug.com/996610.
357DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorCurOpsTaskIndexTest,
358 reporter, ctxInfo) {
359 GrContext* context = ctxInfo.grContext();
360 const GrCaps* caps = context->priv().caps();
361 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
362 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
363
Robert Phillipscf39f372019-09-03 10:29:20 -0400364 size_t origMaxBytes = context->getResourceCacheLimit();
Greg Danield72dd4d2019-08-29 14:37:46 -0400365
366 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400367 context->setResourceCacheLimit(0);
Greg Danield72dd4d2019-08-29 14:37:46 -0400368
369 ProxyParams params;
370 params.fFit = SkBackingFit::kExact;
371 params.fOrigin = kTopLeft_GrSurfaceOrigin;
372 params.fColorType = GrColorType::kRGBA_8888;
373 params.fRenderable = GrRenderable::kYes;
374 params.fSampleCnt = 1;
375 params.fSize = 100;
376 params.fBudgeted = SkBudgeted::kYes;
377
378 sk_sp<GrSurfaceProxy> proxy1 = make_deferred(proxyProvider, caps, params);
379 if (!proxy1) {
380 return;
381 }
382 sk_sp<GrSurfaceProxy> proxy2 = make_deferred(proxyProvider, caps, params);
383 if (!proxy2) {
384 return;
385 }
386
387 // Wrapped proxy that will be ignored by the resourceAllocator. We use this to try and get the
388 // resource allocator fCurOpsTaskIndex to fall behind what it really should be.
389 GrBackendTexture backEndTex;
390 sk_sp<GrSurfaceProxy> proxyWrapped = make_backend(ctxInfo.grContext(), params,
391 &backEndTex);
392 if (!proxyWrapped) {
393 return;
394 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400395
396 // Same as above, but we actually need to have at least two intervals that don't go through the
397 // resource allocator to expose the index bug.
398 GrBackendTexture backEndTex2;
399 sk_sp<GrSurfaceProxy> proxyWrapped2 = make_backend(ctxInfo.grContext(), params,
400 &backEndTex2);
401 if (!proxyWrapped2) {
402 cleanup_backend(ctxInfo.grContext(), backEndTex);
403 return;
404 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400405
Brian Salomonbeb7f522019-08-30 16:19:42 -0400406 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 4));
Greg Danield72dd4d2019-08-29 14:37:46 -0400407
408 alloc.addInterval(proxyWrapped.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
409 alloc.incOps();
410 alloc.markEndOfOpsTask(0);
411
412 alloc.addInterval(proxyWrapped2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
413 alloc.incOps();
414 alloc.markEndOfOpsTask(1);
415
416 alloc.addInterval(proxy1.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
417 alloc.incOps();
418 alloc.markEndOfOpsTask(2);
419
420 // We want to force the resource allocator to do a intermediateFlush for the previous interval.
421 // But if it is the resource allocator is at the of its list of intervals it skips the
422 // intermediate flush call, so we add another interval here so it is not skipped.
423 alloc.addInterval(proxy2.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
424 alloc.incOps();
425 alloc.markEndOfOpsTask(3);
426
427 int startIndex, stopIndex;
428 GrResourceAllocator::AssignError error;
429
430 alloc.determineRecyclability();
431
432 alloc.assign(&startIndex, &stopIndex, &error);
433 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
434 // The original bug in the allocator here would return a stopIndex of 2 since it would have only
435 // incremented its fCurOpsTaskIndex once instead of the needed two times to skip the first two
436 // unused intervals.
437 REPORTER_ASSERT(reporter, 0 == startIndex && 3 == stopIndex);
438
439 alloc.assign(&startIndex, &stopIndex, &error);
440 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
441 REPORTER_ASSERT(reporter, 3 == startIndex && 4 == stopIndex);
442
443 cleanup_backend(ctxInfo.grContext(), backEndTex);
444 cleanup_backend(ctxInfo.grContext(), backEndTex2);
445
Robert Phillipscf39f372019-09-03 10:29:20 -0400446 context->setResourceCacheLimit(origMaxBytes);
Greg Danield72dd4d2019-08-29 14:37:46 -0400447}
448