Allow importing VMOs as surfaces

Clients can now use VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 to create a
surface from a VMO. The VMO can be specified as the ID for the image.

Change-Id: I519a82ab0663db63dc78ceb4ffb89410a0de0d84
diff --git a/media_driver/linux/common/os/magma/mos_bufmgr_magma.cpp b/media_driver/linux/common/os/magma/mos_bufmgr_magma.cpp
index 0a3a82b..fd4afd1 100644
--- a/media_driver/linux/common/os/magma/mos_bufmgr_magma.cpp
+++ b/media_driver/linux/common/os/magma/mos_bufmgr_magma.cpp
@@ -69,6 +69,7 @@
     }
 
     MagmaBo* CreateBo(const char *name, unsigned long size);
+    MagmaBo* ImportBo(int zx_handle, unsigned long size);
 
     InflightList* inflight_list() { return &inflight_list_;}
 
@@ -225,6 +226,15 @@
     return bo;
 }
 
+struct mos_linux_bo *mos_bo_gem_create_from_prime(struct mos_bufmgr *mos_bufmgr, int prime_fd, int size)
+{
+    MagmaBo* bo = static_cast<MagmaBufMgr*>(mos_bufmgr)->ImportBo(prime_fd, size);
+    if (bo)
+        LOG_VERBOSE("bo_create_from_prime size %d buffer %lu", size, bo->id());
+    return bo;
+}
+
+
 static uint64_t get_pitch_with_tiling(uint64_t pitch, uint32_t tiling)
 {
     switch (tiling) {
@@ -493,6 +503,26 @@
     return bo;
 }
 
+MagmaBo* MagmaBufMgr::ImportBo(int handle, unsigned long size)
+{
+    magma_buffer_t buffer;
+    magma_status_t status = magma_import(connection(), handle, &buffer);
+    if (status != MAGMA_STATUS_OK) {
+        LOG_VERBOSE("magma_import failed: %d", status);
+        return nullptr;
+    }
+
+    auto bo = new MagmaBo(buffer, magma_get_buffer_size(buffer), this);
+
+    if (!bo->MapGpu()) {
+        bo->RemoveRef();
+        return nullptr;
+    }
+
+    return bo;
+}
+
+
 ///////////////////////////////////////////////////////////////////////////////
 
 struct mos_bufmgr* mos_bufmgr_gem_init(int fd, int /*batch_size*/)
diff --git a/media_driver/linux/common/os/magma/mos_bufmgr_stub.c b/media_driver/linux/common/os/magma/mos_bufmgr_stub.c
index 8143ec2..2ddbd3f 100644
--- a/media_driver/linux/common/os/magma/mos_bufmgr_stub.c
+++ b/media_driver/linux/common/os/magma/mos_bufmgr_stub.c
@@ -51,12 +51,6 @@
     return 0;
 }
 
-struct mos_linux_bo *mos_bo_gem_create_from_prime(struct mos_bufmgr *bufmgr, int prime_fd, int size)
-{
-    LOG_VERBOSE("mos_bo_gem_create_from_prime unimplemented");
-    return 0;
-}
-
 int mos_gem_bo_map_gtt(struct mos_linux_bo *bo)
 {
     LOG_VERBOSE("mos_gem_bo_map_gtt unimplemented");