Use the config to control the ivserver launch.

Bug: 123592422
Test: Ran aosp_cf_x86_phone-userdebug
Change-Id: Ice284f7067b601bb7b12a38318fca8ce42ee29bc
diff --git a/host/commands/launch/Android.bp b/host/commands/launch/Android.bp
index eceb206..e1a6616 100644
--- a/host/commands/launch/Android.bp
+++ b/host/commands/launch/Android.bp
@@ -23,6 +23,7 @@
         "wifi_region_handler.cc",
         "boot_image_unpacker.cc",
         "process_monitor.cc",
+        "launch.cc",
     ],
     header_libs: [
         "cuttlefish_glog",
diff --git a/host/commands/launch/launch.cc b/host/commands/launch/launch.cc
new file mode 100644
index 0000000..d48f91d
--- /dev/null
+++ b/host/commands/launch/launch.cc
@@ -0,0 +1,37 @@
+#include "host/commands/launch/launch.h"
+
+#include "common/libs/fs/shared_fd.h"
+#include "common/libs/utils/size_utils.h"
+#include "common/vsoc/shm/screen_layout.h"
+#include "host/commands/launch/vsoc_shared_memory.h"
+
+cvd::SharedFD CreateIvServerUnixSocket(const std::string& path) {
+  return cvd::SharedFD::SocketLocalServer(path.c_str(), false, SOCK_STREAM,
+                                          0666);
+}
+
+cvd::Command GetIvServerCommand(const vsoc::CuttlefishConfig& config) {
+  // Resize gralloc region
+  auto actual_width = cvd::AlignToPowerOf2(config.x_res() * 4, 4);// align to 16
+  uint32_t screen_buffers_size =
+      config.num_screen_buffers() *
+      cvd::AlignToPageSize(actual_width * config.y_res() + 16 /* padding */);
+  screen_buffers_size +=
+      (config.num_screen_buffers() - 1) * 4096; /* Guard pages */
+
+  // TODO(b/79170615) Resize gralloc region too.
+
+  vsoc::CreateSharedMemoryFile(
+      config.mempath(),
+      {{vsoc::layout::screen::ScreenLayout::region_name, screen_buffers_size}});
+
+
+  cvd::Command ivserver(config.ivserver_binary());
+  ivserver.AddParameter(
+      "-qemu_socket_fd=",
+      CreateIvServerUnixSocket(config.ivshmem_qemu_socket_path()));
+  ivserver.AddParameter(
+      "-client_socket_fd=",
+      CreateIvServerUnixSocket(config.ivshmem_client_socket_path()));
+  return ivserver;
+}
diff --git a/host/commands/launch/launch.h b/host/commands/launch/launch.h
new file mode 100644
index 0000000..ffe79f1
--- /dev/null
+++ b/host/commands/launch/launch.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include "common/libs/utils/subprocess.h"
+#include "host/libs/config/cuttlefish_config.h"
+
+cvd::Command GetIvServerCommand(const vsoc::CuttlefishConfig& config);
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index 6946f2a..de196cb 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -48,6 +48,7 @@
 #include "common/vsoc/lib/vsoc_memory.h"
 #include "common/vsoc/shm/screen_layout.h"
 #include "host/commands/launch/boot_image_unpacker.h"
+#include "host/commands/launch/launch.h"
 #include "host/commands/launch/launcher_defs.h"
 #include "host/commands/launch/pre_launch_initializers.h"
 #include "host/commands/launch/process_monitor.h"
@@ -391,11 +392,6 @@
   }
 }
 
-cvd::SharedFD CreateIvServerUnixSocket(const std::string& path) {
-  return cvd::SharedFD::SocketLocalServer(path.c_str(), false, SOCK_STREAM,
-                                          0666);
-}
-
 bool AdbConnectorEnabled() {
   return FLAGS_run_adb_connector && (AdbTunnelEnabled() || AdbVsockTunnelEnabled());
 }
@@ -557,32 +553,6 @@
   return kernel_log_monitor;
 }
 
-cvd::Command GetIvServerCommand(const vsoc::CuttlefishConfig& config) {
-  // Resize gralloc region
-  auto actual_width = cvd::AlignToPowerOf2(FLAGS_x_res * 4, 4);  // align to 16
-  uint32_t screen_buffers_size =
-      FLAGS_num_screen_buffers *
-      cvd::AlignToPageSize(actual_width * FLAGS_y_res + 16 /* padding */);
-  screen_buffers_size +=
-      (FLAGS_num_screen_buffers - 1) * 4096; /* Guard pages */
-
-  // TODO(b/79170615) Resize gralloc region too.
-
-  vsoc::CreateSharedMemoryFile(
-      config.mempath(),
-      {{vsoc::layout::screen::ScreenLayout::region_name, screen_buffers_size}});
-
-
-  cvd::Command ivserver(FLAGS_ivserver_binary);
-  ivserver.AddParameter(
-      "-qemu_socket_fd=",
-      CreateIvServerUnixSocket(config.ivshmem_qemu_socket_path()));
-  ivserver.AddParameter(
-      "-client_socket_fd=",
-      CreateIvServerUnixSocket(config.ivshmem_client_socket_path()));
-  return ivserver;
-}
-
 void LaunchAdbConnectorIfEnabled(cvd::ProcessMonitor* process_monitor) {
   if (AdbConnectorEnabled()) {
     cvd::Command adb_connector(FLAGS_adb_connector_binary);
@@ -735,6 +705,7 @@
   tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
   tmp_config_obj.set_x_res(FLAGS_x_res);
   tmp_config_obj.set_y_res(FLAGS_y_res);
+  tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
   tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
   tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
   tmp_config_obj.set_adb_mode(FLAGS_adb_mode);
@@ -855,6 +826,7 @@
   tmp_config_obj.set_disable_app_armor_security(FLAGS_disable_app_armor_security);
 
   tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
+  tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
   tmp_config_obj.set_hypervisor_uri(FLAGS_hypervisor_uri);
   tmp_config_obj.set_log_xml(FLAGS_log_xml);
 
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 771093c..c802fe3 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -74,6 +74,7 @@
 const char* kDpi = "dpi";
 const char* kXRes = "x_res";
 const char* kYRes = "y_res";
+const char* kNumScreenBuffers = "num_screen_buffers";
 const char* kRefreshRateHz = "refresh_rate_hz";
 
 const char* kKernelImagePath = "kernel_image_path";
@@ -122,6 +123,7 @@
 const char* kLogXml = "log_xml";
 const char* kHypervisorUri = "hypervisor_uri";
 const char* kQemuBinary = "qemu_binary";
+const char* kIvServerBinary = "ivserver_binary";
 }  // namespace
 
 namespace vsoc {
@@ -166,6 +168,13 @@
 int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
 void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
 
+int CuttlefishConfig::num_screen_buffers() const {
+  return (*dictionary_)[kNumScreenBuffers].asInt();
+}
+void CuttlefishConfig::set_num_screen_buffers(int num_screen_buffers) {
+  (*dictionary_)[kNumScreenBuffers] = num_screen_buffers;
+}
+
 int CuttlefishConfig::refresh_rate_hz() const {
   return (*dictionary_)[kRefreshRateHz].asInt();
 }
@@ -541,6 +550,14 @@
   (*dictionary_)[kQemuBinary] = qemu_binary;
 }
 
+std::string CuttlefishConfig::ivserver_binary() const {
+  return (*dictionary_)[kIvServerBinary].asString();
+}
+
+void CuttlefishConfig::set_ivserver_binary(const std::string& ivserver_binary) {
+  (*dictionary_)[kIvServerBinary] = ivserver_binary;
+}
+
 // Creates the (initially empty) config object and populates it with values from
 // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
 // Returns nullptr if there was an error loading from file
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index 0ecc9be..9a68ca4 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -75,6 +75,9 @@
   int y_res() const;
   void set_y_res(int y_res);
 
+  int num_screen_buffers() const;
+  void set_num_screen_buffers(int num_screen_buffers);
+
   int refresh_rate_hz() const;
   void set_refresh_rate_hz(int refresh_rate_hz);
 
@@ -211,6 +214,9 @@
   void set_qemu_binary(const std::string& qemu_binary);
   std::string qemu_binary() const;
 
+  void set_ivserver_binary(const std::string& ivserver_binary);
+  std::string ivserver_binary() const;
+
  private:
   std::unique_ptr<Json::Value> dictionary_;