OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkWGL.h" | 9 #include "SkWGL.h" |
10 | 10 |
11 #include "SkTDArray.h" | 11 #include "SkTDArray.h" |
12 #include "SkTSearch.h" | 12 #include "SkTSearch.h" |
13 #include "SkTSort.h" | 13 #include "SkTSort.h" |
14 | 14 |
15 bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const { | 15 bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const { |
16 if (NULL == this->fGetExtensionsString) { | 16 if (nullptr == this->fGetExtensionsString) { |
17 return false; | 17 return false; |
18 } | 18 } |
19 if (!strcmp("WGL_ARB_extensions_string", ext)) { | 19 if (!strcmp("WGL_ARB_extensions_string", ext)) { |
20 return true; | 20 return true; |
21 } | 21 } |
22 const char* extensionString = this->getExtensionsString(dc); | 22 const char* extensionString = this->getExtensionsString(dc); |
23 size_t extLength = strlen(ext); | 23 size_t extLength = strlen(ext); |
24 | 24 |
25 while (true) { | 25 while (true) { |
26 size_t n = strcspn(extensionString, " "); | 26 size_t n = strcspn(extensionString, " "); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 161 |
162 #if defined(UNICODE) | 162 #if defined(UNICODE) |
163 #define STR_LIT(X) L## #X | 163 #define STR_LIT(X) L## #X |
164 #else | 164 #else |
165 #define STR_LIT(X) #X | 165 #define STR_LIT(X) #X |
166 #endif | 166 #endif |
167 | 167 |
168 #define DUMMY_CLASS STR_LIT("DummyClass") | 168 #define DUMMY_CLASS STR_LIT("DummyClass") |
169 | 169 |
170 HWND create_dummy_window() { | 170 HWND create_dummy_window() { |
171 HMODULE module = GetModuleHandle(NULL); | 171 HMODULE module = GetModuleHandle(nullptr); |
172 HWND dummy; | 172 HWND dummy; |
173 RECT windowRect; | 173 RECT windowRect; |
174 windowRect.left = 0; | 174 windowRect.left = 0; |
175 windowRect.right = 8; | 175 windowRect.right = 8; |
176 windowRect.top = 0; | 176 windowRect.top = 0; |
177 windowRect.bottom = 8; | 177 windowRect.bottom = 8; |
178 | 178 |
179 WNDCLASS wc; | 179 WNDCLASS wc; |
180 | 180 |
181 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; | 181 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
182 wc.lpfnWndProc = (WNDPROC) DefWindowProc; | 182 wc.lpfnWndProc = (WNDPROC) DefWindowProc; |
183 wc.cbClsExtra = 0; | 183 wc.cbClsExtra = 0; |
184 wc.cbWndExtra = 0; | 184 wc.cbWndExtra = 0; |
185 wc.hInstance = module; | 185 wc.hInstance = module; |
186 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); | 186 wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO); |
187 wc.hCursor = LoadCursor(NULL, IDC_ARROW); | 187 wc.hCursor = LoadCursor(nullptr, IDC_ARROW); |
188 wc.hbrBackground = NULL; | 188 wc.hbrBackground = nullptr; |
189 wc.lpszMenuName = NULL; | 189 wc.lpszMenuName = nullptr; |
190 wc.lpszClassName = DUMMY_CLASS; | 190 wc.lpszClassName = DUMMY_CLASS; |
191 | 191 |
192 if(!RegisterClass(&wc)) { | 192 if(!RegisterClass(&wc)) { |
193 return 0; | 193 return 0; |
194 } | 194 } |
195 | 195 |
196 DWORD style, exStyle; | 196 DWORD style, exStyle; |
197 exStyle = WS_EX_CLIENTEDGE; | 197 exStyle = WS_EX_CLIENTEDGE; |
198 style = WS_SYSMENU; | 198 style = WS_SYSMENU; |
199 | 199 |
200 AdjustWindowRectEx(&windowRect, style, false, exStyle); | 200 AdjustWindowRectEx(&windowRect, style, false, exStyle); |
201 if(!(dummy = CreateWindowEx(exStyle, | 201 if(!(dummy = CreateWindowEx(exStyle, |
202 DUMMY_CLASS, | 202 DUMMY_CLASS, |
203 STR_LIT("DummyWindow"), | 203 STR_LIT("DummyWindow"), |
204 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style, | 204 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style, |
205 0, 0, | 205 0, 0, |
206 windowRect.right-windowRect.left, | 206 windowRect.right-windowRect.left, |
207 windowRect.bottom-windowRect.top, | 207 windowRect.bottom-windowRect.top, |
208 NULL, NULL, | 208 nullptr, nullptr, |
209 module, | 209 module, |
210 NULL))) { | 210 nullptr))) { |
211 UnregisterClass(DUMMY_CLASS, module); | 211 UnregisterClass(DUMMY_CLASS, module); |
212 return NULL; | 212 return nullptr; |
213 } | 213 } |
214 ShowWindow(dummy, SW_HIDE); | 214 ShowWindow(dummy, SW_HIDE); |
215 | 215 |
216 return dummy; | 216 return dummy; |
217 } | 217 } |
218 | 218 |
219 void destroy_dummy_window(HWND dummy) { | 219 void destroy_dummy_window(HWND dummy) { |
220 DestroyWindow(dummy); | 220 DestroyWindow(dummy); |
221 HMODULE module = GetModuleHandle(NULL); | 221 HMODULE module = GetModuleHandle(nullptr); |
222 UnregisterClass(DUMMY_CLASS, module); | 222 UnregisterClass(DUMMY_CLASS, module); |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 #define GET_PROC(NAME, SUFFIX) f##NAME = \ | 226 #define GET_PROC(NAME, SUFFIX) f##NAME = \ |
227 (##NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX) | 227 (##NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX) |
228 | 228 |
229 SkWGLExtensions::SkWGLExtensions() | 229 SkWGLExtensions::SkWGLExtensions() |
230 : fGetExtensionsString(NULL) | 230 : fGetExtensionsString(nullptr) |
231 , fChoosePixelFormat(NULL) | 231 , fChoosePixelFormat(nullptr) |
232 , fGetPixelFormatAttribfv(NULL) | 232 , fGetPixelFormatAttribfv(nullptr) |
233 , fGetPixelFormatAttribiv(NULL) | 233 , fGetPixelFormatAttribiv(nullptr) |
234 , fCreateContextAttribs(NULL) | 234 , fCreateContextAttribs(nullptr) |
235 , fSwapInterval(NULL) | 235 , fSwapInterval(nullptr) |
236 , fCreatePbuffer(NULL) | 236 , fCreatePbuffer(nullptr) |
237 , fGetPbufferDC(NULL) | 237 , fGetPbufferDC(nullptr) |
238 , fReleasePbufferDC(NULL) | 238 , fReleasePbufferDC(nullptr) |
239 , fDestroyPbuffer(NULL) | 239 , fDestroyPbuffer(nullptr) |
240 { | 240 { |
241 HDC prevDC = wglGetCurrentDC(); | 241 HDC prevDC = wglGetCurrentDC(); |
242 HGLRC prevGLRC = wglGetCurrentContext(); | 242 HGLRC prevGLRC = wglGetCurrentContext(); |
243 | 243 |
244 PIXELFORMATDESCRIPTOR dummyPFD; | 244 PIXELFORMATDESCRIPTOR dummyPFD; |
245 | 245 |
246 ZeroMemory(&dummyPFD, sizeof(dummyPFD)); | 246 ZeroMemory(&dummyPFD, sizeof(dummyPFD)); |
247 dummyPFD.nSize = sizeof(dummyPFD); | 247 dummyPFD.nSize = sizeof(dummyPFD); |
248 dummyPFD.nVersion = 1; | 248 dummyPFD.nVersion = 1; |
249 dummyPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; | 249 dummyPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; |
(...skipping 15 matching lines...) Expand all Loading... |
265 GET_PROC(ChoosePixelFormat, ARB); | 265 GET_PROC(ChoosePixelFormat, ARB); |
266 GET_PROC(GetPixelFormatAttribiv, ARB); | 266 GET_PROC(GetPixelFormatAttribiv, ARB); |
267 GET_PROC(GetPixelFormatAttribfv, ARB); | 267 GET_PROC(GetPixelFormatAttribfv, ARB); |
268 GET_PROC(CreateContextAttribs, ARB); | 268 GET_PROC(CreateContextAttribs, ARB); |
269 GET_PROC(SwapInterval, EXT); | 269 GET_PROC(SwapInterval, EXT); |
270 GET_PROC(CreatePbuffer, ARB); | 270 GET_PROC(CreatePbuffer, ARB); |
271 GET_PROC(GetPbufferDC, ARB); | 271 GET_PROC(GetPbufferDC, ARB); |
272 GET_PROC(ReleasePbufferDC, ARB); | 272 GET_PROC(ReleasePbufferDC, ARB); |
273 GET_PROC(DestroyPbuffer, ARB); | 273 GET_PROC(DestroyPbuffer, ARB); |
274 | 274 |
275 wglMakeCurrent(dummyDC, NULL); | 275 wglMakeCurrent(dummyDC, nullptr); |
276 wglDeleteContext(dummyGLRC); | 276 wglDeleteContext(dummyGLRC); |
277 destroy_dummy_window(dummyWND); | 277 destroy_dummy_window(dummyWND); |
278 } | 278 } |
279 | 279 |
280 wglMakeCurrent(prevDC, prevGLRC); | 280 wglMakeCurrent(prevDC, prevGLRC); |
281 } | 281 } |
282 | 282 |
283 /////////////////////////////////////////////////////////////////////////////// | 283 /////////////////////////////////////////////////////////////////////////////// |
284 | 284 |
285 static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions, | 285 static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // Get a non-MSAA format | 322 // Get a non-MSAA format |
323 int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1]; | 323 int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1]; |
324 unsigned int num; | 324 unsigned int num; |
325 extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, format, &num); | 325 extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, format, &num); |
326 } | 326 } |
327 | 327 |
328 static HGLRC create_gl_context(HDC dc, SkWGLExtensions extensions, SkWGLContextR
equest contextType) { | 328 static HGLRC create_gl_context(HDC dc, SkWGLExtensions extensions, SkWGLContextR
equest contextType) { |
329 HDC prevDC = wglGetCurrentDC(); | 329 HDC prevDC = wglGetCurrentDC(); |
330 HGLRC prevGLRC = wglGetCurrentContext(); | 330 HGLRC prevGLRC = wglGetCurrentContext(); |
331 | 331 |
332 HGLRC glrc = NULL; | 332 HGLRC glrc = nullptr; |
333 if (kGLES_SkWGLContextRequest == contextType) { | 333 if (kGLES_SkWGLContextRequest == contextType) { |
334 if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile"))
{ | 334 if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile"))
{ |
335 wglMakeCurrent(prevDC, prevGLRC); | 335 wglMakeCurrent(prevDC, prevGLRC); |
336 return NULL; | 336 return nullptr; |
337 } | 337 } |
338 static const int glesAttribs[] = { | 338 static const int glesAttribs[] = { |
339 SK_WGL_CONTEXT_MAJOR_VERSION, 3, | 339 SK_WGL_CONTEXT_MAJOR_VERSION, 3, |
340 SK_WGL_CONTEXT_MINOR_VERSION, 0, | 340 SK_WGL_CONTEXT_MINOR_VERSION, 0, |
341 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_ES2_PROFILE_BIT, | 341 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_ES2_PROFILE_BIT, |
342 0, | 342 0, |
343 }; | 343 }; |
344 glrc = extensions.createContextAttribs(dc, NULL, glesAttribs); | 344 glrc = extensions.createContextAttribs(dc, nullptr, glesAttribs); |
345 if (NULL == glrc) { | 345 if (nullptr == glrc) { |
346 wglMakeCurrent(prevDC, prevGLRC); | 346 wglMakeCurrent(prevDC, prevGLRC); |
347 return NULL; | 347 return nullptr; |
348 } | 348 } |
349 } else { | 349 } else { |
350 if (kGLPreferCoreProfile_SkWGLContextRequest == contextType && | 350 if (kGLPreferCoreProfile_SkWGLContextRequest == contextType && |
351 extensions.hasExtension(dc, "WGL_ARB_create_context")) { | 351 extensions.hasExtension(dc, "WGL_ARB_create_context")) { |
352 static const int kCoreGLVersions[] = { | 352 static const int kCoreGLVersions[] = { |
353 4, 3, | 353 4, 3, |
354 4, 2, | 354 4, 2, |
355 4, 1, | 355 4, 1, |
356 4, 0, | 356 4, 0, |
357 3, 3, | 357 3, 3, |
358 3, 2, | 358 3, 2, |
359 }; | 359 }; |
360 int coreProfileAttribs[] = { | 360 int coreProfileAttribs[] = { |
361 SK_WGL_CONTEXT_MAJOR_VERSION, -1, | 361 SK_WGL_CONTEXT_MAJOR_VERSION, -1, |
362 SK_WGL_CONTEXT_MINOR_VERSION, -1, | 362 SK_WGL_CONTEXT_MINOR_VERSION, -1, |
363 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT, | 363 SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT, |
364 0, | 364 0, |
365 }; | 365 }; |
366 for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) { | 366 for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) { |
367 coreProfileAttribs[1] = kCoreGLVersions[2 * v]; | 367 coreProfileAttribs[1] = kCoreGLVersions[2 * v]; |
368 coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1]; | 368 coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1]; |
369 glrc = extensions.createContextAttribs(dc, NULL, coreProfileAttr
ibs); | 369 glrc = extensions.createContextAttribs(dc, nullptr, coreProfileA
ttribs); |
370 if (glrc) { | 370 if (glrc) { |
371 break; | 371 break; |
372 } | 372 } |
373 } | 373 } |
374 } | 374 } |
375 } | 375 } |
376 | 376 |
377 if (NULL == glrc) { | 377 if (nullptr == glrc) { |
378 glrc = wglCreateContext(dc); | 378 glrc = wglCreateContext(dc); |
379 } | 379 } |
380 SkASSERT(glrc); | 380 SkASSERT(glrc); |
381 | 381 |
382 wglMakeCurrent(prevDC, prevGLRC); | 382 wglMakeCurrent(prevDC, prevGLRC); |
383 | 383 |
384 // This might help make the context non-vsynced. | 384 // This might help make the context non-vsynced. |
385 if (extensions.hasExtension(dc, "WGL_EXT_swap_control")) { | 385 if (extensions.hasExtension(dc, "WGL_EXT_swap_control")) { |
386 extensions.swapInterval(-1); | 386 extensions.swapInterval(-1); |
387 } | 387 } |
388 return glrc; | 388 return glrc; |
389 } | 389 } |
390 | 390 |
391 HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, SkWGLContextRequest contex
tType) { | 391 HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, SkWGLContextRequest contex
tType) { |
392 SkWGLExtensions extensions; | 392 SkWGLExtensions extensions; |
393 if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) { | 393 if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) { |
394 return NULL; | 394 return nullptr; |
395 } | 395 } |
396 | 396 |
397 BOOL set = FALSE; | 397 BOOL set = FALSE; |
398 | 398 |
399 int pixelFormatsToTry[] = { -1, -1 }; | 399 int pixelFormatsToTry[] = { -1, -1 }; |
400 get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, pixelFormats
ToTry); | 400 get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, pixelFormats
ToTry); |
401 for (int f = 0; | 401 for (int f = 0; |
402 !set && -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsTo
Try); | 402 !set && -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsTo
Try); |
403 ++f) { | 403 ++f) { |
404 PIXELFORMATDESCRIPTOR pfd; | 404 PIXELFORMATDESCRIPTOR pfd; |
405 DescribePixelFormat(dc, pixelFormatsToTry[f], sizeof(pfd), &pfd); | 405 DescribePixelFormat(dc, pixelFormatsToTry[f], sizeof(pfd), &pfd); |
406 set = SetPixelFormat(dc, pixelFormatsToTry[f], &pfd); | 406 set = SetPixelFormat(dc, pixelFormatsToTry[f], &pfd); |
407 } | 407 } |
408 | 408 |
409 if (!set) { | 409 if (!set) { |
410 return NULL; | 410 return nullptr; |
411 } | 411 } |
412 | 412 |
413 return create_gl_context(dc, extensions, contextType);} | 413 return create_gl_context(dc, extensions, contextType);} |
414 | 414 |
415 SkWGLPbufferContext* SkWGLPbufferContext::Create(HDC parentDC, int msaaSampleCou
nt, | 415 SkWGLPbufferContext* SkWGLPbufferContext::Create(HDC parentDC, int msaaSampleCou
nt, |
416 SkWGLContextRequest contextType
) { | 416 SkWGLContextRequest contextType
) { |
417 SkWGLExtensions extensions; | 417 SkWGLExtensions extensions; |
418 if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") || | 418 if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") || |
419 !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) { | 419 !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) { |
420 return NULL; | 420 return nullptr; |
421 } | 421 } |
422 | 422 |
423 // try for single buffer first | 423 // try for single buffer first |
424 for (int dblBuffer = 0; dblBuffer < 2; ++dblBuffer) { | 424 for (int dblBuffer = 0; dblBuffer < 2; ++dblBuffer) { |
425 int pixelFormatsToTry[] = { -1, -1 }; | 425 int pixelFormatsToTry[] = { -1, -1 }; |
426 get_pixel_formats_to_try(parentDC, extensions, (0 != dblBuffer), msaaSam
pleCount, | 426 get_pixel_formats_to_try(parentDC, extensions, (0 != dblBuffer), msaaSam
pleCount, |
427 pixelFormatsToTry); | 427 pixelFormatsToTry); |
428 for (int f = 0; -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFo
rmatsToTry); ++f) { | 428 for (int f = 0; -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFo
rmatsToTry); ++f) { |
429 HPBUFFER pbuf = extensions.createPbuffer(parentDC, pixelFormatsToTry
[f], 1, 1, NULL); | 429 HPBUFFER pbuf = extensions.createPbuffer(parentDC, pixelFormatsToTry
[f], 1, 1, nullptr); |
430 if (0 != pbuf) { | 430 if (0 != pbuf) { |
431 HDC dc = extensions.getPbufferDC(pbuf); | 431 HDC dc = extensions.getPbufferDC(pbuf); |
432 if (dc) { | 432 if (dc) { |
433 HGLRC glrc = create_gl_context(dc, extensions, contextType); | 433 HGLRC glrc = create_gl_context(dc, extensions, contextType); |
434 if (glrc) { | 434 if (glrc) { |
435 return new SkWGLPbufferContext(pbuf, dc, glrc); | 435 return new SkWGLPbufferContext(pbuf, dc, glrc); |
436 } | 436 } |
437 extensions.releasePbufferDC(pbuf, dc); | 437 extensions.releasePbufferDC(pbuf, dc); |
438 } | 438 } |
439 extensions.destroyPbuffer(pbuf); | 439 extensions.destroyPbuffer(pbuf); |
440 } | 440 } |
441 } | 441 } |
442 } | 442 } |
443 return NULL; | 443 return nullptr; |
444 } | 444 } |
445 | 445 |
446 SkWGLPbufferContext::~SkWGLPbufferContext() { | 446 SkWGLPbufferContext::~SkWGLPbufferContext() { |
447 SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer")); | 447 SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer")); |
448 wglDeleteContext(fGLRC); | 448 wglDeleteContext(fGLRC); |
449 fExtensions.releasePbufferDC(fPbuffer, fDC); | 449 fExtensions.releasePbufferDC(fPbuffer, fDC); |
450 fExtensions.destroyPbuffer(fPbuffer); | 450 fExtensions.destroyPbuffer(fPbuffer); |
451 } | 451 } |
452 | 452 |
453 SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc) | 453 SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc) |
454 : fPbuffer(pbuffer) | 454 : fPbuffer(pbuffer) |
455 , fDC(dc) | 455 , fDC(dc) |
456 , fGLRC(glrc) { | 456 , fGLRC(glrc) { |
457 } | 457 } |
OLD | NEW |