OSDN Git Service

minigbm: add BO_USE_TEXTURE
[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 <fcntl.h>
12 #include <xf86drm.h>
13
14 uint64_t cros_gralloc_convert_flags(int flags)
15 {
16         uint64_t usage = BO_USE_NONE;
17
18         if (flags & GRALLOC_USAGE_CURSOR)
19                 usage |= BO_USE_NONE;
20         if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_RARELY)
21                 usage |= BO_USE_SW_READ_RARELY;
22         if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_OFTEN)
23                 usage |= BO_USE_SW_READ_OFTEN;
24         if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_RARELY)
25                 usage |= BO_USE_SW_WRITE_RARELY;
26         if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_OFTEN)
27                 usage |= BO_USE_SW_WRITE_OFTEN;
28         if (flags & GRALLOC_USAGE_HW_TEXTURE)
29                 usage |= BO_USE_TEXTURE;
30         if (flags & GRALLOC_USAGE_HW_RENDER)
31                 usage |= BO_USE_RENDERING;
32         if (flags & GRALLOC_USAGE_HW_2D)
33                 usage |= BO_USE_RENDERING;
34         if (flags & GRALLOC_USAGE_HW_COMPOSER)
35         /* HWC wants to use display hardware, but can defer to OpenGL. */
36                 usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
37         if (flags & GRALLOC_USAGE_HW_FB)
38                 usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
39         if (flags & GRALLOC_USAGE_EXTERNAL_DISP)
40         /* We're ignoring this flag until we decide what to with display link */
41                 usage |= BO_USE_NONE;
42         if (flags & GRALLOC_USAGE_PROTECTED)
43                 usage |= BO_USE_PROTECTED;
44         if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
45                 /*HACK: See b/30054495 */
46                 usage |= BO_USE_SW_READ_OFTEN;
47         if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
48                 usage |= BO_USE_HW_CAMERA_WRITE;
49         if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
50                 usage |= BO_USE_HW_CAMERA_READ;
51         if (flags & GRALLOC_USAGE_HW_CAMERA_ZSL)
52                 usage |= BO_USE_HW_CAMERA_ZSL;
53         if (flags & GRALLOC_USAGE_RENDERSCRIPT)
54                 usage |= BO_USE_RENDERSCRIPT;
55
56         return usage;
57 }
58
59 uint32_t cros_gralloc_convert_format(int format)
60 {
61         /*
62          * Conversion from HAL to fourcc-based DRV formats based on
63          * platform_android.c in mesa.
64          */
65
66         switch (format) {
67         case HAL_PIXEL_FORMAT_BGRA_8888:
68                 return DRM_FORMAT_ARGB8888;
69         case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
70                 return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
71         case HAL_PIXEL_FORMAT_RGB_565:
72                 return DRM_FORMAT_RGB565;
73         case HAL_PIXEL_FORMAT_RGB_888:
74                 return DRM_FORMAT_RGB888;
75         case HAL_PIXEL_FORMAT_RGBA_8888:
76                 return DRM_FORMAT_ABGR8888;
77         case HAL_PIXEL_FORMAT_RGBX_8888:
78                 return DRM_FORMAT_XBGR8888;
79         case HAL_PIXEL_FORMAT_YCbCr_420_888:
80                 return DRM_FORMAT_FLEX_YCbCr_420_888;
81         case HAL_PIXEL_FORMAT_YV12:
82                 return DRM_FORMAT_YVU420;
83         }
84
85         return DRM_FORMAT_NONE;
86 }
87
88 static int32_t cros_gralloc_query_rendernode(struct driver **drv,
89                                              const char *undesired)
90 {
91         /*
92          * Create a driver from rendernode while filtering out
93          * the specified undesired driver.
94          *
95          * TODO(gsingh): Enable render nodes on udl/evdi.
96          */
97
98         int fd;
99         drmVersionPtr version;
100         char const *str = "%s/renderD%d";
101         int32_t num_nodes = 63;
102         int32_t min_node = 128;
103         int32_t max_node = (min_node + num_nodes);
104
105         for (int i = min_node; i < max_node; i++) {
106                 char *node;
107
108                 if (asprintf(&node, str, DRM_DIR_NAME, i) < 0)
109                         continue;
110
111                 fd = open(node, O_RDWR, 0);
112                 free(node);
113
114                 if (fd < 0)
115                         continue;
116
117                 version = drmGetVersion(fd);
118                 if (!version)
119                         continue;
120
121                 if (undesired && !strcmp(version->name, undesired)) {
122                         drmFreeVersion(version);
123                         continue;
124                 }
125
126                 drmFreeVersion(version);
127                 *drv = drv_create(fd);
128
129                 if (*drv)
130                         return CROS_GRALLOC_ERROR_NONE;
131         }
132
133         return CROS_GRALLOC_ERROR_NO_RESOURCES;
134 }
135
136 int32_t cros_gralloc_rendernode_open(struct driver **drv)
137 {
138         int32_t ret;
139         ret = cros_gralloc_query_rendernode(drv, "vgem");
140
141         /* Allow vgem driver if no hardware is found. */
142         if (ret)
143                 ret = cros_gralloc_query_rendernode(drv, NULL);
144
145         return ret;
146 }
147
148 int32_t cros_gralloc_validate_handle(struct cros_gralloc_handle *hnd)
149 {
150         if (!hnd || hnd->magic != cros_gralloc_magic())
151                 return CROS_GRALLOC_ERROR_BAD_HANDLE;
152
153         return CROS_GRALLOC_ERROR_NONE;
154 }
155
156 void cros_gralloc_log(const char *prefix, const char *file, int line,
157                       const char *format, ...)
158 {
159         char buf[50];
160         snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
161
162         va_list args;
163         va_start(args, format);
164         __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
165         va_end(args);
166 }