1 /*
2 Copyright (c) 2003-2012 Sony Pictures Imageworks Inc., et al.
3 All Rights Reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 * Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
13 * Neither the name of Sony Pictures Imageworks nor the names of its
14   contributors may be used to endorse or promote products derived from
15   this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 
30 #include "OpenColorIO_AE_GL.h"
31 
32 #include <windows.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 
36 static HWND         g_win = NULL;
37 static HDC          g_hdc = NULL;
38 static HGLRC        g_context = NULL;
39 static GLuint       g_framebuffer = GL_INVALID_VALUE;
40 
41 
HaveRequiredExtensions()42 static bool HaveRequiredExtensions()
43 {
44     const GLubyte *strVersion = glGetString(GL_VERSION);
45     const GLubyte *strExt = glGetString(GL_EXTENSIONS);
46 
47     if(strVersion == NULL)
48         return false;
49 
50 #define CheckExtension(N) glewIsExtensionSupported(N)
51 
52     return (GLEW_VERSION_2_0 &&
53             CheckExtension("GL_ARB_color_buffer_float") &&
54             CheckExtension("GL_ARB_texture_float") &&
55             CheckExtension("GL_ARB_vertex_program") &&
56             CheckExtension("GL_ARB_vertex_shader") &&
57             CheckExtension("GL_ARB_texture_cube_map") &&
58             CheckExtension("GL_ARB_fragment_shader") &&
59             CheckExtension("GL_ARB_draw_buffers") &&
60             CheckExtension("GL_ARB_framebuffer_object") );
61 }
62 
63 
GlobalSetup_GL()64 void GlobalSetup_GL()
65 {
66     GLenum init = glewInit();
67 
68     if(init != GLEW_OK)
69         return;
70 
71 
72     WNDCLASSEX winClass;
73     MSG        uMsg;
74 
75     memset(&uMsg,0,sizeof(uMsg));
76 
77     winClass.lpszClassName = "OpenColorIO_AE_Win_Class";
78     winClass.cbSize        = sizeof(WNDCLASSEX);
79     winClass.style         = CS_HREDRAW | CS_VREDRAW;
80     winClass.lpfnWndProc   = DefWindowProc;
81     winClass.hInstance     = NULL;
82     winClass.hIcon         = NULL;
83     winClass.hIconSm       = NULL;
84     winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
85     winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
86     winClass.lpszMenuName  = NULL;
87     winClass.cbClsExtra    = 0;
88     winClass.cbWndExtra    = 0;
89 
90     if( !( RegisterClassEx(&winClass) ) )
91         return;
92 
93     g_win = CreateWindowEx( NULL, "OpenColorIO_AE_Win_Class",
94                              "OpenGL-using FBOs in AE",
95                                 0, 0,
96                                 0, 50, 50,
97                                 NULL,
98                                 NULL,
99                                 NULL,
100                                 NULL    );
101 
102     if(g_win == NULL)
103         return;
104 
105     g_hdc = GetDC(g_win);
106 
107 
108     int pixelFormat;
109 
110     PIXELFORMATDESCRIPTOR pfd;
111     ZeroMemory( &pfd, sizeof( pfd ) );
112 
113     pfd.nSize           = sizeof(PIXELFORMATDESCRIPTOR);
114     pfd.nVersion        = 1;
115     pfd.dwFlags         = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
116     pfd.iPixelType      = PFD_TYPE_RGBA;
117     pfd.cColorBits      = 128;
118     pfd.cDepthBits      = 32;
119     pfd.cStencilBits    = 32;
120     pfd.iLayerType      = PFD_MAIN_PLANE;
121 
122     pixelFormat = ChoosePixelFormat(g_hdc, &pfd);
123 
124     BOOL set_format = SetPixelFormat(g_hdc, pixelFormat, &pfd);
125 
126     if(!set_format)
127     {
128         GlobalSetdown_GL();
129         return;
130     }
131 
132     g_context = wglCreateContext(g_hdc);
133 
134     glFlush();
135 
136     if(g_context == NULL)
137     {
138         GlobalSetdown_GL();
139         return;
140     }
141 
142 
143     SetPluginContext();
144 
145 
146     GLint units;
147     glGetIntegerv(GL_MAX_TEXTURE_UNITS, &units);
148 
149 
150     if( !HaveRequiredExtensions() || units < 2)
151     {
152         GlobalSetdown_GL();
153         SetAEContext();
154         return;
155     }
156 
157     glGenFramebuffersEXT(1, &g_framebuffer);
158 
159 
160     SetAEContext();
161 }
162 
163 
HaveOpenGL()164 bool HaveOpenGL()
165 {
166     return (g_context != NULL && g_win != NULL);
167 }
168 
169 
170 static HDC g_ae_hdc;
171 static HGLRC g_ae_context;
172 
SetPluginContext()173 void SetPluginContext()
174 {
175     g_ae_hdc = wglGetCurrentDC();
176     g_ae_context = wglGetCurrentContext();
177 
178     wglMakeCurrent(g_hdc, g_context);
179 }
180 
181 
SetAEContext()182 void SetAEContext()
183 {
184     wglMakeCurrent(g_ae_hdc, g_ae_context);
185 }
186 
187 
GetFrameBuffer()188 GLuint GetFrameBuffer()
189 {
190     return g_framebuffer;
191 }
192 
193 
GlobalSetdown_GL()194 void GlobalSetdown_GL()
195 {
196     if(g_framebuffer != GL_INVALID_VALUE)
197     {
198         glDeleteFramebuffersEXT(1, &g_framebuffer);
199         g_framebuffer = GL_INVALID_VALUE;
200     }
201 
202     if(g_context)
203     {
204         wglDeleteContext(g_context);
205         g_context = NULL;
206     }
207 
208     if(g_win)
209     {
210         ReleaseDC(g_win, g_hdc);
211         g_win = NULL;
212         g_hdc = NULL;
213 
214         UnregisterClass("OpenColorIO_AE_Win_Class", NULL);
215     }
216 }
217