blob: 214b24ded19c024440d24140272ef5da06c7d3b2 [file] [log] [blame]
bsalomoned0bcad2015-05-04 10:36:42 -07001/*
2 * Copyright 2015 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
Brian Salomon1047a492019-07-02 12:25:21 -04008#include "src/gpu/GrResourceProvider.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrContext.h"
12#include "include/private/GrResourceKey.h"
13#include "include/private/GrSingleOwner.h"
Brian Salomon1047a492019-07-02 12:25:21 -040014#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
16#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrGpuBuffer.h"
20#include "src/gpu/GrPath.h"
21#include "src/gpu/GrPathRendering.h"
22#include "src/gpu/GrProxyProvider.h"
23#include "src/gpu/GrRenderTargetPriv.h"
24#include "src/gpu/GrResourceCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrSemaphore.h"
26#include "src/gpu/GrStencilAttachment.h"
27#include "src/gpu/GrTexturePriv.h"
28#include "src/gpu/SkGr.h"
bsalomoned0bcad2015-05-04 10:36:42 -070029
Robert Phillips1bfece82017-06-01 13:56:52 -040030const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050031
32#define ASSERT_SINGLE_OWNER \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34
Robert Phillips12c46292019-04-23 07:36:17 -040035GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050036 : fCache(cache)
37 , fGpu(gpu)
38#ifdef SK_DEBUG
39 , fSingleOwner(owner)
40#endif
Robert Phillips4150eea2018-02-07 17:08:21 -050041{
Robert Phillips26c90e02017-03-14 14:39:29 -040042 fCaps = sk_ref_sp(fGpu->caps());
bsalomoned0bcad2015-05-04 10:36:42 -070043}
44
Brian Salomona3ffaab2019-07-09 12:26:46 -040045// Ensures the row bytes are populated (not 0) and makes a copy to a temporary
46// to make the row bytes tight if necessary. Returns false if the input row bytes are invalid.
47static bool prepare_level(const GrMipLevel& inLevel, size_t bpp, int w, int h, bool rowBytesSupport,
Brian Salomona3e29962019-07-16 11:52:08 -040048 bool mustInitializeAllLevels, GrMipLevel* outLevel,
49 std::unique_ptr<char[]>* data) {
50 size_t minRB = w * bpp;
Brian Salomona3ffaab2019-07-09 12:26:46 -040051 if (!inLevel.fPixels) {
Brian Salomona3e29962019-07-16 11:52:08 -040052 if (mustInitializeAllLevels) {
53 data->reset(new char[minRB * h]());
54 outLevel->fPixels = data->get();
55 outLevel->fRowBytes = minRB;
56 } else {
57 outLevel->fPixels = nullptr;
58 outLevel->fRowBytes = 0;
59 }
Brian Salomona3ffaab2019-07-09 12:26:46 -040060 return true;
61 }
Brian Salomona3ffaab2019-07-09 12:26:46 -040062 size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB;
63 if (actualRB < minRB) {
64 return false;
65 }
66 if (actualRB == minRB || rowBytesSupport) {
67 outLevel->fRowBytes = actualRB;
68 outLevel->fPixels = inLevel.fPixels;
69 } else {
70 data->reset(new char[minRB * h]);
71 outLevel->fPixels = data->get();
72 outLevel->fRowBytes = minRB;
73 SkRectMemcpy(data->get(), outLevel->fRowBytes, inLevel.fPixels, inLevel.fRowBytes, minRB,
74 h);
75 }
76 return true;
77}
78
Brian Salomonf2c2ba92019-07-17 09:59:59 -040079sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040080 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040081 GrRenderable renderable,
Brian Salomon2af3e702019-08-11 19:10:31 -040082 int renderTargetSampleCnt,
83 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040084 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -040085 const GrMipLevel texels[],
86 int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050087 ASSERT_SINGLE_OWNER
88
Robert Phillips7f1b4f82017-11-28 07:38:39 -050089 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040090
Brian Osman32342f02017-03-04 08:12:46 -050091 if (this->isAbandoned()) {
92 return nullptr;
93 }
Robert Phillips1119dc32017-04-11 12:54:57 -040094
Brian Salomonbdecacf2018-02-02 20:32:49 -050095 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -040096 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
97 renderTargetSampleCnt, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050098 return nullptr;
99 }
Brian Salomona3e29962019-07-16 11:52:08 -0400100 bool mustInitializeAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
101 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
Brian Salomon1047a492019-07-02 12:25:21 -0400102 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -0400103 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon1047a492019-07-02 12:25:21 -0400104 if (mipLevelCount > 0 && texels) {
105 tmpTexels.reset(mipLevelCount);
106 tmpDatas.reset(mipLevelCount);
107 int w = desc.fWidth;
108 int h = desc.fHeight;
109 size_t bpp = GrBytesPerPixel(desc.fConfig);
110 for (int i = 0; i < mipLevelCount; ++i) {
Brian Salomona3e29962019-07-16 11:52:08 -0400111 if (!prepare_level(texels[i], bpp, w, h, rowBytesSupport, mustInitializeAllLevels,
Brian Salomona3ffaab2019-07-09 12:26:46 -0400112 &tmpTexels[i], &tmpDatas[i])) {
113 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -0400114 }
115 w = std::max(w / 2, 1);
116 h = std::max(h / 2, 1);
117 }
118 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400119 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
120 isProtected, tmpTexels.get(), mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -0500121}
122
Robert Phillips45fdae12017-04-17 12:57:27 -0400123sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400124 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400125 GrRenderable renderable,
126 int renderTargetSampleCnt,
127 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400128 GrProtected isProtected) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400129 sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400130 isProtected));
Robert Phillips45fdae12017-04-17 12:57:27 -0400131 if (tex && SkBudgeted::kNo == budgeted) {
132 tex->resourcePriv().makeUnbudgeted();
133 }
134
135 return tex;
136}
137
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500138sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400140 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400141 int renderTargetSampleCnt,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500142 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500143 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400144 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -0400145 GrColorType srcColorType,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400146 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400147 ASSERT_SINGLE_OWNER
148
149 if (this->isAbandoned()) {
150 return nullptr;
151 }
152
Robert Phillips45fdae12017-04-17 12:57:27 -0400153 if (!mipLevel.fPixels) {
154 return nullptr;
155 }
156
Greg Daniel6fa62e22019-08-07 15:52:37 -0400157 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
158 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400159 return nullptr;
160 }
161
Robert Phillips45fdae12017-04-17 12:57:27 -0400162 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500163 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400164
Brian Salomona3e29962019-07-16 11:52:08 -0400165 bool mustInitialize = this->caps()->createTextureMustSpecifyAllLevels();
166 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
167
Brian Salomona3ffaab2019-07-09 12:26:46 -0400168 size_t bpp = GrBytesPerPixel(desc.fConfig);
169 std::unique_ptr<char[]> tmpData;
170 GrMipLevel tmpLevel;
Brian Salomona3e29962019-07-16 11:52:08 -0400171 if (!prepare_level(mipLevel, bpp, desc.fWidth, desc.fHeight, rowBytesSupport, mustInitialize,
172 &tmpLevel, &tmpData)) {
Brian Salomona3ffaab2019-07-09 12:26:46 -0400173 return nullptr;
174 }
175
Brian Salomone8a766b2019-07-19 14:24:36 -0400176 sk_sp<GrTexture> tex =
177 (SkBackingFit::kApprox == fit)
Brian Salomon4eb38b72019-08-05 12:58:39 -0400178 ? this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400179 isProtected)
Brian Salomon4eb38b72019-08-05 12:58:39 -0400180 : this->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400181 isProtected);
Brian Salomona495d6d2019-07-13 17:48:58 -0400182 if (!tex) {
183 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400184 }
Brian Salomona495d6d2019-07-13 17:48:58 -0400185
Brian Salomonbeb7f522019-08-30 16:19:42 -0400186 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(
187 tex, srcColorType, kTopLeft_GrSurfaceOrigin, GrSurfaceProxy::UseAllocator::kYes);
Brian Salomona495d6d2019-07-13 17:48:58 -0400188 if (!proxy) {
189 return nullptr;
190 }
191 // Here we don't really know the alpha type of the data we want to upload. All we really
192 // care about is that it is not converted. So we use the same alpha type for the data
193 // and the surface context.
Brian Salomon2af3e702019-08-11 19:10:31 -0400194 static constexpr auto kAlphaType = kUnpremul_SkAlphaType;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400195 auto sContext =
Brian Salomon2af3e702019-08-11 19:10:31 -0400196 context->priv().makeWrappedSurfaceContext(std::move(proxy), srcColorType, kAlphaType);
Brian Salomona495d6d2019-07-13 17:48:58 -0400197 if (!sContext) {
198 return nullptr;
199 }
Brian Salomon2af3e702019-08-11 19:10:31 -0400200 GrPixelInfo srcInfo(srcColorType, kAlphaType, nullptr, desc.fWidth, desc.fHeight);
Brian Salomona495d6d2019-07-13 17:48:58 -0400201 SkAssertResult(sContext->writePixels(srcInfo, tmpLevel.fPixels, tmpLevel.fRowBytes, {0, 0}));
202 return tex;
Robert Phillips45fdae12017-04-17 12:57:27 -0400203}
204
Brian Salomonbb8dde82019-06-27 10:52:13 -0400205sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400206 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400207 SkImage::CompressionType compression,
208 SkBudgeted budgeted, SkData* data) {
209 ASSERT_SINGLE_OWNER
210 if (this->isAbandoned()) {
211 return nullptr;
212 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400213 return fGpu->createCompressedTexture(width, height, format, compression, budgeted, data->data(),
Brian Salomonbb8dde82019-06-27 10:52:13 -0400214 data->size());
215}
216
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400217sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400218 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400219 GrRenderable renderable,
220 int renderTargetSampleCnt,
221 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400222 GrProtected isProtected) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400223 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400224 if (this->isAbandoned()) {
225 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500226 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400227
Greg Daniel6fa62e22019-08-07 15:52:37 -0400228 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
229 renderTargetSampleCnt, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400230 return nullptr;
231 }
232
Jim Van Verth1676cb92019-01-15 13:24:45 -0500233 // Compressed textures are read-only so they don't support re-use for scratch.
234 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400235 sk_sp<GrTexture> tex = this->getExactScratch(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400236 desc, format, renderable, renderTargetSampleCnt, budgeted, isProtected);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500237 if (tex) {
238 return tex;
239 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400240 }
241
Brian Salomona3e29962019-07-16 11:52:08 -0400242 if (fCaps->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000243 size_t rowBytes = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
244 size_t size = rowBytes * desc.fHeight;
245 std::unique_ptr<char[]> zeros(new char[size]());
246 GrMipLevel level;
247 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400248 level.fPixels = zeros.get();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400249 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
250 isProtected, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400251 }
252
Brian Salomon4eb38b72019-08-05 12:58:39 -0400253 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
254 isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500255}
256
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400257// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
258// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
259uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
260 static const int kMagicTol = 1024;
261
262 value = SkTMax(kMinScratchTextureSize, value);
263
264 if (SkIsPow2(value)) {
265 return value;
266 }
267
268 uint32_t ceilPow2 = GrNextPow2(value);
269 if (value <= kMagicTol) {
270 return ceilPow2;
271 }
272
273 uint32_t floorPow2 = ceilPow2 >> 1;
274 uint32_t mid = floorPow2 + (floorPow2 >> 1);
275
276 if (value <= mid) {
277 return mid;
278 }
279
280 return ceilPow2;
281}
282
Robert Phillips67d52cf2017-06-05 13:38:13 -0400283sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400284 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400285 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400286 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400287 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500288 ASSERT_SINGLE_OWNER
Brian Osman32342f02017-03-04 08:12:46 -0500289
Brian Osman32342f02017-03-04 08:12:46 -0500290 if (this->isAbandoned()) {
291 return nullptr;
292 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400293
Jim Van Verth1676cb92019-01-15 13:24:45 -0500294 // Currently we don't recycle compressed textures as scratch.
295 if (GrPixelConfigIsCompressed(desc.fConfig)) {
296 return nullptr;
297 }
298
Greg Daniel6fa62e22019-08-07 15:52:37 -0400299 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
300 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400301 return nullptr;
302 }
303
Brian Salomon4eb38b72019-08-05 12:58:39 -0400304 if (auto tex = this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400305 isProtected)) {
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400306 return tex;
307 }
308
Greg Daniel29bf84f2017-09-25 12:25:12 -0400309 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
310
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400311 // bin by some multiple or power of 2 with a reasonable min
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400312 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400313 GrSurfaceDesc* wdesc = copyDesc.writable();
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400314 wdesc->fWidth = MakeApprox(wdesc->fWidth);
315 wdesc->fHeight = MakeApprox(wdesc->fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400316 }
317
Brian Salomon4eb38b72019-08-05 12:58:39 -0400318 if (auto tex = this->refScratchTexture(*copyDesc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400319 isProtected)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400320 return tex;
321 }
322
Brian Salomona3e29962019-07-16 11:52:08 -0400323 if (this->caps()->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000324 size_t rowBytes = GrBytesPerPixel(copyDesc->fConfig) * copyDesc->fWidth;
325 size_t size = rowBytes * copyDesc->fHeight;
326 std::unique_ptr<char[]> zeros(new char[size]());
327 GrMipLevel level;
328 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400329 level.fPixels = zeros.get();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400330 return fGpu->createTexture(*copyDesc, format, renderable, renderTargetSampleCnt,
331 SkBudgeted::kYes, isProtected, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400332 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400333 return fGpu->createTexture(*copyDesc, format, renderable, renderTargetSampleCnt,
334 SkBudgeted::kYes, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500335}
336
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400337sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400338 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400339 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400340 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400341 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500342 ASSERT_SINGLE_OWNER
343 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500344 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Greg Daniel6fa62e22019-08-07 15:52:37 -0400345 SkASSERT(fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
346 renderable, renderTargetSampleCnt, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500347
Brian Salomond17b4a62017-05-23 16:53:47 -0400348 // We could make initial clears work with scratch textures but it is a rare case so we just opt
349 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400350 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500351 GrScratchKey key;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400352 GrTexturePriv::ComputeScratchKey(desc, renderable, renderTargetSampleCnt, &key);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400353 GrGpuResource* resource = fCache->findAndRefScratchResource(key);
Brian Osman32342f02017-03-04 08:12:46 -0500354 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400355 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500356 GrSurface* surface = static_cast<GrSurface*>(resource);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600357 if (GrRenderTarget* rt = surface->asRenderTarget()) {
358 rt->flagAsResolved(); // Scratch textures always start without dirty MSAA.
359 }
Robert Phillips67d52cf2017-06-05 13:38:13 -0400360 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500361 }
362 }
363
Brian Osman32342f02017-03-04 08:12:46 -0500364 return nullptr;
365}
366
Greg Daniel7ef28f32017-04-20 16:41:55 +0000367sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400368 GrColorType colorType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500369 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500370 GrWrapCacheable cacheable,
371 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500372 ASSERT_SINGLE_OWNER
373 if (this->isAbandoned()) {
374 return nullptr;
375 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400376 return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400377}
378
379sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400380 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400381 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500382 GrWrapOwnership ownership,
383 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400384 ASSERT_SINGLE_OWNER
385 if (this->isAbandoned()) {
386 return nullptr;
387 }
Robert Phillips0902c982019-07-16 07:47:56 -0400388 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500389}
390
391sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400392 const GrBackendRenderTarget& backendRT, GrColorType colorType)
Brian Osman32342f02017-03-04 08:12:46 -0500393{
394 ASSERT_SINGLE_OWNER
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400395 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500396}
397
Greg Danielb46add82019-01-02 14:51:29 -0500398sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
399 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
400 ASSERT_SINGLE_OWNER
401 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
402 vkInfo);
403
404}
405
Brian Osman32342f02017-03-04 08:12:46 -0500406void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
407 GrGpuResource* resource) {
408 ASSERT_SINGLE_OWNER
409 if (this->isAbandoned() || !resource) {
410 return;
411 }
412 resource->resourcePriv().setUniqueKey(key);
413}
414
Brian Salomond28a79d2017-10-16 13:01:07 -0400415sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500416 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400417 return this->isAbandoned() ? nullptr
418 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500419}
420
Brian Salomondbf70722019-02-07 11:31:24 -0500421sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
422 size_t size,
423 const void* data,
424 const GrUniqueKey& key) {
425 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400426 return buffer;
Chris Dalton5d2de082017-12-19 10:40:23 -0700427 }
Brian Salomondbf70722019-02-07 11:31:24 -0500428 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500429 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500430 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700431 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
Chris Dalton5d2de082017-12-19 10:40:23 -0700432 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500433 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700434 }
435 return nullptr;
436}
437
Brian Salomondbf70722019-02-07 11:31:24 -0500438sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
439 int patternSize,
440 int reps,
441 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500442 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700443 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
444
Brian Salomondbf70722019-02-07 11:31:24 -0500445 sk_sp<GrGpuBuffer> buffer(
446 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700447 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700448 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700449 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400450 uint16_t* data = (uint16_t*) buffer->map();
451 SkAutoTArray<uint16_t> temp;
452 if (!data) {
453 temp.reset(reps * patternSize);
454 data = temp.get();
455 }
bsalomoned0bcad2015-05-04 10:36:42 -0700456 for (int i = 0; i < reps; ++i) {
457 int baseIdx = i * patternSize;
458 uint16_t baseVert = (uint16_t)(i * vertCount);
459 for (int j = 0; j < patternSize; ++j) {
460 data[baseIdx+j] = baseVert + pattern[j];
461 }
462 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400463 if (temp.get()) {
464 if (!buffer->updateData(data, bufferSize)) {
465 return nullptr;
466 }
467 } else {
468 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700469 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500470 if (key) {
471 SkASSERT(key->isValid());
472 this->assignUniqueKeyToResource(*key, buffer.get());
473 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400474 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700475}
476
Brian Salomon34169692017-08-28 15:32:01 -0400477static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
478
Brian Salomondbf70722019-02-07 11:31:24 -0500479sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700480 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000481 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500482 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700483}
484
Brian Salomon763abf02018-05-01 18:49:38 +0000485int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400486
Robert Phillips67d52cf2017-06-05 13:38:13 -0400487sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400488 if (this->isAbandoned()) {
489 return nullptr;
490 }
491
bsalomon706f08f2015-05-22 07:35:58 -0700492 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700493 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700494}
495
Brian Salomondbf70722019-02-07 11:31:24 -0500496sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
497 GrAccessPattern accessPattern,
498 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700499 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700500 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700501 }
cdaltond37fe762016-04-21 07:41:50 -0700502 if (kDynamic_GrAccessPattern != accessPattern) {
503 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
504 }
cdaltond37fe762016-04-21 07:41:50 -0700505 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400506 static const size_t MIN_SIZE = 1 << 12;
507 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700508
cdaltond37fe762016-04-21 07:41:50 -0700509 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500510 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
511 auto buffer =
512 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400513 key)));
cdaltond37fe762016-04-21 07:41:50 -0700514 if (!buffer) {
515 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
516 if (!buffer) {
517 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700518 }
519 }
cdaltond37fe762016-04-21 07:41:50 -0700520 if (data) {
521 buffer->updateData(data, size);
522 }
523 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800524}
525
Chris Daltoneffee202019-07-01 22:28:03 -0600526bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700527 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600528 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
529 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400530 return true;
egdanielec00d942015-09-14 12:56:10 -0700531 }
532
533 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
534 GrUniqueKey sbKey;
535
536 int width = rt->width();
537 int height = rt->height();
538#if 0
539 if (this->caps()->oversizedStencilSupport()) {
540 width = SkNextPow2(width);
541 height = SkNextPow2(height);
542 }
543#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600544 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
545 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400546 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700547 if (!stencil) {
548 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600549 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
550 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400551 if (!stencil) {
552 return false;
egdanielec00d942015-09-14 12:56:10 -0700553 }
Robert Phillips01a91282018-07-26 08:03:04 -0400554 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700555 }
Greg Danielcfa39352018-10-05 12:01:59 -0400556 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700557 }
Chris Dalton215ff332019-07-02 09:38:22 -0600558
559 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
560 return stencil->numSamples() >= minStencilSampleCount;
561 }
562 return false;
egdanielec00d942015-09-14 12:56:10 -0700563}
564
bungeman6bd52842016-10-27 09:30:08 -0700565sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400566 const GrBackendTexture& tex, int sampleCnt, GrColorType colorType)
bungeman6bd52842016-10-27 09:30:08 -0700567{
ericrkf7b8b8a2016-02-24 14:49:51 -0800568 if (this->isAbandoned()) {
569 return nullptr;
570 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400571 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800572}
Greg Danield85f97d2017-03-07 13:37:21 -0500573
Greg Daniela5cb7812017-06-16 09:45:32 -0400574sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
575 return fGpu->makeSemaphore(isOwned);
576}
577
578sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500579 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400580 GrWrapOwnership ownership) {
581 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500582 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
583 wrapType,
584 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500585}