Passing remaining framebuffers by reference.

Eliminate all raw pointers to framebuffers.

Change-Id: I74adee1ec44d3590de4d4fa3bcb916bcc5591919
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/experimental/+/126431
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Chris Mumford <cmumford@google.com>
diff --git a/applications/terminal_display/main.cc b/applications/terminal_display/main.cc
index 3362873..ee6b2d8 100644
--- a/applications/terminal_display/main.cc
+++ b/applications/terminal_display/main.cc
@@ -145,7 +145,7 @@
 void DrawButton(const Button& button,
                 color_rgb565_t bg_color,
                 FramebufferRgb565& framebuffer) {
-  pw::draw::DrawRectWH(&framebuffer,
+  pw::draw::DrawRectWH(framebuffer,
                        button.tl_.x,
                        button.tl_.y,
                        button.size_.width,
@@ -233,7 +233,7 @@
 
   // Draw the dark blue border
   pw::draw::DrawRectWH(
-      &framebuffer,
+      framebuffer,
       sprite_pos_x - border_size,
       sprite_pos_y - border_size,
       pigweed_farm_sprite_sheet.width * sprite_scale + (border_size * 2),
@@ -246,7 +246,7 @@
 
   // Draw the light blue background
   pw::draw::DrawRectWH(
-      &framebuffer,
+      framebuffer,
       sprite_pos_x - border_size,
       sprite_pos_y - border_size,
       pigweed_farm_sprite_sheet.width * sprite_scale + (border_size * 2),
@@ -268,7 +268,7 @@
     motion_dir = -1;
 
   // Draw the Sun
-  pw::draw::DrawCircle(&framebuffer,
+  pw::draw::DrawCircle(framebuffer,
                        sun_offset.x + sprite_pos_x +
                            (pigweed_farm_sprite_sheet.width * sprite_scale) -
                            32,
@@ -276,7 +276,7 @@
                        20,
                        colors_pico8_rgb565[COLOR_ORANGE],
                        true);
-  pw::draw::DrawCircle(&framebuffer,
+  pw::draw::DrawCircle(framebuffer,
                        sun_offset.x + sprite_pos_x +
                            (pigweed_farm_sprite_sheet.width * sprite_scale) -
                            32,
@@ -287,7 +287,7 @@
 
   // Draw the farm sprite's shadow
   pigweed_farm_sprite_sheet.current_index = 1;
-  pw::draw::DrawSprite(&framebuffer,
+  pw::draw::DrawSprite(framebuffer,
                        sprite_pos_x + 2,
                        sprite_pos_y + 2,
                        &pigweed_farm_sprite_sheet,
@@ -296,7 +296,7 @@
   // Draw the farm sprite
   pigweed_farm_sprite_sheet.current_index = 0;
   pw::draw::DrawSprite(
-      &framebuffer, sprite_pos_x, sprite_pos_y, &pigweed_farm_sprite_sheet, 4);
+      framebuffer, sprite_pos_x, sprite_pos_y, &pigweed_farm_sprite_sheet, 4);
 
   return 76;
 }
@@ -485,7 +485,7 @@
   FramebufferRgb565 framebuffer = display.GetFramebuffer();
   PW_ASSERT(framebuffer.IsValid());
 
-  pw::draw::Fill(&framebuffer, kBlack);
+  pw::draw::Fill(framebuffer, kBlack);
 
   PW_LOG_INFO("pw::touchscreen::Init()");
   pw::touchscreen::Init();
@@ -524,7 +524,7 @@
     framebuffer = display.GetFramebuffer();
     if (!framebuffer.IsValid())
       continue;
-    pw::draw::Fill(&framebuffer, kBlack);
+    pw::draw::Fill(framebuffer, kBlack);
     DrawFrame(framebuffer, fps_view);
     uint32_t end = pw::spin_delay::Millis();
     uint32_t time = end - start;
diff --git a/pw_graphics/pw_draw/draw_test.cc b/pw_graphics/pw_draw/draw_test.cc
index 03222ba..2c9076e 100644
--- a/pw_graphics/pw_draw/draw_test.cc
+++ b/pw_graphics/pw_draw/draw_test.cc
@@ -32,18 +32,18 @@
 
 constexpr color_rgb565_t kBlack = 0x0;
 
-void PrintFramebufferAsANSI(FramebufferRgb565* fb) {
+void PrintFramebufferAsANSI(const FramebufferRgb565& fb) {
   pw::StringBuffer<4096> line;
   pw::StringBuffer<128> color_string;
 
-  for (int y = 0; y < fb->GetHeight(); y += 2) {
+  for (int y = 0; y < fb.GetHeight(); y += 2) {
     line.clear();
 
-    for (int x = 0; x < fb->GetWidth(); x++) {
+    for (int x = 0; x < fb.GetWidth(); x++) {
       color_string.clear();
-      auto row1_color = fb->GetPixel(x, y);
+      auto row1_color = fb.GetPixel(x, y);
       pw::color::ColorRGBA row1(row1_color.ok() ? row1_color.value() : kBlack);
-      auto row2_color = fb->GetPixel(x, y + 1);
+      auto row2_color = fb.GetPixel(x, y + 1);
       if (!row2_color.ok()) {
         color_string.Format("[38;2;%d;%d;%dmâ–€", row1.r, row1.g, row1.b);
       } else {
@@ -72,8 +72,8 @@
 
   fb.Fill(0);
 
-  DrawLine(&fb, 0, 0, fb.GetWidth(), fb.GetHeight(), indigo);
-  PrintFramebufferAsANSI(&fb);
+  DrawLine(fb, 0, 0, fb.GetWidth(), fb.GetHeight(), indigo);
+  PrintFramebufferAsANSI(fb);
 
   // Check the diagonal is set to indigo, everything else should be 0.
   Result<color_rgb565_t> c;
@@ -97,8 +97,8 @@
   fb.Fill(0);
 
   // Horizonal line at y = 0
-  DrawHLine(&fb, 0, fb.GetWidth(), 0, indigo);
-  PrintFramebufferAsANSI(&fb);
+  DrawHLine(fb, 0, fb.GetWidth(), 0, indigo);
+  PrintFramebufferAsANSI(fb);
 
   // Check color at y = 0 is indigo, y = 1 should be 0.
   Result<color_rgb565_t> c;
@@ -119,8 +119,8 @@
   fb.Fill(0);
 
   // 4x4 rectangle, not filled
-  DrawRect(&fb, 0, 0, 3, 3, indigo, false);
-  PrintFramebufferAsANSI(&fb);
+  DrawRect(fb, 0, 0, 3, 3, indigo, false);
+  PrintFramebufferAsANSI(fb);
 
   Result<color_rgb565_t> c;
   c = fb.GetPixel(0, 0);
@@ -211,8 +211,8 @@
   fb.Fill(0);
 
   // 4x4 rectangle, filled
-  DrawRect(&fb, 0, 0, 3, 3, indigo, true);
-  PrintFramebufferAsANSI(&fb);
+  DrawRect(fb, 0, 0, 3, 3, indigo, true);
+  PrintFramebufferAsANSI(fb);
 
   Result<color_rgb565_t> c;
   c = fb.GetPixel(0, 0);
@@ -298,8 +298,8 @@
   fb.Fill(0);
 
   // 4x4 rectangle, not filled
-  DrawRectWH(&fb, 0, 0, 4, 4, indigo, false);
-  PrintFramebufferAsANSI(&fb);
+  DrawRectWH(fb, 0, 0, 4, 4, indigo, false);
+  PrintFramebufferAsANSI(fb);
 
   Result<color_rgb565_t> c;
   c = fb.GetPixel(0, 0);
@@ -389,9 +389,9 @@
   color_rgb565_t indigo = color::colors_pico8_rgb565[12];
   fb.Fill(0);
 
-  DrawCircle(&fb, 3, 3, 3, indigo, false);
+  DrawCircle(fb, 3, 3, 3, indigo, false);
 
-  PrintFramebufferAsANSI(&fb);
+  PrintFramebufferAsANSI(fb);
 
   // ..xxx..
   // .x...x.
@@ -562,12 +562,12 @@
   FramebufferRgb565 fb(data, 5 * 6, 3 * 8, (5 * 6) * sizeof(data[0]));
   fb.Fill(0);
 
-  pw::draw::TextArea text_area(&fb, &font6x8);
+  pw::draw::TextArea text_area(fb, &font6x8);
   text_area.SetForegroundColor(color::colors_pico8_rgb565[COLOR_PINK]);
   text_area.SetBackgroundColor(0);
   text_area.DrawText("Hell\noT\nhere.\nWorld");
 
-  PrintFramebufferAsANSI(&fb);
+  PrintFramebufferAsANSI(fb);
 }
 
 }  // namespace
diff --git a/pw_graphics/pw_draw/public/pw_draw/draw.cc b/pw_graphics/pw_draw/public/pw_draw/draw.cc
index b64c8d9..4d5ff9b 100644
--- a/pw_graphics/pw_draw/public/pw_draw/draw.cc
+++ b/pw_graphics/pw_draw/public/pw_draw/draw.cc
@@ -44,7 +44,7 @@
 
 }  // namespace
 
-void DrawLine(FramebufferRgb565* fb,
+void DrawLine(FramebufferRgb565& fb,
               int x1,
               int y1,
               int x2,
@@ -78,9 +78,9 @@
 
   for (; x1 <= x2; x1++) {
     if (steep_gradient) {
-      fb->SetPixel(y1, x1, pen_color);
+      fb.SetPixel(y1, x1, pen_color);
     } else {
-      fb->SetPixel(x1, y1, pen_color);
+      fb.SetPixel(x1, y1, pen_color);
     }
     error_value -= dy;
     if (error_value < 0) {
@@ -92,7 +92,7 @@
 
 // Draw a circle at center_x, center_y with given radius and color. Only a
 // one-pixel outline is drawn if filled is false.
-void DrawCircle(FramebufferRgb565* fb,
+void DrawCircle(FramebufferRgb565& fb,
                 int center_x,
                 int center_y,
                 int radius,
@@ -109,15 +109,15 @@
     // Draw each quarter circle
     for (int i = x; i <= fx; i++) {
       // Lower right
-      fb->SetPixel(center_x - i, center_y + y, pen_color);
+      fb.SetPixel(center_x - i, center_y + y, pen_color);
       // Upper left
-      fb->SetPixel(center_x + i, center_y - y, pen_color);
+      fb.SetPixel(center_x + i, center_y - y, pen_color);
     }
     for (int i = fy; i <= y; i++) {
       // Lower left
-      fb->SetPixel(center_x - i, center_y - x, pen_color);
+      fb.SetPixel(center_x - i, center_y - x, pen_color);
       // Upper right
-      fb->SetPixel(center_x + i, center_y + x, pen_color);
+      fb.SetPixel(center_x + i, center_y + x, pen_color);
     }
     radius = error_value;
     if (radius <= y) {
@@ -132,13 +132,13 @@
 }
 
 void DrawHLine(
-    FramebufferRgb565* fb, int x1, int x2, int y, color_rgb565_t pen_color) {
+    FramebufferRgb565& fb, int x1, int x2, int y, color_rgb565_t pen_color) {
   for (int i = x1; i <= x2; i++) {
-    fb->SetPixel(i, y, pen_color);
+    fb.SetPixel(i, y, pen_color);
   }
 }
 
-void DrawRect(FramebufferRgb565* fb,
+void DrawRect(FramebufferRgb565& fb,
               int x1,
               int y1,
               int x2,
@@ -154,13 +154,13 @@
     }
   } else {
     for (int y = y1 + 1; y < y2; y++) {
-      fb->SetPixel(x1, y, pen_color);
-      fb->SetPixel(x2, y, pen_color);
+      fb.SetPixel(x1, y, pen_color);
+      fb.SetPixel(x2, y, pen_color);
     }
   }
 }
 
-void DrawRectWH(FramebufferRgb565* fb,
+void DrawRectWH(FramebufferRgb565& fb,
                 int x,
                 int y,
                 int w,
@@ -170,11 +170,11 @@
   DrawRect(fb, x, y, x - 1 + w, y - 1 + h, pen_color, filled);
 }
 
-void Fill(FramebufferRgb565* fb, color_rgb565_t pen_color) {
-  fb->Fill(pen_color);
+void Fill(FramebufferRgb565& fb, color_rgb565_t pen_color) {
+  fb.Fill(pen_color);
 }
 
-void DrawSprite(FramebufferRgb565* fb,
+void DrawSprite(FramebufferRgb565& fb,
                 int x,
                 int y,
                 pw::draw::SpriteSheet* sprite_sheet,
@@ -187,7 +187,7 @@
           current_x, current_y, sprite_sheet->current_index);
       if (color != sprite_sheet->transparent_color) {
         if (integer_scale == 1) {
-          fb->SetPixel(x + current_x, y + current_y, color);
+          fb.SetPixel(x + current_x, y + current_y, color);
         }
         // If integer_scale > 1: draw a rectangle
         else if (integer_scale > 1) {
@@ -201,13 +201,13 @@
   }
 }
 
-void DrawTestPattern(FramebufferRgb565* fb) {
+void DrawTestPattern(FramebufferRgb565& fb) {
   color_rgb565_t color = pw::color::ColorRGBA(0x00, 0xFF, 0xFF).ToRgb565();
   // Create a Test Pattern
-  for (int x = 0; x < fb->GetWidth(); x++) {
-    for (int y = 0; y < fb->GetHeight(); y++) {
+  for (int x = 0; x < fb.GetWidth(); x++) {
+    for (int y = 0; y < fb.GetHeight(); y++) {
       if (y % 10 != x % 10) {
-        fb->SetPixel(x, y, color);
+        fb.SetPixel(x, y, color);
       }
     }
   }
diff --git a/pw_graphics/pw_draw/public/pw_draw/draw.h b/pw_graphics/pw_draw/public/pw_draw/draw.h
index 23b34ea..5f8fc23 100644
--- a/pw_graphics/pw_draw/public/pw_draw/draw.h
+++ b/pw_graphics/pw_draw/public/pw_draw/draw.h
@@ -23,7 +23,7 @@
 
 namespace pw::draw {
 
-void DrawLine(pw::framebuffer::FramebufferRgb565* fb,
+void DrawLine(pw::framebuffer::FramebufferRgb565& fb,
               int x1,
               int y1,
               int x2,
@@ -32,20 +32,20 @@
 
 // Draw a circle at center_x, center_y with given radius and color. Only a
 // one-pixel outline is drawn if filled is false.
-void DrawCircle(pw::framebuffer::FramebufferRgb565* fb,
+void DrawCircle(pw::framebuffer::FramebufferRgb565& fb,
                 int center_x,
                 int center_y,
                 int radius,
                 pw::color::color_rgb565_t pen_color,
                 bool filled);
 
-void DrawHLine(pw::framebuffer::FramebufferRgb565* fb,
+void DrawHLine(pw::framebuffer::FramebufferRgb565& fb,
                int x1,
                int x2,
                int y,
                pw::color::color_rgb565_t pen_color);
 
-void DrawRect(pw::framebuffer::FramebufferRgb565* fb,
+void DrawRect(pw::framebuffer::FramebufferRgb565& fb,
               int x1,
               int y1,
               int x2,
@@ -53,7 +53,7 @@
               pw::color::color_rgb565_t pen_color,
               bool filled);
 
-void DrawRectWH(pw::framebuffer::FramebufferRgb565* fb,
+void DrawRectWH(pw::framebuffer::FramebufferRgb565& fb,
                 int x,
                 int y,
                 int w,
@@ -61,10 +61,10 @@
                 pw::color::color_rgb565_t pen_color,
                 bool filled);
 
-void Fill(pw::framebuffer::FramebufferRgb565* fb,
+void Fill(pw::framebuffer::FramebufferRgb565& fb,
           pw::color::color_rgb565_t pen_color);
 
-void DrawSprite(pw::framebuffer::FramebufferRgb565* fb,
+void DrawSprite(pw::framebuffer::FramebufferRgb565& fb,
                 int x,
                 int y,
                 pw::draw::SpriteSheet* sprite_sheet,
diff --git a/pw_graphics/pw_draw/public/pw_draw/text_area.cc b/pw_graphics/pw_draw/public/pw_draw/text_area.cc
index 241a316..5851f35 100644
--- a/pw_graphics/pw_draw/public/pw_draw/text_area.cc
+++ b/pw_graphics/pw_draw/public/pw_draw/text_area.cc
@@ -24,9 +24,8 @@
 
 namespace pw::draw {
 
-TextArea::TextArea(pw::framebuffer::FramebufferRgb565* fb,
-                   const FontSet* font) {
-  framebuffer = fb;
+TextArea::TextArea(pw::framebuffer::FramebufferRgb565& fb, const FontSet* font)
+    : framebuffer(fb) {
   SetFont(font);
   // Default colors: White on Black
   character_wrap_enabled = true;
@@ -66,7 +65,7 @@
   cursor_x = cursor_x - (column_count * current_font->width);
   column_count = 0;
 
-  if (cursor_y >= framebuffer->GetHeight()) {
+  if (cursor_y >= framebuffer.GetHeight()) {
     ScrollUp(1);
     cursor_y = cursor_y - current_font->height;
   }
@@ -86,7 +85,7 @@
   }
 
   if (character_wrap_enabled &&
-      (current_font->width + cursor_x) > framebuffer->GetWidth()) {
+      (current_font->width + cursor_x) > framebuffer.GetWidth()) {
     InsertLineBreak();
   }
 
@@ -95,7 +94,7 @@
                           foreground_color,
                           background_color,
                           *current_font,
-                          *framebuffer);
+                          framebuffer);
 
   // Move cursor to the right by 1 glyph.
   MoveCursorRightOnce();
@@ -148,12 +147,12 @@
   int start_x = 0;
   int start_y = pixel_height;
 
-  for (int current_x = 0; current_x < framebuffer->GetWidth(); current_x++) {
-    for (int current_y = start_y; current_y < framebuffer->GetHeight();
+  for (int current_x = 0; current_x < framebuffer.GetWidth(); current_x++) {
+    for (int current_y = start_y; current_y < framebuffer.GetHeight();
          current_y++) {
-      if (auto pixel_color = framebuffer->GetPixel(current_x, current_y);
+      if (auto pixel_color = framebuffer.GetPixel(current_x, current_y);
           pixel_color.ok()) {
-        framebuffer->SetPixel(
+        framebuffer.SetPixel(
             start_x + current_x, current_y - start_y, *pixel_color);
       }
     }
@@ -161,11 +160,11 @@
 
   // Draw a filled background_color rectangle at the bottom to erase the old
   // text.
-  for (int x = 0; x < framebuffer->GetWidth(); x++) {
-    for (int y = framebuffer->GetHeight() - pixel_height;
-         y < framebuffer->GetHeight();
+  for (int x = 0; x < framebuffer.GetWidth(); x++) {
+    for (int y = framebuffer.GetHeight() - pixel_height;
+         y < framebuffer.GetHeight();
          y++) {
-      framebuffer->SetPixel(x, y, background_color);
+      framebuffer.SetPixel(x, y, background_color);
     }
   }
 }
diff --git a/pw_graphics/pw_draw/public/pw_draw/text_area.h b/pw_graphics/pw_draw/public/pw_draw/text_area.h
index 1125cee..4e67bc8 100644
--- a/pw_graphics/pw_draw/public/pw_draw/text_area.h
+++ b/pw_graphics/pw_draw/public/pw_draw/text_area.h
@@ -28,9 +28,9 @@
   const FontSet* current_font;
   pw::color::color_rgb565_t foreground_color;
   pw::color::color_rgb565_t background_color;
-  pw::framebuffer::FramebufferRgb565* framebuffer;
+  pw::framebuffer::FramebufferRgb565& framebuffer;
 
-  TextArea(pw::framebuffer::FramebufferRgb565* fb, const FontSet* font);
+  TextArea(pw::framebuffer::FramebufferRgb565& fb, const FontSet* font);
 
   // Change the current font.
   void SetFont(const FontSet* new_font);
diff --git a/pw_graphics/pw_framebuffer/framebuffer_test.cc b/pw_graphics/pw_framebuffer/framebuffer_test.cc
index 11e0c75..178cfcd 100644
--- a/pw_graphics/pw_framebuffer/framebuffer_test.cc
+++ b/pw_graphics/pw_framebuffer/framebuffer_test.cc
@@ -91,9 +91,9 @@
   fb2.Fill(orange);
 
   // Do the blits
-  fb.Blit(&fb2, -3, -3);
+  fb.Blit(fb2, -3, -3);
 
-  fb.Blit(&fb2, 2, 2);
+  fb.Blit(fb2, 2, 2);
 
   // TODO(tonymd): Include PrintFramebufferAsANSI from draw_test.cc
   // PrintFramebufferAsANSI(&fb);
diff --git a/pw_graphics/pw_framebuffer/public/pw_framebuffer/rgb565.h b/pw_graphics/pw_framebuffer/public/pw_framebuffer/rgb565.h
index 63e4fdc..1954cff 100644
--- a/pw_graphics/pw_framebuffer/public/pw_framebuffer/rgb565.h
+++ b/pw_graphics/pw_framebuffer/public/pw_framebuffer/rgb565.h
@@ -50,7 +50,7 @@
   void SetPixel(int x, int y, pw::color::color_rgb565_t rgb565_color);
 
   // Copy the colors from another framebuffer into this one at position x, y.
-  void Blit(FramebufferRgb565* fb, int x, int y);
+  void Blit(const FramebufferRgb565& fb, int x, int y);
 
   // Fill the entire buffer with a color.
   void Fill(pw::color::color_rgb565_t color);
diff --git a/pw_graphics/pw_framebuffer/rgb565.cc b/pw_graphics/pw_framebuffer/rgb565.cc
index ad2c16d..ec93b39 100644
--- a/pw_graphics/pw_framebuffer/rgb565.cc
+++ b/pw_graphics/pw_framebuffer/rgb565.cc
@@ -68,11 +68,11 @@
 }
 
 // Copy the colors from another framebuffer into this one at position x, y.
-void FramebufferRgb565::Blit(FramebufferRgb565* fb, int x, int y) {
-  PW_ASSERT(fb->IsValid());
-  for (int current_x = 0; current_x < fb->width_; current_x++) {
-    for (int current_y = 0; current_y < fb->height_; current_y++) {
-      if (auto pixel_color = fb->GetPixel(current_x, current_y);
+void FramebufferRgb565::Blit(const FramebufferRgb565& fb, int x, int y) {
+  PW_ASSERT(fb.IsValid());
+  for (int current_x = 0; current_x < fb.width_; current_x++) {
+    for (int current_y = 0; current_y < fb.height_; current_y++) {
+      if (auto pixel_color = fb.GetPixel(current_x, current_y);
           pixel_color.ok()) {
         SetPixel(x + current_x, y + current_y, pixel_color.value());
       }