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

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