display: no need to pass the video_format_t for CROP/ASPECT_RATIO changes
[vlc.git] / modules / video_output / evas.c
1 /*****************************************************************************
2  * evas.c: EFL Evas video output
3  *****************************************************************************
4  * Copyright (C) 2015 VLC authors, VideoLAN, and VideoLabs
5  *
6  * Authors: Thomas Guillem <thomas@gllm.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_vout_display.h>
32 #include <vlc_picture_pool.h>
33 #include <vlc_filter.h>
34
35 #include <Evas.h>
36 #include <Ecore.h>
37
38 /* Deactivate TBM surface for now: it's impossible to specify a crop (visible
39  * lines/pitch) and to avoid green borders. */
40 #undef HAVE_TIZEN_SDK
41
42 #ifdef HAVE_TIZEN_SDK
43 # include <tbm_surface.h>
44 #endif
45
46 #if defined(EVAS_VERSION_MAJOR) && defined(EVAS_VERSION_MINOR)
47 # if EVAS_VERSION_MAJOR > 1 || ( EVAS_VERSION_MAJOR == 1 && EVAS_VERSION_MINOR >= 10 )
48 #  define HAVE_EVAS_CALLBACK_KEY_UP
49 # endif
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define CHROMA_TEXT "Chroma used"
56 #define CHROMA_LONGTEXT "Force use of a specific chroma for evas image"
57
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 vlc_module_begin()
62     set_category( CAT_VIDEO )
63     set_subcategory( SUBCAT_VIDEO_VOUT )
64     set_shortname( "evas" )
65     set_description( "evas video output" )
66     set_capability( "vout display", 220 )
67     add_shortcut( "evas" )
68     add_string( "evas-image-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
69     set_callbacks( Open, Close )
70 vlc_module_end()
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75
76 static int EvasImageSetup( vout_display_t * );
77 #ifdef HAVE_TIZEN_SDK
78 static bool EvasIsOpenGLSupported( vout_display_t * );
79 static int TbmSurfaceSetup( vout_display_t * );
80 #endif
81
82 /* Buffer and Event Fifo */
83
84 struct fifo_item
85 {
86     struct fifo_item *p_next;
87 };
88
89 struct fifo
90 {
91     vlc_mutex_t lock;
92     struct fifo_item *p_first;
93     struct fifo_item *p_last;
94 };
95
96 struct buffer
97 {
98     struct fifo_item fifo_item;
99     uint8_t *p[PICTURE_PLANE_MAX];
100     bool b_locked;
101 #ifdef HAVE_TIZEN_SDK
102     tbm_surface_h p_tbm_surface;
103 #endif
104 };
105
106 struct event
107 {
108     struct fifo_item fifo_item;
109     int i_type;
110     union {
111         int i_key;
112         int i_button;
113         struct {
114             int i_x, i_y;
115         } point;
116     } u;
117 };
118
119 struct vout_display_sys_t
120 {
121     picture_pool_t  *p_pool;
122
123     int              i_width, i_height;
124
125     /* Planes */
126     plane_t          p_planes[PICTURE_PLANE_MAX];
127     int              i_planes_order[PICTURE_PLANE_MAX];
128     unsigned int     i_nb_planes;
129
130     /* Array of video buffers */
131     struct buffer   *p_buffers;
132     unsigned int     i_nb_buffers;
133
134     /* FIFO of unlocked video buffers */
135     struct fifo      buffer_fifo;
136
137     /* New buffer to display */
138     struct buffer   *p_new_buffer;
139     /* Buffer being displayed */
140     struct buffer   *p_current_buffer;
141
142     /* Evas */
143     Evas_Object     *p_evas;
144     Ecore_Animator  *p_anim;
145
146     /* If true: this module doesn't own the Evas_Object anymore */
147     bool             b_evas_changed;
148     /* If true: apply rotation to vd->fmt */
149     bool             b_apply_rotation;
150
151     /* FIFO of events */
152     struct fifo      event_fifo;
153
154     /* lock and cond used by EcoreMainLoopCallSync */
155     vlc_mutex_t      cb_lock;
156     vlc_cond_t       cb_wait;
157
158     union {
159         struct evas
160         {
161             Evas_Colorspace     i_colorspace;
162             bool                b_yuv;
163         } evas;
164 #ifdef HAVE_TIZEN_SDK
165         struct {
166             tbm_format          i_format;
167             int                 i_angle;
168         } tbm;
169 #endif
170     } u;
171
172     /* Specific callbacks for EvasImage or TBMSurface */
173     int     (*pf_set_data)( vout_display_t * );
174     int     (*pf_buffers_alloc)( vout_display_t *, video_format_t * );
175     void    (*pf_buffers_free)( vout_display_t * );
176 };
177
178 struct picture_sys_t
179 {
180     vout_display_sys_t *p_vd_sys;
181     struct buffer *p_buffer;
182 };
183
184 static void
185 fifo_push( struct fifo *p_fifo, struct fifo_item *p_new )
186 {
187     struct fifo_item *p_last;
188
189     vlc_mutex_lock( &p_fifo->lock );
190
191     p_new->p_next = NULL;
192     p_last = p_fifo->p_last;
193
194     if( p_last )
195         p_last->p_next = p_new;
196     else
197         p_fifo->p_first = p_new;
198     p_fifo->p_last = p_new;
199
200     vlc_mutex_unlock( &p_fifo->lock );
201 }
202
203 static struct fifo_item *
204 fifo_pop( struct fifo *p_fifo )
205 {
206     struct fifo_item *p_new;
207
208     vlc_mutex_lock( &p_fifo->lock );
209
210     p_new = p_fifo->p_first;
211
212     if( p_new )
213     {
214         if( p_fifo->p_last == p_fifo->p_first )
215             p_fifo->p_first = p_fifo->p_last = NULL;
216         else
217             p_fifo->p_first = p_new->p_next;
218     }
219
220     vlc_mutex_unlock( &p_fifo->lock );
221     return p_new;
222 }
223
224 static void
225 fifo_init( struct fifo *p_fifo )
226 {
227     vlc_mutex_init( &p_fifo->lock );
228     p_fifo->p_first = p_fifo->p_last = NULL;
229 }
230
231 static void
232 fifo_deinit( struct fifo *p_fifo )
233 {
234     vlc_mutex_destroy( &p_fifo->lock );
235 }
236
237 #define BUFFER_FIFO_PUSH( buffer ) fifo_push( &sys->buffer_fifo, (struct fifo_item *)(buffer) )
238 #define BUFFER_FIFO_POP() (struct buffer *) fifo_pop( &sys->buffer_fifo )
239 #define EVENT_FIFO_PUSH( event ) fifo_push( &sys->event_fifo, (struct fifo_item *)(event) )
240 #define EVENT_FIFO_POP() (struct event *) fifo_pop( &sys->event_fifo )
241
242 typedef int (*mainloop_cb)( vout_display_t *vd );
243
244 struct mainloop_cb_args
245 {
246     vout_display_t *vd;
247     mainloop_cb p_cb;
248     int i_ret;
249     bool b_signal;
250 };
251
252 static void
253 EcoreMainLoopCb( void *p_opaque )
254 {
255     struct mainloop_cb_args *p_args = p_opaque;
256     vout_display_sys_t *sys = p_args->vd->sys;
257
258     p_args->i_ret = p_args->p_cb( p_args->vd );
259
260     vlc_mutex_lock( &sys->cb_lock );
261     p_args->b_signal = true;
262     vlc_cond_signal( &sys->cb_wait );
263     vlc_mutex_unlock( &sys->cb_lock );
264 }
265
266 static int
267 EcoreMainLoopCallSync( vout_display_t *vd, mainloop_cb p_cb )
268 {
269     vout_display_sys_t *sys = vd->sys;
270     struct mainloop_cb_args args = { .vd = vd, .p_cb = p_cb, .b_signal = false };
271     ecore_main_loop_thread_safe_call_async( EcoreMainLoopCb, &args );
272
273     vlc_mutex_lock( &sys->cb_lock );
274     while( !args.b_signal )
275         vlc_cond_wait( &sys->cb_wait, &sys->cb_lock );
276     vlc_mutex_unlock( &sys->cb_lock );
277
278     return args.i_ret;
279 }
280
281 #ifdef HAVE_EVAS_CALLBACK_KEY_UP
282 static void
283 EventSendKey( vout_display_t *vd, int i_key )
284 {
285     vout_display_sys_t *sys = vd->sys;
286     struct event *p_event = malloc( sizeof(struct event) );
287
288     if( !p_event )
289         return;
290     p_event->i_type = VOUT_DISPLAY_EVENT_KEY;
291     p_event->u.i_key = i_key;
292     EVENT_FIFO_PUSH( p_event );
293 }
294
295 static void
296 EvasKeyUpCb( void *data, Evas *e, Evas_Object *obj, void *event )
297 {
298     (void) e; (void) obj;
299     Evas_Event_Key_Up *p_key_up = (Evas_Event_Key_Up *) event;
300
301     EventSendKey( data, p_key_up->keycode );
302 }
303 #endif
304
305 static void
306 EventSendMouseMoved( vout_display_t *vd, int i_x, int i_y )
307 {
308     vout_display_sys_t *sys = vd->sys;
309     Evas_Coord i_ox, i_oy, i_ow, i_oh;
310     struct event *p_event = malloc( sizeof(struct event) );
311
312     if( !p_event )
313         return;
314     evas_object_geometry_get( sys->p_evas, &i_ox, &i_oy, &i_ow, &i_oh );
315     p_event->i_type = VOUT_DISPLAY_EVENT_MOUSE_MOVED;
316     p_event->u.point.i_x = ( ( i_x - i_ox ) * sys->i_width ) / i_ow;
317     p_event->u.point.i_y = ( ( i_y - i_oy ) * sys->i_height ) / i_oh;
318     EVENT_FIFO_PUSH( p_event );
319 }
320
321 static void
322 EventSendMouseButton( vout_display_t *vd, int i_type, int i_button )
323 {
324     vout_display_sys_t *sys = vd->sys;
325     struct event *p_event = malloc( sizeof(struct event) );
326
327     if( !p_event )
328         return;
329     p_event->i_type = i_type;
330     p_event->u.i_button = i_button;
331     EVENT_FIFO_PUSH( p_event );
332 }
333
334 static void
335 EventMouseDownCb( void *data, Evas *e, Evas_Object *obj, void *event )
336 {
337     (void) e; (void) obj;
338     Evas_Event_Mouse_Down *p_mouse_down = (Evas_Event_Mouse_Down *) event;
339
340     EventSendMouseButton( data, VOUT_DISPLAY_EVENT_MOUSE_PRESSED,
341                              p_mouse_down->button - 1 );
342 }
343
344 static void
345 EvasMouseUpCb( void *data, Evas *e, Evas_Object *obj, void *event )
346 {
347     (void) e; (void) obj;
348     Evas_Event_Mouse_Up *p_mouse_up = (Evas_Event_Mouse_Up *) event;
349
350     EventSendMouseButton( data, VOUT_DISPLAY_EVENT_MOUSE_RELEASED,
351                              p_mouse_up->button - 1 );
352 }
353
354 static void
355 EvasMouseMoveCb( void *data, Evas *e, Evas_Object *obj, void *event )
356 {
357     (void) e; (void) obj;
358     Evas_Event_Mouse_Move *p_mouse_move = (Evas_Event_Mouse_Move *) event;
359     EventSendMouseMoved( data, p_mouse_move->cur.canvas.x,
360                             p_mouse_move->cur.canvas.y );
361 }
362
363 static void
364 EvasMultiDownCb( void *data, Evas *e, Evas_Object *obj, void *event )
365 {
366     (void) e; (void) obj; (void) event;
367     EventSendMouseButton( data, VOUT_DISPLAY_EVENT_MOUSE_PRESSED, 1 );
368 }
369
370 static void
371 EvasMultiUpCb( void *data, Evas *e, Evas_Object *obj, void *event )
372 {
373     (void) e; (void) obj; (void) event;
374     EventSendMouseButton( data, VOUT_DISPLAY_EVENT_MOUSE_RELEASED, 1 );
375 }
376
377 static void
378 EvasMultiMoveCb( void *data, Evas *e, Evas_Object *obj, void *event )
379 {
380     (void) e; (void) obj;
381     Evas_Event_Multi_Move *p_multi_move = (Evas_Event_Multi_Move *) event;
382
383     EventSendMouseMoved( data, p_multi_move->cur.canvas.x,
384                             p_multi_move->cur.canvas.y );
385 }
386
387 static void
388 FmtUpdate( vout_display_t *vd )
389 {
390     vout_display_sys_t *sys = vd->sys;
391     vout_display_place_t place;
392     video_format_t src;
393
394     vout_display_PlacePicture( &place, &vd->source, vd->cfg, false );
395
396     if( sys->b_apply_rotation )
397     {
398         video_format_ApplyRotation( &src, &vd->source );
399         vd->fmt.orientation = 0;
400     }
401     else
402         src = vd->source;
403
404     vd->fmt.i_width  = src.i_width  * place.width / src.i_visible_width;
405     vd->fmt.i_height = src.i_height * place.height / src.i_visible_height;
406
407     vd->fmt.i_visible_width  = place.width;
408     vd->fmt.i_visible_height = place.height;
409     vd->fmt.i_x_offset = src.i_x_offset * place.width / src.i_visible_width;
410     vd->fmt.i_y_offset = src.i_y_offset * place.height / src.i_visible_height;
411
412     sys->i_width  = vd->fmt.i_visible_width;
413     sys->i_height = vd->fmt.i_visible_height;
414 }
415
416 static Eina_Bool
417 mainloop_evas_anim_cb( void *p_opaque )
418 {
419     vout_display_t *vd = p_opaque;
420     vout_display_sys_t *sys = vd->sys;
421     evas_object_image_pixels_dirty_set( sys->p_evas, 1 );
422
423     sys->p_anim = NULL;
424     return false;
425 }
426
427 static void
428 EvasResizeCb( void *data, Evas *e, Evas_Object *obj, void *event )
429 {
430     (void) e; (void) obj; (void) event;
431     vout_display_t *vd = data;
432     vout_display_sys_t *sys = vd->sys;
433
434     sys->b_evas_changed = true;
435 }
436
437 static int
438 EvasDisplayMainloopCb( vout_display_t *vd )
439 {
440     vout_display_sys_t *sys = vd->sys;
441
442     if( sys->b_evas_changed || sys->pf_set_data( vd ) )
443         return -1;
444
445     evas_object_image_data_update_add( sys->p_evas, 0, 0,
446                                        sys->i_width, sys->i_height );
447     evas_object_image_pixels_dirty_set( sys->p_evas, 0 );
448
449     if( !sys->p_anim )
450         sys->p_anim = ecore_animator_add( mainloop_evas_anim_cb, vd );
451
452     if( sys->p_current_buffer )
453         BUFFER_FIFO_PUSH( sys->p_current_buffer );
454
455     sys->p_current_buffer = sys->p_new_buffer;
456     sys->p_new_buffer = NULL;
457     return 0;
458 }
459
460 static int
461 EvasInitMainloopCb( vout_display_t *vd )
462 {
463     vout_display_sys_t *sys = vd->sys;
464
465 #ifdef HAVE_TIZEN_SDK
466     if( !EvasIsOpenGLSupported( vd ) || TbmSurfaceSetup( vd ) )
467 #endif
468     if( EvasImageSetup( vd ) )
469         return -1;
470
471     evas_object_image_alpha_set( sys->p_evas, 0 );
472     evas_object_image_size_set( sys->p_evas, sys->i_width, sys->i_height );
473
474     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MOUSE_DOWN,
475                                     EventMouseDownCb, vd );
476     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MOUSE_UP,
477                                     EvasMouseUpCb, vd );
478     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MOUSE_MOVE,
479                                     EvasMouseMoveCb, vd );
480     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MULTI_DOWN,
481                                     EvasMultiDownCb, vd );
482     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MULTI_UP,
483                                     EvasMultiUpCb, vd );
484     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_MULTI_MOVE,
485                                     EvasMultiMoveCb, vd );
486 #ifdef HAVE_EVAS_CALLBACK_KEY_UP
487     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_KEY_UP,
488                                     EvasKeyUpCb, sys );
489 #endif
490
491     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_IMAGE_RESIZE,
492                                     EvasResizeCb, vd );
493
494     return 0;
495 }
496
497 static int
498 EvasDeinitMainloopCb( vout_display_t *vd )
499 {
500     vout_display_sys_t *sys = vd->sys;
501
502     if( sys->p_anim )
503     {
504         ecore_animator_del( sys->p_anim );
505         sys->p_anim = NULL;
506     }
507
508     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_IMAGE_RESIZE,
509                                          EvasResizeCb, vd );
510
511     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MOUSE_DOWN,
512                                          EventMouseDownCb, vd );
513     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MOUSE_UP,
514                                          EvasMouseUpCb, vd );
515     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MOUSE_MOVE,
516                                          EvasMouseMoveCb, vd );
517     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MULTI_DOWN,
518                                          EvasMultiDownCb, vd );
519     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MULTI_UP,
520                                          EvasMultiUpCb, vd );
521     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_MULTI_MOVE,
522                                          EvasMultiMoveCb, vd );
523 #ifdef HAVE_EVAS_CALLBACK_KEY_UP
524     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_KEY_UP,
525                                          EvasKeyUpCb, vd );
526 #endif
527
528     if( !sys->b_evas_changed )
529     {
530         evas_object_image_data_set( sys->p_evas, NULL );
531         evas_object_image_pixels_dirty_set( sys->p_evas, 0 );
532     }
533
534     return 0;
535 }
536
537 static int
538 EvasResetMainloopCb( vout_display_t *vd )
539 {
540     vout_display_sys_t *sys = vd->sys;
541
542     if( sys->b_evas_changed )
543         return -1;
544
545     if( sys->p_anim )
546     {
547         ecore_animator_del( sys->p_anim );
548         sys->p_anim = NULL;
549     }
550
551     evas_object_event_callback_del_full( sys->p_evas, EVAS_CALLBACK_IMAGE_RESIZE,
552                                          EvasResizeCb, vd );
553
554     FmtUpdate( vd );
555
556     evas_object_image_data_set( sys->p_evas, NULL );
557     evas_object_image_size_set( sys->p_evas, sys->i_width, sys->i_height );
558
559     evas_object_event_callback_add( sys->p_evas, EVAS_CALLBACK_IMAGE_RESIZE,
560                                     EvasResizeCb, vd );
561     return 0;
562 }
563
564 static int
565 BuffersSetup( vout_display_t *vd, video_format_t *p_fmt,
566               unsigned int *p_requested_count )
567 {
568     vout_display_sys_t *sys = vd->sys;
569
570     sys->i_nb_buffers = *p_requested_count;
571     if( sys->pf_buffers_alloc( vd, p_fmt ) )
572     {
573         sys->i_nb_planes = 0;
574         return VLC_EGENERIC;
575     }
576     *p_requested_count = sys->i_nb_buffers;
577
578     for( unsigned int i = 0; i < sys->i_nb_buffers; ++i )
579         BUFFER_FIFO_PUSH( &sys->p_buffers[i] );
580     return VLC_SUCCESS;
581 }
582
583 static void
584 BuffersClean( vout_display_t *vd )
585 {
586     vout_display_sys_t *sys = vd->sys;
587
588     if( sys->p_buffers )
589         sys->pf_buffers_free( vd );
590     sys->buffer_fifo.p_first = sys->buffer_fifo.p_last = NULL;
591 }
592
593 static picture_t *
594 PictureAlloc( vout_display_t *vd )
595 {
596     vout_display_sys_t *sys = vd->sys;
597     picture_resource_t rsc;
598     picture_t *p_pic = NULL;
599     picture_sys_t *p_picsys = calloc(1, sizeof(*p_picsys));
600
601     if( !p_picsys )
602         return NULL;
603
604     p_picsys->p_vd_sys = vd->sys;
605     memset(&rsc, 0, sizeof(picture_resource_t));
606     rsc.p_sys = p_picsys;
607     for( unsigned int i = 0; i < sys->i_nb_planes; ++i )
608     {
609         rsc.p[i].i_lines = sys->p_planes[i].i_lines;
610         rsc.p[i].i_pitch = sys->p_planes[i].i_pitch;
611     }
612
613     p_pic = picture_NewFromResource( &vd->fmt, &rsc );
614     if( !p_pic )
615     {
616         free( p_picsys );
617         return NULL;
618     }
619
620     return p_pic;
621 }
622
623 static int
624 PoolLockPicture(picture_t *p_pic)
625 {
626     picture_sys_t *p_picsys = p_pic->p_sys;
627     vout_display_sys_t *sys = p_picsys->p_vd_sys;
628
629     struct buffer *p_buffer = BUFFER_FIFO_POP();
630     if( !p_buffer || !p_buffer->p[0] )
631         return -1;
632
633     p_picsys->p_buffer = p_buffer;
634     for( unsigned int i = 0; i < sys->i_nb_planes; ++i )
635         p_pic->p[i].p_pixels = p_buffer->p[i];
636     return 0;
637 }
638
639 static void
640 PoolUnlockPicture(picture_t *p_pic)
641 {
642     picture_sys_t *p_picsys = p_pic->p_sys;
643     vout_display_sys_t *sys = p_picsys->p_vd_sys;
644
645     if( p_picsys->p_buffer )
646     {
647         BUFFER_FIFO_PUSH( p_picsys->p_buffer );
648         p_picsys->p_buffer = NULL;
649     }
650 }
651
652 static picture_pool_t *
653 PoolAlloc( vout_display_t *vd, unsigned i_requested_count )
654 {
655     picture_t **pp_pics = NULL;
656     picture_pool_t *p_pool;
657     picture_pool_configuration_t pool_cfg;
658
659     msg_Dbg(vd, "PoolAlloc, requested_count: %d", i_requested_count);
660
661     i_requested_count++; /* picture owned by evas */
662
663     if( BuffersSetup( vd, &vd->fmt, &i_requested_count) )
664     {
665         msg_Err( vd, "BuffersSetup failed" );
666         return NULL;
667     }
668     if( i_requested_count <= 1 )
669     {
670         msg_Err( vd, "not enough buffers allocated" );
671         goto error;
672     }
673     i_requested_count--;
674
675     msg_Dbg( vd, "PoolAlloc, got: %d", i_requested_count );
676
677     if( !( pp_pics = calloc( i_requested_count, sizeof(picture_t) ) ) )
678         goto error;
679
680     for( unsigned int i = 0; i < i_requested_count; ++i )
681     {
682         if( !( pp_pics[i] = PictureAlloc( vd ) ) )
683         {
684             i_requested_count = i;
685             msg_Err( vd, "PictureAlloc failed" );
686             goto error;
687         }
688     }
689
690     memset( &pool_cfg, 0, sizeof(pool_cfg) );
691     pool_cfg.picture_count = i_requested_count;
692     pool_cfg.picture       = pp_pics;
693     pool_cfg.lock          = PoolLockPicture;
694     pool_cfg.unlock        = PoolUnlockPicture;
695
696     p_pool = picture_pool_NewExtended( &pool_cfg );
697     if( p_pool )
698         return p_pool;
699
700 error:
701     if( pp_pics )
702     {
703         for( unsigned int i = 0; i < i_requested_count; ++i )
704             picture_Release( pp_pics[i] );
705         free( pp_pics );
706     }
707     BuffersClean( vd );
708     return NULL;
709 }
710
711 static picture_pool_t *
712 Pool( vout_display_t *vd, unsigned i_requested_count )
713 {
714     vout_display_sys_t *sys = vd->sys;
715
716     if( sys->p_pool == NULL )
717         sys->p_pool = PoolAlloc( vd, i_requested_count );
718     return sys->p_pool;
719 }
720
721 static void
722 Display( vout_display_t *vd, picture_t *p_pic, subpicture_t *p_subpic )
723 {
724     (void) p_subpic;
725     vout_display_sys_t *sys = vd->sys;
726     picture_sys_t *p_picsys = p_pic->p_sys;
727
728     if( p_picsys->p_buffer )
729     {
730         sys->p_new_buffer = p_picsys->p_buffer;
731         p_picsys->p_buffer = NULL;
732
733         EcoreMainLoopCallSync( vd, EvasDisplayMainloopCb );
734     }
735     picture_Release( p_pic );
736 }
737
738 static int
739 Control( vout_display_t *vd, int i_query, va_list ap )
740 {
741     vout_display_sys_t *sys = vd->sys;
742
743     switch( i_query )
744     {
745     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
746     {
747         vout_display_place_t place;
748         video_format_t fmt;
749
750         msg_Dbg( vd, "VOUT_DISPLAY_CHANGE_SOURCE_ASPECT" );
751
752         video_format_ApplyRotation( &fmt, &vd->source );
753         vout_display_PlacePicture( &place, &fmt, vd->cfg, false );
754
755         if( place.width != (unsigned) sys->i_width
756          && place.height != (unsigned) sys->i_height )
757         {
758             if( vd->info.has_pictures_invalid )
759             {
760                 msg_Warn( vd, "ratio changed: invalidate pictures" );
761                 vout_display_SendEventPicturesInvalid( vd );
762             }
763             else
764                 return VLC_EGENERIC;
765         }
766         return VLC_SUCCESS;
767     }
768     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
769         return VLC_SUCCESS;
770     case VOUT_DISPLAY_RESET_PICTURES:
771         msg_Dbg( vd, "VOUT_DISPLAY_RESET_PICTURES" );
772
773         EcoreMainLoopCallSync( vd, EvasResetMainloopCb );
774
775         BuffersClean( vd );
776
777         if( sys->p_pool )
778         {
779             picture_pool_Release( sys->p_pool );
780             sys->p_pool = NULL;
781         }
782         return VLC_SUCCESS;
783     case VOUT_DISPLAY_CHANGE_ZOOM:
784     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
785     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
786     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
787     case VOUT_DISPLAY_HIDE_MOUSE:
788         return VLC_EGENERIC;
789     default:
790         msg_Warn( vd, "Unknown request in evas_output" );
791         return VLC_EGENERIC;
792     }
793 }
794
795 static void
796 Manage( vout_display_t *vd )
797 {
798     vout_display_sys_t *sys = vd->sys;
799     struct event *p_event;
800
801     while( ( p_event = EVENT_FIFO_POP() ) )
802     {
803         switch( p_event->i_type )
804         {
805             case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
806                 vout_display_SendEventMouseMoved( vd, p_event->u.point.i_x,
807                                                   p_event->u.point.i_y );
808                 break;
809             case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
810             case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
811                 vout_display_SendEvent( vd, p_event->i_type,
812                                         p_event->u.i_button );
813                 break;
814             case VOUT_DISPLAY_EVENT_KEY:
815                 vout_display_SendEventKey( vd, p_event->u.i_key );
816                 break;
817         }
818         free( p_event );
819     }
820 }
821
822 static void
823 Close( vlc_object_t *p_this )
824 {
825     vout_display_t *vd = (vout_display_t *)p_this;
826     vout_display_sys_t *sys = vd->sys;
827     struct event *p_event;
828
829     if (!sys)
830         return;
831
832     EcoreMainLoopCallSync( vd, EvasDeinitMainloopCb );
833
834     BuffersClean( vd );
835     fifo_deinit( &sys->buffer_fifo );
836
837     if( sys->p_pool )
838         picture_pool_Release(sys->p_pool);
839
840     while( ( p_event = EVENT_FIFO_POP() ) )
841         free( p_event );
842
843     vlc_mutex_destroy( &sys->cb_lock );
844     vlc_cond_destroy( &sys->cb_wait );
845
846     free( sys );
847 }
848
849 static int
850 Open( vlc_object_t *p_this )
851 {
852     vout_display_t *vd = (vout_display_t*)p_this;
853     vout_display_sys_t *sys;
854     Evas_Object *p_evas;
855
856     if( vout_display_IsWindowed( vd ) )
857         return VLC_EGENERIC;
858
859     p_evas = var_InheritAddress( p_this, "drawable-evasobject" );
860     if( !p_evas )
861         return VLC_EGENERIC;
862
863     vd->sys = sys = (struct vout_display_sys_t *) calloc( 1, sizeof(*sys) );
864     if( !sys )
865         return VLC_ENOMEM;
866
867     vlc_mutex_init( &sys->cb_lock );
868     vlc_cond_init( &sys->cb_wait );
869     fifo_init( &sys->buffer_fifo );
870     fifo_init( &sys->event_fifo );
871
872     sys->p_evas = p_evas;
873
874     msg_Dbg( vd, "request video format: %4.4s",
875              (const char *)&vd->fmt.i_chroma );
876
877     /* Evas Initialisation must be done from the Mainloop */
878     if( EcoreMainLoopCallSync( vd, EvasInitMainloopCb ) )
879     {
880         msg_Err( vd, "EvasInitMainloopCb failed" );
881         Close( p_this );
882         return VLC_EGENERIC;
883     }
884
885     for( unsigned int i = 0; i < PICTURE_PLANE_MAX; ++i )
886         sys->i_planes_order[i] = i;
887
888     switch( vd->fmt.i_chroma )
889     {
890         case VLC_CODEC_RGB32:
891         case VLC_CODEC_RGB16:
892             video_format_FixRgb(&vd->fmt);
893             break;
894         case VLC_CODEC_YV12:
895             sys->i_planes_order[1] = 2;
896             sys->i_planes_order[2] = 1;
897         default:
898             break;
899     }
900
901     msg_Dbg( vd, "got video format: %4.4s, size: %dx%d",
902              (const char *)&vd->fmt.i_chroma,
903              sys->i_width, sys->i_height );
904
905     /* Setup vout_display */
906     vd->pool    = Pool;
907     vd->prepare = NULL;
908     vd->display = Display;
909     vd->control = Control;
910     vd->manage  = Manage;
911
912     return VLC_SUCCESS;
913 }
914
915 static int
916 EvasImageSetData( vout_display_t *vd )
917 {
918     vout_display_sys_t *sys = vd->sys;
919     struct buffer *p_buffer = sys->p_new_buffer;
920
921     if( sys->u.evas.b_yuv )
922     {
923         void *p_data = evas_object_image_data_get( sys->p_evas, 1 );
924         const uint8_t **pp_rows = (const uint8_t **) p_data;
925
926         if( !p_data )
927             return -1;
928
929         for( unsigned int i = 0; i < sys->i_nb_planes; ++i )
930         {
931             plane_t *p_plane = &sys->p_planes[sys->i_planes_order[i]];
932
933             for( int j = 0; j < p_plane->i_visible_lines; ++j )
934                 *(pp_rows++) = &p_buffer->p[i][j * p_plane->i_pitch];
935         }
936
937         evas_object_image_data_set( sys->p_evas, p_data );
938     }
939     else
940         evas_object_image_data_set( sys->p_evas, p_buffer->p[0] );
941
942     return 0;
943 }
944
945 static void
946 EvasImageBuffersFree( vout_display_t *vd )
947 {
948     vout_display_sys_t *sys = vd->sys;
949
950     for( unsigned int i = 0; i < sys->i_nb_buffers; i++ )
951         aligned_free( sys->p_buffers[i].p[0] );
952     free( sys->p_buffers );
953     sys->p_buffers = NULL;
954     sys->i_nb_buffers = 0;
955     sys->i_nb_planes = 0;
956 }
957
958 static int
959 EvasImageBuffersAlloc( vout_display_t *vd, video_format_t *p_fmt )
960 {
961     vout_display_sys_t *sys = vd->sys;
962     picture_t *p_pic = NULL;
963     picture_resource_t rsc;
964     size_t i_bytes = 0;
965
966     memset(&rsc, 0, sizeof(picture_resource_t));
967     if( !( p_pic = picture_NewFromResource( p_fmt, &rsc ) ) )
968         return -1;
969
970     if( picture_Setup( p_pic, p_fmt ) )
971     {
972         picture_Release( p_pic );
973         return -1;
974     }
975
976     for( int i = 0; i < p_pic->i_planes; ++i )
977         memcpy( &sys->p_planes[i], &p_pic->p[i], sizeof(plane_t));
978     sys->i_nb_planes = p_pic->i_planes;
979     picture_Release( p_pic );
980
981     if( !( sys->p_buffers = calloc( sys->i_nb_buffers, sizeof(struct buffer) ) ) )
982         goto error;
983
984     /* Calculate how big the new image should be */
985     for( unsigned int i = 0; i < sys->i_nb_planes; i++ )
986     {
987         const plane_t *p = &sys->p_planes[i];
988
989         if( p->i_pitch < 0 || p->i_lines <= 0 ||
990             (size_t)p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
991             goto error;
992         i_bytes += p->i_pitch * p->i_lines;
993     }
994
995     if( !i_bytes )
996         goto error;
997
998     for( unsigned int i = 0; i < sys->i_nb_buffers; ++i )
999     {
1000         struct buffer *p_buffer = &sys->p_buffers[i];
1001
1002         p_buffer->p[0] = aligned_alloc( 16, i_bytes );
1003
1004         if( !p_buffer->p[0] )
1005         {
1006             sys->i_nb_buffers = i;
1007             break;
1008         }
1009
1010         for( unsigned int j = 1; j < sys->i_nb_planes; j++ )
1011             p_buffer->p[j] = &p_buffer->p[j-1][ sys->p_planes[j-1].i_lines *
1012                                                 sys->p_planes[j-1].i_pitch ];
1013     }
1014
1015     return 0;
1016
1017 error:
1018     if( sys->p_buffers )
1019         EvasImageBuffersFree( vd );
1020     return -1;
1021 }
1022
1023 static int
1024 EvasImageSetup( vout_display_t *vd )
1025 {
1026     vout_display_sys_t *sys = vd->sys;
1027     char *psz_fcc = var_InheritString( vd, "evas-image-chroma" );
1028
1029     if( psz_fcc )
1030     {
1031         vd->fmt.i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_fcc );
1032         free( psz_fcc );
1033     }
1034
1035     switch( vd->fmt.i_chroma )
1036     {
1037         case VLC_CODEC_RGB32:
1038             sys->u.evas.i_colorspace = EVAS_COLORSPACE_ARGB8888;
1039             break;
1040         /* Not implemented yet */
1041 #if 0
1042         case VLC_CODEC_RGB16:
1043             sys->u.evas.i_colorspace = EVAS_COLORSPACE_RGB565_A5P;
1044             break;
1045 #endif
1046         case VLC_CODEC_YUYV:
1047             sys->u.evas.i_colorspace = EVAS_COLORSPACE_YCBCR422601_PL;
1048             sys->u.evas.b_yuv = true;
1049             break;
1050         /* FIXME: SIGSEGV in evas_gl_common_texture_nv12_update */
1051 #if 0
1052         case VLC_CODEC_NV12:
1053             sys->u.evas.i_colorspace = EVAS_COLORSPACE_YCBCR420NV12601_PL;
1054             sys->u.evas.b_yuv = true;
1055             break;
1056 #endif
1057         case VLC_CODEC_YV12:
1058             sys->u.evas.i_colorspace = EVAS_COLORSPACE_YCBCR422P601_PL;
1059             sys->u.evas.b_yuv = true;
1060             break;
1061         default:
1062         case VLC_CODEC_I420:
1063             sys->u.evas.i_colorspace = EVAS_COLORSPACE_YCBCR422P601_PL;
1064             vd->fmt.i_chroma = VLC_CODEC_I420;
1065             sys->u.evas.b_yuv = true;
1066             break;
1067     }
1068
1069     evas_object_image_colorspace_set( sys->p_evas, sys->u.evas.i_colorspace );
1070     evas_object_image_data_set( sys->p_evas, NULL );
1071
1072     /* No rotation support with EvasImage */
1073     sys->b_apply_rotation = true;
1074     FmtUpdate( vd );
1075
1076     /* No aspect ratio support with EvasImage */
1077     vd->info.has_pictures_invalid = true;
1078
1079     sys->pf_set_data = EvasImageSetData;
1080     sys->pf_buffers_alloc = EvasImageBuffersAlloc;
1081     sys->pf_buffers_free = EvasImageBuffersFree;
1082
1083     msg_Dbg( vd, "using evas_image" );
1084     return 0;
1085 }
1086
1087 #ifdef HAVE_TIZEN_SDK
1088
1089 struct tbm_format_to_vlc
1090 {
1091    tbm_format  i_tbm_format;
1092    vlc_fourcc_t i_vlc_chroma;
1093 };
1094
1095 struct tbm_format_to_vlc tbm_format_to_vlc_list[] = {
1096    { TBM_FORMAT_NV12, VLC_CODEC_NV12 },
1097    { TBM_FORMAT_YUV420, VLC_CODEC_I420 },
1098    { TBM_FORMAT_BGRA8888, VLC_CODEC_RGB32 },
1099 };
1100 #define TBM_FORMAT_TO_VLC_LIST_COUNT \
1101   ( sizeof(tbm_format_to_vlc_list) / sizeof(struct tbm_format_to_vlc) )
1102
1103 static bool
1104 EvasIsOpenGLSupported( vout_display_t *vd )
1105 {
1106     vout_display_sys_t *sys = vd->sys;
1107     Evas *p_canvas = evas_object_evas_get(sys->p_evas);
1108     Eina_List *p_engine_list, *p_l;
1109     int i_render_id;
1110     char *psz_render_name;
1111     bool b_is_gl = false;
1112
1113     if( !p_canvas )
1114         return false;
1115     i_render_id = evas_output_method_get( p_canvas );
1116
1117     p_engine_list = evas_render_method_list();
1118     if( !p_engine_list )
1119         return false;
1120
1121     EINA_LIST_FOREACH( p_engine_list, p_l, psz_render_name )
1122     {
1123         if( evas_render_method_lookup( psz_render_name ) == i_render_id )
1124         {
1125             b_is_gl = strncmp( psz_render_name, "gl", 2 ) == 0;
1126             break;
1127         }
1128     }
1129
1130     evas_render_method_list_free( p_engine_list );
1131     return b_is_gl;
1132 }
1133
1134 static int
1135 TbmSurfaceBufferLock( struct buffer *p_buffer )
1136 {
1137     tbm_surface_info_s tbm_surface_info;
1138     if( tbm_surface_map( p_buffer->p_tbm_surface, TBM_SURF_OPTION_WRITE,
1139                          &tbm_surface_info ) )
1140         return -1;
1141
1142     for( unsigned i = 0; i < tbm_surface_info.num_planes; ++i )
1143         p_buffer->p[i] = tbm_surface_info.planes[i].ptr;
1144     return 0;
1145 }
1146
1147 static int
1148 TbmSurfaceBufferUnlock( struct buffer *p_buffer )
1149 {
1150     tbm_surface_unmap( p_buffer->p_tbm_surface );
1151     p_buffer->p[0] = NULL;
1152     return 0;
1153 }
1154
1155 static int
1156 TbmSurfaceSetData( vout_display_t *vd )
1157 {
1158     vout_display_sys_t *sys = vd->sys;
1159     Evas_Native_Surface surf;
1160
1161     TbmSurfaceBufferUnlock( sys->p_new_buffer );
1162
1163     surf.version = EVAS_NATIVE_SURFACE_VERSION;
1164     surf.type = EVAS_NATIVE_SURFACE_TBM;
1165     surf.data.tizen.buffer = sys->p_new_buffer->p_tbm_surface;
1166     surf.data.tizen.rot = sys->u.tbm.i_angle;
1167     surf.data.tizen.ratio = 0;
1168     surf.data.tizen.flip = 0;
1169     evas_object_image_native_surface_set( sys->p_evas, &surf );
1170
1171     if( sys->p_current_buffer )
1172         TbmSurfaceBufferLock( sys->p_current_buffer );
1173     return 0;
1174 }
1175
1176 static void
1177 TbmSurfaceBuffersFree( vout_display_t *vd )
1178 {
1179     vout_display_sys_t *sys = vd->sys;
1180
1181     for( unsigned int i = 0; i < sys->i_nb_buffers; i++ )
1182     {
1183         if( sys->p_buffers[i].p[0] )
1184             tbm_surface_unmap( sys->p_buffers[i].p_tbm_surface );
1185         tbm_surface_destroy( sys->p_buffers[i].p_tbm_surface );
1186     }
1187     free( sys->p_buffers );
1188     sys->p_buffers = NULL;
1189     sys->i_nb_buffers = 0;
1190     sys->i_nb_planes = 0;
1191 }
1192
1193 static int
1194 TbmSurfaceBuffersAllocMainloopCb( vout_display_t *vd )
1195 {
1196     vout_display_sys_t *sys = vd->sys;
1197     tbm_surface_info_s tbm_surface_info;
1198
1199     sys->i_nb_buffers = 2;
1200
1201     if( !( sys->p_buffers = calloc( sys->i_nb_buffers, sizeof(struct buffer) ) ) )
1202         return -1;
1203
1204     for( unsigned i = 0; i < sys->i_nb_buffers; ++i )
1205     {
1206         struct buffer *p_buffer = &sys->p_buffers[i];
1207         tbm_surface_h p_tbm_surface = tbm_surface_create( sys->i_width,
1208                                                           sys->i_height,
1209                                                           sys->u.tbm.i_format );
1210         if( !p_tbm_surface
1211          || tbm_surface_get_info( p_tbm_surface, &tbm_surface_info ) )
1212         {
1213             tbm_surface_destroy( p_tbm_surface );
1214             p_tbm_surface = NULL;
1215         }
1216
1217         if( !p_tbm_surface )
1218         {
1219             sys->i_nb_buffers = i;
1220             break;
1221         }
1222         p_buffer->p_tbm_surface = p_tbm_surface;
1223         TbmSurfaceBufferLock( p_buffer );
1224     }
1225
1226     sys->i_nb_planes = tbm_surface_info.num_planes;
1227     for( unsigned i = 0; i < tbm_surface_info.num_planes; ++i )
1228     {
1229         sys->p_planes[i].i_lines = tbm_surface_info.planes[i].size
1230                                  / tbm_surface_info.planes[i].stride;
1231         sys->p_planes[i].i_visible_lines = sys->p_planes[i].i_lines;
1232         sys->p_planes[i].i_pitch = tbm_surface_info.planes[i].stride;
1233         sys->p_planes[i].i_visible_pitch = sys->p_planes[i].i_pitch;
1234     }
1235
1236     return 0;
1237 }
1238
1239 static int
1240 TbmSurfaceBuffersAlloc( vout_display_t *vd, video_format_t *p_fmt )
1241 {
1242     (void) p_fmt;
1243     return EcoreMainLoopCallSync( vd, TbmSurfaceBuffersAllocMainloopCb );
1244 }
1245
1246 static int
1247 TbmSurfaceSetup( vout_display_t *vd )
1248 {
1249     vout_display_sys_t *sys = vd->sys;
1250     tbm_format i_tbm_format = 0;
1251     bool b_found = false;
1252     uint32_t *p_formats;
1253     uint32_t i_format_num;
1254
1255     for( unsigned int i = 0; i < TBM_FORMAT_TO_VLC_LIST_COUNT; ++i )
1256     {
1257         if( tbm_format_to_vlc_list[i].i_vlc_chroma == vd->fmt.i_chroma )
1258         {
1259             i_tbm_format = tbm_format_to_vlc_list[i].i_tbm_format;
1260             break;
1261         }
1262      }
1263      if( !i_tbm_format )
1264      {
1265         msg_Err( vd, "no tbm format found" );
1266         return -1;
1267      }
1268
1269     if( tbm_surface_query_formats( &p_formats, &i_format_num ) )
1270     {
1271         msg_Warn( vd, "tbm_surface_query_formats failed" );
1272         return -1;
1273     }
1274
1275     for( unsigned int i = 0; i < i_format_num; i++ )
1276     {
1277         if( p_formats[i] == i_tbm_format )
1278         {
1279             b_found = true;
1280             break;
1281         }
1282     }
1283     if( !b_found )
1284     {
1285         if( i_tbm_format != TBM_FORMAT_YUV420 )
1286         {
1287             msg_Warn( vd, "vlc format not matching any tbm format: trying with I420");
1288             i_tbm_format = TBM_FORMAT_YUV420;
1289             for( uint32_t i = 0; i < i_format_num; i++ )
1290             {
1291                 if( p_formats[i] == i_tbm_format )
1292                 {
1293                     vd->fmt.i_chroma = VLC_CODEC_I420;
1294                     b_found = true;
1295                     break;
1296                 }
1297             }
1298         }
1299     }
1300     free( p_formats );
1301
1302     if( !b_found )
1303     {
1304         msg_Warn( vd, "can't find any compatible tbm format" );
1305         return -1;
1306     }
1307     sys->u.tbm.i_format = i_tbm_format;
1308
1309     switch( vd->fmt.orientation )
1310     {
1311         case ORIENT_ROTATED_90:
1312             sys->u.tbm.i_angle = 270;
1313             break;
1314         case ORIENT_ROTATED_180:
1315             sys->u.tbm.i_angle = 180;
1316             break;
1317         case ORIENT_ROTATED_270:
1318             sys->u.tbm.i_angle = 90;
1319             break;
1320         default:
1321             sys->u.tbm.i_angle = 0;
1322     }
1323
1324     sys->b_apply_rotation = false;
1325
1326     FmtUpdate( vd );
1327
1328     vd->info.has_pictures_invalid = true;
1329
1330     sys->pf_set_data = TbmSurfaceSetData;
1331     sys->pf_buffers_alloc = TbmSurfaceBuffersAlloc;
1332     sys->pf_buffers_free = TbmSurfaceBuffersFree;
1333
1334     msg_Dbg( vd, "using tbm_surface" );
1335
1336     return 0;
1337 }
1338 #endif