blob: 6b830d63ff7aa026209ee447515788fabda002f7 [file] [log] [blame]
Greg Danielcebcb842017-07-31 10:45:52 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTDArray.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/mtl/GrMtlStencilAttachment.h"
Greg Danielcebcb842017-07-31 10:45:52 -040014
15#import <Metal/Metal.h>
16
17class GrShaderCaps;
18
19/**
20 * Stores some capabilities of a Mtl backend.
21 */
22class GrMtlCaps : public GrCaps {
23public:
Ethan Nicholas01063512018-10-08 16:58:25 -040024 typedef GrMtlStencilAttachment::Format StencilFormat;
25
Greg Danielcebcb842017-07-31 10:45:52 -040026 GrMtlCaps(const GrContextOptions& contextOptions, id<MTLDevice> device,
27 MTLFeatureSet featureSet);
28
Robert Phillips8ff8bcc2019-07-29 17:03:35 -040029 bool isFormatSRGB(const GrBackendFormat&) const override;
Robert Phillipsd6df7b52019-12-13 11:17:46 -050030 SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
Robert Phillipsf209e882019-06-25 15:59:50 -040031
Greg Daniel7bfc9132019-08-14 14:23:53 -040032 bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
33 bool isFormatTexturable(const GrBackendFormat&) const override;
Robert Phillips161ec322019-07-17 14:35:23 -040034 bool isFormatTexturable(MTLPixelFormat) const;
Robert Phillipsd8f79a22019-06-24 13:25:42 -040035
Greg Daniel6980c4e2019-07-30 16:18:18 -040036 bool isFormatCopyable(const GrBackendFormat&) const override { return true; }
Greg Danielcebcb842017-07-31 10:45:52 -040037
Greg Daniel900583a2019-08-06 12:05:31 -040038 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 Phillips62221e72019-07-24 15:07:38 -040042
Greg Daniel6fa62e22019-08-07 15:52:37 -040043 int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override;
Robert Phillips161ec322019-07-17 14:35:23 -040044 int getRenderTargetSampleCount(int requestedCount, MTLPixelFormat) const;
Robert Phillipsd8f79a22019-06-24 13:25:42 -040045
Greg Danieleadfac92019-08-02 09:03:53 -040046 int maxRenderTargetSampleCount(const GrBackendFormat&) const override;
Robert Phillips161ec322019-07-17 14:35:23 -040047 int maxRenderTargetSampleCount(MTLPixelFormat) const;
Greg Danielcebcb842017-07-31 10:45:52 -040048
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040049 size_t bytesPerPixel(const GrBackendFormat&) const override;
Greg Daniel8803c2d2019-10-08 15:02:10 -040050 size_t bytesPerPixel(MTLPixelFormat) const;
51
Brian Salomon01915c02019-08-02 09:57:21 -040052 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
53 const GrBackendFormat& surfaceFormat,
Brian Salomon42be09d2019-07-26 12:12:26 -040054 GrColorType srcColorType) const override;
55
Brian Salomondc0710f2019-07-01 14:59:32 -040056 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
57 return SurfaceReadPixelsSupport::kSupported;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040058 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -050059
Greg Danielcebcb842017-07-31 10:45:52 -040060 /**
61 * Returns both a supported and most prefered stencil format to use in draws.
62 */
Ethan Nicholas01063512018-10-08 16:58:25 -040063 const StencilFormat& preferredStencilFormat() const {
64 return fPreferredStencilFormat;
Greg Danielcebcb842017-07-31 10:45:52 -040065 }
Ethan Nicholas01063512018-10-08 16:58:25 -040066
Jim Van Verth9187e492019-11-11 16:14:13 -050067 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 Liange35055f2018-07-20 16:53:00 -040074 bool areDstSrcSameObj) const;
75
Jim Van Verthd361e642019-07-11 15:40:53 -040076 bool canCopyAsResolve(GrSurface* dst, int dstSampleCount, GrSurface* src, int srcSampleCount,
77 const SkIRect& srcRect, const SkIPoint& dstPoint) const;
78
Robert Phillips00c9f0d2019-08-02 17:17:35 -040079 GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat&,
80 bool isAlphaChannel) const override;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -040081
Brian Salomonbb8dde82019-06-27 10:52:13 -040082 GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
Greg Daniel4065d452018-11-16 15:43:41 -050083
Greg Daniel457da502019-08-16 14:40:19 -040084 MTLPixelFormat getFormatFromColorType(GrColorType colorType) const {
85 int idx = static_cast<int>(colorType);
86 return fColorTypeToFormatTable[idx];
87 }
88
Greg Daniel14b57212019-12-17 16:18:06 -050089 GrSwizzle getReadSwizzle(const GrBackendFormat&, GrColorType) const override;
Greg Danieleb4a8272019-05-16 16:52:55 -040090 GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override;
91
Robert Phillips03e4c952019-11-26 16:20:22 -050092 GrProgramDesc makeDesc(const GrRenderTarget*, const GrProgramInfo&) const override;
93
Robert Phillipsffe27292019-08-01 10:08:07 -040094#if GR_TEST_UTILS
95 std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override;
96#endif
97
Greg Danielfaa095e2017-12-19 13:15:02 -050098private:
Greg Danielcebcb842017-07-31 10:45:52 -040099 void initFeatureSet(MTLFeatureSet featureSet);
Ben Wagner63fd7602017-10-09 15:45:33 -0400100
Ethan Nicholas01063512018-10-08 16:58:25 -0400101 void initStencilFormat(const id<MTLDevice> device);
102
Greg Danielcebcb842017-07-31 10:45:52 -0400103 void initGrCaps(const id<MTLDevice> device);
104 void initShaderCaps();
Timothy Liang036fdfe2018-06-28 15:50:36 -0400105
Robert Phillips161ec322019-07-17 14:35:23 -0400106 void initFormatTable();
Greg Danielcebcb842017-07-31 10:45:52 -0400107
Jim Van Verth686046b2019-03-18 15:39:22 -0400108 bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
Brian Salomonc67c31c2018-12-06 10:00:03 -0500109 bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
110 const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400111 GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
Greg Daniel627d0532019-07-08 16:48:14 -0400112 GrPixelConfig onGetConfigFromBackendFormat(const GrBackendFormat&, GrColorType) const override;
Robert Phillipsdf6c7342019-12-16 13:40:51 -0500113 GrPixelConfig onGetConfigFromCompressedBackendFormat(const GrBackendFormat&) const override;
Greg Daniel627d0532019-07-08 16:48:14 -0400114 bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
Brian Salomonc67c31c2018-12-06 10:00:03 -0500115
Brian Salomon42be09d2019-07-26 12:12:26 -0400116 SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
117 GrColorType) const override;
118
Greg Danielea6bb442019-08-05 15:38:40 -0400119 // 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 Daniel14b57212019-12-17 16:18:06 -0500130 GrSwizzle fReadSwizzle;
Greg Danielea6bb442019-08-05 15:38:40 -0400131 GrSwizzle fOutputSwizzle;
132 };
133
Robert Phillips161ec322019-07-17 14:35:23 -0400134 struct FormatInfo {
Greg Danielea6bb442019-08-05 15:38:40 -0400135 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 Danielcebcb842017-07-31 10:45:52 -0400144 enum {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400145 kTexturable_Flag = 0x1,
Greg Danielcebcb842017-07-31 10:45:52 -0400146 kRenderable_Flag = 0x2, // Color attachment and blendable
147 kMSAA_Flag = 0x4,
148 kResolve_Flag = 0x8,
149 };
Greg Daniel7bfc9132019-08-14 14:23:53 -0400150 static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag |
Jim Van Verthd361e642019-07-11 15:40:53 -0400151 kMSAA_Flag | kResolve_Flag;
Greg Danielcebcb842017-07-31 10:45:52 -0400152
Brian Salomond4764a12019-08-08 12:08:24 -0400153 uint16_t fFlags = 0;
Greg Danielea6bb442019-08-05 15:38:40 -0400154
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400155 // This value is only valid for regular formats. Compressed formats will be 0.
Greg Daniel8803c2d2019-10-08 15:02:10 -0400156 size_t fBytesPerPixel = 0;
157
Greg Danielea6bb442019-08-05 15:38:40 -0400158 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
159 int fColorTypeInfoCount = 0;
Greg Danielcebcb842017-07-31 10:45:52 -0400160 };
Robert Phillips161ec322019-07-17 14:35:23 -0400161#ifdef SK_BUILD_FOR_IOS
Brian Salomondcbc3592019-09-27 14:40:20 -0400162 static constexpr size_t kNumMtlFormats = 17;
Robert Phillips161ec322019-07-17 14:35:23 -0400163#else
Brian Salomondcbc3592019-09-27 14:40:20 -0400164 static constexpr size_t kNumMtlFormats = 14;
Robert Phillips161ec322019-07-17 14:35:23 -0400165#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 Danielcebcb842017-07-31 10:45:52 -0400173
Greg Daniel457da502019-08-16 14:40:19 -0400174 MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt];
175 void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats);
176
Greg Danielcebcb842017-07-31 10:45:52 -0400177 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 Nicholas01063512018-10-08 16:58:25 -0400190 StencilFormat fPreferredStencilFormat;
191
Greg Danielcebcb842017-07-31 10:45:52 -0400192 typedef GrCaps INHERITED;
193};
194
195#endif