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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkMath.h" |
| 9 | #include "include/core/SkTypes.h" |
| 10 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrTexture.h" |
| 12 | #include "include/gpu/GrTypes.h" |
| 13 | #include "include/private/GrResourceKey.h" |
| 14 | #include "src/core/SkMipMap.h" |
| 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
| 17 | #include "src/gpu/GrGpu.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrSurfacePriv.h" |
| 20 | #include "src/gpu/GrTexturePriv.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 { |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 34 | const GrCaps& caps = *this->getGpu()->caps(); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 35 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1, |
| 36 | this->texturePriv().mipMapped()); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 39 | ///////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 40 | GrTexture::GrTexture(GrGpu* gpu, |
| 41 | const SkISize& dimensions, |
| 42 | GrPixelConfig config, |
| 43 | GrProtected isProtected, |
| 44 | GrTextureType textureType, |
| 45 | GrMipMapsStatus mipMapsStatus) |
| 46 | : INHERITED(gpu, dimensions, config, isProtected) |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 47 | , fTextureType(textureType) |
| 48 | , fMipMapsStatus(mipMapsStatus) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 49 | if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 50 | fMaxMipMapLevel = 0; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 51 | } else { |
| 52 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 53 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 55 | |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 56 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture, |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 57 | GrBackendTexture* backendTexture, |
| 58 | SkImage::BackendTextureReleaseProc* releaseProc) { |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 59 | if (!texture->unique()) { |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 64 | return false; |
| 65 | } |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 66 | #ifdef SK_DEBUG |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 67 | GrResourceCache* cache = texture->getContext()->priv().getResourceCache(); |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 68 | int preCount = cache->getResourceCount(); |
| 69 | #endif |
| 70 | // Ensure that the texture will be released by the cache when we drop the last ref. |
| 71 | // A texture that has no refs and no keys should be immediately removed. |
| 72 | if (texture->getUniqueKey().isValid()) { |
| 73 | texture->resourcePriv().removeUniqueKey(); |
| 74 | } |
| 75 | if (texture->resourcePriv().getScratchKey().isValid()) { |
| 76 | texture->resourcePriv().removeScratchKey(); |
| 77 | } |
| 78 | #ifdef SK_DEBUG |
| 79 | texture.reset(); |
| 80 | int postCount = cache->getResourceCount(); |
| 81 | SkASSERT(postCount < preCount); |
| 82 | #endif |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 86 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 87 | if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 88 | int sampleCount = 1; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 89 | GrRenderable renderable = GrRenderable::kNo; |
| 90 | if (const auto* rt = this->asRenderTarget()) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 91 | sampleCount = rt->numSamples(); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 92 | renderable = GrRenderable::kYes; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 93 | } |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 94 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 95 | GrTexturePriv::ComputeScratchKey(this->config(), this->dimensions(), renderable, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 96 | sampleCount, this->texturePriv().mipMapped(), isProtected, |
| 97 | key); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 101 | void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 102 | SkISize dimensions, |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 103 | GrRenderable renderable, |
| 104 | int sampleCnt, |
| 105 | GrMipMapped mipMapped, |
| 106 | GrProtected isProtected, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 107 | GrScratchKey* key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 108 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 109 | SkASSERT(!dimensions.isEmpty()); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 110 | SkASSERT(sampleCnt > 0); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 111 | SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 112 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 113 | // make sure desc.fConfig fits in 5 bits |
| 114 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 115 | SkASSERT(static_cast<uint32_t>(config) < (1 << 5)); |
| 116 | SkASSERT(static_cast<uint32_t>(mipMapped) <= 1); |
| 117 | SkASSERT(static_cast<uint32_t>(isProtected) <= 1); |
| 118 | SkASSERT(static_cast<uint32_t>(renderable) <= 1); |
| 119 | SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 8))); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 120 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 121 | GrScratchKey::Builder builder(key, kType, 3); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 122 | builder[0] = dimensions.width(); |
| 123 | builder[1] = dimensions.height(); |
Brian Salomon | 14cb413 | 2019-09-16 13:14:47 -0400 | [diff] [blame] | 124 | builder[2] = (static_cast<uint32_t>(config) << 0) |
| 125 | | (static_cast<uint32_t>(mipMapped) << 5) |
| 126 | | (static_cast<uint32_t>(isProtected) << 6) |
| 127 | | (static_cast<uint32_t>(renderable) << 7) |
| 128 | | (static_cast<uint32_t>(sampleCnt) << 8); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 129 | } |