blob: ed3565bfb4f9374886e5a14c7aa146afc84135ee [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,
80 GrRenderable renderable, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040081 GrProtected isProtected,
Brian Osman2b23c4b2018-06-01 12:25:08 -040082 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050083 ASSERT_SINGLE_OWNER
84
Robert Phillips7f1b4f82017-11-28 07:38:39 -050085 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040086
Brian Osman32342f02017-03-04 08:12:46 -050087 if (this->isAbandoned()) {
88 return nullptr;
89 }
Robert Phillips1119dc32017-04-11 12:54:57 -040090
Brian Salomonbdecacf2018-02-02 20:32:49 -050091 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040092 if (!fCaps->validateSurfaceDesc(desc, renderable, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050093 return nullptr;
94 }
Brian Salomona3e29962019-07-16 11:52:08 -040095 bool mustInitializeAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
96 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
Brian Salomon1047a492019-07-02 12:25:21 -040097 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -040098 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon1047a492019-07-02 12:25:21 -040099 if (mipLevelCount > 0 && texels) {
100 tmpTexels.reset(mipLevelCount);
101 tmpDatas.reset(mipLevelCount);
102 int w = desc.fWidth;
103 int h = desc.fHeight;
104 size_t bpp = GrBytesPerPixel(desc.fConfig);
105 for (int i = 0; i < mipLevelCount; ++i) {
Brian Salomona3e29962019-07-16 11:52:08 -0400106 if (!prepare_level(texels[i], bpp, w, h, rowBytesSupport, mustInitializeAllLevels,
Brian Salomona3ffaab2019-07-09 12:26:46 -0400107 &tmpTexels[i], &tmpDatas[i])) {
108 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -0400109 }
110 w = std::max(w / 2, 1);
111 h = std::max(h / 2, 1);
112 }
113 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400114 return fGpu->createTexture(desc, renderable, budgeted, isProtected, tmpTexels.get(),
115 mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -0500116}
117
Robert Phillips45fdae12017-04-17 12:57:27 -0400118sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400119 GrRenderable renderable, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400120 GrProtected isProtected, Flags flags) {
121 sk_sp<GrTexture> tex(this->refScratchTexture(desc, renderable, isProtected, flags));
Robert Phillips45fdae12017-04-17 12:57:27 -0400122 if (tex && SkBudgeted::kNo == budgeted) {
123 tex->resourcePriv().makeUnbudgeted();
124 }
125
126 return tex;
127}
128
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500129sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400130 GrRenderable renderable,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500131 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500132 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400133 GrProtected isProtected,
Chris Daltond004e0b2018-09-27 09:28:03 -0600134 const GrMipLevel& mipLevel,
135 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -0400136 ASSERT_SINGLE_OWNER
137
138 if (this->isAbandoned()) {
139 return nullptr;
140 }
141
Robert Phillips45fdae12017-04-17 12:57:27 -0400142 if (!mipLevel.fPixels) {
143 return nullptr;
144 }
145
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400146 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400147 return nullptr;
148 }
149
Robert Phillips45fdae12017-04-17 12:57:27 -0400150 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500151 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400152
Brian Salomona3e29962019-07-16 11:52:08 -0400153 bool mustInitialize = this->caps()->createTextureMustSpecifyAllLevels();
154 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
155
Brian Salomona3ffaab2019-07-09 12:26:46 -0400156 size_t bpp = GrBytesPerPixel(desc.fConfig);
157 std::unique_ptr<char[]> tmpData;
158 GrMipLevel tmpLevel;
Brian Salomona3e29962019-07-16 11:52:08 -0400159 if (!prepare_level(mipLevel, bpp, desc.fWidth, desc.fHeight, rowBytesSupport, mustInitialize,
160 &tmpLevel, &tmpData)) {
Brian Salomona3ffaab2019-07-09 12:26:46 -0400161 return nullptr;
162 }
163
Brian Salomona495d6d2019-07-13 17:48:58 -0400164 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
Brian Salomone8a766b2019-07-19 14:24:36 -0400165 sk_sp<GrTexture> tex =
166 (SkBackingFit::kApprox == fit)
167 ? this->createApproxTexture(desc, renderable, isProtected, flags)
168 : this->createTexture(desc, renderable, budgeted, isProtected, flags);
Brian Salomona495d6d2019-07-13 17:48:58 -0400169 if (!tex) {
170 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400171 }
Brian Salomona495d6d2019-07-13 17:48:58 -0400172
173 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(tex, kTopLeft_GrSurfaceOrigin);
174 if (!proxy) {
175 return nullptr;
176 }
177 // Here we don't really know the alpha type of the data we want to upload. All we really
178 // care about is that it is not converted. So we use the same alpha type for the data
179 // and the surface context.
180 static constexpr auto kAlphaType = kPremul_SkAlphaType;
181 sk_sp<GrSurfaceContext> sContext =
182 context->priv().makeWrappedSurfaceContext(std::move(proxy), colorType, kAlphaType);
183 if (!sContext) {
184 return nullptr;
185 }
186 GrPixelInfo srcInfo(colorType, kAlphaType, nullptr, desc.fWidth, desc.fHeight);
187 SkAssertResult(sContext->writePixels(srcInfo, tmpLevel.fPixels, tmpLevel.fRowBytes, {0, 0}));
188 return tex;
Robert Phillips45fdae12017-04-17 12:57:27 -0400189}
190
Brian Salomonbb8dde82019-06-27 10:52:13 -0400191sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
192 SkImage::CompressionType compression,
193 SkBudgeted budgeted, SkData* data) {
194 ASSERT_SINGLE_OWNER
195 if (this->isAbandoned()) {
196 return nullptr;
197 }
198 return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(),
199 data->size());
200}
201
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400202sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
203 GrRenderable renderable, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400204 GrProtected isProtected, Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400205 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400206 if (this->isAbandoned()) {
207 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500208 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400209
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400210 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400211 return nullptr;
212 }
213
Jim Van Verth1676cb92019-01-15 13:24:45 -0500214 // Compressed textures are read-only so they don't support re-use for scratch.
215 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomone8a766b2019-07-19 14:24:36 -0400216 sk_sp<GrTexture> tex =
217 this->getExactScratch(desc, renderable, budgeted, isProtected, flags);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500218 if (tex) {
219 return tex;
220 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400221 }
222
Brian Salomona3e29962019-07-16 11:52:08 -0400223 if (fCaps->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000224 size_t rowBytes = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
225 size_t size = rowBytes * desc.fHeight;
226 std::unique_ptr<char[]> zeros(new char[size]());
227 GrMipLevel level;
228 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400229 level.fPixels = zeros.get();
Brian Salomone8a766b2019-07-19 14:24:36 -0400230 return fGpu->createTexture(desc, renderable, budgeted, isProtected, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400231 }
232
Brian Salomone8a766b2019-07-19 14:24:36 -0400233 return fGpu->createTexture(desc, renderable, budgeted, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500234}
235
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400236// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
237// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
238uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
239 static const int kMagicTol = 1024;
240
241 value = SkTMax(kMinScratchTextureSize, value);
242
243 if (SkIsPow2(value)) {
244 return value;
245 }
246
247 uint32_t ceilPow2 = GrNextPow2(value);
248 if (value <= kMagicTol) {
249 return ceilPow2;
250 }
251
252 uint32_t floorPow2 = ceilPow2 >> 1;
253 uint32_t mid = floorPow2 + (floorPow2 >> 1);
254
255 if (value <= mid) {
256 return mid;
257 }
258
259 return ceilPow2;
260}
261
Robert Phillips67d52cf2017-06-05 13:38:13 -0400262sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomone8a766b2019-07-19 14:24:36 -0400263 GrRenderable renderable,
264 GrProtected isProtected, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500265 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600266 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500267
Brian Osman32342f02017-03-04 08:12:46 -0500268 if (this->isAbandoned()) {
269 return nullptr;
270 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400271
Jim Van Verth1676cb92019-01-15 13:24:45 -0500272 // Currently we don't recycle compressed textures as scratch.
273 if (GrPixelConfigIsCompressed(desc.fConfig)) {
274 return nullptr;
275 }
276
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400277 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400278 return nullptr;
279 }
280
Brian Salomone8a766b2019-07-19 14:24:36 -0400281 if (auto tex = this->refScratchTexture(desc, renderable, isProtected, flags)) {
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400282 return tex;
283 }
284
Greg Daniel29bf84f2017-09-25 12:25:12 -0400285 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
286
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400287 // bin by some multiple or power of 2 with a reasonable min
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400288 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400289 GrSurfaceDesc* wdesc = copyDesc.writable();
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400290 wdesc->fWidth = MakeApprox(wdesc->fWidth);
291 wdesc->fHeight = MakeApprox(wdesc->fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400292 }
293
Brian Salomone8a766b2019-07-19 14:24:36 -0400294 if (auto tex = this->refScratchTexture(*copyDesc, renderable, isProtected, flags)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400295 return tex;
296 }
297
Brian Salomona3e29962019-07-16 11:52:08 -0400298 if (this->caps()->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000299 size_t rowBytes = GrBytesPerPixel(copyDesc->fConfig) * copyDesc->fWidth;
300 size_t size = rowBytes * copyDesc->fHeight;
301 std::unique_ptr<char[]> zeros(new char[size]());
302 GrMipLevel level;
303 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400304 level.fPixels = zeros.get();
Brian Salomone8a766b2019-07-19 14:24:36 -0400305 return fGpu->createTexture(*copyDesc, renderable, SkBudgeted::kYes, isProtected, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400306 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400307 return fGpu->createTexture(*copyDesc, renderable, SkBudgeted::kYes, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500308}
309
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400310sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomone8a766b2019-07-19 14:24:36 -0400311 GrRenderable renderable,
312 GrProtected isProtected, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500313 ASSERT_SINGLE_OWNER
314 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500315 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400316 SkASSERT(fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500317
Brian Salomond17b4a62017-05-23 16:53:47 -0400318 // We could make initial clears work with scratch textures but it is a rare case so we just opt
319 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400320 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500321 GrScratchKey key;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400322 GrTexturePriv::ComputeScratchKey(desc, renderable, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600323 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
324 if (Flags::kNoPendingIO & flags) {
325 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400326 } else if (renderable == GrRenderable::kNo) {
Brian Osman32342f02017-03-04 08:12:46 -0500327 // If it is not a render target then it will most likely be populated by
328 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600329 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500330 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400331 GrGpuResource* resource = fCache->findAndRefScratchResource(
332 key, GrSurface::WorstCaseSize(desc, renderable), scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500333 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400334 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500335 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400336 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500337 }
338 }
339
Brian Osman32342f02017-03-04 08:12:46 -0500340 return nullptr;
341}
342
Greg Daniel7ef28f32017-04-20 16:41:55 +0000343sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500344 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500345 GrWrapCacheable cacheable,
346 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500347 ASSERT_SINGLE_OWNER
348 if (this->isAbandoned()) {
349 return nullptr;
350 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000351 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400352}
353
354sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400355 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400356 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500357 GrWrapOwnership ownership,
358 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400359 ASSERT_SINGLE_OWNER
360 if (this->isAbandoned()) {
361 return nullptr;
362 }
Robert Phillips0902c982019-07-16 07:47:56 -0400363 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500364}
365
366sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000367 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500368{
369 ASSERT_SINGLE_OWNER
Robert Phillipsdd399802019-07-18 12:28:00 +0000370 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500371}
372
Greg Danielb46add82019-01-02 14:51:29 -0500373sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
374 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
375 ASSERT_SINGLE_OWNER
376 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
377 vkInfo);
378
379}
380
Brian Osman32342f02017-03-04 08:12:46 -0500381void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
382 GrGpuResource* resource) {
383 ASSERT_SINGLE_OWNER
384 if (this->isAbandoned() || !resource) {
385 return;
386 }
387 resource->resourcePriv().setUniqueKey(key);
388}
389
Brian Salomond28a79d2017-10-16 13:01:07 -0400390sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500391 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400392 return this->isAbandoned() ? nullptr
393 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500394}
395
Brian Salomondbf70722019-02-07 11:31:24 -0500396sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
397 size_t size,
398 const void* data,
399 const GrUniqueKey& key) {
400 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400401 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700402 }
Brian Salomondbf70722019-02-07 11:31:24 -0500403 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500404 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500405 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700406 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
407 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
408 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500409 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700410 }
411 return nullptr;
412}
413
Brian Salomondbf70722019-02-07 11:31:24 -0500414sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
415 int patternSize,
416 int reps,
417 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500418 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700419 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
420
Brian Salomon09d994e2016-12-21 11:14:46 -0500421 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500422 sk_sp<GrGpuBuffer> buffer(
423 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700424 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700425 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700426 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400427 uint16_t* data = (uint16_t*) buffer->map();
428 SkAutoTArray<uint16_t> temp;
429 if (!data) {
430 temp.reset(reps * patternSize);
431 data = temp.get();
432 }
bsalomoned0bcad2015-05-04 10:36:42 -0700433 for (int i = 0; i < reps; ++i) {
434 int baseIdx = i * patternSize;
435 uint16_t baseVert = (uint16_t)(i * vertCount);
436 for (int j = 0; j < patternSize; ++j) {
437 data[baseIdx+j] = baseVert + pattern[j];
438 }
439 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400440 if (temp.get()) {
441 if (!buffer->updateData(data, bufferSize)) {
442 return nullptr;
443 }
444 } else {
445 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700446 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500447 if (key) {
448 SkASSERT(key->isValid());
449 this->assignUniqueKeyToResource(*key, buffer.get());
450 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400451 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700452}
453
Brian Salomon34169692017-08-28 15:32:01 -0400454static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
455
Brian Salomondbf70722019-02-07 11:31:24 -0500456sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700457 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000458 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500459 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700460}
461
Brian Salomon763abf02018-05-01 18:49:38 +0000462int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400463
Robert Phillips67d52cf2017-06-05 13:38:13 -0400464sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400465 if (this->isAbandoned()) {
466 return nullptr;
467 }
468
bsalomon706f08f2015-05-22 07:35:58 -0700469 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700470 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700471}
472
Brian Salomondbf70722019-02-07 11:31:24 -0500473sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
474 GrAccessPattern accessPattern,
475 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700476 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700477 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700478 }
cdaltond37fe762016-04-21 07:41:50 -0700479 if (kDynamic_GrAccessPattern != accessPattern) {
480 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
481 }
cdaltond37fe762016-04-21 07:41:50 -0700482 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400483 static const size_t MIN_SIZE = 1 << 12;
484 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700485
cdaltond37fe762016-04-21 07:41:50 -0700486 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500487 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
488 auto buffer =
489 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
490 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700491 if (!buffer) {
492 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
493 if (!buffer) {
494 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700495 }
496 }
cdaltond37fe762016-04-21 07:41:50 -0700497 if (data) {
498 buffer->updateData(data, size);
499 }
500 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800501}
502
Chris Daltoneffee202019-07-01 22:28:03 -0600503bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700504 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600505 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
506 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400507 return true;
egdanielec00d942015-09-14 12:56:10 -0700508 }
509
510 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
511 GrUniqueKey sbKey;
512
513 int width = rt->width();
514 int height = rt->height();
515#if 0
516 if (this->caps()->oversizedStencilSupport()) {
517 width = SkNextPow2(width);
518 height = SkNextPow2(height);
519 }
520#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600521 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
522 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400523 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700524 if (!stencil) {
525 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600526 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
527 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400528 if (!stencil) {
529 return false;
egdanielec00d942015-09-14 12:56:10 -0700530 }
Robert Phillips01a91282018-07-26 08:03:04 -0400531 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700532 }
Greg Danielcfa39352018-10-05 12:01:59 -0400533 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700534 }
Chris Dalton215ff332019-07-02 09:38:22 -0600535
536 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
537 return stencil->numSamples() >= minStencilSampleCount;
538 }
539 return false;
egdanielec00d942015-09-14 12:56:10 -0700540}
541
bungeman6bd52842016-10-27 09:30:08 -0700542sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000543 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700544{
ericrkf7b8b8a2016-02-24 14:49:51 -0800545 if (this->isAbandoned()) {
546 return nullptr;
547 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000548 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800549}
Greg Danield85f97d2017-03-07 13:37:21 -0500550
Greg Daniela5cb7812017-06-16 09:45:32 -0400551sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
552 return fGpu->makeSemaphore(isOwned);
553}
554
555sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500556 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400557 GrWrapOwnership ownership) {
558 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500559 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
560 wrapType,
561 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500562}