blob: dccfaae8753d1db9bf5893b521c18ffa7539dea4 [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"
egdanielec00d942015-09-14 12:56:10 -070018#include "GrRenderTarget.h"
19#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"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040024#include "GrSurfaceProxyPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050025#include "GrTexturePriv.h"
26#include "../private/GrSingleOwner.h"
Robert Phillips45fdae12017-04-17 12:57:27 -040027#include "SkGr.h"
halcanary4dbbd042016-06-07 17:21:10 -070028#include "SkMathPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070029
30GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
31
Robert Phillips1bfece82017-06-01 13:56:52 -040032const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050033
34#define ASSERT_SINGLE_OWNER \
35 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
36
joshualitt6d0872d2016-01-11 08:27:48 -080037GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050038 : fCache(cache)
39 , fGpu(gpu)
40#ifdef SK_DEBUG
41 , fSingleOwner(owner)
42#endif
43 {
Robert Phillips26c90e02017-03-14 14:39:29 -040044 fCaps = sk_ref_sp(fGpu->caps());
45
bsalomoned0bcad2015-05-04 10:36:42 -070046 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
47 fQuadIndexBufferKey = gQuadIndexBufferKey;
48}
49
Robert Phillipsf7a72612017-03-31 10:03:45 -040050bool GrResourceProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040051 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
52}
Brian Osman32342f02017-03-04 08:12:46 -050053
Brian Salomond34edf32017-05-19 15:45:48 -040054bool validate_desc(const GrSurfaceDesc& desc, const GrCaps& caps, int levelCount = 0) {
55 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
56 return false;
57 }
58 if (!caps.isConfigTexturable(desc.fConfig)) {
59 return false;
60 }
61 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
62 if (!caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
63 return false;
64 }
65 } else {
66 if (desc.fSampleCnt) {
67 return false;
68 }
69 }
Brian Osman48c99192017-06-02 08:45:06 -040070 if (levelCount > 1 && (GrPixelConfigIsSint(desc.fConfig) || !caps.mipMapSupport())) {
Brian Salomond34edf32017-05-19 15:45:48 -040071 return false;
72 }
73 return true;
74}
75
Robert Phillipse78b7252017-04-06 07:59:41 -040076// MDB TODO: this should probably be a factory on GrSurfaceProxy
77sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
78 const GrSurfaceDesc& desc,
79 SkBudgeted budgeted,
80 const GrMipLevel* texels,
81 int mipLevelCount,
Robert Phillipsa4c41b32017-03-15 13:02:45 -040082 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050083 ASSERT_SINGLE_OWNER
84
Robert Phillips1119dc32017-04-11 12:54:57 -040085 if (!mipLevelCount) {
86 if (texels) {
87 return nullptr;
88 }
89 return GrSurfaceProxy::MakeDeferred(this, desc, budgeted, nullptr, 0);
Robert Phillips45fdae12017-04-17 12:57:27 -040090 } else if (1 == mipLevelCount) {
91 if (!texels) {
92 return nullptr;
93 }
94 return this->createTextureProxy(desc, budgeted, texels[0]);
Robert Phillips1119dc32017-04-11 12:54:57 -040095 }
96
Brian Osman32342f02017-03-04 08:12:46 -050097 if (this->isAbandoned()) {
98 return nullptr;
99 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400100
Brian Salomond34edf32017-05-19 15:45:48 -0400101 if (!validate_desc(desc, *fCaps, mipLevelCount)) {
Brian Osman32342f02017-03-04 08:12:46 -0500102 return nullptr;
103 }
Brian Osman32342f02017-03-04 08:12:46 -0500104
105 SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount);
106 for (int i = 0; i < mipLevelCount; ++i) {
Robert Phillips45fdae12017-04-17 12:57:27 -0400107 if (!texels[i].fPixels) {
108 return nullptr;
109 }
110
Brian Osman32342f02017-03-04 08:12:46 -0500111 texelsShallowCopy.push_back(texels[i]);
112 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400113 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texelsShallowCopy));
114 if (tex) {
115 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400116 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400117
118 return GrSurfaceProxy::MakeWrapped(std::move(tex));
Brian Osman32342f02017-03-04 08:12:46 -0500119}
120
Robert Phillips45fdae12017-04-17 12:57:27 -0400121sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
122 SkBudgeted budgeted, uint32_t flags) {
123
124 flags |= kExact_Flag | kNoCreate_Flag;
125 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
126 if (tex && SkBudgeted::kNo == budgeted) {
127 tex->resourcePriv().makeUnbudgeted();
128 }
129
130 return tex;
131}
132
133static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
134 SkColorType colorType;
135 if (!GrPixelConfigToColorType(config, &colorType)) {
136 return false;
137 }
138
139 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
140 return true;
141}
142
143sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
144 SkBudgeted budgeted,
145 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400146 ASSERT_SINGLE_OWNER
147
148 if (this->isAbandoned()) {
149 return nullptr;
150 }
151
Robert Phillips45fdae12017-04-17 12:57:27 -0400152 if (!mipLevel.fPixels) {
153 return nullptr;
154 }
155
Brian Salomond34edf32017-05-19 15:45:48 -0400156 if (!validate_desc(desc, *fCaps)) {
157 return nullptr;
158 }
159
Robert Phillips45fdae12017-04-17 12:57:27 -0400160 GrContext* context = fGpu->getContext();
161
Robert Phillips92de6312017-05-23 07:43:48 -0400162 SkImageInfo srcInfo;
Robert Phillips45fdae12017-04-17 12:57:27 -0400163
Robert Phillips92de6312017-05-23 07:43:48 -0400164 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
165 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
Robert Phillipsfe50d962017-06-01 10:22:03 -0400166 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
167 if (proxy) {
168 sk_sp<GrSurfaceContext> sContext =
169 context->contextPriv().makeWrappedSurfaceContext(std::move(proxy), nullptr);
170 if (sContext) {
171 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
172 return sContext->asTextureProxyRef();
173 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400174 }
175 }
176 }
177
178 SkTArray<GrMipLevel> texels(1);
179 texels.push_back(mipLevel);
180
181 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels));
182 return GrSurfaceProxy::MakeWrapped(std::move(tex));
183}
184
185
Robert Phillipse78b7252017-04-06 07:59:41 -0400186sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
187 uint32_t flags) {
188 ASSERT_SINGLE_OWNER
189
190 if (this->isAbandoned()) {
191 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500192 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400193
Brian Salomond34edf32017-05-19 15:45:48 -0400194 if (!validate_desc(desc, *fCaps)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400195 return nullptr;
196 }
197
Robert Phillips92de6312017-05-23 07:43:48 -0400198 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
199 if (tex) {
200 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400201 }
202
Robert Phillips67d52cf2017-06-05 13:38:13 -0400203 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500204}
205
Robert Phillips67d52cf2017-06-05 13:38:13 -0400206sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
207 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500208 ASSERT_SINGLE_OWNER
209 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500210
Brian Osman32342f02017-03-04 08:12:46 -0500211 if (this->isAbandoned()) {
212 return nullptr;
213 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400214
Brian Salomond34edf32017-05-19 15:45:48 -0400215 if (!validate_desc(desc, *fCaps)) {
216 return nullptr;
217 }
218
Robert Phillips1119dc32017-04-11 12:54:57 -0400219 return this->refScratchTexture(desc, flags);
Brian Osman32342f02017-03-04 08:12:46 -0500220}
221
Robert Phillips67d52cf2017-06-05 13:38:13 -0400222sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& inDesc,
223 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500224 ASSERT_SINGLE_OWNER
225 SkASSERT(!this->isAbandoned());
Brian Salomond34edf32017-05-19 15:45:48 -0400226 SkASSERT(validate_desc(inDesc, *fCaps));
Brian Osman32342f02017-03-04 08:12:46 -0500227
228 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
229
Brian Salomond17b4a62017-05-23 16:53:47 -0400230 // We could make initial clears work with scratch textures but it is a rare case so we just opt
231 // to fall back to making a new texture.
232 if (!SkToBool(inDesc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
233 (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500234 if (!(kExact_Flag & flags)) {
235 // bin by pow2 with a reasonable min
236 GrSurfaceDesc* wdesc = desc.writable();
237 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth));
238 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight));
239 }
240
241 GrScratchKey key;
242 GrTexturePriv::ComputeScratchKey(*desc, &key);
243 uint32_t scratchFlags = 0;
244 if (kNoPendingIO_Flag & flags) {
245 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
246 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
247 // If it is not a render target then it will most likely be populated by
248 // writePixels() which will trigger a flush if the texture has pending IO.
249 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
250 }
251 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
252 GrSurface::WorstCaseSize(*desc),
253 scratchFlags);
254 if (resource) {
255 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400256 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500257 }
258 }
259
260 if (!(kNoCreate_Flag & flags)) {
261 return fGpu->createTexture(*desc, SkBudgeted::kYes);
262 }
263
264 return nullptr;
265}
266
Greg Daniel7ef28f32017-04-20 16:41:55 +0000267sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
268 GrSurfaceOrigin origin,
269 GrBackendTextureFlags flags,
270 int sampleCnt,
Brian Osman32342f02017-03-04 08:12:46 -0500271 GrWrapOwnership ownership) {
272 ASSERT_SINGLE_OWNER
273 if (this->isAbandoned()) {
274 return nullptr;
275 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000276 return fGpu->wrapBackendTexture(tex, origin, flags, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500277}
278
279sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Greg Danielbcf612b2017-05-01 13:50:58 +0000280 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin)
Brian Osman32342f02017-03-04 08:12:46 -0500281{
282 ASSERT_SINGLE_OWNER
Greg Danielbcf612b2017-05-01 13:50:58 +0000283 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, origin);
Brian Osman32342f02017-03-04 08:12:46 -0500284}
285
286void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
287 GrGpuResource* resource) {
288 ASSERT_SINGLE_OWNER
289 if (this->isAbandoned() || !resource) {
290 return;
291 }
292 resource->resourcePriv().setUniqueKey(key);
293}
294
295GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
296 ASSERT_SINGLE_OWNER
297 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
298}
299
300GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
301 ASSERT_SINGLE_OWNER
302 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
303 if (resource) {
304 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
305 SkASSERT(texture);
306 return texture;
307 }
308 return NULL;
309}
310
Robert Phillips646e4292017-06-13 12:44:56 -0400311void GrResourceProvider::assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
312 SkASSERT(key.isValid());
313 this->assignUniqueKeyToResource(key, texture);
314}
315
Robert Phillipsd3749482017-03-14 09:17:43 -0400316// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
317void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
318 ASSERT_SINGLE_OWNER
319 SkASSERT(key.isValid());
320 if (this->isAbandoned() || !proxy) {
321 return;
322 }
323
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400324 if (!proxy->instantiate(this)) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400325 return;
326 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400327 GrTexture* texture = proxy->priv().peekTexture();
Robert Phillipsd3749482017-03-14 09:17:43 -0400328
329 this->assignUniqueKeyToResource(key, texture);
330}
331
332// MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs
333sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
334 ASSERT_SINGLE_OWNER
335
336 sk_sp<GrTexture> texture(this->findAndRefTextureByUniqueKey(key));
337 if (!texture) {
338 return nullptr;
339 }
340
341 return GrSurfaceProxy::MakeWrapped(std::move(texture));
342}
343
Chris Daltonff926502017-05-03 14:36:54 -0400344const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700345 int patternSize,
346 int reps,
347 int vertCount,
348 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700349 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
350
Brian Salomon09d994e2016-12-21 11:14:46 -0500351 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700352 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700353 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700354 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700355 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700356 }
357 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -0700358 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -0700359 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -0700360 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -0700361 }
362 for (int i = 0; i < reps; ++i) {
363 int baseIdx = i * patternSize;
364 uint16_t baseVert = (uint16_t)(i * vertCount);
365 for (int j = 0; j < patternSize; ++j) {
366 data[baseIdx+j] = baseVert + pattern[j];
367 }
368 }
369 if (useTempData) {
370 if (!buffer->updateData(data, bufferSize)) {
371 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700372 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700373 }
halcanary385fe4d2015-08-26 13:07:48 -0700374 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -0700375 } else {
376 buffer->unmap();
377 }
378 this->assignUniqueKeyToResource(key, buffer);
379 return buffer;
380}
381
cdalton397536c2016-03-25 12:15:03 -0700382const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700383 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
384 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
385 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
386
Chris Daltonff926502017-05-03 14:36:54 -0400387 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700388}
389
Robert Phillips67d52cf2017-06-05 13:38:13 -0400390sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700391 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700392 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700393}
394
Robert Phillips67d52cf2017-06-05 13:38:13 -0400395sk_sp<GrPathRange> GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
396 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700397 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700398 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700399}
400
Robert Phillips67d52cf2017-06-05 13:38:13 -0400401sk_sp<GrPathRange> GrResourceProvider::createGlyphs(const SkTypeface* tf,
402 const SkScalerContextEffects& effects,
403 const SkDescriptor* desc,
404 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700405
406 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700407 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700408}
409
cdaltone2e71c22016-04-07 18:13:29 -0700410GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700411 GrAccessPattern accessPattern, uint32_t flags,
412 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700413 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700414 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700415 }
cdaltond37fe762016-04-21 07:41:50 -0700416 if (kDynamic_GrAccessPattern != accessPattern) {
417 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
418 }
csmartdalton485a1202016-07-13 10:16:32 -0700419 if (!(flags & kRequireGpuMemory_Flag) &&
420 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
421 GrBufferTypeIsVertexOrIndex(intendedType) &&
422 kDynamic_GrAccessPattern == accessPattern) {
423 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
424 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700425
cdaltond37fe762016-04-21 07:41:50 -0700426 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400427 static const size_t MIN_SIZE = 1 << 12;
428 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700429
cdaltond37fe762016-04-21 07:41:50 -0700430 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700431 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700432 uint32_t scratchFlags = 0;
433 if (flags & kNoPendingIO_Flag) {
434 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
435 } else {
436 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
437 }
438 GrBuffer* buffer = static_cast<GrBuffer*>(
439 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
440 if (!buffer) {
441 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
442 if (!buffer) {
443 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700444 }
445 }
cdaltond37fe762016-04-21 07:41:50 -0700446 if (data) {
447 buffer->updateData(data, size);
448 }
csmartdalton485a1202016-07-13 10:16:32 -0700449 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700450 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800451}
452
egdanielec00d942015-09-14 12:56:10 -0700453GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
454 SkASSERT(rt);
455 if (rt->renderTargetPriv().getStencilAttachment()) {
456 return rt->renderTargetPriv().getStencilAttachment();
457 }
458
459 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
460 GrUniqueKey sbKey;
461
462 int width = rt->width();
463 int height = rt->height();
464#if 0
465 if (this->caps()->oversizedStencilSupport()) {
466 width = SkNextPow2(width);
467 height = SkNextPow2(height);
468 }
469#endif
470 bool newStencil = false;
471 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
472 rt->numStencilSamples(), &sbKey);
473 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
474 this->findAndRefResourceByUniqueKey(sbKey));
475 if (!stencil) {
476 // Need to try and create a new stencil
477 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
478 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500479 this->assignUniqueKeyToResource(sbKey, stencil);
egdanielec00d942015-09-14 12:56:10 -0700480 newStencil = true;
481 }
482 }
483 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
484 if (newStencil) {
485 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800486 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700487 // stencil buffers with smaller color targets this will no longer
488 // be correct because it won't be guaranteed to clear the entire
489 // sb.
490 // We used to clear down in the GL subclass using a special purpose
491 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
492 // FBO status.
493 this->gpu()->clearStencil(rt);
494 }
495 }
496 }
497 return rt->renderTargetPriv().getStencilAttachment();
498}
499
bungeman6bd52842016-10-27 09:30:08 -0700500sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000501 const GrBackendTexture& tex, GrSurfaceOrigin origin, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700502{
ericrkf7b8b8a2016-02-24 14:49:51 -0800503 if (this->isAbandoned()) {
504 return nullptr;
505 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000506 return this->gpu()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800507}
Greg Danield85f97d2017-03-07 13:37:21 -0500508
Greg Daniela5cb7812017-06-16 09:45:32 -0400509sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
510 return fGpu->makeSemaphore(isOwned);
511}
512
513sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
514 GrWrapOwnership ownership) {
515 ASSERT_SINGLE_OWNER
516 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500517}
518
519void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
520 semaphore->resetGpu(fGpu);
521}
522
523void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
524 semaphore->resetGpu(nullptr);
525}