OSDN Git Service

minigbm: don't advertise BGR24 as a render/texture target
[android-x86/external-minigbm.git] / virtio_gpu.c
1 /*
2  * Copyright 2017 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
7 #include <errno.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/mman.h>
12 #include <virtgpu_drm.h>
13 #include <xf86drm.h>
14
15 #include "drv_priv.h"
16 #include "helpers.h"
17 #include "util.h"
18 #include "virgl_hw.h"
19
20 #ifndef PAGE_SIZE
21 #define PAGE_SIZE 0x1000
22 #endif
23 #define PIPE_TEXTURE_2D 2
24
25 #define MESA_LLVMPIPE_TILE_ORDER 6
26 #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
27
28 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
29                                                   DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
30                                                   DRM_FORMAT_XRGB8888 };
31
32 static const uint32_t dumb_texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
33                                                         DRM_FORMAT_YVU420_ANDROID,
34                                                         DRM_FORMAT_NV12 };
35
36 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_RG88 };
37
38 struct virtio_gpu_priv {
39         int has_3d;
40 };
41
42 static uint32_t translate_format(uint32_t drm_fourcc, uint32_t plane)
43 {
44         switch (drm_fourcc) {
45         case DRM_FORMAT_XRGB8888:
46                 return VIRGL_FORMAT_B8G8R8X8_UNORM;
47         case DRM_FORMAT_ARGB8888:
48                 return VIRGL_FORMAT_B8G8R8A8_UNORM;
49         case DRM_FORMAT_XBGR8888:
50                 return VIRGL_FORMAT_R8G8B8X8_UNORM;
51         case DRM_FORMAT_ABGR8888:
52                 return VIRGL_FORMAT_R8G8B8A8_UNORM;
53         case DRM_FORMAT_RGB565:
54                 return VIRGL_FORMAT_B5G6R5_UNORM;
55         case DRM_FORMAT_R8:
56                 return VIRGL_FORMAT_R8_UNORM;
57         case DRM_FORMAT_RG88:
58                 return VIRGL_FORMAT_R8G8_UNORM;
59         default:
60                 return 0;
61         }
62 }
63
64 static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
65                                  uint64_t use_flags)
66 {
67         if (bo->format != DRM_FORMAT_R8) {
68                 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
69                 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
70         }
71
72         return drv_dumb_bo_create(bo, width, height, format, use_flags);
73 }
74
75 static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
76                                   uint64_t use_flags)
77 {
78         int ret;
79         ssize_t plane;
80         ssize_t num_planes = drv_num_planes_from_format(format);
81         uint32_t stride0;
82
83         for (plane = 0; plane < num_planes; plane++) {
84                 uint32_t stride = drv_stride_from_format(format, width, plane);
85                 uint32_t size = drv_size_from_format(format, stride, height, plane);
86                 uint32_t res_format = translate_format(format, plane);
87                 struct drm_virtgpu_resource_create res_create;
88
89                 memset(&res_create, 0, sizeof(res_create));
90                 size = ALIGN(size, PAGE_SIZE);
91                 /*
92                  * Setting the target is intended to ensure this resource gets bound as a 2D
93                  * texture in the host renderer's GL state. All of these resource properties are
94                  * sent unchanged by the kernel to the host, which in turn sends them unchanged to
95                  * virglrenderer. When virglrenderer makes a resource, it will convert the target
96                  * enum to the equivalent one in GL and then bind the resource to that target.
97                  */
98                 res_create.target = PIPE_TEXTURE_2D;
99                 res_create.format = res_format;
100                 res_create.bind = VIRGL_BIND_RENDER_TARGET;
101                 res_create.width = width;
102                 res_create.height = height;
103                 res_create.depth = 1;
104                 res_create.array_size = 1;
105                 res_create.last_level = 0;
106                 res_create.nr_samples = 0;
107                 res_create.stride = stride;
108                 res_create.size = size;
109
110                 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
111                 if (ret) {
112                         drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n",
113                                 strerror(errno));
114                         goto fail;
115                 }
116
117                 bo->handles[plane].u32 = res_create.bo_handle;
118         }
119
120         stride0 = drv_stride_from_format(format, width, 0);
121         drv_bo_from_format(bo, stride0, height, format);
122
123         for (plane = 0; plane < num_planes; plane++)
124                 bo->offsets[plane] = 0;
125
126         return 0;
127
128 fail:
129         for (plane--; plane >= 0; plane--) {
130                 struct drm_gem_close gem_close;
131                 memset(&gem_close, 0, sizeof(gem_close));
132                 gem_close.handle = bo->handles[plane].u32;
133                 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
134         }
135
136         return ret;
137 }
138
139 static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
140 {
141         int ret;
142         struct drm_virtgpu_map gem_map;
143
144         memset(&gem_map, 0, sizeof(gem_map));
145         gem_map.handle = bo->handles[0].u32;
146
147         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
148         if (ret) {
149                 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
150                 return MAP_FAILED;
151         }
152
153         vma->length = bo->total_size;
154         return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
155                     gem_map.offset);
156 }
157
158 static int virtio_gpu_init(struct driver *drv)
159 {
160         int ret;
161         struct virtio_gpu_priv *priv;
162         struct drm_virtgpu_getparam args;
163
164         priv = calloc(1, sizeof(*priv));
165         drv->priv = priv;
166
167         memset(&args, 0, sizeof(args));
168         args.param = VIRTGPU_PARAM_3D_FEATURES;
169         args.value = (uint64_t)(uintptr_t)&priv->has_3d;
170         ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &args);
171         if (ret) {
172                 drv_log("virtio 3D acceleration is not available\n");
173                 /* Be paranoid */
174                 priv->has_3d = 0;
175         }
176
177         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
178                              &LINEAR_METADATA, BO_USE_RENDER_MASK);
179
180         if (priv->has_3d)
181                 drv_add_combinations(drv, texture_source_formats,
182                                      ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
183                                      BO_USE_TEXTURE_MASK);
184         else
185                 drv_add_combinations(drv, dumb_texture_source_formats,
186                                      ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
187                                      BO_USE_TEXTURE_MASK);
188
189         /* Android CTS tests require this. */
190         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
191
192         drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
193                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
194         drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
195                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
196
197         return drv_modify_linear_combinations(drv);
198 }
199
200 static void virtio_gpu_close(struct driver *drv)
201 {
202         free(drv->priv);
203         drv->priv = NULL;
204 }
205
206 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
207                                 uint64_t use_flags)
208 {
209         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
210         if (priv->has_3d)
211                 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
212         else
213                 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
214 }
215
216 static int virtio_gpu_bo_destroy(struct bo *bo)
217 {
218         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
219         if (priv->has_3d)
220                 return drv_gem_bo_destroy(bo);
221         else
222                 return drv_dumb_bo_destroy(bo);
223 }
224
225 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
226 {
227         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
228         if (priv->has_3d)
229                 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
230         else
231                 return drv_dumb_bo_map(bo, vma, plane, map_flags);
232 }
233
234 static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
235 {
236         int ret;
237         struct drm_virtgpu_3d_transfer_from_host xfer;
238         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
239
240         if (!priv->has_3d)
241                 return 0;
242
243         memset(&xfer, 0, sizeof(xfer));
244         xfer.bo_handle = mapping->vma->handle;
245         xfer.box.x = mapping->rect.x;
246         xfer.box.y = mapping->rect.y;
247         xfer.box.w = mapping->rect.width;
248         xfer.box.h = mapping->rect.height;
249         xfer.box.d = 1;
250
251         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
252         if (ret) {
253                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", strerror(errno));
254                 return ret;
255         }
256
257         return 0;
258 }
259
260 static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
261 {
262         int ret;
263         struct drm_virtgpu_3d_transfer_to_host xfer;
264         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
265
266         if (!priv->has_3d)
267                 return 0;
268
269         if (!(mapping->vma->map_flags & BO_MAP_WRITE))
270                 return 0;
271
272         memset(&xfer, 0, sizeof(xfer));
273         xfer.bo_handle = mapping->vma->handle;
274         xfer.box.x = mapping->rect.x;
275         xfer.box.y = mapping->rect.y;
276         xfer.box.w = mapping->rect.width;
277         xfer.box.h = mapping->rect.height;
278         xfer.box.d = 1;
279
280         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
281         if (ret) {
282                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", strerror(errno));
283                 return ret;
284         }
285
286         return 0;
287 }
288
289 static uint32_t virtio_gpu_resolve_format(uint32_t format, uint64_t use_flags)
290 {
291         switch (format) {
292         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
293                 /* Camera subsystem requires NV12. */
294                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
295                         return DRM_FORMAT_NV12;
296                 /*HACK: See b/28671744 */
297                 return DRM_FORMAT_XBGR8888;
298         case DRM_FORMAT_FLEX_YCbCr_420_888:
299                 return DRM_FORMAT_YVU420;
300         default:
301                 return format;
302         }
303 }
304
305 const struct backend backend_virtio_gpu = {
306         .name = "virtio_gpu",
307         .init = virtio_gpu_init,
308         .close = virtio_gpu_close,
309         .bo_create = virtio_gpu_bo_create,
310         .bo_destroy = virtio_gpu_bo_destroy,
311         .bo_import = drv_prime_bo_import,
312         .bo_map = virtio_gpu_bo_map,
313         .bo_unmap = drv_bo_munmap,
314         .bo_invalidate = virtio_gpu_bo_invalidate,
315         .bo_flush = virtio_gpu_bo_flush,
316         .resolve_format = virtio_gpu_resolve_format,
317 };