1 /*
2  * Copyright (C) 2016-2019 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 #include <errno.h>
19 #include <inttypes.h>
20 #include <inttypes.h>
21 #include <sync/sync.h>
22 
23 #if GRALLOC_VERSION_MAJOR == 0
24 #include <hardware/gralloc.h>
25 #elif GRALLOC_VERSION_MAJOR == 1
26 #include <hardware/gralloc1.h>
27 #elif GRALLOC_VERSION_MAJOR == 2
28 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
29 #include <android/hardware/graphics/common/1.0/types.h>
30 using android::hardware::graphics::mapper::V2_0::Error;
31 #endif
32 
33 #include "mali_gralloc_module.h"
34 #include "mali_gralloc_private_interface_types.h"
35 #include "mali_gralloc_buffer.h"
36 #include "mali_gralloc_formats.h"
37 #include "mali_gralloc_usages.h"
38 #include "mali_gralloc_ion.h"
39 #include "gralloc_helper.h"
40 #include "format_info.h"
41 
42 
43 enum tx_direction
44 {
45 	TX_NONE = 0,
46 	TX_TO_DEVICE,
47 	TX_FROM_DEVICE,
48 	TX_BOTH,
49 };
50 
51 
get_tx_direction(const uint64_t usage)52 static enum tx_direction get_tx_direction(const uint64_t usage)
53 {
54 	const bool read = (usage & GRALLOC_USAGE_SW_READ_MASK) ? true : false;
55 	const bool write = (usage & GRALLOC_USAGE_SW_WRITE_MASK) ? true : false;
56 	enum tx_direction dir = TX_NONE;
57 
58 	if (read && write)
59 	{
60 		dir = TX_BOTH;
61 	}
62 	else if (write)
63 	{
64 		dir = TX_TO_DEVICE;
65 	}
66 	else if (read)
67 	{
68 		dir = TX_FROM_DEVICE;
69 	}
70 
71 	return dir;
72 }
73 
buffer_sync(private_handle_t * const hnd,const enum tx_direction direction)74 static void buffer_sync(private_handle_t * const hnd,
75                         const enum tx_direction direction)
76 {
77 	const uint64_t usage = hnd->producer_usage | hnd->consumer_usage;
78 
79 	if ((usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) == 0)
80 	{
81 		return;
82 	}
83 
84 	if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
85 	{
86 		if (direction != TX_NONE)
87 		{
88 			hnd->cpu_read = (direction == TX_FROM_DEVICE || direction == TX_BOTH) ? 1 : 0;
89 			hnd->cpu_write = (direction == TX_TO_DEVICE || direction == TX_BOTH) ? 1 : 0;
90 
91 			const int status = mali_gralloc_ion_sync_start(hnd,
92 			                                               hnd->cpu_read ? true : false,
93 			                                               hnd->cpu_write ? true : false);
94 			if (status < 0)
95 			{
96 				return;
97 			}
98 		}
99 		else if (hnd->cpu_read || hnd->cpu_write)
100 		{
101 			const int status = mali_gralloc_ion_sync_end(hnd,
102 			                                             hnd->cpu_read ? true : false,
103 			                                             hnd->cpu_write ? true : false);
104 			if (status < 0)
105 			{
106 				return;
107 			}
108 
109 			hnd->cpu_read = 0;
110 			hnd->cpu_write = 0;
111 		}
112 	}
113 }
114 
115 
ion_map_for_lock(private_handle_t * hnd)116 static int ion_map_for_lock(private_handle_t *hnd)
117 {
118 	int ret = 0;
119 
120 	if (!hnd->bases[0] && !hnd->bases[1] && !hnd->bases[2])
121 	{
122 		ret = mali_gralloc_ion_map(hnd);
123 
124 		switch (ret)
125 		{
126 			case 1:
127 				AWAR("AFBC buffer must not be mapped. handle(%p)", hnd);
128 				break;
129 			case 2:
130 				if (!(hnd->flags & (private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA | private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA)))
131 				{
132 					AWAR("Secure buffer with no video private data cannot be mapped. handle(%p)", hnd);
133 				}
134 				break;
135 			case 3:
136 				AWAR("HFR buffer cannot be mapped. handle(%p)", hnd);
137 				break;
138 		}
139 	}
140 
141 	return ret;
142 }
143 
144 
145 /*
146  *  Validates input parameters of lock request.
147  *
148  * @param buffer   [in]    The buffer to lock.
149  * @param l        [in]    Access region left offset (in pixels).
150  * @param t        [in]    Access region top offset (in pixels).
151  * @param w        [in]    Access region requested width (in pixels).
152  * @param h        [in]    Access region requested height (in pixels).
153  * @param usage    [in]    Lock request (producer and consumer combined) usage.
154  *
155  * @return 0,for valid input parameters;
156  *         -EINVAL, for erroneous input parameters
157  */
validate_lock_input_parameters(const buffer_handle_t buffer,const int l,const int t,const int w,const int h,uint64_t usage)158 int validate_lock_input_parameters(const buffer_handle_t buffer, const int l,
159                                    const int t, const int w, const int h,
160                                    uint64_t usage)
161 {
162 	const int lock_pid = getpid();
163 	const private_handle_t * const hnd = (private_handle_t *)buffer;
164 
165 	if ((l < 0) || (t < 0) || (w < 0) || (h < 0))
166 	{
167 		AERR("Negative values for access region (l = %d t = %d w = %d and "
168 		     "h = %d) in buffer lock request are invalid. Locking PID:%d",
169 		      l, t, w, h, lock_pid);
170 		return -EINVAL;
171 	}
172 
173 	/* Test overflow conditions on access region parameters */
174 	if (((l + w) < 0) || ((t + h) < 0))
175 	{
176 		AERR("Encountered overflow with access region (l = %d t = %d w = %d and"
177 		     " h = %d) in buffer lock request. Locking PID:%d",
178 		       l, t, w, h, lock_pid);
179 		return -EINVAL;
180 	}
181 
182 	/* Region of interest shall be inside the allocated buffer */
183 	if (((t + h) > hnd->height)  || ((l + w) > hnd->width))
184 	{
185 		if ((t + h) * (l + w) > hnd->height * hnd->width)
186 		{
187 			AERR("Buffer lock access region (l = %d t = %d w = %d "
188 			     "and h = %d) is larger than allocated buffer (width = %d and height = %d)"
189 			     " Locking PID:%d", l, t, w, h, hnd->width, hnd->height, lock_pid);
190 			return -EINVAL;
191 		}
192 		else
193 		{
194 			AWAR("Buffer lock access region (l = %d t = %d w = %d "
195 			     "and h = %d) is outside allocated buffer (width = %d and height = %d)"
196 			     " Locking PID:%d", l, t, w, h, hnd->width, hnd->height, lock_pid);
197 		}
198 	}
199 
200 #if GRALLOC_IMPORT_BUFFER_REQUIRED == 1
201 	bool is_registered_process = false;
202 
203 	/* Locking process should have a valid buffer virtual address. A process
204 	 * will have a valid buffer virtual address if it is the allocating
205 	 * process or it retained / registered a cloned buffer handle
206 	 */
207 	if ((hnd->allocating_pid == lock_pid) || (hnd->remote_pid == lock_pid))
208 	{
209 		is_registered_process = true;
210 	}
211 
212 	if ((is_registered_process == false) || (hnd->base == NULL))
213 	{
214 #if GRALLOC_VERSION_MAJOR == 0
215 		AERR("The buffer must be registered before lock request");
216 #else
217 		AERR("The buffer must be retained before lock request");
218 #endif
219 		return -EINVAL;
220 	}
221 #endif
222 
223 	/* Reject lock requests for AFBC (compressed format) enabled buffers */
224 	if ((hnd->alloc_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK) != 0)
225 	{
226 		AERR("Lock is not supported for AFBC enabled buffers. wxh(%d %d) "
227 		     "Internal Format:0x%" PRIx64, hnd->width, hnd->height, hnd->alloc_format);
228 
229 #if GRALLOC_VERSION_MAJOR == 0
230 		return -EINVAL;
231 #elif GRALLOC_VERSION_MAJOR == 1
232 		return GRALLOC1_ERROR_UNSUPPORTED;
233 #elif GRALLOC_VERSION_MAJOR == 2
234 		return static_cast<int32_t>(Error::UNSUPPORTED);
235 #endif
236 	}
237 
238 #if GRALLOC_VERSION_MAJOR == 0
239 	/* Check if the buffer was created with a usage mask incompatible with the
240 	 * requested usage flags. For compatibility, requested lock usage can be a
241 	 * subset of allocation usage
242 	 */
243 	if ((usage & (hnd->producer_usage | hnd->consumer_usage)) == 0)
244 	{
245 		AERR("Buffer lock usage:0x%" PRIx64" does not match allocation usage:0x%"
246 		      PRIx64, usage, (hnd->producer_usage | hnd->consumer_usage));
247 		return -EINVAL;
248 	}
249 #else
250 	/* Producer and consumer usage is verified in gralloc1 specific code. */
251 	GRALLOC_UNUSED(usage);
252 #endif
253 
254 	return 0;
255 }
256 
257 
258 /*
259  *  Locks the given buffer for the specified CPU usage.
260  *
261  * @param m        [in]    Gralloc module.
262  * @param buffer   [in]    The buffer to lock.
263  * @param usage    [in]    Producer and consumer combined usage.
264  * @param l        [in]    Access region left offset (in pixels).
265  * @param t        [in]    Access region top offset (in pixels).
266  * @param w        [in]    Access region requested width (in pixels).
267  * @param h        [in]    Access region requested height (in pixels).
268  * @param vaddr    [out]   To be filled with a CPU-accessible pointer to
269  *                         the buffer data for CPU usage.
270  *
271  * @return 0, when the locking is successful;
272  *         Appropriate error, otherwise
273  *
274  * @Note:  There is no way to ascertain whether buffer data is valid or not (for
275  *         example, establishing if the h/w needs to finish rendering or if CPU
276  *         caches need to be synchronized).
277  *
278  * @Note:  Locking a buffer simultaneously for write or read/write leaves the
279  *         buffer's content in an indeterminate state.
280  */
mali_gralloc_lock(const mali_gralloc_module * const m,buffer_handle_t buffer,uint64_t usage,int l,int t,int w,int h,void ** vaddr)281 int mali_gralloc_lock(const mali_gralloc_module * const m, buffer_handle_t buffer,
282                       uint64_t usage, int l, int t, int w, int h, void **vaddr)
283 {
284 	int status;
285 	GRALLOC_UNUSED(m);
286 
287 	if (private_handle_t::validate(buffer) < 0)
288 	{
289 		AERR("Locking invalid buffer %p, returning error", buffer);
290 		return -EINVAL;
291 	}
292 
293 	/* Validate input parameters for lock request */
294 	status = validate_lock_input_parameters(buffer, l, t, w, h, usage);
295 	if (status != 0)
296 	{
297 		return status;
298 	}
299 
300 	private_handle_t *hnd = (private_handle_t *)buffer;
301 
302 	/* HAL_PIXEL_FORMAT_YCbCr_*_888 buffers 'must' be locked with lock_ycbcr() */
303 	if ((hnd->req_format == HAL_PIXEL_FORMAT_YCbCr_420_888) ||
304 	    (hnd->req_format == HAL_PIXEL_FORMAT_YCbCr_422_888) ||
305 	    (hnd->req_format == HAL_PIXEL_FORMAT_YCbCr_444_888))
306 	{
307 		AERR("Buffers with format YCbCr_*_888 must be locked using "
308 		     "(*lock_ycbcr). Requested format is:0x%x", hnd->req_format);
309 		return -EINVAL;
310 	}
311 
312 	/* YUV compatible formats 'should' be locked with lock_ycbcr() */
313 	const int32_t format_idx = get_format_index(hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
314 	if (format_idx == -1)
315 	{
316 		AERR("Corrupted buffer format 0x%" PRIx64 " of buffer %p", hnd->alloc_format, hnd);
317 		return -EINVAL;
318 	}
319 
320 	if (formats[format_idx].is_yuv == true)
321 	{
322 		switch (formats[format_idx].id)
323 		{
324 			case HAL_PIXEL_FORMAT_YCbCr_422_I:
325 			case HAL_PIXEL_FORMAT_Y8:
326 			case HAL_PIXEL_FORMAT_Y16:
327 			case HAL_PIXEL_FORMAT_YV12:
328 				break;
329 			default:
330 #if GRALLOC_VERSION_MAJOR == 0
331 				AWAR("Buffers with YUV compatible formats should be locked using "
332 				     "(*lock_ycbcr). Requested format is:0x%x", hnd->req_format);
333 #elif GRALLOC_VERSION_MAJOR == 1
334 				AWAR("Buffers with YUV compatible formats should be locked using "
335 				     "GRALLOC1_FUNCTION_LOCK_FLEX. Requested format is:0x%x", hnd->req_format);
336 #endif
337 				break;
338 		}
339 	}
340 
341 	/* Populate CPU-accessible pointer when requested for CPU usage */
342 	if ((usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) != 0)
343 	{
344 		if (vaddr == NULL)
345 		{
346 			return -EINVAL;
347 		}
348 
349 		if (ion_map_for_lock(hnd) < 0)
350 		{
351 			return -EINVAL;
352 		}
353 
354 		*vaddr = (void *)hnd->base;
355 
356 		buffer_sync(hnd, get_tx_direction(usage));
357 	}
358 
359 	return 0;
360 }
361 
362 /*
363  *  Locks the given ycbcr buffer for the specified CPU usage. This function can
364  *  only be used for buffers with "8 bit sample depth"
365  *
366  * @param m        [in]    Gralloc module.
367  * @param buffer   [in]    The buffer to lock.
368  * @param usage    [in]    Producer and consumer combined usage.
369  * @param l        [in]    Access region left offset (in pixels).
370  * @param t        [in]    Access region top offset (in pixels).
371  * @param w        [in]    Access region requested width (in pixels).
372  * @param h        [in]    Access region requested height (in pixels).
373  * @param ycbcr    [out]   Describes YCbCr formats for consumption by applications.
374  *
375  * @return 0, when the locking is successful;
376  *         Appropriate error, otherwise
377  *
378  * @Note:  There is no way to ascertain whether buffer data is valid or not (for
379  *         example, establishing if the h/w needs to finish rendering or if CPU
380  *         caches need to be synchronized).
381  *
382  * @Note:  Locking a buffer simultaneously for write or read/write leaves the
383  *         buffer's content in an indeterminate state.
384  *
385  */
mali_gralloc_lock_ycbcr(const mali_gralloc_module * m,const buffer_handle_t buffer,const uint64_t usage,const int l,const int t,const int w,const int h,android_ycbcr * ycbcr)386 int mali_gralloc_lock_ycbcr(const mali_gralloc_module *m,
387                             const buffer_handle_t buffer,
388                             const uint64_t usage, const int l, const int t,
389                             const int w, const int h, android_ycbcr *ycbcr)
390 {
391 	GRALLOC_UNUSED(m);
392 	int status;
393 
394 	if (private_handle_t::validate(buffer) < 0)
395 	{
396 		AERR("Locking invalid buffer %p, returning error", buffer);
397 		return -EINVAL;
398 	}
399 
400 	private_handle_t * const hnd = (private_handle_t *)buffer;
401 	const uint32_t base_format = hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK;
402 
403 	/* Validate input parameters for lock request */
404 	status = validate_lock_input_parameters(buffer, l, t, w, h, usage);
405 	if (status != 0)
406 	{
407 		return status;
408 	}
409 
410 	const int32_t format_idx = get_format_index(base_format);
411 	if (format_idx == -1)
412 	{
413 		AERR("Corrupted buffer format 0x%" PRIx64 " of buffer %p", hnd->alloc_format, hnd);
414 		return -EINVAL;
415 	}
416 
417 	if (formats[format_idx].is_yuv != true)
418 	{
419 		AERR("Buffer format:0x%" PRIx64 " is not a YUV compatible format", hnd->alloc_format);
420 		return -EINVAL;
421 	}
422 
423 	if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
424 	{
425 		if (NULL == ycbcr)
426 		{
427 			return -EINVAL;
428 		}
429 
430 		if (ion_map_for_lock(hnd) < 0)
431 		{
432 			return -EINVAL;
433 		}
434 
435 		ycbcr->y = (char *)hnd->base;
436 		ycbcr->ystride = hnd->plane_info[0].byte_stride;
437 
438 		switch (base_format)
439 		{
440 		case MALI_GRALLOC_FORMAT_INTERNAL_Y8:
441 		case MALI_GRALLOC_FORMAT_INTERNAL_Y16:
442 			/* No UV plane */
443 			ycbcr->cstride = 0;
444 			ycbcr->cb = NULL;
445 			ycbcr->cr = NULL;
446 			ycbcr->chroma_step = 0;
447 		break;
448 
449 		case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
450 			/* UV plane */
451 			ycbcr->cstride = hnd->plane_info[1].byte_stride;
452 			ycbcr->cb = (char *)hnd->base + hnd->plane_info[1].offset;
453 			ycbcr->cr = (char *)ycbcr->cb + 1;
454 			ycbcr->chroma_step = 2;
455 			break;
456 
457 		case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
458 			/* VU plane */
459 			ycbcr->cstride = hnd->plane_info[1].byte_stride;
460 			ycbcr->cr = (char *)hnd->base + hnd->plane_info[1].offset;
461 			ycbcr->cb = (char *)ycbcr->cr + 1;
462 			ycbcr->chroma_step = 2;
463 			break;
464 
465 		case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
466 			/* V plane, U plane */
467 			ycbcr->cstride = hnd->plane_info[1].byte_stride;
468 			ycbcr->cr = (char *)hnd->base + hnd->plane_info[1].offset;
469 			ycbcr->cb = (char *)hnd->base + hnd->plane_info[2].offset;
470 			ycbcr->chroma_step = 1;
471 			break;
472 
473 		default:
474 			AERR("Buffer:%p of format 0x%" PRIx64 "can't be represented in"
475 			     " android_ycbcr format", hnd, hnd->alloc_format);
476 			return -EINVAL;
477 		}
478 
479 		buffer_sync(hnd, get_tx_direction(usage));
480 	}
481 	else
482 	{
483 		ycbcr->y = NULL;
484 		ycbcr->cb = NULL;
485 		ycbcr->cr = NULL;
486 		ycbcr->ystride = 0;
487 		ycbcr->cstride = 0;
488 		ycbcr->chroma_step = 0;
489 	}
490 
491 	/* Reserved parameters should be set to 0 by gralloc's (*lock_ycbcr)()*/
492 	memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
493 
494 	return 0;
495 }
496 
497 /*
498  *  Unlocks the given buffer.
499  *
500  * @param m           [in]   Gralloc module.
501  * @param buffer      [in]   The buffer to unlock.
502  *
503  * @return 0, when the locking is successful;
504  *         Appropriate error, otherwise
505  *
506  * Note: unlocking a buffer which is not locked results in an unexpected behaviour.
507  *       Though it is possible to create a state machine to track the buffer state to
508  *       recognize erroneous conditions, it is expected of client to adhere to API
509  *       call sequence
510  */
mali_gralloc_unlock(const mali_gralloc_module * m,buffer_handle_t buffer)511 int mali_gralloc_unlock(const mali_gralloc_module *m, buffer_handle_t buffer)
512 {
513 	GRALLOC_UNUSED(m);
514 
515 	if (private_handle_t::validate(buffer) < 0)
516 	{
517 		AERR("Unlocking invalid buffer %p, returning error", buffer);
518 		return -EINVAL;
519 	}
520 
521 	private_handle_t *hnd = (private_handle_t *)buffer;
522 	buffer_sync(hnd, TX_NONE);
523 
524 	return 0;
525 }
526 
527 #if GRALLOC_VERSION_MAJOR == 1
528 /*
529  *  Returns the number of flex layout planes which are needed to represent the
530  *  given buffer.
531  *
532  * @param m           [in]   Gralloc module.
533  * @param buffer      [in]   The buffer handle for which the number of planes should be queried
534  * @param num_planes  [out]  The number of flex planes required to describe the given buffer
535  *
536  * @return GRALLOC1_ERROR_NONE         The buffer's format can be represented in flex layout
537  *         GRALLOC1_ERROR_UNSUPPORTED - The buffer's format can't be represented in flex layout
538  */
mali_gralloc_get_num_flex_planes(const mali_gralloc_module * const m,const buffer_handle_t buffer,uint32_t * const num_planes)539 int mali_gralloc_get_num_flex_planes(const mali_gralloc_module *const m,
540                                      const buffer_handle_t buffer,
541                                      uint32_t * const num_planes)
542 {
543 	GRALLOC_UNUSED(m);
544 
545 	private_handle_t *hnd = (private_handle_t *)buffer;
546 	const uint32_t base_format = hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK;
547 
548 	if ((hnd->alloc_format & MALI_GRALLOC_INTFMT_EXT_MASK) != 0)
549 	{
550 		AERR("AFBC enabled buffers can't be represented in flex layout."
551 		     "Internal Format:0x%" PRIx64, hnd->alloc_format);
552 		return GRALLOC1_ERROR_UNSUPPORTED;
553 	}
554 
555 	const int32_t format_idx = get_format_index(base_format);
556 	if (format_idx == -1)
557 	{
558 		AERR("Corrupted buffer format 0x%" PRIx64 " of buffer %p", hnd->alloc_format, hnd);
559 		return -EINVAL;
560 	}
561 
562 	if (formats[format_idx].flex != true)
563 	{
564 		AERR("Format 0x%" PRIx64 " of %p can't be represented in flex", hnd->alloc_format, hnd);
565 		return GRALLOC1_ERROR_UNSUPPORTED;
566 	}
567 
568 	*num_planes = formats[format_idx].ncmp;
569 
570 	return GRALLOC1_ERROR_NONE;
571 }
572 #endif
573 
574 /*
575  *  Locks the given buffer asynchronously for the specified CPU usage.
576  *
577  * @param m        [in]    Gralloc1 module.
578  * @param buffer   [in]    The buffer to lock.
579  * @param usage    [in]    Producer and consumer combined usage.
580  * @param l        [in]    Access region left offset (in bytes).
581  * @param t        [in]    Access region top offset (in bytes).
582  * @param h        [in]    Access region requested height (in bytes).
583  * @param w        [in]    Access region requested width (in bytes).
584  * @param vaddr    [out]   To be filled with a CPU-accessible pointer to
585  *                         the buffer data for CPU usage.
586  * @param fence_fd [in]    Refers to an acquire sync fence object.
587  *
588  * @return 0, when the locking is successful;
589  *         Appropriate error, otherwise
590  */
mali_gralloc_lock_async(const mali_gralloc_module * m,buffer_handle_t buffer,uint64_t usage,int l,int t,int w,int h,void ** vaddr,int32_t fence_fd)591 int mali_gralloc_lock_async(const mali_gralloc_module *m, buffer_handle_t buffer,
592                             uint64_t usage, int l, int t, int w, int h,
593                             void **vaddr, int32_t fence_fd)
594 {
595 	if (fence_fd >= 0)
596 	{
597 		sync_wait(fence_fd, -1);
598 		close(fence_fd);
599 	}
600 
601 	return mali_gralloc_lock(m, buffer, usage, l, t, w, h, vaddr);
602 }
603 
604 /*
605  *  Locks the given ycbcr buffer for the specified CPU usage asynchronously.
606  *  This function can only be used for buffers with "8 bit sample depth"
607  *
608  * @param m        [in]    Gralloc module.
609  * @param buffer   [in]    The buffer to lock.
610  * @param usage    [in]    Producer and consumer combined usage.
611  * @param l        [in]    Access region left offset (in pixels).
612  * @param t        [in]    Access region top offset (in pixels).
613  * @param w        [in]    Access region requested width (in pixels).
614  * @param h        [in]    Access region requested height (in pixels).
615  * @param ycbcr    [out]   Describes YCbCr formats for consumption by applications.
616  * @param fence_fd [in]    Refers to an acquire sync fence object.
617  *
618  * @return 0, when the locking is successful;
619  *         Appropriate error, otherwise
620  */
mali_gralloc_lock_ycbcr_async(const mali_gralloc_module * m,const buffer_handle_t buffer,const uint64_t usage,const int l,const int t,const int w,const int h,android_ycbcr * ycbcr,const int32_t fence_fd)621 int mali_gralloc_lock_ycbcr_async(const mali_gralloc_module *m,
622                                   const buffer_handle_t buffer, const uint64_t usage,
623                                   const int l, const int t, const int w,
624                                   const int h, android_ycbcr *ycbcr,
625                                   const int32_t fence_fd)
626 {
627 	if (fence_fd >= 0)
628 	{
629 		sync_wait(fence_fd, -1);
630 		close(fence_fd);
631 	}
632 
633 	return mali_gralloc_lock_ycbcr(m, buffer, usage, l, t, w, h, ycbcr);
634 }
635 
636 #if GRALLOC_VERSION_MAJOR == 1
637 
638 /*
639  *  Sets Android flex layout parameters.
640  *
641  * @param top_left            [in]  Pointer to the first byte of the top-left
642  *                                  pixel of the plane.
643  * @param component           [in]  Plane's flex format (YUVA/RGBA).
644  * @param bits_per_component  [in]  Bits allocated for the component in each pixel.
645  * @param bits_used           [in]  Number of the most significant bits used in
646  *                                  the format for this component.
647  * @param h_increment         [in]  Horizontal increment, in bytes, in plane to
648  *                                  traverse to next horizontal pixel
649  * @param v_increment         [in]  Vertical increment, in bytes, in plane to
650  *                                  traverse to next vertical pixel
651  * @param h_subsampling       [in]  Horizontal subsampling. Must be a positive power of 2.
652  * @param v_subsampling       [in]  Vertical subsampling. Must be a positive power of 2.
653  * @param plane               [out] Flex plane layout, to be composed.
654  *
655  */
set_flex_plane_params(uint8_t * const top_left,const android_flex_component_t component,const int32_t bits_per_component,const int32_t bits_used,const int32_t h_increment,const int32_t v_increment,const int32_t h_subsampling,const int32_t v_subsampling,android_flex_plane_t * const plane)656 static void set_flex_plane_params(uint8_t * const top_left,
657                                   const android_flex_component_t component,
658                                   const int32_t bits_per_component,
659                                   const int32_t bits_used,
660                                   const int32_t h_increment,
661                                   const int32_t v_increment,
662                                   const int32_t h_subsampling,
663                                   const int32_t v_subsampling,
664                                   android_flex_plane_t * const plane)
665 {
666 	plane->top_left = top_left;
667 	plane->component = component;
668 	plane->bits_per_component = bits_per_component;
669 	plane->bits_used = bits_used;
670 	plane->h_increment = h_increment;
671 	plane->v_increment = v_increment;
672 	plane->h_subsampling = h_subsampling;
673 	plane->v_subsampling = v_subsampling;
674 
675 	return;
676 }
677 
678 
get_flexlayout_exynos_formats_only(int base_format,uint64_t usage,private_handle_t * const hnd,struct android_flex_layout * const flex_layout)679 static int get_flexlayout_exynos_formats_only (int base_format, uint64_t usage,
680 		private_handle_t * const hnd, struct android_flex_layout * const flex_layout)
681 {
682 	static const int ext_size = 256;
683 	size_t uOffset;
684 	size_t vOffset;
685 	android_ycbcr ycbcr;
686 	uint32_t r = 100;
687 
688 	switch (base_format)
689 	{
690 	case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
691 	case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
692 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
693 		uOffset = 1;
694 		ycbcr.chroma_step = 2;
695 		ycbcr.y  = (void *)((unsigned long)hnd->base);
696 		ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
697 		ycbcr.cb = (void *)(((unsigned long)hnd->bases[1]) + uOffset);
698 
699 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA
700 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
701 		{
702 			ycbcr.cb = (void *)((unsigned long)hnd->bases[2]);
703 		}
704 
705 		break;
706 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
707 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
708 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
709 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
710 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
711 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
712 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
713 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
714 		vOffset = 1;
715 		ycbcr.chroma_step = 2;
716 		ycbcr.y  = (void *)((unsigned long)hnd->base);
717 		ycbcr.cb = (void *)((unsigned long)hnd->bases[1]);
718 		ycbcr.cr = (void *)(((unsigned long)hnd->bases[1]) + vOffset);
719 
720 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA
721 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
722 		{
723 			ycbcr.cr = (void *)((unsigned long)hnd->bases[2]);
724 		}
725 		break;
726 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC:
727 
728 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
729 		uOffset = (SBWC_8B_Y_SIZE(hnd->width, hnd->height) + SBWC_8B_Y_HEADER_SIZE(hnd->width, hnd->height));
730 		vOffset = uOffset + 1;
731 		ycbcr.chroma_step = 2;
732 		ycbcr.y  = (void *)((unsigned long)hnd->base);
733 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
734 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
735 
736 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
737 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
738 		{
739 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
740 		}
741 		break;
742 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC:
743 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
744 		uOffset = (SBWC_10B_Y_SIZE(hnd->width, hnd->height) + SBWC_10B_Y_HEADER_SIZE(hnd->width, hnd->height));
745 		vOffset = uOffset + 1;
746 		ycbcr.chroma_step = 2;
747 		ycbcr.y  = (void *)((unsigned long)hnd->base);
748 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
749 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
750 
751 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
752 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
753 		{
754 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
755 		}
756 		break;
757 	case HAL_PIXEL_FORMAT_YCrCb_420_SP:
758 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
759 		vOffset = ycbcr.ystride * hnd->height;
760 		uOffset = vOffset + 1;
761 		ycbcr.chroma_step = 2;
762 		ycbcr.y  = (void *)((unsigned long)hnd->base);
763 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
764 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
765 
766 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
767 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
768 		{
769 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
770 		}
771 		break;
772 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50:
773 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75:
774 		switch (hnd->format)
775 		{
776 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50:
777 				r = 50;
778 				break;
779 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75:
780 				r = 75;
781 				break;
782 		}
783 
784 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
785 		uOffset = SBWCL_8B_Y_SIZE(hnd->width, hnd->height, r);
786 		vOffset = uOffset + 1;
787 		ycbcr.chroma_step = 2;
788 		ycbcr.y  = (void *)((unsigned long)hnd->base);
789 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
790 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
791 
792 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
793 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
794 		{
795 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
796 		}
797 		break;
798 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
799 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
800 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
801 		switch (hnd->format)
802 		{
803 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
804 				r = 40;
805 				break;
806 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
807 				r = 60;
808 				break;
809 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
810 				r = 80;
811 				break;
812 		}
813 
814 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
815 		uOffset = SBWCL_10B_Y_SIZE(hnd->width, hnd->height, r);
816 		vOffset = uOffset + 1;
817 		ycbcr.chroma_step = 2;
818 		ycbcr.y  = (void *)((unsigned long)hnd->base);
819 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
820 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
821 
822 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
823 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
824 		{
825 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
826 		}
827 		break;
828 	case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
829 	case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
830 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
831 		uOffset = 1;
832 		ycbcr.chroma_step = 2;
833 		ycbcr.y  = (void *)((unsigned long)hnd->base);
834 		ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
835 		ycbcr.cb = (void *)(((unsigned long)hnd->bases[1]) + uOffset);
836 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA
837 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
838 		{
839 			ycbcr.cb = (void *)((unsigned long)hnd->bases[2]);
840 		}
841 
842 		break;
843 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
844 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
845 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
846 		vOffset = 1;
847 		ycbcr.chroma_step = 2;
848 		ycbcr.y  = (void *)((unsigned long)hnd->base);
849 		ycbcr.cb = (void *)((unsigned long)hnd->bases[1]);
850 		ycbcr.cr = (void *)(((unsigned long)hnd->bases[1]) + vOffset);
851 
852 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA
853 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
854 		{
855 			ycbcr.cr = (void *)((unsigned long)hnd->bases[2]);
856 		}
857 
858 		break;
859 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
860 		ycbcr.ystride = hnd->plane_info[0].alloc_width;
861 		ycbcr.cstride = GRALLOC_ALIGN(ycbcr.ystride/2, 16);
862 		uOffset = ycbcr.ystride * hnd->height;
863 		vOffset = uOffset + (ycbcr.cstride * (hnd->height / 2));
864 		ycbcr.chroma_step = 1;
865 		ycbcr.y  = (void *)((unsigned long)hnd->base);
866 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
867 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
868 		break;
869 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
870 		if (usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER)
871 		{
872 			ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].byte_stride;
873 			ycbcr.chroma_step   = 1;
874 			ycbcr.y  = (void *)((unsigned long)hnd->base);
875 			ycbcr.cb = (void *)((unsigned long)hnd->bases[1]);
876 			ycbcr.cr = NULL;
877 		} else {
878 			AERR("Unexpected internal format %" PRIx64, hnd->internal_format);
879 			return -EINVAL;
880 		}
881 		break;
882 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
883 		ycbcr.ystride = hnd->plane_info[0].alloc_width;
884 		ycbcr.cstride = GRALLOC_ALIGN(ycbcr.ystride/2, 16);
885 		ycbcr.chroma_step = 1;
886 		ycbcr.y  = (void *)((unsigned long)hnd->base);
887 		ycbcr.cb = (void *)((unsigned long)hnd->bases[1]);
888 		ycbcr.cr = (void *)((unsigned long)hnd->bases[2]);
889 		break;
890 	case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
891 		ycbcr.ystride = hnd->plane_info[0].alloc_width;
892 		ycbcr.cstride = GRALLOC_ALIGN(ycbcr.ystride/2, 16);
893 		ycbcr.chroma_step = 1;
894 		ycbcr.y  = (void *)((unsigned long)hnd->base);
895 		ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
896 		ycbcr.cb = (void *)((unsigned long)hnd->bases[2]);
897 		break;
898 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
899 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
900 		uOffset = NV12N_S8B_LUMA_SIZE(ycbcr.ystride, hnd->plane_info[0].alloc_height, ext_size);
901 		vOffset = uOffset + 1;
902 		ycbcr.chroma_step = 2;
903 		ycbcr.y  = (void *)((unsigned long)hnd->base);
904 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
905 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
906 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
907 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
908 		{
909 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
910 		}
911 		break;
912 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B:
913 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
914 		uOffset = NV12N_S8B_LUMA_SIZE(ycbcr.ystride, hnd->plane_info[0].alloc_height, ext_size) + NV12N_S2B_SIZE(hnd->width, hnd->plane_info[0].alloc_height);
915 		vOffset = uOffset + 1;
916 		ycbcr.chroma_step = 2;
917 		ycbcr.y  = (void *)((unsigned long)hnd->base);
918 		ycbcr.cb = (void *)(((unsigned long)hnd->base) + uOffset);
919 		ycbcr.cr = (void *)(((unsigned long)hnd->base) + vOffset);
920 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_2PRIVATE_DATA
921 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
922 		{
923 			ycbcr.cr = (void *)((unsigned long)hnd->bases[1]);
924 		}
925 		break;
926 	case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
927 		ycbcr.ystride = ycbcr.cstride = hnd->plane_info[0].alloc_width;
928 		vOffset = 2;
929 		ycbcr.chroma_step = 2;
930 		ycbcr.y  = (void *)((unsigned long)hnd->base);
931 		ycbcr.cb = (void *)((unsigned long)hnd->bases[1]);
932 		ycbcr.cr = (void *)((unsigned long)hnd->bases[1] + vOffset);
933 		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_3PRIVATE_DATA
934 			&& usage & GRALLOC1_CONSUMER_USAGE_VIDEO_PRIVATE_DATA)
935 		{
936 			ycbcr.cr = (void *)((unsigned long)hnd->bases[2]);
937 		}
938 		break;
939 	default:
940 		AERR("not exynos format: format(0x%" PRIx32 ") usage(0x%" PRIx64 ")", base_format, usage);
941 		return -1;
942 	}
943 
944 	flex_layout->format = FLEX_FORMAT_YCbCr;
945 	flex_layout->num_planes = 3;
946 	for (uint32_t i = 0; i < flex_layout->num_planes; i++) {
947 		switch (base_format)
948 		{
949 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
950 			case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
951 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC:
952 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
953 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
954 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
955 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
956 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
957 			case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
958 				/* Mapper2.0 expoects bits_per_componect and bits_used to be 8
959 				 * or else locking fails at Mapper2.0
960 				 */
961 				flex_layout->planes[i].bits_per_component = 8;
962 				flex_layout->planes[i].bits_used = 8;
963 				break;
964 			default:
965 				flex_layout->planes[i].bits_per_component = 8;
966 				flex_layout->planes[i].bits_used = 8;
967 		}
968 
969 		flex_layout->planes[i].h_increment = 1;
970 		flex_layout->planes[i].v_increment = 1;
971 		flex_layout->planes[i].h_subsampling = 2;
972 		flex_layout->planes[i].v_subsampling = 2;
973 	}
974 
975 	flex_layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
976 	flex_layout->planes[0].component = FLEX_COMPONENT_Y;
977 	flex_layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
978 
979 	flex_layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
980 	flex_layout->planes[1].component = FLEX_COMPONENT_Cb;
981 	flex_layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
982 	flex_layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
983 
984 	flex_layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
985 	flex_layout->planes[2].component = FLEX_COMPONENT_Cr;
986 	flex_layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
987 	flex_layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
988 
989 	return 0;
990 }
991 
992 
993 /*
994  *  Locks the Gralloc 1.0 buffer, for the specified CPU usage, asynchronously.
995  *  This function can be called on any format but populates layout parameters
996  *  only for formats compatible with Android Flex Format.
997  *
998  * @param m           [in]   Gralloc module.
999  * @param buffer      [in]   The buffer to lock.
1000  * @param usage       [in]   Producer and consumer combined usage.
1001  * @param l           [in]   Access region left offset (in pixels).
1002  * @param t           [in]   Access region top offset (in pixels).
1003  * @param w           [in]   Access region requested width (in pixels).
1004  * @param h           [in]   Access region requested height (in pixels).
1005  * @param flex_layout [out]  Describes flex YUV format for consumption by applications.
1006  * @param fence_fd    [in]   Refers to an acquire sync fence object.
1007  *
1008  * @return 0, when the locking is successful;
1009  *         Appropriate error, otherwise
1010  */
mali_gralloc_lock_flex_async(const mali_gralloc_module * m,const buffer_handle_t buffer,const uint64_t usage,const int l,const int t,const int w,const int h,struct android_flex_layout * const flex_layout,const int32_t fence_fd)1011 int mali_gralloc_lock_flex_async(const mali_gralloc_module *m,
1012                                  const buffer_handle_t buffer,
1013                                  const uint64_t usage, const int l, const int t,
1014                                  const int w, const  int h,
1015                                  struct android_flex_layout * const flex_layout,
1016                                  const int32_t fence_fd)
1017 {
1018 	GRALLOC_UNUSED(m);
1019 
1020 	if (fence_fd >= 0)
1021 	{
1022 		sync_wait(fence_fd, -1);
1023 		close(fence_fd);
1024 	}
1025 
1026 	private_handle_t * const hnd = (private_handle_t *)buffer;
1027 	const uint32_t base_format = hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK;
1028 
1029 	/* Validate input parameters for lock request */
1030 	int status = validate_lock_input_parameters(buffer, l, t, w, h, usage);
1031 	if (status != 0)
1032 	{
1033 		return status;
1034 	}
1035 
1036 	const int32_t format_idx = get_format_index(base_format);
1037 	if (format_idx == -1)
1038 	{
1039 		AERR("Corrupted buffer format 0x%" PRIx64 " of buffer %p", hnd->alloc_format, hnd);
1040 		return -EINVAL;
1041 	}
1042 
1043 	if (formats[format_idx].flex != true)
1044 	{
1045 		AERR("Format 0x%" PRIx64 " of %p can't be represented in flex", hnd->alloc_format, hnd);
1046 		return GRALLOC1_ERROR_UNSUPPORTED;
1047 	}
1048 
1049 	if (ion_map_for_lock(hnd) < 0)
1050 	{
1051 		return -EINVAL;
1052 	}
1053 
1054 	if (is_exynos_format((uint32_t)base_format))
1055 	{
1056 		if (get_flexlayout_exynos_formats_only(base_format, usage, hnd, flex_layout) < 0)
1057 		{
1058 			AERR("Can't lock buffer %p: format %" PRIx64 " not handled", hnd, hnd->alloc_format);
1059 			return GRALLOC1_ERROR_UNSUPPORTED;
1060 		}
1061 
1062 		buffer_sync(hnd, get_tx_direction(usage));
1063 
1064 		return GRALLOC1_ERROR_NONE;
1065 	}
1066 
1067 	flex_layout->num_planes = formats[format_idx].ncmp;
1068 	switch (base_format)
1069 	{
1070 	case MALI_GRALLOC_FORMAT_INTERNAL_Y8:
1071 		flex_layout->format = FLEX_FORMAT_Y;
1072 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1073 		                      hnd->plane_info[0].byte_stride, 1, 1,
1074 		                      &flex_layout->planes[0]);
1075 		break;
1076 
1077 	case MALI_GRALLOC_FORMAT_INTERNAL_Y16:
1078 		flex_layout->format = FLEX_FORMAT_Y;
1079 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 16, 16, 2,
1080 		                      hnd->plane_info[0].byte_stride, 1, 1,
1081 		                      &flex_layout->planes[0]);
1082 		break;
1083 
1084 	case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
1085 		/* Y:UV 4:2:0 */
1086 		flex_layout->format = FLEX_FORMAT_YCbCr;
1087 
1088 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1089 		                      hnd->plane_info[0].byte_stride, 1, 1,
1090 		                      &flex_layout->planes[0]);
1091 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1092 		                      FLEX_COMPONENT_Cb, 8, 8, 2,
1093 		                      hnd->plane_info[1].byte_stride, 2, 2,
1094 		                      &flex_layout->planes[1]);
1095 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 1,
1096 		                      FLEX_COMPONENT_Cr, 8, 8, 2,
1097 		                      hnd->plane_info[1].byte_stride, 2, 2,
1098 		                      &flex_layout->planes[2]);
1099 		break;
1100 
1101 	case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
1102 		/* Y:VU 4:2:0 ordering. The flex format plane order must still
1103 		 * follow YCbCr order (as defined by 'android_flex_component_t').
1104 		 */
1105 		flex_layout->format = FLEX_FORMAT_YCbCr;
1106 
1107 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1108 		                      hnd->plane_info[0].byte_stride, 1, 1,
1109 		                      &flex_layout->planes[0]);
1110 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 1,
1111 		                      FLEX_COMPONENT_Cb, 8, 8, 2,
1112 		                      hnd->plane_info[1].byte_stride, 2, 2,
1113 		                      &flex_layout->planes[1]);
1114 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1115 		                      FLEX_COMPONENT_Cr, 8, 8, 2,
1116 		                      hnd->plane_info[1].byte_stride, 2, 2,
1117 		                      &flex_layout->planes[2]);
1118 		break;
1119 
1120 	case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
1121 		/* Y:V:U 4:2:0 . The flex format plane order must still follow YCbCr
1122 		 * order (as defined by 'android_flex_component_t').
1123 		 */
1124 		flex_layout->format = FLEX_FORMAT_YCbCr;
1125 
1126 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1127 		                      hnd->plane_info[0].byte_stride, 1, 1,
1128 		                      &flex_layout->planes[0]);
1129 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[2].offset,
1130 		                      FLEX_COMPONENT_Cb, 8, 8, 1,
1131 		                      hnd->plane_info[2].byte_stride, 2, 2,
1132 		                      &flex_layout->planes[1]);
1133 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1134 		                      FLEX_COMPONENT_Cr, 8, 8, 1,
1135 		                      hnd->plane_info[1].byte_stride, 2, 2,
1136 		                      &flex_layout->planes[2]);
1137 		break;
1138 
1139 	case MALI_GRALLOC_FORMAT_INTERNAL_P010:
1140 		/* Y:UV 4:2:0 */
1141 		flex_layout->format = FLEX_FORMAT_YCbCr;
1142 
1143 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 16, 10, 2,
1144 		                      hnd->plane_info[0].byte_stride, 1, 1,
1145 		                      &flex_layout->planes[0]);
1146 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1147 		                      FLEX_COMPONENT_Cb, 16, 10, 4,
1148 		                      hnd->plane_info[1].byte_stride, 2, 2,
1149 		                      &flex_layout->planes[1]);
1150 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 2,
1151 		                      FLEX_COMPONENT_Cr, 16, 10, 4,
1152 		                      hnd->plane_info[1].byte_stride, 2, 2,
1153 		                      &flex_layout->planes[2]);
1154 		break;
1155 
1156 	case MALI_GRALLOC_FORMAT_INTERNAL_P210:
1157 		/* Y:UV 4:2:2 */
1158 		flex_layout->format = FLEX_FORMAT_YCbCr;
1159 
1160 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 16, 10, 2,
1161 		                      hnd->plane_info[0].byte_stride, 1, 1,
1162 		                      &flex_layout->planes[0]);
1163 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1164 		                      FLEX_COMPONENT_Cb, 16, 10, 4,
1165 		                      hnd->plane_info[1].byte_stride, 2, 1,
1166 		                      &flex_layout->planes[1]);
1167 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 2,
1168 		                      FLEX_COMPONENT_Cr, 16, 10, 4,
1169 		                      hnd->plane_info[1].byte_stride, 2, 1,
1170 		                      &flex_layout->planes[2]);
1171 		break;
1172 
1173 	case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT:
1174 		/* YUYV 4:2:2 */
1175 		flex_layout->format = FLEX_FORMAT_YCbCr;
1176 
1177 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 2,
1178 		                      hnd->plane_info[0].byte_stride, 1, 1,
1179 		                      &flex_layout->planes[0]);
1180 		set_flex_plane_params((uint8_t *)hnd->base + 1, FLEX_COMPONENT_Cb, 8, 8, 4,
1181 		                      hnd->plane_info[0].byte_stride, 2, 1,
1182 		                      &flex_layout->planes[1]);
1183 		set_flex_plane_params((uint8_t *)hnd->base + 3, FLEX_COMPONENT_Cr, 8, 8, 4,
1184 		                      hnd->plane_info[0].byte_stride, 2, 1,
1185 		                      &flex_layout->planes[2]);
1186 
1187 		break;
1188 
1189 	case MALI_GRALLOC_FORMAT_INTERNAL_NV16:
1190 		/* Y:UV 4:2:2 */
1191 		flex_layout->format = FLEX_FORMAT_YCbCr;
1192 
1193 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1194 		                      hnd->plane_info[0].byte_stride, 1, 1,
1195 		                      &flex_layout->planes[0]);
1196 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1197 		                      FLEX_COMPONENT_Cb, 8, 8, 2,
1198 		                      hnd->plane_info[1].byte_stride, 2, 1,
1199 		                      &flex_layout->planes[1]);
1200 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 1,
1201 		                      FLEX_COMPONENT_Cr, 8, 8, 2,
1202 		                      hnd->plane_info[1].byte_stride, 2, 1,
1203 		                      &flex_layout->planes[2]);
1204 
1205 		break;
1206 
1207 	case MALI_GRALLOC_FORMAT_INTERNAL_Y210:
1208 		/* YUYV 4:2:2 */
1209 		flex_layout->format = FLEX_FORMAT_YCbCr;
1210 
1211 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 16, 10, 4,
1212 		                      hnd->plane_info[0].byte_stride, 1, 1,
1213 		                      &flex_layout->planes[0]);
1214 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_Cb, 16, 10, 8,
1215 		                      hnd->plane_info[0].byte_stride, 2, 1,
1216 		                      &flex_layout->planes[1]);
1217 		set_flex_plane_params((uint8_t *)hnd->base + 6, FLEX_COMPONENT_Cr, 16, 10, 8,
1218 		                      hnd->plane_info[0].byte_stride, 2, 1,
1219 		                      &flex_layout->planes[2]);
1220 
1221 		break;
1222 
1223 #if PLATFORM_SDK_VERSION >= 26
1224 	/* 64-bit format that has 16-bit R, G, B, and A components, in that order */
1225 	case MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616:
1226 		flex_layout->format = FLEX_FORMAT_RGBA;
1227 
1228 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_R, 16, 16, 8,
1229 		                      hnd->plane_info[0].byte_stride, 1, 1,
1230 		                      &flex_layout->planes[0]);
1231 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_G, 16, 16, 8,
1232 		                      hnd->plane_info[0].byte_stride, 1, 1,
1233 		                      &flex_layout->planes[1]);
1234 		set_flex_plane_params((uint8_t *)hnd->base + 4, FLEX_COMPONENT_B, 16, 16, 8,
1235 		                      hnd->plane_info[0].byte_stride, 1, 1,
1236 		                      &flex_layout->planes[2]);
1237 		set_flex_plane_params((uint8_t *)hnd->base + 6, FLEX_COMPONENT_A, 16, 16, 8,
1238 		                      hnd->plane_info[0].byte_stride, 1, 1,
1239 		                      &flex_layout->planes[3]);
1240 		break;
1241 #endif
1242 
1243 	case MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888:
1244 		/* 32-bit format that has 8-bit R, G, B, and A components, in that order */
1245 		flex_layout->format = FLEX_FORMAT_RGBA;
1246 
1247 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_R, 8, 8, 4,
1248 		                      hnd->plane_info[0].byte_stride, 1, 1,
1249 		                      &flex_layout->planes[0]);
1250 		set_flex_plane_params((uint8_t *)hnd->base + 1, FLEX_COMPONENT_G, 8, 8, 4,
1251 		                      hnd->plane_info[0].byte_stride, 1, 1,
1252 		                      &flex_layout->planes[1]);
1253 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_B, 8, 8, 4,
1254 		                      hnd->plane_info[0].byte_stride, 1, 1,
1255 		                      &flex_layout->planes[2]);
1256 		set_flex_plane_params((uint8_t *)hnd->base + 3, FLEX_COMPONENT_A, 8, 8, 4,
1257 		                      hnd->plane_info[0].byte_stride, 1, 1,
1258 		                      &flex_layout->planes[3]);
1259 		break;
1260 
1261 	case MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888:
1262 		/* 32-bit format that has 8-bit R, G, B, and unused components, in that order */
1263 		flex_layout->format = FLEX_FORMAT_RGB;
1264 
1265 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_R, 8, 8, 4,
1266 		                      hnd->plane_info[0].byte_stride, 1, 1,
1267 		                      &flex_layout->planes[0]);
1268 		set_flex_plane_params((uint8_t *)hnd->base + 1, FLEX_COMPONENT_G, 8, 8, 4,
1269 		                      hnd->plane_info[0].byte_stride, 1, 1,
1270 		                      &flex_layout->planes[1]);
1271 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_B, 8, 8, 4,
1272 		                      hnd->plane_info[0].byte_stride, 1, 1,
1273 		                      &flex_layout->planes[2]);
1274 		break;
1275 
1276 	case MALI_GRALLOC_FORMAT_INTERNAL_RGB_888:
1277 		/* 24-bit format that has 8-bit R, G, and B components, in that order */
1278 		flex_layout->format = FLEX_FORMAT_RGB;
1279 
1280 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_R, 8, 8, 3,
1281 		                      hnd->plane_info[0].byte_stride, 1, 1,
1282 		                      &flex_layout->planes[0]);
1283 		set_flex_plane_params((uint8_t *)hnd->base + 1, FLEX_COMPONENT_G, 8, 8, 3,
1284 		                      hnd->plane_info[0].byte_stride, 1, 1,
1285 		                      &flex_layout->planes[1]);
1286 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_B, 8, 8, 3,
1287 		                      hnd->plane_info[0].byte_stride, 1, 1,
1288 		                      &flex_layout->planes[2]);
1289 		break;
1290 
1291 	case MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888:
1292 		/* 32-bit format that has 8-bit B, G, R, and A components, in that order.
1293 		 * The flex format plane order must still follow FLEX_FORMAT_RGBA
1294 		 * order (as defined by 'android_flex_component_t').
1295 		 */
1296 		flex_layout->format = FLEX_FORMAT_RGBA;
1297 
1298 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_B, 8, 8, 4,
1299 		                      hnd->plane_info[0].byte_stride, 1, 1,
1300 		                      &flex_layout->planes[2]);
1301 		set_flex_plane_params((uint8_t *)hnd->base + 1, FLEX_COMPONENT_G, 8, 8, 4,
1302 		                      hnd->plane_info[0].byte_stride, 1, 1,
1303 		                      &flex_layout->planes[1]);
1304 		set_flex_plane_params((uint8_t *)hnd->base + 2, FLEX_COMPONENT_R, 8, 8, 4,
1305 		                      hnd->plane_info[0].byte_stride, 1, 1,
1306 		                      &flex_layout->planes[0]);
1307 		set_flex_plane_params((uint8_t *)hnd->base + 3, FLEX_COMPONENT_A, 8, 8, 4,
1308 		                      hnd->plane_info[0].byte_stride, 1, 1,
1309 		                      &flex_layout->planes[3]);
1310 		break;
1311 	case HAL_PIXEL_FORMAT_GOOGLE_NV12_SP:
1312 		flex_layout->format = FLEX_FORMAT_YCbCr;
1313 
1314 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1315 				      hnd->plane_info[0].byte_stride, 1, 1,
1316 				      &flex_layout->planes[0]);
1317 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1318 				      FLEX_COMPONENT_Cb, 8, 8, 2,
1319 				      hnd->plane_info[1].byte_stride, 2, 2,
1320 				      &flex_layout->planes[1]);
1321 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 1,
1322 				      FLEX_COMPONENT_Cr, 8, 8, 2,
1323 				      hnd->plane_info[1].byte_stride, 2, 2,
1324 				      &flex_layout->planes[2]);
1325 		break;
1326 	case HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B:
1327 		/* With Mapper2.0's default Gralloc1Hal, bits_per_component
1328 		 * is restricted to 8 bits.
1329 		 */
1330 		flex_layout->format = FLEX_FORMAT_YCbCr;
1331 
1332 		set_flex_plane_params((uint8_t *)hnd->base, FLEX_COMPONENT_Y, 8, 8, 1,
1333 				      hnd->plane_info[0].byte_stride, 1, 1,
1334 				      &flex_layout->planes[0]);
1335 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset,
1336 				      FLEX_COMPONENT_Cb, 8, 8, 2,
1337 				      hnd->plane_info[1].byte_stride, 2, 2,
1338 				      &flex_layout->planes[1]);
1339 		set_flex_plane_params((uint8_t *)hnd->base + hnd->plane_info[1].offset + 1,
1340 				      FLEX_COMPONENT_Cr, 8, 8, 2,
1341 				      hnd->plane_info[1].byte_stride, 2, 2,
1342 				      &flex_layout->planes[2]);
1343 		break;
1344 
1345 	default:
1346 		AERR("Can't lock buffer %p: format 0x%" PRIx64 " not handled", hnd, hnd->alloc_format);
1347 		return GRALLOC1_ERROR_UNSUPPORTED;
1348 	}
1349 
1350 	buffer_sync(hnd, get_tx_direction(usage));
1351 
1352 	return GRALLOC1_ERROR_NONE;
1353 }
1354 #endif
1355 
1356 /*
1357  *  Unlocks the buffer asynchronously.
1358  *
1359  * @param m           [in]   Gralloc module.
1360  * @param buffer      [in]   The buffer to unlock.
1361  * @param fence_fd    [out]  Refers to an acquire sync fence object.
1362  *
1363  * @return 0, when the locking is successful;
1364  *         Appropriate error, otherwise
1365  *
1366  * Note: unlocking a buffer which is not locked results in an unexpected behaviour.
1367  *       Though it is possible to create a state machine to track the buffer state to
1368  *       recognize erroneous conditions, it is expected of client to adhere to API
1369  *       call sequence
1370  */
mali_gralloc_unlock_async(const mali_gralloc_module * m,buffer_handle_t buffer,int32_t * const fence_fd)1371 int mali_gralloc_unlock_async(const mali_gralloc_module *m, buffer_handle_t buffer,
1372                               int32_t * const fence_fd)
1373 {
1374 	*fence_fd = -1;
1375 
1376 	if (mali_gralloc_unlock(m, buffer) < 0)
1377 	{
1378 		return -EINVAL;
1379 	}
1380 
1381 	return 0;
1382 }
1383