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()
35 osd->removeSurface(this);
37 for (SVGCommand& command : commands)
39 osd->decrementDrawStyleHandleRefCount(command.getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
41 ImageIndex ii = command.getImageIndex();
42 if (ii) osd->removeImageRef(ii);
44 LoadIndex li = command.getLoadIndex();
45 if (li) osd->removeLoadIndexRef(li);
49 int SurfaceVector::getFontHeight()
51 return (int)osd->getFontHeight();
54 float SurfaceVector::getCharWidth(wchar_t c)
56 return osd->getCharWidth(c);
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
66 * and have this function in here:
67 * void SurfaceVector::clear()
69 * removeCommands(0, 0, swidth, sheight);
71 * Or preferably a simpler one that doesn't do all the comparison work.
74 int SurfaceVector::drawText(const char* text, int x, int y, const DrawStyle& c)
76 return drawText(text, x, y, 0, c);
79 int SurfaceVector::drawText(const char* text, int x, int y, int width, const DrawStyle& c)
82 const char* run = text;
83 size_t length = strlen(text);
89 float* charwidtharray = osd->getCharWidthArray();
90 int commands_size = commands.size();
92 commands.resize(commands_size + strlen(text));
97 memset((void*)&state, 0, sizeof(state));
98 num_bytes = mbrtowc(&tempo, run, length, &state);
100 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
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);
106 float cur_shift = charwidtharray[tempo & 0xFF];
108 if (tempo && 0xFFFFFF00) cur_shift = osd->getCharWidth(tempo);
114 if (shift > width && width > 0)
116 command_mutex.unlock();
120 num_bytes = mbrtowc(&tempo, run, length, &state);
124 wchar_t* temptext = new wchar_t[length + 1];
125 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
126 temptext, length + 1) - 1;
128 for (int i = 0; i < real_length; i++)
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);
134 float cur_shift = charwidtharray[temptext[i] & 0xFF];
136 if (temptext[i] && 0xFFFFFF00) cur_shift = osd->getCharWidth(temptext[i]);
144 commands.resize(commands_size + chars);
145 command_mutex.unlock();
149 int SurfaceVector::drawTextRJ(const char* text, int x, int y, const DrawStyle& c)
152 const char* run = text;
153 size_t length = strlen(text);
158 size_t num_bytes = 1;
159 memset((void*)&state, 0, sizeof(state));
160 num_bytes = mbrtowc(tempo, run, length, &state);
162 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
164 shift += osd->getCharWidth(*tempo);
167 num_bytes = mbrtowc(tempo, run, length, &state);
171 wchar_t* temptext = new wchar_t[length + 1];
172 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
173 temptext, length + 1) - 1;
175 for (int i = 0; i < real_length; i++)
177 shift += osd->getCharWidth(temptext[i]);
182 return drawText(text, (int)(x - shift), y, c);
185 int SurfaceVector::drawTextCentre(const char* text, int x, int y, const DrawStyle& c)
188 const char* run = text;
189 size_t length = strlen(text);
194 size_t num_bytes = 1;
195 memset((void*)&state, 0, sizeof(state));
196 num_bytes = mbrtowc(tempo, run, length, &state);
198 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
200 shift += osd->getCharWidth(*tempo);
203 num_bytes = mbrtowc(tempo, run, length, &state);
207 wchar_t* temptext = new wchar_t[length + 1];
208 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
209 temptext, length + 1) - 1;
211 for (int i = 0; i < real_length; i++)
213 shift += osd->getCharWidth(temptext[i]);
219 return drawText(text, (int)(x - shift / 2.), y, c);
222 void SurfaceVector::drawJpeg(const char* fileName, int x, int y, int* width, int* height)
224 StaticArtwork index = sa_MAX; // This is for compatibility only
226 if (strcmp(fileName, "/vdr.jpg") == 0)
229 *height = 100; // this is faked so that the system does use the old coordinate system
230 *width = (int)ceil(190.f * osd->getPixelAspect());
232 else if (strcmp(fileName, "/wallpaperPAL.jpg") == 0)
234 index = sa_wallpaper;
235 *width = 720; // this is faked so that the system does use the old coordinate system
242 info.setStaticArtwork(index);
243 drawTVMedia(info, x, y, *width, *height, TopLeft);
248 void SurfaceVector::drawJpeg(const char *fileName,int x, int y,int *width, int *height)
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();
257 void SurfaceVector::drawTVMedia(TVMediaInfo& tvmedia, float x, float y, float width, float height, Corner corner)
259 command_mutex.lock();
260 ImageIndex image = 0;
261 LoadIndex load_index = osd->getTVMediaRef(tvmedia, image);
263 if (width != 0 && height != 0)
265 removeCommands(x, y, width, height);
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));
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);
280 command_mutex.unlock();
283 void SurfaceVector::drawClippingRectangle(float x, float y, float w, float h)
285 command_mutex.lock();
286 commands.push_back(SVGCommand::PaintClipping((float)x, (float)y, (float)w, (float)h));
287 command_mutex.unlock();
290 int SurfaceVector::create(UINT width, UINT height)
296 void SurfaceVector::display()
298 //nothing this is really mvp specific // FIXME remove?
301 int SurfaceVector::fillblt(int x, int y, int width, int height, const DrawStyle& c)
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();
311 void SurfaceVector::drawHorzLine(int x1, int x2, int y, const DrawStyle& c)
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();
319 void SurfaceVector::drawVertLine(int x, int y1, int y2, const DrawStyle& c)
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();
327 void SurfaceVector::drawBitmap(int x, int y, const Bitmap& bm, const DisplayRegion& region)
329 //this is complicated
330 command_mutex.lock();
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)
336 data[i+j*bm.getHeight()]=bm.getColour(i,j);
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
342 float tx = x + region.windowx;
343 float ty = y + region.windowy;
344 float th = bm.getHeight();
345 float tw = bm.getWidth();
347 float scalex = 720.f / ((float) (region.framewidth + 1));
348 float scaley = 576.f / ((float) (region.frameheight + 1));
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();
359 void SurfaceVector::drawPoint(int x, int y, DrawStyle& c, bool fastdraw)
361 if (!fastdraw) command_mutex.lock();
363 VectorHandle ref = osd->getDrawStyleHandle(c);
364 commands.push_back(SVGCommand::PaintPath(x, y, 1, 1, PIPoint, ref));
366 if (!fastdraw) command_mutex.unlock();
368 void SurfaceVector::drawMonoBitmap(UCHAR* base, int dx, int dy, unsigned int height, unsigned int width, DrawStyle& nextColour)
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();
379 int SurfaceVector::removeCommands(float x, float y, float width, float height)
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;
385 float cx, cy, cw, ch;
389 bool clipping_erases = false;
391 while (itty != commands.end())
393 if ((clipping_erases // test if clipping helps
394 || (*itty).Test(x, y, width, height) )
395 && (*itty).instr != DrawClipping)
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();
402 if (ii) osd->removeImageRef(ii);
404 LoadIndex li = (*itty).getLoadIndex();
406 if (li) osd->removeLoadIndexRef(li);
416 if ((*itty).instr == DrawClipping)
418 if ((*itty).w == 0.f && (*itty).h == 0.f)
433 clipping_erases = (cx >= x) && (cy >= y) && ((cx + cw) <= (x + width)) && ((cy + ch) <= (y + height));
438 itty = commands.erase(remstart, itty);
448 itty = commands.erase(remstart, itty);
455 int SurfaceVector::updateToScreen(int sx, int sy, int w, int h, int dx, int dy)
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();
465 /* This is for systems which need a locking of the drawing surface to speed up drawing */
466 void SurfaceVector::startFastDraw()
468 command_mutex.lock();
470 void SurfaceVector::endFastDraw()
472 command_mutex.unlock();
476 void SurfaceVector::drawTTChar(int ox, int oy, int x, int y, cTeletextChar c)
478 command_mutex.lock();
479 std::vector<SVGCommand>::iterator itty = commands.begin();
481 while (itty != commands.end())
483 if ((*itty).TTTest(ox, oy, x, y) )
485 itty = commands.erase(itty);
494 commands.push_back(SVGCommand::PaintTTchar(ox, oy, x, y, c.getInternal()));
495 command_mutex.unlock();