blob: ec94968e21aca8628bc61b458c83a20fadcb1dd8 [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"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050018#include "GrProxyProvider.h"
egdanielec00d942015-09-14 12:56:10 -070019#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"
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 Phillips8e8c7552017-07-10 12:06:05 -040049sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040050 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips8e8c7552017-07-10 12:06:05 -040051 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050052 ASSERT_SINGLE_OWNER
53
Robert Phillips7f1b4f82017-11-28 07:38:39 -050054 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040055
Brian Osman32342f02017-03-04 08:12:46 -050056 if (this->isAbandoned()) {
57 return nullptr;
58 }
Robert Phillips1119dc32017-04-11 12:54:57 -040059
Brian Salomonbdecacf2018-02-02 20:32:49 -050060 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
61 if (!fCaps->validateSurfaceDesc(desc, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050062 return nullptr;
63 }
Brian Osman32342f02017-03-04 08:12:46 -050064
Robert Phillips590533f2017-07-11 14:22:35 -040065 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels, mipLevelCount));
Robert Phillipse78b7252017-04-06 07:59:41 -040066 if (tex) {
67 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -040068 }
Robert Phillipse78b7252017-04-06 07:59:41 -040069
Robert Phillips8e8c7552017-07-10 12:06:05 -040070 return tex;
Brian Osman32342f02017-03-04 08:12:46 -050071}
72
Robert Phillips45fdae12017-04-17 12:57:27 -040073sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -040074 SkBudgeted budgeted, uint32_t flags) {
Robert Phillips45fdae12017-04-17 12:57:27 -040075 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
76 if (tex && SkBudgeted::kNo == budgeted) {
77 tex->resourcePriv().makeUnbudgeted();
78 }
79
80 return tex;
81}
82
83static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
84 SkColorType colorType;
85 if (!GrPixelConfigToColorType(config, &colorType)) {
86 return false;
87 }
88
89 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
90 return true;
91}
92
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
94 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -050095 SkBackingFit fit,
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -040097 ASSERT_SINGLE_OWNER
98
99 if (this->isAbandoned()) {
100 return nullptr;
101 }
102
Robert Phillips45fdae12017-04-17 12:57:27 -0400103 if (!mipLevel.fPixels) {
104 return nullptr;
105 }
106
Brian Salomonbdecacf2018-02-02 20:32:49 -0500107 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400108 return nullptr;
109 }
110
Robert Phillips45fdae12017-04-17 12:57:27 -0400111 GrContext* context = fGpu->getContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500112 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400113
Robert Phillips92de6312017-05-23 07:43:48 -0400114 SkImageInfo srcInfo;
Robert Phillips45fdae12017-04-17 12:57:27 -0400115
Robert Phillips92de6312017-05-23 07:43:48 -0400116 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500117 // DDL TODO: remove this use of createInstantiatedProxy and convert it to a testing-only
118 // method.
119 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(desc,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500120 fit,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500121 budgeted);
Robert Phillipsfe50d962017-06-01 10:22:03 -0400122 if (proxy) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500123 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
124 std::move(proxy));
Robert Phillipsfe50d962017-06-01 10:22:03 -0400125 if (sContext) {
126 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500127 return sk_ref_sp(sContext->asTextureProxy()->priv().peekTexture());
Robert Phillipsfe50d962017-06-01 10:22:03 -0400128 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400129 }
130 }
131 }
132
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500133 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400134}
135
Robert Phillipse78b7252017-04-06 07:59:41 -0400136sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
137 uint32_t flags) {
138 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400139 if (this->isAbandoned()) {
140 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500141 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400142
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400144 return nullptr;
145 }
146
Greg Daniel21918232017-09-08 14:46:23 -0400147 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillips92de6312017-05-23 07:43:48 -0400148 if (tex) {
149 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400150 }
151
Robert Phillips67d52cf2017-06-05 13:38:13 -0400152 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500153}
154
Robert Phillips67d52cf2017-06-05 13:38:13 -0400155sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
156 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500157 ASSERT_SINGLE_OWNER
158 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500159
Brian Osman32342f02017-03-04 08:12:46 -0500160 if (this->isAbandoned()) {
161 return nullptr;
162 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400163
Brian Salomonbdecacf2018-02-02 20:32:49 -0500164 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400165 return nullptr;
166 }
167
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400168 if (auto tex = this->refScratchTexture(desc, flags)) {
169 return tex;
170 }
171
Greg Daniel29bf84f2017-09-25 12:25:12 -0400172 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
173
174 // bin by pow2 with a reasonable min
175 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
176 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
177 GrSurfaceDesc* wdesc = copyDesc.writable();
178 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
179 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
180 }
181
182 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
183 return tex;
184 }
185
186 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500187}
188
Greg Daniel29bf84f2017-09-25 12:25:12 -0400189sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -0400190 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500191 ASSERT_SINGLE_OWNER
192 SkASSERT(!this->isAbandoned());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500193 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500194
Brian Salomond17b4a62017-05-23 16:53:47 -0400195 // We could make initial clears work with scratch textures but it is a rare case so we just opt
196 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400197 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
198 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500199
200 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400201 GrTexturePriv::ComputeScratchKey(desc, &key);
Brian Osman32342f02017-03-04 08:12:46 -0500202 uint32_t scratchFlags = 0;
203 if (kNoPendingIO_Flag & flags) {
204 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400205 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500206 // If it is not a render target then it will most likely be populated by
207 // writePixels() which will trigger a flush if the texture has pending IO.
208 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
209 }
210 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400211 GrSurface::WorstCaseSize(desc),
212 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500213 if (resource) {
214 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400215 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500216 }
217 }
218
Brian Osman32342f02017-03-04 08:12:46 -0500219 return nullptr;
220}
221
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -0500223 GrWrapOwnership ownership) {
224 ASSERT_SINGLE_OWNER
225 if (this->isAbandoned()) {
226 return nullptr;
227 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400228 return fGpu->wrapBackendTexture(tex, ownership);
Brian Salomond17f6582017-07-19 18:28:58 -0400229}
230
231sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400232 int sampleCnt,
233 GrWrapOwnership ownership) {
234 ASSERT_SINGLE_OWNER
235 if (this->isAbandoned()) {
236 return nullptr;
237 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400238 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500239}
240
241sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400242 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500243{
244 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400245 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500246}
247
248void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
249 GrGpuResource* resource) {
250 ASSERT_SINGLE_OWNER
251 if (this->isAbandoned() || !resource) {
252 return;
253 }
254 resource->resourcePriv().setUniqueKey(key);
255}
256
Brian Salomond28a79d2017-10-16 13:01:07 -0400257sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500258 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400259 return this->isAbandoned() ? nullptr
260 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500261}
262
Chris Dalton5d2de082017-12-19 10:40:23 -0700263sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
264 size_t size,
265 const void* data,
266 const GrUniqueKey& key) {
267 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
268 return buffer;
269 }
270 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, 0,
271 data)) {
272 // We shouldn't bin and/or cachestatic buffers.
273 SkASSERT(buffer->sizeInBytes() == size);
274 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
275 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
276 buffer->resourcePriv().setUniqueKey(key);
277 return sk_sp<const GrBuffer>(buffer);
278 }
279 return nullptr;
280}
281
Brian Salomond28a79d2017-10-16 13:01:07 -0400282sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
283 int patternSize,
284 int reps,
285 int vertCount,
286 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700287 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
288
Brian Salomon09d994e2016-12-21 11:14:46 -0500289 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400290 sk_sp<GrBuffer> buffer(this->createBuffer(bufferSize, kIndex_GrBufferType,
291 kStatic_GrAccessPattern, kNoPendingIO_Flag));
bsalomoned0bcad2015-05-04 10:36:42 -0700292 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700293 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700294 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400295 uint16_t* data = (uint16_t*) buffer->map();
296 SkAutoTArray<uint16_t> temp;
297 if (!data) {
298 temp.reset(reps * patternSize);
299 data = temp.get();
300 }
bsalomoned0bcad2015-05-04 10:36:42 -0700301 for (int i = 0; i < reps; ++i) {
302 int baseIdx = i * patternSize;
303 uint16_t baseVert = (uint16_t)(i * vertCount);
304 for (int j = 0; j < patternSize; ++j) {
305 data[baseIdx+j] = baseVert + pattern[j];
306 }
307 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400308 if (temp.get()) {
309 if (!buffer->updateData(data, bufferSize)) {
310 return nullptr;
311 }
312 } else {
313 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700314 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400315 this->assignUniqueKeyToResource(key, buffer.get());
Brian Salomond28a79d2017-10-16 13:01:07 -0400316 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700317}
318
Brian Salomon34169692017-08-28 15:32:01 -0400319static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
320
Brian Salomond28a79d2017-10-16 13:01:07 -0400321sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700322 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000323 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Chris Daltonff926502017-05-03 14:36:54 -0400324 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700325}
326
Brian Salomon34169692017-08-28 15:32:01 -0400327int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
328
Robert Phillips67d52cf2017-06-05 13:38:13 -0400329sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400330 if (this->isAbandoned()) {
331 return nullptr;
332 }
333
bsalomon706f08f2015-05-22 07:35:58 -0700334 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700335 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700336}
337
Robert Phillips67d52cf2017-06-05 13:38:13 -0400338sk_sp<GrPathRange> GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
339 const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400340 if (this->isAbandoned()) {
341 return nullptr;
342 }
343
bsalomon706f08f2015-05-22 07:35:58 -0700344 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700345 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700346}
347
Robert Phillips67d52cf2017-06-05 13:38:13 -0400348sk_sp<GrPathRange> GrResourceProvider::createGlyphs(const SkTypeface* tf,
349 const SkScalerContextEffects& effects,
350 const SkDescriptor* desc,
351 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700352
353 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700354 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700355}
356
cdaltone2e71c22016-04-07 18:13:29 -0700357GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700358 GrAccessPattern accessPattern, uint32_t flags,
359 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700360 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700361 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700362 }
cdaltond37fe762016-04-21 07:41:50 -0700363 if (kDynamic_GrAccessPattern != accessPattern) {
364 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
365 }
csmartdalton485a1202016-07-13 10:16:32 -0700366 if (!(flags & kRequireGpuMemory_Flag) &&
367 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
368 GrBufferTypeIsVertexOrIndex(intendedType) &&
369 kDynamic_GrAccessPattern == accessPattern) {
370 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
371 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700372
cdaltond37fe762016-04-21 07:41:50 -0700373 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400374 static const size_t MIN_SIZE = 1 << 12;
375 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700376
cdaltond37fe762016-04-21 07:41:50 -0700377 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700378 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700379 uint32_t scratchFlags = 0;
380 if (flags & kNoPendingIO_Flag) {
381 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
382 } else {
383 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
384 }
385 GrBuffer* buffer = static_cast<GrBuffer*>(
386 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
387 if (!buffer) {
388 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
389 if (!buffer) {
390 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700391 }
392 }
cdaltond37fe762016-04-21 07:41:50 -0700393 if (data) {
394 buffer->updateData(data, size);
395 }
csmartdalton485a1202016-07-13 10:16:32 -0700396 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700397 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800398}
399
Robert Phillipsc0192e32017-09-21 12:00:26 -0400400bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700401 SkASSERT(rt);
402 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400403 return true;
egdanielec00d942015-09-14 12:56:10 -0700404 }
405
406 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
407 GrUniqueKey sbKey;
408
409 int width = rt->width();
410 int height = rt->height();
411#if 0
412 if (this->caps()->oversizedStencilSupport()) {
413 width = SkNextPow2(width);
414 height = SkNextPow2(height);
415 }
416#endif
Robert Phillipscb2e2352017-08-30 16:44:40 -0400417 SkDEBUGCODE(bool newStencil = false;)
egdanielec00d942015-09-14 12:56:10 -0700418 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
419 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400420 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700421 if (!stencil) {
422 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400423 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
egdanielec00d942015-09-14 12:56:10 -0700424 if (stencil) {
Brian Salomond28a79d2017-10-16 13:01:07 -0400425 this->assignUniqueKeyToResource(sbKey, stencil.get());
Robert Phillipscb2e2352017-08-30 16:44:40 -0400426 SkDEBUGCODE(newStencil = true;)
egdanielec00d942015-09-14 12:56:10 -0700427 }
428 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400429 if (rt->renderTargetPriv().attachStencilAttachment(std::move(stencil))) {
Robert Phillips95214472017-08-08 18:00:03 -0400430#ifdef SK_DEBUG
431 // Fill the SB with an inappropriate value. opLists that use the
432 // SB should clear it properly.
Robert Phillipscb2e2352017-08-30 16:44:40 -0400433 if (newStencil) {
Brian Salomond28a79d2017-10-16 13:01:07 -0400434 SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
Robert Phillipscb2e2352017-08-30 16:44:40 -0400435 this->gpu()->clearStencil(rt, 0xFFFF);
Brian Salomond28a79d2017-10-16 13:01:07 -0400436 SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
Robert Phillipscb2e2352017-08-30 16:44:40 -0400437 }
Robert Phillips95214472017-08-08 18:00:03 -0400438#endif
egdanielec00d942015-09-14 12:56:10 -0700439 }
440 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400441 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700442}
443
bungeman6bd52842016-10-27 09:30:08 -0700444sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400445 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700446{
ericrkf7b8b8a2016-02-24 14:49:51 -0800447 if (this->isAbandoned()) {
448 return nullptr;
449 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500450 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800451}
Greg Danield85f97d2017-03-07 13:37:21 -0500452
Greg Daniela5cb7812017-06-16 09:45:32 -0400453sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
454 return fGpu->makeSemaphore(isOwned);
455}
456
457sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500458 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400459 GrWrapOwnership ownership) {
460 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500461 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
462 wrapType,
463 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500464}
465
466void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
467 semaphore->resetGpu(fGpu);
468}
469
470void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
471 semaphore->resetGpu(nullptr);
472}