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