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