Really hacky python performance scripts!
[ipdf/code.git] / src / debugscript.cpp
1 #include "debugscript.h"
2
3 #include <string>
4
5 using namespace IPDF;
6 using namespace std;
7
8 void DebugScript::ParseAction(View * view, Screen * scr)
9 {
10         if (m_input == NULL || !m_input->good())
11                 return;
12         istream & inp = *m_input;
13         Debug("Get action type...");
14         std::string actionType;
15         inp >> actionType;
16         Debug("Action type: %s", actionType.c_str());
17         // Skip comments
18         while (actionType[0] == '#')
19         {
20                 std::string tmp;
21                 std::getline(inp, tmp);
22                 inp >> std::ws >> actionType;
23         }
24         if (actionType == "loop")
25         {
26                 inp >> currentAction.loops >> actionType;
27         }
28         else
29         {
30                 currentAction.loops = 1;
31         }
32
33         if (actionType == "wait")
34         {
35                 currentAction.type = AT_WaitFrame;
36                 return;
37         }
38         else if (actionType == "translate")
39         {
40                 std::string _x, _y;
41                 inp >> _x >> _y;
42                 currentAction.type = AT_Translate;
43                 currentAction.x = RealFromStr(_x.c_str());
44                 currentAction.y = RealFromStr(_y.c_str());
45                 return;
46         }
47         else if (actionType == "zoom")
48         {
49                 std::string _x, _y, _z;
50                 inp >> _x >> _y >> _z;
51                 currentAction.type = AT_Zoom;
52                 currentAction.x = RealFromStr(_x.c_str());
53                 currentAction.y = RealFromStr(_y.c_str());
54                 currentAction.z = RealFromStr(_z.c_str());
55         }
56         else if (actionType == "pxtranslate")
57         {
58                 inp >> currentAction.ix >> currentAction.iy;
59                 currentAction.type = AT_TranslatePx;
60         }
61         else if (actionType == "pxzoom")
62         {
63                 inp >> currentAction.ix >> currentAction.iy >> currentAction.iz;
64                 currentAction.type = AT_ZoomPx;
65         }
66         else if (actionType == "gpu")
67         {
68                 currentAction.type = AT_SetGPURendering;
69         }
70         else if (actionType == "cpu")
71         {
72                 currentAction.type = AT_SetCPURendering;
73         }
74         else if (actionType == "lazy")
75         {
76                 currentAction.type = AT_EnableLazyRendering;
77         }
78         else if (actionType == "nolazy")
79         {
80                 currentAction.type = AT_DisableLazyRendering;
81         }
82         else if (actionType == "quit")
83         {
84                 currentAction.type = AT_Quit;
85         }
86         else if (actionType == "loadsvg")
87         {
88                 currentAction.type = AT_LoadSVG;
89                 inp >> currentAction.textargs;
90         }
91         else if (actionType == "label")
92         {
93                 currentAction.type = AT_Label;
94                 inp >> currentAction.textargs;
95         }
96         else if (actionType == "goto")
97         {
98                 currentAction.type = AT_Goto;
99                 inp >> currentAction.textargs;
100         }
101         else if (actionType == "debug")
102         {
103                 currentAction.type = AT_Debug;
104                 getline(inp,currentAction.textargs);
105         }
106         else if (actionType == "clear")
107         {
108                 currentAction.type = AT_ClearDocument;
109         }
110         else if (actionType == "clearperf")
111         {
112                 currentAction.type = AT_ClearPerformance;
113         }
114         else if (actionType == "printperf")
115         {
116                 currentAction.type = AT_PrintPerformance;
117         }
118         else if (actionType == "recordperf")
119         {
120                 currentAction.type = AT_RecordPerformance;
121         }
122         else if (actionType == "debugfont")
123         {
124                 currentAction.type = AT_DebugFont;
125                 inp >> currentAction.textargs;
126         }
127         else if (actionType == "approachz") // approach zenoistically
128         {
129                 currentAction.type = AT_ApproachBoundsZeno;
130                 std::string _x, _y, _w, _h, _z;
131                 inp >> _x >> _y >> _w >> _h >> _z;
132                 currentAction.x = RealFromStr(_x.c_str());
133                 currentAction.y = RealFromStr(_y.c_str());
134                 currentAction.w = RealFromStr(_w.c_str());
135                 currentAction.h = RealFromStr(_h.c_str());
136                 currentAction.z = RealFromStr(_z.c_str());
137         }
138         else if (actionType == "approachl") // approach linearly
139         {
140                 currentAction.type = AT_ApproachBoundsLinear;
141                 std::string _x, _y, _w, _h, _z;
142                 inp >> _x >> _y >> _w >> _h >> _z;
143                 currentAction.x = RealFromStr(_x.c_str());
144                 currentAction.y = RealFromStr(_y.c_str());
145                 currentAction.w = RealFromStr(_w.c_str());
146                 currentAction.h = RealFromStr(_h.c_str());
147                 currentAction.z = RealFromStr(_z.c_str());
148                 currentAction.x = (currentAction.x - view->GetBounds().x)/currentAction.z;
149                 currentAction.y = (currentAction.y - view->GetBounds().y)/currentAction.z;
150                 currentAction.w = (currentAction.w - view->GetBounds().w)/currentAction.z;
151                 currentAction.h = (currentAction.h - view->GetBounds().h)/currentAction.z;
152         }
153         else if (actionType == "setbounds")
154         {
155                 currentAction.type = AT_SetBounds;
156                 std::string _x, _y, _w, _h;
157                 inp >> _x >> _y >> _w >> _h;
158                 currentAction.x = RealFromStr(_x.c_str());
159                 currentAction.y = RealFromStr(_y.c_str());
160                 currentAction.w = RealFromStr(_w.c_str());
161                 currentAction.h = RealFromStr(_h.c_str());
162         }
163         else if (actionType == "querygpubounds")
164         {
165                 currentAction.type = AT_QueryGPUBounds;
166                 inp >> currentAction.textargs;
167                 currentAction.loops = 1;
168         }
169         else if (actionType == "screenshot")
170         {
171                 currentAction.type = AT_ScreenShot;
172                 inp >> currentAction.textargs;  
173         }
174         else
175                 Fatal("Unknown action %s", actionType.c_str());
176
177 }
178
179 bool DebugScript::Execute(View *view, Screen *scr)
180 {
181         if (currentAction.loops <= 0)
182         {
183                 if (m_index >= m_actions.size())
184                 {
185                         ParseAction(view, scr);
186                         if (m_labels.size() > 0)
187                         {
188                                 m_actions.push_back(currentAction);
189                                 m_index++;
190                         }
191                                 
192                 }
193                 else
194                         currentAction = m_actions[m_index++];
195         }
196
197         switch(currentAction.type)
198         {
199         case AT_Quit:
200                 return true;
201         case AT_WaitFrame:
202                 break;
203         case AT_Translate:
204                 view->Translate(currentAction.x, currentAction.y);
205                 break;
206         case AT_TranslatePx:
207                 view->Translate(Real(currentAction.ix)/Real(scr->ViewportWidth()), Real(currentAction.iy)/Real(scr->ViewportHeight()));
208                 break;
209         case AT_Zoom:
210                 view->ScaleAroundPoint(currentAction.x, currentAction.y, currentAction.z);
211                 break;
212         case AT_ZoomPx:
213                 view->ScaleAroundPoint(Real(currentAction.ix)/Real(scr->ViewportWidth()),Real(currentAction.iy)/Real(scr->ViewportHeight()), Real(expf(-currentAction.iz/20.f)));
214                 break;
215         case AT_SetGPURendering:
216                 view->SetGPURendering(true);
217                 break;
218         case AT_SetCPURendering:
219                 view->SetGPURendering(false);
220                 break;
221         case AT_EnableLazyRendering:
222                 view->SetLazyRendering(true);
223                 break;
224         case AT_DisableLazyRendering:
225                 view->SetLazyRendering(false);
226                 break;
227         case AT_LoadSVG:
228         {
229                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
230                         view->Doc().LoadSVG(currentAction.textargs, Rect(Real(1)/Real(2),Real(1)/Real(2),Real(1)/Real(800),Real(1)/Real(600))); 
231                 #else
232                         const Rect & bounds = view->GetBounds();
233                         view->Doc().LoadSVG(currentAction.textargs, Rect(bounds.x+bounds.w/Real(2),bounds.y+bounds.h/Real(2),bounds.w/Real(800),bounds.h/Real(600)));
234                 #endif
235                 currentAction.type = AT_WaitFrame;
236                 view->ForceRenderDirty();
237                 view->ForceBufferDirty();
238                 view->ForceBoundsDirty();
239                 currentAction.loops = 1;
240                 break;
241         }
242         case AT_Label:
243                 m_labels[currentAction.textargs] = m_index;
244                 currentAction.type = AT_WaitFrame;
245                 currentAction.loops = 1;
246                 break;
247         case AT_Goto:
248                 m_index = m_labels[currentAction.textargs];
249                 currentAction.loops = 1;
250                 break;
251         case AT_Debug:
252                 Debug("View bounds: %s", view->GetBounds().Str().c_str());
253                 if (currentAction.textargs.size() > 0)
254                         Debug("%s", currentAction.textargs.c_str());
255                 break;
256         case AT_ClearDocument:
257                 view->Doc().ClearObjects();
258                 currentAction.loops = 1;
259                 break;
260         case AT_ClearPerformance:
261                 ClearPerformance(view, scr);
262                 currentAction.loops = 1;
263                 break;
264         case AT_PrintPerformance:
265                 PrintPerformance(view, scr);
266                 currentAction.loops = 1;        
267                 break;
268         case AT_RecordPerformance:
269                 PrintPerformance(view, scr);
270                 break;
271         case AT_DebugFont:
272                 scr->ShowDebugFont(currentAction.textargs == "1" || currentAction.textargs == "on");
273                 currentAction.loops = 1;
274                 break;
275                 
276         case AT_ApproachBoundsZeno:
277         {       
278                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
279                 if (currentAction.z != VReal(1))
280                 {
281                         target.x = view->GetBounds().x + (target.x-view->GetBounds().x)/VReal(currentAction.z);
282                         target.y = view->GetBounds().y + (target.y-view->GetBounds().y)/VReal(currentAction.z);
283                         target.w = view->GetBounds().w + (target.w-view->GetBounds().w)/VReal(currentAction.z);
284                         target.h = view->GetBounds().h + (target.h-view->GetBounds().h)/VReal(currentAction.z);
285                 }
286                 
287
288                 VReal s = target.w/(view->GetBounds().w);
289                 if (Real(s) != 1)
290                 {
291                         VReal x0;
292                         VReal y0;
293                         x0 = (view->GetBounds().x - target.x)/((s - VReal(1))*view->GetBounds().w);
294                         y0 = (view->GetBounds().y - target.y)/((s - VReal(1))*view->GetBounds().h);
295                         view->ScaleAroundPoint(x0, y0, s);
296                         currentAction.loops++;
297                 }
298                 else
299                 {
300                         Debug("Already at target view; Waiting for remaining %d frames", currentAction.loops);
301                         currentAction.type = AT_WaitFrame;
302                 }
303                 break;
304         }
305         case AT_ApproachBoundsLinear:
306         {
307                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
308                 target.x += view->GetBounds().x;
309                 target.y += view->GetBounds().y;
310                 target.w += view->GetBounds().w;
311                 target.h += view->GetBounds().h;
312                 VReal s = target.w/(view->GetBounds().w);
313                 if (Real(s) != 1)
314                 {
315                         VReal x0;
316                         VReal y0;
317                         x0 = (view->GetBounds().x - target.x)/((s - VReal(1))*view->GetBounds().w);
318                         y0 = (view->GetBounds().y - target.y)/((s - VReal(1))*view->GetBounds().h);
319                         view->ScaleAroundPoint(x0, y0, s);
320                         currentAction.loops++;
321                 }
322                 else
323                 {
324                         Debug("Already at target view; Waiting for remaining %d frames", currentAction.loops);
325                         currentAction.type = AT_WaitFrame;
326                 }
327                 break;
328         }
329         case AT_SetBounds:
330         {
331                 VRect target(currentAction.x, currentAction.y, currentAction.w, currentAction.h);
332                 view->SetBounds(target);
333                 break;
334         }
335         
336         case AT_QueryGPUBounds:
337         {
338                 view->QueryGPUBounds(currentAction.textargs.c_str(), "w");
339                 currentAction.loops = 1;
340                 break;
341         }
342         case AT_ScreenShot:
343         {
344                 view->SaveBMP(currentAction.textargs.c_str());
345                 currentAction.loops = 1;
346                 break;
347         }
348         default:
349                 Fatal("Unknown script command in queue.");
350         }
351         currentAction.loops--;
352         return false;
353 }
354
355 void DebugScript::ClearPerformance(View * view, Screen * scr)
356 {
357         m_perf_start.clock = clock();
358         m_perf_start.object_count = view->Doc().ObjectCount();
359         m_perf_start.view_bounds = view->GetBounds();
360         m_perf_last = m_perf_start;
361 }
362
363 void DebugScript::PrintPerformance(View * view, Screen * scr)
364 {
365         DebugScript::PerformanceData now;
366         now.clock = clock();
367         now.object_count = view->Doc().ObjectCount();
368         now.view_bounds = view->GetBounds();
369
370         // object_count  clock  delta_clock  x  Log10(x)  y  Log10(y)  w  Log10(w)  Size(w)
371         #ifdef QUADTREE_DISABLED
372         printf("%d\t%llu\t%llu\t%s\t%s\t%s\t%s\t%s\t%s\t%u\n",
373                 now.object_count, (long long unsigned)now.clock,
374                 (long long unsigned)(now.clock - m_perf_last.clock),
375                 Str(now.view_bounds.x).c_str(), Str(Log10(Abs(now.view_bounds.x))).c_str(),
376                 Str(now.view_bounds.y).c_str(), Str(Log10(Abs(now.view_bounds.y))).c_str(),
377                 Str(now.view_bounds.w).c_str(), Str(Log10(now.view_bounds.w)).c_str(),
378                 (unsigned)Size(now.view_bounds.w));
379         #endif
380         m_perf_last = now;
381 }

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