blob: b0a913626f15d5e6265dbc47ea41a7cb492f08e3 [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
Greg Daniel21918232017-09-08 14:46:23 -0400153 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillips92de6312017-05-23 07:43:48 -0400154 if (tex) {
155 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400156 }
157
Robert Phillips67d52cf2017-06-05 13:38:13 -0400158 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500159}
160
Robert Phillips67d52cf2017-06-05 13:38:13 -0400161sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600162 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500163 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600164 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500165
Brian Osman32342f02017-03-04 08:12:46 -0500166 if (this->isAbandoned()) {
167 return nullptr;
168 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400169
Brian Salomonbdecacf2018-02-02 20:32:49 -0500170 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400171 return nullptr;
172 }
173
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400174 if (auto tex = this->refScratchTexture(desc, flags)) {
175 return tex;
176 }
177
Greg Daniel29bf84f2017-09-25 12:25:12 -0400178 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
179
180 // bin by pow2 with a reasonable min
181 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
182 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
183 GrSurfaceDesc* wdesc = copyDesc.writable();
184 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
185 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
186 }
187
188 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
189 return tex;
190 }
191
192 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500193}
194
Chris Daltond004e0b2018-09-27 09:28:03 -0600195sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500196 ASSERT_SINGLE_OWNER
197 SkASSERT(!this->isAbandoned());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500198 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500199
Brian Salomond17b4a62017-05-23 16:53:47 -0400200 // We could make initial clears work with scratch textures but it is a rare case so we just opt
201 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400202 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
203 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500204
205 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400206 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600207 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
208 if (Flags::kNoPendingIO & flags) {
209 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400210 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500211 // If it is not a render target then it will most likely be populated by
212 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600213 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500214 }
215 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400216 GrSurface::WorstCaseSize(desc),
217 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500218 if (resource) {
219 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400220 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500221 }
222 }
223
Brian Osman32342f02017-03-04 08:12:46 -0500224 return nullptr;
225}
226
Greg Daniel7ef28f32017-04-20 16:41:55 +0000227sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500228 GrWrapOwnership ownership,
229 bool purgeImmediately) {
Brian Osman32342f02017-03-04 08:12:46 -0500230 ASSERT_SINGLE_OWNER
231 if (this->isAbandoned()) {
232 return nullptr;
233 }
Greg Daniel2268ad22018-11-15 09:27:38 -0500234 return fGpu->wrapBackendTexture(tex, ownership, purgeImmediately);
Brian Salomond17f6582017-07-19 18:28:58 -0400235}
236
237sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400238 int sampleCnt,
239 GrWrapOwnership ownership) {
240 ASSERT_SINGLE_OWNER
241 if (this->isAbandoned()) {
242 return nullptr;
243 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400244 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500245}
246
247sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400248 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500249{
250 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400251 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500252}
253
254void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
255 GrGpuResource* resource) {
256 ASSERT_SINGLE_OWNER
257 if (this->isAbandoned() || !resource) {
258 return;
259 }
260 resource->resourcePriv().setUniqueKey(key);
261}
262
Brian Salomond28a79d2017-10-16 13:01:07 -0400263sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500264 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400265 return this->isAbandoned() ? nullptr
266 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500267}
268
Chris Dalton5d2de082017-12-19 10:40:23 -0700269sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
270 size_t size,
271 const void* data,
272 const GrUniqueKey& key) {
273 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400274 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700275 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600276 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, Flags::kNone,
Chris Dalton5d2de082017-12-19 10:40:23 -0700277 data)) {
278 // We shouldn't bin and/or cachestatic buffers.
279 SkASSERT(buffer->sizeInBytes() == size);
280 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
281 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
282 buffer->resourcePriv().setUniqueKey(key);
283 return sk_sp<const GrBuffer>(buffer);
284 }
285 return nullptr;
286}
287
Brian Salomond28a79d2017-10-16 13:01:07 -0400288sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
289 int patternSize,
290 int reps,
291 int vertCount,
292 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700293 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
294
Brian Salomon09d994e2016-12-21 11:14:46 -0500295 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400296 sk_sp<GrBuffer> buffer(this->createBuffer(bufferSize, kIndex_GrBufferType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600297 kStatic_GrAccessPattern, Flags::kNoPendingIO));
bsalomoned0bcad2015-05-04 10:36:42 -0700298 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700299 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700300 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400301 uint16_t* data = (uint16_t*) buffer->map();
302 SkAutoTArray<uint16_t> temp;
303 if (!data) {
304 temp.reset(reps * patternSize);
305 data = temp.get();
306 }
bsalomoned0bcad2015-05-04 10:36:42 -0700307 for (int i = 0; i < reps; ++i) {
308 int baseIdx = i * patternSize;
309 uint16_t baseVert = (uint16_t)(i * vertCount);
310 for (int j = 0; j < patternSize; ++j) {
311 data[baseIdx+j] = baseVert + pattern[j];
312 }
313 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400314 if (temp.get()) {
315 if (!buffer->updateData(data, bufferSize)) {
316 return nullptr;
317 }
318 } else {
319 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700320 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400321 this->assignUniqueKeyToResource(key, buffer.get());
Brian Salomond28a79d2017-10-16 13:01:07 -0400322 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700323}
324
Brian Salomon34169692017-08-28 15:32:01 -0400325static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
326
Brian Salomond28a79d2017-10-16 13:01:07 -0400327sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700328 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000329 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Chris Daltonff926502017-05-03 14:36:54 -0400330 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700331}
332
Brian Salomon763abf02018-05-01 18:49:38 +0000333int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400334
Robert Phillips67d52cf2017-06-05 13:38:13 -0400335sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400336 if (this->isAbandoned()) {
337 return nullptr;
338 }
339
bsalomon706f08f2015-05-22 07:35:58 -0700340 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700341 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700342}
343
cdaltone2e71c22016-04-07 18:13:29 -0700344GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600345 GrAccessPattern accessPattern, Flags flags,
cdalton1bf3e712016-04-19 10:00:02 -0700346 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700347 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700348 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700349 }
cdaltond37fe762016-04-21 07:41:50 -0700350 if (kDynamic_GrAccessPattern != accessPattern) {
351 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
352 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600353 if (!(flags & Flags::kRequireGpuMemory) &&
csmartdalton485a1202016-07-13 10:16:32 -0700354 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
355 GrBufferTypeIsVertexOrIndex(intendedType) &&
356 kDynamic_GrAccessPattern == accessPattern) {
357 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
358 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700359
cdaltond37fe762016-04-21 07:41:50 -0700360 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400361 static const size_t MIN_SIZE = 1 << 12;
362 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700363
cdaltond37fe762016-04-21 07:41:50 -0700364 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700365 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600366 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
367 if (flags & Flags::kNoPendingIO) {
368 scratchFlags = GrResourceCache::ScratchFlags::kRequireNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700369 } else {
Chris Daltond004e0b2018-09-27 09:28:03 -0600370 scratchFlags = GrResourceCache::ScratchFlags::kPreferNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700371 }
372 GrBuffer* buffer = static_cast<GrBuffer*>(
373 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
374 if (!buffer) {
375 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
376 if (!buffer) {
377 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700378 }
379 }
cdaltond37fe762016-04-21 07:41:50 -0700380 if (data) {
381 buffer->updateData(data, size);
382 }
csmartdalton485a1202016-07-13 10:16:32 -0700383 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700384 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800385}
386
Robert Phillipsc0192e32017-09-21 12:00:26 -0400387bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700388 SkASSERT(rt);
389 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400390 return true;
egdanielec00d942015-09-14 12:56:10 -0700391 }
392
393 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
394 GrUniqueKey sbKey;
395
396 int width = rt->width();
397 int height = rt->height();
398#if 0
399 if (this->caps()->oversizedStencilSupport()) {
400 width = SkNextPow2(width);
401 height = SkNextPow2(height);
402 }
403#endif
egdanielec00d942015-09-14 12:56:10 -0700404 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
405 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400406 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700407 if (!stencil) {
408 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400409 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400410 if (!stencil) {
411 return false;
egdanielec00d942015-09-14 12:56:10 -0700412 }
Robert Phillips01a91282018-07-26 08:03:04 -0400413 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700414 }
Greg Danielcfa39352018-10-05 12:01:59 -0400415 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700416 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400417 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700418}
419
bungeman6bd52842016-10-27 09:30:08 -0700420sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400421 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700422{
ericrkf7b8b8a2016-02-24 14:49:51 -0800423 if (this->isAbandoned()) {
424 return nullptr;
425 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500426 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800427}
Greg Danield85f97d2017-03-07 13:37:21 -0500428
Greg Daniela5cb7812017-06-16 09:45:32 -0400429sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
430 return fGpu->makeSemaphore(isOwned);
431}
432
433sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500434 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400435 GrWrapOwnership ownership) {
436 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500437 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
438 wrapType,
439 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500440}