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