<commit_before>
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2006 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//


#include "SDLDisplayEngine.h"
#ifdef __APPLE__
#include "SDLMain.h"
#endif

#include "Region.h"
#include "Player.h"
#include "Node.h"
#include "AVGNode.h"

#include "Event.h"
#include "MouseEvent.h"
#include "KeyEvent.h"

#include "OGLSurface.h"
#include "OGLHelper.h"

#include "../base/Exception.h"
#include "../base/Logger.h"
#include "../base/ScopeTimer.h"
#include "../base/Profiler.h"

#include "../graphics/Filterflip.h"
#include "../graphics/Filterfliprgb.h"

#ifdef __APPLE__
#include <ApplicationServices/ApplicationServices.h>
#else
#include <X11/Xlib.h>
#include <X11/extensions/xf86vmode.h>
#endif

#define XMD_H 1
#define GLX_GLXEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#include "GL/gl.h"
#include "GL/glu.h"

#ifdef __APPLE__
#include "AGL/agl.h"
#else
#include "GL/glx.h"
#endif

#include <sys/ioctl.h>
#include <sys/fcntl.h>

#include <signal.h>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <assert.h>

using namespace std;

namespace avg {

double SDLDisplayEngine::s_RefreshRate = 0.0;

void dumpSDLGLParams() {
    int value;
    cerr << "SDL display parameters used: "<< endl;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
    fprintf(stderr,"  SDL_GL_RED_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE,  &value);
    fprintf(stderr,"  SDL_GL_GREEN_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE , &value);
    fprintf(stderr,"  SDL_GL_BLUE_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE , &value);
    fprintf(stderr,"  SDL_GL_ALPHA_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE , &value);
    fprintf(stderr,"  SDL_GL_DEPTH_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER , &value);
    fprintf(stderr,"  SDL_GL_DOUBLEBUFFER = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE , &value);
    fprintf(stderr,"  SDL_GL_BUFFER_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE , &value);
    fprintf(stderr,"  SDL_GL_STENCIL_SIZE = %d\n",value);
}

SDLDisplayEngine::SDLDisplayEngine()
    : m_bEnableCrop(false),
      m_pScreen(0),
      m_VBMod(0)
{
#ifdef __APPLE__
    static bool bSDLInitialized = false;
    if (!bSDLInitialized) {
        CustomSDLMain();
        bSDLInitialized = true;
    }
#endif
    if (SDL_InitSubSystem(SDL_INIT_VIDEO)==-1) {
        AVG_TRACE(Logger::ERROR, "Can't init SDL display subsystem.");
        exit(-1);
    }
    initTranslationTable();
    calcRefreshRate();
}

SDLDisplayEngine::~SDLDisplayEngine()
{
    if (m_pScreen) {
        teardown();
    }
}

void SDLDisplayEngine::init(int width, int height, bool isFullscreen, 
        int bpp, int WindowWidth, int WindowHeight)
{
    double AspectRatio = double(width)/double(height);
    if (WindowWidth == 0 && WindowHeight == 0) {
        m_WindowWidth = width;
        m_WindowHeight = height;
    } else if (WindowWidth == 0) {
        m_WindowWidth = int(WindowHeight*AspectRatio);
        m_WindowHeight = WindowHeight;
    } else {
        m_WindowWidth = WindowWidth;
        m_WindowHeight = int(WindowWidth/AspectRatio);
    }
    
    switch (bpp) {
        case 32:
            safeSetAttribute( SDL_GL_RED_SIZE, 8 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 8 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 8 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 32 );
            break;
        case 24:
            safeSetAttribute( SDL_GL_RED_SIZE, 8 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 8 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 8 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 24 );
            break;
        case 16:
            safeSetAttribute( SDL_GL_RED_SIZE, 5 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 6 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 5 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 16 );
            break;
        case 15:
            safeSetAttribute( SDL_GL_RED_SIZE, 5 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 5 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 5 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 15 );
            break;
        default:
            AVG_TRACE(Logger::ERROR, "Unsupported bpp " << bpp <<
                    "in SDLDisplayEngine::init()");
            exit(-1);
    }
    safeSetAttribute( SDL_GL_DEPTH_SIZE, 0 );
    safeSetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    unsigned int Flags = SDL_OPENGL;
    if (isFullscreen) {
        Flags |= SDL_FULLSCREEN;
    }
    m_pScreen = SDL_SetVideoMode(m_WindowWidth, m_WindowHeight, bpp, Flags);
    if (!m_pScreen) {
        AVG_TRACE(Logger::ERROR, "Setting SDL video mode failed: " 
                << SDL_GetError() <<". (width=" << m_WindowWidth << ", height=" 
                << m_WindowHeight << ", bpp=" << bpp << ").");
        exit(-1);
    }   
    SDL_WM_SetCaption("AVG Renderer", 0);

    glEnable(GL_BLEND);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glEnable(GL_BLEND)");
    glShadeModel(GL_FLAT);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glShadeModel()");
    glDisable(GL_DEPTH_TEST);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glDisable(GL_DEPTH_TEST)");
    int TexMode = OGLSurface::getTextureMode();
    glEnable(TexMode);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glEnable(TexMode);");

    m_YCbCrMode = NONE;
    if (queryOGLExtension("GL_MESA_ycbcr_texture")) {
        m_YCbCrMode = MESA;
    } else if (queryOGLExtension("GL_APPLE_ycbcr_422")) {
        m_YCbCrMode = APPLE;
    }

    m_Width = width;
    m_Height = height;
    initInput();
    // SDL sets up a signal handler we really don't want.
    signal(SIGSEGV, SIG_DFL);
    
    logConfig();
    m_bEnableCrop = false;
}

void SDLDisplayEngine::teardown()
{
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

double SDLDisplayEngine::getRefreshRate() {
    if (s_RefreshRate == 0.0) {
        calcRefreshRate();
    }
    return s_RefreshRate;
}

void SDLDisplayEngine::logConfig() 
{<commit_msg>*** empty log message ***<commit_after>
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2006 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//


#include "SDLDisplayEngine.h"
#ifdef __APPLE__
#include "SDLMain.h"
#endif

#include "Region.h"
#include "Player.h"
#include "Node.h"
#include "AVGNode.h"

#include "Event.h"
#include "MouseEvent.h"
#include "KeyEvent.h"

#include "OGLSurface.h"
#include "OGLHelper.h"

#include "../base/Exception.h"
#include "../base/Logger.h"
#include "../base/ScopeTimer.h"
#include "../base/Profiler.h"

#include "../graphics/Filterflip.h"
#include "../graphics/Filterfliprgb.h"

#ifdef __APPLE__
#include <ApplicationServices/ApplicationServices.h>
#else
#include <X11/Xlib.h>
#include <X11/extensions/xf86vmode.h>
#endif

#define XMD_H 1
#define GLX_GLXEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#include "GL/gl.h"
#include "GL/glu.h"

#ifdef __APPLE__
#include "AGL/agl.h"
#else
#include "GL/glx.h"
#endif

#include <sys/ioctl.h>
#include <sys/fcntl.h>

#include <signal.h>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <assert.h>

using namespace std;

namespace avg {

double SDLDisplayEngine::s_RefreshRate = 0.0;

void dumpSDLGLParams() {
    int value;
    cerr << "SDL display parameters used: "<< endl;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
    fprintf(stderr,"  SDL_GL_RED_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE,  &value);
    fprintf(stderr,"  SDL_GL_GREEN_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE , &value);
    fprintf(stderr,"  SDL_GL_BLUE_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE , &value);
    fprintf(stderr,"  SDL_GL_ALPHA_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE , &value);
    fprintf(stderr,"  SDL_GL_DEPTH_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER , &value);
    fprintf(stderr,"  SDL_GL_DOUBLEBUFFER = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE , &value);
    fprintf(stderr,"  SDL_GL_BUFFER_SIZE = %d\n",value);
    SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE , &value);
    fprintf(stderr,"  SDL_GL_STENCIL_SIZE = %d\n",value);
}

SDLDisplayEngine::SDLDisplayEngine()
    : m_bEnableCrop(false),
      m_pScreen(0),
      m_VBMod(0)
{
#ifdef __APPLE__
    static bool bSDLInitialized = false;
    if (!bSDLInitialized) {
        CustomSDLMain();
        bSDLInitialized = true;
    }
#endif
    if (SDL_InitSubSystem(SDL_INIT_VIDEO)==-1) {
        AVG_TRACE(Logger::ERROR, "Can't init SDL display subsystem.");
        exit(-1);
    }
    initTranslationTable();
    calcRefreshRate();
}

SDLDisplayEngine::~SDLDisplayEngine()
{
    if (m_pScreen) {
        teardown();
    }
}

void SDLDisplayEngine::init(int width, int height, bool isFullscreen, 
        int bpp, int WindowWidth, int WindowHeight)
{
    double AspectRatio = double(width)/double(height);
    if (WindowWidth == 0 && WindowHeight == 0) {
        m_WindowWidth = width;
        m_WindowHeight = height;
    } else if (WindowWidth == 0) {
        m_WindowWidth = int(WindowHeight*AspectRatio);
        m_WindowHeight = WindowHeight;
    } else {
        m_WindowWidth = WindowWidth;
        m_WindowHeight = int(WindowWidth/AspectRatio);
    }
    
    switch (bpp) {
        case 32:
            safeSetAttribute( SDL_GL_RED_SIZE, 8 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 8 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 8 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 32 );
            break;
        case 24:
            safeSetAttribute( SDL_GL_RED_SIZE, 8 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 8 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 8 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 24 );
            break;
        case 16:
            safeSetAttribute( SDL_GL_RED_SIZE, 5 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 6 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 5 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 16 );
            break;
        case 15:
            safeSetAttribute( SDL_GL_RED_SIZE, 5 );
            safeSetAttribute( SDL_GL_GREEN_SIZE, 5 );
            safeSetAttribute( SDL_GL_BLUE_SIZE, 5 );
            safeSetAttribute( SDL_GL_BUFFER_SIZE, 15 );
            break;
        default:
            AVG_TRACE(Logger::ERROR, "Unsupported bpp " << bpp <<
                    "in SDLDisplayEngine::init()");
            exit(-1);
    }
    safeSetAttribute( SDL_GL_DEPTH_SIZE, 0 );
    safeSetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    unsigned int Flags = SDL_OPENGL;
    if (isFullscreen) {
        Flags |= SDL_FULLSCREEN;
    }
    m_pScreen = SDL_SetVideoMode(m_WindowWidth, m_WindowHeight, bpp, Flags);
    if (!m_pScreen) {
        AVG_TRACE(Logger::ERROR, "Setting SDL video mode failed: " 
                << SDL_GetError() <<". (width=" << m_WindowWidth << ", height=" 
                << m_WindowHeight << ", bpp=" << bpp << ").");
        exit(-1);
    }   
    SDL_WM_SetCaption("AVG Renderer", 0);

    glEnable(GL_BLEND);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glEnable(GL_BLEND)");
    glShadeModel(GL_FLAT);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glShadeModel()");
    glDisable(GL_DEPTH_TEST);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glDisable(GL_DEPTH_TEST)");
    int TexMode = OGLSurface::getTextureMode();
    glEnable(TexMode);
    OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "init: glEnable(TexMode);");

    m_YCbCrMode = NONE;
    if (queryOGLExtension("GL_MESA_ycbcr_texture")) {
        m_YCbCrMode = MESA;
    } else if (queryOGLExtension("GL_APPLE_ycbcr_422")) {
        m_YCbCrMode = APPLE;
    }

    m_Width = width;
    m_Height = height;
    initInput();
    // SDL sets up a signal handler we really don't want.
    signal(SIGSEGV, SIG_DFL);
    
    logConfig();
    
//    dumpSDLGLParams();
    m_bEnableCrop = false;
}

void SDLDisplayEngine::teardown()
{
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

double SDLDisplayEngine::getRefreshRate() {
    if (s_RefreshRate == 0.0) {
        calcRefreshRate();
    }
    return s_RefreshRate;
}

void SDLDisplayEngine::logConfig() 
{
{'fraction_non_alphanumeric': 0.11921060297116225, 'fraction_numerical': 0.011214681036993883, 'mean_word_length': 3.188167124123208, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4099780', 'n_tokens_mistral': 5014, 'n_tokens_neox': 4734, 'n_words': 1025}