blob: fc234f10151d72b23689960d08a85bc7d547070f [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 Daniel4065d452018-11-16 15:43:41 -050044
Brian Salomonbeb7f522019-08-30 16:19:42 -040045 return proxyProvider->createProxy(format, desc, p.fRenderable, p.fSampleCnt, p.fOrigin,
46 GrMipMapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040047}
48
Robert Phillips3d4cac52019-06-11 08:08:08 -040049static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
50 GrBackendTexture* backendTex) {
Robert Phillips9da87e02019-02-04 13:26:26 -050051 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050052
Robert Phillipsc80b0e92019-07-23 10:27:09 -040053 SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType);
54 SkASSERT(SkColorType::kUnknown_SkColorType != skColorType);
55
56 *backendTex = context->createBackendTexture(p.fSize, p.fSize, skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -040057 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040058 GrMipMapped::kNo, GrRenderable::kNo,
59 GrProtected::kNo);
Robert Phillips646f6372018-09-25 09:31:10 -040060 if (!backendTex->isValid()) {
61 return nullptr;
62 }
Robert Phillips57aa3672017-07-21 11:38:13 -040063
Robert Phillipsc80b0e92019-07-23 10:27:09 -040064 return proxyProvider->wrapBackendTexture(*backendTex, p.fColorType, p.fOrigin,
65 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
66 kRead_GrIOType);
Robert Phillips57aa3672017-07-21 11:38:13 -040067}
68
Brian Salomon26102cb2018-03-09 09:33:19 -050069static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040070 context->deleteBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040071}
72
Robert Phillips5af44de2017-07-18 14:49:38 -040073// Basic test that two proxies with overlapping intervals and compatible descriptors are
74// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040075static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -040076 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Brian Salomon2c791fc2019-04-02 11:52:03 -040077 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040078 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040079
Robert Phillips3d4cac52019-06-11 08:08:08 -040080 alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040081 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -040082 alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040083 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -040084 alloc.markEndOfOpsTask(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040085
Robert Phillipsc73666f2019-04-24 08:49:48 -040086 alloc.determineRecyclability();
87
Robert Phillipseafd48a2017-11-16 07:52:08 -050088 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050089 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -050090 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050091 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040092
Brian Salomonfd98c2c2018-07-31 17:25:29 -040093 REPORTER_ASSERT(reporter, p1->peekSurface());
94 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040095 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
96 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040097}
98
Robert Phillips57aa3672017-07-21 11:38:13 -040099// Test various cases when two proxies do not have overlapping intervals.
100// This mainly acts as a test of the ResourceAllocator's free pool.
101static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -0400102 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Robert Phillips57aa3672017-07-21 11:38:13 -0400103 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400104 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400105
106 alloc.incOps();
107 alloc.incOps();
108 alloc.incOps();
109 alloc.incOps();
110 alloc.incOps();
111 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -0400112
Robert Phillips3d4cac52019-06-11 08:08:08 -0400113 alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
114 alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400115 alloc.markEndOfOpsTask(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400116
Robert Phillipsc73666f2019-04-24 08:49:48 -0400117 alloc.determineRecyclability();
118
Robert Phillipseafd48a2017-11-16 07:52:08 -0500119 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500120 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500121 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500122 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400123
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400124 REPORTER_ASSERT(reporter, p1->peekSurface());
125 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400126 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
127 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
128}
129
130DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
132 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
133 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400134
Robert Phillips57aa3672017-07-21 11:38:13 -0400135 struct TestCase {
136 ProxyParams fP1;
137 ProxyParams fP2;
138 bool fExpectation;
139 };
140
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400141 constexpr GrRenderable kRT = GrRenderable::kYes;
142 constexpr GrRenderable kNotRT = GrRenderable::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400143
144 constexpr bool kShare = true;
145 constexpr bool kDontShare = false;
146 // Non-RT GrSurfaces are never recycled on some platforms.
147 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
148
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400149 const GrColorType kRGBA = GrColorType::kRGBA_8888;
150 const GrColorType kBGRA = GrColorType::kBGRA_8888;
Robert Phillips57aa3672017-07-21 11:38:13 -0400151
152 const SkBackingFit kE = SkBackingFit::kExact;
153 const SkBackingFit kA = SkBackingFit::kApprox;
154
155 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
156 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
157
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400158 const SkBudgeted kNotB = SkBudgeted::kNo;
159
Robert Phillips57aa3672017-07-21 11:38:13 -0400160 //--------------------------------------------------------------------------------------------
161 TestCase gOverlappingTests[] = {
162 //----------------------------------------------------------------------------------------
163 // Two proxies with overlapping intervals and compatible descriptors should never share
164 // RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400165 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400166 // non-RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400167 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400168 };
169
170 for (auto test : gOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400171 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
172 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillipse5f73282019-06-18 17:15:04 -0400173 overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400174 }
175
Greg Daniel6fa62e22019-08-07 15:52:37 -0400176 auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
177 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(2, beFormat);
178 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(4, beFormat);
Robert Phillips57aa3672017-07-21 11:38:13 -0400179
180 //--------------------------------------------------------------------------------------------
181 TestCase gNonOverlappingTests[] = {
182 //----------------------------------------------------------------------------------------
183 // Two non-overlapping intervals w/ compatible proxies should share
184 // both same size & approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400185 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
186 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400187 // diffs sizes but still approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400188 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
189 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400190 // sames sizes but exact
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400191 { { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, kShare },
192 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400193 //----------------------------------------------------------------------------------------
194 // Two non-overlapping intervals w/ different exact sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400195 { { 56, kRT, kRGBA, kE, 1, kTL, kNotB }, { 54, kRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400196 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400197 { { 255, kRT, kRGBA, kA, 1, kTL, kNotB }, { 127, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400198 // Two non-overlapping intervals w/ different MSAA sample counts should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400199 { { 64, kRT, kRGBA, kA, k2, kTL, kNotB },{ 64, kRT, kRGBA, kA, k4,kTL, kNotB}, k2 == k4 },
Robert Phillips57aa3672017-07-21 11:38:13 -0400200 // Two non-overlapping intervals w/ different configs should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400201 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kBGRA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400202 // Two non-overlapping intervals w/ different RT classifications should never share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400203 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
204 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400205 // Two non-overlapping intervals w/ different origins should share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400206 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kBL, kNotB }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400207 };
208
209 for (auto test : gNonOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400210 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
211 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400212
Robert Phillips57aa3672017-07-21 11:38:13 -0400213 if (!p1 || !p2) {
214 continue; // creation can fail (i.e., for msaa4 on iOS)
215 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400216
Robert Phillipse5f73282019-06-18 17:15:04 -0400217 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
218 test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400219 }
220
221 {
222 // Wrapped backend textures should never be reused
223 TestCase t[1] = {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400224 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare }
Robert Phillips57aa3672017-07-21 11:38:13 -0400225 };
226
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500227 GrBackendTexture backEndTex;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400228 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
229 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400230
Robert Phillipse5f73282019-06-18 17:15:04 -0400231 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
232 t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400233
Brian Salomon26102cb2018-03-09 09:33:19 -0500234 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400235 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400236}
Robert Phillips1734dd32018-08-21 13:52:09 -0400237
238static void draw(GrContext* context) {
239 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
240
241 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
242 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
243
244 SkCanvas* c = s->getCanvas();
245
246 c->clear(SK_ColorBLACK);
247}
248
249
250DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
251 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400252
Robert Phillipscf39f372019-09-03 10:29:20 -0400253 size_t maxBytes = context->getResourceCacheLimit();
Robert Phillips1734dd32018-08-21 13:52:09 -0400254
Robert Phillipscf39f372019-09-03 10:29:20 -0400255 context->setResourceCacheLimit(0); // We'll always be overbudget
Robert Phillips1734dd32018-08-21 13:52:09 -0400256
257 draw(context);
258 draw(context);
259 draw(context);
260 draw(context);
261 context->flush();
262
Robert Phillipscf39f372019-09-03 10:29:20 -0400263 context->setResourceCacheLimit(maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400264}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500265
266sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400267 const ProxyParams& p) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400268 GrPixelConfig config = GrColorTypeToPixelConfig(p.fColorType);
Brian Salomon4eb38b72019-08-05 12:58:39 -0400269 const auto format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500270
271 GrSurfaceDesc desc;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500272 desc.fWidth = p.fSize;
273 desc.fHeight = p.fSize;
274 desc.fConfig = config;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500275
276 SkBackingFit fit = p.fFit;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400277 auto callback = [fit, desc, format, p](GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400278 sk_sp<GrTexture> texture;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500279 if (fit == SkBackingFit::kApprox) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400280 texture = resourceProvider->createApproxTexture(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400281 desc, format, p.fRenderable, p.fSampleCnt, GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500282 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400283 texture = resourceProvider->createTexture(desc, format, p.fRenderable, p.fSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400284 GrMipMapped::kNo, SkBudgeted::kNo,
285 GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500286 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400287 return GrSurfaceProxy::LazyCallbackResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500288 };
Brian Salomon577aa0f2018-11-30 13:32:23 -0500289 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600290 return proxyProvider->createLazyProxy(
291 callback, format, desc, p.fRenderable, p.fSampleCnt, p.fOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400292 GrMipMapsStatus::kNotAllocated, flags, p.fFit, p.fBudgeted, GrProtected::kNo,
293 GrSurfaceProxy::UseAllocator::kYes);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500294}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400295
Greg Danielf41b2bd2019-08-22 16:19:24 -0400296// Set up so there are two opsTasks that need to be flushed but the resource allocator thinks
297// it is over budget. The two opsTasks should be flushed separately and the opsTask indices
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400298// returned from assign should be correct.
299DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
300 GrContext* context = ctxInfo.grContext();
301 const GrCaps* caps = context->priv().caps();
302 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
303 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
304
Robert Phillipscf39f372019-09-03 10:29:20 -0400305 size_t origMaxBytes = context->getResourceCacheLimit();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400306
307 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400308 context->setResourceCacheLimit(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400309
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400310 const ProxyParams params = { 64, GrRenderable::kNo, GrColorType::kRGBA_8888,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400311 SkBackingFit::kExact, 1, kTopLeft_GrSurfaceOrigin,
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400312 SkBudgeted::kYes };
313
314 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400315 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params);
316 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params);
317 sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params);
318 sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400319
Brian Salomonbeb7f522019-08-30 16:19:42 -0400320 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 2));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400321
Robert Phillips3d4cac52019-06-11 08:08:08 -0400322 alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400323 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400324 alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400325 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400326 alloc.markEndOfOpsTask(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400327
Robert Phillips3d4cac52019-06-11 08:08:08 -0400328 alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400329 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400330 alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400331 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400332 alloc.markEndOfOpsTask(1);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400333
334 int startIndex, stopIndex;
335 GrResourceAllocator::AssignError error;
336
Robert Phillipsc73666f2019-04-24 08:49:48 -0400337 alloc.determineRecyclability();
338
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400339 alloc.assign(&startIndex, &stopIndex, &error);
340 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
341 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
342
343 alloc.assign(&startIndex, &stopIndex, &error);
344 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
345 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400346 }
347
Robert Phillipscf39f372019-09-03 10:29:20 -0400348 context->setResourceCacheLimit(origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400349}
Greg Danield72dd4d2019-08-29 14:37:46 -0400350
351// This test is used to make sure we are tracking the current task index during the assign call in
352// the GrResourceAllocator. Specifically we can fall behind if we have intervals that don't
353// use the allocator. In this case we need to possibly increment the fCurOpsTaskIndex multiple times
354// to get in back in sync. We had a bug where we'd only every increment the index by one,
355// http://crbug.com/996610.
356DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorCurOpsTaskIndexTest,
357 reporter, ctxInfo) {
358 GrContext* context = ctxInfo.grContext();
359 const GrCaps* caps = context->priv().caps();
360 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
361 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
362
Robert Phillipscf39f372019-09-03 10:29:20 -0400363 size_t origMaxBytes = context->getResourceCacheLimit();
Greg Danield72dd4d2019-08-29 14:37:46 -0400364
365 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400366 context->setResourceCacheLimit(0);
Greg Danield72dd4d2019-08-29 14:37:46 -0400367
368 ProxyParams params;
369 params.fFit = SkBackingFit::kExact;
370 params.fOrigin = kTopLeft_GrSurfaceOrigin;
371 params.fColorType = GrColorType::kRGBA_8888;
372 params.fRenderable = GrRenderable::kYes;
373 params.fSampleCnt = 1;
374 params.fSize = 100;
375 params.fBudgeted = SkBudgeted::kYes;
376
377 sk_sp<GrSurfaceProxy> proxy1 = make_deferred(proxyProvider, caps, params);
378 if (!proxy1) {
379 return;
380 }
381 sk_sp<GrSurfaceProxy> proxy2 = make_deferred(proxyProvider, caps, params);
382 if (!proxy2) {
383 return;
384 }
385
386 // Wrapped proxy that will be ignored by the resourceAllocator. We use this to try and get the
387 // resource allocator fCurOpsTaskIndex to fall behind what it really should be.
388 GrBackendTexture backEndTex;
389 sk_sp<GrSurfaceProxy> proxyWrapped = make_backend(ctxInfo.grContext(), params,
390 &backEndTex);
391 if (!proxyWrapped) {
392 return;
393 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400394
395 // Same as above, but we actually need to have at least two intervals that don't go through the
396 // resource allocator to expose the index bug.
397 GrBackendTexture backEndTex2;
398 sk_sp<GrSurfaceProxy> proxyWrapped2 = make_backend(ctxInfo.grContext(), params,
399 &backEndTex2);
400 if (!proxyWrapped2) {
401 cleanup_backend(ctxInfo.grContext(), backEndTex);
402 return;
403 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400404
Brian Salomonbeb7f522019-08-30 16:19:42 -0400405 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 4));
Greg Danield72dd4d2019-08-29 14:37:46 -0400406
407 alloc.addInterval(proxyWrapped.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
408 alloc.incOps();
409 alloc.markEndOfOpsTask(0);
410
411 alloc.addInterval(proxyWrapped2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
412 alloc.incOps();
413 alloc.markEndOfOpsTask(1);
414
415 alloc.addInterval(proxy1.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
416 alloc.incOps();
417 alloc.markEndOfOpsTask(2);
418
419 // We want to force the resource allocator to do a intermediateFlush for the previous interval.
420 // But if it is the resource allocator is at the of its list of intervals it skips the
421 // intermediate flush call, so we add another interval here so it is not skipped.
422 alloc.addInterval(proxy2.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
423 alloc.incOps();
424 alloc.markEndOfOpsTask(3);
425
426 int startIndex, stopIndex;
427 GrResourceAllocator::AssignError error;
428
429 alloc.determineRecyclability();
430
431 alloc.assign(&startIndex, &stopIndex, &error);
432 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
433 // The original bug in the allocator here would return a stopIndex of 2 since it would have only
434 // incremented its fCurOpsTaskIndex once instead of the needed two times to skip the first two
435 // unused intervals.
436 REPORTER_ASSERT(reporter, 0 == startIndex && 3 == stopIndex);
437
438 alloc.assign(&startIndex, &stopIndex, &error);
439 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
440 REPORTER_ASSERT(reporter, 3 == startIndex && 4 == stopIndex);
441
442 cleanup_backend(ctxInfo.grContext(), backEndTex);
443 cleanup_backend(ctxInfo.grContext(), backEndTex2);
444
Robert Phillipscf39f372019-09-03 10:29:20 -0400445 context->setResourceCacheLimit(origMaxBytes);
Greg Danield72dd4d2019-08-29 14:37:46 -0400446}
447