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

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