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