Define for Transformations on Path only, also fixed segfault due to GraphicsBuffer
[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 inline 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");
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         struct timespec real_clock_start;
94         struct timespec real_clock_now;
95         struct timespec real_clock_prev;
96         clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
97         real_clock_now = real_clock_start;
98         double frames = 0;
99         double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
100         uint64_t data_points = 0;
101         setbuf(stdout, NULL);
102         int frame_number = 0;
103         while (scr.PumpEvents() && (max_frames < 0 || frame_number++ < max_frames))
104         {
105                 real_clock_prev = real_clock_now;
106                 ++frames;
107                 scr.Clear();
108                 //view.ForceBoundsDirty();
109                 //view.ForceBufferDirty();
110                 //view.ForceRenderDirty();
111
112                 if (script_filename)
113                 {
114                         if (script.Execute(&view, &scr))
115                                 return;
116                 }
117
118                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
119
120                 double cpu_frame = scr.GetLastFrameTimeCPU();
121                 double gpu_frame = scr.GetLastFrameTimeGPU();
122                 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
123                 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);
124
125
126                 total_real_time += real_frame; total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
127                 if (data_rate > 0 && total_real_time > data_rate*(data_points+1)) 
128                 {
129                         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);
130                         data_points++;
131                 }
132                 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
133                 scr.DebugFontPrintF("Lazy Rendering = %d\n", view.UsingLazyRendering());
134                 if (cpu_frame > 0 && total_cpu_time > 0)
135                         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);
136                 if (gpu_frame > 0 && total_gpu_time > 0)
137                         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);
138                 if (real_frame > 0 && total_real_time > 0)
139                         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);
140
141                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
142                 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
143                 //#if REALTYPE == REAL_MPFRCPP
144                 //      scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
145                 //#endif 
146
147                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
148                 scr.DebugFontPrint("Doing cumulative coordinate transforms on Objects.\n");
149                 #else
150                 if (view.UsingGPUTransform())
151                 {
152                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
153                 }
154                 else
155                 {
156                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
157                 }
158                 #endif
159                 
160                 #ifdef TRANSFORM_BEZIERS_TO_PATH
161                         scr.DebugFontPrint("Beziers have been transformed to Path\n");
162                 #endif
163
164                 
165                 if (view.UsingGPURendering())
166                 {
167                         scr.DebugFontPrint("Doing rendering using GPU.\n");
168                 }
169                 else
170                 {
171                         scr.DebugFontPrint("Doing rendering using CPU.\n");
172                 }
173                 scr.Present();
174         }
175 }

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