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_mem.h"
|
---|
8 | #include "cr_environment.h"
|
---|
9 | #include "cr_string.h"
|
---|
10 | #include "cr_dll.h"
|
---|
11 | #include "cr_error.h"
|
---|
12 | #include "cr_spu.h"
|
---|
13 |
|
---|
14 |
|
---|
15 | #include <iprt/param.h>
|
---|
16 | #include <iprt/string.h>
|
---|
17 | #include <iprt/path.h>
|
---|
18 |
|
---|
19 | #include <stdio.h>
|
---|
20 |
|
---|
21 | #ifdef WINDOWS
|
---|
22 | #ifdef VBOX_WDDM_WOW64
|
---|
23 | #define DLL_SUFFIX "-x86.dll"
|
---|
24 | #else
|
---|
25 | #define DLL_SUFFIX ".dll"
|
---|
26 | #endif
|
---|
27 | #define DLL_PREFIX "VBoxOGL"
|
---|
28 | #define snprintf _snprintf
|
---|
29 | #elif defined(DARWIN)
|
---|
30 | #define DLL_SUFFIX ".dylib"
|
---|
31 | #define DLL_PREFIX "VBoxOGL"
|
---|
32 | /*
|
---|
33 | #define DLL_SUFFIX ".bundle"
|
---|
34 | #define DLL_PREFIX ""
|
---|
35 | */
|
---|
36 | #else
|
---|
37 | #ifdef AIX
|
---|
38 | #define DLL_SUFFIX ".o"
|
---|
39 | #define DLL_PREFIX "VBoxOGL"
|
---|
40 | #else
|
---|
41 | #define DLL_SUFFIX ".so"
|
---|
42 | #define DLL_PREFIX "VBoxOGL"
|
---|
43 | #endif
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | extern void __buildDispatch( SPU *spu );
|
---|
47 |
|
---|
48 | static char *__findDLL( char *name, char *dir )
|
---|
49 | {
|
---|
50 | static char path[8092];
|
---|
51 |
|
---|
52 | if (!dir)
|
---|
53 | {
|
---|
54 | #if defined(DARWIN)
|
---|
55 | char szSharedLibPath[8092];
|
---|
56 | int rc = RTPathAppPrivateArch (szSharedLibPath, sizeof(szSharedLibPath));
|
---|
57 | if (RT_SUCCESS(rc))
|
---|
58 | sprintf ( path, "%s/%s%sspu%s", szSharedLibPath, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
59 | else
|
---|
60 | #endif /* DARWIN */
|
---|
61 | #ifdef VBOX
|
---|
62 | snprintf ( path, sizeof(path), "%s%sspu%s", DLL_PREFIX, name, DLL_SUFFIX );
|
---|
63 | #else
|
---|
64 | sprintf ( path, "%s%sspu%s", DLL_PREFIX, name, DLL_SUFFIX );
|
---|
65 | #endif
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | #ifdef VBOX
|
---|
70 | snprintf ( path, sizeof(path), "%s/%s%sspu%s", dir, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
71 | #else
|
---|
72 | sprintf ( path, "%s/%s%sspu%s", dir, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 | return path;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Load a single SPU from disk and initialize it. Is there any reason
|
---|
80 | * to export this from the SPU loader library? */
|
---|
81 |
|
---|
82 | SPU * crSPULoad( SPU *child, int id, char *name, char *dir, void *server )
|
---|
83 | {
|
---|
84 | SPU *the_spu;
|
---|
85 | char *path;
|
---|
86 |
|
---|
87 | CRASSERT( name != NULL );
|
---|
88 |
|
---|
89 | the_spu = (SPU*)crAlloc( sizeof( *the_spu ) );
|
---|
90 | the_spu->id = id;
|
---|
91 | the_spu->privatePtr = NULL;
|
---|
92 | path = __findDLL( name, dir );
|
---|
93 | the_spu->dll = crDLLOpen( path, 0/*resolveGlobal*/ );
|
---|
94 | the_spu->entry_point =
|
---|
95 | (SPULoadFunction) crDLLGetNoError( the_spu->dll, SPU_ENTRY_POINT_NAME );
|
---|
96 | if (!the_spu->entry_point)
|
---|
97 | {
|
---|
98 | crError( "Couldn't load the SPU entry point \"%s\" from SPU \"%s\"!",
|
---|
99 | SPU_ENTRY_POINT_NAME, name );
|
---|
100 | crSPUUnloadChain(the_spu);
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* This basically calls the SPU's SPULoad() function */
|
---|
105 | if (!the_spu->entry_point( &(the_spu->name), &(the_spu->super_name),
|
---|
106 | &(the_spu->init), &(the_spu->self),
|
---|
107 | &(the_spu->cleanup),
|
---|
108 | &(the_spu->options),
|
---|
109 | &(the_spu->spu_flags)) )
|
---|
110 | {
|
---|
111 | crError( "I found the SPU \"%s\", but loading it failed!", name );
|
---|
112 | crSPUUnloadChain(the_spu);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 | #ifdef IN_GUEST
|
---|
116 | if (crStrcmp(the_spu->name,"error"))
|
---|
117 | {
|
---|
118 | /* the default super/base class for an SPU is the error SPU */
|
---|
119 | if (the_spu->super_name == NULL)
|
---|
120 | {
|
---|
121 | the_spu->super_name = "error";
|
---|
122 | }
|
---|
123 | the_spu->superSPU = crSPULoad( child, id, the_spu->super_name, dir, server );
|
---|
124 | }
|
---|
125 | #else
|
---|
126 | if (crStrcmp(the_spu->name,"hosterror"))
|
---|
127 | {
|
---|
128 | /* the default super/base class for an SPU is the error SPU */
|
---|
129 | if (the_spu->super_name == NULL)
|
---|
130 | {
|
---|
131 | the_spu->super_name = "hosterror";
|
---|
132 | }
|
---|
133 | the_spu->superSPU = crSPULoad( child, id, the_spu->super_name, dir, server );
|
---|
134 | }
|
---|
135 | #endif
|
---|
136 | else
|
---|
137 | {
|
---|
138 | the_spu->superSPU = NULL;
|
---|
139 | }
|
---|
140 | crDebug("Initializing %s SPU", name);
|
---|
141 | the_spu->function_table = the_spu->init( id, child, the_spu, 0, 1 );
|
---|
142 | if (!the_spu->function_table) {
|
---|
143 | crDebug("Failed to init %s SPU", name);
|
---|
144 | crSPUUnloadChain(the_spu);
|
---|
145 | return NULL;
|
---|
146 | }
|
---|
147 | __buildDispatch( the_spu );
|
---|
148 | /*crDebug( "initializing dispatch table %p (for SPU %s)", (void*)&(the_spu->dispatch_table), name );*/
|
---|
149 | crSPUInitDispatchTable( &(the_spu->dispatch_table) );
|
---|
150 | /*crDebug( "Done initializing the dispatch table for SPU %s, calling the self function", name );*/
|
---|
151 |
|
---|
152 | the_spu->dispatch_table.server = server;
|
---|
153 | the_spu->self( &(the_spu->dispatch_table) );
|
---|
154 | /*crDebug( "Done with the self function" );*/
|
---|
155 |
|
---|
156 | return the_spu;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Load the entire chain of SPUs and initialize all of them.
|
---|
161 | * This function returns the first one in the chain.
|
---|
162 | */
|
---|
163 | SPU *
|
---|
164 | crSPULoadChain( int count, int *ids, char **names, char *dir, void *server )
|
---|
165 | {
|
---|
166 | int i;
|
---|
167 | SPU *child_spu = NULL;
|
---|
168 | CRASSERT( count > 0 );
|
---|
169 |
|
---|
170 | for (i = count-1 ; i >= 0 ; i--)
|
---|
171 | {
|
---|
172 | int spu_id = ids[i];
|
---|
173 | char *spu_name = names[i];
|
---|
174 | SPU *the_spu, *temp;
|
---|
175 |
|
---|
176 | /* This call passes the previous version of spu, which is the SPU's
|
---|
177 | * "child" in this chain. */
|
---|
178 |
|
---|
179 | the_spu = crSPULoad( child_spu, spu_id, spu_name, dir, server );
|
---|
180 | if (!the_spu) {
|
---|
181 | return NULL;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (child_spu != NULL)
|
---|
185 | {
|
---|
186 | /* keep track of this so that people can pass functions through but
|
---|
187 | * still get updated when API's change on the fly. */
|
---|
188 | for (temp = the_spu ; temp ; temp = temp->superSPU )
|
---|
189 | {
|
---|
190 | struct _copy_list_node *node = (struct _copy_list_node *) crAlloc( sizeof( *node ) );
|
---|
191 | node->copy = &(temp->dispatch_table);
|
---|
192 | node->next = child_spu->dispatch_table.copyList;
|
---|
193 | child_spu->dispatch_table.copyList = node;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | child_spu = the_spu;
|
---|
197 | }
|
---|
198 | return child_spu;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | #if 00
|
---|
203 | /* XXXX experimental code - not used at this time */
|
---|
204 | /**
|
---|
205 | * Like crSPUChangeInterface(), but don't loop over all functions in
|
---|
206 | * the table to search for 'old_func'.
|
---|
207 | */
|
---|
208 | void
|
---|
209 | crSPUChangeFunction(SPUDispatchTable *table, unsigned int funcOffset,
|
---|
210 | void *newFunc)
|
---|
211 | {
|
---|
212 | SPUGenericFunction *f = (SPUGenericFunction *) table + funcOffset;
|
---|
213 | struct _copy_list_node *temp;
|
---|
214 |
|
---|
215 | CRASSERT(funcOffset < sizeof(*table) / sizeof(SPUGenericFunction));
|
---|
216 |
|
---|
217 | printf("%s\n", __FUNCTION__);
|
---|
218 | if (table->mark == 1)
|
---|
219 | return;
|
---|
220 | table->mark = 1;
|
---|
221 | *f = newFunc;
|
---|
222 |
|
---|
223 | /* update all copies of this table */
|
---|
224 | #if 1
|
---|
225 | for (temp = table->copyList ; temp ; temp = temp->next)
|
---|
226 | {
|
---|
227 | crSPUChangeFunction( temp->copy, funcOffset, newFunc );
|
---|
228 | }
|
---|
229 | #endif
|
---|
230 | if (table->copy_of != NULL)
|
---|
231 | {
|
---|
232 | crSPUChangeFunction( table->copy_of, funcOffset, newFunc );
|
---|
233 | }
|
---|
234 | #if 0
|
---|
235 | for (temp = table->copyList ; temp ; temp = temp->next)
|
---|
236 | {
|
---|
237 | crSPUChangeFunction( temp->copy, funcOffset, newFunc );
|
---|
238 | }
|
---|
239 | #endif
|
---|
240 | table->mark = 0;
|
---|
241 | }
|
---|
242 | #endif
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Call the cleanup() function for each SPU in a chain, close the SPU
|
---|
248 | * DLLs and free the SPU objects.
|
---|
249 | * \param headSPU pointer to the first SPU in the chain
|
---|
250 | */
|
---|
251 | void
|
---|
252 | crSPUUnloadChain(SPU *headSPU)
|
---|
253 | {
|
---|
254 | SPU *the_spu = headSPU, *next_spu;
|
---|
255 |
|
---|
256 | while (the_spu)
|
---|
257 | {
|
---|
258 | crDebug("Cleaning up SPU %s", the_spu->name);
|
---|
259 |
|
---|
260 | if (the_spu->cleanup)
|
---|
261 | the_spu->cleanup();
|
---|
262 |
|
---|
263 | next_spu = the_spu->superSPU;
|
---|
264 | crDLLClose(the_spu->dll);
|
---|
265 | crFree(the_spu);
|
---|
266 | the_spu = next_spu;
|
---|
267 | }
|
---|
268 | }
|
---|