blob: b23f44a03809998d089726d470ff41a62742a01e [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/GrDawnUtil.h"
Stephen White985741a2019-07-18 11:43:45 -04009
10GrPixelConfig GrDawnFormatToPixelConfig(dawn::TextureFormat format) {
11 switch (format) {
Stephen White3e45e122019-07-22 09:12:29 -040012 case dawn::TextureFormat::RGBA8Unorm:
Stephen White985741a2019-07-18 11:43:45 -040013 return kRGBA_8888_GrPixelConfig;
Stephen White3e45e122019-07-22 09:12:29 -040014 case dawn::TextureFormat::BGRA8Unorm:
Stephen White985741a2019-07-18 11:43:45 -040015 return kBGRA_8888_GrPixelConfig;
16 case dawn::TextureFormat::R8Unorm:
17 return kAlpha_8_GrPixelConfig;
Stephen White3e45e122019-07-22 09:12:29 -040018 case dawn::TextureFormat::Depth24PlusStencil8:
Stephen White985741a2019-07-18 11:43:45 -040019 default:
20 SkASSERT(false);
21 return kRGBA_8888_GrPixelConfig;
22 }
23}
24
Stephen Whited7325182019-08-02 17:22:59 -040025bool GrDawnFormatIsRenderable(dawn::TextureFormat format) {
26 // For now, all the formats above are renderable. If a non-renderable format is added
27 // (see dawn/src/dawn_native/Format.cpp), an exception should be added here.
28 return true;
29}
30
Stephen White985741a2019-07-18 11:43:45 -040031bool GrPixelConfigToDawnFormat(GrPixelConfig config, dawn::TextureFormat* format) {
32 switch (config) {
33 case kRGBA_8888_GrPixelConfig:
34 case kRGBA_4444_GrPixelConfig:
35 case kRGB_565_GrPixelConfig:
36 case kGray_8_GrPixelConfig:
Stephen White3e45e122019-07-22 09:12:29 -040037 *format = dawn::TextureFormat::RGBA8Unorm;
Stephen White985741a2019-07-18 11:43:45 -040038 return true;
39 case kBGRA_8888_GrPixelConfig:
Stephen White3e45e122019-07-22 09:12:29 -040040 *format = dawn::TextureFormat::BGRA8Unorm;
Stephen White985741a2019-07-18 11:43:45 -040041 return true;
42 case kAlpha_8_GrPixelConfig:
Stephen White170d9902019-08-15 16:48:24 -040043 case kAlpha_8_as_Red_GrPixelConfig:
Stephen White985741a2019-07-18 11:43:45 -040044 *format = dawn::TextureFormat::R8Unorm;
45 return true;
46 default:
47 return false;
48 }
49}
Stephen Whited7325182019-08-02 17:22:59 -040050
Stephen White0850dcd2019-08-13 15:28:47 -040051size_t GrDawnRoundRowBytes(size_t rowBytes) {
52 // Dawn requires that rowBytes be a multiple of 256. (This is actually imposed by D3D12.)
53 return (rowBytes + 0xFF) & ~0xFF;
54}
55
Stephen Whited7325182019-08-02 17:22:59 -040056#if GR_TEST_UTILS
57const char* GrDawnFormatToStr(dawn::TextureFormat format) {
58 switch (format) {
59 case dawn::TextureFormat::RGBA8Unorm:
60 return "RGBA8Unorm";
61 case dawn::TextureFormat::BGRA8Unorm:
62 return "BGRA8Unorm";
63 case dawn::TextureFormat::R8Unorm:
64 return "R8Unorm";
65 case dawn::TextureFormat::Depth24PlusStencil8:
66 return "Depth24PlusStencil8";
67 default:
68 SkASSERT(false);
69 return "Unknown";
70 }
71}
72#endif