MtlBackendAllocationTest.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2019 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/gpu/GrContext.h"
  8. #include "src/gpu/GrContextPriv.h"
  9. #include "tests/Test.h"
  10. #import <Metal/Metal.h>
  11. #include "src/gpu/mtl/GrMtlCaps.h"
  12. // In BackendAllocationTest.cpp
  13. void test_wrapping(GrContext* context, skiatest::Reporter* reporter,
  14. std::function<GrBackendTexture (GrContext*,
  15. GrMipMapped,
  16. GrRenderable)> create,
  17. SkColorType colorType, GrMipMapped mipMapped, GrRenderable renderable);
  18. void test_color_init(GrContext* context, skiatest::Reporter* reporter,
  19. std::function<GrBackendTexture (GrContext*,
  20. const SkColor4f&,
  21. GrMipMapped,
  22. GrRenderable)> create,
  23. SkColorType colorType, const SkColor4f& color,
  24. GrMipMapped mipMapped, GrRenderable renderable);
  25. DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) {
  26. GrContext* context = ctxInfo.grContext();
  27. const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(context->priv().caps());
  28. constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
  29. struct {
  30. SkColorType fColorType;
  31. GrMTLPixelFormat fFormat;
  32. // TODO: remove 'fConfig' and directly use 'fFormat' in GrMtlCaps::isFormatTexturable
  33. GrPixelConfig fConfig;
  34. SkColor4f fColor;
  35. } combinations[] = {
  36. { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm,
  37. kRGBA_8888_GrPixelConfig, SkColors::kRed },
  38. { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm_sRGB,
  39. kSRGBA_8888_GrPixelConfig, SkColors::kRed },
  40. { kRGB_888x_SkColorType, MTLPixelFormatRGBA8Unorm,
  41. kRGBA_8888_GrPixelConfig, { 1, 1, 0, 0.5f } },
  42. { kBGRA_8888_SkColorType, MTLPixelFormatBGRA8Unorm,
  43. kBGRA_8888_GrPixelConfig, SkColors::kBlue },
  44. { kRGBA_1010102_SkColorType, MTLPixelFormatRGB10A2Unorm,
  45. kRGBA_1010102_GrPixelConfig, { 0.5f, 0, 0, 1.0f } },
  46. #ifdef SK_BUILD_FOR_IOS
  47. { kRGB_565_SkColorType, MTLPixelFormatB5G6R5Unorm,
  48. kRGB_565_GrPixelConfig, SkColors::kRed },
  49. { kARGB_4444_SkColorType, MTLPixelFormatABGR4Unorm,
  50. kRGBA_4444_GrPixelConfig, SkColors::kGreen },
  51. #endif
  52. { kAlpha_8_SkColorType, MTLPixelFormatA8Unorm,
  53. kAlpha_8_as_Alpha_GrPixelConfig, kTransCol },
  54. { kAlpha_8_SkColorType, MTLPixelFormatR8Unorm,
  55. kAlpha_8_as_Red_GrPixelConfig, kTransCol },
  56. { kGray_8_SkColorType, MTLPixelFormatR8Unorm,
  57. kGray_8_as_Red_GrPixelConfig, SkColors::kDkGray },
  58. { kRGBA_F32_SkColorType, MTLPixelFormatRGBA32Float,
  59. kRGBA_float_GrPixelConfig, SkColors::kRed },
  60. { kRGBA_F16Norm_SkColorType, MTLPixelFormatRGBA16Float,
  61. kRGBA_half_Clamped_GrPixelConfig, SkColors::kLtGray },
  62. { kRGBA_F16_SkColorType, MTLPixelFormatRGBA16Float,
  63. kRGBA_half_GrPixelConfig, SkColors::kYellow },
  64. // These backend formats don't have SkColorType equivalents
  65. { kUnknown_SkColorType, MTLPixelFormatRG8Unorm,
  66. kRG_88_GrPixelConfig, { 0.5f, 0.5f, 0, 0 }},
  67. { kUnknown_SkColorType, MTLPixelFormatR16Float,
  68. kAlpha_half_as_Red_GrPixelConfig, { 1.0f, 0, 0, 0.5f }},
  69. #ifdef SK_BUILD_FOR_IOS
  70. { kUnknown_SkColorType, MTLPixelFormatETC2_RGB8,
  71. kRGB_ETC1_GrPixelConfig, SkColors::kRed }
  72. #endif
  73. };
  74. for (auto combo : combinations) {
  75. GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
  76. if (!mtlCaps->isConfigTexturable(combo.fConfig)) {
  77. continue;
  78. }
  79. // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
  80. if (kRGBA_F32_SkColorType == combo.fColorType) {
  81. continue;
  82. }
  83. for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
  84. if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) {
  85. continue;
  86. }
  87. for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
  88. if (GrRenderable::kYes == renderable) {
  89. if (kRGB_888x_SkColorType == combo.fColorType) {
  90. // Ganesh can't perform the blends correctly when rendering this format
  91. continue;
  92. }
  93. if (!mtlCaps->isConfigRenderable(combo.fConfig)) {
  94. continue;
  95. }
  96. }
  97. {
  98. auto uninitCreateMtd = [format](GrContext* context,
  99. GrMipMapped mipMapped,
  100. GrRenderable renderable) {
  101. return context->createBackendTexture(32, 32, format,
  102. mipMapped, renderable,
  103. GrProtected::kNo);
  104. };
  105. test_wrapping(context, reporter, uninitCreateMtd,
  106. combo.fColorType, mipMapped, renderable);
  107. }
  108. // Not implemented for Metal yet
  109. #if 0
  110. {
  111. auto createWithColorMtd = [format](GrContext* context,
  112. const SkColor4f& color,
  113. GrMipMapped mipMapped,
  114. GrRenderable renderable) {
  115. return context->priv().createBackendTexture(32, 32, format, color,
  116. mipMapped, renderable);
  117. };
  118. test_color_init(context, reporter, createWithColorMtd,
  119. combo.fColorType, combo.fColor, mipMapped, renderable);
  120. }
  121. #endif
  122. }
  123. }
  124. }
  125. }