1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef THIRD_PARTY_QUIC_TRACE_TOOLS_TRACE_RENDERER_H_
16 #define THIRD_PARTY_QUIC_TRACE_TOOLS_TRACE_RENDERER_H_
17 
18 #include <cstdint>
19 
20 #include "absl/types/optional.h"
21 #include "tools/render/program_state.h"
22 #include "tools/render/sdl_util.h"
23 #include "tools/render/shader.h"
24 
25 namespace quic_trace {
26 namespace render {
27 
28 enum class PacketType { SENT, ACKED, LOST, APP_LIMITED };
29 
30 // Draws the trace on the current OpenGL context.
31 class TraceRenderer {
32  public:
33   TraceRenderer(const ProgramState* state);
34 
35   // Preallocate the packet buffer.
36   void PacketCountHint(size_t count);
37   // Add a new packet to be drawn.
38   void AddPacket(uint64_t time,
39                  uint64_t offset,
40                  uint64_t size,
41                  PacketType type);
42   // Uploads all of the packets to the GPU and frees the local buffer.  Must be
43   // called before Render().
44   void FinishPackets();
45 
46   // Actually draw the trace.
47   void Render();
48 
max_x()49   float max_x() const { return max_x_; }
max_y()50   float max_y() const { return max_y_; }
51 
52   // Sets the packet to highlight on trace.
set_highlighted_packet(int highlighted_packet)53   void set_highlighted_packet(int highlighted_packet) {
54     highlighted_packet_ = highlighted_packet;
55   }
56 
57  private:
58   // Packet metadata as uploaded onto the GPU.
59   struct Packet {
60     float time;
61     float offset;
62     float size;
63     float kind;
64   };
65 
66   std::vector<Packet> packet_buffer_;
67   bool buffer_ready_ = false;
68   GlVertexBuffer buffer_;
69   size_t packet_count_;
70   GlVertexArray array_;
71 
72   const ProgramState* state_;
73   Shader shader_;
74 
75   float max_x_ = 0.f;
76   float max_y_ = 0.f;
77 
78   int highlighted_packet_ = -1;
79 };
80 
81 }  // namespace render
82 }  // namespace quic_trace
83 
84 #endif  // THIRD_PARTY_QUIC_TRACE_TOOLS_TRACE_RENDERER_H_
85