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