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