1 /*
2  * Copyright (C) 2018-2020 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef FORMAT_INFO_H_
20 #define FORMAT_INFO_H_
21 
22 #include "mali_gralloc_buffer.h"
23 
24 typedef uint8_t format_support_flags;
25 
26 /* Base format unsupported */
27 #define F_NONE 0
28 /* Base format supports uncompressed */
29 #define F_LIN ((uint8_t)1 << 0)
30 /* Base format supports AFBC */
31 #define F_AFBC ((uint8_t)1 << 1)
32 /* Base format supports AFBC with different swizzle */
33 #define F_AFBC_SWIZ ((uint8_t)1 << 2)
34 
35 
36 typedef struct
37 {
38 	uint16_t width;
39 	uint16_t height;
40 } rect_t;
41 
is_power2(uint8_t n)42 constexpr bool is_power2(uint8_t n)
43 {
44 	return ((n & (n-1)) == 0);
45 }
46 
47 /*
48  * Pixel format information.
49  *
50  * These properties are used by gralloc for buffer allocation.
51  * Each format is uniquely identified with 'id'.
52  */
53 typedef struct
54 {
55 	uint32_t id;                    /* Format ID. */
56 	uint8_t npln;                   /* Number of planes. */
57 	uint8_t ncmp[MAX_PLANES];       /* Number of components in each plane. */
58 	uint8_t bps;                    /* Bits per sample (primary/largest). */
59 	uint8_t bpp_afbc[MAX_PLANES];   /* Bits per pixel (AFBC), without implicit padding. 'X' in RGBX is still included. */
60 	uint8_t bpp[MAX_PLANES];        /* Bits per pixel (linear/uncompressed), including any implicit sample padding defined by format (e.g. 10-bit Y210 padded to 16-bits).
61 	                                 * NOTE: bpp[n] and/or (bpp[n] * align_w_cpu) must be multiples of 8. */
62 	uint8_t hsub;                   /* Horizontal sub-sampling (YUV formats). Pixel rounding in width (all formats). Must be a power of 2. */
63 	uint8_t vsub;                   /* Vertical sub-sampling (YUV formats). Pixel rounding in height (all formats). Must be a power of 2. */
64 	uint8_t align_w;                /* Alignment of width (per plane, in pixels). Must be a power of 2. NOTE: where 'is_yuv == true', this must be a multiple of 'hsub'. */
65 	uint8_t align_h;                /* Alignment of height (per plane, in pixels). Must be a power of 2. NOTE: where 'is_yuv == true', this must be a multiple of 'vsub'. */
66 	uint8_t align_w_cpu;            /* Alignment of width for CPU access (per plane, in pixels). ALIGN_W_CPU_DEFAULT: 1. Must be a power of 2. */
67 	uint16_t tile_size;             /* Tile size (in pixels), assumed square. Uncompressed only. */
68 	bool has_alpha;                 /* Alpha channel present. */
69 	bool is_rgb;                    /* RGB format. */
70 	bool is_yuv;                    /* YUV format. */
71 	bool afbc;                      /* AFBC supported (per specification and by gralloc). IP support not considered. */
72 	bool linear;                    /* Linear/uncompressed supported. */
73 	bool yuv_transform;             /* Supports AFBC YUV transform: 3+ channel RGB (strict R-G-B-? order) with less than 12-bit per sample. */
74 	bool flex;                      /* Linear version of format can be represented as flex. */
75 	bool planes_contiguous;         /* True if all planes in format are contiguous in memory.  Has no effect on non-planar formats. */
76 	const char *name;               /* Human-readable name. */
77 	/* Computes the total number of components in the format. */
total_components__anonac4642b5020878 	int total_components() const
79 	{
80 		int sum = 0;
81 		for (auto n: ncmp)
82 		{
83 			sum += n;
84 		}
85 		return sum;
86 	}
87 } format_info_t;
88 
89 typedef struct
90 {
91 	uint32_t id;                       /* Format ID. */
92 	format_support_flags cpu_wr;       /* CPU producer. */
93 	format_support_flags cpu_rd;       /* CPU consumer. */
94 	format_support_flags gpu_wr;       /* GPU producer. */
95 	format_support_flags gpu_rd;       /* GPU consumer. */
96 	format_support_flags dpu_wr;       /* DPU producer. */
97 	format_support_flags dpu_rd;       /* DPU consumer. */
98 	format_support_flags dpu_aeu_wr;   /* DPU AEU producer. (UNUSED IN EXYNOS) */
99 	format_support_flags vpu_wr;       /* VPU producer. */
100 	format_support_flags vpu_rd;       /* VPU consumer. */
101 	format_support_flags cam_wr;       /* Camera producer. */
102 
103 } format_ip_support_t;
104 
105 
106 extern const format_info_t formats[];
107 extern const format_ip_support_t formats_ip_support[];
108 extern const size_t num_formats;
109 extern const size_t num_ip_formats;
110 
111 extern int32_t get_format_index(const uint32_t base_format);
112 extern int32_t get_ip_format_index(const uint32_t base_format);
113 extern uint32_t get_internal_format(const uint32_t base_format, const bool map_to_internal);
114 void get_format_dataspace(uint32_t base_format,
115                           uint64_t usage,
116                           int width,
117                           int height,
118                           android_dataspace_t *dataspace);
119 extern bool sanitize_formats(void);
120 
121 extern const char *format_name(const uint32_t base_format);
122 
123 #endif
124