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#include "src/gpu/mtl/GrMtlCaps.h" 14 15// In BackendAllocationTest.cpp 16void test_wrapping(GrContext* context, skiatest::Reporter* reporter, 17 std::function<GrBackendTexture (GrContext*, 18 GrMipMapped, 19 GrRenderable)> create, 20 GrColorType colorType, GrMipMapped mipMapped, GrRenderable renderable); 21 22void test_color_init(GrContext* context, skiatest::Reporter* reporter, 23 std::function<GrBackendTexture (GrContext*, 24 const SkColor4f&, 25 GrMipMapped, 26 GrRenderable)> create, 27 GrColorType colorType, const SkColor4f& color, 28 GrMipMapped mipMapped, GrRenderable renderable); 29 30DEF_GPUTEST_FOR_METAL_CONTEXT(MtlBackendAllocationTest, reporter, ctxInfo) { 31 GrContext* context = ctxInfo.grContext(); 32 const GrMtlCaps* mtlCaps = static_cast<const GrMtlCaps*>(context->priv().caps()); 33 34 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; 35 36 struct { 37 GrColorType fColorType; 38 MTLPixelFormat fFormat; 39 SkColor4f fColor; 40 } combinations[] = { 41 { GrColorType::kRGBA_8888, MTLPixelFormatRGBA8Unorm, SkColors::kRed }, 42 { GrColorType::kRGBA_8888_SRGB, MTLPixelFormatRGBA8Unorm_sRGB, SkColors::kRed }, 43 44 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format), 45 // there is nothing to tell Skia to make the provided color opaque. Clients will need 46 // to provide an opaque initialization color in this case. 47 { GrColorType::kRGB_888x, MTLPixelFormatRGBA8Unorm, SkColors::kYellow }, 48 49 { GrColorType::kBGRA_8888, MTLPixelFormatBGRA8Unorm, SkColors::kBlue }, 50 51 { GrColorType::kRGBA_1010102, MTLPixelFormatRGB10A2Unorm, { 0.5f, 0, 0, 1.0f } }, 52#ifdef SK_BUILD_FOR_IOS 53 { GrColorType::kBGR_565, MTLPixelFormatB5G6R5Unorm, SkColors::kRed }, 54 { GrColorType::kABGR_4444, MTLPixelFormatABGR4Unorm, SkColors::kGreen }, 55#endif 56 57 { GrColorType::kAlpha_8, MTLPixelFormatA8Unorm, kTransCol }, 58 { GrColorType::kAlpha_8, MTLPixelFormatR8Unorm, kTransCol }, 59 { GrColorType::kGray_8, MTLPixelFormatR8Unorm, SkColors::kDkGray }, 60 61 { GrColorType::kRGBA_F32, MTLPixelFormatRGBA32Float, SkColors::kRed }, 62 63 { GrColorType::kRGBA_F16_Clamped, MTLPixelFormatRGBA16Float, SkColors::kLtGray }, 64 { GrColorType::kRGBA_F16, MTLPixelFormatRGBA16Float, SkColors::kYellow }, 65 66 { GrColorType::kRG_88, MTLPixelFormatRG8Unorm, { 0.5f, 0.5f, 0, 0 } }, 67 { GrColorType::kAlpha_F16, MTLPixelFormatR16Float, { 1.0f, 0, 0, 0.5f } }, 68 69 { GrColorType::kR_16, MTLPixelFormatR16Unorm, SkColors::kRed }, 70 { GrColorType::kRG_1616, MTLPixelFormatRG16Unorm, SkColors::kYellow }, 71 72 // Experimental (for Y416 and mutant P016/P010) 73 { GrColorType::kRGBA_16161616, MTLPixelFormatRGBA16Unorm, SkColors::kLtGray }, 74 { GrColorType::kRG_F16, MTLPixelFormatRG16Float, SkColors::kYellow }, 75 76#ifdef SK_BUILD_FOR_IOS 77 { GrColorType::kUnknown, MTLPixelFormatETC2_RGB8, SkColors::kRed } 78#endif 79 }; 80 81 for (auto combo : combinations) { 82 GrBackendFormat format = GrBackendFormat::MakeMtl(combo.fFormat); 83 84 if (!mtlCaps->isFormatTexturable(combo.fFormat)) { 85 continue; 86 } 87 88 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly) 89 if (GrColorType::kRGBA_F32 == combo.fColorType) { 90 continue; 91 } 92 93 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { 94 if (GrMipMapped::kYes == mipMapped && !mtlCaps->mipMapSupport()) { 95 continue; 96 } 97 98 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { 99 100 if (GrRenderable::kYes == renderable) { 101 // We must also check whether we allow rendering to the format using the 102 // color type. 103 if (!mtlCaps->isFormatAsColorTypeRenderable( 104 combo.fColorType, GrBackendFormat::MakeMtl(combo.fFormat), 1)) { 105 continue; 106 } 107 } 108 109#ifdef SK_BUILD_FOR_IOS 110 // We current disallow uninitialized compressed textures in the Metal backend 111 if (combo.fFormat != MTLPixelFormatETC2_RGB8) 112#endif 113 { 114 auto uninitCreateMtd = [format](GrContext* context, 115 GrMipMapped mipMapped, 116 GrRenderable renderable) { 117 return context->createBackendTexture(32, 32, format, 118 mipMapped, renderable, 119 GrProtected::kNo); 120 }; 121 122 test_wrapping(context, reporter, uninitCreateMtd, 123 combo.fColorType, mipMapped, renderable); 124 } 125 126 // Not implemented for Metal yet 127 { 128 auto createWithColorMtd = [format](GrContext* context, 129 const SkColor4f& color, 130 GrMipMapped mipMapped, 131 GrRenderable renderable) { 132 return context->createBackendTexture(32, 32, format, color, 133 mipMapped, renderable); 134 }; 135 136 test_color_init(context, reporter, createWithColorMtd, 137 combo.fColorType, combo.fColor, mipMapped, renderable); 138 } 139 } 140 } 141 } 142} 143