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(wgpu::TextureFormat format)10 size_t GrDawnBytesPerPixel(wgpu::TextureFormat format) {
11     switch (format) {
12         case wgpu::TextureFormat::RGBA8Unorm:
13         case wgpu::TextureFormat::BGRA8Unorm:
14             return 4;
15         case wgpu::TextureFormat::R8Unorm:
16             return 1;
17         case wgpu::TextureFormat::Depth24PlusStencil8:
18             return 4;
19         default:
20             SkASSERT(false);
21             return 4;
22     }
23 }
24 
GrDawnFormatIsRenderable(wgpu::TextureFormat format)25 bool GrDawnFormatIsRenderable(wgpu::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 
GrColorTypeToDawnFormat(GrColorType ct,wgpu::TextureFormat * format)31 bool GrColorTypeToDawnFormat(GrColorType ct, wgpu::TextureFormat* format) {
32     switch (ct) {
33         case GrColorType::kRGBA_8888:
34         case GrColorType::kABGR_4444:
35         case GrColorType::kBGR_565:
36         case GrColorType::kGray_8:
37             *format = wgpu::TextureFormat::RGBA8Unorm;
38             return true;
39         case GrColorType::kBGRA_8888:
40             *format = wgpu::TextureFormat::BGRA8Unorm;
41             return true;
42         case GrColorType::kAlpha_8:
43             *format = wgpu::TextureFormat::R8Unorm;
44             return true;
45         default:
46             return false;
47     }
48 }
49 
GrDawnRoundRowBytes(size_t rowBytes)50 size_t GrDawnRoundRowBytes(size_t rowBytes) {
51     // Dawn requires that rowBytes be a multiple of 256. (This is actually imposed by D3D12.)
52     return (rowBytes + 0xFF) & ~0xFF;
53 }
54 
55 #if GR_TEST_UTILS
GrDawnFormatToStr(wgpu::TextureFormat format)56 const char* GrDawnFormatToStr(wgpu::TextureFormat format) {
57     switch (format) {
58         case wgpu::TextureFormat::RGBA8Unorm:
59             return "RGBA8Unorm";
60         case wgpu::TextureFormat::BGRA8Unorm:
61             return "BGRA8Unorm";
62         case wgpu::TextureFormat::R8Unorm:
63             return "R8Unorm";
64         case wgpu::TextureFormat::Depth24PlusStencil8:
65             return "Depth24PlusStencil8";
66         default:
67             SkASSERT(false);
68             return "Unknown";
69     }
70 }
71 #endif
72