blob: d4346cd55f1e6729d400ec326832a7571c7f13d8 [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"
Brian Salomondbf70722019-02-07 11:31:24 -05009#include "../private/GrSingleOwner.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
robertphillips5fa7f302016-07-21 09:21:04 -070011#include "GrCaps.h"
Robert Phillips26c90e02017-03-14 14:39:29 -040012#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040013#include "GrContextPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070014#include "GrGpu.h"
Brian Salomondbf70722019-02-07 11:31:24 -050015#include "GrGpuBuffer.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"
Robert Phillips45fdae12017-04-17 12:57:27 -040025#include "SkGr.h"
halcanary4dbbd042016-06-07 17:21:10 -070026#include "SkMathPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070027
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);
Robert Phillips9da87e02019-02-04 13:26:26 -0500109 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400110 std::move(proxy));
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
Robert Phillipse78b7252017-04-06 07:59:41 -0400121sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600122 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400123 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400124 if (this->isAbandoned()) {
125 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500126 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400127
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400129 return nullptr;
130 }
131
Jim Van Verth1676cb92019-01-15 13:24:45 -0500132 // Compressed textures are read-only so they don't support re-use for scratch.
133 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
134 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
135 if (tex) {
136 return tex;
137 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400138 }
139
Robert Phillips67d52cf2017-06-05 13:38:13 -0400140 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500141}
142
Robert Phillips67d52cf2017-06-05 13:38:13 -0400143sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600144 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500145 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600146 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500147
Brian Osman32342f02017-03-04 08:12:46 -0500148 if (this->isAbandoned()) {
149 return nullptr;
150 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400151
Jim Van Verth1676cb92019-01-15 13:24:45 -0500152 // Currently we don't recycle compressed textures as scratch.
153 if (GrPixelConfigIsCompressed(desc.fConfig)) {
154 return nullptr;
155 }
156
Brian Salomonbdecacf2018-02-02 20:32:49 -0500157 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400158 return nullptr;
159 }
160
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400161 if (auto tex = this->refScratchTexture(desc, flags)) {
162 return tex;
163 }
164
Greg Daniel29bf84f2017-09-25 12:25:12 -0400165 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
166
167 // bin by pow2 with a reasonable min
168 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
169 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
170 GrSurfaceDesc* wdesc = copyDesc.writable();
171 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
172 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
173 }
174
175 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
176 return tex;
177 }
178
179 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500180}
181
Chris Daltond004e0b2018-09-27 09:28:03 -0600182sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500183 ASSERT_SINGLE_OWNER
184 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500185 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500186 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500187
Brian Salomond17b4a62017-05-23 16:53:47 -0400188 // We could make initial clears work with scratch textures but it is a rare case so we just opt
189 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400190 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
191 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500192
193 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400194 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600195 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
196 if (Flags::kNoPendingIO & flags) {
197 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400198 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500199 // If it is not a render target then it will most likely be populated by
200 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600201 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500202 }
203 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400204 GrSurface::WorstCaseSize(desc),
205 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500206 if (resource) {
207 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400208 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500209 }
210 }
211
Brian Osman32342f02017-03-04 08:12:46 -0500212 return nullptr;
213}
214
Greg Daniel7ef28f32017-04-20 16:41:55 +0000215sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500216 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500217 GrWrapCacheable cacheable,
218 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500219 ASSERT_SINGLE_OWNER
220 if (this->isAbandoned()) {
221 return nullptr;
222 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500223 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400224}
225
226sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400227 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500228 GrWrapOwnership ownership,
229 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400230 ASSERT_SINGLE_OWNER
231 if (this->isAbandoned()) {
232 return nullptr;
233 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500234 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500235}
236
237sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400238 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500239{
240 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400241 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500242}
243
Greg Danielb46add82019-01-02 14:51:29 -0500244sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
245 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
246 ASSERT_SINGLE_OWNER
247 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
248 vkInfo);
249
250}
251
Brian Osman32342f02017-03-04 08:12:46 -0500252void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
253 GrGpuResource* resource) {
254 ASSERT_SINGLE_OWNER
255 if (this->isAbandoned() || !resource) {
256 return;
257 }
258 resource->resourcePriv().setUniqueKey(key);
259}
260
Brian Salomond28a79d2017-10-16 13:01:07 -0400261sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500262 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400263 return this->isAbandoned() ? nullptr
264 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500265}
266
Brian Salomondbf70722019-02-07 11:31:24 -0500267sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
268 size_t size,
269 const void* data,
270 const GrUniqueKey& key) {
271 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400272 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700273 }
Brian Salomondbf70722019-02-07 11:31:24 -0500274 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500275 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500276 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700277 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
278 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
279 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500280 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700281 }
282 return nullptr;
283}
284
Brian Salomondbf70722019-02-07 11:31:24 -0500285sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
286 int patternSize,
287 int reps,
288 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500289 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700290 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
291
Brian Salomon09d994e2016-12-21 11:14:46 -0500292 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500293 sk_sp<GrGpuBuffer> buffer(
294 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700295 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700297 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400298 uint16_t* data = (uint16_t*) buffer->map();
299 SkAutoTArray<uint16_t> temp;
300 if (!data) {
301 temp.reset(reps * patternSize);
302 data = temp.get();
303 }
bsalomoned0bcad2015-05-04 10:36:42 -0700304 for (int i = 0; i < reps; ++i) {
305 int baseIdx = i * patternSize;
306 uint16_t baseVert = (uint16_t)(i * vertCount);
307 for (int j = 0; j < patternSize; ++j) {
308 data[baseIdx+j] = baseVert + pattern[j];
309 }
310 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400311 if (temp.get()) {
312 if (!buffer->updateData(data, bufferSize)) {
313 return nullptr;
314 }
315 } else {
316 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700317 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500318 if (key) {
319 SkASSERT(key->isValid());
320 this->assignUniqueKeyToResource(*key, buffer.get());
321 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400322 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700323}
324
Brian Salomon34169692017-08-28 15:32:01 -0400325static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
326
Brian Salomondbf70722019-02-07 11:31:24 -0500327sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700328 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000329 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500330 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700331}
332
Brian Salomon763abf02018-05-01 18:49:38 +0000333int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400334
Robert Phillips67d52cf2017-06-05 13:38:13 -0400335sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400336 if (this->isAbandoned()) {
337 return nullptr;
338 }
339
bsalomon706f08f2015-05-22 07:35:58 -0700340 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700341 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700342}
343
Brian Salomondbf70722019-02-07 11:31:24 -0500344sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
345 GrAccessPattern accessPattern,
346 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700347 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700348 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700349 }
cdaltond37fe762016-04-21 07:41:50 -0700350 if (kDynamic_GrAccessPattern != accessPattern) {
351 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
352 }
cdaltond37fe762016-04-21 07:41:50 -0700353 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400354 static const size_t MIN_SIZE = 1 << 12;
355 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700356
cdaltond37fe762016-04-21 07:41:50 -0700357 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500358 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
359 auto buffer =
360 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
361 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700362 if (!buffer) {
363 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
364 if (!buffer) {
365 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700366 }
367 }
cdaltond37fe762016-04-21 07:41:50 -0700368 if (data) {
369 buffer->updateData(data, size);
370 }
371 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800372}
373
Robert Phillipsc0192e32017-09-21 12:00:26 -0400374bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700375 SkASSERT(rt);
376 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400377 return true;
egdanielec00d942015-09-14 12:56:10 -0700378 }
379
380 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
381 GrUniqueKey sbKey;
382
383 int width = rt->width();
384 int height = rt->height();
385#if 0
386 if (this->caps()->oversizedStencilSupport()) {
387 width = SkNextPow2(width);
388 height = SkNextPow2(height);
389 }
390#endif
egdanielec00d942015-09-14 12:56:10 -0700391 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
392 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400393 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700394 if (!stencil) {
395 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400396 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400397 if (!stencil) {
398 return false;
egdanielec00d942015-09-14 12:56:10 -0700399 }
Robert Phillips01a91282018-07-26 08:03:04 -0400400 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700401 }
Greg Danielcfa39352018-10-05 12:01:59 -0400402 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700403 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400404 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700405}
406
bungeman6bd52842016-10-27 09:30:08 -0700407sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400408 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700409{
ericrkf7b8b8a2016-02-24 14:49:51 -0800410 if (this->isAbandoned()) {
411 return nullptr;
412 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500413 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800414}
Greg Danield85f97d2017-03-07 13:37:21 -0500415
Greg Daniela5cb7812017-06-16 09:45:32 -0400416sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
417 return fGpu->makeSemaphore(isOwned);
418}
419
420sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500421 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400422 GrWrapOwnership ownership) {
423 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500424 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
425 wrapType,
426 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500427}