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