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 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
Brian Osman | 0c75727 | 2018-12-10 10:27:26 -0500 | [diff] [blame] | 9 | #if defined(SK_BUILD_FOR_WIN) && !defined(_M_ARM64) |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 10 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 11 | #include "SkWGL.h" |
| 12 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 13 | #include "SkOnce.h" |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
| 15 | #include "SkTSearch.h" |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 16 | #include "SkTSort.h" |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 18 | bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | if (nullptr == this->fGetExtensionsString) { |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 20 | return false; |
| 21 | } |
| 22 | if (!strcmp("WGL_ARB_extensions_string", ext)) { |
| 23 | return true; |
| 24 | } |
| 25 | const char* extensionString = this->getExtensionsString(dc); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 26 | size_t extLength = strlen(ext); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 27 | |
| 28 | while (true) { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 29 | size_t n = strcspn(extensionString, " "); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 30 | if (n == extLength && 0 == strncmp(ext, extensionString, n)) { |
| 31 | return true; |
| 32 | } |
| 33 | if (0 == extensionString[n]) { |
| 34 | return false; |
| 35 | } |
| 36 | extensionString += n+1; |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | const char* SkWGLExtensions::getExtensionsString(HDC hdc) const { |
| 43 | return fGetExtensionsString(hdc); |
| 44 | } |
| 45 | |
| 46 | BOOL SkWGLExtensions::choosePixelFormat(HDC hdc, |
| 47 | const int* piAttribIList, |
| 48 | const FLOAT* pfAttribFList, |
| 49 | UINT nMaxFormats, |
| 50 | int* piFormats, |
| 51 | UINT* nNumFormats) const { |
| 52 | return fChoosePixelFormat(hdc, piAttribIList, pfAttribFList, |
| 53 | nMaxFormats, piFormats, nNumFormats); |
| 54 | } |
| 55 | |
| 56 | BOOL SkWGLExtensions::getPixelFormatAttribiv(HDC hdc, |
| 57 | int iPixelFormat, |
| 58 | int iLayerPlane, |
| 59 | UINT nAttributes, |
| 60 | const int *piAttributes, |
| 61 | int *piValues) const { |
| 62 | return fGetPixelFormatAttribiv(hdc, iPixelFormat, iLayerPlane, |
| 63 | nAttributes, piAttributes, piValues); |
| 64 | } |
| 65 | |
| 66 | BOOL SkWGLExtensions::getPixelFormatAttribfv(HDC hdc, |
| 67 | int iPixelFormat, |
| 68 | int iLayerPlane, |
| 69 | UINT nAttributes, |
| 70 | const int *piAttributes, |
| 71 | float *pfValues) const { |
| 72 | return fGetPixelFormatAttribfv(hdc, iPixelFormat, iLayerPlane, |
| 73 | nAttributes, piAttributes, pfValues); |
| 74 | } |
| 75 | HGLRC SkWGLExtensions::createContextAttribs(HDC hDC, |
| 76 | HGLRC hShareContext, |
| 77 | const int *attribList) const { |
| 78 | return fCreateContextAttribs(hDC, hShareContext, attribList); |
| 79 | } |
| 80 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 81 | BOOL SkWGLExtensions::swapInterval(int interval) const { |
| 82 | return fSwapInterval(interval); |
| 83 | } |
| 84 | |
| 85 | HPBUFFER SkWGLExtensions::createPbuffer(HDC hDC, |
| 86 | int iPixelFormat, |
| 87 | int iWidth, |
| 88 | int iHeight, |
| 89 | const int *piAttribList) const { |
| 90 | return fCreatePbuffer(hDC, iPixelFormat, iWidth, iHeight, piAttribList); |
| 91 | } |
| 92 | |
| 93 | HDC SkWGLExtensions::getPbufferDC(HPBUFFER hPbuffer) const { |
| 94 | return fGetPbufferDC(hPbuffer); |
| 95 | } |
| 96 | |
| 97 | int SkWGLExtensions::releasePbufferDC(HPBUFFER hPbuffer, HDC hDC) const { |
| 98 | return fReleasePbufferDC(hPbuffer, hDC); |
| 99 | } |
| 100 | |
| 101 | BOOL SkWGLExtensions::destroyPbuffer(HPBUFFER hPbuffer) const { |
| 102 | return fDestroyPbuffer(hPbuffer); |
| 103 | } |
| 104 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 105 | namespace { |
| 106 | |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 107 | struct PixelFormat { |
| 108 | int fFormat; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 109 | int fSampleCnt; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 110 | int fChoosePixelFormatRank; |
| 111 | }; |
| 112 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 113 | bool pf_less(const PixelFormat& a, const PixelFormat& b) { |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 114 | if (a.fSampleCnt < b.fSampleCnt) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 115 | return true; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 116 | } else if (b.fSampleCnt < a.fSampleCnt) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } else if (a.fChoosePixelFormatRank < b.fChoosePixelFormatRank) { |
| 119 | return true; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 120 | } |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 121 | return false; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | int SkWGLExtensions::selectFormat(const int formats[], |
| 126 | int formatCount, |
| 127 | HDC dc, |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 128 | int desiredSampleCount) const { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 129 | SkASSERT(desiredSampleCount >= 1); |
aleksandar.stojiljkovic | 2944fbb | 2015-11-05 07:48:12 -0800 | [diff] [blame] | 130 | if (formatCount <= 0) { |
| 131 | return -1; |
| 132 | } |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 133 | PixelFormat desiredFormat = { |
| 134 | 0, |
| 135 | desiredSampleCount, |
| 136 | 0, |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 137 | }; |
| 138 | SkTDArray<PixelFormat> rankedFormats; |
| 139 | rankedFormats.setCount(formatCount); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 140 | for (int i = 0; i < formatCount; ++i) { |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 141 | static const int kQueryAttr = SK_WGL_SAMPLES; |
| 142 | int numSamples; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 143 | this->getPixelFormatAttribiv(dc, |
| 144 | formats[i], |
| 145 | 0, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 146 | 1, |
| 147 | &kQueryAttr, |
| 148 | &numSamples); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 149 | rankedFormats[i].fFormat = formats[i]; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 150 | rankedFormats[i].fSampleCnt = SkTMax(1, numSamples); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 151 | rankedFormats[i].fChoosePixelFormatRank = i; |
| 152 | } |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 153 | SkTQSort(rankedFormats.begin(), |
| 154 | rankedFormats.begin() + rankedFormats.count() - 1, |
| 155 | SkTLessFunctionToFunctorAdaptor<PixelFormat, pf_less>()); |
| 156 | int idx = SkTSearch<PixelFormat, pf_less>(rankedFormats.begin(), |
| 157 | rankedFormats.count(), |
| 158 | desiredFormat, |
| 159 | sizeof(PixelFormat)); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 160 | if (idx < 0) { |
| 161 | idx = ~idx; |
| 162 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 163 | // If the caller asked for non-MSAA fail if the closest format has MSAA. |
| 164 | if (desiredSampleCount == 1 && rankedFormats[idx].fSampleCnt != 1) { |
| 165 | return -1; |
| 166 | } |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 167 | return rankedFormats[idx].fFormat; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | namespace { |
| 172 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 173 | #if defined(UNICODE) |
| 174 | #define STR_LIT(X) L## #X |
| 175 | #else |
| 176 | #define STR_LIT(X) #X |
| 177 | #endif |
| 178 | |
| 179 | #define DUMMY_CLASS STR_LIT("DummyClass") |
| 180 | |
| 181 | HWND create_dummy_window() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 182 | HMODULE module = GetModuleHandle(nullptr); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 183 | HWND dummy; |
| 184 | RECT windowRect; |
| 185 | windowRect.left = 0; |
| 186 | windowRect.right = 8; |
| 187 | windowRect.top = 0; |
| 188 | windowRect.bottom = 8; |
| 189 | |
| 190 | WNDCLASS wc; |
| 191 | |
| 192 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
| 193 | wc.lpfnWndProc = (WNDPROC) DefWindowProc; |
| 194 | wc.cbClsExtra = 0; |
| 195 | wc.cbWndExtra = 0; |
| 196 | wc.hInstance = module; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO); |
| 198 | wc.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 199 | wc.hbrBackground = nullptr; |
| 200 | wc.lpszMenuName = nullptr; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 201 | wc.lpszClassName = DUMMY_CLASS; |
| 202 | |
| 203 | if(!RegisterClass(&wc)) { |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | DWORD style, exStyle; |
| 208 | exStyle = WS_EX_CLIENTEDGE; |
| 209 | style = WS_SYSMENU; |
| 210 | |
| 211 | AdjustWindowRectEx(&windowRect, style, false, exStyle); |
| 212 | if(!(dummy = CreateWindowEx(exStyle, |
| 213 | DUMMY_CLASS, |
| 214 | STR_LIT("DummyWindow"), |
| 215 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style, |
| 216 | 0, 0, |
| 217 | windowRect.right-windowRect.left, |
| 218 | windowRect.bottom-windowRect.top, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | nullptr, nullptr, |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 220 | module, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 221 | nullptr))) { |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 222 | UnregisterClass(DUMMY_CLASS, module); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 223 | return nullptr; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 224 | } |
| 225 | ShowWindow(dummy, SW_HIDE); |
| 226 | |
| 227 | return dummy; |
| 228 | } |
| 229 | |
| 230 | void destroy_dummy_window(HWND dummy) { |
| 231 | DestroyWindow(dummy); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | HMODULE module = GetModuleHandle(nullptr); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 233 | UnregisterClass(DUMMY_CLASS, module); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | #define GET_PROC(NAME, SUFFIX) f##NAME = \ |
Mike Klein | c722f79 | 2017-07-31 11:57:21 -0400 | [diff] [blame] | 238 | (NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX) |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 239 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 240 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 241 | SkWGLExtensions::GetExtensionsStringProc SkWGLExtensions::fGetExtensionsString = nullptr; |
| 242 | SkWGLExtensions::ChoosePixelFormatProc SkWGLExtensions::fChoosePixelFormat = nullptr; |
| 243 | SkWGLExtensions::GetPixelFormatAttribfvProc SkWGLExtensions::fGetPixelFormatAttribfv = nullptr; |
| 244 | SkWGLExtensions::GetPixelFormatAttribivProc SkWGLExtensions::fGetPixelFormatAttribiv = nullptr; |
| 245 | SkWGLExtensions::CreateContextAttribsProc SkWGLExtensions::fCreateContextAttribs = nullptr; |
| 246 | SkWGLExtensions::SwapIntervalProc SkWGLExtensions::fSwapInterval = nullptr; |
| 247 | SkWGLExtensions::CreatePbufferProc SkWGLExtensions::fCreatePbuffer = nullptr; |
| 248 | SkWGLExtensions::GetPbufferDCProc SkWGLExtensions::fGetPbufferDC = nullptr; |
| 249 | SkWGLExtensions::ReleasePbufferDCProc SkWGLExtensions::fReleasePbufferDC = nullptr; |
| 250 | SkWGLExtensions::DestroyPbufferProc SkWGLExtensions::fDestroyPbuffer = nullptr; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 251 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 252 | SkWGLExtensions::SkWGLExtensions() { |
| 253 | // We cache these function pointers once, and then reuse them. That's possibly incorrect if |
| 254 | // there are multiple GPUs, or if we intend to use these for rendering contexts of different |
| 255 | // pixel formats (where wglGetProcAddress is not guaranteed to return the same pointer). |
| 256 | static SkOnce once; |
| 257 | once([] { |
| 258 | HDC prevDC = wglGetCurrentDC(); |
| 259 | HGLRC prevGLRC = wglGetCurrentContext(); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 260 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 261 | PIXELFORMATDESCRIPTOR dummyPFD; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 262 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 263 | ZeroMemory(&dummyPFD, sizeof(dummyPFD)); |
| 264 | dummyPFD.nSize = sizeof(dummyPFD); |
| 265 | dummyPFD.nVersion = 1; |
| 266 | dummyPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; |
| 267 | dummyPFD.iPixelType = PFD_TYPE_RGBA; |
| 268 | dummyPFD.cColorBits = 32; |
| 269 | dummyPFD.cDepthBits = 0; |
| 270 | dummyPFD.cStencilBits = 8; |
| 271 | dummyPFD.iLayerType = PFD_MAIN_PLANE; |
| 272 | HWND dummyWND = create_dummy_window(); |
| 273 | if (dummyWND) { |
| 274 | HDC dummyDC = GetDC(dummyWND); |
| 275 | int dummyFormat = ChoosePixelFormat(dummyDC, &dummyPFD); |
| 276 | SetPixelFormat(dummyDC, dummyFormat, &dummyPFD); |
| 277 | HGLRC dummyGLRC = wglCreateContext(dummyDC); |
| 278 | SkASSERT(dummyGLRC); |
| 279 | wglMakeCurrent(dummyDC, dummyGLRC); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 280 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 281 | GET_PROC(GetExtensionsString, ARB); |
| 282 | GET_PROC(ChoosePixelFormat, ARB); |
| 283 | GET_PROC(GetPixelFormatAttribiv, ARB); |
| 284 | GET_PROC(GetPixelFormatAttribfv, ARB); |
| 285 | GET_PROC(CreateContextAttribs, ARB); |
| 286 | GET_PROC(SwapInterval, EXT); |
| 287 | GET_PROC(CreatePbuffer, ARB); |
| 288 | GET_PROC(GetPbufferDC, ARB); |
| 289 | GET_PROC(ReleasePbufferDC, ARB); |
| 290 | GET_PROC(DestroyPbuffer, ARB); |
| 291 | |
| 292 | wglMakeCurrent(dummyDC, nullptr); |
| 293 | wglDeleteContext(dummyGLRC); |
| 294 | destroy_dummy_window(dummyWND); |
| 295 | } |
| 296 | |
| 297 | wglMakeCurrent(prevDC, prevGLRC); |
| 298 | }); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 299 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 300 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 301 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 302 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 303 | static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions, |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 304 | bool doubleBuffered, int msaaSampleCount, bool deepColor, |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 305 | int formatsToTry[2]) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 306 | auto appendAttr = [](SkTDArray<int>& attrs, int attr, int value) { |
Mike Reed | b547579 | 2018-08-08 16:17:42 -0400 | [diff] [blame] | 307 | attrs.push_back(attr); |
| 308 | attrs.push_back(value); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 311 | SkTDArray<int> iAttrs; |
| 312 | appendAttr(iAttrs, SK_WGL_DRAW_TO_WINDOW, TRUE); |
| 313 | appendAttr(iAttrs, SK_WGL_DOUBLE_BUFFER, (doubleBuffered ? TRUE : FALSE)); |
| 314 | appendAttr(iAttrs, SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION); |
| 315 | appendAttr(iAttrs, SK_WGL_SUPPORT_OPENGL, TRUE); |
| 316 | if (deepColor) { |
| 317 | appendAttr(iAttrs, SK_WGL_RED_BITS, 10); |
| 318 | appendAttr(iAttrs, SK_WGL_GREEN_BITS, 10); |
| 319 | appendAttr(iAttrs, SK_WGL_BLUE_BITS, 10); |
| 320 | appendAttr(iAttrs, SK_WGL_ALPHA_BITS, 2); |
| 321 | } else { |
| 322 | appendAttr(iAttrs, SK_WGL_COLOR_BITS, 24); |
| 323 | appendAttr(iAttrs, SK_WGL_ALPHA_BITS, 8); |
| 324 | } |
| 325 | appendAttr(iAttrs, SK_WGL_STENCIL_BITS, 8); |
| 326 | |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 327 | float fAttrs[] = {0, 0}; |
| 328 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 329 | // Get a MSAA format if requested and possible. |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 330 | if (msaaSampleCount > 0 && |
| 331 | extensions.hasExtension(dc, "WGL_ARB_multisample")) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 332 | SkTDArray<int> msaaIAttrs = iAttrs; |
| 333 | appendAttr(msaaIAttrs, SK_WGL_SAMPLE_BUFFERS, TRUE); |
| 334 | appendAttr(msaaIAttrs, SK_WGL_SAMPLES, msaaSampleCount); |
| 335 | appendAttr(msaaIAttrs, 0, 0); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 336 | unsigned int num; |
| 337 | int formats[64]; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 338 | extensions.choosePixelFormat(dc, msaaIAttrs.begin(), fAttrs, 64, formats, &num); |
bsalomon@google.com | f3f2d16 | 2013-07-31 18:52:31 +0000 | [diff] [blame] | 339 | num = SkTMin(num, 64U); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 340 | formatsToTry[0] = extensions.selectFormat(formats, num, dc, msaaSampleCount); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 341 | } |
| 342 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 343 | // Get a non-MSAA format |
| 344 | int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1]; |
| 345 | unsigned int num; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 346 | appendAttr(iAttrs, 0, 0); |
| 347 | extensions.choosePixelFormat(dc, iAttrs.begin(), fAttrs, 1, format, &num); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Brian Osman | 6269f71 | 2017-08-16 15:14:59 -0400 | [diff] [blame] | 350 | static HGLRC create_gl_context(HDC dc, const SkWGLExtensions& extensions, |
| 351 | SkWGLContextRequest contextType, HGLRC shareContext) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 352 | HDC prevDC = wglGetCurrentDC(); |
| 353 | HGLRC prevGLRC = wglGetCurrentContext(); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 354 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 355 | HGLRC glrc = nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 356 | if (kGLES_SkWGLContextRequest == contextType) { |
| 357 | if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile")) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 358 | wglMakeCurrent(prevDC, prevGLRC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 359 | return nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 360 | } |
| 361 | static const int glesAttribs[] = { |
| 362 | SK_WGL_CONTEXT_MAJOR_VERSION, 3, |
| 363 | SK_WGL_CONTEXT_MINOR_VERSION, 0, |
| 364 | SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_ES2_PROFILE_BIT, |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 365 | 0, |
| 366 | }; |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 367 | glrc = extensions.createContextAttribs(dc, shareContext, glesAttribs); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 368 | if (nullptr == glrc) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 369 | wglMakeCurrent(prevDC, prevGLRC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 370 | return nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 371 | } |
| 372 | } else { |
| 373 | if (kGLPreferCoreProfile_SkWGLContextRequest == contextType && |
| 374 | extensions.hasExtension(dc, "WGL_ARB_create_context")) { |
| 375 | static const int kCoreGLVersions[] = { |
| 376 | 4, 3, |
| 377 | 4, 2, |
| 378 | 4, 1, |
| 379 | 4, 0, |
| 380 | 3, 3, |
| 381 | 3, 2, |
| 382 | }; |
| 383 | int coreProfileAttribs[] = { |
| 384 | SK_WGL_CONTEXT_MAJOR_VERSION, -1, |
| 385 | SK_WGL_CONTEXT_MINOR_VERSION, -1, |
| 386 | SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT, |
| 387 | 0, |
| 388 | }; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 389 | for (size_t v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) { |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 390 | coreProfileAttribs[1] = kCoreGLVersions[2 * v]; |
| 391 | coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1]; |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 392 | glrc = extensions.createContextAttribs(dc, shareContext, coreProfileAttribs); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 393 | if (glrc) { |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 394 | break; |
| 395 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 400 | if (nullptr == glrc) { |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 401 | glrc = wglCreateContext(dc); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 402 | if (shareContext) { |
| 403 | if (!wglShareLists(shareContext, glrc)) { |
| 404 | wglDeleteContext(glrc); |
| 405 | return nullptr; |
| 406 | } |
| 407 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 408 | } |
| 409 | SkASSERT(glrc); |
| 410 | |
| 411 | wglMakeCurrent(prevDC, prevGLRC); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 412 | |
| 413 | // This might help make the context non-vsynced. |
| 414 | if (extensions.hasExtension(dc, "WGL_EXT_swap_control")) { |
| 415 | extensions.swapInterval(-1); |
| 416 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 417 | return glrc; |
| 418 | } |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 419 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 420 | HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 421 | SkWGLContextRequest contextType, HGLRC shareContext) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 422 | SkWGLExtensions extensions; |
| 423 | if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 424 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | BOOL set = FALSE; |
| 428 | |
| 429 | int pixelFormatsToTry[] = { -1, -1 }; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 430 | get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, deepColor, pixelFormatsToTry); |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 431 | for (size_t f = 0; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 432 | !set && -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsToTry); |
| 433 | ++f) { |
| 434 | PIXELFORMATDESCRIPTOR pfd; |
| 435 | DescribePixelFormat(dc, pixelFormatsToTry[f], sizeof(pfd), &pfd); |
| 436 | set = SetPixelFormat(dc, pixelFormatsToTry[f], &pfd); |
| 437 | } |
| 438 | |
| 439 | if (!set) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 443 | return create_gl_context(dc, extensions, contextType, shareContext); |
| 444 | } |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 445 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 446 | sk_sp<SkWGLPbufferContext> SkWGLPbufferContext::Create(HDC parentDC, |
| 447 | SkWGLContextRequest contextType, |
| 448 | HGLRC shareContext) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 449 | SkWGLExtensions extensions; |
| 450 | if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") || |
| 451 | !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 452 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Brian Osman | de3b710 | 2017-08-17 12:58:28 -0400 | [diff] [blame] | 455 | // We cache the pixel formats once, and then reuse them. That's possibly incorrect if |
| 456 | // there are multiple GPUs, but this function is always called with a freshly made, |
| 457 | // identically constructed HDC (see WinGLTestContext). |
| 458 | // |
| 459 | // We only store two potential pixel formats, one for single buffer, one for double buffer. |
| 460 | // We never ask for MSAA, so we don't need the second pixel format for each buffering state. |
| 461 | static int gPixelFormats[2] = { -1, -1 }; |
| 462 | static SkOnce once; |
| 463 | once([=] { |
| 464 | { |
| 465 | // Single buffer |
| 466 | int pixelFormatsToTry[2] = { -1, -1 }; |
| 467 | get_pixel_formats_to_try(parentDC, extensions, false, 0, false, pixelFormatsToTry); |
| 468 | gPixelFormats[0] = pixelFormatsToTry[0]; |
| 469 | } |
| 470 | { |
| 471 | // Double buffer |
| 472 | int pixelFormatsToTry[2] = { -1, -1 }; |
| 473 | get_pixel_formats_to_try(parentDC, extensions, true, 0, false, pixelFormatsToTry); |
| 474 | gPixelFormats[1] = pixelFormatsToTry[0]; |
| 475 | } |
| 476 | }); |
| 477 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 478 | // try for single buffer first |
Brian Osman | de3b710 | 2017-08-17 12:58:28 -0400 | [diff] [blame] | 479 | for (int pixelFormat : gPixelFormats) { |
| 480 | if (-1 == pixelFormat) { |
| 481 | continue; |
| 482 | } |
| 483 | HPBUFFER pbuf = extensions.createPbuffer(parentDC, pixelFormat, 1, 1, nullptr); |
| 484 | if (0 != pbuf) { |
| 485 | HDC dc = extensions.getPbufferDC(pbuf); |
| 486 | if (dc) { |
| 487 | HGLRC glrc = create_gl_context(dc, extensions, contextType, shareContext); |
| 488 | if (glrc) { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 489 | return sk_sp<SkWGLPbufferContext>(new SkWGLPbufferContext(pbuf, dc, glrc)); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 490 | } |
Brian Osman | de3b710 | 2017-08-17 12:58:28 -0400 | [diff] [blame] | 491 | extensions.releasePbufferDC(pbuf, dc); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 492 | } |
Brian Osman | de3b710 | 2017-08-17 12:58:28 -0400 | [diff] [blame] | 493 | extensions.destroyPbuffer(pbuf); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 496 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | SkWGLPbufferContext::~SkWGLPbufferContext() { |
| 500 | SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer")); |
| 501 | wglDeleteContext(fGLRC); |
| 502 | fExtensions.releasePbufferDC(fPbuffer, fDC); |
| 503 | fExtensions.destroyPbuffer(fPbuffer); |
| 504 | } |
| 505 | |
| 506 | SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc) |
| 507 | : fPbuffer(pbuffer) |
| 508 | , fDC(dc) |
| 509 | , fGLRC(glrc) { |
| 510 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 511 | |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 512 | #endif//defined(SK_BUILD_FOR_WIN) |