blob: 5f55f962a94e70427ee0cf52f8c1ee3ac51af716 [file] [log] [blame]
Robert Phillips0c6daf02019-05-16 12:43:11 -04001/*
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 Phillips6d344c32020-07-06 10:56:46 -04008#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -04009#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon20f1b342020-12-15 20:23:03 -050010#include "src/gpu/mtl/GrMtlCaps.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040011#include "tests/Test.h"
Brian Salomon20f1b342020-12-15 20:23:03 -050012#include "tools/gpu/ManagedBackendTexture.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040013
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020014#import <Metal/Metal.h>
Brian Salomon20f1b342020-12-15 20:23:03 -050015
16using sk_gpu_test::ManagedBackendTexture;
Robert Phillips0c6daf02019-05-16 12:43:11 -040017
18// In BackendAllocationTest.cpp
Robert Phillipsfe4b4812020-07-17 14:15:51 -040019void test_wrapping(GrDirectContext*,
20 skiatest::Reporter*,
Brian Salomon20f1b342020-12-15 20:23:03 -050021 std::function<sk_sp<ManagedBackendTexture>(GrDirectContext*,
22 GrMipmapped,
23 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040024 GrColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040025 GrMipmapped,
Brian Salomon20f1b342020-12-15 20:23:03 -050026 GrRenderable);
Robert Phillips0c6daf02019-05-16 12:43:11 -040027
Robert Phillipsfe4b4812020-07-17 14:15:51 -040028void test_color_init(GrDirectContext*,
29 skiatest::Reporter*,
Brian Salomon20f1b342020-12-15 20:23:03 -050030 std::function<sk_sp<ManagedBackendTexture>(GrDirectContext*,
31 const SkColor4f&,
32 GrMipmapped,
33 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040034 GrColorType,
35 const SkColor4f&,
Brian Salomon7e67dca2020-07-21 09:27:25 -040036 GrMipmapped,
Brian Salomon20f1b342020-12-15 20:23:03 -050037 GrRenderable);
Robert Phillips459b2952019-05-23 09:38:27 -040038
Robert Phillips0c6daf02019-05-16 12:43:11 -040039DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040040 auto dContext = ctxInfo.directContext();
41 const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(dContext->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -040042
Robert Phillips459b2952019-05-23 09:38:27 -040043 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -040044 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -040045
Robert Phillips0c6daf02019-05-16 12:43:11 -040046 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040047 GrColorType fColorType;
Robert Phillips7f367982019-09-26 14:01:36 -040048 MTLPixelFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -040049 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -040050 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040051 { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed },
52 { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040053
Robert Phillips143987e2019-08-14 10:43:28 -040054 // 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 Phillips0c6daf02019-05-16 12:43:11 -040058
Robert Phillipsb7f95d12019-07-26 11:13:19 -040059 { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -040060
Robert Phillips9a30ee02020-04-29 08:58:39 -040061 { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm,
62 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040063#ifdef SK_BUILD_FOR_MAC
Robert Phillips9a30ee02020-04-29 08:58:39 -040064 { GrColorType::kBGRA_1010102, MTLPixelFormatBGR10A2Unorm,
65 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040066#endif
Robert Phillips0c6daf02019-05-16 12:43:11 -040067#ifdef SK_BUILD_FOR_IOS
Robert Phillipsb7f95d12019-07-26 11:13:19 -040068 { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed },
69 { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -040070#endif
71
Robert Phillipsb7f95d12019-07-26 11:13:19 -040072 { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol },
73 { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -040074 { GrColorType::kGray_8, MTLPixelFormatR8Unorm, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -040075
Robert Phillipsb7f95d12019-07-26 11:13:19 -040076 { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray },
77 { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040078
Robert Phillipsd470e1b2019-09-04 15:05:35 -040079 { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040080 { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } },
81
Robert Phillips429f0d32019-09-11 17:03:28 -040082 { GrColorType::kAlpha_16, MTLPixelFormatR16Unorm, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040083 { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow },
84
Robert Phillipsb7f95d12019-07-26 11:13:19 -040085 { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray },
86 { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040087 };
88
89 for (auto combo : combinations) {
90 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
91
Robert Phillipsb7f95d12019-07-26 11:13:19 -040092 if (!mtlCaps->isFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040093 continue;
94 }
95
96 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
Robert Phillipsb7f95d12019-07-26 11:13:19 -040097 if (GrColorType::kRGBA_F32 == combo.fColorType) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040098 continue;
99 }
100
Brian Salomon7e67dca2020-07-21 09:27:25 -0400101 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -0400102 if (GrMipmapped::kYes == mipMapped && !mtlCaps->mipmapSupport()) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400103 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400104 }
105
Robert Phillipsefb9f142019-05-17 14:19:44 -0400106 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400107
Robert Phillipsefb9f142019-05-17 14:19:44 -0400108 if (GrRenderable::kYes == renderable) {
Greg Danielea6bb442019-08-05 15:38:40 -0400109 // We must also check whether we allow rendering to the format using the
110 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -0400111 if (!mtlCaps->isFormatAsColorTypeRenderable(
112 combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400113 continue;
114 }
115 }
116
Robert Phillipsb04b6942019-05-21 17:24:31 -0400117 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400118 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400119 GrMipmapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -0400120 GrRenderable renderable) {
Brian Salomon20f1b342020-12-15 20:23:03 -0500121 return ManagedBackendTexture::MakeWithoutData(dContext,
122 32, 32,
123 format,
124 mipMapped,
125 renderable,
126 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400127 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400128
Brian Salomon20f1b342020-12-15 20:23:03 -0500129 test_wrapping(dContext, reporter, uninitCreateMtd, combo.fColorType, mipMapped,
130 renderable);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400131 }
Robert Phillips459b2952019-05-23 09:38:27 -0400132
Robert Phillips459b2952019-05-23 09:38:27 -0400133 {
Brian Salomon85c3d682019-11-04 15:04:54 -0500134 // 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 Danielc1ad77c2020-05-06 11:40:03 -0400156
Brian Salomon20f1b342020-12-15 20:23:03 -0500157 auto createWithColorMtd = [format, swizzle](GrDirectContext* dContext,
158 const SkColor4f& color,
159 GrMipmapped mipMapped,
160 GrRenderable renderable) {
Brian Salomon85c3d682019-11-04 15:04:54 -0500161 auto swizzledColor = swizzle.applyTo(color);
Brian Salomon20f1b342020-12-15 20:23:03 -0500162 return ManagedBackendTexture::MakeWithData(dContext,
163 32, 32,
164 format,
165 swizzledColor,
166 mipMapped,
167 renderable,
168 GrProtected::kNo);
Robert Phillips459b2952019-05-23 09:38:27 -0400169 };
Brian Salomon20f1b342020-12-15 20:23:03 -0500170 test_color_init(dContext, reporter, createWithColorMtd, combo.fColorType,
171 combo.fColor, mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400172 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400173 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400174 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400175 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400176}