Use Meyers singleton for Display and Touchscreen

This type of singleton guarantees that the objects are initialized when
their getters are called.

Change-Id: I8600cf47732ad5d969a47132294a269fd5a97983
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/kudzu/+/176410
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
diff --git a/applications/app_common_impl/common_host_imgui.cc b/applications/app_common_impl/common_host_imgui.cc
index 994c531..35e9b8d 100644
--- a/applications/app_common_impl/common_host_imgui.cc
+++ b/applications/app_common_impl/common_host_imgui.cc
@@ -51,8 +51,6 @@
     .pixel_format = PixelFormat::RGB565,
 });
 pw::display_driver::DisplayDriverImgUI s_display_driver;
-pw::display::DisplayImgUI s_display(s_display_driver, kDisplaySize, s_fb_pool);
-Touchscreen s_touchscreen = Touchscreen(s_display_driver);
 
 }  // namespace
 
@@ -62,9 +60,16 @@
 Status Common::Init() { return s_display_driver.Init(); }
 
 // static
-pw::display::Display& Common::GetDisplay() { return s_display; }
+pw::display::Display& Common::GetDisplay() {
+  static pw::display::DisplayImgUI s_display(
+      s_display_driver, kDisplaySize, s_fb_pool);
+  return s_display;
+}
 
-pw::touchscreen::Touchscreen& Common::GetTouchscreen() { return s_touchscreen; }
+pw::touchscreen::Touchscreen& Common::GetTouchscreen() {
+  static Touchscreen s_touchscreen = Touchscreen(s_display_driver);
+  return s_touchscreen;
+}
 
 const pw::thread::Options& Common::DisplayDrawThreadOptions() {
   static pw::thread::stl::Options display_draw_thread_options;
diff --git a/applications/app_common_impl/common_host_null.cc b/applications/app_common_impl/common_host_null.cc
index a44fd3c..1a05e4b 100644
--- a/applications/app_common_impl/common_host_null.cc
+++ b/applications/app_common_impl/common_host_null.cc
@@ -36,9 +36,6 @@
     .row_bytes = 0,
     .pixel_format = PixelFormat::None,
 });
-pw::display::Display s_display(s_display_driver, kDisplaySize, s_fb_pool);
-pw::touchscreen::TouchscreenNull s_touchscreen =
-    pw::touchscreen::TouchscreenNull();
 
 }  // namespace
 
@@ -46,9 +43,17 @@
 Status Common::Init() { return s_display_driver.Init(); }
 
 // static
-pw::display::Display& Common::GetDisplay() { return s_display; }
+pw::display::Display& Common::GetDisplay() {
+  static pw::display::Display s_display(
+      s_display_driver, kDisplaySize, s_fb_pool);
+  return s_display;
+}
 
-pw::touchscreen::Touchscreen& Common::GetTouchscreen() { return s_touchscreen; }
+pw::touchscreen::Touchscreen& Common::GetTouchscreen() {
+  static pw::touchscreen::TouchscreenNull s_touchscreen =
+      pw::touchscreen::TouchscreenNull();
+  return s_touchscreen;
+}
 
 const pw::thread::Options& Common::DisplayDrawThreadOptions() {
   static pw::thread::stl::Options display_draw_thread_options;
diff --git a/applications/app_common_impl/common_pico.cc b/applications/app_common_impl/common_pico.cc
index 03551a1..acc184d 100644
--- a/applications/app_common_impl/common_pico.cc
+++ b/applications/app_common_impl/common_pico.cc
@@ -155,25 +155,24 @@
     .pixel_format = PixelFormat::RGB565,
 });
 DisplayDriver s_display_driver({
-  .data_cmd_gpio = s_display_dc_pin.as<pw::digital_io::DigitalOut>(),
-  .spi_cs_gpio = s_display_cs_pin.as<pw::digital_io::DigitalOut>(),
+    .data_cmd_gpio = s_display_dc_pin.as<pw::digital_io::DigitalOut>(),
+    .spi_cs_gpio = s_display_cs_pin.as<pw::digital_io::DigitalOut>(),
 #if DISPLAY_RESET_GPIO != -1
-  .reset_gpio = &s_display_reset_pin.as<pw::digital_io::DigitalOut>(),
+    .reset_gpio = &s_display_reset_pin.as<pw::digital_io::DigitalOut>(),
 #else
-  .reset_gpio = nullptr,
+    .reset_gpio = nullptr,
 #endif
 #if DISPLAY_TE_GPIO != -1
-  .tear_effect_gpio = &s_display_tear_effect_pin,
+    .tear_effect_gpio = &s_display_tear_effect_pin,
 #else
-  .tear_effect_gpio = nullptr,
+    .tear_effect_gpio = nullptr,
 #endif
-  .spi_device_8_bit = s_spi_8_bit.device,
-  .spi_device_16_bit = s_spi_16_bit.device,
+    .spi_device_8_bit = s_spi_8_bit.device,
+    .spi_device_16_bit = s_spi_16_bit.device,
 #if USE_PIO
-  .pixel_pusher = &s_pixel_pusher,
+    .pixel_pusher = &s_pixel_pusher,
 #endif
 });
-Display s_display(s_display_driver, kDisplaySize, s_fb_pool);
 
 #if BACKLIGHT_GPIO != -1
 void SetBacklight(uint16_t brightness) {
@@ -236,7 +235,6 @@
 pw::icm42670p::Device imu(i2c0_bus);
 pw::max17948::Device fuel_guage(i2c0_bus);
 pw::ft6236::Device touch_screen_controller(i2c0_bus);
-Touchscreen s_touchscreen = Touchscreen(&touch_screen_controller);
 
 static constexpr size_t kDisplayDrawThreadStackWords = 512;
 static pw::thread::freertos::StaticContextWithStack<
@@ -343,9 +341,15 @@
 }
 
 // static
-pw::display::Display& Common::GetDisplay() { return s_display; }
+pw::display::Display& Common::GetDisplay() {
+  static Display s_display(s_display_driver, kDisplaySize, s_fb_pool);
+  return s_display;
+}
 
-pw::touchscreen::Touchscreen& Common::GetTouchscreen() { return s_touchscreen; }
+pw::touchscreen::Touchscreen& Common::GetTouchscreen() {
+  static Touchscreen s_touchscreen = Touchscreen(&touch_screen_controller);
+  return s_touchscreen;
+}
 
 const pw::thread::Options& Common::DisplayDrawThreadOptions() {
   static constexpr auto options =