GrMtlTextureRenderTarget.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2018 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/GrMtlGpu.h"
  8. #include "src/gpu/mtl/GrMtlTextureRenderTarget.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. GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
  14. SkBudgeted budgeted,
  15. const GrSurfaceDesc& desc,
  16. int sampleCnt,
  17. id<MTLTexture> colorTexture,
  18. id<MTLTexture> resolveTexture,
  19. GrMipMapsStatus mipMapsStatus)
  20. : GrSurface(gpu, desc, GrProtected::kNo)
  21. , GrMtlTexture(gpu, desc, resolveTexture, mipMapsStatus)
  22. , GrMtlRenderTarget(gpu, desc, sampleCnt, colorTexture, resolveTexture) {
  23. this->registerWithCache(budgeted);
  24. }
  25. GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
  26. SkBudgeted budgeted,
  27. const GrSurfaceDesc& desc,
  28. id<MTLTexture> colorTexture,
  29. GrMipMapsStatus mipMapsStatus)
  30. : GrSurface(gpu, desc, GrProtected::kNo)
  31. , GrMtlTexture(gpu, desc, colorTexture, mipMapsStatus)
  32. , GrMtlRenderTarget(gpu, desc, colorTexture) {
  33. this->registerWithCache(budgeted);
  34. }
  35. GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
  36. const GrSurfaceDesc& desc,
  37. int sampleCnt,
  38. id<MTLTexture> colorTexture,
  39. id<MTLTexture> resolveTexture,
  40. GrMipMapsStatus mipMapsStatus,
  41. GrWrapCacheable cacheable)
  42. : GrSurface(gpu, desc, GrProtected::kNo)
  43. , GrMtlTexture(gpu, desc, resolveTexture, mipMapsStatus)
  44. , GrMtlRenderTarget(gpu, desc, sampleCnt, colorTexture, resolveTexture) {
  45. this->registerWithCacheWrapped(cacheable);
  46. }
  47. GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
  48. const GrSurfaceDesc& desc,
  49. id<MTLTexture> colorTexture,
  50. GrMipMapsStatus mipMapsStatus,
  51. GrWrapCacheable cacheable)
  52. : GrSurface(gpu, desc, GrProtected::kNo)
  53. , GrMtlTexture(gpu, desc, colorTexture, mipMapsStatus)
  54. , GrMtlRenderTarget(gpu, desc, colorTexture) {
  55. this->registerWithCacheWrapped(cacheable);
  56. }
  57. id<MTLTexture> create_msaa_texture(GrMtlGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt) {
  58. MTLPixelFormat format;
  59. if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) {
  60. return nullptr;
  61. }
  62. MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init];
  63. texDesc.textureType = MTLTextureType2DMultisample;
  64. texDesc.pixelFormat = format;
  65. texDesc.width = desc.fWidth;
  66. texDesc.height = desc.fHeight;
  67. texDesc.depth = 1;
  68. texDesc.mipmapLevelCount = 1;
  69. texDesc.sampleCount = sampleCnt;
  70. texDesc.arrayLength = 1;
  71. texDesc.storageMode = MTLStorageModePrivate;
  72. texDesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
  73. return [gpu->device() newTextureWithDescriptor:texDesc];
  74. }
  75. sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeNewTextureRenderTarget(
  76. GrMtlGpu* gpu,
  77. SkBudgeted budgeted,
  78. const GrSurfaceDesc& desc,
  79. int sampleCnt,
  80. MTLTextureDescriptor* texDesc,
  81. GrMipMapsStatus mipMapsStatus) {
  82. id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc];
  83. if (!texture) {
  84. return nullptr;
  85. }
  86. SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & texture.usage);
  87. if (sampleCnt > 1) {
  88. id<MTLTexture> colorTexture = create_msaa_texture(gpu, desc, sampleCnt);
  89. if (!colorTexture) {
  90. return nullptr;
  91. }
  92. SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & colorTexture.usage);
  93. return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
  94. gpu, budgeted, desc, sampleCnt, colorTexture, texture, mipMapsStatus));
  95. } else {
  96. return sk_sp<GrMtlTextureRenderTarget>(
  97. new GrMtlTextureRenderTarget(gpu, budgeted, desc, texture, mipMapsStatus));
  98. }
  99. }
  100. sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeWrappedTextureRenderTarget(
  101. GrMtlGpu* gpu,
  102. const GrSurfaceDesc& desc,
  103. int sampleCnt,
  104. id<MTLTexture> texture,
  105. GrWrapCacheable cacheable) {
  106. SkASSERT(nil != texture);
  107. SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & texture.usage);
  108. GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1
  109. ? GrMipMapsStatus::kDirty
  110. : GrMipMapsStatus::kNotAllocated;
  111. if (sampleCnt > 1) {
  112. id<MTLTexture> colorTexture = create_msaa_texture(gpu, desc, sampleCnt);
  113. if (!colorTexture) {
  114. return nullptr;
  115. }
  116. SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & colorTexture.usage);
  117. return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
  118. gpu, desc, sampleCnt, colorTexture, texture, mipMapsStatus, cacheable));
  119. } else {
  120. return sk_sp<GrMtlTextureRenderTarget>(
  121. new GrMtlTextureRenderTarget(gpu, desc, texture, mipMapsStatus, cacheable));
  122. }
  123. }