OSDN Git Service

minigbm: vgem: add a few more supported formats
[android-x86/external-minigbm.git] / helpers.c
1 /*
2  * Copyright (c) 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 <stdbool.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <xf86drm.h>
14 #include <xf86drmMode.h>
15
16 #include "drv_priv.h"
17 #include "helpers.h"
18 #include "util.h"
19
20 int drv_bpp_from_format(uint32_t format, size_t plane)
21 {
22         assert(plane < drv_num_planes_from_format(format));
23
24         switch (format) {
25         case DRM_FORMAT_BGR233:
26         case DRM_FORMAT_C8:
27         case DRM_FORMAT_R8:
28         case DRM_FORMAT_RGB332:
29         case DRM_FORMAT_YVU420:
30                 return 8;
31
32         /*
33          * NV12 is laid out as follows. Each letter represents a byte.
34          * Y plane:
35          * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N
36          * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N
37          * ...
38          * YM_0, YM_1, YM_2, YM_3, ..., YM_N
39          * CbCr plane:
40          * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N
41          * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N
42          * ...
43          * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N
44          *
45          * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires
46          * Y0_1, Cb01_01 and Cr01_01.  So for a single pixel, 2 bytes of luma data
47          * are required.
48          */
49         case DRM_FORMAT_NV12:
50                 return (plane == 0) ? 8 : 16;
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 /*
107  * This function fills in the buffer object given driver aligned dimensions
108  * and a format.  This function assumes there is just one kernel buffer per
109  * buffer object.
110  */
111 int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
112                        uint32_t format)
113 {
114
115         size_t p, num_planes;
116         uint32_t offset = 0;
117
118         num_planes = drv_num_planes_from_format(format);
119         assert(num_planes);
120
121         for (p = 0; p < num_planes; p++) {
122                 bo->strides[p] = drv_stride_from_format(format, width, p);
123                 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
124                                                     height, p);
125                 bo->offsets[p] = offset;
126                 offset += bo->sizes[p];
127         }
128
129         bo->total_size = bo->offsets[bo->num_planes - 1] +
130                          bo->sizes[bo->num_planes - 1];
131
132         return 0;
133 }
134
135 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
136                        uint32_t format, uint32_t flags)
137 {
138         struct drm_mode_create_dumb create_dumb;
139         int ret;
140
141         /* Only single-plane formats are supported */
142         assert(drv_num_planes_from_format(format) == 1);
143
144         memset(&create_dumb, 0, sizeof(create_dumb));
145         create_dumb.height = height;
146         create_dumb.width = width;
147         create_dumb.bpp = drv_bpp_from_format(format, 0);
148         create_dumb.flags = 0;
149
150         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
151         if (ret) {
152                 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
153                 return ret;
154         }
155
156         bo->handles[0].u32 = create_dumb.handle;
157         bo->offsets[0] = 0;
158         bo->total_size = bo->sizes[0] = create_dumb.size;
159         bo->strides[0] = create_dumb.pitch;
160
161         return 0;
162 }
163
164 int drv_dumb_bo_destroy(struct bo *bo)
165 {
166         struct drm_mode_destroy_dumb destroy_dumb;
167         int ret;
168
169         memset(&destroy_dumb, 0, sizeof(destroy_dumb));
170         destroy_dumb.handle = bo->handles[0].u32;
171
172         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
173         if (ret) {
174                 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
175                                 "(handle=%x)\n", bo->handles[0].u32);
176                 return ret;
177         }
178
179         return 0;
180 }
181
182 int drv_gem_bo_destroy(struct bo *bo)
183 {
184         struct drm_gem_close gem_close;
185         int ret, error = 0;
186         size_t plane, i;
187
188         for (plane = 0; plane < bo->num_planes; plane++) {
189                 for (i = 0; i < plane; i++)
190                         if (bo->handles[i].u32 == bo->handles[plane].u32)
191                                 break;
192                 /* Make sure close hasn't already been called on this handle */
193                 if (i != plane)
194                         continue;
195
196                 memset(&gem_close, 0, sizeof(gem_close));
197                 gem_close.handle = bo->handles[plane].u32;
198
199                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
200                 if (ret) {
201                         fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
202                                         "(handle=%x) error %d\n",
203                                         bo->handles[plane].u32, ret);
204                         error = ret;
205                 }
206         }
207
208         return error;
209 }
210
211 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
212 {
213         int ret;
214         size_t i;
215         struct drm_mode_map_dumb map_dumb;
216
217         memset(&map_dumb, 0, sizeof(map_dumb));
218         map_dumb.handle = bo->handles[plane].u32;
219
220         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
221         if (ret) {
222                 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
223                 return MAP_FAILED;
224         }
225
226         for (i = 0; i < bo->num_planes; i++)
227                 if (bo->handles[i].u32 == bo->handles[plane].u32)
228                         data->length += bo->sizes[i];
229
230         return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
231                     bo->drv->fd, map_dumb.offset);
232 }
233
234 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
235                                   size_t plane)
236 {
237         void *count;
238         uintptr_t num = 0;
239
240         if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
241                 num = (uintptr_t) (count);
242
243         return num;
244 }
245
246 void drv_increment_reference_count(struct driver *drv, struct bo *bo,
247                                    size_t plane)
248 {
249         uintptr_t num = drv_get_reference_count(drv, bo, plane);
250
251         /* If a value isn't in the table, drmHashDelete is a no-op */
252         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
253         drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
254                       (void *) (num + 1));
255 }
256
257 void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
258                                    size_t plane)
259 {
260         uintptr_t num = drv_get_reference_count(drv, bo, plane);
261
262         drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
263
264         if (num > 0)
265                 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
266                               (void *) (num - 1));
267 }
268
269 uint32_t drv_log_base2(uint32_t value)
270 {
271         int ret = 0;
272
273         while (value >>= 1)
274                 ++ret;
275
276         return ret;
277 }
278
279 /* Inserts a combination into list -- caller should have lock on driver. */
280 void drv_insert_supported_combination(struct driver *drv, uint32_t format,
281                                       uint64_t usage, uint64_t modifier)
282 {
283         struct combination_list_element *elem;
284
285         elem = calloc(1, sizeof(*elem));
286         elem->combination.format = format;
287         elem->combination.modifier = modifier;
288         elem->combination.usage = usage;
289         LIST_ADD(&elem->link, &drv->backend->combinations);
290 }
291
292 void drv_insert_combinations(struct driver *drv, struct supported_combination *combos,
293                              uint32_t size)
294 {
295         unsigned int i;
296
297         pthread_mutex_lock(&drv->driver_lock);
298
299         for (i = 0; i < size; i++)
300                 drv_insert_supported_combination(drv, combos[i].format,
301                                                  combos[i].usage,
302                                                  combos[i].modifier);
303
304         pthread_mutex_unlock(&drv->driver_lock);
305 }
306
307 void drv_modify_supported_combination(struct driver *drv, uint32_t format,
308                                       uint64_t usage, uint64_t modifier)
309 {
310         /*
311          * Attempts to add the specified usage to an existing {format, modifier}
312          * pair. If the pair is not present, a new combination is created.
313          */
314         int found = 0;
315
316         pthread_mutex_lock(&drv->driver_lock);
317
318         list_for_each_entry(struct combination_list_element,
319                             elem, &drv->backend->combinations, link) {
320                 if (elem->combination.format == format &&
321                     elem->combination.modifier == modifier) {
322                         elem->combination.usage |= usage;
323                         found = 1;
324                 }
325         }
326
327
328         if (!found)
329                 drv_insert_supported_combination(drv, format, usage, modifier);
330
331         pthread_mutex_unlock(&drv->driver_lock);
332 }
333
334 int drv_add_kms_flags(struct driver *drv)
335 {
336         int ret;
337         uint32_t i, j;
338         uint64_t flag, usage;
339         drmModePlanePtr plane;
340         drmModePropertyPtr prop;
341         drmModePlaneResPtr resources;
342         drmModeObjectPropertiesPtr props;
343
344         /*
345          * All current drivers can scanout XRGB8888/ARGB8888 as a primary plane.
346          * Some older kernel versions can only return overlay planes, so add the
347          * combination here. Note that the kernel disregards the alpha component
348          * of ARGB unless it's an overlay plane.
349          */
350         drv_modify_supported_combination(drv, DRM_FORMAT_XRGB8888,
351                                          BO_USE_SCANOUT, 0);
352         drv_modify_supported_combination(drv, DRM_FORMAT_ARGB8888,
353                                          BO_USE_SCANOUT, 0);
354
355         /*
356          * The ability to return universal planes is only complete on
357          * ChromeOS kernel versions >= v3.18.  The SET_CLIENT_CAP ioctl
358          * therefore might return an error code, so don't check it.  If it
359          * fails, it'll just return the plane list as overlay planes, which is
360          * fine in our case (our drivers already have cursor bits set).
361          * modetest in libdrm does the same thing.
362          */
363         drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
364
365         resources = drmModeGetPlaneResources(drv->fd);
366         if (!resources)
367                 goto err;
368
369         for (i = 0; i < resources->count_planes; i++) {
370
371                 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
372
373                 if (!plane)
374                         goto err;
375
376                 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
377                                                    DRM_MODE_OBJECT_PLANE);
378                 if (!props)
379                         goto err;
380
381                 for (j = 0; j < props->count_props; j++) {
382
383                         prop = drmModeGetProperty(drv->fd, props->props[j]);
384                         if (prop) {
385                                 if (strcmp(prop->name, "type") == 0) {
386                                         flag = props->prop_values[j];
387                                 }
388                                 drmModeFreeProperty(prop);
389                         }
390                 }
391
392                 switch (flag) {
393                 case DRM_PLANE_TYPE_OVERLAY:
394                 case DRM_PLANE_TYPE_PRIMARY:
395                         usage = BO_USE_SCANOUT;
396                         break;
397                 case DRM_PLANE_TYPE_CURSOR:
398                         usage = BO_USE_CURSOR;
399                         break;
400                 default:
401                         assert(0);
402                 }
403
404                 for (j = 0; j < plane->count_formats; j++)
405                         drv_modify_supported_combination(drv, plane->formats[j],
406                                                          usage, 0);
407
408                 drmModeFreeObjectProperties(props);
409                 drmModeFreePlane(plane);
410
411         }
412
413         drmModeFreePlaneResources(resources);
414         return 0;
415
416 err:
417         ret = -1;
418         return ret;
419 }