blob: 1ef926515d09465b572e62a65fd9e702f1609fba [file] [log] [blame]
Gurchetan Singhd6b8b032017-05-31 14:31:31 -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_driver.h"
8
9#include <hardware/gralloc.h>
10#include <memory.h>
11
12struct gralloc0_module {
13 gralloc_module_t base;
14 std::unique_ptr<alloc_device_t> alloc;
15 std::unique_ptr<cros_gralloc_driver> driver;
16};
17
18/* This enumeration must match the one in <gralloc_drm.h>.
19 * The functions supported by this gralloc's temporary private API are listed
20 * below. Use of these functions is highly discouraged and should only be
21 * reserved for cases where no alternative to get same information (such as
22 * querying ANativeWindow) exists.
23 */
24// clang-format off
25enum {
26 GRALLOC_DRM_GET_STRIDE,
27 GRALLOC_DRM_GET_FORMAT,
28 GRALLOC_DRM_GET_DIMENSIONS,
29 GRALLOC_DRM_GET_BACKING_STORE,
30};
31// clang-format on
32
33static int64_t gralloc0_convert_flags(int flags)
34{
35 uint64_t usage = BO_USE_NONE;
36
37 if (flags & GRALLOC_USAGE_CURSOR)
38 usage |= BO_USE_NONE;
39 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
40 usage |= BO_USE_SW_READ_RARELY;
41 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
42 usage |= BO_USE_SW_READ_OFTEN;
43 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
44 usage |= BO_USE_SW_WRITE_RARELY;
45 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
46 usage |= BO_USE_SW_WRITE_OFTEN;
47 if (flags & GRALLOC_USAGE_HW_TEXTURE)
48 usage |= BO_USE_TEXTURE;
49 if (flags & GRALLOC_USAGE_HW_RENDER)
50 usage |= BO_USE_RENDERING;
51 if (flags & GRALLOC_USAGE_HW_2D)
52 usage |= BO_USE_RENDERING;
53 if (flags & GRALLOC_USAGE_HW_COMPOSER)
54 /* HWC wants to use display hardware, but can defer to OpenGL. */
55 usage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
56 if (flags & GRALLOC_USAGE_HW_FB)
57 usage |= BO_USE_NONE;
58 if (flags & GRALLOC_USAGE_EXTERNAL_DISP)
59 /*
60 * This flag potentially covers external display for the normal drivers (i915,
61 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
62 * */
63 usage |= BO_USE_NONE;
64 if (flags & GRALLOC_USAGE_PROTECTED)
65 usage |= BO_USE_PROTECTED;
66 if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
67 /*HACK: See b/30054495 */
68 usage |= BO_USE_SW_READ_OFTEN;
69 if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
70 usage |= BO_USE_HW_CAMERA_WRITE;
71 if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
72 usage |= BO_USE_HW_CAMERA_READ;
73 if (flags & GRALLOC_USAGE_HW_CAMERA_ZSL)
74 usage |= BO_USE_HW_CAMERA_ZSL;
75 if (flags & GRALLOC_USAGE_RENDERSCRIPT)
76 /* We use CPU for compute. */
77 usage |= BO_USE_LINEAR;
78
79 return usage;
80}
81
82static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
83 buffer_handle_t *handle, int *stride)
84{
85 int32_t ret;
86 bool supported;
87 struct cros_gralloc_buffer_descriptor descriptor;
88 auto mod = (struct gralloc0_module *)dev->common.module;
89
90 descriptor.width = w;
91 descriptor.height = h;
92 descriptor.droid_format = format;
93 descriptor.producer_usage = descriptor.consumer_usage = usage;
94 descriptor.drm_format = cros_gralloc_convert_format(format);
95 descriptor.drv_usage = gralloc0_convert_flags(usage);
96
97 supported = mod->driver->is_supported(&descriptor);
98 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
99 descriptor.drv_usage &= ~BO_USE_SCANOUT;
100 supported = mod->driver->is_supported(&descriptor);
101 }
102
103 if (!supported) {
104 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, "
105 "drv_format: %4.4s, drv_flags: %llu",
106 format, usage, reinterpret_cast<char *>(descriptor.drm_format),
107 static_cast<unsigned long long>(descriptor.drv_usage));
108 return CROS_GRALLOC_ERROR_UNSUPPORTED;
109 }
110
111 ret = mod->driver->allocate(&descriptor, handle);
112 if (ret)
113 return ret;
114
115 auto hnd = cros_gralloc_convert_handle(*handle);
116 *stride = hnd->pixel_stride;
117
118 return CROS_GRALLOC_ERROR_NONE;
119}
120
121static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
122{
123 auto mod = (struct gralloc0_module *)dev->common.module;
124 return mod->driver->release(handle);
125}
126
127static int gralloc0_close(struct hw_device_t *dev)
128{
129 /* Memory is freed by managed pointers on process close. */
130 return CROS_GRALLOC_ERROR_NONE;
131}
132
133static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
134{
135 auto module = (struct gralloc0_module *)mod;
136
137 if (module->alloc) {
138 *dev = &module->alloc->common;
139 return CROS_GRALLOC_ERROR_NONE;
140 }
141
142 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
143 cros_gralloc_error("Incorrect device name - %s.", name);
144 return CROS_GRALLOC_ERROR_UNSUPPORTED;
145 }
146
147 module->driver = std::make_unique<cros_gralloc_driver>();
148 if (module->driver->init()) {
149 cros_gralloc_error("Failed to initialize driver.");
150 return CROS_GRALLOC_ERROR_NO_RESOURCES;
151 }
152
153 module->alloc = std::make_unique<alloc_device_t>();
154
155 module->alloc->alloc = gralloc0_alloc;
156 module->alloc->free = gralloc0_free;
157 module->alloc->common.tag = HARDWARE_DEVICE_TAG;
158 module->alloc->common.version = 0;
159 module->alloc->common.module = (hw_module_t *)mod;
160 module->alloc->common.close = gralloc0_close;
161
162 *dev = &module->alloc->common;
163 return CROS_GRALLOC_ERROR_NONE;
164}
165
166static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
167{
168 auto mod = (struct gralloc0_module *)module;
169
170 if (!mod->driver) {
171 mod->driver = std::make_unique<cros_gralloc_driver>();
172 if (mod->driver->init()) {
173 cros_gralloc_error("Failed to initialize driver.");
174 return CROS_GRALLOC_ERROR_NO_RESOURCES;
175 }
176 }
177
178 return mod->driver->retain(handle);
179}
180
181static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
182{
183 auto mod = (struct gralloc0_module *)module;
184 return mod->driver->release(handle);
185}
186
187static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
188 int l, int t, int w, int h, void **vaddr)
189{
190 int32_t ret, fence;
191 uint64_t flags;
192 uint8_t *addr[DRV_MAX_PLANES];
193 auto mod = (struct gralloc0_module *)module;
194
195 auto hnd = cros_gralloc_convert_handle(handle);
196 if (!hnd) {
197 cros_gralloc_error("Invalid handle.");
198 return CROS_GRALLOC_ERROR_BAD_HANDLE;
199 }
200
201 if ((hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888)) {
202 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
203 return CROS_GRALLOC_ERROR_BAD_HANDLE;
204 }
205
206 fence = -1;
207 flags = gralloc0_convert_flags(usage);
208 ret = mod->driver->lock(handle, fence, flags, addr);
209 *vaddr = addr[0];
210 return ret;
211}
212
213static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
214{
215 auto mod = (struct gralloc0_module *)module;
216 return mod->driver->unlock(handle);
217}
218
219static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
220{
221 va_list args;
222 int32_t *out_format, ret;
223 uint64_t *out_store;
224 buffer_handle_t handle;
225 uint32_t *out_width, *out_height, *out_stride;
226 auto mod = (struct gralloc0_module *)module;
227
228 switch (op) {
229 case GRALLOC_DRM_GET_STRIDE:
230 case GRALLOC_DRM_GET_FORMAT:
231 case GRALLOC_DRM_GET_DIMENSIONS:
232 case GRALLOC_DRM_GET_BACKING_STORE:
233 break;
234 default:
235 return CROS_GRALLOC_ERROR_UNSUPPORTED;
236 }
237
238 va_start(args, op);
239
240 ret = CROS_GRALLOC_ERROR_NONE;
241 handle = va_arg(args, buffer_handle_t);
242 auto hnd = cros_gralloc_convert_handle(handle);
243 if (!hnd) {
244 cros_gralloc_error("Invalid handle.");
245 return CROS_GRALLOC_ERROR_BAD_HANDLE;
246 }
247
248 switch (op) {
249 case GRALLOC_DRM_GET_STRIDE:
250 out_stride = va_arg(args, uint32_t *);
251 *out_stride = hnd->pixel_stride;
252 break;
253 case GRALLOC_DRM_GET_FORMAT:
254 out_format = va_arg(args, int32_t *);
255 *out_format = hnd->droid_format;
256 break;
257 case GRALLOC_DRM_GET_DIMENSIONS:
258 out_width = va_arg(args, uint32_t *);
259 out_height = va_arg(args, uint32_t *);
260 *out_width = hnd->width;
261 *out_height = hnd->height;
262 break;
263 case GRALLOC_DRM_GET_BACKING_STORE:
264 out_store = va_arg(args, uint64_t *);
265 ret = mod->driver->get_backing_store(handle, out_store);
266 break;
267 default:
268 ret = CROS_GRALLOC_ERROR_UNSUPPORTED;
269 }
270
271 va_end(args);
272
273 return ret;
274}
275
276static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
277 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
278{
279 uint64_t flags;
280 int32_t fence, ret;
281 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
282 auto mod = (struct gralloc0_module *)module;
283
284 auto hnd = cros_gralloc_convert_handle(handle);
285 if (!hnd) {
286 cros_gralloc_error("Invalid handle.");
287 return CROS_GRALLOC_ERROR_BAD_HANDLE;
288 }
289
290 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
291 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12)) {
292 cros_gralloc_error("Non-YUV format not compatible.");
293 return CROS_GRALLOC_ERROR_BAD_HANDLE;
294 }
295
296 fence = -1;
297 flags = gralloc0_convert_flags(usage);
298 ret = mod->driver->lock(handle, fence, flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900299 if (ret)
300 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700301
302 switch (hnd->format) {
303 case DRM_FORMAT_NV12:
304 ycbcr->y = addr[0];
305 ycbcr->cb = addr[1];
306 ycbcr->cr = addr[1] + 1;
307 ycbcr->ystride = hnd->strides[0];
308 ycbcr->cstride = hnd->strides[1];
309 ycbcr->chroma_step = 2;
310 break;
311 case DRM_FORMAT_YVU420:
312 case DRM_FORMAT_YVU420_ANDROID:
313 ycbcr->y = addr[0];
314 ycbcr->cb = addr[2];
315 ycbcr->cr = addr[1];
316 ycbcr->ystride = hnd->strides[0];
317 ycbcr->cstride = hnd->strides[1];
318 ycbcr->chroma_step = 1;
319 break;
320 default:
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900321 mod->driver->unlock(handle);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700322 return CROS_GRALLOC_ERROR_UNSUPPORTED;
323 }
324
325 return ret;
326}
327
328static struct hw_module_methods_t gralloc0_module_methods = {.open = gralloc0_open };
329
330struct gralloc0_module HAL_MODULE_INFO_SYM = {
331 .base =
332 {
333 .common =
334 {
335 .tag = HARDWARE_MODULE_TAG,
336 .module_api_version = GRALLOC_MODULE_API_VERSION_0_2,
337 .hal_api_version = 0,
338 .id = GRALLOC_HARDWARE_MODULE_ID,
339 .name = "CrOS Gralloc",
340 .author = "Chrome OS",
341 .methods = &gralloc0_module_methods,
342 },
343
344 .registerBuffer = gralloc0_register_buffer,
345 .unregisterBuffer = gralloc0_unregister_buffer,
346 .lock = gralloc0_lock,
347 .unlock = gralloc0_unlock,
348 .perform = gralloc0_perform,
349 .lock_ycbcr = gralloc0_lock_ycbcr,
350 },
351
352 .alloc = nullptr,
353 .driver = nullptr,
354};