• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "include/private/SkTDArray.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/mtl/GrMtlStencilAttachment.h"
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:
24     typedef GrMtlStencilAttachment::Format StencilFormat;
25 
26     GrMtlCaps(const GrContextOptions& contextOptions, id<MTLDevice> device,
27               MTLFeatureSet featureSet);
28 
29     bool isFormatSRGB(const GrBackendFormat&) const override;
30     bool isFormatCompressed(const GrBackendFormat&) const override;
31 
32     bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
33     bool isFormatTexturable(const GrBackendFormat&) const override;
34     bool isFormatTexturable(MTLPixelFormat) const;
35 
isFormatCopyable(const GrBackendFormat &)36     bool isFormatCopyable(const GrBackendFormat&) const override { return true; }
37 
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;
42 
43     int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override;
44     int getRenderTargetSampleCount(int requestedCount, MTLPixelFormat) const;
45 
46     int maxRenderTargetSampleCount(const GrBackendFormat&) const override;
47     int maxRenderTargetSampleCount(MTLPixelFormat) const;
48 
49     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
50                                                  const GrBackendFormat& surfaceFormat,
51                                                  GrColorType srcColorType) const override;
52 
surfaceSupportsReadPixels(const GrSurface *)53     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
54         return SurfaceReadPixelsSupport::kSupported;
55     }
56 
57     /**
58      * Returns both a supported and most prefered stencil format to use in draws.
59      */
preferredStencilFormat()60     const StencilFormat& preferredStencilFormat() const {
61         return fPreferredStencilFormat;
62     }
63 
64     bool canCopyAsBlit(MTLPixelFormat dstFormat, int dstSampleCount, MTLPixelFormat srcFormat,
65                        int srcSampleCount, const SkIRect& srcRect, const SkIPoint& dstPoint,
66                        bool areDstSrcSameObj) const;
67 
68     bool canCopyAsResolve(GrSurface* dst, int dstSampleCount, GrSurface* src, int srcSampleCount,
69                           const SkIRect& srcRect, const SkIPoint& dstPoint) const;
70 
71     GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat&,
72                                                   bool isAlphaChannel) const override;
73 
74     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
75 
getFormatFromColorType(GrColorType colorType)76     MTLPixelFormat getFormatFromColorType(GrColorType colorType) const {
77         int idx = static_cast<int>(colorType);
78         return fColorTypeToFormatTable[idx];
79     }
80 
canClearTextureOnCreation()81     bool canClearTextureOnCreation() const override { return true; }
82 
83     GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override;
84     GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override;
85 
86 #if GR_TEST_UTILS
87     std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override;
88 #endif
89 
90 private:
91     void initFeatureSet(MTLFeatureSet featureSet);
92 
93     void initStencilFormat(const id<MTLDevice> device);
94 
95     void initGrCaps(const id<MTLDevice> device);
96     void initShaderCaps();
97 
98     void initFormatTable();
99 
100     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
101     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
102                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
103     GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
104     GrPixelConfig onGetConfigFromBackendFormat(const GrBackendFormat&, GrColorType) const override;
105     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
106 
107     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
108                                                  GrColorType) const override;
109 
110     // ColorTypeInfo for a specific format
111     struct ColorTypeInfo {
112         GrColorType fColorType = GrColorType::kUnknown;
113         enum {
114             kUploadData_Flag = 0x1,
115             // Does Ganesh itself support rendering to this colorType & format pair. Renderability
116             // still additionally depends on if the format itself is renderable.
117             kRenderable_Flag = 0x2,
118         };
119         uint32_t fFlags = 0;
120 
121         GrSwizzle fTextureSwizzle;
122         GrSwizzle fOutputSwizzle;
123     };
124 
125     struct FormatInfo {
colorTypeFlagsFormatInfo126         uint32_t colorTypeFlags(GrColorType colorType) const {
127             for (int i = 0; i < fColorTypeInfoCount; ++i) {
128                 if (fColorTypeInfos[i].fColorType == colorType) {
129                     return fColorTypeInfos[i].fFlags;
130                 }
131             }
132             return 0;
133         }
134 
135         enum {
136             kTexturable_Flag  = 0x1,
137             kRenderable_Flag  = 0x2, // Color attachment and blendable
138             kMSAA_Flag        = 0x4,
139             kResolve_Flag     = 0x8,
140         };
141         static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag |
142                                           kMSAA_Flag | kResolve_Flag;
143 
144         uint16_t fFlags = 0;
145 
146         std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
147         int fColorTypeInfoCount = 0;
148     };
149 #ifdef SK_BUILD_FOR_IOS
150     static constexpr size_t kNumMtlFormats = 18;
151 #else
152     static constexpr size_t kNumMtlFormats = 15;
153 #endif
154     static size_t GetFormatIndex(MTLPixelFormat);
155     FormatInfo fFormatTable[kNumMtlFormats];
156 
getFormatInfo(const MTLPixelFormat pixelFormat)157     const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const {
158         size_t index = GetFormatIndex(pixelFormat);
159         return fFormatTable[index];
160     }
161 
162     MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt];
163     void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats);
164 
165     enum class Platform {
166         kMac,
167         kIOS
168     };
isMac()169     bool isMac() { return Platform::kMac == fPlatform; }
isIOS()170     bool isIOS() { return Platform::kIOS == fPlatform; }
171 
172     Platform fPlatform;
173     int fFamilyGroup;
174     int fVersion;
175 
176     SkTDArray<int> fSampleCounts;
177 
178     StencilFormat fPreferredStencilFormat;
179 
180     typedef GrCaps INHERITED;
181 };
182 
183 #endif
184