]> git.vomp.tv Git - vompclient.git/blob - osdvector.h
Rewrite timers class using std::thread/mutex/cond/chrono
[vompclient.git] / osdvector.h
1 /*
2     Copyright 2004-2005 Chris Tallon, 2006,2011-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
21 #ifndef OSDVECTOR_H
22 #define OSDVECTOR_H
23 #include "osd.h"
24 #include "mutex.h"
25 #include "colour.h"
26 #include <set>
27 #include <list>
28 #include <vector>
29 #include <map>
30 #include <queue>
31 #include <string>
32 #include "tvmedia.h"
33 #include "vdr.h"
34
35 #include "teletextdecodervbiebu.h"
36
37 enum SVGCommandInstr {
38         DrawNoop,
39         DrawPath,
40         DrawGlyph,
41         DrawImage,
42         DrawTTchar,
43         DrawClipping,
44         DrawImageLoading
45 };
46 enum PathIndex {
47         PIHorzLine,
48         PIVertLine,
49         PIRectangle,
50         PIPoint
51 };
52
53 enum Corner{
54         TopLeft,
55         TopRight,
56         BottomLeft,
57         BottomRight,
58         TopMiddle,
59         BottomMiddle,
60         TopLeftLimited
61 };
62
63 typedef  VectorHandle ImageIndex;
64 typedef unsigned long long LoadIndex;
65
66 class SVGCommand
67 {
68 public:
69         SVGCommand()
70     {
71                 instr=DrawNoop;
72                 x=y=w=h=0;
73                 reference = VECTOR_HANDLE_INIT;
74     };
75
76         inline static SVGCommand PaintPath(float ix, float iy, float iw, float ih, PathIndex path, VectorHandle ref)
77         {
78                 SVGCommand nc;
79                 nc.instr=DrawPath;
80                 nc.x=ix;
81                 nc.y=iy;
82                 nc.w=iw;
83                 nc.h=ih;
84                 nc.target.path_index=path;
85                 nc.reference=ref;
86                 return nc;
87         };
88
89         inline static SVGCommand PaintImageLoading(LoadIndex load_in, float ix, float iy, float iw, float ih, VectorHandle ref, Corner corner = TopLeft)
90         {
91                 SVGCommand nc;
92                 nc.instr=DrawImageLoading;
93                 nc.x=ix;
94                 nc.y=iy;
95                 nc.w=iw;
96                 nc.h=ih;
97                 nc.target.loadindex=load_in;
98                 nc.reference=ref;
99                 nc.corner=corner;
100                 return nc;
101         };
102
103         inline static SVGCommand PaintImage(float ix, float iy, float iw, float ih, ImageIndex image_in, VectorHandle ref, Corner corner = TopLeft)
104         {
105                 SVGCommand nc;
106                 nc.instr=DrawImage;
107                 nc.x=ix;
108                 nc.y=iy;
109                 nc.w=iw;
110                 nc.h=ih;
111                 nc.target.image=image_in;
112                 nc.reference=ref;
113                 nc.corner=corner;
114                 return nc;
115         };
116
117
118
119         inline static SVGCommand PaintTTchar(float ix, float iy,float iw,float ih,unsigned int ttchar_in)
120         {
121                 SVGCommand nc;
122                 nc.instr=DrawTTchar;
123                 nc.x=ix;
124                 nc.y=iy;
125                 nc.w=iw;
126                 nc.h=ih;
127                 nc.reference=0;
128                 nc.target.ttchar=ttchar_in;
129                 nc.corner=TopLeft;
130                 return nc;
131         };
132         inline static SVGCommand PaintClipping(float ix, float iy,float iw,float ih)
133         {
134                 SVGCommand nc;
135                 nc.instr=DrawClipping;
136                 nc.x=ix;
137                 nc.y=iy;
138                 nc.w=iw;
139                 nc.h=ih;
140                 nc.reference=0;
141                 nc.target.ttchar=0;
142                 return nc;
143         };
144
145
146         inline static void PaintGlyph(SVGCommand& nc, float ix, float iy, wchar_t char_in, VectorHandle ref)
147         {
148                 nc.instr=DrawGlyph;
149                 nc.x=ix;
150                 nc.y=iy;
151                 nc.w=0;
152                 nc.h=0;
153                 nc.reference=ref;
154                 nc.target.textchar=char_in;
155         };
156
157         bool Test(float tx,float ty,float tw, float th)
158         {
159                 return (x>=tx) && (y>=ty) && ((x+w)<=(tx+tw)) && ((y+h)<=(ty+th));
160         }
161         bool TTTest(float tox,float toy,float tx, float ty)
162         {
163                 return (x==tox) && (toy==y) && (w==tx) && (h==ty);
164         }
165         bool Outside(float tx,float ty,float tw, float th)
166         {
167                 return ((x+w)<tx) || ((y+h)<ty) || ((tx+tw) < x) || ((ty+th) < y);
168         }
169
170         VectorHandle getRef(){ return reference; };
171         ImageIndex getImageIndex() {
172                 if (instr!=DrawImage) return 0;
173                 else return target.image;
174         };
175         LoadIndex getLoadIndex() {
176                 if (instr!=DrawImageLoading) return 0;
177                 else return target.loadindex;
178         };
179
180         SVGCommandInstr instr;
181         Corner corner;
182         float x,y,w,h;
183         VectorHandle reference;
184         union {
185                 PathIndex path_index; //path_index
186                 wchar_t textchar;
187                 ImageIndex image;
188                 unsigned int ttchar;
189                 LoadIndex loadindex;
190         } target;
191
192
193 };
194
195 class SurfaceVector;
196 class VDR_ResponsePacket;
197
198 struct SurfaceCommands{
199         const SurfaceVector* surf;
200         std::vector<SVGCommand> commands;
201         float x,y,w,h;
202 };
203
204
205 class OsdVector : public Osd
206 {
207   public:
208     OsdVector();
209     virtual ~OsdVector();
210
211
212     int restore();
213
214     int getFD();
215
216     void screenShot(const char* fileName);
217     virtual bool screenShot(void *buffer, int width, int height, bool osd /*include osd*/)=0;
218
219     Surface * createNewSurface();
220
221         void Blank();
222         virtual void updateBackgroundColor(DrawStyle /* bg */) {};
223
224         void updateOrAddSurface(const SurfaceVector *surf,float x,float y,float height,float width,
225                         std::vector<SVGCommand>& commands);
226         void removeSurface(const SurfaceVector *surf);
227
228         virtual float getFontHeight()=0;
229         virtual float getCharWidth(wchar_t c)=0;
230         float *getCharWidthArray() {return byte_char_width;};
231
232         //virtual ImageIndex getJpegRef(const char* fileName, int *width,int *height);
233         virtual LoadIndex getTVMediaRef(TVMediaInfo& tvmedia,ImageIndex& image);
234         virtual ImageIndex getMonoBitmapRef(void *base,int width,int height);
235         virtual ImageIndex getImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data);
236
237
238         virtual bool getStaticImageData(unsigned int static_id, UCHAR **userdata, ULONG *length)=0;
239
240         void removeImageRef(const ImageIndex ref);
241         void removeLoadIndexRef(const LoadIndex ref);
242         VectorHandle getStyleRef(const DrawStyle &c);
243         virtual void removeStyleRef(VectorHandle ref);
244         virtual void getScreenSize(int &width, int &height)=0;
245         virtual void getRealScreenSize(int &width, int &height)=0;
246
247         // should be only called from command thread
248         void informPicture(LoadIndex index, ImageIndex i_index);
249
250
251
252
253     int charSet() {return 2;}; //UTF-8
254
255
256     class PictureDecoder;
257     struct PictureInfo
258     {
259         enum PictType {
260                 RGBAMemBlock,
261                 EGLImage,
262                         D2DBitmap
263         };
264         PictType type;
265         ULONG width;
266         ULONG height;
267         LoadIndex lindex;
268     union {
269         const void * image;
270         unsigned int handle;
271     };
272         void *reference;
273         PictureDecoder* decoder;
274     };
275
276
277     class PictureReader;
278
279     class PictureDecoder
280     {
281     public:
282         PictureDecoder(PictureReader * treader) {reader=treader;};
283         virtual ~PictureDecoder() {};
284
285         // its is always guaranted, that after getDecodedPicture a call to decodePicture follows, if the return value was true;
286         virtual unsigned char * decodePicture(LoadIndex index, unsigned char * buffer, unsigned int length, bool freemem=true)=0;
287
288         virtual bool getDecodedPicture(struct PictureInfo& pict_inf)=0;
289         virtual void freeReference(void * ref)=0;
290
291         virtual void init() {};
292         virtual void shutdown() {};
293
294     protected:
295         PictureReader * reader;
296     };
297
298     class PictureReader: public Thread_TYPE {
299     public:
300
301         ~PictureReader();
302
303         void init();
304         void addDecoder(PictureDecoder*);
305         void removeDecoder(PictureDecoder*);
306
307         void shutdown();
308
309
310         bool processReceivedPictures();
311
312         // should be called from command thread
313         void receivePicture(VDR_ResponsePacket *vresp);
314
315         void addStaticImage(unsigned int id);
316
317         void invalidateLoadIndex(LoadIndex index);
318
319         void informFallback(LoadIndex index, int fallback);
320
321
322
323
324     protected:
325
326         void threadMethod();
327
328         Mutex pict_lock_incoming; //locks
329         Mutex decoders_lock;
330         std::queue<VDR_ResponsePacket*> pict_incoming;
331         std::queue<unsigned int> pict_incoming_static;
332         std::list<PictureDecoder*> decoders;
333         std::map<LoadIndex,int> inform_fallback;
334         std::set<LoadIndex> invalid_loadindex;
335
336         bool picture_update;
337
338     };
339
340     PictureReader *getPictReader() { return &reader;};
341
342
343
344 protected:
345
346     PictureReader reader;
347
348         void incImageRef(ImageIndex index);
349         int getImageRef(ImageIndex index);
350         virtual void destroyImageRef(ImageIndex index)=0;
351         void incLoadIndexRef(LoadIndex index);
352         int getLoadIndexRef(LoadIndex index);
353
354
355         //virtual ImageIndex createJpeg(const char* fileName, int *width,int *height)=0;
356         virtual ImageIndex createMonoBitmap(void *base,int width,int height)=0;
357         virtual ImageIndex createImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data)=0;
358         virtual void createPicture(struct PictureInfo& pict_inf)=0;
359
360         virtual LoadIndex loadTVMedia(TVMediaInfo& tvmedia);
361
362
363
364         std::map<ImageIndex,int> images_ref;
365         std::map<void *,ImageIndex> monobitmaps;
366         //map<string,ImageIndex> jpegs;
367         std::map<TVMediaInfo,ImageIndex> tvmedias;
368         std::list<ImageIndex> palettepics;
369
370
371
372         std::map<LoadIndex,int> loadindex_ref;
373         std::map<TVMediaInfo,LoadIndex> tvmedias_load;
374         std::map<LoadIndex,TVMediaInfo> tvmedias_load_inv;
375         std::map<LoadIndex,ImageIndex> tvmedias_loaded;
376
377
378
379         void incStyleRef(VectorHandle index);
380         int getStyleRef(VectorHandle index);
381         virtual void destroyStyleRef(VectorHandle index) = 0;
382
383
384         std::map<DrawStyle, VectorHandle> styles;
385         std::map<VectorHandle, int> styles_ref;
386         std::map<DrawStyle, VectorHandle>::iterator styles_lastit;
387         bool styles_lastit_valid;
388         std::map<VectorHandle, int>::iterator styles_ref_lastit;
389         bool styles_ref_lastit_valid;
390
391         virtual VectorHandle createStyleRef(const DrawStyle &c) = 0;
392
393         void dereferenceSVGCommand(std::vector<SVGCommand>& commands );
394         void referenceSVGCommand(std::vector<SVGCommand>& commands );
395         void cleanupOrphanedRefs();
396
397
398
399         virtual void drawSetTrans(SurfaceCommands & sc)=0;
400         virtual void executeDrawCommand(SVGCommand & command)=0;
401
402
403         std::list<SurfaceCommands> scommands;
404
405         Mutex surfaces_mutex;
406
407         float byte_char_width[256];
408
409         void drawSurfaces();
410 };
411
412 #endif