6 #include "debugscript.h"
15 extern const char *script_filename;
17 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))
21 View view(doc, scr, bounds, c);
26 scr.ScreenShot(output);
30 // It is the only way.
31 // Dear god what have I done
32 void RatCatcher(int x, int y, int buttons, int wheel, Screen * scr, View * view)
34 static bool oldButtonDown = false;
35 static int oldx, oldy;
36 if (buttons == 3 && !oldButtonDown)
39 view->ToggleGPUTransform();
44 if (buttons == 2 && !oldButtonDown)
47 view->ToggleGPURendering();
51 if (buttons && !oldButtonDown)
53 // We're beginning a drag.
57 scr->SetMouseCursor(Screen::CursorMove);
61 view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
65 oldButtonDown = false;
66 scr->SetMouseCursor(Screen::CursorArrow);
73 view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), Real(expf(-wheel/20.f)));
78 void MainLoop(Document & doc, Screen & scr, View & view, int max_frames = -1)
80 // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
83 //scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 12);
84 scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 18);
85 scr.SetMouseHandler(RatCatcher);
88 istream * script_input = NULL;
89 if (script_filename != NULL)
91 if (strcmp(script_filename, "stdin") == 0)
95 tmp.open(script_filename);
99 DebugScript script(script_input);
101 double total_cpu_time = 0;
102 double total_gpu_time = 0;
103 double total_real_time = 0;
105 // MINGW doesn't support a lot of ctime stuff here
107 struct timespec real_clock_start;
108 struct timespec real_clock_now;
109 struct timespec real_clock_prev;
110 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
111 real_clock_now = real_clock_start;
116 double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
117 uint64_t data_points = 0;
118 setbuf(stdout, NULL);
119 int frame_number = 0;
120 while (scr.PumpEvents() && (max_frames < 0 || frame_number++ < max_frames))
123 real_clock_prev = real_clock_now;
126 g_profiler.BeginZone("scr.Clear()");
128 g_profiler.EndZone();
129 //view.ForceBoundsDirty();
130 //view.ForceBufferDirty();
131 //view.ForceRenderDirty();
135 if (script.Execute(&view, &scr))
139 g_profiler.BeginZone("view.Render");
140 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
141 g_profiler.EndZone();
143 double cpu_frame = scr.GetLastFrameTimeCPU();
144 double gpu_frame = scr.GetLastFrameTimeGPU();
145 total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
148 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
149 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);
151 double real_frame = cpu_frame;
154 total_real_time += real_frame;
155 if (data_rate > 0 && total_real_time > data_rate*(data_points+1))
157 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);
164 scr.DebugFontPrintF("Top Left: (%s,%s)\n", Str(view.GetBounds().x).c_str(),Str(view.GetBounds().y).c_str());
165 scr.DebugFontPrintF("Width: %s\n", Str(view.GetBounds().w).c_str());
166 scr.DebugFontPrintF("Zoom: %s %%\n", Str(VReal(100)/VReal(view.GetBounds().w)).c_str());
167 //scr.DebugFontPrintF("Similar size: %s\n", HumanScale(view.GetBounds().w * VReal(22e-3)));
170 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
171 scr.DebugFontPrintF("Lazy Rendering = %d\n", view.UsingLazyRendering());
172 if (cpu_frame > 0 && total_cpu_time > 0)
173 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);
174 if (gpu_frame > 0 && total_gpu_time > 0)
175 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);
177 if (real_frame > 0 && total_real_time > 0)
178 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);
180 //scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
181 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
182 //#if REALTYPE == REAL_MPFRCPP
183 // scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
186 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
187 scr.DebugFontPrint("Doing cumulative coordinate transforms on Objects.\n");
189 if (view.UsingGPUTransform())
191 scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
195 scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
199 #ifdef TRANSFORM_BEZIERS_TO_PATH
200 scr.DebugFontPrint("Beziers have been transformed to Path\n");
204 if (view.UsingGPURendering())
206 scr.DebugFontPrint("Doing rendering using GPU.\n");
210 scr.DebugFontPrint("Doing rendering using CPU.\n");
214 g_profiler.BeginZone("scr.Present()");
216 g_profiler.EndZone();
217 g_profiler.EndFrame();