bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1 | /* |
| 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 Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrResourceProvider.h" |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrBackendSemaphore.h" |
| 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "include/private/GrResourceKey.h" |
| 13 | #include "include/private/GrSingleOwner.h" |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 14 | #include "src/core/SkConvertPixels.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrSemaphore.h" |
| 26 | #include "src/gpu/GrStencilAttachment.h" |
| 27 | #include "src/gpu/GrTexturePriv.h" |
| 28 | #include "src/gpu/SkGr.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 29 | |
Robert Phillips | 1bfece8 | 2017-06-01 13:56:52 -0400 | [diff] [blame] | 30 | const uint32_t GrResourceProvider::kMinScratchTextureSize = 16; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 31 | |
| 32 | #define ASSERT_SINGLE_OWNER \ |
| 33 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
| 34 | |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 35 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 36 | : fCache(cache) |
| 37 | , fGpu(gpu) |
| 38 | #ifdef SK_DEBUG |
| 39 | , fSingleOwner(owner) |
| 40 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 41 | { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 42 | fCaps = sk_ref_sp(fGpu->caps()); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame^] | 45 | // 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. |
| 47 | static bool prepare_level(const GrMipLevel& inLevel, size_t bpp, int w, int h, bool rowBytesSupport, |
| 48 | GrMipLevel* outLevel, std::unique_ptr<char[]>* data) { |
| 49 | if (!inLevel.fPixels) { |
| 50 | outLevel->fPixels = nullptr; |
| 51 | outLevel->fRowBytes = 0; |
| 52 | return true; |
| 53 | } |
| 54 | size_t minRB = w * bpp; |
| 55 | size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB; |
| 56 | if (actualRB < minRB) { |
| 57 | return false; |
| 58 | } |
| 59 | if (actualRB == minRB || rowBytesSupport) { |
| 60 | outLevel->fRowBytes = actualRB; |
| 61 | outLevel->fPixels = inLevel.fPixels; |
| 62 | } else { |
| 63 | data->reset(new char[minRB * h]); |
| 64 | outLevel->fPixels = data->get(); |
| 65 | outLevel->fRowBytes = minRB; |
| 66 | SkRectMemcpy(data->get(), outLevel->fRowBytes, inLevel.fPixels, inLevel.fRowBytes, minRB, |
| 67 | h); |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 72 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 73 | const GrMipLevel texels[], int mipLevelCount) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 74 | ASSERT_SINGLE_OWNER |
| 75 | |
Robert Phillips | 7f1b4f8 | 2017-11-28 07:38:39 -0500 | [diff] [blame] | 76 | SkASSERT(mipLevelCount > 0); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 77 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 78 | if (this->isAbandoned()) { |
| 79 | return nullptr; |
| 80 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 81 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 82 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 83 | if (!fCaps->validateSurfaceDesc(desc, mipMapped)) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 86 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 87 | SkAutoSTMalloc<14, GrMipLevel> tmpTexels; |
Brian Salomon | c9d81f7 | 2019-07-03 07:52:41 -0400 | [diff] [blame] | 88 | SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 89 | if (mipLevelCount > 0 && texels) { |
| 90 | tmpTexels.reset(mipLevelCount); |
| 91 | tmpDatas.reset(mipLevelCount); |
| 92 | int w = desc.fWidth; |
| 93 | int h = desc.fHeight; |
| 94 | size_t bpp = GrBytesPerPixel(desc.fConfig); |
| 95 | for (int i = 0; i < mipLevelCount; ++i) { |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame^] | 96 | if (!prepare_level(texels[i], bpp, w, h, this->caps()->writePixelsRowBytesSupport(), |
| 97 | &tmpTexels[i], &tmpDatas[i])) { |
| 98 | return nullptr; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 99 | } |
| 100 | w = std::max(w / 2, 1); |
| 101 | h = std::max(h / 2, 1); |
| 102 | } |
| 103 | } |
| 104 | return fGpu->createTexture(desc, budgeted, tmpTexels.get(), mipLevelCount); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 107 | sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 108 | SkBudgeted budgeted, Flags flags) { |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 109 | sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags)); |
| 110 | if (tex && SkBudgeted::kNo == budgeted) { |
| 111 | tex->resourcePriv().makeUnbudgeted(); |
| 112 | } |
| 113 | |
| 114 | return tex; |
| 115 | } |
| 116 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 117 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, |
| 118 | SkBudgeted budgeted, |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 119 | SkBackingFit fit, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 120 | const GrMipLevel& mipLevel, |
| 121 | Flags flags) { |
Robert Phillips | 774831a | 2017-04-20 10:19:33 -0400 | [diff] [blame] | 122 | ASSERT_SINGLE_OWNER |
| 123 | |
| 124 | if (this->isAbandoned()) { |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 128 | if (!mipLevel.fPixels) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 132 | if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 133 | return nullptr; |
| 134 | } |
| 135 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 136 | GrContext* context = fGpu->getContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 137 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 138 | |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame^] | 139 | size_t bpp = GrBytesPerPixel(desc.fConfig); |
| 140 | std::unique_ptr<char[]> tmpData; |
| 141 | GrMipLevel tmpLevel; |
| 142 | if (!prepare_level(mipLevel, bpp, desc.fWidth, desc.fHeight, |
| 143 | this->caps()->writePixelsRowBytesSupport(), &tmpLevel, &tmpData)) { |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Brian Salomon | 1fcac05 | 2018-05-09 16:33:09 -0400 | [diff] [blame] | 147 | SkColorType colorType; |
| 148 | if (GrPixelConfigToColorType(desc.fConfig, &colorType)) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 149 | sk_sp<GrTexture> tex = (SkBackingFit::kApprox == fit) |
| 150 | ? this->createApproxTexture(desc, flags) |
| 151 | : this->createTexture(desc, budgeted, flags); |
| 152 | if (!tex) { |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | sk_sp<GrTextureProxy> proxy = proxyProvider->createWrapped(std::move(tex), |
| 157 | kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 1fcac05 | 2018-05-09 16:33:09 -0400 | [diff] [blame] | 158 | if (!proxy) { |
| 159 | return nullptr; |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 160 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 161 | // Here we don't really know the alpha type of the data we want to upload. All we really |
| 162 | // care about is that it is not converted. So we use the same alpha type of the data |
| 163 | // and the surface context. |
| 164 | static constexpr auto kAlphaType = kPremul_SkAlphaType; |
| 165 | auto srcInfo = SkImageInfo::Make(desc.fWidth, desc.fHeight, colorType, kAlphaType); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 166 | sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext( |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 167 | std::move(proxy), SkColorTypeToGrColorType(colorType), kAlphaType); |
Brian Salomon | 1fcac05 | 2018-05-09 16:33:09 -0400 | [diff] [blame] | 168 | if (!sContext) { |
| 169 | return nullptr; |
| 170 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 171 | SkAssertResult( |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame^] | 172 | sContext->writePixels(srcInfo, tmpLevel.fPixels, tmpLevel.fRowBytes, {0, 0})); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 173 | return sk_ref_sp(sContext->asTextureProxy()->peekTexture()); |
Brian Salomon | 1fcac05 | 2018-05-09 16:33:09 -0400 | [diff] [blame] | 174 | } else { |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame^] | 175 | return fGpu->createTexture(desc, budgeted, &tmpLevel, 1); |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 176 | } |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 179 | sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height, |
| 180 | SkImage::CompressionType compression, |
| 181 | SkBudgeted budgeted, SkData* data) { |
| 182 | ASSERT_SINGLE_OWNER |
| 183 | if (this->isAbandoned()) { |
| 184 | return nullptr; |
| 185 | } |
| 186 | return fGpu->createCompressedTexture(width, height, compression, budgeted, data->data(), |
| 187 | data->size()); |
| 188 | } |
| 189 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 190 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 191 | Flags flags) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 192 | ASSERT_SINGLE_OWNER |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 193 | if (this->isAbandoned()) { |
| 194 | return nullptr; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 195 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 196 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 197 | if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 201 | // Compressed textures are read-only so they don't support re-use for scratch. |
| 202 | if (!GrPixelConfigIsCompressed(desc.fConfig)) { |
| 203 | sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags); |
| 204 | if (tex) { |
| 205 | return tex; |
| 206 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 209 | return fGpu->createTexture(desc, budgeted); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 212 | sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 213 | Flags flags) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 214 | ASSERT_SINGLE_OWNER |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 215 | SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 216 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 217 | if (this->isAbandoned()) { |
| 218 | return nullptr; |
| 219 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 220 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 221 | // Currently we don't recycle compressed textures as scratch. |
| 222 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 226 | if (!fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 227 | return nullptr; |
| 228 | } |
| 229 | |
Greg Daniel | 6b60b6e | 2017-09-26 16:37:12 -0400 | [diff] [blame] | 230 | if (auto tex = this->refScratchTexture(desc, flags)) { |
| 231 | return tex; |
| 232 | } |
| 233 | |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 234 | SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc); |
| 235 | |
| 236 | // bin by pow2 with a reasonable min |
| 237 | if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && |
| 238 | (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) { |
| 239 | GrSurfaceDesc* wdesc = copyDesc.writable(); |
| 240 | wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth)); |
| 241 | wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight)); |
| 242 | } |
| 243 | |
| 244 | if (auto tex = this->refScratchTexture(*copyDesc, flags)) { |
| 245 | return tex; |
| 246 | } |
| 247 | |
| 248 | return fGpu->createTexture(*copyDesc, SkBudgeted::kYes); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 251 | sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, Flags flags) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 252 | ASSERT_SINGLE_OWNER |
| 253 | SkASSERT(!this->isAbandoned()); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 254 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 255 | SkASSERT(fCaps->validateSurfaceDesc(desc, GrMipMapped::kNo)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 256 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 257 | // We could make initial clears work with scratch textures but it is a rare case so we just opt |
| 258 | // to fall back to making a new texture. |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 259 | if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && |
| 260 | (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 261 | |
| 262 | GrScratchKey key; |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 263 | GrTexturePriv::ComputeScratchKey(desc, &key); |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 264 | auto scratchFlags = GrResourceCache::ScratchFlags::kNone; |
| 265 | if (Flags::kNoPendingIO & flags) { |
| 266 | scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO; |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 267 | } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 268 | // If it is not a render target then it will most likely be populated by |
| 269 | // writePixels() which will trigger a flush if the texture has pending IO. |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 270 | scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 271 | } |
| 272 | GrGpuResource* resource = fCache->findAndRefScratchResource(key, |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 273 | GrSurface::WorstCaseSize(desc), |
| 274 | scratchFlags); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 275 | if (resource) { |
| 276 | GrSurface* surface = static_cast<GrSurface*>(resource); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 277 | return sk_sp<GrTexture>(surface->asTexture()); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 281 | return nullptr; |
| 282 | } |
| 283 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 284 | sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 285 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 286 | GrWrapCacheable cacheable, |
| 287 | GrIOType ioType) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 288 | ASSERT_SINGLE_OWNER |
| 289 | if (this->isAbandoned()) { |
| 290 | return nullptr; |
| 291 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 292 | return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 296 | int sampleCnt, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 297 | GrWrapOwnership ownership, |
| 298 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 299 | ASSERT_SINGLE_OWNER |
| 300 | if (this->isAbandoned()) { |
| 301 | return nullptr; |
| 302 | } |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 303 | return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget( |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 307 | const GrBackendRenderTarget& backendRT) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 308 | { |
| 309 | ASSERT_SINGLE_OWNER |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 310 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 313 | sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget( |
| 314 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) { |
| 315 | ASSERT_SINGLE_OWNER |
| 316 | return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, |
| 317 | vkInfo); |
| 318 | |
| 319 | } |
| 320 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 321 | void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key, |
| 322 | GrGpuResource* resource) { |
| 323 | ASSERT_SINGLE_OWNER |
| 324 | if (this->isAbandoned() || !resource) { |
| 325 | return; |
| 326 | } |
| 327 | resource->resourcePriv().setUniqueKey(key); |
| 328 | } |
| 329 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 330 | sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 331 | ASSERT_SINGLE_OWNER |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 332 | return this->isAbandoned() ? nullptr |
| 333 | : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 336 | sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType, |
| 337 | size_t size, |
| 338 | const void* data, |
| 339 | const GrUniqueKey& key) { |
| 340 | if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) { |
Kevin Lubick | f7621cb | 2018-04-16 15:51:44 -0400 | [diff] [blame] | 341 | return std::move(buffer); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 342 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 343 | if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 344 | // We shouldn't bin and/or cache static buffers. |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 345 | SkASSERT(buffer->size() == size); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 346 | SkASSERT(!buffer->resourcePriv().getScratchKey().isValid()); |
| 347 | SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly()); |
| 348 | buffer->resourcePriv().setUniqueKey(key); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 349 | return sk_sp<const GrGpuBuffer>(buffer); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 350 | } |
| 351 | return nullptr; |
| 352 | } |
| 353 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 354 | sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern, |
| 355 | int patternSize, |
| 356 | int reps, |
| 357 | int vertCount, |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 358 | const GrUniqueKey* key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 359 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 360 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 361 | // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO. |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 362 | sk_sp<GrGpuBuffer> buffer( |
| 363 | this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern)); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 364 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 365 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 366 | } |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 367 | uint16_t* data = (uint16_t*) buffer->map(); |
| 368 | SkAutoTArray<uint16_t> temp; |
| 369 | if (!data) { |
| 370 | temp.reset(reps * patternSize); |
| 371 | data = temp.get(); |
| 372 | } |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 373 | for (int i = 0; i < reps; ++i) { |
| 374 | int baseIdx = i * patternSize; |
| 375 | uint16_t baseVert = (uint16_t)(i * vertCount); |
| 376 | for (int j = 0; j < patternSize; ++j) { |
| 377 | data[baseIdx+j] = baseVert + pattern[j]; |
| 378 | } |
| 379 | } |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 380 | if (temp.get()) { |
| 381 | if (!buffer->updateData(data, bufferSize)) { |
| 382 | return nullptr; |
| 383 | } |
| 384 | } else { |
| 385 | buffer->unmap(); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 386 | } |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 387 | if (key) { |
| 388 | SkASSERT(key->isValid()); |
| 389 | this->assignUniqueKeyToResource(*key, buffer.get()); |
| 390 | } |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 391 | return std::move(buffer); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 394 | static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 395 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 396 | sk_sp<const GrGpuBuffer> GrResourceProvider::createQuadIndexBuffer() { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 397 | GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 398 | static const uint16_t kPattern[] = { 0, 1, 2, 2, 1, 3 }; |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 399 | return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, nullptr); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 402 | int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 403 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 404 | sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) { |
Robert Phillips | 0f17181 | 2017-09-21 14:25:31 -0400 | [diff] [blame] | 405 | if (this->isAbandoned()) { |
| 406 | return nullptr; |
| 407 | } |
| 408 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 409 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 410 | return this->gpu()->pathRendering()->createPath(path, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 413 | sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 414 | GrAccessPattern accessPattern, |
| 415 | const void* data) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 416 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 417 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 418 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 419 | if (kDynamic_GrAccessPattern != accessPattern) { |
| 420 | return this->gpu()->createBuffer(size, intendedType, accessPattern, data); |
| 421 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 422 | // bin by pow2 with a reasonable min |
Robert Phillips | 9e38047 | 2016-10-28 12:15:03 -0400 | [diff] [blame] | 423 | static const size_t MIN_SIZE = 1 << 12; |
| 424 | size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size)); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 425 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 426 | GrScratchKey key; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 427 | GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key); |
| 428 | auto buffer = |
| 429 | sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource( |
| 430 | key, allocSize, GrResourceCache::ScratchFlags::kNone))); |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 431 | if (!buffer) { |
| 432 | buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern); |
| 433 | if (!buffer) { |
| 434 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 437 | if (data) { |
| 438 | buffer->updateData(data, size); |
| 439 | } |
| 440 | return buffer; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 443 | bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 444 | SkASSERT(rt); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 445 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
| 446 | if (stencil && stencil->numSamples() >= minStencilSampleCount) { |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 447 | return true; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) { |
| 451 | GrUniqueKey sbKey; |
| 452 | |
| 453 | int width = rt->width(); |
| 454 | int height = rt->height(); |
| 455 | #if 0 |
| 456 | if (this->caps()->oversizedStencilSupport()) { |
| 457 | width = SkNextPow2(width); |
| 458 | height = SkNextPow2(height); |
| 459 | } |
| 460 | #endif |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 461 | GrStencilAttachment::ComputeSharedStencilAttachmentKey( |
| 462 | width, height, minStencilSampleCount, &sbKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 463 | auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 464 | if (!stencil) { |
| 465 | // Need to try and create a new stencil |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 466 | stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget( |
| 467 | rt, width, height, minStencilSampleCount)); |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 468 | if (!stencil) { |
| 469 | return false; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 470 | } |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 471 | this->assignUniqueKeyToResource(sbKey, stencil.get()); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 472 | } |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 473 | rt->renderTargetPriv().attachStencilAttachment(std::move(stencil)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 474 | } |
Chris Dalton | 215ff33 | 2019-07-02 09:38:22 -0600 | [diff] [blame] | 475 | |
| 476 | if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) { |
| 477 | return stencil->numSamples() >= minStencilSampleCount; |
| 478 | } |
| 479 | return false; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 480 | } |
| 481 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 482 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget( |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 483 | const GrBackendTexture& tex, int sampleCnt) |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 484 | { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 485 | if (this->isAbandoned()) { |
| 486 | return nullptr; |
| 487 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 488 | return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 489 | } |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 490 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 491 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) { |
| 492 | return fGpu->makeSemaphore(isOwned); |
| 493 | } |
| 494 | |
| 495 | sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 496 | SemaphoreWrapType wrapType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 497 | GrWrapOwnership ownership) { |
| 498 | ASSERT_SINGLE_OWNER |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 499 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, |
| 500 | wrapType, |
| 501 | ownership); |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 502 | } |