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_PROGRAM_H_ 16 #define THIRD_PARTY_QUIC_TRACE_TOOLS_TRACE_PROGRAM_H_ 17 18 #include "absl/time/time.h" 19 #include "quic_trace/quic_trace.pb.h" 20 #include "tools/render/axis_renderer.h" 21 #include "tools/render/processed_trace.h" 22 #include "tools/render/program_state.h" 23 #include "tools/render/rectangle_renderer.h" 24 #include "tools/render/sdl_util.h" 25 #include "tools/render/table.h" 26 #include "tools/render/text.h" 27 #include "tools/render/trace_renderer.h" 28 29 namespace quic_trace { 30 namespace render { 31 32 // The framerate with respect to which all of the animations are scaled. Equal 33 // to 60 frames per second. 34 constexpr absl::Duration kReferenceFrameDuration = absl::Microseconds(16667); 35 36 // Top-level code for the trace rendering tool. Handles loading the trace from 37 // proto, the rendering and event loop, and the input events themselves. 38 class TraceProgram { 39 public: 40 TraceProgram(); 41 42 // Loads the trace into the renderer buffer. 43 void LoadTrace(std::unique_ptr<Trace> trace); 44 45 // Handle events and redraw the trace accordingly. 46 void Loop(); 47 48 private: 49 ProgramStateData state_; 50 51 ScopedSDL sdl_; 52 ScopedSdlWindow window_; 53 OpenGlContext context_; 54 55 std::unique_ptr<ProcessedTrace> trace_; 56 57 std::unique_ptr<ProgramState> state_buffer_; 58 std::unique_ptr<TraceRenderer> renderer_; 59 std::unique_ptr<TextRenderer> text_renderer_; 60 std::unique_ptr<AxisRenderer> axis_renderer_; 61 std::unique_ptr<RectangleRenderer> rectangle_renderer_; 62 63 bool quit_ = false; 64 65 // If true, panning is currently in progress. 66 bool panning_ = false; 67 vec2 panning_last_pos_; 68 bool zooming_ = false; 69 int zoom_start_x_; 70 int zoom_start_y_; 71 bool summary_ = false; 72 int summary_start_x_; 73 absl::optional<Table> summary_table_; 74 bool show_online_help_ = false; 75 76 // On OS X, the window has different size for rendering and for mouse input 77 // purposes. This vector contains the factor we use to translate mouse 78 // coordinates into the rendering coordinates. 79 vec2 input_scale_; 80 81 // An EWMA-filtered frame duration, used for scaling animations. 82 absl::Duration frame_duration_ = kReferenceFrameDuration; 83 84 // Scales a value with respect to framerate so that if you add |x| to some 85 // variable every frame, the effect is always same numerically as if the 86 // program is running at 60 frames per second. 87 float ScaleAdditiveFactor(float x); 88 // Same as above, except |k| is being multiplied instead of added. 89 float ScaleMultiplicativeFactor(float k); 90 // Zooms in or out, depending on the sign. For example, if |zoom| is +0.98, 91 // the size of the viewport would be 98% of what it was before, and -0.98 92 // would perform an inverse transform. 93 void Zoom(float zoom); 94 95 void UpdateWindowSize(); 96 97 // Handle input and window events. 98 void PollEvents(); 99 // Handle keyboard inputs. 100 void PollKeyboard(); 101 // Handle mouse inputs . 102 void PollMouse(); 103 // Handle panning via mouse. 104 void HandlePanning(bool pressed, int x, int y); 105 void HandleZooming(bool pressed, int x, int y); 106 void HandleSummary(bool pressed, int x); 107 void HandleMouseover(int x, int y); 108 // Ensure that the currently rendered part of the trace stays within the 109 // bounds of the trace. 110 void EnsureBounds(); 111 112 // Converts a relative coordinate window vector into an appropriate trace 113 // coordinate vector. 114 vec2 WindowToTraceRelative(vec2 vector); 115 // Converts an absolute coordinate window vector into an appropriate trace 116 // coordinate vector. 117 vec2 WindowToTraceCoordinates(vec2 point); 118 Box WindowToTraceCoordinates(Box box); 119 Box TraceBounds(); 120 121 // Shows the current framerate of the program in the corner if --show_fps is 122 // enabled. 123 void MaybeShowFramerate(); 124 Table GenerateOnlineHelp(); 125 // Draws all of the tables on the right side of the window. 126 void DrawRightSideTables(); 127 }; 128 129 } // namespace render 130 } // namespace quic_trace 131 132 #endif // THIRD_PARTY_QUIC_TRACE_TOOLS_TRACE_PROGRAM_H_ 133