OLD | NEW |
| (Empty) |
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 "gl/SkGLContext.h" | |
10 | |
11 #include <windows.h> | |
12 #include <GL/GL.h> | |
13 #include "SkWGL.h" | |
14 | |
15 #define WIN32_LEAN_AND_MEAN | |
16 #include <windows.h> | |
17 | |
18 namespace { | |
19 | |
20 class WinGLContext : public SkGLContext { | |
21 public: | |
22 WinGLContext(); | |
23 | |
24 virtual ~WinGLContext(); | |
25 | |
26 virtual void makeCurrent() const SK_OVERRIDE; | |
27 virtual void swapBuffers() const SK_OVERRIDE; | |
28 protected: | |
29 virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_O
VERRIDE; | |
30 virtual void destroyGLContext() SK_OVERRIDE; | |
31 | |
32 private: | |
33 HWND fWindow; | |
34 HDC fDeviceContext; | |
35 HGLRC fGlRenderContext; | |
36 static ATOM gWC; | |
37 SkWGLPbufferContext* fPbufferContext; | |
38 }; | |
39 | |
40 ATOM WinGLContext::gWC = 0; | |
41 | |
42 WinGLContext::WinGLContext() | |
43 : fWindow(NULL) | |
44 , fDeviceContext(NULL) | |
45 , fGlRenderContext(0) | |
46 , fPbufferContext(NULL) { | |
47 } | |
48 | |
49 WinGLContext::~WinGLContext() { | |
50 this->destroyGLContext(); | |
51 } | |
52 | |
53 void WinGLContext::destroyGLContext() { | |
54 SkSafeSetNull(fPbufferContext); | |
55 if (fGlRenderContext) { | |
56 wglDeleteContext(fGlRenderContext); | |
57 fGlRenderContext = 0; | |
58 } | |
59 if (fWindow && fDeviceContext) { | |
60 ReleaseDC(fWindow, fDeviceContext); | |
61 fDeviceContext = 0; | |
62 } | |
63 if (fWindow) { | |
64 DestroyWindow(fWindow); | |
65 fWindow = 0; | |
66 } | |
67 } | |
68 | |
69 const GrGLInterface* WinGLContext::createGLContext(GrGLStandard forcedGpuAPI) { | |
70 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); | |
71 | |
72 if (!gWC) { | |
73 WNDCLASS wc; | |
74 wc.cbClsExtra = 0; | |
75 wc.cbWndExtra = 0; | |
76 wc.hbrBackground = NULL; | |
77 wc.hCursor = LoadCursor(NULL, IDC_ARROW); | |
78 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); | |
79 wc.hInstance = hInstance; | |
80 wc.lpfnWndProc = (WNDPROC) DefWindowProc; | |
81 wc.lpszClassName = TEXT("Griffin"); | |
82 wc.lpszMenuName = NULL; | |
83 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; | |
84 | |
85 gWC = RegisterClass(&wc); | |
86 if (!gWC) { | |
87 SkDebugf("Could not register window class.\n"); | |
88 return NULL; | |
89 } | |
90 } | |
91 | |
92 if (!(fWindow = CreateWindow(TEXT("Griffin"), | |
93 TEXT("The Invisible Man"), | |
94 WS_OVERLAPPEDWINDOW, | |
95 0, 0, 1, 1, | |
96 NULL, NULL, | |
97 hInstance, NULL))) { | |
98 SkDebugf("Could not create window.\n"); | |
99 return NULL; | |
100 } | |
101 | |
102 if (!(fDeviceContext = GetDC(fWindow))) { | |
103 SkDebugf("Could not get device context.\n"); | |
104 this->destroyGLContext(); | |
105 return NULL; | |
106 } | |
107 // Requesting a Core profile would bar us from using NVPR. So we request | |
108 // compatibility profile or GL ES. | |
109 SkWGLContextRequest contextType = | |
110 kGLES_GrGLStandard == forcedGpuAPI ? | |
111 kGLES_SkWGLContextRequest : kGLPreferCompatibilityProfile_SkWGLContextRe
quest; | |
112 | |
113 fPbufferContext = SkWGLPbufferContext::Create(fDeviceContext, 0, contextType
); | |
114 | |
115 HDC dc; | |
116 HGLRC glrc; | |
117 | |
118 if (NULL == fPbufferContext) { | |
119 if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, contextTy
pe))) { | |
120 SkDebugf("Could not create rendering context.\n"); | |
121 this->destroyGLContext(); | |
122 return NULL; | |
123 } | |
124 dc = fDeviceContext; | |
125 glrc = fGlRenderContext; | |
126 } else { | |
127 ReleaseDC(fWindow, fDeviceContext); | |
128 fDeviceContext = 0; | |
129 DestroyWindow(fWindow); | |
130 fWindow = 0; | |
131 | |
132 dc = fPbufferContext->getDC(); | |
133 glrc = fPbufferContext->getGLRC(); | |
134 } | |
135 | |
136 if (!(wglMakeCurrent(dc, glrc))) { | |
137 SkDebugf("Could not set the context.\n"); | |
138 this->destroyGLContext(); | |
139 return NULL; | |
140 } | |
141 | |
142 const GrGLInterface* interface = GrGLCreateNativeInterface(); | |
143 if (NULL == interface) { | |
144 SkDebugf("Could not create GL interface.\n"); | |
145 this->destroyGLContext(); | |
146 return NULL; | |
147 } | |
148 | |
149 return interface; | |
150 } | |
151 | |
152 void WinGLContext::makeCurrent() const { | |
153 HDC dc; | |
154 HGLRC glrc; | |
155 | |
156 if (NULL == fPbufferContext) { | |
157 dc = fDeviceContext; | |
158 glrc = fGlRenderContext; | |
159 } else { | |
160 dc = fPbufferContext->getDC(); | |
161 glrc = fPbufferContext->getGLRC(); | |
162 } | |
163 | |
164 if (!wglMakeCurrent(dc, glrc)) { | |
165 SkDebugf("Could not create rendering context.\n"); | |
166 } | |
167 } | |
168 | |
169 void WinGLContext::swapBuffers() const { | |
170 HDC dc; | |
171 | |
172 if (NULL == fPbufferContext) { | |
173 dc = fDeviceContext; | |
174 } else { | |
175 dc = fPbufferContext->getDC(); | |
176 } | |
177 if (!SwapBuffers(dc)) { | |
178 SkDebugf("Could not complete SwapBuffers.\n"); | |
179 } | |
180 } | |
181 | |
182 } // anonymous namespace | |
183 | |
184 SkGLContext* SkCreatePlatformGLContext() { | |
185 return SkNEW(WinGLContext); | |
186 } | |
187 | |
OLD | NEW |