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