bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrTextureProvider.h" |
| 10 | #include "GrTexturePriv.h" |
| 11 | #include "GrResourceCache.h" |
| 12 | #include "GrGpu.h" |
| 13 | |
| 14 | enum ScratchTextureFlags { |
| 15 | kExact_ScratchTextureFlag = 0x1, |
| 16 | kNoPendingIO_ScratchTextureFlag = 0x2, |
| 17 | kNoCreate_ScratchTextureFlag = 0x4, |
| 18 | }; |
| 19 | |
| 20 | GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 21 | const void* srcData, size_t rowBytes) { |
| 22 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 24 | } |
| 25 | if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && |
| 26 | !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 28 | } |
| 29 | if (!GrPixelConfigIsCompressed(desc.fConfig)) { |
| 30 | static const uint32_t kFlags = kExact_ScratchTextureFlag | |
| 31 | kNoCreate_ScratchTextureFlag; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 32 | if (GrTexture* texture = this->refScratchTexture(desc, kFlags)) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 33 | if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 34 | srcData, rowBytes)) { |
| 35 | if (!budgeted) { |
| 36 | texture->resourcePriv().makeUnbudgeted(); |
| 37 | } |
| 38 | return texture; |
| 39 | } |
| 40 | texture->unref(); |
| 41 | } |
| 42 | } |
| 43 | return fGpu->createTexture(desc, budgeted, srcData, rowBytes); |
| 44 | } |
| 45 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 46 | GrTexture* GrTextureProvider::createApproxTexture(const GrSurfaceDesc& desc) { |
| 47 | return this->internalCreateApproxTexture(desc, 0); |
| 48 | } |
| 49 | |
| 50 | GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& desc, |
| 51 | uint32_t scratchFlags) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 52 | if (this->isAbandoned()) { |
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 | } |
| 55 | // Currently we don't recycle compressed textures as scratch. |
| 56 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 58 | } else { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 59 | return this->refScratchTexture(desc, scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 63 | GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc, |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 64 | uint32_t flags) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 65 | SkASSERT(!this->isAbandoned()); |
| 66 | SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); |
| 67 | |
| 68 | SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
| 69 | |
| 70 | if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 71 | if (!(kExact_ScratchTextureFlag & flags)) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 72 | // bin by pow2 with a reasonable min |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame^] | 73 | const int kMinSize = 16; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 74 | GrSurfaceDesc* wdesc = desc.writable(); |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame^] | 75 | wdesc->fWidth = SkTMax(kMinSize, GrNextPow2(desc->fWidth)); |
| 76 | wdesc->fHeight = SkTMax(kMinSize, GrNextPow2(desc->fHeight)); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | GrScratchKey key; |
| 80 | GrTexturePriv::ComputeScratchKey(*desc, &key); |
| 81 | uint32_t scratchFlags = 0; |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 82 | if (kNoPendingIO_ScratchTextureFlag & flags) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 83 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 84 | } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 85 | // If it is not a render target then it will most likely be populated by |
| 86 | // writePixels() which will trigger a flush if the texture has pending IO. |
| 87 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 88 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 89 | GrGpuResource* resource = fCache->findAndRefScratchResource(key, |
| 90 | GrSurface::WorseCaseSize(*desc), |
| 91 | scratchFlags); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 92 | if (resource) { |
| 93 | GrSurface* surface = static_cast<GrSurface*>(resource); |
| 94 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 95 | if (rt && fGpu->caps()->discardRenderTargetSupport()) { |
| 96 | rt->discard(); |
| 97 | } |
| 98 | return surface->asTexture(); |
| 99 | } |
| 100 | } |
| 101 | |
brucedawson | 7b77ac1 | 2015-08-05 14:05:17 -0700 | [diff] [blame] | 102 | if (!(kNoCreate_ScratchTextureFlag & flags)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | return fGpu->createTexture(*desc, true, nullptr, 0); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 104 | } |
| 105 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 109 | GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& desc, |
| 110 | GrWrapOwnership ownership) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 111 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | return nullptr; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 113 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 114 | return fGpu->wrapBackendTexture(desc, ownership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | GrRenderTarget* GrTextureProvider::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc, |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 119 | kBorrow_GrWrapOwnership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuResource* resource) { |
| 123 | if (this->isAbandoned() || !resource) { |
| 124 | return; |
| 125 | } |
| 126 | resource->resourcePriv().setUniqueKey(key); |
| 127 | } |
| 128 | |
| 129 | bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) const { |
| 130 | return this->isAbandoned() ? false : fCache->hasUniqueKey(key); |
| 131 | } |
| 132 | |
| 133 | GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 135 | } |