blob: 4f024c9636a193e87ef4c3ba38499bde48c832e6 [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 Salomon1d435302019-07-01 13:05:28 -0400107 // Here we don't really know the alpha type of the data we want to upload. All we really
108 // care about is that it is not converted. So we use the same alpha type of the data
109 // and the surface context.
110 static constexpr auto kAlphaType = kPremul_SkAlphaType;
111 auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType, kAlphaType);
Brian Salomond6287472019-06-24 15:50:07 -0400112 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomon1d435302019-07-01 13:05:28 -0400113 std::move(proxy), SkColorTypeToGrColorType(colorType), kAlphaType);
Brian Salomon1fcac052018-05-09 16:33:09 -0400114 if (!sContext) {
115 return nullptr;
116 }
Brian Salomon1d435302019-07-01 13:05:28 -0400117 SkAssertResult(
118 sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, {0, 0}));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400119 return sk_ref_sp(sContext->asTextureProxy()->peekTexture());
Brian Salomon1fcac052018-05-09 16:33:09 -0400120 } else {
121 return fGpu->createTexture(desc, budgeted, &mipLevel, 1);
Robert Phillips45fdae12017-04-17 12:57:27 -0400122 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400123}
124
Brian Salomonbb8dde82019-06-27 10:52:13 -0400125sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
126 SkImage::CompressionType compression,
127 SkBudgeted budgeted, SkData* data) {
128 ASSERT_SINGLE_OWNER
129 if (this->isAbandoned()) {
130 return nullptr;
131 }
132 return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(),
133 data->size());
134}
135
Robert Phillipse78b7252017-04-06 07:59:41 -0400136sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600137 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400138 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
Jim Van Verth1676cb92019-01-15 13:24:45 -0500147 // Compressed textures are read-only so they don't support re-use for scratch.
148 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
149 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
150 if (tex) {
151 return tex;
152 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400153 }
154
Robert Phillips67d52cf2017-06-05 13:38:13 -0400155 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500156}
157
Robert Phillips67d52cf2017-06-05 13:38:13 -0400158sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Chris Daltond004e0b2018-09-27 09:28:03 -0600159 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500160 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600161 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500162
Brian Osman32342f02017-03-04 08:12:46 -0500163 if (this->isAbandoned()) {
164 return nullptr;
165 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400166
Jim Van Verth1676cb92019-01-15 13:24:45 -0500167 // Currently we don't recycle compressed textures as scratch.
168 if (GrPixelConfigIsCompressed(desc.fConfig)) {
169 return nullptr;
170 }
171
Brian Salomonbdecacf2018-02-02 20:32:49 -0500172 if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400173 return nullptr;
174 }
175
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400176 if (auto tex = this->refScratchTexture(desc, flags)) {
177 return tex;
178 }
179
Greg Daniel29bf84f2017-09-25 12:25:12 -0400180 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
181
182 // bin by pow2 with a reasonable min
183 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
184 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
185 GrSurfaceDesc* wdesc = copyDesc.writable();
186 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
187 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
188 }
189
190 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
191 return tex;
192 }
193
194 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500195}
196
Chris Daltond004e0b2018-09-27 09:28:03 -0600197sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500198 ASSERT_SINGLE_OWNER
199 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500200 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500201 SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500202
Brian Salomond17b4a62017-05-23 16:53:47 -0400203 // We could make initial clears work with scratch textures but it is a rare case so we just opt
204 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400205 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
206 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500207
208 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400209 GrTexturePriv::ComputeScratchKey(desc, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600210 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
211 if (Flags::kNoPendingIO & flags) {
212 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400213 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500214 // If it is not a render target then it will most likely be populated by
215 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600216 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500217 }
218 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400219 GrSurface::WorstCaseSize(desc),
220 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500221 if (resource) {
222 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400223 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500224 }
225 }
226
Brian Osman32342f02017-03-04 08:12:46 -0500227 return nullptr;
228}
229
Greg Daniel7ef28f32017-04-20 16:41:55 +0000230sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500231 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500232 GrWrapCacheable cacheable,
233 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500234 ASSERT_SINGLE_OWNER
235 if (this->isAbandoned()) {
236 return nullptr;
237 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500238 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400239}
240
241sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400242 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500243 GrWrapOwnership ownership,
244 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400245 ASSERT_SINGLE_OWNER
246 if (this->isAbandoned()) {
247 return nullptr;
248 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500249 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500250}
251
252sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400253 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500254{
255 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400256 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500257}
258
Greg Danielb46add82019-01-02 14:51:29 -0500259sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
260 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
261 ASSERT_SINGLE_OWNER
262 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
263 vkInfo);
264
265}
266
Brian Osman32342f02017-03-04 08:12:46 -0500267void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
268 GrGpuResource* resource) {
269 ASSERT_SINGLE_OWNER
270 if (this->isAbandoned() || !resource) {
271 return;
272 }
273 resource->resourcePriv().setUniqueKey(key);
274}
275
Brian Salomond28a79d2017-10-16 13:01:07 -0400276sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500277 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400278 return this->isAbandoned() ? nullptr
279 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500280}
281
Brian Salomondbf70722019-02-07 11:31:24 -0500282sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
283 size_t size,
284 const void* data,
285 const GrUniqueKey& key) {
286 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400287 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700288 }
Brian Salomondbf70722019-02-07 11:31:24 -0500289 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500290 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500291 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700292 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
293 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
294 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500295 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700296 }
297 return nullptr;
298}
299
Brian Salomondbf70722019-02-07 11:31:24 -0500300sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
301 int patternSize,
302 int reps,
303 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500304 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700305 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
306
Brian Salomon09d994e2016-12-21 11:14:46 -0500307 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500308 sk_sp<GrGpuBuffer> buffer(
309 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700310 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700312 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400313 uint16_t* data = (uint16_t*) buffer->map();
314 SkAutoTArray<uint16_t> temp;
315 if (!data) {
316 temp.reset(reps * patternSize);
317 data = temp.get();
318 }
bsalomoned0bcad2015-05-04 10:36:42 -0700319 for (int i = 0; i < reps; ++i) {
320 int baseIdx = i * patternSize;
321 uint16_t baseVert = (uint16_t)(i * vertCount);
322 for (int j = 0; j < patternSize; ++j) {
323 data[baseIdx+j] = baseVert + pattern[j];
324 }
325 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400326 if (temp.get()) {
327 if (!buffer->updateData(data, bufferSize)) {
328 return nullptr;
329 }
330 } else {
331 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700332 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500333 if (key) {
334 SkASSERT(key->isValid());
335 this->assignUniqueKeyToResource(*key, buffer.get());
336 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400337 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700338}
339
Brian Salomon34169692017-08-28 15:32:01 -0400340static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
341
Brian Salomondbf70722019-02-07 11:31:24 -0500342sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700343 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000344 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500345 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700346}
347
Brian Salomon763abf02018-05-01 18:49:38 +0000348int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400349
Robert Phillips67d52cf2017-06-05 13:38:13 -0400350sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400351 if (this->isAbandoned()) {
352 return nullptr;
353 }
354
bsalomon706f08f2015-05-22 07:35:58 -0700355 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700356 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700357}
358
Brian Salomondbf70722019-02-07 11:31:24 -0500359sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
360 GrAccessPattern accessPattern,
361 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700362 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700363 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700364 }
cdaltond37fe762016-04-21 07:41:50 -0700365 if (kDynamic_GrAccessPattern != accessPattern) {
366 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
367 }
cdaltond37fe762016-04-21 07:41:50 -0700368 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400369 static const size_t MIN_SIZE = 1 << 12;
370 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700371
cdaltond37fe762016-04-21 07:41:50 -0700372 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500373 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
374 auto buffer =
375 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
376 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700377 if (!buffer) {
378 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
379 if (!buffer) {
380 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700381 }
382 }
cdaltond37fe762016-04-21 07:41:50 -0700383 if (data) {
384 buffer->updateData(data, size);
385 }
386 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800387}
388
Chris Daltoneffee202019-07-01 22:28:03 -0600389bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700390 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600391 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
392 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400393 return true;
egdanielec00d942015-09-14 12:56:10 -0700394 }
395
396 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
397 GrUniqueKey sbKey;
398
399 int width = rt->width();
400 int height = rt->height();
401#if 0
402 if (this->caps()->oversizedStencilSupport()) {
403 width = SkNextPow2(width);
404 height = SkNextPow2(height);
405 }
406#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600407 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
408 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400409 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700410 if (!stencil) {
411 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600412 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
413 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400414 if (!stencil) {
415 return false;
egdanielec00d942015-09-14 12:56:10 -0700416 }
Robert Phillips01a91282018-07-26 08:03:04 -0400417 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700418 }
Greg Danielcfa39352018-10-05 12:01:59 -0400419 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700420 }
Chris Dalton215ff332019-07-02 09:38:22 -0600421
422 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
423 return stencil->numSamples() >= minStencilSampleCount;
424 }
425 return false;
egdanielec00d942015-09-14 12:56:10 -0700426}
427
bungeman6bd52842016-10-27 09:30:08 -0700428sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400429 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700430{
ericrkf7b8b8a2016-02-24 14:49:51 -0800431 if (this->isAbandoned()) {
432 return nullptr;
433 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800435}
Greg Danield85f97d2017-03-07 13:37:21 -0500436
Greg Daniela5cb7812017-06-16 09:45:32 -0400437sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
438 return fGpu->makeSemaphore(isOwned);
439}
440
441sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500442 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400443 GrWrapOwnership ownership) {
444 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500445 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
446 wrapType,
447 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500448}