michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "gl/SkNativeGLContext.h" michael@0: #include "SkWGL.h" michael@0: michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: michael@0: SkNativeGLContext::AutoContextRestore::AutoContextRestore() { michael@0: fOldHGLRC = wglGetCurrentContext(); michael@0: fOldHDC = wglGetCurrentDC(); michael@0: } michael@0: michael@0: SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { michael@0: wglMakeCurrent(fOldHDC, fOldHGLRC); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: ATOM SkNativeGLContext::gWC = 0; michael@0: michael@0: SkNativeGLContext::SkNativeGLContext() michael@0: : fWindow(NULL) michael@0: , fDeviceContext(NULL) michael@0: , fGlRenderContext(0) { michael@0: } michael@0: michael@0: SkNativeGLContext::~SkNativeGLContext() { michael@0: this->destroyGLContext(); michael@0: } michael@0: michael@0: void SkNativeGLContext::destroyGLContext() { michael@0: if (fGlRenderContext) { michael@0: wglDeleteContext(fGlRenderContext); michael@0: } michael@0: if (fWindow && fDeviceContext) { michael@0: ReleaseDC(fWindow, fDeviceContext); michael@0: } michael@0: if (fWindow) { michael@0: DestroyWindow(fWindow); michael@0: } michael@0: } michael@0: michael@0: const GrGLInterface* SkNativeGLContext::createGLContext() { michael@0: HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); michael@0: michael@0: if (!gWC) { michael@0: WNDCLASS wc; michael@0: wc.cbClsExtra = 0; michael@0: wc.cbWndExtra = 0; michael@0: wc.hbrBackground = NULL; michael@0: wc.hCursor = LoadCursor(NULL, IDC_ARROW); michael@0: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); michael@0: wc.hInstance = hInstance; michael@0: wc.lpfnWndProc = (WNDPROC) DefWindowProc; michael@0: wc.lpszClassName = TEXT("Griffin"); michael@0: wc.lpszMenuName = NULL; michael@0: wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; michael@0: michael@0: gWC = RegisterClass(&wc); michael@0: if (!gWC) { michael@0: SkDebugf("Could not register window class.\n"); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: if (!(fWindow = CreateWindow(TEXT("Griffin"), michael@0: TEXT("The Invisible Man"), michael@0: WS_OVERLAPPEDWINDOW, michael@0: 0, 0, 1, 1, michael@0: NULL, NULL, michael@0: hInstance, NULL))) { michael@0: SkDebugf("Could not create window.\n"); michael@0: return NULL; michael@0: } michael@0: michael@0: if (!(fDeviceContext = GetDC(fWindow))) { michael@0: SkDebugf("Could not get device context.\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: // Requesting a Core profile would bar us from using NVPR. So we pass false. michael@0: if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, false))) { michael@0: SkDebugf("Could not create rendering context.\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: if (!(wglMakeCurrent(fDeviceContext, fGlRenderContext))) { michael@0: SkDebugf("Could not set the context.\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: const GrGLInterface* interface = GrGLCreateNativeInterface(); michael@0: if (NULL == interface) { michael@0: SkDebugf("Could not create GL interface.\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: return interface; michael@0: } michael@0: michael@0: void SkNativeGLContext::makeCurrent() const { michael@0: if (!wglMakeCurrent(fDeviceContext, fGlRenderContext)) { michael@0: SkDebugf("Could not create rendering context.\n"); michael@0: } michael@0: } michael@0: michael@0: void SkNativeGLContext::swapBuffers() const { michael@0: if (!SwapBuffers(fDeviceContext)) { michael@0: SkDebugf("Could not complete SwapBuffers.\n"); michael@0: } michael@0: }