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