OSDN Git Service

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