1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup GHOST
22  */
23 
24 #pragma once
25 
26 //#define WIN32_COMPOSITING
27 
28 #include "GHOST_Context.h"
29 
30 #include <GL/wglew.h>
31 
32 #ifndef GHOST_OPENGL_WGL_RESET_NOTIFICATION_STRATEGY
33 #  define GHOST_OPENGL_WGL_RESET_NOTIFICATION_STRATEGY 0
34 #endif
35 
36 class GHOST_ContextWGL : public GHOST_Context {
37   /* XR code needs low level graphics data to send to OpenXR. */
38   friend class GHOST_XrGraphicsBindingOpenGL;
39 
40  public:
41   /**
42    * Constructor.
43    */
44   GHOST_ContextWGL(bool stereoVisual,
45                    bool alphaBackground,
46                    HWND hWnd,
47                    HDC hDC,
48                    int contextProfileMask,
49                    int contextMajorVersion,
50                    int contextMinorVersion,
51                    int contextFlags,
52                    int contextResetNotificationStrategy);
53 
54   /**
55    * Destructor.
56    */
57   ~GHOST_ContextWGL();
58 
59   /**
60    * Swaps front and back buffers of a window.
61    * \return  A boolean success indicator.
62    */
63   GHOST_TSuccess swapBuffers();
64 
65   /**
66    * Activates the drawing context of this window.
67    * \return  A boolean success indicator.
68    */
69   GHOST_TSuccess activateDrawingContext();
70 
71   /**
72    * Release the drawing context of the calling thread.
73    * \return  A boolean success indicator.
74    */
75   GHOST_TSuccess releaseDrawingContext();
76 
77   /**
78    * Call immediately after new to initialize.  If this fails then immediately delete the object.
79    * \return Indication as to whether initialization has succeeded.
80    */
81   GHOST_TSuccess initializeDrawingContext();
82 
83   /**
84    * Removes references to native handles from this context and then returns
85    * \return GHOST_kSuccess if it is OK for the parent to release the handles and
86    * GHOST_kFailure if releasing the handles will interfere with sharing
87    */
88   GHOST_TSuccess releaseNativeHandles();
89 
90   /**
91    * Sets the swap interval for swapBuffers.
92    * \param interval The swap interval to use.
93    * \return A boolean success indicator.
94    */
95   GHOST_TSuccess setSwapInterval(int interval);
96 
97   /**
98    * Gets the current swap interval for swapBuffers.
99    * \param intervalOut Variable to store the swap interval if it can be read.
100    * \return Whether the swap interval can be read.
101    */
102   GHOST_TSuccess getSwapInterval(int &intervalOut);
103 
104  private:
105   int choose_pixel_format(bool stereoVisual, bool needAlpha);
106   int choose_pixel_format_arb(bool stereoVisual, bool needAlpha);
107   int _choose_pixel_format_arb_1(bool stereoVisual, bool needAlpha);
108 
109   void initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD);
110 
111   HWND m_hWnd;
112   HDC m_hDC;
113 
114   const int m_contextProfileMask;
115   const int m_contextMajorVersion;
116   const int m_contextMinorVersion;
117   const int m_contextFlags;
118   const bool m_alphaBackground;
119   const int m_contextResetNotificationStrategy;
120 
121   HGLRC m_hGLRC;
122 
123 #ifndef NDEBUG
124   const char *m_dummyVendor;
125   const char *m_dummyRenderer;
126   const char *m_dummyVersion;
127 #endif
128 
129   static HGLRC s_sharedHGLRC;
130   static int s_sharedCount;
131 };
132