blob: e89c0674149c86d706a3e8b46efcf942b43cc08b [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>
12#include <xf86drm.h>
13
14#include "gbm_priv.h"
Lauri Peltonen904a8792015-01-17 13:57:51 +020015#include "helpers.h"
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050016#include "util.h"
17
18size_t gbm_num_planes_from_format(uint32_t format)
19{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050020 switch(format)
21 {
22 case GBM_FORMAT_C8:
Dongseong Hwangb26f26d2016-04-07 14:51:29 +030023 case GBM_FORMAT_R8:
24 case GBM_FORMAT_RG88:
25 case GBM_FORMAT_GR88:
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050026 case GBM_FORMAT_RGB332:
27 case GBM_FORMAT_BGR233:
28 case GBM_FORMAT_XRGB4444:
29 case GBM_FORMAT_XBGR4444:
30 case GBM_FORMAT_RGBX4444:
31 case GBM_FORMAT_BGRX4444:
32 case GBM_FORMAT_ARGB4444:
33 case GBM_FORMAT_ABGR4444:
34 case GBM_FORMAT_RGBA4444:
35 case GBM_FORMAT_BGRA4444:
36 case GBM_FORMAT_XRGB1555:
37 case GBM_FORMAT_XBGR1555:
38 case GBM_FORMAT_RGBX5551:
39 case GBM_FORMAT_BGRX5551:
40 case GBM_FORMAT_ARGB1555:
41 case GBM_FORMAT_ABGR1555:
42 case GBM_FORMAT_RGBA5551:
43 case GBM_FORMAT_BGRA5551:
44 case GBM_FORMAT_RGB565:
45 case GBM_FORMAT_BGR565:
46 case GBM_FORMAT_YUYV:
47 case GBM_FORMAT_YVYU:
48 case GBM_FORMAT_UYVY:
49 case GBM_FORMAT_VYUY:
50 case GBM_FORMAT_RGB888:
51 case GBM_FORMAT_BGR888:
52 case GBM_FORMAT_XRGB8888:
53 case GBM_FORMAT_XBGR8888:
54 case GBM_FORMAT_RGBX8888:
55 case GBM_FORMAT_BGRX8888:
56 case GBM_FORMAT_ARGB8888:
57 case GBM_FORMAT_ABGR8888:
58 case GBM_FORMAT_RGBA8888:
59 case GBM_FORMAT_BGRA8888:
60 case GBM_FORMAT_XRGB2101010:
61 case GBM_FORMAT_XBGR2101010:
62 case GBM_FORMAT_RGBX1010102:
63 case GBM_FORMAT_BGRX1010102:
64 case GBM_FORMAT_ARGB2101010:
65 case GBM_FORMAT_ABGR2101010:
66 case GBM_FORMAT_RGBA1010102:
67 case GBM_FORMAT_BGRA1010102:
68 case GBM_FORMAT_AYUV:
69 return 1;
70 case GBM_FORMAT_NV12:
71 return 2;
72 }
73
74 fprintf(stderr, "minigbm: UNKNOWN FORMAT %d\n", format);
75 return 0;
76}
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077
78int gbm_bpp_from_format(uint32_t format)
79{
Stéphane Marchesin25a26062014-09-12 16:18:59 -070080 switch(format)
81 {
82 case GBM_FORMAT_C8:
Dongseong Hwangb26f26d2016-04-07 14:51:29 +030083 case GBM_FORMAT_R8:
Stéphane Marchesin25a26062014-09-12 16:18:59 -070084 case GBM_FORMAT_RGB332:
85 case GBM_FORMAT_BGR233:
86 return 8;
87
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050088 case GBM_FORMAT_NV12:
89 return 12;
90
Dongseong Hwangb26f26d2016-04-07 14:51:29 +030091 case GBM_FORMAT_RG88:
92 case GBM_FORMAT_GR88:
Stéphane Marchesin25a26062014-09-12 16:18:59 -070093 case GBM_FORMAT_XRGB4444:
94 case GBM_FORMAT_XBGR4444:
95 case GBM_FORMAT_RGBX4444:
96 case GBM_FORMAT_BGRX4444:
97 case GBM_FORMAT_ARGB4444:
98 case GBM_FORMAT_ABGR4444:
99 case GBM_FORMAT_RGBA4444:
100 case GBM_FORMAT_BGRA4444:
101 case GBM_FORMAT_XRGB1555:
102 case GBM_FORMAT_XBGR1555:
103 case GBM_FORMAT_RGBX5551:
104 case GBM_FORMAT_BGRX5551:
105 case GBM_FORMAT_ARGB1555:
106 case GBM_FORMAT_ABGR1555:
107 case GBM_FORMAT_RGBA5551:
108 case GBM_FORMAT_BGRA5551:
109 case GBM_FORMAT_RGB565:
110 case GBM_FORMAT_BGR565:
William Xiedc94b472015-10-23 10:15:17 +0800111 case GBM_FORMAT_YUYV:
112 case GBM_FORMAT_YVYU:
113 case GBM_FORMAT_UYVY:
114 case GBM_FORMAT_VYUY:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700115 return 16;
116
117 case GBM_FORMAT_RGB888:
118 case GBM_FORMAT_BGR888:
119 return 24;
120
121 case GBM_FORMAT_XRGB8888:
122 case GBM_FORMAT_XBGR8888:
123 case GBM_FORMAT_RGBX8888:
124 case GBM_FORMAT_BGRX8888:
125 case GBM_FORMAT_ARGB8888:
126 case GBM_FORMAT_ABGR8888:
127 case GBM_FORMAT_RGBA8888:
128 case GBM_FORMAT_BGRA8888:
129 case GBM_FORMAT_XRGB2101010:
130 case GBM_FORMAT_XBGR2101010:
131 case GBM_FORMAT_RGBX1010102:
132 case GBM_FORMAT_BGRX1010102:
133 case GBM_FORMAT_ARGB2101010:
134 case GBM_FORMAT_ABGR2101010:
135 case GBM_FORMAT_RGBA1010102:
136 case GBM_FORMAT_BGRA1010102:
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700137 case GBM_FORMAT_AYUV:
138 return 32;
139 }
140
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500141 fprintf(stderr, "minigbm: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700142 return 0;
143}
144
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500145int gbm_stride_from_format(uint32_t format, uint32_t width)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700146{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500147 /* Only single-plane formats are supported */
148 assert(gbm_num_planes_from_format(format) == 1);
149 return DIV_ROUND_UP(width * gbm_bpp_from_format(format), 8);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700150}
151
152int gbm_is_format_supported(struct gbm_bo *bo)
153{
154 return 1;
155}
156
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800157int gbm_dumb_bo_create(struct gbm_bo *bo, uint32_t width, uint32_t height,
158 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700159{
160 struct drm_mode_create_dumb create_dumb;
161 int ret;
162
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500163 /* Only single-plane formats are supported */
164 assert(gbm_num_planes_from_format(format) == 1);
165
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700166 memset(&create_dumb, 0, sizeof(create_dumb));
167 create_dumb.height = height;
168 create_dumb.width = width;
169 create_dumb.bpp = gbm_bpp_from_format(format);
170 create_dumb.flags = 0;
171
172 ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700173 if (ret) {
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500174 fprintf(stderr, "minigbm: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700175 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700176 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700177
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500178 bo->handles[0].u32 = create_dumb.handle;
179 bo->offsets[0] = 0;
180 bo->sizes[0] = create_dumb.size;
181 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700182
183 return 0;
184}
185
186int gbm_dumb_bo_destroy(struct gbm_bo *bo)
187{
188 struct drm_mode_destroy_dumb destroy_dumb;
189 int ret;
190
191 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500192 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700193
194 ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700195 if (ret) {
196 fprintf(stderr, "minigbm: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500197 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700198 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700199 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700200
201 return 0;
202}
203
204int gbm_gem_bo_destroy(struct gbm_bo *bo)
205{
206 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500207 int ret, error = 0;
208 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700209
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500210 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700211 for (i = 0; i < plane; i++)
212 if (bo->handles[i].u32 == bo->handles[plane].u32)
213 break;
214 /* Make sure close hasn't already been called on this handle */
215 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500216 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500218 memset(&gem_close, 0, sizeof(gem_close));
219 gem_close.handle = bo->handles[plane].u32;
220
221 ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
222 if (ret) {
223 fprintf(stderr, "minigbm: DRM_IOCTL_GEM_CLOSE failed "
224 "(handle=%x) error %d\n",
225 bo->handles[plane].u32, ret);
226 error = ret;
227 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700228 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700229
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500230 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700231}