blob: 023f86d267950186e30accaac7fca641e0254647 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrBackendSemaphore.h"
9#include "include/gpu/GrContext.h"
10#include "include/private/GrResourceKey.h"
11#include "include/private/GrSingleOwner.h"
12#include "src/core/SkMathPriv.h"
13#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrGpuBuffer.h"
17#include "src/gpu/GrPath.h"
18#include "src/gpu/GrPathRendering.h"
19#include "src/gpu/GrProxyProvider.h"
20#include "src/gpu/GrRenderTargetPriv.h"
21#include "src/gpu/GrResourceCache.h"
22#include "src/gpu/GrResourceProvider.h"
23#include "src/gpu/GrSemaphore.h"
24#include "src/gpu/GrStencilAttachment.h"
25#include "src/gpu/GrTexturePriv.h"
26#include "src/gpu/SkGr.h"
bsalomoned0bcad2015-05-04 10:36:42 -070027
Robert Phillips1bfece82017-06-01 13:56:52 -040028const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050029
30#define ASSERT_SINGLE_OWNER \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
32
Robert Phillips12c46292019-04-23 07:36:17 -040033GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050034 : fCache(cache)
35 , fGpu(gpu)
36#ifdef SK_DEBUG
37 , fSingleOwner(owner)
38#endif
Robert Phillips4150eea2018-02-07 17:08:21 -050039{
Robert Phillips26c90e02017-03-14 14:39:29 -040040 fCaps = sk_ref_sp(fGpu->caps());
bsalomoned0bcad2015-05-04 10:36:42 -070041}
42
Robert Phillips8e8c7552017-07-10 12:06:05 -040043sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Osman2b23c4b2018-06-01 12:25:08 -040044 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050045 ASSERT_SINGLE_OWNER
46
Robert Phillips7f1b4f82017-11-28 07:38:39 -050047 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040048
Brian Osman32342f02017-03-04 08:12:46 -050049 if (this->isAbandoned()) {
50 return nullptr;
51 }
Robert Phillips1119dc32017-04-11 12:54:57 -040052
Brian Salomonbdecacf2018-02-02 20:32:49 -050053 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
54 if (!fCaps->validateSurfaceDesc(desc, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050055 return nullptr;
56 }
Brian Osman32342f02017-03-04 08:12:46 -050057
Brian Osman2b23c4b2018-06-01 12:25:08 -040058 return fGpu->createTexture(desc, budgeted, texels, mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -050059}
60
Robert Phillips45fdae12017-04-17 12:57:27 -040061sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -060062 SkBudgeted budgeted, Flags flags) {
Robert Phillips45fdae12017-04-17 12:57:27 -040063 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
64 if (tex && SkBudgeted::kNo == budgeted) {
65 tex->resourcePriv().makeUnbudgeted();
66 }
67
68 return tex;
69}
70
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
72 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -050073 SkBackingFit fit,
Chris Daltond004e0b2018-09-27 09:28:03 -060074 const GrMipLevel& mipLevel,
75 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -040076 ASSERT_SINGLE_OWNER
77
78 if (this->isAbandoned()) {
79 return nullptr;
80 }
81
Robert Phillips45fdae12017-04-17 12:57:27 -040082 if (!mipLevel.fPixels) {
83 return nullptr;
84 }
85
Brian Salomonbdecacf2018-02-02 20:32:49 -050086 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -040087 return nullptr;
88 }
89
Robert Phillips45fdae12017-04-17 12:57:27 -040090 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050091 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -040092
Brian Salomon1fcac052018-05-09 16:33:09 -040093 SkColorType colorType;
94 if (GrPixelConfigToColorType(desc.fConfig, &colorType)) {
Chris Daltond004e0b2018-09-27 09:28:03 -060095 sk_sp<GrTexture> tex = (SkBackingFit::kApprox == fit)
96 ? this->createApproxTexture(desc, flags)
97 : this->createTexture(desc, budgeted, flags);
98 if (!tex) {
99 return nullptr;
100 }
101
102 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(std::move(tex),
103 kTopLeft_GrSurfaceOrigin);
Brian Salomon1fcac052018-05-09 16:33:09 -0400104 if (!proxy) {
105 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400106 }
Brian Salomon1fcac052018-05-09 16:33:09 -0400107 auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType,
Brian Osman9363ac42018-06-01 16:10:53 -0400108 kUnknown_SkAlphaType);
Brian Salomond6287472019-06-24 15:50:07 -0400109 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
110 std::move(proxy), SkColorTypeToGrColorType(colorType), kUnknown_SkAlphaType);
Brian Salomon1fcac052018-05-09 16:33:09 -0400111 if (!sContext) {
112 return nullptr;
113 }
114 SkAssertResult(sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400115 return sk_ref_sp(sContext->asTextureProxy()->peekTexture());
Brian Salomon1fcac052018-05-09 16:33:09 -0400116 } else {
117 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400118 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400119}
120
Brian Salomonbb8dde82019-06-27 10:52:13 -0400121sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
122 SkImage::CompressionType compression,
123 SkBudgeted budgeted, SkData* data) {
124 ASSERT_SINGLE_OWNER
125 if (this->isAbandoned()) {
126 return nullptr;
127 }
128 return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(),
129 data->size());
130}
131
Robert Phillipse78b7252017-04-06 07:59:41 -0400132sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600133 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400134 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400135 if (this->isAbandoned()) {
136 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500137 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400138
Brian Salomonbdecacf2018-02-02 20:32:49 -0500139 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400140 return nullptr;
141 }
142
Jim Van Verth1676cb92019-01-15 13:24:45 -0500143 // Compressed textures are read-only so they don't support re-use for scratch.
144 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
145 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
146 if (tex) {
147 return tex;
148 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400149 }
150
Robert Phillips67d52cf2017-06-05 13:38:13 -0400151 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500152}
153
Robert Phillips67d52cf2017-06-05 13:38:13 -0400154sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600155 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500156 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600157 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500158
Brian Osman32342f02017-03-04 08:12:46 -0500159 if (this->isAbandoned()) {
160 return nullptr;
161 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400162
Jim Van Verth1676cb92019-01-15 13:24:45 -0500163 // Currently we don't recycle compressed textures as scratch.
164 if (GrPixelConfigIsCompressed(desc.fConfig)) {
165 return nullptr;
166 }
167
Brian Salomonbdecacf2018-02-02 20:32:49 -0500168 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400169 return nullptr;
170 }
171
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400172 if (auto tex = this->refScratchTexture(desc, flags)) {
173 return tex;
174 }
175
Greg Daniel29bf84f2017-09-25 12:25:12 -0400176 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
177
178 // bin by pow2 with a reasonable min
179 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
180 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
181 GrSurfaceDesc* wdesc = copyDesc.writable();
182 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
183 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
184 }
185
186 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
187 return tex;
188 }
189
190 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500191}
192
Chris Daltond004e0b2018-09-27 09:28:03 -0600193sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500194 ASSERT_SINGLE_OWNER
195 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500196 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500197 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500198
Brian Salomond17b4a62017-05-23 16:53:47 -0400199 // We could make initial clears work with scratch textures but it is a rare case so we just opt
200 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400201 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
202 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500203
204 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400205 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600206 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
207 if (Flags::kNoPendingIO & flags) {
208 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400209 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500210 // If it is not a render target then it will most likely be populated by
211 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600212 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500213 }
214 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400215 GrSurface::WorstCaseSize(desc),
216 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500217 if (resource) {
218 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400219 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500220 }
221 }
222
Brian Osman32342f02017-03-04 08:12:46 -0500223 return nullptr;
224}
225
Greg Daniel7ef28f32017-04-20 16:41:55 +0000226sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500227 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500228 GrWrapCacheable cacheable,
229 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500230 ASSERT_SINGLE_OWNER
231 if (this->isAbandoned()) {
232 return nullptr;
233 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500234 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400235}
236
237sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400238 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500239 GrWrapOwnership ownership,
240 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400241 ASSERT_SINGLE_OWNER
242 if (this->isAbandoned()) {
243 return nullptr;
244 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500245 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500246}
247
248sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400249 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500250{
251 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400252 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500253}
254
Greg Danielb46add82019-01-02 14:51:29 -0500255sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
256 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
257 ASSERT_SINGLE_OWNER
258 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
259 vkInfo);
260
261}
262
Brian Osman32342f02017-03-04 08:12:46 -0500263void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
264 GrGpuResource* resource) {
265 ASSERT_SINGLE_OWNER
266 if (this->isAbandoned() || !resource) {
267 return;
268 }
269 resource->resourcePriv().setUniqueKey(key);
270}
271
Brian Salomond28a79d2017-10-16 13:01:07 -0400272sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500273 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400274 return this->isAbandoned() ? nullptr
275 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500276}
277
Brian Salomondbf70722019-02-07 11:31:24 -0500278sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
279 size_t size,
280 const void* data,
281 const GrUniqueKey& key) {
282 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400283 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700284 }
Brian Salomondbf70722019-02-07 11:31:24 -0500285 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500286 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500287 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700288 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
289 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
290 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500291 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700292 }
293 return nullptr;
294}
295
Brian Salomondbf70722019-02-07 11:31:24 -0500296sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
297 int patternSize,
298 int reps,
299 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500300 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700301 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
302
Brian Salomon09d994e2016-12-21 11:14:46 -0500303 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500304 sk_sp<GrGpuBuffer> buffer(
305 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700306 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700307 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700308 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400309 uint16_t* data = (uint16_t*) buffer->map();
310 SkAutoTArray<uint16_t> temp;
311 if (!data) {
312 temp.reset(reps * patternSize);
313 data = temp.get();
314 }
bsalomoned0bcad2015-05-04 10:36:42 -0700315 for (int i = 0; i < reps; ++i) {
316 int baseIdx = i * patternSize;
317 uint16_t baseVert = (uint16_t)(i * vertCount);
318 for (int j = 0; j < patternSize; ++j) {
319 data[baseIdx+j] = baseVert + pattern[j];
320 }
321 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400322 if (temp.get()) {
323 if (!buffer->updateData(data, bufferSize)) {
324 return nullptr;
325 }
326 } else {
327 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700328 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500329 if (key) {
330 SkASSERT(key->isValid());
331 this->assignUniqueKeyToResource(*key, buffer.get());
332 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400333 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700334}
335
Brian Salomon34169692017-08-28 15:32:01 -0400336static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
337
Brian Salomondbf70722019-02-07 11:31:24 -0500338sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700339 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000340 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500341 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700342}
343
Brian Salomon763abf02018-05-01 18:49:38 +0000344int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400345
Robert Phillips67d52cf2017-06-05 13:38:13 -0400346sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400347 if (this->isAbandoned()) {
348 return nullptr;
349 }
350
bsalomon706f08f2015-05-22 07:35:58 -0700351 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700352 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700353}
354
Brian Salomondbf70722019-02-07 11:31:24 -0500355sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
356 GrAccessPattern accessPattern,
357 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700358 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700359 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700360 }
cdaltond37fe762016-04-21 07:41:50 -0700361 if (kDynamic_GrAccessPattern != accessPattern) {
362 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
363 }
cdaltond37fe762016-04-21 07:41:50 -0700364 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400365 static const size_t MIN_SIZE = 1 << 12;
366 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700367
cdaltond37fe762016-04-21 07:41:50 -0700368 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500369 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
370 auto buffer =
371 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
372 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700373 if (!buffer) {
374 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
375 if (!buffer) {
376 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700377 }
378 }
cdaltond37fe762016-04-21 07:41:50 -0700379 if (data) {
380 buffer->updateData(data, size);
381 }
382 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800383}
384
Robert Phillipsc0192e32017-09-21 12:00:26 -0400385bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700386 SkASSERT(rt);
387 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400388 return true;
egdanielec00d942015-09-14 12:56:10 -0700389 }
390
391 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
392 GrUniqueKey sbKey;
393
394 int width = rt->width();
395 int height = rt->height();
396#if 0
397 if (this->caps()->oversizedStencilSupport()) {
398 width = SkNextPow2(width);
399 height = SkNextPow2(height);
400 }
401#endif
egdanielec00d942015-09-14 12:56:10 -0700402 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600403 rt->numSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400404 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700405 if (!stencil) {
406 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400407 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400408 if (!stencil) {
409 return false;
egdanielec00d942015-09-14 12:56:10 -0700410 }
Robert Phillips01a91282018-07-26 08:03:04 -0400411 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700412 }
Greg Danielcfa39352018-10-05 12:01:59 -0400413 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700414 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400415 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700416}
417
bungeman6bd52842016-10-27 09:30:08 -0700418sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400419 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700420{
ericrkf7b8b8a2016-02-24 14:49:51 -0800421 if (this->isAbandoned()) {
422 return nullptr;
423 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500424 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800425}
Greg Danield85f97d2017-03-07 13:37:21 -0500426
Greg Daniela5cb7812017-06-16 09:45:32 -0400427sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
428 return fGpu->makeSemaphore(isOwned);
429}
430
431sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500432 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400433 GrWrapOwnership ownership) {
434 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500435 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
436 wrapType,
437 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500438}