blob: 1a42c8afa47a1785e378da2fe7554e7c1cbfad3b [file] [log] [blame]
bsalomon@google.combd7c6412011-12-01 16:34:28 +00001/*
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
mtklein1ee76512015-11-02 10:20:27 -08008#include "SkTypes.h"
Brian Osman0c757272018-12-10 10:27:26 -05009#if defined(SK_BUILD_FOR_WIN) && !defined(_M_ARM64)
mtklein1ee76512015-11-02 10:20:27 -080010
bsalomon@google.combd7c6412011-12-01 16:34:28 +000011#include "SkWGL.h"
12
Brian Osman6269f712017-08-16 15:14:59 -040013#include "SkOnce.h"
bsalomon@google.com8a189b02012-04-17 12:43:00 +000014#include "SkTDArray.h"
15#include "SkTSearch.h"
bsalomon@google.com20f7f172013-05-17 19:05:03 +000016#include "SkTSort.h"
bsalomon@google.com8a189b02012-04-17 12:43:00 +000017
bsalomon@google.combd7c6412011-12-01 16:34:28 +000018bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const {
halcanary96fcdcc2015-08-27 07:41:13 -070019 if (nullptr == this->fGetExtensionsString) {
bsalomon@google.combd7c6412011-12-01 16:34:28 +000020 return false;
21 }
22 if (!strcmp("WGL_ARB_extensions_string", ext)) {
23 return true;
24 }
25 const char* extensionString = this->getExtensionsString(dc);
robertphillips@google.comadacc702013-10-14 21:53:24 +000026 size_t extLength = strlen(ext);
bsalomon@google.combd7c6412011-12-01 16:34:28 +000027
28 while (true) {
robertphillips@google.comadacc702013-10-14 21:53:24 +000029 size_t n = strcspn(extensionString, " ");
bsalomon@google.combd7c6412011-12-01 16:34:28 +000030 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
42const char* SkWGLExtensions::getExtensionsString(HDC hdc) const {
43 return fGetExtensionsString(hdc);
44}
45
46BOOL 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
56BOOL 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
66BOOL 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}
75HGLRC SkWGLExtensions::createContextAttribs(HDC hDC,
76 HGLRC hShareContext,
77 const int *attribList) const {
78 return fCreateContextAttribs(hDC, hShareContext, attribList);
79}
80
bsalomon9245b7e2014-07-01 07:20:11 -070081BOOL SkWGLExtensions::swapInterval(int interval) const {
82 return fSwapInterval(interval);
83}
84
85HPBUFFER 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
93HDC SkWGLExtensions::getPbufferDC(HPBUFFER hPbuffer) const {
94 return fGetPbufferDC(hPbuffer);
95}
96
97int SkWGLExtensions::releasePbufferDC(HPBUFFER hPbuffer, HDC hDC) const {
98 return fReleasePbufferDC(hPbuffer, hDC);
99}
100
101BOOL SkWGLExtensions::destroyPbuffer(HPBUFFER hPbuffer) const {
102 return fDestroyPbuffer(hPbuffer);
103}
104
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000105namespace {
106
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000107struct PixelFormat {
108 int fFormat;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000109 int fSampleCnt;
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000110 int fChoosePixelFormatRank;
111};
112
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000113bool pf_less(const PixelFormat& a, const PixelFormat& b) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000114 if (a.fSampleCnt < b.fSampleCnt) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000115 return true;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000116 } else if (b.fSampleCnt < a.fSampleCnt) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000117 return false;
118 } else if (a.fChoosePixelFormatRank < b.fChoosePixelFormatRank) {
119 return true;
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000120 }
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000121 return false;
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000122}
123}
124
125int SkWGLExtensions::selectFormat(const int formats[],
126 int formatCount,
127 HDC dc,
bsalomon9245b7e2014-07-01 07:20:11 -0700128 int desiredSampleCount) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 SkASSERT(desiredSampleCount >= 1);
aleksandar.stojiljkovic2944fbb2015-11-05 07:48:12 -0800130 if (formatCount <= 0) {
131 return -1;
132 }
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000133 PixelFormat desiredFormat = {
134 0,
135 desiredSampleCount,
136 0,
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000137 };
138 SkTDArray<PixelFormat> rankedFormats;
139 rankedFormats.setCount(formatCount);
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000140 for (int i = 0; i < formatCount; ++i) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000141 static const int kQueryAttr = SK_WGL_SAMPLES;
142 int numSamples;
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000143 this->getPixelFormatAttribiv(dc,
144 formats[i],
145 0,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000146 1,
147 &kQueryAttr,
148 &numSamples);
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000149 rankedFormats[i].fFormat = formats[i];
Brian Salomonbdecacf2018-02-02 20:32:49 -0500150 rankedFormats[i].fSampleCnt = SkTMax(1, numSamples);
bsalomon@google.com8a189b02012-04-17 12:43:00 +0000151 rankedFormats[i].fChoosePixelFormatRank = i;
152 }
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000153 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.com8a189b02012-04-17 12:43:00 +0000160 if (idx < 0) {
161 idx = ~idx;
162 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500163 // 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.com8a189b02012-04-17 12:43:00 +0000167 return rankedFormats[idx].fFormat;
168}
169
170
171namespace {
172
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000173#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
181HWND create_dummy_window() {
halcanary96fcdcc2015-08-27 07:41:13 -0700182 HMODULE module = GetModuleHandle(nullptr);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000183 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;
halcanary96fcdcc2015-08-27 07:41:13 -0700197 wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
198 wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
199 wc.hbrBackground = nullptr;
200 wc.lpszMenuName = nullptr;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000201 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,
halcanary96fcdcc2015-08-27 07:41:13 -0700219 nullptr, nullptr,
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000220 module,
halcanary96fcdcc2015-08-27 07:41:13 -0700221 nullptr))) {
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000222 UnregisterClass(DUMMY_CLASS, module);
halcanary96fcdcc2015-08-27 07:41:13 -0700223 return nullptr;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000224 }
225 ShowWindow(dummy, SW_HIDE);
226
227 return dummy;
228}
229
230void destroy_dummy_window(HWND dummy) {
231 DestroyWindow(dummy);
halcanary96fcdcc2015-08-27 07:41:13 -0700232 HMODULE module = GetModuleHandle(nullptr);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000233 UnregisterClass(DUMMY_CLASS, module);
234}
235}
236
237#define GET_PROC(NAME, SUFFIX) f##NAME = \
Mike Kleinc722f792017-07-31 11:57:21 -0400238 (NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX)
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000239
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000240
Brian Osman6269f712017-08-16 15:14:59 -0400241SkWGLExtensions::GetExtensionsStringProc SkWGLExtensions::fGetExtensionsString = nullptr;
242SkWGLExtensions::ChoosePixelFormatProc SkWGLExtensions::fChoosePixelFormat = nullptr;
243SkWGLExtensions::GetPixelFormatAttribfvProc SkWGLExtensions::fGetPixelFormatAttribfv = nullptr;
244SkWGLExtensions::GetPixelFormatAttribivProc SkWGLExtensions::fGetPixelFormatAttribiv = nullptr;
245SkWGLExtensions::CreateContextAttribsProc SkWGLExtensions::fCreateContextAttribs = nullptr;
246SkWGLExtensions::SwapIntervalProc SkWGLExtensions::fSwapInterval = nullptr;
247SkWGLExtensions::CreatePbufferProc SkWGLExtensions::fCreatePbuffer = nullptr;
248SkWGLExtensions::GetPbufferDCProc SkWGLExtensions::fGetPbufferDC = nullptr;
249SkWGLExtensions::ReleasePbufferDCProc SkWGLExtensions::fReleasePbufferDC = nullptr;
250SkWGLExtensions::DestroyPbufferProc SkWGLExtensions::fDestroyPbuffer = nullptr;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000251
Brian Osman6269f712017-08-16 15:14:59 -0400252SkWGLExtensions::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.combd7c6412011-12-01 16:34:28 +0000260
Brian Osman6269f712017-08-16 15:14:59 -0400261 PIXELFORMATDESCRIPTOR dummyPFD;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000262
Brian Osman6269f712017-08-16 15:14:59 -0400263 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.combd7c6412011-12-01 16:34:28 +0000280
Brian Osman6269f712017-08-16 15:14:59 -0400281 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.combd7c6412011-12-01 16:34:28 +0000299}
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000300
bsalomon9245b7e2014-07-01 07:20:11 -0700301///////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000302
bsalomon9245b7e2014-07-01 07:20:11 -0700303static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions,
brianosman2d1ee792016-05-05 12:24:31 -0700304 bool doubleBuffered, int msaaSampleCount, bool deepColor,
bsalomon9245b7e2014-07-01 07:20:11 -0700305 int formatsToTry[2]) {
brianosman2d1ee792016-05-05 12:24:31 -0700306 auto appendAttr = [](SkTDArray<int>& attrs, int attr, int value) {
Mike Reedb5475792018-08-08 16:17:42 -0400307 attrs.push_back(attr);
308 attrs.push_back(value);
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000309 };
310
brianosman2d1ee792016-05-05 12:24:31 -0700311 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.comb7f20f22013-03-05 19:13:09 +0000327 float fAttrs[] = {0, 0};
328
bsalomon9245b7e2014-07-01 07:20:11 -0700329 // Get a MSAA format if requested and possible.
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000330 if (msaaSampleCount > 0 &&
331 extensions.hasExtension(dc, "WGL_ARB_multisample")) {
brianosman2d1ee792016-05-05 12:24:31 -0700332 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.comb7f20f22013-03-05 19:13:09 +0000336 unsigned int num;
337 int formats[64];
brianosman2d1ee792016-05-05 12:24:31 -0700338 extensions.choosePixelFormat(dc, msaaIAttrs.begin(), fAttrs, 64, formats, &num);
bsalomon@google.comf3f2d162013-07-31 18:52:31 +0000339 num = SkTMin(num, 64U);
bsalomon9245b7e2014-07-01 07:20:11 -0700340 formatsToTry[0] = extensions.selectFormat(formats, num, dc, msaaSampleCount);
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000341 }
342
bsalomon9245b7e2014-07-01 07:20:11 -0700343 // Get a non-MSAA format
344 int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1];
345 unsigned int num;
brianosman2d1ee792016-05-05 12:24:31 -0700346 appendAttr(iAttrs, 0, 0);
347 extensions.choosePixelFormat(dc, iAttrs.begin(), fAttrs, 1, format, &num);
bsalomon9245b7e2014-07-01 07:20:11 -0700348}
349
Brian Osman6269f712017-08-16 15:14:59 -0400350static HGLRC create_gl_context(HDC dc, const SkWGLExtensions& extensions,
351 SkWGLContextRequest contextType, HGLRC shareContext) {
bsalomon9245b7e2014-07-01 07:20:11 -0700352 HDC prevDC = wglGetCurrentDC();
353 HGLRC prevGLRC = wglGetCurrentContext();
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000354
halcanary96fcdcc2015-08-27 07:41:13 -0700355 HGLRC glrc = nullptr;
kkinnunen80549fc2014-06-30 06:36:31 -0700356 if (kGLES_SkWGLContextRequest == contextType) {
357 if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile")) {
bsalomon9245b7e2014-07-01 07:20:11 -0700358 wglMakeCurrent(prevDC, prevGLRC);
halcanary96fcdcc2015-08-27 07:41:13 -0700359 return nullptr;
kkinnunen80549fc2014-06-30 06:36:31 -0700360 }
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.comb7f20f22013-03-05 19:13:09 +0000365 0,
366 };
Brian Osman60c774d2017-02-21 16:58:08 -0500367 glrc = extensions.createContextAttribs(dc, shareContext, glesAttribs);
halcanary96fcdcc2015-08-27 07:41:13 -0700368 if (nullptr == glrc) {
bsalomon9245b7e2014-07-01 07:20:11 -0700369 wglMakeCurrent(prevDC, prevGLRC);
halcanary96fcdcc2015-08-27 07:41:13 -0700370 return nullptr;
kkinnunen80549fc2014-06-30 06:36:31 -0700371 }
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 Dalton1ef80942017-12-04 12:01:30 -0700389 for (size_t v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) {
kkinnunen80549fc2014-06-30 06:36:31 -0700390 coreProfileAttribs[1] = kCoreGLVersions[2 * v];
391 coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1];
Brian Osman60c774d2017-02-21 16:58:08 -0500392 glrc = extensions.createContextAttribs(dc, shareContext, coreProfileAttribs);
bsalomon49f085d2014-09-05 13:34:00 -0700393 if (glrc) {
kkinnunen80549fc2014-06-30 06:36:31 -0700394 break;
395 }
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000396 }
397 }
398 }
399
halcanary96fcdcc2015-08-27 07:41:13 -0700400 if (nullptr == glrc) {
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000401 glrc = wglCreateContext(dc);
Brian Osman60c774d2017-02-21 16:58:08 -0500402 if (shareContext) {
403 if (!wglShareLists(shareContext, glrc)) {
404 wglDeleteContext(glrc);
405 return nullptr;
406 }
407 }
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000408 }
409 SkASSERT(glrc);
410
411 wglMakeCurrent(prevDC, prevGLRC);
bsalomon9245b7e2014-07-01 07:20:11 -0700412
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.comb7f20f22013-03-05 19:13:09 +0000417 return glrc;
418}
bsalomon9245b7e2014-07-01 07:20:11 -0700419
brianosman2d1ee792016-05-05 12:24:31 -0700420HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor,
Brian Osman60c774d2017-02-21 16:58:08 -0500421 SkWGLContextRequest contextType, HGLRC shareContext) {
bsalomon9245b7e2014-07-01 07:20:11 -0700422 SkWGLExtensions extensions;
423 if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700424 return nullptr;
bsalomon9245b7e2014-07-01 07:20:11 -0700425 }
426
427 BOOL set = FALSE;
428
429 int pixelFormatsToTry[] = { -1, -1 };
brianosman2d1ee792016-05-05 12:24:31 -0700430 get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, deepColor, pixelFormatsToTry);
Chris Dalton1ef80942017-12-04 12:01:30 -0700431 for (size_t f = 0;
bsalomon9245b7e2014-07-01 07:20:11 -0700432 !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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700440 return nullptr;
bsalomon9245b7e2014-07-01 07:20:11 -0700441 }
442
Brian Osman60c774d2017-02-21 16:58:08 -0500443 return create_gl_context(dc, extensions, contextType, shareContext);
444}
bsalomon9245b7e2014-07-01 07:20:11 -0700445
Ben Wagner9ec70c62018-07-12 13:30:47 -0400446sk_sp<SkWGLPbufferContext> SkWGLPbufferContext::Create(HDC parentDC,
447 SkWGLContextRequest contextType,
448 HGLRC shareContext) {
bsalomon9245b7e2014-07-01 07:20:11 -0700449 SkWGLExtensions extensions;
450 if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") ||
451 !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700452 return nullptr;
bsalomon9245b7e2014-07-01 07:20:11 -0700453 }
454
Brian Osmande3b7102017-08-17 12:58:28 -0400455 // 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
bsalomon9245b7e2014-07-01 07:20:11 -0700478 // try for single buffer first
Brian Osmande3b7102017-08-17 12:58:28 -0400479 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 Wagner9ec70c62018-07-12 13:30:47 -0400489 return sk_sp<SkWGLPbufferContext>(new SkWGLPbufferContext(pbuf, dc, glrc));
bsalomon9245b7e2014-07-01 07:20:11 -0700490 }
Brian Osmande3b7102017-08-17 12:58:28 -0400491 extensions.releasePbufferDC(pbuf, dc);
bsalomon9245b7e2014-07-01 07:20:11 -0700492 }
Brian Osmande3b7102017-08-17 12:58:28 -0400493 extensions.destroyPbuffer(pbuf);
bsalomon9245b7e2014-07-01 07:20:11 -0700494 }
495 }
halcanary96fcdcc2015-08-27 07:41:13 -0700496 return nullptr;
bsalomon9245b7e2014-07-01 07:20:11 -0700497}
498
499SkWGLPbufferContext::~SkWGLPbufferContext() {
500 SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer"));
501 wglDeleteContext(fGLRC);
502 fExtensions.releasePbufferDC(fPbuffer, fDC);
503 fExtensions.destroyPbuffer(fPbuffer);
504}
505
506SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc)
507 : fPbuffer(pbuffer)
508 , fDC(dc)
509 , fGLRC(glrc) {
510}
mtklein1ee76512015-11-02 10:20:27 -0800511
Mike Klein8f11d4d2018-01-24 12:42:55 -0500512#endif//defined(SK_BUILD_FOR_WIN)