OSDN Git Service

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