blob: 806c152ee6ff677f1f745cbefa0ab2316721ba1e [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 Singh1b1d56a2017-03-10 16:25:23 -080021static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
22{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -070023
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:
Shirish Sdf423df2017-04-18 16:21:59 +053050 case DRM_FORMAT_NV21:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -070051 return (plane == 0) ? 8 : 4;
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 Singh6423ecb2017-03-29 08:23:40 -0700107uint32_t drv_bo_get_stride_in_pixels(struct bo *bo)
108{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800109 uint32_t bytes_per_pixel = DIV_ROUND_UP(bpp_from_format(bo->format, 0), 8);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700110 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 Singh1b1d56a2017-03-10 16:25:23 -0800118 uint32_t stride = DIV_ROUND_UP(width * bpp_from_format(format, plane), 8);
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700119
120 /*
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700121 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
122 * (see <system/graphics.h>).
123 */
124 if (format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800125 stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700126
127 return stride;
128}
129
130/*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700131 * This function fills in the buffer object given the driver aligned stride of
132 * the first plane, height and a format. This function assumes there is just
133 * one kernel buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700134 */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800135int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700136{
137
Gurchetan Singh2a119342016-11-02 10:40:51 -0700138 size_t p, num_planes;
139 uint32_t offset = 0;
140
141 num_planes = drv_num_planes_from_format(format);
142 assert(num_planes);
Owen Linbbb69fd2017-06-05 14:33:08 +0800143
144 /*
145 * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
146 * - the aligned height is same as the buffer's height.
147 * - the chroma stride is 16 bytes aligned, i.e., the luma's strides
148 * is 32 bytes aligned.
149 */
150 if (bo->format == DRM_FORMAT_YVU420_ANDROID) {
151 assert(aligned_height == bo->height);
152 assert(stride == ALIGN(stride, 32));
153 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700154
155 for (p = 0; p < num_planes; p++) {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700156 bo->strides[p] = subsample_stride(stride, format, p);
Owen Linbbb69fd2017-06-05 14:33:08 +0800157 bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700158 bo->offsets[p] = offset;
159 offset += bo->sizes[p];
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700160 }
161
Owen Linbbb69fd2017-06-05 14:33:08 +0800162 bo->total_size = offset;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700163 return 0;
164}
165
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800166int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
167 uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700168{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700169 int ret;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700170 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700171 uint32_t aligned_width, aligned_height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700172 struct drm_mode_create_dumb create_dumb;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700174 aligned_width = width;
175 aligned_height = height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700176 if (format == DRM_FORMAT_YVU420_ANDROID) {
177 /*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700178 * Align width to 32 pixels, so chroma strides are 16 bytes as
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700179 * Android requires.
180 */
181 aligned_width = ALIGN(width, 32);
Owen Linbbb69fd2017-06-05 14:33:08 +0800182 }
183
184 if (format == DRM_FORMAT_YVU420_ANDROID || format == DRM_FORMAT_YVU420) {
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700185 aligned_height = 3 * DIV_ROUND_UP(height, 2);
186 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500187
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700188 memset(&create_dumb, 0, sizeof(create_dumb));
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700189 create_dumb.height = aligned_height;
190 create_dumb.width = aligned_width;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700191 create_dumb.bpp = bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700192 create_dumb.flags = 0;
193
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700194 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700195 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700196 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700197 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700198 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700199
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700200 drv_bo_from_format(bo, create_dumb.pitch, height, format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700201
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700202 for (plane = 0; plane < bo->num_planes; plane++)
203 bo->handles[plane].u32 = create_dumb.handle;
204
205 bo->total_size = create_dumb.size;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700206 return 0;
207}
208
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210{
211 struct drm_mode_destroy_dumb destroy_dumb;
212 int ret;
213
214 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500215 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700216
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700218 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700219 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800220 bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700221 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700222 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700223
224 return 0;
225}
226
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700228{
229 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500230 int ret, error = 0;
231 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700232
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500233 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700234 for (i = 0; i < plane; i++)
235 if (bo->handles[i].u32 == bo->handles[plane].u32)
236 break;
237 /* Make sure close hasn't already been called on this handle */
238 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500239 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700240
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500241 memset(&gem_close, 0, sizeof(gem_close));
242 gem_close.handle = bo->handles[plane].u32;
243
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700244 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500245 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700246 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800247 bo->handles[plane].u32, ret);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500248 error = ret;
249 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700250 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700251
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500252 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700253}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700254
Gurchetan Singh71611d62017-01-03 16:49:56 -0800255int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
256{
257 int ret;
258 size_t plane;
259 struct drm_prime_handle prime_handle;
260
261 for (plane = 0; plane < bo->num_planes; plane++) {
262 memset(&prime_handle, 0, sizeof(prime_handle));
263 prime_handle.fd = data->fds[plane];
264
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800265 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800266
267 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700268 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800269 prime_handle.fd);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800270
271 /*
272 * Need to call GEM close on planes that were opened,
273 * if any. Adjust the num_planes variable to be the
274 * plane that failed, so GEM close will be called on
275 * planes before that plane.
276 */
277 bo->num_planes = plane;
278 drv_gem_bo_destroy(bo);
279 return ret;
280 }
281
282 bo->handles[plane].u32 = prime_handle.handle;
283 }
284
285 for (plane = 0; plane < bo->num_planes; plane++) {
286 pthread_mutex_lock(&bo->drv->driver_lock);
287 drv_increment_reference_count(bo->drv, bo, plane);
288 pthread_mutex_unlock(&bo->drv->driver_lock);
289 }
290
291 return 0;
292}
293
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700294void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700295{
296 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700297 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700298 struct drm_mode_map_dumb map_dumb;
299
300 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700301 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700302
303 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
304 if (ret) {
305 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
306 return MAP_FAILED;
307 }
308
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700309 for (i = 0; i < bo->num_planes; i++)
310 if (bo->handles[i].u32 == bo->handles[plane].u32)
311 data->length += bo->sizes[i];
312
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800313 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd,
314 map_dumb.offset);
Gurchetan Singhef920532016-08-12 16:38:25 -0700315}
316
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800317uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700318{
319 void *count;
320 uintptr_t num = 0;
321
322 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800323 num = (uintptr_t)(count);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324
325 return num;
326}
327
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800328void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700329{
330 uintptr_t num = drv_get_reference_count(drv, bo, plane);
331
332 /* If a value isn't in the table, drmHashDelete is a no-op */
333 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800334 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700335}
336
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800337void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700338{
339 uintptr_t num = drv_get_reference_count(drv, bo, plane);
340
341 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
342
343 if (num > 0)
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800344 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700345}
Gurchetan Singhef920532016-08-12 16:38:25 -0700346
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530347uint32_t drv_log_base2(uint32_t value)
348{
349 int ret = 0;
350
351 while (value >>= 1)
352 ++ret;
353
354 return ret;
355}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700356
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800357int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
358 uint64_t usage)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700359{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800360 struct combinations *combos = &drv->backend->combos;
361 if (combos->size >= combos->allocations) {
362 struct combination *new_data;
363 combos->allocations *= 2;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800364 new_data = realloc(combos->data, combos->allocations * sizeof(*combos->data));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800365 if (!new_data)
366 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700367
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800368 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700369 }
370
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800371 combos->data[combos->size].format = format;
372 combos->data[combos->size].metadata.priority = metadata->priority;
373 combos->data[combos->size].metadata.tiling = metadata->tiling;
374 combos->data[combos->size].metadata.modifier = metadata->modifier;
375 combos->data[combos->size].usage = usage;
376 combos->size++;
377 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700378}
379
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800380int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
381 struct format_metadata *metadata, 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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800394void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
395 uint64_t usage)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800396{
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];
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800402 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800403 combo->metadata.modifier == metadata->modifier)
404 combo->usage |= usage;
405 }
406}
407
408struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
409{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700410 uint64_t flag, usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800411 struct kms_item *items;
412 uint32_t i, j, k, allocations, item_size;
413
Gurchetan Singh179687e2016-10-28 10:07:35 -0700414 drmModePlanePtr plane;
415 drmModePropertyPtr prop;
416 drmModePlaneResPtr resources;
417 drmModeObjectPropertiesPtr props;
418
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800419 /* Start with a power of 2 number of allocations. */
420 allocations = 2;
421 item_size = 0;
422 items = calloc(allocations, sizeof(*items));
423 if (!items)
424 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700425
426 /*
427 * The ability to return universal planes is only complete on
428 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
429 * therefore might return an error code, so don't check it. If it
430 * fails, it'll just return the plane list as overlay planes, which is
431 * fine in our case (our drivers already have cursor bits set).
432 * modetest in libdrm does the same thing.
433 */
434 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
435
436 resources = drmModeGetPlaneResources(drv->fd);
437 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800438 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700439
440 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700441 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700442 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800443 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700444
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800445 props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700446 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800447 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700448
449 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700450 prop = drmModeGetProperty(drv->fd, props->props[j]);
451 if (prop) {
452 if (strcmp(prop->name, "type") == 0) {
453 flag = props->prop_values[j];
454 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800455
Gurchetan Singh179687e2016-10-28 10:07:35 -0700456 drmModeFreeProperty(prop);
457 }
458 }
459
460 switch (flag) {
461 case DRM_PLANE_TYPE_OVERLAY:
462 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800463 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700464 break;
465 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800466 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700467 break;
468 default:
469 assert(0);
470 }
471
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800472 for (j = 0; j < plane->count_formats; j++) {
473 bool found = false;
474 for (k = 0; k < item_size; k++) {
475 if (items[k].format == plane->formats[j] &&
476 items[k].modifier == DRM_FORMAT_MOD_NONE) {
477 items[k].usage |= usage;
478 found = true;
479 break;
480 }
481 }
482
483 if (!found && item_size >= allocations) {
484 struct kms_item *new_data = NULL;
485 allocations *= 2;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800486 new_data = realloc(items, allocations * sizeof(*items));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800487 if (!new_data) {
488 item_size = 0;
489 goto out;
490 }
491
492 items = new_data;
493 }
494
495 if (!found) {
496 items[item_size].format = plane->formats[j];
497 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
498 items[item_size].usage = usage;
499 item_size++;
500 }
501 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700502
503 drmModeFreeObjectProperties(props);
504 drmModeFreePlane(plane);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700505 }
506
507 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800508out:
509 if (items && item_size == 0) {
510 free(items);
511 items = NULL;
512 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700513
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800514 *num_items = item_size;
515 return items;
516}
517
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700518int drv_modify_linear_combinations(struct driver *drv)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800519{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800520 uint32_t i, j, num_items;
521 struct kms_item *items;
522 struct combination *combo;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800523
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800524 /*
525 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
526 * plane and as a cursor. Some drivers don't support
527 * drmModeGetPlaneResources, so add the combination here. Note that the
528 * kernel disregards the alpha component of ARGB unless it's an overlay
529 * plane.
530 */
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700531 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
532 BO_USE_CURSOR | BO_USE_SCANOUT);
533 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
534 BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800535
536 items = drv_query_kms(drv, &num_items);
537 if (!items || !num_items)
538 return 0;
539
540 for (i = 0; i < num_items; i++) {
541 for (j = 0; j < drv->backend->combos.size; j++) {
542 combo = &drv->backend->combos.data[j];
543 if (items[i].format == combo->format)
544 combo->usage |= BO_USE_SCANOUT;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800545 }
546 }
547
548 free(items);
549 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700550}