blob: 490b5aa24e7a3508b041a2bfe9d4d6945ab0718e [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 White6db3b7b2019-08-15 16:48:05 -040029bool GrDawnCaps::isFormatTexturable(const GrBackendFormat& format) const {
30 // Currently, all the formats in GrDawnFormatToPixelConfig are texturable.
31 dawn::TextureFormat dawnFormat;
32 return format.asDawnFormat(&dawnFormat);
Stephen White985741a2019-07-18 11:43:45 -040033}
34
35GrPixelConfig GrDawnCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format,
Stephen Whited7325182019-08-02 17:22:59 -040036 GrColorType colorType) const {
Brian Salomond4764a12019-08-08 12:08:24 -040037 dawn::TextureFormat dawnFormat;
38 if (!format.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -040039 return kUnknown_GrPixelConfig;
40 }
Stephen White985741a2019-07-18 11:43:45 -040041 switch (colorType) {
42 case GrColorType::kUnknown:
43 return kUnknown_GrPixelConfig;
44 case GrColorType::kAlpha_8:
Brian Salomond4764a12019-08-08 12:08:24 -040045 if (dawn::TextureFormat::R8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040046 return kAlpha_8_as_Red_GrPixelConfig;
47 }
48 break;
49 case GrColorType::kRGBA_8888:
Brian Salomond4764a12019-08-08 12:08:24 -040050 if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040051 return kRGBA_8888_GrPixelConfig;
Stephen Whitefe55c732019-08-09 12:24:30 -040052 } else if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
Stephen Whited67c71f2019-08-06 11:15:41 -040053 // FIXME: This shouldn't be necessary, but on some platforms (Mac)
54 // Skia byte order is RGBA, while preferred swap format is BGRA.
55 return kBGRA_8888_GrPixelConfig;
Stephen White985741a2019-07-18 11:43:45 -040056 }
57 break;
58 case GrColorType::kRGB_888x:
59 break;
60 case GrColorType::kBGRA_8888:
Brian Salomond4764a12019-08-08 12:08:24 -040061 if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) {
Stephen White985741a2019-07-18 11:43:45 -040062 return kBGRA_8888_GrPixelConfig;
Stephen Whitefe55c732019-08-09 12:24:30 -040063 } else if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) {
Stephen Whited67c71f2019-08-06 11:15:41 -040064 return kRGBA_8888_GrPixelConfig;
Stephen White985741a2019-07-18 11:43:45 -040065 }
66 break;
67 default:
68 break;
69 }
70 return kUnknown_GrPixelConfig;
71}
72
Stephen White985741a2019-07-18 11:43:45 -040073static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
74 bool forOutput) {
Stephen White985741a2019-07-18 11:43:45 -040075 switch (colorType) {
76 case GrColorType::kAlpha_8: // fall through
77 case GrColorType::kAlpha_F16:
78 if (forOutput) {
79 return GrSwizzle::AAAA();
80 } else {
81 return GrSwizzle::RRRR();
82 }
83 case GrColorType::kGray_8:
84 if (!forOutput) {
85 return GrSwizzle::RRRA();
86 }
87 break;
88 case GrColorType::kRGB_888x:
89 if (!forOutput) {
90 return GrSwizzle::RGB1();
91 }
92 default:
93 return GrSwizzle::RGBA();
94 }
95 return GrSwizzle::RGBA();
96}
97
Stephen White6db3b7b2019-08-15 16:48:05 -040098bool GrDawnCaps::isFormatTexturableAndUploadable(GrColorType ct,
99 const GrBackendFormat& format) const {
100 dawn::TextureFormat dawnFormat;
101 if (!format.asDawnFormat(&dawnFormat)) {
Stephen White985741a2019-07-18 11:43:45 -0400102 return false;
103 }
Stephen White6db3b7b2019-08-15 16:48:05 -0400104 switch (ct) {
105 case GrColorType::kAlpha_8:
106 return dawn::TextureFormat::R8Unorm == dawnFormat;
107 case GrColorType::kRGBA_8888:
108 case GrColorType::kRGB_888x:
109 case GrColorType::kBGRA_8888:
110 return dawn::TextureFormat::RGBA8Unorm == dawnFormat ||
111 dawn::TextureFormat::BGRA8Unorm == dawnFormat;
112 default:
113 return false;
114 }
Stephen White985741a2019-07-18 11:43:45 -0400115}
116
Stephen Whitefe55c732019-08-09 12:24:30 -0400117bool GrDawnCaps::isFormatRenderable(const GrBackendFormat& format,
Stephen Whited7325182019-08-02 17:22:59 -0400118 int sampleCount) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400119 dawn::TextureFormat dawnFormat;
120 if (!format.isValid() || sampleCount > 1 || !format.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -0400121 return false;
Stephen White985741a2019-07-18 11:43:45 -0400122 }
123
Brian Salomond4764a12019-08-08 12:08:24 -0400124 return GrDawnFormatIsRenderable(dawnFormat);
Stephen Whited7325182019-08-02 17:22:59 -0400125}
126
Stephen Whitefe55c732019-08-09 12:24:30 -0400127bool GrDawnCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
128 int sampleCount) const {
129 return isFormatRenderable(format, sampleCount);
130}
131
132int GrDawnCaps::getRenderTargetSampleCount(int requestedCount,
Stephen Whited7325182019-08-02 17:22:59 -0400133 const GrBackendFormat& backendFormat) const {
Brian Salomond4764a12019-08-08 12:08:24 -0400134 dawn::TextureFormat dawnFormat;
Stephen Whitefe55c732019-08-09 12:24:30 -0400135 if (!backendFormat.asDawnFormat(&dawnFormat)) {
Stephen Whited7325182019-08-02 17:22:59 -0400136 return 0;
137 }
Brian Salomond4764a12019-08-08 12:08:24 -0400138 return GrDawnFormatIsRenderable(dawnFormat) ? 1 : 0;
Stephen Whited7325182019-08-02 17:22:59 -0400139}
140
Stephen Whited7325182019-08-02 17:22:59 -0400141int GrDawnCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
142 return format.isValid() ? 1 : 0;
Stephen White985741a2019-07-18 11:43:45 -0400143}
144
Robert Phillips0a15cc62019-07-30 12:49:10 -0400145GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct,
146 GrRenderable renderable) const {
Stephen White985741a2019-07-18 11:43:45 -0400147 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
148 if (config == kUnknown_GrPixelConfig) {
149 return GrBackendFormat();
150 }
151 dawn::TextureFormat format;
152 if (!GrPixelConfigToDawnFormat(config, &format)) {
153 return GrBackendFormat();
154 }
155 return GrBackendFormat::MakeDawn(format);
156}
157
158GrBackendFormat GrDawnCaps::getBackendFormatFromCompressionType(SkImage::CompressionType type) const
159{
160 return GrBackendFormat();
161}
162
163GrSwizzle GrDawnCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const
164{
165 return get_swizzle(format, colorType, false);
166}
167
168bool GrDawnCaps::canClearTextureOnCreation() const {
169 return true;
170}
171
172GrSwizzle GrDawnCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const
173{
174 return get_swizzle(format, colorType, true);
175}
176
177bool GrDawnCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
178 const GrBackendFormat& format) const {
179 return true;
180}
Stephen Whiteb05ca082019-07-29 11:39:28 -0400181
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400182GrColorType GrDawnCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat&,
183 bool isAlphaChannel) const {
Stephen Whiteb05ca082019-07-29 11:39:28 -0400184 return GrColorType::kUnknown;
185}
Stephen Whited7325182019-08-02 17:22:59 -0400186
187#if GR_TEST_UTILS
188std::vector<GrCaps::TestFormatColorTypeCombination> GrDawnCaps::getTestingCombinations() const {
189 std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
190 { GrColorType::kAlpha_8, GrBackendFormat::MakeDawn(dawn::TextureFormat::R8Unorm) },
191 { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
Stephen White6db3b7b2019-08-15 16:48:05 -0400192 { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
Stephen Whited7325182019-08-02 17:22:59 -0400193 { GrColorType::kRGB_888x, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
Stephen White6db3b7b2019-08-15 16:48:05 -0400194 { GrColorType::kRGB_888x, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
Stephen Whited7325182019-08-02 17:22:59 -0400195 { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) },
Stephen White6db3b7b2019-08-15 16:48:05 -0400196 { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) },
Stephen Whited7325182019-08-02 17:22:59 -0400197 };
198
199#ifdef SK_DEBUG
200 for (auto combo : combos) {
201 SkASSERT(this->onAreColorTypeAndFormatCompatible(combo.fColorType, combo.fFormat));
202 }
203#endif
204 return combos;
205}
206#endif