blob: f7ea5b52ce408d122a0a3b564c3e59448d6bb8d6 [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,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040020 GrColorType colorType, GrMipMapped mipMapped, GrRenderable renderable);
Robert Phillips0c6daf02019-05-16 12:43:11 -040021
Robert Phillips459b2952019-05-23 09:38:27 -040022void test_color_init(GrContext* context, skiatest::Reporter* reporter,
23 std::function<GrBackendTexture (GrContext*,
24 const SkColor4f&,
25 GrMipMapped,
26 GrRenderable)> create,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040027 GrColorType colorType, const SkColor4f& color,
Robert Phillips459b2952019-05-23 09:38:27 -040028 GrMipMapped mipMapped, GrRenderable renderable);
29
Robert Phillips0c6daf02019-05-16 12:43:11 -040030DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) {
31 GrContext* context = ctxInfo.grContext();
Robert Phillipsefb9f142019-05-17 14:19:44 -040032 const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(context->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -040033
Robert Phillips459b2952019-05-23 09:38:27 -040034 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -040035 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -040036
Robert Phillips0c6daf02019-05-16 12:43:11 -040037 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040038 GrColorType fColorType;
Robert Phillips7f367982019-09-26 14:01:36 -040039 MTLPixelFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -040040 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -040041 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040042 { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed },
43 { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040044
Robert Phillips143987e2019-08-14 10:43:28 -040045 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format),
46 // there is nothing to tell Skia to make the provided color opaque. Clients will need
47 // to provide an opaque initialization color in this case.
48 { GrColorType::kRGB_888x, MTLPixelFormatRGBA8Unorm, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040049
Robert Phillipsb7f95d12019-07-26 11:13:19 -040050 { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -040051
Robert Phillipsb7f95d12019-07-26 11:13:19 -040052 { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm, { 0.5f, 0, 0, 1.0f } },
Robert Phillips0c6daf02019-05-16 12:43:11 -040053#ifdef SK_BUILD_FOR_IOS
Robert Phillipsb7f95d12019-07-26 11:13:19 -040054 { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed },
55 { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -040056#endif
57
Robert Phillipsb7f95d12019-07-26 11:13:19 -040058 { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol },
59 { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -040060 { GrColorType::kGray_8, MTLPixelFormatR8Unorm, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -040061
Robert Phillipsb7f95d12019-07-26 11:13:19 -040062 { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray },
63 { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040064
Robert Phillipsd470e1b2019-09-04 15:05:35 -040065 { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040066 { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } },
67
Robert Phillips429f0d32019-09-11 17:03:28 -040068 { GrColorType::kAlpha_16, MTLPixelFormatR16Unorm, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -040069 { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow },
70
Robert Phillipsb7f95d12019-07-26 11:13:19 -040071 { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray },
72 { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040073 };
74
75 for (auto combo : combinations) {
76 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
77
Robert Phillipsb7f95d12019-07-26 11:13:19 -040078 if (!mtlCaps->isFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040079 continue;
80 }
81
82 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
Robert Phillipsb7f95d12019-07-26 11:13:19 -040083 if (GrColorType::kRGBA_F32 == combo.fColorType) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040084 continue;
85 }
86
Robert Phillipsefb9f142019-05-17 14:19:44 -040087 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
88 if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) {
89 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -040090 }
91
Robert Phillipsefb9f142019-05-17 14:19:44 -040092 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040093
Robert Phillipsefb9f142019-05-17 14:19:44 -040094 if (GrRenderable::kYes == renderable) {
Greg Danielea6bb442019-08-05 15:38:40 -040095 // We must also check whether we allow rendering to the format using the
96 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -040097 if (!mtlCaps->isFormatAsColorTypeRenderable(
98 combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -040099 continue;
100 }
101 }
102
Robert Phillipsb04b6942019-05-21 17:24:31 -0400103 {
104 auto uninitCreateMtd = [format](GrContext* context,
105 GrMipMapped mipMapped,
106 GrRenderable renderable) {
107 return context->createBackendTexture(32, 32, format,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400108 mipMapped, renderable,
109 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400110 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400111
Robert Phillipsb04b6942019-05-21 17:24:31 -0400112 test_wrapping(context, reporter, uninitCreateMtd,
113 combo.fColorType, mipMapped, renderable);
114 }
Robert Phillips459b2952019-05-23 09:38:27 -0400115
Robert Phillips459b2952019-05-23 09:38:27 -0400116 {
Brian Salomon85c3d682019-11-04 15:04:54 -0500117 // We're creating backend textures without specifying a color type "view" of
118 // them at the public API level. Therefore, Ganesh will not apply any swizzles
119 // before writing the color to the texture. However, our validation code does
120 // rely on interpreting the texture contents via a SkColorType and therefore
121 // swizzles may be applied during the read step.
122 // Ideally we'd update our validation code to use a "raw" read that doesn't
123 // impose a color type but for now we just munge the data we upload to match the
124 // expectation.
125 GrSwizzle swizzle;
126 switch (combo.fColorType) {
127 case GrColorType::kAlpha_8:
128 swizzle = GrSwizzle("aaaa");
129 break;
130 case GrColorType::kAlpha_16:
131 swizzle = GrSwizzle("aaaa");
132 break;
133 case GrColorType::kAlpha_F16:
134 swizzle = GrSwizzle("aaaa");
135 break;
136 default:
137 break;
138 }
139 auto createWithColorMtd = [format, swizzle](GrContext* context,
140 const SkColor4f& color,
141 GrMipMapped mipMapped,
142 GrRenderable renderable) {
143 auto swizzledColor = swizzle.applyTo(color);
144 return context->createBackendTexture(32, 32, format, swizzledColor,
Robert Phillips143987e2019-08-14 10:43:28 -0400145 mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400146 };
Brian Salomon85c3d682019-11-04 15:04:54 -0500147 // We make our comparison color using SkPixmap::erase(color) on a pixmap of
148 // combo.fColorType and then calling SkPixmap::readPixels(). erase() will premul
149 // the color passed to it. However, createBackendTexture() that takes a
150 // SkColor4f is color type/alpha type unaware and will simply compute luminance
151 // from the r, g, b, channels.
152 SkColor4f color = combo.fColor;
153 if (combo.fColorType == GrColorType::kGray_8) {
154 color = {color.fR * color.fA,
155 color.fG * color.fA,
156 color.fB * color.fA,
157 1.f};
158 }
159 test_color_init(context, reporter, createWithColorMtd, combo.fColorType, color,
160 mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400161 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400162 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400163 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400164 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400165}