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 | |
| 8 | #include "GrDawnCaps.h" |
| 9 | |
| 10 | GrDawnCaps::GrDawnCaps(const GrContextOptions& contextOptions) : INHERITED(contextOptions) { |
| 11 | fBufferMapThreshold = SK_MaxS32; // FIXME: get this from Dawn? |
| 12 | fShaderCaps.reset(new GrShaderCaps(contextOptions)); |
| 13 | fMaxTextureSize = 2048; |
| 14 | fPerformPartialClearsAsDraws = true; |
| 15 | } |
| 16 | |
| 17 | bool GrDawnCaps::isFormatSRGB(const GrBackendFormat& format) const { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | bool GrDawnCaps::isConfigTexturable(GrPixelConfig config) const { |
| 22 | switch (config) { |
| 23 | case kRGBA_8888_GrPixelConfig: |
| 24 | case kBGRA_8888_GrPixelConfig: |
| 25 | case kAlpha_8_GrPixelConfig: |
| 26 | return true; |
| 27 | default: |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | GrPixelConfig GrDawnCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format, |
| 33 | GrColorType colorType) const { |
| 34 | dawn::TextureFormat textureFormat = *format.getDawnFormat(); |
| 35 | switch (colorType) { |
| 36 | case GrColorType::kUnknown: |
| 37 | return kUnknown_GrPixelConfig; |
| 38 | case GrColorType::kAlpha_8: |
| 39 | if (dawn::TextureFormat::R8Unorm == textureFormat) { |
| 40 | return kAlpha_8_as_Red_GrPixelConfig; |
| 41 | } |
| 42 | break; |
| 43 | case GrColorType::kRGBA_8888: |
Stephen White | 3e45e12 | 2019-07-22 09:12:29 -0400 | [diff] [blame^] | 44 | if (dawn::TextureFormat::RGBA8Unorm == textureFormat) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 45 | return kRGBA_8888_GrPixelConfig; |
| 46 | } |
| 47 | break; |
| 48 | case GrColorType::kRGB_888x: |
| 49 | break; |
| 50 | case GrColorType::kBGRA_8888: |
Stephen White | 3e45e12 | 2019-07-22 09:12:29 -0400 | [diff] [blame^] | 51 | if (dawn::TextureFormat::BGRA8Unorm == textureFormat) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 52 | return kBGRA_8888_GrPixelConfig; |
| 53 | } |
| 54 | break; |
| 55 | default: |
| 56 | break; |
| 57 | } |
| 58 | return kUnknown_GrPixelConfig; |
| 59 | } |
| 60 | |
| 61 | GrPixelConfig GrDawnCaps::getYUVAConfigFromBackendFormat(const GrBackendFormat& backendFormat) |
| 62 | const { |
| 63 | const dawn::TextureFormat* format = backendFormat.getDawnFormat(); |
| 64 | if (!format) { |
| 65 | return kUnknown_GrPixelConfig; |
| 66 | } |
| 67 | switch (*format) { |
| 68 | case dawn::TextureFormat::R8Unorm: |
| 69 | return kAlpha_8_as_Red_GrPixelConfig; |
| 70 | break; |
Stephen White | 3e45e12 | 2019-07-22 09:12:29 -0400 | [diff] [blame^] | 71 | case dawn::TextureFormat::RGBA8Unorm: |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 72 | return kRGBA_8888_GrPixelConfig; |
| 73 | break; |
Stephen White | 3e45e12 | 2019-07-22 09:12:29 -0400 | [diff] [blame^] | 74 | case dawn::TextureFormat::BGRA8Unorm: |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 75 | return kBGRA_8888_GrPixelConfig; |
| 76 | break; |
| 77 | default: |
| 78 | return kUnknown_GrPixelConfig; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | size_t GrDawnCaps::onTransferFromOffsetAlignment(GrColorType bufferColorType) const { |
| 84 | if (bufferColorType == GrColorType::kRGB_888x) { |
| 85 | return false; |
| 86 | } |
| 87 | size_t bpp = GrColorTypeBytesPerPixel(bufferColorType); |
| 88 | switch (bpp & 0b11) { |
| 89 | case 0: return bpp; |
| 90 | case 2: return 2 * bpp; |
| 91 | default: return 4 * bpp; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType, |
| 96 | bool forOutput) { |
| 97 | SkASSERT(format.getDawnFormat()); |
| 98 | |
| 99 | switch (colorType) { |
| 100 | case GrColorType::kAlpha_8: // fall through |
| 101 | case GrColorType::kAlpha_F16: |
| 102 | if (forOutput) { |
| 103 | return GrSwizzle::AAAA(); |
| 104 | } else { |
| 105 | return GrSwizzle::RRRR(); |
| 106 | } |
| 107 | case GrColorType::kGray_8: |
| 108 | if (!forOutput) { |
| 109 | return GrSwizzle::RRRA(); |
| 110 | } |
| 111 | break; |
| 112 | case GrColorType::kRGB_888x: |
| 113 | if (!forOutput) { |
| 114 | return GrSwizzle::RGB1(); |
| 115 | } |
| 116 | default: |
| 117 | return GrSwizzle::RGBA(); |
| 118 | } |
| 119 | return GrSwizzle::RGBA(); |
| 120 | } |
| 121 | |
| 122 | bool GrDawnCaps::isFormatTexturable(GrColorType ct, const GrBackendFormat& format) const { |
| 123 | GrPixelConfig config = this->getConfigFromBackendFormat(format, ct); |
| 124 | if (kUnknown_GrPixelConfig == config) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return this->isConfigTexturable(config); |
| 129 | } |
| 130 | |
| 131 | bool GrDawnCaps::isFormatCopyable(GrColorType ct, const GrBackendFormat& format) const { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | int GrDawnCaps::getRenderTargetSampleCount(int requestedCount, GrColorType ct, |
| 136 | const GrBackendFormat& format) const { |
| 137 | GrPixelConfig config = this->getConfigFromBackendFormat(format, ct); |
| 138 | if (kUnknown_GrPixelConfig == config) { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | return this->getRenderTargetSampleCount(requestedCount, config); |
| 143 | } |
| 144 | |
| 145 | GrBackendFormat GrDawnCaps::getBackendFormatFromColorType(GrColorType ct) const { |
| 146 | GrPixelConfig config = GrColorTypeToPixelConfig(ct); |
| 147 | if (config == kUnknown_GrPixelConfig) { |
| 148 | return GrBackendFormat(); |
| 149 | } |
| 150 | dawn::TextureFormat format; |
| 151 | if (!GrPixelConfigToDawnFormat(config, &format)) { |
| 152 | return GrBackendFormat(); |
| 153 | } |
| 154 | return GrBackendFormat::MakeDawn(format); |
| 155 | } |
| 156 | |
| 157 | GrBackendFormat GrDawnCaps::getBackendFormatFromCompressionType(SkImage::CompressionType type) const |
| 158 | { |
| 159 | return GrBackendFormat(); |
| 160 | } |
| 161 | |
| 162 | GrSwizzle GrDawnCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const |
| 163 | { |
| 164 | return get_swizzle(format, colorType, false); |
| 165 | } |
| 166 | |
| 167 | bool GrDawnCaps::canClearTextureOnCreation() const { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | GrSwizzle GrDawnCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const |
| 172 | { |
| 173 | return get_swizzle(format, colorType, true); |
| 174 | } |
| 175 | |
| 176 | bool GrDawnCaps::onAreColorTypeAndFormatCompatible(GrColorType ct, |
| 177 | const GrBackendFormat& format) const { |
| 178 | return true; |
| 179 | } |