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" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrPath.h" |
| 22 | #include "src/gpu/GrPathRendering.h" |
| 23 | #include "src/gpu/GrProxyProvider.h" |
| 24 | #include "src/gpu/GrRenderTargetPriv.h" |
| 25 | #include "src/gpu/GrResourceCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/GrSemaphore.h" |
| 27 | #include "src/gpu/GrStencilAttachment.h" |
| 28 | #include "src/gpu/GrTexturePriv.h" |
| 29 | #include "src/gpu/SkGr.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 30 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 31 | const int GrResourceProvider::kMinScratchTextureSize = 16; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 32 | |
| 33 | #define ASSERT_SINGLE_OWNER \ |
| 34 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
| 35 | |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 36 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 37 | : fCache(cache) |
| 38 | , fGpu(gpu) |
| 39 | #ifdef SK_DEBUG |
| 40 | , fSingleOwner(owner) |
| 41 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 42 | { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 43 | fCaps = sk_ref_sp(fGpu->caps()); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 46 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 47 | const GrBackendFormat& format, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 48 | GrColorType colorType, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 49 | GrRenderable renderable, |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 50 | int renderTargetSampleCnt, |
| 51 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 52 | GrProtected isProtected, |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 53 | const GrMipLevel texels[], |
| 54 | int mipLevelCount) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 55 | ASSERT_SINGLE_OWNER |
| 56 | |
Robert Phillips | 7f1b4f8 | 2017-11-28 07:38:39 -0500 | [diff] [blame] | 57 | SkASSERT(mipLevelCount > 0); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 58 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 59 | if (this->isAbandoned()) { |
| 60 | return nullptr; |
| 61 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 62 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 63 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 64 | if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable, |
| 65 | renderTargetSampleCnt, mipMapped)) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 68 | // Current rule is that you can provide no level data, just the base, or all the levels. |
| 69 | bool hasPixels = mipLevelCount && texels[0].fPixels; |
| 70 | auto scratch = this->getExactScratch(desc, format, renderable, renderTargetSampleCnt, budgeted, |
| 71 | mipMapped, isProtected); |
| 72 | if (scratch) { |
| 73 | if (!hasPixels) { |
| 74 | return scratch; |
| 75 | } |
| 76 | return this->writePixels(std::move(scratch), colorType, {desc.fWidth, desc.fHeight}, texels, |
| 77 | mipLevelCount); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 78 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 79 | SkAutoSTMalloc<14, GrMipLevel> tmpTexels; |
Brian Salomon | c9d81f7 | 2019-07-03 07:52:41 -0400 | [diff] [blame] | 80 | SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas; |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 81 | GrColorType tempColorType = GrColorType::kUnknown; |
| 82 | if (hasPixels) { |
| 83 | tempColorType = this->prepareLevels(format, colorType, {desc.fWidth, desc.fHeight}, texels, |
| 84 | mipLevelCount, &tmpTexels, &tmpDatas); |
| 85 | if (tempColorType == GrColorType::kUnknown) { |
| 86 | return nullptr; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 89 | return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 90 | isProtected, colorType, tempColorType, tmpTexels.get(), |
| 91 | mipLevelCount); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 94 | sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 95 | const GrBackendFormat& format, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 96 | GrRenderable renderable, |
| 97 | int renderTargetSampleCnt, |
| 98 | SkBudgeted budgeted, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 99 | GrMipMapped mipMapped, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 100 | GrProtected isProtected) { |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 101 | sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 102 | mipMapped, isProtected)); |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 103 | if (tex && SkBudgeted::kNo == budgeted) { |
| 104 | tex->resourcePriv().makeUnbudgeted(); |
| 105 | } |
| 106 | |
| 107 | return tex; |
| 108 | } |
| 109 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 110 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 111 | const GrBackendFormat& format, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 112 | GrColorType colorType, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 113 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 114 | int renderTargetSampleCnt, |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 115 | SkBudgeted budgeted, |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 116 | SkBackingFit fit, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 117 | GrProtected isProtected, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 118 | const GrMipLevel& mipLevel) { |
Robert Phillips | 774831a | 2017-04-20 10:19:33 -0400 | [diff] [blame] | 119 | ASSERT_SINGLE_OWNER |
| 120 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 121 | if (!mipLevel.fPixels) { |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 125 | if (SkBackingFit::kApprox == fit) { |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 126 | if (this->isAbandoned()) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, |
| 130 | renderable, renderTargetSampleCnt, GrMipMapped::kNo)) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
| 134 | auto tex = this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt, |
| 135 | isProtected); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 136 | if (!tex) { |
| 137 | return nullptr; |
| 138 | } |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 139 | return this->writePixels(std::move(tex), colorType, {desc.fWidth, desc.fHeight}, &mipLevel, |
| 140 | 1); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 141 | } else { |
| 142 | return this->createTexture(desc, format, colorType, renderable, renderTargetSampleCnt, |
| 143 | budgeted, isProtected, &mipLevel, 1); |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame] | 144 | } |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 147 | sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(int width, int height, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 148 | const GrBackendFormat& format, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 149 | SkImage::CompressionType compression, |
| 150 | SkBudgeted budgeted, SkData* data) { |
| 151 | ASSERT_SINGLE_OWNER |
| 152 | if (this->isAbandoned()) { |
| 153 | return nullptr; |
| 154 | } |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 155 | return fGpu->createCompressedTexture(width, height, format, compression, budgeted, data->data(), |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 156 | data->size()); |
| 157 | } |
| 158 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 159 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 160 | const GrBackendFormat& format, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 161 | GrRenderable renderable, |
| 162 | int renderTargetSampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 163 | GrMipMapped mipMapped, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 164 | SkBudgeted budgeted, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 165 | GrProtected isProtected) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 166 | ASSERT_SINGLE_OWNER |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 167 | if (this->isAbandoned()) { |
| 168 | return nullptr; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 169 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 170 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 171 | if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 172 | renderTargetSampleCnt, mipMapped)) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 173 | return nullptr; |
| 174 | } |
| 175 | |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 176 | // Currently we don't recycle compressed textures as scratch. Additionally all compressed |
| 177 | // textures should be created through the createCompressedTexture function. |
| 178 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
| 179 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 180 | // TODO: Support GrMipMapped::kYes in scratch texture lookup here. |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 181 | sk_sp<GrTexture> tex = this->getExactScratch( |
| 182 | desc, format, renderable, renderTargetSampleCnt, budgeted, mipMapped, isProtected); |
| 183 | if (tex) { |
| 184 | return tex; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 187 | return fGpu->createTexture(desc, format, renderable, renderTargetSampleCnt, mipMapped, budgeted, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 188 | isProtected); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 191 | // Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to |
| 192 | // the next power of 2. Those above 'kMagicTol' will only go up half the floor power of 2. |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 193 | SkISize GrResourceProvider::MakeApprox(SkISize dimensions) { |
| 194 | auto adjust = [](int value) { |
| 195 | static const int kMagicTol = 1024; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 196 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 197 | value = SkTMax(kMinScratchTextureSize, value); |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 198 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 199 | if (SkIsPow2(value)) { |
| 200 | return value; |
| 201 | } |
| 202 | |
| 203 | int ceilPow2 = SkNextPow2(value); |
| 204 | if (value <= kMagicTol) { |
| 205 | return ceilPow2; |
| 206 | } |
| 207 | |
| 208 | int floorPow2 = ceilPow2 >> 1; |
| 209 | int mid = floorPow2 + (floorPow2 >> 1); |
| 210 | |
| 211 | if (value <= mid) { |
| 212 | return mid; |
| 213 | } |
Brian Salomon | 322301a | 2019-10-24 12:50:50 -0400 | [diff] [blame] | 214 | return ceilPow2; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 215 | }; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 216 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 217 | return {adjust(dimensions.width()), adjust(dimensions.height())}; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 220 | sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 221 | const GrBackendFormat& format, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 222 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 223 | int renderTargetSampleCnt, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 224 | GrProtected isProtected) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 225 | ASSERT_SINGLE_OWNER |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 226 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 227 | if (this->isAbandoned()) { |
| 228 | return nullptr; |
| 229 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 230 | |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 231 | // Currently we don't recycle compressed textures as scratch. Additionally all compressed |
| 232 | // textures should be created through the createCompressedTexture function. |
| 233 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 234 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 235 | if (!fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable, |
| 236 | renderTargetSampleCnt, GrMipMapped::kNo)) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 237 | return nullptr; |
| 238 | } |
| 239 | |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 240 | // bin by some multiple or power of 2 with a reasonable min |
Michael Ludwig | bd2f070 | 2019-09-13 15:29:41 -0400 | [diff] [blame] | 241 | GrSurfaceDesc copyDesc(desc); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 242 | auto size = MakeApprox({desc.fWidth, desc.fHeight}); |
| 243 | copyDesc.fWidth = size.width(); |
| 244 | copyDesc.fHeight = size.height(); |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 245 | |
Michael Ludwig | bd2f070 | 2019-09-13 15:29:41 -0400 | [diff] [blame] | 246 | if (auto tex = this->refScratchTexture(copyDesc, format, renderable, renderTargetSampleCnt, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 247 | GrMipMapped::kNo, isProtected)) { |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 248 | return tex; |
| 249 | } |
| 250 | |
Michael Ludwig | bd2f070 | 2019-09-13 15:29:41 -0400 | [diff] [blame] | 251 | return fGpu->createTexture(copyDesc, format, renderable, renderTargetSampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 252 | GrMipMapped::kNo, SkBudgeted::kYes, isProtected); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 255 | sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 256 | const GrBackendFormat& format, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 257 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 258 | int renderTargetSampleCnt, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 259 | GrMipMapped mipMapped, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 260 | GrProtected isProtected) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 261 | ASSERT_SINGLE_OWNER |
| 262 | SkASSERT(!this->isAbandoned()); |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 263 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 264 | SkASSERT(fCaps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, |
| 265 | renderable, renderTargetSampleCnt, GrMipMapped::kNo)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 266 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 267 | // We could make initial clears work with scratch textures but it is a rare case so we just opt |
| 268 | // to fall back to making a new texture. |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 269 | if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 270 | GrScratchKey key; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 271 | GrTexturePriv::ComputeScratchKey(desc.fConfig, {desc.fWidth, desc.fHeight}, renderable, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 272 | renderTargetSampleCnt, mipMapped, isProtected, &key); |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 273 | GrGpuResource* resource = fCache->findAndRefScratchResource(key); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 274 | if (resource) { |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 275 | fGpu->stats()->incNumScratchTexturesReused(); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 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, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 285 | GrColorType colorType, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 286 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 287 | GrWrapCacheable cacheable, |
| 288 | GrIOType ioType) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 289 | ASSERT_SINGLE_OWNER |
| 290 | if (this->isAbandoned()) { |
| 291 | return nullptr; |
| 292 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 293 | return fGpu->wrapBackendTexture(tex, colorType, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 297 | int sampleCnt, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 298 | GrColorType colorType, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 299 | GrWrapOwnership ownership, |
| 300 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 301 | ASSERT_SINGLE_OWNER |
| 302 | if (this->isAbandoned()) { |
| 303 | return nullptr; |
| 304 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 305 | return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, colorType, ownership, cacheable); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget( |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 309 | const GrBackendRenderTarget& backendRT, GrColorType colorType) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 310 | { |
| 311 | ASSERT_SINGLE_OWNER |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 312 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, colorType); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 315 | sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget( |
| 316 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) { |
| 317 | ASSERT_SINGLE_OWNER |
| 318 | return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, |
| 319 | vkInfo); |
| 320 | |
| 321 | } |
| 322 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 323 | void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key, |
| 324 | GrGpuResource* resource) { |
| 325 | ASSERT_SINGLE_OWNER |
| 326 | if (this->isAbandoned() || !resource) { |
| 327 | return; |
| 328 | } |
| 329 | resource->resourcePriv().setUniqueKey(key); |
| 330 | } |
| 331 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 332 | sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 333 | ASSERT_SINGLE_OWNER |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 334 | return this->isAbandoned() ? nullptr |
| 335 | : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 338 | sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType, |
| 339 | size_t size, |
| 340 | const void* data, |
| 341 | const GrUniqueKey& key) { |
| 342 | if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) { |
Brian Salomon | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 343 | return buffer; |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 344 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 345 | if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 346 | // We shouldn't bin and/or cache static buffers. |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 347 | SkASSERT(buffer->size() == size); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 348 | SkASSERT(!buffer->resourcePriv().getScratchKey().isValid()); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 349 | buffer->resourcePriv().setUniqueKey(key); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 350 | return sk_sp<const GrGpuBuffer>(buffer); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 351 | } |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 355 | sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern, |
| 356 | int patternSize, |
| 357 | int reps, |
| 358 | int vertCount, |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 359 | const GrUniqueKey* key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 360 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 361 | |
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 | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 391 | return buffer; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 394 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 395 | static constexpr int kMaxNumNonAAQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 396 | static const int kVertsPerNonAAQuad = 4; |
| 397 | static const int kIndicesPerNonAAQuad = 6; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 398 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 399 | sk_sp<const GrGpuBuffer> GrResourceProvider::createNonAAQuadIndexBuffer() { |
| 400 | GR_STATIC_ASSERT(kVertsPerNonAAQuad * kMaxNumNonAAQuads <= 65535); // indices fit in a uint16_t |
| 401 | |
| 402 | static const uint16_t kNonAAQuadIndexPattern[] = { |
| 403 | 0, 1, 2, 2, 1, 3 |
| 404 | }; |
| 405 | |
| 406 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kNonAAQuadIndexPattern) == kIndicesPerNonAAQuad); |
| 407 | |
| 408 | return this->createPatternedIndexBuffer(kNonAAQuadIndexPattern, kIndicesPerNonAAQuad, |
| 409 | kMaxNumNonAAQuads, kVertsPerNonAAQuad, nullptr); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 412 | int GrResourceProvider::MaxNumNonAAQuads() { return kMaxNumNonAAQuads; } |
| 413 | int GrResourceProvider::NumVertsPerNonAAQuad() { return kVertsPerNonAAQuad; } |
| 414 | int GrResourceProvider::NumIndicesPerNonAAQuad() { return kIndicesPerNonAAQuad; } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 415 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 416 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 8bc157e | 2019-11-15 12:04:48 -0500 | [diff] [blame^] | 417 | static constexpr int kMaxNumAAQuads = 1 << 9; // max possible: (1 << 13) - 1; |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 418 | static const int kVertsPerAAQuad = 8; |
| 419 | static const int kIndicesPerAAQuad = 30; |
| 420 | |
| 421 | sk_sp<const GrGpuBuffer> GrResourceProvider::createAAQuadIndexBuffer() { |
| 422 | GR_STATIC_ASSERT(kVertsPerAAQuad * kMaxNumAAQuads <= 65535); // indices fit in a uint16_t |
| 423 | |
| 424 | // clang-format off |
| 425 | static const uint16_t kAAQuadIndexPattern[] = { |
| 426 | 0, 1, 2, 1, 3, 2, |
| 427 | 0, 4, 1, 4, 5, 1, |
| 428 | 0, 6, 4, 0, 2, 6, |
| 429 | 2, 3, 6, 3, 7, 6, |
| 430 | 1, 5, 3, 3, 5, 7, |
| 431 | }; |
| 432 | // clang-format on |
| 433 | |
| 434 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAAQuadIndexPattern) == kIndicesPerAAQuad); |
| 435 | |
| 436 | return this->createPatternedIndexBuffer(kAAQuadIndexPattern, kIndicesPerAAQuad, |
| 437 | kMaxNumAAQuads, kVertsPerAAQuad, nullptr); |
| 438 | } |
| 439 | |
| 440 | int GrResourceProvider::MaxNumAAQuads() { return kMaxNumAAQuads; } |
| 441 | int GrResourceProvider::NumVertsPerAAQuad() { return kVertsPerAAQuad; } |
| 442 | int GrResourceProvider::NumIndicesPerAAQuad() { return kIndicesPerAAQuad; } |
| 443 | |
| 444 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 445 | sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) { |
Robert Phillips | 0f17181 | 2017-09-21 14:25:31 -0400 | [diff] [blame] | 446 | if (this->isAbandoned()) { |
| 447 | return nullptr; |
| 448 | } |
| 449 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 450 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 451 | return this->gpu()->pathRendering()->createPath(path, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 454 | sk_sp<GrGpuBuffer> GrResourceProvider::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 455 | GrAccessPattern accessPattern, |
| 456 | const void* data) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 457 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 458 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 459 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 460 | if (kDynamic_GrAccessPattern != accessPattern) { |
| 461 | return this->gpu()->createBuffer(size, intendedType, accessPattern, data); |
| 462 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 463 | // bin by pow2 with a reasonable min |
Robert Phillips | 9e38047 | 2016-10-28 12:15:03 -0400 | [diff] [blame] | 464 | static const size_t MIN_SIZE = 1 << 12; |
| 465 | size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size)); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 466 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 467 | GrScratchKey key; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 468 | GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key); |
| 469 | auto buffer = |
| 470 | sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource( |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 471 | key))); |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 472 | if (!buffer) { |
| 473 | buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern); |
| 474 | if (!buffer) { |
| 475 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 478 | if (data) { |
| 479 | buffer->updateData(data, size); |
| 480 | } |
| 481 | return buffer; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 484 | bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 485 | SkASSERT(rt); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 486 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 487 | if (stencil && stencil->numSamples() == numStencilSamples) { |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 488 | return true; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) { |
| 492 | GrUniqueKey sbKey; |
| 493 | |
| 494 | int width = rt->width(); |
| 495 | int height = rt->height(); |
| 496 | #if 0 |
| 497 | if (this->caps()->oversizedStencilSupport()) { |
| 498 | width = SkNextPow2(width); |
| 499 | height = SkNextPow2(height); |
| 500 | } |
| 501 | #endif |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 502 | GrStencilAttachment::ComputeSharedStencilAttachmentKey( |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 503 | width, height, numStencilSamples, &sbKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 504 | auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 505 | if (!stencil) { |
| 506 | // Need to try and create a new stencil |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 507 | stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget( |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 508 | rt, width, height, numStencilSamples)); |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 509 | if (!stencil) { |
| 510 | return false; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 511 | } |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 512 | this->assignUniqueKeyToResource(sbKey, stencil.get()); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 513 | } |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 514 | rt->renderTargetPriv().attachStencilAttachment(std::move(stencil)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 515 | } |
Chris Dalton | 215ff33 | 2019-07-02 09:38:22 -0600 | [diff] [blame] | 516 | |
| 517 | if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) { |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 518 | return stencil->numSamples() == numStencilSamples; |
Chris Dalton | 215ff33 | 2019-07-02 09:38:22 -0600 | [diff] [blame] | 519 | } |
| 520 | return false; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 521 | } |
| 522 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 523 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget( |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 524 | const GrBackendTexture& tex, int sampleCnt, GrColorType colorType) |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 525 | { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 526 | if (this->isAbandoned()) { |
| 527 | return nullptr; |
| 528 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 529 | return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt, colorType); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 530 | } |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 531 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 532 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) { |
| 533 | return fGpu->makeSemaphore(isOwned); |
| 534 | } |
| 535 | |
| 536 | sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 537 | SemaphoreWrapType wrapType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 538 | GrWrapOwnership ownership) { |
| 539 | ASSERT_SINGLE_OWNER |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 540 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, |
| 541 | wrapType, |
| 542 | ownership); |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 543 | } |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 544 | |
| 545 | // Ensures the row bytes are populated (not 0) and makes a copy to a temporary |
| 546 | // to make the row bytes tight if necessary. Returns false if the input row bytes are invalid. |
| 547 | static bool prepare_level(const GrMipLevel& inLevel, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 548 | const SkISize& dimensions, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 549 | bool rowBytesSupport, |
| 550 | GrColorType origColorType, |
| 551 | GrColorType allowedColorType, |
| 552 | GrMipLevel* outLevel, |
| 553 | std::unique_ptr<char[]>* data) { |
| 554 | if (!inLevel.fPixels) { |
| 555 | outLevel->fPixels = nullptr; |
| 556 | outLevel->fRowBytes = 0; |
| 557 | return true; |
| 558 | } |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 559 | size_t minRB = dimensions.fWidth * GrColorTypeBytesPerPixel(origColorType); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 560 | size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB; |
| 561 | if (actualRB < minRB) { |
| 562 | return false; |
| 563 | } |
| 564 | if (origColorType == allowedColorType && (actualRB == minRB || rowBytesSupport)) { |
| 565 | outLevel->fRowBytes = actualRB; |
| 566 | outLevel->fPixels = inLevel.fPixels; |
| 567 | return true; |
| 568 | } |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 569 | auto tempRB = dimensions.fWidth * GrColorTypeBytesPerPixel(allowedColorType); |
| 570 | data->reset(new char[tempRB * dimensions.fHeight]); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 571 | outLevel->fPixels = data->get(); |
| 572 | outLevel->fRowBytes = tempRB; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 573 | GrImageInfo srcInfo(origColorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
| 574 | GrImageInfo dstInfo(allowedColorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 575 | return GrConvertPixels(dstInfo, data->get(), tempRB, srcInfo, inLevel.fPixels, actualRB); |
| 576 | } |
| 577 | |
| 578 | GrColorType GrResourceProvider::prepareLevels(const GrBackendFormat& format, |
| 579 | GrColorType colorType, |
| 580 | const SkISize& baseSize, |
| 581 | const GrMipLevel texels[], |
| 582 | int mipLevelCount, |
| 583 | TempLevels* tempLevels, |
| 584 | TempLevelDatas* tempLevelDatas) const { |
| 585 | SkASSERT(mipLevelCount && texels && texels[0].fPixels); |
| 586 | |
| 587 | auto allowedColorType = |
| 588 | this->caps()->supportedWritePixelsColorType(colorType, format, colorType).fColorType; |
| 589 | if (allowedColorType == GrColorType::kUnknown) { |
| 590 | return GrColorType::kUnknown; |
| 591 | } |
| 592 | bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport(); |
| 593 | tempLevels->reset(mipLevelCount); |
| 594 | tempLevelDatas->reset(mipLevelCount); |
| 595 | auto size = baseSize; |
| 596 | for (int i = 0; i < mipLevelCount; ++i) { |
| 597 | if (!prepare_level(texels[i], size, rowBytesSupport, colorType, allowedColorType, |
| 598 | &(*tempLevels)[i], &(*tempLevelDatas)[i])) { |
| 599 | return GrColorType::kUnknown; |
| 600 | } |
| 601 | size = {std::max(size.fWidth / 2, 1), std::max(size.fHeight / 2, 1)}; |
| 602 | } |
| 603 | return allowedColorType; |
| 604 | } |
| 605 | |
| 606 | sk_sp<GrTexture> GrResourceProvider::writePixels(sk_sp<GrTexture> texture, |
| 607 | GrColorType colorType, |
| 608 | const SkISize& baseSize, |
| 609 | const GrMipLevel texels[], |
| 610 | int mipLevelCount) const { |
| 611 | SkASSERT(!this->isAbandoned()); |
| 612 | SkASSERT(texture); |
| 613 | SkASSERT(colorType != GrColorType::kUnknown); |
| 614 | SkASSERT(mipLevelCount && texels && texels[0].fPixels); |
| 615 | |
| 616 | SkAutoSTMalloc<14, GrMipLevel> tmpTexels; |
| 617 | SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas; |
| 618 | auto tempColorType = this->prepareLevels(texture->backendFormat(), colorType, baseSize, texels, |
| 619 | mipLevelCount, &tmpTexels, &tmpDatas); |
| 620 | if (tempColorType == GrColorType::kUnknown) { |
| 621 | return nullptr; |
| 622 | } |
| 623 | SkAssertResult(fGpu->writePixels(texture.get(), 0, 0, baseSize.fWidth, baseSize.fHeight, |
| 624 | colorType, tempColorType, tmpTexels.get(), mipLevelCount)); |
| 625 | return texture; |
| 626 | } |