1 /*
2  * Copyright (C) 2017 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <hardware/hardware.h>
20 #include <hardware/gralloc1.h>
21 
22 #include "mali_gralloc_private_interface.h"
23 #include "mali_gralloc_buffer.h"
24 #include "gralloc_helper.h"
25 #include "gralloc_buffer_priv.h"
26 #include "mali_gralloc_bufferdescriptor.h"
27 
28 #define CHECK_FUNCTION(A, B, C)                    \
29 	do                                             \
30 	{                                              \
31 		if (A == B)                                \
32 			return (gralloc1_function_pointer_t)C; \
33 	} while (0)
34 
mali_gralloc_private_get_buff_int_fmt(gralloc1_device_t * device,buffer_handle_t handle,uint64_t * internal_format)35 static int32_t mali_gralloc_private_get_buff_int_fmt(gralloc1_device_t *device, buffer_handle_t handle,
36                                                      uint64_t *internal_format)
37 {
38 	GRALLOC_UNUSED(device);
39 
40 	if (private_handle_t::validate(handle) < 0 || internal_format == NULL)
41 	{
42 		return GRALLOC1_ERROR_BAD_HANDLE;
43 	}
44 
45 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
46 	*internal_format = hnd->internal_format;
47 
48 	return GRALLOC1_ERROR_NONE;
49 }
50 
mali_gralloc_private_get_buff_fd(gralloc1_device_t * device,buffer_handle_t handle,int * fd)51 static int32_t mali_gralloc_private_get_buff_fd(gralloc1_device_t *device, buffer_handle_t handle, int *fd)
52 {
53 	GRALLOC_UNUSED(device);
54 
55 	if (private_handle_t::validate(handle) < 0 || fd == NULL)
56 	{
57 		return GRALLOC1_ERROR_BAD_HANDLE;
58 	}
59 
60 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
61 	*fd = hnd->share_fd;
62 
63 	return GRALLOC1_ERROR_NONE;
64 }
65 
mali_gralloc_private_get_buff_int_dims(gralloc1_device_t * device,buffer_handle_t handle,int * internalWidth,int * internalHeight)66 static int32_t mali_gralloc_private_get_buff_int_dims(gralloc1_device_t *device, buffer_handle_t handle,
67                                                       int *internalWidth, int *internalHeight)
68 {
69 	GRALLOC_UNUSED(device);
70 
71 	if (private_handle_t::validate(handle) < 0 || internalWidth == NULL || internalHeight == NULL)
72 	{
73 		return GRALLOC1_ERROR_BAD_HANDLE;
74 	}
75 
76 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
77 	*internalWidth = hnd->internalWidth;
78 	*internalHeight = hnd->internalHeight;
79 
80 	return GRALLOC1_ERROR_NONE;
81 }
82 
mali_gralloc_private_get_buff_offset(gralloc1_device_t * device,buffer_handle_t handle,int64_t * offset)83 static int32_t mali_gralloc_private_get_buff_offset(gralloc1_device_t *device, buffer_handle_t handle, int64_t *offset)
84 {
85 	GRALLOC_UNUSED(device);
86 
87 	if (private_handle_t::validate(handle) < 0 || offset == NULL)
88 	{
89 		return GRALLOC1_ERROR_BAD_HANDLE;
90 	}
91 
92 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
93 	*offset = hnd->offset;
94 
95 	return GRALLOC1_ERROR_NONE;
96 }
97 
mali_gralloc_private_get_buff_bytestride(gralloc1_device_t * device,buffer_handle_t handle,int * bytestride)98 static int32_t mali_gralloc_private_get_buff_bytestride(gralloc1_device_t *device, buffer_handle_t handle,
99                                                         int *bytestride)
100 {
101 	GRALLOC_UNUSED(device);
102 
103 	if (private_handle_t::validate(handle) < 0 || bytestride == NULL)
104 	{
105 		return GRALLOC1_ERROR_BAD_HANDLE;
106 	}
107 
108 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
109 	*bytestride = hnd->byte_stride;
110 
111 	return GRALLOC1_ERROR_NONE;
112 }
113 
mali_gralloc_private_get_buff_yuvinfo(gralloc1_device_t * device,buffer_handle_t handle,mali_gralloc_yuv_info * yuvinfo)114 static int32_t mali_gralloc_private_get_buff_yuvinfo(gralloc1_device_t *device, buffer_handle_t handle,
115                                                      mali_gralloc_yuv_info *yuvinfo)
116 {
117 	GRALLOC_UNUSED(device);
118 
119 	if (private_handle_t::validate(handle) < 0 || yuvinfo == NULL)
120 	{
121 		return GRALLOC1_ERROR_BAD_HANDLE;
122 	}
123 
124 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
125 	*yuvinfo = hnd->yuv_info;
126 
127 	return GRALLOC1_ERROR_NONE;
128 }
129 
mali_gralloc_private_get_buff_size(gralloc1_device_t * device,buffer_handle_t handle,int * size)130 static int32_t mali_gralloc_private_get_buff_size(gralloc1_device_t *device, buffer_handle_t handle, int *size)
131 {
132 	GRALLOC_UNUSED(device);
133 
134 	if (private_handle_t::validate(handle) < 0 || size == NULL)
135 	{
136 		return GRALLOC1_ERROR_BAD_HANDLE;
137 	}
138 
139 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
140 	*size = hnd->size;
141 
142 	return GRALLOC1_ERROR_NONE;
143 }
144 
mali_gralloc_private_get_buff_flags(gralloc1_device_t * device,buffer_handle_t handle,int * flags)145 static int32_t mali_gralloc_private_get_buff_flags(gralloc1_device_t *device, buffer_handle_t handle, int *flags)
146 {
147 	GRALLOC_UNUSED(device);
148 
149 	if (private_handle_t::validate(handle) < 0 || flags == NULL)
150 	{
151 		return GRALLOC1_ERROR_BAD_HANDLE;
152 	}
153 
154 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
155 	*flags = hnd->flags;
156 
157 	return GRALLOC1_ERROR_NONE;
158 }
159 
mali_gralloc_private_get_buff_min_page_size(gralloc1_device_t * device,buffer_handle_t handle,int * min_pgsz)160 static int32_t mali_gralloc_private_get_buff_min_page_size(gralloc1_device_t *device, buffer_handle_t handle,
161                                                            int *min_pgsz)
162 {
163 	GRALLOC_UNUSED(device);
164 
165 	if (private_handle_t::validate(handle) < 0 || min_pgsz == NULL)
166 	{
167 		return GRALLOC1_ERROR_BAD_HANDLE;
168 	}
169 
170 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
171 	*min_pgsz = hnd->min_pgsz;
172 
173 	return GRALLOC1_ERROR_NONE;
174 }
175 
mali_gralloc_private_get_attr_param(gralloc1_device_t * device,buffer_handle_t handle,buf_attr attr,int32_t * val,int32_t last_call)176 static int32_t mali_gralloc_private_get_attr_param(gralloc1_device_t *device, buffer_handle_t handle, buf_attr attr,
177                                                    int32_t *val, int32_t last_call)
178 {
179 	GRALLOC_UNUSED(device);
180 
181 	if (private_handle_t::validate(handle) < 0 || val == NULL)
182 	{
183 		return GRALLOC1_ERROR_BAD_HANDLE;
184 	}
185 
186 	const private_handle_t *const_hnd = static_cast<const private_handle_t *>(handle);
187 	private_handle_t *hnd = const_cast<private_handle_t *>(const_hnd);
188 
189 	if (hnd->attr_base == MAP_FAILED)
190 	{
191 		if (gralloc_buffer_attr_map(hnd, 1) < 0)
192 		{
193 			return GRALLOC1_ERROR_BAD_HANDLE;
194 		}
195 	}
196 
197 	if (gralloc_buffer_attr_read(hnd, attr, val) < 0)
198 	{
199 		gralloc_buffer_attr_unmap(hnd);
200 		return GRALLOC1_ERROR_BAD_HANDLE;
201 	}
202 
203 	if (last_call)
204 	{
205 		gralloc_buffer_attr_unmap(hnd);
206 	}
207 
208 	return GRALLOC1_ERROR_NONE;
209 }
210 
mali_gralloc_private_set_attr_param(gralloc1_device_t * device,buffer_handle_t handle,buf_attr attr,int32_t * val,int32_t last_call)211 static int32_t mali_gralloc_private_set_attr_param(gralloc1_device_t *device, buffer_handle_t handle, buf_attr attr,
212                                                    int32_t *val, int32_t last_call)
213 {
214 	GRALLOC_UNUSED(device);
215 
216 	if (private_handle_t::validate(handle) < 0 || val == NULL)
217 	{
218 		return GRALLOC1_ERROR_BAD_HANDLE;
219 	}
220 
221 	const private_handle_t *const_hnd = static_cast<const private_handle_t *>(handle);
222 	private_handle_t *hnd = const_cast<private_handle_t *>(const_hnd);
223 
224 	if (hnd->attr_base == MAP_FAILED)
225 	{
226 		if (gralloc_buffer_attr_map(hnd, 1) < 0)
227 		{
228 			return GRALLOC1_ERROR_BAD_HANDLE;
229 		}
230 	}
231 
232 	if (gralloc_buffer_attr_write(hnd, attr, val) < 0)
233 	{
234 		gralloc_buffer_attr_unmap(hnd);
235 		return GRALLOC1_ERROR_BAD_HANDLE;
236 	}
237 
238 	if (last_call)
239 	{
240 		gralloc_buffer_attr_unmap(hnd);
241 	}
242 
243 	return GRALLOC1_ERROR_NONE;
244 }
245 
mali_gralloc_private_set_priv_fmt(gralloc1_device_t * device,gralloc1_buffer_descriptor_t desc,uint64_t internal_format)246 static int32_t mali_gralloc_private_set_priv_fmt(gralloc1_device_t *device, gralloc1_buffer_descriptor_t desc,
247                                                  uint64_t internal_format)
248 {
249 	GRALLOC_UNUSED(device);
250 
251 	buffer_descriptor_t *priv_desc = reinterpret_cast<buffer_descriptor_t *>(desc);
252 
253 	if (priv_desc == NULL)
254 	{
255 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
256 	}
257 
258 	priv_desc->hal_format = internal_format;
259 	priv_desc->format_type = MALI_GRALLOC_FORMAT_TYPE_INTERNAL;
260 
261 	return GRALLOC1_ERROR_NONE;
262 }
263 
mali_gralloc_private_interface_getFunction(int32_t descriptor)264 gralloc1_function_pointer_t mali_gralloc_private_interface_getFunction(int32_t descriptor)
265 {
266 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_INT_FMT, mali_gralloc_private_get_buff_int_fmt);
267 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_FD, mali_gralloc_private_get_buff_fd);
268 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_INTERNAL_DIMS, mali_gralloc_private_get_buff_int_dims);
269 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_OFFSET, mali_gralloc_private_get_buff_offset);
270 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_BYTESTRIDE, mali_gralloc_private_get_buff_bytestride);
271 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_YUVINFO, mali_gralloc_private_get_buff_yuvinfo);
272 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_SIZE, mali_gralloc_private_get_buff_size);
273 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_FLAGS, mali_gralloc_private_get_buff_flags);
274 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_MIN_PAGESIZE,
275 	               mali_gralloc_private_get_buff_min_page_size);
276 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_ATTR_PARAM, mali_gralloc_private_get_attr_param);
277 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_SET_ATTR_PARAM, mali_gralloc_private_set_attr_param);
278 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_SET_PRIV_FMT, mali_gralloc_private_set_priv_fmt);
279 
280 	return NULL;
281 }
282