GrMtlRenderTarget.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/GrMtlRenderTarget.h"
  8. #include "src/gpu/mtl/GrMtlGpu.h"
  9. #include "src/gpu/mtl/GrMtlUtil.h"
  10. #if !__has_feature(objc_arc)
  11. #error This file must be compiled with Arc. Use -fobjc-arc flag
  12. #endif
  13. // Called for wrapped non-texture render targets.
  14. GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu,
  15. const GrSurfaceDesc& desc,
  16. int sampleCnt,
  17. id<MTLTexture> colorTexture,
  18. id<MTLTexture> resolveTexture,
  19. Wrapped)
  20. : GrSurface(gpu, desc, GrProtected::kNo)
  21. , GrRenderTarget(gpu, desc, sampleCnt, GrProtected::kNo)
  22. , fColorTexture(colorTexture)
  23. , fResolveTexture(resolveTexture) {
  24. SkASSERT(sampleCnt > 1);
  25. this->registerWithCacheWrapped(GrWrapCacheable::kNo);
  26. }
  27. GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu,
  28. const GrSurfaceDesc& desc,
  29. id<MTLTexture> colorTexture,
  30. Wrapped)
  31. : GrSurface(gpu, desc, GrProtected::kNo)
  32. , GrRenderTarget(gpu, desc, 1, GrProtected::kNo)
  33. , fColorTexture(colorTexture)
  34. , fResolveTexture(nil) {
  35. this->registerWithCacheWrapped(GrWrapCacheable::kNo);
  36. }
  37. // Called by subclass constructors.
  38. GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu,
  39. const GrSurfaceDesc& desc,
  40. int sampleCnt,
  41. id<MTLTexture> colorTexture,
  42. id<MTLTexture> resolveTexture)
  43. : GrSurface(gpu, desc, GrProtected::kNo)
  44. , GrRenderTarget(gpu, desc, sampleCnt, GrProtected::kNo)
  45. , fColorTexture(colorTexture)
  46. , fResolveTexture(resolveTexture) {
  47. SkASSERT(sampleCnt > 1);
  48. }
  49. GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu,
  50. const GrSurfaceDesc& desc,
  51. id<MTLTexture> colorTexture)
  52. : GrSurface(gpu, desc, GrProtected::kNo)
  53. , GrRenderTarget(gpu, desc, 1, GrProtected::kNo)
  54. , fColorTexture(colorTexture)
  55. , fResolveTexture(nil) {}
  56. sk_sp<GrMtlRenderTarget> GrMtlRenderTarget::MakeWrappedRenderTarget(GrMtlGpu* gpu,
  57. const GrSurfaceDesc& desc,
  58. int sampleCnt,
  59. id<MTLTexture> texture) {
  60. SkASSERT(nil != texture);
  61. SkASSERT(1 == texture.mipmapLevelCount);
  62. SkASSERT(MTLTextureUsageRenderTarget & texture.usage);
  63. GrMtlRenderTarget* mtlRT;
  64. if (sampleCnt > 1) {
  65. MTLPixelFormat format;
  66. if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) {
  67. return nullptr;
  68. }
  69. MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init];
  70. texDesc.textureType = MTLTextureType2DMultisample;
  71. texDesc.pixelFormat = format;
  72. texDesc.width = desc.fWidth;
  73. texDesc.height = desc.fHeight;
  74. texDesc.depth = 1;
  75. texDesc.mipmapLevelCount = 1;
  76. texDesc.sampleCount = sampleCnt;
  77. texDesc.arrayLength = 1;
  78. texDesc.storageMode = MTLStorageModePrivate;
  79. texDesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
  80. id<MTLTexture> colorTexture = [gpu->device() newTextureWithDescriptor:texDesc];
  81. if (!colorTexture) {
  82. return nullptr;
  83. }
  84. SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & colorTexture.usage);
  85. mtlRT = new GrMtlRenderTarget(gpu, desc, sampleCnt, colorTexture, texture, kWrapped);
  86. } else {
  87. mtlRT = new GrMtlRenderTarget(gpu, desc, texture, kWrapped);
  88. }
  89. return sk_sp<GrMtlRenderTarget>(mtlRT);
  90. }
  91. GrMtlRenderTarget::~GrMtlRenderTarget() {
  92. SkASSERT(nil == fColorTexture);
  93. SkASSERT(nil == fResolveTexture);
  94. }
  95. GrBackendRenderTarget GrMtlRenderTarget::getBackendRenderTarget() const {
  96. GrMtlTextureInfo info;
  97. info.fTexture.reset(GrRetainPtrFromId(fColorTexture));
  98. return GrBackendRenderTarget(this->width(), this->height(), fColorTexture.sampleCount, info);
  99. }
  100. GrBackendFormat GrMtlRenderTarget::backendFormat() const {
  101. return GrBackendFormat::MakeMtl(fColorTexture.pixelFormat);
  102. }
  103. GrMtlGpu* GrMtlRenderTarget::getMtlGpu() const {
  104. SkASSERT(!this->wasDestroyed());
  105. return static_cast<GrMtlGpu*>(this->getGpu());
  106. }
  107. void GrMtlRenderTarget::onAbandon() {
  108. fColorTexture = nil;
  109. fResolveTexture = nil;
  110. INHERITED::onAbandon();
  111. }
  112. void GrMtlRenderTarget::onRelease() {
  113. fColorTexture = nil;
  114. fResolveTexture = nil;
  115. INHERITED::onRelease();
  116. }
  117. bool GrMtlRenderTarget::completeStencilAttachment() {
  118. return true;
  119. }