blob: 16df58f95a0cc17400ebb06af548f443cee3b477 [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, MTLPixelFormatRG8Unorm,
81 kRG_88_GrPixelConfig, { 0.5f, 0.5f, 0, 0 }},
82 { kUnknown_SkColorType, MTLPixelFormatR16Float,
83 kAlpha_half_as_Red_GrPixelConfig, { 1.0f, 0, 0, 0.5f }},
Robert Phillips0c6daf02019-05-16 12:43:11 -040084#ifdef SK_BUILD_FOR_IOS
Robert Phillips459b2952019-05-23 09:38:27 -040085 { kUnknown_SkColorType, MTLPixelFormatETC2_RGB8,
86 kRGB_ETC1_GrPixelConfig, SkColors::kRed }
Robert Phillips0c6daf02019-05-16 12:43:11 -040087#endif
88 };
89
90 for (auto combo : combinations) {
91 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat);
92
Robert Phillipsefb9f142019-05-17 14:19:44 -040093 if (!mtlCaps->isConfigTexturable(combo.fConfig)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040094 continue;
95 }
96
97 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
98 if (kRGBA_F32_SkColorType == combo.fColorType) {
99 continue;
100 }
101
Robert Phillipsefb9f142019-05-17 14:19:44 -0400102 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
103 if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) {
104 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400105 }
106
Robert Phillipsefb9f142019-05-17 14:19:44 -0400107 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400108
Robert Phillipsefb9f142019-05-17 14:19:44 -0400109 if (GrRenderable::kYes == renderable) {
110 if (kRGB_888x_SkColorType == combo.fColorType) {
111 // Ganesh can't perform the blends correctly when rendering this format
112 continue;
113 }
114 if (!mtlCaps->isConfigRenderable(combo.fConfig)) {
115 continue;
116 }
117 }
118
Robert Phillipsb04b6942019-05-21 17:24:31 -0400119 {
120 auto uninitCreateMtd = [format](GrContext* context,
121 GrMipMapped mipMapped,
122 GrRenderable renderable) {
123 return context->createBackendTexture(32, 32, format,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400124 mipMapped, renderable,
125 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400126 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400127
Robert Phillipsb04b6942019-05-21 17:24:31 -0400128 test_wrapping(context, reporter, uninitCreateMtd,
129 combo.fColorType, mipMapped, renderable);
130 }
Robert Phillips459b2952019-05-23 09:38:27 -0400131
132 // Not implemented for Metal yet
133#if 0
134 {
135 auto createWithColorMtd = [format](GrContext* context,
136 const SkColor4f& color,
137 GrMipMapped mipMapped,
138 GrRenderable renderable) {
139 return context->priv().createBackendTexture(32, 32, format, color,
140 mipMapped, renderable);
141 };
142
143 test_color_init(context, reporter, createWithColorMtd,
144 combo.fColorType, combo.fColor, mipMapped, renderable);
145 }
146#endif
Robert Phillipsefb9f142019-05-17 14:19:44 -0400147 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400148 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400149 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400150}