Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 12 | #import <metal/metal.h> |
| 13 | |
| 14 | // In BackendAllocationTest.cpp |
| 15 | void test_wrapping(GrContext* context, skiatest::Reporter* reporter, |
| 16 | std::function<GrBackendTexture (GrContext*, GrRenderable)> createMtd, |
| 17 | SkColorType colorType, GrRenderable renderable); |
| 18 | |
| 19 | DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) { |
| 20 | GrContext* context = ctxInfo.grContext(); |
| 21 | const GrCaps* caps = context->priv().caps(); |
| 22 | |
| 23 | struct { |
| 24 | SkColorType fColorType; |
| 25 | GrMTLPixelFormat fFormat; |
| 26 | // TODO: remove 'fConfig' and directly use 'fFormat' in GrMtlCaps::isFormatTexturable |
| 27 | GrPixelConfig fConfig; |
| 28 | } combinations[] = { |
| 29 | { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm, kRGBA_8888_GrPixelConfig }, |
| 30 | { kRGBA_8888_SkColorType, MTLPixelFormatRGBA8Unorm_sRGB, kSRGBA_8888_GrPixelConfig }, |
| 31 | |
| 32 | { kRGB_888x_SkColorType, MTLPixelFormatRGBA8Unorm, kRGBA_8888_GrPixelConfig }, |
| 33 | |
| 34 | { kBGRA_8888_SkColorType, MTLPixelFormatBGRA8Unorm, kBGRA_8888_GrPixelConfig }, |
| 35 | { kBGRA_8888_SkColorType, MTLPixelFormatBGRA8Unorm_sRGB, kSBGRA_8888_GrPixelConfig }, |
| 36 | |
| 37 | { kRGBA_1010102_SkColorType, MTLPixelFormatRGB10A2Unorm, kRGBA_1010102_GrPixelConfig }, |
| 38 | #ifdef SK_BUILD_FOR_IOS |
| 39 | { kRGB_565_SkColorType, MTLPixelFormatB5G6R5Unorm, kRGB_565_GrPixelConfig }, |
| 40 | { kARGB_4444_SkColorType, MTLPixelFormatABGR4Unorm, kRGBA_4444_GrPixelConfig }, |
| 41 | #endif |
| 42 | |
| 43 | { kAlpha_8_SkColorType, MTLPixelFormatA8Unorm, kAlpha_8_as_Alpha_GrPixelConfig }, |
| 44 | { kAlpha_8_SkColorType, MTLPixelFormatR8Unorm, kAlpha_8_as_Red_GrPixelConfig }, |
| 45 | |
| 46 | { kGray_8_SkColorType, MTLPixelFormatR8Unorm, kGray_8_as_Red_GrPixelConfig }, |
| 47 | |
| 48 | { kRGBA_F32_SkColorType, MTLPixelFormatRGBA32Float, kRGBA_float_GrPixelConfig }, |
| 49 | |
| 50 | { kRGBA_F16Norm_SkColorType, MTLPixelFormatRGBA16Float, kRGBA_half_Clamped_GrPixelConfig }, |
| 51 | { kRGBA_F16_SkColorType, MTLPixelFormatRGBA16Float, kRGBA_half_GrPixelConfig }, |
| 52 | |
| 53 | // These backend formats don't have SkColorType equivalents |
| 54 | { kUnknown_SkColorType, MTLPixelFormatRG32Float, kRG_float_GrPixelConfig }, |
| 55 | { kUnknown_SkColorType, MTLPixelFormatRG8Unorm, kRG_88_GrPixelConfig }, |
| 56 | { kUnknown_SkColorType, MTLPixelFormatR16Float, kAlpha_half_as_Red_GrPixelConfig }, |
| 57 | #ifdef SK_BUILD_FOR_IOS |
| 58 | { kUnknown_SkColorType, MTLPixelFormatETC2_RGB8, kRGB_ETC1_GrPixelConfig } |
| 59 | #endif |
| 60 | }; |
| 61 | |
| 62 | for (auto combo : combinations) { |
| 63 | GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat); |
| 64 | |
| 65 | if (!caps->isConfigTexturable(combo.fConfig)) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly) |
| 70 | if (kRGBA_F32_SkColorType == combo.fColorType) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
| 75 | |
| 76 | if (GrRenderable::kYes == renderable) { |
| 77 | if (kRGB_888x_SkColorType == combo.fColorType) { |
| 78 | // Ganesh can't perform the blends correctly when rendering this format |
| 79 | continue; |
| 80 | } |
| 81 | if (!caps->isConfigRenderable(combo.fConfig)) { |
| 82 | continue; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | auto createMtd = [format](GrContext* context, GrRenderable renderable) { |
| 87 | return context->priv().createBackendTexture(32, 32, format, |
| 88 | GrMipMapped::kNo, renderable); |
| 89 | }; |
| 90 | |
| 91 | test_wrapping(context, reporter, createMtd, combo.fColorType, renderable); |
| 92 | } |
| 93 | } |
| 94 | } |