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