OSDN Git Service

AndroidIA: Added support for Travis CI
[android-x86/external-minigbm.git] / cros_gralloc / cros_gralloc_helpers.cc
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 <cstdlib>
10 #include <cutils/log.h>
11 #include <sync/sync.h>
12
13 uint32_t cros_gralloc_convert_format(int format)
14 {
15         /*
16          * Conversion from HAL to fourcc-based DRV formats based on
17          * platform_android.c in mesa.
18          */
19
20         switch (format) {
21         case HAL_PIXEL_FORMAT_BGRA_8888:
22                 return DRM_FORMAT_ARGB8888;
23         case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
24                 return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
25         case HAL_PIXEL_FORMAT_RGB_565:
26                 return DRM_FORMAT_RGB565;
27         case HAL_PIXEL_FORMAT_RGB_888:
28                 return DRM_FORMAT_RGB888;
29         case HAL_PIXEL_FORMAT_RGBA_8888:
30                 return DRM_FORMAT_ABGR8888;
31         case HAL_PIXEL_FORMAT_RGBX_8888:
32                 return DRM_FORMAT_XBGR8888;
33         case HAL_PIXEL_FORMAT_YCbCr_420_888:
34                 return DRM_FORMAT_FLEX_YCbCr_420_888;
35         case HAL_PIXEL_FORMAT_YV12:
36                 return DRM_FORMAT_YVU420_ANDROID;
37         /*
38          * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
39          * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
40          * equal to their size in bytes.
41          */
42         case HAL_PIXEL_FORMAT_BLOB:
43                 return DRM_FORMAT_R8;
44         }
45
46         return DRM_FORMAT_NONE;
47 }
48
49 cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
50 {
51         auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
52         if (!hnd || hnd->magic != cros_gralloc_magic)
53                 return nullptr;
54
55         return hnd;
56 }
57
58 int32_t cros_gralloc_sync_wait(int32_t acquire_fence)
59 {
60         if (acquire_fence < 0)
61                 return 0;
62
63         /*
64          * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
65          * documentation states the caller waits indefinitely on the fence if timeout < 0.
66          */
67         int err = sync_wait(acquire_fence, 1000);
68         if (err < 0) {
69                 cros_gralloc_error("Timed out on sync wait, err = %s", strerror(errno));
70                 err = sync_wait(acquire_fence, -1);
71                 if (err < 0) {
72                         cros_gralloc_error("sync wait error = %s", strerror(errno));
73                         return -errno;
74                 }
75         }
76
77         err = close(acquire_fence);
78         if (err) {
79                 cros_gralloc_error("Unable to close fence fd, err = %s", strerror(errno));
80                 return -errno;
81         }
82
83         return 0;
84 }
85
86 void cros_gralloc_log(const char *prefix, const char *file, int line, const char *format, ...)
87 {
88         char buf[50];
89         snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
90
91         va_list args;
92         va_start(args, format);
93         __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
94         va_end(args);
95 }