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" |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 9 | |
| 10 | #include "GrCaps.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 11 | #include "GrTexturePriv.h" |
| 12 | #include "GrResourceCache.h" |
| 13 | #include "GrGpu.h" |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 14 | #include "../private/GrSingleOwner.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 15 | #include "SkMathPriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 16 | #include "SkTArray.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 17 | #include "SkTLazy.h" |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 18 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 19 | const int GrTextureProvider::kMinScratchTextureSize = 16; |
| 20 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 21 | #define ASSERT_SINGLE_OWNER \ |
| 22 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 23 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 24 | GrTextureProvider::GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner) |
| 25 | : fCache(cache) |
| 26 | , fGpu(gpu) |
| 27 | #ifdef SK_DEBUG |
| 28 | , fSingleOwner(singleOwner) |
| 29 | #endif |
| 30 | { |
| 31 | } |
| 32 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 33 | GrTexture* GrTextureProvider::createMipMappedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
| 34 | const GrMipLevel* texels, int mipLevelCount) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 35 | ASSERT_SINGLE_OWNER |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 36 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 37 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 39 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 40 | if (mipLevelCount && !texels) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | for (int i = 0; i < mipLevelCount; ++i) { |
| 44 | if (!texels[i].fPixels) { |
| 45 | return nullptr; |
| 46 | } |
| 47 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 48 | if (mipLevelCount > 1 && GrPixelConfigIsSint(desc.fConfig)) { |
| 49 | return nullptr; |
| 50 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 51 | if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && |
| 52 | !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 54 | } |
bsalomon | 7e68ab7 | 2016-04-13 14:29:25 -0700 | [diff] [blame] | 55 | if (!GrPixelConfigIsCompressed(desc.fConfig)) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 56 | if (mipLevelCount < 2) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 57 | static const uint32_t kFlags = kExact_ScratchTextureFlag | |
| 58 | kNoCreate_ScratchTextureFlag; |
| 59 | if (GrTexture* texture = this->refScratchTexture(desc, kFlags)) { |
bsalomon | 7467ab8 | 2016-03-09 09:52:45 -0800 | [diff] [blame] | 60 | if (!mipLevelCount || |
| 61 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 62 | texels[0].fPixels, texels[0].fRowBytes)) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 63 | if (SkBudgeted::kNo == budgeted) { |
| 64 | texture->resourcePriv().makeUnbudgeted(); |
| 65 | } |
| 66 | return texture; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 67 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 68 | texture->unref(); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 69 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 72 | |
| 73 | SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount); |
| 74 | for (int i = 0; i < mipLevelCount; ++i) { |
| 75 | texelsShallowCopy.push_back(texels[i]); |
| 76 | } |
| 77 | return fGpu->createTexture(desc, budgeted, texelsShallowCopy); |
| 78 | } |
| 79 | |
| 80 | GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
| 81 | const void* srcData, size_t rowBytes) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 82 | GrMipLevel tempTexels; |
| 83 | GrMipLevel* texels = nullptr; |
| 84 | int levelCount = 0; |
| 85 | if (srcData) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 86 | tempTexels.fPixels = srcData; |
| 87 | tempTexels.fRowBytes = rowBytes; |
| 88 | texels = &tempTexels; |
| 89 | levelCount = 1; |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 90 | } |
| 91 | return this->createMipMappedTexture(desc, budgeted, texels, levelCount); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 94 | GrTexture* GrTextureProvider::createApproxTexture(const GrSurfaceDesc& desc) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 95 | ASSERT_SINGLE_OWNER |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 96 | return this->internalCreateApproxTexture(desc, 0); |
| 97 | } |
| 98 | |
| 99 | GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& desc, |
| 100 | uint32_t scratchFlags) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 101 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 102 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 104 | } |
| 105 | // Currently we don't recycle compressed textures as scratch. |
| 106 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 108 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 109 | return this->refScratchTexture(desc, scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 113 | GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc, |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 114 | uint32_t flags) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 115 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 116 | SkASSERT(!this->isAbandoned()); |
| 117 | SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); |
| 118 | |
| 119 | SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
| 120 | |
| 121 | if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 122 | if (!(kExact_ScratchTextureFlag & flags)) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 123 | // bin by pow2 with a reasonable min |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 124 | GrSurfaceDesc* wdesc = desc.writable(); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 125 | wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fWidth)); |
| 126 | wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc->fHeight)); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | GrScratchKey key; |
| 130 | GrTexturePriv::ComputeScratchKey(*desc, &key); |
| 131 | uint32_t scratchFlags = 0; |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 132 | if (kNoPendingIO_ScratchTextureFlag & flags) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 133 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 134 | } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 135 | // If it is not a render target then it will most likely be populated by |
| 136 | // writePixels() which will trigger a flush if the texture has pending IO. |
| 137 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 138 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 139 | GrGpuResource* resource = fCache->findAndRefScratchResource(key, |
robertphillips | f1c6cd7 | 2016-08-18 07:11:13 -0700 | [diff] [blame] | 140 | GrSurface::WorstCaseSize(*desc), |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 141 | scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 142 | if (resource) { |
| 143 | GrSurface* surface = static_cast<GrSurface*>(resource); |
| 144 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 145 | if (rt && fGpu->caps()->discardRenderTargetSupport()) { |
| 146 | rt->discard(); |
| 147 | } |
| 148 | return surface->asTexture(); |
| 149 | } |
| 150 | } |
| 151 | |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 152 | if (!(kNoCreate_ScratchTextureFlag & flags)) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 153 | return fGpu->createTexture(*desc, SkBudgeted::kYes); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 154 | } |
| 155 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 157 | } |
| 158 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 159 | sk_sp<GrTexture> GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& desc, |
| 160 | GrWrapOwnership ownership) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 161 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 162 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 164 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 165 | return fGpu->wrapBackendTexture(desc, ownership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 168 | sk_sp<GrRenderTarget> GrTextureProvider::wrapBackendRenderTarget( |
| 169 | const GrBackendRenderTargetDesc& desc) |
| 170 | { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 171 | ASSERT_SINGLE_OWNER |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 172 | return this->isAbandoned() ? nullptr |
| 173 | : fGpu->wrapBackendRenderTarget(desc, kBorrow_GrWrapOwnership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuResource* resource) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 177 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 178 | if (this->isAbandoned() || !resource) { |
| 179 | return; |
| 180 | } |
| 181 | resource->resourcePriv().setUniqueKey(key); |
| 182 | } |
| 183 | |
| 184 | bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) const { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 185 | ASSERT_SINGLE_OWNER |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 186 | return this->isAbandoned() ? false : fCache->hasUniqueKey(key); |
| 187 | } |
| 188 | |
| 189 | GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) { |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 190 | ASSERT_SINGLE_OWNER |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 191 | return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 192 | } |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 193 | |
| 194 | GrTexture* GrTextureProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) { |
| 195 | ASSERT_SINGLE_OWNER |
| 196 | GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); |
| 197 | if (resource) { |
| 198 | GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture(); |
| 199 | SkASSERT(texture); |
| 200 | return texture; |
| 201 | } |
| 202 | return NULL; |
| 203 | } |