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