blob: 3450b008f24e2733f50c4e25b1eca09c4b987042 [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,
Greg Daniel21918232017-09-08 14:46:23 -040083 SkBudgeted budgeted, uint32_t 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,
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -040096 ASSERT_SINGLE_OWNER
97
98 if (this->isAbandoned()) {
99 return nullptr;
100 }
101
Robert Phillips45fdae12017-04-17 12:57:27 -0400102 if (!mipLevel.fPixels) {
103 return nullptr;
104 }
105
Brian Salomonbdecacf2018-02-02 20:32:49 -0500106 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400107 return nullptr;
108 }
109
Robert Phillips45fdae12017-04-17 12:57:27 -0400110 GrContext* context = fGpu->getContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500111 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400112
Brian Salomon1fcac052018-05-09 16:33:09 -0400113 SkColorType colorType;
114 if (GrPixelConfigToColorType(desc.fConfig, &colorType)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115 // DDL TODO: remove this use of createInstantiatedProxy and convert it to a testing-only
116 // method.
Brian Salomon58389b92018-03-07 13:01:25 -0500117 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(
118 desc, kTopLeft_GrSurfaceOrigin, fit, budgeted);
Brian Salomon1fcac052018-05-09 16:33:09 -0400119 if (!proxy) {
120 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400121 }
Brian Salomon1fcac052018-05-09 16:33:09 -0400122 auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType,
Brian Osman9363ac42018-06-01 16:10:53 -0400123 kUnknown_SkAlphaType);
Brian Salomon1fcac052018-05-09 16:33:09 -0400124 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400125 std::move(proxy));
Brian Salomon1fcac052018-05-09 16:33:09 -0400126 if (!sContext) {
127 return nullptr;
128 }
129 SkAssertResult(sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0));
130 return sk_ref_sp(sContext->asTextureProxy()->priv().peekTexture());
131 } else {
132 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400133 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400134}
135
Robert Phillipse78b7252017-04-06 07:59:41 -0400136sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
137 uint32_t flags) {
138 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400139 if (this->isAbandoned()) {
140 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500141 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400142
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400144 return nullptr;
145 }
146
Greg Daniel21918232017-09-08 14:46:23 -0400147 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillips92de6312017-05-23 07:43:48 -0400148 if (tex) {
149 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400150 }
151
Robert Phillips67d52cf2017-06-05 13:38:13 -0400152 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500153}
154
Robert Phillips67d52cf2017-06-05 13:38:13 -0400155sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
156 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500157 ASSERT_SINGLE_OWNER
158 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500159
Brian Osman32342f02017-03-04 08:12:46 -0500160 if (this->isAbandoned()) {
161 return nullptr;
162 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400163
Brian Salomonbdecacf2018-02-02 20:32:49 -0500164 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400165 return nullptr;
166 }
167
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400168 if (auto tex = this->refScratchTexture(desc, flags)) {
169 return tex;
170 }
171
Greg Daniel29bf84f2017-09-25 12:25:12 -0400172 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
173
174 // bin by pow2 with a reasonable min
175 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
176 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
177 GrSurfaceDesc* wdesc = copyDesc.writable();
178 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
179 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
180 }
181
182 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
183 return tex;
184 }
185
186 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500187}
188
Greg Daniel29bf84f2017-09-25 12:25:12 -0400189sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -0400190 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500191 ASSERT_SINGLE_OWNER
192 SkASSERT(!this->isAbandoned());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500193 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500194
Brian Salomond17b4a62017-05-23 16:53:47 -0400195 // We could make initial clears work with scratch textures but it is a rare case so we just opt
196 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400197 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
198 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500199
200 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400201 GrTexturePriv::ComputeScratchKey(desc, &key);
Brian Osman32342f02017-03-04 08:12:46 -0500202 uint32_t scratchFlags = 0;
203 if (kNoPendingIO_Flag & flags) {
204 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400205 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500206 // If it is not a render target then it will most likely be populated by
207 // writePixels() which will trigger a flush if the texture has pending IO.
208 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
209 }
210 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400211 GrSurface::WorstCaseSize(desc),
212 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500213 if (resource) {
214 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400215 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500216 }
217 }
218
Brian Osman32342f02017-03-04 08:12:46 -0500219 return nullptr;
220}
221
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -0500223 GrWrapOwnership ownership) {
224 ASSERT_SINGLE_OWNER
225 if (this->isAbandoned()) {
226 return nullptr;
227 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400228 return fGpu->wrapBackendTexture(tex, ownership);
Brian Salomond17f6582017-07-19 18:28:58 -0400229}
230
231sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400232 int sampleCnt,
233 GrWrapOwnership ownership) {
234 ASSERT_SINGLE_OWNER
235 if (this->isAbandoned()) {
236 return nullptr;
237 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400238 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500239}
240
241sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400242 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500243{
244 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400245 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500246}
247
248void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
249 GrGpuResource* resource) {
250 ASSERT_SINGLE_OWNER
251 if (this->isAbandoned() || !resource) {
252 return;
253 }
254 resource->resourcePriv().setUniqueKey(key);
255}
256
Brian Salomond28a79d2017-10-16 13:01:07 -0400257sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500258 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400259 return this->isAbandoned() ? nullptr
260 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500261}
262
Chris Dalton5d2de082017-12-19 10:40:23 -0700263sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
264 size_t size,
265 const void* data,
266 const GrUniqueKey& key) {
267 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400268 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700269 }
270 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, 0,
271 data)) {
272 // We shouldn't bin and/or cachestatic buffers.
273 SkASSERT(buffer->sizeInBytes() == size);
274 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
275 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
276 buffer->resourcePriv().setUniqueKey(key);
277 return sk_sp<const GrBuffer>(buffer);
278 }
279 return nullptr;
280}
281
Brian Salomond28a79d2017-10-16 13:01:07 -0400282sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
283 int patternSize,
284 int reps,
285 int vertCount,
286 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700287 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
288
Brian Salomon09d994e2016-12-21 11:14:46 -0500289 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400290 sk_sp<GrBuffer> buffer(this->createBuffer(bufferSize, kIndex_GrBufferType,
291 kStatic_GrAccessPattern, kNoPendingIO_Flag));
bsalomoned0bcad2015-05-04 10:36:42 -0700292 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700293 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700294 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400295 uint16_t* data = (uint16_t*) buffer->map();
296 SkAutoTArray<uint16_t> temp;
297 if (!data) {
298 temp.reset(reps * patternSize);
299 data = temp.get();
300 }
bsalomoned0bcad2015-05-04 10:36:42 -0700301 for (int i = 0; i < reps; ++i) {
302 int baseIdx = i * patternSize;
303 uint16_t baseVert = (uint16_t)(i * vertCount);
304 for (int j = 0; j < patternSize; ++j) {
305 data[baseIdx+j] = baseVert + pattern[j];
306 }
307 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400308 if (temp.get()) {
309 if (!buffer->updateData(data, bufferSize)) {
310 return nullptr;
311 }
312 } else {
313 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700314 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400315 this->assignUniqueKeyToResource(key, buffer.get());
Brian Salomond28a79d2017-10-16 13:01:07 -0400316 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700317}
318
Brian Salomon34169692017-08-28 15:32:01 -0400319static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
320
Brian Salomond28a79d2017-10-16 13:01:07 -0400321sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700322 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000323 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Chris Daltonff926502017-05-03 14:36:54 -0400324 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700325}
326
Brian Salomon763abf02018-05-01 18:49:38 +0000327int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400328
Robert Phillips67d52cf2017-06-05 13:38:13 -0400329sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400330 if (this->isAbandoned()) {
331 return nullptr;
332 }
333
bsalomon706f08f2015-05-22 07:35:58 -0700334 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700335 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700336}
337
cdaltone2e71c22016-04-07 18:13:29 -0700338GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700339 GrAccessPattern accessPattern, uint32_t flags,
340 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700341 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700342 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700343 }
cdaltond37fe762016-04-21 07:41:50 -0700344 if (kDynamic_GrAccessPattern != accessPattern) {
345 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
346 }
csmartdalton485a1202016-07-13 10:16:32 -0700347 if (!(flags & kRequireGpuMemory_Flag) &&
348 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
349 GrBufferTypeIsVertexOrIndex(intendedType) &&
350 kDynamic_GrAccessPattern == accessPattern) {
351 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
352 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700353
cdaltond37fe762016-04-21 07:41:50 -0700354 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400355 static const size_t MIN_SIZE = 1 << 12;
356 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700357
cdaltond37fe762016-04-21 07:41:50 -0700358 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700359 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700360 uint32_t scratchFlags = 0;
361 if (flags & kNoPendingIO_Flag) {
362 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
363 } else {
364 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
365 }
366 GrBuffer* buffer = static_cast<GrBuffer*>(
367 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
368 if (!buffer) {
369 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
370 if (!buffer) {
371 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700372 }
373 }
cdaltond37fe762016-04-21 07:41:50 -0700374 if (data) {
375 buffer->updateData(data, size);
376 }
csmartdalton485a1202016-07-13 10:16:32 -0700377 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700378 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800379}
380
Robert Phillipsc0192e32017-09-21 12:00:26 -0400381bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700382 SkASSERT(rt);
383 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400384 return true;
egdanielec00d942015-09-14 12:56:10 -0700385 }
386
387 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
388 GrUniqueKey sbKey;
389
390 int width = rt->width();
391 int height = rt->height();
392#if 0
393 if (this->caps()->oversizedStencilSupport()) {
394 width = SkNextPow2(width);
395 height = SkNextPow2(height);
396 }
397#endif
Robert Phillipscb2e2352017-08-30 16:44:40 -0400398 SkDEBUGCODE(bool newStencil = false;)
egdanielec00d942015-09-14 12:56:10 -0700399 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
400 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400401 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700402 if (!stencil) {
403 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400404 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400405 if (!stencil) {
406 return false;
egdanielec00d942015-09-14 12:56:10 -0700407 }
Robert Phillips01a91282018-07-26 08:03:04 -0400408 this->assignUniqueKeyToResource(sbKey, stencil.get());
409 SkDEBUGCODE(newStencil = true;)
egdanielec00d942015-09-14 12:56:10 -0700410 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400411 if (rt->renderTargetPriv().attachStencilAttachment(std::move(stencil))) {
Robert Phillips95214472017-08-08 18:00:03 -0400412#ifdef SK_DEBUG
413 // Fill the SB with an inappropriate value. opLists that use the
414 // SB should clear it properly.
Robert Phillipscb2e2352017-08-30 16:44:40 -0400415 if (newStencil) {
Brian Salomond28a79d2017-10-16 13:01:07 -0400416 SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
Robert Phillipscb2e2352017-08-30 16:44:40 -0400417 this->gpu()->clearStencil(rt, 0xFFFF);
Brian Salomond28a79d2017-10-16 13:01:07 -0400418 SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
Robert Phillipscb2e2352017-08-30 16:44:40 -0400419 }
Robert Phillips95214472017-08-08 18:00:03 -0400420#endif
egdanielec00d942015-09-14 12:56:10 -0700421 }
422 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400423 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700424}
425
bungeman6bd52842016-10-27 09:30:08 -0700426sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400427 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700428{
ericrkf7b8b8a2016-02-24 14:49:51 -0800429 if (this->isAbandoned()) {
430 return nullptr;
431 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500432 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800433}
Greg Danield85f97d2017-03-07 13:37:21 -0500434
Greg Daniela5cb7812017-06-16 09:45:32 -0400435sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
436 return fGpu->makeSemaphore(isOwned);
437}
438
439sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500440 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400441 GrWrapOwnership ownership) {
442 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500443 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
444 wrapType,
445 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500446}
447
448void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
449 semaphore->resetGpu(fGpu);
450}
451
452void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
453 semaphore->resetGpu(nullptr);
454}