stw: move pixelformat_get/set to shared
[mesa.git] / src / gallium / state_trackers / wgl / shared / stw_context.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <windows.h>
29
30 #include "main/mtypes.h"
31 #include "main/context.h"
32 #include "pipe/p_compiler.h"
33 #include "pipe/p_context.h"
34 #include "state_tracker/st_context.h"
35 #include "state_tracker/st_public.h"
36 #include "shared/stw_device.h"
37 #include "shared/stw_winsys.h"
38 #include "shared/stw_framebuffer.h"
39 #include "shared/stw_pixelformat.h"
40 #include "wgl/stw_wgl_arbmultisample.h"
41 #include "stw_context.h"
42 //#include "stw_wgl.h"
43
44 static struct wgl_context *ctx_head = NULL;
45
46 static HDC current_hdc = NULL;
47 static HGLRC current_hrc = NULL;
48
49 BOOL
50 stw_wgl_copy_context(
51 HGLRC hglrcSrc,
52 HGLRC hglrcDst,
53 UINT mask )
54 {
55 (void) hglrcSrc;
56 (void) hglrcDst;
57 (void) mask;
58
59 return FALSE;
60 }
61
62 HGLRC
63 stw_wgl_create_context(
64 HDC hdc,
65 int iLayerPlane )
66 {
67 uint pfi;
68 const struct pixelformat_info *pf;
69 struct wgl_context *ctx;
70 GLvisual *visual;
71 struct pipe_context *pipe;
72
73 if (iLayerPlane != 0)
74 return NULL;
75
76 pfi = wglGetPixelFormat( hdc );
77 if (pfi == 0)
78 return NULL;
79
80 pf = pixelformat_get_info( pfi - 1 );
81
82 ctx = CALLOC_STRUCT( wgl_context );
83 if (ctx == NULL)
84 return NULL;
85
86 ctx->hdc = hdc;
87 ctx->color_bits = GetDeviceCaps( ctx->hdc, BITSPIXEL );
88
89 /* Create visual based on flags
90 */
91 visual = _mesa_create_visual(
92 GL_TRUE,
93 (pf->flags & PF_FLAG_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
94 GL_FALSE,
95 pf->color.redbits,
96 pf->color.greenbits,
97 pf->color.bluebits,
98 pf->alpha.alphabits,
99 0,
100 pf->depth.depthbits,
101 pf->depth.stencilbits,
102 0,
103 0,
104 0,
105 0,
106 (pf->flags & PF_FLAG_MULTISAMPLED) ? wgl_query_samples() : 0 );
107 if (visual == NULL) {
108 FREE( ctx );
109 return NULL;
110 }
111
112 pipe = stw_dev->stw_winsys->create_context( stw_dev->screen );
113 if (!pipe) {
114 _mesa_destroy_visual( visual );
115 FREE( ctx );
116 return NULL;
117 }
118
119 assert(!pipe->priv);
120 pipe->priv = hdc;
121
122 ctx->st = st_create_context( pipe, visual, NULL );
123 if (ctx->st == NULL) {
124 pipe->destroy( pipe );
125 _mesa_destroy_visual( visual );
126 FREE( ctx );
127 return NULL;
128 }
129 ctx->st->ctx->DriverCtx = ctx;
130
131 ctx->next = ctx_head;
132 ctx_head = ctx;
133
134 return (HGLRC) ctx;
135 }
136
137
138 BOOL
139 stw_wgl_delete_context(
140 HGLRC hglrc )
141 {
142 struct wgl_context **link = &ctx_head;
143 struct wgl_context *ctx = ctx_head;
144
145 while (ctx != NULL) {
146 if (ctx == (struct wgl_context *) hglrc) {
147 GLcontext *glctx = ctx->st->ctx;
148 GET_CURRENT_CONTEXT( glcurctx );
149 struct stw_framebuffer *fb;
150
151 /* Unbind current if deleting current context.
152 */
153 if (glcurctx == glctx)
154 st_make_current( NULL, NULL, NULL );
155
156 fb = framebuffer_from_hdc( ctx->hdc );
157 if (fb)
158 framebuffer_destroy( fb );
159
160 if (WindowFromDC( ctx->hdc ) != NULL)
161 ReleaseDC( WindowFromDC( ctx->hdc ), ctx->hdc );
162
163 st_destroy_context( ctx->st );
164
165 *link = ctx->next;
166 FREE( ctx );
167 return TRUE;
168 }
169
170 link = &ctx->next;
171 ctx = ctx->next;
172 }
173
174 return FALSE;
175 }
176
177 /* Find the width and height of the window named by hdc.
178 */
179 static void
180 get_window_size( HDC hdc, GLuint *width, GLuint *height )
181 {
182 if (WindowFromDC( hdc )) {
183 RECT rect;
184
185 GetClientRect( WindowFromDC( hdc ), &rect );
186 *width = rect.right - rect.left;
187 *height = rect.bottom - rect.top;
188 }
189 else {
190 *width = GetDeviceCaps( hdc, HORZRES );
191 *height = GetDeviceCaps( hdc, VERTRES );
192 }
193 }
194
195 HGLRC
196 stw_wgl_get_current_context( void )
197 {
198 return current_hrc;
199 }
200
201 HDC
202 stw_wgl_get_current_dc( void )
203 {
204 return current_hdc;
205 }
206
207 BOOL
208 stw_wgl_make_current(
209 HDC hdc,
210 HGLRC hglrc )
211 {
212 struct wgl_context *ctx = ctx_head;
213 GET_CURRENT_CONTEXT( glcurctx );
214 struct stw_framebuffer *fb;
215 GLuint width = 0;
216 GLuint height = 0;
217
218 current_hdc = hdc;
219 current_hrc = hglrc;
220
221 if (hdc == NULL || hglrc == NULL) {
222 st_make_current( NULL, NULL, NULL );
223 return TRUE;
224 }
225
226 while (ctx != NULL) {
227 if (ctx == (struct wgl_context *) hglrc)
228 break;
229 ctx = ctx->next;
230 }
231 if (ctx == NULL)
232 return FALSE;
233
234 /* Return if already current.
235 */
236 if (glcurctx != NULL) {
237 struct wgl_context *curctx = (struct wgl_context *) glcurctx->DriverCtx;
238
239 if (curctx != NULL && curctx == ctx && ctx->hdc == hdc)
240 return TRUE;
241 }
242
243 fb = framebuffer_from_hdc( hdc );
244
245 if (hdc != NULL)
246 get_window_size( hdc, &width, &height );
247
248 /* Lazy creation of framebuffers.
249 */
250 if (fb == NULL && ctx != NULL && hdc != NULL) {
251 GLvisual *visual = &ctx->st->ctx->Visual;
252
253 fb = framebuffer_create( hdc, visual, width, height );
254 if (fb == NULL)
255 return FALSE;
256
257 fb->dib_hDC = CreateCompatibleDC( hdc );
258 fb->hbmDIB = NULL;
259 fb->pbPixels = NULL;
260 }
261
262 if (ctx && fb) {
263 st_make_current( ctx->st, fb->stfb, fb->stfb );
264 framebuffer_resize( fb, width, height );
265 ctx->hdc = hdc;
266 ctx->st->pipe->priv = hdc;
267 }
268 else {
269 /* Detach */
270 st_make_current( NULL, NULL, NULL );
271 }
272
273 return TRUE;
274 }
275
276 struct wgl_context *
277 wgl_context_from_hdc(
278 HDC hdc )
279 {
280 struct wgl_context *ctx = ctx_head;
281
282 while (ctx != NULL) {
283 if (ctx->hdc == hdc)
284 return ctx;
285 ctx = ctx->next;
286 }
287 return NULL;
288 }
289
290
291