blob: 5d518dd13885f89ebf27b791194ba2d3b104dc93 [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
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
bsalomon@google.comd47fafe2011-10-20 21:09:45 +000014SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
15 fOldHGLRC = wglGetCurrentContext();
16 fOldHDC = wglGetCurrentDC();
17}
18
19SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
20 wglMakeCurrent(fOldHDC, fOldHGLRC);
21}
22
23///////////////////////////////////////////////////////////////////////////////
24
bsalomon@google.com373a6632011-10-19 20:43:20 +000025ATOM SkNativeGLContext::gWC = 0;
26
27SkNativeGLContext::SkNativeGLContext()
bsalomon@google.comd47fafe2011-10-20 21:09:45 +000028 : fWindow(NULL)
29 , fDeviceContext(NULL)
30 , fGlRenderContext(0) {
bsalomon@google.com373a6632011-10-19 20:43:20 +000031}
32
33SkNativeGLContext::~SkNativeGLContext() {
34 this->destroyGLContext();
35}
36
37void SkNativeGLContext::destroyGLContext() {
38 if (fGlRenderContext) {
39 wglDeleteContext(fGlRenderContext);
40 }
41 if (fWindow && fDeviceContext) {
42 ReleaseDC(fWindow, fDeviceContext);
43 }
44 if (fWindow) {
45 DestroyWindow(fWindow);
46 }
47}
48
49const GrGLInterface* SkNativeGLContext::createGLContext() {
50 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
51
52 if (!gWC) {
53 WNDCLASS wc;
54 wc.cbClsExtra = 0;
55 wc.cbWndExtra = 0;
56 wc.hbrBackground = NULL;
57 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
58 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
59 wc.hInstance = hInstance;
60 wc.lpfnWndProc = (WNDPROC) DefWindowProc;
61 wc.lpszClassName = TEXT("Griffin");
62 wc.lpszMenuName = NULL;
63 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
64
65 gWC = RegisterClass(&wc);
66 if (!gWC) {
67 SkDebugf("Could not register window class.\n");
68 return NULL;
69 }
70 }
71
72 if (!(fWindow = CreateWindow(TEXT("Griffin"),
73 TEXT("The Invisible Man"),
74 WS_OVERLAPPEDWINDOW,
75 0, 0, 1, 1,
76 NULL, NULL,
77 hInstance, NULL))) {
78 SkDebugf("Could not create window.\n");
79 return NULL;
80 }
81
82 if (!(fDeviceContext = GetDC(fWindow))) {
83 SkDebugf("Could not get device context.\n");
84 this->destroyGLContext();
85 return NULL;
86 }
87
88 PIXELFORMATDESCRIPTOR pfd;
89 ZeroMemory(&pfd, sizeof(pfd));
90 pfd.nSize = sizeof(pfd);
91 pfd.nVersion = 1;
92 pfd.dwFlags = PFD_SUPPORT_OPENGL;
93 pfd.iPixelType = PFD_TYPE_RGBA;
94 pfd.cColorBits = 32;
95 pfd.cDepthBits = 0;
96 pfd.cStencilBits = 0;
97 pfd.iLayerType = PFD_MAIN_PLANE;
98
99 int pixelFormat = 0;
100 if (!(pixelFormat = ChoosePixelFormat(fDeviceContext, &pfd))) {
101 SkDebugf("No matching pixel format descriptor.\n");
102 this->destroyGLContext();
103 return NULL;
104 }
105
106 if (!SetPixelFormat(fDeviceContext, pixelFormat, &pfd)) {
107 SkDebugf("Could not set the pixel format %d.\n", pixelFormat);
108 this->destroyGLContext();
109 return NULL;
110 }
111
112 if (!(fGlRenderContext = wglCreateContext(fDeviceContext))) {
113 SkDebugf("Could not create rendering context.\n");
114 this->destroyGLContext();
115 return NULL;
116 }
117
118 if (!(wglMakeCurrent(fDeviceContext, fGlRenderContext))) {
119 SkDebugf("Could not set the context.\n");
120 this->destroyGLContext();
121 return NULL;
122 }
123 const GrGLInterface* interface = GrGLCreateNativeInterface();
124 if (NULL == interface) {
125 SkDebugf("Could not create GL interface.\n");
126 this->destroyGLContext();
127 return NULL;
128 }
129
130 return interface;
131}
132
133void SkNativeGLContext::makeCurrent() const {
134 if (!wglMakeCurrent(fDeviceContext, fGlRenderContext)) {
135 SkDebugf("Could not create rendering context.\n");
136 }
137}