1 /*
2 * Copyright (c) 2011-2016,2018-2020, The Linux Foundation. All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef __GR_UTILS_H__
31 #define __GR_UTILS_H__
32
33 #include <android/hardware/graphics/common/1.2/types.h>
34 #include "gralloc_priv.h"
35 #include "qdMetaData.h"
36
37 #define SZ_2M 0x200000
38 #define SZ_1M 0x100000
39 #define SZ_4K 0x1000
40
41 #define SIZE_4K 4096
42 #define SIZE_8K 8192
43
44 #ifdef SLAVE_SIDE_CP
45 #define SECURE_ALIGN SZ_1M
46 #else // MASTER_SIDE_CP
47 #define SECURE_ALIGN SZ_4K
48 #endif
49
50 #define INT(exp) static_cast<int>(exp)
51 #define UINT(exp) static_cast<unsigned int>(exp)
52
53 using android::hardware::graphics::common::V1_1::BufferUsage;
54
55 namespace gralloc {
56 struct BufferInfo {
57 BufferInfo(int w, int h, int f, uint64_t usage = 0)
widthBufferInfo58 : width(w), height(h), format(f), layer_count(1), usage(usage) {}
59 int width;
60 int height;
61 int format;
62 int layer_count;
63 uint64_t usage;
64 };
65
66 template <class Type1, class Type2>
ALIGN(Type1 x,Type2 align)67 inline Type1 ALIGN(Type1 x, Type2 align) {
68 return (Type1)((x + (Type1)align - 1) & ~((Type1)align - 1));
69 }
70
71 enum class Error : int32_t {
72 /**
73 * No error.
74 */
75 NONE = 0,
76 /**
77 * Invalid BufferDescriptor.
78 */
79 BAD_DESCRIPTOR = 1,
80 /**
81 * Invalid buffer handle.
82 */
83 BAD_BUFFER = 2,
84 /**
85 * Invalid HardwareBufferDescription.
86 */
87 BAD_VALUE = 3,
88 /**
89 * Resource unavailable.
90 */
91 NO_RESOURCES = 5,
92 /**
93 * Permanent failure.
94 */
95 UNSUPPORTED = 7,
96 };
97
98 enum PlaneComponent {
99 /* luma */
100 PLANE_COMPONENT_Y = 1 << 0,
101 /* chroma blue */
102 PLANE_COMPONENT_Cb = 1 << 1,
103 /* chroma red */
104 PLANE_COMPONENT_Cr = 1 << 2,
105
106 /* red */
107 PLANE_COMPONENT_R = 1 << 10,
108 /* green */
109 PLANE_COMPONENT_G = 1 << 11,
110 /* blue */
111 PLANE_COMPONENT_B = 1 << 12,
112
113 /* alpha */
114 PLANE_COMPONENT_A = 1 << 20,
115
116 /* raw data plane */
117 PLANE_COMPONENT_RAW = 1 << 30,
118
119 /* meta information plane */
120 PLANE_COMPONENT_META = 1 << 31,
121 };
122
123 struct PlaneLayoutInfo {
124 /** Components represented the type of plane. */
125 PlaneComponent component;
126
127 /** horizontal subsampling. Must be a positive power of 2. */
128 uint32_t h_subsampling;
129
130 /** vertical subsampling. Must be a positive power of 2. */
131 uint32_t v_subsampling;
132
133 /** offset to the first byte of the top-left pixel of the plane
134 * and it is calculated from the start of the buffer.
135 * Add base of the handle with offset to get the first byte of the plane.
136 */
137 uint32_t offset;
138
139 /** step is the distance in bytes from one pixel value to the next. */
140 int32_t step;
141
142 /** stride of the plane in pixels */
143 int32_t stride;
144
145 /** stride of the plane in in bytes */
146 int32_t stride_bytes;
147
148 /** plane height or vertical stride */
149 int32_t scanlines;
150
151 /** size of the plane in bytes */
152 uint32_t size;
153 };
154
155 bool IsYuvFormat(int format);
156 bool IsCompressedRGBFormat(int format);
157 bool IsUncompressedRGBFormat(int format);
158 uint32_t GetBppForUncompressedRGB(int format);
159 bool CpuCanAccess(uint64_t usage);
160 bool CpuCanRead(uint64_t usage);
161 bool CpuCanWrite(uint64_t usage);
162 int GetBpp(int format);
163 unsigned int GetSize(const BufferInfo &d, unsigned int alignedw, unsigned int alignedh);
164 int GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
165 unsigned int *alignedh);
166 int GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
167 unsigned int *alignedh, GraphicsMetadata *graphics_metadata);
168 void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height);
169 void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space);
170 void GetAlignedWidthAndHeight(const BufferInfo &d, unsigned int *aligned_w,
171 unsigned int *aligned_h);
172 int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]);
173 int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
174 int32_t flags, int *plane_count, PlaneLayoutInfo plane_info[8]);
175 void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
176 int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info);
177 unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage);
178 void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling);
179 void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
180 struct android_ycbcr *ycbcr);
181 int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data);
182 bool IsUBwcFormat(int format);
183 bool IsUBwcSupported(int format);
184 bool IsUBwcPISupported(int format, uint64_t usage);
185 bool IsUBwcEnabled(int format, uint64_t usage);
186 void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
187 unsigned int *aligned_h);
188 void GetYuvSPPlaneInfo(const BufferInfo &info, int format, uint32_t width, uint32_t height,
189 uint32_t bpp, PlaneLayoutInfo *plane_info);
190 void GetYuvUbwcSPPlaneInfo(uint32_t width, uint32_t height, int color_format,
191 PlaneLayoutInfo *plane_info);
192 void GetYuvUbwcInterlacedSPPlaneInfo(uint32_t width, uint32_t height,
193 PlaneLayoutInfo plane_info[8]);
194 void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height);
195 unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp);
196 unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
197 unsigned int alignedh);
198 int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
199 uint32_t *num_planes);
200 uint32_t GetDataAlignment(int format, uint64_t usage);
201 int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
202 unsigned int *alignedw, unsigned int *alignedh,
203 GraphicsMetadata *graphics_metadata);
204 bool CanUseAdrenoForSize(int buffer_type, uint64_t usage);
205 bool GetAdrenoSizeAPIStatus();
206 bool UseUncached(int format, uint64_t usage);
207 uint64_t GetHandleFlags(int format, uint64_t usage);
208 int GetImplDefinedFormat(uint64_t usage, int format);
209 int GetCustomFormatFlags(int format, uint64_t usage, int *custom_format, uint64_t *priv_flags);
210 int GetBufferType(int inputFormat);
211 bool IsGPUFlagSupported(uint64_t usage);
212 bool HasAlphaComponent(int32_t format);
213
214 void GetDRMFormat(uint32_t format, uint32_t flags, uint32_t *drm_format,
215 uint64_t *drm_format_modifier);
216 } // namespace gralloc
217
218 #endif // __GR_UTILS_H__
219