OSDN Git Service

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