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