Home | History | Annotate | Download | only in cros_gralloc
      1 /*
      2  * Copyright 2016 The Chromium OS Authors. All rights reserved.
      3  * Use of this source code is governed by a BSD-style license that can be
      4  * found in the LICENSE file.
      5  */
      6 
      7 #include "cros_gralloc_helpers.h"
      8 
      9 #include <sync/sync.h>
     10 
     11 uint32_t cros_gralloc_convert_format(int format)
     12 {
     13 	/*
     14 	 * Conversion from HAL to fourcc-based DRV formats based on
     15 	 * platform_android.c in mesa.
     16 	 */
     17 
     18 	switch (format) {
     19 	case HAL_PIXEL_FORMAT_BGRA_8888:
     20 		return DRM_FORMAT_ARGB8888;
     21 	case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
     22 		return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
     23 	case HAL_PIXEL_FORMAT_RGB_565:
     24 		return DRM_FORMAT_RGB565;
     25 	case HAL_PIXEL_FORMAT_RGB_888:
     26 		return DRM_FORMAT_BGR888;
     27 	case HAL_PIXEL_FORMAT_RGBA_8888:
     28 		return DRM_FORMAT_ABGR8888;
     29 	case HAL_PIXEL_FORMAT_RGBX_8888:
     30 		return DRM_FORMAT_XBGR8888;
     31 	case HAL_PIXEL_FORMAT_YCbCr_420_888:
     32 		return DRM_FORMAT_FLEX_YCbCr_420_888;
     33 	case HAL_PIXEL_FORMAT_YV12:
     34 		return DRM_FORMAT_YVU420_ANDROID;
     35 	/*
     36 	 * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
     37 	 * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
     38 	 * equal to their size in bytes.
     39 	 */
     40 	case HAL_PIXEL_FORMAT_BLOB:
     41 		return DRM_FORMAT_R8;
     42 	}
     43 
     44 	return DRM_FORMAT_NONE;
     45 }
     46 
     47 cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
     48 {
     49 	auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
     50 	if (!hnd || hnd->magic != cros_gralloc_magic)
     51 		return nullptr;
     52 
     53 	return hnd;
     54 }
     55 
     56 int32_t cros_gralloc_sync_wait(int32_t acquire_fence)
     57 {
     58 	if (acquire_fence < 0)
     59 		return 0;
     60 
     61 	/*
     62 	 * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
     63 	 * documentation states the caller waits indefinitely on the fence if timeout < 0.
     64 	 */
     65 	int err = sync_wait(acquire_fence, 1000);
     66 	if (err < 0) {
     67 		drv_log("Timed out on sync wait, err = %s\n", strerror(errno));
     68 		err = sync_wait(acquire_fence, -1);
     69 		if (err < 0) {
     70 			drv_log("sync wait error = %s\n", strerror(errno));
     71 			return -errno;
     72 		}
     73 	}
     74 
     75 	err = close(acquire_fence);
     76 	if (err) {
     77 		drv_log("Unable to close fence fd, err = %s\n", strerror(errno));
     78 		return -errno;
     79 	}
     80 
     81 	return 0;
     82 }
     83