GrTexture.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "include/core/SkMath.h"
  8. #include "include/core/SkTypes.h"
  9. #include "include/gpu/GrContext.h"
  10. #include "include/gpu/GrRenderTarget.h"
  11. #include "include/gpu/GrTexture.h"
  12. #include "include/gpu/GrTypes.h"
  13. #include "include/private/GrResourceKey.h"
  14. #include "src/core/SkMipMap.h"
  15. #include "src/gpu/GrCaps.h"
  16. #include "src/gpu/GrContextPriv.h"
  17. #include "src/gpu/GrGpu.h"
  18. #include "src/gpu/GrSurfacePriv.h"
  19. #include "src/gpu/GrTexturePriv.h"
  20. void GrTexture::markMipMapsDirty() {
  21. if (GrMipMapsStatus::kValid == fMipMapsStatus) {
  22. fMipMapsStatus = GrMipMapsStatus::kDirty;
  23. }
  24. }
  25. void GrTexture::markMipMapsClean() {
  26. SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus);
  27. fMipMapsStatus = GrMipMapsStatus::kValid;
  28. }
  29. size_t GrTexture::onGpuMemorySize() const {
  30. return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
  31. this->texturePriv().mipMapped());
  32. }
  33. /////////////////////////////////////////////////////////////////////////////
  34. GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
  35. GrTextureType textureType, GrMipMapsStatus mipMapsStatus)
  36. : INHERITED(gpu, desc, isProtected)
  37. , fTextureType(textureType)
  38. , fMipMapsStatus(mipMapsStatus) {
  39. if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
  40. fMaxMipMapLevel = 0;
  41. } else {
  42. fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
  43. }
  44. }
  45. bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
  46. GrBackendTexture* backendTexture,
  47. SkImage::BackendTextureReleaseProc* releaseProc) {
  48. if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
  49. return false;
  50. }
  51. if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
  52. return false;
  53. }
  54. #ifdef SK_DEBUG
  55. GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
  56. int preCount = cache->getResourceCount();
  57. #endif
  58. // Ensure that the texture will be released by the cache when we drop the last ref.
  59. // A texture that has no refs and no keys should be immediately removed.
  60. if (texture->getUniqueKey().isValid()) {
  61. texture->resourcePriv().removeUniqueKey();
  62. }
  63. if (texture->resourcePriv().getScratchKey().isValid()) {
  64. texture->resourcePriv().removeScratchKey();
  65. }
  66. #ifdef SK_DEBUG
  67. texture.reset();
  68. int postCount = cache->getResourceCount();
  69. SkASSERT(postCount < preCount);
  70. #endif
  71. return true;
  72. }
  73. void GrTexture::computeScratchKey(GrScratchKey* key) const {
  74. if (!GrPixelConfigIsCompressed(this->config())) {
  75. int sampleCount = 1;
  76. GrRenderable renderable = GrRenderable::kNo;
  77. if (const auto* rt = this->asRenderTarget()) {
  78. sampleCount = rt->numSamples();
  79. renderable = GrRenderable::kYes;
  80. }
  81. GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), renderable,
  82. sampleCount, this->texturePriv().mipMapped(), key);
  83. }
  84. }
  85. void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
  86. GrRenderable renderable, int sampleCnt, GrMipMapped mipMapped,
  87. GrScratchKey* key) {
  88. static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
  89. SkASSERT(width > 0);
  90. SkASSERT(height > 0);
  91. SkASSERT(sampleCnt > 0);
  92. SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
  93. // make sure desc.fConfig fits in 5 bits
  94. SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
  95. SkASSERT(static_cast<int>(config) < (1 << 5));
  96. SkASSERT(sampleCnt < (1 << 8));
  97. SkASSERT(static_cast<int>(mipMapped) <= 1);
  98. GrScratchKey::Builder builder(key, kType, 3);
  99. builder[0] = width;
  100. builder[1] = height;
  101. builder[2] = config
  102. | (static_cast<uint32_t>(mipMapped) << 5)
  103. | (sampleCnt << 6)
  104. | (static_cast<uint32_t>(renderable) << 14);
  105. }
  106. void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrRenderable renderable,
  107. int sampleCnt, GrScratchKey* key) {
  108. // Note: the fOrigin field is not used in the scratch key
  109. return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable, sampleCnt,
  110. GrMipMapped::kNo, key);
  111. }