blob: 1226f9b9f61107476dc6b9329ff8c9fbdfaa6da7 [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
Robert Phillipse78b7252017-04-06 07:59:41 -040052// MDB TODO: this should probably be a factory on GrSurfaceProxy
53sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
54 const GrSurfaceDesc& desc,
55 SkBudgeted budgeted,
56 const GrMipLevel* texels,
57 int mipLevelCount,
Robert Phillipsa4c41b32017-03-15 13:02:45 -040058 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050059 ASSERT_SINGLE_OWNER
60
Robert Phillips1119dc32017-04-11 12:54:57 -040061 if (!mipLevelCount) {
62 if (texels) {
63 return nullptr;
64 }
65 return GrSurfaceProxy::MakeDeferred(this, desc, budgeted, nullptr, 0);
Robert Phillips45fdae12017-04-17 12:57:27 -040066 } else if (1 == mipLevelCount) {
67 if (!texels) {
68 return nullptr;
69 }
70 return this->createTextureProxy(desc, budgeted, texels[0]);
Robert Phillips1119dc32017-04-11 12:54:57 -040071 }
72
Brian Osman32342f02017-03-04 08:12:46 -050073 if (this->isAbandoned()) {
74 return nullptr;
75 }
Robert Phillips1119dc32017-04-11 12:54:57 -040076
Brian Osman32342f02017-03-04 08:12:46 -050077 if (mipLevelCount > 1 && GrPixelConfigIsSint(desc.fConfig)) {
78 return nullptr;
79 }
80 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
81 !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
82 return nullptr;
83 }
Brian Osman32342f02017-03-04 08:12:46 -050084
85 SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount);
86 for (int i = 0; i < mipLevelCount; ++i) {
Robert Phillips45fdae12017-04-17 12:57:27 -040087 if (!texels[i].fPixels) {
88 return nullptr;
89 }
90
Brian Osman32342f02017-03-04 08:12:46 -050091 texelsShallowCopy.push_back(texels[i]);
92 }
Robert Phillipse78b7252017-04-06 07:59:41 -040093 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texelsShallowCopy));
94 if (tex) {
95 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -040096 }
Robert Phillipse78b7252017-04-06 07:59:41 -040097
98 return GrSurfaceProxy::MakeWrapped(std::move(tex));
Brian Osman32342f02017-03-04 08:12:46 -050099}
100
Robert Phillips45fdae12017-04-17 12:57:27 -0400101sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
102 SkBudgeted budgeted, uint32_t flags) {
103
104 flags |= kExact_Flag | kNoCreate_Flag;
105 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
106 if (tex && SkBudgeted::kNo == budgeted) {
107 tex->resourcePriv().makeUnbudgeted();
108 }
109
110 return tex;
111}
112
113static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
114 SkColorType colorType;
115 if (!GrPixelConfigToColorType(config, &colorType)) {
116 return false;
117 }
118
119 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
120 return true;
121}
122
123sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
124 SkBudgeted budgeted,
125 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400126 ASSERT_SINGLE_OWNER
127
128 if (this->isAbandoned()) {
129 return nullptr;
130 }
131
Robert Phillips45fdae12017-04-17 12:57:27 -0400132 if (!mipLevel.fPixels) {
133 return nullptr;
134 }
135
136 GrContext* context = fGpu->getContext();
137
138 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
139 SkImageInfo srcInfo;
140
141 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
142 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
143 sk_sp<GrSurfaceContext> sContext =
144 context->contextPriv().makeWrappedSurfaceContext(std::move(tex));
145 if (sContext) {
146 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
147 return sContext->asTextureProxyRef();
148 }
149 }
150 }
151 }
152
153 SkTArray<GrMipLevel> texels(1);
154 texels.push_back(mipLevel);
155
156 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels));
157 return GrSurfaceProxy::MakeWrapped(std::move(tex));
158}
159
160
Robert Phillipse78b7252017-04-06 07:59:41 -0400161sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
162 uint32_t flags) {
163 ASSERT_SINGLE_OWNER
164
165 if (this->isAbandoned()) {
166 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500167 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400168
169 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
170 !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
171 return nullptr;
172 }
173
174 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Robert Phillips45fdae12017-04-17 12:57:27 -0400175 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillipse78b7252017-04-06 07:59:41 -0400176 if (tex) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400177 return tex;
178 }
179 }
180
181 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted));
182 return tex;
Brian Osman32342f02017-03-04 08:12:46 -0500183}
184
185GrTexture* GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
186 ASSERT_SINGLE_OWNER
187 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500188
Brian Osman32342f02017-03-04 08:12:46 -0500189 if (this->isAbandoned()) {
190 return nullptr;
191 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400192
Brian Osman32342f02017-03-04 08:12:46 -0500193 // Currently we don't recycle compressed textures as scratch.
194 if (GrPixelConfigIsCompressed(desc.fConfig)) {
195 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500196 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400197
198 return this->refScratchTexture(desc, flags);
Brian Osman32342f02017-03-04 08:12:46 -0500199}
200
201GrTexture* GrResourceProvider::refScratchTexture(const GrSurfaceDesc& inDesc,
202 uint32_t flags) {
203 ASSERT_SINGLE_OWNER
204 SkASSERT(!this->isAbandoned());
205 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
206
207 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
208
209 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
210 if (!(kExact_Flag & flags)) {
211 // bin by pow2 with a reasonable min
212 GrSurfaceDesc* wdesc = desc.writable();
213 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth));
214 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight));
215 }
216
217 GrScratchKey key;
218 GrTexturePriv::ComputeScratchKey(*desc, &key);
219 uint32_t scratchFlags = 0;
220 if (kNoPendingIO_Flag & flags) {
221 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
222 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
223 // If it is not a render target then it will most likely be populated by
224 // writePixels() which will trigger a flush if the texture has pending IO.
225 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
226 }
227 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
228 GrSurface::WorstCaseSize(*desc),
229 scratchFlags);
230 if (resource) {
231 GrSurface* surface = static_cast<GrSurface*>(resource);
Brian Osman32342f02017-03-04 08:12:46 -0500232 return surface->asTexture();
233 }
234 }
235
236 if (!(kNoCreate_Flag & flags)) {
237 return fGpu->createTexture(*desc, SkBudgeted::kYes);
238 }
239
240 return nullptr;
241}
242
Stan Iliev7fa5c312017-04-19 00:23:39 +0000243sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTextureDesc& desc,
Brian Osman32342f02017-03-04 08:12:46 -0500244 GrWrapOwnership ownership) {
245 ASSERT_SINGLE_OWNER
246 if (this->isAbandoned()) {
247 return nullptr;
248 }
Stan Iliev7fa5c312017-04-19 00:23:39 +0000249 return fGpu->wrapBackendTexture(desc, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500250}
251
252sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
253 const GrBackendRenderTargetDesc& desc)
254{
255 ASSERT_SINGLE_OWNER
Brian Osman0b791f52017-03-10 08:30:22 -0500256 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc);
Brian Osman32342f02017-03-04 08:12:46 -0500257}
258
259void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
260 GrGpuResource* resource) {
261 ASSERT_SINGLE_OWNER
262 if (this->isAbandoned() || !resource) {
263 return;
264 }
265 resource->resourcePriv().setUniqueKey(key);
266}
267
268GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
269 ASSERT_SINGLE_OWNER
270 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
271}
272
273GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
274 ASSERT_SINGLE_OWNER
275 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
276 if (resource) {
277 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
278 SkASSERT(texture);
279 return texture;
280 }
281 return NULL;
282}
283
Robert Phillipsd3749482017-03-14 09:17:43 -0400284// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
285void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
286 ASSERT_SINGLE_OWNER
287 SkASSERT(key.isValid());
288 if (this->isAbandoned() || !proxy) {
289 return;
290 }
291
292 GrTexture* texture = proxy->instantiate(this);
293 if (!texture) {
294 return;
295 }
296
297 this->assignUniqueKeyToResource(key, texture);
298}
299
300// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
301sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
302 ASSERT_SINGLE_OWNER
303
304 sk_sp<GrTexture> texture(this->findAndRefTextureByUniqueKey(key));
305 if (!texture) {
306 return nullptr;
307 }
308
309 return GrSurfaceProxy::MakeWrapped(std::move(texture));
310}
311
cdalton397536c2016-03-25 12:15:03 -0700312const GrBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
313 int patternSize,
314 int reps,
315 int vertCount,
316 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700317 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
318
Brian Salomon09d994e2016-12-21 11:14:46 -0500319 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700320 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700321 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700322 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700323 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700324 }
325 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -0700326 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -0700327 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -0700328 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -0700329 }
330 for (int i = 0; i < reps; ++i) {
331 int baseIdx = i * patternSize;
332 uint16_t baseVert = (uint16_t)(i * vertCount);
333 for (int j = 0; j < patternSize; ++j) {
334 data[baseIdx+j] = baseVert + pattern[j];
335 }
336 }
337 if (useTempData) {
338 if (!buffer->updateData(data, bufferSize)) {
339 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700340 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700341 }
halcanary385fe4d2015-08-26 13:07:48 -0700342 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -0700343 } else {
344 buffer->unmap();
345 }
346 this->assignUniqueKeyToResource(key, buffer);
347 return buffer;
348}
349
cdalton397536c2016-03-25 12:15:03 -0700350const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700351 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
352 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
353 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
354
355 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
356}
357
bsalomon6663acf2016-05-10 09:14:17 -0700358GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700359 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700360 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700361}
362
363GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
bsalomon6663acf2016-05-10 09:14:17 -0700364 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700365 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700366 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700367}
368
reeda9322c22016-04-12 06:47:05 -0700369GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf,
370 const SkScalerContextEffects& effects,
371 const SkDescriptor* desc,
bsalomon6663acf2016-05-10 09:14:17 -0700372 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700373
374 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700375 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700376}
377
cdaltone2e71c22016-04-07 18:13:29 -0700378GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700379 GrAccessPattern accessPattern, uint32_t flags,
380 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700381 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700382 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700383 }
cdaltond37fe762016-04-21 07:41:50 -0700384 if (kDynamic_GrAccessPattern != accessPattern) {
385 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
386 }
csmartdalton485a1202016-07-13 10:16:32 -0700387 if (!(flags & kRequireGpuMemory_Flag) &&
388 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
389 GrBufferTypeIsVertexOrIndex(intendedType) &&
390 kDynamic_GrAccessPattern == accessPattern) {
391 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
392 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700393
cdaltond37fe762016-04-21 07:41:50 -0700394 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400395 static const size_t MIN_SIZE = 1 << 12;
396 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700397
cdaltond37fe762016-04-21 07:41:50 -0700398 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700399 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700400 uint32_t scratchFlags = 0;
401 if (flags & kNoPendingIO_Flag) {
402 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
403 } else {
404 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
405 }
406 GrBuffer* buffer = static_cast<GrBuffer*>(
407 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
408 if (!buffer) {
409 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
410 if (!buffer) {
411 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700412 }
413 }
cdaltond37fe762016-04-21 07:41:50 -0700414 if (data) {
415 buffer->updateData(data, size);
416 }
csmartdalton485a1202016-07-13 10:16:32 -0700417 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700418 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800419}
420
egdanielec00d942015-09-14 12:56:10 -0700421GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
422 SkASSERT(rt);
423 if (rt->renderTargetPriv().getStencilAttachment()) {
424 return rt->renderTargetPriv().getStencilAttachment();
425 }
426
427 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
428 GrUniqueKey sbKey;
429
430 int width = rt->width();
431 int height = rt->height();
432#if 0
433 if (this->caps()->oversizedStencilSupport()) {
434 width = SkNextPow2(width);
435 height = SkNextPow2(height);
436 }
437#endif
438 bool newStencil = false;
439 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
440 rt->numStencilSamples(), &sbKey);
441 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
442 this->findAndRefResourceByUniqueKey(sbKey));
443 if (!stencil) {
444 // Need to try and create a new stencil
445 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
446 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500447 this->assignUniqueKeyToResource(sbKey, stencil);
egdanielec00d942015-09-14 12:56:10 -0700448 newStencil = true;
449 }
450 }
451 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
452 if (newStencil) {
453 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800454 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700455 // stencil buffers with smaller color targets this will no longer
456 // be correct because it won't be guaranteed to clear the entire
457 // sb.
458 // We used to clear down in the GL subclass using a special purpose
459 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
460 // FBO status.
461 this->gpu()->clearStencil(rt);
462 }
463 }
464 }
465 return rt->renderTargetPriv().getStencilAttachment();
466}
467
bungeman6bd52842016-10-27 09:30:08 -0700468sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Stan Iliev7fa5c312017-04-19 00:23:39 +0000469 const GrBackendTextureDesc& desc)
bungeman6bd52842016-10-27 09:30:08 -0700470{
ericrkf7b8b8a2016-02-24 14:49:51 -0800471 if (this->isAbandoned()) {
472 return nullptr;
473 }
Stan Iliev7fa5c312017-04-19 00:23:39 +0000474 return this->gpu()->wrapBackendTextureAsRenderTarget(desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800475}
Greg Danield85f97d2017-03-07 13:37:21 -0500476
477sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore() {
478 return fGpu->makeSemaphore();
479}
480
481void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
482 semaphore->resetGpu(fGpu);
483}
484
485void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
486 semaphore->resetGpu(nullptr);
487}