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 = SkTMin(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->fSampleVariablesSupport = true;
38         fShaderCaps->fSampleVariablesStencilSupport = true;
39 
40         this->applyOptionsOverrides(contextOptions);
41     }
42 
isFormatSRGB(const GrBackendFormat & format)43     bool isFormatSRGB(const GrBackendFormat& format) const override {
44         auto ct = format.asMockColorType();
45         return GrGetColorTypeDesc(ct).encoding() == GrColorTypeEncoding::kSRGBUnorm;
46     }
47 
48     // Mock caps doesn't support any compressed formats right now
49     bool isFormatCompressed(const GrBackendFormat&,
50                             SkImage::CompressionType* compressionType = nullptr) const override {
51         return false;
52     }
53 
isFormatTexturableAndUploadable(GrColorType,const GrBackendFormat & format)54     bool isFormatTexturableAndUploadable(GrColorType,
55                                          const GrBackendFormat& format) const override {
56         return this->isFormatTexturable(format);
57     }
isFormatTexturable(const GrBackendFormat & format)58     bool isFormatTexturable(const GrBackendFormat& format) const override {
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         return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType());
80     }
81 
getRenderTargetSampleCount(int requestCount,GrColorType ct)82     int getRenderTargetSampleCount(int requestCount, GrColorType ct) const {
83         requestCount = SkTMax(requestCount, 1);
84 
85         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
86             case GrMockOptions::ConfigOptions::Renderability::kNo:
87                 return 0;
88             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
89                 return requestCount > 1 ? 0 : 1;
90             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
91                 return requestCount > kMaxSampleCnt ? 0 : GrNextPow2(requestCount);
92         }
93         return 0;
94     }
95 
getRenderTargetSampleCount(int requestCount,const GrBackendFormat & format)96     int getRenderTargetSampleCount(int requestCount,
97                                    const GrBackendFormat& format) const override {
98         return this->getRenderTargetSampleCount(requestCount, format.asMockColorType());
99     }
100 
maxRenderTargetSampleCount(GrColorType ct)101     int maxRenderTargetSampleCount(GrColorType ct) const {
102         switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
103             case GrMockOptions::ConfigOptions::Renderability::kNo:
104                 return 0;
105             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
106                 return 1;
107             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
108                 return kMaxSampleCnt;
109         }
110         return 0;
111     }
112 
maxRenderTargetSampleCount(const GrBackendFormat & format)113     int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
114         return this->maxRenderTargetSampleCount(format.asMockColorType());
115     }
116 
bytesPerPixel(const GrBackendFormat & format)117     size_t bytesPerPixel(const GrBackendFormat& format) const override {
118         return GrColorTypeBytesPerPixel(format.asMockColorType());
119     }
120 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType)121     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
122                                                  const GrBackendFormat& surfaceFormat,
123                                                  GrColorType srcColorType) const override {
124         return {surfaceColorType, 1};
125     }
126 
surfaceSupportsReadPixels(const GrSurface *)127     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
128         return SurfaceReadPixelsSupport::kSupported;
129     }
130 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat & format,bool isAlphaChannel)131     GrColorType getYUVAColorTypeFromBackendFormat(const GrBackendFormat& format,
132                                                   bool isAlphaChannel) const override {
133         return format.asMockColorType();
134     }
135 
getBackendFormatFromCompressionType(SkImage::CompressionType)136     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override {
137         return {};
138     }
139 
getTextureSwizzle(const GrBackendFormat &,GrColorType)140     GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override {
141         return GrSwizzle();
142     }
getOutputSwizzle(const GrBackendFormat &,GrColorType)143     GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override {
144         return GrSwizzle();
145     }
146 
147 #if GR_TEST_UTILS
148     std::vector<GrCaps::TestFormatColorTypeCombination> getTestingCombinations() const override;
149 #endif
150 
151 private:
onSurfaceSupportsWritePixels(const GrSurface *)152     bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)153     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
154                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
155         return true;
156     }
onGetDefaultBackendFormat(GrColorType ct,GrRenderable)157     GrBackendFormat onGetDefaultBackendFormat(GrColorType ct, GrRenderable) const override {
158         return GrBackendFormat::MakeMock(ct);
159     }
160 
onGetConfigFromBackendFormat(const GrBackendFormat & format,GrColorType)161     GrPixelConfig onGetConfigFromBackendFormat(const GrBackendFormat& format,
162                                                GrColorType) const override {
163         return GrColorTypeToPixelConfig(format.asMockColorType());
164     }
165 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format)166     bool onAreColorTypeAndFormatCompatible(GrColorType ct,
167                                            const GrBackendFormat& format) const override {
168         if (ct == GrColorType::kUnknown) {
169             return false;
170         }
171 
172         return ct == format.asMockColorType();
173     }
174 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat &,GrColorType)175     SupportedRead onSupportedReadPixelsColorType(GrColorType srcColorType, const GrBackendFormat&,
176                                                  GrColorType) const override {
177         return SupportedRead{srcColorType, 1};
178     }
179 
180     static const int kMaxSampleCnt = 16;
181 
182     GrMockOptions fOptions;
183     typedef GrCaps INHERITED;
184 };
185 
186 #endif
187