diff mbox

[FFmpeg-devel] lavd/opengl: use SDL2

Message ID 20160917223043.52603-1-josh@itanimul.li
State Superseded
Headers show

Commit Message

Josh de Kock Sept. 17, 2016, 10:30 p.m. UTC
I did this in about 5 minutes, and only tested it with one sample (on
OSX using the cocoa opengl renderer). Seems to work, but probably
won't for all cases. Would like some feedback where it doesnt work etc

Signed-off-by: Josh de Kock <josh@itanimul.li>
---
 libavdevice/opengl_enc.c | 87 +++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 41 deletions(-)

Comments

Lukasz Marek Sept. 18, 2016, 1:21 a.m. UTC | #1
On 18 September 2016 at 00:30, Josh de Kock <josh@itanimul.li> wrote:

> I did this in about 5 minutes, and only tested it with one sample (on
> OSX using the cocoa opengl renderer). Seems to work, but probably
> won't for all cases. Would like some feedback where it doesnt work etc
>
> Signed-off-by: Josh de Kock <josh@itanimul.li>
> ---
>  libavdevice/opengl_enc.c | 87 +++++++++++++++++++++++++-----
> ------------------
>  1 file changed, 46 insertions(+), 41 deletions(-)
>
> diff --git a/libavdevice/opengl_enc.c b/libavdevice/opengl_enc.c
> index 1dbbb80..1917459 100644
> --- a/libavdevice/opengl_enc.c
> +++ b/libavdevice/opengl_enc.c
> @@ -46,7 +46,7 @@
>  #include <GL/glx.h>
>  #endif
>
> -#if HAVE_SDL
> +#if HAVE_SDL2
>  #include <SDL.h>
>  #endif
>
> @@ -174,8 +174,10 @@ static const GLushort g_index[6] =
>  typedef struct OpenGLContext {
>      AVClass *class;                    ///< class for private options
>
> -#if HAVE_SDL
> -    SDL_Surface *surface;
> +#if HAVE_SDL2
> +    SDL_Texture  *texture;
> +    SDL_Window   *window;
> +    SDL_Renderer *renderer;
>

I said I will patch opengl, your implemantation donsn't look good at this
point, declined,
Lou Logan Sept. 18, 2016, 1:59 a.m. UTC | #2
On Sat, Sep 17, 2016, at 05:21 PM, Lukasz Marek wrote:
> On 18 September 2016 at 00:30, Josh de Kock <josh@itanimul.li> wrote:
[...]
>>  Would like some feedback where it doesnt work etc
[...]
> I said I will patch opengl, your implemantation donsn't look good at this
> point, declined,

It would be helpful to provide some details describing what exactly does
not look good.
Lukasz Marek Sept. 18, 2016, 4:51 a.m. UTC | #3
W dniu niedziela, 18 września 2016 Lou Logan <lou@lrcd.com
<javascript:_e(%7B%7D,'cvml','lou@lrcd.com');>> napisał(a):

> On Sat, Sep 17, 2016, at 05:21 PM, Lukasz Marek wrote:
> > On 18 September 2016 at 00:30, Josh de Kock <josh@itanimul.li> wrote:
> [...]
> >>  Would like some feedback where it doesnt work etc
> [...]
> > I said I will patch opengl, your implemantation donsn't look good at this
> > point, declined,
>
> It would be helpful to provide some details describing what exactly does
> not look good.
> _______________________________________________


opengl device depends on opengl context. this patch works on sdl renderer
which works on X. in many cases X is opengł so patch is working for many
cases, but is shity in general.

>
>
Josh de Kock Sept. 18, 2016, 8:04 a.m. UTC | #4
On 18/09/2016 05:51, Lukasz Marek wrote:
> W dniu niedziela, 18 września 2016 Lou Logan <lou@lrcd.com
> <javascript:_e(%7B%7D,'cvml','lou@lrcd.com');>> napisał(a):
>
>> On Sat, Sep 17, 2016, at 05:21 PM, Lukasz Marek wrote:
>>> On 18 September 2016 at 00:30, Josh de Kock <josh@itanimul.li> wrote:
>> [...]
>>>>  Would like some feedback where it doesnt work etc
>> [...]
>>> I said I will patch opengl, your implemantation donsn't look good at this
>>> point, declined,
>>
>> It would be helpful to provide some details describing what exactly does
>> not look good.
>> _______________________________________________
>
>
> opengl device depends on opengl context. this patch works on sdl renderer
> which works on X. in many cases X is opengł so patch is working for many
> cases, but is shity in general.
>

Sorry, I don't understand. Could you rephrase this?

--
Josh
Lukasz Marek Sept. 18, 2016, 3:04 p.m. UTC | #5
On 18.09.2016 10:04, Josh de Kock wrote:
> On 18/09/2016 05:51, Lukasz Marek wrote:
>> W dniu niedziela, 18 września 2016 Lou Logan <lou@lrcd.com
>> <javascript:_e(%7B%7D,'cvml','lou@lrcd.com');>> napisał(a):
>>
>>> On Sat, Sep 17, 2016, at 05:21 PM, Lukasz Marek wrote:
>>>> On 18 September 2016 at 00:30, Josh de Kock <josh@itanimul.li> wrote:
>>> [...]
>>>>>  Would like some feedback where it doesnt work etc
>>> [...]
>>>> I said I will patch opengl, your implemantation donsn't look good at
>>>> this
>>>> point, declined,
>>>
>>> It would be helpful to provide some details describing what exactly does
>>> not look good.
>>> _______________________________________________
>>
>>
>> opengl device depends on opengl context. this patch works on sdl renderer
>> which works on X. in many cases X is opengł so patch is working for many
>> cases, but is shity in general.
>>
>
> Sorry, I don't understand. Could you rephrase this?

To make opengl work it needs a context. Could you point where you create it?
diff mbox

Patch

diff --git a/libavdevice/opengl_enc.c b/libavdevice/opengl_enc.c
index 1dbbb80..1917459 100644
--- a/libavdevice/opengl_enc.c
+++ b/libavdevice/opengl_enc.c
@@ -46,7 +46,7 @@ 
 #include <GL/glx.h>
 #endif
 
-#if HAVE_SDL
+#if HAVE_SDL2
 #include <SDL.h>
 #endif
 
@@ -174,8 +174,10 @@  static const GLushort g_index[6] =
 typedef struct OpenGLContext {
     AVClass *class;                    ///< class for private options
 
-#if HAVE_SDL
-    SDL_Surface *surface;
+#if HAVE_SDL2
+    SDL_Texture  *texture;
+    SDL_Window   *window;
+    SDL_Renderer *renderer;
 #endif
     FFOpenGLFunctions glprocs;
 
@@ -341,12 +343,18 @@  static int opengl_control_message(AVFormatContext *h, int type, void *data, size
     return AVERROR(ENOSYS);
 }
 
-#if HAVE_SDL
+#if HAVE_SDL2
 static int opengl_sdl_recreate_window(OpenGLContext *opengl, int width, int height)
 {
-    opengl->surface = SDL_SetVideoMode(width, height,
-                                       32, SDL_OPENGL | SDL_RESIZABLE);
-    if (!opengl->surface) {
+    int ret;
+    if ((ret = SDL_CreateWindowAndRenderer(width, height, 0, &opengl->window,
+                                           &opengl->renderer)) < 0)
+        return ret;
+
+    opengl->texture = SDL_CreateTexture(opengl->renderer, SDL_PIXELFORMAT_ARGB8888,
+                                        SDL_TEXTUREACCESS_STREAMING, width, height);
+
+    if (!opengl->texture) {
         av_log(opengl, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
         return AVERROR_EXTERNAL;
     }
@@ -363,8 +371,7 @@  static int opengl_sdl_process_events(AVFormatContext *h)
     int ret;
     OpenGLContext *opengl = h->priv_data;
     SDL_Event event;
-    SDL_PumpEvents();
-    while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS) > 0) {
+    while (SDL_PollEvent(&event) > 0) {
         switch (event.type) {
         case SDL_QUIT:
             return AVERROR(EIO);
@@ -375,24 +382,23 @@  static int opengl_sdl_process_events(AVFormatContext *h)
                 return AVERROR(EIO);
             }
             return 0;
-        case SDL_VIDEORESIZE: {
-            char buffer[100];
-            int reinit;
-            AVDeviceRect message;
-            /* clean up old context because SDL_SetVideoMode may lose its state. */
-            SDL_VideoDriverName(buffer, sizeof(buffer));
-            reinit = !av_strncasecmp(buffer, "quartz", sizeof(buffer));
-            if (reinit) {
-                opengl_deinit_context(opengl);
-            }
-            if ((ret = opengl_sdl_recreate_window(opengl, event.resize.w, event.resize.h)) < 0)
-                return ret;
-            if (reinit && (ret = opengl_init_context(opengl)) < 0)
-                return ret;
-            message.width = opengl->surface->w;
-            message.height = opengl->surface->h;
-            return opengl_control_message(h, AV_APP_TO_DEV_WINDOW_SIZE, &message, sizeof(AVDeviceRect));
+        case SDL_WINDOWEVENT:
+            switch(event.window.event){
+            case SDL_WINDOWEVENT_SIZE_CHANGED:
+            case SDL_WINDOWEVENT_RESIZED: {
+                AVDeviceRect message;
+                if ((ret = opengl_sdl_recreate_window(opengl, event.window.data1, event.window.data2)) < 0)
+                    return ret;
+                if ((SDL_QueryTexture(opengl->texture, NULL, NULL, &message.width, &message.height)) < 0)
+                    return ret;
+                return opengl_control_message(h, AV_APP_TO_DEV_WINDOW_SIZE, &message, sizeof(AVDeviceRect));
+                }
+            default:
+                break;
             }
+            break;
+        default:
+            break;
         }
     }
     return 0;
@@ -401,7 +407,6 @@  static int opengl_sdl_process_events(AVFormatContext *h)
 static int av_cold opengl_sdl_create_window(AVFormatContext *h)
 {
     int ret;
-    char buffer[100];
     OpenGLContext *opengl = h->priv_data;
     AVDeviceRect message;
     if (SDL_Init(SDL_INIT_VIDEO)) {
@@ -411,10 +416,10 @@  static int av_cold opengl_sdl_create_window(AVFormatContext *h)
     if ((ret = opengl_sdl_recreate_window(opengl, opengl->window_width,
                                           opengl->window_height)) < 0)
         return ret;
-    av_log(opengl, AV_LOG_INFO, "SDL driver: '%s'.\n", SDL_VideoDriverName(buffer, sizeof(buffer)));
-    message.width = opengl->surface->w;
-    message.height = opengl->surface->h;
-    SDL_WM_SetCaption(opengl->window_title, NULL);
+    av_log(opengl, AV_LOG_INFO, "SDL driver: '%s'.\n", SDL_GetCurrentVideoDriver());
+    if ((ret = SDL_QueryTexture(opengl->texture, NULL, NULL, &message.width, &message.height)) < 0)
+        return ret;
+    SDL_SetWindowTitle(opengl->window, opengl->window_title);
     opengl_control_message(h, AV_APP_TO_DEV_WINDOW_SIZE, &message, sizeof(AVDeviceRect));
     return 0;
 }
@@ -460,14 +465,14 @@  static int av_cold opengl_sdl_load_procedures(OpenGLContext *opengl)
 
 #undef LOAD_OPENGL_FUN
 }
-#endif /* HAVE_SDL */
+#endif /* HAVE_SDL2 */
 
 #if defined(__APPLE__)
 static int av_cold opengl_load_procedures(OpenGLContext *opengl)
 {
     FFOpenGLFunctions *procs = &opengl->glprocs;
 
-#if HAVE_SDL
+#if HAVE_SDL2
     if (!opengl->no_window)
         return opengl_sdl_load_procedures(opengl);
 #endif
@@ -517,7 +522,7 @@  static int av_cold opengl_load_procedures(OpenGLContext *opengl)
         return AVERROR(ENOSYS); \
     }
 
-#if HAVE_SDL
+#if HAVE_SDL2
     if (!opengl->no_window)
         return opengl_sdl_load_procedures(opengl);
 #endif
@@ -943,7 +948,7 @@  static int opengl_create_window(AVFormatContext *h)
     int ret;
 
     if (!opengl->no_window) {
-#if HAVE_SDL
+#if HAVE_SDL2
         if ((ret = opengl_sdl_create_window(h)) < 0) {
             av_log(opengl, AV_LOG_ERROR, "Cannot create default SDL window.\n");
             return ret;
@@ -975,7 +980,7 @@  static int opengl_release_window(AVFormatContext *h)
     int ret;
     OpenGLContext *opengl = h->priv_data;
     if (!opengl->no_window) {
-#if HAVE_SDL
+#if HAVE_SDL2
         SDL_Quit();
 #endif
     } else if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER, NULL , 0)) < 0) {
@@ -1109,9 +1114,9 @@  static av_cold int opengl_write_header(AVFormatContext *h)
 
     glClear(GL_COLOR_BUFFER_BIT);
 
-#if HAVE_SDL
+#if HAVE_SDL2
     if (!opengl->no_window)
-        SDL_GL_SwapBuffers();
+        SDL_GL_SwapWindow(opengl->window);
 #endif
     if (opengl->no_window &&
         (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {
@@ -1204,7 +1209,7 @@  static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
     int ret;
 
-#if HAVE_SDL
+#if HAVE_SDL2
     if (!opengl->no_window && (ret = opengl_sdl_process_events(h)) < 0)
         goto fail;
 #endif
@@ -1245,9 +1250,9 @@  static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
     ret = AVERROR_EXTERNAL;
     OPENGL_ERROR_CHECK(opengl);
 
-#if HAVE_SDL
+#if HAVE_SDL2
     if (!opengl->no_window)
-        SDL_GL_SwapBuffers();
+        SDL_GL_SwapWindow(opengl->window);
 #endif
     if (opengl->no_window &&
         (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {