bsalomon | d309e7a | 2015-04-30 14:18:54 -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 "GrTextureProvider.h" |
| 9 | #include "GrTexturePriv.h" |
| 10 | #include "GrResourceCache.h" |
| 11 | #include "GrGpu.h" |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 12 | #include "../private/GrSingleOwner.h" |
| 13 | |
| 14 | #define ASSERT_SINGLE_OWNER \ |
| 15 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 16 | |
| 17 | enum ScratchTextureFlags { |
| 18 | kExact_ScratchTextureFlag = 0x1, |
| 19 | kNoPendingIO_ScratchTextureFlag = 0x2, |
| 20 | kNoCreate_ScratchTextureFlag = 0x4, |
| 21 | }; |
| 22 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 23 | GrTextureProvider::GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner) |
| 24 | : fCache(cache) |
| 25 | , fGpu(gpu) |
| 26 | #ifdef SK_DEBUG |
| 27 | , fSingleOwner(singleOwner) |
| 28 | #endif |
| 29 | { |
| 30 | } |
| 31 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 32 | GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 33 | const void* srcData, size_t rowBytes) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 34 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 35 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 37 | } |
| 38 | if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && |
| 39 | !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 41 | } |
erikchen | 7fec91c | 2016-02-05 12:10:55 -0800 | [diff] [blame^] | 42 | if (!GrPixelConfigIsCompressed(desc.fConfig) && |
| 43 | !desc.fTextureStorageAllocator.fAllocateTextureStorage) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 44 | static const uint32_t kFlags = kExact_ScratchTextureFlag | |
| 45 | kNoCreate_ScratchTextureFlag; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 46 | if (GrTexture* texture = this->refScratchTexture(desc, kFlags)) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 47 | if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 48 | srcData, rowBytes)) { |
| 49 | if (!budgeted) { |
| 50 | texture->resourcePriv().makeUnbudgeted(); |
| 51 | } |
| 52 | return texture; |
| 53 | } |
| 54 | texture->unref(); |
| 55 | } |
| 56 | } |
| 57 | return fGpu->createTexture(desc, budgeted, srcData, rowBytes); |
| 58 | } |
| 59 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 60 | GrTexture* GrTextureProvider::createApproxTexture(const GrSurfaceDesc& desc) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 61 | ASSERT_SINGLE_OWNER |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 62 | return this->internalCreateApproxTexture(desc, 0); |
| 63 | } |
| 64 | |
| 65 | GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& desc, |
| 66 | uint32_t scratchFlags) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 67 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 68 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 70 | } |
| 71 | // Currently we don't recycle compressed textures as scratch. |
| 72 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 74 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 75 | return this->refScratchTexture(desc, scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 79 | GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc, |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 80 | uint32_t flags) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 81 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 82 | SkASSERT(!this->isAbandoned()); |
| 83 | SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); |
| 84 | |
| 85 | SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
| 86 | |
| 87 | if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 88 | if (!(kExact_ScratchTextureFlag & flags)) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 89 | // bin by pow2 with a reasonable min |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 90 | const int kMinSize = 16; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 91 | GrSurfaceDesc* wdesc = desc.writable(); |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 92 | wdesc->fWidth = SkTMax(kMinSize, GrNextPow2(desc->fWidth)); |
| 93 | wdesc->fHeight = SkTMax(kMinSize, GrNextPow2(desc->fHeight)); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | GrScratchKey key; |
| 97 | GrTexturePriv::ComputeScratchKey(*desc, &key); |
| 98 | uint32_t scratchFlags = 0; |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 99 | if (kNoPendingIO_ScratchTextureFlag & flags) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 100 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 101 | } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 102 | // If it is not a render target then it will most likely be populated by |
| 103 | // writePixels() which will trigger a flush if the texture has pending IO. |
| 104 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 105 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 106 | GrGpuResource* resource = fCache->findAndRefScratchResource(key, |
| 107 | GrSurface::WorseCaseSize(*desc), |
| 108 | scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 109 | if (resource) { |
| 110 | GrSurface* surface = static_cast<GrSurface*>(resource); |
| 111 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 112 | if (rt && fGpu->caps()->discardRenderTargetSupport()) { |
| 113 | rt->discard(); |
| 114 | } |
| 115 | return surface->asTexture(); |
| 116 | } |
| 117 | } |
| 118 | |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 119 | if (!(kNoCreate_ScratchTextureFlag & flags)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | return fGpu->createTexture(*desc, true, nullptr, 0); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 121 | } |
| 122 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 126 | GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& desc, |
| 127 | GrWrapOwnership ownership) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 128 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 129 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 131 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 132 | return fGpu->wrapBackendTexture(desc, ownership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | GrRenderTarget* GrTextureProvider::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 136 | ASSERT_SINGLE_OWNER |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc, |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 138 | kBorrow_GrWrapOwnership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuResource* resource) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 142 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 143 | if (this->isAbandoned() || !resource) { |
| 144 | return; |
| 145 | } |
| 146 | resource->resourcePriv().setUniqueKey(key); |
| 147 | } |
| 148 | |
| 149 | bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) const { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 150 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 151 | return this->isAbandoned() ? false : fCache->hasUniqueKey(key); |
| 152 | } |
| 153 | |
| 154 | GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 155 | ASSERT_SINGLE_OWNER |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 157 | } |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 158 | |
| 159 | GrTexture* GrTextureProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) { |
| 160 | ASSERT_SINGLE_OWNER |
| 161 | GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); |
| 162 | if (resource) { |
| 163 | GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture(); |
| 164 | SkASSERT(texture); |
| 165 | return texture; |
| 166 | } |
| 167 | return NULL; |
| 168 | } |