OSDN Git Service

Merge 'goog/mirror-aosp-master' into 'goog/rvc-dev' am: 3d804156b0 am: d25b8b1f3d...
[android-x86/external-minigbm.git] / virtio_gpu.c
index e059497..4dbcc4f 100644 (file)
@@ -9,13 +9,13 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/mman.h>
-#include <virtgpu_drm.h>
 #include <xf86drm.h>
 
 #include "drv_priv.h"
 #include "helpers.h"
 #include "util.h"
 #include "virgl_hw.h"
+#include "virtgpu_drm.h"
 
 #ifndef PAGE_SIZE
 #define PAGE_SIZE 0x1000
 #define MESA_LLVMPIPE_TILE_ORDER 6
 #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
 
+struct feature {
+       uint64_t feature;
+       const char *name;
+       uint32_t enabled;
+};
+
+enum feature_id {
+       feat_3d,
+       feat_capset_fix,
+       feat_max,
+};
+
+#define FEATURE(x)                                                                                 \
+       (struct feature)                                                                           \
+       {                                                                                          \
+               x, #x, 0                                                                           \
+       }
+
+static struct feature features[] = { FEATURE(VIRTGPU_PARAM_3D_FEATURES),
+                                    FEATURE(VIRTGPU_PARAM_CAPSET_QUERY_FIX) };
+
 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
                                                  DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
                                                  DRM_FORMAT_XRGB8888 };
@@ -37,7 +58,6 @@ static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R
                                                   DRM_FORMAT_YVU420_ANDROID };
 
 struct virtio_gpu_priv {
-       int has_3d;
        int caps_is_v2;
        union virgl_caps caps;
 };
@@ -89,7 +109,7 @@ static void virtio_gpu_add_combination(struct driver *drv, uint32_t drm_format,
 {
        struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
 
-       if (priv->has_3d && priv->caps.max_version >= 1) {
+       if (features[feat_3d].enabled && priv->caps.max_version >= 1) {
                if ((use_flags & BO_USE_RENDERING) &&
                    !virtio_gpu_supports_format(&priv->caps.v1.render, drm_format)) {
                        drv_log("Skipping unsupported render format: %d\n", drm_format);
@@ -239,21 +259,11 @@ static int virtio_gpu_get_caps(struct driver *drv, union virgl_caps *caps, int *
 {
        int ret;
        struct drm_virtgpu_get_caps cap_args;
-       struct drm_virtgpu_getparam param_args;
-       uint32_t can_query_v2 = 0;
-
-       memset(&param_args, 0, sizeof(param_args));
-       param_args.param = VIRTGPU_PARAM_CAPSET_QUERY_FIX;
-       param_args.value = (uint64_t)(uintptr_t)&can_query_v2;
-       ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &param_args);
-       if (ret) {
-               drv_log("DRM_IOCTL_VIRTGPU_GETPARAM failed with %s\n", strerror(errno));
-       }
 
        *caps_is_v2 = 0;
        memset(&cap_args, 0, sizeof(cap_args));
        cap_args.addr = (unsigned long long)caps;
-       if (can_query_v2) {
+       if (features[feat_capset_fix].enabled) {
                *caps_is_v2 = 1;
                cap_args.cap_set_id = 2;
                cap_args.size = sizeof(union virgl_caps);
@@ -284,22 +294,20 @@ static int virtio_gpu_init(struct driver *drv)
 {
        int ret;
        struct virtio_gpu_priv *priv;
-       struct drm_virtgpu_getparam args;
 
        priv = calloc(1, sizeof(*priv));
        drv->priv = priv;
-
-       memset(&args, 0, sizeof(args));
-       args.param = VIRTGPU_PARAM_3D_FEATURES;
-       args.value = (uint64_t)(uintptr_t)&priv->has_3d;
-       ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &args);
-       if (ret) {
-               drv_log("virtio 3D acceleration is not available\n");
-               /* Be paranoid */
-               priv->has_3d = 0;
+       for (uint32_t i = 0; i < ARRAY_SIZE(features); i++) {
+               struct drm_virtgpu_getparam params = { 0 };
+
+               params.param = features[i].feature;
+               params.value = (uint64_t)(uintptr_t)&features[i].enabled;
+               ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &params);
+               if (ret)
+                       drv_log("DRM_IOCTL_VIRTGPU_GET_PARAM failed with %s\n", strerror(errno));
        }
 
-       if (priv->has_3d) {
+       if (features[feat_3d].enabled) {
                virtio_gpu_get_caps(drv, &priv->caps, &priv->caps_is_v2);
 
                /* This doesn't mean host can scanout everything, it just means host
@@ -337,7 +345,7 @@ static int virtio_gpu_init(struct driver *drv)
                               BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
                                   BO_USE_HW_VIDEO_ENCODER);
        drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
-                              BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
+                              BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER);
 
        return drv_modify_linear_combinations(drv);
 }
@@ -351,8 +359,7 @@ static void virtio_gpu_close(struct driver *drv)
 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
                                uint64_t use_flags)
 {
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
-       if (priv->has_3d)
+       if (features[feat_3d].enabled)
                return virtio_virgl_bo_create(bo, width, height, format, use_flags);
        else
                return virtio_dumb_bo_create(bo, width, height, format, use_flags);
@@ -360,8 +367,7 @@ static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height,
 
 static int virtio_gpu_bo_destroy(struct bo *bo)
 {
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
-       if (priv->has_3d)
+       if (features[feat_3d].enabled)
                return drv_gem_bo_destroy(bo);
        else
                return drv_dumb_bo_destroy(bo);
@@ -369,8 +375,7 @@ static int virtio_gpu_bo_destroy(struct bo *bo)
 
 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
-       if (priv->has_3d)
+       if (features[feat_3d].enabled)
                return virtio_virgl_bo_map(bo, vma, plane, map_flags);
        else
                return drv_dumb_bo_map(bo, vma, plane, map_flags);
@@ -380,10 +385,9 @@ static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
 {
        int ret;
        struct drm_virtgpu_3d_transfer_from_host xfer;
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
        struct drm_virtgpu_3d_wait waitcmd;
 
-       if (!priv->has_3d)
+       if (!features[feat_3d].enabled)
                return 0;
 
        // Invalidate is only necessary if the host writes to the buffer.
@@ -434,10 +438,9 @@ static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
 {
        int ret;
        struct drm_virtgpu_3d_transfer_to_host xfer;
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
        struct drm_virtgpu_3d_wait waitcmd;
 
-       if (!priv->has_3d)
+       if (!features[feat_3d].enabled)
                return 0;
 
        if (!(mapping->vma->map_flags & BO_MAP_WRITE))
@@ -482,7 +485,6 @@ static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
 
 static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
 {
-       struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
 
        switch (format) {
        case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
@@ -496,7 +498,7 @@ static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, u
                 * All of our host drivers prefer NV12 as their flexible media format.
                 * If that changes, this will need to be modified.
                 */
-               if (priv->has_3d)
+               if (features[feat_3d].enabled)
                        return DRM_FORMAT_NV12;
                else
                        return DRM_FORMAT_YVU420;
@@ -505,6 +507,37 @@ static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, u
        }
 }
 
+static int virtio_gpu_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
+                                   uint32_t offsets[DRV_MAX_PLANES])
+{
+       int ret;
+       struct drm_virtgpu_resource_info res_info;
+
+       if (!features[feat_3d].enabled)
+               return 0;
+
+       memset(&res_info, 0, sizeof(res_info));
+       res_info.bo_handle = bo->handles[0].u32;
+       ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO, &res_info);
+       if (ret) {
+               drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
+               return ret;
+       }
+
+       for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
+               /*
+                * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
+                * ioctl.
+                */
+               if (res_info.strides[plane]) {
+                       strides[plane] = res_info.strides[plane];
+                       offsets[plane] = res_info.offsets[plane];
+               }
+       }
+
+       return 0;
+}
+
 const struct backend backend_virtio_gpu = {
        .name = "virtio_gpu",
        .init = virtio_gpu_init,
@@ -517,4 +550,5 @@ const struct backend backend_virtio_gpu = {
        .bo_invalidate = virtio_gpu_bo_invalidate,
        .bo_flush = virtio_gpu_bo_flush,
        .resolve_format = virtio_gpu_resolve_format,
+       .resource_info = virtio_gpu_resource_info,
 };