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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
#define INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
#include <string.h>
#include <GL/glew.h>
#if defined( MACOSX )
#elif defined( IOS )
#elif defined( ANDROID )
#elif defined( LIBO_HEADLESS )
#elif defined( UNX )
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include "GL/glxew.h"
#elif defined( _WIN32 )
#ifndef INCLUDED_PRE_POST_WIN_H
#define INCLUDED_PRE_POST_WIN_H
# include "prewin.h"
# include "postwin.h"
#endif
#endif
#if defined( _WIN32 )
#include <GL/wglew.h>
#elif defined( MACOSX )
#include <OpenGL/OpenGL.h>
#ifdef __OBJC__
@class NSOpenGLView;
#else
class NSOpenGLView;
#endif
#elif defined( IOS )
#elif defined( ANDROID )
#elif defined( LIBO_HEADLESS )
#elif defined( UNX )
#endif
#include <vcl/dllapi.h>
#include <vcl/window.hxx>
#include <tools/gen.hxx>
#include <vcl/syschild.hxx>
#include <rtl/crc.h>
#include <rtl/ref.hxx>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
class OpenGLFramebuffer;
class OpenGLProgram;
class OpenGLTexture;
class SalGraphicsImpl;
class OpenGLTests;
class RenderState;
/// Holds the information of our new child window
struct VCL_DLLPUBLIC GLWindow
{
#if defined( _WIN32 )
HWND hWnd;
HDC hDC;
HGLRC hRC;
#elif defined( MACOSX )
#elif defined( IOS )
#elif defined( ANDROID )
#elif defined( LIBO_HEADLESS )
#elif defined( UNX )
Display* dpy;
int screen;
Window win;
XVisualInfo* vi;
GLXContext ctx;
bool HasGLXExtension( const char* name ) const;
const char* GLXExtensions;
#endif
unsigned int Width;
unsigned int Height;
const GLubyte* GLExtensions;
bool bMultiSampleSupported;
GLWindow()
:
#if defined( _WIN32 )
hWnd(NULL),
hDC(NULL),
hRC(NULL),
#elif defined( MACOSX )
#elif defined( IOS )
#elif defined( ANDROID )
#elif defined( LIBO_HEADLESS )
#elif defined( UNX )
dpy(nullptr),
screen(0),
win(0),
vi(nullptr),
ctx(nullptr),
GLXExtensions(nullptr),
#endif
Width(0),
Height(0),
GLExtensions(nullptr),
bMultiSampleSupported(false)
{
}
bool Synchronize(bool bOnoff) const;
~GLWindow();
};
class VCL_DLLPUBLIC OpenGLContext
{
friend class OpenGLTests;
protected:
OpenGLContext();
public:
static rtl::Reference<OpenGLContext> Create();
virtual ~OpenGLContext();
void acquire() { mnRefCount++; }
void release() { if ( --mnRefCount == 0 ) delete this; }
void dispose();
void requestLegacyContext();
void requestSingleBufferedRendering();
bool init(vcl::Window* pParent = nullptr);
bool init(SystemChildWindow* pChildWindow);
void reset();
// use these methods right after setting a context to make sure drawing happens
// in the right FBO (default one is for onscreen painting)
bool BindFramebuffer( OpenGLFramebuffer* pFramebuffer );
bool AcquireDefaultFramebuffer();
OpenGLFramebuffer* AcquireFramebuffer( const OpenGLTexture& rTexture );
static void ReleaseFramebuffer( OpenGLFramebuffer* pFramebuffer );
void UnbindTextureFromFramebuffers( GLuint nTexture );
static bool IsTextureAttachedAnywhere( GLuint nTexture );
void ReleaseFramebuffer( const OpenGLTexture& rTexture );
void ReleaseFramebuffers();
// retrieve a program from the cache or compile/link it
OpenGLProgram* GetProgram( const OUString& rVertexShader, const OUString& rFragmentShader, const OString& preamble = "" );
OpenGLProgram* UseProgram( const OUString& rVertexShader, const OUString& rFragmentShader, const OString& preamble = "" );
void UseNoProgram();
std::unique_ptr<RenderState>& state()
{
return mpRenderState;
}
/// Is this GL context the current context ?
virtual bool isCurrent();
/// Is any GL context the current context ?
virtual bool isAnyCurrent();
/// release bound resources from the current context
static void clearCurrent();
/// release contexts etc. before (potentially) allowing another thread run.
static void prepareForYield();
/// Is there a current GL context ?
static bool hasCurrent();
/// make a VCL context (any context) current, create it if necessary.
static void makeVCLCurrent();
/// fetch any VCL context, creating one if bMakeIfNecessary is set.
static rtl::Reference<OpenGLContext> getVCLContext(bool bMakeIfNecessary = true);
/// make this GL context current - so it is implicit in subsequent GL calls
virtual void makeCurrent();
/// Put this GL context to the end of the context list.
void registerAsCurrent();
/// reset the GL context so this context is not implicit in subsequent GL calls.
virtual void resetCurrent();
virtual void swapBuffers();
virtual void sync();
void show();
void setWinPosAndSize(const Point &rPos, const Size& rSize);
void setWinSize(const Size& rSize);
const GLWindow& getOpenGLWindow() const;
SystemChildWindow* getChildWindow();
const SystemChildWindow* getChildWindow() const;
bool isInitialized()
{
return mbInitialized;
}
/// VCL promiscuously re-uses its own contexts:
void setVCLOnly() { mbVCLOnly = true; }
bool isVCLOnly() { return mbVCLOnly; }
bool supportMultiSampling() const;
virtual SystemWindowData generateWinData(vcl::Window* pParent, bool bRequestLegacyContext);
private:
virtual bool initWindow();
virtual void destroyCurrentContext();
protected:
GLWindow m_aGLWin;
bool InitGLEW();
void InitGLEWDebugging();
void InitChildWindow(SystemChildWindow *pChildWindow);
void BuffersSwapped();
virtual bool ImplInit();
VclPtr<vcl::Window> m_xWindow;
VclPtr<vcl::Window> mpWindow; //points to m_pWindow or the parent window, don't delete it
VclPtr<SystemChildWindow> m_pChildWindow;
bool mbInitialized;
int mnRefCount;
bool mbRequestLegacyContext;
bool mbUseDoubleBufferedRendering;
bool mbVCLOnly;
int mnFramebufferCount;
OpenGLFramebuffer* mpCurrentFramebuffer;
OpenGLFramebuffer* mpFirstFramebuffer;
OpenGLFramebuffer* mpLastFramebuffer;
private:
struct ProgramHash
{
size_t operator()( const rtl::OString& aDigest ) const
{
return (size_t)( rtl_crc32( 0, aDigest.getStr(), aDigest.getLength() ) );
}
};
typedef std::unordered_map< rtl::OString, std::shared_ptr<OpenGLProgram>, ProgramHash > ProgramCollection;
ProgramCollection maPrograms;
OpenGLProgram* mpCurrentProgram;
std::unique_ptr<RenderState> mpRenderState;
public:
vcl::Region maClipRegion;
int mnPainting;
// Don't hold references to ourselves:
OpenGLContext *mpPrevContext;
OpenGLContext *mpNextContext;
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|