blob: 9336ced0583d430fc845c6c9f4ef9836f4397b1d [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{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050021 switch(format)
22 {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070023 case DRV_FORMAT_C8:
24 case DRV_FORMAT_R8:
25 case DRV_FORMAT_RG88:
26 case DRV_FORMAT_GR88:
27 case DRV_FORMAT_RGB332:
28 case DRV_FORMAT_BGR233:
29 case DRV_FORMAT_XRGB4444:
30 case DRV_FORMAT_XBGR4444:
31 case DRV_FORMAT_RGBX4444:
32 case DRV_FORMAT_BGRX4444:
33 case DRV_FORMAT_ARGB4444:
34 case DRV_FORMAT_ABGR4444:
35 case DRV_FORMAT_RGBA4444:
36 case DRV_FORMAT_BGRA4444:
37 case DRV_FORMAT_XRGB1555:
38 case DRV_FORMAT_XBGR1555:
39 case DRV_FORMAT_RGBX5551:
40 case DRV_FORMAT_BGRX5551:
41 case DRV_FORMAT_ARGB1555:
42 case DRV_FORMAT_ABGR1555:
43 case DRV_FORMAT_RGBA5551:
44 case DRV_FORMAT_BGRA5551:
45 case DRV_FORMAT_RGB565:
46 case DRV_FORMAT_BGR565:
47 case DRV_FORMAT_YUYV:
48 case DRV_FORMAT_YVYU:
49 case DRV_FORMAT_UYVY:
50 case DRV_FORMAT_VYUY:
51 case DRV_FORMAT_RGB888:
52 case DRV_FORMAT_BGR888:
53 case DRV_FORMAT_XRGB8888:
54 case DRV_FORMAT_XBGR8888:
55 case DRV_FORMAT_RGBX8888:
56 case DRV_FORMAT_BGRX8888:
57 case DRV_FORMAT_ARGB8888:
58 case DRV_FORMAT_ABGR8888:
59 case DRV_FORMAT_RGBA8888:
60 case DRV_FORMAT_BGRA8888:
61 case DRV_FORMAT_XRGB2101010:
62 case DRV_FORMAT_XBGR2101010:
63 case DRV_FORMAT_RGBX1010102:
64 case DRV_FORMAT_BGRX1010102:
65 case DRV_FORMAT_ARGB2101010:
66 case DRV_FORMAT_ABGR2101010:
67 case DRV_FORMAT_RGBA1010102:
68 case DRV_FORMAT_BGRA1010102:
69 case DRV_FORMAT_AYUV:
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050070 return 1;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070071 case DRV_FORMAT_NV12:
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050072 return 2;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070073 case DRV_FORMAT_YVU420:
74 return 3;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050075 }
76
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070077 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050078 return 0;
79}
Stéphane Marchesin25a26062014-09-12 16:18:59 -070080
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070081int drv_bpp_from_format(uint32_t format, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070082{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070083 assert(plane < drv_num_planes_from_format(format));
84
85 switch (format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070086 {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070087 case DRV_FORMAT_C8:
88 case DRV_FORMAT_R8:
89 case DRV_FORMAT_RGB332:
90 case DRV_FORMAT_BGR233:
Stéphane Marchesin25a26062014-09-12 16:18:59 -070091 return 8;
92
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070093 case DRV_FORMAT_NV12:
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070094 return (plane == 0) ? 8 : 4;
95 case DRV_FORMAT_YVU420:
96 return (plane == 0) ? 8 : 2;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050097
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070098 case DRV_FORMAT_RG88:
99 case DRV_FORMAT_GR88:
100 case DRV_FORMAT_XRGB4444:
101 case DRV_FORMAT_XBGR4444:
102 case DRV_FORMAT_RGBX4444:
103 case DRV_FORMAT_BGRX4444:
104 case DRV_FORMAT_ARGB4444:
105 case DRV_FORMAT_ABGR4444:
106 case DRV_FORMAT_RGBA4444:
107 case DRV_FORMAT_BGRA4444:
108 case DRV_FORMAT_XRGB1555:
109 case DRV_FORMAT_XBGR1555:
110 case DRV_FORMAT_RGBX5551:
111 case DRV_FORMAT_BGRX5551:
112 case DRV_FORMAT_ARGB1555:
113 case DRV_FORMAT_ABGR1555:
114 case DRV_FORMAT_RGBA5551:
115 case DRV_FORMAT_BGRA5551:
116 case DRV_FORMAT_RGB565:
117 case DRV_FORMAT_BGR565:
118 case DRV_FORMAT_YUYV:
119 case DRV_FORMAT_YVYU:
120 case DRV_FORMAT_UYVY:
121 case DRV_FORMAT_VYUY:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700122 return 16;
123
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700124 case DRV_FORMAT_RGB888:
125 case DRV_FORMAT_BGR888:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700126 return 24;
127
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128 case DRV_FORMAT_XRGB8888:
129 case DRV_FORMAT_XBGR8888:
130 case DRV_FORMAT_RGBX8888:
131 case DRV_FORMAT_BGRX8888:
132 case DRV_FORMAT_ARGB8888:
133 case DRV_FORMAT_ABGR8888:
134 case DRV_FORMAT_RGBA8888:
135 case DRV_FORMAT_BGRA8888:
136 case DRV_FORMAT_XRGB2101010:
137 case DRV_FORMAT_XBGR2101010:
138 case DRV_FORMAT_RGBX1010102:
139 case DRV_FORMAT_BGRX1010102:
140 case DRV_FORMAT_ARGB2101010:
141 case DRV_FORMAT_ABGR2101010:
142 case DRV_FORMAT_RGBA1010102:
143 case DRV_FORMAT_BGRA1010102:
144 case DRV_FORMAT_AYUV:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700145 return 32;
146 }
147
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700148 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700149 return 0;
150}
151
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700152/*
153 * This function returns the stride for a given format, width and plane.
154 */
155int drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700156{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700157 /* Get stride of the first plane */
158 int stride = width * DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8);
159
160 /*
161 * Only downsample for certain multiplanar formats which are not
162 * interleaved and have horizontal subsampling. Only formats supported
163 * by our drivers are listed here -- add more as needed.
164 */
165 if (plane != 0) {
166 switch (format) {
167 case DRV_FORMAT_YVU420:
168 stride = stride / 2;
169 }
170 }
171
172 return stride;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173}
174
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700175int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800176 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700177{
178 struct drm_mode_create_dumb create_dumb;
179 int ret;
180
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500181 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700182 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500183
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700184 memset(&create_dumb, 0, sizeof(create_dumb));
185 create_dumb.height = height;
186 create_dumb.width = width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700187 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700188 create_dumb.flags = 0;
189
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700191 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700193 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700194 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700195
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500196 bo->handles[0].u32 = create_dumb.handle;
197 bo->offsets[0] = 0;
198 bo->sizes[0] = create_dumb.size;
199 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700200
201 return 0;
202}
203
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700204int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700205{
206 struct drm_mode_destroy_dumb destroy_dumb;
207 int ret;
208
209 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500210 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700211
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700212 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700213 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500215 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700216 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700217 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700218
219 return 0;
220}
221
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700223{
224 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500225 int ret, error = 0;
226 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700227
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500228 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700229 for (i = 0; i < plane; i++)
230 if (bo->handles[i].u32 == bo->handles[plane].u32)
231 break;
232 /* Make sure close hasn't already been called on this handle */
233 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500234 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700235
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500236 memset(&gem_close, 0, sizeof(gem_close));
237 gem_close.handle = bo->handles[plane].u32;
238
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700239 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500240 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500242 "(handle=%x) error %d\n",
243 bo->handles[plane].u32, ret);
244 error = ret;
245 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700246 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700247
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500248 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700249}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700250
Gurchetan Singhef920532016-08-12 16:38:25 -0700251void *drv_dumb_bo_map(struct bo *bo)
252{
253 int ret;
254 struct drm_mode_map_dumb map_dumb;
255
256 memset(&map_dumb, 0, sizeof(map_dumb));
257 map_dumb.handle = bo->handles[0].u32;
258
259 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
260 if (ret) {
261 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
262 return MAP_FAILED;
263 }
264
265 return mmap(0, bo->sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED,
266 bo->drv->fd, map_dumb.offset);
267}
268
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700269uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
270 size_t plane)
271{
272 void *count;
273 uintptr_t num = 0;
274
275 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
276 num = (uintptr_t) (count);
277
278 return num;
279}
280
281void drv_increment_reference_count(struct driver *drv, struct bo *bo,
282 size_t plane)
283{
284 uintptr_t num = drv_get_reference_count(drv, bo, plane);
285
286 /* If a value isn't in the table, drmHashDelete is a no-op */
287 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
288 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
289 (void *) (num + 1));
290}
291
292void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
293 size_t plane)
294{
295 uintptr_t num = drv_get_reference_count(drv, bo, plane);
296
297 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
298
299 if (num > 0)
300 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
301 (void *) (num - 1));
302}
Gurchetan Singhef920532016-08-12 16:38:25 -0700303
304uint32_t drv_num_buffers_per_bo(struct bo *bo)
305{
306 uint32_t count = 0;
307 size_t plane, p;
308
309 for (plane = 0; plane < bo->num_planes; plane++) {
310 for (p = 0; p < plane; p++) {
311 if (bo->handles[p].u32 == bo->handles[plane].u32)
312 break;
313 }
314
315 if (p == plane)
316 count++;
317 }
318
319 return count;
320}