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