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 | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 109 | * This function fills in the buffer object given driver aligned dimensions |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 110 | * (in pixels) and a format. This function assumes there is just one kernel |
| 111 | * buffer per buffer object. |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 112 | */ |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 113 | int drv_bo_from_format(struct bo *bo, uint32_t aligned_width, |
| 114 | uint32_t aligned_height, uint32_t format) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 115 | { |
| 116 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 117 | size_t p, num_planes; |
| 118 | uint32_t offset = 0; |
| 119 | |
| 120 | num_planes = drv_num_planes_from_format(format); |
| 121 | assert(num_planes); |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 122 | bo->total_size = 0; |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 123 | |
| 124 | for (p = 0; p < num_planes; p++) { |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 125 | bo->strides[p] = drv_stride_from_format(format, aligned_width, |
| 126 | p); |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 127 | bo->sizes[p] = drv_size_from_format(format, bo->strides[p], |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 128 | bo->height, p); |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 129 | bo->offsets[p] = offset; |
| 130 | offset += bo->sizes[p]; |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 131 | bo->total_size += drv_size_from_format(format, bo->strides[p], |
| 132 | aligned_height, p); |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 138 | 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] | 139 | uint32_t format, uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 140 | { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 141 | int ret; |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame^] | 142 | size_t plane; |
| 143 | uint32_t aligned_width, aligned_height, bytes_per_pixel; |
| 144 | struct drm_mode_create_dumb create_dumb; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 145 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame^] | 146 | aligned_width = width; |
| 147 | aligned_height = height; |
| 148 | bytes_per_pixel = DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8); |
| 149 | if (format == DRM_FORMAT_YVU420_ANDROID) { |
| 150 | /* |
| 151 | * Align width to 16 pixels, so chroma strides are 16 bytes as |
| 152 | * Android requires. |
| 153 | */ |
| 154 | aligned_width = ALIGN(width, 32); |
| 155 | aligned_height = 3 * DIV_ROUND_UP(height, 2); |
| 156 | } |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 157 | |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 158 | memset(&create_dumb, 0, sizeof(create_dumb)); |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame^] | 159 | create_dumb.height = aligned_height; |
| 160 | create_dumb.width = aligned_width; |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 161 | create_dumb.bpp = drv_bpp_from_format(format, 0); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 162 | create_dumb.flags = 0; |
| 163 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 164 | 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] | 165 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 166 | fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n"); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 167 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 168 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 169 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame^] | 170 | drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel), |
| 171 | height, format); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 172 | |
Gurchetan Singh | d5f8e44 | 2017-03-16 13:05:45 -0700 | [diff] [blame^] | 173 | for (plane = 0; plane < bo->num_planes; plane++) |
| 174 | bo->handles[plane].u32 = create_dumb.handle; |
| 175 | |
| 176 | bo->total_size = create_dumb.size; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 180 | int drv_dumb_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 181 | { |
| 182 | struct drm_mode_destroy_dumb destroy_dumb; |
| 183 | int ret; |
| 184 | |
| 185 | memset(&destroy_dumb, 0, sizeof(destroy_dumb)); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 186 | destroy_dumb.handle = bo->handles[0].u32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 187 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 188 | 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] | 189 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 190 | fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 191 | "(handle=%x)\n", bo->handles[0].u32); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 192 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 193 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 198 | int drv_gem_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 199 | { |
| 200 | struct drm_gem_close gem_close; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 201 | int ret, error = 0; |
| 202 | size_t plane, i; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 203 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 204 | for (plane = 0; plane < bo->num_planes; plane++) { |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 205 | for (i = 0; i < plane; i++) |
| 206 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 207 | break; |
| 208 | /* Make sure close hasn't already been called on this handle */ |
| 209 | if (i != plane) |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 210 | continue; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 211 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 212 | memset(&gem_close, 0, sizeof(gem_close)); |
| 213 | gem_close.handle = bo->handles[plane].u32; |
| 214 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 215 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 216 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 217 | fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 218 | "(handle=%x) error %d\n", |
| 219 | bo->handles[plane].u32, ret); |
| 220 | error = ret; |
| 221 | } |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 222 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 223 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 224 | return error; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 225 | } |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 226 | |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 227 | int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 228 | { |
| 229 | int ret; |
| 230 | size_t plane; |
| 231 | struct drm_prime_handle prime_handle; |
| 232 | |
| 233 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 234 | memset(&prime_handle, 0, sizeof(prime_handle)); |
| 235 | prime_handle.fd = data->fds[plane]; |
| 236 | |
| 237 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, |
| 238 | &prime_handle); |
| 239 | |
| 240 | if (ret) { |
| 241 | fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE " |
| 242 | "failed (fd=%u)\n", prime_handle.fd); |
| 243 | |
| 244 | /* |
| 245 | * Need to call GEM close on planes that were opened, |
| 246 | * if any. Adjust the num_planes variable to be the |
| 247 | * plane that failed, so GEM close will be called on |
| 248 | * planes before that plane. |
| 249 | */ |
| 250 | bo->num_planes = plane; |
| 251 | drv_gem_bo_destroy(bo); |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | bo->handles[plane].u32 = prime_handle.handle; |
| 256 | } |
| 257 | |
| 258 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 259 | pthread_mutex_lock(&bo->drv->driver_lock); |
| 260 | drv_increment_reference_count(bo->drv, bo, plane); |
| 261 | pthread_mutex_unlock(&bo->drv->driver_lock); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 267 | 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] | 268 | { |
| 269 | int ret; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 270 | size_t i; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 271 | struct drm_mode_map_dumb map_dumb; |
| 272 | |
| 273 | memset(&map_dumb, 0, sizeof(map_dumb)); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 274 | map_dumb.handle = bo->handles[plane].u32; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 275 | |
| 276 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); |
| 277 | if (ret) { |
| 278 | fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n"); |
| 279 | return MAP_FAILED; |
| 280 | } |
| 281 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 282 | for (i = 0; i < bo->num_planes; i++) |
| 283 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 284 | data->length += bo->sizes[i]; |
| 285 | |
| 286 | return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 287 | bo->drv->fd, map_dumb.offset); |
| 288 | } |
| 289 | |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 290 | uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, |
| 291 | size_t plane) |
| 292 | { |
| 293 | void *count; |
| 294 | uintptr_t num = 0; |
| 295 | |
| 296 | if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count)) |
| 297 | num = (uintptr_t) (count); |
| 298 | |
| 299 | return num; |
| 300 | } |
| 301 | |
| 302 | void drv_increment_reference_count(struct driver *drv, struct bo *bo, |
| 303 | size_t plane) |
| 304 | { |
| 305 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 306 | |
| 307 | /* If a value isn't in the table, drmHashDelete is a no-op */ |
| 308 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 309 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 310 | (void *) (num + 1)); |
| 311 | } |
| 312 | |
| 313 | void drv_decrement_reference_count(struct driver *drv, struct bo *bo, |
| 314 | size_t plane) |
| 315 | { |
| 316 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 317 | |
| 318 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 319 | |
| 320 | if (num > 0) |
| 321 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 322 | (void *) (num - 1)); |
| 323 | } |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 324 | |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 325 | uint32_t drv_log_base2(uint32_t value) |
| 326 | { |
| 327 | int ret = 0; |
| 328 | |
| 329 | while (value >>= 1) |
| 330 | ++ret; |
| 331 | |
| 332 | return ret; |
| 333 | } |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 334 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 335 | int drv_add_combination(struct driver *drv, uint32_t format, |
| 336 | struct format_metadata *metadata, uint64_t usage) |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 337 | { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 338 | struct combinations *combos = &drv->backend->combos; |
| 339 | if (combos->size >= combos->allocations) { |
| 340 | struct combination *new_data; |
| 341 | combos->allocations *= 2; |
| 342 | new_data = realloc(combos->data, combos->allocations |
| 343 | * sizeof(*combos->data)); |
| 344 | if (!new_data) |
| 345 | return -ENOMEM; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 346 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 347 | combos->data = new_data; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 350 | combos->data[combos->size].format = format; |
| 351 | combos->data[combos->size].metadata.priority = metadata->priority; |
| 352 | combos->data[combos->size].metadata.tiling = metadata->tiling; |
| 353 | combos->data[combos->size].metadata.modifier = metadata->modifier; |
| 354 | combos->data[combos->size].usage = usage; |
| 355 | combos->size++; |
| 356 | return 0; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 359 | int drv_add_combinations(struct driver *drv, const uint32_t *formats, |
| 360 | uint32_t num_formats, struct format_metadata *metadata, |
| 361 | uint64_t usage) |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 362 | { |
| 363 | int ret; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 364 | uint32_t i; |
| 365 | for (i = 0; i < num_formats; i++) { |
| 366 | ret = drv_add_combination(drv, formats[i], metadata, usage); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | void drv_modify_combination(struct driver *drv, uint32_t format, |
| 375 | struct format_metadata *metadata, uint64_t usage) |
| 376 | { |
| 377 | uint32_t i; |
| 378 | struct combination *combo; |
| 379 | /* Attempts to add the specified usage to an existing combination. */ |
| 380 | for (i = 0; i < drv->backend->combos.size; i++) { |
| 381 | combo = &drv->backend->combos.data[i]; |
| 382 | if (combo->format == format && |
| 383 | combo->metadata.tiling == metadata->tiling && |
| 384 | combo->metadata.modifier == metadata->modifier) |
| 385 | combo->usage |= usage; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items) |
| 390 | { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 391 | uint64_t flag, usage; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 392 | struct kms_item *items; |
| 393 | uint32_t i, j, k, allocations, item_size; |
| 394 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 395 | drmModePlanePtr plane; |
| 396 | drmModePropertyPtr prop; |
| 397 | drmModePlaneResPtr resources; |
| 398 | drmModeObjectPropertiesPtr props; |
| 399 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 400 | /* Start with a power of 2 number of allocations. */ |
| 401 | allocations = 2; |
| 402 | item_size = 0; |
| 403 | items = calloc(allocations, sizeof(*items)); |
| 404 | if (!items) |
| 405 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * The ability to return universal planes is only complete on |
| 409 | * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl |
| 410 | * therefore might return an error code, so don't check it. If it |
| 411 | * fails, it'll just return the plane list as overlay planes, which is |
| 412 | * fine in our case (our drivers already have cursor bits set). |
| 413 | * modetest in libdrm does the same thing. |
| 414 | */ |
| 415 | drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 416 | |
| 417 | resources = drmModeGetPlaneResources(drv->fd); |
| 418 | if (!resources) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 419 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 420 | |
| 421 | for (i = 0; i < resources->count_planes; i++) { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 422 | plane = drmModeGetPlane(drv->fd, resources->planes[i]); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 423 | if (!plane) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 424 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 425 | |
| 426 | props = drmModeObjectGetProperties(drv->fd, plane->plane_id, |
| 427 | DRM_MODE_OBJECT_PLANE); |
| 428 | if (!props) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 429 | goto out; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 430 | |
| 431 | for (j = 0; j < props->count_props; j++) { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 432 | prop = drmModeGetProperty(drv->fd, props->props[j]); |
| 433 | if (prop) { |
| 434 | if (strcmp(prop->name, "type") == 0) { |
| 435 | flag = props->prop_values[j]; |
| 436 | } |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 437 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 438 | drmModeFreeProperty(prop); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | switch (flag) { |
| 443 | case DRM_PLANE_TYPE_OVERLAY: |
| 444 | case DRM_PLANE_TYPE_PRIMARY: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 445 | usage = BO_USE_SCANOUT; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 446 | break; |
| 447 | case DRM_PLANE_TYPE_CURSOR: |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 448 | usage = BO_USE_CURSOR; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 449 | break; |
| 450 | default: |
| 451 | assert(0); |
| 452 | } |
| 453 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 454 | for (j = 0; j < plane->count_formats; j++) { |
| 455 | bool found = false; |
| 456 | for (k = 0; k < item_size; k++) { |
| 457 | if (items[k].format == plane->formats[j] && |
| 458 | items[k].modifier == DRM_FORMAT_MOD_NONE) { |
| 459 | items[k].usage |= usage; |
| 460 | found = true; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (!found && item_size >= allocations) { |
| 466 | struct kms_item *new_data = NULL; |
| 467 | allocations *= 2; |
| 468 | new_data = realloc(items, allocations * |
| 469 | sizeof(*items)); |
| 470 | if (!new_data) { |
| 471 | item_size = 0; |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | items = new_data; |
| 476 | } |
| 477 | |
| 478 | if (!found) { |
| 479 | items[item_size].format = plane->formats[j]; |
| 480 | items[item_size].modifier = DRM_FORMAT_MOD_NONE; |
| 481 | items[item_size].usage = usage; |
| 482 | item_size++; |
| 483 | } |
| 484 | } |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 485 | |
| 486 | drmModeFreeObjectProperties(props); |
| 487 | drmModeFreePlane(plane); |
| 488 | |
| 489 | } |
| 490 | |
| 491 | drmModeFreePlaneResources(resources); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 492 | out: |
| 493 | if (items && item_size == 0) { |
| 494 | free(items); |
| 495 | items = NULL; |
| 496 | } |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 497 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 498 | *num_items = item_size; |
| 499 | return items; |
| 500 | } |
| 501 | |
| 502 | int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats, |
| 503 | uint32_t num_formats) |
| 504 | { |
| 505 | int ret; |
| 506 | uint32_t i, j, num_items; |
| 507 | struct kms_item *items; |
| 508 | struct combination *combo; |
| 509 | struct format_metadata metadata; |
| 510 | |
| 511 | metadata.tiling = 0; |
| 512 | metadata.priority = 1; |
| 513 | metadata.modifier = DRM_FORMAT_MOD_NONE; |
| 514 | |
| 515 | ret = drv_add_combinations(drv, formats, num_formats, &metadata, |
| 516 | BO_COMMON_USE_MASK); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | /* |
| 520 | * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary |
| 521 | * plane and as a cursor. Some drivers don't support |
| 522 | * drmModeGetPlaneResources, so add the combination here. Note that the |
| 523 | * kernel disregards the alpha component of ARGB unless it's an overlay |
| 524 | * plane. |
| 525 | */ |
| 526 | drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, |
| 527 | BO_USE_CURSOR | BO_USE_SCANOUT); |
| 528 | drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, |
| 529 | BO_USE_CURSOR | BO_USE_SCANOUT); |
| 530 | |
| 531 | items = drv_query_kms(drv, &num_items); |
| 532 | if (!items || !num_items) |
| 533 | return 0; |
| 534 | |
| 535 | for (i = 0; i < num_items; i++) { |
| 536 | for (j = 0; j < drv->backend->combos.size; j++) { |
| 537 | combo = &drv->backend->combos.data[j]; |
| 538 | if (items[i].format == combo->format) |
| 539 | combo->usage |= BO_USE_SCANOUT; |
| 540 | |
| 541 | |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | free(items); |
| 546 | return 0; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 547 | } |