blob: 622cea236e52fd6b36365b0b824588a458e9440e [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 Phillips4150eea2018-02-07 17:08:21 -050033GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner,
Robert Phillips292a6b22019-02-14 14:49:02 -050034 bool explicitlyAllocateGPUResources)
Brian Osman32342f02017-03-04 08:12:46 -050035 : fCache(cache)
36 , fGpu(gpu)
Robert Phillips292a6b22019-02-14 14:49:02 -050037 , fExplicitlyAllocateGPUResources(explicitlyAllocateGPUResources)
Brian Osman32342f02017-03-04 08:12:46 -050038#ifdef SK_DEBUG
39 , fSingleOwner(owner)
40#endif
Robert Phillips4150eea2018-02-07 17:08:21 -050041{
Robert Phillips26c90e02017-03-14 14:39:29 -040042 fCaps = sk_ref_sp(fGpu->caps());
bsalomoned0bcad2015-05-04 10:36:42 -070043}
44
Robert Phillips8e8c7552017-07-10 12:06:05 -040045sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Osman2b23c4b2018-06-01 12:25:08 -040046 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050047 ASSERT_SINGLE_OWNER
48
Robert Phillips7f1b4f82017-11-28 07:38:39 -050049 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040050
Brian Osman32342f02017-03-04 08:12:46 -050051 if (this->isAbandoned()) {
52 return nullptr;
53 }
Robert Phillips1119dc32017-04-11 12:54:57 -040054
Brian Salomonbdecacf2018-02-02 20:32:49 -050055 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
56 if (!fCaps->validateSurfaceDesc(desc, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050057 return nullptr;
58 }
Brian Osman32342f02017-03-04 08:12:46 -050059
Brian Osman2b23c4b2018-06-01 12:25:08 -040060 return fGpu->createTexture(desc, budgeted, texels, mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -050061}
62
Robert Phillips45fdae12017-04-17 12:57:27 -040063sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -060064 SkBudgeted budgeted, Flags flags) {
Robert Phillips45fdae12017-04-17 12:57:27 -040065 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
66 if (tex && SkBudgeted::kNo == budgeted) {
67 tex->resourcePriv().makeUnbudgeted();
68 }
69
70 return tex;
71}
72
Robert Phillips1afd4cd2018-01-08 13:40:32 -050073sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
74 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -050075 SkBackingFit fit,
Chris Daltond004e0b2018-09-27 09:28:03 -060076 const GrMipLevel& mipLevel,
77 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -040078 ASSERT_SINGLE_OWNER
79
80 if (this->isAbandoned()) {
81 return nullptr;
82 }
83
Robert Phillips45fdae12017-04-17 12:57:27 -040084 if (!mipLevel.fPixels) {
85 return nullptr;
86 }
87
Brian Salomonbdecacf2018-02-02 20:32:49 -050088 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -040089 return nullptr;
90 }
91
Robert Phillips45fdae12017-04-17 12:57:27 -040092 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050093 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -040094
Brian Salomon1fcac052018-05-09 16:33:09 -040095 SkColorType colorType;
96 if (GrPixelConfigToColorType(desc.fConfig, &colorType)) {
Chris Daltond004e0b2018-09-27 09:28:03 -060097 sk_sp<GrTexture> tex = (SkBackingFit::kApprox == fit)
98 ? this->createApproxTexture(desc, flags)
99 : this->createTexture(desc, budgeted, flags);
100 if (!tex) {
101 return nullptr;
102 }
103
104 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(std::move(tex),
105 kTopLeft_GrSurfaceOrigin);
Brian Salomon1fcac052018-05-09 16:33:09 -0400106 if (!proxy) {
107 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400108 }
Brian Salomon1fcac052018-05-09 16:33:09 -0400109 auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType,
Brian Osman9363ac42018-06-01 16:10:53 -0400110 kUnknown_SkAlphaType);
Robert Phillips9da87e02019-02-04 13:26:26 -0500111 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400112 std::move(proxy));
Brian Salomon1fcac052018-05-09 16:33:09 -0400113 if (!sContext) {
114 return nullptr;
115 }
116 SkAssertResult(sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400117 return sk_ref_sp(sContext->asTextureProxy()->peekTexture());
Brian Salomon1fcac052018-05-09 16:33:09 -0400118 } else {
119 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400120 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400121}
122
Robert Phillipse78b7252017-04-06 07:59:41 -0400123sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600124 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400125 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400126 if (this->isAbandoned()) {
127 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500128 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400129
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400131 return nullptr;
132 }
133
Jim Van Verth1676cb92019-01-15 13:24:45 -0500134 // Compressed textures are read-only so they don't support re-use for scratch.
135 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
136 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
137 if (tex) {
138 return tex;
139 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400140 }
141
Robert Phillips67d52cf2017-06-05 13:38:13 -0400142 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500143}
144
Robert Phillips67d52cf2017-06-05 13:38:13 -0400145sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600146 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500147 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600148 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500149
Brian Osman32342f02017-03-04 08:12:46 -0500150 if (this->isAbandoned()) {
151 return nullptr;
152 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400153
Jim Van Verth1676cb92019-01-15 13:24:45 -0500154 // Currently we don't recycle compressed textures as scratch.
155 if (GrPixelConfigIsCompressed(desc.fConfig)) {
156 return nullptr;
157 }
158
Brian Salomonbdecacf2018-02-02 20:32:49 -0500159 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400160 return nullptr;
161 }
162
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400163 if (auto tex = this->refScratchTexture(desc, flags)) {
164 return tex;
165 }
166
Greg Daniel29bf84f2017-09-25 12:25:12 -0400167 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
168
169 // bin by pow2 with a reasonable min
170 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
171 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
172 GrSurfaceDesc* wdesc = copyDesc.writable();
173 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
174 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
175 }
176
177 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
178 return tex;
179 }
180
181 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500182}
183
Chris Daltond004e0b2018-09-27 09:28:03 -0600184sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500185 ASSERT_SINGLE_OWNER
186 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500187 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500188 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500189
Brian Salomond17b4a62017-05-23 16:53:47 -0400190 // We could make initial clears work with scratch textures but it is a rare case so we just opt
191 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400192 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
193 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500194
195 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400196 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600197 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
198 if (Flags::kNoPendingIO & flags) {
199 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400200 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500201 // If it is not a render target then it will most likely be populated by
202 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600203 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500204 }
205 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400206 GrSurface::WorstCaseSize(desc),
207 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500208 if (resource) {
209 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400210 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500211 }
212 }
213
Brian Osman32342f02017-03-04 08:12:46 -0500214 return nullptr;
215}
216
Greg Daniel7ef28f32017-04-20 16:41:55 +0000217sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500218 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500219 GrWrapCacheable cacheable,
220 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500221 ASSERT_SINGLE_OWNER
222 if (this->isAbandoned()) {
223 return nullptr;
224 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500225 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400226}
227
228sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400229 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500230 GrWrapOwnership ownership,
231 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400232 ASSERT_SINGLE_OWNER
233 if (this->isAbandoned()) {
234 return nullptr;
235 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500236 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500237}
238
239sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400240 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500241{
242 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400243 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500244}
245
Greg Danielb46add82019-01-02 14:51:29 -0500246sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
247 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
248 ASSERT_SINGLE_OWNER
249 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
250 vkInfo);
251
252}
253
Brian Osman32342f02017-03-04 08:12:46 -0500254void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
255 GrGpuResource* resource) {
256 ASSERT_SINGLE_OWNER
257 if (this->isAbandoned() || !resource) {
258 return;
259 }
260 resource->resourcePriv().setUniqueKey(key);
261}
262
Brian Salomond28a79d2017-10-16 13:01:07 -0400263sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500264 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400265 return this->isAbandoned() ? nullptr
266 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500267}
268
Brian Salomondbf70722019-02-07 11:31:24 -0500269sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
270 size_t size,
271 const void* data,
272 const GrUniqueKey& key) {
273 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400274 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700275 }
Brian Salomondbf70722019-02-07 11:31:24 -0500276 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500277 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500278 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700279 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
280 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
281 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500282 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700283 }
284 return nullptr;
285}
286
Brian Salomondbf70722019-02-07 11:31:24 -0500287sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
288 int patternSize,
289 int reps,
290 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500291 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700292 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
293
Brian Salomon09d994e2016-12-21 11:14:46 -0500294 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500295 sk_sp<GrGpuBuffer> buffer(
296 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700297 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700298 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700299 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400300 uint16_t* data = (uint16_t*) buffer->map();
301 SkAutoTArray<uint16_t> temp;
302 if (!data) {
303 temp.reset(reps * patternSize);
304 data = temp.get();
305 }
bsalomoned0bcad2015-05-04 10:36:42 -0700306 for (int i = 0; i < reps; ++i) {
307 int baseIdx = i * patternSize;
308 uint16_t baseVert = (uint16_t)(i * vertCount);
309 for (int j = 0; j < patternSize; ++j) {
310 data[baseIdx+j] = baseVert + pattern[j];
311 }
312 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400313 if (temp.get()) {
314 if (!buffer->updateData(data, bufferSize)) {
315 return nullptr;
316 }
317 } else {
318 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700319 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500320 if (key) {
321 SkASSERT(key->isValid());
322 this->assignUniqueKeyToResource(*key, buffer.get());
323 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400324 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700325}
326
Brian Salomon34169692017-08-28 15:32:01 -0400327static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
328
Brian Salomondbf70722019-02-07 11:31:24 -0500329sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700330 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000331 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500332 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700333}
334
Brian Salomon763abf02018-05-01 18:49:38 +0000335int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400336
Robert Phillips67d52cf2017-06-05 13:38:13 -0400337sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400338 if (this->isAbandoned()) {
339 return nullptr;
340 }
341
bsalomon706f08f2015-05-22 07:35:58 -0700342 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700343 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700344}
345
Brian Salomondbf70722019-02-07 11:31:24 -0500346sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
347 GrAccessPattern accessPattern,
348 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700349 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700350 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700351 }
cdaltond37fe762016-04-21 07:41:50 -0700352 if (kDynamic_GrAccessPattern != accessPattern) {
353 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
354 }
cdaltond37fe762016-04-21 07:41:50 -0700355 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400356 static const size_t MIN_SIZE = 1 << 12;
357 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700358
cdaltond37fe762016-04-21 07:41:50 -0700359 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500360 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
361 auto buffer =
362 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
363 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700364 if (!buffer) {
365 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
366 if (!buffer) {
367 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700368 }
369 }
cdaltond37fe762016-04-21 07:41:50 -0700370 if (data) {
371 buffer->updateData(data, size);
372 }
373 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800374}
375
Robert Phillipsc0192e32017-09-21 12:00:26 -0400376bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700377 SkASSERT(rt);
378 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400379 return true;
egdanielec00d942015-09-14 12:56:10 -0700380 }
381
382 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
383 GrUniqueKey sbKey;
384
385 int width = rt->width();
386 int height = rt->height();
387#if 0
388 if (this->caps()->oversizedStencilSupport()) {
389 width = SkNextPow2(width);
390 height = SkNextPow2(height);
391 }
392#endif
egdanielec00d942015-09-14 12:56:10 -0700393 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
394 rt->numStencilSamples(), &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400395 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700396 if (!stencil) {
397 // Need to try and create a new stencil
Brian Salomond28a79d2017-10-16 13:01:07 -0400398 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
Robert Phillips01a91282018-07-26 08:03:04 -0400399 if (!stencil) {
400 return false;
egdanielec00d942015-09-14 12:56:10 -0700401 }
Robert Phillips01a91282018-07-26 08:03:04 -0400402 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700403 }
Greg Danielcfa39352018-10-05 12:01:59 -0400404 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700405 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400406 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700407}
408
bungeman6bd52842016-10-27 09:30:08 -0700409sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400410 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700411{
ericrkf7b8b8a2016-02-24 14:49:51 -0800412 if (this->isAbandoned()) {
413 return nullptr;
414 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500415 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800416}
Greg Danield85f97d2017-03-07 13:37:21 -0500417
Greg Daniela5cb7812017-06-16 09:45:32 -0400418sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
419 return fGpu->makeSemaphore(isOwned);
420}
421
422sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500423 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400424 GrWrapOwnership ownership) {
425 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500426 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
427 wrapType,
428 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500429}