blob: 933f732a2a8804dcf98268df117337dbde816256 [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 "GrRenderTargetPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070019#include "GrResourceCache.h"
20#include "GrResourceKey.h"
Greg Danield85f97d2017-03-07 13:37:21 -050021#include "GrSemaphore.h"
egdanielec00d942015-09-14 12:56:10 -070022#include "GrStencilAttachment.h"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040023#include "GrSurfaceProxyPriv.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
33#define ASSERT_SINGLE_OWNER \
34 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
35
joshualitt6d0872d2016-01-11 08:27:48 -080036GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050037 : fCache(cache)
38 , fGpu(gpu)
39#ifdef SK_DEBUG
40 , fSingleOwner(owner)
41#endif
42 {
Robert Phillips26c90e02017-03-14 14:39:29 -040043 fCaps = sk_ref_sp(fGpu->caps());
44
bsalomoned0bcad2015-05-04 10:36:42 -070045 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
46 fQuadIndexBufferKey = gQuadIndexBufferKey;
47}
48
Robert Phillipsf7a72612017-03-31 10:03:45 -040049bool GrResourceProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040050 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
51}
Brian Osman32342f02017-03-04 08:12:46 -050052
Brian Salomond34edf32017-05-19 15:45:48 -040053bool validate_desc(const GrSurfaceDesc& desc, const GrCaps& caps, int levelCount = 0) {
54 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
55 return false;
56 }
57 if (!caps.isConfigTexturable(desc.fConfig)) {
58 return false;
59 }
60 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
61 if (!caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
62 return false;
63 }
64 } else {
65 if (desc.fSampleCnt) {
66 return false;
67 }
68 }
Brian Osman48c99192017-06-02 08:45:06 -040069 if (levelCount > 1 && (GrPixelConfigIsSint(desc.fConfig) || !caps.mipMapSupport())) {
Brian Salomond34edf32017-05-19 15:45:48 -040070 return false;
71 }
72 return true;
73}
74
Robert Phillips8e8c7552017-07-10 12:06:05 -040075sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040076 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips8e8c7552017-07-10 12:06:05 -040077 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050078 ASSERT_SINGLE_OWNER
79
Robert Phillips590533f2017-07-11 14:22:35 -040080 SkASSERT(mipLevelCount >= 1);
Robert Phillips1119dc32017-04-11 12:54:57 -040081
Brian Osman32342f02017-03-04 08:12:46 -050082 if (this->isAbandoned()) {
83 return nullptr;
84 }
Robert Phillips1119dc32017-04-11 12:54:57 -040085
Robert Phillips590533f2017-07-11 14:22:35 -040086 if (!validate_desc(desc, *fCaps, mipLevelCount)) {
Brian Osman32342f02017-03-04 08:12:46 -050087 return nullptr;
88 }
Brian Osman32342f02017-03-04 08:12:46 -050089
Robert Phillips590533f2017-07-11 14:22:35 -040090 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels, mipLevelCount));
Robert Phillipse78b7252017-04-06 07:59:41 -040091 if (tex) {
92 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -040093 }
Robert Phillipse78b7252017-04-06 07:59:41 -040094
Robert Phillips8e8c7552017-07-10 12:06:05 -040095 return tex;
Brian Osman32342f02017-03-04 08:12:46 -050096}
97
Robert Phillips45fdae12017-04-17 12:57:27 -040098sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -040099 SkBudgeted budgeted, uint32_t flags) {
100 flags |= kExact_Flag | kNoCreate_Flag;
Robert Phillips45fdae12017-04-17 12:57:27 -0400101 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
102 if (tex && SkBudgeted::kNo == budgeted) {
103 tex->resourcePriv().makeUnbudgeted();
104 }
105
106 return tex;
107}
108
109static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
110 SkColorType colorType;
111 if (!GrPixelConfigToColorType(config, &colorType)) {
112 return false;
113 }
114
115 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
116 return true;
117}
118
119sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
120 SkBudgeted budgeted,
121 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400122 ASSERT_SINGLE_OWNER
123
124 if (this->isAbandoned()) {
125 return nullptr;
126 }
127
Robert Phillips45fdae12017-04-17 12:57:27 -0400128 if (!mipLevel.fPixels) {
129 return nullptr;
130 }
131
Brian Salomond34edf32017-05-19 15:45:48 -0400132 if (!validate_desc(desc, *fCaps)) {
133 return nullptr;
134 }
135
Robert Phillips45fdae12017-04-17 12:57:27 -0400136 GrContext* context = fGpu->getContext();
137
Robert Phillips92de6312017-05-23 07:43:48 -0400138 SkImageInfo srcInfo;
Robert Phillips45fdae12017-04-17 12:57:27 -0400139
Robert Phillips92de6312017-05-23 07:43:48 -0400140 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
Greg Daniel21918232017-09-08 14:46:23 -0400141 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
Robert Phillips066f0202017-07-25 10:16:35 -0400142 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
Robert Phillipsfe50d962017-06-01 10:22:03 -0400143 if (proxy) {
144 sk_sp<GrSurfaceContext> sContext =
145 context->contextPriv().makeWrappedSurfaceContext(std::move(proxy), nullptr);
146 if (sContext) {
147 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
148 return sContext->asTextureProxyRef();
149 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400150 }
151 }
152 }
153
Robert Phillips590533f2017-07-11 14:22:35 -0400154 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, &mipLevel, 1));
Robert Phillips066f0202017-07-25 10:16:35 -0400155 return GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
Robert Phillips45fdae12017-04-17 12:57:27 -0400156}
157
Robert Phillipse78b7252017-04-06 07:59:41 -0400158sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
159 uint32_t flags) {
160 ASSERT_SINGLE_OWNER
161
162 if (this->isAbandoned()) {
163 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500164 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400165
Brian Salomond34edf32017-05-19 15:45:48 -0400166 if (!validate_desc(desc, *fCaps)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400167 return nullptr;
168 }
169
Greg Daniel21918232017-09-08 14:46:23 -0400170 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillips92de6312017-05-23 07:43:48 -0400171 if (tex) {
172 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400173 }
174
Robert Phillips67d52cf2017-06-05 13:38:13 -0400175 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500176}
177
Robert Phillips67d52cf2017-06-05 13:38:13 -0400178sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
179 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500180 ASSERT_SINGLE_OWNER
181 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500182
Brian Osman32342f02017-03-04 08:12:46 -0500183 if (this->isAbandoned()) {
184 return nullptr;
185 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400186
Brian Salomond34edf32017-05-19 15:45:48 -0400187 if (!validate_desc(desc, *fCaps)) {
188 return nullptr;
189 }
190
Greg Daniel21918232017-09-08 14:46:23 -0400191 return this->refScratchTexture(desc, flags);
Brian Osman32342f02017-03-04 08:12:46 -0500192}
193
Greg Daniel21918232017-09-08 14:46:23 -0400194sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& inDesc,
195 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500196 ASSERT_SINGLE_OWNER
197 SkASSERT(!this->isAbandoned());
Greg Daniel21918232017-09-08 14:46:23 -0400198 SkASSERT(validate_desc(inDesc, *fCaps));
199
200 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
Brian Osman32342f02017-03-04 08:12:46 -0500201
Brian Salomond17b4a62017-05-23 16:53:47 -0400202 // We could make initial clears work with scratch textures but it is a rare case so we just opt
203 // to fall back to making a new texture.
Greg Daniel21918232017-09-08 14:46:23 -0400204 if (!SkToBool(inDesc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
205 (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag))) {
206 if (!(kExact_Flag & flags)) {
207 // bin by pow2 with a reasonable min
208 GrSurfaceDesc* wdesc = desc.writable();
209 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth));
210 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight));
211 }
Brian Osman32342f02017-03-04 08:12:46 -0500212
213 GrScratchKey key;
Greg Daniel21918232017-09-08 14:46:23 -0400214 GrTexturePriv::ComputeScratchKey(*desc, &key);
Brian Osman32342f02017-03-04 08:12:46 -0500215 uint32_t scratchFlags = 0;
216 if (kNoPendingIO_Flag & flags) {
217 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
Greg Daniel21918232017-09-08 14:46:23 -0400218 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500219 // If it is not a render target then it will most likely be populated by
220 // writePixels() which will trigger a flush if the texture has pending IO.
221 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
222 }
223 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel21918232017-09-08 14:46:23 -0400224 GrSurface::WorstCaseSize(*desc),
225 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500226 if (resource) {
227 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400228 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500229 }
230 }
231
Greg Daniel21918232017-09-08 14:46:23 -0400232 if (!(kNoCreate_Flag & flags)) {
233 return fGpu->createTexture(*desc, SkBudgeted::kYes);
234 }
235
Brian Osman32342f02017-03-04 08:12:46 -0500236 return nullptr;
237}
238
Greg Daniel7ef28f32017-04-20 16:41:55 +0000239sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -0500240 GrWrapOwnership ownership) {
241 ASSERT_SINGLE_OWNER
242 if (this->isAbandoned()) {
243 return nullptr;
244 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400245 return fGpu->wrapBackendTexture(tex, ownership);
Brian Salomond17f6582017-07-19 18:28:58 -0400246}
247
248sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400249 int sampleCnt,
250 GrWrapOwnership ownership) {
251 ASSERT_SINGLE_OWNER
252 if (this->isAbandoned()) {
253 return nullptr;
254 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400255 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500256}
257
258sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400259 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500260{
261 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400262 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500263}
264
265void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
266 GrGpuResource* resource) {
267 ASSERT_SINGLE_OWNER
268 if (this->isAbandoned() || !resource) {
269 return;
270 }
271 resource->resourcePriv().setUniqueKey(key);
272}
273
274GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
275 ASSERT_SINGLE_OWNER
276 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
277}
278
279GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
280 ASSERT_SINGLE_OWNER
281 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
282 if (resource) {
283 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
284 SkASSERT(texture);
285 return texture;
286 }
Ben Wagnera93a14a2017-08-28 10:34:05 -0400287 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500288}
289
Robert Phillips646e4292017-06-13 12:44:56 -0400290void GrResourceProvider::assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
291 SkASSERT(key.isValid());
292 this->assignUniqueKeyToResource(key, texture);
293}
294
Robert Phillipsd3749482017-03-14 09:17:43 -0400295void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
296 ASSERT_SINGLE_OWNER
297 SkASSERT(key.isValid());
298 if (this->isAbandoned() || !proxy) {
299 return;
300 }
301
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400302 fCache->assignUniqueKeyToProxy(key, proxy);
Robert Phillipsd3749482017-03-14 09:17:43 -0400303}
304
Robert Phillips066f0202017-07-25 10:16:35 -0400305sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key,
306 GrSurfaceOrigin origin) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400307 ASSERT_SINGLE_OWNER
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400308 return this->isAbandoned() ? nullptr : fCache->findProxyByUniqueKey(key, origin);
Robert Phillipsd3749482017-03-14 09:17:43 -0400309}
310
Chris Daltonff926502017-05-03 14:36:54 -0400311const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700312 int patternSize,
313 int reps,
314 int vertCount,
315 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700316 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
317
Brian Salomon09d994e2016-12-21 11:14:46 -0500318 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700319 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700320 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700321 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700322 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700323 }
Mike Klein65e70632017-09-21 13:46:28 -0400324
325 SkAutoTArray<uint16_t> data(reps * patternSize);
bsalomoned0bcad2015-05-04 10:36:42 -0700326 for (int i = 0; i < reps; ++i) {
327 int baseIdx = i * patternSize;
328 uint16_t baseVert = (uint16_t)(i * vertCount);
329 for (int j = 0; j < patternSize; ++j) {
330 data[baseIdx+j] = baseVert + pattern[j];
331 }
332 }
Mike Klein65e70632017-09-21 13:46:28 -0400333
334 if (!buffer->updateData(data.get(), bufferSize)) {
335 buffer->unref();
336 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700337 }
Mike Klein65e70632017-09-21 13:46:28 -0400338
bsalomoned0bcad2015-05-04 10:36:42 -0700339 this->assignUniqueKeyToResource(key, buffer);
340 return buffer;
341}
342
Brian Salomon34169692017-08-28 15:32:01 -0400343static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
344
cdalton397536c2016-03-25 12:15:03 -0700345const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700346 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
347 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
348
Chris Daltonff926502017-05-03 14:36:54 -0400349 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700350}
351
Brian Salomon34169692017-08-28 15:32:01 -0400352int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
353
Robert Phillips67d52cf2017-06-05 13:38:13 -0400354sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400355 if (this->isAbandoned()) {
356 return nullptr;
357 }
358
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
Robert Phillips67d52cf2017-06-05 13:38:13 -0400363sk_sp<GrPathRange> GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
364 const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400365 if (this->isAbandoned()) {
366 return nullptr;
367 }
368
bsalomon706f08f2015-05-22 07:35:58 -0700369 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700370 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700371}
372
Robert Phillips67d52cf2017-06-05 13:38:13 -0400373sk_sp<GrPathRange> GrResourceProvider::createGlyphs(const SkTypeface* tf,
374 const SkScalerContextEffects& effects,
375 const SkDescriptor* desc,
376 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700377
378 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700379 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700380}
381
cdaltone2e71c22016-04-07 18:13:29 -0700382GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700383 GrAccessPattern accessPattern, uint32_t flags,
384 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700385 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700386 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700387 }
cdaltond37fe762016-04-21 07:41:50 -0700388 if (kDynamic_GrAccessPattern != accessPattern) {
389 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
390 }
csmartdalton485a1202016-07-13 10:16:32 -0700391 if (!(flags & kRequireGpuMemory_Flag) &&
392 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
393 GrBufferTypeIsVertexOrIndex(intendedType) &&
394 kDynamic_GrAccessPattern == accessPattern) {
395 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
396 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700397
cdaltond37fe762016-04-21 07:41:50 -0700398 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400399 static const size_t MIN_SIZE = 1 << 12;
400 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700401
cdaltond37fe762016-04-21 07:41:50 -0700402 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700403 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700404 uint32_t scratchFlags = 0;
405 if (flags & kNoPendingIO_Flag) {
406 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
407 } else {
408 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
409 }
410 GrBuffer* buffer = static_cast<GrBuffer*>(
411 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
412 if (!buffer) {
413 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
414 if (!buffer) {
415 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700416 }
417 }
cdaltond37fe762016-04-21 07:41:50 -0700418 if (data) {
419 buffer->updateData(data, size);
420 }
csmartdalton485a1202016-07-13 10:16:32 -0700421 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700422 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800423}
424
Robert Phillipsc0192e32017-09-21 12:00:26 -0400425bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700426 SkASSERT(rt);
427 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400428 return true;
egdanielec00d942015-09-14 12:56:10 -0700429 }
430
431 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
432 GrUniqueKey sbKey;
433
434 int width = rt->width();
435 int height = rt->height();
436#if 0
437 if (this->caps()->oversizedStencilSupport()) {
438 width = SkNextPow2(width);
439 height = SkNextPow2(height);
440 }
441#endif
Robert Phillipscb2e2352017-08-30 16:44:40 -0400442 SkDEBUGCODE(bool newStencil = false;)
egdanielec00d942015-09-14 12:56:10 -0700443 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
444 rt->numStencilSamples(), &sbKey);
445 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
446 this->findAndRefResourceByUniqueKey(sbKey));
447 if (!stencil) {
448 // Need to try and create a new stencil
449 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
450 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500451 this->assignUniqueKeyToResource(sbKey, stencil);
Robert Phillipscb2e2352017-08-30 16:44:40 -0400452 SkDEBUGCODE(newStencil = true;)
egdanielec00d942015-09-14 12:56:10 -0700453 }
454 }
455 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
Robert Phillips95214472017-08-08 18:00:03 -0400456#ifdef SK_DEBUG
457 // Fill the SB with an inappropriate value. opLists that use the
458 // SB should clear it properly.
Robert Phillipscb2e2352017-08-30 16:44:40 -0400459 if (newStencil) {
460 SkASSERT(stencil->isDirty());
461 this->gpu()->clearStencil(rt, 0xFFFF);
462 SkASSERT(stencil->isDirty());
463 }
Robert Phillips95214472017-08-08 18:00:03 -0400464#endif
egdanielec00d942015-09-14 12:56:10 -0700465 }
466 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400467 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700468}
469
bungeman6bd52842016-10-27 09:30:08 -0700470sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400471 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700472{
ericrkf7b8b8a2016-02-24 14:49:51 -0800473 if (this->isAbandoned()) {
474 return nullptr;
475 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400476 return this->gpu()->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800477}
Greg Danield85f97d2017-03-07 13:37:21 -0500478
Greg Daniela5cb7812017-06-16 09:45:32 -0400479sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
480 return fGpu->makeSemaphore(isOwned);
481}
482
483sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
484 GrWrapOwnership ownership) {
485 ASSERT_SINGLE_OWNER
486 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500487}
488
489void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
490 semaphore->resetGpu(fGpu);
491}
492
493void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
494 semaphore->resetGpu(nullptr);
495}