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