OSDN Git Service

463c1b3ec52392586e66b1cdffb0169f129912de
[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 struct planar_layout {
22         size_t num_planes;
23         int horizontal_subsampling[DRV_MAX_PLANES];
24         int vertical_subsampling[DRV_MAX_PLANES];
25         int bytes_per_pixel[DRV_MAX_PLANES];
26 };
27
28 // clang-format off
29
30 static const struct planar_layout packed_1bpp_layout = {
31         .num_planes = 1,
32         .horizontal_subsampling = { 1 },
33         .vertical_subsampling = { 1 },
34         .bytes_per_pixel = { 1 }
35 };
36
37 static const struct planar_layout packed_2bpp_layout = {
38         .num_planes = 1,
39         .horizontal_subsampling = { 1 },
40         .vertical_subsampling = { 1 },
41         .bytes_per_pixel = { 2 }
42 };
43
44 static const struct planar_layout packed_3bpp_layout = {
45         .num_planes = 1,
46         .horizontal_subsampling = { 1 },
47         .vertical_subsampling = { 1 },
48         .bytes_per_pixel = { 3 }
49 };
50
51 static const struct planar_layout packed_4bpp_layout = {
52         .num_planes = 1,
53         .horizontal_subsampling = { 1 },
54         .vertical_subsampling = { 1 },
55         .bytes_per_pixel = { 4 }
56 };
57
58 static const struct planar_layout biplanar_yuv_420_layout = {
59         .num_planes = 2,
60         .horizontal_subsampling = { 1, 2 },
61         .vertical_subsampling = { 1, 2 },
62         .bytes_per_pixel = { 1, 2 }
63 };
64
65 static const struct planar_layout triplanar_yuv_420_layout = {
66         .num_planes = 3,
67         .horizontal_subsampling = { 1, 2, 2 },
68         .vertical_subsampling = { 1, 2, 2 },
69         .bytes_per_pixel = { 1, 1, 1 }
70 };
71
72 // clang-format on
73
74 static const struct planar_layout *layout_from_format(uint32_t format)
75 {
76         switch (format) {
77         case DRM_FORMAT_BGR233:
78         case DRM_FORMAT_C8:
79         case DRM_FORMAT_R8:
80         case DRM_FORMAT_RGB332:
81                 return &packed_1bpp_layout;
82
83         case DRM_FORMAT_YVU420:
84         case DRM_FORMAT_YVU420_ANDROID:
85                 return &triplanar_yuv_420_layout;
86
87         case DRM_FORMAT_NV12:
88         case DRM_FORMAT_NV21:
89                 return &biplanar_yuv_420_layout;
90
91         case DRM_FORMAT_ABGR1555:
92         case DRM_FORMAT_ABGR4444:
93         case DRM_FORMAT_ARGB1555:
94         case DRM_FORMAT_ARGB4444:
95         case DRM_FORMAT_BGR565:
96         case DRM_FORMAT_BGRA4444:
97         case DRM_FORMAT_BGRA5551:
98         case DRM_FORMAT_BGRX4444:
99         case DRM_FORMAT_BGRX5551:
100         case DRM_FORMAT_GR88:
101         case DRM_FORMAT_RG88:
102         case DRM_FORMAT_RGB565:
103         case DRM_FORMAT_RGBA4444:
104         case DRM_FORMAT_RGBA5551:
105         case DRM_FORMAT_RGBX4444:
106         case DRM_FORMAT_RGBX5551:
107         case DRM_FORMAT_UYVY:
108         case DRM_FORMAT_VYUY:
109         case DRM_FORMAT_XBGR1555:
110         case DRM_FORMAT_XBGR4444:
111         case DRM_FORMAT_XRGB1555:
112         case DRM_FORMAT_XRGB4444:
113         case DRM_FORMAT_YUYV:
114         case DRM_FORMAT_YVYU:
115                 return &packed_2bpp_layout;
116
117         case DRM_FORMAT_BGR888:
118         case DRM_FORMAT_RGB888:
119                 return &packed_3bpp_layout;
120
121         case DRM_FORMAT_ABGR2101010:
122         case DRM_FORMAT_ABGR8888:
123         case DRM_FORMAT_ARGB2101010:
124         case DRM_FORMAT_ARGB8888:
125         case DRM_FORMAT_AYUV:
126         case DRM_FORMAT_BGRA1010102:
127         case DRM_FORMAT_BGRA8888:
128         case DRM_FORMAT_BGRX1010102:
129         case DRM_FORMAT_BGRX8888:
130         case DRM_FORMAT_RGBA1010102:
131         case DRM_FORMAT_RGBA8888:
132         case DRM_FORMAT_RGBX1010102:
133         case DRM_FORMAT_RGBX8888:
134         case DRM_FORMAT_XBGR2101010:
135         case DRM_FORMAT_XBGR8888:
136         case DRM_FORMAT_XRGB2101010:
137         case DRM_FORMAT_XRGB8888:
138                 return &packed_4bpp_layout;
139
140         default:
141                 drv_log("UNKNOWN FORMAT %d\n", format);
142                 return NULL;
143         }
144 }
145
146 size_t drv_num_planes_from_format(uint32_t format)
147 {
148         const struct planar_layout *layout = layout_from_format(format);
149
150         /*
151          * drv_bo_new calls this function early to query number of planes and
152          * considers 0 planes to mean unknown format, so we have to support
153          * that.  All other layout_from_format() queries can assume that the
154          * format is supported and that the return value is non-NULL.
155          */
156
157         return layout ? layout->num_planes : 0;
158 }
159
160 uint32_t drv_height_from_format(uint32_t format, uint32_t height, size_t plane)
161 {
162         const struct planar_layout *layout = layout_from_format(format);
163
164         assert(plane < layout->num_planes);
165
166         return DIV_ROUND_UP(height, layout->vertical_subsampling[plane]);
167 }
168
169 uint32_t drv_bytes_per_pixel_from_format(uint32_t format, size_t plane)
170 {
171         const struct planar_layout *layout = layout_from_format(format);
172
173         assert(plane < layout->num_planes);
174
175         return layout->bytes_per_pixel[plane];
176 }
177
178 /*
179  * This function returns the stride for a given format, width and plane.
180  */
181 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
182 {
183         const struct planar_layout *layout = layout_from_format(format);
184         assert(plane < layout->num_planes);
185
186         uint32_t plane_width = DIV_ROUND_UP(width, layout->horizontal_subsampling[plane]);
187         uint32_t stride = plane_width * layout->bytes_per_pixel[plane];
188
189         /*
190          * The stride of Android YV12 buffers is required to be aligned to 16 bytes
191          * (see <system/graphics.h>).
192          */
193         if (format == DRM_FORMAT_YVU420_ANDROID)
194                 stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
195
196         return stride;
197 }
198
199 uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
200 {
201         return stride * drv_height_from_format(format, height, plane);
202 }
203
204 static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
205 {
206         if (plane != 0) {
207                 switch (format) {
208                 case DRM_FORMAT_YVU420:
209                 case DRM_FORMAT_YVU420_ANDROID:
210                         stride = DIV_ROUND_UP(stride, 2);
211                         break;
212                 }
213         }
214
215         return stride;
216 }
217
218 /*
219  * This function fills in the buffer object given the driver aligned stride of
220  * the first plane, height and a format. This function assumes there is just
221  * one kernel buffer per buffer object.
222  */
223 int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
224 {
225
226         size_t p, num_planes;
227         uint32_t offset = 0;
228
229         num_planes = drv_num_planes_from_format(format);
230         assert(num_planes);
231
232         /*
233          * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
234          *  - the aligned height is same as the buffer's height.
235          *  - the chroma stride is 16 bytes aligned, i.e., the luma's strides
236          *    is 32 bytes aligned.
237          */
238         if (format == DRM_FORMAT_YVU420_ANDROID) {
239                 assert(aligned_height == bo->height);
240                 assert(stride == ALIGN(stride, 32));
241         }
242
243         for (p = 0; p < num_planes; p++) {
244                 bo->strides[p] = subsample_stride(stride, format, p);
245                 bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
246                 bo->offsets[p] = offset;
247                 offset += bo->sizes[p];
248         }
249
250         bo->total_size = offset;
251         return 0;
252 }
253
254 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
255                        uint64_t use_flags)
256 {
257         int ret;
258         size_t plane;
259         uint32_t aligned_width, aligned_height;
260         struct drm_mode_create_dumb create_dumb;
261
262         aligned_width = width;
263         aligned_height = height;
264         switch (format) {
265         case DRM_FORMAT_YVU420_ANDROID:
266                 /* Align width to 32 pixels, so chroma strides are 16 bytes as
267                  * Android requires. */
268                 aligned_width = ALIGN(width, 32);
269                 /* Adjust the height to include room for chroma planes */
270                 aligned_height = 3 * DIV_ROUND_UP(height, 2);
271                 break;
272         case DRM_FORMAT_YVU420:
273         case DRM_FORMAT_NV12:
274                 /* Adjust the height to include room for chroma planes */
275                 aligned_height = 3 * DIV_ROUND_UP(height, 2);
276                 break;
277         default:
278                 break;
279         }
280
281         memset(&create_dumb, 0, sizeof(create_dumb));
282         create_dumb.height = aligned_height;
283         create_dumb.width = aligned_width;
284         create_dumb.bpp = layout_from_format(format)->bytes_per_pixel[0] * 8;
285         create_dumb.flags = 0;
286
287         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
288         if (ret) {
289                 drv_log("DRM_IOCTL_MODE_CREATE_DUMB failed (%d, %d)\n", bo->drv->fd, errno);
290                 return ret;
291         }
292
293         drv_bo_from_format(bo, create_dumb.pitch, height, format);
294
295         for (plane = 0; plane < bo->num_planes; plane++)
296                 bo->handles[plane].u32 = create_dumb.handle;
297
298         bo->total_size = create_dumb.size;
299         return 0;
300 }
301
302 int drv_dumb_bo_destroy(struct bo *bo)
303 {
304         struct drm_mode_destroy_dumb destroy_dumb;
305         int ret;
306
307         memset(&destroy_dumb, 0, sizeof(destroy_dumb));
308         destroy_dumb.handle = bo->handles[0].u32;
309
310         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
311         if (ret) {
312                 drv_log("DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n", bo->handles[0].u32);
313                 return ret;
314         }
315
316         return 0;
317 }
318
319 int drv_gem_bo_destroy(struct bo *bo)
320 {
321         struct drm_gem_close gem_close;
322         int ret, error = 0;
323         size_t plane, i;
324
325         for (plane = 0; plane < bo->num_planes; plane++) {
326                 for (i = 0; i < plane; i++)
327                         if (bo->handles[i].u32 == bo->handles[plane].u32)
328                                 break;
329                 /* Make sure close hasn't already been called on this handle */
330                 if (i != plane)
331                         continue;
332
333                 memset(&gem_close, 0, sizeof(gem_close));
334                 gem_close.handle = bo->handles[plane].u32;
335
336                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
337                 if (ret) {
338                         drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
339                                 bo->handles[plane].u32, ret);
340                         error = ret;
341                 }
342         }
343
344         return error;
345 }
346
347 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
348 {
349         int ret;
350         size_t plane;
351         struct drm_prime_handle prime_handle;
352
353         for (plane = 0; plane < bo->num_planes; plane++) {
354                 memset(&prime_handle, 0, sizeof(prime_handle));
355                 prime_handle.fd = data->fds[plane];
356
357                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
358
359                 if (ret) {
360                         drv_log("DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n", prime_handle.fd);
361
362                         /*
363                          * Need to call GEM close on planes that were opened,
364                          * if any. Adjust the num_planes variable to be the
365                          * plane that failed, so GEM close will be called on
366                          * planes before that plane.
367                          */
368                         bo->num_planes = plane;
369                         drv_gem_bo_destroy(bo);
370                         return ret;
371                 }
372
373                 bo->handles[plane].u32 = prime_handle.handle;
374         }
375
376         return 0;
377 }
378
379 void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
380 {
381         int ret;
382         size_t i;
383         struct drm_mode_map_dumb map_dumb;
384
385         memset(&map_dumb, 0, sizeof(map_dumb));
386         map_dumb.handle = bo->handles[plane].u32;
387
388         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
389         if (ret) {
390                 drv_log("DRM_IOCTL_MODE_MAP_DUMB failed\n");
391                 return MAP_FAILED;
392         }
393
394         for (i = 0; i < bo->num_planes; i++)
395                 if (bo->handles[i].u32 == bo->handles[plane].u32)
396                         vma->length += bo->sizes[i];
397
398         return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
399                     map_dumb.offset);
400 }
401
402 int drv_bo_munmap(struct bo *bo, struct vma *vma)
403 {
404         return munmap(vma->addr, vma->length);
405 }
406
407 int drv_mapping_destroy(struct bo *bo)
408 {
409         int ret;
410         size_t plane;
411         struct mapping *mapping;
412         uint32_t idx;
413
414         /*
415          * This function is called right before the buffer is destroyed. It will free any mappings
416          * associated with the buffer.
417          */
418
419         idx = 0;
420         for (plane = 0; plane < bo->num_planes; plane++) {
421                 while (idx < drv_array_size(bo->drv->mappings)) {
422                         mapping = (struct mapping *)drv_array_at_idx(bo->drv->mappings, idx);
423                         if (mapping->vma->handle != bo->handles[plane].u32) {
424                                 idx++;
425                                 continue;
426                         }
427
428                         if (!--mapping->vma->refcount) {
429                                 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
430                                 if (ret) {
431                                         drv_log("munmap failed\n");
432                                         return ret;
433                                 }
434
435                                 free(mapping->vma);
436                         }
437
438                         /* This shrinks and shifts the array, so don't increment idx. */
439                         drv_array_remove(bo->drv->mappings, idx);
440                 }
441         }
442
443         return 0;
444 }
445
446 int drv_get_prot(uint32_t map_flags)
447 {
448         return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
449 }
450
451 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
452 {
453         void *count;
454         uintptr_t num = 0;
455
456         if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
457                 num = (uintptr_t)(count);
458
459         return num;
460 }
461
462 void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
463 {
464         uintptr_t num = drv_get_reference_count(drv, bo, plane);
465
466         /* If a value isn't in the table, drmHashDelete is a no-op */
467         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
468         drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
469 }
470
471 void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
472 {
473         uintptr_t num = drv_get_reference_count(drv, bo, plane);
474
475         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
476
477         if (num > 0)
478                 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
479 }
480
481 uint32_t drv_log_base2(uint32_t value)
482 {
483         int ret = 0;
484
485         while (value >>= 1)
486                 ++ret;
487
488         return ret;
489 }
490
491 void drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
492                           struct format_metadata *metadata, uint64_t use_flags)
493 {
494         uint32_t i;
495
496         for (i = 0; i < num_formats; i++) {
497                 struct combination combo = { .format = formats[i],
498                                              .metadata = *metadata,
499                                              .use_flags = use_flags };
500
501                 drv_array_append(drv->combos, &combo);
502         }
503 }
504
505 void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
506                             uint64_t use_flags)
507 {
508         uint32_t i;
509         struct combination *combo;
510         /* Attempts to add the specified flags to an existing combination. */
511         for (i = 0; i < drv_array_size(drv->combos); i++) {
512                 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
513                 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
514                     combo->metadata.modifier == metadata->modifier)
515                         combo->use_flags |= use_flags;
516         }
517 }
518
519 struct drv_array *drv_query_kms(struct driver *drv)
520 {
521         struct drv_array *kms_items;
522         uint64_t plane_type, use_flag;
523         uint32_t i, j, k;
524
525         drmModePlanePtr plane;
526         drmModePropertyPtr prop;
527         drmModePlaneResPtr resources;
528         drmModeObjectPropertiesPtr props;
529
530         kms_items = drv_array_init(sizeof(struct kms_item));
531         if (!kms_items)
532                 goto out;
533
534         /*
535          * The ability to return universal planes is only complete on
536          * ChromeOS kernel versions >= v3.18.  The SET_CLIENT_CAP ioctl
537          * therefore might return an error code, so don't check it.  If it
538          * fails, it'll just return the plane list as overlay planes, which is
539          * fine in our case (our drivers already have cursor bits set).
540          * modetest in libdrm does the same thing.
541          */
542         drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
543
544         resources = drmModeGetPlaneResources(drv->fd);
545         if (!resources)
546                 goto out;
547
548         for (i = 0; i < resources->count_planes; i++) {
549                 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
550                 if (!plane)
551                         goto out;
552
553                 props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
554                 if (!props)
555                         goto out;
556
557                 for (j = 0; j < props->count_props; j++) {
558                         prop = drmModeGetProperty(drv->fd, props->props[j]);
559                         if (prop) {
560                                 if (strcmp(prop->name, "type") == 0) {
561                                         plane_type = props->prop_values[j];
562                                 }
563
564                                 drmModeFreeProperty(prop);
565                         }
566                 }
567
568                 switch (plane_type) {
569                 case DRM_PLANE_TYPE_OVERLAY:
570                 case DRM_PLANE_TYPE_PRIMARY:
571                         use_flag = BO_USE_SCANOUT;
572                         break;
573                 case DRM_PLANE_TYPE_CURSOR:
574                         use_flag = BO_USE_CURSOR;
575                         break;
576                 default:
577                         assert(0);
578                 }
579
580                 for (j = 0; j < plane->count_formats; j++) {
581                         bool found = false;
582                         for (k = 0; k < drv_array_size(kms_items); k++) {
583                                 struct kms_item *item = drv_array_at_idx(kms_items, k);
584                                 if (item->format == plane->formats[j] &&
585                                     item->modifier == DRM_FORMAT_MOD_LINEAR) {
586                                         item->use_flags |= use_flag;
587                                         found = true;
588                                         break;
589                                 }
590                         }
591
592                         if (!found) {
593                                 struct kms_item item = { .format = plane->formats[j],
594                                                          .modifier = DRM_FORMAT_MOD_LINEAR,
595                                                          .use_flags = use_flag };
596
597                                 drv_array_append(kms_items, &item);
598                         }
599                 }
600
601                 drmModeFreeObjectProperties(props);
602                 drmModeFreePlane(plane);
603         }
604
605         drmModeFreePlaneResources(resources);
606 out:
607         if (kms_items && !drv_array_size(kms_items)) {
608                 drv_array_destroy(kms_items);
609                 return NULL;
610         }
611
612         return kms_items;
613 }
614
615 int drv_modify_linear_combinations(struct driver *drv)
616 {
617         uint32_t i, j;
618         struct kms_item *item;
619         struct combination *combo;
620         struct drv_array *kms_items;
621
622         /*
623          * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
624          * plane and as a cursor. Some drivers don't support
625          * drmModeGetPlaneResources, so add the combination here. Note that the
626          * kernel disregards the alpha component of ARGB unless it's an overlay
627          * plane.
628          */
629         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
630                                BO_USE_CURSOR | BO_USE_SCANOUT);
631         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
632                                BO_USE_CURSOR | BO_USE_SCANOUT);
633
634         kms_items = drv_query_kms(drv);
635         if (!kms_items)
636                 return 0;
637
638         for (i = 0; i < drv_array_size(kms_items); i++) {
639                 item = (struct kms_item *)drv_array_at_idx(kms_items, i);
640                 for (j = 0; j < drv_array_size(drv->combos); j++) {
641                         combo = drv_array_at_idx(drv->combos, j);
642                         if (item->format == combo->format)
643                                 combo->use_flags |= BO_USE_SCANOUT;
644                 }
645         }
646
647         drv_array_destroy(kms_items);
648         return 0;
649 }
650
651 /*
652  * Pick the best modifier from modifiers, according to the ordering
653  * given by modifier_order.
654  */
655 uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
656                            const uint64_t *modifier_order, uint32_t order_count)
657 {
658         uint32_t i, j;
659
660         for (i = 0; i < order_count; i++) {
661                 for (j = 0; j < count; j++) {
662                         if (modifiers[j] == modifier_order[i]) {
663                                 return modifiers[j];
664                         }
665                 }
666         }
667
668         return DRM_FORMAT_MOD_LINEAR;
669 }