blob: b80f9f1ca8daec627003f2a85cfc5e7ac488b729 [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 Osman2b23c4b2018-06-01 12:25:08 -040081 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050082 ASSERT_SINGLE_OWNER
83
Robert Phillips7f1b4f82017-11-28 07:38:39 -050084 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040085
Brian Osman32342f02017-03-04 08:12:46 -050086 if (this->isAbandoned()) {
87 return nullptr;
88 }
Robert Phillips1119dc32017-04-11 12:54:57 -040089
Brian Salomonbdecacf2018-02-02 20:32:49 -050090 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040091 if (!fCaps->validateSurfaceDesc(desc, renderable, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050092 return nullptr;
93 }
Brian Salomona3e29962019-07-16 11:52:08 -040094 bool mustInitializeAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
95 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
Brian Salomon1047a492019-07-02 12:25:21 -040096 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -040097 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon1047a492019-07-02 12:25:21 -040098 if (mipLevelCount > 0 && texels) {
99 tmpTexels.reset(mipLevelCount);
100 tmpDatas.reset(mipLevelCount);
101 int w = desc.fWidth;
102 int h = desc.fHeight;
103 size_t bpp = GrBytesPerPixel(desc.fConfig);
104 for (int i = 0; i < mipLevelCount; ++i) {
Brian Salomona3e29962019-07-16 11:52:08 -0400105 if (!prepare_level(texels[i], bpp, w, h, rowBytesSupport, mustInitializeAllLevels,
Brian Salomona3ffaab2019-07-09 12:26:46 -0400106 &tmpTexels[i], &tmpDatas[i])) {
107 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -0400108 }
109 w = std::max(w / 2, 1);
110 h = std::max(h / 2, 1);
111 }
112 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400113 return fGpu->createTexture(desc, renderable, budgeted, tmpTexels.get(), mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -0500114}
115
Robert Phillips45fdae12017-04-17 12:57:27 -0400116sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400117 GrRenderable renderable, SkBudgeted budgeted,
118 Flags flags) {
119 sk_sp<GrTexture> tex(this->refScratchTexture(desc, renderable, flags));
Robert Phillips45fdae12017-04-17 12:57:27 -0400120 if (tex && SkBudgeted::kNo == budgeted) {
121 tex->resourcePriv().makeUnbudgeted();
122 }
123
124 return tex;
125}
126
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500127sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400128 GrRenderable renderable,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500129 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500130 SkBackingFit fit,
Chris Daltond004e0b2018-09-27 09:28:03 -0600131 const GrMipLevel& mipLevel,
132 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -0400133 ASSERT_SINGLE_OWNER
134
135 if (this->isAbandoned()) {
136 return nullptr;
137 }
138
Robert Phillips45fdae12017-04-17 12:57:27 -0400139 if (!mipLevel.fPixels) {
140 return nullptr;
141 }
142
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400143 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400144 return nullptr;
145 }
146
Robert Phillips45fdae12017-04-17 12:57:27 -0400147 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500148 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400149
Brian Salomona3e29962019-07-16 11:52:08 -0400150 bool mustInitialize = this->caps()->createTextureMustSpecifyAllLevels();
151 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
152
Brian Salomona3ffaab2019-07-09 12:26:46 -0400153 size_t bpp = GrBytesPerPixel(desc.fConfig);
154 std::unique_ptr<char[]> tmpData;
155 GrMipLevel tmpLevel;
Brian Salomona3e29962019-07-16 11:52:08 -0400156 if (!prepare_level(mipLevel, bpp, desc.fWidth, desc.fHeight, rowBytesSupport, mustInitialize,
157 &tmpLevel, &tmpData)) {
Brian Salomona3ffaab2019-07-09 12:26:46 -0400158 return nullptr;
159 }
160
Brian Salomona495d6d2019-07-13 17:48:58 -0400161 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
162 sk_sp<GrTexture> tex = (SkBackingFit::kApprox == fit)
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400163 ? this->createApproxTexture(desc, renderable, flags)
164 : this->createTexture(desc, renderable, budgeted, flags);
Brian Salomona495d6d2019-07-13 17:48:58 -0400165 if (!tex) {
166 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400167 }
Brian Salomona495d6d2019-07-13 17:48:58 -0400168
169 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(tex, kTopLeft_GrSurfaceOrigin);
170 if (!proxy) {
171 return nullptr;
172 }
173 // Here we don't really know the alpha type of the data we want to upload. All we really
174 // care about is that it is not converted. So we use the same alpha type for the data
175 // and the surface context.
176 static constexpr auto kAlphaType = kPremul_SkAlphaType;
177 sk_sp<GrSurfaceContext> sContext =
178 context->priv().makeWrappedSurfaceContext(std::move(proxy), colorType, kAlphaType);
179 if (!sContext) {
180 return nullptr;
181 }
182 GrPixelInfo srcInfo(colorType, kAlphaType, nullptr, desc.fWidth, desc.fHeight);
183 SkAssertResult(sContext->writePixels(srcInfo, tmpLevel.fPixels, tmpLevel.fRowBytes, {0, 0}));
184 return tex;
Robert Phillips45fdae12017-04-17 12:57:27 -0400185}
186
Brian Salomonbb8dde82019-06-27 10:52:13 -0400187sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
188 SkImage::CompressionType compression,
189 SkBudgeted budgeted, SkData* data) {
190 ASSERT_SINGLE_OWNER
191 if (this->isAbandoned()) {
192 return nullptr;
193 }
194 return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(),
195 data->size());
196}
197
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400198sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
199 GrRenderable renderable, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600200 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400201 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400202 if (this->isAbandoned()) {
203 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500204 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400205
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400206 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400207 return nullptr;
208 }
209
Jim Van Verth1676cb92019-01-15 13:24:45 -0500210 // Compressed textures are read-only so they don't support re-use for scratch.
211 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400212 sk_sp<GrTexture> tex = this->getExactScratch(desc, renderable, budgeted, flags);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500213 if (tex) {
214 return tex;
215 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400216 }
217
Brian Salomona3e29962019-07-16 11:52:08 -0400218 if (fCaps->createTextureMustSpecifyAllLevels()) {
219 size_t rowBytes = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
220 size_t size = rowBytes * desc.fHeight;
221 std::unique_ptr<char[]> zeros(new char[size]());
222 GrMipLevel level;
223 level.fRowBytes = rowBytes;
224 level.fPixels = zeros.get();
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400225 return fGpu->createTexture(desc, renderable, budgeted, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400226 }
227
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400228 return fGpu->createTexture(desc, renderable, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500229}
230
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400231// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
232// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
233uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
234 static const int kMagicTol = 1024;
235
236 value = SkTMax(kMinScratchTextureSize, value);
237
238 if (SkIsPow2(value)) {
239 return value;
240 }
241
242 uint32_t ceilPow2 = GrNextPow2(value);
243 if (value <= kMagicTol) {
244 return ceilPow2;
245 }
246
247 uint32_t floorPow2 = ceilPow2 >> 1;
248 uint32_t mid = floorPow2 + (floorPow2 >> 1);
249
250 if (value <= mid) {
251 return mid;
252 }
253
254 return ceilPow2;
255}
256
Robert Phillips67d52cf2017-06-05 13:38:13 -0400257sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400258 GrRenderable renderable, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500259 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600260 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500261
Brian Osman32342f02017-03-04 08:12:46 -0500262 if (this->isAbandoned()) {
263 return nullptr;
264 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400265
Jim Van Verth1676cb92019-01-15 13:24:45 -0500266 // Currently we don't recycle compressed textures as scratch.
267 if (GrPixelConfigIsCompressed(desc.fConfig)) {
268 return nullptr;
269 }
270
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400271 if (!fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400272 return nullptr;
273 }
274
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400275 if (auto tex = this->refScratchTexture(desc, renderable, flags)) {
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400276 return tex;
277 }
278
Greg Daniel29bf84f2017-09-25 12:25:12 -0400279 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
280
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400281 // bin by some multiple or power of 2 with a reasonable min
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400282 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400283 GrSurfaceDesc* wdesc = copyDesc.writable();
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400284 wdesc->fWidth = MakeApprox(wdesc->fWidth);
285 wdesc->fHeight = MakeApprox(wdesc->fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400286 }
287
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400288 if (auto tex = this->refScratchTexture(*copyDesc, renderable, flags)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400289 return tex;
290 }
291
Brian Salomona3e29962019-07-16 11:52:08 -0400292 if (this->caps()->createTextureMustSpecifyAllLevels()) {
293 size_t rowBytes = GrBytesPerPixel(copyDesc->fConfig) * copyDesc->fWidth;
294 size_t size = rowBytes * copyDesc->fHeight;
295 std::unique_ptr<char[]> zeros(new char[size]());
296 GrMipLevel level;
297 level.fRowBytes = rowBytes;
298 level.fPixels = zeros.get();
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400299 return fGpu->createTexture(*copyDesc, renderable, SkBudgeted::kYes, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400300 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400301 return fGpu->createTexture(*copyDesc, renderable, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500302}
303
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400304sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
305 GrRenderable renderable, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500306 ASSERT_SINGLE_OWNER
307 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500308 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400309 SkASSERT(fCaps->validateSurfaceDesc(desc, renderable, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500310
Brian Salomond17b4a62017-05-23 16:53:47 -0400311 // We could make initial clears work with scratch textures but it is a rare case so we just opt
312 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400313 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500314 GrScratchKey key;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400315 GrTexturePriv::ComputeScratchKey(desc, renderable, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600316 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
317 if (Flags::kNoPendingIO & flags) {
318 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400319 } else if (renderable == GrRenderable::kNo) {
Brian Osman32342f02017-03-04 08:12:46 -0500320 // If it is not a render target then it will most likely be populated by
321 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600322 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500323 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400324 GrGpuResource* resource = fCache->findAndRefScratchResource(
325 key, GrSurface::WorstCaseSize(desc, renderable), scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500326 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400327 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500328 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400329 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500330 }
331 }
332
Brian Osman32342f02017-03-04 08:12:46 -0500333 return nullptr;
334}
335
Greg Daniel7ef28f32017-04-20 16:41:55 +0000336sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Robert Phillips97256382019-07-17 15:26:36 -0400337 GrColorType colorType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500338 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500339 GrWrapCacheable cacheable,
340 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500341 ASSERT_SINGLE_OWNER
342 if (this->isAbandoned()) {
343 return nullptr;
344 }
Robert Phillips97256382019-07-17 15:26:36 -0400345 return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400346}
347
348sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400349 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400350 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500351 GrWrapOwnership ownership,
352 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400353 ASSERT_SINGLE_OWNER
354 if (this->isAbandoned()) {
355 return nullptr;
356 }
Robert Phillips0902c982019-07-16 07:47:56 -0400357 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500358}
359
360sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillips97256382019-07-17 15:26:36 -0400361 const GrBackendRenderTarget& backendRT, GrColorType colorType)
Brian Osman32342f02017-03-04 08:12:46 -0500362{
363 ASSERT_SINGLE_OWNER
Robert Phillips97256382019-07-17 15:26:36 -0400364 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500365}
366
Greg Danielb46add82019-01-02 14:51:29 -0500367sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
368 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
369 ASSERT_SINGLE_OWNER
370 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
371 vkInfo);
372
373}
374
Brian Osman32342f02017-03-04 08:12:46 -0500375void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
376 GrGpuResource* resource) {
377 ASSERT_SINGLE_OWNER
378 if (this->isAbandoned() || !resource) {
379 return;
380 }
381 resource->resourcePriv().setUniqueKey(key);
382}
383
Brian Salomond28a79d2017-10-16 13:01:07 -0400384sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500385 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400386 return this->isAbandoned() ? nullptr
387 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500388}
389
Brian Salomondbf70722019-02-07 11:31:24 -0500390sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
391 size_t size,
392 const void* data,
393 const GrUniqueKey& key) {
394 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400395 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700396 }
Brian Salomondbf70722019-02-07 11:31:24 -0500397 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500398 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500399 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700400 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
401 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
402 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500403 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700404 }
405 return nullptr;
406}
407
Brian Salomondbf70722019-02-07 11:31:24 -0500408sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
409 int patternSize,
410 int reps,
411 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500412 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700413 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
414
Brian Salomon09d994e2016-12-21 11:14:46 -0500415 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500416 sk_sp<GrGpuBuffer> buffer(
417 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700418 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700419 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700420 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400421 uint16_t* data = (uint16_t*) buffer->map();
422 SkAutoTArray<uint16_t> temp;
423 if (!data) {
424 temp.reset(reps * patternSize);
425 data = temp.get();
426 }
bsalomoned0bcad2015-05-04 10:36:42 -0700427 for (int i = 0; i < reps; ++i) {
428 int baseIdx = i * patternSize;
429 uint16_t baseVert = (uint16_t)(i * vertCount);
430 for (int j = 0; j < patternSize; ++j) {
431 data[baseIdx+j] = baseVert + pattern[j];
432 }
433 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400434 if (temp.get()) {
435 if (!buffer->updateData(data, bufferSize)) {
436 return nullptr;
437 }
438 } else {
439 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700440 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500441 if (key) {
442 SkASSERT(key->isValid());
443 this->assignUniqueKeyToResource(*key, buffer.get());
444 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400445 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700446}
447
Brian Salomon34169692017-08-28 15:32:01 -0400448static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
449
Brian Salomondbf70722019-02-07 11:31:24 -0500450sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700451 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000452 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500453 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700454}
455
Brian Salomon763abf02018-05-01 18:49:38 +0000456int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400457
Robert Phillips67d52cf2017-06-05 13:38:13 -0400458sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400459 if (this->isAbandoned()) {
460 return nullptr;
461 }
462
bsalomon706f08f2015-05-22 07:35:58 -0700463 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700464 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700465}
466
Brian Salomondbf70722019-02-07 11:31:24 -0500467sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
468 GrAccessPattern accessPattern,
469 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700470 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700471 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700472 }
cdaltond37fe762016-04-21 07:41:50 -0700473 if (kDynamic_GrAccessPattern != accessPattern) {
474 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
475 }
cdaltond37fe762016-04-21 07:41:50 -0700476 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400477 static const size_t MIN_SIZE = 1 << 12;
478 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700479
cdaltond37fe762016-04-21 07:41:50 -0700480 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500481 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
482 auto buffer =
483 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
484 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700485 if (!buffer) {
486 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
487 if (!buffer) {
488 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700489 }
490 }
cdaltond37fe762016-04-21 07:41:50 -0700491 if (data) {
492 buffer->updateData(data, size);
493 }
494 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800495}
496
Chris Daltoneffee202019-07-01 22:28:03 -0600497bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700498 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600499 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
500 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400501 return true;
egdanielec00d942015-09-14 12:56:10 -0700502 }
503
504 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
505 GrUniqueKey sbKey;
506
507 int width = rt->width();
508 int height = rt->height();
509#if 0
510 if (this->caps()->oversizedStencilSupport()) {
511 width = SkNextPow2(width);
512 height = SkNextPow2(height);
513 }
514#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600515 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
516 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400517 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700518 if (!stencil) {
519 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600520 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
521 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400522 if (!stencil) {
523 return false;
egdanielec00d942015-09-14 12:56:10 -0700524 }
Robert Phillips01a91282018-07-26 08:03:04 -0400525 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700526 }
Greg Danielcfa39352018-10-05 12:01:59 -0400527 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700528 }
Chris Dalton215ff332019-07-02 09:38:22 -0600529
530 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
531 return stencil->numSamples() >= minStencilSampleCount;
532 }
533 return false;
egdanielec00d942015-09-14 12:56:10 -0700534}
535
bungeman6bd52842016-10-27 09:30:08 -0700536sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillips97256382019-07-17 15:26:36 -0400537 const GrBackendTexture& tex, int sampleCnt, GrColorType colorType)
bungeman6bd52842016-10-27 09:30:08 -0700538{
ericrkf7b8b8a2016-02-24 14:49:51 -0800539 if (this->isAbandoned()) {
540 return nullptr;
541 }
Robert Phillips97256382019-07-17 15:26:36 -0400542 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800543}
Greg Danield85f97d2017-03-07 13:37:21 -0500544
Greg Daniela5cb7812017-06-16 09:45:32 -0400545sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
546 return fGpu->makeSemaphore(isOwned);
547}
548
549sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500550 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400551 GrWrapOwnership ownership) {
552 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500553 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
554 wrapType,
555 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500556}