Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 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 | |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 8 | #include "src/gpu/dawn/GrDawnCaps.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 9 | |
| 10 | GrDawnCaps::GrDawnCaps(const GrContextOptions& contextOptions) : INHERITED(contextOptions) { |
| 11 | fBufferMapThreshold = SK_MaxS32; // FIXME: get this from Dawn? |
| 12 | fShaderCaps.reset(new GrShaderCaps(contextOptions)); |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 13 | fMaxTextureSize = 2048; // FIXME |
| 14 | fMaxVertexAttributes = 16; // FIXME |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 15 | fPerformPartialClearsAsDraws = true; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 16 | |
| 17 | this->applyOptionsOverrides(contextOptions); |
| 18 | fShaderCaps->applyOptionsOverrides(contextOptions); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | bool GrDawnCaps::isFormatSRGB(const GrBackendFormat& format) const { |
| 22 | return false; |
| 23 | } |
| 24 | |
Robert Phillips | 8ff8bcc | 2019-07-29 17:03:35 -0400 | [diff] [blame] | 25 | bool GrDawnCaps::isFormatCompressed(const GrBackendFormat& format) const { |
| 26 | return false; |
| 27 | } |
| 28 | |
Stephen White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 29 | bool 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 White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | GrPixelConfig GrDawnCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 36 | GrColorType colorType) const { |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 37 | dawn::TextureFormat dawnFormat; |
| 38 | if (!format.asDawnFormat(&dawnFormat)) { |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 39 | return kUnknown_GrPixelConfig; |
| 40 | } |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 41 | switch (colorType) { |
| 42 | case GrColorType::kUnknown: |
| 43 | return kUnknown_GrPixelConfig; |
| 44 | case GrColorType::kAlpha_8: |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 45 | if (dawn::TextureFormat::R8Unorm == dawnFormat) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 46 | return kAlpha_8_as_Red_GrPixelConfig; |
| 47 | } |
| 48 | break; |
| 49 | case GrColorType::kRGBA_8888: |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 50 | if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 51 | return kRGBA_8888_GrPixelConfig; |
Stephen White | fe55c73 | 2019-08-09 12:24:30 -0400 | [diff] [blame] | 52 | } else if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 53 | // 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 White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 56 | } |
| 57 | break; |
| 58 | case GrColorType::kRGB_888x: |
| 59 | break; |
| 60 | case GrColorType::kBGRA_8888: |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 61 | if (dawn::TextureFormat::BGRA8Unorm == dawnFormat) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 62 | return kBGRA_8888_GrPixelConfig; |
Stephen White | fe55c73 | 2019-08-09 12:24:30 -0400 | [diff] [blame] | 63 | } else if (dawn::TextureFormat::RGBA8Unorm == dawnFormat) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 64 | return kRGBA_8888_GrPixelConfig; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 65 | } |
| 66 | break; |
| 67 | default: |
| 68 | break; |
| 69 | } |
| 70 | return kUnknown_GrPixelConfig; |
| 71 | } |
| 72 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 73 | static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType, |
| 74 | bool forOutput) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 75 | 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 White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 98 | bool GrDawnCaps::isFormatTexturableAndUploadable(GrColorType ct, |
| 99 | const GrBackendFormat& format) const { |
| 100 | dawn::TextureFormat dawnFormat; |
| 101 | if (!format.asDawnFormat(&dawnFormat)) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 102 | return false; |
| 103 | } |
Stephen White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 104 | 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 White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Stephen White | fe55c73 | 2019-08-09 12:24:30 -0400 | [diff] [blame] | 117 | bool GrDawnCaps::isFormatRenderable(const GrBackendFormat& format, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 118 | int sampleCount) const { |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 119 | dawn::TextureFormat dawnFormat; |
| 120 | if (!format.isValid() || sampleCount > 1 || !format.asDawnFormat(&dawnFormat)) { |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 121 | return false; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 124 | return GrDawnFormatIsRenderable(dawnFormat); |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Stephen White | fe55c73 | 2019-08-09 12:24:30 -0400 | [diff] [blame] | 127 | bool GrDawnCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format, |
| 128 | int sampleCount) const { |
| 129 | return isFormatRenderable(format, sampleCount); |
| 130 | } |
| 131 | |
| 132 | int GrDawnCaps::getRenderTargetSampleCount(int requestedCount, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 133 | const GrBackendFormat& backendFormat) const { |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 134 | dawn::TextureFormat dawnFormat; |
Stephen White | fe55c73 | 2019-08-09 12:24:30 -0400 | [diff] [blame] | 135 | if (!backendFormat.asDawnFormat(&dawnFormat)) { |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 138 | return GrDawnFormatIsRenderable(dawnFormat) ? 1 : 0; |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 141 | int GrDawnCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const { |
| 142 | return format.isValid() ? 1 : 0; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 145 | GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct, |
| 146 | GrRenderable renderable) const { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 147 | 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 | |
| 158 | GrBackendFormat GrDawnCaps::getBackendFormatFromCompressionType(SkImage::CompressionType type) const |
| 159 | { |
| 160 | return GrBackendFormat(); |
| 161 | } |
| 162 | |
| 163 | GrSwizzle GrDawnCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const |
| 164 | { |
| 165 | return get_swizzle(format, colorType, false); |
| 166 | } |
| 167 | |
| 168 | bool GrDawnCaps::canClearTextureOnCreation() const { |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | GrSwizzle GrDawnCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const |
| 173 | { |
| 174 | return get_swizzle(format, colorType, true); |
| 175 | } |
| 176 | |
| 177 | bool GrDawnCaps::onAreColorTypeAndFormatCompatible(GrColorType ct, |
| 178 | const GrBackendFormat& format) const { |
| 179 | return true; |
| 180 | } |
Stephen White | b05ca08 | 2019-07-29 11:39:28 -0400 | [diff] [blame] | 181 | |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 182 | GrColorType GrDawnCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat&, |
| 183 | bool isAlphaChannel) const { |
Stephen White | b05ca08 | 2019-07-29 11:39:28 -0400 | [diff] [blame] | 184 | return GrColorType::kUnknown; |
| 185 | } |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 186 | |
| 187 | #if GR_TEST_UTILS |
| 188 | std::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 White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 192 | { GrColorType::kRGBA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) }, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 193 | { GrColorType::kRGB_888x, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) }, |
Stephen White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 194 | { GrColorType::kRGB_888x, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) }, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 195 | { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::BGRA8Unorm) }, |
Stephen White | 6db3b7b | 2019-08-15 16:48:05 -0400 | [diff] [blame^] | 196 | { GrColorType::kBGRA_8888, GrBackendFormat::MakeDawn(dawn::TextureFormat::RGBA8Unorm) }, |
Stephen White | d732518 | 2019-08-02 17:22:59 -0400 | [diff] [blame] | 197 | }; |
| 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 |