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() { |
| 28 | const bool sizeChanged = GrMipMapsStatus::kNotAllocated == fMipMapsStatus; |
| 29 | fMipMapsStatus = GrMipMapsStatus::kValid; |
| 30 | if (sizeChanged) { |
| 31 | // This must not be called until after changing fMipMapsStatus. |
| 32 | this->didChangeGpuMemorySize(); |
| 33 | // TODO(http://skbug.com/4548) - The desc and scratch key should be |
| 34 | // updated to reflect the newly-allocated mipmaps. |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 38 | size_t GrTexture::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 39 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 40 | this->texturePriv().mipMapped(), false); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 43 | ///////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 44 | GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 45 | GrSamplerState::Filter highestFilterMode, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 46 | GrMipMapsStatus mipMapsStatus) |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 47 | : INHERITED(gpu, desc) |
| 48 | , fSamplerType(samplerType) |
| 49 | , fHighestFilterMode(highestFilterMode) |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 50 | , fMipMapsStatus(mipMapsStatus) |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 51 | // Mip color mode is explicitly set after creation via GrTexturePriv |
| 52 | , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 53 | if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 54 | fMaxMipMapLevel = 0; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 55 | } else { |
| 56 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 57 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 58 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 59 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 60 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture>&& texture, |
| 61 | GrBackendTexture* backendTexture, |
| 62 | SkImage::BackendTextureReleaseProc* releaseProc) { |
| 63 | if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Release any not-stolen data being held by this class. |
| 72 | texture->onRelease(); |
| 73 | // Abandon the GrTexture so it can't be re-used. |
| 74 | texture->abandon(); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 79 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 80 | const GrRenderTarget* rt = this->asRenderTarget(); |
| 81 | int sampleCount = 0; |
| 82 | if (rt) { |
| 83 | sampleCount = rt->numStencilSamples(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 84 | } |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 85 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 86 | SkToBool(rt), sampleCount, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 87 | this->texturePriv().mipMapped(), key); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 90 | void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 91 | bool isRenderTarget, int sampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 92 | GrMipMapped mipMapped, GrScratchKey* key) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 93 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 94 | uint32_t flags = isRenderTarget; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 95 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 96 | SkASSERT(0 == sampleCnt || isRenderTarget); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 97 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 98 | // make sure desc.fConfig fits in 5 bits |
| 99 | SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 100 | SkASSERT(static_cast<int>(config) < (1 << 5)); |
| 101 | SkASSERT(sampleCnt < (1 << 8)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 102 | SkASSERT(flags < (1 << 10)); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 103 | SkASSERT(static_cast<int>(mipMapped) <= 1); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 104 | |
joshualitt | b90de31 | 2015-10-01 11:54:34 -0700 | [diff] [blame] | 105 | GrScratchKey::Builder builder(key, kType, 3); |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 106 | builder[0] = width; |
| 107 | builder[1] = height; |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 108 | 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] | 109 | } |
| 110 | |
Greg Daniel | 2191823 | 2017-09-08 14:46:23 -0400 | [diff] [blame] | 111 | void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 112 | // Note: the fOrigin field is not used in the scratch key |
| 113 | return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 114 | SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 115 | GrMipMapped::kNo, key); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 116 | } |