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