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)
41 	{
42 		return GRALLOC1_ERROR_BAD_HANDLE;
43 	}
44 
45 	if (internal_format == NULL)
46 	{
47 		return GRALLOC1_ERROR_BAD_VALUE;
48 	}
49 
50 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
51 	*internal_format = hnd->internal_format;
52 
53 	return GRALLOC1_ERROR_NONE;
54 }
55 
mali_gralloc_private_get_buff_fd(gralloc1_device_t * device,buffer_handle_t handle,int * fd)56 static int32_t mali_gralloc_private_get_buff_fd(gralloc1_device_t *device, buffer_handle_t handle, int *fd)
57 {
58 	GRALLOC_UNUSED(device);
59 
60 	if (private_handle_t::validate(handle) < 0)
61 	{
62 		return GRALLOC1_ERROR_BAD_HANDLE;
63 	}
64 
65 	if (fd == NULL)
66 	{
67 		return GRALLOC1_ERROR_BAD_VALUE;
68 	}
69 
70 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
71 	*fd = hnd->share_fd;
72 
73 	return GRALLOC1_ERROR_NONE;
74 }
75 
mali_gralloc_private_get_buff_int_dims(gralloc1_device_t * device,buffer_handle_t handle,int * internalWidth,int * internalHeight)76 static int32_t mali_gralloc_private_get_buff_int_dims(gralloc1_device_t *device, buffer_handle_t handle,
77                                                       int *internalWidth, int *internalHeight)
78 {
79 	GRALLOC_UNUSED(device);
80 
81 	if (private_handle_t::validate(handle) < 0)
82 	{
83 		return GRALLOC1_ERROR_BAD_HANDLE;
84 	}
85 
86 	if (internalWidth == NULL || internalHeight == NULL)
87 	{
88 		return GRALLOC1_ERROR_BAD_VALUE;
89 	}
90 
91 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
92 	*internalWidth = hnd->plane_info[0].alloc_width;
93 	*internalHeight = hnd->plane_info[0].alloc_height;
94 
95 	return GRALLOC1_ERROR_NONE;
96 }
97 
mali_gralloc_private_get_buff_offset(gralloc1_device_t * device,buffer_handle_t handle,int64_t * offset)98 static int32_t mali_gralloc_private_get_buff_offset(gralloc1_device_t *device, buffer_handle_t handle, int64_t *offset)
99 {
100 	GRALLOC_UNUSED(device);
101 
102 	if (private_handle_t::validate(handle) < 0)
103 	{
104 		return GRALLOC1_ERROR_BAD_HANDLE;
105 	}
106 
107 	if (offset == NULL)
108 	{
109 		return GRALLOC1_ERROR_BAD_VALUE;
110 	}
111 
112 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
113 	*offset = hnd->offset;
114 
115 	return GRALLOC1_ERROR_NONE;
116 }
117 
mali_gralloc_private_get_buff_bytestride(gralloc1_device_t * device,buffer_handle_t handle,int * bytestride)118 static int32_t mali_gralloc_private_get_buff_bytestride(gralloc1_device_t *device, buffer_handle_t handle,
119                                                         int *bytestride)
120 {
121 	GRALLOC_UNUSED(device);
122 
123 	if (private_handle_t::validate(handle) < 0)
124 	{
125 		return GRALLOC1_ERROR_BAD_HANDLE;
126 	}
127 
128 	if (bytestride == NULL)
129 	{
130 		return GRALLOC1_ERROR_BAD_VALUE;
131 	}
132 
133 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
134 	*bytestride = hnd->plane_info[0].byte_stride;
135 
136 	return GRALLOC1_ERROR_NONE;
137 }
138 
mali_gralloc_private_get_buff_yuvinfo(gralloc1_device_t * device,buffer_handle_t handle,mali_gralloc_yuv_info * yuvinfo)139 static int32_t mali_gralloc_private_get_buff_yuvinfo(gralloc1_device_t *device, buffer_handle_t handle,
140                                                      mali_gralloc_yuv_info *yuvinfo)
141 {
142 	GRALLOC_UNUSED(device);
143 
144 	if (private_handle_t::validate(handle) < 0)
145 	{
146 		return GRALLOC1_ERROR_BAD_HANDLE;
147 	}
148 
149 	if (yuvinfo == NULL)
150 	{
151 		return GRALLOC1_ERROR_BAD_VALUE;
152 	}
153 
154 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
155 	*yuvinfo = hnd->yuv_info;
156 
157 	return GRALLOC1_ERROR_NONE;
158 }
159 
mali_gralloc_private_get_buff_size(gralloc1_device_t * device,buffer_handle_t handle,int * size)160 static int32_t mali_gralloc_private_get_buff_size(gralloc1_device_t *device, buffer_handle_t handle, int *size)
161 {
162 	GRALLOC_UNUSED(device);
163 
164 	if (private_handle_t::validate(handle) < 0)
165 	{
166 		return GRALLOC1_ERROR_BAD_HANDLE;
167 	}
168 
169 	if (size == NULL)
170 	{
171 		return GRALLOC1_ERROR_BAD_VALUE;
172 	}
173 
174 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
175 	*size = hnd->size;
176 
177 	return GRALLOC1_ERROR_NONE;
178 }
179 
mali_gralloc_private_get_buff_flags(gralloc1_device_t * device,buffer_handle_t handle,int * flags)180 static int32_t mali_gralloc_private_get_buff_flags(gralloc1_device_t *device, buffer_handle_t handle, int *flags)
181 {
182 	GRALLOC_UNUSED(device);
183 
184 	if (private_handle_t::validate(handle) < 0)
185 	{
186 		return GRALLOC1_ERROR_BAD_HANDLE;
187 	}
188 
189 	if (flags == NULL)
190 	{
191 		return GRALLOC1_ERROR_BAD_VALUE;
192 	}
193 
194 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
195 	*flags = hnd->flags;
196 
197 	return GRALLOC1_ERROR_NONE;
198 }
199 
mali_gralloc_private_get_buff_min_page_size(gralloc1_device_t * device,buffer_handle_t handle,int * min_pgsz)200 static int32_t mali_gralloc_private_get_buff_min_page_size(gralloc1_device_t *device, buffer_handle_t handle,
201                                                            int *min_pgsz)
202 {
203 	GRALLOC_UNUSED(device);
204 
205 	if (private_handle_t::validate(handle) < 0)
206 	{
207 		return GRALLOC1_ERROR_BAD_HANDLE;
208 	}
209 
210 	if (min_pgsz == NULL)
211 	{
212 		return GRALLOC1_ERROR_BAD_VALUE;
213 	}
214 
215 	const private_handle_t *hnd = static_cast<const private_handle_t *>(handle);
216 	*min_pgsz = hnd->min_pgsz;
217 
218 	return GRALLOC1_ERROR_NONE;
219 }
220 
mali_gralloc_private_get_attr_param(gralloc1_device_t * device,buffer_handle_t handle,buf_attr attr,int32_t * val,int32_t last_call)221 static int32_t mali_gralloc_private_get_attr_param(gralloc1_device_t *device, buffer_handle_t handle, buf_attr attr,
222                                                    int32_t *val, int32_t last_call)
223 {
224 	GRALLOC_UNUSED(device);
225 
226 	if (private_handle_t::validate(handle) < 0 || val == NULL)
227 	{
228 		return GRALLOC1_ERROR_BAD_HANDLE;
229 	}
230 
231 	const private_handle_t *const_hnd = static_cast<const private_handle_t *>(handle);
232 	private_handle_t *hnd = const_cast<private_handle_t *>(const_hnd);
233 
234 	if (hnd->attr_base == MAP_FAILED)
235 	{
236 		if (gralloc_buffer_attr_map(hnd, 1) < 0)
237 		{
238 			return GRALLOC1_ERROR_BAD_HANDLE;
239 		}
240 	}
241 
242 	if (gralloc_buffer_attr_read(hnd, attr, val) < 0)
243 	{
244 		gralloc_buffer_attr_unmap(hnd);
245 		return GRALLOC1_ERROR_BAD_HANDLE;
246 	}
247 
248 	if (last_call)
249 	{
250 		gralloc_buffer_attr_unmap(hnd);
251 	}
252 
253 	return GRALLOC1_ERROR_NONE;
254 }
255 
mali_gralloc_private_set_attr_param(gralloc1_device_t * device,buffer_handle_t handle,buf_attr attr,int32_t * val,int32_t last_call)256 static int32_t mali_gralloc_private_set_attr_param(gralloc1_device_t *device, buffer_handle_t handle, buf_attr attr,
257                                                    int32_t *val, int32_t last_call)
258 {
259 	GRALLOC_UNUSED(device);
260 
261 	if (private_handle_t::validate(handle) < 0 || val == NULL)
262 	{
263 		return GRALLOC1_ERROR_BAD_HANDLE;
264 	}
265 
266 	const private_handle_t *const_hnd = static_cast<const private_handle_t *>(handle);
267 	private_handle_t *hnd = const_cast<private_handle_t *>(const_hnd);
268 
269 	if (hnd->attr_base == MAP_FAILED)
270 	{
271 		if (gralloc_buffer_attr_map(hnd, 1) < 0)
272 		{
273 			return GRALLOC1_ERROR_BAD_HANDLE;
274 		}
275 	}
276 
277 	if (gralloc_buffer_attr_write(hnd, attr, val) < 0)
278 	{
279 		gralloc_buffer_attr_unmap(hnd);
280 		return GRALLOC1_ERROR_BAD_HANDLE;
281 	}
282 
283 	if (last_call)
284 	{
285 		gralloc_buffer_attr_unmap(hnd);
286 	}
287 
288 	return GRALLOC1_ERROR_NONE;
289 }
290 
mali_gralloc_private_set_priv_fmt(gralloc1_device_t * device,gralloc1_buffer_descriptor_t desc,uint64_t internal_format)291 static int32_t mali_gralloc_private_set_priv_fmt(gralloc1_device_t *device, gralloc1_buffer_descriptor_t desc,
292                                                  uint64_t internal_format)
293 {
294 	GRALLOC_UNUSED(device);
295 
296 	buffer_descriptor_t *priv_desc = reinterpret_cast<buffer_descriptor_t *>(desc);
297 
298 	if (priv_desc == NULL)
299 	{
300 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
301 	}
302 
303 	priv_desc->hal_format = internal_format;
304 	priv_desc->format_type = MALI_GRALLOC_FORMAT_TYPE_INTERNAL;
305 
306 	return GRALLOC1_ERROR_NONE;
307 }
308 
mali_gralloc_private_interface_getFunction(int32_t descriptor)309 gralloc1_function_pointer_t mali_gralloc_private_interface_getFunction(int32_t descriptor)
310 {
311 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_INT_FMT, mali_gralloc_private_get_buff_int_fmt);
312 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_FD, mali_gralloc_private_get_buff_fd);
313 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_INTERNAL_DIMS, mali_gralloc_private_get_buff_int_dims);
314 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_OFFSET, mali_gralloc_private_get_buff_offset);
315 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_BYTESTRIDE, mali_gralloc_private_get_buff_bytestride);
316 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_YUVINFO, mali_gralloc_private_get_buff_yuvinfo);
317 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_SIZE, mali_gralloc_private_get_buff_size);
318 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_FLAGS, mali_gralloc_private_get_buff_flags);
319 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_BUFF_MIN_PAGESIZE,
320 	               mali_gralloc_private_get_buff_min_page_size);
321 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_GET_ATTR_PARAM, mali_gralloc_private_get_attr_param);
322 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_SET_ATTR_PARAM, mali_gralloc_private_set_attr_param);
323 	CHECK_FUNCTION(descriptor, MALI_GRALLOC1_FUNCTION_SET_PRIV_FMT, mali_gralloc_private_set_priv_fmt);
324 
325 	return NULL;
326 }
327