• 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 GrMockCaps_DEFINED
9 #define GrMockCaps_DEFINED
10 
11 #include "include/gpu/mock/GrMockTypes.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/SkGr.h"
14 
15 class GrMockCaps : public GrCaps {
16 public:
GrMockCaps(const GrContextOptions & contextOptions,const GrMockOptions & options)17     GrMockCaps(const GrContextOptions& contextOptions, const GrMockOptions& options)
18             : INHERITED(contextOptions), fOptions(options) {
19         fMipmapSupport = options.fMipmapSupport;
20         fDrawInstancedSupport = options.fDrawInstancedSupport;
21         fHalfFloatVertexAttributeSupport = options.fHalfFloatVertexAttributeSupport;
22         fMapBufferFlags = options.fMapBufferFlags;
23         fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions.
24         fMaxTextureSize = options.fMaxTextureSize;
25         fMaxWindowRectangles = options.fMaxWindowRectangles;
26         fMaxRenderTargetSize = std::min(options.fMaxRenderTargetSize, fMaxTextureSize);
27         fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
28         fMaxVertexAttributes = options.fMaxVertexAttributes;
29         fSampleLocationsSupport = true;
30 
31         fShaderCaps = std::make_unique<GrShaderCaps>();
32         fShaderCaps->fIntegerSupport = options.fIntegerSupport;
33         fShaderCaps->fFlatInterpolationSupport = options.fFlatInterpolationSupport;
34         fShaderCaps->fMaxFragmentSamplers = options.fMaxFragmentSamplers;
35         fShaderCaps->fShaderDerivativeSupport = options.fShaderDerivativeSupport;
36         fShaderCaps->fDualSourceBlendingSupport = options.fDualSourceBlendingSupport;
37         fShaderCaps->fSampleMaskSupport = true;
38         fShaderCaps->fMaxTessellationSegments = options.fMaxTessellationSegments;
39 
40         this->finishInitialization(contextOptions);
41     }
42 
isFormatSRGB(const GrBackendFormat & format)43     bool isFormatSRGB(const GrBackendFormat& format) const override {
44         SkImage::CompressionType compression = format.asMockCompressionType();
45         if (compression != SkImage::CompressionType::kNone) {
46             return false;
47         }
48 
49         auto ct = format.asMockColorType();
50         return GrGetColorTypeDesc(ct).encoding() == GrColorTypeEncoding::kSRGBUnorm;
51     }
52 
isFormatTexturable(const GrBackendFormat & format,GrTextureType)53     bool isFormatTexturable(const GrBackendFormat& format, GrTextureType) const override {
54         SkImage::CompressionType compression = format.asMockCompressionType();
55         if (compression != SkImage::CompressionType::kNone) {
56             return fOptions.fCompressedOptions[(int)compression].fTexturable;
57         }
58 
59         auto index = static_cast<int>(format.asMockColorType());
60         return fOptions.fConfigOptions[index].fTexturable;
61     }
62 
isFormatCopyable(const GrBackendFormat & format)63     bool isFormatCopyable(const GrBackendFormat& format) const override {
64         return false;
65     }
66 
67     bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
68                                        int sampleCount = 1) const override {
69         // Currently we don't allow RGB_888X to be renderable because we don't have a way to
70         // handle blends that reference dst alpha when the values in the dst alpha channel are
71         // uninitialized.
72         if (ct == GrColorType::kRGB_888x) {
73             return false;
74         }
75         return this->isFormatRenderable(format, sampleCount);
76     }
77 
isFormatRenderable(const GrBackendFormat & format,int sampleCount)78     bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override {
79         if (format.asMockCompressionType() != SkImage::CompressionType::kNone) {
80             return false;  // compressed formats are never renderable
81         }
82 
83         return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType());
84     }
85 
86     int getRenderTargetSampleCount(int requestCount, GrColorType) const;
87 
getRenderTargetSampleCount(int requestCount,const GrBackendFormat & format)88     int getRenderTargetSampleCount(int requestCount,
89                                    const GrBackendFormat& format) const override {
90         SkImage::CompressionType compression = format.asMockCompressionType();
91         if (compression != SkImage::CompressionType::kNone) {
92             return 0; // no compressed format is renderable
93         }
94 
95         return this->getRenderTargetSampleCount(requestCount, format.asMockColorType());
96     }
97 
maxRenderTargetSampleCount(GrColorType ct)98     int maxRenderTargetSampleCount(GrColorType ct) const {
99         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
100             case GrMockOptions::ConfigOptions::Renderability::kNo:
101                 return 0;
102             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
103                 return 1;
104             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
105                 return kMaxSampleCnt;
106         }
107         return 0;
108     }
109 
maxRenderTargetSampleCount(const GrBackendFormat & format)110     int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
111         SkImage::CompressionType compression = format.asMockCompressionType();
112         if (compression != SkImage::CompressionType::kNone) {
113             return 0; // no compressed format is renderable
114         }
115 
116         return this->maxRenderTargetSampleCount(format.asMockColorType());
117     }
118 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType)119     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
120                                                  const GrBackendFormat& surfaceFormat,
121                                                  GrColorType srcColorType) const override {
122         return {surfaceColorType, 1};
123     }
124 
surfaceSupportsReadPixels(const GrSurface *)125     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
126         return SurfaceReadPixelsSupport::kSupported;
127     }
128 
getBackendFormatFromCompressionType(SkImage::CompressionType)129     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override {
130         return {};
131     }
132 
getWriteSwizzle(const GrBackendFormat & format,GrColorType ct)133     GrSwizzle getWriteSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
134         SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
135         return GrSwizzle("rgba");
136     }
137 
138     uint64_t computeFormatKey(const GrBackendFormat&) const override;
139 
140     GrProgramDesc makeDesc(GrRenderTarget*,
141                            const GrProgramInfo&,
142                            ProgramDescOverrideFlags) const override;
143 
144 #if GR_TEST_UTILS
145     std::vector<GrCaps::TestFormatColorTypeCombination> getTestingCombinations() const override;
146 #endif
147 
148 private:
onSurfaceSupportsWritePixels(const GrSurface *)149     bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)150     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
151                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
152         return true;
153     }
onGetDefaultBackendFormat(GrColorType ct)154     GrBackendFormat onGetDefaultBackendFormat(GrColorType ct) const override {
155         return GrBackendFormat::MakeMock(ct, SkImage::CompressionType::kNone);
156     }
157 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format)158     bool onAreColorTypeAndFormatCompatible(GrColorType ct,
159                                            const GrBackendFormat& format) const override {
160         if (ct == GrColorType::kUnknown) {
161             return false;
162         }
163 
164         SkImage::CompressionType compression = format.asMockCompressionType();
165         if (compression == SkImage::CompressionType::kETC2_RGB8_UNORM ||
166             compression == SkImage::CompressionType::kBC1_RGB8_UNORM) {
167             return ct == GrColorType::kRGB_888x; // TODO: this may be too restrictive
168         }
169         if (compression == SkImage::CompressionType::kBC1_RGBA8_UNORM) {
170             return ct == GrColorType::kRGBA_8888;
171         }
172 
173         return ct == format.asMockColorType();
174     }
175 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat &,GrColorType)176     SupportedRead onSupportedReadPixelsColorType(GrColorType srcColorType, const GrBackendFormat&,
177                                                  GrColorType) const override {
178         return SupportedRead{srcColorType, 1};
179     }
180 
onGetReadSwizzle(const GrBackendFormat & format,GrColorType ct)181     GrSwizzle onGetReadSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
182         SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
183         return GrSwizzle("rgba");
184     }
185 
186     static const int kMaxSampleCnt = 16;
187 
188     GrMockOptions fOptions;
189     using INHERITED = GrCaps;
190 };
191 
192 #endif
193