bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkNativeGLContext.h" |
| 10 | |
| 11 | #define WIN32_LEAN_AND_MEAN |
| 12 | #include <Windows.h> |
| 13 | |
| 14 | ATOM SkNativeGLContext::gWC = 0; |
| 15 | |
| 16 | SkNativeGLContext::SkNativeGLContext() |
bsalomon@google.com | d92780b | 2011-10-20 21:54:46 +0000 | [diff] [blame^] | 17 | : fWindow(NULL) |
| 18 | , fDeviceContext(NULL) |
| 19 | , fGlRenderContext(0) { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SkNativeGLContext::~SkNativeGLContext() { |
| 23 | this->destroyGLContext(); |
| 24 | } |
| 25 | |
| 26 | void SkNativeGLContext::destroyGLContext() { |
| 27 | if (fGlRenderContext) { |
| 28 | wglDeleteContext(fGlRenderContext); |
| 29 | } |
| 30 | if (fWindow && fDeviceContext) { |
| 31 | ReleaseDC(fWindow, fDeviceContext); |
| 32 | } |
| 33 | if (fWindow) { |
| 34 | DestroyWindow(fWindow); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | const GrGLInterface* SkNativeGLContext::createGLContext() { |
| 39 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); |
| 40 | |
| 41 | if (!gWC) { |
| 42 | WNDCLASS wc; |
| 43 | wc.cbClsExtra = 0; |
| 44 | wc.cbWndExtra = 0; |
| 45 | wc.hbrBackground = NULL; |
| 46 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 47 | wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 48 | wc.hInstance = hInstance; |
| 49 | wc.lpfnWndProc = (WNDPROC) DefWindowProc; |
| 50 | wc.lpszClassName = TEXT("Griffin"); |
| 51 | wc.lpszMenuName = NULL; |
| 52 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
| 53 | |
| 54 | gWC = RegisterClass(&wc); |
| 55 | if (!gWC) { |
| 56 | SkDebugf("Could not register window class.\n"); |
| 57 | return NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (!(fWindow = CreateWindow(TEXT("Griffin"), |
| 62 | TEXT("The Invisible Man"), |
| 63 | WS_OVERLAPPEDWINDOW, |
| 64 | 0, 0, 1, 1, |
| 65 | NULL, NULL, |
| 66 | hInstance, NULL))) { |
| 67 | SkDebugf("Could not create window.\n"); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | if (!(fDeviceContext = GetDC(fWindow))) { |
| 72 | SkDebugf("Could not get device context.\n"); |
| 73 | this->destroyGLContext(); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | PIXELFORMATDESCRIPTOR pfd; |
| 78 | ZeroMemory(&pfd, sizeof(pfd)); |
| 79 | pfd.nSize = sizeof(pfd); |
| 80 | pfd.nVersion = 1; |
| 81 | pfd.dwFlags = PFD_SUPPORT_OPENGL; |
| 82 | pfd.iPixelType = PFD_TYPE_RGBA; |
| 83 | pfd.cColorBits = 32; |
| 84 | pfd.cDepthBits = 0; |
| 85 | pfd.cStencilBits = 0; |
| 86 | pfd.iLayerType = PFD_MAIN_PLANE; |
| 87 | |
| 88 | int pixelFormat = 0; |
| 89 | if (!(pixelFormat = ChoosePixelFormat(fDeviceContext, &pfd))) { |
| 90 | SkDebugf("No matching pixel format descriptor.\n"); |
| 91 | this->destroyGLContext(); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | if (!SetPixelFormat(fDeviceContext, pixelFormat, &pfd)) { |
| 96 | SkDebugf("Could not set the pixel format %d.\n", pixelFormat); |
| 97 | this->destroyGLContext(); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | if (!(fGlRenderContext = wglCreateContext(fDeviceContext))) { |
| 102 | SkDebugf("Could not create rendering context.\n"); |
| 103 | this->destroyGLContext(); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | if (!(wglMakeCurrent(fDeviceContext, fGlRenderContext))) { |
| 108 | SkDebugf("Could not set the context.\n"); |
| 109 | this->destroyGLContext(); |
| 110 | return NULL; |
| 111 | } |
| 112 | const GrGLInterface* interface = GrGLCreateNativeInterface(); |
| 113 | if (NULL == interface) { |
| 114 | SkDebugf("Could not create GL interface.\n"); |
| 115 | this->destroyGLContext(); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | return interface; |
| 120 | } |
| 121 | |
| 122 | void SkNativeGLContext::makeCurrent() const { |
| 123 | if (!wglMakeCurrent(fDeviceContext, fGlRenderContext)) { |
| 124 | SkDebugf("Could not create rendering context.\n"); |
| 125 | } |
| 126 | } |