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

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