1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "PixelFormatUtils.h"
16 
17 #include <android-base/logging.h>
18 
19 namespace android {
20 namespace automotive {
21 namespace computepipe {
22 namespace runner {
23 
numBytesPerPixel(AHardwareBuffer_Format format)24 int numBytesPerPixel(AHardwareBuffer_Format format) {
25     switch (format) {
26         case AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
27             return 4;
28         case AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
29             return 3;
30         case AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT:
31             return 1;
32         default:
33             CHECK(false) << "Unrecognized pixel format seen";
34     }
35     return 0;
36 }
37 
PixelFormatToHardwareBufferFormat(PixelFormat pixelFormat)38 AHardwareBuffer_Format PixelFormatToHardwareBufferFormat(PixelFormat pixelFormat) {
39     switch (pixelFormat) {
40         case PixelFormat::RGBA:
41             return AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
42         case PixelFormat::RGB:
43             return AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
44         case PixelFormat::GRAY:
45             return AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT;  // TODO: Check if this works
46         default:
47             CHECK(false) << "Unrecognized pixel format seen";
48     }
49     return AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_BLOB;
50 }
51 
52 }  // namespace runner
53 }  // namespace computepipe
54 }  // namespace automotive
55 }  // namespace android
56