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