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