Changes to build a host-side libvirglrender.so

BUG: 77276633
Change-Id: Ie6b4ddc5010e35bd7940a5c82e84480460ef542a
Test: builds
diff --git a/host/Android.bp b/host/Android.bp
new file mode 100644
index 0000000..5464fac
--- /dev/null
+++ b/host/Android.bp
@@ -0,0 +1,5 @@
+cc_library_headers {
+  name: "virtio_gpu_uapi_headers",
+  host_supported: true,
+  export_include_dirs: ["linux_uapi"],
+}
diff --git a/host/commands/Android.bp b/host/commands/Android.bp
index 8aebc1d..485e2ef 100644
--- a/host/commands/Android.bp
+++ b/host/commands/Android.bp
@@ -1,5 +1,5 @@
 //
-// Copyright (C) 2017 The Android Open Source Project
+// Copyright (C) 2018 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -15,7 +15,18 @@
 
 subdirs = [
     "emugen",
-    "launch",
-    "stop_cvd",
-    "record_audio",
 ]
+
+python_binary_host {
+  name: "gen_entries_cuttlefish",
+  srcs: ["gen_entries.py"],
+  main: "gen_entries.py",
+  version: {
+    py2: {
+      enabled: true,
+    },
+    py3: {
+      enabled: false,
+    },
+  },
+}
diff --git a/host/commands/gen-entries.py b/host/commands/gen_entries.py
similarity index 100%
rename from host/commands/gen-entries.py
rename to host/commands/gen_entries.py
diff --git a/host/libs/virglrenderer/AVDVirglRenderer.cpp b/host/libs/virglrenderer/AVDVirglRenderer.cpp
index a089489..2be15d5 100644
--- a/host/libs/virglrenderer/AVDVirglRenderer.cpp
+++ b/host/libs/virglrenderer/AVDVirglRenderer.cpp
@@ -17,7 +17,7 @@
 
 extern "C" {
 #include <linux/virtio_gpu.h>
-#include <virgl/virglrenderer.h>
+#include <virglrenderer.h>
 #include <virgl_hw.h>
 }
 
diff --git a/host/libs/virglrenderer/Android.bp b/host/libs/virglrenderer/Android.bp
index 7fd35d9..6c31b7a 100644
--- a/host/libs/virglrenderer/Android.bp
+++ b/host/libs/virglrenderer/Android.bp
@@ -14,30 +14,195 @@
 // limitations under the License.
 //
 
+genrule {
+  name: "glesv1_dec_cuttlefish_gensrc",
+  srcs: ["GLESv1_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/GLESv1_dec " +
+       "-D $(genDir) gles1",
+  out: ["gles1_dec.cpp"],
+}
+
+genrule {
+  name: "glesv1_dec_cuttlefish_genhdr",
+  srcs: ["GLESv1_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/GLESv1_dec " +
+       "-D $(genDir) gles1",
+  out: [
+    "gles1_dec.h",
+    "gles1_opcodes.h",
+    "gles1_server_context.h",
+    "gles1_server_proc.h",
+  ],
+}
+
+genrule {
+  name: "glesv3_dec_cuttlefish_gensrc",
+  srcs: ["GLESv3_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/GLESv3_dec " +
+       "-D $(genDir) gles3",
+  out: ["gles3_dec.cpp"],
+}
+
+genrule {
+  name: "glesv3_dec_cuttlefish_genhdr",
+  srcs: ["GLESv3_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/GLESv3_dec " +
+       "-D $(genDir) gles3",
+  out: [
+    "gles3_dec.h",
+    "gles3_opcodes.h",
+    "gles3_server_context.h",
+    "gles3_server_proc.h",
+  ],
+}
+
+genrule {
+  name: "rendercontrol_dec_cuttlefish_gensrc",
+  srcs: ["renderControl_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/renderControl_dec " +
+       "-D $(genDir) renderControl",
+  out: ["renderControl_dec.cpp"],
+}
+
+genrule {
+  name: "rendercontrol_dec_cuttlefish_genhdr",
+  srcs: ["renderControl_dec/*"],
+  tools: ["emugen_cuttlefish"],
+  cmd: "$(location emugen_cuttlefish) " +
+       "-i device/generic/opengl-transport/host/libs/virglrenderer/renderControl_dec " +
+       "-D $(genDir) renderControl",
+  out: [
+    "renderControl_dec.h",
+    "renderControl_opcodes.h",
+    "renderControl_server_context.h",
+    "renderControl_server_proc.h",
+  ],
+}
+
+// out/host/linux-x86/bin/emugen_cuttlefish -i device/generic/opengl-transport/host/libs/virglrenderer/renderControl_dec -D /tmp/foo renderControl
+
+genrule {
+  name: "gles1_core_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles1_core.entries"],
+  out: ["gles1_core_functions.h"],
+}
+
+genrule {
+  name: "gles1_extensions_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles1_extensions.entries"],
+  out: ["gles1_extensions_functions.h"],
+}
+
+genrule {
+  name: "egl_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/egl.entries"],
+  out: ["egl_functions.h"],
+}
+
+genrule {
+  name: "gles3_only_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles3_only.entries"],
+  out: ["gles3_only_functions.h"],
+}
+
+genrule {
+  name: "gles31_only_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles31_only.entries"],
+  out: ["gles31_only_functions.h"],
+}
+
+genrule {
+  name: "gles2_extensions_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles2_extensions.entries"],
+  out: ["gles2_extensions_functions.h"],
+}
+
+genrule {
+  name: "egl_extensions_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/egl_extensions.entries"],
+  out: ["egl_extensions_functions.h"],
+}
+
+genrule {
+  name: "gles2_core_functions_hdr",
+  tools: ["gen_entries_cuttlefish"],
+  cmd: "$(location gen_entries_cuttlefish) --mode=funcargs $(in) --output $(out)",
+  srcs: ["OpenGLESDispatch/gles2_core.entries"],
+  out: ["gles2_core_functions.h"],
+}
+
 cc_library_host_shared {
-    name: "libvirglrenderer",
+    name: "libvirglrenderer_cuttlefish",
     include_dirs: [
         "external/libdrm",
         "external/libdrm/include",
         "device/generic/goldfish-opengl/system",
     ],
-    local_include_dirs: [ "include" ],
+    local_include_dirs: [
+      "GLESv1_dec",
+      "GLESv3_dec",
+      "include",
+      "renderControl_dec",
+    ],
     srcs: [
         "AVDVirglRenderer.cpp",
         "ChecksumCalculator.cpp",
         "GLESv1.cpp",
-        "GLESv1_dec/gles1_dec.cpp",
         "GLESv3.cpp",
-        "GLESv3_dec/gles3_dec.cpp",
         "Gralloc1.cpp",
         "OpenGLESDispatch/EGLDispatch.cpp",
         "OpenGLESDispatch/GLESv1Dispatch.cpp",
         "OpenGLESDispatch/GLESv3Dispatch.cpp",
         "RenderControl.cpp",
-        "renderControl_dec/renderControl_dec.cpp",
     ],
     cflags: ["-Wno-unused-parameter", "-DOPENGL_DEBUG_PRINTOUT"],
     host_ldlibs: [ "-ldl" ],
     version_script : "libvirglrenderer.lds",
     defaults: [ "cuttlefish_host_only" ],
+    generated_sources: [
+      "glesv1_dec_cuttlefish_gensrc",
+      "glesv3_dec_cuttlefish_gensrc",
+      "rendercontrol_dec_cuttlefish_gensrc",
+    ],
+    generated_headers: [
+      "glesv1_dec_cuttlefish_genhdr",
+      "glesv3_dec_cuttlefish_genhdr",
+      "rendercontrol_dec_cuttlefish_genhdr",
+      "gles1_core_functions_hdr",
+      "gles1_extensions_functions_hdr",
+      "egl_functions_hdr",
+      "gles3_only_functions_hdr",
+      "gles31_only_functions_hdr",
+      "gles2_extensions_functions_hdr",
+      "egl_extensions_functions_hdr",
+      "gles2_core_functions_hdr",
+    ],
+    header_libs: [
+      "virtio_gpu_uapi_headers",
+      "virgl_headers"
+    ],
 }
diff --git a/host/libs/virglrenderer/GLESv1.h b/host/libs/virglrenderer/GLESv1.h
index f8b9073..00c1ec4 100644
--- a/host/libs/virglrenderer/GLESv1.h
+++ b/host/libs/virglrenderer/GLESv1.h
@@ -16,7 +16,7 @@
 
 #pragma once
 
-#include "GLESv1_dec/gles1_dec.h"
+#include "gles1_dec.h"
 
 struct GLESv1 : public gles1_decoder_context_t {
     GLESv1();
diff --git a/host/libs/virglrenderer/GLESv3.h b/host/libs/virglrenderer/GLESv3.h
index 33eab48..a077853 100644
--- a/host/libs/virglrenderer/GLESv3.h
+++ b/host/libs/virglrenderer/GLESv3.h
@@ -16,7 +16,7 @@
 
 #pragma once
 
-#include "GLESv3_dec/gles3_dec.h"
+#include "gles3_dec.h"
 
 #include <map>
 
diff --git a/host/libs/virglrenderer/OpenGLESDispatch/EGLDispatch.h b/host/libs/virglrenderer/OpenGLESDispatch/EGLDispatch.h
index eb3cd88..13c6078 100644
--- a/host/libs/virglrenderer/OpenGLESDispatch/EGLDispatch.h
+++ b/host/libs/virglrenderer/OpenGLESDispatch/EGLDispatch.h
@@ -19,8 +19,8 @@
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
 
-#include <OpenGLESDispatch/egl_functions.h>
-#include <OpenGLESDispatch/egl_extensions_functions.h>
+#include <egl_functions.h>
+#include <egl_extensions_functions.h>
 
 #define EGL_DEFINE_TYPE(return_type, function_name, signature, callargs) \
     typedef return_type(EGLAPIENTRY* function_name##_t) signature;
diff --git a/host/libs/virglrenderer/OpenGLESDispatch/gles_functions.h b/host/libs/virglrenderer/OpenGLESDispatch/gles_functions.h
index d45dda3..1219391 100644
--- a/host/libs/virglrenderer/OpenGLESDispatch/gles_functions.h
+++ b/host/libs/virglrenderer/OpenGLESDispatch/gles_functions.h
@@ -16,12 +16,12 @@
 
 #pragma once
 
-#include "OpenGLESDispatch/gles1_core_functions.h"
-#include "OpenGLESDispatch/gles1_extensions_functions.h"
-#include "OpenGLESDispatch/gles2_core_functions.h"
-#include "OpenGLESDispatch/gles2_extensions_functions.h"
-#include "OpenGLESDispatch/gles3_only_functions.h"
-#include "OpenGLESDispatch/gles31_only_functions.h"
+#include "gles1_core_functions.h"
+#include "gles1_extensions_functions.h"
+#include "gles2_core_functions.h"
+#include "gles2_extensions_functions.h"
+#include "gles3_only_functions.h"
+#include "gles31_only_functions.h"
 
 #define LIST_GLES1_FUNCTIONS(X, Y) \
     LIST_GLES1_CORE_FUNCTIONS(X) \
diff --git a/host/libs/virglrenderer/RenderControl.h b/host/libs/virglrenderer/RenderControl.h
index 146adf4..88734e5 100644
--- a/host/libs/virglrenderer/RenderControl.h
+++ b/host/libs/virglrenderer/RenderControl.h
@@ -16,7 +16,7 @@
 
 #pragma once
 
-#include "renderControl_dec/renderControl_dec.h"
+#include "renderControl_dec.h"
 
 typedef void* EGLDisplay;
 
diff --git a/host/libs/virglrenderer/Resource.h b/host/libs/virglrenderer/Resource.h
index eb12782..9fd462e 100644
--- a/host/libs/virglrenderer/Resource.h
+++ b/host/libs/virglrenderer/Resource.h
@@ -17,7 +17,7 @@
 #pragma once
 
 extern "C" {
-#include <virgl/virglrenderer.h>
+#include <virglrenderer.h>
 }
 
 #include <cstdint>
diff --git a/host/linux_uapi/linux/virtio_gpu.h b/host/linux_uapi/linux/virtio_gpu.h
new file mode 100644
index 0000000..b43671f
--- /dev/null
+++ b/host/linux_uapi/linux/virtio_gpu.h
@@ -0,0 +1,226 @@
+/****************************************************************************
+ ****************************************************************************
+ ***
+ ***   This header was automatically generated from a Linux kernel header
+ ***   of the same name, to make information necessary for userspace to
+ ***   call into the kernel available to libc.  It contains only constants,
+ ***   structures, and macros generated from the original header, and thus,
+ ***   contains no copyrightable information.
+ ***
+ ***   To edit the content of this header, modify the corresponding
+ ***   source file (e.g. under external/kernel-headers/original/) then
+ ***   run bionic/libc/kernel/tools/update_all.py
+ ***
+ ***   Any manual change here will be lost the next time this script will
+ ***   be run. You've been warned!
+ ***
+ ****************************************************************************
+ ****************************************************************************/
+#ifndef VIRTIO_GPU_HW_H
+#define VIRTIO_GPU_HW_H
+#include <linux/types.h>
+#define VIRTIO_GPU_F_VIRGL 0
+enum virtio_gpu_ctrl_type {
+  VIRTIO_GPU_UNDEFINED = 0,
+  VIRTIO_GPU_CMD_GET_DISPLAY_INFO = 0x0100,
+  VIRTIO_GPU_CMD_RESOURCE_CREATE_2D,
+  VIRTIO_GPU_CMD_RESOURCE_UNREF,
+  VIRTIO_GPU_CMD_SET_SCANOUT,
+  VIRTIO_GPU_CMD_RESOURCE_FLUSH,
+  VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D,
+  VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING,
+  VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING,
+  VIRTIO_GPU_CMD_GET_CAPSET_INFO,
+  VIRTIO_GPU_CMD_GET_CAPSET,
+  VIRTIO_GPU_CMD_CTX_CREATE = 0x0200,
+  VIRTIO_GPU_CMD_CTX_DESTROY,
+  VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE,
+  VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE,
+  VIRTIO_GPU_CMD_RESOURCE_CREATE_3D,
+  VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D,
+  VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D,
+  VIRTIO_GPU_CMD_SUBMIT_3D,
+  VIRTIO_GPU_CMD_UPDATE_CURSOR = 0x0300,
+  VIRTIO_GPU_CMD_MOVE_CURSOR,
+  VIRTIO_GPU_RESP_OK_NODATA = 0x1100,
+  VIRTIO_GPU_RESP_OK_DISPLAY_INFO,
+  VIRTIO_GPU_RESP_OK_CAPSET_INFO,
+  VIRTIO_GPU_RESP_OK_CAPSET,
+  VIRTIO_GPU_RESP_ERR_UNSPEC = 0x1200,
+  VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY,
+  VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID,
+  VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID,
+  VIRTIO_GPU_RESP_ERR_INVALID_CONTEXT_ID,
+  VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER,
+};
+#define VIRTIO_GPU_FLAG_FENCE (1 << 0)
+struct virtio_gpu_ctrl_hdr {
+  __le32 type;
+  __le32 flags;
+  __le64 fence_id;
+  __le32 ctx_id;
+  __le32 padding;
+};
+struct virtio_gpu_cursor_pos {
+  __le32 scanout_id;
+  __le32 x;
+  __le32 y;
+  __le32 padding;
+};
+struct virtio_gpu_update_cursor {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_cursor_pos pos;
+  __le32 resource_id;
+  __le32 hot_x;
+  __le32 hot_y;
+  __le32 padding;
+};
+struct virtio_gpu_rect {
+  __le32 x;
+  __le32 y;
+  __le32 width;
+  __le32 height;
+};
+struct virtio_gpu_resource_unref {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 padding;
+};
+struct virtio_gpu_resource_create_2d {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 format;
+  __le32 width;
+  __le32 height;
+};
+struct virtio_gpu_set_scanout {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_rect r;
+  __le32 scanout_id;
+  __le32 resource_id;
+};
+struct virtio_gpu_resource_flush {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_rect r;
+  __le32 resource_id;
+  __le32 padding;
+};
+struct virtio_gpu_transfer_to_host_2d {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_rect r;
+  __le64 offset;
+  __le32 resource_id;
+  __le32 padding;
+};
+struct virtio_gpu_mem_entry {
+  __le64 addr;
+  __le32 length;
+  __le32 padding;
+};
+struct virtio_gpu_resource_attach_backing {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 nr_entries;
+};
+struct virtio_gpu_resource_detach_backing {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 padding;
+};
+#define VIRTIO_GPU_MAX_SCANOUTS 16
+struct virtio_gpu_resp_display_info {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_display_one {
+    struct virtio_gpu_rect r;
+    __le32 enabled;
+    __le32 flags;
+  } pmodes[VIRTIO_GPU_MAX_SCANOUTS];
+};
+struct virtio_gpu_box {
+  __le32 x, y, z;
+  __le32 w, h, d;
+};
+struct virtio_gpu_transfer_host_3d {
+  struct virtio_gpu_ctrl_hdr hdr;
+  struct virtio_gpu_box box;
+  __le64 offset;
+  __le32 resource_id;
+  __le32 level;
+  __le32 stride;
+  __le32 layer_stride;
+};
+#define VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP (1 << 0)
+struct virtio_gpu_resource_create_3d {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 target;
+  __le32 format;
+  __le32 bind;
+  __le32 width;
+  __le32 height;
+  __le32 depth;
+  __le32 array_size;
+  __le32 last_level;
+  __le32 nr_samples;
+  __le32 flags;
+  __le32 padding;
+};
+struct virtio_gpu_ctx_create {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 nlen;
+  __le32 padding;
+  char debug_name[64];
+};
+struct virtio_gpu_ctx_destroy {
+  struct virtio_gpu_ctrl_hdr hdr;
+};
+struct virtio_gpu_ctx_resource {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 resource_id;
+  __le32 padding;
+};
+struct virtio_gpu_cmd_submit {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 size;
+  __le32 padding;
+};
+#define VIRTIO_GPU_CAPSET_VIRGL 1
+struct virtio_gpu_get_capset_info {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 capset_index;
+  __le32 padding;
+};
+struct virtio_gpu_resp_capset_info {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 capset_id;
+  __le32 capset_max_version;
+  __le32 capset_max_size;
+  __le32 padding;
+};
+struct virtio_gpu_get_capset {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __le32 capset_id;
+  __le32 capset_version;
+};
+struct virtio_gpu_resp_capset {
+  struct virtio_gpu_ctrl_hdr hdr;
+  __u8 capset_data[];
+};
+#define VIRTIO_GPU_EVENT_DISPLAY (1 << 0)
+struct virtio_gpu_config {
+  __u32 events_read;
+  __u32 events_clear;
+  __u32 num_scanouts;
+  __u32 num_capsets;
+};
+enum virtio_gpu_formats {
+  VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM = 1,
+  VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM = 2,
+  VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM = 3,
+  VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM = 4,
+  VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM = 67,
+  VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM = 68,
+  VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM = 121,
+  VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM = 134,
+};
+#endif