1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/win/SkNativeGLContext_win.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 +#include "gl/SkNativeGLContext.h" 1.13 +#include "SkWGL.h" 1.14 + 1.15 +#define WIN32_LEAN_AND_MEAN 1.16 +#include <windows.h> 1.17 + 1.18 +SkNativeGLContext::AutoContextRestore::AutoContextRestore() { 1.19 + fOldHGLRC = wglGetCurrentContext(); 1.20 + fOldHDC = wglGetCurrentDC(); 1.21 +} 1.22 + 1.23 +SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { 1.24 + wglMakeCurrent(fOldHDC, fOldHGLRC); 1.25 +} 1.26 + 1.27 +/////////////////////////////////////////////////////////////////////////////// 1.28 + 1.29 +ATOM SkNativeGLContext::gWC = 0; 1.30 + 1.31 +SkNativeGLContext::SkNativeGLContext() 1.32 + : fWindow(NULL) 1.33 + , fDeviceContext(NULL) 1.34 + , fGlRenderContext(0) { 1.35 +} 1.36 + 1.37 +SkNativeGLContext::~SkNativeGLContext() { 1.38 + this->destroyGLContext(); 1.39 +} 1.40 + 1.41 +void SkNativeGLContext::destroyGLContext() { 1.42 + if (fGlRenderContext) { 1.43 + wglDeleteContext(fGlRenderContext); 1.44 + } 1.45 + if (fWindow && fDeviceContext) { 1.46 + ReleaseDC(fWindow, fDeviceContext); 1.47 + } 1.48 + if (fWindow) { 1.49 + DestroyWindow(fWindow); 1.50 + } 1.51 +} 1.52 + 1.53 +const GrGLInterface* SkNativeGLContext::createGLContext() { 1.54 + HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); 1.55 + 1.56 + if (!gWC) { 1.57 + WNDCLASS wc; 1.58 + wc.cbClsExtra = 0; 1.59 + wc.cbWndExtra = 0; 1.60 + wc.hbrBackground = NULL; 1.61 + wc.hCursor = LoadCursor(NULL, IDC_ARROW); 1.62 + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 1.63 + wc.hInstance = hInstance; 1.64 + wc.lpfnWndProc = (WNDPROC) DefWindowProc; 1.65 + wc.lpszClassName = TEXT("Griffin"); 1.66 + wc.lpszMenuName = NULL; 1.67 + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 1.68 + 1.69 + gWC = RegisterClass(&wc); 1.70 + if (!gWC) { 1.71 + SkDebugf("Could not register window class.\n"); 1.72 + return NULL; 1.73 + } 1.74 + } 1.75 + 1.76 + if (!(fWindow = CreateWindow(TEXT("Griffin"), 1.77 + TEXT("The Invisible Man"), 1.78 + WS_OVERLAPPEDWINDOW, 1.79 + 0, 0, 1, 1, 1.80 + NULL, NULL, 1.81 + hInstance, NULL))) { 1.82 + SkDebugf("Could not create window.\n"); 1.83 + return NULL; 1.84 + } 1.85 + 1.86 + if (!(fDeviceContext = GetDC(fWindow))) { 1.87 + SkDebugf("Could not get device context.\n"); 1.88 + this->destroyGLContext(); 1.89 + return NULL; 1.90 + } 1.91 + 1.92 + // Requesting a Core profile would bar us from using NVPR. So we pass false. 1.93 + if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, false))) { 1.94 + SkDebugf("Could not create rendering context.\n"); 1.95 + this->destroyGLContext(); 1.96 + return NULL; 1.97 + } 1.98 + 1.99 + if (!(wglMakeCurrent(fDeviceContext, fGlRenderContext))) { 1.100 + SkDebugf("Could not set the context.\n"); 1.101 + this->destroyGLContext(); 1.102 + return NULL; 1.103 + } 1.104 + const GrGLInterface* interface = GrGLCreateNativeInterface(); 1.105 + if (NULL == interface) { 1.106 + SkDebugf("Could not create GL interface.\n"); 1.107 + this->destroyGLContext(); 1.108 + return NULL; 1.109 + } 1.110 + 1.111 + return interface; 1.112 +} 1.113 + 1.114 +void SkNativeGLContext::makeCurrent() const { 1.115 + if (!wglMakeCurrent(fDeviceContext, fGlRenderContext)) { 1.116 + SkDebugf("Could not create rendering context.\n"); 1.117 + } 1.118 +} 1.119 + 1.120 +void SkNativeGLContext::swapBuffers() const { 1.121 + if (!SwapBuffers(fDeviceContext)) { 1.122 + SkDebugf("Could not complete SwapBuffers.\n"); 1.123 + } 1.124 +}