1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2011 - 2018, The Linux Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef GRALLOC_PRIV_H_
19 #define GRALLOC_PRIV_H_
20 
21 #include <errno.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <stdint.h>
25 #include <sys/cdefs.h>
26 #include <unistd.h>
27 
28 
29 /* DEPRECATION NOTICE: This file is no longer used, please use the new
30  * implementation in libgralloc1
31  */
32 
33 
34 
35 
36 #include <cutils/native_handle.h>
37 #include <log/log.h>
38 
39 #include <hardware/gralloc.h>
40 
41 #define ROUND_UP_PAGESIZE(x) (unsigned int)( ((x) + getpagesize()-1)  & \
42                                              (~(getpagesize()-1)) )
43 
44 /* Gralloc usage bits indicating the type of allocation that should be used */
45 /* SYSTEM heap comes from kernel vmalloc (ION_SYSTEM_HEAP_ID)
46  * is cached by default and
47  * is not secured */
48 
49 /* GRALLOC_USAGE_PRIVATE_0 is unused */
50 
51 /* Non linear, Universal Bandwidth Compression */
52 #define GRALLOC_USAGE_PRIVATE_ALLOC_UBWC      GRALLOC_USAGE_PRIVATE_1
53 
54 /* IOMMU heap comes from manually allocated pages, can be cached/uncached,
55  * is not secured */
56 #define GRALLOC_USAGE_PRIVATE_IOMMU_HEAP      GRALLOC_USAGE_PRIVATE_2
57 
58 /* MM heap is a carveout heap for video, can be secured */
59 #define GRALLOC_USAGE_PRIVATE_MM_HEAP         GRALLOC_USAGE_PRIVATE_3
60 
61 /* ADSP heap is a carveout heap, is not secured */
62 #define GRALLOC_USAGE_PRIVATE_ADSP_HEAP       0x01000000
63 
64 /* Set this for allocating uncached memory (using O_DSYNC),
65  * cannot be used with noncontiguous heaps */
66 #define GRALLOC_USAGE_PRIVATE_UNCACHED        0x02000000
67 
68 /* Buffer content should be displayed on an primary display only */
69 #define GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY   0x04000000
70 
71 /* Buffer content should be displayed on an external display only */
72 #define GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY   0x08000000
73 
74 /* This flag is set for WFD usecase */
75 #define GRALLOC_USAGE_PRIVATE_WFD             0x00200000
76 
77 /* CAMERA heap is a carveout heap for camera, is not secured */
78 #define GRALLOC_USAGE_PRIVATE_CAMERA_HEAP     0x00400000
79 
80 /* This flag is used for SECURE display usecase */
81 #define GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY  0x00800000
82 
83 /* define Gralloc perform */
84 #define GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER 1
85 // This will be used by the graphics drivers to know if certain features
86 // are defined in this display HAL.
87 // Ex: Newer GFX libraries + Older Display HAL
88 #define GRALLOC_MODULE_PERFORM_GET_STRIDE 2
89 #define GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE 3
90 #define GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE 4
91 #define GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES 5
92 #define GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE 6
93 #define GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO 7
94 #define GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO 8
95 #define GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG 9
96 #define GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS 10
97 #define GRALLOC_MODULE_PERFORM_GET_IGC 11
98 #define GRALLOC_MODULE_PERFORM_SET_IGC 12
99 #define GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE 13
100 
101 /* OEM specific HAL formats */
102 
103 #define HAL_PIXEL_FORMAT_RGBA_5551               6
104 #define HAL_PIXEL_FORMAT_RGBA_4444               7
105 #define HAL_PIXEL_FORMAT_NV12_ENCODEABLE         0x102
106 #define HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS      0x7FA30C04
107 #define HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED      0x7FA30C03
108 #define HAL_PIXEL_FORMAT_YCbCr_420_SP            0x109
109 #define HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO     0x7FA30C01
110 #define HAL_PIXEL_FORMAT_YCrCb_422_SP            0x10B
111 #define HAL_PIXEL_FORMAT_R_8                     0x10D
112 #define HAL_PIXEL_FORMAT_RG_88                   0x10E
113 #define HAL_PIXEL_FORMAT_YCbCr_444_SP            0x10F
114 #define HAL_PIXEL_FORMAT_YCrCb_444_SP            0x110
115 #define HAL_PIXEL_FORMAT_YCrCb_422_I             0x111
116 #define HAL_PIXEL_FORMAT_BGRX_8888               0x112
117 #define HAL_PIXEL_FORMAT_NV21_ZSL                0x113
118 #define HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS      0x114
119 #define HAL_PIXEL_FORMAT_BGR_565                 0x115
120 #define HAL_PIXEL_FORMAT_RGBA_1010102            0x116
121 #define HAL_PIXEL_FORMAT_ARGB_2101010            0x117
122 #define HAL_PIXEL_FORMAT_RGBX_1010102            0x118
123 #define HAL_PIXEL_FORMAT_XRGB_2101010            0x119
124 #define HAL_PIXEL_FORMAT_BGRA_1010102            0x11A
125 #define HAL_PIXEL_FORMAT_ABGR_2101010            0x11B
126 #define HAL_PIXEL_FORMAT_BGRX_1010102            0x11C
127 #define HAL_PIXEL_FORMAT_XBGR_2101010            0x11D
128 #define HAL_PIXEL_FORMAT_YCbCr_420_P010          0x11F
129 #define HAL_PIXEL_FORMAT_CbYCrY_422_I            0x120
130 #define HAL_PIXEL_FORMAT_BGR_888                 0x121
131 #define HAL_PIXEL_FORMAT_RAW8                    0x123
132 
133 #define HAL_PIXEL_FORMAT_INTERLACE               0x180
134 
135 //v4l2_fourcc('Y', 'U', 'Y', 'L'). 24 bpp YUYV 4:2:2 10 bit per component
136 #define HAL_PIXEL_FORMAT_YCbCr_422_I_10BIT       0x4C595559
137 
138 //v4l2_fourcc('Y', 'B', 'W', 'C'). 10 bit per component. This compressed
139 //format reduces the memory access bandwidth
140 #define HAL_PIXEL_FORMAT_YCbCr_422_I_10BIT_COMPRESSED  0x43574259
141 
142 // UBWC aligned Venus format
143 #define HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC 0x7FA30C06
144 #define HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC     0x7FA30C09
145 
146 //Khronos ASTC formats
147 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR             0x93B0
148 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR             0x93B1
149 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR             0x93B2
150 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR             0x93B3
151 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR             0x93B4
152 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR             0x93B5
153 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR             0x93B6
154 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR             0x93B7
155 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR            0x93B8
156 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR            0x93B9
157 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR            0x93BA
158 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR           0x93BB
159 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR           0x93BC
160 #define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR           0x93BD
161 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR     0x93D0
162 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR     0x93D1
163 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR     0x93D2
164 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR     0x93D3
165 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR     0x93D4
166 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR     0x93D5
167 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR     0x93D6
168 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR     0x93D7
169 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR    0x93D8
170 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR    0x93D9
171 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR    0x93DA
172 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR   0x93DB
173 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR   0x93DC
174 #define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR   0x93DD
175 
176 /* possible values for inverse gamma correction */
177 #define HAL_IGC_NOT_SPECIFIED     0
178 #define HAL_IGC_s_RGB             1
179 
180 /* Color Space: Values maps to ColorSpace_t in qdMetadata.h */
181 #define HAL_CSC_ITU_R_601         0
182 #define HAL_CSC_ITU_R_601_FR      1
183 #define HAL_CSC_ITU_R_709         2
184 #define HAL_CSC_ITU_R_2020        3
185 #define HAL_CSC_ITU_R_2020_FR     4
186 
187 /* possible formats for 3D content*/
188 enum {
189     HAL_NO_3D                      = 0x0,
190     HAL_3D_SIDE_BY_SIDE_L_R        = 0x1,
191     HAL_3D_SIDE_BY_SIDE_R_L        = 0x2,
192     HAL_3D_TOP_BOTTOM              = 0x4,
193     HAL_3D_IN_SIDE_BY_SIDE_L_R     = 0x10000, //unused legacy format
194 };
195 
196 enum {
197     BUFFER_TYPE_UI = 0,
198     BUFFER_TYPE_VIDEO
199 };
200 
201 #ifdef __cplusplus
202 struct private_handle_t : public native_handle {
203 #else
204     struct private_handle_t {
205         native_handle_t nativeHandle;
206 #endif
207         enum {
208             PRIV_FLAGS_FRAMEBUFFER        = 0x00000001,
209             PRIV_FLAGS_USES_ION           = 0x00000008,
210             PRIV_FLAGS_USES_ASHMEM        = 0x00000010,
211             PRIV_FLAGS_NEEDS_FLUSH        = 0x00000020,
212             PRIV_FLAGS_INTERNAL_ONLY      = 0x00000040,
213             PRIV_FLAGS_NON_CPU_WRITER     = 0x00000080,
214             PRIV_FLAGS_NONCONTIGUOUS_MEM  = 0x00000100,
215             PRIV_FLAGS_CACHED             = 0x00000200,
216             PRIV_FLAGS_SECURE_BUFFER      = 0x00000400,
217             // Display on external only
218             PRIV_FLAGS_EXTERNAL_ONLY      = 0x00002000,
219             // Set by HWC for protected non secure buffers
220             PRIV_FLAGS_PROTECTED_BUFFER   = 0x00004000,
221             PRIV_FLAGS_VIDEO_ENCODER      = 0x00010000,
222             PRIV_FLAGS_CAMERA_WRITE       = 0x00020000,
223             PRIV_FLAGS_CAMERA_READ        = 0x00040000,
224             PRIV_FLAGS_HW_COMPOSER        = 0x00080000,
225             PRIV_FLAGS_HW_TEXTURE         = 0x00100000,
226             PRIV_FLAGS_ITU_R_601          = 0x00200000, //Unused from display
227             PRIV_FLAGS_ITU_R_601_FR       = 0x00400000, //Unused from display
228             PRIV_FLAGS_ITU_R_709          = 0x00800000, //Unused from display
229             PRIV_FLAGS_SECURE_DISPLAY     = 0x01000000,
230             // Buffer is rendered in Tile Format
231             PRIV_FLAGS_TILE_RENDERED      = 0x02000000,
232             // Buffer rendered using CPU/SW renderer
233             PRIV_FLAGS_CPU_RENDERED       = 0x04000000,
234             // Buffer is allocated with UBWC alignment
235             PRIV_FLAGS_UBWC_ALIGNED       = 0x08000000,
236             // Buffer allocated will be consumed by SF/HWC
237             PRIV_FLAGS_DISP_CONSUMER      = 0x10000000
238         };
239 
240         // file-descriptors
241         int     fd;
242         int     fd_metadata;          // fd for the meta-data
243         // ints
244         int     magic;
245         int     flags;
246         unsigned int  size;
247         unsigned int  offset;
248         int     bufferType;
249         uint64_t base __attribute__((aligned(8)));
250         unsigned int  offset_metadata;
251         // The gpu address mapped into the mmu.
252         uint64_t gpuaddr __attribute__((aligned(8)));
253         int     format;
254         int     width;   // holds aligned width of the actual buffer allocated
255         int     height;  // holds aligned height of the  actual buffer allocated
256         uint64_t base_metadata __attribute__((aligned(8)));
257         int unaligned_width;   // holds width client asked to allocate
258         int unaligned_height;  // holds height client asked to allocate
259         unsigned int gem_handle;
260         unsigned int fb_id;
261 
262 #ifdef __cplusplus
263         static const int sNumFds = 2;
sNumIntsprivate_handle_t264         static inline int sNumInts() {
265             return ((sizeof(private_handle_t) - sizeof(native_handle_t)) /
266                     sizeof(int)) - sNumFds;
267         }
268         static const int sMagic = 'gmsm';
269 
private_handle_tprivate_handle_t270         private_handle_t(int fd, unsigned int size, int flags, int bufferType,
271                 int format, int width, int height) :
272             fd(fd), fd_metadata(-1), magic(sMagic),
273             flags(flags), size(size), offset(0), bufferType(bufferType),
274             base(0), offset_metadata(0), gpuaddr(0),
275             format(format), width(width), height(height),
276             base_metadata(0), unaligned_width(width),
277             unaligned_height(height), gem_handle(0), fb_id(0)
278         {
279             version = (int) sizeof(native_handle);
280             numInts = sNumInts();
281             numFds = sNumFds;
282         }
283 
private_handle_tprivate_handle_t284         private_handle_t(int fd, unsigned int size, int flags, int bufferType,
285                 int format, int width, int height,
286                 int eFd, unsigned int eOffset, uint64_t eBase) :
287             private_handle_t(fd, size, flags, bufferType, format, width, height)
288         {
289             fd_metadata = eFd;
290             offset_metadata = eOffset;
291             base_metadata = eBase;
292         }
293 
private_handle_tprivate_handle_t294         private_handle_t(int fd, unsigned int size, int flags, int bufferType,
295                 int format, int width, int height,
296                 int eFd, unsigned int eOffset, uint64_t eBase,
297                 int unaligned_w, int unaligned_h) :
298             private_handle_t(fd, size, flags, bufferType, format, width, height,
299                     eFd, eOffset, eBase)
300         {
301             unaligned_width = unaligned_w;
302             unaligned_height = unaligned_h;
303         }
304 
~private_handle_tprivate_handle_t305         ~private_handle_t() {
306             magic = 0;
307         }
308 
clean_magic_byteprivate_handle_t309         static inline char clean_magic_byte(int b) {
310           return (b & 0xff) ? (b & 0xff) : '-';
311         }
312 
validateprivate_handle_t313         static int validate(const native_handle* h) {
314             const private_handle_t* hnd = (const private_handle_t*)h;
315             if (!h || h->version != sizeof(native_handle) ||
316                 h->numInts != sNumInts() || h->numFds != sNumFds) {
317                 ALOGD("Invalid gralloc handle (at %p): "
318                       "ver(%d/%zu) ints(%d/%d) fds(%d/%d)",
319                       h, h ? h->version : -1, sizeof(native_handle),
320                       h ? h->numInts : -1, sNumInts(),
321                       h ? h->numFds : -1, sNumFds);
322                 return -EINVAL;
323             }
324             if (hnd->magic != sMagic) {
325                 ALOGD("magic(%c%c%c%c/%c%c%c%c)",
326                       clean_magic_byte(hnd->magic >> 24),
327                       clean_magic_byte(hnd->magic >> 16),
328                       clean_magic_byte(hnd->magic >> 8),
329                       clean_magic_byte(hnd->magic >> 0),
330                       (sMagic >> 24) & 0xFF,
331                       (sMagic >> 16) & 0xFF,
332                       (sMagic >> 8) & 0xFF,
333                       (sMagic >> 0) & 0xFF);
334                 return -EINVAL;
335             }
336             return 0;
337         }
338 
dynamicCastprivate_handle_t339         static private_handle_t* dynamicCast(const native_handle* in) {
340             if (validate(in) == 0) {
341                 return (private_handle_t*) in;
342             }
343             return NULL;
344         }
345 #endif
346     };
347 
348 #endif /* GRALLOC_PRIV_H_ */
349