Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -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 | #ifndef GrMtlCaps_DEFINED |
| 9 | #define GrMtlCaps_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkTDArray.h" |
| 12 | #include "src/gpu/GrCaps.h" |
| 13 | #include "src/gpu/mtl/GrMtlStencilAttachment.h" |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 14 | |
| 15 | #import <Metal/Metal.h> |
| 16 | |
| 17 | class GrShaderCaps; |
| 18 | |
| 19 | /** |
| 20 | * Stores some capabilities of a Mtl backend. |
| 21 | */ |
| 22 | class GrMtlCaps : public GrCaps { |
| 23 | public: |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 24 | typedef GrMtlStencilAttachment::Format StencilFormat; |
| 25 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 26 | GrMtlCaps(const GrContextOptions& contextOptions, id<MTLDevice> device, |
| 27 | MTLFeatureSet featureSet); |
| 28 | |
Robert Phillips | 8ff8bcc | 2019-07-29 17:03:35 -0400 | [diff] [blame] | 29 | bool isFormatSRGB(const GrBackendFormat&) const override; |
Robert Phillips | d6df7b5 | 2019-12-13 11:17:46 -0500 | [diff] [blame] | 30 | SkImage::CompressionType compressionType(const GrBackendFormat&) const override; |
Robert Phillips | f209e88 | 2019-06-25 15:59:50 -0400 | [diff] [blame] | 31 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 32 | bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override; |
| 33 | bool isFormatTexturable(const GrBackendFormat&) const override; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 34 | bool isFormatTexturable(MTLPixelFormat) const; |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 35 | |
Greg Daniel | 6980c4e | 2019-07-30 16:18:18 -0400 | [diff] [blame] | 36 | bool isFormatCopyable(const GrBackendFormat&) const override { return true; } |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 37 | |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 38 | bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format, |
| 39 | int sampleCount = 1) const override; |
| 40 | bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override; |
| 41 | bool isFormatRenderable(MTLPixelFormat, int sampleCount) const; |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 42 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 43 | int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 44 | int getRenderTargetSampleCount(int requestedCount, MTLPixelFormat) const; |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 45 | |
Greg Daniel | eadfac9 | 2019-08-02 09:03:53 -0400 | [diff] [blame] | 46 | int maxRenderTargetSampleCount(const GrBackendFormat&) const override; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 47 | int maxRenderTargetSampleCount(MTLPixelFormat) const; |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 48 | |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 49 | size_t bytesPerPixel(const GrBackendFormat&) const override; |
Greg Daniel | 8803c2d | 2019-10-08 15:02:10 -0400 | [diff] [blame] | 50 | size_t bytesPerPixel(MTLPixelFormat) const; |
| 51 | |
Brian Salomon | 01915c0 | 2019-08-02 09:57:21 -0400 | [diff] [blame] | 52 | SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType, |
| 53 | const GrBackendFormat& surfaceFormat, |
Brian Salomon | 42be09d | 2019-07-26 12:12:26 -0400 | [diff] [blame] | 54 | GrColorType srcColorType) const override; |
| 55 | |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 56 | SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override { |
| 57 | return SurfaceReadPixelsSupport::kSupported; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 58 | } |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 59 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Returns both a supported and most prefered stencil format to use in draws. |
| 62 | */ |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 63 | const StencilFormat& preferredStencilFormat() const { |
| 64 | return fPreferredStencilFormat; |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 65 | } |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 66 | |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 67 | bool canCopyAsBlit(GrSurface* dst, int dstSampleCount, GrSurface* src, int srcSampleCount, |
| 68 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 69 | bool areDstSrcSameObj) const; |
| 70 | |
| 71 | bool canCopyAsBlit(MTLPixelFormat dstFormat, int dstSampleCount, |
| 72 | MTLPixelFormat srcFormat, int srcSampleCount, |
| 73 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 74 | bool areDstSrcSameObj) const; |
| 75 | |
Jim Van Verth | d361e64 | 2019-07-11 15:40:53 -0400 | [diff] [blame] | 76 | bool canCopyAsResolve(GrSurface* dst, int dstSampleCount, GrSurface* src, int srcSampleCount, |
| 77 | const SkIRect& srcRect, const SkIPoint& dstPoint) const; |
| 78 | |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 79 | GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat&, |
| 80 | bool isAlphaChannel) const override; |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 81 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 82 | GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 83 | |
Greg Daniel | 457da50 | 2019-08-16 14:40:19 -0400 | [diff] [blame] | 84 | MTLPixelFormat getFormatFromColorType(GrColorType colorType) const { |
| 85 | int idx = static_cast<int>(colorType); |
| 86 | return fColorTypeToFormatTable[idx]; |
| 87 | } |
| 88 | |
Greg Daniel | 14b5721 | 2019-12-17 16:18:06 -0500 | [diff] [blame^] | 89 | GrSwizzle getReadSwizzle(const GrBackendFormat&, GrColorType) const override; |
Greg Daniel | eb4a827 | 2019-05-16 16:52:55 -0400 | [diff] [blame] | 90 | GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override; |
| 91 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 92 | GrProgramDesc makeDesc(const GrRenderTarget*, const GrProgramInfo&) const override; |
| 93 | |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 94 | #if GR_TEST_UTILS |
| 95 | std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override; |
| 96 | #endif |
| 97 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 98 | private: |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 99 | void initFeatureSet(MTLFeatureSet featureSet); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 100 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 101 | void initStencilFormat(const id<MTLDevice> device); |
| 102 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 103 | void initGrCaps(const id<MTLDevice> device); |
| 104 | void initShaderCaps(); |
Timothy Liang | 036fdfe | 2018-06-28 15:50:36 -0400 | [diff] [blame] | 105 | |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 106 | void initFormatTable(); |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 107 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 108 | bool onSurfaceSupportsWritePixels(const GrSurface*) const override; |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 109 | bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src, |
| 110 | const SkIRect& srcRect, const SkIPoint& dstPoint) const override; |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 111 | GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 112 | GrPixelConfig onGetConfigFromBackendFormat(const GrBackendFormat&, GrColorType) const override; |
Robert Phillips | df6c734 | 2019-12-16 13:40:51 -0500 | [diff] [blame] | 113 | GrPixelConfig onGetConfigFromCompressedBackendFormat(const GrBackendFormat&) const override; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 114 | bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override; |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 115 | |
Brian Salomon | 42be09d | 2019-07-26 12:12:26 -0400 | [diff] [blame] | 116 | SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&, |
| 117 | GrColorType) const override; |
| 118 | |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 119 | // ColorTypeInfo for a specific format |
| 120 | struct ColorTypeInfo { |
| 121 | GrColorType fColorType = GrColorType::kUnknown; |
| 122 | enum { |
| 123 | kUploadData_Flag = 0x1, |
| 124 | // Does Ganesh itself support rendering to this colorType & format pair. Renderability |
| 125 | // still additionally depends on if the format itself is renderable. |
| 126 | kRenderable_Flag = 0x2, |
| 127 | }; |
| 128 | uint32_t fFlags = 0; |
| 129 | |
Greg Daniel | 14b5721 | 2019-12-17 16:18:06 -0500 | [diff] [blame^] | 130 | GrSwizzle fReadSwizzle; |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 131 | GrSwizzle fOutputSwizzle; |
| 132 | }; |
| 133 | |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 134 | struct FormatInfo { |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 135 | uint32_t colorTypeFlags(GrColorType colorType) const { |
| 136 | for (int i = 0; i < fColorTypeInfoCount; ++i) { |
| 137 | if (fColorTypeInfos[i].fColorType == colorType) { |
| 138 | return fColorTypeInfos[i].fFlags; |
| 139 | } |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 144 | enum { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 145 | kTexturable_Flag = 0x1, |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 146 | kRenderable_Flag = 0x2, // Color attachment and blendable |
| 147 | kMSAA_Flag = 0x4, |
| 148 | kResolve_Flag = 0x8, |
| 149 | }; |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 150 | static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag | |
Jim Van Verth | d361e64 | 2019-07-11 15:40:53 -0400 | [diff] [blame] | 151 | kMSAA_Flag | kResolve_Flag; |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 152 | |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 153 | uint16_t fFlags = 0; |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 154 | |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 155 | // This value is only valid for regular formats. Compressed formats will be 0. |
Greg Daniel | 8803c2d | 2019-10-08 15:02:10 -0400 | [diff] [blame] | 156 | size_t fBytesPerPixel = 0; |
| 157 | |
Greg Daniel | ea6bb44 | 2019-08-05 15:38:40 -0400 | [diff] [blame] | 158 | std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; |
| 159 | int fColorTypeInfoCount = 0; |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 160 | }; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 161 | #ifdef SK_BUILD_FOR_IOS |
Brian Salomon | dcbc359 | 2019-09-27 14:40:20 -0400 | [diff] [blame] | 162 | static constexpr size_t kNumMtlFormats = 17; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 163 | #else |
Brian Salomon | dcbc359 | 2019-09-27 14:40:20 -0400 | [diff] [blame] | 164 | static constexpr size_t kNumMtlFormats = 14; |
Robert Phillips | 161ec32 | 2019-07-17 14:35:23 -0400 | [diff] [blame] | 165 | #endif |
| 166 | static size_t GetFormatIndex(MTLPixelFormat); |
| 167 | FormatInfo fFormatTable[kNumMtlFormats]; |
| 168 | |
| 169 | const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const { |
| 170 | size_t index = GetFormatIndex(pixelFormat); |
| 171 | return fFormatTable[index]; |
| 172 | } |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 173 | |
Greg Daniel | 457da50 | 2019-08-16 14:40:19 -0400 | [diff] [blame] | 174 | MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt]; |
| 175 | void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats); |
| 176 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 177 | enum class Platform { |
| 178 | kMac, |
| 179 | kIOS |
| 180 | }; |
| 181 | bool isMac() { return Platform::kMac == fPlatform; } |
| 182 | bool isIOS() { return Platform::kIOS == fPlatform; } |
| 183 | |
| 184 | Platform fPlatform; |
| 185 | int fFamilyGroup; |
| 186 | int fVersion; |
| 187 | |
| 188 | SkTDArray<int> fSampleCounts; |
| 189 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 190 | StencilFormat fPreferredStencilFormat; |
| 191 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 192 | typedef GrCaps INHERITED; |
| 193 | }; |
| 194 | |
| 195 | #endif |