blob: 201ea104fbae46c914c497f154594905772cc399 [file] [log] [blame]
Stephen White985741a2019-07-18 11:43:45 -04001/*
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
Mike Klein52337de2019-07-25 09:00:52 -05008#include "src/gpu/dawn/GrDawnCaps.h"
Stephen White985741a2019-07-18 11:43:45 -04009
10GrDawnCaps::GrDawnCaps(const GrContextOptions& contextOptions) : INHERITED(contextOptions) {
11 fBufferMapThreshold = SK_MaxS32; // FIXME: get this from Dawn?
12 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Stephen Whited67c71f2019-08-06 11:15:41 -040013 fMaxTextureSize = 2048; // FIXME
14 fMaxVertexAttributes = 16; // FIXME
Stephen White985741a2019-07-18 11:43:45 -040015 fPerformPartialClearsAsDraws = true;
Stephen Whitef813ef72019-08-09 12:28:37 -040016
17 this->applyOptionsOverrides(contextOptions);
18 fShaderCaps->applyOptionsOverrides(contextOptions);
Stephen White985741a2019-07-18 11:43:45 -040019}
20
21bool GrDawnCaps::isFormatSRGB(const GrBackendFormat& format) const {
22 return false;
23}
24
Robert Phillips8ff8bcc2019-07-29 17:03:35 -040025bool GrDawnCaps::isFormatCompressed(const GrBackendFormat& format) const {
26 return false;
27}
28
Stephen White985741a2019-07-18 11:43:45 -040029bool GrDawnCaps::isConfigTexturable(GrPixelConfig config) const {
30 switch (config) {
31 case kRGBA_8888_GrPixelConfig:
32 case kBGRA_8888_GrPixelConfig:
33 case kAlpha_8_GrPixelConfig:
34 return true;
35 default:
36 return false;
37 }
38}
39
40GrPixelConfig GrDawnCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format,
Stephen Whited7325182019-08-02 17:22:59 -040041 GrColorType colorType) const {
Brian Salomond4764a12019-08-08 12:08:24 -040042 dawn::TextureFormat dawnFormat;
43 if (!format.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -040044 return kUnknown_GrPixelConfig;
45 }
Stephen White985741a2019-07-18 11:43:45 -040046 switch (colorType) {
47 case GrColorType::kUnknown:
48 return kUnknown_GrPixelConfig;
49 case GrColorType::kAlpha_8:
Brian Salomond4764a12019-08-08 12:08:24 -040050 if (dawn::TextureFormat::R8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040051 return kAlpha_8_as_Red_GrPixelConfig;
52 }
53 break;
54 case GrColorType::kRGBA_8888:
Brian Salomond4764a12019-08-08 12:08:24 -040055 if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040056 return kRGBA_8888_GrPixelConfig;
Stephen Whitefe55c732019-08-09 12:24:30 -040057 } else if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
Stephen Whited67c71f2019-08-06 11:15:41 -040058 // FIXME: This shouldn't be necessary, but on some platforms (Mac)
59 // Skia byte order is RGBA, while preferred swap format is BGRA.
60 return kBGRA_8888_GrPixelConfig;
Stephen White985741a2019-07-18 11:43:45 -040061 }
62 break;
63 case GrColorType::kRGB_888x:
64 break;
65 case GrColorType::kBGRA_8888:
Brian Salomond4764a12019-08-08 12:08:24 -040066 if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040067 return kBGRA_8888_GrPixelConfig;
Stephen Whitefe55c732019-08-09 12:24:30 -040068 } else if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
Stephen Whited67c71f2019-08-06 11:15:41 -040069 return kRGBA_8888_GrPixelConfig;
Stephen White985741a2019-07-18 11:43:45 -040070 }
71 break;
72 default:
73 break;
74 }
75 return kUnknown_GrPixelConfig;
76}
77
Stephen White985741a2019-07-18 11:43:45 -040078static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
79 bool forOutput) {
Brian Salomond4764a12019-08-08 12:08:24 -040080 SkDEBUGCODE(dawn::TextureFormat format;)
81 SkASSERT(format.asDawnFormat(&format));
Stephen White985741a2019-07-18 11:43:45 -040082
83 switch (colorType) {
84 case GrColorType::kAlpha_8: // fall through
85 case GrColorType::kAlpha_F16:
86 if (forOutput) {
87 return GrSwizzle::AAAA();
88 } else {
89 return GrSwizzle::RRRR();
90 }
91 case GrColorType::kGray_8:
92 if (!forOutput) {
93 return GrSwizzle::RRRA();
94 }
95 break;
96 case GrColorType::kRGB_888x:
97 if (!forOutput) {
98 return GrSwizzle::RGB1();
99 }
100 default:
101 return GrSwizzle::RGBA();
102 }
103 return GrSwizzle::RGBA();
104}
105
106bool GrDawnCaps::isFormatTexturable(GrColorType ct, const GrBackendFormat& format) const {
107 GrPixelConfig config = this->getConfigFromBackendFormat(format, ct);
108 if (kUnknown_GrPixelConfig == config) {
109 return false;
110 }
111
112 return this->isConfigTexturable(config);
113}
114
Stephen Whitefe55c732019-08-09 12:24:30 -0400115bool GrDawnCaps::isFormatRenderable(const GrBackendFormat& format,
Stephen Whited7325182019-08-02 17:22:59 -0400116 int sampleCount) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400117 dawn::TextureFormat dawnFormat;
118 if (!format.isValid() || sampleCount > 1 || !format.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -0400119 return false;
Stephen White985741a2019-07-18 11:43:45 -0400120 }
121
Brian Salomond4764a12019-08-08 12:08:24 -0400122 return GrDawnFormatIsRenderable(dawnFormat);
Stephen Whited7325182019-08-02 17:22:59 -0400123}
124
Stephen Whitefe55c732019-08-09 12:24:30 -0400125bool GrDawnCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
126 int sampleCount) const {
127 return isFormatRenderable(format, sampleCount);
128}
129
130int GrDawnCaps::getRenderTargetSampleCount(int requestedCount,
Stephen Whited7325182019-08-02 17:22:59 -0400131 const GrBackendFormat& backendFormat) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400132 dawn::TextureFormat dawnFormat;
Stephen Whitefe55c732019-08-09 12:24:30 -0400133 if (!backendFormat.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -0400134 return 0;
135 }
Brian Salomond4764a12019-08-08 12:08:24 -0400136 return GrDawnFormatIsRenderable(dawnFormat) ? 1 : 0;
Stephen Whited7325182019-08-02 17:22:59 -0400137}
138
Stephen Whited7325182019-08-02 17:22:59 -0400139int GrDawnCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
140 return format.isValid() ? 1 : 0;
Stephen White985741a2019-07-18 11:43:45 -0400141}
142
Robert Phillips0a15cc62019-07-30 12:49:10 -0400143GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct,
144 GrRenderable renderable) const {
Stephen White985741a2019-07-18 11:43:45 -0400145 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
146 if (config == kUnknown_GrPixelConfig) {
147 return GrBackendFormat();
148 }
149 dawn::TextureFormat format;
150 if (!GrPixelConfigToDawnFormat(config, &format)) {
151 return GrBackendFormat();
152 }
153 return GrBackendFormat::MakeDawn(format);
154}
155
156GrBackendFormat GrDawnCaps::getBackendFormatFromCompressionType(SkImage::CompressionType type) const
157{
158 return GrBackendFormat();
159}
160
161GrSwizzle GrDawnCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const
162{
163 return get_swizzle(format, colorType, false);
164}
165
166bool GrDawnCaps::canClearTextureOnCreation() const {
167 return true;
168}
169
170GrSwizzle GrDawnCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const
171{
172 return get_swizzle(format, colorType, true);
173}
174
175bool GrDawnCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
176 const GrBackendFormat& format) const {
177 return true;
178}
Stephen Whiteb05ca082019-07-29 11:39:28 -0400179
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400180GrColorType GrDawnCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat&,
181 bool isAlphaChannel) const {
Stephen Whiteb05ca082019-07-29 11:39:28 -0400182 return GrColorType::kUnknown;
183}
Stephen Whited7325182019-08-02 17:22:59 -0400184
185#if GR_TEST_UTILS
186std::vector<GrCaps::TestFormatColorTypeCombination> GrDawnCaps::getTestingCombinations() const {
187 std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
188 { GrColorType::kAlpha_8, GrBackendFormat::MakeDawn(dawn::TextureFormat::R8Unorm) },
189 { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
190 { GrColorType::kRGB_888x, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
191 { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
192 };
193
194#ifdef SK_DEBUG
195 for (auto combo : combos) {
196 SkASSERT(this->onAreColorTypeAndFormatCompatible(combo.fColorType, combo.fFormat));
197 }
198#endif
199 return combos;
200}
201#endif