6 #include "debugscript.h"
15 extern const char *script_filename;
16 extern bool make_movie; // whyyy
17 extern const char * program_name;
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))
23 View view(doc, scr, bounds, c);
28 scr.ScreenShot(output);
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)
36 static bool oldButtonDown = false;
37 static int oldx, oldy;
38 if (buttons == 3 && !oldButtonDown)
41 view->ToggleGPUTransform();
46 if (buttons == 2 && !oldButtonDown)
49 view->ToggleGPURendering();
53 if (buttons && !oldButtonDown)
55 // We're beginning a drag.
59 scr->SetMouseCursor(Screen::CursorMove);
63 view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
67 oldButtonDown = false;
68 scr->SetMouseCursor(Screen::CursorArrow);
75 view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), exp(Real(-wheel)/Real(20)));
80 void MainLoop(Document & doc, Screen & scr, View & view, int max_frames = -1)
82 // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
85 //scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 12);
86 scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 36);
87 scr.SetMouseHandler(RatCatcher);
90 istream * script_input = NULL;
91 if (script_filename != NULL)
93 if (strcmp(script_filename, "stdin") == 0)
97 tmp.open(script_filename);
101 DebugScript script(script_input);
103 double total_cpu_time = 0;
104 double total_gpu_time = 0;
105 double total_real_time = 0;
107 // MINGW doesn't support a lot of ctime stuff here
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;
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))
125 real_clock_prev = real_clock_now;
128 g_profiler.BeginZone("scr.Clear()");
130 g_profiler.EndZone();
131 //view.ForceBoundsDirty();
132 //view.ForceBufferDirty();
133 //view.ForceRenderDirty();
137 if (script.Execute(&view, &scr))
141 g_profiler.BeginZone("view.Render");
142 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
143 g_profiler.EndZone();
145 double cpu_frame = scr.GetLastFrameTimeCPU();
146 double gpu_frame = scr.GetLastFrameTimeGPU();
147 total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
150 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
151 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 double real_frame = cpu_frame;
156 total_real_time += real_frame;
157 if (data_rate > 0 && total_real_time > data_rate*(data_points+1))
159 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);
165 scr.DebugFontPrintF("%s\n", program_name);
166 scr.DebugFontPrintF("Top Left: (%s,%s)\n", Str(view.GetBounds().x).c_str(),Str(view.GetBounds().y).c_str());
167 scr.DebugFontPrintF("Width: %s\n", Str(view.GetBounds().w).c_str());
169 zoom = zoom/Real(view.GetBounds().w);
170 scr.DebugFontPrintF("Zoom: %s %%\n", Str(zoom).c_str());
171 scr.DebugFontPrintF("Similar size: %s\n", HumanScale(ClampFloat(Double(view.GetBounds().w))));
174 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
175 scr.DebugFontPrintF("Lazy Rendering = %d\n", view.UsingLazyRendering());
176 if (cpu_frame > 0 && total_cpu_time > 0)
177 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);
178 if (gpu_frame > 0 && total_gpu_time > 0)
179 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 if (real_frame > 0 && total_real_time > 0)
182 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 //scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
185 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
186 //#if REALTYPE == REAL_MPFRCPP
187 // scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
190 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
191 scr.DebugFontPrint("Doing cumulative coordinate transforms on Objects.\n");
193 if (view.UsingGPUTransform())
195 scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
199 scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
203 #ifdef TRANSFORM_BEZIERS_TO_PATH
204 scr.DebugFontPrint("Beziers have been transformed to Path\n");
208 if (view.UsingGPURendering())
210 scr.DebugFontPrint("Doing rendering using GPU.\n");
214 scr.DebugFontPrint("Doing rendering using CPU.\n");
221 s << "frame" << frames << ".bmp";
222 scr.ScreenShot(s.str().c_str());
227 g_profiler.BeginZone("scr.Present()");
229 g_profiler.EndZone();
230 g_profiler.EndFrame();