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