blob: 9ba9505a4ac251de05fdf5e6bbb3eac1a3b5b7fa [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"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040020#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrPath.h"
22#include "src/gpu/GrPathRendering.h"
23#include "src/gpu/GrProxyProvider.h"
24#include "src/gpu/GrRenderTargetPriv.h"
25#include "src/gpu/GrResourceCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrSemaphore.h"
27#include "src/gpu/GrStencilAttachment.h"
28#include "src/gpu/GrTexturePriv.h"
29#include "src/gpu/SkGr.h"
bsalomoned0bcad2015-05-04 10:36:42 -070030
Robert Phillips1bfece82017-06-01 13:56:52 -040031const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050032
33#define ASSERT_SINGLE_OWNER \
34 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
35
Robert Phillips12c46292019-04-23 07:36:17 -040036GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050037 : fCache(cache)
38 , fGpu(gpu)
39#ifdef SK_DEBUG
40 , fSingleOwner(owner)
41#endif
Robert Phillips4150eea2018-02-07 17:08:21 -050042{
Robert Phillips26c90e02017-03-14 14:39:29 -040043 fCaps = sk_ref_sp(fGpu->caps());
bsalomoned0bcad2015-05-04 10:36:42 -070044}
45
Brian Salomonf2c2ba92019-07-17 09:59:59 -040046sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040047 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -040048 GrColorType colorType,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040049 GrRenderable renderable,
Brian Salomon2af3e702019-08-11 19:10:31 -040050 int renderTargetSampleCnt,
51 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040052 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -040053 const GrMipLevel texels[],
54 int mipLevelCount) {
Brian Osman32342f02017-03-04 08:12:46 -050055 ASSERT_SINGLE_OWNER
56
Robert Phillips7f1b4f82017-11-28 07:38:39 -050057 SkASSERT(mipLevelCount > 0);
Robert Phillips1119dc32017-04-11 12:54:57 -040058
Brian Osman32342f02017-03-04 08:12:46 -050059 if (this->isAbandoned()) {
60 return nullptr;
61 }
Robert Phillips1119dc32017-04-11 12:54:57 -040062
Brian Salomonbdecacf2018-02-02 20:32:49 -050063 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -040064 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
65 renderTargetSampleCnt, mipMapped)) {
Brian Osman32342f02017-03-04 08:12:46 -050066 return nullptr;
67 }
Brian Salomon8660eb02019-09-20 13:04:13 -040068 // Current rule is that you can provide no level data, just the base, or all the levels.
69 bool hasPixels = mipLevelCount && texels[0].fPixels;
70 auto scratch = this->getExactScratch(desc, format, renderable, renderTargetSampleCnt, budgeted,
71 mipMapped, isProtected);
72 if (scratch) {
73 if (!hasPixels) {
74 return scratch;
75 }
76 return this->writePixels(std::move(scratch), colorType, {desc.fWidth, desc.fHeight}, texels,
77 mipLevelCount);
Brian Salomona90382f2019-09-17 09:01:56 -040078 }
Brian Salomon1047a492019-07-02 12:25:21 -040079 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
Brian Salomonc9d81f72019-07-03 07:52:41 -040080 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
Brian Salomon8660eb02019-09-20 13:04:13 -040081 GrColorType tempColorType = GrColorType::kUnknown;
82 if (hasPixels) {
83 tempColorType = this->prepareLevels(format, colorType, {desc.fWidth, desc.fHeight}, texels,
84 mipLevelCount, &tmpTexels, &tmpDatas);
85 if (tempColorType == GrColorType::kUnknown) {
86 return nullptr;
Brian Salomon1047a492019-07-02 12:25:21 -040087 }
88 }
Brian Salomon4eb38b72019-08-05 12:58:39 -040089 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
Brian Salomon8660eb02019-09-20 13:04:13 -040090 isProtected, colorType, tempColorType, tmpTexels.get(),
91 mipLevelCount);
Brian Osman32342f02017-03-04 08:12:46 -050092}
93
Robert Phillips45fdae12017-04-17 12:57:27 -040094sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -040095 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040096 GrRenderable renderable,
97 int renderTargetSampleCnt,
98 SkBudgeted budgeted,
Brian Salomon14cb4132019-09-16 13:14:47 -040099 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400100 GrProtected isProtected) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400101 sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400102 mipMapped, isProtected));
Robert Phillips45fdae12017-04-17 12:57:27 -0400103 if (tex && SkBudgeted::kNo == budgeted) {
104 tex->resourcePriv().makeUnbudgeted();
105 }
106
107 return tex;
108}
109
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400111 const GrBackendFormat& format,
Brian Salomona90382f2019-09-17 09:01:56 -0400112 GrColorType colorType,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400113 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400114 int renderTargetSampleCnt,
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500115 SkBudgeted budgeted,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500116 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400117 GrProtected isProtected,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400118 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400119 ASSERT_SINGLE_OWNER
120
Robert Phillips45fdae12017-04-17 12:57:27 -0400121 if (!mipLevel.fPixels) {
122 return nullptr;
123 }
124
Brian Salomona90382f2019-09-17 09:01:56 -0400125 if (SkBackingFit::kApprox == fit) {
Brian Salomon8660eb02019-09-20 13:04:13 -0400126 if (this->isAbandoned()) {
127 return nullptr;
128 }
129 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
130 renderable, renderTargetSampleCnt, GrMipMapped::kNo)) {
131 return nullptr;
132 }
133
134 auto tex = this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
135 isProtected);
Brian Salomona90382f2019-09-17 09:01:56 -0400136 if (!tex) {
137 return nullptr;
138 }
Brian Salomon8660eb02019-09-20 13:04:13 -0400139 return this->writePixels(std::move(tex), colorType, {desc.fWidth, desc.fHeight}, &mipLevel,
140 1);
Brian Salomona90382f2019-09-17 09:01:56 -0400141 } else {
142 return this->createTexture(desc, format, colorType, renderable, renderTargetSampleCnt,
143 budgeted, isProtected, &mipLevel, 1);
Brian Salomona3ffaab2019-07-09 12:26:46 -0400144 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400145}
146
Brian Salomonbb8dde82019-06-27 10:52:13 -0400147sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400148 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400149 SkImage::CompressionType compression,
150 SkBudgeted budgeted, SkData* data) {
151 ASSERT_SINGLE_OWNER
152 if (this->isAbandoned()) {
153 return nullptr;
154 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400155 return fGpu->createCompressedTexture(width, height, format, compression, budgeted, data->data(),
Brian Salomonbb8dde82019-06-27 10:52:13 -0400156 data->size());
157}
158
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400159sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400160 const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400161 GrRenderable renderable,
162 int renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400163 GrMipMapped mipMapped,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400164 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400165 GrProtected isProtected) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400166 ASSERT_SINGLE_OWNER
Robert Phillipse78b7252017-04-06 07:59:41 -0400167 if (this->isAbandoned()) {
168 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500169 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400170
Greg Daniel6fa62e22019-08-07 15:52:37 -0400171 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400172 renderTargetSampleCnt, mipMapped)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400173 return nullptr;
174 }
175
Jim Van Verth1676cb92019-01-15 13:24:45 -0500176 // Compressed textures are read-only so they don't support re-use for scratch.
Brian Salomona90382f2019-09-17 09:01:56 -0400177 // TODO: Support GrMipMapped::kYes in scratch texture lookup here.
Brian Salomon14cb4132019-09-16 13:14:47 -0400178 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400179 sk_sp<GrTexture> tex = this->getExactScratch(
Brian Salomon14cb4132019-09-16 13:14:47 -0400180 desc, format, renderable, renderTargetSampleCnt, budgeted, mipMapped, isProtected);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500181 if (tex) {
182 return tex;
183 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400184 }
185
Brian Salomona90382f2019-09-17 09:01:56 -0400186 return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, mipMapped, budgeted,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400187 isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500188}
189
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400190// Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to
191// the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2.
192uint32_t GrResourceProvider::MakeApprox(uint32_t value) {
193 static const int kMagicTol = 1024;
194
195 value = SkTMax(kMinScratchTextureSize, value);
196
197 if (SkIsPow2(value)) {
198 return value;
199 }
200
201 uint32_t ceilPow2 = GrNextPow2(value);
202 if (value <= kMagicTol) {
203 return ceilPow2;
204 }
205
206 uint32_t floorPow2 = ceilPow2 >> 1;
207 uint32_t mid = floorPow2 + (floorPow2 >> 1);
208
209 if (value <= mid) {
210 return mid;
211 }
212
213 return ceilPow2;
214}
215
Robert Phillips67d52cf2017-06-05 13:38:13 -0400216sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400217 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400218 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400219 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400220 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500221 ASSERT_SINGLE_OWNER
Brian Osman32342f02017-03-04 08:12:46 -0500222
Brian Osman32342f02017-03-04 08:12:46 -0500223 if (this->isAbandoned()) {
224 return nullptr;
225 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400226
Jim Van Verth1676cb92019-01-15 13:24:45 -0500227 // Currently we don't recycle compressed textures as scratch.
228 if (GrPixelConfigIsCompressed(desc.fConfig)) {
229 return nullptr;
230 }
231
Greg Daniel6fa62e22019-08-07 15:52:37 -0400232 if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
233 renderTargetSampleCnt, GrMipMapped::kNo)) {
Brian Salomond34edf32017-05-19 15:45:48 -0400234 return nullptr;
235 }
236
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400237 // bin by some multiple or power of 2 with a reasonable min
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400238 GrSurfaceDesc copyDesc(desc);
239 copyDesc.fWidth = MakeApprox(desc.fWidth);
240 copyDesc.fHeight = MakeApprox(desc.fHeight);
Greg Daniel29bf84f2017-09-25 12:25:12 -0400241
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400242 if (auto tex = this->refScratchTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400243 GrMipMapped::kNo, isProtected)) {
Greg Daniel29bf84f2017-09-25 12:25:12 -0400244 return tex;
245 }
246
Michael Ludwigbd2f0702019-09-13 15:29:41 -0400247 return fGpu->createTexture(copyDesc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400248 GrMipMapped::kNo, SkBudgeted::kYes, isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500249}
250
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400251sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400252 const GrBackendFormat& format,
Brian Salomone8a766b2019-07-19 14:24:36 -0400253 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400254 int renderTargetSampleCnt,
Brian Salomon14cb4132019-09-16 13:14:47 -0400255 GrMipMapped mipMapped,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400256 GrProtected isProtected) {
Brian Osman32342f02017-03-04 08:12:46 -0500257 ASSERT_SINGLE_OWNER
258 SkASSERT(!this->isAbandoned());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500259 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Greg Daniel6fa62e22019-08-07 15:52:37 -0400260 SkASSERT(fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
261 renderable, renderTargetSampleCnt, GrMipMapped::kNo));
Brian Osman32342f02017-03-04 08:12:46 -0500262
Brian Salomond17b4a62017-05-23 16:53:47 -0400263 // We could make initial clears work with scratch textures but it is a rare case so we just opt
264 // to fall back to making a new texture.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400265 if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
Brian Osman32342f02017-03-04 08:12:46 -0500266 GrScratchKey key;
Brian Salomon14cb4132019-09-16 13:14:47 -0400267 GrTexturePriv::ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable,
268 renderTargetSampleCnt, mipMapped, isProtected, &key);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400269 GrGpuResource* resource = fCache->findAndRefScratchResource(key);
Brian Osman32342f02017-03-04 08:12:46 -0500270 if (resource) {
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400271 fGpu->stats()->incNumScratchTexturesReused();
Brian Osman32342f02017-03-04 08:12:46 -0500272 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400273 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500274 }
275 }
276
Brian Osman32342f02017-03-04 08:12:46 -0500277 return nullptr;
278}
279
Greg Daniel7ef28f32017-04-20 16:41:55 +0000280sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400281 GrColorType colorType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500282 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500283 GrWrapCacheable cacheable,
284 GrIOType ioType) {
Brian Osman32342f02017-03-04 08:12:46 -0500285 ASSERT_SINGLE_OWNER
286 if (this->isAbandoned()) {
287 return nullptr;
288 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400289 return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400290}
291
292sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400293 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400294 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500295 GrWrapOwnership ownership,
296 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400297 ASSERT_SINGLE_OWNER
298 if (this->isAbandoned()) {
299 return nullptr;
300 }
Robert Phillips0902c982019-07-16 07:47:56 -0400301 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable);
Brian Osman32342f02017-03-04 08:12:46 -0500302}
303
304sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400305 const GrBackendRenderTarget& backendRT, GrColorType colorType)
Brian Osman32342f02017-03-04 08:12:46 -0500306{
307 ASSERT_SINGLE_OWNER
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400308 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500309}
310
Greg Danielb46add82019-01-02 14:51:29 -0500311sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget(
312 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
313 ASSERT_SINGLE_OWNER
314 return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
315 vkInfo);
316
317}
318
Brian Osman32342f02017-03-04 08:12:46 -0500319void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
320 GrGpuResource* resource) {
321 ASSERT_SINGLE_OWNER
322 if (this->isAbandoned() || !resource) {
323 return;
324 }
325 resource->resourcePriv().setUniqueKey(key);
326}
327
Brian Salomond28a79d2017-10-16 13:01:07 -0400328sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
Brian Osman32342f02017-03-04 08:12:46 -0500329 ASSERT_SINGLE_OWNER
Brian Salomond28a79d2017-10-16 13:01:07 -0400330 return this->isAbandoned() ? nullptr
331 : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
Brian Osman32342f02017-03-04 08:12:46 -0500332}
333
Brian Salomondbf70722019-02-07 11:31:24 -0500334sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType,
335 size_t size,
336 const void* data,
337 const GrUniqueKey& key) {
338 if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) {
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400339 return buffer;
Chris Dalton5d2de082017-12-19 10:40:23 -0700340 }
Brian Salomondbf70722019-02-07 11:31:24 -0500341 if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) {
Chris Dalton133944a2018-11-16 23:30:29 -0500342 // We shouldn't bin and/or cache static buffers.
Brian Salomondbf70722019-02-07 11:31:24 -0500343 SkASSERT(buffer->size() == size);
Chris Dalton5d2de082017-12-19 10:40:23 -0700344 SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
Chris Dalton5d2de082017-12-19 10:40:23 -0700345 buffer->resourcePriv().setUniqueKey(key);
Brian Salomondbf70722019-02-07 11:31:24 -0500346 return sk_sp<const GrGpuBuffer>(buffer);
Chris Dalton5d2de082017-12-19 10:40:23 -0700347 }
348 return nullptr;
349}
350
Brian Salomondbf70722019-02-07 11:31:24 -0500351sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
352 int patternSize,
353 int reps,
354 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500355 const GrUniqueKey* key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700356 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
357
Brian Salomondbf70722019-02-07 11:31:24 -0500358 sk_sp<GrGpuBuffer> buffer(
359 this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
bsalomoned0bcad2015-05-04 10:36:42 -0700360 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700361 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700362 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400363 uint16_t* data = (uint16_t*) buffer->map();
364 SkAutoTArray<uint16_t> temp;
365 if (!data) {
366 temp.reset(reps * patternSize);
367 data = temp.get();
368 }
bsalomoned0bcad2015-05-04 10:36:42 -0700369 for (int i = 0; i < reps; ++i) {
370 int baseIdx = i * patternSize;
371 uint16_t baseVert = (uint16_t)(i * vertCount);
372 for (int j = 0; j < patternSize; ++j) {
373 data[baseIdx+j] = baseVert + pattern[j];
374 }
375 }
Brian Salomon7f56d3d2017-10-09 13:02:49 -0400376 if (temp.get()) {
377 if (!buffer->updateData(data, bufferSize)) {
378 return nullptr;
379 }
380 } else {
381 buffer->unmap();
bsalomoned0bcad2015-05-04 10:36:42 -0700382 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500383 if (key) {
384 SkASSERT(key->isValid());
385 this->assignUniqueKeyToResource(*key, buffer.get());
386 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400387 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700388}
389
Brian Salomon34169692017-08-28 15:32:01 -0400390static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
391
Brian Salomondbf70722019-02-07 11:31:24 -0500392sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700393 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
Brian Salomon57caa662017-10-18 12:21:05 +0000394 static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 };
Brian Salomona29dd9d2019-02-07 13:27:18 -0500395 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr);
bsalomoned0bcad2015-05-04 10:36:42 -0700396}
397
Brian Salomon763abf02018-05-01 18:49:38 +0000398int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
Brian Salomon34169692017-08-28 15:32:01 -0400399
Robert Phillips67d52cf2017-06-05 13:38:13 -0400400sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400401 if (this->isAbandoned()) {
402 return nullptr;
403 }
404
bsalomon706f08f2015-05-22 07:35:58 -0700405 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700406 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700407}
408
Brian Salomondbf70722019-02-07 11:31:24 -0500409sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType,
410 GrAccessPattern accessPattern,
411 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700412 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700413 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700414 }
cdaltond37fe762016-04-21 07:41:50 -0700415 if (kDynamic_GrAccessPattern != accessPattern) {
416 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
417 }
cdaltond37fe762016-04-21 07:41:50 -0700418 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400419 static const size_t MIN_SIZE = 1 << 12;
420 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700421
cdaltond37fe762016-04-21 07:41:50 -0700422 GrScratchKey key;
Brian Salomondbf70722019-02-07 11:31:24 -0500423 GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
424 auto buffer =
425 sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
Robert Phillipsaee18c92019-09-06 11:48:27 -0400426 key)));
cdaltond37fe762016-04-21 07:41:50 -0700427 if (!buffer) {
428 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
429 if (!buffer) {
430 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700431 }
432 }
cdaltond37fe762016-04-21 07:41:50 -0700433 if (data) {
434 buffer->updateData(data, size);
435 }
436 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800437}
438
Chris Daltoneffee202019-07-01 22:28:03 -0600439bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
egdanielec00d942015-09-14 12:56:10 -0700440 SkASSERT(rt);
Chris Daltoneffee202019-07-01 22:28:03 -0600441 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
442 if (stencil && stencil->numSamples() >= minStencilSampleCount) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400443 return true;
egdanielec00d942015-09-14 12:56:10 -0700444 }
445
446 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
447 GrUniqueKey sbKey;
448
449 int width = rt->width();
450 int height = rt->height();
451#if 0
452 if (this->caps()->oversizedStencilSupport()) {
453 width = SkNextPow2(width);
454 height = SkNextPow2(height);
455 }
456#endif
Chris Daltoneffee202019-07-01 22:28:03 -0600457 GrStencilAttachment::ComputeSharedStencilAttachmentKey(
458 width, height, minStencilSampleCount, &sbKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400459 auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
egdanielec00d942015-09-14 12:56:10 -0700460 if (!stencil) {
461 // Need to try and create a new stencil
Chris Daltoneffee202019-07-01 22:28:03 -0600462 stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
463 rt, width, height, minStencilSampleCount));
Robert Phillips01a91282018-07-26 08:03:04 -0400464 if (!stencil) {
465 return false;
egdanielec00d942015-09-14 12:56:10 -0700466 }
Robert Phillips01a91282018-07-26 08:03:04 -0400467 this->assignUniqueKeyToResource(sbKey, stencil.get());
egdanielec00d942015-09-14 12:56:10 -0700468 }
Greg Danielcfa39352018-10-05 12:01:59 -0400469 rt->renderTargetPriv().attachStencilAttachment(std::move(stencil));
egdanielec00d942015-09-14 12:56:10 -0700470 }
Chris Dalton215ff332019-07-02 09:38:22 -0600471
472 if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
473 return stencil->numSamples() >= minStencilSampleCount;
474 }
475 return false;
egdanielec00d942015-09-14 12:56:10 -0700476}
477
bungeman6bd52842016-10-27 09:30:08 -0700478sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400479 const GrBackendTexture& tex, int sampleCnt, GrColorType colorType)
bungeman6bd52842016-10-27 09:30:08 -0700480{
ericrkf7b8b8a2016-02-24 14:49:51 -0800481 if (this->isAbandoned()) {
482 return nullptr;
483 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400484 return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800485}
Greg Danield85f97d2017-03-07 13:37:21 -0500486
Greg Daniela5cb7812017-06-16 09:45:32 -0400487sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
488 return fGpu->makeSemaphore(isOwned);
489}
490
491sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
Greg Daniel17b7c052018-01-09 13:55:33 -0500492 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400493 GrWrapOwnership ownership) {
494 ASSERT_SINGLE_OWNER
Greg Daniel17b7c052018-01-09 13:55:33 -0500495 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore,
496 wrapType,
497 ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500498}
Brian Salomon8660eb02019-09-20 13:04:13 -0400499
500// Ensures the row bytes are populated (not 0) and makes a copy to a temporary
501// to make the row bytes tight if necessary. Returns false if the input row bytes are invalid.
502static bool prepare_level(const GrMipLevel& inLevel,
503 const SkISize& size,
504 bool rowBytesSupport,
505 GrColorType origColorType,
506 GrColorType allowedColorType,
507 GrMipLevel* outLevel,
508 std::unique_ptr<char[]>* data) {
509 if (!inLevel.fPixels) {
510 outLevel->fPixels = nullptr;
511 outLevel->fRowBytes = 0;
512 return true;
513 }
514 size_t minRB = size.fWidth * GrColorTypeBytesPerPixel(origColorType);
515 size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB;
516 if (actualRB < minRB) {
517 return false;
518 }
519 if (origColorType == allowedColorType && (actualRB == minRB || rowBytesSupport)) {
520 outLevel->fRowBytes = actualRB;
521 outLevel->fPixels = inLevel.fPixels;
522 return true;
523 }
524 auto tempRB = size.fWidth * GrColorTypeBytesPerPixel(allowedColorType);
525 data->reset(new char[tempRB * size.fHeight]);
526 outLevel->fPixels = data->get();
527 outLevel->fRowBytes = tempRB;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400528 GrImageInfo srcInfo(origColorType, kUnpremul_SkAlphaType, nullptr, size);
529 GrImageInfo dstInfo(allowedColorType, kUnpremul_SkAlphaType, nullptr, size);
Brian Salomon8660eb02019-09-20 13:04:13 -0400530 return GrConvertPixels(dstInfo, data->get(), tempRB, srcInfo, inLevel.fPixels, actualRB);
531}
532
533GrColorType GrResourceProvider::prepareLevels(const GrBackendFormat& format,
534 GrColorType colorType,
535 const SkISize& baseSize,
536 const GrMipLevel texels[],
537 int mipLevelCount,
538 TempLevels* tempLevels,
539 TempLevelDatas* tempLevelDatas) const {
540 SkASSERT(mipLevelCount && texels && texels[0].fPixels);
541
542 auto allowedColorType =
543 this->caps()->supportedWritePixelsColorType(colorType, format, colorType).fColorType;
544 if (allowedColorType == GrColorType::kUnknown) {
545 return GrColorType::kUnknown;
546 }
547 bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport();
548 tempLevels->reset(mipLevelCount);
549 tempLevelDatas->reset(mipLevelCount);
550 auto size = baseSize;
551 for (int i = 0; i < mipLevelCount; ++i) {
552 if (!prepare_level(texels[i], size, rowBytesSupport, colorType, allowedColorType,
553 &(*tempLevels)[i], &(*tempLevelDatas)[i])) {
554 return GrColorType::kUnknown;
555 }
556 size = {std::max(size.fWidth / 2, 1), std::max(size.fHeight / 2, 1)};
557 }
558 return allowedColorType;
559}
560
561sk_sp<GrTexture> GrResourceProvider::writePixels(sk_sp<GrTexture> texture,
562 GrColorType colorType,
563 const SkISize& baseSize,
564 const GrMipLevel texels[],
565 int mipLevelCount) const {
566 SkASSERT(!this->isAbandoned());
567 SkASSERT(texture);
568 SkASSERT(colorType != GrColorType::kUnknown);
569 SkASSERT(mipLevelCount && texels && texels[0].fPixels);
570
571 SkAutoSTMalloc<14, GrMipLevel> tmpTexels;
572 SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas;
573 auto tempColorType = this->prepareLevels(texture->backendFormat(), colorType, baseSize, texels,
574 mipLevelCount, &tmpTexels, &tmpDatas);
575 if (tempColorType == GrColorType::kUnknown) {
576 return nullptr;
577 }
578 SkAssertResult(fGpu->writePixels(texture.get(), 0, 0, baseSize.fWidth, baseSize.fHeight,
579 colorType, tempColorType, tmpTexels.get(), mipLevelCount));
580 return texture;
581}