blob: afb1a96dceffb1735c67ae2c27ef96c68aaa20ef [file] [log] [blame]
Gurchetan Singh2e786ad2016-08-24 18:31:23 -07001/*
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
14uint64_t cros_gralloc_convert_flags(int flags)
15{
Gurchetan Singh458976f2016-11-23 17:32:33 -080016 uint64_t usage = BO_USE_NONE;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070017
18 if (flags & GRALLOC_USAGE_CURSOR)
Gurchetan Singhf17c8132016-11-29 14:45:46 -080019 usage |= BO_USE_NONE;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070020 if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_RARELY)
Gurchetan Singh458976f2016-11-23 17:32:33 -080021 usage |= BO_USE_SW_READ_RARELY;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070022 if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_OFTEN)
Gurchetan Singh458976f2016-11-23 17:32:33 -080023 usage |= BO_USE_SW_READ_OFTEN;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070024 if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_RARELY)
Gurchetan Singh458976f2016-11-23 17:32:33 -080025 usage |= BO_USE_SW_WRITE_RARELY;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070026 if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_OFTEN)
Gurchetan Singh458976f2016-11-23 17:32:33 -080027 usage |= BO_USE_SW_WRITE_OFTEN;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070028 if (flags & GRALLOC_USAGE_HW_TEXTURE)
Gurchetan Singh458976f2016-11-23 17:32:33 -080029 usage |= BO_USE_RENDERING;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070030 if (flags & GRALLOC_USAGE_HW_RENDER)
Gurchetan Singh458976f2016-11-23 17:32:33 -080031 usage |= BO_USE_RENDERING;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070032 if (flags & GRALLOC_USAGE_HW_2D)
Gurchetan Singh458976f2016-11-23 17:32:33 -080033 usage |= BO_USE_RENDERING;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070034 if (flags & GRALLOC_USAGE_HW_COMPOSER)
Gurchetan Singh56662da2016-09-12 16:21:29 -070035 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singh458976f2016-11-23 17:32:33 -080036 usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070037 if (flags & GRALLOC_USAGE_HW_FB)
Gurchetan Singhf17c8132016-11-29 14:45:46 -080038 usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070039 if (flags & GRALLOC_USAGE_EXTERNAL_DISP)
Gurchetan Singh56662da2016-09-12 16:21:29 -070040 /* We're ignoring this flag until we decide what to with display link */
Gurchetan Singh458976f2016-11-23 17:32:33 -080041 usage |= BO_USE_NONE;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070042 if (flags & GRALLOC_USAGE_PROTECTED)
Gurchetan Singh458976f2016-11-23 17:32:33 -080043 usage |= BO_USE_PROTECTED;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070044 if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
45 /*HACK: See b/30054495 */
Gurchetan Singh458976f2016-11-23 17:32:33 -080046 usage |= BO_USE_SW_READ_OFTEN;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070047 if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
Gurchetan Singh458976f2016-11-23 17:32:33 -080048 usage |= BO_USE_HW_CAMERA_WRITE;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070049 if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
Gurchetan Singh458976f2016-11-23 17:32:33 -080050 usage |= BO_USE_HW_CAMERA_READ;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070051 if (flags & GRALLOC_USAGE_HW_CAMERA_ZSL)
Gurchetan Singh458976f2016-11-23 17:32:33 -080052 usage |= BO_USE_HW_CAMERA_ZSL;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070053 if (flags & GRALLOC_USAGE_RENDERSCRIPT)
Gurchetan Singh458976f2016-11-23 17:32:33 -080054 usage |= BO_USE_RENDERSCRIPT;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070055
56 return usage;
57}
58
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080059uint32_t cros_gralloc_convert_format(int format)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070060{
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:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080068 return DRM_FORMAT_ARGB8888;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070069 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080070 return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070071 case HAL_PIXEL_FORMAT_RGB_565:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080072 return DRM_FORMAT_RGB565;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070073 case HAL_PIXEL_FORMAT_RGB_888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080074 return DRM_FORMAT_RGB888;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070075 case HAL_PIXEL_FORMAT_RGBA_8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080076 return DRM_FORMAT_ABGR8888;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070077 case HAL_PIXEL_FORMAT_RGBX_8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080078 return DRM_FORMAT_XBGR8888;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070079 case HAL_PIXEL_FORMAT_YCbCr_420_888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080080 return DRM_FORMAT_FLEX_YCbCr_420_888;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070081 case HAL_PIXEL_FORMAT_YV12:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080082 return DRM_FORMAT_YVU420;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070083 }
84
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080085 return DRM_FORMAT_NONE;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070086}
87
Gurchetan Singh5521bde2016-09-30 14:53:32 -070088static int32_t cros_gralloc_query_rendernode(struct driver **drv,
Gurchetan Singh0d210f92016-12-02 18:15:35 -080089 const char *undesired)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070090{
Gurchetan Singh0d210f92016-12-02 18:15:35 -080091 /*
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
Gurchetan Singh2e786ad2016-08-24 18:31:23 -070098 int fd;
Gurchetan Singh5521bde2016-09-30 14:53:32 -070099 drmVersionPtr version;
100 char const *str = "%s/renderD%d";
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700101 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
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700108 if (asprintf(&node, str, DRM_DIR_NAME, i) < 0)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700109 continue;
110
111 fd = open(node, O_RDWR, 0);
112 free(node);
113
114 if (fd < 0)
115 continue;
116
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700117 version = drmGetVersion(fd);
Gurchetan Singh0d210f92016-12-02 18:15:35 -0800118 if (!version)
119 continue;
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700120
Gurchetan Singh0d210f92016-12-02 18:15:35 -0800121 if (undesired && !strcmp(version->name, undesired)) {
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700122 drmFreeVersion(version);
123 continue;
124 }
125
126 drmFreeVersion(version);
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700127 *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
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700136int32_t cros_gralloc_rendernode_open(struct driver **drv)
137{
138 int32_t ret;
Gurchetan Singh0d210f92016-12-02 18:15:35 -0800139 ret = cros_gralloc_query_rendernode(drv, "vgem");
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700140
Gurchetan Singh0d210f92016-12-02 18:15:35 -0800141 /* Allow vgem driver if no hardware is found. */
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700142 if (ret)
Gurchetan Singh0d210f92016-12-02 18:15:35 -0800143 ret = cros_gralloc_query_rendernode(drv, NULL);
Gurchetan Singh5521bde2016-09-30 14:53:32 -0700144
145 return ret;
146}
147
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700148int32_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
156void cros_gralloc_log(const char *prefix, const char *file, int line,
157 const char *format, ...)
158{
Gurchetan Singh54150e82016-12-02 18:28:38 -0800159 char buf[50];
160 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
161
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700162 va_list args;
163 va_start(args, format);
Gurchetan Singh54150e82016-12-02 18:28:38 -0800164 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700165 va_end(args);
166}