Added a profiler, which outputs time taken and calls to various functions.
[ipdf/code.git] / src / main.h
1 #include "common.h"
2
3 #include "document.h"
4 #include "view.h"
5 #include "screen.h"
6 #include "debugscript.h"
7 #include "profiler.h"
8 #include <unistd.h>
9
10
11 using namespace std;
12 using namespace IPDF;
13
14
15 extern const char *script_filename;
16
17 inline void OverlayBMP(Document & doc, const char * input, const char * output, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f))
18 {
19
20         Screen scr;
21         View view(doc, scr, bounds, c);
22         if (input != NULL)
23                 scr.RenderBMP(input);
24         view.Render();
25         if (output != NULL)
26                 scr.ScreenShot(output);
27         scr.Present();
28 }
29
30 // It is the only way.
31 // Dear god what have I done
32 void RatCatcher(int x, int y, int buttons, int wheel, Screen * scr, View * view)
33 {
34         static bool oldButtonDown = false;
35         static int oldx, oldy;
36         if (buttons == 3 && !oldButtonDown)
37         {
38                 oldButtonDown = true;
39                 view->ToggleGPUTransform();
40                 oldx = x;
41                 oldy = y;
42                 return;
43         }
44         if (buttons == 2 && !oldButtonDown)
45         {
46                 oldButtonDown = true;
47                 view->ToggleGPURendering();
48                 oldx = x;
49                 oldy = y;
50         }
51         if (buttons && !oldButtonDown)
52         {
53                 // We're beginning a drag.
54                 oldButtonDown = true;
55                 oldx = x;
56                 oldy = y;
57                 scr->SetMouseCursor(Screen::CursorMove);
58         }
59         if (buttons)
60         {
61                 view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
62         }
63         else
64         {
65                 oldButtonDown = false;
66                 scr->SetMouseCursor(Screen::CursorArrow);
67         }
68         oldx = x;
69         oldy = y;
70                 
71         if (wheel)
72         {
73                 view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), Real(expf(-wheel/20.f)));
74         }
75 }
76
77
78 void MainLoop(Document & doc, Screen & scr, View & view, int max_frames = -1)
79 {
80         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
81         
82
83         //scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 12);
84         scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 18);
85         scr.SetMouseHandler(RatCatcher);
86
87         ifstream tmp;
88         istream * script_input = NULL;
89         if (script_filename != NULL)
90         {
91                 if (strcmp(script_filename, "stdin") == 0)
92                         script_input = &cin;
93                 else
94                 {
95                         tmp.open(script_filename);
96                         script_input = &tmp;
97                 }
98         }
99         DebugScript script(script_input);
100
101         double total_cpu_time = 0;
102         double total_gpu_time = 0;
103         double total_real_time = 0;
104
105         // MINGW doesn't support a lot of ctime stuff here
106         #ifndef __MINGW32__     
107         struct timespec real_clock_start;
108         struct timespec real_clock_now;
109         struct timespec real_clock_prev;
110         clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
111         real_clock_now = real_clock_start;
112         #endif
113
114
115         double frames = 0;
116         double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
117         uint64_t data_points = 0;
118         setbuf(stdout, NULL);
119         int frame_number = 0;
120         while (scr.PumpEvents() && (max_frames < 0 || frame_number++ < max_frames))
121         {
122                 #ifndef __MINGW32__
123                 real_clock_prev = real_clock_now;
124                 #endif
125                 ++frames;
126                 g_profiler.BeginZone("scr.Clear()");
127                 scr.Clear();
128                 g_profiler.EndZone();
129                 //view.ForceBoundsDirty();
130                 //view.ForceBufferDirty();
131                 //view.ForceRenderDirty();
132
133                 if (script_filename)
134                 {
135                         if (script.Execute(&view, &scr))
136                                 return;
137                 }
138
139                 g_profiler.BeginZone("view.Render");
140                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
141                 g_profiler.EndZone();
142
143                 double cpu_frame = scr.GetLastFrameTimeCPU();
144                 double gpu_frame = scr.GetLastFrameTimeGPU();
145                 total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
146                 
147                 #ifndef __MINGW32__
148                 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
149                 double real_frame = (real_clock_now.tv_sec - real_clock_prev.tv_sec) + 1e-9*(real_clock_now.tv_nsec - real_clock_prev.tv_nsec);
150                 #else
151                 double real_frame = cpu_frame;
152                 #endif
153                 
154                 total_real_time += real_frame; 
155                 if (data_rate > 0 && total_real_time > data_rate*(data_points+1)) 
156                 {
157                         printf("%lu\t%f\t%f\t%f\t%f\t%f\t%f\n", (long unsigned int)frames, total_real_time, total_cpu_time, total_gpu_time, real_frame, cpu_frame, gpu_frame);
158                         data_points++;
159                 }
160                 
161
162                 
163
164                 scr.DebugFontPrintF("Top Left: (%s,%s)\n", Str(view.GetBounds().x).c_str(),Str(view.GetBounds().y).c_str());
165                 scr.DebugFontPrintF("Width: %s\n", Str(view.GetBounds().w).c_str());
166                 scr.DebugFontPrintF("Zoom: %s %%\n", Str(VReal(100)/VReal(view.GetBounds().w)).c_str());
167                 //scr.DebugFontPrintF("Similar size: %s\n", HumanScale(view.GetBounds().w * VReal(22e-3)));
168                 
169                 #if 0
170                 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
171                 scr.DebugFontPrintF("Lazy Rendering = %d\n", view.UsingLazyRendering());
172                 if (cpu_frame > 0 && total_cpu_time > 0)
173                         scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", cpu_frame*1e3, 1.0/cpu_frame, total_cpu_time,frames/total_cpu_time);
174                 if (gpu_frame > 0 && total_gpu_time > 0)
175                         scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", gpu_frame*1e3, 1.0/gpu_frame, total_gpu_time, frames/total_gpu_time);
176                 
177                 if (real_frame > 0 && total_real_time > 0)
178                         scr.DebugFontPrintF("[REALTIME] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", real_frame*1e3, 1.0/real_frame, total_real_time,frames/total_real_time);
179
180                 //scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
181                 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
182                 //#if REALTYPE == REAL_MPFRCPP
183                 //      scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
184                 //#endif 
185
186                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
187                 scr.DebugFontPrint("Doing cumulative coordinate transforms on Objects.\n");
188                 #else
189                 if (view.UsingGPUTransform())
190                 {
191                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
192                 }
193                 else
194                 {
195                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
196                 }
197                 #endif
198                 
199                 #ifdef TRANSFORM_BEZIERS_TO_PATH
200                         scr.DebugFontPrint("Beziers have been transformed to Path\n");
201                 #endif
202
203                 
204                 if (view.UsingGPURendering())
205                 {
206                         scr.DebugFontPrint("Doing rendering using GPU.\n");
207                 }
208                 else
209                 {
210                         scr.DebugFontPrint("Doing rendering using CPU.\n");
211                 }
212                 #endif // 0
213                 
214                 g_profiler.BeginZone("scr.Present()");
215                 scr.Present();
216                 g_profiler.EndZone();
217                 g_profiler.EndFrame();
218         }
219 }

UCC git Repository :: git.ucc.asn.au