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