blob: 3e7d2c7142e34f056e0eea8ec0652b9ff4b6aaf0 [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
8#include "GrResourceProvider.h"
9
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070012#include "GrCaps.h"
Robert Phillips26c90e02017-03-14 14:39:29 -040013#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040014#include "GrContextPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070015#include "GrGpu.h"
Robert Phillips67d52cf2017-06-05 13:38:13 -040016#include "GrPath.h"
kkinnunencabe20c2015-06-01 01:37:26 -070017#include "GrPathRendering.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050018#include "GrProxyProvider.h"
egdanielec00d942015-09-14 12:56:10 -070019#include "GrRenderTargetPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070020#include "GrResourceCache.h"
21#include "GrResourceKey.h"
Greg Danield85f97d2017-03-07 13:37:21 -050022#include "GrSemaphore.h"
egdanielec00d942015-09-14 12:56:10 -070023#include "GrStencilAttachment.h"
Brian Osman32342f02017-03-04 08:12:46 -050024#include "GrTexturePriv.h"
25#include "../private/GrSingleOwner.h"
Robert Phillips45fdae12017-04-17 12:57:27 -040026#include "SkGr.h"
halcanary4dbbd042016-06-07 17:21:10 -070027#include "SkMathPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070028
29GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
30
Robert Phillips1bfece82017-06-01 13:56:52 -040031const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050032
Robert Phillipsa3f70262018-02-08 10:59:38 -050033#ifdef SK_DISABLE_EXPLICIT_GPU_RESOURCE_ALLOCATION
34static const bool kDefaultExplicitlyAllocateGPUResources = false;
35#else
36static const bool kDefaultExplicitlyAllocateGPUResources = true;
37#endif
38
Brian Osman32342f02017-03-04 08:12:46 -050039#define ASSERT_SINGLE_OWNER \
40 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
41
Robert Phillips4150eea2018-02-07 17:08:21 -050042GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner,
Robert Phillipsa3f70262018-02-08 10:59:38 -050043 GrContextOptions::Enable explicitlyAllocateGPUResources)
Brian Osman32342f02017-03-04 08:12:46 -050044 : fCache(cache)
45 , fGpu(gpu)
46#ifdef SK_DEBUG
47 , fSingleOwner(owner)
48#endif
Robert Phillips4150eea2018-02-07 17:08:21 -050049{
Robert Phillipsa3f70262018-02-08 10:59:38 -050050 if (GrContextOptions::Enable::kNo == explicitlyAllocateGPUResources) {
51 fExplicitlyAllocateGPUResources = false;
52 } else if (GrContextOptions::Enable::kYes == explicitlyAllocateGPUResources) {
53 fExplicitlyAllocateGPUResources = true;
54 } else {
55 fExplicitlyAllocateGPUResources = kDefaultExplicitlyAllocateGPUResources;
56 }
57
Robert Phillips26c90e02017-03-14 14:39:29 -040058 fCaps = sk_ref_sp(fGpu->caps());
59
bsalomoned0bcad2015-05-04 10:36:42 -070060 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
61 fQuadIndexBufferKey = gQuadIndexBufferKey;
62}
63
Robert Phillips8e8c7552017-07-10 12:06:05 -040064sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Osman2b23c4b2018-06-01 12:25:08 -040065 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050066 ASSERT_SINGLE_OWNER
67
Robert Phillips7f1b4f82017-11-28 07:38:39 -050068 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040069
Brian Osman32342f02017-03-04 08:12:46 -050070 if (this->isAbandoned()) {
71 return nullptr;
72 }
Robert Phillips1119dc32017-04-11 12:54:57 -040073
Brian Salomonbdecacf2018-02-02 20:32:49 -050074 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
75 if (!fCaps->validateSurfaceDesc(desc, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050076 return nullptr;
77 }
Brian Osman32342f02017-03-04 08:12:46 -050078
Brian Osman2b23c4b2018-06-01 12:25:08 -040079 return fGpu->createTexture(desc, budgeted, texels, mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -050080}
81
Robert Phillips45fdae12017-04-17 12:57:27 -040082sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -060083 SkBudgeted budgeted, Flags flags) {
Robert Phillips45fdae12017-04-17 12:57:27 -040084 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
85 if (tex && SkBudgeted::kNo == budgeted) {
86 tex->resourcePriv().makeUnbudgeted();
87 }
88
89 return tex;
90}
91
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
93 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -050094 SkBackingFit fit,
Chris Daltond004e0b2018-09-27 09:28:03 -060095 const GrMipLevel& mipLevel,
96 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -040097 ASSERT_SINGLE_OWNER
98
99 if (this->isAbandoned()) {
100 return nullptr;
101 }
102
Robert Phillips45fdae12017-04-17 12:57:27 -0400103 if (!mipLevel.fPixels) {
104 return nullptr;
105 }
106
Brian Salomonbdecacf2018-02-02 20:32:49 -0500107 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400108 return nullptr;
109 }
110
Robert Phillips45fdae12017-04-17 12:57:27 -0400111 GrContext* context = fGpu->getContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500112 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400113
Brian Salomon1fcac052018-05-09 16:33:09 -0400114 SkColorType colorType;
115 if (GrPixelConfigToColorType(desc.fConfig, &colorType)) {
Chris Daltond004e0b2018-09-27 09:28:03 -0600116 sk_sp<GrTexture> tex = (SkBackingFit::kApprox == fit)
117 ? this->createApproxTexture(desc, flags)
118 : this->createTexture(desc, budgeted, flags);
119 if (!tex) {
120 return nullptr;
121 }
122
123 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(std::move(tex),
124 kTopLeft_GrSurfaceOrigin);
Brian Salomon1fcac052018-05-09 16:33:09 -0400125 if (!proxy) {
126 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400127 }
Brian Salomon1fcac052018-05-09 16:33:09 -0400128 auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType,
Brian Osman9363ac42018-06-01 16:10:53 -0400129 kUnknown_SkAlphaType);
Brian Salomon1fcac052018-05-09 16:33:09 -0400130 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400131 std::move(proxy));
Brian Salomon1fcac052018-05-09 16:33:09 -0400132 if (!sContext) {
133 return nullptr;
134 }
135 SkAssertResult(sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400136 return sk_ref_sp(sContext->asTextureProxy()->peekTexture());
Brian Salomon1fcac052018-05-09 16:33:09 -0400137 } else {
138 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400139 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400140}
141
Robert Phillipse78b7252017-04-06 07:59:41 -0400142sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600143 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400144 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400145 if (this->isAbandoned()) {
146 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500147 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400148
Brian Salomonbdecacf2018-02-02 20:32:49 -0500149 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400150 return nullptr;
151 }
152
Jim Van Verth1676cb92019-01-15 13:24:45 -0500153 // Compressed textures are read-only so they don't support re-use for scratch.
154 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
155 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
156 if (tex) {
157 return tex;
158 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400159 }
160
Robert Phillips67d52cf2017-06-05 13:38:13 -0400161 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500162}
163
Robert Phillips67d52cf2017-06-05 13:38:13 -0400164sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600165 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500166 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600167 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500168
Brian Osman32342f02017-03-04 08:12:46 -0500169 if (this->isAbandoned()) {
170 return nullptr;
171 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400172
Jim Van Verth1676cb92019-01-15 13:24:45 -0500173 // Currently we don't recycle compressed textures as scratch.
174 if (GrPixelConfigIsCompressed(desc.fConfig)) {
175 return nullptr;
176 }
177
Brian Salomonbdecacf2018-02-02 20:32:49 -0500178 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400179 return nullptr;
180 }
181
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400182 if (auto tex = this->refScratchTexture(desc, flags)) {
183 return tex;
184 }
185
Greg Daniel29bf84f2017-09-25 12:25:12 -0400186 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
187
188 // bin by pow2 with a reasonable min
189 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
190 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
191 GrSurfaceDesc* wdesc = copyDesc.writable();
192 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
193 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
194 }
195
196 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
197 return tex;
198 }
199
200 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500201}
202
Chris Daltond004e0b2018-09-27 09:28:03 -0600203sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500204 ASSERT_SINGLE_OWNER
205 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500206 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500207 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500208
Brian Salomond17b4a62017-05-23 16:53:47 -0400209 // We could make initial clears work with scratch textures but it is a rare case so we just opt
210 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400211 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
212 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500213
214 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400215 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600216 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
217 if (Flags::kNoPendingIO & flags) {
218 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400219 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500220 // If it is not a render target then it will most likely be populated by
221 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600222 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500223 }
224 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400225 GrSurface::WorstCaseSize(desc),
226 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500227 if (resource) {
228 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400229 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500230 }
231 }
232
Brian Osman32342f02017-03-04 08:12:46 -0500233 return nullptr;
234}
235
Greg Daniel7ef28f32017-04-20 16:41:55 +0000236sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500237 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500238 GrWrapCacheable cacheable,
239 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500240 ASSERT_SINGLE_OWNER
241 if (this->isAbandoned()) {
242 return nullptr;
243 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500244 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400245}
246
247sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400248 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500249 GrWrapOwnership ownership,
250 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400251 ASSERT_SINGLE_OWNER
252 if (this->isAbandoned()) {
253 return nullptr;
254 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500255 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500256}
257
258sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400259 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500260{
261 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400262 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500263}
264
Greg Danielb46add82019-01-02 14:51:29 -0500265sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
266 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
267 ASSERT_SINGLE_OWNER
268 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
269 vkInfo);
270
271}
272
Brian Osman32342f02017-03-04 08:12:46 -0500273void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
274 GrGpuResource* resource) {
275 ASSERT_SINGLE_OWNER
276 if (this->isAbandoned() || !resource) {
277 return;
278 }
279 resource->resourcePriv().setUniqueKey(key);
280}
281
Brian Salomond28a79d2017-10-16 13:01:07 -0400282sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500283 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400284 return this->isAbandoned() ? nullptr
285 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500286}
287
Chris Dalton5d2de082017-12-19 10:40:23 -0700288sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
289 size_t size,
290 const void* data,
291 const GrUniqueKey& key) {
292 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400293 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700294 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600295 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, Flags::kNone,
Chris Dalton5d2de082017-12-19 10:40:23 -0700296 data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500297 // We shouldn't bin and/or cache static buffers.
Chris Dalton5d2de082017-12-19 10:40:23 -0700298 SkASSERT(buffer->sizeInBytes() == size);
299 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
300 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
301 buffer->resourcePriv().setUniqueKey(key);
302 return sk_sp<const GrBuffer>(buffer);
303 }
304 return nullptr;
305}
306
Brian Salomond28a79d2017-10-16 13:01:07 -0400307sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
308 int patternSize,
309 int reps,
310 int vertCount,
311 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700312 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
313
Brian Salomon09d994e2016-12-21 11:14:46 -0500314 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400315 sk_sp<GrBuffer> buffer(this->createBuffer(bufferSize, kIndex_GrBufferType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600316 kStatic_GrAccessPattern, Flags::kNoPendingIO));
bsalomoned0bcad2015-05-04 10:36:42 -0700317 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700318 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700319 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400320 uint16_t* data = (uint16_t*) buffer->map();
321 SkAutoTArray<uint16_t> temp;
322 if (!data) {
323 temp.reset(reps * patternSize);
324 data = temp.get();
325 }
bsalomoned0bcad2015-05-04 10:36:42 -0700326 for (int i = 0; i < reps; ++i) {
327 int baseIdx = i * patternSize;
328 uint16_t baseVert = (uint16_t)(i * vertCount);
329 for (int j = 0; j < patternSize; ++j) {
330 data[baseIdx+j] = baseVert + pattern[j];
331 }
332 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400333 if (temp.get()) {
334 if (!buffer->updateData(data, bufferSize)) {
335 return nullptr;
336 }
337 } else {
338 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700339 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400340 this->assignUniqueKeyToResource(key, buffer.get());
Brian Salomond28a79d2017-10-16 13:01:07 -0400341 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700342}
343
Brian Salomon34169692017-08-28 15:32:01 -0400344static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
345
Brian Salomond28a79d2017-10-16 13:01:07 -0400346sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700347 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000348 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Chris Daltonff926502017-05-03 14:36:54 -0400349 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700350}
351
Brian Salomon763abf02018-05-01 18:49:38 +0000352int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400353
Robert Phillips67d52cf2017-06-05 13:38:13 -0400354sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400355 if (this->isAbandoned()) {
356 return nullptr;
357 }
358
bsalomon706f08f2015-05-22 07:35:58 -0700359 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700360 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700361}
362
cdaltone2e71c22016-04-07 18:13:29 -0700363GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600364 GrAccessPattern accessPattern, Flags flags,
cdalton1bf3e712016-04-19 10:00:02 -0700365 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700366 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700367 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700368 }
cdaltond37fe762016-04-21 07:41:50 -0700369 if (kDynamic_GrAccessPattern != accessPattern) {
370 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
371 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600372 if (!(flags & Flags::kRequireGpuMemory) &&
csmartdalton485a1202016-07-13 10:16:32 -0700373 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
374 GrBufferTypeIsVertexOrIndex(intendedType) &&
375 kDynamic_GrAccessPattern == accessPattern) {
376 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
377 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700378
cdaltond37fe762016-04-21 07:41:50 -0700379 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400380 static const size_t MIN_SIZE = 1 << 12;
381 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700382
cdaltond37fe762016-04-21 07:41:50 -0700383 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700384 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600385 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
386 if (flags & Flags::kNoPendingIO) {
387 scratchFlags = GrResourceCache::ScratchFlags::kRequireNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700388 } else {
Chris Daltond004e0b2018-09-27 09:28:03 -0600389 scratchFlags = GrResourceCache::ScratchFlags::kPreferNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700390 }
391 GrBuffer* buffer = static_cast<GrBuffer*>(
392 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
393 if (!buffer) {
394 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
395 if (!buffer) {
396 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700397 }
398 }
cdaltond37fe762016-04-21 07:41:50 -0700399 if (data) {
400 buffer->updateData(data, size);
401 }
csmartdalton485a1202016-07-13 10:16:32 -0700402 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700403 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800404}
405
Robert Phillipsc0192e32017-09-21 12:00:26 -0400406bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700407 SkASSERT(rt);
408 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400409 return true;
egdanielec00d942015-09-14 12:56:10 -0700410 }
411
412 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
413 GrUniqueKey sbKey;
414
415 int width = rt->width();
416 int height = rt->height();
417#if 0
418 if (this->caps()->oversizedStencilSupport()) {
419 width = SkNextPow2(width);
420 height = SkNextPow2(height);
421 }
422#endif
egdanielec00d942015-09-14 12:56:10 -0700423 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
424 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400425 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700426 if (!stencil) {
427 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400428 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400429 if (!stencil) {
430 return false;
egdanielec00d942015-09-14 12:56:10 -0700431 }
Robert Phillips01a91282018-07-26 08:03:04 -0400432 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700433 }
Greg Danielcfa39352018-10-05 12:01:59 -0400434 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700435 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400436 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700437}
438
bungeman6bd52842016-10-27 09:30:08 -0700439sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400440 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700441{
ericrkf7b8b8a2016-02-24 14:49:51 -0800442 if (this->isAbandoned()) {
443 return nullptr;
444 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500445 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800446}
Greg Danield85f97d2017-03-07 13:37:21 -0500447
Greg Daniela5cb7812017-06-16 09:45:32 -0400448sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
449 return fGpu->makeSemaphore(isOwned);
450}
451
452sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500453 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400454 GrWrapOwnership ownership) {
455 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500456 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
457 wrapType,
458 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500459}