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