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