| 1 | /* |
| 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. |
| 6 | */ |
| 7 | |
| 8 | #include "include/core/SkMath.h" |
| 9 | #include "include/core/SkTypes.h" |
| 10 | #include "include/gpu/GrContext.h" |
| 11 | #include "include/gpu/GrTypes.h" |
| 12 | #include "include/private/GrResourceKey.h" |
| 13 | #include "src/core/SkMipMap.h" |
| 14 | #include "src/gpu/GrCaps.h" |
| 15 | #include "src/gpu/GrContextPriv.h" |
| 16 | #include "src/gpu/GrGpu.h" |
| 17 | #include "src/gpu/GrRenderTarget.h" |
| 18 | #include "src/gpu/GrSurfacePriv.h" |
| 19 | #include "src/gpu/GrTexture.h" |
| 20 | #include "src/gpu/GrTexturePriv.h" |
| 21 | |
| 22 | void GrTexture::markMipMapsDirty() { |
| 23 | if (GrMipMapsStatus::kValid == fMipMapsStatus) { |
| 24 | fMipMapsStatus = GrMipMapsStatus::kDirty; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void GrTexture::markMipMapsClean() { |
| 29 | SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus); |
| 30 | fMipMapsStatus = GrMipMapsStatus::kValid; |
| 31 | } |
| 32 | |
| 33 | size_t GrTexture::onGpuMemorySize() const { |
| 34 | const GrCaps& caps = *this->getGpu()->caps(); |
| 35 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1, |
| 36 | this->texturePriv().mipMapped()); |
| 37 | } |
| 38 | |
| 39 | ///////////////////////////////////////////////////////////////////////////// |
| 40 | GrTexture::GrTexture(GrGpu* gpu, |
| 41 | const SkISize& dimensions, |
| 42 | GrProtected isProtected, |
| 43 | GrTextureType textureType, |
| 44 | GrMipMapsStatus mipMapsStatus) |
| 45 | : INHERITED(gpu, dimensions, isProtected) |
| 46 | , fTextureType(textureType) |
| 47 | , fMipMapsStatus(mipMapsStatus) { |
| 48 | if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) { |
| 49 | fMaxMipMapLevel = 0; |
| 50 | } else { |
| 51 | fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture, |
| 56 | GrBackendTexture* backendTexture, |
| 57 | SkImage::BackendTextureReleaseProc* releaseProc) { |
| 58 | if (!texture->unique()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 63 | return false; |
| 64 | } |
| 65 | #ifdef SK_DEBUG |
| 66 | GrResourceCache* cache = texture->getContext()->priv().getResourceCache(); |
| 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 |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
| 86 | if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) { |
| 87 | int sampleCount = 1; |
| 88 | GrRenderable renderable = GrRenderable::kNo; |
| 89 | if (const auto* rt = this->asRenderTarget()) { |
| 90 | sampleCount = rt->numSamples(); |
| 91 | renderable = GrRenderable::kYes; |
| 92 | } |
| 93 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 94 | GrTexturePriv::ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), |
| 95 | this->dimensions(), renderable, sampleCount, |
| 96 | this->texturePriv().mipMapped(), isProtected, key); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void GrTexturePriv::ComputeScratchKey(const GrCaps& caps, |
| 101 | const GrBackendFormat& format, |
| 102 | SkISize dimensions, |
| 103 | GrRenderable renderable, |
| 104 | int sampleCnt, |
| 105 | GrMipMapped mipMapped, |
| 106 | GrProtected isProtected, |
| 107 | GrScratchKey* key) { |
| 108 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 109 | SkASSERT(!dimensions.isEmpty()); |
| 110 | SkASSERT(sampleCnt > 0); |
| 111 | SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes); |
| 112 | |
| 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 - 3))); |
| 117 | |
| 118 | uint64_t formatKey = caps.computeFormatKey(format); |
| 119 | |
| 120 | GrScratchKey::Builder builder(key, kType, 5); |
| 121 | builder[0] = dimensions.width(); |
| 122 | builder[1] = dimensions.height(); |
| 123 | builder[2] = formatKey & 0xFFFFFFFF; |
| 124 | builder[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 125 | builder[4] = (static_cast<uint32_t>(mipMapped) << 0) |
| 126 | | (static_cast<uint32_t>(isProtected) << 1) |
| 127 | | (static_cast<uint32_t>(renderable) << 2) |
| 128 | | (static_cast<uint32_t>(sampleCnt) << 3); |
| 129 | } |
| 130 | |