1 /************************************************************************
2 * file name         : globals.h
3 * ----------------- :
4 * creation time     : 2016/08/03
5 * author            : Victor Zarubkin
6 * email             : v.s.zarubkin@gmail.com
7 * ----------------- :
8 * description       : The file contains declaration of global constants and variables for profiler gui.
9 * ----------------- :
10 * change log        : * 2016/08/03 Victor Zarubkin: initial commit.
11 *                   :
12 *                   : *
13 * ----------------- :
14 * license           : Lightweight profiler library for c++
15 *                   : Copyright(C) 2016-2017  Sergey Yagovtsev, Victor Zarubkin
16 *                   :
17 *                   : Licensed under either of
18 *                   :     * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
19 *                   :     * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
20 *                   : at your option.
21 *                   :
22 *                   : The MIT License
23 *                   :
24 *                   : Permission is hereby granted, free of charge, to any person obtaining a copy
25 *                   : of this software and associated documentation files (the "Software"), to deal
26 *                   : in the Software without restriction, including without limitation the rights
27 *                   : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 *                   : of the Software, and to permit persons to whom the Software is furnished
29 *                   : to do so, subject to the following conditions:
30 *                   :
31 *                   : The above copyright notice and this permission notice shall be included in all
32 *                   : copies or substantial portions of the Software.
33 *                   :
34 *                   : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
35 *                   : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 *                   : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
37 *                   : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
38 *                   : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39 *                   : USE OR OTHER DEALINGS IN THE SOFTWARE.
40 *                   :
41 *                   : The Apache License, Version 2.0 (the "License")
42 *                   :
43 *                   : You may not use this file except in compliance with the License.
44 *                   : You may obtain a copy of the License at
45 *                   :
46 *                   : http://www.apache.org/licenses/LICENSE-2.0
47 *                   :
48 *                   : Unless required by applicable law or agreed to in writing, software
49 *                   : distributed under the License is distributed on an "AS IS" BASIS,
50 *                   : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51 *                   : See the License for the specific language governing permissions and
52 *                   : limitations under the License.
53 ************************************************************************/
54 
55 #ifndef EASY_PROFILER__GUI_GLOBALS_H
56 #define EASY_PROFILER__GUI_GLOBALS_H
57 
58 #include <string>
59 #include <QObject>
60 #include <QColor>
61 #include <QTextCodec>
62 #include <QSize>
63 #include <QFont>
64 #include "common_functions.h"
65 #include "globals_qobjects.h"
66 
67 //////////////////////////////////////////////////////////////////////////
68 //////////////////////////////////////////////////////////////////////////
69 
70 namespace profiler_gui {
71 
72     const QString ORGANAZATION_NAME = "EasyProfiler";
73     const QString APPLICATION_NAME = "Easy profiler gui application";
74 
75     const QColor CHRONOMETER_COLOR = QColor::fromRgba(0x40000000 | (::profiler::colors::RichBlue & 0x00ffffff));// 0x402020c0);
76     const QColor CHRONOMETER_COLOR2 = QColor::fromRgba(0x40000000 | (::profiler::colors::Dark & 0x00ffffff));// 0x40408040);
77     const QColor TEXT_COLOR = QColor::fromRgb(0xff504040);
78     const QColor SYSTEM_BORDER_COLOR = QColor::fromRgb(0xffcccccc);
79     EASY_CONSTEXPR QRgb SELECTED_THREAD_BACKGROUND = 0xffe0e060;
80     EASY_CONSTEXPR QRgb SELECTED_THREAD_FOREGROUND = 0xffffffff - (SELECTED_THREAD_BACKGROUND & 0x00ffffff);
81 
82     EASY_CONSTEXPR qreal SCALING_COEFFICIENT = 1.25;
83     EASY_CONSTEXPR qreal SCALING_COEFFICIENT_INV = 1.0 / SCALING_COEFFICIENT;
84 
85     EASY_CONSTEXPR uint32_t V130 = 0x01030000;
86 
87     Q_CONSTEXPR QSize ICONS_SIZE {28, 28};
88     EASY_CONSTEXPR uint16_t GRAPHICS_ROW_SIZE = 20;
89     EASY_CONSTEXPR uint16_t GRAPHICS_ROW_SPACING = 0;
90     EASY_CONSTEXPR uint16_t GRAPHICS_ROW_SIZE_FULL = GRAPHICS_ROW_SIZE + GRAPHICS_ROW_SPACING;
91     EASY_CONSTEXPR uint16_t THREADS_ROW_SPACING = 10;
92 
93 #ifdef _WIN32
94     EASY_CONSTEXPR qreal FONT_METRICS_FACTOR = 1.05;
95 #else
96     EASY_CONSTEXPR qreal FONT_METRICS_FACTOR = 1.;
97 #endif
98 
99     //////////////////////////////////////////////////////////////////////////
100 
101     template <class T>
102     inline auto toUnicode(const T& _inputString) -> decltype(QTextCodec::codecForLocale()->toUnicode(_inputString))
103     {
104         return QTextCodec::codecForLocale()->toUnicode(_inputString);
105     }
106 
107     //////////////////////////////////////////////////////////////////////////
108 
109     inline QString decoratedThreadName(bool _use_decorated_thread_name, const::profiler::BlocksTreeRoot& _root, const QString& _unicodeThreadWord, bool _hex = false)
110     {
111         if (_root.got_name())
112         {
113             QString rootname(toUnicode(_root.name()));
114             if (!_use_decorated_thread_name || rootname.contains(_unicodeThreadWord, Qt::CaseInsensitive))
115             {
116                 if (_hex)
117                     return QString("%1 0x%2").arg(rootname).arg(_root.thread_id, 0, 16);
118                 return QString("%1 %2").arg(rootname).arg(_root.thread_id);
119             }
120 
121             if (_hex)
122                 return QString("%1 Thread 0x%2").arg(rootname).arg(_root.thread_id, 0, 16);
123             return QString("%1 Thread %2").arg(rootname).arg(_root.thread_id);
124         }
125 
126         if (_hex)
127             return QString("Thread 0x%1").arg(_root.thread_id, 0, 16);
128         return QString("Thread %1").arg(_root.thread_id);
129     }
130 
131     inline QString decoratedThreadName(bool _use_decorated_thread_name, const ::profiler::BlocksTreeRoot& _root, bool _hex = false)
132     {
133         if (_root.got_name())
134         {
135             QString rootname(toUnicode(_root.name()));
136             if (!_use_decorated_thread_name || rootname.contains(toUnicode("thread"), Qt::CaseInsensitive))
137             {
138                 if (_hex)
139                     return QString("%1 0x%2").arg(rootname).arg(_root.thread_id, 0, 16);
140                 return QString("%1 %2").arg(rootname).arg(_root.thread_id);
141             }
142 
143             if (_hex)
144                 return QString("%1 Thread 0x%2").arg(rootname).arg(_root.thread_id, 0, 16);
145             return QString("%1 Thread %2").arg(rootname).arg(_root.thread_id);
146         }
147 
148         if (_hex)
149             return QString("Thread 0x%1").arg(_root.thread_id, 0, 16);
150         return QString("Thread %1").arg(_root.thread_id);
151     }
152 
153     //////////////////////////////////////////////////////////////////////////
154 
155     enum ChronometerTextPosition : int8_t
156     {
157         ChronoTextPosition_Center = 0,
158         ChronoTextPosition_Top,
159         ChronoTextPosition_Bottom,
160 
161     }; // END of enum ChronometerTextPosition.
162 
163     //////////////////////////////////////////////////////////////////////////
164 
165     struct EasyGlobals Q_DECL_FINAL
166     {
167         static EasyGlobals& instance();
168 
169         EasyGlobalSignals                         events; ///< Global signals
170         ::profiler::thread_blocks_tree_t profiler_blocks; ///< Profiler blocks tree loaded from file
171         ::profiler::descriptors_list_t       descriptors; ///< Profiler block descriptors list
172         EasyBlocks                            gui_blocks; ///< Profiler graphics blocks builded by GUI
173 
174         QString                                    theme; ///< Current UI theme name
175         QFont                                    bg_font; ///< Font for blocks_graphics_view
176         QFont                           chronometer_font; ///< Font for easy_chronometer_item
177         QFont                                 items_font; ///< Font for easy_graphics_item
178         QFont                         selected_item_font; ///< Font for easy_graphics_item
179 
180         double                                scene_left; ///< Graphics scene left boundary
181         double                               scene_right; ///< Graphics scene right boundary
182         ::profiler::timestamp_t               begin_time; ///<
183         ::profiler::thread_id_t          selected_thread; ///< Current selected thread id
184         ::profiler::block_index_t         selected_block; ///< Current selected profiler block index
185         ::profiler::block_id_t         selected_block_id; ///< Current selected profiler block id
186         uint32_t                                 version; ///< Opened file version (files may have different format)
187         float                                 frame_time; ///< Expected frame time value in microseconds to be displayed at minimap on graphics scrollbar
188         int                               blocks_spacing; ///< Minimum blocks spacing on diagram
189         int                              blocks_size_min; ///< Minimum blocks size on diagram
190         int                           blocks_narrow_size; ///< Width indicating narrow blocks
191         int                              max_fps_history; ///< Max frames history displayed in FPS Monitor
192         int                           fps_timer_interval; ///< Interval in milliseconds for sending network requests to the profiled application (used by FPS Monitor)
193         int                        fps_widget_line_width; ///< Line width in pixels of FPS lines for FPS Monitor
194         ChronometerTextPosition     chrono_text_position; ///< Selected interval text position
195         TimeUnits                             time_units; ///< Units type for time (milliseconds, microseconds, nanoseconds or auto-definition)
196         bool                                   connected; ///< Is connected to source (to be able to capture profiling information)
197         bool                                 fps_enabled; ///< Is FPS Monitor enabled
198         bool                   use_decorated_thread_name; ///< Add "Thread" to the name of each thread (if there is no one)
199         bool                               hex_thread_id; ///< Use hex view for thread-id instead of decimal
200         bool                        enable_event_markers; ///< Enable event indicators painting (These are narrow rectangles at the bottom of each thread)
201         bool                           enable_statistics; ///< Enable gathering and using statistics (Disable if you want to consume less memory)
202         bool                          enable_zero_length; ///< Enable zero length blocks (if true, then such blocks will have width == 1 pixel on each scale)
203         bool                add_zero_blocks_to_hierarchy; ///< Enable adding zero blocks into hierarchy tree
204         bool                 draw_graphics_items_borders; ///< Draw borders for graphics blocks or not
205         bool                        hide_narrow_children; ///< Hide children for narrow graphics blocks (See blocks_narrow_size)
206         bool                         hide_minsize_blocks; ///< Hide blocks which screen size is less than blocks_size_min
207         bool                 display_only_relevant_stats; ///< Display only relevant information in ProfTreeWidget (excludes min, max, average times if there are only 1 calls number)
208         bool                collapse_items_on_tree_close; ///< Collapse all items which were displayed in the hierarchy tree after tree close/reset
209         bool               all_items_expanded_by_default; ///< Expand all items after file is opened
210         bool               only_current_thread_hierarchy; ///< Build hierarchy tree for current thread only
211         bool               highlight_blocks_with_same_id; ///< Highlight all blocks with same id on diagram
212         bool              selecting_block_changes_thread; ///< If true then current selected thread will change every time you select block
213         bool                auto_adjust_histogram_height; ///< Automatically adjust histogram height to the visible region
214         bool            display_only_frames_on_histogram; ///< Display only top-level blocks on histogram when drawing histogram by block id
215         bool           bind_scene_and_tree_expand_status; /** \brief If true then items on graphics scene and in the tree (blocks hierarchy) are binded on each other
216                                                                 so expanding/collapsing items on scene also expands/collapse items in the tree. */
217 
218     private:
219 
220         EasyGlobals();
221 
222     }; // END of struct EasyGlobals.
223 
224     //////////////////////////////////////////////////////////////////////////
225 
226 } // END of namespace profiler_gui.
227 
228 #ifndef IGNORE_GLOBALS_DECLARATION
229 #define EASY_GLOBALS ::profiler_gui::EasyGlobals::instance()
230 
easyBlock(::profiler::block_index_t i)231 inline ::profiler_gui::EasyBlock& easyBlock(::profiler::block_index_t i) {
232     return EASY_GLOBALS.gui_blocks[i];
233 }
234 
easyDescriptor(::profiler::block_id_t i)235 inline ::profiler::SerializedBlockDescriptor& easyDescriptor(::profiler::block_id_t i) {
236     return *EASY_GLOBALS.descriptors[i];
237 }
238 
easyBlocksTree(::profiler::block_index_t i)239 EASY_FORCE_INLINE const ::profiler::BlocksTree& easyBlocksTree(::profiler::block_index_t i) {
240     return easyBlock(i).tree;
241 }
242 
easyBlockName(const::profiler::BlocksTree & _block)243 EASY_FORCE_INLINE const char* easyBlockName(const ::profiler::BlocksTree& _block) {
244     const char* name = _block.node->name();
245     return *name != 0 ? name : easyDescriptor(_block.node->id()).name();
246 }
247 
easyBlockName(const::profiler::BlocksTree & _block,const::profiler::SerializedBlockDescriptor & _desc)248 EASY_FORCE_INLINE const char* easyBlockName(const ::profiler::BlocksTree& _block, const ::profiler::SerializedBlockDescriptor& _desc) {
249     const char* name = _block.node->name();
250     return *name != 0 ? name : _desc.name();
251 }
252 
easyBlockName(::profiler::block_index_t i)253 EASY_FORCE_INLINE const char* easyBlockName(::profiler::block_index_t i) {
254     return easyBlockName(easyBlock(i).tree);
255 }
256 
sceneX(profiler::timestamp_t _time)257 inline qreal sceneX(profiler::timestamp_t _time) {
258     return PROF_MICROSECONDS(qreal(_time - EASY_GLOBALS.begin_time));
259 }
260 
imagePath(const QString & _resource)261 inline QString imagePath(const QString& _resource) {
262     return QString(":/images/%1/%2").arg(EASY_GLOBALS.theme).arg(_resource);
263 }
264 
imagePath(const char * _resource)265 inline QString imagePath(const char* _resource) {
266     return QString(":/images/%1/%2").arg(EASY_GLOBALS.theme).arg(_resource);
267 }
268 #endif
269 
270 //////////////////////////////////////////////////////////////////////////
271 //////////////////////////////////////////////////////////////////////////
272 
273 #endif // EASY_PROFILER__GUI_GLOBALS_H
274