blob: 800d314d39d20bba977f661d5aadc6d2498db932 [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 Singh6423ecb2017-03-29 08:23:40 -070021static uint32_t subsample_stride(uint32_t stride, uint32_t format,
22 size_t plane) {
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
36static uint32_t bpp_from_format(uint32_t format, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070037{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070038 assert(plane < drv_num_planes_from_format(format));
39
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070040 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080041 case DRM_FORMAT_BGR233:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080042 case DRM_FORMAT_C8:
43 case DRM_FORMAT_R8:
44 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080045 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -080046 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070047 return 8;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070048
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080049 case DRM_FORMAT_NV12:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -070050 return (plane == 0) ? 8 : 4;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050051
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080052 case DRM_FORMAT_ABGR1555:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080053 case DRM_FORMAT_ABGR4444:
54 case DRM_FORMAT_ARGB1555:
55 case DRM_FORMAT_ARGB4444:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080056 case DRM_FORMAT_BGR565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080057 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:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080068 case DRM_FORMAT_UYVY:
69 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080070 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070076 return 16;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080078 case DRM_FORMAT_BGR888:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080079 case DRM_FORMAT_RGB888:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070080 return 24;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070081
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080082 case DRM_FORMAT_ABGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080083 case DRM_FORMAT_ABGR8888:
84 case DRM_FORMAT_ARGB2101010:
85 case DRM_FORMAT_ARGB8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080086 case DRM_FORMAT_AYUV:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080087 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:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070099 return 32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700100 }
101
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700103 return 0;
104}
105
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700106uint32_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),
109 8);
110 return DIV_ROUND_UP(bo->strides[0], bytes_per_pixel);
111}
112
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700113/*
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700114 * This function returns the stride for a given format, width and plane.
115 */
116uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
117{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700118 uint32_t stride = DIV_ROUND_UP(width * bpp_from_format(format, plane),
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700119 8);
120
121 /*
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700122 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
123 * (see <system/graphics.h>).
124 */
125 if (format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700126 stride = (plane == 0) ? ALIGN(stride, 32): ALIGN(stride, 16);
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700127
128 return stride;
129}
130
131/*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700132 * This function fills in the buffer object given the driver aligned stride of
133 * the first plane, height and a format. This function assumes there is just
134 * one kernel buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700135 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700136int drv_bo_from_format(struct bo *bo, uint32_t stride,
Gurchetan Singh03f13562017-02-08 15:21:14 -0800137 uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700138{
139
Gurchetan Singh2a119342016-11-02 10:40:51 -0700140 size_t p, num_planes;
141 uint32_t offset = 0;
142
143 num_planes = drv_num_planes_from_format(format);
144 assert(num_planes);
Gurchetan Singh03f13562017-02-08 15:21:14 -0800145 bo->total_size = 0;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700146
147 for (p = 0; p < num_planes; p++) {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700148 bo->strides[p] = subsample_stride(stride, format, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700149 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
Gurchetan Singh03f13562017-02-08 15:21:14 -0800150 bo->height, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700151 bo->offsets[p] = offset;
152 offset += bo->sizes[p];
Gurchetan Singh03f13562017-02-08 15:21:14 -0800153 bo->total_size += drv_size_from_format(format, bo->strides[p],
154 aligned_height, p);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700155 }
156
157 return 0;
158}
159
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700160int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800161 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700162{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700163 int ret;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700164 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700165 uint32_t aligned_width, aligned_height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700166 struct drm_mode_create_dumb create_dumb;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700167
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700168 aligned_width = width;
169 aligned_height = height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700170 if (format == DRM_FORMAT_YVU420_ANDROID) {
171 /*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700172 * Align width to 32 pixels, so chroma strides are 16 bytes as
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700173 * Android requires.
174 */
175 aligned_width = ALIGN(width, 32);
176 aligned_height = 3 * DIV_ROUND_UP(height, 2);
177 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500178
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700179 memset(&create_dumb, 0, sizeof(create_dumb));
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700180 create_dumb.height = aligned_height;
181 create_dumb.width = aligned_width;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700182 create_dumb.bpp = bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700183 create_dumb.flags = 0;
184
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700185 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700186 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700187 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700188 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700189 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700190
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700191 drv_bo_from_format(bo, create_dumb.pitch, height, format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700192
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700193 for (plane = 0; plane < bo->num_planes; plane++)
194 bo->handles[plane].u32 = create_dumb.handle;
195
196 bo->total_size = create_dumb.size;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700197 return 0;
198}
199
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700201{
202 struct drm_mode_destroy_dumb destroy_dumb;
203 int ret;
204
205 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500206 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700207
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700209 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700210 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500211 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700212 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700213 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214
215 return 0;
216}
217
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700219{
220 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500221 int ret, error = 0;
222 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700223
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500224 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700225 for (i = 0; i < plane; i++)
226 if (bo->handles[i].u32 == bo->handles[plane].u32)
227 break;
228 /* Make sure close hasn't already been called on this handle */
229 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500230 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700231
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500232 memset(&gem_close, 0, sizeof(gem_close));
233 gem_close.handle = bo->handles[plane].u32;
234
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500236 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500238 "(handle=%x) error %d\n",
239 bo->handles[plane].u32, ret);
240 error = ret;
241 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700242 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700243
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500244 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700245}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700246
Gurchetan Singh71611d62017-01-03 16:49:56 -0800247int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
248{
249 int ret;
250 size_t plane;
251 struct drm_prime_handle prime_handle;
252
253 for (plane = 0; plane < bo->num_planes; plane++) {
254 memset(&prime_handle, 0, sizeof(prime_handle));
255 prime_handle.fd = data->fds[plane];
256
257 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
258 &prime_handle);
259
260 if (ret) {
261 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
262 "failed (fd=%u)\n", prime_handle.fd);
263
264 /*
265 * Need to call GEM close on planes that were opened,
266 * if any. Adjust the num_planes variable to be the
267 * plane that failed, so GEM close will be called on
268 * planes before that plane.
269 */
270 bo->num_planes = plane;
271 drv_gem_bo_destroy(bo);
272 return ret;
273 }
274
275 bo->handles[plane].u32 = prime_handle.handle;
276 }
277
278 for (plane = 0; plane < bo->num_planes; plane++) {
279 pthread_mutex_lock(&bo->drv->driver_lock);
280 drv_increment_reference_count(bo->drv, bo, plane);
281 pthread_mutex_unlock(&bo->drv->driver_lock);
282 }
283
284 return 0;
285}
286
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700287void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700288{
289 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700290 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700291 struct drm_mode_map_dumb map_dumb;
292
293 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700294 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700295
296 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
297 if (ret) {
298 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
299 return MAP_FAILED;
300 }
301
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700302 for (i = 0; i < bo->num_planes; i++)
303 if (bo->handles[i].u32 == bo->handles[plane].u32)
304 data->length += bo->sizes[i];
305
306 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700307 bo->drv->fd, map_dumb.offset);
308}
309
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700310uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
311 size_t plane)
312{
313 void *count;
314 uintptr_t num = 0;
315
316 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
317 num = (uintptr_t) (count);
318
319 return num;
320}
321
322void drv_increment_reference_count(struct driver *drv, struct bo *bo,
323 size_t plane)
324{
325 uintptr_t num = drv_get_reference_count(drv, bo, plane);
326
327 /* If a value isn't in the table, drmHashDelete is a no-op */
328 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
329 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
330 (void *) (num + 1));
331}
332
333void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
334 size_t plane)
335{
336 uintptr_t num = drv_get_reference_count(drv, bo, plane);
337
338 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
339
340 if (num > 0)
341 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
342 (void *) (num - 1));
343}
Gurchetan Singhef920532016-08-12 16:38:25 -0700344
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530345uint32_t drv_log_base2(uint32_t value)
346{
347 int ret = 0;
348
349 while (value >>= 1)
350 ++ret;
351
352 return ret;
353}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700354
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800355int drv_add_combination(struct driver *drv, uint32_t format,
356 struct format_metadata *metadata, uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700357{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800358 struct combinations *combos = &drv->backend->combos;
359 if (combos->size >= combos->allocations) {
360 struct combination *new_data;
361 combos->allocations *= 2;
362 new_data = realloc(combos->data, combos->allocations
363 * sizeof(*combos->data));
364 if (!new_data)
365 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700366
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800367 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700368 }
369
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800370 combos->data[combos->size].format = format;
371 combos->data[combos->size].metadata.priority = metadata->priority;
372 combos->data[combos->size].metadata.tiling = metadata->tiling;
373 combos->data[combos->size].metadata.modifier = metadata->modifier;
374 combos->data[combos->size].usage = usage;
375 combos->size++;
376 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700377}
378
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800379int drv_add_combinations(struct driver *drv, const uint32_t *formats,
380 uint32_t num_formats, struct format_metadata *metadata,
381 uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700382{
383 int ret;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800384 uint32_t i;
385 for (i = 0; i < num_formats; i++) {
386 ret = drv_add_combination(drv, formats[i], metadata, usage);
387 if (ret)
388 return ret;
389 }
390
391 return 0;
392}
393
394void drv_modify_combination(struct driver *drv, uint32_t format,
395 struct format_metadata *metadata, uint64_t usage)
396{
397 uint32_t i;
398 struct combination *combo;
399 /* Attempts to add the specified usage to an existing combination. */
400 for (i = 0; i < drv->backend->combos.size; i++) {
401 combo = &drv->backend->combos.data[i];
402 if (combo->format == format &&
403 combo->metadata.tiling == metadata->tiling &&
404 combo->metadata.modifier == metadata->modifier)
405 combo->usage |= usage;
406 }
407}
408
409struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
410{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700411 uint64_t flag, usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800412 struct kms_item *items;
413 uint32_t i, j, k, allocations, item_size;
414
Gurchetan Singh179687e2016-10-28 10:07:35 -0700415 drmModePlanePtr plane;
416 drmModePropertyPtr prop;
417 drmModePlaneResPtr resources;
418 drmModeObjectPropertiesPtr props;
419
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800420 /* Start with a power of 2 number of allocations. */
421 allocations = 2;
422 item_size = 0;
423 items = calloc(allocations, sizeof(*items));
424 if (!items)
425 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700426
427 /*
428 * The ability to return universal planes is only complete on
429 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
430 * therefore might return an error code, so don't check it. If it
431 * fails, it'll just return the plane list as overlay planes, which is
432 * fine in our case (our drivers already have cursor bits set).
433 * modetest in libdrm does the same thing.
434 */
435 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
436
437 resources = drmModeGetPlaneResources(drv->fd);
438 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800439 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700440
441 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700442 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700443 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800444 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700445
446 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
447 DRM_MODE_OBJECT_PLANE);
448 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800449 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700450
451 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700452 prop = drmModeGetProperty(drv->fd, props->props[j]);
453 if (prop) {
454 if (strcmp(prop->name, "type") == 0) {
455 flag = props->prop_values[j];
456 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800457
Gurchetan Singh179687e2016-10-28 10:07:35 -0700458 drmModeFreeProperty(prop);
459 }
460 }
461
462 switch (flag) {
463 case DRM_PLANE_TYPE_OVERLAY:
464 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800465 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700466 break;
467 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800468 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700469 break;
470 default:
471 assert(0);
472 }
473
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800474 for (j = 0; j < plane->count_formats; j++) {
475 bool found = false;
476 for (k = 0; k < item_size; k++) {
477 if (items[k].format == plane->formats[j] &&
478 items[k].modifier == DRM_FORMAT_MOD_NONE) {
479 items[k].usage |= usage;
480 found = true;
481 break;
482 }
483 }
484
485 if (!found && item_size >= allocations) {
486 struct kms_item *new_data = NULL;
487 allocations *= 2;
488 new_data = realloc(items, allocations *
489 sizeof(*items));
490 if (!new_data) {
491 item_size = 0;
492 goto out;
493 }
494
495 items = new_data;
496 }
497
498 if (!found) {
499 items[item_size].format = plane->formats[j];
500 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
501 items[item_size].usage = usage;
502 item_size++;
503 }
504 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700505
506 drmModeFreeObjectProperties(props);
507 drmModeFreePlane(plane);
508
509 }
510
511 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800512out:
513 if (items && item_size == 0) {
514 free(items);
515 items = NULL;
516 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700517
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800518 *num_items = item_size;
519 return items;
520}
521
522int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
523 uint32_t num_formats)
524{
525 int ret;
526 uint32_t i, j, num_items;
527 struct kms_item *items;
528 struct combination *combo;
529 struct format_metadata metadata;
530
531 metadata.tiling = 0;
532 metadata.priority = 1;
533 metadata.modifier = DRM_FORMAT_MOD_NONE;
534
535 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
536 BO_COMMON_USE_MASK);
537 if (ret)
538 return ret;
539 /*
540 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
541 * plane and as a cursor. Some drivers don't support
542 * drmModeGetPlaneResources, so add the combination here. Note that the
543 * kernel disregards the alpha component of ARGB unless it's an overlay
544 * plane.
545 */
546 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
547 BO_USE_CURSOR | BO_USE_SCANOUT);
548 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
549 BO_USE_CURSOR | BO_USE_SCANOUT);
550
551 items = drv_query_kms(drv, &num_items);
552 if (!items || !num_items)
553 return 0;
554
555 for (i = 0; i < num_items; i++) {
556 for (j = 0; j < drv->backend->combos.size; j++) {
557 combo = &drv->backend->combos.data[j];
558 if (items[i].format == combo->format)
559 combo->usage |= BO_USE_SCANOUT;
560
561
562 }
563 }
564
565 free(items);
566 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700567}