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/SkNativeGLContext.h"
11 #define WIN32_LEAN_AND_MEAN
14 SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
15 fOldHGLRC = wglGetCurrentContext();
16 fOldHDC = wglGetCurrentDC();
19 SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
20 wglMakeCurrent(fOldHDC, fOldHGLRC);
23 ///////////////////////////////////////////////////////////////////////////////
25 ATOM SkNativeGLContext::gWC = 0;
27 SkNativeGLContext::SkNativeGLContext()
29 , fDeviceContext(NULL)
31 , fPbufferContext(NULL) {
34 SkNativeGLContext::~SkNativeGLContext() {
35 this->destroyGLContext();
38 void SkNativeGLContext::destroyGLContext() {
39 SkSafeSetNull(fPbufferContext);
40 if (fGlRenderContext) {
41 wglDeleteContext(fGlRenderContext);
44 if (fWindow && fDeviceContext) {
45 ReleaseDC(fWindow, fDeviceContext);
49 DestroyWindow(fWindow);
54 const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
55 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
61 wc.hbrBackground = NULL;
62 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
63 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
64 wc.hInstance = hInstance;
65 wc.lpfnWndProc = (WNDPROC) DefWindowProc;
66 wc.lpszClassName = TEXT("Griffin");
67 wc.lpszMenuName = NULL;
68 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
70 gWC = RegisterClass(&wc);
72 SkDebugf("Could not register window class.\n");
77 if (!(fWindow = CreateWindow(TEXT("Griffin"),
78 TEXT("The Invisible Man"),
83 SkDebugf("Could not create window.\n");
87 if (!(fDeviceContext = GetDC(fWindow))) {
88 SkDebugf("Could not get device context.\n");
89 this->destroyGLContext();
92 // Requesting a Core profile would bar us from using NVPR. So we request
93 // compatibility profile or GL ES.
94 SkWGLContextRequest contextType =
95 kGLES_GrGLStandard == forcedGpuAPI ?
96 kGLES_SkWGLContextRequest : kGLPreferCompatibilityProfile_SkWGLContextRequest;
98 fPbufferContext = SkWGLPbufferContext::Create(fDeviceContext, 0, contextType);
103 if (NULL == fPbufferContext) {
104 if (!(fGlRenderContext = SkCreateWGLContext(fDeviceContext, 0, contextType))) {
105 SkDebugf("Could not create rendering context.\n");
106 this->destroyGLContext();
110 glrc = fGlRenderContext;
112 ReleaseDC(fWindow, fDeviceContext);
114 DestroyWindow(fWindow);
117 dc = fPbufferContext->getDC();
118 glrc = fPbufferContext->getGLRC();
121 if (!(wglMakeCurrent(dc, glrc))) {
122 SkDebugf("Could not set the context.\n");
123 this->destroyGLContext();
127 const GrGLInterface* interface = GrGLCreateNativeInterface();
128 if (NULL == interface) {
129 SkDebugf("Could not create GL interface.\n");
130 this->destroyGLContext();
137 void SkNativeGLContext::makeCurrent() const {
141 if (NULL == fPbufferContext) {
143 glrc = fGlRenderContext;
145 dc = fPbufferContext->getDC();
146 glrc = fPbufferContext->getGLRC();
149 if (!wglMakeCurrent(dc, glrc)) {
150 SkDebugf("Could not create rendering context.\n");
154 void SkNativeGLContext::swapBuffers() const {
157 if (NULL == fPbufferContext) {
160 dc = fPbufferContext->getDC();
162 if (!SwapBuffers(dc)) {
163 SkDebugf("Could not complete SwapBuffers.\n");