OSDN Git Service

Open the DRM device of the primary framebuffer (fb0)
[android-x86/external-gbm_gralloc.git] / gralloc_gbm.cpp
1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  * Copyright (C) 2016 Linaro, Ltd., Rob Herring <robh@kernel.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #define LOG_TAG "GRALLOC-GBM"
26
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
29 #include <cutils/properties.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <assert.h>
37 #include <xf86drm.h>
38
39 #include <hardware/gralloc.h>
40 #include <system/graphics.h>
41
42 #include <gbm.h>
43
44 #include "gralloc_gbm_priv.h"
45 #include <android/gralloc_handle.h>
46
47 #include <unordered_map>
48
49 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
50
51 #define unlikely(x) __builtin_expect(!!(x), 0)
52
53 static std::unordered_map<buffer_handle_t, struct gbm_bo *> gbm_bo_handle_map;
54
55 struct bo_data_t {
56         void *map_data;
57         int lock_count;
58         int locked_for;
59 };
60
61 void gralloc_gbm_destroy_user_data(struct gbm_bo *bo, void *data)
62 {
63         struct bo_data_t *bo_data = (struct bo_data_t *)data;
64         delete bo_data;
65
66         (void)bo;
67 }
68
69 static struct bo_data_t *gbm_bo_data(struct gbm_bo *bo) {
70         return (struct bo_data_t *)gbm_bo_get_user_data(bo);
71 }
72
73
74 static uint32_t get_gbm_format(int format)
75 {
76         uint32_t fmt;
77
78         switch (format) {
79         case HAL_PIXEL_FORMAT_RGBA_8888:
80                 fmt = GBM_FORMAT_ABGR8888;
81                 break;
82         case HAL_PIXEL_FORMAT_RGBX_8888:
83                 fmt = GBM_FORMAT_XBGR8888;
84                 break;
85         case HAL_PIXEL_FORMAT_RGB_888:
86                 fmt = GBM_FORMAT_RGB888;
87                 break;
88         case HAL_PIXEL_FORMAT_RGB_565:
89                 fmt = GBM_FORMAT_RGB565;
90                 break;
91         case HAL_PIXEL_FORMAT_BGRA_8888:
92                 fmt = GBM_FORMAT_ARGB8888;
93                 break;
94         case HAL_PIXEL_FORMAT_YV12:
95                 /* YV12 is planar, but must be a single buffer so ask for GR88 */
96                 fmt = GBM_FORMAT_GR88;
97                 break;
98         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
99         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
100         default:
101                 fmt = 0;
102                 break;
103         }
104
105         return fmt;
106 }
107
108 static int gralloc_gbm_get_bpp(int format)
109 {
110         int bpp;
111
112         switch (format) {
113         case HAL_PIXEL_FORMAT_RGBA_8888:
114         case HAL_PIXEL_FORMAT_RGBX_8888:
115         case HAL_PIXEL_FORMAT_BGRA_8888:
116                 bpp = 4;
117                 break;
118         case HAL_PIXEL_FORMAT_RGB_888:
119                 bpp = 3;
120                 break;
121         case HAL_PIXEL_FORMAT_RGB_565:
122         case HAL_PIXEL_FORMAT_YCbCr_422_I:
123                 bpp = 2;
124                 break;
125         /* planar; only Y is considered */
126         case HAL_PIXEL_FORMAT_YV12:
127         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
128         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
129                 bpp = 1;
130                 break;
131         default:
132                 bpp = 0;
133                 break;
134         }
135
136         return bpp;
137 }
138
139 static unsigned int get_pipe_bind(int usage)
140 {
141         unsigned int bind = 0;
142
143         if (usage & (GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN))
144                 bind |= GBM_BO_USE_LINEAR;
145         if (usage & GRALLOC_USAGE_CURSOR)
146                 ;//bind |= GBM_BO_USE_CURSOR;
147         if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
148                 bind |= GBM_BO_USE_RENDERING;
149         if (usage & GRALLOC_USAGE_HW_FB)
150                 bind |= GBM_BO_USE_SCANOUT;
151         if (usage & GRALLOC_USAGE_HW_COMPOSER)
152                 bind |= GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
153
154         return bind;
155 }
156
157 static struct gbm_bo *gbm_import(struct gbm_device *gbm,
158                 buffer_handle_t _handle)
159 {
160         struct gbm_bo *bo;
161         struct gralloc_handle_t *handle = gralloc_handle(_handle);
162         #ifdef GBM_BO_IMPORT_FD_MODIFIER
163         struct gbm_import_fd_modifier_data data;
164         #else
165         struct gbm_import_fd_data data;
166         #endif
167
168         int format = get_gbm_format(handle->format);
169         if (handle->prime_fd < 0)
170                 return NULL;
171
172         memset(&data, 0, sizeof(data));
173         data.width = handle->width;
174         data.height = handle->height;
175         data.format = format;
176         /* Adjust the width and height for a GBM GR88 buffer */
177         if (handle->format == HAL_PIXEL_FORMAT_YV12) {
178                 data.width /= 2;
179                 data.height += handle->height / 2;
180         }
181
182         #ifdef GBM_BO_IMPORT_FD_MODIFIER
183         data.num_fds = 1;
184         data.fds[0] = handle->prime_fd;
185         data.strides[0] = handle->stride;
186         data.modifier = handle->modifier;
187         bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD_MODIFIER, &data, 0);
188         #else
189         data.fd = handle->prime_fd;
190         data.stride = handle->stride;
191         bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD, &data, 0);
192         #endif
193
194         return bo;
195 }
196
197 static struct gbm_bo *gbm_alloc(struct gbm_device *gbm,
198                 buffer_handle_t _handle)
199 {
200         struct gbm_bo *bo;
201         struct gralloc_handle_t *handle = gralloc_handle(_handle);
202         int format = get_gbm_format(handle->format);
203         int usage = get_pipe_bind(handle->usage);
204         int width, height;
205
206         width = handle->width;
207         height = handle->height;
208         if (usage & GBM_BO_USE_CURSOR) {
209                 if (handle->width < 64)
210                         width = 64;
211                 if (handle->height < 64)
212                         height = 64;
213         }
214
215         /*
216          * For YV12, we request GR88, so halve the width since we're getting
217          * 16bpp. Then increase the height by 1.5 for the U and V planes.
218          */
219         if (handle->format == HAL_PIXEL_FORMAT_YV12) {
220                 width /= 2;
221                 height += handle->height / 2;
222         }
223
224         ALOGV("create BO, size=%dx%d, fmt=%d, usage=%x",
225               handle->width, handle->height, handle->format, usage);
226         bo = gbm_bo_create(gbm, width, height, format, usage);
227         if (!bo) {
228                 ALOGE("failed to create BO, size=%dx%d, fmt=%d, usage=%x",
229                       handle->width, handle->height, handle->format, usage);
230                 return NULL;
231         }
232
233         handle->prime_fd = gbm_bo_get_fd(bo);
234         handle->stride = gbm_bo_get_stride(bo);
235         #ifdef GBM_BO_IMPORT_FD_MODIFIER
236         handle->modifier = gbm_bo_get_modifier(bo);
237         #endif
238
239         return bo;
240 }
241
242 int gbm_free(buffer_handle_t handle)
243 {
244         struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
245
246         if (!bo)
247                 return -EINVAL;
248
249         gbm_bo_handle_map.erase(handle);
250         gbm_bo_destroy(bo);
251         return 0;
252 }
253
254 /*
255  * Return the bo of a registered handle.
256  */
257 struct gbm_bo *gralloc_gbm_bo_from_handle(buffer_handle_t handle)
258 {
259         return gbm_bo_handle_map[handle];
260 }
261
262 static int gbm_map(buffer_handle_t handle, int x, int y, int w, int h,
263                 int enable_write, void **addr)
264 {
265         int err = 0;
266         int flags = GBM_BO_TRANSFER_READ;
267         struct gralloc_gbm_handle_t *gbm_handle = gralloc_handle(handle);
268         struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
269         struct bo_data_t *bo_data = gbm_bo_data(bo);
270         uint32_t stride;
271
272         if (bo_data->map_data)
273                 return -EINVAL;
274
275         if (gbm_handle->format == HAL_PIXEL_FORMAT_YV12) {
276                 if (x || y)
277                         ALOGE("can't map with offset for planar %p", bo);
278                 w /= 2;
279                 h += h / 2;
280         }
281
282         if (enable_write)
283                 flags |= GBM_BO_TRANSFER_WRITE;
284
285         *addr = gbm_bo_map(bo, 0, 0, x + w, y + h, flags, &stride, &bo_data->map_data);
286         ALOGV("mapped bo %p (%d, %d)-(%d, %d) at %p", bo, x, y, w, h, *addr);
287         if (*addr == NULL)
288                 return -ENOMEM;
289
290         assert(stride == gbm_bo_get_stride(bo));
291
292         return err;
293 }
294
295 static void gbm_unmap(struct gbm_bo *bo)
296 {
297         struct bo_data_t *bo_data = gbm_bo_data(bo);
298
299         gbm_bo_unmap(bo, bo_data->map_data);
300         bo_data->map_data = NULL;
301 }
302
303 void gbm_dev_destroy(struct gbm_device *gbm)
304 {
305         int fd = gbm_device_get_fd(gbm);
306
307         gbm_device_destroy(gbm);
308         close(fd);
309 }
310
311 struct gbm_device *gbm_dev_create(bool master)
312 {
313         struct gbm_device *gbm;
314         char path[PROPERTY_VALUE_MAX];
315         int fd;
316
317         if (property_get("gralloc.gbm.device", path, NULL) > 0)
318                 fd = open(path, O_RDWR | O_CLOEXEC);
319         else
320                 fd = drmOpenByFB(0, master ? DRM_NODE_PRIMARY : DRM_NODE_RENDER);
321
322         if (fd < 0) {
323                 ALOGE("failed to open %s", path);
324                 return NULL;
325         }
326
327         gbm = gbm_create_device(fd);
328         if (!gbm) {
329                 ALOGE("failed to create gbm device");
330                 close(fd);
331         }
332
333         return gbm;
334 }
335
336 /*
337  * Register a buffer handle.
338  */
339 int gralloc_gbm_handle_register(buffer_handle_t _handle, struct gbm_device *gbm)
340 {
341         struct gbm_bo *bo;
342
343         if (!_handle)
344                 return -EINVAL;
345
346         if (gbm_bo_handle_map.count(_handle))
347                 return -EINVAL;
348
349         bo = gbm_import(gbm, _handle);
350         if (!bo)
351                 return -EINVAL;
352
353         gbm_bo_handle_map.emplace(_handle, bo);
354
355         return 0;
356 }
357
358 /*
359  * Unregister a buffer handle.  It is no-op for handles created locally.
360  */
361 int gralloc_gbm_handle_unregister(buffer_handle_t handle)
362 {
363         return gbm_free(handle);
364 }
365
366 /*
367  * Create a bo.
368  */
369 buffer_handle_t gralloc_gbm_bo_create(struct gbm_device *gbm,
370                 int width, int height, int format, int usage, int *stride)
371 {
372         struct gbm_bo *bo;
373         native_handle_t *handle;
374
375         handle = gralloc_handle_create(width, height, format, usage);
376         if (!handle)
377                 return NULL;
378
379         bo = gbm_alloc(gbm, handle);
380         if (!bo) {
381                 native_handle_delete(handle);
382                 return NULL;
383         }
384
385         gbm_bo_handle_map.emplace(handle, bo);
386
387         /* in pixels */
388         *stride = gralloc_handle(handle)->stride / gralloc_gbm_get_bpp(format);
389
390         return handle;
391 }
392
393 /*
394  * Lock a bo.  XXX thread-safety?
395  */
396 int gralloc_gbm_bo_lock(buffer_handle_t handle,
397                 int usage, int x, int y, int w, int h,
398                 void **addr)
399 {
400         struct gralloc_handle_t *gbm_handle = gralloc_handle(handle);
401         struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
402         struct bo_data_t *bo_data;
403
404         if (!bo)
405                 return -EINVAL;
406
407         if ((gbm_handle->usage & usage) != (uint32_t)usage) {
408                 /* make FB special for testing software renderer with */
409
410                 if (!(gbm_handle->usage & GRALLOC_USAGE_SW_READ_OFTEN) &&
411                                 !(gbm_handle->usage & GRALLOC_USAGE_HW_FB) &&
412                                 !(gbm_handle->usage & GRALLOC_USAGE_HW_TEXTURE)) {
413                         ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_FB or GRALLOC_USAGE_HW_TEXTURE",
414                                 gbm_handle->usage, usage);
415                         return -EINVAL;
416                 }
417         }
418
419         bo_data = gbm_bo_data(bo);
420         if (!bo_data) {
421                 bo_data = new struct bo_data_t();
422                 gbm_bo_set_user_data(bo, bo_data, gralloc_gbm_destroy_user_data);
423         }
424
425         ALOGI("lock bo %p, cnt=%d, usage=%x", bo, bo_data->lock_count, usage);
426
427         /* allow multiple locks with compatible usages */
428         if (bo_data->lock_count && (bo_data->locked_for & usage) != usage)
429                 return -EINVAL;
430
431         usage |= bo_data->locked_for;
432
433         if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
434                      GRALLOC_USAGE_SW_READ_MASK)) {
435                 /* the driver is supposed to wait for the bo */
436                 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
437                 int err = gbm_map(handle, x, y, w, h, write, addr);
438                 if (err)
439                         return err;
440         }
441         else {
442                 /* kernel handles the synchronization here */
443         }
444
445         bo_data->lock_count++;
446         bo_data->locked_for |= usage;
447
448         return 0;
449 }
450
451 /*
452  * Unlock a bo.
453  */
454 int gralloc_gbm_bo_unlock(buffer_handle_t handle)
455 {
456         struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
457         struct bo_data_t *bo_data;
458         if (!bo)
459                 return -EINVAL;
460
461         bo_data = gbm_bo_data(bo);
462
463         int mapped = bo_data->locked_for &
464                 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
465
466         if (!bo_data->lock_count)
467                 return 0;
468
469         if (mapped)
470                 gbm_unmap(bo);
471
472         bo_data->lock_count--;
473         if (!bo_data->lock_count)
474                 bo_data->locked_for = 0;
475
476         return 0;
477 }