• 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         fInstanceAttribSupport = options.fInstanceAttribSupport;
20         fHalfFloatVertexAttributeSupport = options.fHalfFloatVertexAttributeSupport;
21         fMapBufferFlags = options.fMapBufferFlags;
22         fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions.
23         fMaxTextureSize = options.fMaxTextureSize;
24         fMaxRenderTargetSize = SkTMin(options.fMaxRenderTargetSize, fMaxTextureSize);
25         fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
26         fMaxVertexAttributes = options.fMaxVertexAttributes;
27         fSampleLocationsSupport = true;
28 
29         fShaderCaps.reset(new GrShaderCaps(contextOptions));
30         fShaderCaps->fGeometryShaderSupport = options.fGeometryShaderSupport;
31         fShaderCaps->fIntegerSupport = options.fIntegerSupport;
32         fShaderCaps->fFlatInterpolationSupport = options.fFlatInterpolationSupport;
33         fShaderCaps->fMaxFragmentSamplers = options.fMaxFragmentSamplers;
34         fShaderCaps->fShaderDerivativeSupport = options.fShaderDerivativeSupport;
35         fShaderCaps->fDualSourceBlendingSupport = options.fDualSourceBlendingSupport;
36         fShaderCaps->fSampleVariablesSupport = true;
37         fShaderCaps->fSampleVariablesStencilSupport = true;
38 
39         this->applyOptionsOverrides(contextOptions);
40     }
41 
isFormatSRGB(const GrBackendFormat & format)42     bool isFormatSRGB(const GrBackendFormat& format) const override {
43         auto ct = format.asMockColorType();
44         return GrGetColorTypeDesc(ct).encoding() == GrColorTypeEncoding::kSRGBUnorm;
45     }
46 
47     // Mock caps doesn't support any compressed formats right now
isFormatCompressed(const GrBackendFormat &)48     bool isFormatCompressed(const GrBackendFormat&) const override {
49         return false;
50     }
51 
isFormatTexturableAndUploadable(GrColorType,const GrBackendFormat & format)52     bool isFormatTexturableAndUploadable(GrColorType,
53                                          const GrBackendFormat& format) const override {
54         return this->isFormatTexturable(format);
55     }
isFormatTexturable(const GrBackendFormat & format)56     bool isFormatTexturable(const GrBackendFormat& format) const override {
57         auto index = static_cast<int>(format.asMockColorType());
58         return fOptions.fConfigOptions[index].fTexturable;
59     }
60 
isFormatCopyable(const GrBackendFormat & format)61     bool isFormatCopyable(const GrBackendFormat& format) const override {
62         return false;
63     }
64 
65     bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
66                                        int sampleCount = 1) const override {
67         // Currently we don't allow RGB_888X to be renderable because we don't have a way to
68         // handle blends that reference dst alpha when the values in the dst alpha channel are
69         // uninitialized.
70         if (ct == GrColorType::kRGB_888x) {
71             return false;
72         }
73         return this->isFormatRenderable(format, sampleCount);
74     }
75 
isFormatRenderable(const GrBackendFormat & format,int sampleCount)76     bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override {
77         return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType());
78     }
79 
getRenderTargetSampleCount(int requestCount,GrColorType ct)80     int getRenderTargetSampleCount(int requestCount, GrColorType ct) const {
81         requestCount = SkTMax(requestCount, 1);
82 
83         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
84             case GrMockOptions::ConfigOptions::Renderability::kNo:
85                 return 0;
86             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
87                 return requestCount > 1 ? 0 : 1;
88             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
89                 return requestCount > kMaxSampleCnt ? 0 : GrNextPow2(requestCount);
90         }
91         return 0;
92     }
93 
getRenderTargetSampleCount(int requestCount,const GrBackendFormat & format)94     int getRenderTargetSampleCount(int requestCount,
95                                    const GrBackendFormat& format) const override {
96         return this->getRenderTargetSampleCount(requestCount, format.asMockColorType());
97     }
98 
maxRenderTargetSampleCount(GrColorType ct)99     int maxRenderTargetSampleCount(GrColorType ct) const {
100         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
101             case GrMockOptions::ConfigOptions::Renderability::kNo:
102                 return 0;
103             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
104                 return 1;
105             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
106                 return kMaxSampleCnt;
107         }
108         return 0;
109     }
110 
maxRenderTargetSampleCount(const GrBackendFormat & format)111     int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
112         return this->maxRenderTargetSampleCount(format.asMockColorType());
113     }
114 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType)115     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
116                                                  const GrBackendFormat& surfaceFormat,
117                                                  GrColorType srcColorType) const override {
118         return {surfaceColorType, 1};
119     }
120 
surfaceSupportsReadPixels(const GrSurface *)121     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
122         return SurfaceReadPixelsSupport::kSupported;
123     }
124 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat & format,bool isAlphaChannel)125     GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat& format,
126                                                   bool isAlphaChannel) const override {
127         return format.asMockColorType();
128     }
129 
getBackendFormatFromCompressionType(SkImage::CompressionType)130     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override {
131         return {};
132     }
133 
canClearTextureOnCreation()134     bool canClearTextureOnCreation() const override { return true; }
135 
getTextureSwizzle(const GrBackendFormat &,GrColorType)136     GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override {
137         return GrSwizzle();
138     }
getOutputSwizzle(const GrBackendFormat &,GrColorType)139     GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override {
140         return GrSwizzle();
141     }
142 
143 #if GR_TEST_UTILS
144     std::vector<GrCaps::TestFormatColorTypeCombination> getTestingCombinations() const override;
145 #endif
146 
147 private:
onSurfaceSupportsWritePixels(const GrSurface *)148     bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)149     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
150                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
151         return true;
152     }
onGetDefaultBackendFormat(GrColorType ct,GrRenderable)153     GrBackendFormat onGetDefaultBackendFormat(GrColorType ct, GrRenderable) const override {
154         return GrBackendFormat::MakeMock(ct);
155     }
156 
onGetConfigFromBackendFormat(const GrBackendFormat & format,GrColorType)157     GrPixelConfig onGetConfigFromBackendFormat(const GrBackendFormat& format,
158                                                GrColorType) const override {
159         return GrColorTypeToPixelConfig(format.asMockColorType());
160     }
161 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format)162     bool onAreColorTypeAndFormatCompatible(GrColorType ct,
163                                            const GrBackendFormat& format) const override {
164         if (ct == GrColorType::kUnknown) {
165             return false;
166         }
167 
168         return ct == format.asMockColorType();
169     }
170 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat &,GrColorType)171     SupportedRead onSupportedReadPixelsColorType(GrColorType srcColorType, const GrBackendFormat&,
172                                                  GrColorType) const override {
173         return SupportedRead{srcColorType, 1};
174     }
175 
176     static const int kMaxSampleCnt = 16;
177 
178     GrMockOptions fOptions;
179     typedef GrCaps INHERITED;
180 };
181 
182 #endif
183