Clean up the logic for rescaling the trace

Remove the copy of GPU vertex buffer, and fix numerical instability in
IsInside() check

PiperOrigin-RevId: 211878102
diff --git a/tools/render/geometry_util.h b/tools/render/geometry_util.h
index 4c56afc..2edaaac 100644
--- a/tools/render/geometry_util.h
+++ b/tools/render/geometry_util.h
@@ -117,7 +117,10 @@
 }
 
 inline bool IsInside(const Box& inner, const Box& outer) {
-  return IntersectBoxes(inner, outer) == inner;
+  const vec2 corner_inner = inner.origin + inner.size;
+  const vec2 corner_outer = outer.origin + outer.size;
+  return inner.origin.x >= outer.origin.x && inner.origin.y >= outer.origin.y &&
+         corner_inner.x <= corner_outer.x && corner_inner.y <= corner_outer.y;
 }
 
 inline bool IsInside(vec2 point, const Box& box) {
diff --git a/tools/render/processed_trace.cc b/tools/render/processed_trace.cc
index 5a557cb..c96d057 100644
--- a/tools/render/processed_trace.cc
+++ b/tools/render/processed_trace.cc
@@ -327,5 +327,29 @@
   }
 }
 
+absl::optional<Box> ProcessedTrace::BoundContainedPackets(Box boundary) {
+  RenderedPacket key{Box(), nullptr};
+
+  key.box.origin.x = boundary.origin.x;
+  auto range_start = absl::c_lower_bound(rendered_packets_, key);
+
+  key.box.origin.x =
+      boundary.origin.x + boundary.size.x + kSentPacketDurationMs;
+  auto range_end = absl::c_upper_bound(rendered_packets_, key);
+
+  if (range_start == rendered_packets_.end() ||
+      range_end == rendered_packets_.end() || range_start > range_end) {
+    return absl::nullopt;
+  }
+
+  absl::optional<Box> result;
+  for (auto it = range_start; it <= range_end; it++) {
+    if (IsInside(it->box, boundary)) {
+      result = result.has_value() ? BoundingBox(*result, it->box) : it->box;
+    }
+  }
+  return result;
+}
+
 }  // namespace render
 }  // namespace quic_trace
diff --git a/tools/render/processed_trace.h b/tools/render/processed_trace.h
index 86791df..f772e7c 100644
--- a/tools/render/processed_trace.h
+++ b/tools/render/processed_trace.h
@@ -53,6 +53,10 @@
                           const Box* as_rendered,
                           const Event* packet);
 
+  // For specified section of the graph, find a bounding box that contains all
+  // of the packets in it, or return nullopt if none are contained.
+  absl::optional<Box> BoundContainedPackets(Box boundary);
+
  private:
   struct RenderedPacket {
     Box box;
diff --git a/tools/render/sdl_util.h b/tools/render/sdl_util.h
index 1940869..f5380f8 100644
--- a/tools/render/sdl_util.h
+++ b/tools/render/sdl_util.h
@@ -208,18 +208,6 @@
   GLuint texture_;
 };
 
-class GlBufferMapCleanup {
- public:
-  GlBufferMapCleanup(GLenum buffer) : buffer_(buffer) {}
-  ~GlBufferMapCleanup() { glUnmapBuffer(buffer_); }
-
-  GlBufferMapCleanup(const GlBufferMapCleanup&) = delete;
-  GlBufferMapCleanup& operator=(const GlBufferMapCleanup&) = delete;
-
- private:
-  GLenum buffer_;
-};
-
 }  // namespace render
 }  // namespace quic_trace
 
diff --git a/tools/render/trace_program.cc b/tools/render/trace_program.cc
index c447b8f..d03f87d 100644
--- a/tools/render/trace_program.cc
+++ b/tools/render/trace_program.cc
@@ -14,8 +14,8 @@
 
 #include "tools/render/trace_program.h"
 
-#include "absl/time/clock.h"
 #include "gflags/gflags.h"
+#include "absl/time/clock.h"
 #include "tools/render/layout_constants.h"
 
 DEFINE_bool(show_fps, false, "Show the current framerate of the program");
@@ -171,7 +171,7 @@
   }
   if (state[SDL_SCANCODE_R]) {
     absl::optional<Box> new_viewport =
-        renderer_->BoundContainedPackets(Box{state_.offset, state_.viewport});
+        trace_->BoundContainedPackets(Box{state_.offset, state_.viewport});
     if (new_viewport) {
       state_.offset = new_viewport->origin;
       state_.viewport = new_viewport->size;
diff --git a/tools/render/trace_renderer.cc b/tools/render/trace_renderer.cc
index 5002d64..493070b 100644
--- a/tools/render/trace_renderer.cc
+++ b/tools/render/trace_renderer.cc
@@ -188,46 +188,5 @@
   glDisable(GL_SCISSOR_TEST);
 }
 
-// TODO(vasilvv): we now have enough information in ProcessedTrace to do this
-// without a copy from GPU.
-absl::optional<Box> TraceRenderer::BoundContainedPackets(Box boundary) {
-  DCHECK(buffer_ready_);
-
-  // Since we don't actually store the list of the rectangles in memory, we get
-  // to fetch it from the GPU.
-  glBindBuffer(GL_ARRAY_BUFFER, *buffer_);
-  const Packet* packets = reinterpret_cast<Packet*>(glMapBufferRange(
-      GL_ARRAY_BUFFER, 0, packet_count_ * sizeof(Packet), GL_MAP_READ_BIT));
-  CHECK(packets != nullptr)
-      << "Failed to fetch packets, GL error: " << glGetError();
-  GlBufferMapCleanup cleanup(GL_ARRAY_BUFFER);
-
-  const float time_start = boundary.origin.x;
-  const float time_end = boundary.origin.x + boundary.size.x;
-  const Packet* packets_end = packets + packet_count_;
-  const auto compare = [](const Packet& a, const Packet& b) {
-    return a.time < b.time;
-  };
-  const Packet* range_start = std::lower_bound(
-      packets, packets_end, Packet{time_start, 0, 0, 0}, compare);
-  const Packet* range_end = std::upper_bound(
-      packets, packets_end, Packet{time_end, 0, 0, 0}, compare);
-
-  if (range_start == packets_end || range_end == packets_end ||
-      range_start > range_end) {
-    return absl::nullopt;
-  }
-
-  absl::optional<Box> result;
-  for (const Packet* it = range_start; it <= range_end; it++) {
-    Box packet{vec2(it->time, it->offset),
-               vec2(kSentPacketDurationMs, it->size)};
-    if (IsInside(packet, boundary)) {
-      result = result.has_value() ? BoundingBox(*result, packet) : packet;
-    }
-  }
-  return result;
-}
-
 }  // namespace render
 }  // namespace quic_trace
diff --git a/tools/render/trace_renderer.h b/tools/render/trace_renderer.h
index 9cc6ac5..e5f833d 100644
--- a/tools/render/trace_renderer.h
+++ b/tools/render/trace_renderer.h
@@ -54,10 +54,6 @@
     highlighted_packet_ = highlighted_packet;
   }
 
-  // For specified section of the graph, find a bounding box that contains all
-  // of the packets in it, or return nullopt if none are contained.
-  absl::optional<Box> BoundContainedPackets(Box boundary);
-
  private:
   // Packet metadata as uploaded onto the GPU.
   struct Packet {