1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
11 #define INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
19 #elif defined( ANDROID )
20 #elif defined( LIBO_HEADLESS )
23 # include "GL/glxew.h"
25 #elif defined( _WIN32 )
26 #ifndef INCLUDED_PRE_POST_WIN_H
27 #define INCLUDED_PRE_POST_WIN_H
36 #include <GL/wglext.h>
37 #elif defined( MACOSX )
38 #include <OpenGL/OpenGL.h>
45 #elif defined( ANDROID )
46 #elif defined( LIBO_HEADLESS )
49 #define GLX_GLXEXT_PROTOTYPES 1
51 #include <GL/glxext.h>
54 #include <vcl/dllapi.h>
55 #include <boost/ptr_container/ptr_map.hpp>
56 #include <vcl/window.hxx>
57 #include <tools/gen.hxx>
58 #include <vcl/syschild.hxx>
60 #include <rtl/ref.hxx>
65 #include <unordered_map>
67 class OpenGLFramebuffer
;
70 class SalGraphicsImpl
;
73 /// Holds the information of our new child window
76 // Copy of gluCheckExtension(), from the Apache-licensed
77 // https://code.google.com/p/glues/source/browse/trunk/glues/source/glues_registry.c
78 static GLboolean
checkExtension(const GLubyte
* extName
, const GLubyte
* extString
)
80 GLboolean flag
=GL_FALSE
;
90 deleteThis
=lookHere
=static_cast<char*>(malloc(strlen(reinterpret_cast<const char*>(extString
))+1));
96 /* strtok() will modify string, so copy it somewhere */
97 strcpy(lookHere
, reinterpret_cast<const char*>(extString
));
99 while ((word
=strtok(lookHere
, " "))!=NULL
)
101 if (strcmp(word
, reinterpret_cast<const char*>(extName
))==0)
106 lookHere
=NULL
; /* get next token */
108 free((void*)deleteThis
);
113 #if defined( _WIN32 )
117 #elif defined( MACOSX )
119 #elif defined( ANDROID )
120 #elif defined( LIBO_HEADLESS )
126 #if defined( GLX_EXT_texture_from_pixmap )
133 bool HasGLXExtension( const char* name
) { return checkExtension( reinterpret_cast<const GLubyte
*>(name
), reinterpret_cast<const GLubyte
*>(GLXExtensions
) ); }
134 const char* GLXExtensions
;
139 const GLubyte
* GLExtensions
;
140 bool bMultiSampleSupported
;
144 #if defined( _WIN32 )
148 #elif defined( MACOSX )
150 #elif defined( ANDROID )
151 #elif defined( LIBO_HEADLESS )
157 #if defined( GLX_EXT_texture_from_pixmap )
169 bMultiSampleSupported(false)
176 class VCL_DLLPUBLIC OpenGLContext
178 friend class OpenGLTests
;
181 static rtl::Reference
<OpenGLContext
> Create();
183 void acquire() { mnRefCount
++; }
184 void release() { if ( --mnRefCount
== 0 ) delete this; }
187 void requestLegacyContext();
188 void requestSingleBufferedRendering();
189 void requestVirtualDevice();
191 bool init(vcl::Window
* pParent
= 0);
192 bool init(SystemChildWindow
* pChildWindow
);
194 // these methods are for the deep platform layer, don't use them in normal code
195 // only in vcl's platform code
196 #if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined(LIBO_HEADLESS)
197 bool init(Display
* dpy
, Window win
, int screen
);
198 bool init(Display
* dpy
, Pixmap pix
, unsigned int width
, unsigned int height
, int nScreen
);
199 #elif defined( _WIN32 )
200 bool init( HDC hDC
, HWND hWnd
);
204 // use these methods right after setting a context to make sure drawing happens
205 // in the right FBO (default one is for onscreen painting)
206 bool BindFramebuffer( OpenGLFramebuffer
* pFramebuffer
);
207 bool AcquireDefaultFramebuffer();
208 OpenGLFramebuffer
* AcquireFramebuffer( const OpenGLTexture
& rTexture
);
209 static void ReleaseFramebuffer( OpenGLFramebuffer
* pFramebuffer
);
210 void UnbindTextureFromFramebuffers( GLuint nTexture
);
212 void ReleaseFramebuffer( const OpenGLTexture
& rTexture
);
213 void ReleaseFramebuffers();
215 // retrieve a program from the cache or compile/link it
216 OpenGLProgram
* GetProgram( const OUString
& rVertexShader
, const OUString
& rFragmentShader
, const OString
& preamble
= "" );
217 OpenGLProgram
* UseProgram( const OUString
& rVertexShader
, const OUString
& rFragmentShader
, const OString
& preamble
= "" );
219 /// Is this GL context the current context ?
221 /// release bound resources from the current context
222 static void clearCurrent();
223 /// release contexts etc. before (potentially) allowing another thread run.
224 static void prepareForYield();
225 /// Is there a current GL context ?
226 static bool hasCurrent();
227 /// make this GL context current - so it is implicit in subsequent GL calls
229 /// Put this GL context to the end of the context list.
230 void registerAsCurrent();
231 /// reset the GL context so this context is not implicit in subsequent GL calls.
237 void setWinPosAndSize(const Point
&rPos
, const Size
& rSize
);
238 void setWinSize(const Size
& rSize
);
239 const GLWindow
& getOpenGLWindow() const { return m_aGLWin
;}
241 SystemChildWindow
* getChildWindow();
242 const SystemChildWindow
* getChildWindow() const;
248 return mbInitialized
;
251 bool requestedLegacy()
253 return mbRequestLegacyContext
;
256 bool supportMultiSampling() const;
258 static SystemWindowData
generateWinData(vcl::Window
* pParent
, bool bRequestLegacyContext
);
261 SAL_DLLPRIVATE
bool InitGLEW();
262 SAL_DLLPRIVATE
void InitGLEWDebugging();
263 SAL_DLLPRIVATE
bool initWindow();
264 SAL_DLLPRIVATE
bool ImplInit();
265 #if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined(LIBO_HEADLESS)
266 SAL_DLLPRIVATE
void initGLWindow(Visual
* pVisual
);
270 NSOpenGLView
* getOpenGLView();
274 VclPtr
<vcl::Window
> m_xWindow
;
275 VclPtr
<vcl::Window
> mpWindow
; //points to m_pWindow or the parent window, don't delete it
276 VclPtr
<SystemChildWindow
> m_pChildWindow
;
279 bool mbRequestLegacyContext
;
280 bool mbUseDoubleBufferedRendering
;
281 bool mbRequestVirtualDevice
;
282 #if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID && !defined(LIBO_HEADLESS)
283 bool mbPixmap
; // is a pixmap instead of a window
286 int mnFramebufferCount
;
287 OpenGLFramebuffer
* mpCurrentFramebuffer
;
288 OpenGLFramebuffer
* mpFirstFramebuffer
;
289 OpenGLFramebuffer
* mpLastFramebuffer
;
293 size_t operator()( const rtl::OString
& aDigest
) const
295 return (size_t)( rtl_crc32( 0, aDigest
.getStr(), aDigest
.getLength() ) );
298 typedef std::unordered_map
< rtl::OString
, std::shared_ptr
<OpenGLProgram
>, ProgramHash
> ProgramCollection
;
299 ProgramCollection maPrograms
;
300 OpenGLProgram
* mpCurrentProgram
;
302 std::set
<SalGraphicsImpl
*> maParents
;
306 vcl::Region maClipRegion
;
309 // Don't hold references to ourselves:
310 OpenGLContext
*mpPrevContext
;
311 OpenGLContext
*mpNextContext
;
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */