blob: 081eb614482cb5e3d26256bc07f3e7e6ac52dd3f [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesin25a26062014-09-12 16:18:59 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Yuly Novikov96c7a3b2015-12-08 22:48:29 -05007#include <assert.h>
Gurchetan Singh6b41fb52017-03-01 20:14:39 -08008#include <errno.h>
Yuly Novikov96c7a3b2015-12-08 22:48:29 -05009#include <stdbool.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070010#include <stdio.h>
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050011#include <stdlib.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070012#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070013#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <xf86drm.h>
Gurchetan Singh179687e2016-10-28 10:07:35 -070015#include <xf86drmMode.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070016
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "drv_priv.h"
Lauri Peltonen904a8792015-01-17 13:57:51 +020018#include "helpers.h"
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050019#include "util.h"
20
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070021int drv_bpp_from_format(uint32_t format, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070022{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070023 assert(plane < drv_num_planes_from_format(format));
24
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070025 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080026 case DRM_FORMAT_BGR233:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080027 case DRM_FORMAT_C8:
28 case DRM_FORMAT_R8:
29 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080030 case DRM_FORMAT_YVU420:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070031 return 8;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070032
Gurchetan Singh2a119342016-11-02 10:40:51 -070033 /*
34 * NV12 is laid out as follows. Each letter represents a byte.
35 * Y plane:
36 * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N
37 * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N
38 * ...
39 * YM_0, YM_1, YM_2, YM_3, ..., YM_N
40 * CbCr plane:
41 * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N
42 * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N
43 * ...
44 * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N
45 *
46 * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires
47 * Y0_1, Cb01_01 and Cr01_01. So for a single pixel, 2 bytes of luma data
48 * are required.
49 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080050 case DRM_FORMAT_NV12:
Gurchetan Singh2a119342016-11-02 10:40:51 -070051 return (plane == 0) ? 8 : 16;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050052
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080053 case DRM_FORMAT_ABGR1555:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080054 case DRM_FORMAT_ABGR4444:
55 case DRM_FORMAT_ARGB1555:
56 case DRM_FORMAT_ARGB4444:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080057 case DRM_FORMAT_BGR565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080058 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:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080069 case DRM_FORMAT_UYVY:
70 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080071 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070077 return 16;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070078
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080079 case DRM_FORMAT_BGR888:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080080 case DRM_FORMAT_RGB888:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070081 return 24;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070082
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080083 case DRM_FORMAT_ABGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080084 case DRM_FORMAT_ABGR8888:
85 case DRM_FORMAT_ARGB2101010:
86 case DRM_FORMAT_ARGB8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080087 case DRM_FORMAT_AYUV:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080088 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700100 return 32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700101 }
102
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700103 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700104 return 0;
105}
106
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700107/*
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700108 * This function fills in the buffer object given driver aligned dimensions
109 * and a format. This function assumes there is just one kernel buffer per
110 * buffer object.
111 */
112int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800113 uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700114{
115
Gurchetan Singh2a119342016-11-02 10:40:51 -0700116 size_t p, num_planes;
117 uint32_t offset = 0;
118
119 num_planes = drv_num_planes_from_format(format);
120 assert(num_planes);
121
122 for (p = 0; p < num_planes; p++) {
123 bo->strides[p] = drv_stride_from_format(format, width, p);
124 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
125 height, p);
126 bo->offsets[p] = offset;
127 offset += bo->sizes[p];
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700128 }
129
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700130 bo->total_size = bo->offsets[bo->num_planes - 1] +
131 bo->sizes[bo->num_planes - 1];
132
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700133 return 0;
134}
135
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700136int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800137 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700138{
139 struct drm_mode_create_dumb create_dumb;
140 int ret;
141
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500142 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700143 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500144
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700145 memset(&create_dumb, 0, sizeof(create_dumb));
146 create_dumb.height = height;
147 create_dumb.width = width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700148 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700149 create_dumb.flags = 0;
150
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700151 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700152 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700154 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700155 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700156
Gurchetan Singhbc17b1d2016-12-06 15:43:17 -0800157 bo->width = width;
158 bo->height = height;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500159 bo->handles[0].u32 = create_dumb.handle;
160 bo->offsets[0] = 0;
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700161 bo->total_size = bo->sizes[0] = create_dumb.size;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500162 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700163
164 return 0;
165}
166
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700167int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700168{
169 struct drm_mode_destroy_dumb destroy_dumb;
170 int ret;
171
172 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500173 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700174
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700175 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700176 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700177 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500178 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700179 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700180 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700181
182 return 0;
183}
184
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700185int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700186{
187 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500188 int ret, error = 0;
189 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700190
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500191 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700192 for (i = 0; i < plane; i++)
193 if (bo->handles[i].u32 == bo->handles[plane].u32)
194 break;
195 /* Make sure close hasn't already been called on this handle */
196 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500197 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700198
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500199 memset(&gem_close, 0, sizeof(gem_close));
200 gem_close.handle = bo->handles[plane].u32;
201
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500203 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700204 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500205 "(handle=%x) error %d\n",
206 bo->handles[plane].u32, ret);
207 error = ret;
208 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700209 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500211 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700212}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700213
Gurchetan Singh71611d62017-01-03 16:49:56 -0800214int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
215{
216 int ret;
217 size_t plane;
218 struct drm_prime_handle prime_handle;
219
220 for (plane = 0; plane < bo->num_planes; plane++) {
221 memset(&prime_handle, 0, sizeof(prime_handle));
222 prime_handle.fd = data->fds[plane];
223
224 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
225 &prime_handle);
226
227 if (ret) {
228 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
229 "failed (fd=%u)\n", prime_handle.fd);
230
231 /*
232 * Need to call GEM close on planes that were opened,
233 * if any. Adjust the num_planes variable to be the
234 * plane that failed, so GEM close will be called on
235 * planes before that plane.
236 */
237 bo->num_planes = plane;
238 drv_gem_bo_destroy(bo);
239 return ret;
240 }
241
242 bo->handles[plane].u32 = prime_handle.handle;
243 }
244
245 for (plane = 0; plane < bo->num_planes; plane++) {
246 pthread_mutex_lock(&bo->drv->driver_lock);
247 drv_increment_reference_count(bo->drv, bo, plane);
248 pthread_mutex_unlock(&bo->drv->driver_lock);
249 }
250
251 return 0;
252}
253
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700254void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700255{
256 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700257 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700258 struct drm_mode_map_dumb map_dumb;
259
260 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700261 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700262
263 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
264 if (ret) {
265 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
266 return MAP_FAILED;
267 }
268
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700269 for (i = 0; i < bo->num_planes; i++)
270 if (bo->handles[i].u32 == bo->handles[plane].u32)
271 data->length += bo->sizes[i];
272
273 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700274 bo->drv->fd, map_dumb.offset);
275}
276
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700277uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
278 size_t plane)
279{
280 void *count;
281 uintptr_t num = 0;
282
283 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
284 num = (uintptr_t) (count);
285
286 return num;
287}
288
289void drv_increment_reference_count(struct driver *drv, struct bo *bo,
290 size_t plane)
291{
292 uintptr_t num = drv_get_reference_count(drv, bo, plane);
293
294 /* If a value isn't in the table, drmHashDelete is a no-op */
295 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
296 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
297 (void *) (num + 1));
298}
299
300void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
301 size_t plane)
302{
303 uintptr_t num = drv_get_reference_count(drv, bo, plane);
304
305 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
306
307 if (num > 0)
308 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
309 (void *) (num - 1));
310}
Gurchetan Singhef920532016-08-12 16:38:25 -0700311
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530312uint32_t drv_log_base2(uint32_t value)
313{
314 int ret = 0;
315
316 while (value >>= 1)
317 ++ret;
318
319 return ret;
320}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700321
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800322int drv_add_combination(struct driver *drv, uint32_t format,
323 struct format_metadata *metadata, uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700324{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800325 struct combinations *combos = &drv->backend->combos;
326 if (combos->size >= combos->allocations) {
327 struct combination *new_data;
328 combos->allocations *= 2;
329 new_data = realloc(combos->data, combos->allocations
330 * sizeof(*combos->data));
331 if (!new_data)
332 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700333
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800334 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700335 }
336
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800337 combos->data[combos->size].format = format;
338 combos->data[combos->size].metadata.priority = metadata->priority;
339 combos->data[combos->size].metadata.tiling = metadata->tiling;
340 combos->data[combos->size].metadata.modifier = metadata->modifier;
341 combos->data[combos->size].usage = usage;
342 combos->size++;
343 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700344}
345
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800346int drv_add_combinations(struct driver *drv, const uint32_t *formats,
347 uint32_t num_formats, struct format_metadata *metadata,
348 uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700349{
350 int ret;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800351 uint32_t i;
352 for (i = 0; i < num_formats; i++) {
353 ret = drv_add_combination(drv, formats[i], metadata, usage);
354 if (ret)
355 return ret;
356 }
357
358 return 0;
359}
360
361void drv_modify_combination(struct driver *drv, uint32_t format,
362 struct format_metadata *metadata, uint64_t usage)
363{
364 uint32_t i;
365 struct combination *combo;
366 /* Attempts to add the specified usage to an existing combination. */
367 for (i = 0; i < drv->backend->combos.size; i++) {
368 combo = &drv->backend->combos.data[i];
369 if (combo->format == format &&
370 combo->metadata.tiling == metadata->tiling &&
371 combo->metadata.modifier == metadata->modifier)
372 combo->usage |= usage;
373 }
374}
375
376struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
377{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700378 uint64_t flag, usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800379 struct kms_item *items;
380 uint32_t i, j, k, allocations, item_size;
381
Gurchetan Singh179687e2016-10-28 10:07:35 -0700382 drmModePlanePtr plane;
383 drmModePropertyPtr prop;
384 drmModePlaneResPtr resources;
385 drmModeObjectPropertiesPtr props;
386
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800387 /* Start with a power of 2 number of allocations. */
388 allocations = 2;
389 item_size = 0;
390 items = calloc(allocations, sizeof(*items));
391 if (!items)
392 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700393
394 /*
395 * The ability to return universal planes is only complete on
396 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
397 * therefore might return an error code, so don't check it. If it
398 * fails, it'll just return the plane list as overlay planes, which is
399 * fine in our case (our drivers already have cursor bits set).
400 * modetest in libdrm does the same thing.
401 */
402 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
403
404 resources = drmModeGetPlaneResources(drv->fd);
405 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800406 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700407
408 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700409 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700410 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800411 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700412
413 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
414 DRM_MODE_OBJECT_PLANE);
415 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800416 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700417
418 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700419 prop = drmModeGetProperty(drv->fd, props->props[j]);
420 if (prop) {
421 if (strcmp(prop->name, "type") == 0) {
422 flag = props->prop_values[j];
423 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800424
Gurchetan Singh179687e2016-10-28 10:07:35 -0700425 drmModeFreeProperty(prop);
426 }
427 }
428
429 switch (flag) {
430 case DRM_PLANE_TYPE_OVERLAY:
431 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800432 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700433 break;
434 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800435 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700436 break;
437 default:
438 assert(0);
439 }
440
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800441 for (j = 0; j < plane->count_formats; j++) {
442 bool found = false;
443 for (k = 0; k < item_size; k++) {
444 if (items[k].format == plane->formats[j] &&
445 items[k].modifier == DRM_FORMAT_MOD_NONE) {
446 items[k].usage |= usage;
447 found = true;
448 break;
449 }
450 }
451
452 if (!found && item_size >= allocations) {
453 struct kms_item *new_data = NULL;
454 allocations *= 2;
455 new_data = realloc(items, allocations *
456 sizeof(*items));
457 if (!new_data) {
458 item_size = 0;
459 goto out;
460 }
461
462 items = new_data;
463 }
464
465 if (!found) {
466 items[item_size].format = plane->formats[j];
467 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
468 items[item_size].usage = usage;
469 item_size++;
470 }
471 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700472
473 drmModeFreeObjectProperties(props);
474 drmModeFreePlane(plane);
475
476 }
477
478 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800479out:
480 if (items && item_size == 0) {
481 free(items);
482 items = NULL;
483 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700484
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800485 *num_items = item_size;
486 return items;
487}
488
489int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
490 uint32_t num_formats)
491{
492 int ret;
493 uint32_t i, j, num_items;
494 struct kms_item *items;
495 struct combination *combo;
496 struct format_metadata metadata;
497
498 metadata.tiling = 0;
499 metadata.priority = 1;
500 metadata.modifier = DRM_FORMAT_MOD_NONE;
501
502 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
503 BO_COMMON_USE_MASK);
504 if (ret)
505 return ret;
506 /*
507 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
508 * plane and as a cursor. Some drivers don't support
509 * drmModeGetPlaneResources, so add the combination here. Note that the
510 * kernel disregards the alpha component of ARGB unless it's an overlay
511 * plane.
512 */
513 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
514 BO_USE_CURSOR | BO_USE_SCANOUT);
515 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
516 BO_USE_CURSOR | BO_USE_SCANOUT);
517
518 items = drv_query_kms(drv, &num_items);
519 if (!items || !num_items)
520 return 0;
521
522 for (i = 0; i < num_items; i++) {
523 for (j = 0; j < drv->backend->combos.size; j++) {
524 combo = &drv->backend->combos.data[j];
525 if (items[i].format == combo->format)
526 combo->usage |= BO_USE_SCANOUT;
527
528
529 }
530 }
531
532 free(items);
533 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700534}