1 /*
2  * Copyright (C) 2016-2018 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 <stdlib.h>
20 #include <inttypes.h>
21 
22 #if GRALLOC_VERSION_MAJOR == 1
23 #include <hardware/gralloc1.h>
24 #elif GRALLOC_VERSION_MAJOR == 0
25 #include <hardware/gralloc.h>
26 #endif
27 
28 #include "mali_gralloc_module.h"
29 #include "mali_gralloc_bufferdescriptor.h"
30 #include "mali_gralloc_private_interface_types.h"
31 #include "mali_gralloc_buffer.h"
32 
33 /*
34  * Validate descriptor to ensure that it originated from this version
35  * of gralloc. Rudimentary signature is simply calculated from size of
36  * buffer descriptor structure and initialised immediately after
37  * structure is allocated.
38  *
39  * @param buffer_descriptor    [in]    Buffer descriptor.
40  *
41  * @return true, for valid buffer descriptor;
42  *         false, otherwise
43  */
descriptor_is_valid(buffer_descriptor_t * buffer_descriptor)44 static bool descriptor_is_valid(buffer_descriptor_t *buffer_descriptor)
45 {
46 	if (buffer_descriptor && buffer_descriptor->signature == sizeof(*buffer_descriptor))
47 	{
48 		return true;
49 	}
50 
51 	return false;
52 }
53 
54 #if GRALLOC_VERSION_MAJOR == 1
mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t * outDescriptor)55 int mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t *outDescriptor)
56 {
57 	buffer_descriptor_t *buffer_descriptor;
58 
59 	if (NULL == outDescriptor)
60 	{
61 		return GRALLOC1_ERROR_BAD_VALUE;
62 	}
63 
64 	buffer_descriptor = reinterpret_cast<buffer_descriptor_t *>(malloc(sizeof(buffer_descriptor_t)));
65 
66 	if (NULL == buffer_descriptor)
67 	{
68 		AERR("failed to create buffer descriptor");
69 		return GRALLOC1_ERROR_NO_RESOURCES;
70 	}
71 
72 	/*
73 	 * Initialise the buffer descriptor.
74 	 *
75 	 * Layer count is initialised to a single layer in
76 	 * case clients don't support multi-layer or use
77 	 * function GRALLOC1_PFN_SET_LAYER_COUNT.
78 	 */
79 	memset((void *)buffer_descriptor, 0, sizeof(*buffer_descriptor));
80 	buffer_descriptor->fd_count = 1;
81 	buffer_descriptor->layer_count = 1;
82 	buffer_descriptor->signature = sizeof(buffer_descriptor_t);
83 
84 	*outDescriptor = (gralloc1_buffer_descriptor_t)buffer_descriptor;
85 	return GRALLOC1_ERROR_NONE;
86 }
87 
mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)88 int mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)
89 {
90 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
91 	if (!descriptor_is_valid(buffer_descriptor))
92 	{
93 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
94 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
95 	}
96 
97 	free(buffer_descriptor);
98 	return GRALLOC1_ERROR_NONE;
99 }
100 
mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)101 int mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor, uint32_t width, uint32_t height)
102 {
103 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
104 	if (!descriptor_is_valid(buffer_descriptor))
105 	{
106 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
107 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
108 	}
109 
110 	buffer_descriptor->width = width;
111 	buffer_descriptor->height = height;
112 	return GRALLOC1_ERROR_NONE;
113 }
114 
mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor,int32_t format)115 int mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor, int32_t format)
116 {
117 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
118 	if (!descriptor_is_valid(buffer_descriptor))
119 	{
120 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
121 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
122 	}
123 
124 	/*
125 	 * Explicit cast to unsigned 64-bit 'hal_format',
126 	 * which can also store internal_format provided
127 	 * through mali_gralloc_private_set_priv_fmt().
128 	 */
129 	buffer_descriptor->hal_format = (uint64_t)format;
130 	buffer_descriptor->format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
131 	return GRALLOC1_ERROR_NONE;
132 }
133 
mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)134 int mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
135 {
136 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
137 	if (!descriptor_is_valid(buffer_descriptor))
138 	{
139 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
140 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
141 	}
142 
143 	buffer_descriptor->producer_usage = usage;
144 	return GRALLOC1_ERROR_NONE;
145 }
146 
mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)147 int mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
148 {
149 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
150 	if (!descriptor_is_valid(buffer_descriptor))
151 	{
152 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
153 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
154 	}
155 
156 	buffer_descriptor->consumer_usage = usage;
157 	return GRALLOC1_ERROR_NONE;
158 }
159 
mali_gralloc_get_backing_store_internal(buffer_handle_t buffer,gralloc1_backing_store_t * outStore)160 int mali_gralloc_get_backing_store_internal(buffer_handle_t buffer, gralloc1_backing_store_t *outStore)
161 {
162 	if (private_handle_t::validate(buffer) < 0)
163 	{
164 		AERR("Invalid buffer %p, returning error", buffer);
165 		return GRALLOC1_ERROR_BAD_HANDLE;
166 	}
167 
168 	if (outStore == NULL)
169 	{
170 		return GRALLOC1_ERROR_BAD_VALUE;
171 	}
172 
173 	private_handle_t *hnd = (private_handle_t *)buffer;
174 
175 	*outStore = (gralloc1_backing_store_t)hnd->backing_store_id;
176 	return GRALLOC1_ERROR_NONE;
177 }
178 
mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)179 int mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
180 {
181 	if (private_handle_t::validate(buffer) < 0)
182 	{
183 		AERR("Invalid buffer %p, returning error", buffer);
184 		return GRALLOC1_ERROR_BAD_HANDLE;
185 	}
186 
187 	if (outUsage == NULL)
188 	{
189 		return GRALLOC1_ERROR_BAD_VALUE;
190 	}
191 
192 	private_handle_t *hnd = (private_handle_t *)buffer;
193 	*outUsage = hnd->consumer_usage;
194 	return GRALLOC1_ERROR_NONE;
195 }
196 
mali_gralloc_get_dimensions_internal(buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)197 int mali_gralloc_get_dimensions_internal(buffer_handle_t buffer, uint32_t *outWidth, uint32_t *outHeight)
198 {
199 	if (private_handle_t::validate(buffer) < 0)
200 	{
201 		AERR("Invalid buffer %p, returning error", buffer);
202 		return GRALLOC1_ERROR_BAD_HANDLE;
203 	}
204 
205 	if (outWidth == NULL || outHeight == NULL)
206 	{
207 		return GRALLOC1_ERROR_BAD_VALUE;
208 	}
209 
210 	private_handle_t *hnd = (private_handle_t *)buffer;
211 	*outWidth = (uint32_t)hnd->width;
212 	*outHeight = (uint32_t)hnd->height;
213 	return GRALLOC1_ERROR_NONE;
214 }
215 
mali_gralloc_get_format_internal(buffer_handle_t buffer,int32_t * outFormat)216 int mali_gralloc_get_format_internal(buffer_handle_t buffer, int32_t *outFormat)
217 {
218 	if (private_handle_t::validate(buffer) < 0)
219 	{
220 		AERR("Invalid buffer %p, returning error", buffer);
221 		return GRALLOC1_ERROR_BAD_HANDLE;
222 	}
223 
224 	if (outFormat == NULL)
225 	{
226 		return GRALLOC1_ERROR_BAD_VALUE;
227 	}
228 
229 	private_handle_t *hnd = (private_handle_t *)buffer;
230 	*outFormat = hnd->req_format;
231 	return GRALLOC1_ERROR_NONE;
232 }
233 
mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)234 int mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
235 {
236 	if (private_handle_t::validate(buffer) < 0)
237 	{
238 		AERR("Invalid buffer %p, returning error", buffer);
239 		return GRALLOC1_ERROR_BAD_HANDLE;
240 	}
241 
242 	if (outUsage == NULL)
243 	{
244 		return GRALLOC1_ERROR_BAD_VALUE;
245 	}
246 
247 	private_handle_t *hnd = (private_handle_t *)buffer;
248 	*outUsage = hnd->producer_usage;
249 	return GRALLOC1_ERROR_NONE;
250 }
251 
252 #if PLATFORM_SDK_VERSION >= 26
mali_gralloc_set_layer_count_internal(gralloc1_buffer_descriptor_t descriptor,uint32_t layerCount)253 int mali_gralloc_set_layer_count_internal(gralloc1_buffer_descriptor_t descriptor, uint32_t layerCount)
254 {
255 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
256 	if (!descriptor_is_valid(buffer_descriptor))
257 	{
258 		AERR("Invalid buffer descriptor %p", buffer_descriptor);
259 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
260 	}
261 
262 	if (layerCount == 0)
263 	{
264 		AERR("Invalid layer count: %" PRIu32, layerCount);
265 		return GRALLOC1_ERROR_BAD_VALUE;
266 	}
267 
268 	buffer_descriptor->layer_count = layerCount;
269 	return GRALLOC1_ERROR_NONE;
270 }
271 
mali_gralloc_get_layer_count_internal(buffer_handle_t buffer,uint32_t * outLayerCount)272 int mali_gralloc_get_layer_count_internal(buffer_handle_t buffer, uint32_t *outLayerCount)
273 {
274 	if (private_handle_t::validate(buffer) < 0)
275 	{
276 		AERR("Invalid buffer %p, returning error", buffer);
277 		return GRALLOC1_ERROR_BAD_HANDLE;
278 	}
279 
280 	if (outLayerCount == NULL)
281 	{
282 		return GRALLOC1_ERROR_BAD_VALUE;
283 	}
284 
285 	private_handle_t *hnd = (private_handle_t *)buffer;
286 	*outLayerCount = hnd->layer_count;
287 	return GRALLOC1_ERROR_NONE;
288 }
289 #endif /* PLATFORM_SDK_VERSION >= 26 */
290 
291 #endif
mali_gralloc_query_getstride(buffer_handle_t buffer,int * pixelStride)292 int mali_gralloc_query_getstride(buffer_handle_t buffer, int *pixelStride)
293 {
294 	if (private_handle_t::validate(buffer) < 0)
295 	{
296 		AERR("Invalid buffer %p, returning error", buffer);
297 #if GRALLOC_VERSION_MAJOR == 1
298 		return GRALLOC1_ERROR_BAD_HANDLE;
299 #else
300 		return -EINVAL;
301 #endif
302 	}
303 
304 	if (pixelStride == NULL)
305 	{
306 #if GRALLOC_VERSION_MAJOR == 1
307 		return GRALLOC1_ERROR_BAD_VALUE;
308 #else
309 		return -EINVAL;
310 #endif
311 	}
312 
313 	private_handle_t *hnd = (private_handle_t *)buffer;
314 	*pixelStride = hnd->stride;
315 
316 #if GRALLOC_VERSION_MAJOR == 1
317 	return GRALLOC1_ERROR_NONE;
318 #else
319 	return 0;
320 #endif
321 }
322