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