• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #include "src/gpu/dawn/GrDawnCaps.h"
9 
GrDawnCaps(const GrContextOptions & contextOptions)10 GrDawnCaps::GrDawnCaps(const GrContextOptions& contextOptions) : INHERITED(contextOptions) {
11     fMipMapSupport = true;
12     fBufferMapThreshold = SK_MaxS32;  // FIXME: get this from Dawn?
13     fShaderCaps.reset(new GrShaderCaps(contextOptions));
14     fMaxTextureSize = fMaxRenderTargetSize = 4096; // FIXME
15     fMaxVertexAttributes = 16; // FIXME
16     fClampToBorderSupport = false;
17     fPerformPartialClearsAsDraws = true;
18 
19     fShaderCaps->fFlatInterpolationSupport = true;
20     fShaderCaps->fIntegerSupport = true;
21     // FIXME: each fragment sampler takes two binding slots in Dawn (sampler + texture). Limit to
22     // 6 * 2 = 12, since kMaxBindingsPerGroup is 16 in Dawn, and we need to keep a few for
23     // non-texture bindings. Eventually, we may be able to increase kMaxBindingsPerGroup in Dawn.
24     fShaderCaps->fMaxFragmentSamplers = 6;
25     fShaderCaps->fShaderDerivativeSupport = true;
26 
27     this->applyOptionsOverrides(contextOptions);
28     fShaderCaps->applyOptionsOverrides(contextOptions);
29 }
30 
isFormatSRGB(const GrBackendFormat & format) const31 bool GrDawnCaps::isFormatSRGB(const GrBackendFormat& format) const {
32     return false;
33 }
34 
isFormatCompressed(const GrBackendFormat & format) const35 bool GrDawnCaps::isFormatCompressed(const GrBackendFormat& format) const {
36     return false;
37 }
38 
isFormatTexturable(const GrBackendFormat & format) const39 bool GrDawnCaps::isFormatTexturable(const GrBackendFormat& format) const {
40     // Currently, all the formats in GrDawnFormatToPixelConfig are texturable.
41     dawn::TextureFormat dawnFormat;
42     return format.asDawnFormat(&dawnFormat);
43 }
44 
onGetConfigFromBackendFormat(const GrBackendFormat & format,GrColorType colorType) const45 GrPixelConfig GrDawnCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format,
46                                                        GrColorType colorType) const {
47     dawn::TextureFormat dawnFormat;
48     if (!format.asDawnFormat(&dawnFormat)) {
49         return kUnknown_GrPixelConfig;
50     }
51     switch (colorType) {
52         case GrColorType::kUnknown:
53             return kUnknown_GrPixelConfig;
54         case GrColorType::kAlpha_8:
55             if (dawn::TextureFormat::R8Unorm == dawnFormat) {
56                 return kAlpha_8_as_Red_GrPixelConfig;
57             }
58             break;
59         case GrColorType::kRGBA_8888:
60             if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
61                 return kRGBA_8888_GrPixelConfig;
62             } else if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
63                 // FIXME: This shouldn't be necessary, but on some platforms (Mac)
64                 // Skia byte order is RGBA, while preferred swap format is BGRA.
65                 return kBGRA_8888_GrPixelConfig;
66             }
67             break;
68         case GrColorType::kRGB_888x:
69             break;
70         case GrColorType::kBGRA_8888:
71             if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
72                 return kBGRA_8888_GrPixelConfig;
73             } else if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
74                 return kRGBA_8888_GrPixelConfig;
75             }
76             break;
77         default:
78             break;
79     }
80     return kUnknown_GrPixelConfig;
81 }
82 
get_swizzle(const GrBackendFormat & format,GrColorType colorType,bool forOutput)83 static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
84                              bool forOutput) {
85     switch (colorType) {
86         case GrColorType::kAlpha_8: // fall through
87         case GrColorType::kAlpha_F16:
88             if (forOutput) {
89                 return GrSwizzle::AAAA();
90             } else {
91                 return GrSwizzle::RRRR();
92             }
93         case GrColorType::kGray_8:
94             if (!forOutput) {
95                 return GrSwizzle::RRRA();
96             }
97             break;
98         case GrColorType::kRGB_888x:
99             if (!forOutput) {
100                 return GrSwizzle::RGB1();
101             }
102         default:
103             return GrSwizzle::RGBA();
104     }
105     return GrSwizzle::RGBA();
106 }
107 
isFormatTexturableAndUploadable(GrColorType ct,const GrBackendFormat & format) const108 bool GrDawnCaps::isFormatTexturableAndUploadable(GrColorType ct,
109                                                  const GrBackendFormat& format) const {
110     dawn::TextureFormat dawnFormat;
111     if (!format.asDawnFormat(&dawnFormat)) {
112         return false;
113     }
114     switch (ct) {
115         case GrColorType::kAlpha_8:
116             return dawn::TextureFormat::R8Unorm == dawnFormat;
117         case GrColorType::kRGBA_8888:
118         case GrColorType::kRGB_888x:
119         case GrColorType::kBGRA_8888:
120             return dawn::TextureFormat::RGBA8Unorm == dawnFormat ||
121                    dawn::TextureFormat::BGRA8Unorm == dawnFormat;
122         default:
123             return false;
124     }
125 }
126 
isFormatRenderable(const GrBackendFormat & format,int sampleCount) const127 bool GrDawnCaps::isFormatRenderable(const GrBackendFormat& format,
128                                     int sampleCount) const {
129     dawn::TextureFormat dawnFormat;
130     if (!format.isValid() || sampleCount > 1 || !format.asDawnFormat(&dawnFormat)) {
131         return false;
132     }
133 
134     return GrDawnFormatIsRenderable(dawnFormat);
135 }
136 
isFormatAsColorTypeRenderable(GrColorType ct,const GrBackendFormat & format,int sampleCount) const137 bool GrDawnCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
138                                                int sampleCount) const {
139     return isFormatRenderable(format, sampleCount);
140 }
141 
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & backendFormat) const142 int GrDawnCaps::getRenderTargetSampleCount(int requestedCount,
143                                            const GrBackendFormat& backendFormat) const {
144     dawn::TextureFormat dawnFormat;
145     if (!backendFormat.asDawnFormat(&dawnFormat)) {
146         return 0;
147     }
148     return GrDawnFormatIsRenderable(dawnFormat) ? 1 : 0;
149 }
150 
maxRenderTargetSampleCount(const GrBackendFormat & format) const151 int GrDawnCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
152     return format.isValid() ? 1 : 0;
153 }
154 
onGetDefaultBackendFormat(GrColorType ct,GrRenderable renderable) const155 GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct,
156                                                       GrRenderable renderable) const {
157     GrPixelConfig config = GrColorTypeToPixelConfig(ct);
158     if (config == kUnknown_GrPixelConfig) {
159         return GrBackendFormat();
160     }
161     dawn::TextureFormat format;
162     if (!GrPixelConfigToDawnFormat(config, &format)) {
163         return GrBackendFormat();
164     }
165     return GrBackendFormat::MakeDawn(format);
166 }
167 
getBackendFormatFromCompressionType(SkImage::CompressionType type) const168 GrBackendFormat GrDawnCaps::getBackendFormatFromCompressionType(SkImage::CompressionType type) const
169 {
170     return GrBackendFormat();
171 }
172 
getTextureSwizzle(const GrBackendFormat & format,GrColorType colorType) const173 GrSwizzle GrDawnCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const
174 {
175     return get_swizzle(format, colorType, false);
176 }
177 
canClearTextureOnCreation() const178 bool GrDawnCaps::canClearTextureOnCreation() const {
179     return true;
180 }
181 
getOutputSwizzle(const GrBackendFormat & format,GrColorType colorType) const182 GrSwizzle GrDawnCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const
183 {
184     return get_swizzle(format, colorType, true);
185 }
186 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format) const187 bool GrDawnCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
188                                                    const GrBackendFormat& format) const {
189     return true;
190 }
191 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat &,bool isAlphaChannel) const192 GrColorType GrDawnCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat&,
193                                                           bool isAlphaChannel) const {
194     return GrColorType::kUnknown;
195 }
196 
197 #if GR_TEST_UTILS
getTestingCombinations() const198 std::vector<GrCaps::TestFormatColorTypeCombination> GrDawnCaps::getTestingCombinations() const {
199     std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
200         { GrColorType::kAlpha_8,   GrBackendFormat::MakeDawn(dawn::TextureFormat::R8Unorm)    },
201         { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
202         { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
203         { GrColorType::kRGB_888x,  GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
204         { GrColorType::kRGB_888x,  GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
205         { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
206         { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
207     };
208 
209 #ifdef SK_DEBUG
210     for (auto combo : combos) {
211         SkASSERT(this->onAreColorTypeAndFormatCompatible(combo.fColorType, combo.fFormat));
212     }
213 #endif
214     return combos;
215 }
216 #endif
217