blob: 3bd3b280a57546a5bf136d1a359cf480bc54f0ec [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
Greg Danielef59d872017-11-17 16:47:21 -050010#include "GrTypesPriv.h"
11
Greg Daniel0576a452017-07-31 16:32:36 -040012bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format) {
13 MTLPixelFormat dontCare;
14 if (!format) {
15 format = &dontCare;
16 }
17
18 switch (config) {
19 case kUnknown_GrPixelConfig:
20 return false;
21 case kRGBA_8888_GrPixelConfig:
22 *format = MTLPixelFormatRGBA8Unorm;
23 return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040024 case kRGB_888_GrPixelConfig:
25 // TODO: MTLPixelFormatRGB8Unorm
26 return false;
Greg Daniel0576a452017-07-31 16:32:36 -040027 case kBGRA_8888_GrPixelConfig:
28 *format = MTLPixelFormatBGRA8Unorm;
29 return true;
30 case kSRGBA_8888_GrPixelConfig:
31 *format = MTLPixelFormatRGBA8Unorm_sRGB;
32 return true;
33 case kSBGRA_8888_GrPixelConfig:
34 *format = MTLPixelFormatBGRA8Unorm_sRGB;
35 return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -050036 case kRGBA_1010102_GrPixelConfig:
37 *format = MTLPixelFormatRGB10A2Unorm;
38 return true;
Greg Daniel0576a452017-07-31 16:32:36 -040039 case kRGB_565_GrPixelConfig:
40#ifdef SK_BUILD_FOR_IOS
Brian Osman10fc6fd2018-03-02 11:01:10 -050041 *format = MTLPixelFormatB5G6R5Unorm;
Greg Daniel0576a452017-07-31 16:32:36 -040042 return true;
43#else
44 return false;
45#endif
46 case kRGBA_4444_GrPixelConfig:
47#ifdef SK_BUILD_FOR_IOS
48 *format = MTLPixelFormatABGR4Unorm;
49 return true;
50#else
51 return false;
52#endif
Greg Danielef59d872017-11-17 16:47:21 -050053 case kAlpha_8_GrPixelConfig: // fall through
54 case kAlpha_8_as_Red_GrPixelConfig:
Greg Daniel0576a452017-07-31 16:32:36 -040055 *format = MTLPixelFormatR8Unorm;
56 return true;
Greg Danielef59d872017-11-17 16:47:21 -050057 case kAlpha_8_as_Alpha_GrPixelConfig:
58 return false;
Greg Daniel7af060a2017-12-05 16:27:11 -050059 case kGray_8_GrPixelConfig: // fall through
60 case kGray_8_as_Red_GrPixelConfig:
Greg Daniel0576a452017-07-31 16:32:36 -040061 *format = MTLPixelFormatR8Unorm;
62 return true;
Greg Daniel7af060a2017-12-05 16:27:11 -050063 case kGray_8_as_Lum_GrPixelConfig:
64 return false;
Greg Daniel0576a452017-07-31 16:32:36 -040065 case kRGBA_float_GrPixelConfig:
66 *format = MTLPixelFormatRGBA32Float;
67 return true;
68 case kRG_float_GrPixelConfig:
69 *format = MTLPixelFormatRG32Float;
70 return true;
71 case kRGBA_half_GrPixelConfig:
72 *format = MTLPixelFormatRGBA16Float;
73 return true;
Greg Danielef59d872017-11-17 16:47:21 -050074 case kAlpha_half_GrPixelConfig: // fall through
75 case kAlpha_half_as_Red_GrPixelConfig:
Greg Daniel0576a452017-07-31 16:32:36 -040076 *format = MTLPixelFormatR16Float;
77 return true;
78 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040079 SK_ABORT("Unexpected config");
Greg Daniel0576a452017-07-31 16:32:36 -040080 return false;
81}
82
83GrPixelConfig GrMTLFormatToPixelConfig(MTLPixelFormat format) {
84 switch (format) {
85 case MTLPixelFormatRGBA8Unorm:
86 return kRGBA_8888_GrPixelConfig;
87 case MTLPixelFormatBGRA8Unorm:
88 return kBGRA_8888_GrPixelConfig;
89 case MTLPixelFormatRGBA8Unorm_sRGB:
90 return kSRGBA_8888_GrPixelConfig;
91 case MTLPixelFormatBGRA8Unorm_sRGB:
92 return kSBGRA_8888_GrPixelConfig;
Brian Osman10fc6fd2018-03-02 11:01:10 -050093 case MTLPixelFormatRGB10A2Unorm:
94 return kRGBA_1010102_GrPixelConfig;
Greg Daniel0576a452017-07-31 16:32:36 -040095#ifdef SK_BUILD_FOR_IOS
96 case MTLPixelFormatB5G6R5Unorm:
97 return kRGB_565_GrPixelConfig;
98 case MTLPixelFormatABGR4Unorm:
99 return kRGBA_4444_GrPixelConfig;
100#endif
101 case MTLPixelFormatR8Unorm:
102 // We currently set this to be Alpha_8 and have no way to go to Gray_8
103 return kAlpha_8_GrPixelConfig;
104 case MTLPixelFormatRGBA32Float:
105 return kRGBA_float_GrPixelConfig;
106 case MTLPixelFormatRG32Float:
107 return kRG_float_GrPixelConfig;
108 case MTLPixelFormatRGBA16Float:
109 return kRGBA_half_GrPixelConfig;
110 case MTLPixelFormatR16Float:
111 return kAlpha_half_GrPixelConfig;
112 default:
113 return kUnknown_GrPixelConfig;
114 }
115}
Timothy Liang4e85e802018-06-28 16:37:18 -0400116
117id<MTLTexture> GrGetMTLTexture(const void* mtlTexture, GrWrapOwnership wrapOwnership) {
118 if (GrWrapOwnership::kAdopt_GrWrapOwnership == wrapOwnership) {
119 return (__bridge_transfer id<MTLTexture>)mtlTexture;
120 } else {
121 return (__bridge id<MTLTexture>)mtlTexture;
122 }
123}
124