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) && !defined(WINUWP)
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
hasExtension(HDC dc,const char * ext) const18 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
getExtensionsString(HDC hdc) const42 const char* SkWGLExtensions::getExtensionsString(HDC hdc) const {
43 return fGetExtensionsString(hdc);
44 }
45
choosePixelFormat(HDC hdc,const int * piAttribIList,const FLOAT * pfAttribFList,UINT nMaxFormats,int * piFormats,UINT * nNumFormats) const46 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
getPixelFormatAttribiv(HDC hdc,int iPixelFormat,int iLayerPlane,UINT nAttributes,const int * piAttributes,int * piValues) const56 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
getPixelFormatAttribfv(HDC hdc,int iPixelFormat,int iLayerPlane,UINT nAttributes,const int * piAttributes,float * pfValues) const66 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 }
createContextAttribs(HDC hDC,HGLRC hShareContext,const int * attribList) const75 HGLRC SkWGLExtensions::createContextAttribs(HDC hDC,
76 HGLRC hShareContext,
77 const int *attribList) const {
78 return fCreateContextAttribs(hDC, hShareContext, attribList);
79 }
80
swapInterval(int interval) const81 BOOL SkWGLExtensions::swapInterval(int interval) const {
82 return fSwapInterval(interval);
83 }
84
createPbuffer(HDC hDC,int iPixelFormat,int iWidth,int iHeight,const int * piAttribList) const85 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
getPbufferDC(HPBUFFER hPbuffer) const93 HDC SkWGLExtensions::getPbufferDC(HPBUFFER hPbuffer) const {
94 return fGetPbufferDC(hPbuffer);
95 }
96
releasePbufferDC(HPBUFFER hPbuffer,HDC hDC) const97 int SkWGLExtensions::releasePbufferDC(HPBUFFER hPbuffer, HDC hDC) const {
98 return fReleasePbufferDC(hPbuffer, hDC);
99 }
100
destroyPbuffer(HPBUFFER hPbuffer) const101 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
pf_less(const PixelFormat & a,const PixelFormat & b)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
selectFormat(const int formats[],int formatCount,HDC dc,int desiredSampleCount) const125 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 TEMP_CLASS STR_LIT("TempClass")
178
create_temp_window()179 HWND create_temp_window() {
180 HMODULE module = GetModuleHandle(nullptr);
181 HWND wnd;
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 = TEMP_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(!(wnd = CreateWindowEx(exStyle,
211 TEMP_CLASS,
212 STR_LIT("PlaceholderWindow"),
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(TEMP_CLASS, module);
221 return nullptr;
222 }
223 ShowWindow(wnd, SW_HIDE);
224
225 return wnd;
226 }
227
destroy_temp_window(HWND wnd)228 void destroy_temp_window(HWND wnd) {
229 DestroyWindow(wnd);
230 HMODULE module = GetModuleHandle(nullptr);
231 UnregisterClass(TEMP_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
SkWGLExtensions()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 tempPFD;
260
261 ZeroMemory(&tempPFD, sizeof(tempPFD));
262 tempPFD.nSize = sizeof(tempPFD);
263 tempPFD.nVersion = 1;
264 tempPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
265 tempPFD.iPixelType = PFD_TYPE_RGBA;
266 tempPFD.cColorBits = 32;
267 tempPFD.cDepthBits = 0;
268 tempPFD.cStencilBits = 8;
269 tempPFD.iLayerType = PFD_MAIN_PLANE;
270 HWND tempWND = create_temp_window();
271 if (tempWND) {
272 HDC tempDC = GetDC(tempWND);
273 int tempFormat = ChoosePixelFormat(tempDC, &tempPFD);
274 SetPixelFormat(tempDC, tempFormat, &tempPFD);
275 HGLRC tempGLRC = wglCreateContext(tempDC);
276 SkASSERT(tempGLRC);
277 wglMakeCurrent(tempDC, tempGLRC);
278
279 #if defined(__clang__)
280 #pragma clang diagnostic push
281 #pragma clang diagnostic ignored "-Wcast-function-type"
282 #endif
283
284 GET_PROC(GetExtensionsString, ARB);
285 GET_PROC(ChoosePixelFormat, ARB);
286 GET_PROC(GetPixelFormatAttribiv, ARB);
287 GET_PROC(GetPixelFormatAttribfv, ARB);
288 GET_PROC(CreateContextAttribs, ARB);
289 GET_PROC(SwapInterval, EXT);
290 GET_PROC(CreatePbuffer, ARB);
291 GET_PROC(GetPbufferDC, ARB);
292 GET_PROC(ReleasePbufferDC, ARB);
293 GET_PROC(DestroyPbuffer, ARB);
294
295 #if defined(__clang__)
296 #pragma clang diagnostic pop
297 #endif
298
299 wglMakeCurrent(tempDC, nullptr);
300 wglDeleteContext(tempGLRC);
301 destroy_temp_window(tempWND);
302 }
303
304 wglMakeCurrent(prevDC, prevGLRC);
305 });
306 }
307
308 ///////////////////////////////////////////////////////////////////////////////
309
get_pixel_formats_to_try(HDC dc,const SkWGLExtensions & extensions,bool doubleBuffered,int msaaSampleCount,bool deepColor,int formatsToTry[2])310 static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions,
311 bool doubleBuffered, int msaaSampleCount, bool deepColor,
312 int formatsToTry[2]) {
313 auto appendAttr = [](SkTDArray<int>& attrs, int attr, int value) {
314 attrs.push_back(attr);
315 attrs.push_back(value);
316 };
317
318 SkTDArray<int> iAttrs;
319 appendAttr(iAttrs, SK_WGL_DRAW_TO_WINDOW, TRUE);
320 appendAttr(iAttrs, SK_WGL_DOUBLE_BUFFER, (doubleBuffered ? TRUE : FALSE));
321 appendAttr(iAttrs, SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION);
322 appendAttr(iAttrs, SK_WGL_SUPPORT_OPENGL, TRUE);
323 if (deepColor) {
324 appendAttr(iAttrs, SK_WGL_RED_BITS, 10);
325 appendAttr(iAttrs, SK_WGL_GREEN_BITS, 10);
326 appendAttr(iAttrs, SK_WGL_BLUE_BITS, 10);
327 appendAttr(iAttrs, SK_WGL_ALPHA_BITS, 2);
328 } else {
329 appendAttr(iAttrs, SK_WGL_COLOR_BITS, 24);
330 appendAttr(iAttrs, SK_WGL_ALPHA_BITS, 8);
331 }
332 appendAttr(iAttrs, SK_WGL_STENCIL_BITS, 8);
333
334 float fAttrs[] = {0, 0};
335
336 // Get a MSAA format if requested and possible.
337 if (msaaSampleCount > 0 &&
338 extensions.hasExtension(dc, "WGL_ARB_multisample")) {
339 SkTDArray<int> msaaIAttrs = iAttrs;
340 appendAttr(msaaIAttrs, SK_WGL_SAMPLE_BUFFERS, TRUE);
341 appendAttr(msaaIAttrs, SK_WGL_SAMPLES, msaaSampleCount);
342 appendAttr(msaaIAttrs, 0, 0);
343 unsigned int num;
344 int formats[64];
345 extensions.choosePixelFormat(dc, msaaIAttrs.begin(), fAttrs, 64, formats, &num);
346 num = std::min(num, 64U);
347 formatsToTry[0] = extensions.selectFormat(formats, num, dc, msaaSampleCount);
348 }
349
350 // Get a non-MSAA format
351 int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1];
352 unsigned int num;
353 appendAttr(iAttrs, 0, 0);
354 extensions.choosePixelFormat(dc, iAttrs.begin(), fAttrs, 1, format, &num);
355 }
356
create_gl_context(HDC dc,const SkWGLExtensions & extensions,SkWGLContextRequest contextType,HGLRC shareContext)357 static HGLRC create_gl_context(HDC dc, const SkWGLExtensions& extensions,
358 SkWGLContextRequest contextType, HGLRC shareContext) {
359 HDC prevDC = wglGetCurrentDC();
360 HGLRC prevGLRC = wglGetCurrentContext();
361
362 HGLRC glrc = nullptr;
363 if (kGLES_SkWGLContextRequest == contextType) {
364 if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile")) {
365 wglMakeCurrent(prevDC, prevGLRC);
366 return nullptr;
367 }
368 static const int glesAttribs[] = {
369 SK_WGL_CONTEXT_MAJOR_VERSION, 3,
370 SK_WGL_CONTEXT_MINOR_VERSION, 0,
371 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_ES2_PROFILE_BIT,
372 0,
373 };
374 glrc = extensions.createContextAttribs(dc, shareContext, glesAttribs);
375 if (nullptr == glrc) {
376 wglMakeCurrent(prevDC, prevGLRC);
377 return nullptr;
378 }
379 } else {
380 if (kGLPreferCoreProfile_SkWGLContextRequest == contextType &&
381 extensions.hasExtension(dc, "WGL_ARB_create_context")) {
382 static const int kCoreGLVersions[] = {
383 4, 3,
384 4, 2,
385 4, 1,
386 4, 0,
387 3, 3,
388 3, 2,
389 };
390 int coreProfileAttribs[] = {
391 SK_WGL_CONTEXT_MAJOR_VERSION, -1,
392 SK_WGL_CONTEXT_MINOR_VERSION, -1,
393 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT,
394 0,
395 };
396 for (size_t v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) {
397 coreProfileAttribs[1] = kCoreGLVersions[2 * v];
398 coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1];
399 glrc = extensions.createContextAttribs(dc, shareContext, coreProfileAttribs);
400 if (glrc) {
401 break;
402 }
403 }
404 }
405 }
406
407 if (nullptr == glrc) {
408 glrc = wglCreateContext(dc);
409 if (shareContext) {
410 if (!wglShareLists(shareContext, glrc)) {
411 wglDeleteContext(glrc);
412 return nullptr;
413 }
414 }
415 }
416 SkASSERT(glrc);
417
418 wglMakeCurrent(prevDC, prevGLRC);
419
420 return glrc;
421 }
422
SkCreateWGLContext(HDC dc,int msaaSampleCount,bool deepColor,SkWGLContextRequest contextType,HGLRC shareContext)423 HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor,
424 SkWGLContextRequest contextType, HGLRC shareContext) {
425 SkWGLExtensions extensions;
426 if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) {
427 return nullptr;
428 }
429
430 BOOL set = FALSE;
431
432 int pixelFormatsToTry[] = { -1, -1 };
433 get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, deepColor, pixelFormatsToTry);
434 for (size_t f = 0;
435 !set && -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsToTry);
436 ++f) {
437 PIXELFORMATDESCRIPTOR pfd;
438 DescribePixelFormat(dc, pixelFormatsToTry[f], sizeof(pfd), &pfd);
439 set = SetPixelFormat(dc, pixelFormatsToTry[f], &pfd);
440 }
441
442 if (!set) {
443 return nullptr;
444 }
445
446 return create_gl_context(dc, extensions, contextType, shareContext);
447 }
448
Create(HDC parentDC,SkWGLContextRequest contextType,HGLRC shareContext)449 sk_sp<SkWGLPbufferContext> SkWGLPbufferContext::Create(HDC parentDC,
450 SkWGLContextRequest contextType,
451 HGLRC shareContext) {
452 SkWGLExtensions extensions;
453 if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") ||
454 !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) {
455 return nullptr;
456 }
457
458 // We cache the pixel formats once, and then reuse them. That's possibly incorrect if
459 // there are multiple GPUs, but this function is always called with a freshly made,
460 // identically constructed HDC (see WinGLTestContext).
461 //
462 // We only store two potential pixel formats, one for single buffer, one for double buffer.
463 // We never ask for MSAA, so we don't need the second pixel format for each buffering state.
464 static int gPixelFormats[2] = { -1, -1 };
465 static SkOnce once;
466 once([=] {
467 {
468 // Single buffer
469 int pixelFormatsToTry[2] = { -1, -1 };
470 get_pixel_formats_to_try(parentDC, extensions, false, 0, false, pixelFormatsToTry);
471 gPixelFormats[0] = pixelFormatsToTry[0];
472 }
473 {
474 // Double buffer
475 int pixelFormatsToTry[2] = { -1, -1 };
476 get_pixel_formats_to_try(parentDC, extensions, true, 0, false, pixelFormatsToTry);
477 gPixelFormats[1] = pixelFormatsToTry[0];
478 }
479 });
480
481 // try for single buffer first
482 for (int pixelFormat : gPixelFormats) {
483 if (-1 == pixelFormat) {
484 continue;
485 }
486 HPBUFFER pbuf = extensions.createPbuffer(parentDC, pixelFormat, 1, 1, nullptr);
487 if (0 != pbuf) {
488 HDC dc = extensions.getPbufferDC(pbuf);
489 if (dc) {
490 HGLRC glrc = create_gl_context(dc, extensions, contextType, shareContext);
491 if (glrc) {
492 return sk_sp<SkWGLPbufferContext>(new SkWGLPbufferContext(pbuf, dc, glrc));
493 }
494 extensions.releasePbufferDC(pbuf, dc);
495 }
496 extensions.destroyPbuffer(pbuf);
497 }
498 }
499 return nullptr;
500 }
501
~SkWGLPbufferContext()502 SkWGLPbufferContext::~SkWGLPbufferContext() {
503 SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer"));
504 wglDeleteContext(fGLRC);
505 fExtensions.releasePbufferDC(fPbuffer, fDC);
506 fExtensions.destroyPbuffer(fPbuffer);
507 }
508
SkWGLPbufferContext(HPBUFFER pbuffer,HDC dc,HGLRC glrc)509 SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc)
510 : fPbuffer(pbuffer)
511 , fDC(dc)
512 , fGLRC(glrc) {
513 }
514
515 #endif//defined(SK_BUILD_FOR_WIN)
516