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_TEXT_H_ 16 #define THIRD_PARTY_QUIC_TRACE_TOOLS_TEXT_H_ 17 18 #include <memory> 19 #include <unordered_map> 20 21 #include "absl/container/node_hash_map.h" 22 #include "external/sdl2_ttf/SDL_ttf.h" 23 #include "tools/render/program_state.h" 24 #include "tools/render/sdl_util.h" 25 #include "tools/render/shader.h" 26 27 namespace quic_trace { 28 namespace render { 29 30 class TextRenderer; 31 32 // Represents a rendered text object. 33 class Text { 34 public: width()35 size_t width() const { return width_; } height()36 size_t height() const { return height_; } 37 38 private: 39 friend class TextRenderer; 40 Text(TextRenderer * factory)41 Text(TextRenderer* factory) : factory_(factory) {} 42 43 void Draw(int x, int y) const; 44 45 TextRenderer* factory_; 46 GlTexture texture_; 47 float texture_size_; 48 49 size_t width_; 50 size_t height_; 51 }; 52 53 // Factory for Text objects. Upon cosntruction, loads all of the fonts and 54 // shaders used for text rendering into memory. 55 class TextRenderer { 56 public: 57 TextRenderer(const ProgramState* state); 58 ~TextRenderer(); 59 60 // Render the supplied text. This converts the text into pixels (making the 61 // dimensions of the text available), but does not actually draw anything on 62 // screen. RenderText(const std::string & text)63 std::shared_ptr<const Text> RenderText(const std::string& text) { 64 return RenderTextInner(text, TTF_STYLE_NORMAL); 65 } 66 // The equivalent of the method above, except it renders bold text. RenderTextBold(const std::string & text)67 std::shared_ptr<const Text> RenderTextBold(const std::string& text) { 68 return RenderTextInner(text, TTF_STYLE_BOLD); 69 } 70 // Schedules specified text to be drawn at specified window coordinates. 71 void AddText(std::shared_ptr<const Text> text, int x, int y); 72 // Draws all of the scheduled text on-screen. 73 void DrawAll(); 74 75 private: 76 friend class Text; 77 struct CacheEntry { 78 std::shared_ptr<const Text> text; 79 // Was this entry accessed within last frame? 80 bool live; 81 }; 82 struct TextToDraw { 83 std::shared_ptr<const Text> text; 84 int x; 85 int y; 86 }; 87 88 std::shared_ptr<const Text> RenderTextInner(const std::string& text, 89 int style); 90 91 TTF_Font* font_ = nullptr; 92 93 const ProgramState* state_; 94 Shader shader_; 95 96 // Text that will be drawn at the end of the frame. 97 std::vector<TextToDraw> texts_to_draw_; 98 // The text cache. Used to avoid having to render the text into texture every 99 // frame. All entries that are not rendered within the past frame are 100 // evicted. 101 absl::node_hash_map<std::string, CacheEntry> cache_; 102 }; 103 104 } // namespace render 105 } // namespace quic_trace 106 107 #endif // THIRD_PARTY_QUIC_TRACE_TOOLS_TEXT_H_ 108