blob: e048b3b8b10b783f9296914078b1717a0cd6f3d6 [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.
47static bool prepare_level(const GrMipLevel& inLevel, size_t bpp, int w, int h, bool rowBytesSupport,
Brian Salomona3e29962019-07-16 11:52:08 -040048 bool mustInitializeAllLevels, GrMipLevel* outLevel,
49 std::unique_ptr<char[]>* data) {
50 size_t minRB = w * bpp;
Brian Salomona3ffaab2019-07-09 12:26:46 -040051 if (!inLevel.fPixels) {
Brian Salomona3e29962019-07-16 11:52:08 -040052 if (mustInitializeAllLevels) {
53 data->reset(new char[minRB * h]());
54 outLevel->fPixels = data->get();
55 outLevel->fRowBytes = minRB;
56 } else {
57 outLevel->fPixels = nullptr;
58 outLevel->fRowBytes = 0;
59 }
Brian Salomona3ffaab2019-07-09 12:26:46 -040060 return true;
61 }
Brian Salomona3ffaab2019-07-09 12:26:46 -040062 size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB;
63 if (actualRB < minRB) {
64 return false;
65 }
66 if (actualRB == minRB || rowBytesSupport) {
67 outLevel->fRowBytes = actualRB;
68 outLevel->fPixels = inLevel.fPixels;
69 } else {
70 data->reset(new char[minRB * h]);
71 outLevel->fPixels = data->get();
72 outLevel->fRowBytes = minRB;
73 SkRectMemcpy(data->get(), outLevel->fRowBytes, inLevel.fPixels, inLevel.fRowBytes, minRB,
74 h);
75 }
76 return true;
77}
78
Brian Salomonf2c2ba92019-07-17 09:59:59 -040079sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040080 GrRenderable renderable,
81 int renderTargetSampleCnt, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040082 GrProtected isProtected,
Brian Osman2b23c4b2018-06-01 12:25:08 -040083 const GrMipLevel texels[], int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050084 ASSERT_SINGLE_OWNER
85
Robert Phillips7f1b4f82017-11-28 07:38:39 -050086 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040087
Brian Osman32342f02017-03-04 08:12:46 -050088 if (this->isAbandoned()) {
89 return nullptr;
90 }
Robert Phillips1119dc32017-04-11 12:54:57 -040091
Brian Salomonbdecacf2018-02-02 20:32:49 -050092 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomon27b4d8d2019-07-22 14:23:45 -040093 if (!fCaps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050094 return nullptr;
95 }
Brian Salomona3e29962019-07-16 11:52:08 -040096 bool mustInitializeAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
97 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
Brian Salomon1047a492019-07-02 12:25:21 -040098 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -040099 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon1047a492019-07-02 12:25:21 -0400100 if (mipLevelCount > 0 && texels) {
101 tmpTexels.reset(mipLevelCount);
102 tmpDatas.reset(mipLevelCount);
103 int w = desc.fWidth;
104 int h = desc.fHeight;
105 size_t bpp = GrBytesPerPixel(desc.fConfig);
106 for (int i = 0; i < mipLevelCount; ++i) {
Brian Salomona3e29962019-07-16 11:52:08 -0400107 if (!prepare_level(texels[i], bpp, w, h, rowBytesSupport, mustInitializeAllLevels,
Brian Salomona3ffaab2019-07-09 12:26:46 -0400108 &tmpTexels[i], &tmpDatas[i])) {
109 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -0400110 }
111 w = std::max(w / 2, 1);
112 h = std::max(h / 2, 1);
113 }
114 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400115 return fGpu->createTexture(desc, renderable, renderTargetSampleCnt, budgeted, isProtected,
116 tmpTexels.get(), mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -0500117}
118
Robert Phillips45fdae12017-04-17 12:57:27 -0400119sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400120 GrRenderable renderable,
121 int renderTargetSampleCnt,
122 SkBudgeted budgeted,
123 GrProtected isProtected,
124 Flags flags) {
125 sk_sp<GrTexture> tex(
126 this->refScratchTexture(desc, renderable, renderTargetSampleCnt, isProtected, flags));
Robert Phillips45fdae12017-04-17 12:57:27 -0400127 if (tex && SkBudgeted::kNo == budgeted) {
128 tex->resourcePriv().makeUnbudgeted();
129 }
130
131 return tex;
132}
133
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500134sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400135 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400136 int renderTargetSampleCnt,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500137 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500138 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400139 GrProtected isProtected,
Chris Daltond004e0b2018-09-27 09:28:03 -0600140 const GrMipLevel& mipLevel,
141 Flags flags) {
Robert Phillips774831a2017-04-20 10:19:33 -0400142 ASSERT_SINGLE_OWNER
143
144 if (this->isAbandoned()) {
145 return nullptr;
146 }
147
Robert Phillips45fdae12017-04-17 12:57:27 -0400148 if (!mipLevel.fPixels) {
149 return nullptr;
150 }
151
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400152 if (!fCaps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400153 return nullptr;
154 }
155
Robert Phillips45fdae12017-04-17 12:57:27 -0400156 GrContext* context = fGpu->getContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500157 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips45fdae12017-04-17 12:57:27 -0400158
Brian Salomona3e29962019-07-16 11:52:08 -0400159 bool mustInitialize = this->caps()->createTextureMustSpecifyAllLevels();
160 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
161
Brian Salomona3ffaab2019-07-09 12:26:46 -0400162 size_t bpp = GrBytesPerPixel(desc.fConfig);
163 std::unique_ptr<char[]> tmpData;
164 GrMipLevel tmpLevel;
Brian Salomona3e29962019-07-16 11:52:08 -0400165 if (!prepare_level(mipLevel, bpp, desc.fWidth, desc.fHeight, rowBytesSupport, mustInitialize,
166 &tmpLevel, &tmpData)) {
Brian Salomona3ffaab2019-07-09 12:26:46 -0400167 return nullptr;
168 }
169
Brian Salomona495d6d2019-07-13 17:48:58 -0400170 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
Brian Salomone8a766b2019-07-19 14:24:36 -0400171 sk_sp<GrTexture> tex =
172 (SkBackingFit::kApprox == fit)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400173 ? this->createApproxTexture(desc, renderable, renderTargetSampleCnt,
174 isProtected, flags)
175 : this->createTexture(desc, renderable, renderTargetSampleCnt, budgeted,
176 isProtected, flags);
Brian Salomona495d6d2019-07-13 17:48:58 -0400177 if (!tex) {
178 return nullptr;
Robert Phillips45fdae12017-04-17 12:57:27 -0400179 }
Brian Salomona495d6d2019-07-13 17:48:58 -0400180
181 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(tex, kTopLeft_GrSurfaceOrigin);
182 if (!proxy) {
183 return nullptr;
184 }
185 // Here we don't really know the alpha type of the data we want to upload. All we really
186 // care about is that it is not converted. So we use the same alpha type for the data
187 // and the surface context.
188 static constexpr auto kAlphaType = kPremul_SkAlphaType;
189 sk_sp<GrSurfaceContext> sContext =
190 context->priv().makeWrappedSurfaceContext(std::move(proxy), colorType, kAlphaType);
191 if (!sContext) {
192 return nullptr;
193 }
194 GrPixelInfo srcInfo(colorType, kAlphaType, nullptr, desc.fWidth, desc.fHeight);
195 SkAssertResult(sContext->writePixels(srcInfo, tmpLevel.fPixels, tmpLevel.fRowBytes, {0, 0}));
196 return tex;
Robert Phillips45fdae12017-04-17 12:57:27 -0400197}
198
Brian Salomonbb8dde82019-06-27 10:52:13 -0400199sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
200 SkImage::CompressionType compression,
201 SkBudgeted budgeted, SkData* data) {
202 ASSERT_SINGLE_OWNER
203 if (this->isAbandoned()) {
204 return nullptr;
205 }
206 return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(),
207 data->size());
208}
209
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400210sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400211 GrRenderable renderable,
212 int renderTargetSampleCnt,
213 SkBudgeted budgeted,
214 GrProtected isProtected,
215 Flags flags) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400216 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400217 if (this->isAbandoned()) {
218 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500219 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400220
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400221 if (!fCaps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, GrMipMapped::kNo)) {
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.
226 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400227 sk_sp<GrTexture> tex = this->getExactScratch(desc, renderable, renderTargetSampleCnt,
228 budgeted, isProtected, flags);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500229 if (tex) {
230 return tex;
231 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400232 }
233
Brian Salomona3e29962019-07-16 11:52:08 -0400234 if (fCaps->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000235 size_t rowBytes = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
236 size_t size = rowBytes * desc.fHeight;
237 std::unique_ptr<char[]> zeros(new char[size]());
238 GrMipLevel level;
239 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400240 level.fPixels = zeros.get();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400241 return fGpu->createTexture(desc, renderable, renderTargetSampleCnt, budgeted, isProtected,
242 &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400243 }
244
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400245 return fGpu->createTexture(desc, renderable, renderTargetSampleCnt, budgeted, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500246}
247
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400248// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
249// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
250uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
251 static const int kMagicTol = 1024;
252
253 value = SkTMax(kMinScratchTextureSize, value);
254
255 if (SkIsPow2(value)) {
256 return value;
257 }
258
259 uint32_t ceilPow2 = GrNextPow2(value);
260 if (value <= kMagicTol) {
261 return ceilPow2;
262 }
263
264 uint32_t floorPow2 = ceilPow2 >> 1;
265 uint32_t mid = floorPow2 + (floorPow2 >> 1);
266
267 if (value <= mid) {
268 return mid;
269 }
270
271 return ceilPow2;
272}
273
Robert Phillips67d52cf2017-06-05 13:38:13 -0400274sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomone8a766b2019-07-19 14:24:36 -0400275 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400276 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400277 GrProtected isProtected, Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500278 ASSERT_SINGLE_OWNER
Chris Daltond004e0b2018-09-27 09:28:03 -0600279 SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500280
Brian Osman32342f02017-03-04 08:12:46 -0500281 if (this->isAbandoned()) {
282 return nullptr;
283 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400284
Jim Van Verth1676cb92019-01-15 13:24:45 -0500285 // Currently we don't recycle compressed textures as scratch.
286 if (GrPixelConfigIsCompressed(desc.fConfig)) {
287 return nullptr;
288 }
289
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400290 if (!fCaps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400291 return nullptr;
292 }
293
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400294 if (auto tex = this->refScratchTexture(desc, renderable, renderTargetSampleCnt, isProtected,
295 flags)) {
Greg Daniel6b60b6e2017-09-26 16:37:12 -0400296 return tex;
297 }
298
Greg Daniel29bf84f2017-09-25 12:25:12 -0400299 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
300
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400301 // bin by some multiple or power of 2 with a reasonable min
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400302 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400303 GrSurfaceDesc* wdesc = copyDesc.writable();
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400304 wdesc->fWidth = MakeApprox(wdesc->fWidth);
305 wdesc->fHeight = MakeApprox(wdesc->fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400306 }
307
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400308 if (auto tex = this->refScratchTexture(*copyDesc, renderable, renderTargetSampleCnt,
309 isProtected, flags)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400310 return tex;
311 }
312
Brian Salomona3e29962019-07-16 11:52:08 -0400313 if (this->caps()->createTextureMustSpecifyAllLevels()) {
Brian Salomonc7dced52019-07-18 15:02:01 +0000314 size_t rowBytes = GrBytesPerPixel(copyDesc->fConfig) * copyDesc->fWidth;
315 size_t size = rowBytes * copyDesc->fHeight;
316 std::unique_ptr<char[]> zeros(new char[size]());
317 GrMipLevel level;
318 level.fRowBytes = rowBytes;
Brian Salomona3e29962019-07-16 11:52:08 -0400319 level.fPixels = zeros.get();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400320 return fGpu->createTexture(*copyDesc, renderable, renderTargetSampleCnt, SkBudgeted::kYes,
321 isProtected, &level, 1);
Brian Salomona3e29962019-07-16 11:52:08 -0400322 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400323 return fGpu->createTexture(*copyDesc, renderable, renderTargetSampleCnt, SkBudgeted::kYes,
324 isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500325}
326
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400327sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomone8a766b2019-07-19 14:24:36 -0400328 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400329 int renderTargetSampleCnt,
330 GrProtected isProtected,
331 Flags flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500332 ASSERT_SINGLE_OWNER
333 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500334 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400335 SkASSERT(fCaps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500336
Brian Salomond17b4a62017-05-23 16:53:47 -0400337 // We could make initial clears work with scratch textures but it is a rare case so we just opt
338 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400339 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500340 GrScratchKey key;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400341 GrTexturePriv::ComputeScratchKey(desc, renderable, renderTargetSampleCnt, &key);
Chris Daltond004e0b2018-09-27 09:28:03 -0600342 auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
343 if (Flags::kNoPendingIO & flags) {
344 scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400345 } else if (renderable == GrRenderable::kNo) {
Brian Osman32342f02017-03-04 08:12:46 -0500346 // If it is not a render target then it will most likely be populated by
347 // writePixels() which will trigger a flush if the texture has pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600348 scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
Brian Osman32342f02017-03-04 08:12:46 -0500349 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400350 GrGpuResource* resource = fCache->findAndRefScratchResource(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400351 key, GrSurface::WorstCaseSize(desc, renderable, renderTargetSampleCnt),
352 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500353 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400354 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500355 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400356 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500357 }
358 }
359
Brian Osman32342f02017-03-04 08:12:46 -0500360 return nullptr;
361}
362
Greg Daniel7ef28f32017-04-20 16:41:55 +0000363sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Greg Daniel2268ad22018-11-15 09:27:38 -0500364 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500365 GrWrapCacheable cacheable,
366 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500367 ASSERT_SINGLE_OWNER
368 if (this->isAbandoned()) {
369 return nullptr;
370 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000371 return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400372}
373
374sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400375 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400376 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500377 GrWrapOwnership ownership,
378 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400379 ASSERT_SINGLE_OWNER
380 if (this->isAbandoned()) {
381 return nullptr;
382 }
Robert Phillips0902c982019-07-16 07:47:56 -0400383 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500384}
385
386sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000387 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500388{
389 ASSERT_SINGLE_OWNER
Robert Phillipsdd399802019-07-18 12:28:00 +0000390 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500391}
392
Greg Danielb46add82019-01-02 14:51:29 -0500393sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
394 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
395 ASSERT_SINGLE_OWNER
396 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
397 vkInfo);
398
399}
400
Brian Osman32342f02017-03-04 08:12:46 -0500401void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
402 GrGpuResource* resource) {
403 ASSERT_SINGLE_OWNER
404 if (this->isAbandoned() || !resource) {
405 return;
406 }
407 resource->resourcePriv().setUniqueKey(key);
408}
409
Brian Salomond28a79d2017-10-16 13:01:07 -0400410sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500411 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400412 return this->isAbandoned() ? nullptr
413 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500414}
415
Brian Salomondbf70722019-02-07 11:31:24 -0500416sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
417 size_t size,
418 const void* data,
419 const GrUniqueKey& key) {
420 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400421 return std::move(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700422 }
Brian Salomondbf70722019-02-07 11:31:24 -0500423 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500424 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500425 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700426 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
427 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
428 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500429 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700430 }
431 return nullptr;
432}
433
Brian Salomondbf70722019-02-07 11:31:24 -0500434sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
435 int patternSize,
436 int reps,
437 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500438 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700439 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
440
Brian Salomon09d994e2016-12-21 11:14:46 -0500441 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
Brian Salomondbf70722019-02-07 11:31:24 -0500442 sk_sp<GrGpuBuffer> buffer(
443 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700444 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700445 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700446 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400447 uint16_t* data = (uint16_t*) buffer->map();
448 SkAutoTArray<uint16_t> temp;
449 if (!data) {
450 temp.reset(reps * patternSize);
451 data = temp.get();
452 }
bsalomoned0bcad2015-05-04 10:36:42 -0700453 for (int i = 0; i < reps; ++i) {
454 int baseIdx = i * patternSize;
455 uint16_t baseVert = (uint16_t)(i * vertCount);
456 for (int j = 0; j < patternSize; ++j) {
457 data[baseIdx+j] = baseVert + pattern[j];
458 }
459 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400460 if (temp.get()) {
461 if (!buffer->updateData(data, bufferSize)) {
462 return nullptr;
463 }
464 } else {
465 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700466 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500467 if (key) {
468 SkASSERT(key->isValid());
469 this->assignUniqueKeyToResource(*key, buffer.get());
470 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400471 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700472}
473
Brian Salomon34169692017-08-28 15:32:01 -0400474static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
475
Brian Salomondbf70722019-02-07 11:31:24 -0500476sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700477 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000478 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500479 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700480}
481
Brian Salomon763abf02018-05-01 18:49:38 +0000482int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400483
Robert Phillips67d52cf2017-06-05 13:38:13 -0400484sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400485 if (this->isAbandoned()) {
486 return nullptr;
487 }
488
bsalomon706f08f2015-05-22 07:35:58 -0700489 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700490 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700491}
492
Brian Salomondbf70722019-02-07 11:31:24 -0500493sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
494 GrAccessPattern accessPattern,
495 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700496 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700497 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700498 }
cdaltond37fe762016-04-21 07:41:50 -0700499 if (kDynamic_GrAccessPattern != accessPattern) {
500 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
501 }
cdaltond37fe762016-04-21 07:41:50 -0700502 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400503 static const size_t MIN_SIZE = 1 << 12;
504 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700505
cdaltond37fe762016-04-21 07:41:50 -0700506 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500507 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
508 auto buffer =
509 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
510 key, allocSize, GrResourceCache::ScratchFlags::kNone)));
cdaltond37fe762016-04-21 07:41:50 -0700511 if (!buffer) {
512 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
513 if (!buffer) {
514 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700515 }
516 }
cdaltond37fe762016-04-21 07:41:50 -0700517 if (data) {
518 buffer->updateData(data, size);
519 }
520 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800521}
522
Chris Daltoneffee202019-07-01 22:28:03 -0600523bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700524 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600525 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
526 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400527 return true;
egdanielec00d942015-09-14 12:56:10 -0700528 }
529
530 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
531 GrUniqueKey sbKey;
532
533 int width = rt->width();
534 int height = rt->height();
535#if 0
536 if (this->caps()->oversizedStencilSupport()) {
537 width = SkNextPow2(width);
538 height = SkNextPow2(height);
539 }
540#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600541 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
542 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400543 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700544 if (!stencil) {
545 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600546 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
547 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400548 if (!stencil) {
549 return false;
egdanielec00d942015-09-14 12:56:10 -0700550 }
Robert Phillips01a91282018-07-26 08:03:04 -0400551 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700552 }
Greg Danielcfa39352018-10-05 12:01:59 -0400553 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700554 }
Chris Dalton215ff332019-07-02 09:38:22 -0600555
556 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
557 return stencil->numSamples() >= minStencilSampleCount;
558 }
559 return false;
egdanielec00d942015-09-14 12:56:10 -0700560}
561
bungeman6bd52842016-10-27 09:30:08 -0700562sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000563 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700564{
ericrkf7b8b8a2016-02-24 14:49:51 -0800565 if (this->isAbandoned()) {
566 return nullptr;
567 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000568 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800569}
Greg Danield85f97d2017-03-07 13:37:21 -0500570
Greg Daniela5cb7812017-06-16 09:45:32 -0400571sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
572 return fGpu->makeSemaphore(isOwned);
573}
574
575sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500576 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400577 GrWrapOwnership ownership) {
578 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500579 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
580 wrapType,
581 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500582}