OSDN Git Service

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