blob: c2e62f9e7bd5592b9ead8d7e7c77b704cc378971 [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 Salomonf2c2ba92019-07-17 09:59:59 -040045sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040046 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -040047 GrColorType colorType,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040048 GrRenderable renderable,
Brian Salomon2af3e702019-08-11 19:10:31 -040049 int renderTargetSampleCnt,
50 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040051 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -040052 const GrMipLevel texels[],
53 int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050054 ASSERT_SINGLE_OWNER
55
Robert Phillips7f1b4f82017-11-28 07:38:39 -050056 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040057
Brian Osman32342f02017-03-04 08:12:46 -050058 if (this->isAbandoned()) {
59 return nullptr;
60 }
Robert Phillips1119dc32017-04-11 12:54:57 -040061
Brian Salomonbdecacf2018-02-02 20:32:49 -050062 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -040063 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
64 renderTargetSampleCnt, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050065 return nullptr;
66 }
Brian Salomon8660eb02019-09-20 13:04:13 -040067 // Current rule is that you can provide no level data, just the base, or all the levels.
68 bool hasPixels = mipLevelCount && texels[0].fPixels;
69 auto scratch = this->getExactScratch(desc, format, renderable, renderTargetSampleCnt, budgeted,
70 mipMapped, isProtected);
71 if (scratch) {
72 if (!hasPixels) {
73 return scratch;
74 }
75 return this->writePixels(std::move(scratch), colorType, {desc.fWidth, desc.fHeight}, texels,
76 mipLevelCount);
Brian Salomona90382f2019-09-17 09:01:56 -040077 }
Brian Salomon1047a492019-07-02 12:25:21 -040078 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -040079 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon8660eb02019-09-20 13:04:13 -040080 GrColorType tempColorType = GrColorType::kUnknown;
81 if (hasPixels) {
82 tempColorType = this->prepareLevels(format, colorType, {desc.fWidth, desc.fHeight}, texels,
83 mipLevelCount, &tmpTexels, &tmpDatas);
84 if (tempColorType == GrColorType::kUnknown) {
85 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -040086 }
87 }
Brian Salomon4eb38b72019-08-05 12:58:39 -040088 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
Brian Salomon8660eb02019-09-20 13:04:13 -040089 isProtected, colorType, tempColorType, tmpTexels.get(),
90 mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -050091}
92
Robert Phillips45fdae12017-04-17 12:57:27 -040093sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040094 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040095 GrRenderable renderable,
96 int renderTargetSampleCnt,
97 SkBudgeted budgeted,
Brian Salomon14cb4132019-09-16 13:14:47 -040098 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -040099 GrProtected isProtected) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400100 sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400101 mipMapped, isProtected));
Robert Phillips45fdae12017-04-17 12:57:27 -0400102 if (tex && SkBudgeted::kNo == budgeted) {
103 tex->resourcePriv().makeUnbudgeted();
104 }
105
106 return tex;
107}
108
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400110 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -0400111 GrColorType colorType,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400112 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400113 int renderTargetSampleCnt,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500114 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500115 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400116 GrProtected isProtected,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400117 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400118 ASSERT_SINGLE_OWNER
119
Robert Phillips45fdae12017-04-17 12:57:27 -0400120 if (!mipLevel.fPixels) {
121 return nullptr;
122 }
123
Brian Salomona90382f2019-09-17 09:01:56 -0400124 if (SkBackingFit::kApprox == fit) {
Brian Salomon8660eb02019-09-20 13:04:13 -0400125 if (this->isAbandoned()) {
126 return nullptr;
127 }
128 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
129 renderable, renderTargetSampleCnt, GrMipMapped::kNo)) {
130 return nullptr;
131 }
132
133 auto tex = this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
134 isProtected);
Brian Salomona90382f2019-09-17 09:01:56 -0400135 if (!tex) {
136 return nullptr;
137 }
Brian Salomon8660eb02019-09-20 13:04:13 -0400138 return this->writePixels(std::move(tex), colorType, {desc.fWidth, desc.fHeight}, &mipLevel,
139 1);
Brian Salomona90382f2019-09-17 09:01:56 -0400140 } else {
141 return this->createTexture(desc, format, colorType, renderable, renderTargetSampleCnt,
142 budgeted, isProtected, &mipLevel, 1);
Brian Salomona3ffaab2019-07-09 12:26:46 -0400143 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400144}
145
Brian Salomonbb8dde82019-06-27 10:52:13 -0400146sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400147 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400148 SkImage::CompressionType compression,
149 SkBudgeted budgeted, SkData* data) {
150 ASSERT_SINGLE_OWNER
151 if (this->isAbandoned()) {
152 return nullptr;
153 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400154 return fGpu->createCompressedTexture(width, height, format, compression, budgeted, data->data(),
Brian Salomonbb8dde82019-06-27 10:52:13 -0400155 data->size());
156}
157
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400158sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400159 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400160 GrRenderable renderable,
161 int renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400162 GrMipMapped mipMapped,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400163 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400164 GrProtected isProtected) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400165 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400166 if (this->isAbandoned()) {
167 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500168 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400169
Greg Daniel6fa62e22019-08-07 15:52:37 -0400170 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400171 renderTargetSampleCnt, mipMapped)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400172 return nullptr;
173 }
174
Jim Van Verth1676cb92019-01-15 13:24:45 -0500175 // Compressed textures are read-only so they don't support re-use for scratch.
Brian Salomona90382f2019-09-17 09:01:56 -0400176 // TODO: Support GrMipMapped::kYes in scratch texture lookup here.
Brian Salomon14cb4132019-09-16 13:14:47 -0400177 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400178 sk_sp<GrTexture> tex = this->getExactScratch(
Brian Salomon14cb4132019-09-16 13:14:47 -0400179 desc, format, renderable, renderTargetSampleCnt, budgeted, mipMapped, isProtected);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500180 if (tex) {
181 return tex;
182 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400183 }
184
Brian Salomona90382f2019-09-17 09:01:56 -0400185 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, mipMapped, budgeted,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400186 isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500187}
188
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400189// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
190// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
191uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
192 static const int kMagicTol = 1024;
193
194 value = SkTMax(kMinScratchTextureSize, value);
195
196 if (SkIsPow2(value)) {
197 return value;
198 }
199
200 uint32_t ceilPow2 = GrNextPow2(value);
201 if (value <= kMagicTol) {
202 return ceilPow2;
203 }
204
205 uint32_t floorPow2 = ceilPow2 >> 1;
206 uint32_t mid = floorPow2 + (floorPow2 >> 1);
207
208 if (value <= mid) {
209 return mid;
210 }
211
212 return ceilPow2;
213}
214
Robert Phillips67d52cf2017-06-05 13:38:13 -0400215sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400216 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400217 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400218 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400219 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500220 ASSERT_SINGLE_OWNER
Brian Osman32342f02017-03-04 08:12:46 -0500221
Brian Osman32342f02017-03-04 08:12:46 -0500222 if (this->isAbandoned()) {
223 return nullptr;
224 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400225
Jim Van Verth1676cb92019-01-15 13:24:45 -0500226 // Currently we don't recycle compressed textures as scratch.
227 if (GrPixelConfigIsCompressed(desc.fConfig)) {
228 return nullptr;
229 }
230
Greg Daniel6fa62e22019-08-07 15:52:37 -0400231 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
232 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400233 return nullptr;
234 }
235
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400236 // bin by some multiple or power of 2 with a reasonable min
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400237 GrSurfaceDesc copyDesc(desc);
238 copyDesc.fWidth = MakeApprox(desc.fWidth);
239 copyDesc.fHeight = MakeApprox(desc.fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400240
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400241 if (auto tex = this->refScratchTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400242 GrMipMapped::kNo, isProtected)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400243 return tex;
244 }
245
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400246 return fGpu->createTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400247 GrMipMapped::kNo, SkBudgeted::kYes, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500248}
249
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400250sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400251 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400252 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400253 int renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400254 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400255 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500256 ASSERT_SINGLE_OWNER
257 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500258 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Greg Daniel6fa62e22019-08-07 15:52:37 -0400259 SkASSERT(fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
260 renderable, renderTargetSampleCnt, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500261
Brian Salomond17b4a62017-05-23 16:53:47 -0400262 // We could make initial clears work with scratch textures but it is a rare case so we just opt
263 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400264 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500265 GrScratchKey key;
Brian Salomon14cb4132019-09-16 13:14:47 -0400266 GrTexturePriv::ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable,
267 renderTargetSampleCnt, mipMapped, isProtected, &key);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400268 GrGpuResource* resource = fCache->findAndRefScratchResource(key);
Brian Osman32342f02017-03-04 08:12:46 -0500269 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400270 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500271 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400272 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500273 }
274 }
275
Brian Osman32342f02017-03-04 08:12:46 -0500276 return nullptr;
277}
278
Greg Daniel7ef28f32017-04-20 16:41:55 +0000279sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400280 GrColorType colorType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500281 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500282 GrWrapCacheable cacheable,
283 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500284 ASSERT_SINGLE_OWNER
285 if (this->isAbandoned()) {
286 return nullptr;
287 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400288 return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400289}
290
291sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400292 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400293 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500294 GrWrapOwnership ownership,
295 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400296 ASSERT_SINGLE_OWNER
297 if (this->isAbandoned()) {
298 return nullptr;
299 }
Robert Phillips0902c982019-07-16 07:47:56 -0400300 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500301}
302
303sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400304 const GrBackendRenderTarget& backendRT, GrColorType colorType)
Brian Osman32342f02017-03-04 08:12:46 -0500305{
306 ASSERT_SINGLE_OWNER
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400307 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500308}
309
Greg Danielb46add82019-01-02 14:51:29 -0500310sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
311 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
312 ASSERT_SINGLE_OWNER
313 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
314 vkInfo);
315
316}
317
Brian Osman32342f02017-03-04 08:12:46 -0500318void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
319 GrGpuResource* resource) {
320 ASSERT_SINGLE_OWNER
321 if (this->isAbandoned() || !resource) {
322 return;
323 }
324 resource->resourcePriv().setUniqueKey(key);
325}
326
Brian Salomond28a79d2017-10-16 13:01:07 -0400327sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500328 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400329 return this->isAbandoned() ? nullptr
330 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500331}
332
Brian Salomondbf70722019-02-07 11:31:24 -0500333sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
334 size_t size,
335 const void* data,
336 const GrUniqueKey& key) {
337 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400338 return buffer;
Chris Dalton5d2de082017-12-19 10:40:23 -0700339 }
Brian Salomondbf70722019-02-07 11:31:24 -0500340 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500341 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500342 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700343 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
Chris Dalton5d2de082017-12-19 10:40:23 -0700344 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500345 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700346 }
347 return nullptr;
348}
349
Brian Salomondbf70722019-02-07 11:31:24 -0500350sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
351 int patternSize,
352 int reps,
353 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500354 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700355 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
356
Brian Salomondbf70722019-02-07 11:31:24 -0500357 sk_sp<GrGpuBuffer> buffer(
358 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700359 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700360 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700361 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400362 uint16_t* data = (uint16_t*) buffer->map();
363 SkAutoTArray<uint16_t> temp;
364 if (!data) {
365 temp.reset(reps * patternSize);
366 data = temp.get();
367 }
bsalomoned0bcad2015-05-04 10:36:42 -0700368 for (int i = 0; i < reps; ++i) {
369 int baseIdx = i * patternSize;
370 uint16_t baseVert = (uint16_t)(i * vertCount);
371 for (int j = 0; j < patternSize; ++j) {
372 data[baseIdx+j] = baseVert + pattern[j];
373 }
374 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400375 if (temp.get()) {
376 if (!buffer->updateData(data, bufferSize)) {
377 return nullptr;
378 }
379 } else {
380 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700381 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500382 if (key) {
383 SkASSERT(key->isValid());
384 this->assignUniqueKeyToResource(*key, buffer.get());
385 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400386 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700387}
388
Brian Salomon34169692017-08-28 15:32:01 -0400389static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
390
Brian Salomondbf70722019-02-07 11:31:24 -0500391sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700392 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000393 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500394 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700395}
396
Brian Salomon763abf02018-05-01 18:49:38 +0000397int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400398
Robert Phillips67d52cf2017-06-05 13:38:13 -0400399sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400400 if (this->isAbandoned()) {
401 return nullptr;
402 }
403
bsalomon706f08f2015-05-22 07:35:58 -0700404 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700405 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700406}
407
Brian Salomondbf70722019-02-07 11:31:24 -0500408sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
409 GrAccessPattern accessPattern,
410 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700411 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700412 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700413 }
cdaltond37fe762016-04-21 07:41:50 -0700414 if (kDynamic_GrAccessPattern != accessPattern) {
415 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
416 }
cdaltond37fe762016-04-21 07:41:50 -0700417 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400418 static const size_t MIN_SIZE = 1 << 12;
419 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700420
cdaltond37fe762016-04-21 07:41:50 -0700421 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500422 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
423 auto buffer =
424 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400425 key)));
cdaltond37fe762016-04-21 07:41:50 -0700426 if (!buffer) {
427 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
428 if (!buffer) {
429 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700430 }
431 }
cdaltond37fe762016-04-21 07:41:50 -0700432 if (data) {
433 buffer->updateData(data, size);
434 }
435 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800436}
437
Chris Daltoneffee202019-07-01 22:28:03 -0600438bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700439 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600440 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
441 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400442 return true;
egdanielec00d942015-09-14 12:56:10 -0700443 }
444
445 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
446 GrUniqueKey sbKey;
447
448 int width = rt->width();
449 int height = rt->height();
450#if 0
451 if (this->caps()->oversizedStencilSupport()) {
452 width = SkNextPow2(width);
453 height = SkNextPow2(height);
454 }
455#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600456 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
457 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400458 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700459 if (!stencil) {
460 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600461 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
462 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400463 if (!stencil) {
464 return false;
egdanielec00d942015-09-14 12:56:10 -0700465 }
Robert Phillips01a91282018-07-26 08:03:04 -0400466 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700467 }
Greg Danielcfa39352018-10-05 12:01:59 -0400468 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700469 }
Chris Dalton215ff332019-07-02 09:38:22 -0600470
471 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
472 return stencil->numSamples() >= minStencilSampleCount;
473 }
474 return false;
egdanielec00d942015-09-14 12:56:10 -0700475}
476
bungeman6bd52842016-10-27 09:30:08 -0700477sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400478 const GrBackendTexture& tex, int sampleCnt, GrColorType colorType)
bungeman6bd52842016-10-27 09:30:08 -0700479{
ericrkf7b8b8a2016-02-24 14:49:51 -0800480 if (this->isAbandoned()) {
481 return nullptr;
482 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400483 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800484}
Greg Danield85f97d2017-03-07 13:37:21 -0500485
Greg Daniela5cb7812017-06-16 09:45:32 -0400486sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
487 return fGpu->makeSemaphore(isOwned);
488}
489
490sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500491 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400492 GrWrapOwnership ownership) {
493 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500494 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
495 wrapType,
496 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500497}
Brian Salomon8660eb02019-09-20 13:04:13 -0400498
499// Ensures the row bytes are populated (not 0) and makes a copy to a temporary
500// to make the row bytes tight if necessary. Returns false if the input row bytes are invalid.
501static bool prepare_level(const GrMipLevel& inLevel,
502 const SkISize& size,
503 bool rowBytesSupport,
504 GrColorType origColorType,
505 GrColorType allowedColorType,
506 GrMipLevel* outLevel,
507 std::unique_ptr<char[]>* data) {
508 if (!inLevel.fPixels) {
509 outLevel->fPixels = nullptr;
510 outLevel->fRowBytes = 0;
511 return true;
512 }
513 size_t minRB = size.fWidth * GrColorTypeBytesPerPixel(origColorType);
514 size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB;
515 if (actualRB < minRB) {
516 return false;
517 }
518 if (origColorType == allowedColorType && (actualRB == minRB || rowBytesSupport)) {
519 outLevel->fRowBytes = actualRB;
520 outLevel->fPixels = inLevel.fPixels;
521 return true;
522 }
523 auto tempRB = size.fWidth * GrColorTypeBytesPerPixel(allowedColorType);
524 data->reset(new char[tempRB * size.fHeight]);
525 outLevel->fPixels = data->get();
526 outLevel->fRowBytes = tempRB;
527 GrPixelInfo srcInfo(origColorType, kUnpremul_SkAlphaType, nullptr, size);
528 GrPixelInfo dstInfo(allowedColorType, kUnpremul_SkAlphaType, nullptr, size);
529 return GrConvertPixels(dstInfo, data->get(), tempRB, srcInfo, inLevel.fPixels, actualRB);
530}
531
532GrColorType GrResourceProvider::prepareLevels(const GrBackendFormat& format,
533 GrColorType colorType,
534 const SkISize& baseSize,
535 const GrMipLevel texels[],
536 int mipLevelCount,
537 TempLevels* tempLevels,
538 TempLevelDatas* tempLevelDatas) const {
539 SkASSERT(mipLevelCount && texels && texels[0].fPixels);
540
541 auto allowedColorType =
542 this->caps()->supportedWritePixelsColorType(colorType, format, colorType).fColorType;
543 if (allowedColorType == GrColorType::kUnknown) {
544 return GrColorType::kUnknown;
545 }
546 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
547 tempLevels->reset(mipLevelCount);
548 tempLevelDatas->reset(mipLevelCount);
549 auto size = baseSize;
550 for (int i = 0; i < mipLevelCount; ++i) {
551 if (!prepare_level(texels[i], size, rowBytesSupport, colorType, allowedColorType,
552 &(*tempLevels)[i], &(*tempLevelDatas)[i])) {
553 return GrColorType::kUnknown;
554 }
555 size = {std::max(size.fWidth / 2, 1), std::max(size.fHeight / 2, 1)};
556 }
557 return allowedColorType;
558}
559
560sk_sp<GrTexture> GrResourceProvider::writePixels(sk_sp<GrTexture> texture,
561 GrColorType colorType,
562 const SkISize& baseSize,
563 const GrMipLevel texels[],
564 int mipLevelCount) const {
565 SkASSERT(!this->isAbandoned());
566 SkASSERT(texture);
567 SkASSERT(colorType != GrColorType::kUnknown);
568 SkASSERT(mipLevelCount && texels && texels[0].fPixels);
569
570 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
571 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
572 auto tempColorType = this->prepareLevels(texture->backendFormat(), colorType, baseSize, texels,
573 mipLevelCount, &tmpTexels, &tmpDatas);
574 if (tempColorType == GrColorType::kUnknown) {
575 return nullptr;
576 }
577 SkAssertResult(fGpu->writePixels(texture.get(), 0, 0, baseSize.fWidth, baseSize.fHeight,
578 colorType, tempColorType, tmpTexels.get(), mipLevelCount));
579 return texture;
580}