summaryrefslogtreecommitdiff
path: root/src/graphics/GLContext.h
blob: 5043f5f9d0a3203698b3afadb4cccbfee7edc06f (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  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
//
#ifndef _GLContext_H_
#define _GLContext_H_

#include "../api.h"

#include "OGLHelper.h"
#include "GLBufferCache.h"
#include "GLConfig.h"

#include "../base/GLMHelper.h"

#include <boost/shared_ptr.hpp>
#include <boost/thread/tss.hpp>

struct SDL_SysWMinfo;

namespace avg {

class GLContext;
typedef boost::shared_ptr<GLContext> GLContextPtr;
class ShaderRegistry;
typedef boost::shared_ptr<ShaderRegistry> ShaderRegistryPtr;
class StandardShader;
typedef boost::shared_ptr<StandardShader> StandardShaderPtr;

class AVG_API GLContext
{
public:
    static GLContext* create(const GLConfig& glConfig, 
            const IntPoint& windowSize=IntPoint(0,0), const SDL_SysWMinfo* pSDLWMInfo=0);

    GLContext(const IntPoint& windowSize, const SDL_SysWMinfo* pSDLWMInfo);
    virtual ~GLContext();

    virtual void activate()=0;
    ShaderRegistryPtr getShaderRegistry() const;
    StandardShaderPtr getStandardShader();
    bool useGPUYUVConversion() const;
    GLConfig::ShaderUsage getShaderUsage() const;

    // GL Object caching.
    GLBufferCache& getVertexBufferCache();
    GLBufferCache& getIndexBufferCache();
    GLBufferCache& getPBOCache();
    unsigned genFBO();
    void returnFBOToCache(unsigned fboID);

    // GL state cache.
    void setBlendColor(const glm::vec4& color);
    enum BlendMode {BLEND_BLEND, BLEND_ADD, BLEND_MIN, BLEND_MAX, BLEND_COPY};
    void setBlendMode(BlendMode mode, bool bPremultipliedAlpha = false);
    bool isBlendModeSupported(BlendMode mode) const;
    void bindTexture(unsigned unit, unsigned texID);

    const GLConfig& getConfig();
    void logConfig();
    size_t getVideoMemInstalled();
    size_t getVideoMemUsed();
    int getMaxTexSize();
    bool usePOTTextures();
    bool arePBOsSupported();
    OGLMemoryMode getMemoryMode();
    bool isGLES() const;
    bool isVendor(const std::string& sWantedVendor) const;
    virtual bool useDepthBuffer() const;

    virtual bool initVBlank(int rate)=0;
    virtual void swapBuffers();

    static void enableErrorChecks(bool bEnable);
    static void checkError(const char* pszWhere);
    static void mandatoryCheckError(const char* pszWhere);
    void ensureFullShaders(const std::string& sContext) const;

    static BlendMode stringToBlendMode(const std::string& s);

    static GLContext* getCurrent();
    static GLContext* getMain();
    static void setMain(GLContext * pMainContext);

    static int nextMultiSampleValue(int curSamples);
    static bool isGLESSupported();
    static void enableErrorLog(bool bEnable);

protected:
    void init(const GLConfig& glConfig, bool bOwnsContext);
    void deleteObjects();

    void getVersion(int& major, int& minor) const;
    bool ownsContext() const;

    void setCurrent();

private:
    void checkGPUMemInfoSupport();
    bool isDebugContextSupported() const;
    static void APIENTRY debugLogCallback(GLenum source, GLenum type, GLuint id, 
        GLenum severity, GLsizei length, const GLchar* message, void* userParam);

    bool m_bOwnsContext;
    
    ShaderRegistryPtr m_pShaderRegistry;
    StandardShaderPtr m_pStandardShader;

    GLBufferCache m_VertexBufferCache;
    GLBufferCache m_IndexBufferCache;
    GLBufferCache m_PBOCache;
    std::vector<unsigned int> m_FBOIDs;

    int m_MaxTexSize;
    GLConfig m_GLConfig;
    bool m_bCheckedGPUMemInfoExtension;
    bool m_bGPUMemInfoSupported;
    bool m_bCheckedMemoryMode;
    OGLMemoryMode m_MemoryMode;

    // OpenGL state
    glm::vec4 m_BlendColor;
    BlendMode m_BlendMode;
    bool m_bPremultipliedAlpha;
    unsigned m_BoundTextures[16];

    int m_MajorGLVersion;
    int m_MinorGLVersion;

    static bool s_bErrorCheckEnabled;
    static bool s_bErrorLogEnabled;

    static boost::thread_specific_ptr<GLContext*> s_pCurrentContext;
    static GLContext* s_pMainContext;
};

}
#endif