blob: 951093ca068ff1d98c7325e414aa72e53eddc19a [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 Singh4f298f92017-03-23 11:29:26 -0700109 * This function returns the stride for a given format, width and plane.
110 */
111uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
112{
113 uint32_t stride = DIV_ROUND_UP(width * drv_bpp_from_format(format, plane),
114 8);
115
116 /*
117 * Only downsample for certain multiplanar formats which have horizontal
118 * subsampling for chroma planes. Only formats supported by our drivers
119 * are listed here -- add more as needed.
120 */
121 if (plane != 0) {
122 switch (format) {
123 case DRM_FORMAT_NV12:
124 case DRM_FORMAT_YVU420:
125 case DRM_FORMAT_YVU420_ANDROID:
126 stride = DIV_ROUND_UP(stride, 2);
127 break;
128 }
129 }
130
131 /*
132 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
133 * (see <system/graphics.h>).
134 */
135 if (format == DRM_FORMAT_YVU420_ANDROID)
136 stride = ALIGN(stride, 16);
137
138 return stride;
139}
140
141/*
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700142 * This function fills in the buffer object given driver aligned dimensions
Gurchetan Singh03f13562017-02-08 15:21:14 -0800143 * (in pixels) and a format. This function assumes there is just one kernel
144 * buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700145 */
Gurchetan Singh03f13562017-02-08 15:21:14 -0800146int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
147 uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700148{
149
Gurchetan Singh2a119342016-11-02 10:40:51 -0700150 size_t p, num_planes;
151 uint32_t offset = 0;
152
153 num_planes = drv_num_planes_from_format(format);
154 assert(num_planes);
Gurchetan Singh03f13562017-02-08 15:21:14 -0800155 bo->total_size = 0;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700156
157 for (p = 0; p < num_planes; p++) {
Gurchetan Singh03f13562017-02-08 15:21:14 -0800158 bo->strides[p] = drv_stride_from_format(format, aligned_width,
159 p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700160 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
Gurchetan Singh03f13562017-02-08 15:21:14 -0800161 bo->height, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700162 bo->offsets[p] = offset;
163 offset += bo->sizes[p];
Gurchetan Singh03f13562017-02-08 15:21:14 -0800164 bo->total_size += drv_size_from_format(format, bo->strides[p],
165 aligned_height, p);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700166 }
167
168 return 0;
169}
170
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700171int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800172 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700174 int ret;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700175 size_t plane;
176 uint32_t aligned_width, aligned_height, bytes_per_pixel;
177 struct drm_mode_create_dumb create_dumb;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700178
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700179 aligned_width = width;
180 aligned_height = height;
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700181 bytes_per_pixel = drv_bytes_per_pixel(format, 0);
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700182 if (format == DRM_FORMAT_YVU420_ANDROID) {
183 /*
184 * Align width to 16 pixels, so chroma strides are 16 bytes as
185 * Android requires.
186 */
187 aligned_width = ALIGN(width, 32);
188 aligned_height = 3 * DIV_ROUND_UP(height, 2);
189 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500190
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700191 memset(&create_dumb, 0, sizeof(create_dumb));
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700192 create_dumb.height = aligned_height;
193 create_dumb.width = aligned_width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700194 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700195 create_dumb.flags = 0;
196
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700198 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700200 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700201 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700202
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700203 drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel),
204 height, format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700205
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700206 for (plane = 0; plane < bo->num_planes; plane++)
207 bo->handles[plane].u32 = create_dumb.handle;
208
209 bo->total_size = create_dumb.size;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210 return 0;
211}
212
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700213int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214{
215 struct drm_mode_destroy_dumb destroy_dumb;
216 int ret;
217
218 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500219 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700220
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700222 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700223 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500224 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700225 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700226 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700227
228 return 0;
229}
230
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700231int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700232{
233 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500234 int ret, error = 0;
235 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700236
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500237 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700238 for (i = 0; i < plane; i++)
239 if (bo->handles[i].u32 == bo->handles[plane].u32)
240 break;
241 /* Make sure close hasn't already been called on this handle */
242 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500243 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700244
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500245 memset(&gem_close, 0, sizeof(gem_close));
246 gem_close.handle = bo->handles[plane].u32;
247
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700248 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500249 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700250 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500251 "(handle=%x) error %d\n",
252 bo->handles[plane].u32, ret);
253 error = ret;
254 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700255 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700256
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500257 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700258}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700259
Gurchetan Singh71611d62017-01-03 16:49:56 -0800260int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
261{
262 int ret;
263 size_t plane;
264 struct drm_prime_handle prime_handle;
265
266 for (plane = 0; plane < bo->num_planes; plane++) {
267 memset(&prime_handle, 0, sizeof(prime_handle));
268 prime_handle.fd = data->fds[plane];
269
270 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
271 &prime_handle);
272
273 if (ret) {
274 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
275 "failed (fd=%u)\n", prime_handle.fd);
276
277 /*
278 * Need to call GEM close on planes that were opened,
279 * if any. Adjust the num_planes variable to be the
280 * plane that failed, so GEM close will be called on
281 * planes before that plane.
282 */
283 bo->num_planes = plane;
284 drv_gem_bo_destroy(bo);
285 return ret;
286 }
287
288 bo->handles[plane].u32 = prime_handle.handle;
289 }
290
291 for (plane = 0; plane < bo->num_planes; plane++) {
292 pthread_mutex_lock(&bo->drv->driver_lock);
293 drv_increment_reference_count(bo->drv, bo, plane);
294 pthread_mutex_unlock(&bo->drv->driver_lock);
295 }
296
297 return 0;
298}
299
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700300void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700301{
302 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700303 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700304 struct drm_mode_map_dumb map_dumb;
305
306 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700307 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700308
309 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
310 if (ret) {
311 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
312 return MAP_FAILED;
313 }
314
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700315 for (i = 0; i < bo->num_planes; i++)
316 if (bo->handles[i].u32 == bo->handles[plane].u32)
317 data->length += bo->sizes[i];
318
319 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700320 bo->drv->fd, map_dumb.offset);
321}
322
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700323uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
324 size_t plane)
325{
326 void *count;
327 uintptr_t num = 0;
328
329 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
330 num = (uintptr_t) (count);
331
332 return num;
333}
334
335void drv_increment_reference_count(struct driver *drv, struct bo *bo,
336 size_t plane)
337{
338 uintptr_t num = drv_get_reference_count(drv, bo, plane);
339
340 /* If a value isn't in the table, drmHashDelete is a no-op */
341 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
342 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
343 (void *) (num + 1));
344}
345
346void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
347 size_t plane)
348{
349 uintptr_t num = drv_get_reference_count(drv, bo, plane);
350
351 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
352
353 if (num > 0)
354 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
355 (void *) (num - 1));
356}
Gurchetan Singhef920532016-08-12 16:38:25 -0700357
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530358uint32_t drv_log_base2(uint32_t value)
359{
360 int ret = 0;
361
362 while (value >>= 1)
363 ++ret;
364
365 return ret;
366}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700367
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800368int drv_add_combination(struct driver *drv, uint32_t format,
369 struct format_metadata *metadata, uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700370{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800371 struct combinations *combos = &drv->backend->combos;
372 if (combos->size >= combos->allocations) {
373 struct combination *new_data;
374 combos->allocations *= 2;
375 new_data = realloc(combos->data, combos->allocations
376 * sizeof(*combos->data));
377 if (!new_data)
378 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700379
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800380 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700381 }
382
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800383 combos->data[combos->size].format = format;
384 combos->data[combos->size].metadata.priority = metadata->priority;
385 combos->data[combos->size].metadata.tiling = metadata->tiling;
386 combos->data[combos->size].metadata.modifier = metadata->modifier;
387 combos->data[combos->size].usage = usage;
388 combos->size++;
389 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700390}
391
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800392int drv_add_combinations(struct driver *drv, const uint32_t *formats,
393 uint32_t num_formats, struct format_metadata *metadata,
394 uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700395{
396 int ret;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800397 uint32_t i;
398 for (i = 0; i < num_formats; i++) {
399 ret = drv_add_combination(drv, formats[i], metadata, usage);
400 if (ret)
401 return ret;
402 }
403
404 return 0;
405}
406
407void drv_modify_combination(struct driver *drv, uint32_t format,
408 struct format_metadata *metadata, uint64_t usage)
409{
410 uint32_t i;
411 struct combination *combo;
412 /* Attempts to add the specified usage to an existing combination. */
413 for (i = 0; i < drv->backend->combos.size; i++) {
414 combo = &drv->backend->combos.data[i];
415 if (combo->format == format &&
416 combo->metadata.tiling == metadata->tiling &&
417 combo->metadata.modifier == metadata->modifier)
418 combo->usage |= usage;
419 }
420}
421
422struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
423{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700424 uint64_t flag, usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800425 struct kms_item *items;
426 uint32_t i, j, k, allocations, item_size;
427
Gurchetan Singh179687e2016-10-28 10:07:35 -0700428 drmModePlanePtr plane;
429 drmModePropertyPtr prop;
430 drmModePlaneResPtr resources;
431 drmModeObjectPropertiesPtr props;
432
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800433 /* Start with a power of 2 number of allocations. */
434 allocations = 2;
435 item_size = 0;
436 items = calloc(allocations, sizeof(*items));
437 if (!items)
438 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700439
440 /*
441 * The ability to return universal planes is only complete on
442 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
443 * therefore might return an error code, so don't check it. If it
444 * fails, it'll just return the plane list as overlay planes, which is
445 * fine in our case (our drivers already have cursor bits set).
446 * modetest in libdrm does the same thing.
447 */
448 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
449
450 resources = drmModeGetPlaneResources(drv->fd);
451 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800452 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700453
454 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700455 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700456 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800457 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700458
459 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
460 DRM_MODE_OBJECT_PLANE);
461 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800462 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700463
464 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700465 prop = drmModeGetProperty(drv->fd, props->props[j]);
466 if (prop) {
467 if (strcmp(prop->name, "type") == 0) {
468 flag = props->prop_values[j];
469 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800470
Gurchetan Singh179687e2016-10-28 10:07:35 -0700471 drmModeFreeProperty(prop);
472 }
473 }
474
475 switch (flag) {
476 case DRM_PLANE_TYPE_OVERLAY:
477 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800478 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700479 break;
480 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800481 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700482 break;
483 default:
484 assert(0);
485 }
486
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800487 for (j = 0; j < plane->count_formats; j++) {
488 bool found = false;
489 for (k = 0; k < item_size; k++) {
490 if (items[k].format == plane->formats[j] &&
491 items[k].modifier == DRM_FORMAT_MOD_NONE) {
492 items[k].usage |= usage;
493 found = true;
494 break;
495 }
496 }
497
498 if (!found && item_size >= allocations) {
499 struct kms_item *new_data = NULL;
500 allocations *= 2;
501 new_data = realloc(items, allocations *
502 sizeof(*items));
503 if (!new_data) {
504 item_size = 0;
505 goto out;
506 }
507
508 items = new_data;
509 }
510
511 if (!found) {
512 items[item_size].format = plane->formats[j];
513 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
514 items[item_size].usage = usage;
515 item_size++;
516 }
517 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700518
519 drmModeFreeObjectProperties(props);
520 drmModeFreePlane(plane);
521
522 }
523
524 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800525out:
526 if (items && item_size == 0) {
527 free(items);
528 items = NULL;
529 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700530
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800531 *num_items = item_size;
532 return items;
533}
534
535int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
536 uint32_t num_formats)
537{
538 int ret;
539 uint32_t i, j, num_items;
540 struct kms_item *items;
541 struct combination *combo;
542 struct format_metadata metadata;
543
544 metadata.tiling = 0;
545 metadata.priority = 1;
546 metadata.modifier = DRM_FORMAT_MOD_NONE;
547
548 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
549 BO_COMMON_USE_MASK);
550 if (ret)
551 return ret;
552 /*
553 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
554 * plane and as a cursor. Some drivers don't support
555 * drmModeGetPlaneResources, so add the combination here. Note that the
556 * kernel disregards the alpha component of ARGB unless it's an overlay
557 * plane.
558 */
559 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
560 BO_USE_CURSOR | BO_USE_SCANOUT);
561 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
562 BO_USE_CURSOR | BO_USE_SCANOUT);
563
564 items = drv_query_kms(drv, &num_items);
565 if (!items || !num_items)
566 return 0;
567
568 for (i = 0; i < num_items; i++) {
569 for (j = 0; j < drv->backend->combos.size; j++) {
570 combo = &drv->backend->combos.data[j];
571 if (items[i].format == combo->format)
572 combo->usage |= BO_USE_SCANOUT;
573
574
575 }
576 }
577
578 free(items);
579 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700580}