blob: 82a0d12c7fb1b5990fbe2a8999853f827fce25cf [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
Brian Salomon1047a492019-07-02 12:25:21 -04008#include "src/gpu/GrResourceProvider.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrContext.h"
12#include "include/private/GrResourceKey.h"
13#include "include/private/GrSingleOwner.h"
Brian Salomon1047a492019-07-02 12:25:21 -040014#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
16#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrGpuBuffer.h"
20#include "src/gpu/GrPath.h"
21#include "src/gpu/GrPathRendering.h"
22#include "src/gpu/GrProxyProvider.h"
23#include "src/gpu/GrRenderTargetPriv.h"
24#include "src/gpu/GrResourceCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrSemaphore.h"
26#include "src/gpu/GrStencilAttachment.h"
27#include "src/gpu/GrTexturePriv.h"
28#include "src/gpu/SkGr.h"
bsalomoned0bcad2015-05-04 10:36:42 -070029
Robert Phillips1bfece82017-06-01 13:56:52 -040030const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050031
32#define ASSERT_SINGLE_OWNER \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34
Robert Phillips12c46292019-04-23 07:36:17 -040035GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050036 : fCache(cache)
37 , fGpu(gpu)
38#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
Brian Salomona3ffaab2019-07-09 12:26:46 -040045// Ensures the row bytes are populated (not 0) and makes a copy to a temporary
46// to make the row bytes tight if necessary. Returns false if the input row bytes are invalid.
Brian Salomona90382f2019-09-17 09:01:56 -040047static bool prepare_level(const GrMipLevel& inLevel, int w, int h, bool rowBytesSupport,
48 GrColorType origColorType, GrColorType allowedColorType,
Brian Salomond2a8ae22019-09-10 16:03:59 -040049 GrMipLevel* outLevel, std::unique_ptr<char[]>* data) {
Brian Salomona3ffaab2019-07-09 12:26:46 -040050 if (!inLevel.fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -040051 outLevel->fPixels = nullptr;
52 outLevel->fRowBytes = 0;
Brian Salomona3ffaab2019-07-09 12:26:46 -040053 return true;
54 }
Brian Salomona90382f2019-09-17 09:01:56 -040055 size_t minRB = w * GrColorTypeBytesPerPixel(origColorType);
Brian Salomona3ffaab2019-07-09 12:26:46 -040056 size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB;
57 if (actualRB < minRB) {
58 return false;
59 }
Brian Salomona90382f2019-09-17 09:01:56 -040060 if (origColorType == allowedColorType && (actualRB == minRB || rowBytesSupport)) {
Brian Salomona3ffaab2019-07-09 12:26:46 -040061 outLevel->fRowBytes = actualRB;
62 outLevel->fPixels = inLevel.fPixels;
Brian Salomona90382f2019-09-17 09:01:56 -040063 return true;
Brian Salomona3ffaab2019-07-09 12:26:46 -040064 }
Brian Salomona90382f2019-09-17 09:01:56 -040065 auto tempRB = w * GrColorTypeBytesPerPixel(allowedColorType);
66 data->reset(new char[tempRB * h]);
67 outLevel->fPixels = data->get();
68 outLevel->fRowBytes = minRB;
69 GrPixelInfo srcInfo(origColorType, kUnpremul_SkAlphaType, nullptr, w, h);
70 GrPixelInfo dstInfo(allowedColorType, kUnpremul_SkAlphaType, nullptr, w, h);
71 return GrConvertPixels(dstInfo, data->get(), tempRB, srcInfo, inLevel.fPixels, actualRB);
Brian Salomona3ffaab2019-07-09 12:26:46 -040072}
73
Brian Salomonf2c2ba92019-07-17 09:59:59 -040074sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040075 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -040076 GrColorType colorType,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040077 GrRenderable renderable,
Brian Salomon2af3e702019-08-11 19:10:31 -040078 int renderTargetSampleCnt,
79 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040080 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -040081 const GrMipLevel texels[],
82 int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050083 ASSERT_SINGLE_OWNER
84
Robert Phillips7f1b4f82017-11-28 07:38:39 -050085 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040086
Brian Osman32342f02017-03-04 08:12:46 -050087 if (this->isAbandoned()) {
88 return nullptr;
89 }
Robert Phillips1119dc32017-04-11 12:54:57 -040090
Brian Salomonbdecacf2018-02-02 20:32:49 -050091 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -040092 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
93 renderTargetSampleCnt, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050094 return nullptr;
95 }
Brian Salomona90382f2019-09-17 09:01:56 -040096 auto allowedColorType =
97 this->caps()->supportedWritePixelsColorType(colorType, format, colorType).fColorType;
98 if (allowedColorType == GrColorType::kUnknown) {
99 return nullptr;
100 }
Brian Salomona3e29962019-07-16 11:52:08 -0400101 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
Brian Salomon1047a492019-07-02 12:25:21 -0400102 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -0400103 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon1047a492019-07-02 12:25:21 -0400104 if (mipLevelCount > 0 && texels) {
105 tmpTexels.reset(mipLevelCount);
106 tmpDatas.reset(mipLevelCount);
107 int w = desc.fWidth;
108 int h = desc.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -0400109 for (int i = 0; i < mipLevelCount; ++i) {
Brian Salomona90382f2019-09-17 09:01:56 -0400110 if (!prepare_level(texels[i], w, h, rowBytesSupport, colorType, allowedColorType,
111 &tmpTexels[i], &tmpDatas[i])) {
Brian Salomona3ffaab2019-07-09 12:26:46 -0400112 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -0400113 }
114 w = std::max(w / 2, 1);
115 h = std::max(h / 2, 1);
116 }
117 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
Brian Salomona90382f2019-09-17 09:01:56 -0400119 isProtected, colorType, colorType, tmpTexels.get(), mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -0500120}
121
Robert Phillips45fdae12017-04-17 12:57:27 -0400122sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400123 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400124 GrRenderable renderable,
125 int renderTargetSampleCnt,
126 SkBudgeted budgeted,
Brian Salomon14cb4132019-09-16 13:14:47 -0400127 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400128 GrProtected isProtected) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400129 sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400130 mipMapped, isProtected));
Robert Phillips45fdae12017-04-17 12:57:27 -0400131 if (tex && SkBudgeted::kNo == budgeted) {
132 tex->resourcePriv().makeUnbudgeted();
133 }
134
135 return tex;
136}
137
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500138sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -0400140 GrColorType colorType,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400141 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400142 int renderTargetSampleCnt,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500143 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500144 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400145 GrProtected isProtected,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400146 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400147 ASSERT_SINGLE_OWNER
148
149 if (this->isAbandoned()) {
150 return nullptr;
151 }
152
Robert Phillips45fdae12017-04-17 12:57:27 -0400153 if (!mipLevel.fPixels) {
154 return nullptr;
155 }
156
Greg Daniel6fa62e22019-08-07 15:52:37 -0400157 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
158 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400159 return nullptr;
160 }
161
Robert Phillips45fdae12017-04-17 12:57:27 -0400162 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500163 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400164
Brian Salomona90382f2019-09-17 09:01:56 -0400165 sk_sp<GrTexture> tex;
166 if (SkBackingFit::kApprox == fit) {
167 tex = this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
168 isProtected);
169 if (!tex) {
170 return nullptr;
171 }
172 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(
173 tex, colorType, kTopLeft_GrSurfaceOrigin, GrSurfaceProxy::UseAllocator::kYes);
174 if (!proxy) {
175 return nullptr;
176 }
177 // Here we don't really know the alpha type of the data we want to upload. All we really
178 // care about is that it is not converted. So we use the same alpha type for the data
179 // and the surface context.
180 static constexpr auto kAlphaType = kUnpremul_SkAlphaType;
181 auto sContext =
182 context->priv().makeWrappedSurfaceContext(std::move(proxy), colorType, kAlphaType);
183 if (!sContext) {
184 return nullptr;
185 }
186 GrPixelInfo srcInfo(colorType, kAlphaType, nullptr, desc.fWidth, desc.fHeight);
187 SkAssertResult(
188 sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, {0, 0}));
189 return tex;
190 } else {
191 return this->createTexture(desc, format, colorType, renderable, renderTargetSampleCnt,
192 budgeted, isProtected, &mipLevel, 1);
Brian Salomona3ffaab2019-07-09 12:26:46 -0400193 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400194}
195
Brian Salomonbb8dde82019-06-27 10:52:13 -0400196sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400197 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400198 SkImage::CompressionType compression,
199 SkBudgeted budgeted, SkData* data) {
200 ASSERT_SINGLE_OWNER
201 if (this->isAbandoned()) {
202 return nullptr;
203 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400204 return fGpu->createCompressedTexture(width, height, format, compression, budgeted, data->data(),
Brian Salomonbb8dde82019-06-27 10:52:13 -0400205 data->size());
206}
207
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400208sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400209 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400210 GrRenderable renderable,
211 int renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400212 GrMipMapped mipMapped,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400213 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400214 GrProtected isProtected) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400215 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400216 if (this->isAbandoned()) {
217 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500218 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400219
Greg Daniel6fa62e22019-08-07 15:52:37 -0400220 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400221 renderTargetSampleCnt, mipMapped)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400222 return nullptr;
223 }
224
Jim Van Verth1676cb92019-01-15 13:24:45 -0500225 // Compressed textures are read-only so they don't support re-use for scratch.
Brian Salomona90382f2019-09-17 09:01:56 -0400226 // TODO: Support GrMipMapped::kYes in scratch texture lookup here.
Brian Salomon14cb4132019-09-16 13:14:47 -0400227 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400228 sk_sp<GrTexture> tex = this->getExactScratch(
Brian Salomon14cb4132019-09-16 13:14:47 -0400229 desc, format, renderable, renderTargetSampleCnt, budgeted, mipMapped, isProtected);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500230 if (tex) {
231 return tex;
232 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400233 }
234
Brian Salomona90382f2019-09-17 09:01:56 -0400235 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, mipMapped, budgeted,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400236 isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500237}
238
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400239// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
240// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
241uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
242 static const int kMagicTol = 1024;
243
244 value = SkTMax(kMinScratchTextureSize, value);
245
246 if (SkIsPow2(value)) {
247 return value;
248 }
249
250 uint32_t ceilPow2 = GrNextPow2(value);
251 if (value <= kMagicTol) {
252 return ceilPow2;
253 }
254
255 uint32_t floorPow2 = ceilPow2 >> 1;
256 uint32_t mid = floorPow2 + (floorPow2 >> 1);
257
258 if (value <= mid) {
259 return mid;
260 }
261
262 return ceilPow2;
263}
264
Robert Phillips67d52cf2017-06-05 13:38:13 -0400265sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400266 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400267 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400268 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400269 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500270 ASSERT_SINGLE_OWNER
Brian Osman32342f02017-03-04 08:12:46 -0500271
Brian Osman32342f02017-03-04 08:12:46 -0500272 if (this->isAbandoned()) {
273 return nullptr;
274 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400275
Jim Van Verth1676cb92019-01-15 13:24:45 -0500276 // Currently we don't recycle compressed textures as scratch.
277 if (GrPixelConfigIsCompressed(desc.fConfig)) {
278 return nullptr;
279 }
280
Greg Daniel6fa62e22019-08-07 15:52:37 -0400281 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
282 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400283 return nullptr;
284 }
285
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400286 // bin by some multiple or power of 2 with a reasonable min
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400287 GrSurfaceDesc copyDesc(desc);
288 copyDesc.fWidth = MakeApprox(desc.fWidth);
289 copyDesc.fHeight = MakeApprox(desc.fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400290
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400291 if (auto tex = this->refScratchTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400292 GrMipMapped::kNo, isProtected)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400293 return tex;
294 }
295
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400296 return fGpu->createTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400297 GrMipMapped::kNo, SkBudgeted::kYes, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500298}
299
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400300sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400301 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400302 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400303 int renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400304 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400305 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500306 ASSERT_SINGLE_OWNER
307 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500308 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Greg Daniel6fa62e22019-08-07 15:52:37 -0400309 SkASSERT(fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
310 renderable, renderTargetSampleCnt, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500311
Brian Salomond17b4a62017-05-23 16:53:47 -0400312 // We could make initial clears work with scratch textures but it is a rare case so we just opt
313 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400314 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500315 GrScratchKey key;
Brian Salomon14cb4132019-09-16 13:14:47 -0400316 GrTexturePriv::ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable,
317 renderTargetSampleCnt, mipMapped, isProtected, &key);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400318 GrGpuResource* resource = fCache->findAndRefScratchResource(key);
Brian Osman32342f02017-03-04 08:12:46 -0500319 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400320 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500321 GrSurface* surface = static_cast<GrSurface*>(resource);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600322 if (GrRenderTarget* rt = surface->asRenderTarget()) {
323 rt->flagAsResolved(); // Scratch textures always start without dirty MSAA.
324 }
Robert Phillips67d52cf2017-06-05 13:38:13 -0400325 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500326 }
327 }
328
Brian Osman32342f02017-03-04 08:12:46 -0500329 return nullptr;
330}
331
Greg Daniel7ef28f32017-04-20 16:41:55 +0000332sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400333 GrColorType colorType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500334 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500335 GrWrapCacheable cacheable,
336 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500337 ASSERT_SINGLE_OWNER
338 if (this->isAbandoned()) {
339 return nullptr;
340 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400341 return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400342}
343
344sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400345 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400346 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500347 GrWrapOwnership ownership,
348 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400349 ASSERT_SINGLE_OWNER
350 if (this->isAbandoned()) {
351 return nullptr;
352 }
Robert Phillips0902c982019-07-16 07:47:56 -0400353 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500354}
355
356sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400357 const GrBackendRenderTarget& backendRT, GrColorType colorType)
Brian Osman32342f02017-03-04 08:12:46 -0500358{
359 ASSERT_SINGLE_OWNER
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400360 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500361}
362
Greg Danielb46add82019-01-02 14:51:29 -0500363sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
364 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
365 ASSERT_SINGLE_OWNER
366 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
367 vkInfo);
368
369}
370
Brian Osman32342f02017-03-04 08:12:46 -0500371void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
372 GrGpuResource* resource) {
373 ASSERT_SINGLE_OWNER
374 if (this->isAbandoned() || !resource) {
375 return;
376 }
377 resource->resourcePriv().setUniqueKey(key);
378}
379
Brian Salomond28a79d2017-10-16 13:01:07 -0400380sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500381 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400382 return this->isAbandoned() ? nullptr
383 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500384}
385
Brian Salomondbf70722019-02-07 11:31:24 -0500386sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
387 size_t size,
388 const void* data,
389 const GrUniqueKey& key) {
390 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400391 return buffer;
Chris Dalton5d2de082017-12-19 10:40:23 -0700392 }
Brian Salomondbf70722019-02-07 11:31:24 -0500393 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500394 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500395 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700396 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
Chris Dalton5d2de082017-12-19 10:40:23 -0700397 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500398 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700399 }
400 return nullptr;
401}
402
Brian Salomondbf70722019-02-07 11:31:24 -0500403sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
404 int patternSize,
405 int reps,
406 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500407 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700408 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
409
Brian Salomondbf70722019-02-07 11:31:24 -0500410 sk_sp<GrGpuBuffer> buffer(
411 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700412 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700413 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700414 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400415 uint16_t* data = (uint16_t*) buffer->map();
416 SkAutoTArray<uint16_t> temp;
417 if (!data) {
418 temp.reset(reps * patternSize);
419 data = temp.get();
420 }
bsalomoned0bcad2015-05-04 10:36:42 -0700421 for (int i = 0; i < reps; ++i) {
422 int baseIdx = i * patternSize;
423 uint16_t baseVert = (uint16_t)(i * vertCount);
424 for (int j = 0; j < patternSize; ++j) {
425 data[baseIdx+j] = baseVert + pattern[j];
426 }
427 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400428 if (temp.get()) {
429 if (!buffer->updateData(data, bufferSize)) {
430 return nullptr;
431 }
432 } else {
433 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700434 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500435 if (key) {
436 SkASSERT(key->isValid());
437 this->assignUniqueKeyToResource(*key, buffer.get());
438 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400439 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700440}
441
Brian Salomon34169692017-08-28 15:32:01 -0400442static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
443
Brian Salomondbf70722019-02-07 11:31:24 -0500444sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700445 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000446 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500447 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700448}
449
Brian Salomon763abf02018-05-01 18:49:38 +0000450int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400451
Robert Phillips67d52cf2017-06-05 13:38:13 -0400452sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400453 if (this->isAbandoned()) {
454 return nullptr;
455 }
456
bsalomon706f08f2015-05-22 07:35:58 -0700457 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700458 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700459}
460
Brian Salomondbf70722019-02-07 11:31:24 -0500461sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
462 GrAccessPattern accessPattern,
463 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700464 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700465 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700466 }
cdaltond37fe762016-04-21 07:41:50 -0700467 if (kDynamic_GrAccessPattern != accessPattern) {
468 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
469 }
cdaltond37fe762016-04-21 07:41:50 -0700470 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400471 static const size_t MIN_SIZE = 1 << 12;
472 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700473
cdaltond37fe762016-04-21 07:41:50 -0700474 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500475 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
476 auto buffer =
477 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400478 key)));
cdaltond37fe762016-04-21 07:41:50 -0700479 if (!buffer) {
480 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
481 if (!buffer) {
482 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700483 }
484 }
cdaltond37fe762016-04-21 07:41:50 -0700485 if (data) {
486 buffer->updateData(data, size);
487 }
488 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800489}
490
Chris Daltoneffee202019-07-01 22:28:03 -0600491bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700492 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600493 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
494 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400495 return true;
egdanielec00d942015-09-14 12:56:10 -0700496 }
497
498 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
499 GrUniqueKey sbKey;
500
501 int width = rt->width();
502 int height = rt->height();
503#if 0
504 if (this->caps()->oversizedStencilSupport()) {
505 width = SkNextPow2(width);
506 height = SkNextPow2(height);
507 }
508#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600509 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
510 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400511 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700512 if (!stencil) {
513 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600514 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
515 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400516 if (!stencil) {
517 return false;
egdanielec00d942015-09-14 12:56:10 -0700518 }
Robert Phillips01a91282018-07-26 08:03:04 -0400519 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700520 }
Greg Danielcfa39352018-10-05 12:01:59 -0400521 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700522 }
Chris Dalton215ff332019-07-02 09:38:22 -0600523
524 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
525 return stencil->numSamples() >= minStencilSampleCount;
526 }
527 return false;
egdanielec00d942015-09-14 12:56:10 -0700528}
529
bungeman6bd52842016-10-27 09:30:08 -0700530sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400531 const GrBackendTexture& tex, int sampleCnt, GrColorType colorType)
bungeman6bd52842016-10-27 09:30:08 -0700532{
ericrkf7b8b8a2016-02-24 14:49:51 -0800533 if (this->isAbandoned()) {
534 return nullptr;
535 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400536 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800537}
Greg Danield85f97d2017-03-07 13:37:21 -0500538
Greg Daniela5cb7812017-06-16 09:45:32 -0400539sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
540 return fGpu->makeSemaphore(isOwned);
541}
542
543sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500544 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400545 GrWrapOwnership ownership) {
546 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500547 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
548 wrapType,
549 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500550}