Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 1 | /* |
Daniele Castagna | 7a755de | 2016-12-16 17:32:30 -0500 | [diff] [blame] | 2 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 7 | #include <assert.h> |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 8 | #include <errno.h> |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 9 | #include <stdbool.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 11 | #include <stdlib.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 12 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 13 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 14 | #include <xf86drm.h> |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 15 | #include <xf86drmMode.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 16 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 17 | #include "drv_priv.h" |
Lauri Peltonen | 904a879 | 2015-01-17 13:57:51 +0200 | [diff] [blame] | 18 | #include "helpers.h" |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 19 | #include "util.h" |
| 20 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 21 | int drv_bpp_from_format(uint32_t format, size_t plane) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 22 | { |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 23 | assert(plane < drv_num_planes_from_format(format)); |
| 24 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 25 | switch (format) { |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 26 | case DRM_FORMAT_BGR233: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 27 | case DRM_FORMAT_C8: |
| 28 | case DRM_FORMAT_R8: |
| 29 | case DRM_FORMAT_RGB332: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 30 | case DRM_FORMAT_YVU420: |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 31 | case DRM_FORMAT_YVU420_ANDROID: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 32 | return 8; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 33 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 34 | /* |
| 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 Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 51 | case DRM_FORMAT_NV12: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 52 | return (plane == 0) ? 8 : 16; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 53 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 54 | case DRM_FORMAT_ABGR1555: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 55 | case DRM_FORMAT_ABGR4444: |
| 56 | case DRM_FORMAT_ARGB1555: |
| 57 | case DRM_FORMAT_ARGB4444: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 58 | case DRM_FORMAT_BGR565: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 59 | 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 Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 70 | case DRM_FORMAT_UYVY: |
| 71 | case DRM_FORMAT_VYUY: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 72 | 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 Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 78 | return 16; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 79 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 80 | case DRM_FORMAT_BGR888: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 81 | case DRM_FORMAT_RGB888: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 82 | return 24; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 83 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 84 | case DRM_FORMAT_ABGR2101010: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 85 | case DRM_FORMAT_ABGR8888: |
| 86 | case DRM_FORMAT_ARGB2101010: |
| 87 | case DRM_FORMAT_ARGB8888: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 88 | case DRM_FORMAT_AYUV: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 89 | 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 Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 101 | return 32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 104 | fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 108 | /* |
Gurchetan Singh | 4f298f9 | 2017-03-23 11:29:26 -0700 | [diff] [blame^] | 109 | * This function returns the stride for a given format, width and plane. |
| 110 | */ |
| 111 | uint32_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 Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 142 | * This function fills in the buffer object given driver aligned dimensions |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 143 | * (in pixels) and a format. This function assumes there is just one kernel |
| 144 | * buffer per buffer object. |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 145 | */ |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 146 | int drv_bo_from_format(struct bo *bo, uint32_t aligned_width, |
| 147 | uint32_t aligned_height, uint32_t format) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 148 | { |
| 149 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 150 | 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 Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 155 | bo->total_size = 0; |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 156 | |
| 157 | for (p = 0; p < num_planes; p++) { |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 158 | bo->strides[p] = drv_stride_from_format(format, aligned_width, |
| 159 | p); |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 160 | bo->sizes[p] = drv_size_from_format(format, bo->strides[p], |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 161 | bo->height, p); |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 162 | bo->offsets[p] = offset; |
| 163 | offset += bo->sizes[p]; |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 164 | bo->total_size += drv_size_from_format(format, bo->strides[p], |
| 165 | aligned_height, p); |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 171 | int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, |
Stéphane Marchesin | ec88e89 | 2015-11-03 16:14:59 -0800 | [diff] [blame] | 172 | uint32_t format, uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 173 | { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 174 | int ret; |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 175 | size_t plane; |
| 176 | uint32_t aligned_width, aligned_height, bytes_per_pixel; |
| 177 | struct drm_mode_create_dumb create_dumb; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 178 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 179 | aligned_width = width; |
| 180 | aligned_height = height; |
Gurchetan Singh | 4f298f9 | 2017-03-23 11:29:26 -0700 | [diff] [blame^] | 181 | bytes_per_pixel = drv_bytes_per_pixel(format, 0); |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 182 | 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 Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 190 | |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 191 | memset(&create_dumb, 0, sizeof(create_dumb)); |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 192 | create_dumb.height = aligned_height; |
| 193 | create_dumb.width = aligned_width; |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 194 | create_dumb.bpp = drv_bpp_from_format(format, 0); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 195 | create_dumb.flags = 0; |
| 196 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 197 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 198 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 199 | fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n"); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 200 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 201 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 202 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 203 | drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel), |
| 204 | height, format); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 205 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame] | 206 | 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 Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 213 | int drv_dumb_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 214 | { |
| 215 | struct drm_mode_destroy_dumb destroy_dumb; |
| 216 | int ret; |
| 217 | |
| 218 | memset(&destroy_dumb, 0, sizeof(destroy_dumb)); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 219 | destroy_dumb.handle = bo->handles[0].u32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 220 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 221 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb); |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 222 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 223 | fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 224 | "(handle=%x)\n", bo->handles[0].u32); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 225 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 226 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 231 | int drv_gem_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 232 | { |
| 233 | struct drm_gem_close gem_close; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 234 | int ret, error = 0; |
| 235 | size_t plane, i; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 236 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 237 | for (plane = 0; plane < bo->num_planes; plane++) { |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 238 | 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 Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 243 | continue; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 244 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 245 | memset(&gem_close, 0, sizeof(gem_close)); |
| 246 | gem_close.handle = bo->handles[plane].u32; |
| 247 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 248 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 249 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 250 | fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 251 | "(handle=%x) error %d\n", |
| 252 | bo->handles[plane].u32, ret); |
| 253 | error = ret; |
| 254 | } |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 255 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 256 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 257 | return error; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 258 | } |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 259 | |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 260 | int 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 Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 300 | void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 301 | { |
| 302 | int ret; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 303 | size_t i; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 304 | struct drm_mode_map_dumb map_dumb; |
| 305 | |
| 306 | memset(&map_dumb, 0, sizeof(map_dumb)); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 307 | map_dumb.handle = bo->handles[plane].u32; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 308 | |
| 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 Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 315 | 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 Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 320 | bo->drv->fd, map_dumb.offset); |
| 321 | } |
| 322 | |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 323 | uintptr_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 | |
| 335 | void 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 | |
| 346 | void 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 Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 357 | |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 358 | uint32_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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 367 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 368 | int drv_add_combination(struct driver *drv, uint32_t format, |
| 369 | struct format_metadata *metadata, uint64_t usage) |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 370 | { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 371 | 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 379 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 380 | combos->data = new_data; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 383 | 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 392 | int drv_add_combinations(struct driver *drv, const uint32_t *formats, |
| 393 | uint32_t num_formats, struct format_metadata *metadata, |
| 394 | uint64_t usage) |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 395 | { |
| 396 | int ret; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 397 | 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 | |
| 407 | void 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 | |
| 422 | struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items) |
| 423 | { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 424 | uint64_t flag, usage; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 425 | struct kms_item *items; |
| 426 | uint32_t i, j, k, allocations, item_size; |
| 427 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 428 | drmModePlanePtr plane; |
| 429 | drmModePropertyPtr prop; |
| 430 | drmModePlaneResPtr resources; |
| 431 | drmModeObjectPropertiesPtr props; |
| 432 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 433 | /* 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 439 | |
| 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 Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 452 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 453 | |
| 454 | for (i = 0; i < resources->count_planes; i++) { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 455 | plane = drmModeGetPlane(drv->fd, resources->planes[i]); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 456 | if (!plane) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 457 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 458 | |
| 459 | props = drmModeObjectGetProperties(drv->fd, plane->plane_id, |
| 460 | DRM_MODE_OBJECT_PLANE); |
| 461 | if (!props) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 462 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 463 | |
| 464 | for (j = 0; j < props->count_props; j++) { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 465 | 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 Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 470 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 471 | drmModeFreeProperty(prop); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | switch (flag) { |
| 476 | case DRM_PLANE_TYPE_OVERLAY: |
| 477 | case DRM_PLANE_TYPE_PRIMARY: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 478 | usage = BO_USE_SCANOUT; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 479 | break; |
| 480 | case DRM_PLANE_TYPE_CURSOR: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 481 | usage = BO_USE_CURSOR; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 482 | break; |
| 483 | default: |
| 484 | assert(0); |
| 485 | } |
| 486 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 487 | 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 518 | |
| 519 | drmModeFreeObjectProperties(props); |
| 520 | drmModeFreePlane(plane); |
| 521 | |
| 522 | } |
| 523 | |
| 524 | drmModeFreePlaneResources(resources); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 525 | out: |
| 526 | if (items && item_size == 0) { |
| 527 | free(items); |
| 528 | items = NULL; |
| 529 | } |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 530 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 531 | *num_items = item_size; |
| 532 | return items; |
| 533 | } |
| 534 | |
| 535 | int 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 580 | } |