1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_spu.h"
|
---|
8 | #include "cr_error.h"
|
---|
9 | #include "cr_mem.h"
|
---|
10 | #include "stub.h"
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Returns -1 on error
|
---|
14 | */
|
---|
15 | GLint APIENTRY crCreateContext( const char *dpyName, GLint visBits )
|
---|
16 | {
|
---|
17 | ContextInfo *context;
|
---|
18 | stubInit();
|
---|
19 | /* XXX in Chromium 1.5 and earlier, the last parameter was UNDECIDED.
|
---|
20 | * That didn't seem right so it was changed to CHROMIUM. (Brian)
|
---|
21 | */
|
---|
22 | context = stubNewContext(dpyName, visBits, CHROMIUM, 0);
|
---|
23 | return context ? (int) context->id : -1;
|
---|
24 | }
|
---|
25 |
|
---|
26 | void APIENTRY crDestroyContext( GLint context )
|
---|
27 | {
|
---|
28 | stubDestroyContext(context);
|
---|
29 | }
|
---|
30 |
|
---|
31 | void APIENTRY crMakeCurrent( GLint window, GLint context )
|
---|
32 | {
|
---|
33 | WindowInfo *winInfo = (WindowInfo *)
|
---|
34 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
35 | ContextInfo *contextInfo = (ContextInfo *)
|
---|
36 | crHashtableSearch(stub.contextTable, context);
|
---|
37 | if (contextInfo && contextInfo->type == NATIVE) {
|
---|
38 | crWarning("Can't call crMakeCurrent with native GL context");
|
---|
39 | return;
|
---|
40 | }
|
---|
41 |
|
---|
42 | stubMakeCurrent(winInfo, contextInfo);
|
---|
43 | }
|
---|
44 |
|
---|
45 | GLint APIENTRY crGetCurrentContext( void )
|
---|
46 | {
|
---|
47 | stubInit();
|
---|
48 | if (stub.currentContext)
|
---|
49 | return (GLint) stub.currentContext->id;
|
---|
50 | else
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | GLint APIENTRY crGetCurrentWindow( void )
|
---|
55 | {
|
---|
56 | stubInit();
|
---|
57 | if (stub.currentContext && stub.currentContext->currentDrawable)
|
---|
58 | return stub.currentContext->currentDrawable->spuWindow;
|
---|
59 | else
|
---|
60 | return -1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void APIENTRY crSwapBuffers( GLint window, GLint flags )
|
---|
64 | {
|
---|
65 | const WindowInfo *winInfo = (const WindowInfo *)
|
---|
66 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
67 | if (winInfo)
|
---|
68 | stubSwapBuffers(winInfo, flags);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Returns -1 on error
|
---|
73 | */
|
---|
74 | GLint APIENTRY crWindowCreate( const char *dpyName, GLint visBits )
|
---|
75 | {
|
---|
76 | stubInit();
|
---|
77 | return stubNewWindow( dpyName, visBits );
|
---|
78 | }
|
---|
79 |
|
---|
80 | void APIENTRY crWindowDestroy( GLint window )
|
---|
81 | {
|
---|
82 | WindowInfo *winInfo = (WindowInfo *)
|
---|
83 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
84 | if (winInfo && winInfo->type == CHROMIUM && stub.spu) {
|
---|
85 | stub.spu->dispatch_table.WindowDestroy( winInfo->spuWindow );
|
---|
86 | #ifdef WINDOWS
|
---|
87 | if (winInfo->hVisibleRegion != INVALID_HANDLE_VALUE)
|
---|
88 | {
|
---|
89 | DeleteObject(winInfo->hVisibleRegion);
|
---|
90 | }
|
---|
91 | #endif
|
---|
92 | stub.spu->dispatch_table.Flush();
|
---|
93 |
|
---|
94 | crHashtableDelete(stub.windowTable, window, crFree);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | void APIENTRY crWindowSize( GLint window, GLint w, GLint h )
|
---|
99 | {
|
---|
100 | const WindowInfo *winInfo = (const WindowInfo *)
|
---|
101 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
102 | if (winInfo && winInfo->type == CHROMIUM)
|
---|
103 | {
|
---|
104 | crDebug("Dispatched crWindowSize (%i)", window);
|
---|
105 | stub.spu->dispatch_table.WindowSize( window, w, h );
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void APIENTRY crWindowPosition( GLint window, GLint x, GLint y )
|
---|
110 | {
|
---|
111 | const WindowInfo *winInfo = (const WindowInfo *)
|
---|
112 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
113 | if (winInfo && winInfo->type == CHROMIUM)
|
---|
114 | {
|
---|
115 | crDebug("Dispatched crWindowPosition (%i)", window);
|
---|
116 | stub.spu->dispatch_table.WindowPosition( window, x, y );
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | void APIENTRY crWindowVisibleRegion( GLint window, GLint cRects, void *pRects )
|
---|
121 | {
|
---|
122 | const WindowInfo *winInfo = (const WindowInfo *)
|
---|
123 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
124 | if (winInfo && winInfo->type == CHROMIUM)
|
---|
125 | {
|
---|
126 | crDebug("Dispatched crWindowVisibleRegion (%i, cRects=%i)", window, cRects);
|
---|
127 | stub.spu->dispatch_table.WindowVisibleRegion( window, cRects, pRects );
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | void APIENTRY crWindowShow( GLint window, GLint flag )
|
---|
132 | {
|
---|
133 | WindowInfo *winInfo = (WindowInfo *)
|
---|
134 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
135 | if (winInfo && winInfo->type == CHROMIUM)
|
---|
136 | stub.spu->dispatch_table.WindowShow( window, flag );
|
---|
137 | winInfo->mapped = flag ? GL_TRUE : GL_FALSE;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void APIENTRY stub_GetChromiumParametervCR( GLenum target, GLuint index, GLenum type, GLsizei count, GLvoid *values )
|
---|
141 | {
|
---|
142 | char **ret;
|
---|
143 | switch( target )
|
---|
144 | {
|
---|
145 | case GL_HEAD_SPU_NAME_CR:
|
---|
146 | ret = (char **) values;
|
---|
147 | *ret = stub.spu->name;
|
---|
148 | return;
|
---|
149 | default:
|
---|
150 | stub.spu->dispatch_table.GetChromiumParametervCR( target, index, type, count, values );
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Updates geometry info for given spu window.
|
---|
157 | * Returns GL_TRUE if it changed since last call, GL_FALSE overwise.
|
---|
158 | * bForceUpdate - forces dispatching of geometry info even if it's unchanged
|
---|
159 | */
|
---|
160 | GLboolean stubUpdateWindowGeometry(WindowInfo *pWindow, GLboolean bForceUpdate)
|
---|
161 | {
|
---|
162 | int winX, winY;
|
---|
163 | unsigned int winW, winH;
|
---|
164 | GLboolean res = GL_FALSE;
|
---|
165 |
|
---|
166 | CRASSERT(pWindow);
|
---|
167 |
|
---|
168 | stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
|
---|
169 |
|
---|
170 | /* @todo remove "if (winW && winH)"?*/
|
---|
171 | if (winW && winH) {
|
---|
172 | if (stub.trackWindowSize) {
|
---|
173 | if (bForceUpdate || winW != pWindow->width || winH != pWindow->height) {
|
---|
174 | crDebug("Dispatched WindowSize (%i)", pWindow->spuWindow);
|
---|
175 | stub.spuDispatch.WindowSize(pWindow->spuWindow, winW, winH);
|
---|
176 | pWindow->width = winW;
|
---|
177 | pWindow->height = winH;
|
---|
178 | res = GL_TRUE;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | if (stub.trackWindowPos) {
|
---|
182 | if (bForceUpdate || winX != pWindow->x || winY != pWindow->y) {
|
---|
183 | crDebug("Dispatched WindowPosition (%i)", pWindow->spuWindow);
|
---|
184 | stub.spuDispatch.WindowPosition(pWindow->spuWindow, winX, winY);
|
---|
185 | pWindow->x = winX;
|
---|
186 | pWindow->y = winY;
|
---|
187 | res = GL_TRUE;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return res;
|
---|
193 | }
|
---|
194 |
|
---|
195 | #ifdef WINDOWS
|
---|
196 | /*
|
---|
197 | * Updates visible regions for given spu window.
|
---|
198 | * Returns GL_TRUE if regions changed since last call, GL_FALSE overwise.
|
---|
199 | */
|
---|
200 | GLboolean stubUpdateWindowVisibileRegions(WindowInfo *pWindow)
|
---|
201 | {
|
---|
202 | HRGN hVisRgn;
|
---|
203 | HWND hwnd;
|
---|
204 | DWORD dwCount;
|
---|
205 | LPRGNDATA lpRgnData;
|
---|
206 | POINT pt;
|
---|
207 | int iret;
|
---|
208 |
|
---|
209 | if (!pWindow) return GL_FALSE;
|
---|
210 | hwnd = WindowFromDC(pWindow->drawable);
|
---|
211 | if (!hwnd) return GL_FALSE;
|
---|
212 |
|
---|
213 |
|
---|
214 | hVisRgn = CreateRectRgn(0,0,0,0);
|
---|
215 | iret = GetRandomRgn(pWindow->drawable, hVisRgn, SYSRGN);
|
---|
216 |
|
---|
217 | if (iret==1)
|
---|
218 | {
|
---|
219 | /*@todo check win95/win98 here, as rects should be already in client space there*/
|
---|
220 | /* Convert screen related rectangles to client related rectangles */
|
---|
221 | pt.x = 0;
|
---|
222 | pt.y = 0;
|
---|
223 | ScreenToClient(hwnd, &pt);
|
---|
224 | OffsetRgn(hVisRgn, pt.x, pt.y);
|
---|
225 |
|
---|
226 | /*
|
---|
227 | dwCount = GetRegionData(hVisRgn, 0, NULL);
|
---|
228 | lpRgnData = crAlloc(dwCount);
|
---|
229 | crDebug("GetRandomRgn returned 1, dwCount=%d", dwCount);
|
---|
230 | GetRegionData(hVisRgn, dwCount, lpRgnData);
|
---|
231 | crDebug("Region consists of %d rects", lpRgnData->rdh.nCount);
|
---|
232 |
|
---|
233 | pRects = (RECT*) lpRgnData->Buffer;
|
---|
234 | for (i=0; i<lpRgnData->rdh.nCount; ++i)
|
---|
235 | {
|
---|
236 | crDebug("Rgn[%d] = (%d, %d, %d, %d)", i, pRects[i].left, pRects[i].top, pRects[i].right, pRects[i].bottom);
|
---|
237 | }
|
---|
238 | crFree(lpRgnData);
|
---|
239 | */
|
---|
240 |
|
---|
241 | if (pWindow->hVisibleRegion==INVALID_HANDLE_VALUE
|
---|
242 | || !EqualRgn(pWindow->hVisibleRegion, hVisRgn))
|
---|
243 | {
|
---|
244 | DeleteObject(pWindow->hVisibleRegion);
|
---|
245 | pWindow->hVisibleRegion = hVisRgn;
|
---|
246 |
|
---|
247 | dwCount = GetRegionData(hVisRgn, 0, NULL);
|
---|
248 | lpRgnData = crAlloc(dwCount);
|
---|
249 |
|
---|
250 | if (lpRgnData)
|
---|
251 | {
|
---|
252 | GetRegionData(hVisRgn, dwCount, lpRgnData);
|
---|
253 | crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
|
---|
254 | stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
|
---|
255 | crFree(lpRgnData);
|
---|
256 | return GL_TRUE;
|
---|
257 | }
|
---|
258 | else crWarning("GetRegionData failed, VisibleRegions update failed");
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | DeleteObject(hVisRgn);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | crWarning("GetRandomRgn returned (%d) instead of (1), VisibleRegions update failed", iret);
|
---|
268 | DeleteObject(hVisRgn);
|
---|
269 | }
|
---|
270 |
|
---|
271 | return GL_FALSE;
|
---|
272 | }
|
---|
273 |
|
---|
274 | static void stubCBCheckWindowsInfo(unsigned long key, void *data1, void *data2)
|
---|
275 | {
|
---|
276 | WindowInfo *winInfo = (WindowInfo *) data1;
|
---|
277 | CWPRETSTRUCT *pMsgInfo = (PCWPRETSTRUCT) data2;
|
---|
278 |
|
---|
279 | (void) key;
|
---|
280 |
|
---|
281 | if (winInfo && pMsgInfo && winInfo->type == CHROMIUM)
|
---|
282 | {
|
---|
283 | switch (pMsgInfo->message)
|
---|
284 | {
|
---|
285 | case WM_MOVING:
|
---|
286 | case WM_SIZING:
|
---|
287 | case WM_MOVE:
|
---|
288 | case WM_CREATE:
|
---|
289 | case WM_SIZE:
|
---|
290 | {
|
---|
291 | GLboolean changed = stub.trackWindowVisibleRgn && stubUpdateWindowVisibileRegions(winInfo);
|
---|
292 |
|
---|
293 | if (stubUpdateWindowGeometry(winInfo, GL_FALSE) || changed)
|
---|
294 | {
|
---|
295 | stub.spuDispatch.Flush();
|
---|
296 | }
|
---|
297 | break;
|
---|
298 | }
|
---|
299 |
|
---|
300 | case WM_SHOWWINDOW:
|
---|
301 | case WM_ACTIVATEAPP:
|
---|
302 | case WM_PAINT:
|
---|
303 | case WM_NCPAINT:
|
---|
304 | case WM_NCACTIVATE:
|
---|
305 | case WM_ERASEBKGND:
|
---|
306 | {
|
---|
307 | if (stub.trackWindowVisibleRgn && stubUpdateWindowVisibileRegions(winInfo))
|
---|
308 | {
|
---|
309 | stub.spuDispatch.Flush();
|
---|
310 | }
|
---|
311 | break;
|
---|
312 | }
|
---|
313 |
|
---|
314 | default:
|
---|
315 | {
|
---|
316 | if (stub.trackWindowVisibleRgn && stubUpdateWindowVisibileRegions(winInfo))
|
---|
317 | {
|
---|
318 | crDebug("Visibility info updated due to unknown hooked message (%d)", pMsgInfo->message);
|
---|
319 | stub.spuDispatch.Flush();
|
---|
320 | }
|
---|
321 | break;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | LRESULT CALLBACK stubCBWindowMessageHookProc(int nCode, WPARAM wParam, LPARAM lParam)
|
---|
328 | {
|
---|
329 | CWPRETSTRUCT *pMsgInfo = (PCWPRETSTRUCT) lParam;
|
---|
330 |
|
---|
331 | if (nCode>=0 && pMsgInfo)
|
---|
332 | {
|
---|
333 | #ifdef CHROMIUM_THREADSAFE
|
---|
334 | crLockMutex(&stub.mutex);
|
---|
335 | #endif
|
---|
336 | switch (pMsgInfo->message)
|
---|
337 | {
|
---|
338 | case WM_MOVING:
|
---|
339 | case WM_SIZING:
|
---|
340 | case WM_MOVE:
|
---|
341 | case WM_ACTIVATEAPP:
|
---|
342 | case WM_NCPAINT:
|
---|
343 | case WM_NCACTIVATE:
|
---|
344 | case WM_ERASEBKGND:
|
---|
345 | case WM_CREATE:
|
---|
346 | case WM_SIZE:
|
---|
347 | case WM_SHOWWINDOW:
|
---|
348 | {
|
---|
349 | crHashtableWalk(stub.windowTable, stubCBCheckWindowsInfo, (void *) lParam);
|
---|
350 | break;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* @todo remove it*/
|
---|
354 | default:
|
---|
355 | {
|
---|
356 | /*crDebug("hook: unknown message (%d)", pMsgInfo->message);*/
|
---|
357 | crHashtableWalk(stub.windowTable, stubCBCheckWindowsInfo, (void *) lParam);
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | #ifdef CHROMIUM_THREADSAFE
|
---|
362 | crUnlockMutex(&stub.mutex);
|
---|
363 | #endif
|
---|
364 | }
|
---|
365 |
|
---|
366 | return CallNextHookEx(stub.hMessageHook, nCode, wParam, lParam);
|
---|
367 | }
|
---|
368 |
|
---|
369 | void stubInstallWindowMessageHook()
|
---|
370 | {
|
---|
371 | stub.hMessageHook = SetWindowsHookEx(WH_CALLWNDPROCRET, stubCBWindowMessageHookProc, 0, crThreadID());
|
---|
372 |
|
---|
373 | if (!stub.hMessageHook)
|
---|
374 | crWarning("Window message hook install failed! (not fatal)");
|
---|
375 | }
|
---|
376 |
|
---|
377 | void stubUninstallWindowMessageHook()
|
---|
378 | {
|
---|
379 | if (stub.hMessageHook)
|
---|
380 | UnhookWindowsHookEx(stub.hMessageHook);
|
---|
381 | }
|
---|
382 | #endif
|
---|