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/ganesh/GrCaps.h" 13 #include "src/gpu/ganesh/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 fDrawInstancedSupport = options.fDrawInstancedSupport; 21 fHalfFloatVertexAttributeSupport = options.fHalfFloatVertexAttributeSupport; 22 fMapBufferFlags = options.fMapBufferFlags; 23 fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions. 24 fMaxTextureSize = options.fMaxTextureSize; 25 fMaxWindowRectangles = options.fMaxWindowRectangles; 26 fMaxRenderTargetSize = std::min(options.fMaxRenderTargetSize, fMaxTextureSize); 27 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize; 28 fMaxVertexAttributes = options.fMaxVertexAttributes; 29 fSampleLocationsSupport = true; 30 31 fShaderCaps = std::make_unique<GrShaderCaps>(); 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 isFormatTexturable(const GrBackendFormat & format,GrTextureType)52 bool isFormatTexturable(const GrBackendFormat& format, GrTextureType) const override { 53 SkImage::CompressionType compression = format.asMockCompressionType(); 54 if (compression != SkImage::CompressionType::kNone) { 55 return fOptions.fCompressedOptions[(int)compression].fTexturable; 56 } 57 58 auto index = static_cast<int>(format.asMockColorType()); 59 return fOptions.fConfigOptions[index].fTexturable; 60 } 61 isFormatCopyable(const GrBackendFormat & format)62 bool isFormatCopyable(const GrBackendFormat& format) const override { 63 return false; 64 } 65 66 bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format, 67 int sampleCount = 1) const override { 68 // Currently we don't allow RGB_888X to be renderable because we don't have a way to 69 // handle blends that reference dst alpha when the values in the dst alpha channel are 70 // uninitialized. 71 if (ct == GrColorType::kRGB_888x) { 72 return false; 73 } 74 return this->isFormatRenderable(format, sampleCount); 75 } 76 isFormatRenderable(const GrBackendFormat & format,int sampleCount)77 bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override { 78 if (format.asMockCompressionType() != SkImage::CompressionType::kNone) { 79 return false; // compressed formats are never renderable 80 } 81 82 return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType()); 83 } 84 85 int getRenderTargetSampleCount(int requestCount, GrColorType) const; 86 getRenderTargetSampleCount(int requestCount,const GrBackendFormat & format)87 int getRenderTargetSampleCount(int requestCount, 88 const GrBackendFormat& format) const override { 89 SkImage::CompressionType compression = format.asMockCompressionType(); 90 if (compression != SkImage::CompressionType::kNone) { 91 return 0; // no compressed format is renderable 92 } 93 94 return this->getRenderTargetSampleCount(requestCount, format.asMockColorType()); 95 } 96 maxRenderTargetSampleCount(GrColorType ct)97 int maxRenderTargetSampleCount(GrColorType ct) const { 98 switch (fOptions.fConfigOptions[(int)ct].fRenderability) { 99 case GrMockOptions::ConfigOptions::Renderability::kNo: 100 return 0; 101 case GrMockOptions::ConfigOptions::Renderability::kNonMSAA: 102 return 1; 103 case GrMockOptions::ConfigOptions::Renderability::kMSAA: 104 return kMaxSampleCnt; 105 } 106 return 0; 107 } 108 maxRenderTargetSampleCount(const GrBackendFormat & format)109 int maxRenderTargetSampleCount(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->maxRenderTargetSampleCount(format.asMockColorType()); 116 } 117 supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType)118 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType, 119 const GrBackendFormat& surfaceFormat, 120 GrColorType srcColorType) const override { 121 return {surfaceColorType, 1}; 122 } 123 surfaceSupportsReadPixels(const GrSurface *)124 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override { 125 return SurfaceReadPixelsSupport::kSupported; 126 } 127 getBackendFormatFromCompressionType(SkImage::CompressionType)128 GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override { 129 return {}; 130 } 131 getWriteSwizzle(const GrBackendFormat & format,GrColorType ct)132 skgpu::Swizzle getWriteSwizzle(const GrBackendFormat& format, GrColorType ct) const override { 133 SkASSERT(this->areColorTypeAndFormatCompatible(ct, format)); 134 return skgpu::Swizzle("rgba"); 135 } 136 137 uint64_t computeFormatKey(const GrBackendFormat&) const override; 138 139 GrProgramDesc makeDesc(GrRenderTarget*, 140 const GrProgramInfo&, 141 ProgramDescOverrideFlags) const override; 142 143 #if GR_TEST_UTILS 144 std::vector<GrTest::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 SkIRect & dstRect,const GrSurfaceProxy * src,const SkIRect & srcRect)149 bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect, 150 const GrSurfaceProxy* src, const SkIRect& srcRect) const override { 151 return true; 152 } onGetDefaultBackendFormat(GrColorType ct)153 GrBackendFormat onGetDefaultBackendFormat(GrColorType ct) const override { 154 return GrBackendFormat::MakeMock(ct, SkImage::CompressionType::kNone); 155 } 156 onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format)157 bool onAreColorTypeAndFormatCompatible(GrColorType ct, 158 const GrBackendFormat& format) const override { 159 if (ct == GrColorType::kUnknown) { 160 return false; 161 } 162 163 SkImage::CompressionType compression = format.asMockCompressionType(); 164 if (compression == SkImage::CompressionType::kETC2_RGB8_UNORM || 165 compression == SkImage::CompressionType::kBC1_RGB8_UNORM) { 166 return ct == GrColorType::kRGB_888x; // TODO: this may be too restrictive 167 } 168 if (compression == SkImage::CompressionType::kBC1_RGBA8_UNORM) { 169 return ct == GrColorType::kRGBA_8888; 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 onGetReadSwizzle(const GrBackendFormat & format,GrColorType ct)180 skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat& format, GrColorType ct) const override { 181 SkASSERT(this->areColorTypeAndFormatCompatible(ct, format)); 182 return skgpu::Swizzle("rgba"); 183 } 184 185 static const int kMaxSampleCnt = 16; 186 187 GrMockOptions fOptions; 188 using INHERITED = GrCaps; 189 }; 190 191 #endif 192