1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  * Copyright (C) 2016 Linaro, Ltd., Rob Herring <robh@kernel.org>
5  * Copyright (C) 2018 Collabora, Robert Foss <robert.foss@collabora.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef __ANDROID_GRALLOC_HANDLE_H__
27 #define __ANDROID_GRALLOC_HANDLE_H__
28 
29 #include <cutils/native_handle.h>
30 #include <stdint.h>
31 
32 /* support users of drm_gralloc/gbm_gralloc */
33 #define gralloc_gbm_handle_t gralloc_handle_t
34 #define gralloc_drm_handle_t gralloc_handle_t
35 
36 struct gralloc_handle_t {
37 	native_handle_t base;
38 
39 	/* dma-buf file descriptor
40 	 * Must be located first since, native_handle_t is allocated
41 	 * using native_handle_create(), which allocates space for
42 	 * sizeof(native_handle_t) + sizeof(int) * (numFds + numInts)
43 	 * numFds = GRALLOC_HANDLE_NUM_FDS
44 	 * numInts = GRALLOC_HANDLE_NUM_INTS
45 	 * Where numFds represents the number of FDs and
46 	 * numInts represents the space needed for the
47 	 * remainder of this struct.
48 	 * And the FDs are expected to be found first following
49 	 * native_handle_t.
50 	 */
51 	int prime_fd;
52 
53 	/* api variables */
54 	uint32_t magic; /* differentiate between allocator impls */
55 	uint32_t version; /* api version */
56 
57 	uint32_t width; /* width of buffer in pixels */
58 	uint32_t height; /* height of buffer in pixels */
59 	uint32_t format; /* pixel format (Android) */
60 	uint32_t usage; /* android libhardware usage flags */
61 
62 	uint32_t stride; /* the stride in bytes */
63 	int data_owner; /* owner of data (for validation) */
64 	uint64_t modifier __attribute__((aligned(8))); /* buffer modifiers */
65 
66 	union {
67 		void *data; /* pointer to struct gralloc_gbm_bo_t */
68 		uint64_t reserved;
69 	} __attribute__((aligned(8)));
70 };
71 
72 #define GRALLOC_HANDLE_VERSION 4
73 #define GRALLOC_HANDLE_MAGIC 0x60585350
74 #define GRALLOC_HANDLE_NUM_FDS 1
75 #define GRALLOC_HANDLE_NUM_INTS (	\
76 	((sizeof(struct gralloc_handle_t) - sizeof(native_handle_t))/sizeof(int))	\
77 	 - GRALLOC_HANDLE_NUM_FDS)
78 
gralloc_handle(buffer_handle_t handle)79 static inline struct gralloc_handle_t *gralloc_handle(buffer_handle_t handle)
80 {
81 	return (struct gralloc_handle_t *)handle;
82 }
83 
84 /**
85  * Create a buffer handle.
86  */
gralloc_handle_create(int32_t width,int32_t height,int32_t hal_format,int32_t usage)87 static inline native_handle_t *gralloc_handle_create(int32_t width,
88                                                      int32_t height,
89                                                      int32_t hal_format,
90                                                      int32_t usage)
91 {
92 	struct gralloc_handle_t *handle;
93 	native_handle_t *nhandle = native_handle_create(GRALLOC_HANDLE_NUM_FDS,
94 							GRALLOC_HANDLE_NUM_INTS);
95 
96 	if (!nhandle)
97 		return NULL;
98 
99 	handle = gralloc_handle(nhandle);
100 	handle->magic = GRALLOC_HANDLE_MAGIC;
101 	handle->version = GRALLOC_HANDLE_VERSION;
102 	handle->width = width;
103 	handle->height = height;
104 	handle->format = hal_format;
105 	handle->usage = usage;
106 	handle->prime_fd = -1;
107 
108 	return nhandle;
109 }
110 
111 #endif
112