blob: 5fcf050a54fbfe8770b2c2d57192ea12dac052b3 [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 Phillipsefb9f142019-05-17 14:19:44 -040020 SkColorType 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,
27 SkColorType colorType, const SkColor4f& color,
28 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 };
35
Robert Phillips0c6daf02019-05-16 12:43:11 -040036 struct {
37 SkColorType fColorType;
38 GrMTLPixelFormat fFormat;
39 // TODO: remove 'fConfig' and directly use 'fFormat' in GrMtlCaps::isFormatTexturable
40 GrPixelConfig fConfig;
Robert Phillips459b2952019-05-23 09:38:27 -040041 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -040042 } combinations[] = {
Robert Phillips459b2952019-05-23 09:38:27 -040043 { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm,
44 kRGBA_8888_GrPixelConfig, SkColors::kRed },
45 { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm_sRGB,
46 kSRGBA_8888_GrPixelConfig, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040047
Robert Phillips459b2952019-05-23 09:38:27 -040048 { kRGB_888x_SkColorType, MTLPixelFormatRGBA8Unorm,
49 kRGBA_8888_GrPixelConfig, { 1, 1, 0, 0.5f } },
Robert Phillips0c6daf02019-05-16 12:43:11 -040050
Robert Phillips459b2952019-05-23 09:38:27 -040051 { kBGRA_8888_SkColorType, MTLPixelFormatBGRA8Unorm,
52 kBGRA_8888_GrPixelConfig, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -040053
Robert Phillips459b2952019-05-23 09:38:27 -040054 { kRGBA_1010102_SkColorType, MTLPixelFormatRGB10A2Unorm,
55 kRGBA_1010102_GrPixelConfig, { 0.5f, 0, 0, 1.0f } },
Robert Phillips0c6daf02019-05-16 12:43:11 -040056#ifdef SK_BUILD_FOR_IOS
Robert Phillips459b2952019-05-23 09:38:27 -040057 { kRGB_565_SkColorType, MTLPixelFormatB5G6R5Unorm,
58 kRGB_565_GrPixelConfig, SkColors::kRed },
59 { kARGB_4444_SkColorType, MTLPixelFormatABGR4Unorm,
60 kRGBA_4444_GrPixelConfig, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -040061#endif
62
Robert Phillips459b2952019-05-23 09:38:27 -040063 { kAlpha_8_SkColorType, MTLPixelFormatA8Unorm,
64 kAlpha_8_as_Alpha_GrPixelConfig, kTransCol },
65 { kAlpha_8_SkColorType, MTLPixelFormatR8Unorm,
66 kAlpha_8_as_Red_GrPixelConfig, kTransCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -040067
Robert Phillips459b2952019-05-23 09:38:27 -040068 { kGray_8_SkColorType, MTLPixelFormatR8Unorm,
69 kGray_8_as_Red_GrPixelConfig, SkColors::kDkGray },
Robert Phillips0c6daf02019-05-16 12:43:11 -040070
Robert Phillips459b2952019-05-23 09:38:27 -040071 { kRGBA_F32_SkColorType, MTLPixelFormatRGBA32Float,
72 kRGBA_float_GrPixelConfig, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -040073
Robert Phillips459b2952019-05-23 09:38:27 -040074 { kRGBA_F16Norm_SkColorType, MTLPixelFormatRGBA16Float,
75 kRGBA_half_Clamped_GrPixelConfig, SkColors::kLtGray },
76 { kRGBA_F16_SkColorType, MTLPixelFormatRGBA16Float,
77 kRGBA_half_GrPixelConfig, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -040078
79 // These backend formats don't have SkColorType equivalents
Robert Phillips459b2952019-05-23 09:38:27 -040080 { kUnknown_SkColorType, MTLPixelFormatRG32Float,
81 kRG_float_GrPixelConfig, { 0.7f, 0.7f, 0, 0 }},
82 { kUnknown_SkColorType, MTLPixelFormatRG8Unorm,
83 kRG_88_GrPixelConfig, { 0.5f, 0.5f, 0, 0 }},
84 { kUnknown_SkColorType, MTLPixelFormatR16Float,
85 kAlpha_half_as_Red_GrPixelConfig, { 1.0f, 0, 0, 0.5f }},
Robert Phillips0c6daf02019-05-16 12:43:11 -040086#ifdef SK_BUILD_FOR_IOS
Robert Phillips459b2952019-05-23 09:38:27 -040087 { kUnknown_SkColorType, MTLPixelFormatETC2_RGB8,
88 kRGB_ETC1_GrPixelConfig, SkColors::kRed }
Robert Phillips0c6daf02019-05-16 12:43:11 -040089#endif
90 };
91
92 for (auto combo : combinations) {
93 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
94
Robert Phillipsefb9f142019-05-17 14:19:44 -040095 if (!mtlCaps->isConfigTexturable(combo.fConfig)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040096 continue;
97 }
98
99 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
100 if (kRGBA_F32_SkColorType == combo.fColorType) {
101 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) {
112 if (kRGB_888x_SkColorType == combo.fColorType) {
113 // Ganesh can't perform the blends correctly when rendering this format
114 continue;
115 }
116 if (!mtlCaps->isConfigRenderable(combo.fConfig)) {
117 continue;
118 }
119 }
120
Robert Phillipsb04b6942019-05-21 17:24:31 -0400121 {
122 auto uninitCreateMtd = [format](GrContext* context,
123 GrMipMapped mipMapped,
124 GrRenderable renderable) {
125 return context->createBackendTexture(32, 32, format,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400126 mipMapped, renderable,
127 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400128 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400129
Robert Phillipsb04b6942019-05-21 17:24:31 -0400130 test_wrapping(context, reporter, uninitCreateMtd,
131 combo.fColorType, mipMapped, renderable);
132 }
Robert Phillips459b2952019-05-23 09:38:27 -0400133
134 // Not implemented for Metal yet
135#if 0
136 {
137 auto createWithColorMtd = [format](GrContext* context,
138 const SkColor4f& color,
139 GrMipMapped mipMapped,
140 GrRenderable renderable) {
141 return context->priv().createBackendTexture(32, 32, format, color,
142 mipMapped, renderable);
143 };
144
145 test_color_init(context, reporter, createWithColorMtd,
146 combo.fColorType, combo.fColor, mipMapped, renderable);
147 }
148#endif
Robert Phillipsefb9f142019-05-17 14:19:44 -0400149 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400150 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400151 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400152}