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 "GrCaps.h"
12 #include "mock/GrMockTypes.h"
13 
14 class GrMockCaps : public GrCaps {
15 public:
16     GrMockCaps(const GrContextOptions& contextOptions, const GrMockOptions& options)
17             : INHERITED(contextOptions), fOptions(options) {
18         fInstanceAttribSupport = options.fInstanceAttribSupport;
19         fMapBufferFlags = options.fMapBufferFlags;
20         fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions.
21         fMaxTextureSize = options.fMaxTextureSize;
22         fMaxRenderTargetSize = SkTMin(options.fMaxRenderTargetSize, fMaxTextureSize);
23         fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
24         fMaxVertexAttributes = options.fMaxVertexAttributes;
25 
26         fShaderCaps.reset(new GrShaderCaps(contextOptions));
27         fShaderCaps->fGeometryShaderSupport = options.fGeometryShaderSupport;
28         fShaderCaps->fTexelBufferSupport = options.fTexelBufferSupport;
29         fShaderCaps->fIntegerSupport = options.fIntegerSupport;
30         fShaderCaps->fFlatInterpolationSupport = options.fFlatInterpolationSupport;
31         fShaderCaps->fMaxVertexSamplers = options.fMaxVertexSamplers;
32         fShaderCaps->fMaxFragmentSamplers = options.fMaxFragmentSamplers;
33         fShaderCaps->fShaderDerivativeSupport = options.fShaderDerivativeSupport;
34 
35         this->applyOptionsOverrides(contextOptions);
36     }
37     bool isConfigTexturable(GrPixelConfig config) const override {
38         return fOptions.fConfigOptions[config].fTexturable;
39     }
40 
41     bool isConfigCopyable(GrPixelConfig config) const override {
42         return false;
43     }
44 
45     int getRenderTargetSampleCount(int requestCount, GrPixelConfig config) const override {
46         requestCount = SkTMax(requestCount, 1);
47         switch (fOptions.fConfigOptions[config].fRenderability) {
48             case GrMockOptions::ConfigOptions::Renderability::kNo:
49                 return 0;
50             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
51                 return requestCount > 1 ? 0 : 1;
52             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
53                 return requestCount > kMaxSampleCnt ? 0 : GrNextPow2(requestCount);
54         }
55         return 0;
56     }
57 
58     int maxRenderTargetSampleCount(GrPixelConfig config) const override {
59         switch (fOptions.fConfigOptions[config].fRenderability) {
60             case GrMockOptions::ConfigOptions::Renderability::kNo:
61                 return 0;
62             case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
63                 return 1;
64             case GrMockOptions::ConfigOptions::Renderability::kMSAA:
65                 return kMaxSampleCnt;
66         }
67         return 0;
68     }
69 
70     bool surfaceSupportsWritePixels(const GrSurface* surface) const override { return true; }
71 
72     bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
73                             bool* rectsMustMatch, bool* disallowSubrect) const override {
74         return false;
75     }
76 
77     bool validateBackendTexture(const GrBackendTexture& tex, SkColorType,
78                                 GrPixelConfig* config) const override {
79         const GrMockTextureInfo* texInfo = tex.getMockTextureInfo();
80         if (!texInfo) {
81             return false;
82         }
83 
84         *config = texInfo->fConfig;
85         return true;
86     }
87 
88     bool validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType,
89                                      GrPixelConfig*) const override {
90         return false;
91     }
92 
93     bool getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
94                                     GrPixelConfig* config) const override {
95         const GrPixelConfig* mockFormat = format.getMockFormat();
96         if (!mockFormat) {
97             return false;
98         }
99         *config = *mockFormat;
100         return true;
101     }
102 
103 private:
104     static const int kMaxSampleCnt = 16;
105 
106     GrMockOptions fOptions;
107     typedef GrCaps INHERITED;
108 };
109 
110 #endif
111