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