blob: 8438dcf023c56079afa4a611bdd562f116f5f38a [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"
Robert Phillips0c6daf02019-05-16 12:43:11 -04009#include "src/gpu/GrContextPriv.h"
10#include "tests/Test.h"
11
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020012#import <Metal/Metal.h>
Robert Phillipsefb9f142019-05-17 14:19:44 -040013#include "src/gpu/mtl/GrMtlCaps.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040014
15// In BackendAllocationTest.cpp
Robert Phillipsfe4b4812020-07-17 14:15:51 -040016void test_wrapping(GrDirectContext*,
17 skiatest::Reporter*,
18 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips459b2952019-05-23 09:38:27 -040019 GrMipMapped,
20 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040021 GrColorType,
22 GrMipMapped,
23 GrRenderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -040024 bool* finishedBackendCreation);
Robert Phillips0c6daf02019-05-16 12:43:11 -040025
Robert Phillipsfe4b4812020-07-17 14:15:51 -040026void test_color_init(GrDirectContext*,
27 skiatest::Reporter*,
28 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips459b2952019-05-23 09:38:27 -040029 const SkColor4f&,
30 GrMipMapped,
31 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040032 GrColorType,
33 const SkColor4f&,
34 GrMipMapped,
35 GrRenderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -040036 bool* finishedBackendCreation);
37
38static void mark_signaled(void* context) {
39 *(bool*)context = true;
40}
Robert Phillips459b2952019-05-23 09:38:27 -040041
Robert Phillips0c6daf02019-05-16 12:43:11 -040042DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040043 auto dContext = ctxInfo.directContext();
44 const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(dContext->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -040045
Robert Phillips459b2952019-05-23 09:38:27 -040046 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -040047 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -040048
Robert Phillips0c6daf02019-05-16 12:43:11 -040049 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040050 GrColorType fColorType;
Robert Phillips7f367982019-09-26 14:01:36 -040051 MTLPixelFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -040052 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -040053 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040054 { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed },
55 { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040056
Robert Phillips143987e2019-08-14 10:43:28 -040057 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format),
58 // there is nothing to tell Skia to make the provided color opaque. Clients will need
59 // to provide an opaque initialization color in this case.
60 { GrColorType::kRGB_888x, MTLPixelFormatRGBA8Unorm, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040061
Robert Phillipsb7f95d12019-07-26 11:13:19 -040062 { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -040063
Robert Phillips9a30ee02020-04-29 08:58:39 -040064 { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm,
65 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040066#ifdef SK_BUILD_FOR_MAC
Robert Phillips9a30ee02020-04-29 08:58:39 -040067 { GrColorType::kBGRA_1010102, MTLPixelFormatBGR10A2Unorm,
68 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040069#endif
Robert Phillips0c6daf02019-05-16 12:43:11 -040070#ifdef SK_BUILD_FOR_IOS
Robert Phillipsb7f95d12019-07-26 11:13:19 -040071 { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed },
72 { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -040073#endif
74
Robert Phillipsb7f95d12019-07-26 11:13:19 -040075 { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol },
76 { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -040077 { GrColorType::kGray_8, MTLPixelFormatR8Unorm, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -040078
Robert Phillipsb7f95d12019-07-26 11:13:19 -040079 { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray },
80 { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040081
Robert Phillipsd470e1b2019-09-04 15:05:35 -040082 { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040083 { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } },
84
Robert Phillips429f0d32019-09-11 17:03:28 -040085 { GrColorType::kAlpha_16, MTLPixelFormatR16Unorm, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040086 { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow },
87
Robert Phillipsb7f95d12019-07-26 11:13:19 -040088 { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray },
89 { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040090 };
91
92 for (auto combo : combinations) {
93 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
94
Robert Phillipsb7f95d12019-07-26 11:13:19 -040095 if (!mtlCaps->isFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040096 continue;
97 }
98
99 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400100 if (GrColorType::kRGBA_F32 == combo.fColorType) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400101 continue;
102 }
103
Robert Phillipsefb9f142019-05-17 14:19:44 -0400104 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
105 if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) {
106 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400107 }
108
Robert Phillipsefb9f142019-05-17 14:19:44 -0400109 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400110
Robert Phillipsefb9f142019-05-17 14:19:44 -0400111 if (GrRenderable::kYes == renderable) {
Greg Danielea6bb442019-08-05 15:38:40 -0400112 // We must also check whether we allow rendering to the format using the
113 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -0400114 if (!mtlCaps->isFormatAsColorTypeRenderable(
115 combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400116 continue;
117 }
118 }
119
Robert Phillipsb04b6942019-05-21 17:24:31 -0400120 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400121 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Robert Phillipsb04b6942019-05-21 17:24:31 -0400122 GrMipMapped mipMapped,
123 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400124 return dContext->createBackendTexture(32, 32, format,
125 mipMapped, renderable,
126 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400127 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400128
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400129 test_wrapping(dContext, reporter, uninitCreateMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400130 combo.fColorType, mipMapped, renderable, nullptr);
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
157 bool finishedBackendCreation = false;
158 bool* finishedPtr = &finishedBackendCreation;
159
160 auto createWithColorMtd = [format, swizzle, finishedPtr](
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400161 GrDirectContext* dContext,
162 const SkColor4f& color,
163 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400164 GrRenderable renderable) {
Brian Salomon85c3d682019-11-04 15:04:54 -0500165 auto swizzledColor = swizzle.applyTo(color);
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400166 return dContext->createBackendTexture(32, 32, format, swizzledColor,
167 mipMapped, renderable,
168 GrProtected::kNo,
169 mark_signaled, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400170 };
Brian Salomon85c3d682019-11-04 15:04:54 -0500171 // We make our comparison color using SkPixmap::erase(color) on a pixmap of
172 // combo.fColorType and then calling SkPixmap::readPixels(). erase() will premul
173 // the color passed to it. However, createBackendTexture() that takes a
174 // SkColor4f is color type/alpha type unaware and will simply compute luminance
175 // from the r, g, b, channels.
176 SkColor4f color = combo.fColor;
177 if (combo.fColorType == GrColorType::kGray_8) {
178 color = {color.fR * color.fA,
179 color.fG * color.fA,
180 color.fB * color.fA,
181 1.f};
182 }
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400183 test_color_init(dContext, reporter, createWithColorMtd, combo.fColorType, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400184 mipMapped, renderable, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400185 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400186 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400187 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400188 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400189}