bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 8 | #include "GrTexture.h" |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 9 | #include "GrCaps.h" |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrRenderTarget.h" |
| 14 | #include "GrResourceKey.h" |
| 15 | #include "GrSurfacePriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 16 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 17 | #include "GrTypes.h" |
| 18 | #include "SkMath.h" |
| 19 | #include "SkMipMap.h" |
| 20 | #include "SkTypes.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 21 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 22 | void GrTexture::markMipMapsDirty() { |
| 23 | if (GrMipMapsStatus::kValid == fMipMapsStatus) { |
| 24 | fMipMapsStatus = GrMipMapsStatus::kDirty; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void GrTexture::markMipMapsClean() { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 29 | SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus); |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 30 | fMipMapsStatus = GrMipMapsStatus::kValid; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 33 | size_t GrTexture::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 34 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 35 | this->texturePriv().mipMapped(), false); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 38 | ///////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 39 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrTextureType textureType, |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 40 | GrMipMapsStatus mipMapsStatus) |
| 41 | : INHERITED(gpu, desc), fTextureType(textureType), fMipMapsStatus(mipMapsStatus) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 42 | if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 43 | fMaxMipMapLevel = 0; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 44 | } else { |
| 45 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 46 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 47 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 48 | |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 49 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture, |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 50 | GrBackendTexture* backendTexture, |
| 51 | SkImage::BackendTextureReleaseProc* releaseProc) { |
| 52 | if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 57 | return false; |
| 58 | } |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 59 | #ifdef SK_DEBUG |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 60 | GrResourceCache* cache = texture->getContext()->priv().getResourceCache(); |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 61 | int preCount = cache->getResourceCount(); |
| 62 | #endif |
| 63 | // Ensure that the texture will be released by the cache when we drop the last ref. |
| 64 | // A texture that has no refs and no keys should be immediately removed. |
| 65 | if (texture->getUniqueKey().isValid()) { |
| 66 | texture->resourcePriv().removeUniqueKey(); |
| 67 | } |
| 68 | if (texture->resourcePriv().getScratchKey().isValid()) { |
| 69 | texture->resourcePriv().removeScratchKey(); |
| 70 | } |
| 71 | #ifdef SK_DEBUG |
| 72 | texture.reset(); |
| 73 | int postCount = cache->getResourceCount(); |
| 74 | SkASSERT(postCount < preCount); |
| 75 | #endif |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 79 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 80 | if (!GrPixelConfigIsCompressed(this->config())) { |
| 81 | const GrRenderTarget* rt = this->asRenderTarget(); |
| 82 | int sampleCount = 1; |
| 83 | if (rt) { |
| 84 | sampleCount = rt->numStencilSamples(); |
| 85 | } |
| 86 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
| 87 | SkToBool(rt), sampleCount, |
| 88 | this->texturePriv().mipMapped(), key); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 92 | void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 93 | bool isRenderTarget, int sampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 94 | GrMipMapped mipMapped, GrScratchKey* key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 95 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 96 | uint32_t flags = isRenderTarget; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 97 | SkASSERT(width > 0); |
| 98 | SkASSERT(height > 0); |
| 99 | SkASSERT(sampleCnt > 0); |
| 100 | SkASSERT(1 == sampleCnt || isRenderTarget); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 101 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 102 | // make sure desc.fConfig fits in 5 bits |
| 103 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 104 | SkASSERT(static_cast<int>(config) < (1 << 5)); |
| 105 | SkASSERT(sampleCnt < (1 << 8)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 106 | SkASSERT(flags < (1 << 10)); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 107 | SkASSERT(static_cast<int>(mipMapped) <= 1); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 108 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 109 | GrScratchKey::Builder builder(key, kType, 3); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 110 | builder[0] = width; |
| 111 | builder[1] = height; |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 112 | builder[2] = config | (static_cast<uint8_t>(mipMapped) << 5) | (sampleCnt << 6) | (flags << 14); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Greg Daniel | 2191823 | 2017-09-08 14:46:23 -0400 | [diff] [blame] | 115 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 116 | // Note: the fOrigin field is not used in the scratch key |
| 117 | return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 118 | SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 119 | GrMipMapped::kNo, key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 120 | } |