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 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 9 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 10 | #include "src/gpu/mtl/GrMtlCaps.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 11 | #include "tests/Test.h" |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 12 | #include "tools/gpu/ManagedBackendTexture.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 13 | |
Aaron O'Mullan | 829b6a0 | 2019-07-08 01:31:14 +0200 | [diff] [blame] | 14 | #import <Metal/Metal.h> |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 15 | |
| 16 | using sk_gpu_test::ManagedBackendTexture; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 17 | |
| 18 | // In BackendAllocationTest.cpp |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 19 | void test_wrapping(GrDirectContext*, |
| 20 | skiatest::Reporter*, |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 21 | std::function<sk_sp<ManagedBackendTexture>(GrDirectContext*, |
| 22 | GrMipmapped, |
| 23 | GrRenderable)> create, |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 24 | GrColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 25 | GrMipmapped, |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 26 | GrRenderable); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 27 | |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 28 | void test_color_init(GrDirectContext*, |
| 29 | skiatest::Reporter*, |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 30 | std::function<sk_sp<ManagedBackendTexture>(GrDirectContext*, |
| 31 | const SkColor4f&, |
| 32 | GrMipmapped, |
| 33 | GrRenderable)> create, |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 34 | GrColorType, |
| 35 | const SkColor4f&, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 36 | GrMipmapped, |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 37 | GrRenderable); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 38 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 39 | DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) { |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 40 | auto dContext = ctxInfo.directContext(); |
| 41 | const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(dContext->priv().caps()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 42 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 43 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame] | 44 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 45 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 46 | struct { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 47 | GrColorType fColorType; |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame] | 48 | MTLPixelFormat fFormat; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 49 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 50 | } combinations[] = { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 51 | { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed }, |
| 52 | { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 53 | |
Robert Phillips | 143987e | 2019-08-14 10:43:28 -0400 | [diff] [blame] | 54 | // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format), |
| 55 | // there is nothing to tell Skia to make the provided color opaque. Clients will need |
| 56 | // to provide an opaque initialization color in this case. |
| 57 | { GrColorType::kRGB_888x, MTLPixelFormatRGBA8Unorm, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 58 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 59 | { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 60 | |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 61 | { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm, |
| 62 | { 0.25f, 0.5f, 0.75f, 1.0f } }, |
Robert Phillips | ef41d50 | 2020-05-01 09:43:04 -0400 | [diff] [blame] | 63 | #ifdef SK_BUILD_FOR_MAC |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 64 | { GrColorType::kBGRA_1010102, MTLPixelFormatBGR10A2Unorm, |
| 65 | { 0.25f, 0.5f, 0.75f, 1.0f } }, |
Robert Phillips | ef41d50 | 2020-05-01 09:43:04 -0400 | [diff] [blame] | 66 | #endif |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 67 | #ifdef SK_BUILD_FOR_IOS |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 68 | { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed }, |
| 69 | { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 70 | #endif |
| 71 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 72 | { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol }, |
| 73 | { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol }, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame] | 74 | { GrColorType::kGray_8, MTLPixelFormatR8Unorm, kGrayCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 75 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 76 | { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray }, |
| 77 | { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 78 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 79 | { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 1 } }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 80 | { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } }, |
| 81 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 82 | { GrColorType::kAlpha_16, MTLPixelFormatR16Unorm, kTransCol }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 83 | { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow }, |
| 84 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 85 | { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray }, |
| 86 | { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | for (auto combo : combinations) { |
| 90 | GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat); |
| 91 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 92 | if (!mtlCaps->isFormatTexturable(combo.fFormat)) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 93 | continue; |
| 94 | } |
| 95 | |
| 96 | // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly) |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 97 | if (GrColorType::kRGBA_F32 == combo.fColorType) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 98 | continue; |
| 99 | } |
| 100 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 101 | for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) { |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 102 | if (GrMipmapped::kYes == mipMapped && !mtlCaps->mipmapSupport()) { |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 103 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 106 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 107 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 108 | if (GrRenderable::kYes == renderable) { |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 109 | // We must also check whether we allow rendering to the format using the |
| 110 | // color type. |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 111 | if (!mtlCaps->isFormatAsColorTypeRenderable( |
| 112 | combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) { |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | } |
| 116 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 117 | { |
Robert Phillips | fe4b481 | 2020-07-17 14:15:51 -0400 | [diff] [blame] | 118 | auto uninitCreateMtd = [format](GrDirectContext* dContext, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 119 | GrMipmapped mipMapped, |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 120 | GrRenderable renderable) { |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 121 | return ManagedBackendTexture::MakeWithoutData(dContext, |
| 122 | 32, 32, |
| 123 | format, |
| 124 | mipMapped, |
| 125 | renderable, |
| 126 | GrProtected::kNo); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 127 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 128 | |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 129 | test_wrapping(dContext, reporter, uninitCreateMtd, combo.fColorType, mipMapped, |
| 130 | renderable); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 131 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 132 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 133 | { |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 134 | // We're creating backend textures without specifying a color type "view" of |
| 135 | // them at the public API level. Therefore, Ganesh will not apply any swizzles |
| 136 | // before writing the color to the texture. However, our validation code does |
| 137 | // rely on interpreting the texture contents via a SkColorType and therefore |
| 138 | // swizzles may be applied during the read step. |
| 139 | // Ideally we'd update our validation code to use a "raw" read that doesn't |
| 140 | // impose a color type but for now we just munge the data we upload to match the |
| 141 | // expectation. |
| 142 | GrSwizzle swizzle; |
| 143 | switch (combo.fColorType) { |
| 144 | case GrColorType::kAlpha_8: |
| 145 | swizzle = GrSwizzle("aaaa"); |
| 146 | break; |
| 147 | case GrColorType::kAlpha_16: |
| 148 | swizzle = GrSwizzle("aaaa"); |
| 149 | break; |
| 150 | case GrColorType::kAlpha_F16: |
| 151 | swizzle = GrSwizzle("aaaa"); |
| 152 | break; |
| 153 | default: |
| 154 | break; |
| 155 | } |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 156 | |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 157 | auto createWithColorMtd = [format, swizzle](GrDirectContext* dContext, |
| 158 | const SkColor4f& color, |
| 159 | GrMipmapped mipMapped, |
| 160 | GrRenderable renderable) { |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 161 | auto swizzledColor = swizzle.applyTo(color); |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 162 | return ManagedBackendTexture::MakeWithData(dContext, |
| 163 | 32, 32, |
| 164 | format, |
| 165 | swizzledColor, |
| 166 | mipMapped, |
| 167 | renderable, |
| 168 | GrProtected::kNo); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 169 | }; |
Brian Salomon | 20f1b34 | 2020-12-15 20:23:03 -0500 | [diff] [blame] | 170 | test_color_init(dContext, reporter, createWithColorMtd, combo.fColorType, |
| 171 | combo.fColor, mipMapped, renderable); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 172 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 173 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 174 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 175 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 176 | } |