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> |
| 8 | #include <stdbool.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 10 | #include <stdlib.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 11 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 12 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 13 | #include <xf86drm.h> |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 14 | #include <xf86drmMode.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 15 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 16 | #include "drv_priv.h" |
Lauri Peltonen | 904a879 | 2015-01-17 13:57:51 +0200 | [diff] [blame] | 17 | #include "helpers.h" |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 18 | #include "util.h" |
| 19 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 20 | int drv_bpp_from_format(uint32_t format, size_t plane) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 21 | { |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 22 | assert(plane < drv_num_planes_from_format(format)); |
| 23 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 24 | switch (format) { |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 25 | case DRM_FORMAT_BGR233: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 26 | case DRM_FORMAT_C8: |
| 27 | case DRM_FORMAT_R8: |
| 28 | case DRM_FORMAT_RGB332: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 29 | case DRM_FORMAT_YVU420: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 30 | return 8; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 31 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 32 | /* |
| 33 | * NV12 is laid out as follows. Each letter represents a byte. |
| 34 | * Y plane: |
| 35 | * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N |
| 36 | * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N |
| 37 | * ... |
| 38 | * YM_0, YM_1, YM_2, YM_3, ..., YM_N |
| 39 | * CbCr plane: |
| 40 | * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N |
| 41 | * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N |
| 42 | * ... |
| 43 | * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N |
| 44 | * |
| 45 | * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires |
| 46 | * Y0_1, Cb01_01 and Cr01_01. So for a single pixel, 2 bytes of luma data |
| 47 | * are required. |
| 48 | */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 49 | case DRM_FORMAT_NV12: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 50 | return (plane == 0) ? 8 : 16; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 51 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 52 | case DRM_FORMAT_ABGR1555: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 53 | case DRM_FORMAT_ABGR4444: |
| 54 | case DRM_FORMAT_ARGB1555: |
| 55 | case DRM_FORMAT_ARGB4444: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 56 | case DRM_FORMAT_BGR565: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 57 | case DRM_FORMAT_BGRA4444: |
| 58 | case DRM_FORMAT_BGRA5551: |
| 59 | case DRM_FORMAT_BGRX4444: |
| 60 | case DRM_FORMAT_BGRX5551: |
| 61 | case DRM_FORMAT_GR88: |
| 62 | case DRM_FORMAT_RG88: |
| 63 | case DRM_FORMAT_RGB565: |
| 64 | case DRM_FORMAT_RGBA4444: |
| 65 | case DRM_FORMAT_RGBA5551: |
| 66 | case DRM_FORMAT_RGBX4444: |
| 67 | case DRM_FORMAT_RGBX5551: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 68 | case DRM_FORMAT_UYVY: |
| 69 | case DRM_FORMAT_VYUY: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 70 | case DRM_FORMAT_XBGR1555: |
| 71 | case DRM_FORMAT_XBGR4444: |
| 72 | case DRM_FORMAT_XRGB1555: |
| 73 | case DRM_FORMAT_XRGB4444: |
| 74 | case DRM_FORMAT_YUYV: |
| 75 | case DRM_FORMAT_YVYU: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 76 | return 16; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 77 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 78 | case DRM_FORMAT_BGR888: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 79 | case DRM_FORMAT_RGB888: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 80 | return 24; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 81 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 82 | case DRM_FORMAT_ABGR2101010: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 83 | case DRM_FORMAT_ABGR8888: |
| 84 | case DRM_FORMAT_ARGB2101010: |
| 85 | case DRM_FORMAT_ARGB8888: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 86 | case DRM_FORMAT_AYUV: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 87 | case DRM_FORMAT_BGRA1010102: |
| 88 | case DRM_FORMAT_BGRA8888: |
| 89 | case DRM_FORMAT_BGRX1010102: |
| 90 | case DRM_FORMAT_BGRX8888: |
| 91 | case DRM_FORMAT_RGBA1010102: |
| 92 | case DRM_FORMAT_RGBA8888: |
| 93 | case DRM_FORMAT_RGBX1010102: |
| 94 | case DRM_FORMAT_RGBX8888: |
| 95 | case DRM_FORMAT_XBGR2101010: |
| 96 | case DRM_FORMAT_XBGR8888: |
| 97 | case DRM_FORMAT_XRGB2101010: |
| 98 | case DRM_FORMAT_XRGB8888: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 99 | return 32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 102 | fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 106 | /* |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 107 | * This function fills in the buffer object given driver aligned dimensions |
| 108 | * and a format. This function assumes there is just one kernel buffer per |
| 109 | * buffer object. |
| 110 | */ |
| 111 | int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 112 | uint32_t format) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 113 | { |
| 114 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 115 | size_t p, num_planes; |
| 116 | uint32_t offset = 0; |
| 117 | |
| 118 | num_planes = drv_num_planes_from_format(format); |
| 119 | assert(num_planes); |
| 120 | |
| 121 | for (p = 0; p < num_planes; p++) { |
| 122 | bo->strides[p] = drv_stride_from_format(format, width, p); |
| 123 | bo->sizes[p] = drv_size_from_format(format, bo->strides[p], |
| 124 | height, p); |
| 125 | bo->offsets[p] = offset; |
| 126 | offset += bo->sizes[p]; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 129 | bo->total_size = bo->offsets[bo->num_planes - 1] + |
| 130 | bo->sizes[bo->num_planes - 1]; |
| 131 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 135 | 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] | 136 | uint32_t format, uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 137 | { |
| 138 | struct drm_mode_create_dumb create_dumb; |
| 139 | int ret; |
| 140 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 141 | /* Only single-plane formats are supported */ |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 142 | assert(drv_num_planes_from_format(format) == 1); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 143 | |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 144 | memset(&create_dumb, 0, sizeof(create_dumb)); |
| 145 | create_dumb.height = height; |
| 146 | create_dumb.width = width; |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 147 | create_dumb.bpp = drv_bpp_from_format(format, 0); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 148 | create_dumb.flags = 0; |
| 149 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 150 | 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] | 151 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 152 | fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n"); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 153 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 154 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 155 | |
Gurchetan Singh | bc17b1d | 2016-12-06 15:43:17 -0800 | [diff] [blame] | 156 | bo->width = width; |
| 157 | bo->height = height; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 158 | bo->handles[0].u32 = create_dumb.handle; |
| 159 | bo->offsets[0] = 0; |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 160 | bo->total_size = bo->sizes[0] = create_dumb.size; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 161 | bo->strides[0] = create_dumb.pitch; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 166 | int drv_dumb_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 167 | { |
| 168 | struct drm_mode_destroy_dumb destroy_dumb; |
| 169 | int ret; |
| 170 | |
| 171 | memset(&destroy_dumb, 0, sizeof(destroy_dumb)); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 172 | destroy_dumb.handle = bo->handles[0].u32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 173 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 174 | 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] | 175 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 176 | fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 177 | "(handle=%x)\n", bo->handles[0].u32); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 178 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 179 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 184 | int drv_gem_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 185 | { |
| 186 | struct drm_gem_close gem_close; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 187 | int ret, error = 0; |
| 188 | size_t plane, i; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 189 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 190 | for (plane = 0; plane < bo->num_planes; plane++) { |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 191 | for (i = 0; i < plane; i++) |
| 192 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 193 | break; |
| 194 | /* Make sure close hasn't already been called on this handle */ |
| 195 | if (i != plane) |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 196 | continue; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 197 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 198 | memset(&gem_close, 0, sizeof(gem_close)); |
| 199 | gem_close.handle = bo->handles[plane].u32; |
| 200 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 201 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 202 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 203 | fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 204 | "(handle=%x) error %d\n", |
| 205 | bo->handles[plane].u32, ret); |
| 206 | error = ret; |
| 207 | } |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 208 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 209 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 210 | return error; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 211 | } |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 212 | |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 213 | int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 214 | { |
| 215 | int ret; |
| 216 | size_t plane; |
| 217 | struct drm_prime_handle prime_handle; |
| 218 | |
| 219 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 220 | memset(&prime_handle, 0, sizeof(prime_handle)); |
| 221 | prime_handle.fd = data->fds[plane]; |
| 222 | |
| 223 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, |
| 224 | &prime_handle); |
| 225 | |
| 226 | if (ret) { |
| 227 | fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE " |
| 228 | "failed (fd=%u)\n", prime_handle.fd); |
| 229 | |
| 230 | /* |
| 231 | * Need to call GEM close on planes that were opened, |
| 232 | * if any. Adjust the num_planes variable to be the |
| 233 | * plane that failed, so GEM close will be called on |
| 234 | * planes before that plane. |
| 235 | */ |
| 236 | bo->num_planes = plane; |
| 237 | drv_gem_bo_destroy(bo); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | bo->handles[plane].u32 = prime_handle.handle; |
| 242 | } |
| 243 | |
| 244 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 245 | pthread_mutex_lock(&bo->drv->driver_lock); |
| 246 | drv_increment_reference_count(bo->drv, bo, plane); |
| 247 | pthread_mutex_unlock(&bo->drv->driver_lock); |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 253 | 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] | 254 | { |
| 255 | int ret; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 256 | size_t i; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 257 | struct drm_mode_map_dumb map_dumb; |
| 258 | |
| 259 | memset(&map_dumb, 0, sizeof(map_dumb)); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 260 | map_dumb.handle = bo->handles[plane].u32; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 261 | |
| 262 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); |
| 263 | if (ret) { |
| 264 | fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n"); |
| 265 | return MAP_FAILED; |
| 266 | } |
| 267 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 268 | for (i = 0; i < bo->num_planes; i++) |
| 269 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 270 | data->length += bo->sizes[i]; |
| 271 | |
| 272 | return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 273 | bo->drv->fd, map_dumb.offset); |
| 274 | } |
| 275 | |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 276 | uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, |
| 277 | size_t plane) |
| 278 | { |
| 279 | void *count; |
| 280 | uintptr_t num = 0; |
| 281 | |
| 282 | if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count)) |
| 283 | num = (uintptr_t) (count); |
| 284 | |
| 285 | return num; |
| 286 | } |
| 287 | |
| 288 | void drv_increment_reference_count(struct driver *drv, struct bo *bo, |
| 289 | size_t plane) |
| 290 | { |
| 291 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 292 | |
| 293 | /* If a value isn't in the table, drmHashDelete is a no-op */ |
| 294 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 295 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 296 | (void *) (num + 1)); |
| 297 | } |
| 298 | |
| 299 | void drv_decrement_reference_count(struct driver *drv, struct bo *bo, |
| 300 | size_t plane) |
| 301 | { |
| 302 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 303 | |
| 304 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 305 | |
| 306 | if (num > 0) |
| 307 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 308 | (void *) (num - 1)); |
| 309 | } |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 310 | |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 311 | uint32_t drv_log_base2(uint32_t value) |
| 312 | { |
| 313 | int ret = 0; |
| 314 | |
| 315 | while (value >>= 1) |
| 316 | ++ret; |
| 317 | |
| 318 | return ret; |
| 319 | } |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 320 | |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 321 | /* Inserts a combination into list -- caller should have lock on driver. */ |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 322 | void drv_insert_supported_combination(struct driver *drv, uint32_t format, |
| 323 | uint64_t usage, uint64_t modifier) |
| 324 | { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 325 | struct combination_list_element *elem; |
| 326 | |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 327 | elem = calloc(1, sizeof(*elem)); |
| 328 | elem->combination.format = format; |
| 329 | elem->combination.modifier = modifier; |
| 330 | elem->combination.usage = usage; |
| 331 | LIST_ADD(&elem->link, &drv->backend->combinations); |
| 332 | } |
| 333 | |
| 334 | void drv_insert_combinations(struct driver *drv, struct supported_combination *combos, |
| 335 | uint32_t size) |
| 336 | { |
| 337 | unsigned int i; |
| 338 | |
| 339 | pthread_mutex_lock(&drv->driver_lock); |
| 340 | |
| 341 | for (i = 0; i < size; i++) |
| 342 | drv_insert_supported_combination(drv, combos[i].format, |
| 343 | combos[i].usage, |
| 344 | combos[i].modifier); |
| 345 | |
| 346 | pthread_mutex_unlock(&drv->driver_lock); |
| 347 | } |
| 348 | |
| 349 | void drv_modify_supported_combination(struct driver *drv, uint32_t format, |
| 350 | uint64_t usage, uint64_t modifier) |
| 351 | { |
| 352 | /* |
| 353 | * Attempts to add the specified usage to an existing {format, modifier} |
| 354 | * pair. If the pair is not present, a new combination is created. |
| 355 | */ |
| 356 | int found = 0; |
| 357 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 358 | pthread_mutex_lock(&drv->driver_lock); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 359 | |
| 360 | list_for_each_entry(struct combination_list_element, |
| 361 | elem, &drv->backend->combinations, link) { |
| 362 | if (elem->combination.format == format && |
| 363 | elem->combination.modifier == modifier) { |
| 364 | elem->combination.usage |= usage; |
| 365 | found = 1; |
| 366 | } |
| 367 | } |
| 368 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 369 | |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 370 | if (!found) |
| 371 | drv_insert_supported_combination(drv, format, usage, modifier); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 372 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 373 | pthread_mutex_unlock(&drv->driver_lock); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 376 | int drv_add_kms_flags(struct driver *drv) |
| 377 | { |
| 378 | int ret; |
| 379 | uint32_t i, j; |
| 380 | uint64_t flag, usage; |
| 381 | drmModePlanePtr plane; |
| 382 | drmModePropertyPtr prop; |
| 383 | drmModePlaneResPtr resources; |
| 384 | drmModeObjectPropertiesPtr props; |
| 385 | |
| 386 | /* |
| 387 | * All current drivers can scanout XRGB8888/ARGB8888 as a primary plane. |
| 388 | * Some older kernel versions can only return overlay planes, so add the |
| 389 | * combination here. Note that the kernel disregards the alpha component |
| 390 | * of ARGB unless it's an overlay plane. |
| 391 | */ |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 392 | drv_modify_supported_combination(drv, DRM_FORMAT_XRGB8888, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 393 | BO_USE_SCANOUT, 0); |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 394 | drv_modify_supported_combination(drv, DRM_FORMAT_ARGB8888, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 395 | BO_USE_SCANOUT, 0); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * The ability to return universal planes is only complete on |
| 399 | * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl |
| 400 | * therefore might return an error code, so don't check it. If it |
| 401 | * fails, it'll just return the plane list as overlay planes, which is |
| 402 | * fine in our case (our drivers already have cursor bits set). |
| 403 | * modetest in libdrm does the same thing. |
| 404 | */ |
| 405 | drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 406 | |
| 407 | resources = drmModeGetPlaneResources(drv->fd); |
| 408 | if (!resources) |
| 409 | goto err; |
| 410 | |
| 411 | for (i = 0; i < resources->count_planes; i++) { |
| 412 | |
| 413 | plane = drmModeGetPlane(drv->fd, resources->planes[i]); |
| 414 | |
| 415 | if (!plane) |
| 416 | goto err; |
| 417 | |
| 418 | props = drmModeObjectGetProperties(drv->fd, plane->plane_id, |
| 419 | DRM_MODE_OBJECT_PLANE); |
| 420 | if (!props) |
| 421 | goto err; |
| 422 | |
| 423 | for (j = 0; j < props->count_props; j++) { |
| 424 | |
| 425 | prop = drmModeGetProperty(drv->fd, props->props[j]); |
| 426 | if (prop) { |
| 427 | if (strcmp(prop->name, "type") == 0) { |
| 428 | flag = props->prop_values[j]; |
| 429 | } |
| 430 | drmModeFreeProperty(prop); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | switch (flag) { |
| 435 | case DRM_PLANE_TYPE_OVERLAY: |
| 436 | case DRM_PLANE_TYPE_PRIMARY: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 437 | usage = BO_USE_SCANOUT; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 438 | break; |
| 439 | case DRM_PLANE_TYPE_CURSOR: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 440 | usage = BO_USE_CURSOR; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 441 | break; |
| 442 | default: |
| 443 | assert(0); |
| 444 | } |
| 445 | |
| 446 | for (j = 0; j < plane->count_formats; j++) |
Gurchetan Singh | 0cefbe9 | 2016-12-01 13:36:04 -0800 | [diff] [blame] | 447 | drv_modify_supported_combination(drv, plane->formats[j], |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 448 | usage, 0); |
| 449 | |
| 450 | drmModeFreeObjectProperties(props); |
| 451 | drmModeFreePlane(plane); |
| 452 | |
| 453 | } |
| 454 | |
| 455 | drmModeFreePlaneResources(resources); |
| 456 | return 0; |
| 457 | |
| 458 | err: |
| 459 | ret = -1; |
| 460 | return ret; |
| 461 | } |