• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkRefCnt.h"
9 
10 #ifndef SkWGL_DEFINED
11 #define SkWGL_DEFINED
12 
13 #include "src/core/SkLeanWindows.h"
14 
15 /**
16  * Working with WGL extensions can be a pain. Among the reasons is that you must
17  * have a GL context to get the proc addresses, but you want to use the procs to
18  * create a context in the first place. So you have to create a placeholder GL
19  * ctx to get the proc addresses.
20  *
21  * This file helps by providing SkCreateWGLInterface(). It returns a struct of
22  * function pointers that it initializes. It also has a helper function to query
23  * for WGL extensions. It handles the fact that wglGetExtensionsString is itself
24  * an extension.
25  */
26 
27 #define SK_WGL_DRAW_TO_WINDOW                       0x2001
28 #define SK_WGL_ACCELERATION                         0x2003
29 #define SK_WGL_SUPPORT_OPENGL                       0x2010
30 #define SK_WGL_DOUBLE_BUFFER                        0x2011
31 #define SK_WGL_COLOR_BITS                           0x2014
32 #define SK_WGL_RED_BITS                             0x2015
33 #define SK_WGL_GREEN_BITS                           0x2017
34 #define SK_WGL_BLUE_BITS                            0x2019
35 #define SK_WGL_ALPHA_BITS                           0x201B
36 #define SK_WGL_STENCIL_BITS                         0x2023
37 #define SK_WGL_FULL_ACCELERATION                    0x2027
38 #define SK_WGL_SAMPLE_BUFFERS                       0x2041
39 #define SK_WGL_SAMPLES                              0x2042
40 #define SK_WGL_CONTEXT_MAJOR_VERSION                0x2091
41 #define SK_WGL_CONTEXT_MINOR_VERSION                0x2092
42 #define SK_WGL_CONTEXT_LAYER_PLANE                  0x2093
43 #define SK_WGL_CONTEXT_FLAGS                        0x2094
44 #define SK_WGL_CONTEXT_PROFILE_MASK                 0x9126
45 #define SK_WGL_CONTEXT_DEBUG_BIT                    0x0001
46 #define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT       0x0002
47 #define SK_WGL_CONTEXT_CORE_PROFILE_BIT             0x00000001
48 #define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT    0x00000002
49 #define SK_WGL_CONTEXT_ES2_PROFILE_BIT              0x00000004
50 #define SK_ERROR_INVALID_VERSION                    0x2095
51 #define SK_ERROR_INVALID_PROFILE                    0x2096
52 
53 DECLARE_HANDLE(HPBUFFER);
54 
55 class SkWGLExtensions {
56 public:
57     SkWGLExtensions();
58     /**
59      * Determines if an extensions is available for a given DC.
60      * WGL_extensions_string is considered a prerequisite for all other
61      * extensions. It is necessary to check this before calling other class
62      * functions.
63      */
64     bool hasExtension(HDC dc, const char* ext) const;
65 
66     const char* getExtensionsString(HDC hdc) const;
67     BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
68     BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
69     BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
70     HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
71 
72     BOOL swapInterval(int interval) const;
73 
74     HPBUFFER createPbuffer(HDC, int , int, int, const int*) const;
75     HDC getPbufferDC(HPBUFFER) const;
76     int releasePbufferDC(HPBUFFER, HDC) const;
77     BOOL destroyPbuffer(HPBUFFER) const;
78 
79     /**
80      * WGL doesn't have precise rules for the ordering of formats returned
81      * by wglChoosePixelFormat. This function helps choose among the set of
82      * formats returned by wglChoosePixelFormat. The rules in decreasing
83      * priority are:
84      *     * Choose formats with the smallest sample count that is >=
85      *       desiredSampleCount (or the largest sample count if all formats have
86      *       fewer samples than desiredSampleCount.) If desiredSampleCount is 1 then
87      *       all msaa formats are excluded from consideration.
88      *     * Choose formats with the fewest color samples when coverage sampling
89      *       is available.
90      *     * If the above rules leave multiple formats, choose the one that
91      *       appears first in the formats array parameter.
92      */
93     int selectFormat(const int formats[],
94                      int formatCount,
95                      HDC dc,
96                      int desiredSampleCount) const;
97 private:
98     typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
99     typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
100     typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
101     typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*);
102     typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *);
103     typedef BOOL (WINAPI* SwapIntervalProc)(int);
104     typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*);
105     typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER);
106     typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC);
107     typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER);
108 
109     static GetExtensionsStringProc fGetExtensionsString;
110     static ChoosePixelFormatProc fChoosePixelFormat;
111     static GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
112     static GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
113     static CreateContextAttribsProc fCreateContextAttribs;
114     static SwapIntervalProc fSwapInterval;
115     static CreatePbufferProc fCreatePbuffer;
116     static GetPbufferDCProc fGetPbufferDC;
117     static ReleasePbufferDCProc fReleasePbufferDC;
118     static DestroyPbufferProc fDestroyPbuffer;
119 };
120 
121 enum SkWGLContextRequest {
122     /** Requests to create core profile context if possible, otherwise
123         compatibility profile. */
124     kGLPreferCoreProfile_SkWGLContextRequest,
125     /** Requests to create compatibility profile context if possible, otherwise
126         core profile. */
127     kGLPreferCompatibilityProfile_SkWGLContextRequest,
128     /** Requests to create GL ES profile context. */
129     kGLES_SkWGLContextRequest
130 };
131 /**
132  * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
133  * msaaSampleCount are preferred but if none is available then a context with a lower sample count
134  * (including non-MSAA) will be created. If msaaSampleCount is 1 then this will fail if a non-msaa
135  * context cannot be created. If preferCoreProfile is true but a core profile cannot be created
136  * then a compatible profile context will be created.
137  */
138 HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context,
139                          HGLRC shareContext = nullptr);
140 
141 /**
142  * Helper class for creating a pbuffer context and deleting all the handles when finished. This
143  * requires that a device context has been created. However, the pbuffer gets its own device
144  * context. The original device context can be released once the pbuffer context is created.
145  */
146 class SkWGLPbufferContext : public SkRefCnt {
147 public:
148     static sk_sp<SkWGLPbufferContext> Create(HDC parentDC, SkWGLContextRequest contextType,
149                                              HGLRC shareContext);
150 
151     ~SkWGLPbufferContext() override;
152 
getDC()153     HDC getDC() const { return fDC; }
getGLRC()154     HGLRC getGLRC() const { return fGLRC; }
155 
156 private:
157     SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
158 
159     HPBUFFER        fPbuffer;
160     HDC             fDC;
161     HGLRC           fGLRC;
162     SkWGLExtensions fExtensions;
163 };
164 
165 #endif
166