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