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