2014-03-14 18:51:06 +01:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2014-04-18 18:21:34 +02:00
|
|
|
#ifndef INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
|
|
|
|
#define INCLUDED_VCL_OPENGL_OPENGLCONTEXT_HXX
|
2014-03-14 18:51:06 +01:00
|
|
|
|
2014-04-08 14:21:41 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-11-25 20:25:27 +00:00
|
|
|
#include <epoxy/gl.h>
|
2014-04-08 01:17:16 +02:00
|
|
|
|
2015-04-23 13:44:47 +03:00
|
|
|
#include <vcl/dllapi.h>
|
2014-03-17 05:06:00 +01:00
|
|
|
#include <vcl/window.hxx>
|
|
|
|
#include <tools/gen.hxx>
|
|
|
|
#include <vcl/syschild.hxx>
|
2015-09-13 12:15:13 +02:00
|
|
|
#include <rtl/crc.h>
|
2015-09-07 22:21:15 +01:00
|
|
|
#include <rtl/ref.hxx>
|
2014-03-17 05:06:00 +01:00
|
|
|
|
2015-07-10 18:55:12 +02:00
|
|
|
#include <map>
|
2015-09-13 12:15:13 +02:00
|
|
|
#include <memory>
|
2015-01-20 04:12:18 +01:00
|
|
|
#include <set>
|
2015-09-13 12:15:13 +02:00
|
|
|
#include <unordered_map>
|
2015-01-20 04:12:18 +01:00
|
|
|
|
2014-11-26 09:22:25 -05:00
|
|
|
class OpenGLFramebuffer;
|
2014-11-28 14:56:08 -05:00
|
|
|
class OpenGLProgram;
|
2014-11-26 09:22:25 -05:00
|
|
|
class OpenGLTexture;
|
2015-01-20 04:12:18 +01:00
|
|
|
class SalGraphicsImpl;
|
2015-08-29 23:15:54 +01:00
|
|
|
class OpenGLTests;
|
2016-04-28 14:55:45 +09:00
|
|
|
class RenderState;
|
2014-11-26 09:22:25 -05:00
|
|
|
|
2014-03-17 05:06:00 +01:00
|
|
|
/// Holds the information of our new child window
|
2016-05-20 09:41:18 +01:00
|
|
|
struct VCL_DLLPUBLIC GLWindow
|
2014-03-17 05:06:00 +01:00
|
|
|
{
|
|
|
|
unsigned int Width;
|
|
|
|
unsigned int Height;
|
2014-03-17 08:24:14 +01:00
|
|
|
bool bMultiSampleSupported;
|
2014-03-17 05:06:00 +01:00
|
|
|
|
|
|
|
GLWindow()
|
2016-05-20 15:20:41 +01:00
|
|
|
: Width(0)
|
|
|
|
, Height(0)
|
|
|
|
, bMultiSampleSupported(false)
|
2014-03-17 05:06:00 +01:00
|
|
|
{
|
|
|
|
}
|
2014-05-23 02:11:23 +02:00
|
|
|
|
2016-05-20 15:20:41 +01:00
|
|
|
virtual bool Synchronize(bool bOnoff) const;
|
2016-05-20 09:41:18 +01:00
|
|
|
|
2016-05-20 15:20:41 +01:00
|
|
|
virtual ~GLWindow();
|
2014-03-17 05:06:00 +01:00
|
|
|
};
|
2014-03-14 18:51:06 +01:00
|
|
|
|
2016-11-03 23:11:18 +01:00
|
|
|
struct VCL_DLLPUBLIC OpenGLCapabilitySwitch
|
|
|
|
{
|
|
|
|
bool mbLimitedShaderRegisters;
|
|
|
|
|
|
|
|
OpenGLCapabilitySwitch()
|
|
|
|
: mbLimitedShaderRegisters(false)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2015-04-23 13:44:47 +03:00
|
|
|
class VCL_DLLPUBLIC OpenGLContext
|
2014-03-14 18:51:06 +01:00
|
|
|
{
|
2015-08-29 23:15:54 +01:00
|
|
|
friend class OpenGLTests;
|
2016-05-20 09:41:18 +01:00
|
|
|
protected:
|
2014-03-15 07:07:08 +01:00
|
|
|
OpenGLContext();
|
2015-09-08 11:46:13 +01:00
|
|
|
public:
|
2015-09-07 22:21:15 +01:00
|
|
|
static rtl::Reference<OpenGLContext> Create();
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual ~OpenGLContext();
|
2015-09-07 22:21:15 +01:00
|
|
|
void acquire() { mnRefCount++; }
|
|
|
|
void release() { if ( --mnRefCount == 0 ) delete this; }
|
2015-09-08 15:57:55 +01:00
|
|
|
void dispose();
|
2014-03-15 07:07:08 +01:00
|
|
|
|
2014-08-16 05:36:30 +02:00
|
|
|
void requestLegacyContext();
|
2014-10-31 19:19:47 +01:00
|
|
|
void requestSingleBufferedRendering();
|
2014-08-16 05:36:30 +02:00
|
|
|
|
2015-11-10 10:28:29 +01:00
|
|
|
bool init(vcl::Window* pParent = nullptr);
|
2014-04-27 12:20:13 +02:00
|
|
|
bool init(SystemChildWindow* pChildWindow);
|
|
|
|
|
2014-12-04 22:17:58 -05:00
|
|
|
void reset();
|
2014-10-24 17:03:39 +02:00
|
|
|
|
2014-11-26 09:22:25 -05:00
|
|
|
// use these methods right after setting a context to make sure drawing happens
|
|
|
|
// in the right FBO (default one is for onscreen painting)
|
2014-12-04 22:25:56 -05:00
|
|
|
bool BindFramebuffer( OpenGLFramebuffer* pFramebuffer );
|
2014-11-26 09:22:25 -05:00
|
|
|
bool AcquireDefaultFramebuffer();
|
|
|
|
OpenGLFramebuffer* AcquireFramebuffer( const OpenGLTexture& rTexture );
|
2015-04-01 08:33:09 +02:00
|
|
|
static void ReleaseFramebuffer( OpenGLFramebuffer* pFramebuffer );
|
2015-08-31 12:11:50 +01:00
|
|
|
void UnbindTextureFromFramebuffers( GLuint nTexture );
|
2015-12-31 22:12:59 +00:00
|
|
|
static bool IsTextureAttachedAnywhere( GLuint nTexture );
|
2015-09-07 22:21:15 +01:00
|
|
|
|
2014-12-04 22:25:56 -05:00
|
|
|
void ReleaseFramebuffer( const OpenGLTexture& rTexture );
|
|
|
|
void ReleaseFramebuffers();
|
2014-11-26 09:22:25 -05:00
|
|
|
|
2014-11-28 14:56:08 -05:00
|
|
|
// retrieve a program from the cache or compile/link it
|
2015-01-20 14:48:48 +01:00
|
|
|
OpenGLProgram* GetProgram( const OUString& rVertexShader, const OUString& rFragmentShader, const OString& preamble = "" );
|
|
|
|
OpenGLProgram* UseProgram( const OUString& rVertexShader, const OUString& rFragmentShader, const OString& preamble = "" );
|
2015-11-16 20:31:58 +02:00
|
|
|
void UseNoProgram();
|
2014-11-28 14:56:08 -05:00
|
|
|
|
2016-11-28 16:39:26 +01:00
|
|
|
RenderState& state()
|
2016-04-28 14:55:45 +09:00
|
|
|
{
|
2016-11-28 16:39:26 +01:00
|
|
|
return *mpRenderState;
|
2016-04-28 14:55:45 +09:00
|
|
|
}
|
|
|
|
|
2016-11-03 23:11:18 +01:00
|
|
|
OpenGLCapabilitySwitch& getOpenGLCapabilitySwitch()
|
|
|
|
{
|
|
|
|
return maOpenGLCapabilitySwitch;
|
|
|
|
}
|
|
|
|
|
2015-09-02 17:28:39 +01:00
|
|
|
/// Is this GL context the current context ?
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual bool isCurrent();
|
|
|
|
/// Is any GL context the current context ?
|
|
|
|
virtual bool isAnyCurrent();
|
2015-09-02 22:14:10 +01:00
|
|
|
/// release bound resources from the current context
|
2015-01-06 16:09:09 +00:00
|
|
|
static void clearCurrent();
|
2015-09-02 22:14:10 +01:00
|
|
|
/// release contexts etc. before (potentially) allowing another thread run.
|
|
|
|
static void prepareForYield();
|
2015-09-02 17:28:39 +01:00
|
|
|
/// Is there a current GL context ?
|
|
|
|
static bool hasCurrent();
|
2016-01-05 16:17:41 +00:00
|
|
|
|
|
|
|
/// make a VCL context (any context) current, create it if necessary.
|
|
|
|
static void makeVCLCurrent();
|
2016-01-08 13:53:13 +11:00
|
|
|
/// fetch any VCL context, creating one if bMakeIfNecessary is set.
|
2016-01-05 16:17:41 +00:00
|
|
|
static rtl::Reference<OpenGLContext> getVCLContext(bool bMakeIfNecessary = true);
|
2015-01-06 16:09:09 +00:00
|
|
|
/// make this GL context current - so it is implicit in subsequent GL calls
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual void makeCurrent();
|
windows opengl: make sure mpLastContext is indeed the current context
There were two problems here:
1) The OpenGLContext ctor registered the instance on the list of
contexts, but platform-specific call (e.g. wglMakeCurrent()) was only
made later. Add a registerAsCurrent() member function that helps
ensuring that the last item in the context list is indeed the current
context.
2) OpenGLContext::prepareForYield() is called without the solar mutex
being locked, but it still assumes that the last context in the context
list is the thread's current context, which may not be true. The result
is that during JunitTest_sd_unoapi, we end up in a situation like:
debug:4640:5240: OpenGLContext::registerAsCurrent: wglGetCurrentContext() is 00010001, pSVData->maGDIData.mpLastContext is 00FA65F8
debug:4640:7944: OpenGLContext::registerAsCurrent: wglGetCurrentContext() is 000D0003, pSVData->maGDIData.mpLastContext is 00FA6C70
debug:4640:5240: OpenGLContext::prepareForYield: start, wglGetCurrentContext() is 00010001, pSVData->maGDIData.mpLastContext is 00FA6C70
I.e. one thread registers as current, an other registers as current, too (while
the other thread has the solar mutex), then once the original thread wants to
release the solar mutex, the real current context and the last item in the
context list won't match, so the assert at the end of prepareForYield() will
fail.
Fix this by releasing the GL context in WinSalInstance::DestroyFrame().
With this, JunitTest_sd_unoapi passes on Windows with GL enabled.
Change-Id: Icfb9c65c871586b5df69b5a2ab3aa91843dfc799
Reviewed-on: https://gerrit.libreoffice.org/18473
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Michael Meeks <michael.meeks@collabora.com>
2015-09-10 17:25:27 +02:00
|
|
|
/// Put this GL context to the end of the context list.
|
|
|
|
void registerAsCurrent();
|
2015-01-06 16:09:09 +00:00
|
|
|
/// reset the GL context so this context is not implicit in subsequent GL calls.
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual void resetCurrent();
|
2016-11-25 14:11:13 +00:00
|
|
|
/// unbind the GL_FRAMEBUFFER to its default state, needed for gtk3
|
|
|
|
virtual void restoreDefaultFramebuffer();
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual void swapBuffers();
|
|
|
|
virtual void sync();
|
2014-05-19 19:21:29 +02:00
|
|
|
void show();
|
2014-04-27 12:20:13 +02:00
|
|
|
|
2014-05-19 19:21:29 +02:00
|
|
|
void setWinPosAndSize(const Point &rPos, const Size& rSize);
|
2014-03-14 18:51:06 +01:00
|
|
|
void setWinSize(const Size& rSize);
|
2016-05-20 15:20:41 +01:00
|
|
|
virtual const GLWindow& getOpenGLWindow() const = 0;
|
2014-03-14 18:51:06 +01:00
|
|
|
|
2014-08-08 01:55:33 +02:00
|
|
|
SystemChildWindow* getChildWindow();
|
|
|
|
const SystemChildWindow* getChildWindow() const;
|
|
|
|
|
2014-04-15 00:27:25 +02:00
|
|
|
bool isInitialized()
|
|
|
|
{
|
|
|
|
return mbInitialized;
|
|
|
|
}
|
|
|
|
|
2015-12-11 18:18:50 +00:00
|
|
|
/// VCL promiscuously re-uses its own contexts:
|
|
|
|
void setVCLOnly() { mbVCLOnly = true; }
|
|
|
|
bool isVCLOnly() { return mbVCLOnly; }
|
|
|
|
|
2014-08-29 11:55:23 +02:00
|
|
|
bool supportMultiSampling() const;
|
|
|
|
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual SystemWindowData generateWinData(vcl::Window* pParent, bool bRequestLegacyContext);
|
2014-04-27 12:09:20 +02:00
|
|
|
|
2014-03-14 18:51:06 +01:00
|
|
|
private:
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual bool initWindow();
|
|
|
|
virtual void destroyCurrentContext();
|
2014-09-01 01:59:57 +02:00
|
|
|
|
2016-05-20 09:41:18 +01:00
|
|
|
protected:
|
2016-11-25 20:25:27 +00:00
|
|
|
bool InitGL();
|
|
|
|
static void InitGLDebugging();
|
2016-05-22 21:10:38 +02:00
|
|
|
static void InitChildWindow(SystemChildWindow *pChildWindow);
|
|
|
|
static void BuffersSwapped();
|
2016-05-20 15:20:41 +01:00
|
|
|
virtual GLWindow& getModifiableOpenGLWindow() = 0;
|
2016-05-20 09:41:18 +01:00
|
|
|
virtual bool ImplInit();
|
|
|
|
|
2015-03-09 14:29:30 +02:00
|
|
|
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;
|
2014-04-02 03:31:01 +02:00
|
|
|
bool mbInitialized;
|
2014-12-02 21:51:50 +01:00
|
|
|
int mnRefCount;
|
2014-08-16 05:36:30 +02:00
|
|
|
bool mbRequestLegacyContext;
|
2014-10-31 19:19:47 +01:00
|
|
|
bool mbUseDoubleBufferedRendering;
|
2015-12-11 18:18:50 +00:00
|
|
|
bool mbVCLOnly;
|
2014-11-22 07:58:38 -05:00
|
|
|
|
2014-12-11 07:46:51 +01:00
|
|
|
int mnFramebufferCount;
|
2014-11-26 09:22:25 -05:00
|
|
|
OpenGLFramebuffer* mpCurrentFramebuffer;
|
|
|
|
OpenGLFramebuffer* mpFirstFramebuffer;
|
|
|
|
OpenGLFramebuffer* mpLastFramebuffer;
|
|
|
|
|
2016-11-03 23:11:18 +01:00
|
|
|
OpenGLCapabilitySwitch maOpenGLCapabilitySwitch;
|
|
|
|
|
2016-05-20 09:41:18 +01:00
|
|
|
private:
|
2015-09-13 12:15:13 +02:00
|
|
|
struct ProgramHash
|
2015-01-20 14:48:48 +01:00
|
|
|
{
|
2015-09-13 12:15:13 +02:00
|
|
|
size_t operator()( const rtl::OString& aDigest ) const
|
|
|
|
{
|
|
|
|
return (size_t)( rtl_crc32( 0, aDigest.getStr(), aDigest.getLength() ) );
|
|
|
|
}
|
2015-01-20 14:48:48 +01:00
|
|
|
};
|
2015-09-13 12:15:13 +02:00
|
|
|
|
|
|
|
typedef std::unordered_map< rtl::OString, std::shared_ptr<OpenGLProgram>, ProgramHash > ProgramCollection;
|
|
|
|
ProgramCollection maPrograms;
|
2014-11-28 14:56:08 -05:00
|
|
|
OpenGLProgram* mpCurrentProgram;
|
|
|
|
|
2016-04-28 14:55:45 +09:00
|
|
|
std::unique_ptr<RenderState> mpRenderState;
|
|
|
|
|
2014-11-22 07:58:38 -05:00
|
|
|
public:
|
2014-11-22 08:07:47 -05:00
|
|
|
vcl::Region maClipRegion;
|
2014-11-22 08:04:23 -05:00
|
|
|
int mnPainting;
|
|
|
|
|
2015-09-07 22:21:15 +01:00
|
|
|
// Don't hold references to ourselves:
|
|
|
|
OpenGLContext *mpPrevContext;
|
|
|
|
OpenGLContext *mpNextContext;
|
2014-03-14 18:51:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|