1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <Qt5OpenGLContext.hxx>
21 
22 #include <vcl/sysdata.hxx>
23 #include <opengl/zone.hxx>
24 #include <sal/log.hxx>
25 
26 #include <window.h>
27 
28 #include <Qt5Object.hxx>
29 
30 #include <QtGui/QOpenGLContext>
31 #include <QtGui/QWindow>
32 
33 bool Qt5OpenGLContext::g_bAnyCurrent = false;
34 
swapBuffers()35 void Qt5OpenGLContext::swapBuffers()
36 {
37     OpenGLZone aZone;
38 
39     if (m_pContext && m_pWindow && m_pWindow->isExposed())
40     {
41         m_pContext->swapBuffers(m_pWindow);
42     }
43 
44     BuffersSwapped();
45 }
46 
resetCurrent()47 void Qt5OpenGLContext::resetCurrent()
48 {
49     clearCurrent();
50 
51     OpenGLZone aZone;
52 
53     if (m_pContext)
54     {
55         m_pContext->doneCurrent();
56         g_bAnyCurrent = false;
57     }
58 }
59 
isCurrent()60 bool Qt5OpenGLContext::isCurrent()
61 {
62     OpenGLZone aZone;
63     return g_bAnyCurrent && (QOpenGLContext::currentContext() == m_pContext);
64 }
65 
isAnyCurrent()66 bool Qt5OpenGLContext::isAnyCurrent()
67 {
68     OpenGLZone aZone;
69     return g_bAnyCurrent && (QOpenGLContext::currentContext() != nullptr);
70 }
71 
ImplInit()72 bool Qt5OpenGLContext::ImplInit()
73 {
74     if (!m_pWindow)
75     {
76         SAL_WARN("vcl.opengl.qt5", "failed to create window");
77         return false;
78     }
79 
80     m_pWindow->setSurfaceType(QSurface::OpenGLSurface);
81     m_pWindow->create();
82 
83     m_pContext = new QOpenGLContext(m_pWindow);
84     if (!m_pContext->create())
85     {
86         SAL_WARN("vcl.opengl.qt5", "failed to create context");
87         return false;
88     }
89 
90     m_pContext->makeCurrent(m_pWindow);
91     g_bAnyCurrent = true;
92 
93     bool bRet = InitGL();
94     InitGLDebugging();
95 
96     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
97 
98     registerAsCurrent();
99 
100     return bRet;
101 }
102 
makeCurrent()103 void Qt5OpenGLContext::makeCurrent()
104 {
105     if (isCurrent())
106         return;
107 
108     OpenGLZone aZone;
109 
110     clearCurrent();
111 
112     if (m_pContext && m_pWindow)
113     {
114         m_pContext->makeCurrent(m_pWindow);
115         g_bAnyCurrent = true;
116     }
117 
118     registerAsCurrent();
119 }
120 
destroyCurrentContext()121 void Qt5OpenGLContext::destroyCurrentContext()
122 {
123     OpenGLZone aZone;
124 
125     if (m_pContext)
126     {
127         m_pContext->doneCurrent();
128         g_bAnyCurrent = false;
129     }
130 
131     if (glGetError() != GL_NO_ERROR)
132     {
133         SAL_WARN("vcl.opengl.qt5", "glError: " << glGetError());
134     }
135 }
136 
initWindow()137 void Qt5OpenGLContext::initWindow()
138 {
139     if (!m_pChildWindow)
140     {
141         SystemWindowData winData = generateWinData(mpWindow, mbRequestLegacyContext);
142         m_pChildWindow = VclPtr<SystemChildWindow>::Create(mpWindow, 0, &winData, false);
143     }
144 
145     if (m_pChildWindow)
146     {
147         InitChildWindow(m_pChildWindow.get());
148     }
149 
150     m_pWindow
151         = static_cast<Qt5Object*>(m_pChildWindow->ImplGetWindowImpl()->mpSysObj)->windowHandle();
152 }
153