OSDN Git Service

minigbm: make drv_add_combinations return nothing
[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 uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
131 {
132         assert(plane < drv_num_planes_from_format(format));
133         uint32_t vertical_subsampling;
134
135         switch (format) {
136         case DRM_FORMAT_NV12:
137         case DRM_FORMAT_YVU420:
138         case DRM_FORMAT_YVU420_ANDROID:
139                 vertical_subsampling = (plane == 0) ? 1 : 2;
140                 break;
141         default:
142                 vertical_subsampling = 1;
143         }
144
145         return stride * DIV_ROUND_UP(height, vertical_subsampling);
146 }
147
148 /*
149  * This function fills in the buffer object given the driver aligned stride of
150  * the first plane, height and a format. This function assumes there is just
151  * one kernel buffer per buffer object.
152  */
153 int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
154 {
155
156         size_t p, num_planes;
157         uint32_t offset = 0;
158
159         num_planes = drv_num_planes_from_format(format);
160         assert(num_planes);
161
162         /*
163          * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
164          *  - the aligned height is same as the buffer's height.
165          *  - the chroma stride is 16 bytes aligned, i.e., the luma's strides
166          *    is 32 bytes aligned.
167          */
168         if (format == DRM_FORMAT_YVU420_ANDROID) {
169                 assert(aligned_height == bo->height);
170                 assert(stride == ALIGN(stride, 32));
171         }
172
173         for (p = 0; p < num_planes; p++) {
174                 bo->strides[p] = subsample_stride(stride, format, p);
175                 bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
176                 bo->offsets[p] = offset;
177                 offset += bo->sizes[p];
178         }
179
180         bo->total_size = offset;
181         return 0;
182 }
183
184 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
185                        uint64_t use_flags)
186 {
187         int ret;
188         size_t plane;
189         uint32_t aligned_width, aligned_height;
190         struct drm_mode_create_dumb create_dumb;
191
192         aligned_width = width;
193         aligned_height = height;
194         if (format == DRM_FORMAT_YVU420_ANDROID) {
195                 /*
196                  * Align width to 32 pixels, so chroma strides are 16 bytes as
197                  * Android requires.
198                  */
199                 aligned_width = ALIGN(width, 32);
200         }
201
202         if (format == DRM_FORMAT_YVU420_ANDROID || format == DRM_FORMAT_YVU420) {
203                 aligned_height = 3 * DIV_ROUND_UP(height, 2);
204         }
205
206         memset(&create_dumb, 0, sizeof(create_dumb));
207         create_dumb.height = aligned_height;
208         create_dumb.width = aligned_width;
209         create_dumb.bpp = bpp_from_format(format, 0);
210         create_dumb.flags = 0;
211
212         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
213         if (ret) {
214                 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
215                 return ret;
216         }
217
218         drv_bo_from_format(bo, create_dumb.pitch, height, format);
219
220         for (plane = 0; plane < bo->num_planes; plane++)
221                 bo->handles[plane].u32 = create_dumb.handle;
222
223         bo->total_size = create_dumb.size;
224         return 0;
225 }
226
227 int drv_dumb_bo_destroy(struct bo *bo)
228 {
229         struct drm_mode_destroy_dumb destroy_dumb;
230         int ret;
231
232         memset(&destroy_dumb, 0, sizeof(destroy_dumb));
233         destroy_dumb.handle = bo->handles[0].u32;
234
235         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
236         if (ret) {
237                 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n",
238                         bo->handles[0].u32);
239                 return ret;
240         }
241
242         return 0;
243 }
244
245 int drv_gem_bo_destroy(struct bo *bo)
246 {
247         struct drm_gem_close gem_close;
248         int ret, error = 0;
249         size_t plane, i;
250
251         for (plane = 0; plane < bo->num_planes; plane++) {
252                 for (i = 0; i < plane; i++)
253                         if (bo->handles[i].u32 == bo->handles[plane].u32)
254                                 break;
255                 /* Make sure close hasn't already been called on this handle */
256                 if (i != plane)
257                         continue;
258
259                 memset(&gem_close, 0, sizeof(gem_close));
260                 gem_close.handle = bo->handles[plane].u32;
261
262                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
263                 if (ret) {
264                         fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
265                                 bo->handles[plane].u32, ret);
266                         error = ret;
267                 }
268         }
269
270         return error;
271 }
272
273 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
274 {
275         int ret;
276         size_t plane;
277         struct drm_prime_handle prime_handle;
278
279         for (plane = 0; plane < bo->num_planes; plane++) {
280                 memset(&prime_handle, 0, sizeof(prime_handle));
281                 prime_handle.fd = data->fds[plane];
282
283                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
284
285                 if (ret) {
286                         fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n",
287                                 prime_handle.fd);
288
289                         /*
290                          * Need to call GEM close on planes that were opened,
291                          * if any. Adjust the num_planes variable to be the
292                          * plane that failed, so GEM close will be called on
293                          * planes before that plane.
294                          */
295                         bo->num_planes = plane;
296                         drv_gem_bo_destroy(bo);
297                         return ret;
298                 }
299
300                 bo->handles[plane].u32 = prime_handle.handle;
301         }
302
303         for (plane = 0; plane < bo->num_planes; plane++) {
304                 pthread_mutex_lock(&bo->drv->driver_lock);
305                 drv_increment_reference_count(bo->drv, bo, plane);
306                 pthread_mutex_unlock(&bo->drv->driver_lock);
307         }
308
309         return 0;
310 }
311
312 void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
313 {
314         int ret;
315         size_t i;
316         struct drm_mode_map_dumb map_dumb;
317
318         memset(&map_dumb, 0, sizeof(map_dumb));
319         map_dumb.handle = bo->handles[plane].u32;
320
321         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
322         if (ret) {
323                 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
324                 return MAP_FAILED;
325         }
326
327         for (i = 0; i < bo->num_planes; i++)
328                 if (bo->handles[i].u32 == bo->handles[plane].u32)
329                         vma->length += bo->sizes[i];
330
331         return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
332                     map_dumb.offset);
333 }
334
335 int drv_bo_munmap(struct bo *bo, struct vma *vma)
336 {
337         return munmap(vma->addr, vma->length);
338 }
339
340 int drv_mapping_destroy(struct bo *bo)
341 {
342         int ret;
343         size_t plane;
344         struct mapping *mapping;
345         uint32_t idx;
346
347         /*
348          * This function is called right before the buffer is destroyed. It will free any mappings
349          * associated with the buffer.
350          */
351
352         idx = 0;
353         for (plane = 0; plane < bo->num_planes; plane++) {
354                 while (idx < drv_array_size(bo->drv->mappings)) {
355                         mapping = (struct mapping *)drv_array_at_idx(bo->drv->mappings, idx);
356                         if (mapping->vma->handle != bo->handles[plane].u32) {
357                                 idx++;
358                                 continue;
359                         }
360
361                         if (!--mapping->vma->refcount) {
362                                 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
363                                 if (ret) {
364                                         fprintf(stderr, "drv: munmap failed");
365                                         return ret;
366                                 }
367
368                                 free(mapping->vma);
369                         }
370
371                         /* This shrinks and shifts the array, so don't increment idx. */
372                         drv_array_remove(bo->drv->mappings, idx);
373                 }
374         }
375
376         return 0;
377 }
378
379 int drv_get_prot(uint32_t map_flags)
380 {
381         return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
382 }
383
384 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
385 {
386         void *count;
387         uintptr_t num = 0;
388
389         if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
390                 num = (uintptr_t)(count);
391
392         return num;
393 }
394
395 void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
396 {
397         uintptr_t num = drv_get_reference_count(drv, bo, plane);
398
399         /* If a value isn't in the table, drmHashDelete is a no-op */
400         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
401         drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
402 }
403
404 void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
405 {
406         uintptr_t num = drv_get_reference_count(drv, bo, plane);
407
408         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
409
410         if (num > 0)
411                 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
412 }
413
414 uint32_t drv_log_base2(uint32_t value)
415 {
416         int ret = 0;
417
418         while (value >>= 1)
419                 ++ret;
420
421         return ret;
422 }
423
424 int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
425                         uint64_t use_flags)
426 {
427         struct combinations *combos = &drv->combos;
428         if (combos->size >= combos->allocations) {
429                 struct combination *new_data;
430                 combos->allocations *= 2;
431                 new_data = realloc(combos->data, combos->allocations * sizeof(*combos->data));
432                 if (!new_data)
433                         return -ENOMEM;
434
435                 combos->data = new_data;
436         }
437
438         combos->data[combos->size].format = format;
439         combos->data[combos->size].metadata.priority = metadata->priority;
440         combos->data[combos->size].metadata.tiling = metadata->tiling;
441         combos->data[combos->size].metadata.modifier = metadata->modifier;
442         combos->data[combos->size].use_flags = use_flags;
443         combos->size++;
444         return 0;
445 }
446
447 void drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
448                           struct format_metadata *metadata, uint64_t use_flags)
449 {
450         uint32_t i;
451         for (i = 0; i < num_formats; i++)
452                 drv_add_combination(drv, formats[i], metadata, use_flags);
453 }
454
455 void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
456                             uint64_t use_flags)
457 {
458         uint32_t i;
459         struct combination *combo;
460         /* Attempts to add the specified flags to an existing combination. */
461         for (i = 0; i < drv->combos.size; i++) {
462                 combo = &drv->combos.data[i];
463                 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
464                     combo->metadata.modifier == metadata->modifier)
465                         combo->use_flags |= use_flags;
466         }
467 }
468
469 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
470 {
471         struct kms_item *items;
472         uint64_t plane_type, use_flag;
473         uint32_t i, j, k, allocations, item_size;
474
475         drmModePlanePtr plane;
476         drmModePropertyPtr prop;
477         drmModePlaneResPtr resources;
478         drmModeObjectPropertiesPtr props;
479
480         /* Start with a power of 2 number of allocations. */
481         allocations = 2;
482         item_size = 0;
483         items = calloc(allocations, sizeof(*items));
484         if (!items)
485                 goto out;
486
487         /*
488          * The ability to return universal planes is only complete on
489          * ChromeOS kernel versions >= v3.18.  The SET_CLIENT_CAP ioctl
490          * therefore might return an error code, so don't check it.  If it
491          * fails, it'll just return the plane list as overlay planes, which is
492          * fine in our case (our drivers already have cursor bits set).
493          * modetest in libdrm does the same thing.
494          */
495         drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
496
497         resources = drmModeGetPlaneResources(drv->fd);
498         if (!resources)
499                 goto out;
500
501         for (i = 0; i < resources->count_planes; i++) {
502                 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
503                 if (!plane)
504                         goto out;
505
506                 props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
507                 if (!props)
508                         goto out;
509
510                 for (j = 0; j < props->count_props; j++) {
511                         prop = drmModeGetProperty(drv->fd, props->props[j]);
512                         if (prop) {
513                                 if (strcmp(prop->name, "type") == 0) {
514                                         plane_type = props->prop_values[j];
515                                 }
516
517                                 drmModeFreeProperty(prop);
518                         }
519                 }
520
521                 switch (plane_type) {
522                 case DRM_PLANE_TYPE_OVERLAY:
523                 case DRM_PLANE_TYPE_PRIMARY:
524                         use_flag = BO_USE_SCANOUT;
525                         break;
526                 case DRM_PLANE_TYPE_CURSOR:
527                         use_flag = BO_USE_CURSOR;
528                         break;
529                 default:
530                         assert(0);
531                 }
532
533                 for (j = 0; j < plane->count_formats; j++) {
534                         bool found = false;
535                         for (k = 0; k < item_size; k++) {
536                                 if (items[k].format == plane->formats[j] &&
537                                     items[k].modifier == DRM_FORMAT_MOD_INVALID) {
538                                         items[k].use_flags |= use_flag;
539                                         found = true;
540                                         break;
541                                 }
542                         }
543
544                         if (!found && item_size >= allocations) {
545                                 struct kms_item *new_data = NULL;
546                                 allocations *= 2;
547                                 new_data = realloc(items, allocations * sizeof(*items));
548                                 if (!new_data) {
549                                         item_size = 0;
550                                         goto out;
551                                 }
552
553                                 items = new_data;
554                         }
555
556                         if (!found) {
557                                 items[item_size].format = plane->formats[j];
558                                 items[item_size].modifier = DRM_FORMAT_MOD_INVALID;
559                                 items[item_size].use_flags = use_flag;
560                                 item_size++;
561                         }
562                 }
563
564                 drmModeFreeObjectProperties(props);
565                 drmModeFreePlane(plane);
566         }
567
568         drmModeFreePlaneResources(resources);
569 out:
570         if (items && item_size == 0) {
571                 free(items);
572                 items = NULL;
573         }
574
575         *num_items = item_size;
576         return items;
577 }
578
579 int drv_modify_linear_combinations(struct driver *drv)
580 {
581         uint32_t i, j, num_items;
582         struct kms_item *items;
583         struct combination *combo;
584
585         /*
586          * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
587          * plane and as a cursor. Some drivers don't support
588          * drmModeGetPlaneResources, so add the combination here. Note that the
589          * kernel disregards the alpha component of ARGB unless it's an overlay
590          * plane.
591          */
592         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
593                                BO_USE_CURSOR | BO_USE_SCANOUT);
594         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
595                                BO_USE_CURSOR | BO_USE_SCANOUT);
596
597         items = drv_query_kms(drv, &num_items);
598         if (!items || !num_items)
599                 return 0;
600
601         for (i = 0; i < num_items; i++) {
602                 for (j = 0; j < drv->combos.size; j++) {
603                         combo = &drv->combos.data[j];
604                         if (items[i].format == combo->format)
605                                 combo->use_flags |= BO_USE_SCANOUT;
606                 }
607         }
608
609         free(items);
610         return 0;
611 }
612
613 /*
614  * Pick the best modifier from modifiers, according to the ordering
615  * given by modifier_order.
616  */
617 uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
618                            const uint64_t *modifier_order, uint32_t order_count)
619 {
620         uint32_t i, j;
621
622         for (i = 0; i < order_count; i++) {
623                 for (j = 0; j < count; j++) {
624                         if (modifiers[j] == modifier_order[i]) {
625                                 return modifiers[j];
626                         }
627                 }
628         }
629
630         return DRM_FORMAT_MOD_LINEAR;
631 }