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 "imageloader.h"
27 #include "surfacevector.h"
29 const static char* TAG = "SurfaceVector";
31 SurfaceVector::SurfaceVector(OsdVector* vosd)
33 logger = LogNT::getInstance();
35 imageLoader = ImageLoader::getInstance();
36 commands.reserve(2048);
39 SurfaceVector::~SurfaceVector()
41 osd->removeSurface(this);
43 for (SVGCommand& command : commands)
45 osd->decrementDrawStyleHandleRefCount(command.getHandle()); // We remove the Style reference, so that osd can free stuff // FIXME BUG BUG BUG
47 VectorHandleImage handle = command.getImageHandle();
48 if (handle) osd->removeImageRef(handle);
52 int SurfaceVector::getFontHeight()
54 return (int)osd->getFontHeight();
57 float SurfaceVector::getCharWidth(wchar_t c)
59 return osd->getCharWidth(c);
63 * By current design it's not permitted to have a surface without the first drawing being to
64 * fill with a background colour. The fillblt calls removeCommands which deletes all the old
65 * commands before the background colour becomes the new first command, then all the rest are
66 * redrawn on top. If you want to change this in future then do this as the first thing in
69 * and have this function in here:
70 * void SurfaceVector::clear()
72 * removeCommands(0, 0, swidth, sheight);
74 * Or preferably a simpler one that doesn't do all the comparison work.
77 int SurfaceVector::drawText(const char* text, int x, int y, const DrawStyle& c)
79 return drawText(text, x, y, 0, c);
82 int SurfaceVector::drawText(const char* text, int x, int y, int width, const DrawStyle& c)
85 const char* run = text;
86 size_t length = strlen(text);
92 float* charwidtharray = osd->getCharWidthArray();
93 int commands_size = commands.size();
95 commands.resize(commands_size + strlen(text));
100 memset((void*)&state, 0, sizeof(state));
101 num_bytes = mbrtowc(&tempo, run, length, &state);
103 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
105 ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
106 SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, tempo, ref);
109 float cur_shift = charwidtharray[tempo & 0xFF];
111 if (tempo & 0xFFFFFF00) cur_shift = osd->getCharWidth(tempo);
117 if (shift > width && width > 0)
119 command_mutex.unlock();
123 num_bytes = mbrtowc(&tempo, run, length, &state);
127 wchar_t* temptext = new wchar_t[length + 1];
128 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
129 temptext, length + 1) - 1;
131 for (int i = 0; i < real_length; i++)
133 ref = osd->getDrawStyleHandle(c); // Need to call this each time to have OSD get the ref count right. Maybe expose incRefCount sometime
134 SVGCommand::PaintGlyph(commands[commands_size + chars], x + shift, y, temptext[i], ref);
137 float cur_shift = charwidtharray[temptext[i] & 0xFF];
139 if (temptext[i] && 0xFFFFFF00) cur_shift = osd->getCharWidth(temptext[i]);
147 commands.resize(commands_size + chars);
148 command_mutex.unlock();
152 int SurfaceVector::drawTextRJ(const char* text, int x, int y, const DrawStyle& c)
155 const char* run = text;
156 size_t length = strlen(text);
161 size_t num_bytes = 1;
162 memset((void*)&state, 0, sizeof(state));
163 num_bytes = mbrtowc(tempo, run, length, &state);
165 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
167 shift += osd->getCharWidth(*tempo);
170 num_bytes = mbrtowc(tempo, run, length, &state);
174 wchar_t* temptext = new wchar_t[length + 1];
175 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
176 temptext, length + 1) - 1;
178 for (int i = 0; i < real_length; i++)
180 shift += osd->getCharWidth(temptext[i]);
185 return drawText(text, (int)(x - shift), y, c);
188 int SurfaceVector::drawTextCentre(const char* text, int x, int y, const DrawStyle& c)
191 const char* run = text;
192 size_t length = strlen(text);
197 size_t num_bytes = 1;
198 memset((void*)&state, 0, sizeof(state));
199 num_bytes = mbrtowc(tempo, run, length, &state);
201 while (num_bytes != ((size_t) -1) && num_bytes != ((size_t) -2) && length > 0)
203 shift += osd->getCharWidth(*tempo);
206 num_bytes = mbrtowc(tempo, run, length, &state);
210 wchar_t* temptext = new wchar_t[length + 1];
211 int real_length = MultiByteToWideChar(CP_UTF8, 0, text, -1,
212 temptext, length + 1) - 1;
214 for (int i = 0; i < real_length; i++)
216 shift += osd->getCharWidth(temptext[i]);
222 return drawText(text, (int)(x - shift / 2.), y, c);
225 void SurfaceVector::drawJpeg(const char* fileName, int x, int y, int* width, int* height)
227 StaticArtwork index = sa_MAX; // This is for compatibility only
229 if (strcmp(fileName, "/vdr.jpg") == 0)
232 *height = 100; // this is faked so that the system does use the old coordinate system
233 *width = (int)ceil(190.f * osd->getPixelAspect());
235 else if (strcmp(fileName, "/wallpaperPAL.jpg") == 0)
237 index = sa_wallpaper;
238 *width = 720; // this is faked so that the system does use the old coordinate system
244 ImageStatic image = imageLoader->createStatic(index);
245 Image toDraw = image;
246 drawImage(toDraw, x, y, *width, *height, TopLeft);
251 void SurfaceVector::drawJpeg(const char *fileName,int x, int y,int *width, int *height)
253 command_mutex.lock();
254 VectorHandleImage handle=osd->getJpegRef(fileName,width,height);
255 commands.push_back(SVGCommand::PaintImage(x,y,*width,*height,image));
256 command_mutex.unlock();
260 void SurfaceVector::drawImage(Image& image, float x, float y, float width, float height, Corner corner)
262 command_mutex.lock();
264 if (width != 0 && height != 0)
266 removeCommands(x, y, width, height);
269 if (image->isReady())
271 logger->debug(TAG, "drawImage: READY, Add instr PaintImage for {}", static_cast<void*>(image.get()));
272 commands.push_back(SVGCommand::PaintImage(x, y, width, height, image, corner));
276 logger->debug(TAG, "drawImage: NOT_READY, Add instr PaintImageLoading {} LI {}", static_cast<void*>(image.get()), reinterpret_cast<void*>(image.get()));
277 commands.push_back(SVGCommand::PaintImageLoading(image, x, y, width, height, corner));
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(u4 width, u4 height)
296 void SurfaceVector::display()
298 //nothing this is really mvp specific // FIXME remove? -- yes, only surfacedirectfb has some commented out code in display()
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 (u4 j = 0; j < bm.getHeight(); ++j){
334 for (u4 i = 0; i < bm.getWidth(); ++i)
336 data[i+j*bm.getHeight()]=bm.getColour(i,j);
339 VectorHandleImage handle = 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::PaintBitmap(tx, ty, tw, th, handle, 0);
354 removeCommands(tx, ty, tw, th);
355 commands.push_back(temp);
356 command_mutex.unlock();
359 void SurfaceVector::drawPoint(int x, int y, const 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();
369 void SurfaceVector::drawMonoBitmap(u1* base, int dx, int dy, unsigned int height, unsigned int width, const DrawStyle& nextColour)
371 command_mutex.lock();
372 VectorHandleImage handle = osd->getMonoBitmapRef(base, width, height);
373 VectorHandle ref = osd->getDrawStyleHandle(nextColour);
374 removeCommands(dx, dy, width, height);
375 commands.push_back(SVGCommand::PaintBitmap(dx, dy, height, width, handle, ref)); // FIXME BUG height and width wrong way around?
376 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
401 VectorHandleImage handle = (*itty).getImageHandle();
402 if (handle) osd->removeImageRef(handle);
412 if ((*itty).instr == DrawClipping)
414 if ((*itty).w == 0.f && (*itty).h == 0.f)
429 clipping_erases = (cx >= x) && (cy >= y) && ((cx + cw) <= (x + width)) && ((cy + ch) <= (y + height));
434 itty = commands.erase(remstart, itty);
444 itty = commands.erase(remstart, itty);
451 int SurfaceVector::updateToScreen(int sx, int sy, int /*w*/, int /*h*/, int dx, int dy)
453 // ok this method really works in a pixel oriented way
454 command_mutex.lock();
455 osd->updateOrAddSurface(this, dx - sx, dy - sy, swidth, sheight, commands);
456 command_mutex.unlock();
461 /* This is for systems which need a locking of the drawing surface to speed up drawing */
462 void SurfaceVector::startFastDraw()
464 command_mutex.lock();
466 void SurfaceVector::endFastDraw()
468 command_mutex.unlock();
472 void SurfaceVector::drawTTChar(int ox, int oy, int x, int y, cTeletextChar c)
474 command_mutex.lock();
475 std::vector<SVGCommand>::iterator itty = commands.begin();
477 while (itty != commands.end())
479 if ((*itty).TTTest(ox, oy, x, y) )
481 itty = commands.erase(itty);
490 commands.push_back(SVGCommand::PaintTTchar(ox, oy, x, y, c.getInternal()));
491 command_mutex.unlock();