| 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/GrTypes.h" |
| 11 | #include "include/private/GrResourceKey.h" |
| 12 | #include "src/core/SkMipmap.h" |
| 13 | #include "src/gpu/GrCaps.h" |
| 14 | #include "src/gpu/GrGpu.h" |
| 15 | #include "src/gpu/GrRenderTarget.h" |
| 16 | #include "src/gpu/GrTexture.h" |
| 17 | |
| 18 | #ifdef SK_DEBUG |
| 19 | #include "include/gpu/GrDirectContext.h" |
| 20 | #include "src/gpu/GrContextPriv.h" |
| 21 | #endif |
| 22 | |
| 23 | void GrTexture::markMipmapsDirty() { |
| 24 | if (GrMipmapStatus::kValid == fMipmapStatus) { |
| 25 | fMipmapStatus = GrMipmapStatus::kDirty; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void GrTexture::markMipmapsClean() { |
| 30 | SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus); |
| 31 | fMipmapStatus = GrMipmapStatus::kValid; |
| 32 | } |
| 33 | |
| 34 | size_t GrTexture::onGpuMemorySize() const { |
| 35 | const GrCaps& caps = *this->getGpu()->caps(); |
| 36 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1, |
| 37 | this->mipmapped()); |
| 38 | } |
| 39 | |
| 40 | ///////////////////////////////////////////////////////////////////////////// |
| 41 | GrTexture::GrTexture(GrGpu* gpu, |
| 42 | const SkISize& dimensions, |
| 43 | GrProtected isProtected, |
| 44 | GrTextureType textureType, |
| 45 | GrMipmapStatus mipmapStatus) |
| 46 | : INHERITED(gpu, dimensions, isProtected) |
| 47 | , fTextureType(textureType) |
| 48 | , fMipmapStatus(mipmapStatus) { |
| 49 | if (fMipmapStatus == GrMipmapStatus::kNotAllocated) { |
| 50 | fMaxMipmapLevel = 0; |
| 51 | } else { |
| 52 | fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture, |
| 57 | GrBackendTexture* backendTexture, |
| 58 | SkImage::BackendTextureReleaseProc* releaseProc) { |
| 59 | if (!texture->unique()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (!texture->onStealBackendTexture(backendTexture, releaseProc)) { |
| 64 | return false; |
| 65 | } |
| 66 | #ifdef SK_DEBUG |
| 67 | GrResourceCache* cache = texture->getContext()->priv().getResourceCache(); |
| 68 | int preCount = cache->getResourceCount(); |
| 69 | #endif |
| 70 | // Ensure that the texture will be released by the cache when we drop the last ref. |
| 71 | // A texture that has no refs and no keys should be immediately removed. |
| 72 | if (texture->getUniqueKey().isValid()) { |
| 73 | texture->resourcePriv().removeUniqueKey(); |
| 74 | } |
| 75 | if (texture->resourcePriv().getScratchKey().isValid()) { |
| 76 | texture->resourcePriv().removeScratchKey(); |
| 77 | } |
| 78 | #ifdef SK_DEBUG |
| 79 | texture.reset(); |
| 80 | int postCount = cache->getResourceCount(); |
| 81 | SkASSERT(postCount < preCount); |
| 82 | #endif |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | void GrTexture::computeScratchKey(GrScratchKey* key) const { |
| 87 | if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) { |
| 88 | int sampleCount = 1; |
| 89 | GrRenderable renderable = GrRenderable::kNo; |
| 90 | if (const auto* rt = this->asRenderTarget()) { |
| 91 | sampleCount = rt->numSamples(); |
| 92 | renderable = GrRenderable::kYes; |
| 93 | } |
| 94 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 95 | ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(), |
| 96 | renderable, sampleCount, this->mipmapped(), isProtected, key); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void GrTexture::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 | |