]> git.vomp.tv Git - vompclient.git/blob - surfacevector.cc
Clean up screenShot() - all params, return types, function names
[vompclient.git] / surfacevector.cc
1 /*
2     Copyright 2012 Marten Richter
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <wchar.h>
21 #include <stdlib.h>
22 #include <math.h>
23 #include "bitmap.h"
24 #include "staticartwork.h"
25 #include "surfacevector.h"
26
27 SurfaceVector::SurfaceVector(OsdVector* vosd)
28 {
29   osd = vosd;
30   commands.reserve(2048);
31 }
32
33 SurfaceVector::~SurfaceVector()
34 {
35   osd->removeSurface(this);
36
37   for (SVGCommand& command : commands)
38   {
39     osd->decrementDrawStyleHandleRefCount(command.getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
40
41     ImageIndex ii = command.getImageIndex();
42     if (ii) osd->removeImageRef(ii);
43
44     LoadIndex li = command.getLoadIndex();
45     if (li) osd->removeLoadIndexRef(li);
46   }
47 }
48
49 int SurfaceVector::getFontHeight()
50 {
51   return (int)osd->getFontHeight();
52 }
53
54 float SurfaceVector::getCharWidth(wchar_t c)
55 {
56   return osd->getCharWidth(c);
57 }
58
59 /*
60  * By current design it's not permitted to have a surface without the first drawing being to
61  * fill with a background colour. The fillblt calls removeCommands which deletes all the old
62  * commands before the background colour becomes the new first command, then all the rest are
63  * redrawn on top. If you want to change this in future then do this as the first thing in
64  * Boxx::draw():
65  *   surface->clear();
66  * and have this function in here:
67  *   void SurfaceVector::clear()
68  *   {
69  *     removeCommands(0, 0, swidth, sheight);
70  *   }
71  * Or preferably a simpler one that doesn't do all the comparison work.
72 */
73
74 int SurfaceVector::drawText(const char* text, int x, int y, const DrawStyle& c)
75 {
76   return drawText(text, x, y, 0, c);
77 }
78
79 int SurfaceVector::drawText(const char* text, int x, int y, int width, const DrawStyle& c)
80 {
81   float shift = 0.;
82   const char* run = text;
83   size_t length = strlen(text);
84
85
86   command_mutex.lock();
87
88   VectorHandle ref;
89   float* charwidtharray = osd->getCharWidthArray();
90   int commands_size = commands.size();
91   int chars = 0;
92   commands.resize(commands_size + strlen(text));
93 #ifndef WIN32
94   mbstate_t state;
95   wchar_t tempo;
96   size_t num_bytes = 1;
97   memset((void*)&state, 0, sizeof(state));
98   num_bytes = mbrtowc(&tempo, run, length, &state);
99
100   while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
101   {
102     ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
103     SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, tempo, ref);
104     chars++;
105
106     float cur_shift = charwidtharray[tempo & 0xFF];
107
108     if (tempo && 0xFFFFFF00) cur_shift = osd->getCharWidth(tempo);
109
110     shift += cur_shift;
111     length -= num_bytes;
112     run += num_bytes;
113
114     if (shift > width && width > 0)
115     {
116       command_mutex.unlock();
117       return 1;
118     }
119
120     num_bytes = mbrtowc(&tempo, run, length, &state);
121   }
122
123 #else
124   wchar_t* temptext = new wchar_t[length + 1];
125   int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
126                                         temptext, length + 1) - 1;
127
128   for (int i = 0; i < real_length; i++)
129   {
130     ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
131     SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, temptext[i], ref);
132     chars++;
133
134     float cur_shift = charwidtharray[temptext[i] & 0xFF];
135
136     if (temptext[i] && 0xFFFFFF00) cur_shift = osd->getCharWidth(temptext[i]);
137
138     shift += cur_shift;
139   }
140
141   delete[] temptext;
142 #endif
143
144   commands.resize(commands_size + chars);
145   command_mutex.unlock();
146   return 1;
147
148 }
149 int SurfaceVector::drawTextRJ(const char* text, int x, int y, const DrawStyle& c)
150 {
151   float shift = 0.;
152   const char* run = text;
153   size_t length = strlen(text);
154
155 #ifndef WIN32
156   mbstate_t state;
157   wchar_t tempo[1];
158   size_t num_bytes = 1;
159   memset((void*)&state, 0, sizeof(state));
160   num_bytes = mbrtowc(tempo, run, length, &state);
161
162   while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
163   {
164     shift += osd->getCharWidth(*tempo);
165     length -= num_bytes;
166     run += num_bytes;
167     num_bytes = mbrtowc(tempo, run, length, &state);
168   }
169
170 #else
171   wchar_t* temptext = new wchar_t[length + 1];
172   int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
173                                         temptext, length + 1) - 1;
174
175   for (int i = 0; i < real_length; i++)
176   {
177     shift += osd->getCharWidth(temptext[i]);
178   }
179
180   delete[] temptext;
181 #endif
182   return drawText(text, (int)(x - shift), y, c);
183 }
184
185 int SurfaceVector::drawTextCentre(const char* text, int x, int y, const DrawStyle& c)
186 {
187   float shift = 0;
188   const char* run = text;
189   size_t length = strlen(text);
190
191 #ifndef WIN32
192   mbstate_t state;
193   wchar_t tempo[1];
194   size_t num_bytes = 1;
195   memset((void*)&state, 0, sizeof(state));
196   num_bytes = mbrtowc(tempo, run, length, &state);
197
198   while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
199   {
200     shift += osd->getCharWidth(*tempo);
201     length -= num_bytes;
202     run += num_bytes;
203     num_bytes = mbrtowc(tempo, run, length, &state);
204   }
205
206 #else
207   wchar_t* temptext = new wchar_t[length + 1];
208   int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
209                                         temptext, length + 1) - 1;
210
211   for (int i = 0; i < real_length; i++)
212   {
213     shift += osd->getCharWidth(temptext[i]);
214   }
215
216   delete[] temptext;
217 #endif
218
219   return drawText(text, (int)(x - shift / 2.),  y, c);
220 }
221
222 void SurfaceVector::drawJpeg(const char* fileName, int x, int y, int* width, int* height)
223 {
224   StaticArtwork index = sa_MAX; // This is for compatibility only
225
226   if (strcmp(fileName, "/vdr.jpg") == 0)
227   {
228     index = sa_vdrlogo;
229     *height = 100; // this is faked so that the system does use the old coordinate system
230     *width = (int)ceil(190.f * osd->getPixelAspect());
231   }
232   else if (strcmp(fileName, "/wallpaperPAL.jpg") == 0)
233   {
234     index = sa_wallpaper;
235     *width = 720; // this is faked so that the system does use the old coordinate system
236     *height = 576;
237   }
238
239   if (index != sa_MAX)
240   {
241     TVMediaInfo info;
242     info.setStaticArtwork(index);
243     drawTVMedia(info, x, y, *width, *height, TopLeft);
244   }
245 }
246
247 /*
248 void SurfaceVector::drawJpeg(const char *fileName,int x, int y,int *width, int *height)
249 {
250         command_mutex.lock();
251         ImageIndex image=osd->getJpegRef(fileName,width,height);
252         commands.push_back(SVGCommand::PaintImage(x,y,*width,*height,image,0));
253         command_mutex.unlock();
254 }
255 */
256
257 void SurfaceVector::drawTVMedia(TVMediaInfo& tvmedia, float x, float y, float  width, float height, Corner corner)
258 {
259   command_mutex.lock();
260   ImageIndex image = 0;
261   LoadIndex load_index = osd->getTVMediaRef(tvmedia, image);
262
263   if (width != 0 && height != 0)
264   {
265     removeCommands(x, y, width, height);
266   }
267
268   if (image)
269   {
270     //Log::getInstance()->log("SurfaceVector", Log::DEBUG, "TVMedia Add instru image %d %d", load_index,image);
271     commands.push_back(SVGCommand::PaintImage(x, y, width, height, image, 0, corner));
272   }
273   else
274   {
275
276     commands.push_back(SVGCommand::PaintImageLoading(load_index, x, y, width, height, corner));
277     //Log::getInstance()->log("SurfaceVector", Log::DEBUG, "TVMedia Add instru image loading %d %d", load_index,image);
278   }
279
280   command_mutex.unlock();
281 }
282
283 void SurfaceVector::drawClippingRectangle(float x, float y, float w, float h)
284 {
285   command_mutex.lock();
286   commands.push_back(SVGCommand::PaintClipping((float)x, (float)y, (float)w, (float)h));
287   command_mutex.unlock();
288 }
289
290 int SurfaceVector::create(UINT width, UINT height)
291 {
292   sheight = height;
293   swidth = width;
294   return 1;
295 }
296 void SurfaceVector::display()
297 {
298   //nothing this is really mvp specific  // FIXME remove?
299 }
300
301 int SurfaceVector::fillblt(int x, int y, int width, int height, const DrawStyle& c)
302 {
303   command_mutex.lock();
304   removeCommands(x, y, width, height); // remove commands below the box
305   VectorHandle ref = osd->getDrawStyleHandle(c);
306   commands.push_back(SVGCommand::PaintPath(x, y, width, height, PIRectangle, ref));
307   command_mutex.unlock();
308   return 1;
309
310 }
311 void SurfaceVector::drawHorzLine(int x1, int x2, int y, const DrawStyle& c)
312 {
313   command_mutex.lock();
314   VectorHandle ref = osd->getDrawStyleHandle(c);
315   commands.push_back(SVGCommand::PaintPath(x1, y, x2 - x1, 1, PIHorzLine, ref));
316   command_mutex.unlock();
317 }
318
319 void SurfaceVector::drawVertLine(int x, int y1, int y2, const DrawStyle& c)
320 {
321   command_mutex.lock();
322   VectorHandle ref = osd->getDrawStyleHandle(c);
323   commands.push_back(SVGCommand::PaintPath(x, y1, 1, y2 - y1, PIVertLine, ref));
324   command_mutex.unlock();
325 }
326
327 void SurfaceVector::drawBitmap(int x, int y, const Bitmap& bm, const DisplayRegion& region)
328 {
329   //this is complicated
330   command_mutex.lock();
331   /*
332         unsigned int * data=(unsigned int*)malloc(sizeof(unsigned int)*bm.getWidth()*bm.getHeight());
333         for (UINT j = 0; j < bm.getHeight(); ++j){
334            for (UINT i = 0; i < bm.getWidth(); ++i)
335            {
336                    data[i+j*bm.getHeight()]=bm.getColour(i,j);
337            }
338       }*/
339   ImageIndex image = osd->getImagePalette(bm.getWidth(), bm.getHeight(), &(bm.rawData()[0]),
340                                           (const unsigned int*)&bm.palette.getColourVector()[0]); // data is freed by the OSD
341   //free(data);
342   float tx = x + region.windowx;
343   float ty = y + region.windowy;
344   float th = bm.getHeight();
345   float tw = bm.getWidth();
346
347   float scalex = 720.f / ((float) (region.framewidth + 1));
348   float scaley = 576.f / ((float) (region.frameheight + 1));
349   tx *= scalex;
350   ty *= scaley;
351   tw *= scalex;
352   th *= scaley;
353   SVGCommand temp = SVGCommand::PaintImage(tx, ty, tw, th, image, 0);
354   removeCommands(tx, ty, tw, th);
355   commands.push_back(temp);
356   command_mutex.unlock();
357 }
358
359 void SurfaceVector::drawPoint(int x, int y, DrawStyle& c, bool fastdraw)
360 {
361   if (!fastdraw) command_mutex.lock();
362
363   VectorHandle ref = osd->getDrawStyleHandle(c);
364   commands.push_back(SVGCommand::PaintPath(x, y, 1, 1, PIPoint, ref));
365
366   if (!fastdraw)  command_mutex.unlock();
367 }
368 void SurfaceVector::drawMonoBitmap(UCHAR* base, int dx, int dy, unsigned int height, unsigned int width, DrawStyle& nextColour)
369 {
370   command_mutex.lock();
371   ImageIndex image = osd->getMonoBitmapRef(base, width, height);
372   VectorHandle ref = osd->getDrawStyleHandle(nextColour);
373   removeCommands(dx, dy, width, height);
374   commands.push_back(SVGCommand::PaintImage(dx, dy, height, width, image, ref)); // FIXME BUG height and width wrong way around?
375   command_mutex.unlock();
376 }
377
378
379 int SurfaceVector::removeCommands(float x, float y, float width, float height)
380 {
381   // we iterate through all old commands in order to remove commands hidden by this rectangle
382   std::vector<SVGCommand>::iterator itty = commands.begin();
383   std::vector<SVGCommand>::iterator remstart;
384   bool remove = false;
385   float cx, cy, cw, ch;
386   cx = cy = 0.f;
387   cw = swidth;
388   ch = sheight;
389   bool clipping_erases = false;
390
391   while (itty != commands.end())
392   {
393     if ((clipping_erases // test if clipping helps
394          || (*itty).Test(x, y, width, height)  )
395         && (*itty).instr != DrawClipping)
396     {
397       //Log::getInstance()->log("OSD", Log::DEBUG, "Remove command %d %g %g %g %g %d %d",(*itty).instr,
398       //(*itty).x,(*itty).y,(*itty).w,(*itty).h,(*itty).handle,(*itty).target.image);
399       osd->decrementDrawStyleHandleRefCount((*itty).getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
400       ImageIndex ii = (*itty).getImageIndex();
401
402       if (ii) osd->removeImageRef(ii);
403
404       LoadIndex li = (*itty).getLoadIndex();
405
406       if (li) osd->removeLoadIndexRef(li);
407
408       if (!remove)
409       {
410         remstart = itty;
411         remove = true;
412       }
413     }
414     else
415     {
416       if ((*itty).instr == DrawClipping)
417       {
418         if ((*itty).w == 0.f && (*itty).h == 0.f)
419         {
420           cx = cy = 0.f;
421           cw = swidth;
422           ch = sheight;
423
424         }
425         else
426         {
427           cx = (*itty).x;
428           cy = (*itty).y;
429           cw = (*itty).w;
430           ch = (*itty).h;
431         }
432
433         clipping_erases = (cx >= x) && (cy >= y) && ((cx + cw) <= (x + width)) && ((cy + ch) <= (y + height));
434       }
435
436       if (remove)
437       {
438         itty = commands.erase(remstart, itty);
439         remove = false;
440       }
441     }
442
443     itty++;
444   }
445
446   if (remove)
447   {
448     itty = commands.erase(remstart, itty);
449   }
450
451   return 1;
452
453 }
454
455 int SurfaceVector::updateToScreen(int sx, int sy, int w, int h, int dx, int dy)
456 {
457   // ok this method really works in a pixel oriented way
458   command_mutex.lock();
459   osd->updateOrAddSurface(this, dx - sx, dy - sy, swidth, sheight, commands);
460   command_mutex.unlock();
461   return 1;
462 }
463
464
465 /* This is for systems which need a locking of the drawing surface to speed up drawing */
466 void SurfaceVector::startFastDraw()
467 {
468   command_mutex.lock();
469 }
470 void SurfaceVector::endFastDraw()
471 {
472   command_mutex.unlock();
473 }
474
475
476 void SurfaceVector::drawTTChar(int ox, int oy, int x, int y, cTeletextChar c)
477 {
478   command_mutex.lock();
479   std::vector<SVGCommand>::iterator itty = commands.begin();
480
481   while (itty != commands.end())
482   {
483     if ((*itty).TTTest(ox, oy, x, y) )
484     {
485       itty = commands.erase(itty);
486       break;
487     }
488     else
489     {
490       itty++;
491     }
492   }
493
494   commands.push_back(SVGCommand::PaintTTchar(ox, oy, x, y, c.getInternal()));
495   command_mutex.unlock();
496 }