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