2 Copyright 2012 Marten Richter
4 This file is part of VOMP.
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.
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.
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/>.
24 #include "staticartwork.h"
25 #include "surfacevector.h"
27 SurfaceVector::SurfaceVector(OsdVector* vosd)
30 commands.reserve(2048);
33 SurfaceVector::~SurfaceVector()
36 osd->removeSurface(this);
38 for (SVGCommand& command : commands)
40 osd->decrementDrawStyleHandleRefCount(command.getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
42 ImageIndex ii = command.getImageIndex();
43 if (ii) osd->removeImageRef(ii);
45 LoadIndex li = command.getLoadIndex();
46 if (li) osd->removeLoadIndexRef(li);
50 int SurfaceVector::getFontHeight()
52 return (int)osd->getFontHeight();
55 float SurfaceVector::getCharWidth(wchar_t c)
57 return osd->getCharWidth(c);
61 * By current design it's not permitted to have a surface without the first drawing being to
62 * fill with a background colour. The fillblt calls removeCommands which deletes all the old
63 * commands before the background colour becomes the new first command, then all the rest are
64 * redrawn on top. If you want to change this in future then do this as the first thing in
67 * and have this function in here:
68 * void SurfaceVector::clear()
70 * removeCommands(0, 0, swidth, sheight);
72 * Or preferably a simpler one that doesn't do all the comparison work.
75 int SurfaceVector::drawText(const char* text, int x, int y, const DrawStyle& c)
77 return drawText(text, x, y, 0, c);
80 int SurfaceVector::drawText(const char* text, int x, int y, int width, const DrawStyle& c)
83 const char* run = text;
84 size_t length = strlen(text);
90 float* charwidtharray = osd->getCharWidthArray();
91 int commands_size = commands.size();
93 commands.resize(commands_size + strlen(text));
98 memset((void*)&state, 0, sizeof(state));
99 num_bytes = mbrtowc(&tempo, run, length, &state);
101 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
103 ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
104 SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, tempo, ref);
107 float cur_shift = charwidtharray[tempo & 0xFF];
109 if (tempo && 0xFFFFFF00) cur_shift = osd->getCharWidth(tempo);
115 if (shift > width && width > 0)
117 command_mutex.unlock();
121 num_bytes = mbrtowc(&tempo, run, length, &state);
125 wchar_t* temptext = new wchar_t[length + 1];
126 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
127 temptext, length + 1) - 1;
129 for (int i = 0; i < real_length; i++)
131 ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
132 SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, temptext[i], ref);
135 float cur_shift = charwidtharray[temptext[i] & 0xFF];
137 if (temptext[i] && 0xFFFFFF00) cur_shift = osd->getCharWidth(temptext[i]);
145 commands.resize(commands_size + chars);
146 command_mutex.unlock();
150 int SurfaceVector::drawTextRJ(const char* text, int x, int y, const DrawStyle& c)
153 const char* run = text;
154 size_t length = strlen(text);
159 size_t num_bytes = 1;
160 memset((void*)&state, 0, sizeof(state));
161 num_bytes = mbrtowc(tempo, run, length, &state);
163 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
165 shift += osd->getCharWidth(*tempo);
168 num_bytes = mbrtowc(tempo, run, length, &state);
172 wchar_t* temptext = new wchar_t[length + 1];
173 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
174 temptext, length + 1) - 1;
176 for (int i = 0; i < real_length; i++)
178 shift += osd->getCharWidth(temptext[i]);
183 return drawText(text, (int)(x - shift), y, c);
186 int SurfaceVector::drawTextCentre(const char* text, int x, int y, const DrawStyle& c)
189 const char* run = text;
190 size_t length = strlen(text);
195 size_t num_bytes = 1;
196 memset((void*)&state, 0, sizeof(state));
197 num_bytes = mbrtowc(tempo, run, length, &state);
199 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
201 shift += osd->getCharWidth(*tempo);
204 num_bytes = mbrtowc(tempo, run, length, &state);
208 wchar_t* temptext = new wchar_t[length + 1];
209 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
210 temptext, length + 1) - 1;
212 for (int i = 0; i < real_length; i++)
214 shift += osd->getCharWidth(temptext[i]);
220 return drawText(text, (int)(x - shift / 2.), y, c);
223 void SurfaceVector::drawJpeg(const char* fileName, int x, int y, int* width, int* height)
225 StaticArtwork index = sa_MAX; // This is for compatibility only
227 if (strcmp(fileName, "/vdr.jpg") == 0)
230 *height = 100; // this is faked so that the system does use the old coordinate system
231 *width = (int)ceil(190.f * osd->getPixelAspect());
233 else if (strcmp(fileName, "/wallpaperPAL.jpg") == 0)
235 index = sa_wallpaper;
236 *width = 720; // this is faked so that the system does use the old coordinate system
243 info.setStaticArtwork(index);
244 drawTVMedia(info, x, y, *width, *height, TopLeft);
249 void SurfaceVector::drawJpeg(const char *fileName,int x, int y,int *width, int *height)
251 command_mutex.lock();
252 ImageIndex image=osd->getJpegRef(fileName,width,height);
253 commands.push_back(SVGCommand::PaintImage(x,y,*width,*height,image,0));
254 command_mutex.unlock();
258 void SurfaceVector::drawTVMedia(TVMediaInfo& tvmedia, float x, float y, float width, float height, Corner corner)
260 command_mutex.lock();
261 ImageIndex image = 0;
262 LoadIndex load_index = osd->getTVMediaRef(tvmedia, image);
264 if (width != 0 && height != 0)
266 removeCommands(x, y, width, height);
271 //Log::getInstance()->log("SurfaceVector", Log::DEBUG, "TVMedia Add instru image %d %d", load_index,image);
272 commands.push_back(SVGCommand::PaintImage(x, y, width, height, image, 0, corner));
277 commands.push_back(SVGCommand::PaintImageLoading(load_index, x, y, width, height, corner));
278 //Log::getInstance()->log("SurfaceVector", Log::DEBUG, "TVMedia Add instru image loading %d %d", load_index,image);
281 command_mutex.unlock();
284 void SurfaceVector::drawClippingRectangle(float x, float y, float w, float h)
286 command_mutex.lock();
287 commands.push_back(SVGCommand::PaintClipping((float)x, (float)y, (float)w, (float)h));
288 command_mutex.unlock();
291 int SurfaceVector::create(UINT width, UINT height)
297 void SurfaceVector::display()
299 //nothing this is really mvp specific // FIXME remove?
302 int SurfaceVector::fillblt(int x, int y, int width, int height, const DrawStyle& c)
304 command_mutex.lock();
305 removeCommands(x, y, width, height); // remove commands below the box
306 VectorHandle ref = osd->getDrawStyleHandle(c);
307 commands.push_back(SVGCommand::PaintPath(x, y, width, height, PIRectangle, ref));
308 command_mutex.unlock();
312 void SurfaceVector::drawHorzLine(int x1, int x2, int y, const DrawStyle& c)
314 command_mutex.lock();
315 VectorHandle ref = osd->getDrawStyleHandle(c);
316 commands.push_back(SVGCommand::PaintPath(x1, y, x2 - x1, 1, PIHorzLine, ref));
317 command_mutex.unlock();
320 void SurfaceVector::drawVertLine(int x, int y1, int y2, const DrawStyle& c)
322 command_mutex.lock();
323 VectorHandle ref = osd->getDrawStyleHandle(c);
324 commands.push_back(SVGCommand::PaintPath(x, y1, 1, y2 - y1, PIVertLine, ref));
325 command_mutex.unlock();
328 void SurfaceVector::drawBitmap(int x, int y, const Bitmap& bm, const DisplayRegion& region)
330 //this is complicated
331 command_mutex.lock();
333 unsigned int * data=(unsigned int*)malloc(sizeof(unsigned int)*bm.getWidth()*bm.getHeight());
334 for (UINT j = 0; j < bm.getHeight(); ++j){
335 for (UINT i = 0; i < bm.getWidth(); ++i)
337 data[i+j*bm.getHeight()]=bm.getColour(i,j);
340 ImageIndex image = osd->getImagePalette(bm.getWidth(), bm.getHeight(), &(bm.rawData()[0]),
341 (const unsigned int*)&bm.palette.getColourVector()[0]); // data is freed by the OSD
343 float tx = x + region.windowx;
344 float ty = y + region.windowy;
345 float th = bm.getHeight();
346 float tw = bm.getWidth();
348 float scalex = 720.f / ((float) (region.framewidth + 1));
349 float scaley = 576.f / ((float) (region.frameheight + 1));
354 SVGCommand temp = SVGCommand::PaintImage(tx, ty, tw, th, image, 0);
355 removeCommands(tx, ty, tw, th);
356 commands.push_back(temp);
357 command_mutex.unlock();
360 void SurfaceVector::drawPoint(int x, int y, const DrawStyle& c, bool fastdraw)
362 if (!fastdraw) command_mutex.lock();
364 VectorHandle ref = osd->getDrawStyleHandle(c);
365 commands.push_back(SVGCommand::PaintPath(x, y, 1, 1, PIPoint, ref));
367 if (!fastdraw) command_mutex.unlock();
370 void SurfaceVector::drawMonoBitmap(UCHAR* base, int dx, int dy, unsigned int height, unsigned int width, const DrawStyle& nextColour)
372 command_mutex.lock();
373 ImageIndex image = osd->getMonoBitmapRef(base, width, height);
374 VectorHandle ref = osd->getDrawStyleHandle(nextColour);
375 removeCommands(dx, dy, width, height);
376 commands.push_back(SVGCommand::PaintImage(dx, dy, height, width, image, ref)); // FIXME BUG height and width wrong way around?
377 command_mutex.unlock();
380 int SurfaceVector::removeCommands(float x, float y, float width, float height)
382 // we iterate through all old commands in order to remove commands hidden by this rectangle
383 std::vector<SVGCommand>::iterator itty = commands.begin();
384 std::vector<SVGCommand>::iterator remstart;
386 float cx, cy, cw, ch;
390 bool clipping_erases = false;
392 while (itty != commands.end())
394 if ((clipping_erases // test if clipping helps
395 || (*itty).Test(x, y, width, height) )
396 && (*itty).instr != DrawClipping)
398 //Log::getInstance()->log("OSD", Log::DEBUG, "Remove command %d %g %g %g %g %d %d",(*itty).instr,
399 //(*itty).x,(*itty).y,(*itty).w,(*itty).h,(*itty).handle,(*itty).target.image);
400 osd->decrementDrawStyleHandleRefCount((*itty).getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
401 ImageIndex ii = (*itty).getImageIndex();
403 if (ii) osd->removeImageRef(ii);
405 LoadIndex li = (*itty).getLoadIndex();
407 if (li) osd->removeLoadIndexRef(li);
417 if ((*itty).instr == DrawClipping)
419 if ((*itty).w == 0.f && (*itty).h == 0.f)
434 clipping_erases = (cx >= x) && (cy >= y) && ((cx + cw) <= (x + width)) && ((cy + ch) <= (y + height));
439 itty = commands.erase(remstart, itty);
449 itty = commands.erase(remstart, itty);
456 int SurfaceVector::updateToScreen(int sx, int sy, int w, int h, int dx, int dy)
458 // ok this method really works in a pixel oriented way
459 command_mutex.lock();
460 osd->updateOrAddSurface(this, dx - sx, dy - sy, swidth, sheight, commands);
461 command_mutex.unlock();
466 /* This is for systems which need a locking of the drawing surface to speed up drawing */
467 void SurfaceVector::startFastDraw()
469 command_mutex.lock();
471 void SurfaceVector::endFastDraw()
473 command_mutex.unlock();
477 void SurfaceVector::drawTTChar(int ox, int oy, int x, int y, cTeletextChar c)
479 command_mutex.lock();
480 std::vector<SVGCommand>::iterator itty = commands.begin();
482 while (itty != commands.end())
484 if ((*itty).TTTest(ox, oy, x, y) )
486 itty = commands.erase(itty);
495 commands.push_back(SVGCommand::PaintTTchar(ox, oy, x, y, c.getInternal()));
496 command_mutex.unlock();