blob: 24643669ea28a5ed175580bc78fbd65cb5dc6a12 [file] [log] [blame]
Greg Daniel0576a452017-07-31 16:32:36 -04001/*
2 * Copyright 2017 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 "GrMtlUtil.h"
9
10bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format) {
11 MTLPixelFormat dontCare;
12 if (!format) {
13 format = &dontCare;
14 }
15
16 switch (config) {
17 case kUnknown_GrPixelConfig:
18 return false;
19 case kRGBA_8888_GrPixelConfig:
20 *format = MTLPixelFormatRGBA8Unorm;
21 return true;
22 case kBGRA_8888_GrPixelConfig:
23 *format = MTLPixelFormatBGRA8Unorm;
24 return true;
25 case kSRGBA_8888_GrPixelConfig:
26 *format = MTLPixelFormatRGBA8Unorm_sRGB;
27 return true;
28 case kSBGRA_8888_GrPixelConfig:
29 *format = MTLPixelFormatBGRA8Unorm_sRGB;
30 return true;
31 case kRGBA_8888_sint_GrPixelConfig:
32 *format = MTLPixelFormatRGBA8Sint;
33 return true;
34 case kRGB_565_GrPixelConfig:
35#ifdef SK_BUILD_FOR_IOS
36 *format = MTLPixelFormatR5G6B5Unorm;
37 return true;
38#else
39 return false;
40#endif
41 case kRGBA_4444_GrPixelConfig:
42#ifdef SK_BUILD_FOR_IOS
43 *format = MTLPixelFormatABGR4Unorm;
44 return true;
45#else
46 return false;
47#endif
48 case kAlpha_8_GrPixelConfig:
49 *format = MTLPixelFormatR8Unorm;
50 return true;
51 case kGray_8_GrPixelConfig:
52 *format = MTLPixelFormatR8Unorm;
53 return true;
54 case kRGBA_float_GrPixelConfig:
55 *format = MTLPixelFormatRGBA32Float;
56 return true;
57 case kRG_float_GrPixelConfig:
58 *format = MTLPixelFormatRG32Float;
59 return true;
60 case kRGBA_half_GrPixelConfig:
61 *format = MTLPixelFormatRGBA16Float;
62 return true;
63 case kAlpha_half_GrPixelConfig:
64 *format = MTLPixelFormatR16Float;
65 return true;
66 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040067 SK_ABORT("Unexpected config");
Greg Daniel0576a452017-07-31 16:32:36 -040068 return false;
69}
70
71GrPixelConfig GrMTLFormatToPixelConfig(MTLPixelFormat format) {
72 switch (format) {
73 case MTLPixelFormatRGBA8Unorm:
74 return kRGBA_8888_GrPixelConfig;
75 case MTLPixelFormatBGRA8Unorm:
76 return kBGRA_8888_GrPixelConfig;
77 case MTLPixelFormatRGBA8Unorm_sRGB:
78 return kSRGBA_8888_GrPixelConfig;
79 case MTLPixelFormatBGRA8Unorm_sRGB:
80 return kSBGRA_8888_GrPixelConfig;
81 case MTLPixelFormatRGBA8Sint:
82 return kRGBA_8888_sint_GrPixelConfig;
83#ifdef SK_BUILD_FOR_IOS
84 case MTLPixelFormatB5G6R5Unorm:
85 return kRGB_565_GrPixelConfig;
86 case MTLPixelFormatABGR4Unorm:
87 return kRGBA_4444_GrPixelConfig;
88#endif
89 case MTLPixelFormatR8Unorm:
90 // We currently set this to be Alpha_8 and have no way to go to Gray_8
91 return kAlpha_8_GrPixelConfig;
92 case MTLPixelFormatRGBA32Float:
93 return kRGBA_float_GrPixelConfig;
94 case MTLPixelFormatRG32Float:
95 return kRG_float_GrPixelConfig;
96 case MTLPixelFormatRGBA16Float:
97 return kRGBA_half_GrPixelConfig;
98 case MTLPixelFormatR16Float:
99 return kAlpha_half_GrPixelConfig;
100 default:
101 return kUnknown_GrPixelConfig;
102 }
103}
Greg Daniel4a081e22017-08-04 09:34:44 -0400104
105bool GrMTLFormatIsSRGB(MTLPixelFormat format, MTLPixelFormat* linearFormat) {
106 MTLPixelFormat linearFmt = format;
107 switch (format) {
108 case MTLPixelFormatRGBA8Unorm_sRGB:
109 linearFmt = MTLPixelFormatRGBA8Unorm;
110 break;
111 case MTLPixelFormatBGRA8Unorm_sRGB:
112 linearFmt = MTLPixelFormatBGRA8Unorm;
113 break;
114 default:
115 break;
116 }
117
118 if (linearFormat) {
119 *linearFormat = linearFmt;
120 }
121 return (linearFmt != format);
122}
123