OSDN Git Service

minigbm: add the BO_USE_RENDERSCRIPT flag back in
[android-x86/external-minigbm.git] / helpers.c
1 /*
2  * Copyright 2014 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 <assert.h>
8 #include <errno.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <xf86drm.h>
15 #include <xf86drmMode.h>
16
17 #include "drv_priv.h"
18 #include "helpers.h"
19 #include "util.h"
20
21 static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
22 {
23
24         if (plane != 0) {
25                 switch (format) {
26                 case DRM_FORMAT_YVU420:
27                 case DRM_FORMAT_YVU420_ANDROID:
28                         stride = DIV_ROUND_UP(stride, 2);
29                         break;
30                 }
31         }
32
33         return stride;
34 }
35
36 static uint32_t bpp_from_format(uint32_t format, size_t plane)
37 {
38         assert(plane < drv_num_planes_from_format(format));
39
40         switch (format) {
41         case DRM_FORMAT_BGR233:
42         case DRM_FORMAT_C8:
43         case DRM_FORMAT_R8:
44         case DRM_FORMAT_RGB332:
45         case DRM_FORMAT_YVU420:
46         case DRM_FORMAT_YVU420_ANDROID:
47                 return 8;
48
49         case DRM_FORMAT_NV12:
50         case DRM_FORMAT_NV21:
51                 return (plane == 0) ? 8 : 4;
52
53         case DRM_FORMAT_ABGR1555:
54         case DRM_FORMAT_ABGR4444:
55         case DRM_FORMAT_ARGB1555:
56         case DRM_FORMAT_ARGB4444:
57         case DRM_FORMAT_BGR565:
58         case DRM_FORMAT_BGRA4444:
59         case DRM_FORMAT_BGRA5551:
60         case DRM_FORMAT_BGRX4444:
61         case DRM_FORMAT_BGRX5551:
62         case DRM_FORMAT_GR88:
63         case DRM_FORMAT_RG88:
64         case DRM_FORMAT_RGB565:
65         case DRM_FORMAT_RGBA4444:
66         case DRM_FORMAT_RGBA5551:
67         case DRM_FORMAT_RGBX4444:
68         case DRM_FORMAT_RGBX5551:
69         case DRM_FORMAT_UYVY:
70         case DRM_FORMAT_VYUY:
71         case DRM_FORMAT_XBGR1555:
72         case DRM_FORMAT_XBGR4444:
73         case DRM_FORMAT_XRGB1555:
74         case DRM_FORMAT_XRGB4444:
75         case DRM_FORMAT_YUYV:
76         case DRM_FORMAT_YVYU:
77                 return 16;
78
79         case DRM_FORMAT_BGR888:
80         case DRM_FORMAT_RGB888:
81                 return 24;
82
83         case DRM_FORMAT_ABGR2101010:
84         case DRM_FORMAT_ABGR8888:
85         case DRM_FORMAT_ARGB2101010:
86         case DRM_FORMAT_ARGB8888:
87         case DRM_FORMAT_AYUV:
88         case DRM_FORMAT_BGRA1010102:
89         case DRM_FORMAT_BGRA8888:
90         case DRM_FORMAT_BGRX1010102:
91         case DRM_FORMAT_BGRX8888:
92         case DRM_FORMAT_RGBA1010102:
93         case DRM_FORMAT_RGBA8888:
94         case DRM_FORMAT_RGBX1010102:
95         case DRM_FORMAT_RGBX8888:
96         case DRM_FORMAT_XBGR2101010:
97         case DRM_FORMAT_XBGR8888:
98         case DRM_FORMAT_XRGB2101010:
99         case DRM_FORMAT_XRGB8888:
100                 return 32;
101         }
102
103         fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
104         return 0;
105 }
106
107 uint32_t drv_bo_get_stride_in_pixels(struct bo *bo)
108 {
109         uint32_t bytes_per_pixel = DIV_ROUND_UP(bpp_from_format(bo->format, 0), 8);
110         return DIV_ROUND_UP(bo->strides[0], bytes_per_pixel);
111 }
112
113 /*
114  * This function returns the stride for a given format, width and plane.
115  */
116 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
117 {
118         uint32_t stride = DIV_ROUND_UP(width * bpp_from_format(format, plane), 8);
119
120         /*
121          * The stride of Android YV12 buffers is required to be aligned to 16 bytes
122          * (see <system/graphics.h>).
123          */
124         if (format == DRM_FORMAT_YVU420_ANDROID)
125                 stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
126
127         return stride;
128 }
129
130 /*
131  * This function fills in the buffer object given the driver aligned stride of
132  * the first plane, height and a format. This function assumes there is just
133  * one kernel buffer per buffer object.
134  */
135 int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
136 {
137
138         size_t p, num_planes;
139         uint32_t offset = 0;
140
141         num_planes = drv_num_planes_from_format(format);
142         assert(num_planes);
143
144         /*
145          * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
146          *  - the aligned height is same as the buffer's height.
147          *  - the chroma stride is 16 bytes aligned, i.e., the luma's strides
148          *    is 32 bytes aligned.
149          */
150         if (format == DRM_FORMAT_YVU420_ANDROID) {
151                 assert(aligned_height == bo->height);
152                 assert(stride == ALIGN(stride, 32));
153         }
154
155         for (p = 0; p < num_planes; p++) {
156                 bo->strides[p] = subsample_stride(stride, format, p);
157                 bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
158                 bo->offsets[p] = offset;
159                 offset += bo->sizes[p];
160         }
161
162         bo->total_size = offset;
163         return 0;
164 }
165
166 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
167                        uint32_t flags)
168 {
169         int ret;
170         size_t plane;
171         uint32_t aligned_width, aligned_height;
172         struct drm_mode_create_dumb create_dumb;
173
174         aligned_width = width;
175         aligned_height = height;
176         if (format == DRM_FORMAT_YVU420_ANDROID) {
177                 /*
178                  * Align width to 32 pixels, so chroma strides are 16 bytes as
179                  * Android requires.
180                  */
181                 aligned_width = ALIGN(width, 32);
182         }
183
184         if (format == DRM_FORMAT_YVU420_ANDROID || format == DRM_FORMAT_YVU420) {
185                 aligned_height = 3 * DIV_ROUND_UP(height, 2);
186         }
187
188         memset(&create_dumb, 0, sizeof(create_dumb));
189         create_dumb.height = aligned_height;
190         create_dumb.width = aligned_width;
191         create_dumb.bpp = bpp_from_format(format, 0);
192         create_dumb.flags = 0;
193
194         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
195         if (ret) {
196                 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
197                 return ret;
198         }
199
200         drv_bo_from_format(bo, create_dumb.pitch, height, format);
201
202         for (plane = 0; plane < bo->num_planes; plane++)
203                 bo->handles[plane].u32 = create_dumb.handle;
204
205         bo->total_size = create_dumb.size;
206         return 0;
207 }
208
209 int drv_dumb_bo_destroy(struct bo *bo)
210 {
211         struct drm_mode_destroy_dumb destroy_dumb;
212         int ret;
213
214         memset(&destroy_dumb, 0, sizeof(destroy_dumb));
215         destroy_dumb.handle = bo->handles[0].u32;
216
217         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
218         if (ret) {
219                 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n",
220                         bo->handles[0].u32);
221                 return ret;
222         }
223
224         return 0;
225 }
226
227 int drv_gem_bo_destroy(struct bo *bo)
228 {
229         struct drm_gem_close gem_close;
230         int ret, error = 0;
231         size_t plane, i;
232
233         for (plane = 0; plane < bo->num_planes; plane++) {
234                 for (i = 0; i < plane; i++)
235                         if (bo->handles[i].u32 == bo->handles[plane].u32)
236                                 break;
237                 /* Make sure close hasn't already been called on this handle */
238                 if (i != plane)
239                         continue;
240
241                 memset(&gem_close, 0, sizeof(gem_close));
242                 gem_close.handle = bo->handles[plane].u32;
243
244                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
245                 if (ret) {
246                         fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
247                                 bo->handles[plane].u32, ret);
248                         error = ret;
249                 }
250         }
251
252         return error;
253 }
254
255 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
256 {
257         int ret;
258         size_t plane;
259         struct drm_prime_handle prime_handle;
260
261         for (plane = 0; plane < bo->num_planes; plane++) {
262                 memset(&prime_handle, 0, sizeof(prime_handle));
263                 prime_handle.fd = data->fds[plane];
264
265                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
266
267                 if (ret) {
268                         fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n",
269                                 prime_handle.fd);
270
271                         /*
272                          * Need to call GEM close on planes that were opened,
273                          * if any. Adjust the num_planes variable to be the
274                          * plane that failed, so GEM close will be called on
275                          * planes before that plane.
276                          */
277                         bo->num_planes = plane;
278                         drv_gem_bo_destroy(bo);
279                         return ret;
280                 }
281
282                 bo->handles[plane].u32 = prime_handle.handle;
283         }
284
285         for (plane = 0; plane < bo->num_planes; plane++) {
286                 pthread_mutex_lock(&bo->drv->driver_lock);
287                 drv_increment_reference_count(bo->drv, bo, plane);
288                 pthread_mutex_unlock(&bo->drv->driver_lock);
289         }
290
291         return 0;
292 }
293
294 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
295 {
296         int ret;
297         size_t i;
298         struct drm_mode_map_dumb map_dumb;
299
300         memset(&map_dumb, 0, sizeof(map_dumb));
301         map_dumb.handle = bo->handles[plane].u32;
302
303         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
304         if (ret) {
305                 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
306                 return MAP_FAILED;
307         }
308
309         for (i = 0; i < bo->num_planes; i++)
310                 if (bo->handles[i].u32 == bo->handles[plane].u32)
311                         data->length += bo->sizes[i];
312
313         return mmap(0, data->length, prot, MAP_SHARED, bo->drv->fd, map_dumb.offset);
314 }
315
316 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
317 {
318         void *count;
319         uintptr_t num = 0;
320
321         if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
322                 num = (uintptr_t)(count);
323
324         return num;
325 }
326
327 void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
328 {
329         uintptr_t num = drv_get_reference_count(drv, bo, plane);
330
331         /* If a value isn't in the table, drmHashDelete is a no-op */
332         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
333         drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
334 }
335
336 void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
337 {
338         uintptr_t num = drv_get_reference_count(drv, bo, plane);
339
340         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
341
342         if (num > 0)
343                 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
344 }
345
346 uint32_t drv_log_base2(uint32_t value)
347 {
348         int ret = 0;
349
350         while (value >>= 1)
351                 ++ret;
352
353         return ret;
354 }
355
356 int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
357                         uint64_t usage)
358 {
359         struct combinations *combos = &drv->backend->combos;
360         if (combos->size >= combos->allocations) {
361                 struct combination *new_data;
362                 combos->allocations *= 2;
363                 new_data = realloc(combos->data, combos->allocations * sizeof(*combos->data));
364                 if (!new_data)
365                         return -ENOMEM;
366
367                 combos->data = new_data;
368         }
369
370         combos->data[combos->size].format = format;
371         combos->data[combos->size].metadata.priority = metadata->priority;
372         combos->data[combos->size].metadata.tiling = metadata->tiling;
373         combos->data[combos->size].metadata.modifier = metadata->modifier;
374         combos->data[combos->size].usage = usage;
375         combos->size++;
376         return 0;
377 }
378
379 int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
380                          struct format_metadata *metadata, uint64_t usage)
381 {
382         int ret;
383         uint32_t i;
384         for (i = 0; i < num_formats; i++) {
385                 ret = drv_add_combination(drv, formats[i], metadata, usage);
386                 if (ret)
387                         return ret;
388         }
389
390         return 0;
391 }
392
393 void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
394                             uint64_t usage)
395 {
396         uint32_t i;
397         struct combination *combo;
398         /* Attempts to add the specified usage to an existing combination. */
399         for (i = 0; i < drv->backend->combos.size; i++) {
400                 combo = &drv->backend->combos.data[i];
401                 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
402                     combo->metadata.modifier == metadata->modifier)
403                         combo->usage |= usage;
404         }
405 }
406
407 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
408 {
409         uint64_t flag, usage;
410         struct kms_item *items;
411         uint32_t i, j, k, allocations, item_size;
412
413         drmModePlanePtr plane;
414         drmModePropertyPtr prop;
415         drmModePlaneResPtr resources;
416         drmModeObjectPropertiesPtr props;
417
418         /* Start with a power of 2 number of allocations. */
419         allocations = 2;
420         item_size = 0;
421         items = calloc(allocations, sizeof(*items));
422         if (!items)
423                 goto out;
424
425         /*
426          * The ability to return universal planes is only complete on
427          * ChromeOS kernel versions >= v3.18.  The SET_CLIENT_CAP ioctl
428          * therefore might return an error code, so don't check it.  If it
429          * fails, it'll just return the plane list as overlay planes, which is
430          * fine in our case (our drivers already have cursor bits set).
431          * modetest in libdrm does the same thing.
432          */
433         drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
434
435         resources = drmModeGetPlaneResources(drv->fd);
436         if (!resources)
437                 goto out;
438
439         for (i = 0; i < resources->count_planes; i++) {
440                 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
441                 if (!plane)
442                         goto out;
443
444                 props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
445                 if (!props)
446                         goto out;
447
448                 for (j = 0; j < props->count_props; j++) {
449                         prop = drmModeGetProperty(drv->fd, props->props[j]);
450                         if (prop) {
451                                 if (strcmp(prop->name, "type") == 0) {
452                                         flag = props->prop_values[j];
453                                 }
454
455                                 drmModeFreeProperty(prop);
456                         }
457                 }
458
459                 switch (flag) {
460                 case DRM_PLANE_TYPE_OVERLAY:
461                 case DRM_PLANE_TYPE_PRIMARY:
462                         usage = BO_USE_SCANOUT;
463                         break;
464                 case DRM_PLANE_TYPE_CURSOR:
465                         usage = BO_USE_CURSOR;
466                         break;
467                 default:
468                         assert(0);
469                 }
470
471                 for (j = 0; j < plane->count_formats; j++) {
472                         bool found = false;
473                         for (k = 0; k < item_size; k++) {
474                                 if (items[k].format == plane->formats[j] &&
475                                     items[k].modifier == DRM_FORMAT_MOD_NONE) {
476                                         items[k].usage |= usage;
477                                         found = true;
478                                         break;
479                                 }
480                         }
481
482                         if (!found && item_size >= allocations) {
483                                 struct kms_item *new_data = NULL;
484                                 allocations *= 2;
485                                 new_data = realloc(items, allocations * sizeof(*items));
486                                 if (!new_data) {
487                                         item_size = 0;
488                                         goto out;
489                                 }
490
491                                 items = new_data;
492                         }
493
494                         if (!found) {
495                                 items[item_size].format = plane->formats[j];
496                                 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
497                                 items[item_size].usage = usage;
498                                 item_size++;
499                         }
500                 }
501
502                 drmModeFreeObjectProperties(props);
503                 drmModeFreePlane(plane);
504         }
505
506         drmModeFreePlaneResources(resources);
507 out:
508         if (items && item_size == 0) {
509                 free(items);
510                 items = NULL;
511         }
512
513         *num_items = item_size;
514         return items;
515 }
516
517 int drv_modify_linear_combinations(struct driver *drv)
518 {
519         uint32_t i, j, num_items;
520         struct kms_item *items;
521         struct combination *combo;
522
523         /*
524          * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
525          * plane and as a cursor. Some drivers don't support
526          * drmModeGetPlaneResources, so add the combination here. Note that the
527          * kernel disregards the alpha component of ARGB unless it's an overlay
528          * plane.
529          */
530         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
531                                BO_USE_CURSOR | BO_USE_SCANOUT);
532         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
533                                BO_USE_CURSOR | BO_USE_SCANOUT);
534
535         items = drv_query_kms(drv, &num_items);
536         if (!items || !num_items)
537                 return 0;
538
539         for (i = 0; i < num_items; i++) {
540                 for (j = 0; j < drv->backend->combos.size; j++) {
541                         combo = &drv->backend->combos.data[j];
542                         if (items[i].format == combo->format)
543                                 combo->usage |= BO_USE_SCANOUT;
544                 }
545         }
546
547         free(items);
548         return 0;
549 }