mirrored from git://anongit.freedesktop.org/gstreamer/gst-omx
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtestegl.c
1611 lines (1348 loc) · 44.6 KB
/
testegl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, Broadcom Europe Ltd
Copyright (c) 2012, OtherCrashOverride
Copyright (C) 2013, Fluendo S.A.
@author: Josep Torra <josep@fluendo.com>
Copyright (C) 2013, Video Experts Group LLC.
@author: Ilya Smelykh <ilya@videoexpertsgroup.com>
Copyright (C) 2014 Julien Isorce <julien.isorce@collabora.co.uk>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* A rotating cube rendered with OpenGL|ES and video played using GStreamer on
* the cube faces.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined (USE_OMX_TARGET_RPI) && defined (__GNUC__)
#ifndef __VCCOREVER__
#define __VCCOREVER__ 0x04000000
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
#pragma GCC optimize ("gnu89-inline")
#endif
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
#include <gst/gst.h>
#define GST_USE_UNSTABLE_API
#include <gst/gl/gl.h>
#include <gst/gl/egl/gstgldisplay_egl.h>
#if defined (USE_OMX_TARGET_RPI)
#include <bcm_host.h>
#include <EGL/eglext.h>
#elif defined(HAVE_X11)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
#if defined (USE_OMX_TARGET_RPI) && defined (__GNUC__)
#pragma GCC reset_options
#pragma GCC diagnostic pop
#endif
#include "cube_texture_and_coords.h"
#ifndef M_PI
#define M_PI 3.141592654
#endif
#define SYNC_BUFFERS TRUE
#define TRACE_VC_MEMORY_ENABLED 0
#if defined (USE_OMX_TARGET_RPI) && TRACE_VC_MEMORY_ENABLED
#define TRACE_VC_MEMORY(str) \
fprintf (stderr, "\n\n" str "\n"); \
system ("vcdbg reloc >&2")
#define TRACE_VC_MEMORY_DEFINE_ID(id) \
static int id = 0
#define TRACE_VC_MEMORY_RESET_ID(id) \
G_STMT_START { \
id = 0; \
} G_STMT_END
#define TRACE_VC_MEMORY_ONCE_FOR_ID(str,id) \
G_STMT_START { \
if (id == 0) { \
fprintf (stderr, "\n\n" str "\n"); \
system ("vcdbg reloc >&2"); \
id = 1; \
} \
} G_STMT_END
#define TRACE_VC_MEMORY_ONCE(str,id) \
G_STMT_START { \
static int id = 0; \
if (id == 0) { \
fprintf (stderr, "\n\n" str "\n"); \
system ("vcdbg reloc >&2"); \
id = 1; \
} \
} G_STMT_END
#else
#define TRACE_VC_MEMORY(str) while(0)
#define TRACE_VC_MEMORY_DEFINE_ID(id)
#define TRACE_VC_MEMORY_RESET_ID(id) while(0)
#define TRACE_VC_MEMORY_ONCE_FOR_ID(str,id) while(0)
#define TRACE_VC_MEMORY_ONCE(str,id) while(0)
#endif
/* some helpers that we should provide in libgstgl */
typedef struct
{
GLfloat m[4][4];
} GstGLMatrix;
static void
gst_gl_matrix_load_identity (GstGLMatrix * matrix)
{
memset (matrix, 0x0, sizeof (GstGLMatrix));
matrix->m[0][0] = 1.0f;
matrix->m[1][1] = 1.0f;
matrix->m[2][2] = 1.0f;
matrix->m[3][3] = 1.0f;
}
static void
gst_gl_matrix_multiply (GstGLMatrix * matrix, GstGLMatrix * srcA,
GstGLMatrix * srcB)
{
GstGLMatrix tmp;
int i;
for (i = 0; i < 4; i++) {
tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) +
(srcA->m[i][1] * srcB->m[1][0]) +
(srcA->m[i][2] * srcB->m[2][0]) + (srcA->m[i][3] * srcB->m[3][0]);
tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) +
(srcA->m[i][1] * srcB->m[1][1]) +
(srcA->m[i][2] * srcB->m[2][1]) + (srcA->m[i][3] * srcB->m[3][1]);
tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) +
(srcA->m[i][1] * srcB->m[1][2]) +
(srcA->m[i][2] * srcB->m[2][2]) + (srcA->m[i][3] * srcB->m[3][2]);
tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) +
(srcA->m[i][1] * srcB->m[1][3]) +
(srcA->m[i][2] * srcB->m[2][3]) + (srcA->m[i][3] * srcB->m[3][3]);
}
memcpy (matrix, &tmp, sizeof (GstGLMatrix));
}
static void
gst_gl_matrix_translate (GstGLMatrix * matrix, GLfloat tx, GLfloat ty,
GLfloat tz)
{
matrix->m[3][0] +=
(matrix->m[0][0] * tx + matrix->m[1][0] * ty + matrix->m[2][0] * tz);
matrix->m[3][1] +=
(matrix->m[0][1] * tx + matrix->m[1][1] * ty + matrix->m[2][1] * tz);
matrix->m[3][2] +=
(matrix->m[0][2] * tx + matrix->m[1][2] * ty + matrix->m[2][2] * tz);
matrix->m[3][3] +=
(matrix->m[0][3] * tx + matrix->m[1][3] * ty + matrix->m[2][3] * tz);
}
static void
gst_gl_matrix_frustum (GstGLMatrix * matrix, GLfloat left, GLfloat right,
GLfloat bottom, GLfloat top, GLfloat nearZ, GLfloat farZ)
{
GLfloat deltaX = right - left;
GLfloat deltaY = top - bottom;
GLfloat deltaZ = farZ - nearZ;
GstGLMatrix frust;
if ((nearZ <= 0.0f) || (farZ <= 0.0f) ||
(deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f))
return;
frust.m[0][0] = 2.0f * nearZ / deltaX;
frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;
frust.m[1][1] = 2.0f * nearZ / deltaY;
frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;
frust.m[2][0] = (right + left) / deltaX;
frust.m[2][1] = (top + bottom) / deltaY;
frust.m[2][2] = -(nearZ + farZ) / deltaZ;
frust.m[2][3] = -1.0f;
frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ;
frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f;
gst_gl_matrix_multiply (matrix, &frust, matrix);
}
static void
gst_gl_matrix_perspective (GstGLMatrix * matrix, GLfloat fovy, GLfloat aspect,
GLfloat nearZ, GLfloat farZ)
{
GLfloat frustumW, frustumH;
frustumH = tanf (fovy / 360.0f * M_PI) * nearZ;
frustumW = frustumH * aspect;
gst_gl_matrix_frustum (matrix, -frustumW, frustumW, -frustumH, frustumH,
nearZ, farZ);
}
/* *INDENT-OFF* */
/* vertex source */
static const gchar *cube_v_src =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"uniform float u_rotx; \n"
"uniform float u_roty; \n"
"uniform float u_rotz; \n"
"uniform mat4 u_modelview; \n"
"uniform mat4 u_projection; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" float PI = 3.14159265; \n"
" float xrot = u_rotx*2.0*PI/360.0; \n"
" float yrot = u_roty*2.0*PI/360.0; \n"
" float zrot = u_rotz*2.0*PI/360.0; \n"
" mat4 matX = mat4 ( \n"
" 1.0, 0.0, 0.0, 0.0, \n"
" 0.0, cos(xrot), sin(xrot), 0.0, \n"
" 0.0, -sin(xrot), cos(xrot), 0.0, \n"
" 0.0, 0.0, 0.0, 1.0 ); \n"
" mat4 matY = mat4 ( \n"
" cos(yrot), 0.0, -sin(yrot), 0.0, \n"
" 0.0, 1.0, 0.0, 0.0, \n"
" sin(yrot), 0.0, cos(yrot), 0.0, \n"
" 0.0, 0.0, 0.0, 1.0 ); \n"
" mat4 matZ = mat4 ( \n"
" cos(zrot), sin(zrot), 0.0, 0.0, \n"
" -sin(zrot), cos(zrot), 0.0, 0.0, \n"
" 0.0, 0.0, 1.0, 0.0, \n"
" 0.0, 0.0, 0.0, 1.0 ); \n"
" gl_Position = u_projection * u_modelview * matZ * matY * matX * a_position;\n"
" v_texCoord = a_texCoord; \n"
"} \n";
/* fragment source */
static const gchar *cube_f_src =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D (s_texture, v_texCoord); \n"
"} \n";
/* *INDENT-ON* */
typedef struct
{
#if defined (USE_OMX_TARGET_RPI)
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
#endif
uint32_t screen_width;
uint32_t screen_height;
gboolean animate;
GstCaps *caps;
/* OpenGL|ES objects */
EGLDisplay display;
EGLSurface surface;
EGLContext context;
GLuint tex;
GLint vshader;
GLint fshader;
GLint program;
GLint u_modelviewmatrix;
GLint u_projectionmatrix;
GLint s_texture;
GLint u_rotx;
GLint u_roty;
GLint u_rotz;
GstGLMatrix modelview;
GstGLMatrix projection;
GLfloat fov;
GLfloat aspect;
/* model rotation vector and direction */
GLfloat rot_angle_x_inc;
GLfloat rot_angle_y_inc;
GLfloat rot_angle_z_inc;
/* current model rotation angles */
GLfloat rot_angle_x;
GLfloat rot_angle_y;
GLfloat rot_angle_z;
/* current distance from camera */
GLfloat distance;
GLfloat distance_inc;
/* GStreamer related resources */
GstElement *pipeline;
GstElement *vsink;
GstGLDisplayEGL *gst_display;
GstGLContext *gl_context;
gboolean can_avoid_upload;
/* Interthread comunication */
GAsyncQueue *queue;
GMutex queue_lock;
GCond cond;
gboolean flushing;
GstMiniObject *popped_obj;
GstBuffer *current_buffer;
/* GLib mainloop */
GMainLoop *main_loop;
GstBuffer *last_buffer;
/* Rendering thread state */
gboolean running;
/* number of rendered and dropped frames */
guint64 rendered;
guint64 dropped;
#if !defined (USE_OMX_TARGET_RPI) && defined(HAVE_X11)
Display *xdisplay;
Window xwindow;
#endif
} APP_STATE_T;
static void init_ogl (APP_STATE_T * state);
static void init_model_proj (APP_STATE_T * state);
static void reset_model (APP_STATE_T * state);
static GLfloat inc_and_wrap_angle (GLfloat angle, GLfloat angle_inc);
static void redraw_scene (APP_STATE_T * state);
static void update_model (APP_STATE_T * state);
static void init_textures (APP_STATE_T * state, GstBuffer * buffer);
static APP_STATE_T _state, *state = &_state;
static gboolean queue_object (APP_STATE_T * state, GstMiniObject * obj,
gboolean synchronous);
TRACE_VC_MEMORY_DEFINE_ID (gid0);
TRACE_VC_MEMORY_DEFINE_ID (gid1);
TRACE_VC_MEMORY_DEFINE_ID (gid2);
typedef enum
{
GST_PLAY_FLAG_VIDEO = (1 << 0),
GST_PLAY_FLAG_AUDIO = (1 << 1),
GST_PLAY_FLAG_TEXT = (1 << 2),
GST_PLAY_FLAG_VIS = (1 << 3),
GST_PLAY_FLAG_SOFT_VOLUME = (1 << 4),
GST_PLAY_FLAG_NATIVE_AUDIO = (1 << 5),
GST_PLAY_FLAG_NATIVE_VIDEO = (1 << 6),
GST_PLAY_FLAG_DOWNLOAD = (1 << 7),
GST_PLAY_FLAG_BUFFERING = (1 << 8),
GST_PLAY_FLAG_DEINTERLACE = (1 << 9),
GST_PLAY_FLAG_SOFT_COLORBALANCE = (1 << 10)
} GstPlayFlags;
/***********************************************************
* Name: init_ogl
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Sets the display, OpenGL|ES context and screen stuff
*
* Returns: void
*
***********************************************************/
static void
init_ogl (APP_STATE_T * state)
{
#if defined (USE_OMX_TARGET_RPI)
int32_t success = 0;
#else
gint screen_num = 0;
gulong black_pixel = 0;
#endif
EGLBoolean result;
EGLint num_config;
EGLNativeWindowType window_handle = (EGLNativeWindowType) 0;
#if defined (USE_OMX_TARGET_RPI)
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_UPDATE_HANDLE_T dispman_update;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS, 255, 0 };
#endif
static const EGLint attribute_list[] = {
EGL_DEPTH_SIZE, 16,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
static const EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig config;
/* get an EGL display connection */
state->display = eglGetDisplay (EGL_DEFAULT_DISPLAY);
assert (state->display != EGL_NO_DISPLAY);
/* initialize the EGL display connection */
result = eglInitialize (state->display, NULL, NULL);
assert (EGL_FALSE != result);
#if defined (USE_OMX_TARGET_RPI)
/* get an appropriate EGL frame buffer configuration
* this uses a BRCM extension that gets the closest match, rather
* than standard which returns anything that matches. */
result =
eglSaneChooseConfigBRCM (state->display, attribute_list, &config, 1,
&num_config);
assert (EGL_FALSE != result);
#else
result =
eglChooseConfig (state->display, attribute_list, &config, 1, &num_config);
#endif
/* create an EGL rendering context */
state->context =
eglCreateContext (state->display, config, EGL_NO_CONTEXT,
context_attributes);
assert (state->context != EGL_NO_CONTEXT);
#if defined (USE_OMX_TARGET_RPI)
/* create an EGL window surface */
success = graphics_get_display_size (0 /* LCD */ , &state->screen_width,
&state->screen_height);
assert (success >= 0);
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = state->screen_width;
dst_rect.height = state->screen_height;
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = state->screen_width << 16;
src_rect.height = state->screen_height << 16;
state->dispman_display = vc_dispmanx_display_open (0 /* LCD */ );
dispman_update = vc_dispmanx_update_start (0);
state->dispman_element =
vc_dispmanx_element_add (dispman_update, state->dispman_display,
0 /*layer */ , &dst_rect, 0 /*src */ ,
&src_rect, DISPMANX_PROTECTION_NONE, &alpha, 0 /*clamp */ ,
0 /*transform */ );
nativewindow.element = state->dispman_element;
nativewindow.width = state->screen_width;
nativewindow.height = state->screen_height;
vc_dispmanx_update_submit_sync (dispman_update);
window_handle = &nativewindow;
#elif defined(HAVE_X11)
state->screen_width = 1280;
state->screen_height = 720;
state->xdisplay = XOpenDisplay (NULL);
screen_num = DefaultScreen (state->xdisplay);
black_pixel = XBlackPixel (state->xdisplay, screen_num);
state->xwindow = XCreateSimpleWindow (state->xdisplay,
DefaultRootWindow (state->xdisplay), 0, 0, state->screen_width,
state->screen_height, 0, 0, black_pixel);
XSetWindowBackgroundPixmap (state->xdisplay, state->xwindow, None);
XMapRaised (state->xdisplay, state->xwindow);
XSync (state->xdisplay, FALSE);
window_handle = state->xwindow;
#endif
state->surface =
eglCreateWindowSurface (state->display, config, window_handle, NULL);
assert (state->surface != EGL_NO_SURFACE);
/* connect the context to the surface */
result =
eglMakeCurrent (state->display, state->surface, state->surface,
state->context);
assert (EGL_FALSE != result);
state->gst_display = gst_gl_display_egl_new_with_egl_display (state->display);
state->gl_context =
gst_gl_context_new_wrapped (GST_GL_DISPLAY (state->gst_display),
(guintptr) state->context, GST_GL_PLATFORM_EGL, GST_GL_API_GLES2);
}
/***********************************************************
* Name: init_model_proj
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Sets the OpenGL|ES model to default values
*
* Returns: void
*
***********************************************************/
static void
init_model_proj (APP_STATE_T * state)
{
GLint ret = 0;
state->vshader = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (state->vshader, 1, &cube_v_src, NULL);
glCompileShader (state->vshader);
assert (glGetError () == GL_NO_ERROR);
state->fshader = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (state->fshader, 1, &cube_f_src, NULL);
glCompileShader (state->fshader);
assert (glGetError () == GL_NO_ERROR);
state->program = glCreateProgram ();
glAttachShader (state->program, state->vshader);
glAttachShader (state->program, state->fshader);
glBindAttribLocation (state->program, 0, "a_position");
glBindAttribLocation (state->program, 1, "a_texCoord");
glLinkProgram (state->program);
glGetProgramiv (state->program, GL_LINK_STATUS, &ret);
assert (ret == GL_TRUE);
glUseProgram (state->program);
state->u_rotx = glGetUniformLocation (state->program, "u_rotx");
state->u_roty = glGetUniformLocation (state->program, "u_roty");
state->u_rotz = glGetUniformLocation (state->program, "u_rotz");
state->u_modelviewmatrix =
glGetUniformLocation (state->program, "u_modelview");
state->u_projectionmatrix =
glGetUniformLocation (state->program, "u_projection");
state->s_texture = glGetUniformLocation (state->program, "s_texture");
glViewport (0, 0, (GLsizei) state->screen_width,
(GLsizei) state->screen_height);
state->fov = 45.0f;
state->distance = 5.0f;
state->aspect =
(GLfloat) state->screen_width / (GLfloat) state->screen_height;
gst_gl_matrix_load_identity (&state->projection);
gst_gl_matrix_perspective (&state->projection, state->fov, state->aspect,
1.0f, 100.0f);
gst_gl_matrix_load_identity (&state->modelview);
gst_gl_matrix_translate (&state->modelview, 0.0f, 0.0f, -state->distance);
reset_model (state);
}
/***********************************************************
* Name: reset_model
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Resets the Model projection and rotation direction
*
* Returns: void
*
***********************************************************/
static void
reset_model (APP_STATE_T * state)
{
/* reset model rotation */
state->rot_angle_x = 45.f;
state->rot_angle_y = 30.f;
state->rot_angle_z = 0.f;
state->rot_angle_x_inc = 0.5f;
state->rot_angle_y_inc = 0.5f;
state->rot_angle_z_inc = 0.f;
}
/***********************************************************
* Name: update_model
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Updates model projection to current position/rotation
*
* Returns: void
*
***********************************************************/
static void
update_model (APP_STATE_T * state)
{
if (state->animate) {
/* update position */
state->rot_angle_x =
inc_and_wrap_angle (state->rot_angle_x, state->rot_angle_x_inc);
state->rot_angle_y =
inc_and_wrap_angle (state->rot_angle_y, state->rot_angle_y_inc);
state->rot_angle_z =
inc_and_wrap_angle (state->rot_angle_z, state->rot_angle_z_inc);
}
}
/***********************************************************
* Name: inc_and_wrap_angle
*
* Arguments:
* GLfloat angle current angle
* GLfloat angle_inc angle increment
*
* Description: Increments or decrements angle by angle_inc degrees
* Wraps to 0 at 360 deg.
*
* Returns: new value of angle
*
***********************************************************/
static GLfloat
inc_and_wrap_angle (GLfloat angle, GLfloat angle_inc)
{
angle += angle_inc;
if (angle >= 360.0)
angle -= 360.f;
else if (angle <= 0)
angle += 360.f;
return angle;
}
/***********************************************************
* Name: redraw_scene
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Draws the model and calls eglSwapBuffers
* to render to screen
*
* Returns: void
*
***********************************************************/
static void
redraw_scene (APP_STATE_T * state)
{
glBindFramebuffer (GL_FRAMEBUFFER, 0);
glEnable (GL_CULL_FACE);
glEnable (GL_DEPTH_TEST);
/* Set background color and clear buffers */
glClearColor (0.15f, 0.25f, 0.35f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (state->program);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, quadx);
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, state->tex);
glUniform1i (state->s_texture, 0);
glUniform1f (state->u_rotx, state->rot_angle_x);
glUniform1f (state->u_roty, state->rot_angle_y);
glUniform1f (state->u_rotz, state->rot_angle_z);
glUniformMatrix4fv (state->u_modelviewmatrix, 1, GL_FALSE,
&state->modelview.m[0][0]);
glUniformMatrix4fv (state->u_projectionmatrix, 1, GL_FALSE,
&state->projection.m[0][0]);
/* draw first 4 vertices */
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays (GL_TRIANGLE_STRIP, 4, 4);
glDrawArrays (GL_TRIANGLE_STRIP, 8, 4);
glDrawArrays (GL_TRIANGLE_STRIP, 12, 4);
glDrawArrays (GL_TRIANGLE_STRIP, 16, 4);
glDrawArrays (GL_TRIANGLE_STRIP, 20, 4);
if (!eglSwapBuffers (state->display, state->surface)) {
g_main_loop_quit (state->main_loop);
return;
}
glDisable (GL_DEPTH_TEST);
glDisable (GL_CULL_FACE);
}
/***********************************************************
* Name: init_textures
*
* Arguments:
* APP_STATE_T *state - holds OGLES model info
*
* Description: Initialise OGL|ES texture surfaces to use image
* buffers
*
* Returns: void
*
***********************************************************/
static void
init_textures (APP_STATE_T * state, GstBuffer * buffer)
{
GstCapsFeatures *feature = gst_caps_get_features (state->caps, 0);
if (gst_caps_features_contains (feature, "memory:GLMemory")) {
g_print ("Prepare texture for GLMemory\n");
state->can_avoid_upload = TRUE;
state->tex = 0;
} else if (gst_caps_features_contains (feature,
"meta:GstVideoGLTextureUploadMeta")) {
GstVideoMeta *meta = NULL;
guint internal_format =
gst_gl_sized_gl_format_from_gl_format_type (state->gl_context,
GL_RGBA, GL_UNSIGNED_BYTE);
g_print ("Prepare texture for GstVideoGLTextureUploadMeta\n");
meta = gst_buffer_get_video_meta (buffer);
state->can_avoid_upload = FALSE;
glGenTextures (1, &state->tex);
glBindTexture (GL_TEXTURE_2D, state->tex);
glTexImage2D (GL_TEXTURE_2D, 0, internal_format, meta->width, meta->height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
} else {
g_assert_not_reached ();
}
#if 0
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
#else
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#endif
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
assert (glGetError () == GL_NO_ERROR);
}
static void
render_scene (APP_STATE_T * state)
{
update_model (state);
redraw_scene (state);
TRACE_VC_MEMORY_ONCE_FOR_ID ("after render_scene", gid2);
}
static void
update_image (APP_STATE_T * state, GstBuffer * buffer)
{
GstVideoGLTextureUploadMeta *meta = NULL;
if (state->current_buffer) {
gst_buffer_unref (state->current_buffer);
} else {
/* Setup the model world */
init_model_proj (state);
TRACE_VC_MEMORY ("after init_model_proj");
/* initialize the OGLES texture(s) */
init_textures (state, buffer);
TRACE_VC_MEMORY ("after init_textures");
}
state->current_buffer = gst_buffer_ref (buffer);
TRACE_VC_MEMORY_ONCE_FOR_ID ("before GstVideoGLTextureUploadMeta", gid0);
if (state->can_avoid_upload) {
GstMemory *mem = gst_buffer_peek_memory (state->current_buffer, 0);
g_assert (gst_is_gl_memory (mem));
state->tex = ((GstGLMemory *) mem)->tex_id;
} else if ((meta = gst_buffer_get_video_gl_texture_upload_meta (buffer))) {
if (meta->n_textures == 1) {
guint ids[4] = { state->tex, 0, 0, 0 };
if (!gst_video_gl_texture_upload_meta_upload (meta, ids)) {
GST_WARNING ("failed to upload to texture");
}
}
}
TRACE_VC_MEMORY_ONCE_FOR_ID ("after GstVideoGLTextureUploadMeta", gid1);
}
static void
init_intercom (APP_STATE_T * state)
{
state->queue =
g_async_queue_new_full ((GDestroyNotify) gst_mini_object_unref);
g_mutex_init (&state->queue_lock);
g_cond_init (&state->cond);
}
static void
terminate_intercom (APP_STATE_T * state)
{
/* Release intercom */
if (state->queue) {
g_async_queue_unref (state->queue);
}
g_mutex_clear (&state->queue_lock);
g_cond_clear (&state->cond);
}
static void
flush_internal (APP_STATE_T * state)
{
if (state->current_buffer) {
gst_buffer_unref (state->current_buffer);
}
state->current_buffer = NULL;
}
static void
flush_start (APP_STATE_T * state)
{
GstMiniObject *object = NULL;
g_mutex_lock (&state->queue_lock);
state->flushing = TRUE;
g_cond_broadcast (&state->cond);
g_mutex_unlock (&state->queue_lock);
while ((object = g_async_queue_try_pop (state->queue))) {
gst_mini_object_unref (object);
}
g_mutex_lock (&state->queue_lock);
flush_internal (state);
state->popped_obj = NULL;
g_mutex_unlock (&state->queue_lock);
}
static void
flush_stop (APP_STATE_T * state)
{
GstMiniObject *object = NULL;
g_mutex_lock (&state->queue_lock);
while ((object = GST_MINI_OBJECT_CAST (g_async_queue_try_pop (state->queue)))) {
gst_mini_object_unref (object);
}
flush_internal (state);
state->popped_obj = NULL;
state->flushing = FALSE;
g_mutex_unlock (&state->queue_lock);
}
static void
pipeline_pause (APP_STATE_T * state)
{
gst_element_set_state (state->pipeline, GST_STATE_PAUSED);
}
static void
pipeline_play (APP_STATE_T * state)
{
gst_element_set_state (state->pipeline, GST_STATE_PLAYING);
}
static gint64
pipeline_get_position (APP_STATE_T * state)
{
gint64 position = -1;
if (state->pipeline) {
gst_element_query_position (state->vsink, GST_FORMAT_TIME, &position);
}
return position;
}
static gint64
pipeline_get_duration (APP_STATE_T * state)
{
gint64 duration = -1;
if (state->pipeline) {
gst_element_query_duration (state->pipeline, GST_FORMAT_TIME, &duration);
}
return duration;
}
static void
pipeline_seek (APP_STATE_T * state, gint64 position)
{
if (state->pipeline) {
GstEvent *event;
event = gst_event_new_seek (1.0,
GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
if (!gst_element_send_event (state->vsink, event)) {
g_print ("seek failed\n");
}
}
}
static gboolean
handle_queued_objects (APP_STATE_T * state)
{
GstMiniObject *object = NULL;
g_mutex_lock (&state->queue_lock);
if (state->flushing) {
g_cond_broadcast (&state->cond);
goto beach;
} else if (g_async_queue_length (state->queue) == 0) {
goto beach;
}
if ((object = g_async_queue_try_pop (state->queue))) {
if (GST_IS_BUFFER (object)) {
GstBuffer *buffer = GST_BUFFER_CAST (object);
update_image (state, buffer);
render_scene (state);
gst_buffer_unref (buffer);
if (!SYNC_BUFFERS) {
object = NULL;
}
} else if (GST_IS_EVENT (object)) {
GstEvent *event = GST_EVENT_CAST (object);
g_print ("\nevent %p %s\n", event,
gst_event_type_get_name (GST_EVENT_TYPE (event)));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
flush_internal (state);
break;
default:
break;
}
gst_event_unref (event);
object = NULL;
}
}
if (object) {
state->popped_obj = object;
g_cond_broadcast (&state->cond);
}
beach:
g_mutex_unlock (&state->queue_lock);
return FALSE;
}
static gboolean
queue_object (APP_STATE_T * state, GstMiniObject * obj, gboolean synchronous)