blob: 9a3380416c29896cd5d7b3efeb8f2baa0d791c73 [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
Gurchetan Singhbc24bd72017-09-28 15:21:53 -0700130uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
131{
132 assert(plane < drv_num_planes_from_format(format));
133 uint32_t vertical_subsampling;
134
135 switch (format) {
136 case DRM_FORMAT_NV12:
137 case DRM_FORMAT_YVU420:
138 case DRM_FORMAT_YVU420_ANDROID:
139 vertical_subsampling = (plane == 0) ? 1 : 2;
140 break;
141 default:
142 vertical_subsampling = 1;
143 }
144
145 return stride * DIV_ROUND_UP(height, vertical_subsampling);
146}
147
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700148/*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700149 * This function fills in the buffer object given the driver aligned stride of
150 * the first plane, height and a format. This function assumes there is just
151 * one kernel buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700152 */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800153int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700154{
155
Gurchetan Singh2a119342016-11-02 10:40:51 -0700156 size_t p, num_planes;
157 uint32_t offset = 0;
158
159 num_planes = drv_num_planes_from_format(format);
160 assert(num_planes);
Owen Linbbb69fd2017-06-05 14:33:08 +0800161
162 /*
163 * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
164 * - the aligned height is same as the buffer's height.
165 * - the chroma stride is 16 bytes aligned, i.e., the luma's strides
166 * is 32 bytes aligned.
167 */
Tomasz Figad846de62017-07-29 15:47:54 +0900168 if (format == DRM_FORMAT_YVU420_ANDROID) {
Owen Linbbb69fd2017-06-05 14:33:08 +0800169 assert(aligned_height == bo->height);
170 assert(stride == ALIGN(stride, 32));
171 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700172
173 for (p = 0; p < num_planes; p++) {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700174 bo->strides[p] = subsample_stride(stride, format, p);
Owen Linbbb69fd2017-06-05 14:33:08 +0800175 bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
Gurchetan Singh2a119342016-11-02 10:40:51 -0700176 bo->offsets[p] = offset;
177 offset += bo->sizes[p];
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700178 }
179
Owen Linbbb69fd2017-06-05 14:33:08 +0800180 bo->total_size = offset;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700181 return 0;
182}
183
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800184int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700185 uint64_t use_flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700186{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700187 int ret;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700188 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700189 uint32_t aligned_width, aligned_height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700190 struct drm_mode_create_dumb create_dumb;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700191
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700192 aligned_width = width;
193 aligned_height = height;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700194 if (format == DRM_FORMAT_YVU420_ANDROID) {
195 /*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700196 * Align width to 32 pixels, so chroma strides are 16 bytes as
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700197 * Android requires.
198 */
199 aligned_width = ALIGN(width, 32);
Owen Linbbb69fd2017-06-05 14:33:08 +0800200 }
201
202 if (format == DRM_FORMAT_YVU420_ANDROID || format == DRM_FORMAT_YVU420) {
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700203 aligned_height = 3 * DIV_ROUND_UP(height, 2);
204 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500205
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700206 memset(&create_dumb, 0, sizeof(create_dumb));
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700207 create_dumb.height = aligned_height;
208 create_dumb.width = aligned_width;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700209 create_dumb.bpp = bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210 create_dumb.flags = 0;
211
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700212 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700213 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700215 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700216 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700218 drv_bo_from_format(bo, create_dumb.pitch, height, format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700219
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700220 for (plane = 0; plane < bo->num_planes; plane++)
221 bo->handles[plane].u32 = create_dumb.handle;
222
223 bo->total_size = create_dumb.size;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700224 return 0;
225}
226
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700228{
229 struct drm_mode_destroy_dumb destroy_dumb;
230 int ret;
231
232 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500233 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700234
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700236 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700237 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800238 bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700239 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700240 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700241
242 return 0;
243}
244
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700245int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700246{
247 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500248 int ret, error = 0;
249 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700250
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500251 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700252 for (i = 0; i < plane; i++)
253 if (bo->handles[i].u32 == bo->handles[plane].u32)
254 break;
255 /* Make sure close hasn't already been called on this handle */
256 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500257 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700258
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500259 memset(&gem_close, 0, sizeof(gem_close));
260 gem_close.handle = bo->handles[plane].u32;
261
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700262 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500263 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700264 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800265 bo->handles[plane].u32, ret);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500266 error = ret;
267 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700268 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700269
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500270 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700271}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700272
Gurchetan Singh71611d62017-01-03 16:49:56 -0800273int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
274{
275 int ret;
276 size_t plane;
277 struct drm_prime_handle prime_handle;
278
279 for (plane = 0; plane < bo->num_planes; plane++) {
280 memset(&prime_handle, 0, sizeof(prime_handle));
281 prime_handle.fd = data->fds[plane];
282
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800283 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800284
285 if (ret) {
Gurchetan Singh085bff12017-03-20 13:05:49 -0700286 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800287 prime_handle.fd);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800288
289 /*
290 * Need to call GEM close on planes that were opened,
291 * if any. Adjust the num_planes variable to be the
292 * plane that failed, so GEM close will be called on
293 * planes before that plane.
294 */
295 bo->num_planes = plane;
296 drv_gem_bo_destroy(bo);
297 return ret;
298 }
299
300 bo->handles[plane].u32 = prime_handle.handle;
301 }
302
303 for (plane = 0; plane < bo->num_planes; plane++) {
304 pthread_mutex_lock(&bo->drv->driver_lock);
305 drv_increment_reference_count(bo->drv, bo, plane);
306 pthread_mutex_unlock(&bo->drv->driver_lock);
307 }
308
309 return 0;
310}
311
Gurchetan Singhee43c302017-11-14 18:20:27 -0800312void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700313{
314 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700315 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700316 struct drm_mode_map_dumb map_dumb;
317
318 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700319 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700320
321 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
322 if (ret) {
323 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
324 return MAP_FAILED;
325 }
326
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700327 for (i = 0; i < bo->num_planes; i++)
328 if (bo->handles[i].u32 == bo->handles[plane].u32)
Gurchetan Singhee43c302017-11-14 18:20:27 -0800329 vma->length += bo->sizes[i];
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700330
Gurchetan Singhee43c302017-11-14 18:20:27 -0800331 return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700332 map_dumb.offset);
Gurchetan Singhef920532016-08-12 16:38:25 -0700333}
334
Gurchetan Singhee43c302017-11-14 18:20:27 -0800335int drv_bo_munmap(struct bo *bo, struct vma *vma)
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700336{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800337 return munmap(vma->addr, vma->length);
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700338}
339
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700340int drv_mapping_destroy(struct bo *bo)
Gurchetan Singhde263f12017-01-24 13:30:06 -0800341{
342 int ret;
Gurchetan Singhde263f12017-01-24 13:30:06 -0800343 size_t plane;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700344 struct mapping *mapping;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700345 uint32_t idx;
Gurchetan Singhde263f12017-01-24 13:30:06 -0800346
347 /*
348 * This function is called right before the buffer is destroyed. It will free any mappings
349 * associated with the buffer.
350 */
351
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700352 idx = 0;
Gurchetan Singhde263f12017-01-24 13:30:06 -0800353 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700354 while (idx < drv_array_size(bo->drv->mappings)) {
355 mapping = (struct mapping *)drv_array_at_idx(bo->drv->mappings, idx);
356 if (mapping->vma->handle != bo->handles[plane].u32) {
357 idx++;
358 continue;
Gurchetan Singhde263f12017-01-24 13:30:06 -0800359 }
360
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700361 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800362 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700363 if (ret) {
364 fprintf(stderr, "drv: munmap failed");
365 return ret;
366 }
367
368 free(mapping->vma);
369 }
370
371 /* This shrinks and shifts the array, so don't increment idx. */
372 drv_array_remove(bo->drv->mappings, idx);
Gurchetan Singhde263f12017-01-24 13:30:06 -0800373 }
374 }
375
376 return 0;
377}
378
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700379int drv_get_prot(uint32_t map_flags)
380{
381 return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
382}
383
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800384uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700385{
386 void *count;
387 uintptr_t num = 0;
388
389 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800390 num = (uintptr_t)(count);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700391
392 return num;
393}
394
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800395void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700396{
397 uintptr_t num = drv_get_reference_count(drv, bo, plane);
398
399 /* If a value isn't in the table, drmHashDelete is a no-op */
400 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800401 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700402}
403
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800404void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700405{
406 uintptr_t num = drv_get_reference_count(drv, bo, plane);
407
408 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
409
410 if (num > 0)
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800411 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700412}
Gurchetan Singhef920532016-08-12 16:38:25 -0700413
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530414uint32_t drv_log_base2(uint32_t value)
415{
416 int ret = 0;
417
418 while (value >>= 1)
419 ++ret;
420
421 return ret;
422}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700423
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800424int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700425 uint64_t use_flags)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700426{
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700427 struct combinations *combos = &drv->combos;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800428 if (combos->size >= combos->allocations) {
429 struct combination *new_data;
430 combos->allocations *= 2;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800431 new_data = realloc(combos->data, combos->allocations * sizeof(*combos->data));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800432 if (!new_data)
433 return -ENOMEM;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700434
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800435 combos->data = new_data;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700436 }
437
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800438 combos->data[combos->size].format = format;
439 combos->data[combos->size].metadata.priority = metadata->priority;
440 combos->data[combos->size].metadata.tiling = metadata->tiling;
441 combos->data[combos->size].metadata.modifier = metadata->modifier;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700442 combos->data[combos->size].use_flags = use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800443 combos->size++;
444 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700445}
446
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800447int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700448 struct format_metadata *metadata, uint64_t use_flags)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700449{
450 int ret;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800451 uint32_t i;
452 for (i = 0; i < num_formats; i++) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700453 ret = drv_add_combination(drv, formats[i], metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800454 if (ret)
455 return ret;
456 }
457
458 return 0;
459}
460
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800461void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700462 uint64_t use_flags)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800463{
464 uint32_t i;
465 struct combination *combo;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700466 /* Attempts to add the specified flags to an existing combination. */
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700467 for (i = 0; i < drv->combos.size; i++) {
468 combo = &drv->combos.data[i];
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800469 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800470 combo->metadata.modifier == metadata->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700471 combo->use_flags |= use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800472 }
473}
474
475struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
476{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800477 struct kms_item *items;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700478 uint64_t plane_type, use_flag;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800479 uint32_t i, j, k, allocations, item_size;
480
Gurchetan Singh179687e2016-10-28 10:07:35 -0700481 drmModePlanePtr plane;
482 drmModePropertyPtr prop;
483 drmModePlaneResPtr resources;
484 drmModeObjectPropertiesPtr props;
485
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800486 /* Start with a power of 2 number of allocations. */
487 allocations = 2;
488 item_size = 0;
489 items = calloc(allocations, sizeof(*items));
490 if (!items)
491 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700492
493 /*
494 * The ability to return universal planes is only complete on
495 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
496 * therefore might return an error code, so don't check it. If it
497 * fails, it'll just return the plane list as overlay planes, which is
498 * fine in our case (our drivers already have cursor bits set).
499 * modetest in libdrm does the same thing.
500 */
501 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
502
503 resources = drmModeGetPlaneResources(drv->fd);
504 if (!resources)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800505 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700506
507 for (i = 0; i < resources->count_planes; i++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700508 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700509 if (!plane)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800510 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700511
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800512 props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700513 if (!props)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800514 goto out;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700515
516 for (j = 0; j < props->count_props; j++) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700517 prop = drmModeGetProperty(drv->fd, props->props[j]);
518 if (prop) {
519 if (strcmp(prop->name, "type") == 0) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700520 plane_type = props->prop_values[j];
Gurchetan Singh179687e2016-10-28 10:07:35 -0700521 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800522
Gurchetan Singh179687e2016-10-28 10:07:35 -0700523 drmModeFreeProperty(prop);
524 }
525 }
526
Gurchetan Singha1892b22017-09-28 16:40:52 -0700527 switch (plane_type) {
Gurchetan Singh179687e2016-10-28 10:07:35 -0700528 case DRM_PLANE_TYPE_OVERLAY:
529 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singha1892b22017-09-28 16:40:52 -0700530 use_flag = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700531 break;
532 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singha1892b22017-09-28 16:40:52 -0700533 use_flag = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700534 break;
535 default:
536 assert(0);
537 }
538
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800539 for (j = 0; j < plane->count_formats; j++) {
540 bool found = false;
541 for (k = 0; k < item_size; k++) {
542 if (items[k].format == plane->formats[j] &&
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700543 items[k].modifier == DRM_FORMAT_MOD_INVALID) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700544 items[k].use_flags |= use_flag;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800545 found = true;
546 break;
547 }
548 }
549
550 if (!found && item_size >= allocations) {
551 struct kms_item *new_data = NULL;
552 allocations *= 2;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800553 new_data = realloc(items, allocations * sizeof(*items));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800554 if (!new_data) {
555 item_size = 0;
556 goto out;
557 }
558
559 items = new_data;
560 }
561
562 if (!found) {
563 items[item_size].format = plane->formats[j];
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700564 items[item_size].modifier = DRM_FORMAT_MOD_INVALID;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700565 items[item_size].use_flags = use_flag;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800566 item_size++;
567 }
568 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700569
570 drmModeFreeObjectProperties(props);
571 drmModeFreePlane(plane);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700572 }
573
574 drmModeFreePlaneResources(resources);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800575out:
576 if (items && item_size == 0) {
577 free(items);
578 items = NULL;
579 }
Gurchetan Singh179687e2016-10-28 10:07:35 -0700580
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800581 *num_items = item_size;
582 return items;
583}
584
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700585int drv_modify_linear_combinations(struct driver *drv)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800586{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800587 uint32_t i, j, num_items;
588 struct kms_item *items;
589 struct combination *combo;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800590
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800591 /*
592 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
593 * plane and as a cursor. Some drivers don't support
594 * drmModeGetPlaneResources, so add the combination here. Note that the
595 * kernel disregards the alpha component of ARGB unless it's an overlay
596 * plane.
597 */
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700598 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
599 BO_USE_CURSOR | BO_USE_SCANOUT);
600 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
601 BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800602
603 items = drv_query_kms(drv, &num_items);
604 if (!items || !num_items)
605 return 0;
606
607 for (i = 0; i < num_items; i++) {
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700608 for (j = 0; j < drv->combos.size; j++) {
609 combo = &drv->combos.data[j];
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800610 if (items[i].format == combo->format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700611 combo->use_flags |= BO_USE_SCANOUT;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800612 }
613 }
614
615 free(items);
616 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700617}
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700618
619/*
620 * Pick the best modifier from modifiers, according to the ordering
621 * given by modifier_order.
622 */
623uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
624 const uint64_t *modifier_order, uint32_t order_count)
625{
626 uint32_t i, j;
627
628 for (i = 0; i < order_count; i++) {
629 for (j = 0; j < count; j++) {
630 if (modifiers[j] == modifier_order[i]) {
631 return modifiers[j];
632 }
633 }
634 }
635
636 return DRM_FORMAT_MOD_LINEAR;
637}