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@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 8 | #include "GrContext.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 10 | #include "GrGpu.h" |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 11 | #include "GrResourceKey.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 13 | #include "GrSurfacePriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 14 | #include "GrTexture.h" |
| 15 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 16 | #include "GrTypes.h" |
| 17 | #include "SkMath.h" |
| 18 | #include "SkMipMap.h" |
| 19 | #include "SkTypes.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 20 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 21 | void GrTexture::markMipMapsDirty() { |
| 22 | if (GrMipMapsStatus::kValid == fMipMapsStatus) { |
| 23 | fMipMapsStatus = GrMipMapsStatus::kDirty; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void GrTexture::markMipMapsClean() { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 28 | SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus); |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 29 | fMipMapsStatus = GrMipMapsStatus::kValid; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 32 | size_t GrTexture::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 33 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 34 | this->texturePriv().mipMapped(), false); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 37 | ///////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 38 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrTextureType textureType, |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 39 | GrMipMapsStatus mipMapsStatus) |
| 40 | : INHERITED(gpu, desc), fTextureType(textureType), fMipMapsStatus(mipMapsStatus) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 41 | if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 42 | fMaxMipMapLevel = 0; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 43 | } else { |
| 44 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 45 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 46 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 47 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 48 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture>&& texture, |
| 49 | GrBackendTexture* backendTexture, |
| 50 | SkImage::BackendTextureReleaseProc* releaseProc) { |
| 51 | if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // Release any not-stolen data being held by this class. |
| 60 | texture->onRelease(); |
| 61 | // Abandon the GrTexture so it can't be re-used. |
| 62 | texture->abandon(); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 67 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 68 | const GrRenderTarget* rt = this->asRenderTarget(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 69 | int sampleCount = 1; |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 70 | if (rt) { |
| 71 | sampleCount = rt->numStencilSamples(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 72 | } |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 73 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 74 | SkToBool(rt), sampleCount, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 75 | this->texturePriv().mipMapped(), key); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 78 | void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 79 | bool isRenderTarget, int sampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 80 | GrMipMapped mipMapped, GrScratchKey* key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 81 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 82 | uint32_t flags = isRenderTarget; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 83 | SkASSERT(width > 0); |
| 84 | SkASSERT(height > 0); |
| 85 | SkASSERT(sampleCnt > 0); |
| 86 | SkASSERT(1 == sampleCnt || isRenderTarget); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 87 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 88 | // make sure desc.fConfig fits in 5 bits |
| 89 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 90 | SkASSERT(static_cast<int>(config) < (1 << 5)); |
| 91 | SkASSERT(sampleCnt < (1 << 8)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 92 | SkASSERT(flags < (1 << 10)); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 93 | SkASSERT(static_cast<int>(mipMapped) <= 1); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 94 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 95 | GrScratchKey::Builder builder(key, kType, 3); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 96 | builder[0] = width; |
| 97 | builder[1] = height; |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 98 | 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] | 99 | } |
| 100 | |
Greg Daniel | 2191823 | 2017-09-08 14:46:23 -0400 | [diff] [blame] | 101 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 102 | // Note: the fOrigin field is not used in the scratch key |
| 103 | return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 104 | SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 105 | GrMipMapped::kNo, key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 106 | } |