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.
19 size_t drv_num_planes_from_format(uint32_t format)
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:
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:
72 case DRV_FORMAT_YVU420:
76 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
80 int drv_bpp_from_format(uint32_t format, size_t plane)
82 assert(plane < drv_num_planes_from_format(format));
87 case DRV_FORMAT_RGB332:
88 case DRV_FORMAT_BGR233:
92 return (plane == 0) ? 8 : 4;
93 case DRV_FORMAT_YVU420:
94 return (plane == 0) ? 8 : 2;
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:
122 case DRV_FORMAT_RGB888:
123 case DRV_FORMAT_BGR888:
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:
146 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
151 * This function returns the stride for a given format, width and plane.
153 int drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
155 /* Get stride of the first plane */
156 int stride = width * DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8);
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.
165 case DRV_FORMAT_YVU420:
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
178 int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
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];
190 bo->offsets[1] = bo->sizes[0];
191 bo->offsets[2] = bo->offsets[1] + bo->sizes[1];
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;
199 bo->offsets[1] = height * bo->strides[0];
202 bo->strides[0] = drv_stride_from_format(format, width, 0);
203 bo->sizes[0] = height * bo->strides[0];
207 bo->total_size = bo->offsets[bo->num_planes - 1] +
208 bo->sizes[bo->num_planes - 1];
213 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
214 uint32_t format, uint32_t flags)
216 struct drm_mode_create_dumb create_dumb;
219 /* Only single-plane formats are supported */
220 assert(drv_num_planes_from_format(format) == 1);
222 memset(&create_dumb, 0, sizeof(create_dumb));
223 create_dumb.height = height;
224 create_dumb.width = width;
225 create_dumb.bpp = drv_bpp_from_format(format, 0);
226 create_dumb.flags = 0;
228 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
230 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
234 bo->handles[0].u32 = create_dumb.handle;
236 bo->total_size = bo->sizes[0] = create_dumb.size;
237 bo->strides[0] = create_dumb.pitch;
242 int drv_dumb_bo_destroy(struct bo *bo)
244 struct drm_mode_destroy_dumb destroy_dumb;
247 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
248 destroy_dumb.handle = bo->handles[0].u32;
250 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
252 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
253 "(handle=%x)\n", bo->handles[0].u32);
260 int drv_gem_bo_destroy(struct bo *bo)
262 struct drm_gem_close gem_close;
266 for (plane = 0; plane < bo->num_planes; plane++) {
267 for (i = 0; i < plane; i++)
268 if (bo->handles[i].u32 == bo->handles[plane].u32)
270 /* Make sure close hasn't already been called on this handle */
274 memset(&gem_close, 0, sizeof(gem_close));
275 gem_close.handle = bo->handles[plane].u32;
277 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
279 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
280 "(handle=%x) error %d\n",
281 bo->handles[plane].u32, ret);
289 void *drv_dumb_bo_map(struct bo *bo)
292 struct drm_mode_map_dumb map_dumb;
294 memset(&map_dumb, 0, sizeof(map_dumb));
295 map_dumb.handle = bo->handles[0].u32;
297 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
299 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
303 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
304 bo->drv->fd, map_dumb.offset);
307 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
313 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
314 num = (uintptr_t) (count);
319 void drv_increment_reference_count(struct driver *drv, struct bo *bo,
322 uintptr_t num = drv_get_reference_count(drv, bo, plane);
324 /* If a value isn't in the table, drmHashDelete is a no-op */
325 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
326 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
330 void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
333 uintptr_t num = drv_get_reference_count(drv, bo, plane);
335 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
338 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
342 uint32_t drv_num_buffers_per_bo(struct bo *bo)
347 for (plane = 0; plane < bo->num_planes; plane++) {
348 for (p = 0; p < plane; p++) {
349 if (bo->handles[p].u32 == bo->handles[plane].u32)
360 uint32_t drv_log_base2(uint32_t value)