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