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