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 
GrDawnBytesPerBlock(wgpu::TextureFormat format)10 size_t GrDawnBytesPerBlock(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             SkUNREACHABLE;
21     }
22 }
23 
GrDawnFormatStencilBits(wgpu::TextureFormat format)24 int GrDawnFormatStencilBits(wgpu::TextureFormat format) {
25     switch (format) {
26         case wgpu::TextureFormat::RGBA8Unorm:
27         case wgpu::TextureFormat::BGRA8Unorm:
28         case wgpu::TextureFormat::R8Unorm:
29             return 0;
30         case wgpu::TextureFormat::Depth24PlusStencil8:
31             return 8;
32         default:
33             SkUNREACHABLE;
34     }
35 }
36 
GrDawnFormatIsRenderable(wgpu::TextureFormat format)37 bool GrDawnFormatIsRenderable(wgpu::TextureFormat format) {
38     // For now, all the formats above are renderable. If a non-renderable format is added
39     // (see dawn/src/dawn_native/Format.cpp), an exception should be added here.
40     return true;
41 }
42 
GrColorTypeToDawnFormat(GrColorType ct,wgpu::TextureFormat * format)43 bool GrColorTypeToDawnFormat(GrColorType ct, wgpu::TextureFormat* format) {
44     switch (ct) {
45         case GrColorType::kRGBA_8888:
46             *format = wgpu::TextureFormat::RGBA8Unorm;
47             return true;
48         case GrColorType::kBGRA_8888:
49             *format = wgpu::TextureFormat::BGRA8Unorm;
50             return true;
51         case GrColorType::kAlpha_8:
52         case GrColorType::kGray_8:
53             *format = wgpu::TextureFormat::R8Unorm;
54             return true;
55         default:
56             return false;
57     }
58 }
59 
GrDawnFormatToGrColorType(wgpu::TextureFormat format,GrColorType * colorType)60 bool GrDawnFormatToGrColorType(wgpu::TextureFormat format, GrColorType* colorType) {
61     switch (format) {
62         case wgpu::TextureFormat::RGBA8Unorm:
63             *colorType = GrColorType::kRGBA_8888;
64             return true;
65         case wgpu::TextureFormat::BGRA8Unorm:
66             *colorType = GrColorType::kBGRA_8888;
67             return true;
68         case wgpu::TextureFormat::R8Unorm:
69             *colorType = GrColorType::kR_8;
70             return true;
71         default:
72             return false;
73     }
74 }
75 
GrDawnRoundRowBytes(size_t rowBytes)76 size_t GrDawnRoundRowBytes(size_t rowBytes) {
77     // Dawn requires that rowBytes be a multiple of 256. (This is actually imposed by D3D12.)
78     return (rowBytes + 0xFF) & ~0xFF;
79 }
80 
81 #if defined(SK_DEBUG) || GR_TEST_UTILS
GrDawnFormatToStr(wgpu::TextureFormat format)82 const char* GrDawnFormatToStr(wgpu::TextureFormat format) {
83     switch (format) {
84         case wgpu::TextureFormat::RGBA8Unorm:
85             return "RGBA8Unorm";
86         case wgpu::TextureFormat::BGRA8Unorm:
87             return "BGRA8Unorm";
88         case wgpu::TextureFormat::R8Unorm:
89             return "R8Unorm";
90         case wgpu::TextureFormat::Depth24PlusStencil8:
91             return "Depth24PlusStencil8";
92         default:
93             SkUNREACHABLE;
94     }
95 }
96 #endif
97