Make VrWindowManager system service again.

WIP, For now just build a parallel target until controller data comes
from shared memory.

Bug: None
Test: None

Change-Id: I3aa808a4ac6f774f113abadfa76056d350f2a338
diff --git a/services/vr/vr_window_manager/Android.mk_disable b/services/vr/vr_window_manager/Android.mk_disable
index adce4b9..d7d98b3 100644
--- a/services/vr/vr_window_manager/Android.mk_disable
+++ b/services/vr/vr_window_manager/Android.mk_disable
@@ -14,6 +14,19 @@
 
 LOCAL_PATH := $(call my-dir)
 
+native_src := \
+  application.cpp \
+  controller_mesh.cpp \
+  elbow_model.cpp \
+  hwc_callback.cpp \
+  reticle.cpp \
+  render_thread.cpp \
+  shell_view.cpp \
+  surface_flinger_view.cpp \
+  texture.cpp \
+  vr_window_manager.cpp \
+  ../virtual_touchpad/aidl/android/dvr/VirtualTouchpadService.aidl \
+
 src := \
   vr_window_manager_jni.cpp \
   application.cpp \
@@ -80,6 +93,22 @@
 include $(BUILD_SHARED_LIBRARY)
 
 include $(CLEAR_VARS)
+LOCAL_SRC_FILES := $(native_src)
+LOCAL_C_INCLUDES := hardware/qcom/display/msm8996/libgralloc
+LOCAL_STATIC_LIBRARIES := $(static_libs)
+LOCAL_SHARED_LIBRARIES := $(shared_libs)
+LOCAL_SHARED_LIBRARIES += libgvr
+LOCAL_STATIC_LIBRARIES += libgvr_ext
+LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
+LOCAL_CFLAGS += -DEGL_EGLEXT_PROTOTYPES
+LOCAL_CFLAGS += -DLOG_TAG=\"VrWindowManager\"
+LOCAL_LDLIBS := -llog
+LOCAL_MODULE := vr_wm
+LOCAL_MODULE_TAGS := optional
+LOCAL_INIT_RC := vr_wm.rc
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
 LOCAL_PACKAGE_NAME := VrWindowManager
 
 # We need to be priveleged to run as the system user, which is necessary for
diff --git a/services/vr/vr_window_manager/application.cpp b/services/vr/vr_window_manager/application.cpp
index f84a0d1..081de74 100644
--- a/services/vr/vr_window_manager/application.cpp
+++ b/services/vr/vr_window_manager/application.cpp
@@ -100,6 +100,16 @@
   fov_[1] = FieldOfView(lens_info.right_fov[0], lens_info.right_fov[1],
                         lens_info.right_fov[2], lens_info.right_fov[3]);
 
+  if (java_env_) {
+    int ret = InitializeController();
+    if (ret)
+      return ret;
+  }
+
+  return 0;
+}
+
+int Application::InitializeController() {
   gvr_context_ = gvr::GvrApi::Create(java_env_, app_context_, class_loader_);
   if (gvr_context_ == nullptr) {
     ALOGE("Gvr context creation failed");
@@ -170,7 +180,7 @@
   // Thread should block if we are invisible or not fully initialized.
   std::unique_lock<std::mutex> lock(mutex_);
   wake_up_init_and_render_.wait(lock, [this]() {
-    return is_visible_ && initialized_ || !main_thread_tasks_.empty();
+    return (is_visible_ && initialized_) || !main_thread_tasks_.empty();
   });
 
   // Process main thread tasks if there are any.
@@ -245,6 +255,9 @@
 }
 
 void Application::ProcessControllerInput() {
+  if (!controller_)
+    return;
+
   controller_state_->Update(*controller_);
   gvr::ControllerApiStatus new_api_status = controller_state_->GetApiStatus();
   gvr::ControllerConnectionState new_connection_state =
@@ -286,10 +299,12 @@
   if (changed) {
     is_visible_ = visible;
     dvrGraphicsSurfaceSetVisible(graphics_context_, is_visible_);
-    if (is_visible_)
-      controller_->Resume();
-    else
-      controller_->Pause();
+    if (controller_) {
+      if (is_visible_)
+        controller_->Resume();
+      else
+        controller_->Pause();
+    }
     OnVisibilityChanged(is_visible_);
   }
 }
diff --git a/services/vr/vr_window_manager/application.h b/services/vr/vr_window_manager/application.h
index 3321682..47a0927 100644
--- a/services/vr/vr_window_manager/application.h
+++ b/services/vr/vr_window_manager/application.h
@@ -54,6 +54,8 @@
 
   void QueueTask(MainThreadTask task);
 
+  int InitializeController();
+
   DvrGraphicsContext* graphics_context_ = nullptr;
   DvrPose* pose_client_ = nullptr;
 
diff --git a/services/vr/vr_window_manager/shell_view.cpp b/services/vr/vr_window_manager/shell_view.cpp
index b9a4f86..3b18f74 100644
--- a/services/vr/vr_window_manager/shell_view.cpp
+++ b/services/vr/vr_window_manager/shell_view.cpp
@@ -573,7 +573,7 @@
   vec3 pointer_location = last_pose_.GetPosition();
   quat view_quaternion = last_pose_.GetRotation();
 
-  if (controller_api_status_ == gvr::kControllerApiOk) {
+  if (controller_ && controller_api_status_ == gvr::kControllerApiOk) {
     view_quaternion = FromGvrQuatf(controller_orientation_);
     vec4 controller_location = controller_translate_ * vec4(0, 0, 0, 1);
     pointer_location = vec3(controller_location.x(), controller_location.y(),
@@ -618,6 +618,9 @@
 
 void ShellView::DrawController(const mat4& perspective, const mat4& eye_matrix,
                                const mat4& head_matrix) {
+  if (!controller_)
+    return;
+
   controller_program_->Use();
   mat4 mvp = perspective * eye_matrix * head_matrix;
 
diff --git a/services/vr/vr_window_manager/vr_window_manager.cpp b/services/vr/vr_window_manager/vr_window_manager.cpp
new file mode 100644
index 0000000..736a14f
--- /dev/null
+++ b/services/vr/vr_window_manager/vr_window_manager.cpp
@@ -0,0 +1,23 @@
+#include <binder/ProcessState.h>
+
+#include "shell_view.h"
+
+int main(int /* argc */, char** /* argv */) {
+  android::ProcessState::self()->startThreadPool();
+
+  android::dvr::ShellView app;
+  if (app.Initialize(nullptr, nullptr, nullptr)) {
+    ALOGE("Failed to initialize");
+    return 1;
+  }
+
+  if (app.AllocateResources()) {
+    ALOGE("Failed to allocate resources");
+    return 1;
+  }
+
+  while (true)
+    app.DrawFrame();
+
+  return 0;
+}
diff --git a/services/vr/vr_window_manager/vr_wm.rc b/services/vr/vr_window_manager/vr_wm.rc
new file mode 100644
index 0000000..143b916
--- /dev/null
+++ b/services/vr/vr_window_manager/vr_wm.rc
@@ -0,0 +1,9 @@
+service vr_wm /system/bin/vr_wm
+  class core
+  user system
+  group system graphics input
+  cpuset /system
+  disabled
+
+on property:persist.daydream.vr_wm=1
+  enable vr_wm