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