Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 1 | /* |
| 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 Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 10 | #include "GrTypesPriv.h" |
| 11 | |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 12 | bool 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 Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 24 | case kRGB_888_GrPixelConfig: |
| 25 | // TODO: MTLPixelFormatRGB8Unorm |
| 26 | return false; |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 27 | 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 Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 36 | case kRGBA_1010102_GrPixelConfig: |
| 37 | *format = MTLPixelFormatRGB10A2Unorm; |
| 38 | return true; |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 39 | case kRGB_565_GrPixelConfig: |
| 40 | #ifdef SK_BUILD_FOR_IOS |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 41 | *format = MTLPixelFormatB5G6R5Unorm; |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 42 | 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 Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 53 | case kAlpha_8_GrPixelConfig: // fall through |
| 54 | case kAlpha_8_as_Red_GrPixelConfig: |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 55 | *format = MTLPixelFormatR8Unorm; |
| 56 | return true; |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 57 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 58 | return false; |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 59 | case kGray_8_GrPixelConfig: // fall through |
| 60 | case kGray_8_as_Red_GrPixelConfig: |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 61 | *format = MTLPixelFormatR8Unorm; |
| 62 | return true; |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 63 | case kGray_8_as_Lum_GrPixelConfig: |
| 64 | return false; |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 65 | 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 Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 74 | case kAlpha_half_GrPixelConfig: // fall through |
| 75 | case kAlpha_half_as_Red_GrPixelConfig: |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 76 | *format = MTLPixelFormatR16Float; |
| 77 | return true; |
| 78 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 79 | SK_ABORT("Unexpected config"); |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | GrPixelConfig 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 Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 93 | case MTLPixelFormatRGB10A2Unorm: |
| 94 | return kRGBA_1010102_GrPixelConfig; |
Greg Daniel | 0576a45 | 2017-07-31 16:32:36 -0400 | [diff] [blame] | 95 | #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 Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame^] | 116 | |
| 117 | id<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 | |