blob: 2eb82714a2a60145d073092cd4c29799f2dc5f5c [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
cdalton397536c2016-03-25 12:15:03 -070010#include "GrBuffer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070011#include "GrCaps.h"
Robert Phillips26c90e02017-03-14 14:39:29 -040012#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040013#include "GrContextPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070014#include "GrGpu.h"
kkinnunencabe20c2015-06-01 01:37:26 -070015#include "GrPathRendering.h"
egdanielec00d942015-09-14 12:56:10 -070016#include "GrRenderTarget.h"
17#include "GrRenderTargetPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070018#include "GrResourceCache.h"
19#include "GrResourceKey.h"
Greg Danield85f97d2017-03-07 13:37:21 -050020#include "GrSemaphore.h"
egdanielec00d942015-09-14 12:56:10 -070021#include "GrStencilAttachment.h"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040022#include "GrSurfaceProxyPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050023#include "GrTexturePriv.h"
24#include "../private/GrSingleOwner.h"
Robert Phillips45fdae12017-04-17 12:57:27 -040025#include "SkGr.h"
halcanary4dbbd042016-06-07 17:21:10 -070026#include "SkMathPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070027
28GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
29
Brian Osman32342f02017-03-04 08:12:46 -050030const int GrResourceProvider::kMinScratchTextureSize = 16;
31
32#define ASSERT_SINGLE_OWNER \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34
joshualitt6d0872d2016-01-11 08:27:48 -080035GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050036 : fCache(cache)
37 , fGpu(gpu)
38#ifdef SK_DEBUG
39 , fSingleOwner(owner)
40#endif
41 {
Robert Phillips26c90e02017-03-14 14:39:29 -040042 fCaps = sk_ref_sp(fGpu->caps());
43
bsalomoned0bcad2015-05-04 10:36:42 -070044 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
45 fQuadIndexBufferKey = gQuadIndexBufferKey;
46}
47
Robert Phillipsf7a72612017-03-31 10:03:45 -040048bool GrResourceProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040049 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
50}
Brian Osman32342f02017-03-04 08:12:46 -050051
Brian Salomond34edf32017-05-19 15:45:48 -040052bool validate_desc(const GrSurfaceDesc& desc, const GrCaps& caps, int levelCount = 0) {
53 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
54 return false;
55 }
56 if (!caps.isConfigTexturable(desc.fConfig)) {
57 return false;
58 }
59 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
60 if (!caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
61 return false;
62 }
63 } else {
64 if (desc.fSampleCnt) {
65 return false;
66 }
67 }
68 if (levelCount > 1 && GrPixelConfigIsSint(desc.fConfig)) {
69 return false;
70 }
71 return true;
72}
73
Robert Phillipse78b7252017-04-06 07:59:41 -040074// MDB TODO: this should probably be a factory on GrSurfaceProxy
75sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
76 const GrSurfaceDesc& desc,
77 SkBudgeted budgeted,
78 const GrMipLevel* texels,
79 int mipLevelCount,
Robert Phillipsa4c41b32017-03-15 13:02:45 -040080 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050081 ASSERT_SINGLE_OWNER
82
Robert Phillips1119dc32017-04-11 12:54:57 -040083 if (!mipLevelCount) {
84 if (texels) {
85 return nullptr;
86 }
87 return GrSurfaceProxy::MakeDeferred(this, desc, budgeted, nullptr, 0);
Robert Phillips45fdae12017-04-17 12:57:27 -040088 } else if (1 == mipLevelCount) {
89 if (!texels) {
90 return nullptr;
91 }
92 return this->createTextureProxy(desc, budgeted, texels[0]);
Robert Phillips1119dc32017-04-11 12:54:57 -040093 }
94
Brian Osman32342f02017-03-04 08:12:46 -050095 if (this->isAbandoned()) {
96 return nullptr;
97 }
Robert Phillips1119dc32017-04-11 12:54:57 -040098
Brian Salomond34edf32017-05-19 15:45:48 -040099 if (!validate_desc(desc, *fCaps, mipLevelCount)) {
Brian Osman32342f02017-03-04 08:12:46 -0500100 return nullptr;
101 }
Brian Osman32342f02017-03-04 08:12:46 -0500102
103 SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount);
104 for (int i = 0; i < mipLevelCount; ++i) {
Robert Phillips45fdae12017-04-17 12:57:27 -0400105 if (!texels[i].fPixels) {
106 return nullptr;
107 }
108
Brian Osman32342f02017-03-04 08:12:46 -0500109 texelsShallowCopy.push_back(texels[i]);
110 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400111 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texelsShallowCopy));
112 if (tex) {
113 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400114 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400115
116 return GrSurfaceProxy::MakeWrapped(std::move(tex));
Brian Osman32342f02017-03-04 08:12:46 -0500117}
118
Robert Phillips45fdae12017-04-17 12:57:27 -0400119sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
120 SkBudgeted budgeted, uint32_t flags) {
121
122 flags |= kExact_Flag | kNoCreate_Flag;
123 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
124 if (tex && SkBudgeted::kNo == budgeted) {
125 tex->resourcePriv().makeUnbudgeted();
126 }
127
128 return tex;
129}
130
131static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
132 SkColorType colorType;
133 if (!GrPixelConfigToColorType(config, &colorType)) {
134 return false;
135 }
136
137 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
138 return true;
139}
140
141sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
142 SkBudgeted budgeted,
143 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400144 ASSERT_SINGLE_OWNER
145
146 if (this->isAbandoned()) {
147 return nullptr;
148 }
149
Robert Phillips45fdae12017-04-17 12:57:27 -0400150 if (!mipLevel.fPixels) {
151 return nullptr;
152 }
153
Brian Salomond34edf32017-05-19 15:45:48 -0400154 if (!validate_desc(desc, *fCaps)) {
155 return nullptr;
156 }
157
Robert Phillips45fdae12017-04-17 12:57:27 -0400158 GrContext* context = fGpu->getContext();
159
160 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
161 SkImageInfo srcInfo;
162
163 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
164 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
165 sk_sp<GrSurfaceContext> sContext =
166 context->contextPriv().makeWrappedSurfaceContext(std::move(tex));
167 if (sContext) {
168 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
169 return sContext->asTextureProxyRef();
170 }
171 }
172 }
173 }
174
175 SkTArray<GrMipLevel> texels(1);
176 texels.push_back(mipLevel);
177
178 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels));
179 return GrSurfaceProxy::MakeWrapped(std::move(tex));
180}
181
182
Robert Phillipse78b7252017-04-06 07:59:41 -0400183sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
184 uint32_t flags) {
185 ASSERT_SINGLE_OWNER
186
187 if (this->isAbandoned()) {
188 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500189 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400190
Brian Salomond34edf32017-05-19 15:45:48 -0400191 if (!validate_desc(desc, *fCaps)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400192 return nullptr;
193 }
194
195 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Robert Phillips45fdae12017-04-17 12:57:27 -0400196 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillipse78b7252017-04-06 07:59:41 -0400197 if (tex) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400198 return tex;
199 }
200 }
201
202 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted));
203 return tex;
Brian Osman32342f02017-03-04 08:12:46 -0500204}
205
206GrTexture* GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
207 ASSERT_SINGLE_OWNER
208 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500209
Brian Osman32342f02017-03-04 08:12:46 -0500210 if (this->isAbandoned()) {
211 return nullptr;
212 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400213
Brian Osman32342f02017-03-04 08:12:46 -0500214 // Currently we don't recycle compressed textures as scratch.
215 if (GrPixelConfigIsCompressed(desc.fConfig)) {
216 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500217 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400218
Brian Salomond34edf32017-05-19 15:45:48 -0400219 if (!validate_desc(desc, *fCaps)) {
220 return nullptr;
221 }
222
Robert Phillips1119dc32017-04-11 12:54:57 -0400223 return this->refScratchTexture(desc, flags);
Brian Osman32342f02017-03-04 08:12:46 -0500224}
225
Brian Salomond34edf32017-05-19 15:45:48 -0400226GrTexture* GrResourceProvider::refScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500227 ASSERT_SINGLE_OWNER
228 SkASSERT(!this->isAbandoned());
229 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
Brian Salomond34edf32017-05-19 15:45:48 -0400230 SkASSERT(validate_desc(inDesc, *fCaps));
Brian Osman32342f02017-03-04 08:12:46 -0500231
232 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
233
234 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
235 if (!(kExact_Flag & flags)) {
236 // bin by pow2 with a reasonable min
237 GrSurfaceDesc* wdesc = desc.writable();
238 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth));
239 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight));
240 }
241
242 GrScratchKey key;
243 GrTexturePriv::ComputeScratchKey(*desc, &key);
244 uint32_t scratchFlags = 0;
245 if (kNoPendingIO_Flag & flags) {
246 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
247 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
248 // If it is not a render target then it will most likely be populated by
249 // writePixels() which will trigger a flush if the texture has pending IO.
250 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
251 }
252 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
253 GrSurface::WorstCaseSize(*desc),
254 scratchFlags);
255 if (resource) {
256 GrSurface* surface = static_cast<GrSurface*>(resource);
Brian Osman32342f02017-03-04 08:12:46 -0500257 return surface->asTexture();
258 }
259 }
260
261 if (!(kNoCreate_Flag & flags)) {
262 return fGpu->createTexture(*desc, SkBudgeted::kYes);
263 }
264
265 return nullptr;
266}
267
Greg Daniel7ef28f32017-04-20 16:41:55 +0000268sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
269 GrSurfaceOrigin origin,
270 GrBackendTextureFlags flags,
271 int sampleCnt,
Brian Osman32342f02017-03-04 08:12:46 -0500272 GrWrapOwnership ownership) {
273 ASSERT_SINGLE_OWNER
274 if (this->isAbandoned()) {
275 return nullptr;
276 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000277 return fGpu->wrapBackendTexture(tex, origin, flags, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500278}
279
280sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Greg Danielbcf612b2017-05-01 13:50:58 +0000281 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin)
Brian Osman32342f02017-03-04 08:12:46 -0500282{
283 ASSERT_SINGLE_OWNER
Greg Danielbcf612b2017-05-01 13:50:58 +0000284 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, origin);
Brian Osman32342f02017-03-04 08:12:46 -0500285}
286
287void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
288 GrGpuResource* resource) {
289 ASSERT_SINGLE_OWNER
290 if (this->isAbandoned() || !resource) {
291 return;
292 }
293 resource->resourcePriv().setUniqueKey(key);
294}
295
296GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
297 ASSERT_SINGLE_OWNER
298 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
299}
300
301GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
302 ASSERT_SINGLE_OWNER
303 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
304 if (resource) {
305 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
306 SkASSERT(texture);
307 return texture;
308 }
309 return NULL;
310}
311
Robert Phillipsd3749482017-03-14 09:17:43 -0400312// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
313void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
314 ASSERT_SINGLE_OWNER
315 SkASSERT(key.isValid());
316 if (this->isAbandoned() || !proxy) {
317 return;
318 }
319
Brian Salomonbb5711a2017-05-17 13:49:59 -0400320 GrTexture* texture = proxy->instantiateTexture(this);
Robert Phillipsd3749482017-03-14 09:17:43 -0400321 if (!texture) {
322 return;
323 }
324
325 this->assignUniqueKeyToResource(key, texture);
326}
327
328// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
329sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
330 ASSERT_SINGLE_OWNER
331
332 sk_sp<GrTexture> texture(this->findAndRefTextureByUniqueKey(key));
333 if (!texture) {
334 return nullptr;
335 }
336
337 return GrSurfaceProxy::MakeWrapped(std::move(texture));
338}
339
Chris Daltonff926502017-05-03 14:36:54 -0400340const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700341 int patternSize,
342 int reps,
343 int vertCount,
344 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700345 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
346
Brian Salomon09d994e2016-12-21 11:14:46 -0500347 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700348 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700349 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700350 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700351 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700352 }
353 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -0700354 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -0700355 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -0700356 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -0700357 }
358 for (int i = 0; i < reps; ++i) {
359 int baseIdx = i * patternSize;
360 uint16_t baseVert = (uint16_t)(i * vertCount);
361 for (int j = 0; j < patternSize; ++j) {
362 data[baseIdx+j] = baseVert + pattern[j];
363 }
364 }
365 if (useTempData) {
366 if (!buffer->updateData(data, bufferSize)) {
367 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700368 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700369 }
halcanary385fe4d2015-08-26 13:07:48 -0700370 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -0700371 } else {
372 buffer->unmap();
373 }
374 this->assignUniqueKeyToResource(key, buffer);
375 return buffer;
376}
377
cdalton397536c2016-03-25 12:15:03 -0700378const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700379 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
380 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
381 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
382
Chris Daltonff926502017-05-03 14:36:54 -0400383 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700384}
385
bsalomon6663acf2016-05-10 09:14:17 -0700386GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700387 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700388 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700389}
390
391GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
bsalomon6663acf2016-05-10 09:14:17 -0700392 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700393 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700394 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700395}
396
reeda9322c22016-04-12 06:47:05 -0700397GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf,
398 const SkScalerContextEffects& effects,
399 const SkDescriptor* desc,
bsalomon6663acf2016-05-10 09:14:17 -0700400 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700401
402 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700403 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700404}
405
cdaltone2e71c22016-04-07 18:13:29 -0700406GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700407 GrAccessPattern accessPattern, uint32_t flags,
408 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700409 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700410 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700411 }
cdaltond37fe762016-04-21 07:41:50 -0700412 if (kDynamic_GrAccessPattern != accessPattern) {
413 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
414 }
csmartdalton485a1202016-07-13 10:16:32 -0700415 if (!(flags & kRequireGpuMemory_Flag) &&
416 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
417 GrBufferTypeIsVertexOrIndex(intendedType) &&
418 kDynamic_GrAccessPattern == accessPattern) {
419 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
420 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700421
cdaltond37fe762016-04-21 07:41:50 -0700422 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400423 static const size_t MIN_SIZE = 1 << 12;
424 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700425
cdaltond37fe762016-04-21 07:41:50 -0700426 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700427 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700428 uint32_t scratchFlags = 0;
429 if (flags & kNoPendingIO_Flag) {
430 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
431 } else {
432 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
433 }
434 GrBuffer* buffer = static_cast<GrBuffer*>(
435 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
436 if (!buffer) {
437 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
438 if (!buffer) {
439 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700440 }
441 }
cdaltond37fe762016-04-21 07:41:50 -0700442 if (data) {
443 buffer->updateData(data, size);
444 }
csmartdalton485a1202016-07-13 10:16:32 -0700445 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700446 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800447}
448
egdanielec00d942015-09-14 12:56:10 -0700449GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
450 SkASSERT(rt);
451 if (rt->renderTargetPriv().getStencilAttachment()) {
452 return rt->renderTargetPriv().getStencilAttachment();
453 }
454
455 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
456 GrUniqueKey sbKey;
457
458 int width = rt->width();
459 int height = rt->height();
460#if 0
461 if (this->caps()->oversizedStencilSupport()) {
462 width = SkNextPow2(width);
463 height = SkNextPow2(height);
464 }
465#endif
466 bool newStencil = false;
467 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
468 rt->numStencilSamples(), &sbKey);
469 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
470 this->findAndRefResourceByUniqueKey(sbKey));
471 if (!stencil) {
472 // Need to try and create a new stencil
473 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
474 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500475 this->assignUniqueKeyToResource(sbKey, stencil);
egdanielec00d942015-09-14 12:56:10 -0700476 newStencil = true;
477 }
478 }
479 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
480 if (newStencil) {
481 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800482 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700483 // stencil buffers with smaller color targets this will no longer
484 // be correct because it won't be guaranteed to clear the entire
485 // sb.
486 // We used to clear down in the GL subclass using a special purpose
487 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
488 // FBO status.
489 this->gpu()->clearStencil(rt);
490 }
491 }
492 }
493 return rt->renderTargetPriv().getStencilAttachment();
494}
495
bungeman6bd52842016-10-27 09:30:08 -0700496sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000497 const GrBackendTexture& tex, GrSurfaceOrigin origin, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700498{
ericrkf7b8b8a2016-02-24 14:49:51 -0800499 if (this->isAbandoned()) {
500 return nullptr;
501 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000502 return this->gpu()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800503}
Greg Danield85f97d2017-03-07 13:37:21 -0500504
505sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore() {
506 return fGpu->makeSemaphore();
507}
508
509void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
510 semaphore->resetGpu(fGpu);
511}
512
513void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
514 semaphore->resetGpu(nullptr);
515}