blob: 8305eafad3d44ba64b34866cf8496b53a220776c [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
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 Novikov96c7a3b2015-12-08 22:48:29 -05007#include <assert.h>
8#include <stdbool.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -07009#include <stdio.h>
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050010#include <stdlib.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070011#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070012#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <xf86drm.h>
14
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070015#include "drv_priv.h"
Lauri Peltonen904a8792015-01-17 13:57:51 +020016#include "helpers.h"
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050017#include "util.h"
18
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070019size_t drv_num_planes_from_format(uint32_t format)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050020{
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070021 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 Novikov96c7a3b2015-12-08 22:48:29 -050074 }
75
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070076 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050077 return 0;
78}
Stéphane Marchesin25a26062014-09-12 16:18:59 -070079
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070080int drv_bpp_from_format(uint32_t format, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070081{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070082 assert(plane < drv_num_planes_from_format(format));
83
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070084 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 Marchesin25a26062014-09-12 16:18:59 -070090
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070091 case DRV_FORMAT_NV12:
92 return (plane == 0) ? 8 : 4;
93 case DRV_FORMAT_YVU420:
94 return (plane == 0) ? 8 : 2;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050095
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070096 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 Marchesin25a26062014-09-12 16:18:59 -0700121
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700122 case DRV_FORMAT_RGB888:
123 case DRV_FORMAT_BGR888:
124 return 24;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700125
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700126 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 Marchesin25a26062014-09-12 16:18:59 -0700144 }
145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700147 return 0;
148}
149
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700150/*
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700151 * This function fills in the buffer object given driver aligned dimensions
152 * and a format. This function assumes there is just one kernel buffer per
153 * buffer object.
154 */
155int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
156 drv_format_t format)
157{
158
159 switch (format) {
160 case DRV_FORMAT_YVU420:
161 bo->strides[0] = drv_stride_from_format(format, width, 0);
162 bo->strides[1] = drv_stride_from_format(format, width, 1);
163 bo->strides[2] = drv_stride_from_format(format, width, 2);
164 bo->sizes[0] = height * bo->strides[0];
165 bo->sizes[1] = bo->sizes[2] = (height / 2) * bo->strides[1];
166 bo->offsets[0] = 0;
167 bo->offsets[1] = bo->sizes[0];
168 bo->offsets[2] = bo->offsets[1] + bo->sizes[1];
169 break;
170 case DRV_FORMAT_NV12:
171 bo->strides[0] = drv_stride_from_format(format, width, 0);
172 bo->strides[1] = drv_stride_from_format(format, width, 1);
173 bo->sizes[0] = height * bo->strides[0];
174 bo->sizes[1] = height * bo->strides[1] / 2;
175 bo->offsets[0] = 0;
176 bo->offsets[1] = height * bo->strides[0];
177 break;
178 default:
179 bo->strides[0] = drv_stride_from_format(format, width, 0);
180 bo->sizes[0] = height * bo->strides[0];
181 bo->offsets[0] = 0;
182 }
183
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700184 bo->total_size = bo->offsets[bo->num_planes - 1] +
185 bo->sizes[bo->num_planes - 1];
186
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700187 return 0;
188}
189
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800191 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700192{
193 struct drm_mode_create_dumb create_dumb;
194 int ret;
195
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500196 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500198
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700199 memset(&create_dumb, 0, sizeof(create_dumb));
200 create_dumb.height = height;
201 create_dumb.width = width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700202 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700203 create_dumb.flags = 0;
204
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700205 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700206 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700207 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700208 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700209 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500211 bo->handles[0].u32 = create_dumb.handle;
212 bo->offsets[0] = 0;
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700213 bo->total_size = bo->sizes[0] = create_dumb.size;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500214 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700215
216 return 0;
217}
218
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700220{
221 struct drm_mode_destroy_dumb destroy_dumb;
222 int ret;
223
224 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500225 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700226
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700228 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700229 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500230 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700231 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700232 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700233
234 return 0;
235}
236
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700238{
239 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500240 int ret, error = 0;
241 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700242
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500243 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700244 for (i = 0; i < plane; i++)
245 if (bo->handles[i].u32 == bo->handles[plane].u32)
246 break;
247 /* Make sure close hasn't already been called on this handle */
248 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500249 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700250
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500251 memset(&gem_close, 0, sizeof(gem_close));
252 gem_close.handle = bo->handles[plane].u32;
253
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500255 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700256 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500257 "(handle=%x) error %d\n",
258 bo->handles[plane].u32, ret);
259 error = ret;
260 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700261 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700262
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500263 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700264}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700266void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700267{
268 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700269 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700270 struct drm_mode_map_dumb map_dumb;
271
272 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700273 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700274
275 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
276 if (ret) {
277 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
278 return MAP_FAILED;
279 }
280
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700281 for (i = 0; i < bo->num_planes; i++)
282 if (bo->handles[i].u32 == bo->handles[plane].u32)
283 data->length += bo->sizes[i];
284
285 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700286 bo->drv->fd, map_dumb.offset);
287}
288
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700289uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
290 size_t plane)
291{
292 void *count;
293 uintptr_t num = 0;
294
295 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
296 num = (uintptr_t) (count);
297
298 return num;
299}
300
301void drv_increment_reference_count(struct driver *drv, struct bo *bo,
302 size_t plane)
303{
304 uintptr_t num = drv_get_reference_count(drv, bo, plane);
305
306 /* If a value isn't in the table, drmHashDelete is a no-op */
307 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
308 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
309 (void *) (num + 1));
310}
311
312void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
313 size_t plane)
314{
315 uintptr_t num = drv_get_reference_count(drv, bo, plane);
316
317 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
318
319 if (num > 0)
320 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
321 (void *) (num - 1));
322}
Gurchetan Singhef920532016-08-12 16:38:25 -0700323
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530324uint32_t drv_log_base2(uint32_t value)
325{
326 int ret = 0;
327
328 while (value >>= 1)
329 ++ret;
330
331 return ret;
332}