blob: e7252dec3dee48bca91d821d8ab3f3734f0328d4 [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
Greg Daniel7ef28f32017-04-20 16:41:55 +0000243sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
244 GrSurfaceOrigin origin,
245 GrBackendTextureFlags flags,
246 int sampleCnt,
Brian Osman32342f02017-03-04 08:12:46 -0500247 GrWrapOwnership ownership) {
248 ASSERT_SINGLE_OWNER
249 if (this->isAbandoned()) {
250 return nullptr;
251 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000252 return fGpu->wrapBackendTexture(tex, origin, flags, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500253}
254
255sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
256 const GrBackendRenderTargetDesc& desc)
257{
258 ASSERT_SINGLE_OWNER
Brian Osman0b791f52017-03-10 08:30:22 -0500259 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc);
Brian Osman32342f02017-03-04 08:12:46 -0500260}
261
262void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
263 GrGpuResource* resource) {
264 ASSERT_SINGLE_OWNER
265 if (this->isAbandoned() || !resource) {
266 return;
267 }
268 resource->resourcePriv().setUniqueKey(key);
269}
270
271GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
272 ASSERT_SINGLE_OWNER
273 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
274}
275
276GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
277 ASSERT_SINGLE_OWNER
278 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
279 if (resource) {
280 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
281 SkASSERT(texture);
282 return texture;
283 }
284 return NULL;
285}
286
Robert Phillipsd3749482017-03-14 09:17:43 -0400287// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
288void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
289 ASSERT_SINGLE_OWNER
290 SkASSERT(key.isValid());
291 if (this->isAbandoned() || !proxy) {
292 return;
293 }
294
295 GrTexture* texture = proxy->instantiate(this);
296 if (!texture) {
297 return;
298 }
299
300 this->assignUniqueKeyToResource(key, texture);
301}
302
303// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
304sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
305 ASSERT_SINGLE_OWNER
306
307 sk_sp<GrTexture> texture(this->findAndRefTextureByUniqueKey(key));
308 if (!texture) {
309 return nullptr;
310 }
311
312 return GrSurfaceProxy::MakeWrapped(std::move(texture));
313}
314
cdalton397536c2016-03-25 12:15:03 -0700315const GrBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
316 int patternSize,
317 int reps,
318 int vertCount,
319 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700320 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
321
Brian Salomon09d994e2016-12-21 11:14:46 -0500322 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700323 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700324 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700325 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700326 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700327 }
328 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -0700329 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -0700330 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -0700331 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -0700332 }
333 for (int i = 0; i < reps; ++i) {
334 int baseIdx = i * patternSize;
335 uint16_t baseVert = (uint16_t)(i * vertCount);
336 for (int j = 0; j < patternSize; ++j) {
337 data[baseIdx+j] = baseVert + pattern[j];
338 }
339 }
340 if (useTempData) {
341 if (!buffer->updateData(data, bufferSize)) {
342 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700343 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700344 }
halcanary385fe4d2015-08-26 13:07:48 -0700345 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -0700346 } else {
347 buffer->unmap();
348 }
349 this->assignUniqueKeyToResource(key, buffer);
350 return buffer;
351}
352
cdalton397536c2016-03-25 12:15:03 -0700353const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700354 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
355 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
356 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
357
358 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
359}
360
bsalomon6663acf2016-05-10 09:14:17 -0700361GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700362 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700363 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700364}
365
366GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
bsalomon6663acf2016-05-10 09:14:17 -0700367 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700368 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700369 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700370}
371
reeda9322c22016-04-12 06:47:05 -0700372GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf,
373 const SkScalerContextEffects& effects,
374 const SkDescriptor* desc,
bsalomon6663acf2016-05-10 09:14:17 -0700375 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700376
377 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700378 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700379}
380
cdaltone2e71c22016-04-07 18:13:29 -0700381GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700382 GrAccessPattern accessPattern, uint32_t flags,
383 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700384 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700385 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700386 }
cdaltond37fe762016-04-21 07:41:50 -0700387 if (kDynamic_GrAccessPattern != accessPattern) {
388 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
389 }
csmartdalton485a1202016-07-13 10:16:32 -0700390 if (!(flags & kRequireGpuMemory_Flag) &&
391 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
392 GrBufferTypeIsVertexOrIndex(intendedType) &&
393 kDynamic_GrAccessPattern == accessPattern) {
394 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
395 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700396
cdaltond37fe762016-04-21 07:41:50 -0700397 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400398 static const size_t MIN_SIZE = 1 << 12;
399 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700400
cdaltond37fe762016-04-21 07:41:50 -0700401 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700402 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700403 uint32_t scratchFlags = 0;
404 if (flags & kNoPendingIO_Flag) {
405 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
406 } else {
407 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
408 }
409 GrBuffer* buffer = static_cast<GrBuffer*>(
410 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
411 if (!buffer) {
412 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
413 if (!buffer) {
414 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700415 }
416 }
cdaltond37fe762016-04-21 07:41:50 -0700417 if (data) {
418 buffer->updateData(data, size);
419 }
csmartdalton485a1202016-07-13 10:16:32 -0700420 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700421 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800422}
423
egdanielec00d942015-09-14 12:56:10 -0700424GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
425 SkASSERT(rt);
426 if (rt->renderTargetPriv().getStencilAttachment()) {
427 return rt->renderTargetPriv().getStencilAttachment();
428 }
429
430 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
431 GrUniqueKey sbKey;
432
433 int width = rt->width();
434 int height = rt->height();
435#if 0
436 if (this->caps()->oversizedStencilSupport()) {
437 width = SkNextPow2(width);
438 height = SkNextPow2(height);
439 }
440#endif
441 bool newStencil = false;
442 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
443 rt->numStencilSamples(), &sbKey);
444 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
445 this->findAndRefResourceByUniqueKey(sbKey));
446 if (!stencil) {
447 // Need to try and create a new stencil
448 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
449 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500450 this->assignUniqueKeyToResource(sbKey, stencil);
egdanielec00d942015-09-14 12:56:10 -0700451 newStencil = true;
452 }
453 }
454 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
455 if (newStencil) {
456 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800457 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700458 // stencil buffers with smaller color targets this will no longer
459 // be correct because it won't be guaranteed to clear the entire
460 // sb.
461 // We used to clear down in the GL subclass using a special purpose
462 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
463 // FBO status.
464 this->gpu()->clearStencil(rt);
465 }
466 }
467 }
468 return rt->renderTargetPriv().getStencilAttachment();
469}
470
bungeman6bd52842016-10-27 09:30:08 -0700471sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000472 const GrBackendTexture& tex, GrSurfaceOrigin origin, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700473{
ericrkf7b8b8a2016-02-24 14:49:51 -0800474 if (this->isAbandoned()) {
475 return nullptr;
476 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000477 return this->gpu()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800478}
Greg Danield85f97d2017-03-07 13:37:21 -0500479
480sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore() {
481 return fGpu->makeSemaphore();
482}
483
484void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
485 semaphore->resetGpu(fGpu);
486}
487
488void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
489 semaphore->resetGpu(nullptr);
490}