blob: 289a35d74222f2f272eb6609bd498e8d7f627138 [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,
Brian Osman32342f02017-03-04 08:12:46 -0500228 GrWrapOwnership ownership) {
229 ASSERT_SINGLE_OWNER
230 if (this->isAbandoned()) {
231 return nullptr;
232 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400233 return fGpu->wrapBackendTexture(tex, ownership);
Brian Salomond17f6582017-07-19 18:28:58 -0400234}
235
236sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400237 int sampleCnt,
238 GrWrapOwnership ownership) {
239 ASSERT_SINGLE_OWNER
240 if (this->isAbandoned()) {
241 return nullptr;
242 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400243 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500244}
245
246sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400247 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500248{
249 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400250 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500251}
252
253void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
254 GrGpuResource* resource) {
255 ASSERT_SINGLE_OWNER
256 if (this->isAbandoned() || !resource) {
257 return;
258 }
259 resource->resourcePriv().setUniqueKey(key);
260}
261
Brian Salomond28a79d2017-10-16 13:01:07 -0400262sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500263 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400264 return this->isAbandoned() ? nullptr
265 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500266}
267
Chris Dalton5d2de082017-12-19 10:40:23 -0700268sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
269 size_t size,
270 const void* data,
271 const GrUniqueKey& key) {
272 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400273 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700274 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600275 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, Flags::kNone,
Chris Dalton5d2de082017-12-19 10:40:23 -0700276 data)) {
277 // We shouldn't bin and/or cachestatic buffers.
278 SkASSERT(buffer->sizeInBytes() == size);
279 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
280 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
281 buffer->resourcePriv().setUniqueKey(key);
282 return sk_sp<const GrBuffer>(buffer);
283 }
284 return nullptr;
285}
286
Brian Salomond28a79d2017-10-16 13:01:07 -0400287sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
288 int patternSize,
289 int reps,
290 int vertCount,
291 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700292 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
293
Brian Salomon09d994e2016-12-21 11:14:46 -0500294 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400295 sk_sp<GrBuffer> buffer(this->createBuffer(bufferSize, kIndex_GrBufferType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600296 kStatic_GrAccessPattern, Flags::kNoPendingIO));
bsalomoned0bcad2015-05-04 10:36:42 -0700297 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700298 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700299 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400300 uint16_t* data = (uint16_t*) buffer->map();
301 SkAutoTArray<uint16_t> temp;
302 if (!data) {
303 temp.reset(reps * patternSize);
304 data = temp.get();
305 }
bsalomoned0bcad2015-05-04 10:36:42 -0700306 for (int i = 0; i < reps; ++i) {
307 int baseIdx = i * patternSize;
308 uint16_t baseVert = (uint16_t)(i * vertCount);
309 for (int j = 0; j < patternSize; ++j) {
310 data[baseIdx+j] = baseVert + pattern[j];
311 }
312 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400313 if (temp.get()) {
314 if (!buffer->updateData(data, bufferSize)) {
315 return nullptr;
316 }
317 } else {
318 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700319 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400320 this->assignUniqueKeyToResource(key, buffer.get());
Brian Salomond28a79d2017-10-16 13:01:07 -0400321 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700322}
323
Brian Salomon34169692017-08-28 15:32:01 -0400324static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
325
Brian Salomond28a79d2017-10-16 13:01:07 -0400326sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700327 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000328 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Chris Daltonff926502017-05-03 14:36:54 -0400329 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700330}
331
Brian Salomon763abf02018-05-01 18:49:38 +0000332int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400333
Robert Phillips67d52cf2017-06-05 13:38:13 -0400334sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400335 if (this->isAbandoned()) {
336 return nullptr;
337 }
338
bsalomon706f08f2015-05-22 07:35:58 -0700339 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700340 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700341}
342
cdaltone2e71c22016-04-07 18:13:29 -0700343GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
Chris Daltond004e0b2018-09-27 09:28:03 -0600344 GrAccessPattern accessPattern, Flags flags,
cdalton1bf3e712016-04-19 10:00:02 -0700345 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700346 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700347 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700348 }
cdaltond37fe762016-04-21 07:41:50 -0700349 if (kDynamic_GrAccessPattern != accessPattern) {
350 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
351 }
Chris Daltond004e0b2018-09-27 09:28:03 -0600352 if (!(flags & Flags::kRequireGpuMemory) &&
csmartdalton485a1202016-07-13 10:16:32 -0700353 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
354 GrBufferTypeIsVertexOrIndex(intendedType) &&
355 kDynamic_GrAccessPattern == accessPattern) {
356 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
357 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700358
cdaltond37fe762016-04-21 07:41:50 -0700359 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400360 static const size_t MIN_SIZE = 1 << 12;
361 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700362
cdaltond37fe762016-04-21 07:41:50 -0700363 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700364 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600365 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
366 if (flags & Flags::kNoPendingIO) {
367 scratchFlags = GrResourceCache::ScratchFlags::kRequireNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700368 } else {
Chris Daltond004e0b2018-09-27 09:28:03 -0600369 scratchFlags = GrResourceCache::ScratchFlags::kPreferNoPendingIO;
cdaltond37fe762016-04-21 07:41:50 -0700370 }
371 GrBuffer* buffer = static_cast<GrBuffer*>(
372 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
373 if (!buffer) {
374 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
375 if (!buffer) {
376 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700377 }
378 }
cdaltond37fe762016-04-21 07:41:50 -0700379 if (data) {
380 buffer->updateData(data, size);
381 }
csmartdalton485a1202016-07-13 10:16:32 -0700382 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700383 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800384}
385
Robert Phillipsc0192e32017-09-21 12:00:26 -0400386bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700387 SkASSERT(rt);
388 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400389 return true;
egdanielec00d942015-09-14 12:56:10 -0700390 }
391
392 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
393 GrUniqueKey sbKey;
394
395 int width = rt->width();
396 int height = rt->height();
397#if 0
398 if (this->caps()->oversizedStencilSupport()) {
399 width = SkNextPow2(width);
400 height = SkNextPow2(height);
401 }
402#endif
egdanielec00d942015-09-14 12:56:10 -0700403 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
404 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400405 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700406 if (!stencil) {
407 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400408 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400409 if (!stencil) {
410 return false;
egdanielec00d942015-09-14 12:56:10 -0700411 }
Robert Phillips01a91282018-07-26 08:03:04 -0400412 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700413 }
Greg Danielcfa39352018-10-05 12:01:59 -0400414 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700415 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400416 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700417}
418
bungeman6bd52842016-10-27 09:30:08 -0700419sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400420 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700421{
ericrkf7b8b8a2016-02-24 14:49:51 -0800422 if (this->isAbandoned()) {
423 return nullptr;
424 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500425 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800426}
Greg Danield85f97d2017-03-07 13:37:21 -0500427
Greg Daniela5cb7812017-06-16 09:45:32 -0400428sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
429 return fGpu->makeSemaphore(isOwned);
430}
431
432sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500433 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400434 GrWrapOwnership ownership) {
435 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500436 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
437 wrapType,
438 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500439}