1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
39
40 #include "ddraw.h"
41 #include "d3d.h"
42
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
47
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51
52 /* Device identifier. Don't relay it to WineD3D */
53 static const DDDEVICEIDENTIFIER2 deviceidentifier =
54 {
55 "display",
56 "DirectDraw HAL",
57 { { 0x00010001, 0x00010001 } },
58 0, 0, 0, 0,
59 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
60 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
61 0
62 };
63
64 /*****************************************************************************
65 * IUnknown Methods
66 *****************************************************************************/
67
68 /*****************************************************************************
69 * IDirectDraw7::QueryInterface
70 *
71 * Queries different interfaces of the DirectDraw object. It can return
72 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
73 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
74 * method.
75 * The returned interface is AddRef()-ed before it's returned
76 *
77 * Used for version 1, 2, 4 and 7
78 *
79 * Params:
80 * refiid: Interface ID asked for
81 * obj: Used to return the interface pointer
82 *
83 * Returns:
84 * S_OK if an interface was found
85 * E_NOINTERFACE if the requested interface wasn't found
86 *
87 *****************************************************************************/
88 static HRESULT WINAPI
89 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
90 REFIID refiid,
91 void **obj)
92 {
93 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
94
95 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
96
97 /* Can change surface impl type */
98 EnterCriticalSection(&ddraw_cs);
99
100 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
101 *obj = NULL;
102
103 if(!refiid)
104 {
105 LeaveCriticalSection(&ddraw_cs);
106 return DDERR_INVALIDPARAMS;
107 }
108
109 /* Check DirectDraw Interfaces */
110 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
111 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
112 {
113 *obj = ICOM_INTERFACE(This, IDirectDraw7);
114 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
115 }
116 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
117 {
118 *obj = ICOM_INTERFACE(This, IDirectDraw4);
119 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
120 }
121 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
122 {
123 *obj = ICOM_INTERFACE(This, IDirectDraw3);
124 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
125 }
126 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
127 {
128 *obj = ICOM_INTERFACE(This, IDirectDraw2);
129 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
130 }
131 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
132 {
133 *obj = ICOM_INTERFACE(This, IDirectDraw);
134 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
135 }
136
137 /* Direct3D
138 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
139 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
140 * who had this idea and why. The older interfaces can query and IDirect3D version
141 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
142 * and messy to implement with the common creation function, so it has been left out here.
143 */
144 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
145 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
147 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
148 {
149 /* Check the surface implementation */
150 if(This->ImplType == SURFACE_UNKNOWN)
151 {
152 /* Apps may create the IDirect3D Interface before the primary surface.
153 * set the surface implementation */
154 This->ImplType = SURFACE_OPENGL;
155 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
156 }
157 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
158 {
159 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
160 ERR(" (%p) You may want to contact wine-devel for help\n", This);
161 /* Should I assert(0) here??? */
162 }
163 else if(This->ImplType != SURFACE_OPENGL)
164 {
165 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
166 /* Do not abort here, only reject 3D Device creation */
167 }
168
169 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
170 {
171 This->d3dversion = 1;
172 *obj = ICOM_INTERFACE(This, IDirect3D);
173 TRACE(" returning Direct3D interface at %p.\n", *obj);
174 }
175 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
176 {
177 This->d3dversion = 2;
178 *obj = ICOM_INTERFACE(This, IDirect3D2);
179 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
180 }
181 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
182 {
183 This->d3dversion = 3;
184 *obj = ICOM_INTERFACE(This, IDirect3D3);
185 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
186 }
187 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
188 {
189 This->d3dversion = 7;
190 *obj = ICOM_INTERFACE(This, IDirect3D7);
191 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
192 }
193 }
194
195 /* Unknown interface */
196 else
197 {
198 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
199 LeaveCriticalSection(&ddraw_cs);
200 return E_NOINTERFACE;
201 }
202
203 IUnknown_AddRef( (IUnknown *) *obj );
204 LeaveCriticalSection(&ddraw_cs);
205 return S_OK;
206 }
207
208 /*****************************************************************************
209 * IDirectDraw7::AddRef
210 *
211 * Increases the interfaces refcount, basically
212 *
213 * DDraw refcounting is a bit tricky. The different DirectDraw interface
214 * versions have individual refcounts, but the IDirect3D interfaces do not.
215 * All interfaces are from one object, that means calling QueryInterface on an
216 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
217 * IDirectDrawImpl object.
218 *
219 * That means all AddRef and Release implementations of IDirectDrawX work
220 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
221 * except of IDirect3D7 which thunks to IDirectDraw7
222 *
223 * Returns: The new refcount
224 *
225 *****************************************************************************/
226 static ULONG WINAPI
227 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
228 {
229 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
230 ULONG ref = InterlockedIncrement(&This->ref7);
231
232 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
233
234 if(ref == 1) InterlockedIncrement(&This->numIfaces);
235
236 return ref;
237 }
238
239 /*****************************************************************************
240 * IDirectDrawImpl_Destroy
241 *
242 * Destroys a ddraw object if all refcounts are 0. This is to share code
243 * between the IDirectDrawX::Release functions
244 *
245 * Params:
246 * This: DirectDraw object to destroy
247 *
248 *****************************************************************************/
249 void
250 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
251 {
252 /* Clear the cooplevel to restore window and display mode */
253 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
254 NULL,
255 DDSCL_NORMAL);
256
257 /* Destroy the device window if we created one */
258 if(This->devicewindow != 0)
259 {
260 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
261 DestroyWindow(This->devicewindow);
262 This->devicewindow = 0;
263 }
264
265 /* Unregister the window class */
266 UnregisterClassA(This->classname, 0);
267
268 EnterCriticalSection(&ddraw_cs);
269 list_remove(&This->ddraw_list_entry);
270 LeaveCriticalSection(&ddraw_cs);
271
272 /* Release the attached WineD3D stuff */
273 IWineD3DDevice_Release(This->wineD3DDevice);
274 IWineD3D_Release(This->wineD3D);
275
276 /* Now free the object */
277 HeapFree(GetProcessHeap(), 0, This);
278 }
279
280 /*****************************************************************************
281 * IDirectDraw7::Release
282 *
283 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
284 *
285 * Returns: The new refcount
286 *****************************************************************************/
287 static ULONG WINAPI
288 IDirectDrawImpl_Release(IDirectDraw7 *iface)
289 {
290 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
291 ULONG ref = InterlockedDecrement(&This->ref7);
292
293 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
294
295 if(ref == 0)
296 {
297 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
298 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
299 }
300
301 return ref;
302 }
303
304 /*****************************************************************************
305 * IDirectDraw methods
306 *****************************************************************************/
307
308 /*****************************************************************************
309 * IDirectDraw7::SetCooperativeLevel
310 *
311 * Sets the cooperative level for the DirectDraw object, and the window
312 * assigned to it. The cooperative level determines the general behavior
313 * of the DirectDraw application
314 *
315 * Warning: This is quite tricky, as it's not really documented which
316 * cooperative levels can be combined with each other. If a game fails
317 * after this function, try to check the cooperative levels passed on
318 * Windows, and if it returns something different.
319 *
320 * If you think that this function caused the failure because it writes a
321 * fixme, be sure to run again with a +ddraw trace.
322 *
323 * What is known about cooperative levels (See the ddraw modes test):
324 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
325 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
326 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
327 * DDSCL_FULLSCREEN can be activated
328 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
329 *
330 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
331 * DDSCL_SETFOCUSWINDOW (partially),
332 * DDSCL_MULTITHREADED (work in progress)
333 *
334 * Unhandled flags, which should be implemented
335 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
336 * expect any difference to a normal window for wine)
337 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
338 * rendering (Possible test case: Half-life)
339 *
340 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
341 *
342 * These don't seem very important for wine:
343 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
344 *
345 * Returns:
346 * DD_OK if the cooperative level was set successfully
347 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
348 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
349 * (Probably others too, have to investigate)
350 *
351 *****************************************************************************/
352 static HRESULT WINAPI
353 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
354 HWND hwnd,
355 DWORD cooplevel)
356 {
357 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
358 HWND window;
359 HRESULT hr;
360
361 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
362 DDRAW_dump_cooperativelevel(cooplevel);
363
364 EnterCriticalSection(&ddraw_cs);
365
366 /* Get the old window */
367 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
368 if(hr != D3D_OK)
369 {
370 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
371 LeaveCriticalSection(&ddraw_cs);
372 return hr;
373 }
374
375 /* Tests suggest that we need one of them: */
376 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
377 DDSCL_NORMAL |
378 DDSCL_EXCLUSIVE )))
379 {
380 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
381 LeaveCriticalSection(&ddraw_cs);
382 return DDERR_INVALIDPARAMS;
383 }
384
385 /* Handle those levels first which set various hwnds */
386 if(cooplevel & DDSCL_SETFOCUSWINDOW)
387 {
388 /* This isn't compatible with a lot of flags */
389 if(cooplevel & ( DDSCL_MULTITHREADED |
390 DDSCL_FPUSETUP |
391 DDSCL_FPUPRESERVE |
392 DDSCL_ALLOWREBOOT |
393 DDSCL_ALLOWMODEX |
394 DDSCL_SETDEVICEWINDOW |
395 DDSCL_NORMAL |
396 DDSCL_EXCLUSIVE |
397 DDSCL_FULLSCREEN ) )
398 {
399 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
400 LeaveCriticalSection(&ddraw_cs);
401 return DDERR_INVALIDPARAMS;
402 }
403 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
404 {
405 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
406 LeaveCriticalSection(&ddraw_cs);
407 return DDERR_HWNDALREADYSET;
408 }
409
410 This->focuswindow = hwnd;
411 /* Won't use the hwnd param for anything else */
412 hwnd = NULL;
413
414 /* Use the focus window for drawing too */
415 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
416
417 /* Destroy the device window, if we have one */
418 if(This->devicewindow)
419 {
420 DestroyWindow(This->devicewindow);
421 This->devicewindow = NULL;
422 }
423 }
424 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
425 if(cooplevel & DDSCL_NORMAL)
426 {
427 /* Can't coexist with fullscreen or exclusive */
428 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
429 {
430 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
431 LeaveCriticalSection(&ddraw_cs);
432 return DDERR_INVALIDPARAMS;
433 }
434
435 /* Switching from fullscreen? */
436 if(This->cooperative_level & DDSCL_FULLSCREEN)
437 {
438 /* Restore the display mode */
439 IDirectDraw7_RestoreDisplayMode(iface);
440
441 This->cooperative_level &= ~DDSCL_FULLSCREEN;
442 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
443 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
444 }
445
446 /* Don't override focus windows or private device windows */
447 if( hwnd &&
448 !(This->focuswindow) &&
449 !(This->devicewindow) &&
450 (hwnd != window) )
451 {
452 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
453 }
454
455 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
456 FALSE);
457 }
458 else if(cooplevel & DDSCL_FULLSCREEN)
459 {
460 /* Needs DDSCL_EXCLUSIVE */
461 if(!(cooplevel & DDSCL_EXCLUSIVE) )
462 {
463 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
464 LeaveCriticalSection(&ddraw_cs);
465 return DDERR_INVALIDPARAMS;
466 }
467 /* Need a HWND
468 if(hwnd == 0)
469 {
470 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
471 return DDERR_INVALIDPARAMS;
472 }
473 */
474
475 This->cooperative_level &= ~DDSCL_NORMAL;
476 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
477 TRUE);
478
479 /* Don't override focus windows or private device windows */
480 if( hwnd &&
481 !(This->focuswindow) &&
482 !(This->devicewindow) &&
483 (hwnd != window) )
484 {
485 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
486 }
487 }
488 else if(cooplevel & DDSCL_EXCLUSIVE)
489 {
490 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
491 LeaveCriticalSection(&ddraw_cs);
492 return DDERR_INVALIDPARAMS;
493 }
494
495 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
496 {
497 /* Don't create a device window if a focus window is set */
498 if( !(This->focuswindow) )
499 {
500 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
501 WS_POPUP, 0, 0,
502 GetSystemMetrics(SM_CXSCREEN),
503 GetSystemMetrics(SM_CYSCREEN),
504 NULL, NULL, GetModuleHandleA(0), NULL);
505
506 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
507 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
508
509 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
510 This->devicewindow = devicewindow;
511 }
512 }
513
514 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
515 {
516 /* Enable thread safety in wined3d */
517 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
518 }
519
520 /* Unhandled flags */
521 if(cooplevel & DDSCL_ALLOWREBOOT)
522 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
523 if(cooplevel & DDSCL_ALLOWMODEX)
524 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
525 if(cooplevel & DDSCL_FPUSETUP)
526 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
527 if(cooplevel & DDSCL_FPUPRESERVE)
528 WARN("(%p) Unhandled flag DDSCL_FPUPRESERVE, harmless\n", This);
529
530 /* Store the cooperative_level */
531 This->cooperative_level |= cooplevel;
532 TRACE("SetCooperativeLevel retuning DD_OK\n");
533 LeaveCriticalSection(&ddraw_cs);
534 return DD_OK;
535 }
536
537 /*****************************************************************************
538 *
539 * Helper function for SetDisplayMode and RestoreDisplayMode
540 *
541 * Implements DirectDraw's SetDisplayMode, but ignores the value of
542 * ForceRefreshRate, since it is already handled by
543 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
544 * without worrying that ForceRefreshRate will override the refresh rate. For
545 * argument and return value documentation, see
546 * IDirectDrawImpl_SetDisplayMode.
547 *
548 *****************************************************************************/
549 static HRESULT
550 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
551 DWORD Width,
552 DWORD Height,
553 DWORD BPP,
554 DWORD RefreshRate,
555 DWORD Flags)
556 {
557 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
558 WINED3DDISPLAYMODE Mode;
559 HRESULT hr;
560 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
561
562 EnterCriticalSection(&ddraw_cs);
563 if( !Width || !Height )
564 {
565 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
566 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
567 LeaveCriticalSection(&ddraw_cs);
568 return DD_OK;
569 }
570
571 /* Check the exclusive mode
572 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
573 return DDERR_NOEXCLUSIVEMODE;
574 * This is WRONG. Don't know if the SDK is completely
575 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
576 * is returned, but Half-Life 1.1.1.1 (Steam version)
577 * depends on this
578 */
579
580 Mode.Width = Width;
581 Mode.Height = Height;
582 Mode.RefreshRate = RefreshRate;
583 switch(BPP)
584 {
585 case 8: Mode.Format = WINED3DFMT_P8; break;
586 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
587 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
588 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
589 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
590 }
591
592 /* TODO: The possible return values from msdn suggest that
593 * the screen mode can't be changed if a surface is locked
594 * or some drawing is in progress
595 */
596
597 /* TODO: Lose the primary surface */
598 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
599 0, /* First swapchain */
600 &Mode);
601 LeaveCriticalSection(&ddraw_cs);
602 switch(hr)
603 {
604 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
605 default: return hr;
606 };
607 }
608
609 /*****************************************************************************
610 * IDirectDraw7::SetDisplayMode
611 *
612 * Sets the display screen resolution, color depth and refresh frequency
613 * when in fullscreen mode (in theory).
614 * Possible return values listed in the SDK suggest that this method fails
615 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
616 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
617 * It seems to be valid to pass 0 for With and Height, this has to be tested
618 * It could mean that the current video mode should be left as-is. (But why
619 * call it then?)
620 *
621 * Params:
622 * Height, Width: Screen dimension
623 * BPP: Color depth in Bits per pixel
624 * Refreshrate: Screen refresh rate
625 * Flags: Other stuff
626 *
627 * Returns
628 * DD_OK on success
629 *
630 *****************************************************************************/
631 static HRESULT WINAPI
632 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
633 DWORD Width,
634 DWORD Height,
635 DWORD BPP,
636 DWORD RefreshRate,
637 DWORD Flags)
638 {
639 if (force_refresh_rate != 0)
640 {
641 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
642 RefreshRate = force_refresh_rate;
643 }
644
645 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
646 RefreshRate, Flags);
647 }
648
649 /*****************************************************************************
650 * IDirectDraw7::RestoreDisplayMode
651 *
652 * Restores the display mode to what it was at creation time. Basically.
653 *
654 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
655 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
656 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
657 * -> DD_1 is released. The screen should be left at 1024x768x32.
658 * -> DD_2 is released. The screen should be set to 1400x1050x32
659 * This case is unhandled right now, but Empire Earth does it this way.
660 * (But perhaps there is something in SetCooperativeLevel to prevent this)
661 *
662 * The msdn says that this method resets the display mode to what it was before
663 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
664 *
665 * Returns
666 * DD_OK on success
667 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
668 *
669 *****************************************************************************/
670 static HRESULT WINAPI
671 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
672 {
673 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
674 TRACE("(%p)\n", This);
675
676 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
677 This->orig_width,
678 This->orig_height,
679 This->orig_bpp,
680 0,
681 0);
682 }
683
684 /*****************************************************************************
685 * IDirectDraw7::GetCaps
686 *
687 * Returns the drives capabilities
688 *
689 * Used for version 1, 2, 4 and 7
690 *
691 * Params:
692 * DriverCaps: Structure to write the Hardware accelerated caps to
693 * HelCaps: Structure to write the emulation caps to
694 *
695 * Returns
696 * This implementation returns DD_OK only
697 *
698 *****************************************************************************/
699 static HRESULT WINAPI
700 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
701 DDCAPS *DriverCaps,
702 DDCAPS *HELCaps)
703 {
704 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
705 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
706
707 /* One structure must be != NULL */
708 if( (!DriverCaps) && (!HELCaps) )
709 {
710 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
711 return DDERR_INVALIDPARAMS;
712 }
713
714 if(DriverCaps)
715 {
716 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
717 if (TRACE_ON(ddraw))
718 {
719 TRACE("Driver Caps :\n");
720 DDRAW_dump_DDCAPS(DriverCaps);
721 }
722
723 }
724 if(HELCaps)
725 {
726 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
727 if (TRACE_ON(ddraw))
728 {
729 TRACE("HEL Caps :\n");
730 DDRAW_dump_DDCAPS(HELCaps);
731 }
732 }
733
734 return DD_OK;
735 }
736
737 /*****************************************************************************
738 * IDirectDraw7::Compact
739 *
740 * No idea what it does, MSDN says it's not implemented.
741 *
742 * Returns
743 * DD_OK, but this is unchecked
744 *
745 *****************************************************************************/
746 static HRESULT WINAPI
747 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
748 {
749 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
750 TRACE("(%p)\n", This);
751
752 return DD_OK;
753 }
754
755 /*****************************************************************************
756 * IDirectDraw7::GetDisplayMode
757 *
758 * Returns information about the current display mode
759 *
760 * Exists in Version 1, 2, 4 and 7
761 *
762 * Params:
763 * DDSD: Address of a surface description structure to write the info to
764 *
765 * Returns
766 * DD_OK
767 *
768 *****************************************************************************/
769 static HRESULT WINAPI
770 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
771 DDSURFACEDESC2 *DDSD)
772 {
773 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
774 HRESULT hr;
775 WINED3DDISPLAYMODE Mode;
776 DWORD Size;
777 TRACE("(%p)->(%p): Relay\n", This, DDSD);
778
779 EnterCriticalSection(&ddraw_cs);
780 /* This seems sane */
781 if(!DDSD)
782 {
783 LeaveCriticalSection(&ddraw_cs);
784 return DDERR_INVALIDPARAMS;
785 }
786
787 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
788 * so one method can be used for all versions (Hopefully)
789 */
790 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
791 0 /* swapchain 0 */,
792 &Mode);
793 if( hr != D3D_OK )
794 {
795 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
796 LeaveCriticalSection(&ddraw_cs);
797 return hr;
798 }
799
800 Size = DDSD->dwSize;
801 memset(DDSD, 0, Size);
802
803 DDSD->dwSize = Size;
804 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
805 DDSD->dwWidth = Mode.Width;
806 DDSD->dwHeight = Mode.Height;
807 DDSD->u2.dwRefreshRate = 60;
808 DDSD->ddsCaps.dwCaps = 0;
809 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
810 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
811 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
812
813 if(TRACE_ON(ddraw))
814 {
815 TRACE("Returning surface desc :\n");
816 DDRAW_dump_surface_desc(DDSD);
817 }
818
819 LeaveCriticalSection(&ddraw_cs);
820 return DD_OK;
821 }
822
823 /*****************************************************************************
824 * IDirectDraw7::GetFourCCCodes
825 *
826 * Returns an array of supported FourCC codes.
827 *
828 * Exists in Version 1, 2, 4 and 7
829 *
830 * Params:
831 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
832 * of enumerated codes
833 * Codes: Pointer to an array of DWORDs where the supported codes are written
834 * to
835 *
836 * Returns
837 * Always returns DD_OK, as it's a stub for now
838 *
839 *****************************************************************************/
840 static HRESULT WINAPI
841 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
842 DWORD *NumCodes, DWORD *Codes)
843 {
844 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
845 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
846
847 if(NumCodes) *NumCodes = 0;
848
849 return DD_OK;
850 }
851
852 /*****************************************************************************
853 * IDirectDraw7::GetMonitorFrequency
854 *
855 * Returns the monitor's frequency
856 *
857 * Exists in Version 1, 2, 4 and 7
858 *
859 * Params:
860 * Freq: Pointer to a DWORD to write the frequency to
861 *
862 * Returns
863 * Always returns DD_OK
864 *
865 *****************************************************************************/
866 static HRESULT WINAPI
867 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
868 DWORD *Freq)
869 {
870 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
871 TRACE("(%p)->(%p)\n", This, Freq);
872
873 /* Ideally this should be in WineD3D, as it concerns the screen setup,
874 * but for now this should make the games happy
875 */
876 *Freq = 60;
877 return DD_OK;
878 }
879
880 /*****************************************************************************
881 * IDirectDraw7::GetVerticalBlankStatus
882 *
883 * Returns the Vertical blank status of the monitor. This should be in WineD3D
884 * too basically, but as it's a semi stub, I didn't create a function there
885 *
886 * Params:
887 * status: Pointer to a BOOL to be filled with the vertical blank status
888 *
889 * Returns
890 * DD_OK on success
891 * DDERR_INVALIDPARAMS if status is NULL
892 *
893 *****************************************************************************/
894 static HRESULT WINAPI
895 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
896 BOOL *status)
897 {
898 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
899 TRACE("(%p)->(%p)\n", This, status);
900
901 /* This looks sane, the MSDN suggests it too */
902 EnterCriticalSection(&ddraw_cs);
903 if(!status)
904 {
905 LeaveCriticalSection(&ddraw_cs);
906 return DDERR_INVALIDPARAMS;
907 }
908
909 *status = This->fake_vblank;
910 This->fake_vblank = !This->fake_vblank;
911 LeaveCriticalSection(&ddraw_cs);
912 return DD_OK;
913 }
914
915 /*****************************************************************************
916 * IDirectDraw7::GetAvailableVidMem
917 *
918 * Returns the total and free video memory
919 *
920 * Params:
921 * Caps: Specifies the memory type asked for
922 * total: Pointer to a DWORD to be filled with the total memory
923 * free: Pointer to a DWORD to be filled with the free memory
924 *
925 * Returns
926 * DD_OK on success
927 * DDERR_INVALIDPARAMS of free and total are NULL
928 *
929 *****************************************************************************/
930 static HRESULT WINAPI
931 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
932 {
933 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
934 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
935
936 if(TRACE_ON(ddraw))
937 {
938 TRACE("(%p) Asked for memory with description: ", This);
939 DDRAW_dump_DDSCAPS2(Caps);
940 }
941 EnterCriticalSection(&ddraw_cs);
942
943 /* Todo: System memory vs local video memory vs non-local video memory
944 * The MSDN also mentions differences between texture memory and other
945 * resources, but that's not important
946 */
947
948 if( (!total) && (!free) )
949 {
950 LeaveCriticalSection(&ddraw_cs);
951 return DDERR_INVALIDPARAMS;
952 }
953
954 if(total) *total = This->total_vidmem;
955 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
956
957 LeaveCriticalSection(&ddraw_cs);
958 return DD_OK;
959 }
960
961 /*****************************************************************************
962 * IDirectDraw7::Initialize
963 *
964 * Initializes a DirectDraw interface.
965 *
966 * Params:
967 * GUID: Interface identifier. Well, don't know what this is really good
968 * for
969 *
970 * Returns
971 * Returns DD_OK on the first call,
972 * DDERR_ALREADYINITIALIZED on repeated calls
973 *
974 *****************************************************************************/
975 static HRESULT WINAPI
976 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
977 GUID *Guid)
978 {
979 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
980 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
981
982 if(This->initialized)
983 {
984 return DDERR_ALREADYINITIALIZED;
985 }
986 else
987 {
988 return DD_OK;
989 }
990 }
991
992 /*****************************************************************************
993 * IDirectDraw7::FlipToGDISurface
994 *
995 * "Makes the surface that the GDI writes to the primary surface"
996 * Looks like some windows specific thing we don't have to care about.
997 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
998 * show error boxes ;)
999 * Well, just return DD_OK.
1000 *
1001 * Returns:
1002 * Always returns DD_OK
1003 *
1004 *****************************************************************************/
1005 static HRESULT WINAPI
1006 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1007 {
1008 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1009 TRACE("(%p)\n", This);
1010
1011 return DD_OK;
1012 }
1013
1014 /*****************************************************************************
1015 * IDirectDraw7::WaitForVerticalBlank
1016 *
1017 * This method allows applications to get in sync with the vertical blank
1018 * interval.
1019 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1020 * redraw the screen, most likely because of this stub
1021 *
1022 * Parameters:
1023 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1024 * or DDWAITVB_BLOCKEND
1025 * h: Not used, according to MSDN
1026 *
1027 * Returns:
1028 * Always returns DD_OK
1029 *
1030 *****************************************************************************/
1031 static HRESULT WINAPI
1032 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1033 DWORD Flags,
1034 HANDLE h)
1035 {
1036 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1037 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1038
1039 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1040 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1041 return DDERR_UNSUPPORTED; /* unchecked */
1042
1043 return DD_OK;
1044 }
1045
1046 /*****************************************************************************
1047 * IDirectDraw7::GetScanLine
1048 *
1049 * Returns the scan line that is being drawn on the monitor
1050 *
1051 * Parameters:
1052 * Scanline: Address to write the scan line value to
1053 *
1054 * Returns:
1055 * Always returns DD_OK
1056 *
1057 *****************************************************************************/
1058 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1059 {
1060 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1061 static BOOL hide = FALSE;
1062 WINED3DDISPLAYMODE Mode;
1063
1064 /* This function is called often, so print the fixme only once */
1065 EnterCriticalSection(&ddraw_cs);
1066 if(!hide)
1067 {
1068 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1069 hide = TRUE;
1070 }
1071
1072 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1073 0,
1074 &Mode);
1075
1076 /* Fake the line sweeping of the monitor */
1077 /* FIXME: We should synchronize with a source to keep the refresh rate */
1078 *Scanline = This->cur_scanline++;
1079 /* Assume 20 scan lines in the vertical blank */
1080 if (This->cur_scanline >= Mode.Height + 20)
1081 This->cur_scanline = 0;
1082
1083 LeaveCriticalSection(&ddraw_cs);
1084 return DD_OK;
1085 }
1086
1087 /*****************************************************************************
1088 * IDirectDraw7::TestCooperativeLevel
1089 *
1090 * Informs the application about the state of the video adapter, depending
1091 * on the cooperative level
1092 *
1093 * Returns:
1094 * DD_OK if the device is in a sane state
1095 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1096 * if the state is not correct(See below)
1097 *
1098 *****************************************************************************/
1099 static HRESULT WINAPI
1100 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1101 {
1102 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1103 HRESULT hr;
1104 TRACE("(%p)\n", This);
1105
1106 EnterCriticalSection(&ddraw_cs);
1107 /* Description from MSDN:
1108 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1109 * away from the app with e.g. alt-tab. Windowed apps receive
1110 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1111 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1112 * when the video mode has changed
1113 */
1114
1115 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1116
1117 /* Fix the result value. These values are mapped from their
1118 * d3d9 counterpart.
1119 */
1120 switch(hr)
1121 {
1122 case WINED3DERR_DEVICELOST:
1123 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1124 {
1125 LeaveCriticalSection(&ddraw_cs);
1126 return DDERR_NOEXCLUSIVEMODE;
1127 }
1128 else
1129 {
1130 LeaveCriticalSection(&ddraw_cs);
1131 return DDERR_EXCLUSIVEMODEALREADYSET;
1132 }
1133
1134 case WINED3DERR_DEVICENOTRESET:
1135 LeaveCriticalSection(&ddraw_cs);
1136 return DD_OK;
1137
1138 case WINED3D_OK:
1139 LeaveCriticalSection(&ddraw_cs);
1140 return DD_OK;
1141
1142 case WINED3DERR_DRIVERINTERNALERROR:
1143 default:
1144 ERR("(%p) Unexpected return value %08x from wineD3D, "
1145 " returning DD_OK\n", This, hr);
1146 }
1147
1148 LeaveCriticalSection(&ddraw_cs);
1149 return DD_OK;
1150 }
1151
1152 /*****************************************************************************
1153 * IDirectDraw7::GetGDISurface
1154 *
1155 * Returns the surface that GDI is treating as the primary surface.
1156 * For Wine this is the front buffer
1157 *
1158 * Params:
1159 * GDISurface: Address to write the surface pointer to
1160 *
1161 * Returns:
1162 * DD_OK if the surface was found
1163 * DDERR_NOTFOUND if the GDI surface wasn't found
1164 *
1165 *****************************************************************************/
1166 static HRESULT WINAPI
1167 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1168 IDirectDrawSurface7 **GDISurface)
1169 {
1170 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1171 IWineD3DSurface *Surf;
1172 IDirectDrawSurface7 *ddsurf;
1173 HRESULT hr;
1174 DDSCAPS2 ddsCaps;
1175 TRACE("(%p)->(%p)\n", This, GDISurface);
1176
1177 /* Get the back buffer from the wineD3DDevice and search its
1178 * attached surfaces for the front buffer
1179 */
1180 EnterCriticalSection(&ddraw_cs);
1181 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1182 0, /* SwapChain */
1183 0, /* first back buffer*/
1184 WINED3DBACKBUFFER_TYPE_MONO,
1185 &Surf);
1186
1187 if( (hr != D3D_OK) ||
1188 (!Surf) )
1189 {
1190 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1191 LeaveCriticalSection(&ddraw_cs);
1192 return DDERR_NOTFOUND;
1193 }
1194
1195 /* GetBackBuffer AddRef()ed the surface, release it */
1196 IWineD3DSurface_Release(Surf);
1197
1198 IWineD3DSurface_GetParent(Surf,
1199 (IUnknown **) &ddsurf);
1200 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1201
1202 /* Find the front buffer */
1203 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1204 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1205 &ddsCaps,
1206 GDISurface);
1207 if(hr != DD_OK)
1208 {
1209 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1210 }
1211
1212 /* The AddRef is OK this time */
1213 LeaveCriticalSection(&ddraw_cs);
1214 return hr;
1215 }
1216
1217 /*****************************************************************************
1218 * IDirectDraw7::EnumDisplayModes
1219 *
1220 * Enumerates the supported Display modes. The modes can be filtered with
1221 * the DDSD parameter.
1222 *
1223 * Params:
1224 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1225 * DDSD: Surface description to filter the modes
1226 * Context: Pointer passed back to the callback function
1227 * cb: Application-provided callback function
1228 *
1229 * Returns:
1230 * DD_OK on success
1231 * DDERR_INVALIDPARAMS if the callback wasn't set
1232 *
1233 *****************************************************************************/
1234 static HRESULT WINAPI
1235 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1236 DWORD Flags,
1237 DDSURFACEDESC2 *DDSD,
1238 void *Context,
1239 LPDDENUMMODESCALLBACK2 cb)
1240 {
1241 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1242 unsigned int modenum, fmt;
1243 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1244 WINED3DDISPLAYMODE mode;
1245 DDSURFACEDESC2 callback_sd;
1246 WINED3DDISPLAYMODE *enum_modes = NULL;
1247 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1248
1249 WINED3DFORMAT checkFormatList[] =
1250 {
1251 WINED3DFMT_R8G8B8,
1252 WINED3DFMT_A8R8G8B8,
1253 WINED3DFMT_X8R8G8B8,
1254 WINED3DFMT_R5G6B5,
1255 WINED3DFMT_X1R5G5B5,
1256 WINED3DFMT_A1R5G5B5,
1257 WINED3DFMT_A4R4G4B4,
1258 WINED3DFMT_R3G3B2,
1259 WINED3DFMT_A8R3G3B2,
1260 WINED3DFMT_X4R4G4B4,
1261 WINED3DFMT_A2B10G10R10,
1262 WINED3DFMT_A8B8G8R8,
1263 WINED3DFMT_X8B8G8R8,
1264 WINED3DFMT_A2R10G10B10,
1265 WINED3DFMT_A8P8,
1266 WINED3DFMT_P8
1267 };
1268
1269 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1270
1271 EnterCriticalSection(&ddraw_cs);
1272 /* This looks sane */
1273 if(!cb)
1274 {
1275 LeaveCriticalSection(&ddraw_cs);
1276 return DDERR_INVALIDPARAMS;
1277 }
1278
1279 if(DDSD)
1280 {
1281 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1282 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1283 }
1284
1285 if(!(Flags & DDEDM_REFRESHRATES))
1286 {
1287 enum_mode_array_size = 16;
1288 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1289 if (!enum_modes)
1290 {
1291 ERR("Out of memory\n");
1292 LeaveCriticalSection(&ddraw_cs);
1293 return DDERR_OUTOFMEMORY;
1294 }
1295 }
1296
1297 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1298 {
1299 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1300 {
1301 continue;
1302 }
1303
1304 modenum = 0;
1305 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1306 WINED3DADAPTER_DEFAULT,
1307 checkFormatList[fmt],
1308 modenum++,
1309 &mode) == WINED3D_OK)
1310 {
1311 if(DDSD)
1312 {
1313 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1314 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1315 }
1316
1317 if(!(Flags & DDEDM_REFRESHRATES))
1318 {
1319 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1320 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1321 * to be reduced to a single unique result in such case.
1322 */
1323 BOOL found = FALSE;
1324 unsigned i;
1325
1326 for (i = 0; i < enum_mode_count; i++)
1327 {
1328 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1329 enum_modes[i].Format == mode.Format)
1330 {
1331 found = TRUE;
1332 break;
1333 }
1334 }
1335
1336 if(found) continue;
1337 }
1338
1339 memset(&callback_sd, 0, sizeof(callback_sd));
1340 callback_sd.dwSize = sizeof(callback_sd);
1341 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1342
1343 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1344 if(Flags & DDEDM_REFRESHRATES)
1345 {
1346 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1347 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1348 }
1349
1350 callback_sd.dwWidth = mode.Width;
1351 callback_sd.dwHeight = mode.Height;
1352
1353 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1354
1355 /* Calc pitch and DWORD align like MSDN says */
1356 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1357 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1358
1359 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1360 callback_sd.u2.dwRefreshRate);
1361
1362 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1363 {
1364 TRACE("Application asked to terminate the enumeration\n");
1365 HeapFree(GetProcessHeap(), 0, enum_modes);
1366 LeaveCriticalSection(&ddraw_cs);
1367 return DD_OK;
1368 }
1369
1370 if(!(Flags & DDEDM_REFRESHRATES))
1371 {
1372 if (enum_mode_count == enum_mode_array_size)
1373 {
1374 WINED3DDISPLAYMODE *new_enum_modes;
1375
1376 enum_mode_array_size *= 2;
1377 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1378
1379 if (!new_enum_modes)
1380 {
1381 ERR("Out of memory\n");
1382 HeapFree(GetProcessHeap(), 0, enum_modes);
1383 LeaveCriticalSection(&ddraw_cs);
1384 return DDERR_OUTOFMEMORY;
1385 }
1386
1387 enum_modes = new_enum_modes;
1388 }
1389
1390 enum_modes[enum_mode_count++] = mode;
1391 }
1392 }
1393 }
1394
1395 TRACE("End of enumeration\n");
1396 HeapFree(GetProcessHeap(), 0, enum_modes);
1397 LeaveCriticalSection(&ddraw_cs);
1398 return DD_OK;
1399 }
1400
1401 /*****************************************************************************
1402 * IDirectDraw7::EvaluateMode
1403 *
1404 * Used with IDirectDraw7::StartModeTest to test video modes.
1405 * EvaluateMode is used to pass or fail a mode, and continue with the next
1406 * mode
1407 *
1408 * Params:
1409 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1410 * Timeout: Returns the amount of seconds left before the mode would have
1411 * been failed automatically
1412 *
1413 * Returns:
1414 * This implementation always DD_OK, because it's a stub
1415 *
1416 *****************************************************************************/
1417 static HRESULT WINAPI
1418 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1419 DWORD Flags,
1420 DWORD *Timeout)
1421 {
1422 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1423 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1424
1425 /* When implementing this, implement it in WineD3D */
1426
1427 return DD_OK;
1428 }
1429
1430 /*****************************************************************************
1431 * IDirectDraw7::GetDeviceIdentifier
1432 *
1433 * Returns the device identifier, which gives information about the driver
1434 * Our device identifier is defined at the beginning of this file.
1435 *
1436 * Params:
1437 * DDDI: Address for the returned structure
1438 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1439 *
1440 * Returns:
1441 * On success it returns DD_OK
1442 * DDERR_INVALIDPARAMS if DDDI is NULL
1443 *
1444 *****************************************************************************/
1445 static HRESULT WINAPI
1446 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1447 DDDEVICEIDENTIFIER2 *DDDI,
1448 DWORD Flags)
1449 {
1450 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1451 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1452
1453 if(!DDDI)
1454 return DDERR_INVALIDPARAMS;
1455
1456 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1457 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1458 * to any modern hardware, nor is it interesting for Wine, so ignore it
1459 */
1460
1461 *DDDI = deviceidentifier;
1462 return DD_OK;
1463 }
1464
1465 /*****************************************************************************
1466 * IDirectDraw7::GetSurfaceFromDC
1467 *
1468 * Returns the Surface for a GDI device context handle.
1469 * Is this related to IDirectDrawSurface::GetDC ???
1470 *
1471 * Params:
1472 * hdc: hdc to return the surface for
1473 * Surface: Address to write the surface pointer to
1474 *
1475 * Returns:
1476 * Always returns DD_OK because it's a stub
1477 *
1478 *****************************************************************************/
1479 static HRESULT WINAPI
1480 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1481 HDC hdc,
1482 IDirectDrawSurface7 **Surface)
1483 {
1484 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1485 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1486
1487 /* Implementation idea if needed: Loop through all surfaces and compare
1488 * their hdc with hdc. Implement it in WineD3D! */
1489 return DDERR_NOTFOUND;
1490 }
1491
1492 /*****************************************************************************
1493 * IDirectDraw7::RestoreAllSurfaces
1494 *
1495 * Calls the restore method of all surfaces
1496 *
1497 * Params:
1498 *
1499 * Returns:
1500 * Always returns DD_OK because it's a stub
1501 *
1502 *****************************************************************************/
1503 static HRESULT WINAPI
1504 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1505 {
1506 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1507 FIXME("(%p): Stub\n", This);
1508
1509 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1510 * get their parent and call their restore method. Do not implement
1511 * it in WineD3D, as restoring a surface means re-creating the
1512 * WineD3DDSurface
1513 */
1514 return DD_OK;
1515 }
1516
1517 /*****************************************************************************
1518 * IDirectDraw7::StartModeTest
1519 *
1520 * Tests the specified video modes to update the system registry with
1521 * refresh rate information. StartModeTest starts the mode test,
1522 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1523 * isn't called within 15 seconds, the mode is failed automatically
1524 *
1525 * As refresh rates are handled by the X server, I don't think this
1526 * Method is important
1527 *
1528 * Params:
1529 * Modes: An array of mode specifications
1530 * NumModes: The number of modes in Modes
1531 * Flags: Some flags...
1532 *
1533 * Returns:
1534 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1535 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1536 * otherwise DD_OK
1537 *
1538 *****************************************************************************/
1539 static HRESULT WINAPI
1540 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1541 SIZE *Modes,
1542 DWORD NumModes,
1543 DWORD Flags)
1544 {
1545 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1546 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1547
1548 /* This looks sane */
1549 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1550
1551 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1552 * As it is not, DDERR_TESTFINISHED is returned
1553 * (hopefully that's correct
1554 *
1555 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1556 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1557 */
1558
1559 return DD_OK;
1560 }
1561
1562 /*****************************************************************************
1563 * IDirectDrawImpl_RecreateSurfacesCallback
1564 *
1565 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1566 * It re-recreates the WineD3DSurface. It's pretty straightforward
1567 *
1568 *****************************************************************************/
1569 HRESULT WINAPI
1570 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1571 DDSURFACEDESC2 *desc,
1572 void *Context)
1573 {
1574 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1575 IDirectDrawSurface7,
1576 surf);
1577 IDirectDrawImpl *This = surfImpl->ddraw;
1578 IUnknown *Parent;
1579 IParentImpl *parImpl = NULL;
1580 IWineD3DSurface *wineD3DSurface;
1581 HRESULT hr;
1582 void *tmp;
1583 IWineD3DClipper *clipper = NULL;
1584
1585 WINED3DSURFACE_DESC Desc;
1586 WINED3DFORMAT Format;
1587 WINED3DRESOURCETYPE Type;
1588 DWORD Usage;
1589 WINED3DPOOL Pool;
1590 UINT Size;
1591
1592 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1593 DWORD MultiSampleQuality;
1594 UINT Width;
1595 UINT Height;
1596
1597 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1598
1599 /* For the enumeration */
1600 IDirectDrawSurface7_Release(surf);
1601
1602 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1603
1604 /* Get the objects */
1605 wineD3DSurface = surfImpl->WineD3DSurface;
1606 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1607 IUnknown_Release(Parent); /* For the getParent */
1608
1609 /* Is the parent an IParent interface? */
1610 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1611 {
1612 /* It is a IParent interface! */
1613 IUnknown_Release(Parent); /* For the QueryInterface */
1614 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1615 /* Release the reference the parent interface is holding */
1616 IWineD3DSurface_Release(wineD3DSurface);
1617 }
1618
1619 /* get the clipper */
1620 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1621
1622 /* Get the surface properties */
1623 Desc.Format = &Format;
1624 Desc.Type = &Type;
1625 Desc.Usage = &Usage;
1626 Desc.Pool = &Pool;
1627 Desc.Size = &Size;
1628 Desc.MultiSampleType = &MultiSampleType;
1629 Desc.MultiSampleQuality = &MultiSampleQuality;
1630 Desc.Width = &Width;
1631 Desc.Height = &Height;
1632
1633 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1634 if(hr != D3D_OK) return hr;
1635
1636 /* Create the new surface */
1637 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1638 Width, Height, Format,
1639 TRUE /* Lockable */,
1640 FALSE /* Discard */,
1641 surfImpl->mipmap_level,
1642 &surfImpl->WineD3DSurface,
1643 Type,
1644 Usage,
1645 Pool,
1646 MultiSampleType,
1647 MultiSampleQuality,
1648 0 /* SharedHandle */,
1649 This->ImplType,
1650 Parent);
1651
1652 if(hr != D3D_OK)
1653 return hr;
1654
1655 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1656
1657 /* Update the IParent if it exists */
1658 if(parImpl)
1659 {
1660 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1661 /* Add a reference for the IParent */
1662 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1663 }
1664 /* TODO: Copy the surface content, except for render targets */
1665
1666 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1667 TRACE("Surface released successful, next surface\n");
1668 else
1669 ERR("Something's still holding the old WineD3DSurface\n");
1670
1671 surfImpl->ImplType = This->ImplType;
1672
1673 if(clipper)
1674 {
1675 IWineD3DClipper_Release(clipper);
1676 }
1677 return DDENUMRET_OK;
1678 }
1679
1680 /*****************************************************************************
1681 * IDirectDrawImpl_RecreateAllSurfaces
1682 *
1683 * A function, that converts all wineD3DSurfaces to the new implementation type
1684 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1685 * new WineD3DSurface, copies the content and releases the old surface
1686 *
1687 *****************************************************************************/
1688 static HRESULT
1689 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1690 {
1691 DDSURFACEDESC2 desc;
1692 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1693
1694 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1695 {
1696 /* Should happen almost never */
1697 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1698 /* Shutdown d3d */
1699 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1700 }
1701 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1702
1703 memset(&desc, 0, sizeof(desc));
1704 desc.dwSize = sizeof(desc);
1705
1706 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1707 0,
1708 &desc,
1709 This,
1710 IDirectDrawImpl_RecreateSurfacesCallback);
1711 }
1712
1713 /*****************************************************************************
1714 * D3D7CB_CreateSurface
1715 *
1716 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1717 * correct mipmap sublevel, and returns it to WineD3D.
1718 * The surfaces are created already by IDirectDraw7::CreateSurface
1719 *
1720 * Params:
1721 * With, Height: With and height of the surface
1722 * Format: The requested format
1723 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1724 * level: The mipmap level
1725 * Face: The cube map face type
1726 * Surface: Pointer to pass the created surface back at
1727 * SharedHandle: NULL
1728 *
1729 * Returns:
1730 * D3D_OK
1731 *
1732 *****************************************************************************/
1733 static HRESULT WINAPI
1734 D3D7CB_CreateSurface(IUnknown *device,
1735 IUnknown *pSuperior,
1736 UINT Width, UINT Height,
1737 WINED3DFORMAT Format,
1738 DWORD Usage, WINED3DPOOL Pool, UINT level,
1739 WINED3DCUBEMAP_FACES Face,
1740 IWineD3DSurface **Surface,
1741 HANDLE *SharedHandle)
1742 {
1743 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1744 IDirectDrawSurfaceImpl *surf = NULL;
1745 int i = 0;
1746 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1747 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1748
1749 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1750 switch(Face)
1751 {
1752 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1753 TRACE("Asked for positive x\n");
1754 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1755 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1756 }
1757 surf = This->tex_root; break;
1758 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1759 TRACE("Asked for negative x\n");
1760 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1761 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1762 TRACE("Asked for positive y\n");
1763 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1764 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1765 TRACE("Asked for negative y\n");
1766 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1767 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1768 TRACE("Asked for positive z\n");
1769 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1770 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1771 TRACE("Asked for negative z\n");
1772 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1773 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1774 }
1775
1776 if(!surf)
1777 {
1778 IDirectDrawSurface7 *attached;
1779 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1780 &searchcaps,
1781 &attached);
1782 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1783 IDirectDrawSurface7_Release(attached);
1784 }
1785 if(!surf) ERR("root search surface not found\n");
1786
1787 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1788 while(i < level)
1789 {
1790 IDirectDrawSurface7 *attached;
1791 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1792 &searchcaps,
1793 &attached);
1794 if(!attached) ERR("Surface not found\n");
1795 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1796 IDirectDrawSurface7_Release(attached);
1797 i++;
1798 }
1799
1800 /* Return the surface */
1801 *Surface = surf->WineD3DSurface;
1802
1803 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1804 return D3D_OK;
1805 }
1806
1807 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1808 IUnknown* swapChainParent;
1809 TRACE("(%p) call back\n", pSwapChain);
1810
1811 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1812 IUnknown_Release(swapChainParent);
1813 return IUnknown_Release(swapChainParent);
1814 }
1815
1816 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1817 IUnknown* surfaceParent;
1818 TRACE("(%p) call back\n", pSurface);
1819
1820 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1821 IUnknown_Release(surfaceParent);
1822 return IUnknown_Release(surfaceParent);
1823 }
1824
1825 /*****************************************************************************
1826 * IDirectDrawImpl_CreateNewSurface
1827 *
1828 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1829 * with the passed parameters.
1830 *
1831 * Params:
1832 * DDSD: Description of the surface to create
1833 * Surf: Address to store the interface pointer at
1834 *
1835 * Returns:
1836 * DD_OK on success
1837 *
1838 *****************************************************************************/
1839 static HRESULT WINAPI
1840 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1841 DDSURFACEDESC2 *pDDSD,
1842 IDirectDrawSurfaceImpl **ppSurf,
1843 UINT level)
1844 {
1845 HRESULT hr;
1846 UINT Width = 0, Height = 0;
1847 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1848 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1849 DWORD Usage = 0;
1850 WINED3DSURFTYPE ImplType = This->ImplType;
1851 WINED3DSURFACE_DESC Desc;
1852 IUnknown *Parent;
1853 IParentImpl *parImpl = NULL;
1854 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1855
1856 /* Dummies for GetDesc */
1857 WINED3DPOOL dummy_d3dpool;
1858 WINED3DMULTISAMPLE_TYPE dummy_mst;
1859 UINT dummy_uint;
1860 DWORD dummy_dword;
1861
1862 if (TRACE_ON(ddraw))
1863 {
1864 TRACE(" (%p) Requesting surface desc :\n", This);
1865 DDRAW_dump_surface_desc(pDDSD);
1866 }
1867
1868 /* Select the surface type, if it wasn't choosen yet */
1869 if(ImplType == SURFACE_UNKNOWN)
1870 {
1871 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1872 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1873 {
1874 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1875 ImplType = SURFACE_OPENGL;
1876 }
1877
1878 /* Otherwise use GDI surfaces for now */
1879 else
1880 {
1881 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1882 ImplType = SURFACE_GDI;
1883 }
1884
1885 /* Policy if all surface implementations are available:
1886 * First, check if a default type was set with winecfg. If not,
1887 * try Xrender surfaces, and use them if they work. Next, check if
1888 * accelerated OpenGL is available, and use GL surfaces in this
1889 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1890 * was created, always use OpenGL surfaces.
1891 *
1892 * (Note: Xrender surfaces are not implemented for now, the
1893 * unaccelerated implementation uses GDI to render in Software)
1894 */
1895
1896 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1897 * be re-created. This could be done with IDirectDrawSurface7::Restore
1898 */
1899 This->ImplType = ImplType;
1900 }
1901 else
1902 {
1903 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1904 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1905 {
1906 /* We have to change to OpenGL,
1907 * and re-create all WineD3DSurfaces
1908 */
1909 ImplType = SURFACE_OPENGL;
1910 This->ImplType = ImplType;
1911 TRACE("(%p) Re-creating all surfaces\n", This);
1912 IDirectDrawImpl_RecreateAllSurfaces(This);
1913 TRACE("(%p) Done recreating all surfaces\n", This);
1914 }
1915 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1916 {
1917 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1918 /* Do not fail surface creation, only fail 3D device creation */
1919 }
1920 }
1921
1922 /* Get the correct wined3d usage */
1923 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1924 DDSCAPS_BACKBUFFER |
1925 DDSCAPS_3DDEVICE ) )
1926 {
1927 Usage |= WINED3DUSAGE_RENDERTARGET;
1928
1929 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1930 DDSCAPS_VISIBLE;
1931 }
1932 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1933 {
1934 Usage |= WINED3DUSAGE_OVERLAY;
1935 }
1936 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1937 {
1938 /* The depth stencil creation callback sets this flag.
1939 * Set the WineD3D usage to let it know that it's a depth
1940 * Stencil surface.
1941 */
1942 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1943 }
1944 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1945 {
1946 Pool = WINED3DPOOL_SYSTEMMEM;
1947 }
1948 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1949 {
1950 Pool = WINED3DPOOL_MANAGED;
1951 /* Managed textures have the system memory flag set */
1952 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1953 }
1954 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1955 {
1956 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1957 * and texturemanage
1958 */
1959 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1960 }
1961
1962 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1963 if(Format == WINED3DFMT_UNKNOWN)
1964 {
1965 ERR("Unsupported / Unknown pixelformat\n");
1966 return DDERR_INVALIDPIXELFORMAT;
1967 }
1968
1969 /* Create the Surface object */
1970 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1971 if(!*ppSurf)
1972 {
1973 ERR("(%p) Error allocating memory for a surface\n", This);
1974 return DDERR_OUTOFVIDEOMEMORY;
1975 }
1976 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1977 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1978 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1979 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1980 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1981 (*ppSurf)->ref = 1;
1982 (*ppSurf)->version = 7;
1983 (*ppSurf)->ddraw = This;
1984 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1985 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1986 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1987
1988 /* Surface attachments */
1989 (*ppSurf)->next_attached = NULL;
1990 (*ppSurf)->first_attached = *ppSurf;
1991
1992 /* Needed to re-create the surface on an implementation change */
1993 (*ppSurf)->ImplType = ImplType;
1994
1995 /* For D3DDevice creation */
1996 (*ppSurf)->isRenderTarget = FALSE;
1997
1998 /* A trace message for debugging */
1999 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2000
2001 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2002 {
2003 /* Render targets and textures need a IParent interface,
2004 * because WineD3D will destroy them when the swapchain
2005 * is released
2006 */
2007 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2008 if(!parImpl)
2009 {
2010 ERR("Out of memory when allocating memory for a IParent implementation\n");
2011 return DDERR_OUTOFMEMORY;
2012 }
2013 parImpl->ref = 1;
2014 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2015 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2016 TRACE("Using IParent interface %p as parent\n", parImpl);
2017 }
2018 else
2019 {
2020 /* Use the surface as parent */
2021 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2022 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2023 }
2024
2025 /* Now create the WineD3D Surface */
2026 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2027 pDDSD->dwWidth,
2028 pDDSD->dwHeight,
2029 Format,
2030 TRUE /* Lockable */,
2031 FALSE /* Discard */,
2032 level,
2033 &(*ppSurf)->WineD3DSurface,
2034 ResType, Usage,
2035 Pool,
2036 WINED3DMULTISAMPLE_NONE,
2037 0 /* MultiSampleQuality */,
2038 0 /* SharedHandle */,
2039 ImplType,
2040 Parent);
2041
2042 if(hr != D3D_OK)
2043 {
2044 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2045 return hr;
2046 }
2047
2048 /* Set the child of the parent implementation if it exists */
2049 if(parImpl)
2050 {
2051 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2052 /* The IParent releases the WineD3DSurface, and
2053 * the ddraw surface does that too. Hold a reference
2054 */
2055 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2056 }
2057
2058 /* Increase the surface counter, and attach the surface */
2059 InterlockedIncrement(&This->surfaces);
2060 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2061
2062 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2063 * But this could also be delegated to WineDDraw, as it keeps track of all its
2064 * resources. Not implemented for now, as there are more important things ;)
2065 */
2066
2067 /* Get the pixel format of the WineD3DSurface and store it.
2068 * Don't use the Format choosen above, WineD3D might have
2069 * changed it
2070 */
2071 Desc.Format = &Format;
2072 Desc.Type = &ResType;
2073 Desc.Usage = &Usage;
2074 Desc.Pool = &dummy_d3dpool;
2075 Desc.Size = &dummy_uint;
2076 Desc.MultiSampleType = &dummy_mst;
2077 Desc.MultiSampleQuality = &dummy_dword;
2078 Desc.Width = &Width;
2079 Desc.Height = &Height;
2080
2081 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2082 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2083 if(hr != D3D_OK)
2084 {
2085 ERR("IWineD3DSurface::GetDesc failed\n");
2086 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2087 return hr;
2088 }
2089
2090 if(Format == WINED3DFMT_UNKNOWN)
2091 {
2092 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2093 }
2094 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2095
2096 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2097 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2098 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2099 * TODO: Test other fourcc formats
2100 */
2101 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2102 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2103 {
2104 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2105 if(Format == WINED3DFMT_DXT1)
2106 {
2107 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2108 }
2109 else
2110 {
2111 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2112 }
2113 }
2114 else
2115 {
2116 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2117 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2118 }
2119
2120 /* Application passed a color key? Set it! */
2121 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2122 {
2123 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2124 DDCKEY_DESTOVERLAY,
2125 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2126 }
2127 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2128 {
2129 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2130 DDCKEY_DESTBLT,
2131 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2132 }
2133 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2134 {
2135 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2136 DDCKEY_SRCOVERLAY,
2137 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2138 }
2139 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2140 {
2141 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2142 DDCKEY_SRCBLT,
2143 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2144 }
2145 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2146 {
2147 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2148 if(hr != WINED3D_OK)
2149 {
2150 /* No need for a trace here, wined3d does that for us */
2151 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2152 return hr;
2153 }
2154 }
2155
2156 return DD_OK;
2157 }
2158 /*****************************************************************************
2159 * CreateAdditionalSurfaces
2160 *
2161 * Creates a new mipmap chain.
2162 *
2163 * Params:
2164 * root: Root surface to attach the newly created chain to
2165 * count: number of surfaces to create
2166 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2167 * effects on the caller
2168 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2169 * creates an additional surface without the mipmapping flags
2170 *
2171 *****************************************************************************/
2172 static HRESULT
2173 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2174 IDirectDrawSurfaceImpl *root,
2175 UINT count,
2176 DDSURFACEDESC2 DDSD,
2177 BOOL CubeFaceRoot)
2178 {
2179 UINT i, j, level = 0;
2180 HRESULT hr;
2181 IDirectDrawSurfaceImpl *last = root;
2182
2183 for(i = 0; i < count; i++)
2184 {
2185 IDirectDrawSurfaceImpl *object2 = NULL;
2186
2187 /* increase the mipmap level, but only if a mipmap is created
2188 * In this case, also halve the size
2189 */
2190 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2191 {
2192 level++;
2193 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2194 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2195 /* Set the mipmap sublevel flag according to msdn */
2196 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2197 }
2198 else
2199 {
2200 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2201 }
2202 CubeFaceRoot = FALSE;
2203
2204 hr = IDirectDrawImpl_CreateNewSurface(This,
2205 &DDSD,
2206 &object2,
2207 level);
2208 if(hr != DD_OK)
2209 {
2210 return hr;
2211 }
2212
2213 /* Add the new surface to the complex attachment array */
2214 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2215 {
2216 if(last->complex_array[j]) continue;
2217 last->complex_array[j] = object2;
2218 break;
2219 }
2220 last = object2;
2221
2222 /* Remove the (possible) back buffer cap from the new surface description,
2223 * because only one surface in the flipping chain is a back buffer, one
2224 * is a front buffer, the others are just primary surfaces.
2225 */
2226 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2227 }
2228 return DD_OK;
2229 }
2230
2231 /*****************************************************************************
2232 * IDirectDraw7::CreateSurface
2233 *
2234 * Creates a new IDirectDrawSurface object and returns its interface.
2235 *
2236 * The surface connections with wined3d are a bit tricky. Basically it works
2237 * like this:
2238 *
2239 * |------------------------| |-----------------|
2240 * | DDraw surface | | WineD3DSurface |
2241 * | | | |
2242 * | WineD3DSurface |-------------->| |
2243 * | Child |<------------->| Parent |
2244 * |------------------------| |-----------------|
2245 *
2246 * The DDraw surface is the parent of the wined3d surface, and it releases
2247 * the WineD3DSurface when the ddraw surface is destroyed.
2248 *
2249 * However, for all surfaces which can be in a container in WineD3D,
2250 * we have to do this. These surfaces are usually complex surfaces,
2251 * so this concerns primary surfaces with a front and a back buffer,
2252 * and textures.
2253 *
2254 * |------------------------| |-----------------|
2255 * | DDraw surface | | Container |
2256 * | | | |
2257 * | Child |<------------->| Parent |
2258 * | Texture |<------------->| |
2259 * | WineD3DSurface |<----| | Levels |<--|
2260 * | Complex connection | | | | |
2261 * |------------------------| | |-----------------| |
2262 * ^ | |
2263 * | | |
2264 * | | |
2265 * | |------------------| | |-----------------| |
2266 * | | IParent | |-------->| WineD3DSurface | |
2267 * | | | | | |
2268 * | | Child |<------------->| Parent | |
2269 * | | | | Container |<--|
2270 * | |------------------| |-----------------| |
2271 * | |
2272 * | |----------------------| |
2273 * | | DDraw surface 2 | |
2274 * | | | |
2275 * |<->| Complex root Child | |
2276 * | | Texture | |
2277 * | | WineD3DSurface |<----| |
2278 * | |----------------------| | |
2279 * | | |
2280 * | |---------------------| | |-----------------| |
2281 * | | IParent | |----->| WineD3DSurface | |
2282 * | | | | | |
2283 * | | Child |<---------->| Parent | |
2284 * | |---------------------| | Container |<--|
2285 * | |-----------------| |
2286 * | |
2287 * | ---More surfaces can follow--- |
2288 *
2289 * The reason is that the IWineD3DSwapchain(render target container)
2290 * and the IWineD3DTexure(Texture container) release the parents
2291 * of their surface's children, but by releasing the complex root
2292 * the surfaces which are complexly attached to it are destroyed
2293 * too. See IDirectDrawSurface::Release for a more detailed
2294 * explanation.
2295 *
2296 * Params:
2297 * DDSD: Description of the surface to create
2298 * Surf: Address to store the interface pointer at
2299 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2300 * aggregation, so it has to be NULL
2301 *
2302 * Returns:
2303 * DD_OK on success
2304 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2305 * DDERR_* if an error occurs
2306 *
2307 *****************************************************************************/
2308 static HRESULT WINAPI
2309 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2310 DDSURFACEDESC2 *DDSD,
2311 IDirectDrawSurface7 **Surf,
2312 IUnknown *UnkOuter)
2313 {
2314 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2315 IDirectDrawSurfaceImpl *object = NULL;
2316 HRESULT hr;
2317 LONG extra_surfaces = 0;
2318 DDSURFACEDESC2 desc2;
2319 WINED3DDISPLAYMODE Mode;
2320
2321 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2322
2323 /* Some checks before we start */
2324 if (TRACE_ON(ddraw))
2325 {
2326 TRACE(" (%p) Requesting surface desc :\n", This);
2327 DDRAW_dump_surface_desc(DDSD);
2328 }
2329 EnterCriticalSection(&ddraw_cs);
2330
2331 if (UnkOuter != NULL)
2332 {
2333 FIXME("(%p) : outer != NULL?\n", This);
2334 LeaveCriticalSection(&ddraw_cs);
2335 return CLASS_E_NOAGGREGATION; /* unchecked */
2336 }
2337
2338 if (Surf == NULL)
2339 {
2340 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2341 LeaveCriticalSection(&ddraw_cs);
2342 return E_POINTER; /* unchecked */
2343 }
2344
2345 if (!(DDSD->dwFlags & DDSD_CAPS))
2346 {
2347 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2348 DDSD->dwFlags |= DDSD_CAPS;
2349 }
2350 if (DDSD->ddsCaps.dwCaps == 0)
2351 {
2352 /* This has been checked on real Windows */
2353 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2354 }
2355
2356 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2357 {
2358 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2359 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2360 }
2361
2362 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2363 {
2364 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2365 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2366 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2367 }
2368
2369 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2370 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2371 {
2372 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2373 *Surf = NULL;
2374 LeaveCriticalSection(&ddraw_cs);
2375 return DDERR_NOEXCLUSIVEMODE;
2376 }
2377
2378 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2379 WARN("Application tried to create an explicit front or back buffer\n");
2380 LeaveCriticalSection(&ddraw_cs);
2381 return DDERR_INVALIDCAPS;
2382 }
2383 /* Check cube maps but only if the size includes them */
2384 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2385 {
2386 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2387 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2388 {
2389 WARN("Cube map faces requested without cube map flag\n");
2390 LeaveCriticalSection(&ddraw_cs);
2391 return DDERR_INVALIDCAPS;
2392 }
2393 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2394 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2395 {
2396 WARN("Cube map without faces requested\n");
2397 LeaveCriticalSection(&ddraw_cs);
2398 return DDERR_INVALIDPARAMS;
2399 }
2400
2401 /* Quick tests confirm those can be created, but we don't do that yet */
2402 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2403 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2404 {
2405 FIXME("Partial cube maps not supported yet\n");
2406 }
2407 }
2408
2409 /* According to the msdn this flag is ignored by CreateSurface */
2410 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2411 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2412
2413 /* Modify some flags */
2414 memset(&desc2, 0, sizeof(desc2));
2415 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2416 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2417 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2418 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2419
2420 /* Get the video mode from WineD3D - we will need it */
2421 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2422 0, /* Swapchain 0 */
2423 &Mode);
2424 if(FAILED(hr))
2425 {
2426 ERR("Failed to read display mode from wined3d\n");
2427 switch(This->orig_bpp)
2428 {
2429 case 8:
2430 Mode.Format = WINED3DFMT_P8;
2431 break;
2432
2433 case 15:
2434 Mode.Format = WINED3DFMT_X1R5G5B5;
2435 break;
2436
2437 case 16:
2438 Mode.Format = WINED3DFMT_R5G6B5;
2439 break;
2440
2441 case 24:
2442 Mode.Format = WINED3DFMT_R8G8B8;
2443 break;
2444
2445 case 32:
2446 Mode.Format = WINED3DFMT_X8R8G8B8;
2447 break;
2448 }
2449 Mode.Width = This->orig_width;
2450 Mode.Height = This->orig_height;
2451 }
2452
2453 /* No pixelformat given? Use the current screen format */
2454 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2455 {
2456 desc2.dwFlags |= DDSD_PIXELFORMAT;
2457 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2458
2459 /* Wait: It could be a Z buffer */
2460 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2461 {
2462 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2463 {
2464 case 15:
2465 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2466 break;
2467 case 16:
2468 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2469 break;
2470 case 24:
2471 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2472 break;
2473 case 32:
2474 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2475 break;
2476 default:
2477 ERR("Unknown Z buffer bit depth\n");
2478 }
2479 }
2480 else
2481 {
2482 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2483 }
2484 }
2485
2486 /* No Width or no Height? Use the original screen size
2487 */
2488 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2489 !(desc2.dwFlags & DDSD_HEIGHT) )
2490 {
2491 /* Invalid for non-render targets */
2492 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2493 {
2494 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2495 *Surf = NULL;
2496 LeaveCriticalSection(&ddraw_cs);
2497 return DDERR_INVALIDPARAMS;
2498 }
2499
2500 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2501 desc2.dwWidth = Mode.Width;
2502 desc2.dwHeight = Mode.Height;
2503 }
2504
2505 /* Mipmap count fixes */
2506 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2507 {
2508 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2509 {
2510 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2511 {
2512 /* Mipmap count is given, should not be 0 */
2513 if( desc2.u2.dwMipMapCount == 0 )
2514 {
2515 LeaveCriticalSection(&ddraw_cs);
2516 return DDERR_INVALIDPARAMS;
2517 }
2518 }
2519 else
2520 {
2521 /* Undocumented feature: Create sublevels until
2522 * either the width or the height is 1
2523 */
2524 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2525 desc2.dwWidth : desc2.dwHeight;
2526 desc2.u2.dwMipMapCount = 0;
2527 while( min )
2528 {
2529 desc2.u2.dwMipMapCount += 1;
2530 min >>= 1;
2531 }
2532 }
2533 }
2534 else
2535 {
2536 /* Not-complex mipmap -> Mipmapcount = 1 */
2537 desc2.u2.dwMipMapCount = 1;
2538 }
2539 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2540
2541 /* There's a mipmap count in the created surface in any case */
2542 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2543 }
2544 /* If no mipmap is given, the texture has only one level */
2545
2546 /* The first surface is a front buffer, the back buffer is created afterwards */
2547 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2548 {
2549 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2550 }
2551
2552 /* The root surface in a cube map is positive x */
2553 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2554 {
2555 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2556 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2557 }
2558
2559 /* Create the first surface */
2560 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2561 if( hr != DD_OK)
2562 {
2563 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2564 LeaveCriticalSection(&ddraw_cs);
2565 return hr;
2566 }
2567 object->is_complex_root = TRUE;
2568
2569 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2570
2571 /* Create Additional surfaces if necessary
2572 * This applies to Primary surfaces which have a back buffer count
2573 * set, but not to mipmap textures. In case of Mipmap textures,
2574 * wineD3D takes care of the creation of additional surfaces
2575 */
2576 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2577 {
2578 extra_surfaces = DDSD->dwBackBufferCount;
2579 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2580 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2581 }
2582
2583 hr = DD_OK;
2584 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2585 {
2586 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2587 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2588 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2589 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2590 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2591 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2592 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2593 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2594 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2595 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2596 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2597 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2598 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2599 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2600 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2601 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2602 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2603 }
2604
2605 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2606 if(hr != DD_OK)
2607 {
2608 /* This destroys and possibly created surfaces too */
2609 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2610 LeaveCriticalSection(&ddraw_cs);
2611 return hr;
2612 }
2613
2614 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2615 * But attach the d3ddevice only if the currently created surface was
2616 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2617 * The only case I can think of where this doesn't apply is when a
2618 * 2D app was configured by the user to run with OpenGL and it didn't create
2619 * the render target as first surface. In this case the render target creation
2620 * will cause the 3D init.
2621 */
2622 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2623 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2624 {
2625 IDirectDrawSurfaceImpl *target = object, *surface;
2626 struct list *entry;
2627
2628 /* Search for the primary to use as render target */
2629 LIST_FOR_EACH(entry, &This->surface_list)
2630 {
2631 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2632 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2633 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2634 {
2635 /* found */
2636 target = surface;
2637 TRACE("Using primary %p as render target\n", target);
2638 break;
2639 }
2640 }
2641
2642 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2643 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2644 if(hr != D3D_OK)
2645 {
2646 IDirectDrawSurfaceImpl *release_surf;
2647 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2648 *Surf = NULL;
2649
2650 /* The before created surface structures are in an incomplete state here.
2651 * WineD3D holds the reference on the IParents, and it released them on the failure
2652 * already. So the regular release method implementation would fail on the attempt
2653 * to destroy either the IParents or the swapchain. So free the surface here.
2654 * The surface structure here is a list, not a tree, because onscreen targets
2655 * cannot be cube textures
2656 */
2657 while(object)
2658 {
2659 release_surf = object;
2660 object = object->complex_array[0];
2661 IDirectDrawSurfaceImpl_Destroy(release_surf);
2662 }
2663 LeaveCriticalSection(&ddraw_cs);
2664 return hr;
2665 }
2666 }
2667
2668 /* Addref the ddraw interface to keep an reference for each surface */
2669 IDirectDraw7_AddRef(iface);
2670 object->ifaceToRelease = (IUnknown *) iface;
2671
2672 /* Create a WineD3DTexture if a texture was requested */
2673 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2674 {
2675 UINT levels;
2676 WINED3DFORMAT Format;
2677 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2678
2679 This->tex_root = object;
2680
2681 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2682 {
2683 /* a mipmap is created, create enough levels */
2684 levels = desc2.u2.dwMipMapCount;
2685 }
2686 else
2687 {
2688 /* No mipmap is created, create one level */
2689 levels = 1;
2690 }
2691
2692 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2693 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2694 {
2695 Pool = WINED3DPOOL_SYSTEMMEM;
2696 }
2697 /* Should I forward the MANAGED cap to the managed pool ? */
2698
2699 /* Get the format. It's set already by CreateNewSurface */
2700 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2701
2702 /* The surfaces are already created, the callback only
2703 * passes the IWineD3DSurface to WineD3D
2704 */
2705 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2706 {
2707 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2708 DDSD->dwWidth, /* Edgelength */
2709 levels,
2710 0, /* usage */
2711 Format,
2712 Pool,
2713 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2714 0, /* SharedHandle */
2715 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2716 D3D7CB_CreateSurface);
2717 }
2718 else
2719 {
2720 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2721 DDSD->dwWidth, DDSD->dwHeight,
2722 levels, /* MipMapCount = Levels */
2723 0, /* usage */
2724 Format,
2725 Pool,
2726 (IWineD3DTexture **) &object->wineD3DTexture,
2727 0, /* SharedHandle */
2728 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2729 D3D7CB_CreateSurface );
2730 }
2731 This->tex_root = NULL;
2732 }
2733
2734 LeaveCriticalSection(&ddraw_cs);
2735 return hr;
2736 }
2737
2738 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2739 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2740
2741 static BOOL
2742 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2743 const DDPIXELFORMAT *provided)
2744 {
2745 /* Some flags must be present in both or neither for a match. */
2746 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2747 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2748 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2749
2750 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2751 return FALSE;
2752
2753 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2754 return FALSE;
2755
2756 if (requested->dwFlags & DDPF_FOURCC)
2757 if (requested->dwFourCC != provided->dwFourCC)
2758 return FALSE;
2759
2760 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2761 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2762 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2763 return FALSE;
2764
2765 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2766 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2767 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2768 return FALSE;
2769
2770 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2771 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2772 return FALSE;
2773
2774 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2775 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2776 |DDPF_BUMPDUDV))
2777 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2778 return FALSE;
2779
2780 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2781 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2782 return FALSE;
2783
2784 return TRUE;
2785 }
2786
2787 static BOOL
2788 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2789 const DDSURFACEDESC2* provided)
2790 {
2791 struct compare_info
2792 {
2793 DWORD flag;
2794 ptrdiff_t offset;
2795 size_t size;
2796 };
2797
2798 #define CMP(FLAG, FIELD) \
2799 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2800 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2801
2802 static const struct compare_info compare[] =
2803 {
2804 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2805 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2806 CMP(CAPS, ddsCaps),
2807 CMP(CKDESTBLT, ddckCKDestBlt),
2808 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2809 CMP(CKSRCBLT, ddckCKSrcBlt),
2810 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2811 CMP(HEIGHT, dwHeight),
2812 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2813 CMP(LPSURFACE, lpSurface),
2814 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2815 CMP(PITCH, u1 /* lPitch */),
2816 /* PIXELFORMAT: manual */
2817 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2818 CMP(TEXTURESTAGE, dwTextureStage),
2819 CMP(WIDTH, dwWidth),
2820 /* ZBUFFERBITDEPTH: "obsolete" */
2821 };
2822
2823 #undef CMP
2824
2825 unsigned int i;
2826
2827 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2828 return FALSE;
2829
2830 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2831 {
2832 if (requested->dwFlags & compare[i].flag
2833 && memcmp((const char *)provided + compare[i].offset,
2834 (const char *)requested + compare[i].offset,
2835 compare[i].size) != 0)
2836 return FALSE;
2837 }
2838
2839 if (requested->dwFlags & DDSD_PIXELFORMAT)
2840 {
2841 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2842 &provided->u4.ddpfPixelFormat))
2843 return FALSE;
2844 }
2845
2846 return TRUE;
2847 }
2848
2849 #undef DDENUMSURFACES_SEARCHTYPE
2850 #undef DDENUMSURFACES_MATCHTYPE
2851
2852 /*****************************************************************************
2853 * IDirectDraw7::EnumSurfaces
2854 *
2855 * Loops through all surfaces attached to this device and calls the
2856 * application callback. This can't be relayed to WineD3DDevice,
2857 * because some WineD3DSurfaces' parents are IParent objects
2858 *
2859 * Params:
2860 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2861 * DDSD: Description to filter for
2862 * Context: Application-provided pointer, it's passed unmodified to the
2863 * Callback function
2864 * Callback: Address to call for each surface
2865 *
2866 * Returns:
2867 * DDERR_INVALIDPARAMS if the callback is NULL
2868 * DD_OK on success
2869 *
2870 *****************************************************************************/
2871 static HRESULT WINAPI
2872 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2873 DWORD Flags,
2874 DDSURFACEDESC2 *DDSD,
2875 void *Context,
2876 LPDDENUMSURFACESCALLBACK7 Callback)
2877 {
2878 /* The surface enumeration is handled by WineDDraw,
2879 * because it keeps track of all surfaces attached to
2880 * it. The filtering is done by our callback function,
2881 * because WineDDraw doesn't handle ddraw-like surface
2882 * caps structures
2883 */
2884 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2885 IDirectDrawSurfaceImpl *surf;
2886 BOOL all, nomatch;
2887 DDSURFACEDESC2 desc;
2888 struct list *entry, *entry2;
2889
2890 all = Flags & DDENUMSURFACES_ALL;
2891 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2892
2893 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2894 EnterCriticalSection(&ddraw_cs);
2895
2896 if(!Callback)
2897 {
2898 LeaveCriticalSection(&ddraw_cs);
2899 return DDERR_INVALIDPARAMS;
2900 }
2901
2902 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2903 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2904 {
2905 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2906 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2907 {
2908 desc = surf->surface_desc;
2909 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2910 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2911 {
2912 LeaveCriticalSection(&ddraw_cs);
2913 return DD_OK;
2914 }
2915 }
2916 }
2917 LeaveCriticalSection(&ddraw_cs);
2918 return DD_OK;
2919 }
2920
2921 static HRESULT WINAPI
2922 findRenderTarget(IDirectDrawSurface7 *surface,
2923 DDSURFACEDESC2 *desc,
2924 void *ctx)
2925 {
2926 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2927 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
2928
2929 if(!surf->isRenderTarget) {
2930 *target = surf;
2931 IDirectDrawSurface7_Release(surface);
2932 return DDENUMRET_CANCEL;
2933 }
2934
2935 /* Recurse into the surface tree */
2936 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2937
2938 IDirectDrawSurface7_Release(surface);
2939 if(*target) return DDENUMRET_CANCEL;
2940 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2941 }
2942
2943 /*****************************************************************************
2944 * D3D7CB_CreateRenderTarget
2945 *
2946 * Callback called by WineD3D to create Surfaces for render target usage
2947 * This function takes the D3D target from the IDirectDrawImpl structure,
2948 * and returns the WineD3DSurface. To avoid double usage, the surface
2949 * is marked as render target afterwards
2950 *
2951 * Params
2952 * device: The WineD3DDevice's parent
2953 * Width, Height, Format: Dimensions and pixelformat of the render target
2954 * Ignored, because the surface already exists
2955 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2956 * Lockable: ignored
2957 * ppSurface: Address to pass the surface pointer back at
2958 * pSharedHandle: Ignored
2959 *
2960 * Returns:
2961 * Always returns D3D_OK
2962 *
2963 *****************************************************************************/
2964 static HRESULT WINAPI
2965 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
2966 UINT Width, UINT Height,
2967 WINED3DFORMAT Format,
2968 WINED3DMULTISAMPLE_TYPE MultiSample,
2969 DWORD MultisampleQuality,
2970 BOOL Lockable,
2971 IWineD3DSurface** ppSurface,
2972 HANDLE* pSharedHandle)
2973 {
2974 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2975 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
2976 TRACE("(%p) call back\n", device);
2977
2978 if(d3dSurface->isRenderTarget)
2979 {
2980 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
2981 &target, findRenderTarget);
2982 }
2983 else
2984 {
2985 target = d3dSurface;
2986 }
2987
2988 if(!target)
2989 {
2990 target = This->d3d_target;
2991 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
2992 }
2993
2994 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
2995
2996 *ppSurface = target->WineD3DSurface;
2997 target->isRenderTarget = TRUE;
2998 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
2999 return D3D_OK;
3000 }
3001
3002 static HRESULT WINAPI
3003 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3004 IUnknown *pSuperior,
3005 UINT Width,
3006 UINT Height,
3007 WINED3DFORMAT Format,
3008 WINED3DMULTISAMPLE_TYPE MultiSample,
3009 DWORD MultisampleQuality,
3010 BOOL Discard,
3011 IWineD3DSurface** ppSurface,
3012 HANDLE* pSharedHandle)
3013 {
3014 /* Create a Depth Stencil surface to make WineD3D happy */
3015 HRESULT hr = D3D_OK;
3016 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3017 DDSURFACEDESC2 ddsd;
3018
3019 TRACE("(%p) call back\n", device);
3020
3021 *ppSurface = NULL;
3022
3023 /* Create a DirectDraw surface */
3024 memset(&ddsd, 0, sizeof(ddsd));
3025 ddsd.dwSize = sizeof(ddsd);
3026 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3027 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3028 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3029 ddsd.dwHeight = Height;
3030 ddsd.dwWidth = Width;
3031 if(Format != 0)
3032 {
3033 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3034 }
3035 else
3036 {
3037 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3038 }
3039
3040 This->depthstencil = TRUE;
3041 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3042 &ddsd,
3043 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3044 NULL);
3045 This->depthstencil = FALSE;
3046 if(FAILED(hr))
3047 {
3048 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3049 return hr;
3050 }
3051 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3052 return D3D_OK;
3053 }
3054
3055 /*****************************************************************************
3056 * D3D7CB_CreateAdditionalSwapChain
3057 *
3058 * Callback function for WineD3D which creates a new WineD3DSwapchain
3059 * interface. It also creates an IParent interface to store that pointer,
3060 * so the WineD3DSwapchain has a parent and can be released when the D3D
3061 * device is destroyed
3062 *****************************************************************************/
3063 static HRESULT WINAPI
3064 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3065 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3066 IWineD3DSwapChain ** ppSwapChain)
3067 {
3068 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3069 IParentImpl *object = NULL;
3070 HRESULT res = D3D_OK;
3071 IWineD3DSwapChain *swapchain;
3072 TRACE("(%p) call back\n", device);
3073
3074 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3075 if (NULL == object)
3076 {
3077 FIXME("Allocation of memory failed\n");
3078 *ppSwapChain = NULL;
3079 return DDERR_OUTOFVIDEOMEMORY;
3080 }
3081
3082 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3083 object->ref = 1;
3084
3085 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3086 pPresentationParameters,
3087 &swapchain,
3088 (IUnknown*) ICOM_INTERFACE(object, IParent),
3089 D3D7CB_CreateRenderTarget,
3090 D3D7CB_CreateDepthStencilSurface);
3091 if (res != D3D_OK)
3092 {
3093 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3094 HeapFree(GetProcessHeap(), 0 , object);
3095 *ppSwapChain = NULL;
3096 }
3097 else
3098 {
3099 *ppSwapChain = swapchain;
3100 object->child = (IUnknown *) swapchain;
3101 }
3102
3103 return res;
3104 }
3105
3106 /*****************************************************************************
3107 * IDirectDrawImpl_AttachD3DDevice
3108 *
3109 * Initializes the D3D capabilities of WineD3D
3110 *
3111 * Params:
3112 * primary: The primary surface for D3D
3113 *
3114 * Returns
3115 * DD_OK on success,
3116 * DDERR_* otherwise
3117 *
3118 *****************************************************************************/
3119 static HRESULT WINAPI
3120 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3121 IDirectDrawSurfaceImpl *primary)
3122 {
3123 HRESULT hr;
3124 HWND window;
3125
3126 WINED3DPRESENT_PARAMETERS localParameters;
3127
3128 TRACE("(%p)->(%p)\n", This, primary);
3129
3130 /* Get the window */
3131 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
3132 &window);
3133 if(hr != D3D_OK)
3134 {
3135 ERR("IWineD3DDevice::GetHWND failed\n");
3136 return hr;
3137 }
3138
3139 /* If there's no window, create a hidden window. WineD3D needs it */
3140 if(window == 0)
3141 {
3142 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3143 WS_DISABLED, 0, 0,
3144 GetSystemMetrics(SM_CXSCREEN),
3145 GetSystemMetrics(SM_CYSCREEN),
3146 NULL, NULL, GetModuleHandleA(0), NULL);
3147
3148 ShowWindow(window, SW_HIDE); /* Just to be sure */
3149 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3150 This->d3d_window = window;
3151 }
3152 else
3153 {
3154 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3155 }
3156
3157 /* Store the future Render Target surface */
3158 This->d3d_target = primary;
3159
3160 /* Use the surface description for the device parameters, not the
3161 * Device settings. The app might render to an offscreen surface
3162 */
3163 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3164 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3165 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3166 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3167 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3168 localParameters.MultiSampleQuality = 0;
3169 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3170 localParameters.hDeviceWindow = window;
3171 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3172 localParameters.EnableAutoDepthStencil = TRUE;
3173 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3174 localParameters.Flags = 0;
3175 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3176 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3177
3178 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3179
3180 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3181 * recursive loop until ram or emulated video memory is full
3182 */
3183 This->d3d_initialized = TRUE;
3184
3185 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3186 &localParameters,
3187 D3D7CB_CreateAdditionalSwapChain);
3188 if(FAILED(hr))
3189 {
3190 This->d3d_target = NULL;
3191 This->d3d_initialized = FALSE;
3192 return hr;
3193 }
3194
3195 This->declArraySize = 2;
3196 This->decls = HeapAlloc(GetProcessHeap(),
3197 HEAP_ZERO_MEMORY,
3198 sizeof(*This->decls) * This->declArraySize);
3199 if(!This->decls)
3200 {
3201 ERR("Error allocating an array for the converted vertex decls\n");
3202 This->declArraySize = 0;
3203 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3204 D3D7CB_DestroyDepthStencilSurface,
3205 D3D7CB_DestroySwapChain);
3206 return E_OUTOFMEMORY;
3207 }
3208
3209 /* Create an Index Buffer parent */
3210 TRACE("(%p) Successfully initialized 3D\n", This);
3211 return DD_OK;
3212 }
3213
3214 /*****************************************************************************
3215 * DirectDrawCreateClipper (DDRAW.@)
3216 *
3217 * Creates a new IDirectDrawClipper object.
3218 *
3219 * Params:
3220 * Clipper: Address to write the interface pointer to
3221 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3222 * NULL
3223 *
3224 * Returns:
3225 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3226 * E_OUTOFMEMORY if allocating the object failed
3227 *
3228 *****************************************************************************/
3229 HRESULT WINAPI
3230 DirectDrawCreateClipper(DWORD Flags,
3231 LPDIRECTDRAWCLIPPER *Clipper,
3232 IUnknown *UnkOuter)
3233 {
3234 IDirectDrawClipperImpl* object;
3235 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3236
3237 EnterCriticalSection(&ddraw_cs);
3238 if (UnkOuter != NULL)
3239 {
3240 LeaveCriticalSection(&ddraw_cs);
3241 return CLASS_E_NOAGGREGATION;
3242 }
3243
3244 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3245 sizeof(IDirectDrawClipperImpl));
3246 if (object == NULL)
3247 {
3248 LeaveCriticalSection(&ddraw_cs);
3249 return E_OUTOFMEMORY;
3250 }
3251
3252 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3253 object->ref = 1;
3254 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3255 if(!object->wineD3DClipper)
3256 {
3257 HeapFree(GetProcessHeap(), 0, object);
3258 LeaveCriticalSection(&ddraw_cs);
3259 return E_OUTOFMEMORY;
3260 }
3261
3262 *Clipper = (IDirectDrawClipper *) object;
3263 LeaveCriticalSection(&ddraw_cs);
3264 return DD_OK;
3265 }
3266
3267 /*****************************************************************************
3268 * IDirectDraw7::CreateClipper
3269 *
3270 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3271 *
3272 *****************************************************************************/
3273 static HRESULT WINAPI
3274 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3275 DWORD Flags,
3276 IDirectDrawClipper **Clipper,
3277 IUnknown *UnkOuter)
3278 {
3279 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3280 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3281 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3282 }
3283
3284 /*****************************************************************************
3285 * IDirectDraw7::CreatePalette
3286 *
3287 * Creates a new IDirectDrawPalette object
3288 *
3289 * Params:
3290 * Flags: The flags for the new clipper
3291 * ColorTable: Color table to assign to the new clipper
3292 * Palette: Address to write the interface pointer to
3293 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3294 * NULL
3295 *
3296 * Returns:
3297 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3298 * E_OUTOFMEMORY if allocating the object failed
3299 *
3300 *****************************************************************************/
3301 static HRESULT WINAPI
3302 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3303 DWORD Flags,
3304 PALETTEENTRY *ColorTable,
3305 IDirectDrawPalette **Palette,
3306 IUnknown *pUnkOuter)
3307 {
3308 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3309 IDirectDrawPaletteImpl *object;
3310 HRESULT hr = DDERR_GENERIC;
3311 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3312
3313 EnterCriticalSection(&ddraw_cs);
3314 if(pUnkOuter != NULL)
3315 {
3316 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3317 LeaveCriticalSection(&ddraw_cs);
3318 return CLASS_E_NOAGGREGATION;
3319 }
3320
3321 /* The refcount test shows that a cooplevel is required for this */
3322 if(!This->cooperative_level)
3323 {
3324 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3325 LeaveCriticalSection(&ddraw_cs);
3326 return DDERR_NOCOOPERATIVELEVELSET;
3327 }
3328
3329 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3330 if(!object)
3331 {
3332 ERR("Out of memory when allocating memory for a palette implementation\n");
3333 LeaveCriticalSection(&ddraw_cs);
3334 return E_OUTOFMEMORY;
3335 }
3336
3337 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3338 object->ref = 1;
3339 object->ddraw_owner = This;
3340
3341 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3342 if(hr != DD_OK)
3343 {
3344 HeapFree(GetProcessHeap(), 0, object);
3345 LeaveCriticalSection(&ddraw_cs);
3346 return hr;
3347 }
3348
3349 IDirectDraw7_AddRef(iface);
3350 object->ifaceToRelease = (IUnknown *) iface;
3351 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3352 LeaveCriticalSection(&ddraw_cs);
3353 return DD_OK;
3354 }
3355
3356 /*****************************************************************************
3357 * IDirectDraw7::DuplicateSurface
3358 *
3359 * Duplicates a surface. The surface memory points to the same memory as
3360 * the original surface, and it's released when the last surface referencing
3361 * it is released. I guess that's beyond Wine's surface management right now
3362 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3363 * test application to implement this)
3364 *
3365 * Params:
3366 * Src: Address of the source surface
3367 * Dest: Address to write the new surface pointer to
3368 *
3369 * Returns:
3370 * See IDirectDraw7::CreateSurface
3371 *
3372 *****************************************************************************/
3373 static HRESULT WINAPI
3374 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3375 IDirectDrawSurface7 *Src,
3376 IDirectDrawSurface7 **Dest)
3377 {
3378 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3379 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3380
3381 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3382
3383 /* For now, simply create a new, independent surface */
3384 return IDirectDraw7_CreateSurface(iface,
3385 &Surf->surface_desc,
3386 Dest,
3387 NULL);
3388 }
3389
3390 /*****************************************************************************
3391 * IDirectDraw7 VTable
3392 *****************************************************************************/
3393 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3394 {
3395 /*** IUnknown ***/
3396 IDirectDrawImpl_QueryInterface,
3397 IDirectDrawImpl_AddRef,
3398 IDirectDrawImpl_Release,
3399 /*** IDirectDraw ***/
3400 IDirectDrawImpl_Compact,
3401 IDirectDrawImpl_CreateClipper,
3402 IDirectDrawImpl_CreatePalette,
3403 IDirectDrawImpl_CreateSurface,
3404 IDirectDrawImpl_DuplicateSurface,
3405 IDirectDrawImpl_EnumDisplayModes,
3406 IDirectDrawImpl_EnumSurfaces,
3407 IDirectDrawImpl_FlipToGDISurface,
3408 IDirectDrawImpl_GetCaps,
3409 IDirectDrawImpl_GetDisplayMode,
3410 IDirectDrawImpl_GetFourCCCodes,
3411 IDirectDrawImpl_GetGDISurface,
3412 IDirectDrawImpl_GetMonitorFrequency,
3413 IDirectDrawImpl_GetScanLine,
3414 IDirectDrawImpl_GetVerticalBlankStatus,
3415 IDirectDrawImpl_Initialize,
3416 IDirectDrawImpl_RestoreDisplayMode,
3417 IDirectDrawImpl_SetCooperativeLevel,
3418 IDirectDrawImpl_SetDisplayMode,
3419 IDirectDrawImpl_WaitForVerticalBlank,
3420 /*** IDirectDraw2 ***/
3421 IDirectDrawImpl_GetAvailableVidMem,
3422 /*** IDirectDraw7 ***/
3423 IDirectDrawImpl_GetSurfaceFromDC,
3424 IDirectDrawImpl_RestoreAllSurfaces,
3425 IDirectDrawImpl_TestCooperativeLevel,
3426 IDirectDrawImpl_GetDeviceIdentifier,
3427 /*** IDirectDraw7 ***/
3428 IDirectDrawImpl_StartModeTest,
3429 IDirectDrawImpl_EvaluateMode
3430 };
3431
3432 /*****************************************************************************
3433 * IDirectDrawImpl_FindDecl
3434 *
3435 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3436 * if none was found.
3437 *
3438 * This function is in ddraw.c and the DDraw object space because D3D7
3439 * vertex buffers are created using the IDirect3D interface to the ddraw
3440 * object, so they can be valid across D3D devices(theoretically. The ddraw
3441 * object also owns the wined3d device
3442 *
3443 * Parameters:
3444 * This: Device
3445 * fvf: Fvf to find the decl for
3446 *
3447 * Returns:
3448 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3449 * fvf otherwise.
3450 *
3451 *****************************************************************************/
3452 IWineD3DVertexDeclaration *
3453 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3454 DWORD fvf)
3455 {
3456 HRESULT hr;
3457 IWineD3DVertexDeclaration* pDecl = NULL;
3458 int p, low, high; /* deliberately signed */
3459 struct FvfToDecl *convertedDecls = This->decls;
3460
3461 TRACE("Searching for declaration for fvf %08x... ", fvf);
3462
3463 low = 0;
3464 high = This->numConvertedDecls - 1;
3465 while(low <= high) {
3466 p = (low + high) >> 1;
3467 TRACE("%d ", p);
3468 if(convertedDecls[p].fvf == fvf) {
3469 TRACE("found %p\n", convertedDecls[p].decl);
3470 return convertedDecls[p].decl;
3471 } else if(convertedDecls[p].fvf < fvf) {
3472 low = p + 1;
3473 } else {
3474 high = p - 1;
3475 }
3476 }
3477 TRACE("not found. Creating and inserting at position %d.\n", low);
3478
3479 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3480 &pDecl,
3481 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3482 fvf);
3483 if (hr != S_OK) return NULL;
3484
3485 if(This->declArraySize == This->numConvertedDecls) {
3486 int grow = max(This->declArraySize / 2, 8);
3487 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3488 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3489 if(!convertedDecls) {
3490 /* This will destroy it */
3491 IWineD3DVertexDeclaration_Release(pDecl);
3492 return NULL;
3493 }
3494 This->decls = convertedDecls;
3495 This->declArraySize += grow;
3496 }
3497
3498 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3499 convertedDecls[low].decl = pDecl;
3500 convertedDecls[low].fvf = fvf;
3501 This->numConvertedDecls++;
3502
3503 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3504 return pDecl;
3505 }
3506
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.