]> git.vomp.tv Git - vompclient.git/blob - osdvector.h
Replace TVMedia & OsdVector refcounting with new Image system
[vompclient.git] / osdvector.h
1 /*
2     Copyright 2004-2005,2021 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 "osdvectortypes.h"
35 #include "osd.h"
36 #include "colour.h"
37 #include "image.h"
38 #include "imageloader.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   DrawBitmap
53 };
54
55 enum PathIndex
56 {
57   PIHorzLine,
58   PIVertLine,
59   PIRectangle,
60   PIPoint
61 };
62
63 class SVGCommand
64 {
65   public:
66     inline static SVGCommand PaintPath(float ix, float iy, float iw, float ih, PathIndex path, VectorHandle ref)
67     {
68       SVGCommand nc;
69       nc.instr = DrawPath;
70       nc.x = ix;
71       nc.y = iy;
72       nc.w = iw;
73       nc.h = ih;
74       nc.target.path_index = path;
75       nc.handle = ref; // always a valid DrawStyle handle
76       return nc;
77     };
78
79     inline static SVGCommand PaintImageLoading(Image& image, float ix, float iy, float iw, float ih, Corner corner = TopLeft)
80     {
81       SVGCommand nc;
82       nc.instr = DrawImageLoading;
83       nc.x = ix;
84       nc.y = iy;
85       nc.w = iw;
86       nc.h = ih;
87       nc.handle = 0; // not valid for PaintImageLoading
88       nc.corner = corner;
89
90       nc.image = image;
91
92       return nc;
93     };
94
95     inline static SVGCommand PaintBitmap(float ix, float iy, float iw, float ih,
96                                         VectorHandleImage handle, VectorHandle ref, Corner corner = TopLeft)
97     {
98       SVGCommand nc;
99       nc.instr = DrawBitmap;
100       nc.x = ix;
101       nc.y = iy;
102       nc.w = iw;
103       nc.h = ih;
104       nc.target.bitmap = handle;
105       nc.handle = ref; // can be 0 (no handle) or can be an Drawstyle nandle
106       nc.corner = corner;
107       return nc;
108     };
109
110     inline static SVGCommand PaintImage(float ix, float iy, float iw, float ih,
111                                         Image& image, Corner corner = TopLeft)
112     {
113       SVGCommand nc;
114       nc.instr = DrawImage;
115       nc.x = ix;
116       nc.y = iy;
117       nc.w = iw;
118       nc.h = ih;
119       nc.image = image;
120       nc.corner = corner;
121       return nc;
122     };
123
124     inline static SVGCommand PaintTTchar(float ix, float iy, float iw, float ih, unsigned int ttchar_in)
125     {
126       SVGCommand nc;
127       nc.instr = DrawTTchar;
128       nc.x = ix;
129       nc.y = iy;
130       nc.w = iw;
131       nc.h = ih;
132       nc.handle = 0; // not valid for PaintTTchar
133       nc.target.ttchar = ttchar_in;
134       nc.corner = TopLeft;
135       return nc;
136     };
137
138     inline static SVGCommand PaintClipping(float ix, float iy, float iw, float ih)
139     {
140       SVGCommand nc;
141       nc.instr = DrawClipping;
142       nc.x = ix;
143       nc.y = iy;
144       nc.w = iw;
145       nc.h = ih;
146       nc.handle = 0; // not valid for PaintClipping
147       nc.target.ttchar = 0;
148       return nc;
149     };
150
151     inline static void PaintGlyph(SVGCommand& nc, float ix, float iy, wchar_t char_in, VectorHandle ref)
152     {
153       nc.instr = DrawGlyph;
154       nc.x = ix;
155       nc.y = iy;
156       nc.w = 0;
157       nc.h = 0;
158       nc.handle = ref; // always a valid DrawStyle handle
159       nc.target.textchar = char_in;
160     };
161
162     bool Test(float tx, float ty, float tw, float th)
163     {
164       return (x >= tx) && (y >= ty) && ((x + w) <= (tx + tw)) && ((y + h) <= (ty + th));
165     }
166
167     bool TTTest(float tox, float toy, float tx, float ty)
168     {
169       return (x == tox) && (toy == y) && (w == tx) && (h == ty);
170     }
171
172     bool Outside(float tx, float ty, float tw, float th)
173     {
174       return ((x + w) < tx) || ((y + h) < ty) || ((tx + tw) < x) || ((ty + th) < y);
175     }
176
177     VectorHandle getHandle()        // Is this DrawStyle references?
178     {
179       return handle;
180     };
181
182     VectorHandleImage getImageHandle()
183     {
184       if (instr != DrawBitmap) return 0;
185       else return target.bitmap;
186     };
187
188     SVGCommandInstr instr{DrawNoop};
189     Corner corner;
190     float x{}, y{}, w{}, h{};
191     VectorHandle handle{VECTOR_HANDLE_INIT};
192     union
193     {
194       PathIndex path_index;
195       wchar_t textchar;
196       VectorHandleImage bitmap;
197       unsigned int ttchar;
198     } target;
199
200     Image image; // This would be in the union but it's a non-trivial type
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
214 {
215   public:
216     void dumpImages();
217
218     OsdVector();
219     virtual ~OsdVector();
220
221     // FIXME switch to bool
222     virtual int init(); // Derived classes overriding init() must call this base init()
223
224     int restore();
225
226     bool screenShot(const char* fileName);
227     virtual bool screenShotInternal(void* buffer, int width, int height, bool osd /*include osd*/) = 0;
228
229     Surface* createNewSurface();
230
231     void Blank();
232     virtual void updateBackgroundColor(DrawStyle /* bg */) {};
233
234     void updateOrAddSurface(const SurfaceVector* surf, float x, float y, float height, float width,
235                             std::vector<SVGCommand>& commands);
236     void removeSurface(const SurfaceVector* surf);
237
238     virtual float getFontHeight() = 0;
239     virtual float getCharWidth(wchar_t c) = 0;
240     float* getCharWidthArray() {return byte_char_width;};
241     virtual void getScreenSize(int& width, int& height) = 0;
242     virtual void getRealScreenSize(int& width, int& height) = 0;
243
244     virtual bool getStaticImageData(unsigned int static_id, UCHAR** userdata, ULONG* length) = 0;
245
246     // Used only by OsdVector and SurfaceVector
247     void removeImageRef(const VectorHandleImage ref);
248     VectorHandle getDrawStyleHandle(const DrawStyle& c);
249     void decrementDrawStyleHandleRefCount(VectorHandle ref);
250     //virtual VectorHandleImage getJpegRef(const char* fileName, int *width,int *height);
251     virtual VectorHandleImage getMonoBitmapRef(void* base, int width, int height);
252     virtual VectorHandleImage getImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data);
253
254     int charSet() {return 2;}; //UTF-8
255
256     virtual void createPicture(struct PictureInfo& pict_inf) = 0;
257     virtual void destroyImageRef(VectorHandleImage handle) = 0;
258
259   protected:
260     LogNT* logger{};
261
262     float byte_char_width[256]{};
263
264     //virtual VectorHandleImage createJpeg(const char* fileName, int *width,int *height)=0;
265     virtual VectorHandleImage createMonoBitmap(void* base, int width, int height) = 0;
266     virtual VectorHandleImage createImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data) = 0;
267     virtual VectorHandle createDrawStyleHandle(const DrawStyle& c) = 0;
268     virtual void destroyDrawStyleHandle(VectorHandle index) = 0;
269     virtual void drawSetTrans(SurfaceInfo& sc) = 0;
270     virtual void executeDrawCommand(SVGCommand& command) = 0;
271
272     void drawSurfaces();
273
274     #if DEV
275     void dumpStyles();
276     #endif
277
278   private:
279     // Methods that only seem to be called internally
280     void incImageRef(VectorHandleImage handle);
281     void decrementAllRefCounts(std::vector<SVGCommand>& commands);
282     void incrementAllRefCounts(std::vector<SVGCommand>& commands);
283     void cleanupOrphanedRefs();
284     void incrementDrawStyleHandleRefCount(VectorHandle index);
285
286     // All the below are data structures only touched by this base class OsdVector
287
288     std::map<void*, VectorHandleImage> monobitmaps;
289     //map<string,VectorHandleImage> jpegs;
290     std::list<VectorHandleImage> palettepics;
291     std::map<VectorHandleImage, int> vhi_refcounts;   // This appears to cover all 3 (4) of the above types
292
293     std::map<DrawStyle, VectorHandle> drawstyleHandles;
294     std::map<DrawStyle, VectorHandle>::iterator drawstyleHandles_lastit;
295     bool drawstyleHandles_lastit_valid{};
296
297     std::map<VectorHandle, int> drawstyleHandlesRefCounts;
298     std::map<VectorHandle, int>::iterator drawstyleHandlesRefCounts_lastit;
299     bool drawstyleHandlesRefCounts_lastit_valid{};
300
301     std::list<SurfaceInfo> surfaces;
302     using SurfacesIterator = std::list<SurfaceInfo>::iterator;
303     std::mutex surfaces_mutex;
304 };
305
306 #endif