From 0576a45c7570686a1057ad07d8abf21a3bad403f Mon Sep 17 00:00:00 2001 From: Greg Daniel Date: Mon, 31 Jul 2017 16:32:36 -0400 Subject: Add GrMtlUtil class Currently just adding support functions to go back and forth between GrPixelConfigs and MTLPixelFormats. Bug: skia: Change-Id: I01a7d6877ebed87b87090ac2b920fee45dc0e856 Reviewed-on: https://skia-review.googlesource.com/29080 Reviewed-by: Brian Salomon Commit-Queue: Greg Daniel --- gn/gpu.gni | 2 + src/gpu/mtl/GrMtlUtil.h | 26 ++++++++++++ src/gpu/mtl/GrMtlUtil.mm | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 src/gpu/mtl/GrMtlUtil.h create mode 100644 src/gpu/mtl/GrMtlUtil.mm diff --git a/gn/gpu.gni b/gn/gpu.gni index 49e3361ad2..9bd28e19f1 100644 --- a/gn/gpu.gni +++ b/gn/gpu.gni @@ -587,6 +587,8 @@ skia_metal_sources = [ "$_src/gpu/mtl/GrMtlGpu.mm", "$_src/gpu/mtl/GrMtlTrampoline.h", "$_src/gpu/mtl/GrMtlTrampoline.mm", + "$_src/gpu/mtl/GrMtlUtil.h", + "$_src/gpu/mtl/GrMtlUtil.mm", ] skia_native_gpu_sources = [ diff --git a/src/gpu/mtl/GrMtlUtil.h b/src/gpu/mtl/GrMtlUtil.h new file mode 100644 index 0000000000..78af7bf89f --- /dev/null +++ b/src/gpu/mtl/GrMtlUtil.h @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef GrMtlUtil_DEFINED +#define GrMtlUtil_DEFINED + +#include "GrTypes.h" + +#import + +/** + * Returns the Metal texture format for the given GrPixelConfig + */ +bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format); + +/** +* Returns the GrPixelConfig for the given Metal texture format +*/ +GrPixelConfig GrMTLFormatToPixelConfig(MTLPixelFormat format); + + +#endif diff --git a/src/gpu/mtl/GrMtlUtil.mm b/src/gpu/mtl/GrMtlUtil.mm new file mode 100644 index 0000000000..9c6e3f0eca --- /dev/null +++ b/src/gpu/mtl/GrMtlUtil.mm @@ -0,0 +1,103 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "GrMtlUtil.h" + +bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format) { + MTLPixelFormat dontCare; + if (!format) { + format = &dontCare; + } + + switch (config) { + case kUnknown_GrPixelConfig: + return false; + case kRGBA_8888_GrPixelConfig: + *format = MTLPixelFormatRGBA8Unorm; + return true; + case kBGRA_8888_GrPixelConfig: + *format = MTLPixelFormatBGRA8Unorm; + return true; + case kSRGBA_8888_GrPixelConfig: + *format = MTLPixelFormatRGBA8Unorm_sRGB; + return true; + case kSBGRA_8888_GrPixelConfig: + *format = MTLPixelFormatBGRA8Unorm_sRGB; + return true; + case kRGBA_8888_sint_GrPixelConfig: + *format = MTLPixelFormatRGBA8Sint; + return true; + case kRGB_565_GrPixelConfig: +#ifdef SK_BUILD_FOR_IOS + *format = MTLPixelFormatR5G6B5Unorm; + return true; +#else + return false; +#endif + case kRGBA_4444_GrPixelConfig: +#ifdef SK_BUILD_FOR_IOS + *format = MTLPixelFormatABGR4Unorm; + return true; +#else + return false; +#endif + case kAlpha_8_GrPixelConfig: + *format = MTLPixelFormatR8Unorm; + return true; + case kGray_8_GrPixelConfig: + *format = MTLPixelFormatR8Unorm; + return true; + case kRGBA_float_GrPixelConfig: + *format = MTLPixelFormatRGBA32Float; + return true; + case kRG_float_GrPixelConfig: + *format = MTLPixelFormatRG32Float; + return true; + case kRGBA_half_GrPixelConfig: + *format = MTLPixelFormatRGBA16Float; + return true; + case kAlpha_half_GrPixelConfig: + *format = MTLPixelFormatR16Float; + return true; + } + SkFAIL("Unexpected config"); + return false; +} + +GrPixelConfig GrMTLFormatToPixelConfig(MTLPixelFormat format) { + switch (format) { + case MTLPixelFormatRGBA8Unorm: + return kRGBA_8888_GrPixelConfig; + case MTLPixelFormatBGRA8Unorm: + return kBGRA_8888_GrPixelConfig; + case MTLPixelFormatRGBA8Unorm_sRGB: + return kSRGBA_8888_GrPixelConfig; + case MTLPixelFormatBGRA8Unorm_sRGB: + return kSBGRA_8888_GrPixelConfig; + case MTLPixelFormatRGBA8Sint: + return kRGBA_8888_sint_GrPixelConfig; +#ifdef SK_BUILD_FOR_IOS + case MTLPixelFormatB5G6R5Unorm: + return kRGB_565_GrPixelConfig; + case MTLPixelFormatABGR4Unorm: + return kRGBA_4444_GrPixelConfig; +#endif + case MTLPixelFormatR8Unorm: + // We currently set this to be Alpha_8 and have no way to go to Gray_8 + return kAlpha_8_GrPixelConfig; + case MTLPixelFormatRGBA32Float: + return kRGBA_float_GrPixelConfig; + case MTLPixelFormatRG32Float: + return kRG_float_GrPixelConfig; + case MTLPixelFormatRGBA16Float: + return kRGBA_half_GrPixelConfig; + case MTLPixelFormatR16Float: + return kAlpha_half_GrPixelConfig; + default: + return kUnknown_GrPixelConfig; + } +} -- cgit v1.2.3