| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 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 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkRefCnt.h" | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 9 |  | 
 | 10 | #ifndef SkWGL_DEFINED | 
 | 11 | #define SkWGL_DEFINED | 
 | 12 |  | 
| Ben Wagner | ab6eefe | 2019-05-20 11:02:49 -0400 | [diff] [blame] | 13 | #include "src/core/SkLeanWindows.h" | 
| halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 14 |  | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 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 dummy GL ctx to | 
| rmistry | c3cf5a5 | 2014-08-26 10:43:14 -0700 | [diff] [blame] | 19 |  * get the proc addresses. | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 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 |  | 
| bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 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 | 
| brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 32 | #define SK_WGL_RED_BITS                             0x2015 | 
 | 33 | #define SK_WGL_GREEN_BITS                           0x2017 | 
 | 34 | #define SK_WGL_BLUE_BITS                            0x2019 | 
| bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 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 | 
| bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 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 | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 52 |  | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 53 | DECLARE_HANDLE(HPBUFFER); | 
 | 54 |  | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 55 | class SkWGLExtensions { | 
 | 56 | public: | 
 | 57 |     SkWGLExtensions(); | 
 | 58 |     /** | 
 | 59 |      * Determines if an extensions is available for a given DC. | 
| bsalomon@google.com | eedef25 | 2011-12-01 16:50:24 +0000 | [diff] [blame] | 60 |      * WGL_extensions_string is considered a prerequisite for all other | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 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 |  | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 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 |  | 
| bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 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 | 
| Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 86 |      *       fewer samples than desiredSampleCount.) If desiredSampleCount is 1 then | 
 | 87 |      *       all msaa formats are excluded from consideration. | 
| bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 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, | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 96 |                      int desiredSampleCount) const; | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 97 | private: | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 98 |     typedef const char* (WINAPI *GetExtensionsStringProc)(HDC); | 
 | 99 |     typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *); | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 100 |     typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*); | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 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); | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 108 |  | 
| Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 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; | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 119 | }; | 
 | 120 |  | 
| kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 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 | }; | 
| bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 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 | 
| Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 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. | 
| bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 137 |  */ | 
| Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 138 | HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context, | 
 | 139 |                          HGLRC shareContext = nullptr); | 
| bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 140 |  | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 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: | 
| Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 148 |     static sk_sp<SkWGLPbufferContext> Create(HDC parentDC, SkWGLContextRequest contextType, | 
 | 149 |                                              HGLRC shareContext); | 
| bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 150 |  | 
 | 151 |     virtual ~SkWGLPbufferContext(); | 
 | 152 |  | 
 | 153 |     HDC getDC() const { return fDC; } | 
 | 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 |  | 
| bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 165 | #endif |