blob: fe164721fa4d90489e529f64b71590f5d972b54e [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
8#include "include/gpu/GrContext.h"
9#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
16void test_wrapping(GrContext* context, skiatest::Reporter* reporter,
Robert Phillips459b2952019-05-23 09:38:27 -040017 std::function<GrBackendTexture (GrContext*,
18 GrMipMapped,
19 GrRenderable)> create,
Greg Danielc1ad77c2020-05-06 11:40:03 -040020 GrColorType colorType, GrMipMapped mipMapped, GrRenderable renderable,
21 bool* finishedBackendCreation);
Robert Phillips0c6daf02019-05-16 12:43:11 -040022
Robert Phillips459b2952019-05-23 09:38:27 -040023void test_color_init(GrContext* context, skiatest::Reporter* reporter,
24 std::function<GrBackendTexture (GrContext*,
25 const SkColor4f&,
26 GrMipMapped,
27 GrRenderable)> create,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040028 GrColorType colorType, const SkColor4f& color,
Greg Danielc1ad77c2020-05-06 11:40:03 -040029 GrMipMapped mipMapped, GrRenderable renderable,
30 bool* finishedBackendCreation);
31
32static void mark_signaled(void* context) {
33 *(bool*)context = true;
34}
Robert Phillips459b2952019-05-23 09:38:27 -040035
Robert Phillips0c6daf02019-05-16 12:43:11 -040036DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) {
37 GrContext* context = ctxInfo.grContext();
Robert Phillipsefb9f142019-05-17 14:19:44 -040038 const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(context->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -040039
Robert Phillips459b2952019-05-23 09:38:27 -040040 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -040041 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -040042
Robert Phillips0c6daf02019-05-16 12:43:11 -040043 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040044 GrColorType fColorType;
Robert Phillips7f367982019-09-26 14:01:36 -040045 MTLPixelFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -040046 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -040047 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040048 { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed },
49 { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040050
Robert Phillips143987e2019-08-14 10:43:28 -040051 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format),
52 // there is nothing to tell Skia to make the provided color opaque. Clients will need
53 // to provide an opaque initialization color in this case.
54 { GrColorType::kRGB_888x, MTLPixelFormatRGBA8Unorm, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040055
Robert Phillipsb7f95d12019-07-26 11:13:19 -040056 { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -040057
Robert Phillips9a30ee02020-04-29 08:58:39 -040058 { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm,
59 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040060#ifdef SK_BUILD_FOR_MAC
Robert Phillips9a30ee02020-04-29 08:58:39 -040061 { GrColorType::kBGRA_1010102, MTLPixelFormatBGR10A2Unorm,
62 { 0.25f, 0.5f, 0.75f, 1.0f } },
Robert Phillipsef41d502020-05-01 09:43:04 -040063#endif
Robert Phillips0c6daf02019-05-16 12:43:11 -040064#ifdef SK_BUILD_FOR_IOS
Robert Phillipsb7f95d12019-07-26 11:13:19 -040065 { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed },
66 { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -040067#endif
68
Robert Phillipsb7f95d12019-07-26 11:13:19 -040069 { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol },
70 { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -040071 { GrColorType::kGray_8, MTLPixelFormatR8Unorm, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -040072
Robert Phillipsb7f95d12019-07-26 11:13:19 -040073 { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray },
74 { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040075
Robert Phillipsd470e1b2019-09-04 15:05:35 -040076 { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040077 { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } },
78
Robert Phillips429f0d32019-09-11 17:03:28 -040079 { GrColorType::kAlpha_16, MTLPixelFormatR16Unorm, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040080 { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow },
81
Robert Phillipsb7f95d12019-07-26 11:13:19 -040082 { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray },
83 { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040084 };
85
86 for (auto combo : combinations) {
87 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
88
Robert Phillipsb7f95d12019-07-26 11:13:19 -040089 if (!mtlCaps->isFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040090 continue;
91 }
92
93 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
Robert Phillipsb7f95d12019-07-26 11:13:19 -040094 if (GrColorType::kRGBA_F32 == combo.fColorType) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040095 continue;
96 }
97
Robert Phillipsefb9f142019-05-17 14:19:44 -040098 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
99 if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) {
100 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400101 }
102
Robert Phillipsefb9f142019-05-17 14:19:44 -0400103 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400104
Robert Phillipsefb9f142019-05-17 14:19:44 -0400105 if (GrRenderable::kYes == renderable) {
Greg Danielea6bb442019-08-05 15:38:40 -0400106 // We must also check whether we allow rendering to the format using the
107 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -0400108 if (!mtlCaps->isFormatAsColorTypeRenderable(
109 combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400110 continue;
111 }
112 }
113
Robert Phillipsb04b6942019-05-21 17:24:31 -0400114 {
115 auto uninitCreateMtd = [format](GrContext* context,
116 GrMipMapped mipMapped,
117 GrRenderable renderable) {
118 return context->createBackendTexture(32, 32, format,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400119 mipMapped, renderable,
120 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400121 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400122
Robert Phillipsb04b6942019-05-21 17:24:31 -0400123 test_wrapping(context, reporter, uninitCreateMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400124 combo.fColorType, mipMapped, renderable, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400125 }
Robert Phillips459b2952019-05-23 09:38:27 -0400126
Robert Phillips459b2952019-05-23 09:38:27 -0400127 {
Brian Salomon85c3d682019-11-04 15:04:54 -0500128 // We're creating backend textures without specifying a color type "view" of
129 // them at the public API level. Therefore, Ganesh will not apply any swizzles
130 // before writing the color to the texture. However, our validation code does
131 // rely on interpreting the texture contents via a SkColorType and therefore
132 // swizzles may be applied during the read step.
133 // Ideally we'd update our validation code to use a "raw" read that doesn't
134 // impose a color type but for now we just munge the data we upload to match the
135 // expectation.
136 GrSwizzle swizzle;
137 switch (combo.fColorType) {
138 case GrColorType::kAlpha_8:
139 swizzle = GrSwizzle("aaaa");
140 break;
141 case GrColorType::kAlpha_16:
142 swizzle = GrSwizzle("aaaa");
143 break;
144 case GrColorType::kAlpha_F16:
145 swizzle = GrSwizzle("aaaa");
146 break;
147 default:
148 break;
149 }
Greg Danielc1ad77c2020-05-06 11:40:03 -0400150
151 bool finishedBackendCreation = false;
152 bool* finishedPtr = &finishedBackendCreation;
153
154 auto createWithColorMtd = [format, swizzle, finishedPtr](
155 GrContext* context, const SkColor4f& color, GrMipMapped mipMapped,
156 GrRenderable renderable) {
Brian Salomon85c3d682019-11-04 15:04:54 -0500157 auto swizzledColor = swizzle.applyTo(color);
158 return context->createBackendTexture(32, 32, format, swizzledColor,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400159 mipMapped, renderable,
160 GrProtected::kNo,
161 mark_signaled, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400162 };
Brian Salomon85c3d682019-11-04 15:04:54 -0500163 // We make our comparison color using SkPixmap::erase(color) on a pixmap of
164 // combo.fColorType and then calling SkPixmap::readPixels(). erase() will premul
165 // the color passed to it. However, createBackendTexture() that takes a
166 // SkColor4f is color type/alpha type unaware and will simply compute luminance
167 // from the r, g, b, channels.
168 SkColor4f color = combo.fColor;
169 if (combo.fColorType == GrColorType::kGray_8) {
170 color = {color.fR * color.fA,
171 color.fG * color.fA,
172 color.fB * color.fA,
173 1.f};
174 }
175 test_color_init(context, reporter, createWithColorMtd, combo.fColorType, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400176 mipMapped, renderable, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400177 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400178 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400179 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400180 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400181}