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 | |
| 8 | #include "GrResourceProvider.h" |
| 9 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 10 | #include "GrBuffer.h" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 14 | #include "GrGpu.h" |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 15 | #include "GrPath.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 16 | #include "GrPathRendering.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 17 | #include "GrRenderTarget.h" |
| 18 | #include "GrRenderTargetPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 19 | #include "GrResourceCache.h" |
| 20 | #include "GrResourceKey.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 21 | #include "GrSemaphore.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 22 | #include "GrStencilAttachment.h" |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 23 | #include "GrSurfaceProxyPriv.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 24 | #include "GrTexturePriv.h" |
| 25 | #include "../private/GrSingleOwner.h" |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 26 | #include "SkGr.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 27 | #include "SkMathPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 28 | |
| 29 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 30 | |
Robert Phillips | 1bfece8 | 2017-06-01 13:56:52 -0400 | [diff] [blame] | 31 | const uint32_t 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 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [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 |
| 42 | { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 43 | fCaps = sk_ref_sp(fGpu->caps()); |
| 44 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 45 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 46 | fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 47 | } |
| 48 | |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 49 | bool GrResourceProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) { |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 50 | return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())); |
| 51 | } |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 52 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 53 | bool validate_desc(const GrSurfaceDesc& desc, const GrCaps& caps, int levelCount = 0) { |
| 54 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
| 55 | return false; |
| 56 | } |
| 57 | if (!caps.isConfigTexturable(desc.fConfig)) { |
| 58 | return false; |
| 59 | } |
| 60 | if (desc.fFlags & kRenderTarget_GrSurfaceFlag) { |
| 61 | if (!caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
| 62 | return false; |
| 63 | } |
| 64 | } else { |
| 65 | if (desc.fSampleCnt) { |
| 66 | return false; |
| 67 | } |
| 68 | } |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 69 | if (levelCount > 1 && (GrPixelConfigIsSint(desc.fConfig) || !caps.mipMapSupport())) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 75 | // MDB TODO: this should probably be a factory on GrSurfaceProxy |
| 76 | sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture( |
| 77 | const GrSurfaceDesc& desc, |
| 78 | SkBudgeted budgeted, |
| 79 | const GrMipLevel* texels, |
| 80 | int mipLevelCount, |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 81 | SkDestinationSurfaceColorMode mipColorMode) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 82 | ASSERT_SINGLE_OWNER |
| 83 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 84 | if (!mipLevelCount) { |
| 85 | if (texels) { |
| 86 | return nullptr; |
| 87 | } |
| 88 | return GrSurfaceProxy::MakeDeferred(this, desc, budgeted, nullptr, 0); |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 89 | } else if (1 == mipLevelCount) { |
| 90 | if (!texels) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | return this->createTextureProxy(desc, budgeted, texels[0]); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 96 | if (this->isAbandoned()) { |
| 97 | return nullptr; |
| 98 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 99 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 100 | if (!validate_desc(desc, *fCaps, mipLevelCount)) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 101 | return nullptr; |
| 102 | } |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 103 | |
| 104 | SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount); |
| 105 | for (int i = 0; i < mipLevelCount; ++i) { |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 106 | if (!texels[i].fPixels) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 110 | texelsShallowCopy.push_back(texels[i]); |
| 111 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 112 | sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texelsShallowCopy)); |
| 113 | if (tex) { |
| 114 | tex->texturePriv().setMipColorMode(mipColorMode); |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 115 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 116 | |
| 117 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 120 | sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc, |
| 121 | SkBudgeted budgeted, uint32_t flags) { |
| 122 | |
| 123 | flags |= kExact_Flag | kNoCreate_Flag; |
| 124 | sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags)); |
| 125 | if (tex && SkBudgeted::kNo == budgeted) { |
| 126 | tex->resourcePriv().makeUnbudgeted(); |
| 127 | } |
| 128 | |
| 129 | return tex; |
| 130 | } |
| 131 | |
| 132 | static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) { |
| 133 | SkColorType colorType; |
| 134 | if (!GrPixelConfigToColorType(config, &colorType)) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc, |
| 143 | SkBudgeted budgeted, |
| 144 | const GrMipLevel& mipLevel) { |
Robert Phillips | 774831a | 2017-04-20 10:19:33 -0400 | [diff] [blame] | 145 | ASSERT_SINGLE_OWNER |
| 146 | |
| 147 | if (this->isAbandoned()) { |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 151 | if (!mipLevel.fPixels) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 155 | if (!validate_desc(desc, *fCaps)) { |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 159 | GrContext* context = fGpu->getContext(); |
| 160 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 161 | SkImageInfo srcInfo; |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 162 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 163 | if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) { |
| 164 | sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0); |
Robert Phillips | fe50d96 | 2017-06-01 10:22:03 -0400 | [diff] [blame] | 165 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 166 | if (proxy) { |
| 167 | sk_sp<GrSurfaceContext> sContext = |
| 168 | context->contextPriv().makeWrappedSurfaceContext(std::move(proxy), nullptr); |
| 169 | if (sContext) { |
| 170 | if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) { |
| 171 | return sContext->asTextureProxyRef(); |
| 172 | } |
Robert Phillips | 45fdae1 | 2017-04-17 12:57:27 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | SkTArray<GrMipLevel> texels(1); |
| 178 | texels.push_back(mipLevel); |
| 179 | |
| 180 | sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels)); |
| 181 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 182 | } |
| 183 | |
| 184 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 185 | sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
| 186 | uint32_t flags) { |
| 187 | ASSERT_SINGLE_OWNER |
| 188 | |
| 189 | if (this->isAbandoned()) { |
| 190 | return nullptr; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 191 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 192 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 193 | if (!validate_desc(desc, *fCaps)) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 194 | return nullptr; |
| 195 | } |
| 196 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 197 | sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags); |
| 198 | if (tex) { |
| 199 | return tex; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 202 | return fGpu->createTexture(desc, budgeted); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 205 | sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc, |
| 206 | uint32_t flags) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 207 | ASSERT_SINGLE_OWNER |
| 208 | SkASSERT(0 == flags || kNoPendingIO_Flag == flags); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 209 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 210 | if (this->isAbandoned()) { |
| 211 | return nullptr; |
| 212 | } |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 213 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 214 | if (!validate_desc(desc, *fCaps)) { |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 218 | return this->refScratchTexture(desc, flags); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 221 | sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& inDesc, |
| 222 | uint32_t flags) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 223 | ASSERT_SINGLE_OWNER |
| 224 | SkASSERT(!this->isAbandoned()); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 225 | SkASSERT(validate_desc(inDesc, *fCaps)); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 226 | |
| 227 | SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
| 228 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 229 | // We could make initial clears work with scratch textures but it is a rare case so we just opt |
| 230 | // to fall back to making a new texture. |
| 231 | if (!SkToBool(inDesc.fFlags & kPerformInitialClear_GrSurfaceFlag) && |
| 232 | (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag))) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 233 | if (!(kExact_Flag & flags)) { |
| 234 | // bin by pow2 with a reasonable min |
| 235 | GrSurfaceDesc* wdesc = desc.writable(); |
| 236 | wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth)); |
| 237 | wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight)); |
| 238 | } |
| 239 | |
| 240 | GrScratchKey key; |
| 241 | GrTexturePriv::ComputeScratchKey(*desc, &key); |
| 242 | uint32_t scratchFlags = 0; |
| 243 | if (kNoPendingIO_Flag & flags) { |
| 244 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 245 | } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 246 | // If it is not a render target then it will most likely be populated by |
| 247 | // writePixels() which will trigger a flush if the texture has pending IO. |
| 248 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 249 | } |
| 250 | GrGpuResource* resource = fCache->findAndRefScratchResource(key, |
| 251 | GrSurface::WorstCaseSize(*desc), |
| 252 | scratchFlags); |
| 253 | if (resource) { |
| 254 | GrSurface* surface = static_cast<GrSurface*>(resource); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 255 | return sk_sp<GrTexture>(surface->asTexture()); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | if (!(kNoCreate_Flag & flags)) { |
| 260 | return fGpu->createTexture(*desc, SkBudgeted::kYes); |
| 261 | } |
| 262 | |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 266 | sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex, |
| 267 | GrSurfaceOrigin origin, |
| 268 | GrBackendTextureFlags flags, |
| 269 | int sampleCnt, |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 270 | GrWrapOwnership ownership) { |
| 271 | ASSERT_SINGLE_OWNER |
| 272 | if (this->isAbandoned()) { |
| 273 | return nullptr; |
| 274 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 275 | return fGpu->wrapBackendTexture(tex, origin, flags, sampleCnt, ownership); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 279 | const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 280 | { |
| 281 | ASSERT_SINGLE_OWNER |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 282 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT, origin); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key, |
| 286 | GrGpuResource* resource) { |
| 287 | ASSERT_SINGLE_OWNER |
| 288 | if (this->isAbandoned() || !resource) { |
| 289 | return; |
| 290 | } |
| 291 | resource->resourcePriv().setUniqueKey(key); |
| 292 | } |
| 293 | |
| 294 | GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) { |
| 295 | ASSERT_SINGLE_OWNER |
| 296 | return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key); |
| 297 | } |
| 298 | |
| 299 | GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) { |
| 300 | ASSERT_SINGLE_OWNER |
| 301 | GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); |
| 302 | if (resource) { |
| 303 | GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture(); |
| 304 | SkASSERT(texture); |
| 305 | return texture; |
| 306 | } |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 310 | // MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs |
| 311 | void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) { |
| 312 | ASSERT_SINGLE_OWNER |
| 313 | SkASSERT(key.isValid()); |
| 314 | if (this->isAbandoned() || !proxy) { |
| 315 | return; |
| 316 | } |
| 317 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 318 | if (!proxy->instantiate(this)) { |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 319 | return; |
| 320 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 321 | GrTexture* texture = proxy->priv().peekTexture(); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 322 | |
| 323 | this->assignUniqueKeyToResource(key, texture); |
| 324 | } |
| 325 | |
| 326 | // MDB TODO (caching): this side-steps the issue of texture proxies with unique IDs |
| 327 | sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key) { |
| 328 | ASSERT_SINGLE_OWNER |
| 329 | |
| 330 | sk_sp<GrTexture> texture(this->findAndRefTextureByUniqueKey(key)); |
| 331 | if (!texture) { |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | return GrSurfaceProxy::MakeWrapped(std::move(texture)); |
| 336 | } |
| 337 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 338 | const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 339 | int patternSize, |
| 340 | int reps, |
| 341 | int vertCount, |
| 342 | const GrUniqueKey& key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 343 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 344 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 345 | // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO. |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 346 | GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 347 | kNoPendingIO_Flag); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 348 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 349 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 350 | } |
| 351 | uint16_t* data = (uint16_t*) buffer->map(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 352 | bool useTempData = (nullptr == data); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 353 | if (useTempData) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 354 | data = new uint16_t[reps * patternSize]; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 355 | } |
| 356 | for (int i = 0; i < reps; ++i) { |
| 357 | int baseIdx = i * patternSize; |
| 358 | uint16_t baseVert = (uint16_t)(i * vertCount); |
| 359 | for (int j = 0; j < patternSize; ++j) { |
| 360 | data[baseIdx+j] = baseVert + pattern[j]; |
| 361 | } |
| 362 | } |
| 363 | if (useTempData) { |
| 364 | if (!buffer->updateData(data, bufferSize)) { |
| 365 | buffer->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 367 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 368 | delete[] data; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 369 | } else { |
| 370 | buffer->unmap(); |
| 371 | } |
| 372 | this->assignUniqueKeyToResource(key, buffer); |
| 373 | return buffer; |
| 374 | } |
| 375 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 376 | const GrBuffer* GrResourceProvider::createQuadIndexBuffer() { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 377 | static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 378 | GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); |
| 379 | static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; |
| 380 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 381 | return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 384 | sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 385 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 386 | return this->gpu()->pathRendering()->createPath(path, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 389 | sk_sp<GrPathRange> GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen, |
| 390 | const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 391 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 392 | return this->gpu()->pathRendering()->createPathRange(gen, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame^] | 395 | sk_sp<GrPathRange> GrResourceProvider::createGlyphs(const SkTypeface* tf, |
| 396 | const SkScalerContextEffects& effects, |
| 397 | const SkDescriptor* desc, |
| 398 | const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 399 | |
| 400 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 401 | return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 402 | } |
| 403 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 404 | GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 405 | GrAccessPattern accessPattern, uint32_t flags, |
| 406 | const void* data) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 407 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 408 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 409 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 410 | if (kDynamic_GrAccessPattern != accessPattern) { |
| 411 | return this->gpu()->createBuffer(size, intendedType, accessPattern, data); |
| 412 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 413 | if (!(flags & kRequireGpuMemory_Flag) && |
| 414 | this->gpu()->caps()->preferClientSideDynamicBuffers() && |
| 415 | GrBufferTypeIsVertexOrIndex(intendedType) && |
| 416 | kDynamic_GrAccessPattern == accessPattern) { |
| 417 | return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data); |
| 418 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 419 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 420 | // bin by pow2 with a reasonable min |
Robert Phillips | 9e38047 | 2016-10-28 12:15:03 -0400 | [diff] [blame] | 421 | static const size_t MIN_SIZE = 1 << 12; |
| 422 | size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size)); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 423 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 424 | GrScratchKey key; |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 425 | GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key); |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 426 | uint32_t scratchFlags = 0; |
| 427 | if (flags & kNoPendingIO_Flag) { |
| 428 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 429 | } else { |
| 430 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 431 | } |
| 432 | GrBuffer* buffer = static_cast<GrBuffer*>( |
| 433 | this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags)); |
| 434 | if (!buffer) { |
| 435 | buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern); |
| 436 | if (!buffer) { |
| 437 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 440 | if (data) { |
| 441 | buffer->updateData(data, size); |
| 442 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 443 | SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs. |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 444 | return buffer; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 445 | } |
| 446 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 447 | GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) { |
| 448 | SkASSERT(rt); |
| 449 | if (rt->renderTargetPriv().getStencilAttachment()) { |
| 450 | return rt->renderTargetPriv().getStencilAttachment(); |
| 451 | } |
| 452 | |
| 453 | if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) { |
| 454 | GrUniqueKey sbKey; |
| 455 | |
| 456 | int width = rt->width(); |
| 457 | int height = rt->height(); |
| 458 | #if 0 |
| 459 | if (this->caps()->oversizedStencilSupport()) { |
| 460 | width = SkNextPow2(width); |
| 461 | height = SkNextPow2(height); |
| 462 | } |
| 463 | #endif |
| 464 | bool newStencil = false; |
| 465 | GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, |
| 466 | rt->numStencilSamples(), &sbKey); |
| 467 | GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>( |
| 468 | this->findAndRefResourceByUniqueKey(sbKey)); |
| 469 | if (!stencil) { |
| 470 | // Need to try and create a new stencil |
| 471 | stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height); |
| 472 | if (stencil) { |
Robert Phillips | f7cf81a | 2017-03-02 10:23:52 -0500 | [diff] [blame] | 473 | this->assignUniqueKeyToResource(sbKey, stencil); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 474 | newStencil = true; |
| 475 | } |
| 476 | } |
| 477 | if (rt->renderTargetPriv().attachStencilAttachment(stencil)) { |
| 478 | if (newStencil) { |
| 479 | // Right now we're clearing the stencil attachment here after it is |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 480 | // attached to a RT for the first time. When we start matching |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 481 | // stencil buffers with smaller color targets this will no longer |
| 482 | // be correct because it won't be guaranteed to clear the entire |
| 483 | // sb. |
| 484 | // We used to clear down in the GL subclass using a special purpose |
| 485 | // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported |
| 486 | // FBO status. |
| 487 | this->gpu()->clearStencil(rt); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | return rt->renderTargetPriv().getStencilAttachment(); |
| 492 | } |
| 493 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 494 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 495 | const GrBackendTexture& tex, GrSurfaceOrigin origin, int sampleCnt) |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 496 | { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 497 | if (this->isAbandoned()) { |
| 498 | return nullptr; |
| 499 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 500 | return this->gpu()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 501 | } |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 502 | |
| 503 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore() { |
| 504 | return fGpu->makeSemaphore(); |
| 505 | } |
| 506 | |
| 507 | void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) { |
| 508 | semaphore->resetGpu(fGpu); |
| 509 | } |
| 510 | |
| 511 | void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) { |
| 512 | semaphore->resetGpu(nullptr); |
| 513 | } |