Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <stdbool.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 10 | #include <stdlib.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 11 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 12 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 13 | #include <xf86drm.h> |
| 14 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 15 | #include "drv_priv.h" |
Lauri Peltonen | 904a879 | 2015-01-17 13:57:51 +0200 | [diff] [blame] | 16 | #include "helpers.h" |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 17 | #include "util.h" |
| 18 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 19 | size_t drv_num_planes_from_format(uint32_t format) |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 20 | { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 21 | switch (format) { |
| 22 | case DRV_FORMAT_C8: |
| 23 | case DRV_FORMAT_R8: |
| 24 | case DRV_FORMAT_RG88: |
| 25 | case DRV_FORMAT_GR88: |
| 26 | case DRV_FORMAT_RGB332: |
| 27 | case DRV_FORMAT_BGR233: |
| 28 | case DRV_FORMAT_XRGB4444: |
| 29 | case DRV_FORMAT_XBGR4444: |
| 30 | case DRV_FORMAT_RGBX4444: |
| 31 | case DRV_FORMAT_BGRX4444: |
| 32 | case DRV_FORMAT_ARGB4444: |
| 33 | case DRV_FORMAT_ABGR4444: |
| 34 | case DRV_FORMAT_RGBA4444: |
| 35 | case DRV_FORMAT_BGRA4444: |
| 36 | case DRV_FORMAT_XRGB1555: |
| 37 | case DRV_FORMAT_XBGR1555: |
| 38 | case DRV_FORMAT_RGBX5551: |
| 39 | case DRV_FORMAT_BGRX5551: |
| 40 | case DRV_FORMAT_ARGB1555: |
| 41 | case DRV_FORMAT_ABGR1555: |
| 42 | case DRV_FORMAT_RGBA5551: |
| 43 | case DRV_FORMAT_BGRA5551: |
| 44 | case DRV_FORMAT_RGB565: |
| 45 | case DRV_FORMAT_BGR565: |
| 46 | case DRV_FORMAT_YUYV: |
| 47 | case DRV_FORMAT_YVYU: |
| 48 | case DRV_FORMAT_UYVY: |
| 49 | case DRV_FORMAT_VYUY: |
| 50 | case DRV_FORMAT_RGB888: |
| 51 | case DRV_FORMAT_BGR888: |
| 52 | case DRV_FORMAT_XRGB8888: |
| 53 | case DRV_FORMAT_XBGR8888: |
| 54 | case DRV_FORMAT_RGBX8888: |
| 55 | case DRV_FORMAT_BGRX8888: |
| 56 | case DRV_FORMAT_ARGB8888: |
| 57 | case DRV_FORMAT_ABGR8888: |
| 58 | case DRV_FORMAT_RGBA8888: |
| 59 | case DRV_FORMAT_BGRA8888: |
| 60 | case DRV_FORMAT_XRGB2101010: |
| 61 | case DRV_FORMAT_XBGR2101010: |
| 62 | case DRV_FORMAT_RGBX1010102: |
| 63 | case DRV_FORMAT_BGRX1010102: |
| 64 | case DRV_FORMAT_ARGB2101010: |
| 65 | case DRV_FORMAT_ABGR2101010: |
| 66 | case DRV_FORMAT_RGBA1010102: |
| 67 | case DRV_FORMAT_BGRA1010102: |
| 68 | case DRV_FORMAT_AYUV: |
| 69 | return 1; |
| 70 | case DRV_FORMAT_NV12: |
| 71 | return 2; |
| 72 | case DRV_FORMAT_YVU420: |
| 73 | return 3; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 76 | fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 77 | return 0; |
| 78 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 79 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 80 | int drv_bpp_from_format(uint32_t format, size_t plane) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 81 | { |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 82 | assert(plane < drv_num_planes_from_format(format)); |
| 83 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 84 | switch (format) { |
| 85 | case DRV_FORMAT_C8: |
| 86 | case DRV_FORMAT_R8: |
| 87 | case DRV_FORMAT_RGB332: |
| 88 | case DRV_FORMAT_BGR233: |
| 89 | return 8; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 90 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 91 | case DRV_FORMAT_NV12: |
| 92 | return (plane == 0) ? 8 : 4; |
| 93 | case DRV_FORMAT_YVU420: |
| 94 | return (plane == 0) ? 8 : 2; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 95 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 96 | case DRV_FORMAT_RG88: |
| 97 | case DRV_FORMAT_GR88: |
| 98 | case DRV_FORMAT_XRGB4444: |
| 99 | case DRV_FORMAT_XBGR4444: |
| 100 | case DRV_FORMAT_RGBX4444: |
| 101 | case DRV_FORMAT_BGRX4444: |
| 102 | case DRV_FORMAT_ARGB4444: |
| 103 | case DRV_FORMAT_ABGR4444: |
| 104 | case DRV_FORMAT_RGBA4444: |
| 105 | case DRV_FORMAT_BGRA4444: |
| 106 | case DRV_FORMAT_XRGB1555: |
| 107 | case DRV_FORMAT_XBGR1555: |
| 108 | case DRV_FORMAT_RGBX5551: |
| 109 | case DRV_FORMAT_BGRX5551: |
| 110 | case DRV_FORMAT_ARGB1555: |
| 111 | case DRV_FORMAT_ABGR1555: |
| 112 | case DRV_FORMAT_RGBA5551: |
| 113 | case DRV_FORMAT_BGRA5551: |
| 114 | case DRV_FORMAT_RGB565: |
| 115 | case DRV_FORMAT_BGR565: |
| 116 | case DRV_FORMAT_YUYV: |
| 117 | case DRV_FORMAT_YVYU: |
| 118 | case DRV_FORMAT_UYVY: |
| 119 | case DRV_FORMAT_VYUY: |
| 120 | return 16; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 121 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 122 | case DRV_FORMAT_RGB888: |
| 123 | case DRV_FORMAT_BGR888: |
| 124 | return 24; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 125 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 126 | case DRV_FORMAT_XRGB8888: |
| 127 | case DRV_FORMAT_XBGR8888: |
| 128 | case DRV_FORMAT_RGBX8888: |
| 129 | case DRV_FORMAT_BGRX8888: |
| 130 | case DRV_FORMAT_ARGB8888: |
| 131 | case DRV_FORMAT_ABGR8888: |
| 132 | case DRV_FORMAT_RGBA8888: |
| 133 | case DRV_FORMAT_BGRA8888: |
| 134 | case DRV_FORMAT_XRGB2101010: |
| 135 | case DRV_FORMAT_XBGR2101010: |
| 136 | case DRV_FORMAT_RGBX1010102: |
| 137 | case DRV_FORMAT_BGRX1010102: |
| 138 | case DRV_FORMAT_ARGB2101010: |
| 139 | case DRV_FORMAT_ABGR2101010: |
| 140 | case DRV_FORMAT_RGBA1010102: |
| 141 | case DRV_FORMAT_BGRA1010102: |
| 142 | case DRV_FORMAT_AYUV: |
| 143 | return 32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 146 | fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 150 | /* |
| 151 | * This function returns the stride for a given format, width and plane. |
| 152 | */ |
| 153 | int drv_stride_from_format(uint32_t format, uint32_t width, size_t plane) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 154 | { |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 155 | /* Get stride of the first plane */ |
| 156 | int stride = width * DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8); |
| 157 | |
| 158 | /* |
| 159 | * Only downsample for certain multiplanar formats which are not |
| 160 | * interleaved and have horizontal subsampling. Only formats supported |
| 161 | * by our drivers are listed here -- add more as needed. |
| 162 | */ |
| 163 | if (plane != 0) { |
| 164 | switch (format) { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 165 | case DRV_FORMAT_YVU420: |
| 166 | stride = stride / 2; |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | return stride; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 173 | /* |
| 174 | * This function fills in the buffer object given driver aligned dimensions |
| 175 | * and a format. This function assumes there is just one kernel buffer per |
| 176 | * buffer object. |
| 177 | */ |
| 178 | int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, |
| 179 | drv_format_t format) |
| 180 | { |
| 181 | |
| 182 | switch (format) { |
| 183 | case DRV_FORMAT_YVU420: |
| 184 | bo->strides[0] = drv_stride_from_format(format, width, 0); |
| 185 | bo->strides[1] = drv_stride_from_format(format, width, 1); |
| 186 | bo->strides[2] = drv_stride_from_format(format, width, 2); |
| 187 | bo->sizes[0] = height * bo->strides[0]; |
| 188 | bo->sizes[1] = bo->sizes[2] = (height / 2) * bo->strides[1]; |
| 189 | bo->offsets[0] = 0; |
| 190 | bo->offsets[1] = bo->sizes[0]; |
| 191 | bo->offsets[2] = bo->offsets[1] + bo->sizes[1]; |
| 192 | break; |
| 193 | case DRV_FORMAT_NV12: |
| 194 | bo->strides[0] = drv_stride_from_format(format, width, 0); |
| 195 | bo->strides[1] = drv_stride_from_format(format, width, 1); |
| 196 | bo->sizes[0] = height * bo->strides[0]; |
| 197 | bo->sizes[1] = height * bo->strides[1] / 2; |
| 198 | bo->offsets[0] = 0; |
| 199 | bo->offsets[1] = height * bo->strides[0]; |
| 200 | break; |
| 201 | default: |
| 202 | bo->strides[0] = drv_stride_from_format(format, width, 0); |
| 203 | bo->sizes[0] = height * bo->strides[0]; |
| 204 | bo->offsets[0] = 0; |
| 205 | } |
| 206 | |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 207 | bo->total_size = bo->offsets[bo->num_planes - 1] + |
| 208 | bo->sizes[bo->num_planes - 1]; |
| 209 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 213 | int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, |
Stéphane Marchesin | ec88e89 | 2015-11-03 16:14:59 -0800 | [diff] [blame] | 214 | uint32_t format, uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 215 | { |
| 216 | struct drm_mode_create_dumb create_dumb; |
| 217 | int ret; |
| 218 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 219 | /* Only single-plane formats are supported */ |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 220 | assert(drv_num_planes_from_format(format) == 1); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 221 | |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 222 | memset(&create_dumb, 0, sizeof(create_dumb)); |
| 223 | create_dumb.height = height; |
| 224 | create_dumb.width = width; |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 225 | create_dumb.bpp = drv_bpp_from_format(format, 0); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 226 | create_dumb.flags = 0; |
| 227 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 228 | 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] | 229 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 230 | fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n"); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 231 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 232 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 233 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 234 | bo->handles[0].u32 = create_dumb.handle; |
| 235 | bo->offsets[0] = 0; |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 236 | bo->total_size = bo->sizes[0] = create_dumb.size; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 237 | bo->strides[0] = create_dumb.pitch; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 242 | int drv_dumb_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 243 | { |
| 244 | struct drm_mode_destroy_dumb destroy_dumb; |
| 245 | int ret; |
| 246 | |
| 247 | memset(&destroy_dumb, 0, sizeof(destroy_dumb)); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 248 | destroy_dumb.handle = bo->handles[0].u32; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 249 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 250 | 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] | 251 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 252 | fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 253 | "(handle=%x)\n", bo->handles[0].u32); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 254 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 255 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 260 | int drv_gem_bo_destroy(struct bo *bo) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 261 | { |
| 262 | struct drm_gem_close gem_close; |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 263 | int ret, error = 0; |
| 264 | size_t plane, i; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 265 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 266 | for (plane = 0; plane < bo->num_planes; plane++) { |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 267 | for (i = 0; i < plane; i++) |
| 268 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 269 | break; |
| 270 | /* Make sure close hasn't already been called on this handle */ |
| 271 | if (i != plane) |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 272 | continue; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 273 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 274 | memset(&gem_close, 0, sizeof(gem_close)); |
| 275 | gem_close.handle = bo->handles[plane].u32; |
| 276 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 277 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 278 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 279 | fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed " |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 280 | "(handle=%x) error %d\n", |
| 281 | bo->handles[plane].u32, ret); |
| 282 | error = ret; |
| 283 | } |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 284 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 285 | |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 286 | return error; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 287 | } |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 288 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame^] | 289 | 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] | 290 | { |
| 291 | int ret; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame^] | 292 | size_t i; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 293 | struct drm_mode_map_dumb map_dumb; |
| 294 | |
| 295 | memset(&map_dumb, 0, sizeof(map_dumb)); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame^] | 296 | map_dumb.handle = bo->handles[plane].u32; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 297 | |
| 298 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); |
| 299 | if (ret) { |
| 300 | fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n"); |
| 301 | return MAP_FAILED; |
| 302 | } |
| 303 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame^] | 304 | for (i = 0; i < bo->num_planes; i++) |
| 305 | if (bo->handles[i].u32 == bo->handles[plane].u32) |
| 306 | data->length += bo->sizes[i]; |
| 307 | |
| 308 | return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 309 | bo->drv->fd, map_dumb.offset); |
| 310 | } |
| 311 | |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 312 | uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, |
| 313 | size_t plane) |
| 314 | { |
| 315 | void *count; |
| 316 | uintptr_t num = 0; |
| 317 | |
| 318 | if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count)) |
| 319 | num = (uintptr_t) (count); |
| 320 | |
| 321 | return num; |
| 322 | } |
| 323 | |
| 324 | void drv_increment_reference_count(struct driver *drv, struct bo *bo, |
| 325 | size_t plane) |
| 326 | { |
| 327 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 328 | |
| 329 | /* If a value isn't in the table, drmHashDelete is a no-op */ |
| 330 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 331 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 332 | (void *) (num + 1)); |
| 333 | } |
| 334 | |
| 335 | void drv_decrement_reference_count(struct driver *drv, struct bo *bo, |
| 336 | size_t plane) |
| 337 | { |
| 338 | uintptr_t num = drv_get_reference_count(drv, bo, plane); |
| 339 | |
| 340 | drmHashDelete(drv->buffer_table, bo->handles[plane].u32); |
| 341 | |
| 342 | if (num > 0) |
| 343 | drmHashInsert(drv->buffer_table, bo->handles[plane].u32, |
| 344 | (void *) (num - 1)); |
| 345 | } |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 346 | |
| 347 | uint32_t drv_num_buffers_per_bo(struct bo *bo) |
| 348 | { |
| 349 | uint32_t count = 0; |
| 350 | size_t plane, p; |
| 351 | |
| 352 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 353 | for (p = 0; p < plane; p++) { |
| 354 | if (bo->handles[p].u32 == bo->handles[plane].u32) |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (p == plane) |
| 359 | count++; |
| 360 | } |
| 361 | |
| 362 | return count; |
| 363 | } |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 364 | |
| 365 | uint32_t drv_log_base2(uint32_t value) |
| 366 | { |
| 367 | int ret = 0; |
| 368 | |
| 369 | while (value >>= 1) |
| 370 | ++ret; |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |