blob: 3e3e8b0d07ffd2e0ceba7b53f59fdc0affc02c97 [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;
73 }
74
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070075 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050076 return 0;
77}
Stéphane Marchesin25a26062014-09-12 16:18:59 -070078
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070079int drv_bpp_from_format(uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070080{
Stéphane Marchesin25a26062014-09-12 16:18:59 -070081 switch(format)
82 {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070083 case DRV_FORMAT_C8:
84 case DRV_FORMAT_R8:
85 case DRV_FORMAT_RGB332:
86 case DRV_FORMAT_BGR233:
Stéphane Marchesin25a26062014-09-12 16:18:59 -070087 return 8;
88
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070089 case DRV_FORMAT_NV12:
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050090 return 12;
91
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070092 case DRV_FORMAT_RG88:
93 case DRV_FORMAT_GR88:
94 case DRV_FORMAT_XRGB4444:
95 case DRV_FORMAT_XBGR4444:
96 case DRV_FORMAT_RGBX4444:
97 case DRV_FORMAT_BGRX4444:
98 case DRV_FORMAT_ARGB4444:
99 case DRV_FORMAT_ABGR4444:
100 case DRV_FORMAT_RGBA4444:
101 case DRV_FORMAT_BGRA4444:
102 case DRV_FORMAT_XRGB1555:
103 case DRV_FORMAT_XBGR1555:
104 case DRV_FORMAT_RGBX5551:
105 case DRV_FORMAT_BGRX5551:
106 case DRV_FORMAT_ARGB1555:
107 case DRV_FORMAT_ABGR1555:
108 case DRV_FORMAT_RGBA5551:
109 case DRV_FORMAT_BGRA5551:
110 case DRV_FORMAT_RGB565:
111 case DRV_FORMAT_BGR565:
112 case DRV_FORMAT_YUYV:
113 case DRV_FORMAT_YVYU:
114 case DRV_FORMAT_UYVY:
115 case DRV_FORMAT_VYUY:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700116 return 16;
117
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700118 case DRV_FORMAT_RGB888:
119 case DRV_FORMAT_BGR888:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700120 return 24;
121
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700122 case DRV_FORMAT_XRGB8888:
123 case DRV_FORMAT_XBGR8888:
124 case DRV_FORMAT_RGBX8888:
125 case DRV_FORMAT_BGRX8888:
126 case DRV_FORMAT_ARGB8888:
127 case DRV_FORMAT_ABGR8888:
128 case DRV_FORMAT_RGBA8888:
129 case DRV_FORMAT_BGRA8888:
130 case DRV_FORMAT_XRGB2101010:
131 case DRV_FORMAT_XBGR2101010:
132 case DRV_FORMAT_RGBX1010102:
133 case DRV_FORMAT_BGRX1010102:
134 case DRV_FORMAT_ARGB2101010:
135 case DRV_FORMAT_ABGR2101010:
136 case DRV_FORMAT_RGBA1010102:
137 case DRV_FORMAT_BGRA1010102:
138 case DRV_FORMAT_AYUV:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700139 return 32;
140 }
141
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700142 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700143 return 0;
144}
145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146int drv_stride_from_format(uint32_t format, uint32_t width)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700147{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500148 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149 assert(drv_num_planes_from_format(format) == 1);
150 return DIV_ROUND_UP(width * drv_bpp_from_format(format), 8);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151}
152
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800154 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700155{
156 struct drm_mode_create_dumb create_dumb;
157 int ret;
158
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500159 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700160 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500161
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700162 memset(&create_dumb, 0, sizeof(create_dumb));
163 create_dumb.height = height;
164 create_dumb.width = width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700165 create_dumb.bpp = drv_bpp_from_format(format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700166 create_dumb.flags = 0;
167
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700169 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700170 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700171 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700172 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500174 bo->handles[0].u32 = create_dumb.handle;
175 bo->offsets[0] = 0;
176 bo->sizes[0] = create_dumb.size;
177 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700178
179 return 0;
180}
181
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700182int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700183{
184 struct drm_mode_destroy_dumb destroy_dumb;
185 int ret;
186
187 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500188 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700189
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_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_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500193 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700194 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700195 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700196
197 return 0;
198}
199
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700201{
202 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500203 int ret, error = 0;
204 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700205
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500206 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700207 for (i = 0; i < plane; i++)
208 if (bo->handles[i].u32 == bo->handles[plane].u32)
209 break;
210 /* Make sure close hasn't already been called on this handle */
211 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500212 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700213
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500214 memset(&gem_close, 0, sizeof(gem_close));
215 gem_close.handle = bo->handles[plane].u32;
216
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500218 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500220 "(handle=%x) error %d\n",
221 bo->handles[plane].u32, ret);
222 error = ret;
223 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700224 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700225
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500226 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700227}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700228
Gurchetan Singhef920532016-08-12 16:38:25 -0700229void *drv_dumb_bo_map(struct bo *bo)
230{
231 int ret;
232 struct drm_mode_map_dumb map_dumb;
233
234 memset(&map_dumb, 0, sizeof(map_dumb));
235 map_dumb.handle = bo->handles[0].u32;
236
237 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
238 if (ret) {
239 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
240 return MAP_FAILED;
241 }
242
243 return mmap(0, bo->sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED,
244 bo->drv->fd, map_dumb.offset);
245}
246
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700247uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
248 size_t plane)
249{
250 void *count;
251 uintptr_t num = 0;
252
253 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
254 num = (uintptr_t) (count);
255
256 return num;
257}
258
259void drv_increment_reference_count(struct driver *drv, struct bo *bo,
260 size_t plane)
261{
262 uintptr_t num = drv_get_reference_count(drv, bo, plane);
263
264 /* If a value isn't in the table, drmHashDelete is a no-op */
265 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
266 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
267 (void *) (num + 1));
268}
269
270void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
271 size_t plane)
272{
273 uintptr_t num = drv_get_reference_count(drv, bo, plane);
274
275 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
276
277 if (num > 0)
278 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
279 (void *) (num - 1));
280}
Gurchetan Singhef920532016-08-12 16:38:25 -0700281
282uint32_t drv_num_buffers_per_bo(struct bo *bo)
283{
284 uint32_t count = 0;
285 size_t plane, p;
286
287 for (plane = 0; plane < bo->num_planes; plane++) {
288 for (p = 0; p < plane; p++) {
289 if (bo->handles[p].u32 == bo->handles[plane].u32)
290 break;
291 }
292
293 if (p == plane)
294 count++;
295 }
296
297 return count;
298}