Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.33 KB | None | 0 0
  1.  
  2. /*
  3.  * code stolen from openGL-RPi-tutorial-master/encode_OGL/
  4.  
  5.  cc -g -D_GNU_SOURCE -DUSE_OPENGL -DUSE_EGL -DIS_RPI -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I/opt/vc/src/hello_pi/libs/ilclient -I/opt/vc/src/hello_pi/libs/vgfont -g -c rectangle.c -o rectangle.o -Wno-deprecated-declarations && cc -o rectangle -Wl,--whole-archive rectangle.o -L/opt/vc/lib/ -lGLESv2 -lEGL -lbcm_host -lvcos -lvchiq_arm -lpthread -lrt -Wl,-rpath=./libs -lmpv -L/opt/vc/src/hello_pi/libs/vgfont -ldl -lm -Wl,--no-whole-archive -rdynamic && ./rectangle
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <math.h>
  11.  
  12. #include <EGL/egl.h>
  13. #include <EGL/eglext.h>
  14. #include <GLES2/gl2.h>
  15. #include <GLES2/gl2ext.h>
  16.  
  17. #include <mpv/client.h>
  18. #include <mpv/opengl_cb.h>
  19.  
  20. static void die (int line_number, const char * format, ...)
  21. {
  22.     va_list vargs;
  23.     va_start (vargs, format);
  24.     fprintf (stderr, "%d: ", line_number);
  25.     vfprintf (stderr, format, vargs);
  26.     fprintf (stderr, ".\n");
  27.     exit (1);
  28. }
  29.  
  30. static void *get_proc_address(const GLubyte *name)
  31. {
  32.     void *p = eglGetProcAddress(name);
  33.     // EGL 1.4 (supported by the RPI firmware) does not necessarily return
  34.     // function pointers for core functions.
  35.     if (!p)
  36.         p = dlsym(RTLD_DEFAULT, name);
  37.     return p;
  38. }
  39.  
  40. static void *get_proc_address_mpv(void *fn_ctx, const char *name)
  41. {
  42.     return get_proc_address(name);
  43. }
  44.  
  45. typedef struct
  46. {
  47.     uint32_t screen_width;
  48.     uint32_t screen_height;
  49.  
  50.     EGLDisplay display;
  51.     EGLSurface surface;
  52.     EGLContext context;
  53. } CUBE_STATE_T;
  54.  
  55.  
  56. CUBE_STATE_T state, *p_state = &state;
  57.  
  58. void init_ogl(CUBE_STATE_T *state)
  59. {
  60.     int32_t success = 0;
  61.     EGLBoolean result;
  62.     EGLint num_config;
  63.  
  64.     bcm_host_init();
  65.  
  66.     static EGL_DISPMANX_WINDOW_T nativewindow;
  67.  
  68.     DISPMANX_ELEMENT_HANDLE_T dispman_element;
  69.     DISPMANX_DISPLAY_HANDLE_T dispman_display;
  70.     DISPMANX_UPDATE_HANDLE_T dispman_update;
  71.     VC_RECT_T dst_rect;
  72.     VC_RECT_T src_rect;
  73.  
  74.     static const EGLint attribute_list[] =
  75.     {
  76.         EGL_RED_SIZE, 8,
  77.         EGL_GREEN_SIZE, 8,
  78.         EGL_BLUE_SIZE, 8,
  79.         EGL_ALPHA_SIZE, 8,
  80.         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  81.         EGL_NONE
  82.     };
  83.  
  84.     static const EGLint context_attributes[] =
  85.     {
  86.         EGL_CONTEXT_CLIENT_VERSION, 2,
  87.         EGL_NONE
  88.     };
  89.  
  90.     EGLConfig config;
  91.  
  92.     // get an EGL display connection
  93.     state->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  94.  
  95.     // initialize the EGL display connection
  96.     result = eglInitialize(state->display, NULL, NULL);
  97.  
  98.     // get an appropriate EGL frame buffer configuration
  99.     result = eglChooseConfig(state->display, attribute_list, &config, 1, &num_config);
  100.     assert(EGL_FALSE != result);
  101.  
  102.     // get an appropriate EGL frame buffer configuration
  103.     result = eglBindAPI(EGL_OPENGL_ES_API);
  104.     assert(EGL_FALSE != result);
  105.  
  106.  
  107.     // create an EGL rendering context
  108.     state->context = eglCreateContext(state->display, config, EGL_NO_CONTEXT, context_attributes);
  109.     assert(state->context!=EGL_NO_CONTEXT);
  110.  
  111.     // create an EGL window surface
  112.     success = graphics_get_display_size(0 /* LCD */, &state->screen_width, &state->screen_height);
  113.     assert( success >= 0 );
  114.  
  115.     state->screen_width = 1024;
  116.  
  117.     state->screen_height = 1024;
  118.  
  119.     dst_rect.x = 0;
  120.     dst_rect.y = 0;
  121.     dst_rect.width = state->screen_width;
  122.     dst_rect.height = state->screen_height;
  123.  
  124.     src_rect.x = 0;
  125.     src_rect.y = 0;
  126.     src_rect.width = state->screen_width << 16;
  127.     src_rect.height = state->screen_height << 16;        
  128.  
  129.     dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
  130.     dispman_update = vc_dispmanx_update_start( 0 );
  131.  
  132.     dispman_element =
  133.     vc_dispmanx_element_add(dispman_update, dispman_display,
  134.                 0/*layer*/, &dst_rect, 0/*src*/,
  135.                 &src_rect, DISPMANX_PROTECTION_NONE,
  136.                 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);
  137.  
  138.     nativewindow.element = dispman_element;
  139.     nativewindow.width = state->screen_width;
  140.     nativewindow.height = state->screen_height;
  141.     vc_dispmanx_update_submit_sync( dispman_update );
  142.  
  143.     state->surface = eglCreateWindowSurface( state->display, config, &nativewindow, NULL );
  144.     assert(state->surface != EGL_NO_SURFACE);
  145.  
  146.     // connect the context to the surface
  147.     result = eglMakeCurrent(state->display, state->surface, state->surface, state->context);
  148.     assert(EGL_FALSE != result);
  149. }
  150.  
  151. int
  152. main(int argc, char *argv[])
  153. {
  154.  
  155.     bcm_host_init();
  156.  
  157.     init_ogl(p_state);
  158.    
  159.     // MPV
  160.     mpv_handle *mpv = mpv_create();
  161.     if (!mpv) die(143, "context init failed");
  162.     if (mpv_initialize(mpv) < 0) die(144, "mpv init failed");
  163.    
  164.     // The OpenGL API is somewhat separate from the normal mpv API. This only
  165.     // returns NULL if no OpenGL support is compiled.
  166.     mpv_opengl_cb_context *mpv_gl = mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB);
  167.     if (!mpv_gl) die(149, "failed to create mpv GL API handle");
  168.    
  169.     // This makes mpv use the currently set GL context. It will use the callback
  170.     // to resolve GL builtin functions, as well as extensions.
  171.     int err = mpv_opengl_cb_init_gl(mpv_gl, NULL, get_proc_address_mpv, NULL);
  172.     if (err < 0) die(err, "failed to initialize mpv GL context ");
  173.    
  174.     if (mpv_set_option_string(mpv, "vo", "opengl-cb") < 0) die(162, "failed to set VO");
  175.    
  176.     const char *cmd[] = {"loadfile", "/home/pi/media/bbb.mp4", NULL};
  177.     mpv_command(mpv, cmd);
  178.  
  179.     glClearColor(1.0, 0.0, 0.0, 1.0);
  180.     glClear(GL_COLOR_BUFFER_BIT);
  181.     glFlush();
  182.    
  183.     eglSwapBuffers(p_state->display, p_state->surface);
  184.    
  185.     while (1) {
  186.         //glClearColor(1.0, 0.0, 0.0, 1.0);
  187.         //glClear(GL_COLOR_BUFFER_BIT);
  188.         mpv_opengl_cb_draw(mpv_gl, 0, 800, -600);
  189.         //glFlush();
  190.         eglSwapBuffers(p_state->display, p_state->surface);
  191.         //sleep(1);
  192.         /*
  193.         glClearColor(0.0, 1.0, 0.0, 1.0);
  194.         glClear(GL_COLOR_BUFFER_BIT);
  195.         glFlush();
  196.         mpv_opengl_cb_draw(mpv_gl, 0, 800, -600);
  197.         eglSwapBuffers(p_state->display, p_state->surface);
  198.         sleep(1);
  199.         */
  200.     }
  201.  
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement