-
Notifications
You must be signed in to change notification settings - Fork 11
/
wrap.cpp
348 lines (317 loc) · 9.25 KB
/
wrap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "header.h"
SYSTEM_INFO sSysInfo;
WRAP* wrap_list;
WRAP* wrap_freelist;
DD_LIFETIME* ddlt_list;
DD_LIFETIME* ddlt_freelist;
CRITICAL_SECTION cs;
const void* iid_to_vtbl( const GUID& riid )
{
TRACE( ">" );
LOG_GUID( riid );
const void* xVtbl;
if ( riid == IID_IUnknown ) xVtbl = &unknwn::xVtbl;
else if ( riid == IID_IClassFactory ) xVtbl = &classfactory::xVtbl;
else if ( riid == IID_IDirectDraw ) xVtbl = &dd::xVtbl1;
else if ( riid == IID_IDirectDraw2 ) xVtbl = &dd::xVtbl2;
else if ( riid == IID_IDirectDraw4 ) xVtbl = &dd::xVtbl4;
else if ( riid == IID_IDirectDraw7 ) xVtbl = &dd::xVtbl7;
else if ( riid == IID_IDirectDrawSurface ) xVtbl = &dds::xVtbl1;
else if ( riid == IID_IDirectDrawSurface2 ) xVtbl = &dds::xVtbl2;
else if ( riid == IID_IDirectDrawSurface3 ) xVtbl = &dds::xVtbl3;
else if ( riid == IID_IDirectDrawSurface4 ) xVtbl = &dds::xVtbl4;
else if ( riid == IID_IDirectDrawSurface7 ) xVtbl = &dds::xVtbl7;
else if ( riid == IID_IDirectDrawClipper ) xVtbl = &clipper::xVtbl;
else if ( riid == IID_IDirectDrawPalette ) xVtbl = &palette::xVtbl;
else if ( riid == IID_IDirectDrawColorControl ) xVtbl = &color::xVtbl;
else if ( riid == IID_IDirectDrawGammaControl ) xVtbl = &gamma::xVtbl;
else xVtbl = NULL;
return xVtbl;
}
// simple way to get small amounts of memory on demand...
// will not span page boundries
// will not consider alignment
// allocated memory can not be freed
// not thread safe
void* micro_alloc( int size )
{
TRACE( ">" );
static BYTE* end_region = 0;
static BYTE* end_page = 0;
static BYTE* alloc_ptr = 0;
if( ( alloc_ptr + size ) >= end_page )
{
alloc_ptr = end_page;
if( alloc_ptr == end_region )
{
TRACE( "reserve a new memory region" );
alloc_ptr = (BYTE*) VirtualAlloc( NULL, sSysInfo.dwAllocationGranularity, MEM_RESERVE, PAGE_NOACCESS );
end_region = alloc_ptr + sSysInfo.dwAllocationGranularity;
}
if( alloc_ptr != NULL )
{
TRACE( "commit a page of the reserved region" );
alloc_ptr = (BYTE*) VirtualAlloc( alloc_ptr, sSysInfo.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
end_page = alloc_ptr + sSysInfo.dwPageSize;
}
if( alloc_ptr == NULL )
{
WARN( "Allocation FAILED" );
end_region = 0;
end_page = 0;
}
}
// "bump the pointer"
void* p = alloc_ptr;
alloc_ptr += size;
return p;
}
// find existing or add a new wrapper for *ppvInterface
// returns true if a new wrapper is created
bool Wrap( DD_LIFETIME* dd_parent, const void* xVtbl, void** ppvInterface )
{
TRACE( ">" );
WRAP* w;
bool bNew = false;
// validate args
if( ( xVtbl != NULL ) && ( ppvInterface != NULL ) && ( *ppvInterface != NULL ) )
{
EnterCriticalSection( &cs );
// search list
for( w = wrap_list; ( w != NULL ) && ( w->inner_iface != *ppvInterface ); w = w->next );
if( w != NULL )
{
TRACE( "found existing wrapper" );
if( w->xVtbl != xVtbl ){ WARN("existing wrapper is for a different type"); }
*((WRAP**)ppvInterface) = w;
}
else
{
TRACE( "creating new wrapper" );
if( wrap_freelist != NULL )
{
// pop
w = wrap_freelist;
wrap_freelist = w->next;
}
else
{
w = (WRAP*) micro_alloc( sizeof( WRAP ) );
}
if( w != NULL )
{
// dd obj lifetime tracker //
if( ( dd_parent != NULL ))
{
if( ( ( xVtbl == &unknwn::xVtbl ) && ( dd_parent->obj == ((IUnknown*)(*ppvInterface)) ) ) ||
( ( xVtbl == &dd::xVtbl1 ) || ( xVtbl == &dd::xVtbl2 ) || ( xVtbl == &dd::xVtbl4 ) || ( xVtbl == &dd::xVtbl7 ) ) )
{
TRACE( "new interface of existing dd obj" );
dd_parent->iface_cnt++;
}
}
else
{
if( ( xVtbl == &dd::xVtbl1 ) || ( xVtbl == &dd::xVtbl2 ) || ( xVtbl == &dd::xVtbl4 ) || ( xVtbl == &dd::xVtbl7 ) )
{
TRACE( "new dd obj" );
if( ddlt_freelist != NULL )
{
// pop
dd_parent = ddlt_freelist;
ddlt_freelist = dd_parent->next;
}
else
{
dd_parent = (DD_LIFETIME*) micro_alloc( sizeof( DD_LIFETIME ) );
}
if( dd_parent != NULL )
{
((IUnknown*)(*ppvInterface))->lpVtbl->QueryInterface( ((IUnknown*)(*ppvInterface)), IID_IUnknown, (void**)&dd_parent->obj );
dd_parent->obj->lpVtbl->Release( dd_parent->obj );
dd_parent->iface_cnt = 1;
dd_parent->next = ddlt_list;
ddlt_list = dd_parent; // push
}
}
}
// construct
w->xVtbl = xVtbl;
w->inner_iface = *ppvInterface;
w->dd_parent = dd_parent;
w->next = wrap_list;
wrap_list = w; // push
*((WRAP**)ppvInterface) = w; // hook it !
bNew = true;
}
}
LeaveCriticalSection( &cs );
}
return bNew;
}
ULONG WrapRelease( WRAP* This )
{
TRACE( ">" );
EnterCriticalSection( &cs );
ULONG dwCount = This->unknwn->lpVtbl->Release( This->unknwn );
if( dwCount == 0 )
{
TRACE( "interface destroyed" );
WRAP* w = wrap_list;
WRAP* prev = NULL;
DD_LIFETIME* dd_parent = This->dd_parent;
// if dd interface was destroyed
if( ( dd_parent != NULL ) &&
( (( This->xVtbl == &unknwn::xVtbl ) && ( This->unknwn == dd_parent->obj )) ||
( This->xVtbl == &dd::xVtbl1 ) || ( This->xVtbl == &dd::xVtbl2 ) ||
( This->xVtbl == &dd::xVtbl4 ) || ( This->xVtbl == &dd::xVtbl7 ) ) )
{
dd_parent->iface_cnt--;
if( dd_parent->iface_cnt == 0 )
{
TRACE( "dd obj destroyed" );
// destroy ALL wraps that have a matching dd_parent
while( w != NULL )
{
if( w->dd_parent == dd_parent ) // if match
{
TRACE( "destroyed child\n" );
// move to freelist
if( prev != NULL )
{
prev->next = w->next;
w->next = wrap_freelist;
wrap_freelist = w; // push
w = prev->next; // continue
}
else
{
wrap_list = w->next;
w->next = wrap_freelist;
wrap_freelist = w; // push
w = wrap_list; // continue
}
}
else // else continue search
{
prev = w;
w = w->next;
}
}
// move dd_parent to the freelist
DD_LIFETIME* d_prev = NULL;
DD_LIFETIME* d;
for( d = ddlt_list; ( d != NULL ) && ( d != dd_parent ); d = d->next ) d_prev = d; // find previons link
if( d == NULL ){ WARN( "wrap not in list" ); }
else
{
if( d_prev != NULL ) d_prev->next = d->next;
else ddlt_list = d->next;
d->next = ddlt_freelist;
ddlt_freelist = d;
}
}
}
else
{
// move This to the freelist
for( w = wrap_list; ( w != NULL ) && ( w != This ); w = w->next ) prev = w; // find previons link
if( w == NULL ){ WARN( "wrap not in list" ); }
else
{
if( prev != NULL ) prev->next = w->next;
else wrap_list = w->next;
w->next = wrap_freelist;
wrap_freelist = w;
}
}
}
LeaveCriticalSection( &cs );
return dwCount;
}
HRESULT __stdcall WrapEnumSurfacesCallback( LPDIRECTDRAWSURFACE lpDDSurface, LPDDSURFACEDESC lpDDSurfaceDesc, LPVOID lpContext )
{
PROLOGUE;
EnumStruct* e = (EnumStruct*)lpContext;
bool bNew = Wrap( e->dd_parent, e->xVtbl, (void**)&lpDDSurface );
if( ( bNew != false ) && ( e->must_exist != false ) ){ WARN( "interface was not wrapped" ); }
HRESULT hResult = ((LPDDENUMSURFACESCALLBACK) e->callback)( lpDDSurface, lpDDSurfaceDesc, e->context );
EPILOGUE( hResult );
}
// all versions of dd interfaces share the same functions ... ( for ease-of-use ) //
// all versions of dds interfaces share the same functions ... ( for ease-of-use ) //
// ... so a little extra work is needed to distinguish between different versions //
const void* dd_to_dds_vtbl( WRAP* This )
{
TRACE( ">" );
if( This->xVtbl == &dd::xVtbl7 ) return &dds::xVtbl7;
else if( This->xVtbl == &dd::xVtbl4 ) return &dds::xVtbl4;
return &dds::xVtbl1;
}
const void* dds_to_dd_vtbl( WRAP* This )
{
TRACE( ">" );
if( This->xVtbl == &dds::xVtbl7 ) return &dd::xVtbl7;
else if( This->xVtbl == &dds::xVtbl4 ) return &dd::xVtbl4;
return &dd::xVtbl2;
}
// There are no guarantees that an interface passed as argument is wrapped or not NULL //
IDirectDraw* GetInnerInterface( IDirectDraw* iface )
{
TRACE( "dd" );
if( iface != NULL )
{
if( ( ((WRAP*)iface)->xVtbl == &dd::xVtbl1 ) ||
( ((WRAP*)iface)->xVtbl == &dd::xVtbl2 ) ||
( ((WRAP*)iface)->xVtbl == &dd::xVtbl4 ) ||
( ((WRAP*)iface)->xVtbl == &dd::xVtbl7 ) )
{
return ((WRAP*)iface)->dd1;
}
else WARN( "interface not wrapped" );
}
return iface;
}
IDirectDrawSurface* GetInnerInterface( IDirectDrawSurface* iface )
{
TRACE( "dds" );
if( iface != NULL )
{
if( ( ((WRAP*)iface)->xVtbl == &dds::xVtbl1 ) ||
( ((WRAP*)iface)->xVtbl == &dds::xVtbl2 ) ||
( ((WRAP*)iface)->xVtbl == &dds::xVtbl3 ) ||
( ((WRAP*)iface)->xVtbl == &dds::xVtbl4 ) ||
( ((WRAP*)iface)->xVtbl == &dds::xVtbl7 ) )
{
return ((WRAP*)iface)->dds1;
}
else WARN( "interface not wrapped" );
}
return iface;
}
IDirectDrawClipper* GetInnerInterface( IDirectDrawClipper* iface )
{
TRACE( "clip" );
if( iface != NULL )
{
if( ((WRAP*)iface)->xVtbl == &clipper::xVtbl )
{
return ((WRAP*)iface)->clip;
}
else WARN( "interface not wrapped" );
}
return iface;
}
IDirectDrawPalette* GetInnerInterface( IDirectDrawPalette* iface )
{
TRACE( "pal" );
if( iface != NULL )
{
if( ((WRAP*)iface)->xVtbl == &palette::xVtbl )
{
return ((WRAP*)iface)->pal;
}
else WARN( "interface not wrapped" );
}
return iface;
}