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 "src/gpu/dawn/GrDawnUtil.h"
9 
GrDawnBytesPerPixel(dawn::TextureFormat format)10 size_t GrDawnBytesPerPixel(dawn::TextureFormat format) {
11     switch (format) {
12         case dawn::TextureFormat::RGBA8Unorm:
13         case dawn::TextureFormat::BGRA8Unorm:
14             return 4;
15         case dawn::TextureFormat::R8Unorm:
16             return 1;
17         case dawn::TextureFormat::Depth24PlusStencil8:
18             return 4;
19         default:
20             SkASSERT(false);
21             return 4;
22     }
23 }
24 
GrDawnFormatIsRenderable(dawn::TextureFormat format)25 bool 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 
GrPixelConfigToDawnFormat(GrPixelConfig config,dawn::TextureFormat * format)31 bool 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:
37             *format = dawn::TextureFormat::RGBA8Unorm;
38             return true;
39         case kBGRA_8888_GrPixelConfig:
40             *format = dawn::TextureFormat::BGRA8Unorm;
41             return true;
42         case kAlpha_8_GrPixelConfig:
43         case kAlpha_8_as_Red_GrPixelConfig:
44             *format = dawn::TextureFormat::R8Unorm;
45             return true;
46         default:
47             return false;
48     }
49 }
50 
GrDawnRoundRowBytes(size_t rowBytes)51 size_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 
56 #if GR_TEST_UTILS
GrDawnFormatToStr(dawn::TextureFormat format)57 const 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
73