3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "gl/SkGLContext.h"
15 #define WIN32_LEAN_AND_MEAN
20 class WinGLContext : public SkGLContext {
22 WinGLContext(GrGLStandard forcedGpuAPI);
23 virtual ~WinGLContext() SK_OVERRIDE;
24 virtual void makeCurrent() const SK_OVERRIDE;
25 virtual void swapBuffers() const SK_OVERRIDE;
28 void destroyGLContext();
32 HGLRC fGlRenderContext;
34 SkWGLPbufferContext* fPbufferContext;
37 ATOM WinGLContext::gWC = 0;
39 WinGLContext::WinGLContext(GrGLStandard forcedGpuAPI)
41 , fDeviceContext(NULL)
43 , fPbufferContext(NULL) {
44 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
50 wc.hbrBackground = NULL;
51 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
52 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
53 wc.hInstance = hInstance;
54 wc.lpfnWndProc = (WNDPROC) DefWindowProc;
55 wc.lpszClassName = TEXT("Griffin");
56 wc.lpszMenuName = NULL;
57 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
59 gWC = RegisterClass(&wc);
61 SkDebugf("Could not register window class.\n");
66 if (!(fWindow = CreateWindow(TEXT("Griffin"),
67 TEXT("The Invisible Man"),
72 SkDebugf("Could not create window.\n");
76 if (!(fDeviceContext = GetDC(fWindow))) {
77 SkDebugf("Could not get device context.\n");
78 this->destroyGLContext();
81 // Requesting a Core profile would bar us from using NVPR. So we request
82 // compatibility profile or GL ES.
83 SkWGLContextRequest contextType =
84 kGLES_GrGLStandard == forcedGpuAPI ?
85 kGLES_SkWGLContextRequest : kGLPreferCompatibilityProfile_SkWGLContextRequest;
87 fPbufferContext = SkWGLPbufferContext::Create(fDeviceContext, 0, contextType);
92 if (NULL == fPbufferContext) {
93 if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, contextType))) {
94 SkDebugf("Could not create rendering context.\n");
95 this->destroyGLContext();
99 glrc = fGlRenderContext;
101 ReleaseDC(fWindow, fDeviceContext);
103 DestroyWindow(fWindow);
106 dc = fPbufferContext->getDC();
107 glrc = fPbufferContext->getGLRC();
110 if (!(wglMakeCurrent(dc, glrc))) {
111 SkDebugf("Could not set the context.\n");
112 this->destroyGLContext();
116 fGL.reset(GrGLCreateNativeInterface());
117 if (NULL == fGL.get()) {
118 SkDebugf("Could not create GL interface.\n");
119 this->destroyGLContext();
122 if (!fGL->validate()) {
123 SkDebugf("Could not validate GL interface.\n");
124 this->destroyGLContext();
129 WinGLContext::~WinGLContext() {
130 this->destroyGLContext();
133 void WinGLContext::destroyGLContext() {
135 SkSafeSetNull(fPbufferContext);
136 if (fGlRenderContext) {
137 wglDeleteContext(fGlRenderContext);
138 fGlRenderContext = 0;
140 if (fWindow && fDeviceContext) {
141 ReleaseDC(fWindow, fDeviceContext);
145 DestroyWindow(fWindow);
150 void WinGLContext::makeCurrent() const {
154 if (NULL == fPbufferContext) {
156 glrc = fGlRenderContext;
158 dc = fPbufferContext->getDC();
159 glrc = fPbufferContext->getGLRC();
162 if (!wglMakeCurrent(dc, glrc)) {
163 SkDebugf("Could not create rendering context.\n");
167 void WinGLContext::swapBuffers() const {
170 if (NULL == fPbufferContext) {
173 dc = fPbufferContext->getDC();
175 if (!SwapBuffers(dc)) {
176 SkDebugf("Could not complete SwapBuffers.\n");
180 } // anonymous namespace
182 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) {
183 WinGLContext* ctx = SkNEW_ARGS(WinGLContext, (forcedGpuAPI));
184 if (!ctx->isValid()) {