OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "gl/SkNativeGLContext.h" | 9 #include "gl/SkGLContext.h" |
| 10 |
| 11 #include <windows.h> |
| 12 #include <GL/GL.h> |
| 13 #include "SkWGL.h" |
10 | 14 |
11 #define WIN32_LEAN_AND_MEAN | 15 #define WIN32_LEAN_AND_MEAN |
12 #include <windows.h> | 16 #include <windows.h> |
13 | 17 |
14 SkNativeGLContext::AutoContextRestore::AutoContextRestore() { | 18 namespace { |
15 fOldHGLRC = wglGetCurrentContext(); | |
16 fOldHDC = wglGetCurrentDC(); | |
17 } | |
18 | 19 |
19 SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { | 20 class WinGLContext : public SkGLContext { |
20 wglMakeCurrent(fOldHDC, fOldHGLRC); | 21 public: |
21 } | 22 WinGLContext(); |
22 | 23 |
23 /////////////////////////////////////////////////////////////////////////////// | 24 virtual ~WinGLContext(); |
24 | 25 |
25 ATOM SkNativeGLContext::gWC = 0; | 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; |
26 | 31 |
27 SkNativeGLContext::SkNativeGLContext() | 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() |
28 : fWindow(NULL) | 43 : fWindow(NULL) |
29 , fDeviceContext(NULL) | 44 , fDeviceContext(NULL) |
30 , fGlRenderContext(0) | 45 , fGlRenderContext(0) |
31 , fPbufferContext(NULL) { | 46 , fPbufferContext(NULL) { |
32 } | 47 } |
33 | 48 |
34 SkNativeGLContext::~SkNativeGLContext() { | 49 WinGLContext::~WinGLContext() { |
35 this->destroyGLContext(); | 50 this->destroyGLContext(); |
36 } | 51 } |
37 | 52 |
38 void SkNativeGLContext::destroyGLContext() { | 53 void WinGLContext::destroyGLContext() { |
39 SkSafeSetNull(fPbufferContext); | 54 SkSafeSetNull(fPbufferContext); |
40 if (fGlRenderContext) { | 55 if (fGlRenderContext) { |
41 wglDeleteContext(fGlRenderContext); | 56 wglDeleteContext(fGlRenderContext); |
42 fGlRenderContext = 0; | 57 fGlRenderContext = 0; |
43 } | 58 } |
44 if (fWindow && fDeviceContext) { | 59 if (fWindow && fDeviceContext) { |
45 ReleaseDC(fWindow, fDeviceContext); | 60 ReleaseDC(fWindow, fDeviceContext); |
46 fDeviceContext = 0; | 61 fDeviceContext = 0; |
47 } | 62 } |
48 if (fWindow) { | 63 if (fWindow) { |
49 DestroyWindow(fWindow); | 64 DestroyWindow(fWindow); |
50 fWindow = 0; | 65 fWindow = 0; |
51 } | 66 } |
52 } | 67 } |
53 | 68 |
54 const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
I) { | 69 const GrGLInterface* WinGLContext::createGLContext(GrGLStandard forcedGpuAPI) { |
55 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); | 70 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); |
56 | 71 |
57 if (!gWC) { | 72 if (!gWC) { |
58 WNDCLASS wc; | 73 WNDCLASS wc; |
59 wc.cbClsExtra = 0; | 74 wc.cbClsExtra = 0; |
60 wc.cbWndExtra = 0; | 75 wc.cbWndExtra = 0; |
61 wc.hbrBackground = NULL; | 76 wc.hbrBackground = NULL; |
62 wc.hCursor = LoadCursor(NULL, IDC_ARROW); | 77 wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
63 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); | 78 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
64 wc.hInstance = hInstance; | 79 wc.hInstance = hInstance; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 const GrGLInterface* interface = GrGLCreateNativeInterface(); | 142 const GrGLInterface* interface = GrGLCreateNativeInterface(); |
128 if (NULL == interface) { | 143 if (NULL == interface) { |
129 SkDebugf("Could not create GL interface.\n"); | 144 SkDebugf("Could not create GL interface.\n"); |
130 this->destroyGLContext(); | 145 this->destroyGLContext(); |
131 return NULL; | 146 return NULL; |
132 } | 147 } |
133 | 148 |
134 return interface; | 149 return interface; |
135 } | 150 } |
136 | 151 |
137 void SkNativeGLContext::makeCurrent() const { | 152 void WinGLContext::makeCurrent() const { |
138 HDC dc; | 153 HDC dc; |
139 HGLRC glrc; | 154 HGLRC glrc; |
140 | 155 |
141 if (NULL == fPbufferContext) { | 156 if (NULL == fPbufferContext) { |
142 dc = fDeviceContext; | 157 dc = fDeviceContext; |
143 glrc = fGlRenderContext; | 158 glrc = fGlRenderContext; |
144 } else { | 159 } else { |
145 dc = fPbufferContext->getDC(); | 160 dc = fPbufferContext->getDC(); |
146 glrc = fPbufferContext->getGLRC(); | 161 glrc = fPbufferContext->getGLRC(); |
147 } | 162 } |
148 | 163 |
149 if (!wglMakeCurrent(dc, glrc)) { | 164 if (!wglMakeCurrent(dc, glrc)) { |
150 SkDebugf("Could not create rendering context.\n"); | 165 SkDebugf("Could not create rendering context.\n"); |
151 } | 166 } |
152 } | 167 } |
153 | 168 |
154 void SkNativeGLContext::swapBuffers() const { | 169 void WinGLContext::swapBuffers() const { |
155 HDC dc; | 170 HDC dc; |
156 | 171 |
157 if (NULL == fPbufferContext) { | 172 if (NULL == fPbufferContext) { |
158 dc = fDeviceContext; | 173 dc = fDeviceContext; |
159 } else { | 174 } else { |
160 dc = fPbufferContext->getDC(); | 175 dc = fPbufferContext->getDC(); |
161 } | 176 } |
162 if (!SwapBuffers(dc)) { | 177 if (!SwapBuffers(dc)) { |
163 SkDebugf("Could not complete SwapBuffers.\n"); | 178 SkDebugf("Could not complete SwapBuffers.\n"); |
164 } | 179 } |
165 } | 180 } |
| 181 |
| 182 } // anonymous namespace |
| 183 |
| 184 SkGLContext* SkCreatePlatformGLContext() { |
| 185 return SkNEW(WinGLContext); |
| 186 } |
| 187 |
OLD | NEW |