[evaspixmapsink] Revise codes for updating multiple pixmaps
[framework/multimedia/gst-plugins-ext0.10.git] / evaspixmapsink / evaspixmapsink.h
1 /*
2  * EvasPixmapSink
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@samsung.com>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at your option)
11  * any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23
24 #ifndef __GST_EVASPIXMAPSINK_H__
25 #define __GST_EVASPIXMAPSINK_H__
26
27 #include <gst/video/gstvideosink.h>
28 #include <mm_ta.h>
29
30 #ifdef HAVE_XSHM
31 #include <sys/types.h>
32 #include <sys/ipc.h>
33 #include <sys/shm.h>
34 #endif /* HAVE_XSHM */
35
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38
39 #ifdef HAVE_XSHM
40 #include <X11/extensions/XShm.h>
41 #endif /* HAVE_XSHM */
42
43 #include <X11/extensions/Xv.h>
44 #include <X11/extensions/Xvlib.h>
45 #include <X11/extensions/Xdamage.h>
46 #include <X11/extensions/damagewire.h>
47 #include <X11/Xatom.h>
48 #include <stdio.h>
49
50 #include <Evas.h>
51 #include <Ecore.h>
52 #include <Ecore_X.h>
53
54 #include <string.h>
55 #include <math.h>
56 #include <stdlib.h>
57
58 #define MAX_PLANE_NUM           3
59 #define MAX_BUFFER_NUM          20
60 #define MAX_GEM_BUFFER_NUM      (MAX_PLANE_NUM * MAX_BUFFER_NUM)
61 typedef struct _gem_info_t {
62         int dmabuf_fd;
63         unsigned int gem_handle;
64         unsigned int gem_name;
65 } gem_info_t;
66
67
68 G_BEGIN_DECLS
69
70 #define GST_TYPE_EVASPIXMAPSINK \
71   (gst_evaspixmapsink_get_type())
72 #define GST_EVASPIXMAPSINK(obj) \
73   (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_EVASPIXMAPSINK, GstEvasPixmapSink))
74 #define GST_EVASPIXMAPSINK_CLASS(klass) \
75   (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_EVASPIXMAPSINK, GstEvasPixmapSinkClass))
76 #define GST_IS_EVASPIXMAPSINK(obj) \
77   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_EVASPIXMAPSINK))
78 #define GST_IS_EVASPIXMAPSINK_CLASS(klass) \
79   (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_EVASPIXMAPSINK))
80
81 #define XV_SCREEN_SIZE_WIDTH  4096
82 #define XV_SCREEN_SIZE_HEIGHT 4096
83
84 #define MARGIN_OF_ERROR       0.005
85 #define NUM_OF_PIXMAP         3
86
87 typedef struct _GstXContext GstXContext;
88 typedef struct _GstXPixmap GstXPixmap;
89 typedef struct _GstEvasPixmapFormat GstEvasPixmapFormat;
90 typedef struct _GstEvasPixmapBuffer GstEvasPixmapBuffer;
91 typedef struct _GstEvasPixmapBufferClass GstEvasPixmapBufferClass;
92 typedef struct _GstEvasPixmapSink GstEvasPixmapSink;
93 typedef struct _GstEvasPixmapSinkClass GstEvasPixmapSinkClass;
94
95 /*
96  * GstXContext:
97  * @disp: the X11 Display of this context
98  * @screen: the default Screen of Display @disp
99  * @screen_num: the Screen number of @screen
100  * @visual: the default Visual of Screen @screen
101  * @root: the root Window of Display @disp
102  * @white: the value of a white pixel on Screen @screen
103  * @black: the value of a black pixel on Screen @screen
104  * @depth: the color depth of Display @disp
105  * @bpp: the number of bits per pixel on Display @disp
106  * @endianness: the endianness of image bytes on Display @disp
107  * @width: the width in pixels of Display @disp
108  * @height: the height in pixels of Display @disp
109  * @widthmm: the width in millimeters of Display @disp
110  * @heightmm: the height in millimeters of Display @disp
111  * @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
112  * @heightmm ratio
113  * @use_xshm: used to known wether of not XShm extension is usable or not even
114  * if the Extension is present
115  * @xv_port_id: the XVideo port ID
116  * @im_format: used to store at least a valid format for XShm calls checks
117  * @formats_list: list of supported image formats on @xv_port_id
118  * @channels_list: list of #GstColorBalanceChannels
119  * @caps: the #GstCaps that Display @disp can accept
120  *
121  * Structure used to store various informations collected/calculated for a
122  * Display.
123  */
124 struct _GstXContext {
125         Display *disp;
126
127         Screen *screen;
128         gint screen_num;
129
130         Visual *visual;
131
132         Window root;
133
134         gulong white, black;
135
136         gint depth;
137         gint bpp;
138         gint endianness;
139
140         gint width, height;
141         gint widthmm, heightmm;
142         GValue *par;                  /* calculated pixel aspect ratio */
143
144         gboolean use_xshm;
145
146         XvPortID xv_port_id;
147         guint nb_adaptors;
148         gchar ** adaptors;
149         gint im_format;
150
151         GList *formats_list;
152         GList *channels_list;
153
154         GstCaps *caps;
155 };
156
157 /*
158  * GstXPixmap:
159  * @pixmap: the pixmap ID of this X11 pixmap
160  * @width: the width in pixels of Pixmap @pixmap
161  * @height: the height in pixels of Pixmap @pixmap
162  * @gc: the Graphical Context of Pixmap @pixmap
163  *
164  * Structure used to store informations about a Pixmap.
165  */
166 struct _GstXPixmap {
167         Pixmap pixmap;
168         gint x, y;
169         gint width, height;
170         GC gc;
171         guint ref;
172         gint damaged_time;
173 };
174
175 /**
176  * GstEvasPixmapFormat:
177  * @format: the image format
178  * @caps: generated #GstCaps for this image format
179  *
180  * Structure storing image format to #GstCaps association.
181  */
182 struct _GstEvasPixmapFormat {
183         gint format;
184         GstCaps *caps;
185 };
186
187 /**
188  * GstEvasPixmapBuffer:
189  * @evaspixmapsink: a reference to our #GstEvasPixmapSink
190  * @xvimage: the XvImage of this buffer
191  * @width: the width in pixels of XvImage @xvimage
192  * @height: the height in pixels of XvImage @xvimage
193  * @im_format: the image format of XvImage @xvimage
194  * @size: the size in bytes of XvImage @xvimage
195  *
196  * Subclass of #GstBuffer containing additional information about an XvImage.
197  */
198 struct _GstEvasPixmapBuffer {
199         GstBuffer buffer;
200
201         /* Reference to the evaspixmapsink we belong to */
202         GstEvasPixmapSink *evaspixmapsink;
203         XvImage *xvimage;
204
205 #ifdef HAVE_XSHM
206         XShmSegmentInfo SHMInfo;
207 #endif /* HAVE_XSHM */
208
209         gint width, height;
210         gint im_format;
211         size_t size;
212 };
213
214 /**
215  * GstEvasPixmapSink:
216  * @display_name: the name of the Display we want to render to
217  * @xcontext: our instance's #GstXContext
218  * @xpixmap: the #GstXPixmap we are rendering to
219  * @fps_n: the framerate fraction numerator
220  * @fps_d: the framerate fraction denominator
221  * @x_lock: used to protect X calls as we are not using the XLib in threaded
222  * mode
223  * @flow_lock: used to protect data flow routines from external calls such as
224  * methods from the #GstXOverlay interface
225  * @par: used to override calculated pixel aspect ratio from @xcontext
226  * @synchronous: used to store if XSynchronous should be used or not (for
227  * debugging purpose only)
228  * @keep_aspect: used to remember if reverse negotiation scaling should respect
229  * aspect ratio
230  * @brightness: used to store the user settings for color balance brightness
231  * @contrast: used to store the user settings for color balance contrast
232  * @hue: used to store the user settings for color balance hue
233  * @saturation: used to store the user settings for color balance saturation
234  * @cb_changed: used to store if the color balance settings where changed
235  * @video_width: the width of incoming video frames in pixels
236  * @video_height: the height of incoming video frames in pixels
237  *
238  * The #GstEvasPixmapSink data structure.
239  */
240 struct _GstEvasPixmapSink {
241         /* Our element stuff */
242         GstVideoSink videosink;
243
244         char *display_name;
245         guint adaptor_no;
246
247         GstXContext *xcontext;
248         GstXPixmap *xpixmap[NUM_OF_PIXMAP];
249         GstEvasPixmapBuffer *evas_pixmap_buf;
250
251         GThread *event_thread;
252         gboolean running;
253
254         gint fps_n;
255         gint fps_d;
256
257         GMutex *x_lock;
258         GMutex *flow_lock;
259         GMutex *pixmap_ref_lock;
260
261         /* object-set pixel aspect ratio */
262         GValue *par;
263
264         gboolean synchronous;
265         gboolean double_buffer;
266
267         gint brightness;
268         gint contrast;
269         gint hue;
270         gint saturation;
271         gboolean cb_changed;
272
273         /* size of incoming video, used as the size for XvImage */
274         guint video_width, video_height;
275
276         /* display sizes, used for clipping the image */
277         gint disp_x, disp_y;
278         gint disp_width, disp_height;
279
280         /* port attributes */
281         gboolean autopaint_colorkey;
282         gint colorkey;
283
284         /* port features */
285         gboolean have_autopaint_colorkey;
286         gboolean have_colorkey;
287         gboolean have_double_buffer;
288
289         /* target video rectagle */
290         GstVideoRectangle render_rect;
291         gboolean have_render_rect;
292
293         /* display */
294         guint flip;
295         guint rotate_angle;
296         guint display_geometry_method;
297         GstVideoRectangle dst_roi;
298         guint scr_w, scr_h;
299         /* needed if fourcc is one if S series */
300         guint aligned_width;
301         guint aligned_height;
302
303         gboolean stop_video;
304
305         /* ecore & evas object */
306         Evas_Object *eo;
307         Evas_Coord w;
308         Evas_Coord h;
309         gboolean visible;
310         gint last_updated_idx;
311
312         /* pixmap */
313         gboolean do_link;
314         gboolean use_origin_size;
315         gboolean previous_origin_size;
316         gint sizediff_width;
317         gint sizediff_height;
318         guint num_of_pixmaps;
319
320         /* damage event */
321         Damage damage[NUM_OF_PIXMAP];
322         int damage_case;
323         Ecore_Pipe *epipe;
324
325         gint drm_fd;
326         gem_info_t gem_info[MAX_GEM_BUFFER_NUM];
327 };
328
329 /* max plane count *********************************************************/
330 #define MPLANE_IMGB_MAX_COUNT         (4)
331
332 /* image buffer definition ***************************************************
333
334     +------------------------------------------+ ---
335     |                                          |  ^
336     |     uaddr[], index[]                     |  |
337     |     +---------------------------+ ---    |  |
338     |     |                           |  ^     |  |
339     |     |<-------- width[] -------->|  |     |  |
340     |     |                           |  |     |  |
341     |     |                           |        |
342     |     |                           |height[]|elevation[]
343     |     |                           |        |
344     |     |                           |  |     |  |
345     |     |                           |  |     |  |
346     |     |                           |  v     |  |
347     |     +---------------------------+ ---    |  |
348     |                                          |  v
349     +------------------------------------------+ ---
350
351     |<----------------- stride[] ------------------>|
352 */
353 typedef struct _GstMultiPlaneImageBuffer GstMultiPlaneImageBuffer;
354 struct _GstMultiPlaneImageBuffer
355 {
356     GstBuffer buffer;
357
358     /* width of each image plane */
359     gint      width[MPLANE_IMGB_MAX_COUNT];
360     /* height of each image plane */
361     gint      height[MPLANE_IMGB_MAX_COUNT];
362     /* stride of each image plane */
363     gint      stride[MPLANE_IMGB_MAX_COUNT];
364     /* elevation of each image plane */
365     gint      elevation[MPLANE_IMGB_MAX_COUNT];
366     /* user space address of each image plane */
367     gpointer uaddr[MPLANE_IMGB_MAX_COUNT];
368     /* Index of real address of each image plane, if needs */
369     gpointer index[MPLANE_IMGB_MAX_COUNT];
370     /* left postion, if needs */
371     gint      x;
372     /* top position, if needs */
373     gint      y;
374     /* to align memory */
375     gint      __dummy2;
376     /* arbitrary data */
377     gint      data[16];
378 };
379
380 struct _GstEvasPixmapSinkClass {
381   GstVideoSinkClass parent_class;
382 };
383
384 GType gst_evaspixmapsink_get_type(void);
385
386 G_END_DECLS
387
388 #endif /* __GST_EVASPIXMAPSINK_H__ */