summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-05 19:13:09 +0000
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-05 19:13:09 +0000
commit223aab74b8f72b08a24f8fe8bdd91a365b14cde6 (patch)
treefd2563ea7ce431bd959f3ac9665164baebd99a2f
parentd9ad864e9acb917fe0539cbdc067c859ae6090d9 (diff)
downloadsrc-223aab74b8f72b08a24f8fe8bdd91a365b14cde6.tar.gz
Unify wgl context creation.
R=robertphillips@google.com Review URL: https://codereview.chromium.org/12455009 git-svn-id: http://skia.googlecode.com/svn/trunk/src@7990 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--gpu/gl/win/SkNativeGLContext_win.cpp56
-rw-r--r--utils/win/SkWGL_win.cpp106
-rw-r--r--views/win/SkOSWindow_win.cpp114
3 files changed, 111 insertions, 165 deletions
diff --git a/gpu/gl/win/SkNativeGLContext_win.cpp b/gpu/gl/win/SkNativeGLContext_win.cpp
index 00829207..eabc78a9 100644
--- a/gpu/gl/win/SkNativeGLContext_win.cpp
+++ b/gpu/gl/win/SkNativeGLContext_win.cpp
@@ -7,6 +7,7 @@
*/
#include "gl/SkNativeGLContext.h"
+#include "SkWGL.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
@@ -49,35 +50,6 @@ void SkNativeGLContext::destroyGLContext() {
const GrGLInterface* SkNativeGLContext::createGLContext() {
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
- if (!gWC) {
- WNDCLASS wc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hbrBackground = NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hInstance = hInstance;
- wc.lpfnWndProc = (WNDPROC) DefWindowProc;
- wc.lpszClassName = TEXT("Griffin");
- wc.lpszMenuName = NULL;
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
-
- gWC = RegisterClass(&wc);
- if (!gWC) {
- SkDebugf("Could not register window class.\n");
- return NULL;
- }
- }
-
- if (!(fWindow = CreateWindow(TEXT("Griffin"),
- TEXT("The Invisible Man"),
- WS_OVERLAPPEDWINDOW,
- 0, 0, 1, 1,
- NULL, NULL,
- hInstance, NULL))) {
- SkDebugf("Could not create window.\n");
- return NULL;
- }
if (!(fDeviceContext = GetDC(fWindow))) {
SkDebugf("Could not get device context.\n");
@@ -85,31 +57,7 @@ const GrGLInterface* SkNativeGLContext::createGLContext() {
return NULL;
}
- PIXELFORMATDESCRIPTOR pfd;
- ZeroMemory(&pfd, sizeof(pfd));
- pfd.nSize = sizeof(pfd);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_SUPPORT_OPENGL;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cDepthBits = 0;
- pfd.cStencilBits = 0;
- pfd.iLayerType = PFD_MAIN_PLANE;
-
- int pixelFormat = 0;
- if (!(pixelFormat = ChoosePixelFormat(fDeviceContext, &pfd))) {
- SkDebugf("No matching pixel format descriptor.\n");
- this->destroyGLContext();
- return NULL;
- }
-
- if (!SetPixelFormat(fDeviceContext, pixelFormat, &pfd)) {
- SkDebugf("Could not set the pixel format %d.\n", pixelFormat);
- this->destroyGLContext();
- return NULL;
- }
-
- if (!(fGlRenderContext = wglCreateContext(fDeviceContext))) {
+ if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, false))) {
SkDebugf("Could not create rendering context.\n");
this->destroyGLContext();
return NULL;
diff --git a/utils/win/SkWGL_win.cpp b/utils/win/SkWGL_win.cpp
index 48b270d0..35153f20 100644
--- a/utils/win/SkWGL_win.cpp
+++ b/utils/win/SkWGL_win.cpp
@@ -263,3 +263,109 @@ SkWGLExtensions::SkWGLExtensions()
wglMakeCurrent(prevDC, prevGLRC);
}
+
+HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool preferCoreProfile) {
+ SkWGLExtensions extensions;
+ if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) {
+ return NULL;
+ }
+
+ HDC prevDC = wglGetCurrentDC();
+ HGLRC prevGLRC = wglGetCurrentContext();
+ PIXELFORMATDESCRIPTOR pfd;
+
+ int format = 0;
+
+ static const int iAttrs[] = {
+ SK_WGL_DRAW_TO_WINDOW, TRUE,
+ SK_WGL_DOUBLE_BUFFER, TRUE,
+ SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION,
+ SK_WGL_SUPPORT_OPENGL, TRUE,
+ SK_WGL_COLOR_BITS, 24,
+ SK_WGL_ALPHA_BITS, 8,
+ SK_WGL_STENCIL_BITS, 8,
+ 0, 0
+ };
+
+ float fAttrs[] = {0, 0};
+
+ if (msaaSampleCount > 0 &&
+ extensions.hasExtension(dc, "WGL_ARB_multisample")) {
+ static const int kIAttrsCount = SK_ARRAY_COUNT(iAttrs);
+ int msaaIAttrs[kIAttrsCount + 6];
+ memcpy(msaaIAttrs, iAttrs, sizeof(int) * kIAttrsCount);
+ SkASSERT(0 == msaaIAttrs[kIAttrsCount - 2] &&
+ 0 == msaaIAttrs[kIAttrsCount - 1]);
+ msaaIAttrs[kIAttrsCount - 2] = SK_WGL_SAMPLE_BUFFERS;
+ msaaIAttrs[kIAttrsCount - 1] = TRUE;
+ msaaIAttrs[kIAttrsCount + 0] = SK_WGL_SAMPLES;
+ msaaIAttrs[kIAttrsCount + 1] = msaaSampleCount;
+ if (extensions.hasExtension(dc, "WGL_NV_multisample_coverage")) {
+ msaaIAttrs[kIAttrsCount + 2] = SK_WGL_COLOR_SAMPLES;
+ // We want the fewest number of color samples possible.
+ // Passing 0 gives only the formats where all samples are color
+ // samples.
+ msaaIAttrs[kIAttrsCount + 3] = 1;
+ msaaIAttrs[kIAttrsCount + 4] = 0;
+ msaaIAttrs[kIAttrsCount + 5] = 0;
+ } else {
+ msaaIAttrs[kIAttrsCount + 2] = 0;
+ msaaIAttrs[kIAttrsCount + 3] = 0;
+ }
+ unsigned int num;
+ int formats[64];
+ extensions.choosePixelFormat(dc, msaaIAttrs, fAttrs, 64, formats, &num);
+ num = min(num,64);
+ int formatToTry = extensions.selectFormat(formats,
+ num,
+ dc,
+ msaaSampleCount);
+ DescribePixelFormat(dc, formatToTry, sizeof(pfd), &pfd);
+ if (SetPixelFormat(dc, formatToTry, &pfd)) {
+ format = formatToTry;
+ }
+ }
+
+ if (0 == format) {
+ // Either MSAA wasn't requested or creation failed
+ unsigned int num;
+ extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, &format, &num);
+ DescribePixelFormat(dc, format, sizeof(pfd), &pfd);
+ BOOL set = SetPixelFormat(dc, format, &pfd);
+ SkASSERT(TRUE == set);
+ }
+
+ HGLRC glrc = NULL;
+ if (preferCoreProfile && extensions.hasExtension(dc, "WGL_ARB_create_context")) {
+ static const int kCoreGLVersions[] = {
+ 4, 3,
+ 4, 2,
+ 4, 1,
+ 4, 0,
+ 3, 3,
+ 3, 2,
+ };
+ int coreProfileAttribs[] = {
+ SK_WGL_CONTEXT_MAJOR_VERSION, -1,
+ SK_WGL_CONTEXT_MINOR_VERSION, -1,
+ SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT,
+ 0,
+ };
+ for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) {
+ coreProfileAttribs[1] = kCoreGLVersions[2 * v];
+ coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1];
+ glrc = extensions.createContextAttribs(dc, NULL, coreProfileAttribs);
+ if (NULL != glrc) {
+ break;
+ }
+ }
+ }
+
+ if (NULL == glrc) {
+ glrc = wglCreateContext(dc);
+ }
+ SkASSERT(glrc);
+
+ wglMakeCurrent(prevDC, prevGLRC);
+ return glrc;
+}
diff --git a/views/win/SkOSWindow_win.cpp b/views/win/SkOSWindow_win.cpp
index 4eb41ce1..620bd718 100644
--- a/views/win/SkOSWindow_win.cpp
+++ b/views/win/SkOSWindow_win.cpp
@@ -323,119 +323,11 @@ void SkEvent::SignalQueueTimer(SkMSec delay)
}
#if SK_SUPPORT_GPU
-HGLRC create_gl(HWND hwnd, int msaaSampleCount) {
-
- HDC dc = GetDC(hwnd);
-
- SkWGLExtensions extensions;
- if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) {
- return NULL;
- }
-
- HDC prevDC = wglGetCurrentDC();
- HGLRC prevGLRC = wglGetCurrentContext();
- PIXELFORMATDESCRIPTOR pfd;
-
- int format = 0;
-
- static const GLint iAttrs[] = {
- SK_WGL_DRAW_TO_WINDOW, TRUE,
- SK_WGL_DOUBLE_BUFFER, TRUE,
- SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION,
- SK_WGL_SUPPORT_OPENGL, TRUE,
- SK_WGL_COLOR_BITS, 24,
- SK_WGL_ALPHA_BITS, 8,
- SK_WGL_STENCIL_BITS, 8,
- 0, 0
- };
-
- GLfloat fAttrs[] = {0, 0};
-
- if (msaaSampleCount > 0 &&
- extensions.hasExtension(dc, "WGL_ARB_multisample")) {
- static const int kIAttrsCount = SK_ARRAY_COUNT(iAttrs);
- GLint msaaIAttrs[kIAttrsCount + 6];
- memcpy(msaaIAttrs, iAttrs, sizeof(GLint) * kIAttrsCount);
- SkASSERT(0 == msaaIAttrs[kIAttrsCount - 2] &&
- 0 == msaaIAttrs[kIAttrsCount - 1]);
- msaaIAttrs[kIAttrsCount - 2] = SK_WGL_SAMPLE_BUFFERS;
- msaaIAttrs[kIAttrsCount - 1] = TRUE;
- msaaIAttrs[kIAttrsCount + 0] = SK_WGL_SAMPLES;
- msaaIAttrs[kIAttrsCount + 1] = msaaSampleCount;
- if (extensions.hasExtension(dc, "WGL_NV_multisample_coverage")) {
- msaaIAttrs[kIAttrsCount + 2] = SK_WGL_COLOR_SAMPLES;
- // We want the fewest number of color samples possible.
- // Passing 0 gives only the formats where all samples are color
- // samples.
- msaaIAttrs[kIAttrsCount + 3] = 1;
- msaaIAttrs[kIAttrsCount + 4] = 0;
- msaaIAttrs[kIAttrsCount + 5] = 0;
- } else {
- msaaIAttrs[kIAttrsCount + 2] = 0;
- msaaIAttrs[kIAttrsCount + 3] = 0;
- }
- GLuint num;
- int formats[64];
- extensions.choosePixelFormat(dc, msaaIAttrs, fAttrs, 64, formats, &num);
- num = min(num,64);
- int formatToTry = extensions.selectFormat(formats,
- num,
- dc,
- msaaSampleCount);
- DescribePixelFormat(dc, formatToTry, sizeof(pfd), &pfd);
- if (SetPixelFormat(dc, formatToTry, &pfd)) {
- format = formatToTry;
- }
- }
-
- if (0 == format) {
- GLuint num;
- extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, &format, &num);
- DescribePixelFormat(dc, format, sizeof(pfd), &pfd);
- BOOL set = SetPixelFormat(dc, format, &pfd);
- SkASSERT(TRUE == set);
- }
-
- HGLRC glrc = NULL;
-#if 0 // Change to 1 to attempt to create a core profile GL context of version 4.3 or lower
- if (extensions.hasExtension(dc, "WGL_ARB_create_context")) {
- static const GLint kCoreGLVersions[] = {
- 4, 3,
- 4, 2,
- 4, 1,
- 4, 0,
- 3, 3,
- 3, 2,
- };
- GLint coreProfileAttribs[] = {
- SK_WGL_CONTEXT_MAJOR_VERSION, -1,
- SK_WGL_CONTEXT_MINOR_VERSION, -1,
- SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT,
- 0,
- };
- for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) {
- coreProfileAttribs[1] = kCoreGLVersions[2 * v];
- coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1];
- glrc = extensions.createContextAttribs(dc, NULL, coreProfileAttribs);
- if (NULL != glrc) {
- break;
- }
- }
- }
-#endif
-
- if (NULL == glrc) {
- glrc = wglCreateContext(dc);
- }
- SkASSERT(glrc);
-
- wglMakeCurrent(prevDC, prevGLRC);
- return glrc;
-}
bool SkOSWindow::attachGL(int msaaSampleCount) {
+ HDC dc = GetDC((HWND)fHWND);
if (NULL == fHGLRC) {
- fHGLRC = create_gl((HWND)fHWND, msaaSampleCount);
+ fHGLRC = SkCreateWGLContext(dc, msaaSampleCount, false);
if (NULL == fHGLRC) {
return false;
}
@@ -444,7 +336,7 @@ bool SkOSWindow::attachGL(int msaaSampleCount) {
glStencilMask(0xffffffff);
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
- if (wglMakeCurrent(GetDC((HWND)fHWND), (HGLRC)fHGLRC)) {
+ if (wglMakeCurrent(dc, (HGLRC)fHGLRC)) {
glViewport(0, 0, SkScalarRound(this->width()),
SkScalarRound(this->height()));
return true;