GrMtlTexture.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2017 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 "src/gpu/mtl/GrMtlTexture.h"
  8. #include "src/gpu/GrTexturePriv.h"
  9. #include "src/gpu/mtl/GrMtlGpu.h"
  10. #include "src/gpu/mtl/GrMtlUtil.h"
  11. #if !__has_feature(objc_arc)
  12. #error This file must be compiled with Arc. Use -fobjc-arc flag
  13. #endif
  14. GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
  15. SkBudgeted budgeted,
  16. const GrSurfaceDesc& desc,
  17. id<MTLTexture> texture,
  18. GrMipMapsStatus mipMapsStatus)
  19. : GrSurface(gpu, desc, GrProtected::kNo)
  20. , INHERITED(gpu, desc, GrProtected::kNo, GrTextureType::k2D, mipMapsStatus)
  21. , fTexture(texture) {
  22. SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
  23. this->registerWithCache(budgeted);
  24. }
  25. GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
  26. Wrapped,
  27. const GrSurfaceDesc& desc,
  28. id<MTLTexture> texture,
  29. GrMipMapsStatus mipMapsStatus,
  30. GrWrapCacheable cacheable,
  31. GrIOType ioType)
  32. : GrSurface(gpu, desc, GrProtected::kNo)
  33. , INHERITED(gpu, desc, GrProtected::kNo, GrTextureType::k2D, mipMapsStatus)
  34. , fTexture(texture) {
  35. SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
  36. if (ioType == kRead_GrIOType) {
  37. this->setReadOnly();
  38. }
  39. this->registerWithCacheWrapped(cacheable);
  40. }
  41. GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
  42. const GrSurfaceDesc& desc,
  43. id<MTLTexture> texture,
  44. GrMipMapsStatus mipMapsStatus)
  45. : GrSurface(gpu, desc, GrProtected::kNo)
  46. , INHERITED(gpu, desc, GrProtected::kNo, GrTextureType::k2D, mipMapsStatus)
  47. , fTexture(texture) {
  48. SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
  49. }
  50. sk_sp<GrMtlTexture> GrMtlTexture::MakeNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted,
  51. const GrSurfaceDesc& desc,
  52. MTLTextureDescriptor* texDesc,
  53. GrMipMapsStatus mipMapsStatus) {
  54. id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc];
  55. if (!texture) {
  56. return nullptr;
  57. }
  58. SkASSERT(MTLTextureUsageShaderRead & texture.usage);
  59. return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
  60. }
  61. sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu,
  62. const GrSurfaceDesc& desc,
  63. id<MTLTexture> texture,
  64. GrWrapCacheable cacheable,
  65. GrIOType ioType) {
  66. SkASSERT(nil != texture);
  67. SkASSERT(MTLTextureUsageShaderRead & texture.usage);
  68. GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid
  69. : GrMipMapsStatus::kNotAllocated;
  70. return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus,
  71. cacheable, ioType));
  72. }
  73. GrMtlTexture::~GrMtlTexture() {
  74. SkASSERT(nil == fTexture);
  75. }
  76. GrMtlGpu* GrMtlTexture::getMtlGpu() const {
  77. SkASSERT(!this->wasDestroyed());
  78. return static_cast<GrMtlGpu*>(this->getGpu());
  79. }
  80. GrBackendTexture GrMtlTexture::getBackendTexture() const {
  81. GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes
  82. : GrMipMapped::kNo;
  83. GrMtlTextureInfo info;
  84. info.fTexture.reset(GrRetainPtrFromId(fTexture));
  85. return GrBackendTexture(this->width(), this->height(), mipMapped, info);
  86. }
  87. GrBackendFormat GrMtlTexture::backendFormat() const {
  88. return GrBackendFormat::MakeMtl(fTexture.pixelFormat);
  89. }