blob: 1a838b819da7ce0bb65bcea8b4236abb1d71746a [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 Singh03f13562017-02-08 15:21:14 -080031 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070032 return 8;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070033
Gurchetan Singh2a119342016-11-02 10:40:51 -070034 /*
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 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080051 case DRM_FORMAT_NV12:
Gurchetan Singh2a119342016-11-02 10:40:51 -070052 return (plane == 0) ? 8 : 16;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050053
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080054 case DRM_FORMAT_ABGR1555:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080055 case DRM_FORMAT_ABGR4444:
56 case DRM_FORMAT_ARGB1555:
57 case DRM_FORMAT_ARGB4444:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080058 case DRM_FORMAT_BGR565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080059 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:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080070 case DRM_FORMAT_UYVY:
71 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080072 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070078 return 16;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070079
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080080 case DRM_FORMAT_BGR888:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080081 case DRM_FORMAT_RGB888:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070082 return 24;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070083
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080084 case DRM_FORMAT_ABGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080085 case DRM_FORMAT_ABGR8888:
86 case DRM_FORMAT_ARGB2101010:
87 case DRM_FORMAT_ARGB8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080088 case DRM_FORMAT_AYUV:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080089 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700101 return 32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700102 }
103
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700105 return 0;
106}
107
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700108/*
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700109 * This function fills in the buffer object given driver aligned dimensions
Gurchetan Singh03f13562017-02-08 15:21:14 -0800110 * (in pixels) and a format. This function assumes there is just one kernel
111 * buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700112 */
Gurchetan Singh03f13562017-02-08 15:21:14 -0800113int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
114 uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700115{
116
Gurchetan Singh2a119342016-11-02 10:40:51 -0700117 size_t p, num_planes;
118 uint32_t offset = 0;
119
120 num_planes = drv_num_planes_from_format(format);
121 assert(num_planes);
Gurchetan Singh03f13562017-02-08 15:21:14 -0800122 bo->total_size = 0;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700123
124 for (p = 0; p < num_planes; p++) {
Gurchetan Singh03f13562017-02-08 15:21:14 -0800125 bo->strides[p] = drv_stride_from_format(format, aligned_width,
126 p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700127 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
Gurchetan Singh03f13562017-02-08 15:21:14 -0800128 bo->height, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700129 bo->offsets[p] = offset;
130 offset += bo->sizes[p];
Gurchetan Singh03f13562017-02-08 15:21:14 -0800131 bo->total_size += drv_size_from_format(format, bo->strides[p],
132 aligned_height, p);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700133 }
134
135 return 0;
136}
137
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700138int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800139 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700140{
141 struct drm_mode_create_dumb create_dumb;
142 int ret;
143
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500144 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700145 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500146
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700147 memset(&create_dumb, 0, sizeof(create_dumb));
148 create_dumb.height = height;
149 create_dumb.width = width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700150 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151 create_dumb.flags = 0;
152
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700154 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700155 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700156 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700157 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700158
Gurchetan Singhbc17b1d2016-12-06 15:43:17 -0800159 bo->width = width;
160 bo->height = height;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500161 bo->handles[0].u32 = create_dumb.handle;
162 bo->offsets[0] = 0;
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700163 bo->total_size = bo->sizes[0] = create_dumb.size;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500164 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700165
166 return 0;
167}
168
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700169int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700170{
171 struct drm_mode_destroy_dumb destroy_dumb;
172 int ret;
173
174 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500175 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700176
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700177 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700178 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700179 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500180 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700181 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700182 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700183
184 return 0;
185}
186
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700187int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700188{
189 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500190 int ret, error = 0;
191 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700192
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500193 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700194 for (i = 0; i < plane; i++)
195 if (bo->handles[i].u32 == bo->handles[plane].u32)
196 break;
197 /* Make sure close hasn't already been called on this handle */
198 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500199 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700200
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500201 memset(&gem_close, 0, sizeof(gem_close));
202 gem_close.handle = bo->handles[plane].u32;
203
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700204 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500205 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700206 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500207 "(handle=%x) error %d\n",
208 bo->handles[plane].u32, ret);
209 error = ret;
210 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700211 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700212
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500213 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700215
Gurchetan Singh71611d62017-01-03 16:49:56 -0800216int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
217{
218 int ret;
219 size_t plane;
220 struct drm_prime_handle prime_handle;
221
222 for (plane = 0; plane < bo->num_planes; plane++) {
223 memset(&prime_handle, 0, sizeof(prime_handle));
224 prime_handle.fd = data->fds[plane];
225
226 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
227 &prime_handle);
228
229 if (ret) {
230 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
231 "failed (fd=%u)\n", prime_handle.fd);
232
233 /*
234 * Need to call GEM close on planes that were opened,
235 * if any. Adjust the num_planes variable to be the
236 * plane that failed, so GEM close will be called on
237 * planes before that plane.
238 */
239 bo->num_planes = plane;
240 drv_gem_bo_destroy(bo);
241 return ret;
242 }
243
244 bo->handles[plane].u32 = prime_handle.handle;
245 }
246
247 for (plane = 0; plane < bo->num_planes; plane++) {
248 pthread_mutex_lock(&bo->drv->driver_lock);
249 drv_increment_reference_count(bo->drv, bo, plane);
250 pthread_mutex_unlock(&bo->drv->driver_lock);
251 }
252
253 return 0;
254}
255
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700256void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700257{
258 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700259 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700260 struct drm_mode_map_dumb map_dumb;
261
262 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700263 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700264
265 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
266 if (ret) {
267 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
268 return MAP_FAILED;
269 }
270
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700271 for (i = 0; i < bo->num_planes; i++)
272 if (bo->handles[i].u32 == bo->handles[plane].u32)
273 data->length += bo->sizes[i];
274
275 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700276 bo->drv->fd, map_dumb.offset);
277}
278
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700279uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
280 size_t plane)
281{
282 void *count;
283 uintptr_t num = 0;
284
285 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
286 num = (uintptr_t) (count);
287
288 return num;
289}
290
291void drv_increment_reference_count(struct driver *drv, struct bo *bo,
292 size_t plane)
293{
294 uintptr_t num = drv_get_reference_count(drv, bo, plane);
295
296 /* If a value isn't in the table, drmHashDelete is a no-op */
297 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
298 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
299 (void *) (num + 1));
300}
301
302void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
303 size_t plane)
304{
305 uintptr_t num = drv_get_reference_count(drv, bo, plane);
306
307 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
308
309 if (num > 0)
310 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
311 (void *) (num - 1));
312}
Gurchetan Singhef920532016-08-12 16:38:25 -0700313
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530314uint32_t drv_log_base2(uint32_t value)
315{
316 int ret = 0;
317
318 while (value >>= 1)
319 ++ret;
320
321 return ret;
322}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700323
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800324int drv_add_combination(struct driver *drv, uint32_t format,
325 struct format_metadata *metadata, uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700326{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800327 struct combinations *combos = &drv->backend->combos;
328 if (combos->size >= combos->allocations) {
329 struct combination *new_data;
330 combos->allocations *= 2;
331 new_data = realloc(combos->data, combos->allocations
332 * sizeof(*combos->data));
333 if (!new_data)
334 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700335
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800336 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700337 }
338
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800339 combos->data[combos->size].format = format;
340 combos->data[combos->size].metadata.priority = metadata->priority;
341 combos->data[combos->size].metadata.tiling = metadata->tiling;
342 combos->data[combos->size].metadata.modifier = metadata->modifier;
343 combos->data[combos->size].usage = usage;
344 combos->size++;
345 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700346}
347
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800348int drv_add_combinations(struct driver *drv, const uint32_t *formats,
349 uint32_t num_formats, struct format_metadata *metadata,
350 uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700351{
352 int ret;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800353 uint32_t i;
354 for (i = 0; i < num_formats; i++) {
355 ret = drv_add_combination(drv, formats[i], metadata, usage);
356 if (ret)
357 return ret;
358 }
359
360 return 0;
361}
362
363void drv_modify_combination(struct driver *drv, uint32_t format,
364 struct format_metadata *metadata, uint64_t usage)
365{
366 uint32_t i;
367 struct combination *combo;
368 /* Attempts to add the specified usage to an existing combination. */
369 for (i = 0; i < drv->backend->combos.size; i++) {
370 combo = &drv->backend->combos.data[i];
371 if (combo->format == format &&
372 combo->metadata.tiling == metadata->tiling &&
373 combo->metadata.modifier == metadata->modifier)
374 combo->usage |= usage;
375 }
376}
377
378struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
379{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700380 uint64_t flag, usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800381 struct kms_item *items;
382 uint32_t i, j, k, allocations, item_size;
383
Gurchetan Singh179687e2016-10-28 10:07:35 -0700384 drmModePlanePtr plane;
385 drmModePropertyPtr prop;
386 drmModePlaneResPtr resources;
387 drmModeObjectPropertiesPtr props;
388
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800389 /* Start with a power of 2 number of allocations. */
390 allocations = 2;
391 item_size = 0;
392 items = calloc(allocations, sizeof(*items));
393 if (!items)
394 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700395
396 /*
397 * The ability to return universal planes is only complete on
398 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
399 * therefore might return an error code, so don't check it. If it
400 * fails, it'll just return the plane list as overlay planes, which is
401 * fine in our case (our drivers already have cursor bits set).
402 * modetest in libdrm does the same thing.
403 */
404 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
405
406 resources = drmModeGetPlaneResources(drv->fd);
407 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800408 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700409
410 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700411 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700412 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800413 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700414
415 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
416 DRM_MODE_OBJECT_PLANE);
417 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800418 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700419
420 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700421 prop = drmModeGetProperty(drv->fd, props->props[j]);
422 if (prop) {
423 if (strcmp(prop->name, "type") == 0) {
424 flag = props->prop_values[j];
425 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800426
Gurchetan Singh179687e2016-10-28 10:07:35 -0700427 drmModeFreeProperty(prop);
428 }
429 }
430
431 switch (flag) {
432 case DRM_PLANE_TYPE_OVERLAY:
433 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800434 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700435 break;
436 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800437 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700438 break;
439 default:
440 assert(0);
441 }
442
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800443 for (j = 0; j < plane->count_formats; j++) {
444 bool found = false;
445 for (k = 0; k < item_size; k++) {
446 if (items[k].format == plane->formats[j] &&
447 items[k].modifier == DRM_FORMAT_MOD_NONE) {
448 items[k].usage |= usage;
449 found = true;
450 break;
451 }
452 }
453
454 if (!found && item_size >= allocations) {
455 struct kms_item *new_data = NULL;
456 allocations *= 2;
457 new_data = realloc(items, allocations *
458 sizeof(*items));
459 if (!new_data) {
460 item_size = 0;
461 goto out;
462 }
463
464 items = new_data;
465 }
466
467 if (!found) {
468 items[item_size].format = plane->formats[j];
469 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
470 items[item_size].usage = usage;
471 item_size++;
472 }
473 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700474
475 drmModeFreeObjectProperties(props);
476 drmModeFreePlane(plane);
477
478 }
479
480 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800481out:
482 if (items && item_size == 0) {
483 free(items);
484 items = NULL;
485 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700486
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800487 *num_items = item_size;
488 return items;
489}
490
491int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
492 uint32_t num_formats)
493{
494 int ret;
495 uint32_t i, j, num_items;
496 struct kms_item *items;
497 struct combination *combo;
498 struct format_metadata metadata;
499
500 metadata.tiling = 0;
501 metadata.priority = 1;
502 metadata.modifier = DRM_FORMAT_MOD_NONE;
503
504 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
505 BO_COMMON_USE_MASK);
506 if (ret)
507 return ret;
508 /*
509 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
510 * plane and as a cursor. Some drivers don't support
511 * drmModeGetPlaneResources, so add the combination here. Note that the
512 * kernel disregards the alpha component of ARGB unless it's an overlay
513 * plane.
514 */
515 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
516 BO_USE_CURSOR | BO_USE_SCANOUT);
517 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
518 BO_USE_CURSOR | BO_USE_SCANOUT);
519
520 items = drv_query_kms(drv, &num_items);
521 if (!items || !num_items)
522 return 0;
523
524 for (i = 0; i < num_items; i++) {
525 for (j = 0; j < drv->backend->combos.size; j++) {
526 combo = &drv->backend->combos.data[j];
527 if (items[i].format == combo->format)
528 combo->usage |= BO_USE_SCANOUT;
529
530
531 }
532 }
533
534 free(items);
535 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700536}