1 /*
2  * Copyright (C) Texas Instruments Incorporated - http://www.ti.com/
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 
19 #include <log/log.h>
20 
21 #include <linux/types.h>
22 
23 #include "format.h"
24 #include "img_gralloc1_public.h"
25 
is_valid_format(uint32_t format)26 bool is_valid_format(uint32_t format)
27 {
28     switch (format) {
29     case HAL_PIXEL_FORMAT_RGBA_8888:
30     case HAL_PIXEL_FORMAT_BGRA_8888:
31         return true;
32 
33     case HAL_PIXEL_FORMAT_RGB_565:
34         return true;
35 
36     case HAL_PIXEL_FORMAT_NV12:
37         return true;
38 
39     case HAL_PIXEL_FORMAT_RGBX_8888:
40     case HAL_PIXEL_FORMAT_BGRX_8888:
41         return true;
42 
43     case HAL_PIXEL_FORMAT_TI_NV12:
44     case HAL_PIXEL_FORMAT_TI_NV12_1D:
45         /* legacy formats not supported anymore */
46         return false;
47 
48     default:
49         return false;
50     }
51 }
52 
is_rgb_format(uint32_t format)53 bool is_rgb_format(uint32_t format)
54 {
55     switch (format) {
56     case HAL_PIXEL_FORMAT_BGRA_8888:
57     case HAL_PIXEL_FORMAT_BGRX_8888:
58     case HAL_PIXEL_FORMAT_RGB_565:
59         return true;
60     default:
61         return false;
62     }
63 }
64 
is_bgr_format(uint32_t format)65 bool is_bgr_format(uint32_t format)
66 {
67     switch (format) {
68     case HAL_PIXEL_FORMAT_RGBX_8888:
69     case HAL_PIXEL_FORMAT_RGBA_8888:
70         return true;
71     default:
72         return false;
73     }
74 }
75 
is_nv12_format(uint32_t format)76 bool is_nv12_format(uint32_t format)
77 {
78     switch (format) {
79     case HAL_PIXEL_FORMAT_TI_NV12:
80     case HAL_PIXEL_FORMAT_TI_NV12_1D:
81     case HAL_PIXEL_FORMAT_NV12:
82         return true;
83     default:
84         return false;
85     }
86 }
87 
is_opaque_format(uint32_t format)88 bool is_opaque_format(uint32_t format)
89 {
90     switch (format) {
91     case HAL_PIXEL_FORMAT_RGB_565:
92     case HAL_PIXEL_FORMAT_RGBX_8888:
93     case HAL_PIXEL_FORMAT_BGRX_8888:
94         return true;
95     default:
96         return false;
97     }
98 }
99 
get_format_bpp(uint32_t format)100 uint32_t get_format_bpp(uint32_t format)
101 {
102     switch (format) {
103     case HAL_PIXEL_FORMAT_BGRA_8888:
104     case HAL_PIXEL_FORMAT_BGRX_8888:
105     case HAL_PIXEL_FORMAT_RGBX_8888:
106     case HAL_PIXEL_FORMAT_RGBA_8888:
107         return 32;
108     case HAL_PIXEL_FORMAT_RGB_565:
109         return 16;
110     case HAL_PIXEL_FORMAT_TI_NV12:
111     case HAL_PIXEL_FORMAT_TI_NV12_1D:
112     case HAL_PIXEL_FORMAT_NV12:
113         return 8;
114     default:
115         return 0;
116     }
117 }
118 
convert_hal_to_drm_format(uint32_t hal_format,bool blended)119 uint32_t convert_hal_to_drm_format(uint32_t hal_format, bool blended)
120 {
121     uint32_t dss_format = HAL_PIXEL_FORMAT_RGBA_8888;
122 
123     /* convert color format */
124     switch (hal_format) {
125     case HAL_PIXEL_FORMAT_RGBA_8888:
126     case HAL_PIXEL_FORMAT_BGRA_8888:
127         dss_format = DRM_FORMAT_ARGB8888;
128         if (blended)
129             break;
130 
131     case HAL_PIXEL_FORMAT_RGBX_8888:
132     case HAL_PIXEL_FORMAT_BGRX_8888:
133         dss_format = DRM_FORMAT_XRGB8888;
134         break;
135 
136     case HAL_PIXEL_FORMAT_RGB_565:
137         dss_format = DRM_FORMAT_RGB565;
138         break;
139 
140     case HAL_PIXEL_FORMAT_TI_NV12:
141     case HAL_PIXEL_FORMAT_TI_NV12_1D:
142     case HAL_PIXEL_FORMAT_NV12:
143         dss_format = DRM_FORMAT_NV12;
144         break;
145 
146     default:
147         /* Should have been filtered out */
148         ALOGV("Unsupported pixel format");
149     }
150 
151     return dss_format;
152 }
153