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" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame^] | 18 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrGpu.h" |
| 20 | #include "src/gpu/GrGpuBuffer.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrPath.h" |
| 23 | #include "src/gpu/GrPathRendering.h" |
| 24 | #include "src/gpu/GrProxyProvider.h" |
| 25 | #include "src/gpu/GrRenderTargetPriv.h" |
| 26 | #include "src/gpu/GrResourceCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrSemaphore.h" |
| 28 | #include "src/gpu/GrStencilAttachment.h" |
| 29 | #include "src/gpu/GrTexturePriv.h" |
| 30 | #include "src/gpu/SkGr.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 31 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 32 | const int GrResourceProvider::kMinScratchTextureSize = 16; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 33 | |
| 34 | #define ASSERT_SINGLE_OWNER \ |
| 35 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
| 36 | |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 37 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 38 | : fCache(cache) |
| 39 | , fGpu(gpu) |
| 40 | #ifdef SK_DEBUG |
| 41 | , fSingleOwner(owner) |
| 42 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 43 | { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 44 | fCaps = sk_ref_sp(fGpu->caps()); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 47 | sk_sp<GrTexture> GrResourceProvider::createTexture(SkISize dimensions, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 48 | const GrBackendFormat& format, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 49 | GrColorType colorType, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 50 | GrRenderable renderable, |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 51 | int renderTargetSampleCnt, |
| 52 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 53 | GrProtected isProtected, |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 54 | const GrMipLevel texels[], |
| 55 | int mipLevelCount) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 56 | ASSERT_SINGLE_OWNER |
| 57 | |
Robert Phillips | 7f1b4f8 | 2017-11-28 07:38:39 -0500 | [diff] [blame] | 58 | SkASSERT(mipLevelCount > 0); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 59 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 60 | if (this->isAbandoned()) { |
| 61 | return nullptr; |
| 62 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 63 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 64 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 65 | if (!fCaps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 66 | mipMapped)) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 67 | return nullptr; |
| 68 | } |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 69 | // Current rule is that you can provide no level data, just the base, or all the levels. |
| 70 | bool hasPixels = mipLevelCount && texels[0].fPixels; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 71 | auto scratch = this->getExactScratch(dimensions, format, renderable, renderTargetSampleCnt, |
| 72 | budgeted, mipMapped, isProtected); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 73 | if (scratch) { |
| 74 | if (!hasPixels) { |
| 75 | return scratch; |
| 76 | } |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 77 | return this->writePixels(std::move(scratch), colorType, dimensions, texels, 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) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 83 | tempColorType = this->prepareLevels(format, colorType, dimensions, texels, mipLevelCount, |
| 84 | &tmpTexels, &tmpDatas); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 85 | if (tempColorType == GrColorType::kUnknown) { |
| 86 | return nullptr; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 89 | return fGpu->createTexture(dimensions, 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 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 94 | sk_sp<GrTexture> GrResourceProvider::getExactScratch(SkISize dimensions, |
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 | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 101 | sk_sp<GrTexture> tex(this->refScratchTexture(dimensions, format, renderable, |
| 102 | renderTargetSampleCnt, 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 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 110 | sk_sp<GrTexture> GrResourceProvider::createTexture(SkISize dimensions, |
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 | } |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 129 | if (!fCaps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 130 | GrMipMapped::kNo)) { |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 131 | return nullptr; |
| 132 | } |
| 133 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 134 | auto tex = this->createApproxTexture(dimensions, format, renderable, renderTargetSampleCnt, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 135 | isProtected); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 136 | if (!tex) { |
| 137 | return nullptr; |
| 138 | } |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 139 | return this->writePixels(std::move(tex), colorType, dimensions, &mipLevel, 1); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 140 | } else { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 141 | return this->createTexture(dimensions, format, colorType, renderable, renderTargetSampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 142 | budgeted, isProtected, &mipLevel, 1); |
Brian Salomon | a3ffaab | 2019-07-09 12:26:46 -0400 | [diff] [blame] | 143 | } |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Robert Phillips | 9f744f7 | 2019-12-19 19:14:33 -0500 | [diff] [blame] | 146 | sk_sp<GrTexture> GrResourceProvider::createCompressedTexture(SkISize dimensions, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 147 | const GrBackendFormat& format, |
Robert Phillips | e4720c6 | 2020-01-14 14:33:24 -0500 | [diff] [blame] | 148 | SkBudgeted budgeted, |
| 149 | GrMipMapped mipMapped, |
Robert Phillips | 3a83392 | 2020-01-21 15:25:58 -0500 | [diff] [blame] | 150 | GrProtected isProtected, |
Robert Phillips | e4720c6 | 2020-01-14 14:33:24 -0500 | [diff] [blame] | 151 | SkData* data) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 152 | ASSERT_SINGLE_OWNER |
| 153 | if (this->isAbandoned()) { |
| 154 | return nullptr; |
| 155 | } |
Robert Phillips | e4720c6 | 2020-01-14 14:33:24 -0500 | [diff] [blame] | 156 | return fGpu->createCompressedTexture(dimensions, format, budgeted, mipMapped, |
Robert Phillips | 3a83392 | 2020-01-21 15:25:58 -0500 | [diff] [blame] | 157 | isProtected, data->data(), data->size()); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 160 | sk_sp<GrTexture> GrResourceProvider::createTexture(SkISize dimensions, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 161 | const GrBackendFormat& format, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 162 | GrRenderable renderable, |
| 163 | int renderTargetSampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 164 | GrMipMapped mipMapped, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 165 | SkBudgeted budgeted, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 166 | GrProtected isProtected) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 167 | ASSERT_SINGLE_OWNER |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 168 | if (this->isAbandoned()) { |
| 169 | return nullptr; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 170 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 171 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 172 | if (!fCaps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 173 | mipMapped)) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 174 | return nullptr; |
| 175 | } |
| 176 | |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 177 | // Currently we don't recycle compressed textures as scratch. Additionally all compressed |
| 178 | // textures should be created through the createCompressedTexture function. |
| 179 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
| 180 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 181 | // TODO: Support GrMipMapped::kYes in scratch texture lookup here. |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 182 | sk_sp<GrTexture> tex = |
| 183 | this->getExactScratch(dimensions, format, renderable, renderTargetSampleCnt, budgeted, |
| 184 | mipMapped, isProtected); |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 185 | if (tex) { |
| 186 | return tex; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 189 | return fGpu->createTexture(dimensions, format, renderable, renderTargetSampleCnt, mipMapped, |
| 190 | budgeted, isProtected); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 193 | // Map 'value' to a larger multiple of 2. Values <= 'kMagicTol' will pop up to |
| 194 | // 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] | 195 | SkISize GrResourceProvider::MakeApprox(SkISize dimensions) { |
| 196 | auto adjust = [](int value) { |
| 197 | static const int kMagicTol = 1024; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 198 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 199 | value = std::max(kMinScratchTextureSize, value); |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 200 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 201 | if (SkIsPow2(value)) { |
| 202 | return value; |
| 203 | } |
| 204 | |
| 205 | int ceilPow2 = SkNextPow2(value); |
| 206 | if (value <= kMagicTol) { |
| 207 | return ceilPow2; |
| 208 | } |
| 209 | |
| 210 | int floorPow2 = ceilPow2 >> 1; |
| 211 | int mid = floorPow2 + (floorPow2 >> 1); |
| 212 | |
| 213 | if (value <= mid) { |
| 214 | return mid; |
| 215 | } |
Brian Salomon | 322301a | 2019-10-24 12:50:50 -0400 | [diff] [blame] | 216 | return ceilPow2; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 217 | }; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 218 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 219 | return {adjust(dimensions.width()), adjust(dimensions.height())}; |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 222 | sk_sp<GrTexture> GrResourceProvider::createApproxTexture(SkISize dimensions, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 223 | const GrBackendFormat& format, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 224 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 225 | int renderTargetSampleCnt, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 226 | GrProtected isProtected) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 227 | ASSERT_SINGLE_OWNER |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 228 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 229 | if (this->isAbandoned()) { |
| 230 | return nullptr; |
| 231 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 232 | |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 233 | // Currently we don't recycle compressed textures as scratch. Additionally all compressed |
| 234 | // textures should be created through the createCompressedTexture function. |
| 235 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 236 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 237 | if (!fCaps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 238 | GrMipMapped::kNo)) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 239 | return nullptr; |
| 240 | } |
| 241 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 242 | auto copyDimensions = MakeApprox(dimensions); |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 243 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 244 | if (auto tex = this->refScratchTexture(copyDimensions, format, renderable, |
| 245 | renderTargetSampleCnt, GrMipMapped::kNo, isProtected)) { |
Greg Daniel | 29bf84f | 2017-09-25 12:25:12 -0400 | [diff] [blame] | 246 | return tex; |
| 247 | } |
| 248 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 249 | return fGpu->createTexture(copyDimensions, format, renderable, renderTargetSampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 250 | GrMipMapped::kNo, SkBudgeted::kYes, isProtected); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 253 | sk_sp<GrTexture> GrResourceProvider::refScratchTexture(SkISize dimensions, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 254 | const GrBackendFormat& format, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 255 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 256 | int renderTargetSampleCnt, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 257 | GrMipMapped mipMapped, |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 258 | GrProtected isProtected) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 259 | ASSERT_SINGLE_OWNER |
| 260 | SkASSERT(!this->isAbandoned()); |
Greg Daniel | c3a927f | 2019-10-16 12:03:50 -0400 | [diff] [blame] | 261 | SkASSERT(!this->caps()->isFormatCompressed(format)); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 262 | SkASSERT(fCaps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 263 | GrMipMapped::kNo)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 264 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 265 | // We could make initial clears work with scratch textures but it is a rare case so we just opt |
| 266 | // to fall back to making a new texture. |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 267 | if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 268 | GrScratchKey key; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 269 | GrTexturePriv::ComputeScratchKey(*this->caps(), format, dimensions, renderable, |
| 270 | renderTargetSampleCnt, mipMapped, isProtected, &key); |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 271 | GrGpuResource* resource = fCache->findAndRefScratchResource(key); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 272 | if (resource) { |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 273 | fGpu->stats()->incNumScratchTexturesReused(); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 274 | GrSurface* surface = static_cast<GrSurface*>(resource); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 275 | return sk_sp<GrTexture>(surface->asTexture()); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 279 | return nullptr; |
| 280 | } |
| 281 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 282 | sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 283 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 284 | GrWrapCacheable cacheable, |
| 285 | GrIOType ioType) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 286 | ASSERT_SINGLE_OWNER |
| 287 | if (this->isAbandoned()) { |
| 288 | return nullptr; |
| 289 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 290 | return fGpu->wrapBackendTexture(tex, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Robert Phillips | ead321b | 2019-12-19 10:16:32 -0500 | [diff] [blame] | 293 | sk_sp<GrTexture> GrResourceProvider::wrapCompressedBackendTexture(const GrBackendTexture& tex, |
| 294 | GrWrapOwnership ownership, |
| 295 | GrWrapCacheable cacheable) { |
| 296 | ASSERT_SINGLE_OWNER |
| 297 | if (this->isAbandoned()) { |
| 298 | return nullptr; |
| 299 | } |
| 300 | |
| 301 | return fGpu->wrapCompressedBackendTexture(tex, ownership, cacheable); |
| 302 | } |
| 303 | |
| 304 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 305 | sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 306 | int sampleCnt, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 307 | GrWrapOwnership ownership, |
| 308 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 309 | ASSERT_SINGLE_OWNER |
| 310 | if (this->isAbandoned()) { |
| 311 | return nullptr; |
| 312 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 313 | return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership, cacheable); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 317 | const GrBackendRenderTarget& backendRT) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 318 | ASSERT_SINGLE_OWNER |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 319 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 322 | sk_sp<GrRenderTarget> GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget( |
| 323 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) { |
| 324 | ASSERT_SINGLE_OWNER |
| 325 | return this->isAbandoned() ? nullptr : fGpu->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, |
| 326 | vkInfo); |
| 327 | |
| 328 | } |
| 329 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 330 | void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key, |
| 331 | GrGpuResource* resource) { |
| 332 | ASSERT_SINGLE_OWNER |
| 333 | if (this->isAbandoned() || !resource) { |
| 334 | return; |
| 335 | } |
| 336 | resource->resourcePriv().setUniqueKey(key); |
| 337 | } |
| 338 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 339 | sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 340 | ASSERT_SINGLE_OWNER |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 341 | return this->isAbandoned() ? nullptr |
| 342 | : sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 343 | } |
| 344 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 345 | sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrGpuBufferType intendedType, |
| 346 | size_t size, |
| 347 | const void* data, |
| 348 | const GrUniqueKey& key) { |
| 349 | if (auto buffer = this->findByUniqueKey<GrGpuBuffer>(key)) { |
Mike Klein | a9609ea | 2020-02-18 13:33:23 -0600 | [diff] [blame] | 350 | return std::move(buffer); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 351 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 352 | if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, data)) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 353 | // We shouldn't bin and/or cache static buffers. |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 354 | SkASSERT(buffer->size() == size); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 355 | SkASSERT(!buffer->resourcePriv().getScratchKey().isValid()); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 356 | buffer->resourcePriv().setUniqueKey(key); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 357 | return sk_sp<const GrGpuBuffer>(buffer); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 358 | } |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 362 | sk_sp<const GrGpuBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern, |
| 363 | int patternSize, |
| 364 | int reps, |
| 365 | int vertCount, |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 366 | const GrUniqueKey* key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 367 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 368 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 369 | sk_sp<GrGpuBuffer> buffer( |
| 370 | this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern)); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 371 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 372 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 373 | } |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 374 | uint16_t* data = (uint16_t*) buffer->map(); |
| 375 | SkAutoTArray<uint16_t> temp; |
| 376 | if (!data) { |
| 377 | temp.reset(reps * patternSize); |
| 378 | data = temp.get(); |
| 379 | } |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 380 | for (int i = 0; i < reps; ++i) { |
| 381 | int baseIdx = i * patternSize; |
| 382 | uint16_t baseVert = (uint16_t)(i * vertCount); |
| 383 | for (int j = 0; j < patternSize; ++j) { |
| 384 | data[baseIdx+j] = baseVert + pattern[j]; |
| 385 | } |
| 386 | } |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 387 | if (temp.get()) { |
| 388 | if (!buffer->updateData(data, bufferSize)) { |
| 389 | return nullptr; |
| 390 | } |
| 391 | } else { |
| 392 | buffer->unmap(); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 393 | } |
Brian Salomon | a29dd9d | 2019-02-07 13:27:18 -0500 | [diff] [blame] | 394 | if (key) { |
| 395 | SkASSERT(key->isValid()); |
| 396 | this->assignUniqueKeyToResource(*key, buffer.get()); |
| 397 | } |
Mike Klein | a9609ea | 2020-02-18 13:33:23 -0600 | [diff] [blame] | 398 | return std::move(buffer); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 401 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 402 | static constexpr int kMaxNumNonAAQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 403 | static const int kVertsPerNonAAQuad = 4; |
| 404 | static const int kIndicesPerNonAAQuad = 6; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 405 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 406 | sk_sp<const GrGpuBuffer> GrResourceProvider::createNonAAQuadIndexBuffer() { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 407 | static_assert(kVertsPerNonAAQuad * kMaxNumNonAAQuads <= 65535); // indices fit in a uint16_t |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 408 | |
| 409 | static const uint16_t kNonAAQuadIndexPattern[] = { |
| 410 | 0, 1, 2, 2, 1, 3 |
| 411 | }; |
| 412 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 413 | static_assert(SK_ARRAY_COUNT(kNonAAQuadIndexPattern) == kIndicesPerNonAAQuad); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 414 | |
| 415 | return this->createPatternedIndexBuffer(kNonAAQuadIndexPattern, kIndicesPerNonAAQuad, |
| 416 | kMaxNumNonAAQuads, kVertsPerNonAAQuad, nullptr); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 419 | int GrResourceProvider::MaxNumNonAAQuads() { return kMaxNumNonAAQuads; } |
| 420 | int GrResourceProvider::NumVertsPerNonAAQuad() { return kVertsPerNonAAQuad; } |
| 421 | int GrResourceProvider::NumIndicesPerNonAAQuad() { return kIndicesPerNonAAQuad; } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 422 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 423 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 8bc157e | 2019-11-15 12:04:48 -0500 | [diff] [blame] | 424 | static constexpr int kMaxNumAAQuads = 1 << 9; // max possible: (1 << 13) - 1; |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 425 | static const int kVertsPerAAQuad = 8; |
| 426 | static const int kIndicesPerAAQuad = 30; |
| 427 | |
| 428 | sk_sp<const GrGpuBuffer> GrResourceProvider::createAAQuadIndexBuffer() { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 429 | static_assert(kVertsPerAAQuad * kMaxNumAAQuads <= 65535); // indices fit in a uint16_t |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 430 | |
| 431 | // clang-format off |
| 432 | static const uint16_t kAAQuadIndexPattern[] = { |
| 433 | 0, 1, 2, 1, 3, 2, |
| 434 | 0, 4, 1, 4, 5, 1, |
| 435 | 0, 6, 4, 0, 2, 6, |
| 436 | 2, 3, 6, 3, 7, 6, |
| 437 | 1, 5, 3, 3, 5, 7, |
| 438 | }; |
| 439 | // clang-format on |
| 440 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 441 | static_assert(SK_ARRAY_COUNT(kAAQuadIndexPattern) == kIndicesPerAAQuad); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 442 | |
| 443 | return this->createPatternedIndexBuffer(kAAQuadIndexPattern, kIndicesPerAAQuad, |
| 444 | kMaxNumAAQuads, kVertsPerAAQuad, nullptr); |
| 445 | } |
| 446 | |
| 447 | int GrResourceProvider::MaxNumAAQuads() { return kMaxNumAAQuads; } |
| 448 | int GrResourceProvider::NumVertsPerAAQuad() { return kVertsPerAAQuad; } |
| 449 | int GrResourceProvider::NumIndicesPerAAQuad() { return kIndicesPerAAQuad; } |
| 450 | |
| 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; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 472 | size_t allocSize = std::max(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 | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 491 | bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples) { |
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(); |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 494 | if (stencil && stencil->numSamples() == numStencilSamples) { |
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( |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 510 | width, height, numStencilSamples, &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( |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 515 | rt, width, height, numStencilSamples)); |
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()) { |
Chris Dalton | f00b95b | 2019-11-07 21:14:41 -0700 | [diff] [blame] | 525 | return stencil->numSamples() == numStencilSamples; |
Chris Dalton | 215ff33 | 2019-07-02 09:38:22 -0600 | [diff] [blame] | 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( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 531 | const GrBackendTexture& tex, int sampleCnt) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 532 | if (this->isAbandoned()) { |
| 533 | return nullptr; |
| 534 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 535 | return fGpu->wrapBackendTextureAsRenderTarget(tex, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 536 | } |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 537 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 538 | std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore( |
| 539 | bool isOwned) { |
| 540 | return this->isAbandoned() ? nullptr : fGpu->makeSemaphore(isOwned); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 543 | std::unique_ptr<GrSemaphore> GrResourceProvider::wrapBackendSemaphore( |
| 544 | const GrBackendSemaphore& semaphore, |
| 545 | SemaphoreWrapType wrapType, |
| 546 | GrWrapOwnership ownership) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 547 | ASSERT_SINGLE_OWNER |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 548 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, |
| 549 | wrapType, |
| 550 | ownership); |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 551 | } |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 552 | |
| 553 | // Ensures the row bytes are populated (not 0) and makes a copy to a temporary |
| 554 | // to make the row bytes tight if necessary. Returns false if the input row bytes are invalid. |
| 555 | static bool prepare_level(const GrMipLevel& inLevel, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 556 | SkISize dimensions, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 557 | bool rowBytesSupport, |
| 558 | GrColorType origColorType, |
| 559 | GrColorType allowedColorType, |
| 560 | GrMipLevel* outLevel, |
| 561 | std::unique_ptr<char[]>* data) { |
| 562 | if (!inLevel.fPixels) { |
| 563 | outLevel->fPixels = nullptr; |
| 564 | outLevel->fRowBytes = 0; |
| 565 | return true; |
| 566 | } |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 567 | size_t minRB = dimensions.fWidth * GrColorTypeBytesPerPixel(origColorType); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 568 | size_t actualRB = inLevel.fRowBytes ? inLevel.fRowBytes : minRB; |
| 569 | if (actualRB < minRB) { |
| 570 | return false; |
| 571 | } |
| 572 | if (origColorType == allowedColorType && (actualRB == minRB || rowBytesSupport)) { |
| 573 | outLevel->fRowBytes = actualRB; |
| 574 | outLevel->fPixels = inLevel.fPixels; |
| 575 | return true; |
| 576 | } |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 577 | auto tempRB = dimensions.fWidth * GrColorTypeBytesPerPixel(allowedColorType); |
| 578 | data->reset(new char[tempRB * dimensions.fHeight]); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 579 | outLevel->fPixels = data->get(); |
| 580 | outLevel->fRowBytes = tempRB; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 581 | GrImageInfo srcInfo(origColorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
| 582 | GrImageInfo dstInfo(allowedColorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 583 | return GrConvertPixels(dstInfo, data->get(), tempRB, srcInfo, inLevel.fPixels, actualRB); |
| 584 | } |
| 585 | |
| 586 | GrColorType GrResourceProvider::prepareLevels(const GrBackendFormat& format, |
| 587 | GrColorType colorType, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 588 | SkISize baseSize, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 589 | const GrMipLevel texels[], |
| 590 | int mipLevelCount, |
| 591 | TempLevels* tempLevels, |
| 592 | TempLevelDatas* tempLevelDatas) const { |
| 593 | SkASSERT(mipLevelCount && texels && texels[0].fPixels); |
| 594 | |
| 595 | auto allowedColorType = |
| 596 | this->caps()->supportedWritePixelsColorType(colorType, format, colorType).fColorType; |
| 597 | if (allowedColorType == GrColorType::kUnknown) { |
| 598 | return GrColorType::kUnknown; |
| 599 | } |
| 600 | bool rowBytesSupport = this->caps()->writePixelsRowBytesSupport(); |
| 601 | tempLevels->reset(mipLevelCount); |
| 602 | tempLevelDatas->reset(mipLevelCount); |
| 603 | auto size = baseSize; |
| 604 | for (int i = 0; i < mipLevelCount; ++i) { |
| 605 | if (!prepare_level(texels[i], size, rowBytesSupport, colorType, allowedColorType, |
| 606 | &(*tempLevels)[i], &(*tempLevelDatas)[i])) { |
| 607 | return GrColorType::kUnknown; |
| 608 | } |
| 609 | size = {std::max(size.fWidth / 2, 1), std::max(size.fHeight / 2, 1)}; |
| 610 | } |
| 611 | return allowedColorType; |
| 612 | } |
| 613 | |
| 614 | sk_sp<GrTexture> GrResourceProvider::writePixels(sk_sp<GrTexture> texture, |
| 615 | GrColorType colorType, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 616 | SkISize baseSize, |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 617 | const GrMipLevel texels[], |
| 618 | int mipLevelCount) const { |
| 619 | SkASSERT(!this->isAbandoned()); |
| 620 | SkASSERT(texture); |
| 621 | SkASSERT(colorType != GrColorType::kUnknown); |
| 622 | SkASSERT(mipLevelCount && texels && texels[0].fPixels); |
| 623 | |
| 624 | SkAutoSTMalloc<14, GrMipLevel> tmpTexels; |
| 625 | SkAutoSTArray<14, std::unique_ptr<char[]>> tmpDatas; |
| 626 | auto tempColorType = this->prepareLevels(texture->backendFormat(), colorType, baseSize, texels, |
| 627 | mipLevelCount, &tmpTexels, &tmpDatas); |
| 628 | if (tempColorType == GrColorType::kUnknown) { |
| 629 | return nullptr; |
| 630 | } |
| 631 | SkAssertResult(fGpu->writePixels(texture.get(), 0, 0, baseSize.fWidth, baseSize.fHeight, |
| 632 | colorType, tempColorType, tmpTexels.get(), mipLevelCount)); |
| 633 | return texture; |
| 634 | } |