VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/dlm/dlm.c@ 65641

Last change on this file since 65641 was 56922, checked in by vboxsync, 9 years ago

3D: Display List: Expando SPU and DLM module code significantly reworked (diff unreadable); first revision when glxgears can be saved and successfully restored; more testing and debugging needed; prelimenary tested ane enabled for Mac hosts only.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.7 KB
Line 
1/* $Id: dlm.c 56922 2015-07-13 10:23:52Z vboxsync $ */
2
3#include <float.h>
4#include "cr_dlm.h"
5#include "cr_mem.h"
6#include "dlm.h"
7
8/**
9 * \mainpage Dlm
10 *
11 * \section DlmIntroduction Introduction
12 *
13 * Chromium consists of all the top-level files in the cr
14 * directory. The dlm module basically takes care of API dispatch,
15 * and OpenGL state management.
16 *
17 */
18
19/**
20 * Module globals: the current DLM state, bound either to each thread, or
21 * to a global.
22 */
23#ifdef CHROMIUM_THREADSAFE
24CRtsd CRDLMTSDKey;
25#else
26CRDLMContextState *CRDLMCurrentState = NULL;
27#endif
28
29#define MIN(a,b) ((a)<(b)?(a):(b))
30
31
32/*************************************************************************/
33
34#ifdef CHROMIUM_THREADSAFE
35/**
36 * This is the thread-specific destructor function for the
37 * data used in the DLM. It's very simple: if a thread exits
38 * that has DLM-specific data, the data represents the listState
39 * for the thread. All data and buffers associated with the list
40 * can be deleted, and the structure itself can be freed.
41 *
42 * Most Chromium threads don't have such things; but then,
43 * if a thread dies elsewhere in Chromium, huge buffers
44 * of information won't still be floating around in
45 * unrecoverable allocated areas, either.
46 */
47static void threadDestructor(void *tsd)
48{
49 CRDLMContextState *listState = (CRDLMContextState *)tsd;
50
51 if (listState)
52 {
53 //if (listState->currentListInfo)
54 // crdlm_free_list(listState->currentListInfo);
55
56 crFree(listState);
57 }
58}
59#endif
60
61/**
62 * This function creates and initializes a new display list
63 * manager. It returns a pointer to the manager, or NULL in
64 * the case of insufficient memory. The dispatch table pointer
65 * is passed in to allow the utilities to muck with the table
66 * to gain functional control when GL calls are made.
67 */
68CRDLM DLM_APIENTRY *crDLMNewDLM(unsigned int userConfigSize, const CRDLMConfig *userConfig)
69{
70 CRDLM *dlm;
71
72 /* This is the default configuration. We'll overwrite it later
73 * with user-supplied configuration information.
74 */
75 CRDLMConfig config = {
76 CRDLM_DEFAULT_BUFFERSIZE,
77 };
78
79 dlm = crAlloc(sizeof(*dlm));
80 if (!dlm) {
81 return NULL;
82 }
83
84 /* Start off by initializing all entries that require further
85 * memory allocation, so we can free up all the memory if there's
86 * a problem.
87 */
88 if (!(dlm->displayLists = crAllocHashtable())) {
89 crFree(dlm);
90 return NULL;
91 }
92
93 /* The creator counts as the first user. */
94 dlm->userCount = 1;
95
96#ifdef CHROMIUM_THREADSAFE
97 /* This mutex ensures that only one thread is changing the displayLists
98 * hash at a time. Note that we may also need a mutex to guarantee that
99 * the hash is not changed by one thread while another thread is
100 * traversing it; this issue has not yet been resolved.
101 */
102 crInitMutex(&(dlm->dlMutex));
103
104 /* Although the thread-specific data (TSD) functions will initialize
105 * the thread key themselves when needed, those functions do not allow
106 * us to specify a thread destructor. Since a thread could potentially
107 * exit with considerable memory allocated (e.g. if a thread exits
108 * after it has issued NewList but before EndList, and while there
109 * are considerable content buffers allocated), I do the initialization
110 * myself, in order to be able to reclaim those resources if a thread
111 * exits.
112 */
113 crInitTSDF(&(dlm->tsdKey), threadDestructor);
114 crInitTSD(&CRDLMTSDKey);
115#endif
116
117 /* Copy over any appropriate configuration values */
118 if (userConfig != NULL) {
119 /* Copy over as much configuration information as is provided.
120 * Note that if the CRDLMConfig structure strictly grows, this
121 * allows forward compatability - routines compiled with
122 * older versions of the structure will only initialize that
123 * section of the structure that they know about.
124 */
125 crMemcpy((void *)&config, (void *) userConfig,
126 MIN(userConfigSize, sizeof(config)));
127 }
128 dlm->bufferSize = config.bufferSize;
129
130 /* Return the pointer to the newly-allocated display list manager */
131 return dlm;
132}
133
134void DLM_APIENTRY crDLMUseDLM(CRDLM *dlm)
135{
136 DLM_LOCK(dlm);
137 dlm->userCount++;
138 DLM_UNLOCK(dlm);
139}
140
141/**
142 * This routine is called when a context or thread is done with a DLM.
143 * It maintains an internal count of users, and will only actually destroy
144 * itself when no one is still using the DLM.
145 */
146void DLM_APIENTRY crDLMFreeDLM(CRDLM *dlm, SPUDispatchTable *dispatchTable)
147{
148 /* We're about to change the displayLists hash; lock it first */
149 DLM_LOCK(dlm)
150
151 /* Decrement the user count. If the user count has gone to
152 * 0, then free the rest of the DLM. Otherwise, other
153 * contexts or threads are still using this DLM; keep
154 * it around.
155 */
156 dlm->userCount--;
157 if (dlm->userCount == 0) {
158
159 crFreeHashtableEx(dlm->displayLists, crdlmFreeDisplayListResourcesCb, dispatchTable);
160 dlm->displayLists = NULL;
161
162 /* Must unlock before freeing the mutex */
163 DLM_UNLOCK(dlm)
164
165#ifdef CHROMIUM_THREADSAFE
166 /* We release the mutex here; we really should delete the
167 * thread data key, but there's no utility in Chromium to
168 * do this.
169 *
170 * Note that, should one thread release the entire DLM
171 * while other threads still believe they are using it,
172 * any other threads that have current display lists (i.e.
173 * have issued glNewList more recently than glEndList)
174 * will be unable to reclaim their (likely very large)
175 * content buffers, as there will be no way to reclaim
176 * the thread-specific data.
177 *
178 * On the other hand, if one thread really does release
179 * the DLM while other threads still believe they are
180 * using it, unreclaimed memory is the least of the
181 * application's problems...
182 */
183 crFreeMutex(&(dlm->dlMutex));
184
185 /* We free the TSD key here as well. Note that this will
186 * strand any threads that still have thread-specific data
187 * tied to this key; but as stated above, if any threads
188 * still do have thread-specific data attached to this DLM,
189 * they're in big trouble anyway.
190 */
191 crFreeTSD(&(dlm->tsdKey));
192 crFreeTSD(&CRDLMTSDKey);
193#endif
194
195 /* Free the master record, and we're all done. */
196 crFree(dlm);
197 }
198 else {
199 /* We're keeping the DLM around for other users. Unlock it,
200 * but retain its memory and display lists.
201 */
202 DLM_UNLOCK(dlm)
203 }
204}
205
206/**
207 * The actual run-time state of a DLM is bound to a context
208 * (because each context can be used by at most one thread at
209 * a time, and a thread can only use one context at a time,
210 * while multiple contexts can use the same DLM).
211 * This creates the structure required to hold the state, and
212 * returns it to the caller, who should store it with any other
213 * context-specific information.
214 */
215
216CRDLMContextState DLM_APIENTRY *crDLMNewContext(CRDLM *dlm)
217{
218 CRDLMContextState *state;
219
220 /* Get a record for our own internal state structure */
221 state = (CRDLMContextState *)crAlloc(sizeof(CRDLMContextState));
222 if (!state) {
223 return NULL;
224 }
225
226 state->dlm = dlm;
227 state->currentListIdentifier = 0;
228 state->currentListInfo = NULL;
229 state->currentListMode = GL_FALSE;
230 state->listBase = 0;
231
232 /* Increment the use count of the DLM provided. This guarantees that
233 * the DLM won't be released until all the contexts have released it.
234 */
235 crDLMUseDLM(dlm);
236
237 return state;
238}
239
240
241/**
242 * This routine should be called when a MakeCurrent changes the current
243 * context. It sets the thread data (or global data, in an unthreaded
244 * environment) appropriately; this in turn changes the behavior of
245 * the installed DLM API functions.
246 */
247void DLM_APIENTRY crDLMSetCurrentState(CRDLMContextState *state)
248{
249 CRDLMContextState *currentState = CURRENT_STATE();
250 if (currentState != state) {
251 SET_CURRENT_STATE(state);
252 }
253}
254
255CRDLMContextState DLM_APIENTRY *crDLMGetCurrentState(void)
256{
257 return CURRENT_STATE();
258}
259
260/**
261 * This routine, of course, is used to release a DLM context when it
262 * is no longer going to be used.
263 */
264
265void DLM_APIENTRY crDLMFreeContext(CRDLMContextState *state, SPUDispatchTable *dispatchTable)
266{
267 CRDLMContextState *listState = CURRENT_STATE();
268
269 /* If we're currently using this context, release it first */
270 if (listState == state)
271 crDLMSetCurrentState(NULL);
272
273 /* Try to free the DLM. This will either decrement the use count,
274 * or will actually free the DLM, if we were the last user.
275 */
276 crDLMFreeDLM(state->dlm, dispatchTable);
277 state->dlm = NULL;
278
279 /* If any buffers still remain (e.g. because there was an open
280 * display list), remove those as well.
281 */
282 if (state->currentListInfo)
283 {
284 crdlmFreeDisplayListResourcesCb((void *)state->currentListInfo, (void *)dispatchTable);
285 state->currentListInfo = NULL;
286 }
287 state->currentListIdentifier = 0;
288
289 /* Free the state record itself */
290 crFree(state);
291}
292
293
294/**
295 * This function can be used if the caller wishes to free up the
296 * potentially considerable resources used to store the display list
297 * content, without losing the rest of the display list management.
298 * For one example, consider an SPU that conditionally sends its
299 * input stream to multiple servers. It could broadcast all display
300 * lists to all servers, or it could only send display lists to servers
301 * that need them. After all servers have the display list, the SPU
302 * may wish to release the resources used to manage the content.
303 */
304CRDLMError DLM_APIENTRY crDLMDeleteListContent(CRDLM *dlm, unsigned long listIdentifier)
305{
306 DLMListInfo *listInfo;
307 DLMInstanceList *instance;
308
309 listInfo = (DLMListInfo *) crHashtableSearch(dlm->displayLists, listIdentifier);
310 if (listInfo && (instance = listInfo->first)) {
311 while (instance) {
312 DLMInstanceList *nextInstance;
313 nextInstance = instance->next;
314 crFree(instance);
315 instance = nextInstance;
316 }
317 listInfo->first = listInfo->last = NULL;
318 }
319 return GL_NO_ERROR;
320}
321
322/**
323 *
324 * Playback/execute a list.
325 * dlm - the display list manager context
326 * listIdentifier - the display list ID (as specified by app) to playback
327 * dispatchTable - the GL dispatch table to jump through as we execute commands
328 */
329void DLM_APIENTRY crDLMReplayDLMList(CRDLM *dlm, unsigned long listIdentifier, SPUDispatchTable *dispatchTable)
330{
331 DLMListInfo *listInfo;
332
333 listInfo = (DLMListInfo *)crHashtableSearch(dlm->displayLists, listIdentifier);
334 if (listInfo) {
335 DLMInstanceList *instance = listInfo->first;
336 while (instance) {
337 /* mutex, to make sure another thread doesn't change the list? */
338 /* For now, leave it alone. */
339 (*instance->execute)(instance, dispatchTable);
340 instance = instance->next;
341 }
342 }
343}
344
345/* Playback/execute a list in the current DLM */
346void DLM_APIENTRY crDLMReplayList(unsigned long listIdentifier, SPUDispatchTable *dispatchTable)
347{
348 CRDLMContextState *listState = CURRENT_STATE();
349 if (listState)
350 crDLMReplayDLMList(listState->dlm, listIdentifier, dispatchTable);
351}
352
353/*
354 * Playback/execute the state changing portions of a list.
355 * dlm - the display list manager context
356 * listIdentifier - the display list ID (as specified by app) to playback
357 * dispatchTable - the GL dispatch table to jump through as we execute commands
358 */
359void DLM_APIENTRY crDLMReplayDLMListState(CRDLM *dlm, unsigned long listIdentifier, SPUDispatchTable *dispatchTable)
360{
361 DLMListInfo *listInfo;
362
363 listInfo = (DLMListInfo *)crHashtableSearch(dlm->displayLists, listIdentifier);
364 if (listInfo) {
365 DLMInstanceList *instance = listInfo->stateFirst;
366 while (instance) {
367 /* mutex, to make sure another thread doesn't change the list? */
368 /* For now, leave it alone. */
369 (*instance->execute)(instance, dispatchTable);
370 instance = instance->stateNext;
371 }
372 }
373}
374
375void DLM_APIENTRY crDLMReplayListState(unsigned long listIdentifier, SPUDispatchTable *dispatchTable)
376{
377 CRDLMContextState *listState = CURRENT_STATE();
378 if (listState)
379 crDLMReplayDLMListState(listState->dlm, listIdentifier, dispatchTable);
380}
381
382/* This is a switch statement that lists every "type" value valid for a
383 * glCallLists() function call, with code for decoding the subsequent
384 * values correctly. It uses the current value of the EXPAND() macro,
385 * which must expand into an appropriate action to be taken.
386 * Its codification here allows for multiple uses.
387 */
388#define CALL_LISTS_SWITCH(type, defaultAction) \
389 switch (type) {\
390 EXPAND(GL_BYTE, GLbyte *, *p, p++)\
391 EXPAND(GL_UNSIGNED_BYTE, GLubyte *, *p, p++)\
392 EXPAND(GL_SHORT, GLshort *, *p, p++)\
393 EXPAND(GL_UNSIGNED_SHORT, GLushort *, *p, p++)\
394 EXPAND(GL_INT, GLint *, *p, p++)\
395 EXPAND(GL_FLOAT, GLfloat *, *p, p++)\
396 EXPAND(GL_2_BYTES, unsigned char *, 256*p[0] + p[1], p += 2)\
397 EXPAND(GL_3_BYTES, unsigned char *, 65536*p[0] + 256*p[1] + p[2], p += 3)\
398 EXPAND(GL_4_BYTES, unsigned char *, 16777216*p[0] + 65536*p[1] + 256*p[2] + p[3], p += 4)\
399 default:\
400 defaultAction;\
401 }
402
403void DLM_APIENTRY crDLMReplayDLMLists(CRDLM *dlm, GLsizei n, GLenum type, const GLvoid * lists, SPUDispatchTable *dispatchTable)
404{
405 unsigned long listId;
406 CRDLMContextState *listState = CURRENT_STATE();
407
408#define EXPAND(TYPENAME, TYPE, REFERENCE, INCREMENT) \
409 case TYPENAME: {\
410 TYPE p = (TYPE)lists;\
411 while (n--) {\
412 listId = listState->listBase + (unsigned long) (REFERENCE);\
413 crDLMReplayDLMList(dlm, listId, dispatchTable);\
414 INCREMENT;\
415 }\
416 break;\
417 }
418
419 CALL_LISTS_SWITCH(type, break)
420#undef EXPAND
421
422}
423
424void DLM_APIENTRY crDLMReplayLists(GLsizei n, GLenum type, const GLvoid * lists, SPUDispatchTable *dispatchTable)
425{
426 CRDLMContextState *listState = CURRENT_STATE();
427 if (listState) {
428 crDLMReplayDLMLists(listState->dlm, n, type, lists, dispatchTable);
429 }
430}
431
432void DLM_APIENTRY crDLMReplayDLMListsState(CRDLM *dlm, GLsizei n, GLenum type, const GLvoid * lists, SPUDispatchTable *dispatchTable)
433{
434 unsigned long listId;
435 CRDLMContextState *listState = CURRENT_STATE();
436
437#define EXPAND(TYPENAME, TYPE, REFERENCE, INCREMENT) \
438 case TYPENAME: {\
439 TYPE p = (TYPE)lists;\
440 while (n--) {\
441 listId = listState->listBase + (unsigned long) (REFERENCE);\
442 crDLMReplayDLMListState(dlm, listId, dispatchTable);\
443 INCREMENT;\
444 }\
445 break;\
446 }
447
448 CALL_LISTS_SWITCH(type, break)
449#undef EXPAND
450
451}
452
453void DLM_APIENTRY crDLMReplayListsState(GLsizei n, GLenum type, const GLvoid * lists, SPUDispatchTable *dispatchTable)
454{
455 CRDLMContextState *listState = CURRENT_STATE();
456 if (listState) {
457 crDLMReplayDLMListsState(listState->dlm, n, type, lists, dispatchTable);
458 }
459}
460
461/* When we compiled the display list, we packed all pixel data
462 * tightly. When we execute the display list, we have to make
463 * sure that the client state reflects that the pixel data is
464 * tightly packed, or it will be interpreted incorrectly.
465 */
466void DLM_APIENTRY crDLMSetupClientState(SPUDispatchTable *dispatchTable)
467{
468 dispatchTable->PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
469 dispatchTable->PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
470 dispatchTable->PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
471 dispatchTable->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
472}
473
474void DLM_APIENTRY crDLMRestoreClientState(CRClientState *clientState, SPUDispatchTable *dispatchTable)
475{
476 if (clientState) {
477 dispatchTable->PixelStorei(GL_UNPACK_ROW_LENGTH, clientState->unpack.rowLength);
478 dispatchTable->PixelStorei(GL_UNPACK_SKIP_PIXELS, clientState->unpack.skipPixels);
479 dispatchTable->PixelStorei(GL_UNPACK_SKIP_ROWS, clientState->unpack.skipRows);
480 dispatchTable->PixelStorei(GL_UNPACK_ALIGNMENT, clientState->unpack.alignment);
481 }
482}
483
484void DLM_APIENTRY crDLMSendDLMList(CRDLM *dlm, unsigned long listIdentifier,
485 SPUDispatchTable *dispatchTable)
486{
487 dispatchTable->NewList(listIdentifier, GL_COMPILE);
488 crDLMReplayDLMList(dlm, listIdentifier, dispatchTable);
489 dispatchTable->EndList();
490}
491
492void DLM_APIENTRY crDLMSendList(unsigned long listIdentifier, SPUDispatchTable *dispatchTable)
493{
494 CRDLMContextState *listState = CURRENT_STATE();
495 if (listState) {
496 crDLMSendDLMList(listState->dlm, listIdentifier, dispatchTable);
497 }
498}
499
500struct sendListsCallbackParms {
501 CRDLM *dlm;
502 SPUDispatchTable *dispatchTable;
503};
504
505static void sendListsCallback(unsigned long key, void *data, void *dataPtr2)
506{
507 struct sendListsCallbackParms *parms = (struct sendListsCallbackParms *)dataPtr2;
508
509 crDLMSendDLMList(parms->dlm, key, parms->dispatchTable);
510}
511
512void DLM_APIENTRY crDLMSendAllDLMLists(CRDLM *dlm, SPUDispatchTable *dispatchTable)
513{
514 struct sendListsCallbackParms parms;
515
516 /* This is how we pass our parameter information to the callback routine -
517 * through a pointer to this local structure.
518 */
519 parms.dlm = dlm;
520 parms.dispatchTable = dispatchTable;
521
522 crHashtableWalk(dlm->displayLists, sendListsCallback, (void *)&parms);
523}
524
525void DLM_APIENTRY crDLMSendAllLists(SPUDispatchTable *dispatchTable)
526{
527 CRDLMContextState *listState = CURRENT_STATE();
528 if (listState) {
529 crDLMSendAllDLMLists(listState->dlm, dispatchTable);
530 }
531}
532
533/** Another clever callback arrangement to get the desired data. */
534struct getRefsCallbackParms {
535 int remainingOffset;
536 int remainingCount;
537 unsigned int *buffer;
538 int totalCount;
539};
540
541/*
542 * Return id of list currently being compiled. Returns 0 of there's no
543 * current DLM state, or if no list is being compiled.
544 */
545GLuint DLM_APIENTRY crDLMGetCurrentList(void)
546{
547 CRDLMContextState *listState = CURRENT_STATE();
548 return listState ? listState->currentListIdentifier : 0;
549}
550
551/*
552 * Return mode of list currently being compiled. Should be
553 * GL_FALSE if no list is being compiled, or GL_COMPILE if a
554 * list is being compiled but not executed, or GL_COMPILE_AND_EXECUTE
555 * if a list is being compiled and executed.
556 */
557GLenum DLM_APIENTRY crDLMGetCurrentMode(void)
558{
559 CRDLMContextState *listState = CURRENT_STATE();
560 return listState ? listState->currentListMode : 0;
561}
562
563
564static CRDLMErrorCallback ErrorCallback = NULL;
565
566void DLM_APIENTRY crDLMErrorFunction(CRDLMErrorCallback callback)
567{
568 ErrorCallback = callback;
569}
570
571void crdlm_error(int line, const char *file, GLenum error, const char *info)
572{
573 if (ErrorCallback)
574 (*ErrorCallback)(line, file, error, info);
575}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette