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 | |
| 8 | #include "SkRefCnt.h" |
| 9 | |
| 10 | #ifndef SkWGL_DEFINED |
| 11 | #define SkWGL_DEFINED |
| 12 | |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 13 | #include "SkLeanWindows.h" |
| 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 |
| 86 | * fewer samples than desiredSampleCount.) |
| 87 | * * Choose formats with the fewest color samples when coverage sampling |
| 88 | * is available. |
| 89 | * * If the above rules leave multiple formats, choose the one that |
| 90 | * appears first in the formats array parameter. |
| 91 | */ |
| 92 | int selectFormat(const int formats[], |
| 93 | int formatCount, |
| 94 | HDC dc, |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 95 | int desiredSampleCount) const; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 96 | private: |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 97 | typedef const char* (WINAPI *GetExtensionsStringProc)(HDC); |
| 98 | 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] | 99 | typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 100 | typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*); |
| 101 | typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *); |
| 102 | typedef BOOL (WINAPI* SwapIntervalProc)(int); |
| 103 | typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*); |
| 104 | typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER); |
| 105 | typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC); |
| 106 | typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 107 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame^] | 108 | static GetExtensionsStringProc fGetExtensionsString; |
| 109 | static ChoosePixelFormatProc fChoosePixelFormat; |
| 110 | static GetPixelFormatAttribfvProc fGetPixelFormatAttribfv; |
| 111 | static GetPixelFormatAttribivProc fGetPixelFormatAttribiv; |
| 112 | static CreateContextAttribsProc fCreateContextAttribs; |
| 113 | static SwapIntervalProc fSwapInterval; |
| 114 | static CreatePbufferProc fCreatePbuffer; |
| 115 | static GetPbufferDCProc fGetPbufferDC; |
| 116 | static ReleasePbufferDCProc fReleasePbufferDC; |
| 117 | static DestroyPbufferProc fDestroyPbuffer; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 120 | enum SkWGLContextRequest { |
| 121 | /** Requests to create core profile context if possible, otherwise |
| 122 | compatibility profile. */ |
| 123 | kGLPreferCoreProfile_SkWGLContextRequest, |
| 124 | /** Requests to create compatibility profile context if possible, otherwise |
| 125 | core profile. */ |
| 126 | kGLPreferCompatibilityProfile_SkWGLContextRequest, |
| 127 | /** Requests to create GL ES profile context. */ |
| 128 | kGLES_SkWGLContextRequest |
| 129 | }; |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to |
| 132 | * msaaSampleCount are preferred but if none is available then a context with a lower sample count |
| 133 | * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be |
| 134 | * created then a compatible profile context will be created. |
| 135 | */ |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 136 | HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context, |
| 137 | HGLRC shareContext = nullptr); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 138 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 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 SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount, |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 147 | SkWGLContextRequest contextType, HGLRC shareContext); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 148 | |
| 149 | virtual ~SkWGLPbufferContext(); |
| 150 | |
| 151 | HDC getDC() const { return fDC; } |
| 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 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 163 | #endif |