xref: /aosp_15_r20/external/cronet/third_party/quic_trace/src/tools/render/program_state.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_PROGRAM_STATE_H_
16 #define THIRD_PARTY_QUIC_TRACE_TOOLS_PROGRAM_STATE_H_
17 
18 #include "tools/render/geometry_util.h"
19 #include "tools/render/sdl_util.h"
20 
21 namespace quic_trace {
22 namespace render {
23 
24 extern const char* kProgramStateData;
25 
26 // The global state of the program that every component and shader has access
27 // to.  There are some requirements regarding to how the format of this struct
28 // should be altered:
29 //  1. It should be kept in sync with the GLSL definition in .cc file.
30 //  2. There are some alignment rules that you have to follow, which are
31 //     somewhat elaborate (search "std140" for more details), but the
32 //     relevant part is that it's 4 bytes for floats and 8 bytes for 2D vectors.
33 struct ProgramStateData {
34   // Size of the program window in pixels.
35   vec2 window = vec2{1280.f, 720.f};
36   // Starting point, in axis units (us/bytes), of the part of the trace that is
37   // currently shown.
38   vec2 offset = vec2{0.f, 0.f};
39   // Size of the portion of the trace that is currently shown, in pixels.
40   vec2 viewport = vec2{0.f, 0.f};
41   // The factor by which UI elements need to be scaled due to the DPI.
42   alignas(sizeof(float)) float dpi_scale = 1.f;
43 };
44 
45 // A wrapper around ProgramStateData for the consumers of the data.  Allows
46 // access to the members of ProgramStateData as well as the copy of them in GPU
47 // memory.
48 class ProgramState {
49  public:
50   ProgramState(const ProgramStateData* data);
51   ProgramState(const ProgramState&) = delete;
52   ProgramState& operator=(const ProgramState&) = delete;
53 
window()54   const vec2& window() const { return data_->window; }
offset()55   const vec2& offset() const { return data_->offset; }
viewport()56   const vec2& viewport() const { return data_->viewport; }
dpi_scale()57   float dpi_scale() const { return data_->dpi_scale; }
58 
ScaleForDpi(int value)59   int ScaleForDpi(int value) const {
60     return std::round(data_->dpi_scale * value);
61   }
62 
63   // Binds the Uniform Buffer Object with |data_| onto the specified shader
64   // program.
65   void Bind(const GlProgram& program) const;
66   // Copies |data_| into |buffer_|.
67   void Refresh();
68 
69  private:
70   // Data on host.
71   const ProgramStateData* data_;
72   // Handle to data on the device.
73   GlUniformBuffer buffer_;
74 };
75 
76 }  // namespace render
77 }  // namespace quic_trace
78 
79 #endif  // THIRD_PARTY_QUIC_TRACE_TOOLS_PROGRAM_STATE_H_
80