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 
35 #include <limits>
36 
37 #include "gralloc_priv.h"
38 #include "qdMetaData.h"
39 
40 #define SZ_2M 0x200000
41 #define SZ_1M 0x100000
42 #define SZ_4K 0x1000
43 
44 #define SIZE_4K 4096
45 #define SIZE_8K 8192
46 
47 #ifdef SLAVE_SIDE_CP
48 #define SECURE_ALIGN SZ_1M
49 #else  // MASTER_SIDE_CP
50 #define SECURE_ALIGN SZ_4K
51 #endif
52 
53 #define INT(exp) static_cast<int>(exp)
54 #define UINT(exp) static_cast<unsigned int>(exp)
55 
56 using android::hardware::graphics::common::V1_1::BufferUsage;
57 
58 namespace gralloc {
59 struct BufferInfo {
60   BufferInfo(int w, int h, int f, uint64_t usage = 0)
61       : width(w), height(h), format(f), layer_count(1), usage(usage) {}
62   int width;
63   int height;
64   int format;
65   int layer_count;
66   uint64_t usage;
67 };
68 
69 struct GrallocProperties {
70   bool use_system_heap_for_sensors = true;
71   bool ubwc_disable = false;
72   bool ahardware_buffer_disable = false;
73 };
74 
75 template <class Type1, class Type2>
ALIGN(Type1 x,Type2 align)76 inline Type1 ALIGN(Type1 x, Type2 align) {
77   Type1 max_val = std::numeric_limits<Type1>::max();
78   if (x > (max_val - (Type1)align)) {
79     return x;
80   }
81 
82   return (Type1)((x + (Type1)align - 1) & ~((Type1)align - 1));
83 }
84 
85 enum class Error : int32_t {
86   /**
87    * No error.
88    */
89   NONE = 0,
90   /**
91    * Invalid BufferDescriptor.
92    */
93   BAD_DESCRIPTOR = 1,
94   /**
95    * Invalid buffer handle.
96    */
97   BAD_BUFFER = 2,
98   /**
99    * Invalid HardwareBufferDescription.
100    */
101   BAD_VALUE = 3,
102   /**
103    * Resource unavailable.
104    */
105   NO_RESOURCES = 5,
106   /**
107    * Permanent failure.
108    */
109   UNSUPPORTED = 7,
110 };
111 
112 enum PlaneComponent {
113   /* luma */
114   PLANE_COMPONENT_Y = 1 << 0,
115   /* chroma blue */
116   PLANE_COMPONENT_Cb = 1 << 1,
117   /* chroma red */
118   PLANE_COMPONENT_Cr = 1 << 2,
119 
120   /* red */
121   PLANE_COMPONENT_R = 1 << 10,
122   /* green */
123   PLANE_COMPONENT_G = 1 << 11,
124   /* blue */
125   PLANE_COMPONENT_B = 1 << 12,
126 
127   /* alpha */
128   PLANE_COMPONENT_A = 1 << 20,
129 
130   /* raw data plane */
131   PLANE_COMPONENT_RAW = 1 << 30,
132 
133   /* meta information plane */
134   PLANE_COMPONENT_META = 1 << 31,
135 };
136 
137 struct PlaneLayoutInfo {
138   /** Components represented the type of plane. */
139   PlaneComponent component;
140 
141   /** horizontal subsampling. Must be a positive power of 2. */
142   uint32_t h_subsampling;
143 
144   /** vertical subsampling. Must be a positive power of 2. */
145   uint32_t v_subsampling;
146 
147   /** offset to the first byte of the top-left pixel of the plane
148    *  and it is calculated from the start of the buffer.
149    *  Add base of the handle with offset to get the first byte of the plane.
150    */
151   uint32_t offset;
152 
153   /** step is the distance in bytes from one pixel value to the next. */
154   int32_t step;
155 
156   /** stride of the plane in pixels */
157   int32_t stride;
158 
159   /** stride of the plane in in bytes */
160   int32_t stride_bytes;
161 
162   /** plane height or vertical stride */
163   int32_t scanlines;
164 
165   /** size of the plane in bytes */
166   uint32_t size;
167 };
168 
169 bool IsYuvFormat(int format);
170 bool IsCompressedRGBFormat(int format);
171 bool IsUncompressedRGBFormat(int format);
172 uint32_t GetBppForUncompressedRGB(int format);
173 bool CpuCanAccess(uint64_t usage);
174 bool CpuCanRead(uint64_t usage);
175 bool CpuCanWrite(uint64_t usage);
176 int GetBpp(int format);
177 unsigned int GetSize(const BufferInfo &d, unsigned int alignedw, unsigned int alignedh);
178 int GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
179                                unsigned int *alignedh);
180 int GetBufferSizeAndDimensions(const BufferInfo &d, unsigned int *size, unsigned int *alignedw,
181                                unsigned int *alignedh, GraphicsMetadata *graphics_metadata);
182 void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height);
183 void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space);
184 void GetAlignedWidthAndHeight(const BufferInfo &d, unsigned int *aligned_w,
185                               unsigned int *aligned_h);
186 int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]);
187 int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
188                     int32_t flags, int *plane_count, PlaneLayoutInfo plane_info[8]);
189 void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
190                      int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info);
191 unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage);
192 void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling);
193 void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
194                                        struct android_ycbcr *ycbcr);
195 int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data);
196 bool IsUBwcFormat(int format);
197 bool IsUBwcSupported(int format);
198 bool IsUBwcPISupported(int format, uint64_t usage);
199 bool IsUBwcEnabled(int format, uint64_t usage);
200 bool IsCameraCustomFormat(int format);
201 void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
202                               unsigned int *aligned_h);
203 void GetYuvSPPlaneInfo(const BufferInfo &info, int format, uint32_t width, uint32_t height,
204                        uint32_t bpp, PlaneLayoutInfo *plane_info);
205 void GetYuvUbwcSPPlaneInfo(uint32_t width, uint32_t height, int color_format,
206                            PlaneLayoutInfo *plane_info);
207 void GetYuvUbwcInterlacedSPPlaneInfo(uint32_t width, uint32_t height,
208                                      PlaneLayoutInfo plane_info[8]);
209 void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height);
210 unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp);
211 unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
212                          unsigned int alignedh);
213 int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
214                     uint32_t *num_planes);
215 uint32_t GetDataAlignment(int format, uint64_t usage);
216 int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
217                                     unsigned int *alignedw, unsigned int *alignedh,
218                                     GraphicsMetadata *graphics_metadata);
219 bool CanUseAdrenoForSize(int buffer_type, uint64_t usage);
220 bool GetAdrenoSizeAPIStatus();
221 bool UseUncached(int format, uint64_t usage);
222 uint64_t GetHandleFlags(int format, uint64_t usage);
223 int GetImplDefinedFormat(uint64_t usage, int format);
224 int GetCustomFormatFlags(int format, uint64_t usage, int *custom_format, uint64_t *priv_flags);
225 int GetBufferType(int inputFormat);
226 bool IsGPUFlagSupported(uint64_t usage);
227 bool HasAlphaComponent(int32_t format);
228 
229 void GetDRMFormat(uint32_t format, uint32_t flags, uint32_t *drm_format,
230                   uint64_t *drm_format_modifier);
231 bool CanAllocateZSLForSecureCamera();
232 }  // namespace gralloc
233 
234 #endif  // __GR_UTILS_H__
235