• 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         fInstanceAttribSupport = options.fInstanceAttribSupport;
21         fHalfFloatVertexAttributeSupport = options.fHalfFloatVertexAttributeSupport;
22         fMapBufferFlags = options.fMapBufferFlags;
23         fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions.
24         fMaxTextureSize = options.fMaxTextureSize;
25         fMaxRenderTargetSize = std::min(options.fMaxRenderTargetSize, fMaxTextureSize);
26         fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
27         fMaxVertexAttributes = options.fMaxVertexAttributes;
28         fSampleLocationsSupport = true;
29 
30         fShaderCaps.reset(new GrShaderCaps(contextOptions));
31         fShaderCaps->fGeometryShaderSupport = options.fGeometryShaderSupport;
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 
39         this->finishInitialization(contextOptions);
40     }
41 
isFormatSRGB(const GrBackendFormat & format)42     bool isFormatSRGB(const GrBackendFormat& format) const override {
43         SkImage::CompressionType compression = format.asMockCompressionType();
44         if (compression != SkImage::CompressionType::kNone) {
45             return false;
46         }
47 
48         auto ct = format.asMockColorType();
49         return GrGetColorTypeDesc(ct).encoding() == GrColorTypeEncoding::kSRGBUnorm;
50     }
51 
compressionType(const GrBackendFormat & format)52     SkImage::CompressionType compressionType(const GrBackendFormat& format) const override {
53         return format.asMockCompressionType();
54     }
55 
isFormatTexturableAndUploadable(GrColorType,const GrBackendFormat & format)56     bool isFormatTexturableAndUploadable(GrColorType,
57                                          const GrBackendFormat& format) const override {
58         return this->isFormatTexturable(format);
59     }
60 
isFormatTexturable(const GrBackendFormat & format)61     bool isFormatTexturable(const GrBackendFormat& format) const override {
62         SkImage::CompressionType compression = format.asMockCompressionType();
63         if (compression != SkImage::CompressionType::kNone) {
64             return fOptions.fCompressedOptions[(int)compression].fTexturable;
65         }
66 
67         auto index = static_cast<int>(format.asMockColorType());
68         return fOptions.fConfigOptions[index].fTexturable;
69     }
70 
isFormatCopyable(const GrBackendFormat & format)71     bool isFormatCopyable(const GrBackendFormat& format) const override {
72         return false;
73     }
74 
75     bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
76                                        int sampleCount = 1) const override {
77         // Currently we don't allow RGB_888X to be renderable because we don't have a way to
78         // handle blends that reference dst alpha when the values in the dst alpha channel are
79         // uninitialized.
80         if (ct == GrColorType::kRGB_888x) {
81             return false;
82         }
83         return this->isFormatRenderable(format, sampleCount);
84     }
85 
isFormatRenderable(const GrBackendFormat & format,int sampleCount)86     bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override {
87         if (format.asMockCompressionType() != SkImage::CompressionType::kNone) {
88             return false;  // compressed formats are never renderable
89         }
90 
91         return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType());
92     }
93 
getRenderTargetSampleCount(int requestCount,GrColorType ct)94     int getRenderTargetSampleCount(int requestCount, GrColorType ct) const {
95         requestCount = std::max(requestCount, 1);
96 
97         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
98             case GrMockOptions::ConfigOptions::Renderability::kNo:
99                 return 0;
100             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
101                 return requestCount > 1 ? 0 : 1;
102             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
103                 return requestCount > kMaxSampleCnt ? 0 : GrNextPow2(requestCount);
104         }
105         return 0;
106     }
107 
getRenderTargetSampleCount(int requestCount,const GrBackendFormat & format)108     int getRenderTargetSampleCount(int requestCount,
109                                    const GrBackendFormat& format) const override {
110         SkImage::CompressionType compression = format.asMockCompressionType();
111         if (compression != SkImage::CompressionType::kNone) {
112             return 0; // no compressed format is renderable
113         }
114 
115         return this->getRenderTargetSampleCount(requestCount, format.asMockColorType());
116     }
117 
maxRenderTargetSampleCount(GrColorType ct)118     int maxRenderTargetSampleCount(GrColorType ct) const {
119         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
120             case GrMockOptions::ConfigOptions::Renderability::kNo:
121                 return 0;
122             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
123                 return 1;
124             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
125                 return kMaxSampleCnt;
126         }
127         return 0;
128     }
129 
maxRenderTargetSampleCount(const GrBackendFormat & format)130     int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
131         SkImage::CompressionType compression = format.asMockCompressionType();
132         if (compression != SkImage::CompressionType::kNone) {
133             return 0; // no compressed format is renderable
134         }
135 
136         return this->maxRenderTargetSampleCount(format.asMockColorType());
137     }
138 
bytesPerPixel(const GrBackendFormat & format)139     size_t bytesPerPixel(const GrBackendFormat& format) const override {
140         SkImage::CompressionType compression = format.asMockCompressionType();
141         if (compression != SkImage::CompressionType::kNone) {
142             return 0;
143         }
144 
145         return GrColorTypeBytesPerPixel(format.asMockColorType());
146     }
147 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType)148     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
149                                                  const GrBackendFormat& surfaceFormat,
150                                                  GrColorType srcColorType) const override {
151         return {surfaceColorType, 1};
152     }
153 
surfaceSupportsReadPixels(const GrSurface *)154     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
155         return SurfaceReadPixelsSupport::kSupported;
156     }
157 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat & format,bool isAlphaChannel)158     GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat& format,
159                                                   bool isAlphaChannel) const override {
160         SkImage::CompressionType compression = format.asMockCompressionType();
161         if (compression != SkImage::CompressionType::kNone) {
162             return GrColorType::kUnknown;
163         }
164 
165         return format.asMockColorType();
166     }
167 
getBackendFormatFromCompressionType(SkImage::CompressionType)168     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override {
169         return {};
170     }
171 
getReadSwizzle(const GrBackendFormat &,GrColorType)172     GrSwizzle getReadSwizzle(const GrBackendFormat&, GrColorType) const override {
173         return GrSwizzle();
174     }
getOutputSwizzle(const GrBackendFormat &,GrColorType)175     GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override {
176         return GrSwizzle();
177     }
178 
179     uint64_t computeFormatKey(const GrBackendFormat&) const override;
180 
181     GrProgramDesc makeDesc(const GrRenderTarget*, const GrProgramInfo&) const override;
182 
183 #if GR_TEST_UTILS
184     std::vector<GrCaps::TestFormatColorTypeCombination> getTestingCombinations() const override;
185 #endif
186 
187 private:
onSurfaceSupportsWritePixels(const GrSurface *)188     bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)189     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
190                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
191         return true;
192     }
onGetDefaultBackendFormat(GrColorType ct,GrRenderable)193     GrBackendFormat onGetDefaultBackendFormat(GrColorType ct, GrRenderable) const override {
194         return GrBackendFormat::MakeMock(ct, SkImage::CompressionType::kNone);
195     }
196 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format)197     bool onAreColorTypeAndFormatCompatible(GrColorType ct,
198                                            const GrBackendFormat& format) const override {
199         if (ct == GrColorType::kUnknown) {
200             return false;
201         }
202 
203         SkImage::CompressionType compression = format.asMockCompressionType();
204         if (compression == SkImage::CompressionType::kETC2_RGB8_UNORM ||
205             compression == SkImage::CompressionType::kBC1_RGB8_UNORM) {
206             return ct == GrColorType::kRGB_888x; // TODO: this may be too restrictive
207         }
208         if (compression == SkImage::CompressionType::kBC1_RGBA8_UNORM) {
209             return ct == GrColorType::kRGBA_8888;
210         }
211 
212         return ct == format.asMockColorType();
213     }
214 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat &,GrColorType)215     SupportedRead onSupportedReadPixelsColorType(GrColorType srcColorType, const GrBackendFormat&,
216                                                  GrColorType) const override {
217         return SupportedRead{srcColorType, 1};
218     }
219 
220     static const int kMaxSampleCnt = 16;
221 
222     GrMockOptions fOptions;
223     typedef GrCaps INHERITED;
224 };
225 
226 #endif
227