Ticket #5324: openglvid_8.diff

File openglvid_8.diff, 18.2 KB (added by anonymous, 16 years ago)
  • libs/libmythtv/openglcontext.cpp

    diff -ur -X excl mythtvopengl7/libs/libmythtv/openglcontext.cpp mythtvopengl8/libs/libmythtv/openglcontext.cpp
    old new  
    77#define LOC QString("GLCtx: ")
    88#define LOC_ERR QString("GLCtx, Error: ")
    99
     10ContextLocker::ContextLocker(OpenGLContext *ctx)
     11              : m_ctx(ctx)
     12{
     13    if (m_ctx)
     14        m_ctx->MakeCurrent(true);
     15}
     16ContextLocker::~ContextLocker()
     17{
     18    if (m_ctx)
     19        m_ctx->MakeCurrent(false);
     20}
     21
    1022class PrivateContext
    1123{
    1224  public:
     
    3648    vector<GLuint> m_framebuffers;
    3749};
    3850
    39 OpenGLContext::OpenGLContext() :
     51OpenGLContext::OpenGLContext(QMutex *lock) :
    4052    m_priv(new PrivateContext()),
    4153    m_display(NULL), m_screen_num(0),
    4254    m_major_ver(1), m_minor_ver(2),
    4355    m_extensions(QString::null), m_ext_supported(0),
    44     m_max_tex_size(0), m_viewport(0,0)
     56    m_max_tex_size(0), m_viewport(0,0),
     57    m_lock(lock), m_lock_level(0)
    4558{
    4659    if (!init_opengl())
    4760        VERBOSE(VB_PLAYBACK, LOC_ERR + "Failed to initialize OpenGL support.");
     
    6376
    6477    glFlush();
    6578
    66     MakeCurrent(false);
    67 
    6879    if (m_priv->m_glx_window)
    6980    {
    7081        X11S(glXDestroyWindow(m_display, m_priv->m_glx_window));
     
    7788        m_priv->m_gl_window = 0;
    7889    }
    7990
     91    MakeCurrent(false);
     92
    8093    if (m_priv->m_glx_context)
    8194    {
    8295        X11S(glXDestroyContext(m_display, m_priv->m_glx_context));
     
    89102
    90103void OpenGLContext::Hide(void)
    91104{
     105    MakeCurrent(true);
    92106    X11S(XUnmapWindow(m_display, m_priv->m_gl_window));
     107    MakeCurrent(false);
    93108}
    94109
    95110void OpenGLContext::Show(void)
    96111{
     112    MakeCurrent(true);
    97113    X11S(XMapWindow(m_display, m_priv->m_gl_window));
     114    MakeCurrent(false);
    98115}
    99116
    100117// locking ok
     
    245262        ((has_gl_fbuffer_object_support(m_extensions)) ? kGLExtFBufObj : 0) |
    246263        ((minor >= 3) ? kGLXPBuffer : 0);
    247264
     265    MakeCurrent(true);
    248266    Init2DState();
     267    MakeCurrent(false);
    249268
    250269    return true;
    251270}
     
    253272// locking ok
    254273bool OpenGLContext::MakeCurrent(bool current)
    255274{
    256     bool ok;
     275    bool ok = true;
    257276
    258277    if (current)
    259278    {
    260         if (IsGLXSupported(1,3))
    261         {
    262             X11S(ok = glXMakeCurrent(m_display,
    263                                      m_priv->m_glx_window,
    264                                      m_priv->m_glx_context));
    265         }
    266         else
     279        m_lock->lock();
     280        if (m_lock_level == 0)
    267281        {
    268             X11S(ok = glXMakeCurrent(m_display,
    269                                      m_priv->m_gl_window,
    270                                      m_priv->m_glx_context));
     282            if (IsGLXSupported(1,3))
     283            {
     284                X11S(ok = glXMakeCurrent(m_display,
     285                                         m_priv->m_glx_window,
     286                                         m_priv->m_glx_context));
     287            }
     288            else
     289            {
     290                X11S(ok = glXMakeCurrent(m_display,
     291                                         m_priv->m_gl_window,
     292                                         m_priv->m_glx_context));
     293            }
    271294        }
     295        m_lock_level++;
    272296    }
    273297    else
    274298    {
    275         X11S(ok = glXMakeCurrent(m_display, None, NULL));
     299        m_lock_level--;
     300        if (m_lock_level == 0)
     301        {
     302            X11S(ok = glXMakeCurrent(m_display, None, NULL));
     303        }
     304        else if (m_lock_level < 0)
     305        {
     306            VERBOSE(VB_PLAYBACK, LOC_ERR + "Mis-matched calls to MakeCurrent");
     307        }
     308        m_lock->unlock();
    276309    }
    277310
    278311    if (!ok)
    279         VERBOSE(VB_PLAYBACK, LOC + "Could not make context current.");
     312        VERBOSE(VB_PLAYBACK, LOC_ERR + "Could not make context current.");
    280313
    281314    return ok;
    282315}
     
    298331// locking ok
    299332void OpenGLContext::Flush(void)
    300333{
     334    MakeCurrent(true);
    301335    glFlush();
     336    MakeCurrent(false);
    302337}
    303338
    304339// locking ok
     
    359394// locking ok
    360395void OpenGLContext::SetupTextureFilters(uint tex, int filt)
    361396{
     397    MakeCurrent(true);
    362398    glBindTexture(GetTextureType(), tex);
    363399    glTexParameteri(GetTextureType(), GL_TEXTURE_MIN_FILTER, filt);
    364400    glTexParameteri(GetTextureType(), GL_TEXTURE_MAG_FILTER, filt);
    365401    glTexParameteri(GetTextureType(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    366402    glTexParameteri(GetTextureType(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     403    MakeCurrent(false);
    367404}
    368405
    369406// locking ok
     
    391428// locking ok
    392429void OpenGLContext::DeleteTextures(void)
    393430{
    394     MakeCurrent(true);
    395 
    396431    vector<GLuint>::iterator it;
    397432    for (it = m_priv->m_textures.begin(); it !=m_priv->m_textures.end(); it++)
    398433        glDeleteTextures(1, &(*(it)));
    399434    m_priv->m_textures.clear();
    400435
    401436    Flush();
    402 
    403     MakeCurrent(false);
    404437}
    405438
    406439int OpenGLContext::GetTextureType(void) const
     
    484517
    485518void OpenGLContext::BindFragmentProgram(uint fp)
    486519{
     520    MakeCurrent(true);
    487521    gMythGLBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fp);
     522    MakeCurrent(false);
    488523}
    489524
    490525void OpenGLContext::InitFragmentParams(
    491526    uint fp, float a, float b, float c, float d)
    492527{
     528    MakeCurrent(true);
    493529    gMythGLProgramEnvParameter4fARB(
    494530        GL_FRAGMENT_PROGRAM_ARB, fp, a, b, c, d);
     531    MakeCurrent(false);
    495532}
    496533
    497534void OpenGLContext::DeletePrograms(void)
    498535{
    499     MakeCurrent(true);
    500 
    501536    vector<GLuint>::iterator it;
    502537    for (it = m_priv->m_programs.begin(); it != m_priv->m_programs.end(); it++)
    503538        gMythGLDeleteProgramsARB(1, &(*(it)));
    504539    m_priv->m_programs.clear();
    505540
    506541    Flush();
    507 
    508     MakeCurrent(false);
    509542}
    510543
    511544// locking ok
     
    619652
    620653void OpenGLContext::DeleteFrameBuffers(void)
    621654{
    622     MakeCurrent(true);
    623 
    624655    vector<GLuint>::iterator it;
    625656    for (it = m_priv->m_framebuffers.begin();
    626657         it != m_priv->m_framebuffers.end(); it++)
     
    630661    m_priv->m_framebuffers.clear();
    631662
    632663    Flush();
    633 
    634     MakeCurrent(false);
    635664}
    636665
    637666// locking ok
    638667void OpenGLContext::BindFramebuffer(uint fb)
    639668{
     669    MakeCurrent(true);
    640670    gMythGLBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
     671    MakeCurrent(false);
    641672}
    642673
    643674bool OpenGLContext::IsGLXSupported(
     
    655686
    656687void OpenGLContext::Init2DState(void)
    657688{
    658     MakeCurrent(true);
    659689    glDisable(GL_BLEND);
    660690    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // for gl osd
    661691    glDisable(GL_DEPTH_TEST);
    662692    glDepthMask(GL_FALSE);
    663693    glDisable(GL_CULL_FACE);
     694
    664695    EnableTextures();;
     696
    665697    glShadeModel(GL_FLAT);
    666698    glDisable(GL_POLYGON_SMOOTH);
    667699    glDisable(GL_LINE_SMOOTH);
     
    669701    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    670702    glClear(GL_COLOR_BUFFER_BIT);
    671703    glFlush();
    672     MakeCurrent(false);
    673704}
    674705
    675706void OpenGLContext::SetViewPort(const QSize &size)
     
    678709        size.height() == m_viewport.height())
    679710        return;
    680711
     712    MakeCurrent(true);
     713
    681714    m_viewport = size;
    682715
    683716    glViewport(0, 0, size.width(), size.height());
     
    687720            0, size.height() - 1, 1, -1); // aargh...
    688721    glMatrixMode(GL_MODELVIEW);
    689722    glLoadIdentity();
     723
     724    MakeCurrent(false);
    690725}
  • libs/libmythtv/openglcontext.h

    diff -ur -X excl mythtvopengl7/libs/libmythtv/openglcontext.h mythtvopengl8/libs/libmythtv/openglcontext.h
    old new  
    2323    kGLXPBuffer    = 0x08,
    2424} GLFeatures;
    2525
     26class OpenGLContext;
     27
     28class ContextLocker
     29{
     30    public:
     31        ContextLocker(OpenGLContext *ctx);
     32        ~ContextLocker();
     33
     34    private:
     35        OpenGLContext *m_ctx;
     36};
     37
    2638#ifdef USING_OPENGL
    2739
    2840class OpenGLContext
    2941{
    3042  public:
    31     OpenGLContext();
     43    OpenGLContext(QMutex *lock);
    3244    ~OpenGLContext();
    3345
    3446    bool Create(Display *display, Window window, uint screen_num,
     
    8799    uint            m_ext_supported;
    88100    uint            m_max_tex_size;
    89101    QSize           m_viewport;
     102    QMutex         *m_lock;
     103    int             m_lock_level;
    90104};
    91105
    92106#else // if !USING_OPENGL
     
    94108class OpenGLContext
    95109{
    96110  public:
    97     OpenGLContext() { }
     111    OpenGLContext(QMutex*) { }
    98112    ~OpenGLContext() { }
    99113
    100114    bool Create(Display*, Window, uint, const QSize&) { return false; }
  • libs/libmythtv/openglvideo.cpp

    diff -ur -X excl mythtvopengl7/libs/libmythtv/openglvideo.cpp mythtvopengl8/libs/libmythtv/openglvideo.cpp
    old new  
    5353
    5454OpenGLVideo::~OpenGLVideo()
    5555{
     56    ContextLocker ctx_lock(gl_context);
    5657    Teardown();
    5758}
    5859
     
    6162{
    6263    ShutDownYUV2RGB();
    6364
    64     gl_context->MakeCurrent(true);
    65 
    6665    for (uint i = 0; i < inputTextures.size(); i++)
    6766        gl_context->DeleteTexture(inputTextures[i]);
    6867    inputTextures.clear();
     
    8382        }
    8483    }
    8584    filters.clear();
    86 
    87     gl_context->MakeCurrent(false);
    8885}
    8986
    9087// locking ok
     
    9491                       bool viewport_control, bool osd)
    9592{
    9693    gl_context            = glcontext;
     94    if (!gl_context)
     95        return false;
     96
     97    ContextLocker ctx_lock(gl_context);
     98
    9799    actual_video_dim      = videoDim;
    98100    video_dim             = videoDim;
    99101    if (video_dim.height() == 1088)
     
    347349    VERBOSE(VB_PLAYBACK, LOC + QString("Creating %1 filter.")
    348350            .arg(FilterToString(filter)));
    349351
    350     gl_context->MakeCurrent(true);
    351 
    352352    OpenGLFilter *temp = new OpenGLFilter();
    353353
    354354    temp->numInputs = 1;
     
    405405    VERBOSE(VB_PLAYBACK, QString("Removing %1 filter")
    406406            .arg(FilterToString(filter)));
    407407
    408     gl_context->MakeCurrent(true);
    409 
    410408    gl_context->DeleteFragmentProgram(filters[filter]->fragmentProgram);
    411409
    412410    vector<GLuint> temp;
     
    422420
    423421    filters.erase(filter);
    424422
    425     gl_context->MakeCurrent(false);
    426 
    427423    return true;
    428424}
    429425
    430426// locking ok
    431427bool OpenGLVideo::AddDeinterlacer(const QString &filter)
    432428{
     429    ContextLocker ctx_lock(gl_context);
     430
    433431    QString current_deinterlacer = GetDeinterlacer();
    434432
    435433    if (current_deinterlacer == filter)
     
    552550// locking ok
    553551void OpenGLVideo::UpdateInputFrame(const VideoFrame *frame)
    554552{
     553    ContextLocker ctx_lock(gl_context);
     554
    555555    if (frame->width  != actual_video_dim.width()  ||
    556556        frame->height != actual_video_dim.height() ||
    557557        frame->width  < 1 ||
     
    600600void OpenGLVideo::UpdateInput(const unsigned char *buf, const int *offsets,
    601601                              uint texture_index, int format, QSize size)
    602602{
     603    ContextLocker ctx_lock(gl_context);
     604
    603605    inputUpdated = false;
    604606
    605607    if (texture_index >= inputTextures.size())
     
    640642// TODO shouldn't this take a QSize, not QRect?
    641643void OpenGLVideo::SetVideoResize(const QRect &rect)
    642644{
     645    ContextLocker ctx_lock(gl_context);
     646
    643647    bool abort = ((rect.right()  > video_dim.width())  ||
    644648                  (rect.bottom() > video_dim.height()) ||
    645649                  (rect.width()  > video_dim.width())  ||
     
    662666// locking ok
    663667void OpenGLVideo::DisableVideoResize(void)
    664668{
     669    ContextLocker ctx_lock(gl_context);
     670
    665671    videoResize     = false;
    666672    videoResizeRect = QRect(0, 0, 0, 0);
    667673}
     
    695701    if (deinterlacing == hardwareDeinterlacing)
    696702        return;
    697703
     704    ContextLocker ctx_lock(gl_context);
     705
    698706    VERBOSE(VB_PLAYBACK, LOC + QString("Turning %1 deinterlacing.")
    699707            .arg(deinterlacing ? "on" : "off"));
    700708
     
    723731        }
    724732    }
    725733
    726     gl_context->MakeCurrent(true);
    727734    SetFiltering();
    728     gl_context->MakeCurrent(false);
    729735}
    730736
    731737// locking ok
     
    735741    if (inputTextures.empty() || filters.empty())
    736742        return;
    737743
     744    ContextLocker ctx_lock(gl_context);
     745
    738746    vector<GLuint> inputs = inputTextures;
    739747    QSize inputsize = inputTextureSize;
    740748    uint  numfilters = filters.size();
     
    10411049    if (!useColourControl)
    10421050        return -1;
    10431051
     1052    ContextLocker ctx_lock(gl_context);
     1053
    10441054    int ret = -1;
    10451055    switch (attribute)
    10461056    {
  • libs/libmythtv/videoout_xv.cpp

    diff -ur -X excl mythtvopengl7/libs/libmythtv/videoout_xv.cpp mythtvopengl8/libs/libmythtv/videoout_xv.cpp
    old new  
    122122      xv_colorkey(0),   xv_draw_colorkey(false),
    123123      xv_chroma(0),
    124124
    125       gl_context_lock(false), gl_context(NULL),
     125      gl_context_lock(true), gl_context(NULL),
    126126      gl_videochain(NULL), gl_pipchain(NULL),
    127127      gl_osdchain(NULL),
    128128
     
    219219
    220220    if (gl_videochain)
    221221    {
    222         QMutexLocker locker(&gl_context_lock);
     222        ContextLocker ctx_lock(gl_context);
    223223        gl_videochain->SetVideoRect(display_video_rect, video_rect);
    224224    }
    225225}
     
    943943#ifdef USING_OPENGL_VIDEO
    944944    ok = gl_context;
    945945
    946     gl_context_lock.lock();   
     946    gl_context_lock.lock();
    947947
    948948    if (!ok)
    949949    {
    950         gl_context = new OpenGLContext();
     950        gl_context = new OpenGLContext(&gl_context_lock);
    951951
    952952        ok = gl_context->Create(
    953953            XJ_disp, XJ_win, XJ_screen_num,
    954954            display_visible_rect.size());
    955955    }
    956956
     957    gl_context_lock.unlock();
     958
    957959    if (ok)
    958960    {
     961        ContextLocker ctx_lock(gl_context);
    959962        gl_context->Show();
    960         gl_context->MakeCurrent(true);
    961963        gl_videochain = new OpenGLVideo();
    962964        ok = gl_videochain->Init(gl_context, db_use_picture_controls,
    963965                                 video_dim, display_visible_rect,
    964966                                 display_video_rect, video_rect, true);
    965         gl_context->MakeCurrent(false);
    966967    }
    967968
    968     gl_context_lock.unlock();
    969 
    970969    if (ok)
    971970    {
     971        ContextLocker ctx_lock(gl_context);
    972972        InstallXErrorHandler(XJ_disp);
    973973
    974974        ok = CreateBuffers(OpenGL);
     
    994994            if (!m_deintfiltername.isEmpty() &&
    995995                !m_deintfiltername.contains("opengl"))
    996996            {
    997                 QMutexLocker locker(&gl_context_lock);
    998997                gl_videochain->SetSoftwareDeinterlacer(m_deintfiltername);
    999998            }
    1000999
     
    14131412
    14141413    if (osd_renderer == "opengl2")
    14151414    {
    1416         QMutexLocker locker(&gl_context_lock);
     1415        ContextLocker ctx_lock(gl_context);
    14171416        gl_use_osd_opengl2 = true;
    14181417
    1419         gl_context->MakeCurrent(true);
    1420 
    14211418        gl_osdchain = new OpenGLVideo();
    14221419        if (!gl_osdchain->Init(
    14231420                gl_context, false,
     
    14351432        {
    14361433            gl_osdchain->SetMasterViewport(gl_videochain->GetViewPort());
    14371434        }
    1438 
    1439         gl_context->MakeCurrent(false);
    14401435    }
    14411436
    14421437    if (osd_renderer == "chromakey")
     
    17771772    if (enable && m_deinterlacing && (OpenGL != VideoOutputSubType()))
    17781773        return m_deinterlacing;
    17791774
     1775    ContextLocker ctx_lock(gl_context);
     1776
    17801777    if (enable)
    17811778    {
    17821779        if (m_deintfiltername == "")
     
    17891786        else if (!m_deintfiltername.contains("opengl"))
    17901787        {
    17911788            // make sure opengl deinterlacing is disabled
    1792             gl_context_lock.lock();
    17931789            gl_videochain->SetDeinterlacing(false);
    1794             gl_context_lock.unlock();
    17951790
    17961791            if (!m_deintFiltMan || !m_deintFilter)
    17971792                return VideoOutput::SetupDeinterlace(enable);
     
    17991794    }
    18001795
    18011796    if (gl_videochain)
    1802     {
    1803         QMutexLocker locker(&gl_context_lock);
    18041797        gl_videochain->SetDeinterlacing(enable);
    1805     }
    18061798
    18071799    m_deinterlacing = enable;
    18081800
     
    18151807    (void) interlaced;
    18161808    (void) overridefilter;
    18171809
     1810    ContextLocker ctx_lock(gl_context);
     1811
    18181812    m_deintfiltername = db_vdisp_profile->GetFilteredDeint(overridefilter);
    18191813
    18201814    if (!m_deintfiltername.contains("opengl"))
    18211815    {
    1822         gl_context_lock.lock();
    18231816        gl_videochain->SetDeinterlacing(false);
    1824         gl_context_lock.unlock();
    18251817
    18261818        gl_videochain->SetSoftwareDeinterlacer(QString::null);
    18271819
     
    18511843    if (!gl_videochain)
    18521844        return false;
    18531845
    1854     QMutexLocker locker(&gl_context_lock);
    1855 
    18561846    if (m_deinterlacing && !m_deintfiltername.isEmpty())
    18571847    {
    18581848        if (gl_videochain->GetDeinterlacer() != m_deintfiltername)
     
    23472337#endif // USING_XVMC
    23482338
    23492339    // OpenGL stuff
    2350     gl_context_lock.lock();
     2340    if (gl_context)
     2341        gl_context->MakeCurrent(true);
    23512342
    23522343    if (gl_videochain)
    23532344    {
     
    23662357    }
    23672358
    23682359    if (gl_context)
     2360    {
    23692361        gl_context->Hide();
     2362        gl_context->MakeCurrent(false);
     2363    }
    23702364
    23712365    gl_use_osd_opengl2 = false;
    23722366    gl_pip_ready = false;
    23732367    gl_osd_ready = false;
    23742368    allowpreviewepg = true;
    2375 
    2376     gl_context_lock.unlock();
    23772369    // end OpenGL stuff
    23782370
    23792371    vbuffers.DeleteBuffers();
     
    27162708{
    27172709    (void) t;
    27182710
    2719     QMutexLocker locker(&gl_context_lock);
     2711    ContextLocker ctx_lock(gl_context);
    27202712
    27212713    if (!buffer)
    27222714        buffer = vbuffers.GetScratchFrame();
     
    27272719    if (buffer->codec != FMT_YV12)
    27282720        return;
    27292721
    2730     gl_context->MakeCurrent(true);
    27312722    gl_videochain->PrepareFrame(t, m_deinterlacing, framesPlayed);
    27322723
    27332724    if (gl_pip_ready && gl_pipchain)
     
    27372728        gl_osdchain->PrepareFrame(t, m_deinterlacing, framesPlayed);
    27382729
    27392730    gl_context->Flush();
    2740     gl_context->MakeCurrent(false);
    27412731
    27422732    if (vbuffers.GetScratchFrame() == buffer)
    27432733        vbuffers.SetLastShownFrameToScratch();
     
    31063096    else if (VideoOutputSubType() == XVideo)
    31073097        ShowXVideo(scan);
    31083098    else if (VideoOutputSubType() == OpenGL)
    3109     {
    3110         QMutexLocker locker(&gl_context_lock);
    31113099        gl_context->SwapBuffers();
    3112     }
    31133100
    31143101    X11S(XSync(XJ_disp, False));
    31153102}
     
    37143701    (void) filterList;
    37153702    (void) pipPlayer;
    37163703
    3717     QMutexLocker locker(&gl_context_lock);
     3704    ContextLocker ctx_lock(gl_context);
    37183705
    37193706    bool pauseframe = false;
    37203707    if (!frame)
     
    37243711        pauseframe = true;
    37253712    }
    37263713
    3727     // disable image processing for offscreen rendering
    3728     gl_context->MakeCurrent(true);
    3729 
    37303714    if (filterList)
    37313715        filterList->ProcessFrame(frame);
    37323716
     
    37493733
    37503734    if (gl_videochain)
    37513735        gl_videochain->UpdateInputFrame(frame);
    3752 
    3753     gl_context->MakeCurrent(false);
    37543736}
    37553737
    37563738void VideoOutputXv::ProcessFrameMem(VideoFrame *frame, OSD *osd,