blob: f0d0990b006732ed60f2791acd3cdfe03b39eaef [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 Phillips57aa3672017-07-21 11:38:13 -040036 GrSurfaceDesc desc;
Robert Phillips57aa3672017-07-21 11:38:13 -040037 desc.fWidth = p.fSize;
38 desc.fHeight = p.fSize;
Robert Phillips57aa3672017-07-21 11:38:13 -040039
Robert Phillips0a15cc62019-07-30 12:49:10 -040040 const GrBackendFormat format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Greg Daniel47c20e82020-01-21 14:29:57 -050041 GrSwizzle swizzle = caps->getReadSwizzle(format, p.fColorType);
Greg Daniel4065d452018-11-16 15:43:41 -050042
Greg Daniel47c20e82020-01-21 14:29:57 -050043 return proxyProvider->createProxy(format, desc, swizzle, p.fRenderable, p.fSampleCnt, p.fOrigin,
Brian Salomonbeb7f522019-08-30 16:19:42 -040044 GrMipMapped::kNo, p.fFit, p.fBudgeted, GrProtected::kNo);
Robert Phillips57aa3672017-07-21 11:38:13 -040045}
46
Robert Phillips3d4cac52019-06-11 08:08:08 -040047static sk_sp<GrSurfaceProxy> make_backend(GrContext* context, const ProxyParams& p,
48 GrBackendTexture* backendTex) {
Robert Phillips9da87e02019-02-04 13:26:26 -050049 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050050
Robert Phillipsc80b0e92019-07-23 10:27:09 -040051 SkColorType skColorType = GrColorTypeToSkColorType(p.fColorType);
52 SkASSERT(SkColorType::kUnknown_SkColorType != skColorType);
53
54 *backendTex = context->createBackendTexture(p.fSize, p.fSize, skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -040055 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040056 GrMipMapped::kNo, GrRenderable::kNo,
57 GrProtected::kNo);
Robert Phillips646f6372018-09-25 09:31:10 -040058 if (!backendTex->isValid()) {
59 return nullptr;
60 }
Robert Phillips57aa3672017-07-21 11:38:13 -040061
Robert Phillipsc80b0e92019-07-23 10:27:09 -040062 return proxyProvider->wrapBackendTexture(*backendTex, p.fColorType, p.fOrigin,
63 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
64 kRead_GrIOType);
Robert Phillips57aa3672017-07-21 11:38:13 -040065}
66
Brian Salomon26102cb2018-03-09 09:33:19 -050067static void cleanup_backend(GrContext* context, const GrBackendTexture& backendTex) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040068 context->deleteBackendTexture(backendTex);
Robert Phillips57aa3672017-07-21 11:38:13 -040069}
70
Robert Phillips5af44de2017-07-18 14:49:38 -040071// Basic test that two proxies with overlapping intervals and compatible descriptors are
72// assigned different GrSurfaces.
Robert Phillips57aa3672017-07-21 11:38:13 -040073static void overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -040074 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Brian Salomon2c791fc2019-04-02 11:52:03 -040075 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040076 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillips5af44de2017-07-18 14:49:38 -040077
Robert Phillips3d4cac52019-06-11 08:08:08 -040078 alloc.addInterval(p1.get(), 0, 4, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040079 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -040080 alloc.addInterval(p2.get(), 1, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -040081 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -040082 alloc.markEndOfOpsTask(0);
Robert Phillips5af44de2017-07-18 14:49:38 -040083
Robert Phillipsc73666f2019-04-24 08:49:48 -040084 alloc.determineRecyclability();
85
Robert Phillipseafd48a2017-11-16 07:52:08 -050086 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -050087 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -050088 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -050089 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips5af44de2017-07-18 14:49:38 -040090
Brian Salomonfd98c2c2018-07-31 17:25:29 -040091 REPORTER_ASSERT(reporter, p1->peekSurface());
92 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -040093 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
94 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
Robert Phillips5af44de2017-07-18 14:49:38 -040095}
96
Robert Phillips57aa3672017-07-21 11:38:13 -040097// Test various cases when two proxies do not have overlapping intervals.
98// This mainly acts as a test of the ResourceAllocator's free pool.
99static void non_overlap_test(skiatest::Reporter* reporter, GrResourceProvider* resourceProvider,
Robert Phillips3d4cac52019-06-11 08:08:08 -0400100 sk_sp<GrSurfaceProxy> p1, sk_sp<GrSurfaceProxy> p2,
Robert Phillips57aa3672017-07-21 11:38:13 -0400101 bool expectedResult) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400102 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 1));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400103
104 alloc.incOps();
105 alloc.incOps();
106 alloc.incOps();
107 alloc.incOps();
108 alloc.incOps();
109 alloc.incOps();
Robert Phillips57aa3672017-07-21 11:38:13 -0400110
Robert Phillips3d4cac52019-06-11 08:08:08 -0400111 alloc.addInterval(p1.get(), 0, 2, GrResourceAllocator::ActualUse::kYes);
112 alloc.addInterval(p2.get(), 3, 5, GrResourceAllocator::ActualUse::kYes);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400113 alloc.markEndOfOpsTask(0);
Robert Phillips57aa3672017-07-21 11:38:13 -0400114
Robert Phillipsc73666f2019-04-24 08:49:48 -0400115 alloc.determineRecyclability();
116
Robert Phillipseafd48a2017-11-16 07:52:08 -0500117 int startIndex, stopIndex;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500118 GrResourceAllocator::AssignError error;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500119 alloc.assign(&startIndex, &stopIndex, &error);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500120 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
Robert Phillips57aa3672017-07-21 11:38:13 -0400121
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400122 REPORTER_ASSERT(reporter, p1->peekSurface());
123 REPORTER_ASSERT(reporter, p2->peekSurface());
Robert Phillips57aa3672017-07-21 11:38:13 -0400124 bool doTheBackingStoresMatch = p1->underlyingUniqueID() == p2->underlyingUniqueID();
125 REPORTER_ASSERT(reporter, expectedResult == doTheBackingStoresMatch);
126}
127
128DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500129 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
130 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
131 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
Robert Phillips5af44de2017-07-18 14:49:38 -0400132
Robert Phillips57aa3672017-07-21 11:38:13 -0400133 struct TestCase {
134 ProxyParams fP1;
135 ProxyParams fP2;
136 bool fExpectation;
137 };
138
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400139 constexpr GrRenderable kRT = GrRenderable::kYes;
140 constexpr GrRenderable kNotRT = GrRenderable::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400141
142 constexpr bool kShare = true;
143 constexpr bool kDontShare = false;
144 // Non-RT GrSurfaces are never recycled on some platforms.
145 bool kConditionallyShare = resourceProvider->caps()->reuseScratchTextures();
146
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400147 const GrColorType kRGBA = GrColorType::kRGBA_8888;
Greg Danield51fa2f2020-01-22 16:53:38 -0500148 const GrColorType kAlpha = GrColorType::kAlpha_8;
Robert Phillips57aa3672017-07-21 11:38:13 -0400149
150 const SkBackingFit kE = SkBackingFit::kExact;
151 const SkBackingFit kA = SkBackingFit::kApprox;
152
153 const GrSurfaceOrigin kTL = kTopLeft_GrSurfaceOrigin;
154 const GrSurfaceOrigin kBL = kBottomLeft_GrSurfaceOrigin;
155
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400156 const SkBudgeted kNotB = SkBudgeted::kNo;
157
Robert Phillips57aa3672017-07-21 11:38:13 -0400158 //--------------------------------------------------------------------------------------------
159 TestCase gOverlappingTests[] = {
160 //----------------------------------------------------------------------------------------
161 // Two proxies with overlapping intervals and compatible descriptors should never share
162 // RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400163 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400164 // non-RT version
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400165 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400166 };
167
168 for (auto test : gOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400169 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
170 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillipse5f73282019-06-18 17:15:04 -0400171 overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2), test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400172 }
173
Greg Daniel6fa62e22019-08-07 15:52:37 -0400174 auto beFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
175 int k2 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(2, beFormat);
176 int k4 = ctxInfo.grContext()->priv().caps()->getRenderTargetSampleCount(4, beFormat);
Robert Phillips57aa3672017-07-21 11:38:13 -0400177
178 //--------------------------------------------------------------------------------------------
179 TestCase gNonOverlappingTests[] = {
180 //----------------------------------------------------------------------------------------
181 // Two non-overlapping intervals w/ compatible proxies should share
182 // both same size & approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400183 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
184 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400185 // diffs sizes but still approx
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400186 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kRT, kRGBA, kA, 1, kTL, kNotB }, kShare },
187 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 50, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400188 // sames sizes but exact
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400189 { { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kRT, kRGBA, kE, 1, kTL, kNotB }, kShare },
190 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kConditionallyShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400191 //----------------------------------------------------------------------------------------
192 // Two non-overlapping intervals w/ different exact sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400193 { { 56, kRT, kRGBA, kE, 1, kTL, kNotB }, { 54, kRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400194 // Two non-overlapping intervals w/ _very different_ approx sizes should not share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400195 { { 255, kRT, kRGBA, kA, 1, kTL, kNotB }, { 127, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400196 // Two non-overlapping intervals w/ different MSAA sample counts should not share
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400197 { { 64, kRT, kRGBA, kA, k2, kTL, kNotB },{ 64, kRT, kRGBA, kA, k4,kTL, kNotB}, k2 == k4 },
Robert Phillips57aa3672017-07-21 11:38:13 -0400198 // Two non-overlapping intervals w/ different configs should not share
Greg Danield51fa2f2020-01-22 16:53:38 -0500199 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kAlpha, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400200 // Two non-overlapping intervals w/ different RT classifications should never share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400201 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
202 { { 64, kNotRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, kDontShare },
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400203 // Two non-overlapping intervals w/ different origins should share
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400204 { { 64, kRT, kRGBA, kA, 1, kTL, kNotB }, { 64, kRT, kRGBA, kA, 1, kBL, kNotB }, kShare },
Robert Phillips57aa3672017-07-21 11:38:13 -0400205 };
206
207 for (auto test : gNonOverlappingTests) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400208 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, test.fP1);
209 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, test.fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400210
Robert Phillips57aa3672017-07-21 11:38:13 -0400211 if (!p1 || !p2) {
212 continue; // creation can fail (i.e., for msaa4 on iOS)
213 }
Robert Phillips715d08c2018-07-18 13:56:48 -0400214
Robert Phillipse5f73282019-06-18 17:15:04 -0400215 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
216 test.fExpectation);
Robert Phillips57aa3672017-07-21 11:38:13 -0400217 }
218
219 {
220 // Wrapped backend textures should never be reused
221 TestCase t[1] = {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400222 { { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, { 64, kNotRT, kRGBA, kE, 1, kTL, kNotB }, kDontShare }
Robert Phillips57aa3672017-07-21 11:38:13 -0400223 };
224
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500225 GrBackendTexture backEndTex;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400226 sk_sp<GrSurfaceProxy> p1 = make_backend(ctxInfo.grContext(), t[0].fP1, &backEndTex);
227 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, t[0].fP2);
Robert Phillips715d08c2018-07-18 13:56:48 -0400228
Robert Phillipse5f73282019-06-18 17:15:04 -0400229 non_overlap_test(reporter, resourceProvider, std::move(p1), std::move(p2),
230 t[0].fExpectation);
Robert Phillips715d08c2018-07-18 13:56:48 -0400231
Brian Salomon26102cb2018-03-09 09:33:19 -0500232 cleanup_backend(ctxInfo.grContext(), backEndTex);
Robert Phillips57aa3672017-07-21 11:38:13 -0400233 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400234}
Robert Phillips1734dd32018-08-21 13:52:09 -0400235
236static void draw(GrContext* context) {
237 SkImageInfo ii = SkImageInfo::Make(1024, 1024, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
238
239 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
240 ii, 1, kTopLeft_GrSurfaceOrigin, nullptr);
241
242 SkCanvas* c = s->getCanvas();
243
244 c->clear(SK_ColorBLACK);
245}
246
247
248DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorStressTest, reporter, ctxInfo) {
249 GrContext* context = ctxInfo.grContext();
Robert Phillips1734dd32018-08-21 13:52:09 -0400250
Robert Phillipscf39f372019-09-03 10:29:20 -0400251 size_t maxBytes = context->getResourceCacheLimit();
Robert Phillips1734dd32018-08-21 13:52:09 -0400252
Robert Phillipscf39f372019-09-03 10:29:20 -0400253 context->setResourceCacheLimit(0); // We'll always be overbudget
Robert Phillips1734dd32018-08-21 13:52:09 -0400254
255 draw(context);
256 draw(context);
257 draw(context);
258 draw(context);
259 context->flush();
260
Robert Phillipscf39f372019-09-03 10:29:20 -0400261 context->setResourceCacheLimit(maxBytes);
Robert Phillips1734dd32018-08-21 13:52:09 -0400262}
Brian Salomon577aa0f2018-11-30 13:32:23 -0500263
264sk_sp<GrSurfaceProxy> make_lazy(GrProxyProvider* proxyProvider, const GrCaps* caps,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400265 const ProxyParams& p) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400266 const auto format = caps->getDefaultBackendFormat(p.fColorType, p.fRenderable);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500267
268 GrSurfaceDesc desc;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500269 desc.fWidth = p.fSize;
270 desc.fHeight = p.fSize;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500271
272 SkBackingFit fit = p.fFit;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400273 auto callback = [fit, desc, format, p](GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400274 sk_sp<GrTexture> texture;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500275 if (fit == SkBackingFit::kApprox) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400276 texture = resourceProvider->createApproxTexture(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400277 desc, format, p.fRenderable, p.fSampleCnt, GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500278 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400279 texture = resourceProvider->createTexture(desc, format, p.fRenderable, p.fSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400280 GrMipMapped::kNo, SkBudgeted::kNo,
281 GrProtected::kNo);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500282 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400283 return GrSurfaceProxy::LazyCallbackResult(std::move(texture));
Brian Salomon577aa0f2018-11-30 13:32:23 -0500284 };
Brian Salomon577aa0f2018-11-30 13:32:23 -0500285 GrInternalSurfaceFlags flags = GrInternalSurfaceFlags::kNone;
Greg Danielce3ddaa2020-01-22 16:58:15 -0500286 GrSwizzle readSwizzle = caps->getReadSwizzle(format, p.fColorType);
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600287 return proxyProvider->createLazyProxy(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500288 callback, format, desc, readSwizzle, p.fRenderable, p.fSampleCnt, p.fOrigin,
289 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, flags, p.fFit, p.fBudgeted,
290 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Brian Salomon577aa0f2018-11-30 13:32:23 -0500291}
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400292
Greg Danielf41b2bd2019-08-22 16:19:24 -0400293// Set up so there are two opsTasks that need to be flushed but the resource allocator thinks
294// it is over budget. The two opsTasks should be flushed separately and the opsTask indices
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400295// returned from assign should be correct.
296DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorOverBudgetTest, reporter, ctxInfo) {
297 GrContext* context = ctxInfo.grContext();
298 const GrCaps* caps = context->priv().caps();
299 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
300 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
301
Robert Phillipscf39f372019-09-03 10:29:20 -0400302 size_t origMaxBytes = context->getResourceCacheLimit();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400303
304 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400305 context->setResourceCacheLimit(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400306
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400307 const ProxyParams params = { 64, GrRenderable::kNo, GrColorType::kRGBA_8888,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400308 SkBackingFit::kExact, 1, kTopLeft_GrSurfaceOrigin,
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400309 SkBudgeted::kYes };
310
311 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400312 sk_sp<GrSurfaceProxy> p1 = make_deferred(proxyProvider, caps, params);
313 sk_sp<GrSurfaceProxy> p2 = make_deferred(proxyProvider, caps, params);
314 sk_sp<GrSurfaceProxy> p3 = make_deferred(proxyProvider, caps, params);
315 sk_sp<GrSurfaceProxy> p4 = make_deferred(proxyProvider, caps, params);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400316
Brian Salomonbeb7f522019-08-30 16:19:42 -0400317 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 2));
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400318
Robert Phillips3d4cac52019-06-11 08:08:08 -0400319 alloc.addInterval(p1.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400320 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400321 alloc.addInterval(p2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400322 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400323 alloc.markEndOfOpsTask(0);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400324
Robert Phillips3d4cac52019-06-11 08:08:08 -0400325 alloc.addInterval(p3.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400326 alloc.incOps();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400327 alloc.addInterval(p4.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400328 alloc.incOps();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400329 alloc.markEndOfOpsTask(1);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400330
331 int startIndex, stopIndex;
332 GrResourceAllocator::AssignError error;
333
Robert Phillipsc73666f2019-04-24 08:49:48 -0400334 alloc.determineRecyclability();
335
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400336 alloc.assign(&startIndex, &stopIndex, &error);
337 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
338 REPORTER_ASSERT(reporter, 0 == startIndex && 1 == stopIndex);
339
340 alloc.assign(&startIndex, &stopIndex, &error);
341 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
342 REPORTER_ASSERT(reporter, 1 == startIndex && 2 == stopIndex);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400343 }
344
Robert Phillipscf39f372019-09-03 10:29:20 -0400345 context->setResourceCacheLimit(origMaxBytes);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400346}
Greg Danield72dd4d2019-08-29 14:37:46 -0400347
348// This test is used to make sure we are tracking the current task index during the assign call in
349// the GrResourceAllocator. Specifically we can fall behind if we have intervals that don't
350// use the allocator. In this case we need to possibly increment the fCurOpsTaskIndex multiple times
351// to get in back in sync. We had a bug where we'd only every increment the index by one,
352// http://crbug.com/996610.
353DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceAllocatorCurOpsTaskIndexTest,
354 reporter, ctxInfo) {
355 GrContext* context = ctxInfo.grContext();
356 const GrCaps* caps = context->priv().caps();
357 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
358 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
359
Robert Phillipscf39f372019-09-03 10:29:20 -0400360 size_t origMaxBytes = context->getResourceCacheLimit();
Greg Danield72dd4d2019-08-29 14:37:46 -0400361
362 // Force the resource allocator to always believe it is over budget
Robert Phillipscf39f372019-09-03 10:29:20 -0400363 context->setResourceCacheLimit(0);
Greg Danield72dd4d2019-08-29 14:37:46 -0400364
365 ProxyParams params;
366 params.fFit = SkBackingFit::kExact;
367 params.fOrigin = kTopLeft_GrSurfaceOrigin;
368 params.fColorType = GrColorType::kRGBA_8888;
369 params.fRenderable = GrRenderable::kYes;
370 params.fSampleCnt = 1;
371 params.fSize = 100;
372 params.fBudgeted = SkBudgeted::kYes;
373
374 sk_sp<GrSurfaceProxy> proxy1 = make_deferred(proxyProvider, caps, params);
375 if (!proxy1) {
376 return;
377 }
378 sk_sp<GrSurfaceProxy> proxy2 = make_deferred(proxyProvider, caps, params);
379 if (!proxy2) {
380 return;
381 }
382
383 // Wrapped proxy that will be ignored by the resourceAllocator. We use this to try and get the
384 // resource allocator fCurOpsTaskIndex to fall behind what it really should be.
385 GrBackendTexture backEndTex;
386 sk_sp<GrSurfaceProxy> proxyWrapped = make_backend(ctxInfo.grContext(), params,
387 &backEndTex);
388 if (!proxyWrapped) {
389 return;
390 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400391
392 // Same as above, but we actually need to have at least two intervals that don't go through the
393 // resource allocator to expose the index bug.
394 GrBackendTexture backEndTex2;
395 sk_sp<GrSurfaceProxy> proxyWrapped2 = make_backend(ctxInfo.grContext(), params,
396 &backEndTex2);
397 if (!proxyWrapped2) {
398 cleanup_backend(ctxInfo.grContext(), backEndTex);
399 return;
400 }
Greg Danield72dd4d2019-08-29 14:37:46 -0400401
Brian Salomonbeb7f522019-08-30 16:19:42 -0400402 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, 4));
Greg Danield72dd4d2019-08-29 14:37:46 -0400403
404 alloc.addInterval(proxyWrapped.get(), 0, 0, GrResourceAllocator::ActualUse::kYes);
405 alloc.incOps();
406 alloc.markEndOfOpsTask(0);
407
408 alloc.addInterval(proxyWrapped2.get(), 1, 1, GrResourceAllocator::ActualUse::kYes);
409 alloc.incOps();
410 alloc.markEndOfOpsTask(1);
411
412 alloc.addInterval(proxy1.get(), 2, 2, GrResourceAllocator::ActualUse::kYes);
413 alloc.incOps();
414 alloc.markEndOfOpsTask(2);
415
416 // We want to force the resource allocator to do a intermediateFlush for the previous interval.
417 // But if it is the resource allocator is at the of its list of intervals it skips the
418 // intermediate flush call, so we add another interval here so it is not skipped.
419 alloc.addInterval(proxy2.get(), 3, 3, GrResourceAllocator::ActualUse::kYes);
420 alloc.incOps();
421 alloc.markEndOfOpsTask(3);
422
423 int startIndex, stopIndex;
424 GrResourceAllocator::AssignError error;
425
426 alloc.determineRecyclability();
427
428 alloc.assign(&startIndex, &stopIndex, &error);
429 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
430 // The original bug in the allocator here would return a stopIndex of 2 since it would have only
431 // incremented its fCurOpsTaskIndex once instead of the needed two times to skip the first two
432 // unused intervals.
433 REPORTER_ASSERT(reporter, 0 == startIndex && 3 == stopIndex);
434
435 alloc.assign(&startIndex, &stopIndex, &error);
436 REPORTER_ASSERT(reporter, GrResourceAllocator::AssignError::kNoError == error);
437 REPORTER_ASSERT(reporter, 3 == startIndex && 4 == stopIndex);
438
439 cleanup_backend(ctxInfo.grContext(), backEndTex);
440 cleanup_backend(ctxInfo.grContext(), backEndTex2);
441
Robert Phillipscf39f372019-09-03 10:29:20 -0400442 context->setResourceCacheLimit(origMaxBytes);
Greg Danield72dd4d2019-08-29 14:37:46 -0400443}
444