Added Norwegian translations.
[wine] / dlls / ddraw / surface_main.c
1 /*              DirectDrawSurface base implementation
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 1998-2000 Lionel Ulmer (most of Direct3D stuff)
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <string.h>
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30
31 #include "winerror.h"
32 #include "wine/debug.h"
33 #include "ddraw_private.h"
34 #include "opengl_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
37 WINE_DECLARE_DEBUG_CHANNEL(ddraw_flip);
38 WINE_DECLARE_DEBUG_CHANNEL(ddraw_fps);
39
40 /** Creation/Destruction functions */
41
42 HRESULT
43 Main_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
44                                  IDirectDrawImpl *pDD,
45                                  const DDSURFACEDESC2 *pDDSD)
46 {
47     TRACE("(%p)->(%p,%p)\n", This, pDD, pDDSD);
48
49     if (pDDSD != &This->surface_desc) {
50         This->surface_desc.dwSize = sizeof(This->surface_desc);
51         DD_STRUCT_COPY_BYSIZE(&(This->surface_desc),pDDSD);
52     }
53     This->uniqueness_value = 1; /* unchecked */
54     This->ref = 1;
55
56     This->local.lpSurfMore = &This->more;
57     This->local.lpGbl = &This->global;
58     This->local.dwProcessId = GetCurrentProcessId();
59     This->local.dwFlags = 0; /* FIXME */
60     This->local.ddsCaps.dwCaps = This->surface_desc.ddsCaps.dwCaps;
61     /* FIXME: more local stuff */
62     This->more.lpDD_lcl = &pDD->local;
63     This->more.ddsCapsEx.dwCaps2 = This->surface_desc.ddsCaps.dwCaps2;
64     This->more.ddsCapsEx.dwCaps3 = This->surface_desc.ddsCaps.dwCaps3;
65     This->more.ddsCapsEx.dwCaps4 = This->surface_desc.ddsCaps.dwCaps4;
66     /* FIXME: more more stuff */
67     This->gmore = &This->global_more;
68     This->global.u3.lpDD = pDD->local.lpGbl;
69     /* FIXME: more global stuff */
70
71     This->final_release = Main_DirectDrawSurface_final_release;
72     This->late_allocate = Main_DirectDrawSurface_late_allocate;
73     This->attach = Main_DirectDrawSurface_attach;
74     This->detach = Main_DirectDrawSurface_detach;
75     This->lock_update = Main_DirectDrawSurface_lock_update;
76     This->unlock_update = Main_DirectDrawSurface_unlock_update;
77     This->lose_surface = Main_DirectDrawSurface_lose_surface;
78     This->set_palette    = Main_DirectDrawSurface_set_palette;
79     This->update_palette = Main_DirectDrawSurface_update_palette;
80     This->get_display_window = Main_DirectDrawSurface_get_display_window;
81     This->get_gamma_ramp = Main_DirectDrawSurface_get_gamma_ramp;
82     This->set_gamma_ramp = Main_DirectDrawSurface_set_gamma_ramp;
83
84     ICOM_INIT_INTERFACE(This, IDirectDrawSurface3,
85                         DDRAW_IDDS3_Thunk_VTable);
86     ICOM_INIT_INTERFACE(This, IDirectDrawGammaControl,
87                         DDRAW_IDDGC_VTable);
88
89     /* There is no generic implementation of IDDS7 or texture */
90
91     Main_DirectDraw_AddSurface(pDD, This);
92     return DD_OK;
93 }
94
95 void Main_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
96 {
97     Main_DirectDraw_RemoveSurface(This->ddraw_owner, This);
98 }
99
100 HRESULT Main_DirectDrawSurface_late_allocate(IDirectDrawSurfaceImpl* This)
101 {
102     return DD_OK;
103 }
104
105 static void Main_DirectDrawSurface_Destroy(IDirectDrawSurfaceImpl* This)
106 {
107     if (This->palette) {
108         IDirectDrawPalette_Release(ICOM_INTERFACE(This->palette, IDirectDrawPalette));
109         This->palette = NULL;
110     }
111     This->final_release(This);
112     if (This->private != This+1) HeapFree(GetProcessHeap(), 0, This->private);
113     if (This->tex_private) HeapFree(GetProcessHeap(), 0, This->tex_private);
114     HeapFree(GetProcessHeap(), 0, This);
115 }
116
117 void Main_DirectDrawSurface_ForceDestroy(IDirectDrawSurfaceImpl* This)
118 {
119     WARN("destroying surface %p with refcnt %lu\n", This, This->ref);
120     Main_DirectDrawSurface_Destroy(This);
121 }
122
123 ULONG WINAPI Main_DirectDrawSurface_Release(LPDIRECTDRAWSURFACE7 iface)
124 {
125     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
126     ULONG ref = InterlockedDecrement(&This->ref);
127
128     TRACE("(%p)->(): decreasing from %ld\n", This, ref + 1);
129     
130     if (ref == 0)
131     {
132         if (This->aux_release)
133             This->aux_release(This->aux_ctx, This->aux_data);
134         Main_DirectDrawSurface_Destroy(This);
135
136         TRACE("released surface %p\n", This);
137         
138         return 0;
139     }
140
141     return ref;
142 }
143
144 ULONG WINAPI Main_DirectDrawSurface_AddRef(LPDIRECTDRAWSURFACE7 iface)
145 {
146     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
147     ULONG ref = InterlockedIncrement(&This->ref);
148
149     TRACE("(%p)->(): increasing from %ld\n", This, ref - 1);
150     
151     return ref;
152 }
153
154 HRESULT WINAPI
155 Main_DirectDrawSurface_QueryInterface(LPDIRECTDRAWSURFACE7 iface, REFIID riid,
156                                       LPVOID* ppObj)
157 {
158     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
159     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppObj);
160
161     *ppObj = NULL;
162
163     if (IsEqualGUID(&IID_IUnknown, riid)
164         || IsEqualGUID(&IID_IDirectDrawSurface7, riid)
165         || IsEqualGUID(&IID_IDirectDrawSurface4, riid))
166     {
167         InterlockedIncrement(&This->ref);
168         *ppObj = ICOM_INTERFACE(This, IDirectDrawSurface7);
169         return S_OK;
170     }
171     else if (IsEqualGUID(&IID_IDirectDrawSurface, riid)
172              || IsEqualGUID(&IID_IDirectDrawSurface2, riid)
173              || IsEqualGUID(&IID_IDirectDrawSurface3, riid))
174     {
175         InterlockedIncrement(&This->ref);
176         *ppObj = ICOM_INTERFACE(This, IDirectDrawSurface3);
177         return S_OK;
178     }
179     else if (IsEqualGUID(&IID_IDirectDrawGammaControl, riid))
180     {
181         InterlockedIncrement(&This->ref);
182         *ppObj = ICOM_INTERFACE(This, IDirectDrawGammaControl);
183         return S_OK;
184     }
185 #ifdef HAVE_OPENGL
186     /* interfaces following here require OpenGL */
187     if( !opengl_initialized )
188         return E_NOINTERFACE;
189
190     if ( IsEqualGUID( &IID_D3DDEVICE_OpenGL, riid ) ||
191           IsEqualGUID( &IID_IDirect3DHALDevice, riid) )
192     {
193         IDirect3DDeviceImpl *d3ddevimpl;
194         HRESULT ret_value;
195
196         ret_value = d3ddevice_create(&d3ddevimpl, This->ddraw_owner, This, 1);
197         if (FAILED(ret_value)) return ret_value;
198
199         *ppObj = ICOM_INTERFACE(d3ddevimpl, IDirect3DDevice);
200         TRACE(" returning Direct3DDevice interface at %p.\n", *ppObj);
201         
202         InterlockedIncrement(&This->ref); /* No idea if this is correct.. Need to check using real Windows */
203         return ret_value;
204     }
205     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
206              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
207     {
208         HRESULT ret_value = S_OK;
209
210         /* Note: this is not exactly how Windows does it... But this seems not to hurt the only
211                  application I know creating a texture without this flag set and it will prevent
212                  bugs in other parts of Wine.
213         */
214         This->surface_desc.ddsCaps.dwCaps |= DDSCAPS_TEXTURE;
215
216         /* In case the texture surface was created before the D3D creation */
217         if (This->tex_private == NULL) {
218             if (This->ddraw_owner->d3d_private == NULL) {
219                 ERR("Texture created with no D3D object yet.. Not supported !\n");
220                 return E_NOINTERFACE;
221             }
222
223             ret_value = This->ddraw_owner->d3d_create_texture(This->ddraw_owner, This, FALSE, This->mip_main);
224             if (FAILED(ret_value)) return ret_value;
225         }
226         if (IsEqualGUID( &IID_IDirect3DTexture, riid )) {
227             *ppObj = ICOM_INTERFACE(This, IDirect3DTexture);
228             TRACE(" returning Direct3DTexture interface at %p.\n", *ppObj);
229         } else {
230             *ppObj = ICOM_INTERFACE(This, IDirect3DTexture2);
231             TRACE(" returning Direct3DTexture2 interface at %p.\n", *ppObj);
232         }
233         InterlockedIncrement(&This->ref);
234         return ret_value;
235     }
236 #endif
237
238     return E_NOINTERFACE;
239 }
240
241 /*** Callbacks */
242
243 BOOL
244 Main_DirectDrawSurface_attach(IDirectDrawSurfaceImpl *This,
245                               IDirectDrawSurfaceImpl *to)
246 {
247     return TRUE;
248 }
249
250 BOOL Main_DirectDrawSurface_detach(IDirectDrawSurfaceImpl *This)
251 {
252     return TRUE;
253 }
254
255 void
256 Main_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect,
257         DWORD dwFlags)
258 {
259 }
260
261 void
262 Main_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
263                                      LPCRECT pRect)
264 {
265 }
266
267 void
268 Main_DirectDrawSurface_lose_surface(IDirectDrawSurfaceImpl* This)
269 {
270 }
271
272 void
273 Main_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
274                                    IDirectDrawPaletteImpl* pal)
275 {
276 }
277
278 void
279 Main_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
280                                       IDirectDrawPaletteImpl* pal,
281                                       DWORD dwStart, DWORD dwCount,
282                                       LPPALETTEENTRY palent)
283 {
284 }
285
286 HWND
287 Main_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
288 {
289     return 0;
290 }
291
292 HRESULT
293 Main_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl* This,
294                                       DWORD dwFlags,
295                                       LPDDGAMMARAMP lpGammaRamp)
296 {
297     HDC hDC;
298     HRESULT hr;
299     hr = This->get_dc(This, &hDC);
300     if (FAILED(hr)) return hr;
301     hr = GetDeviceGammaRamp(hDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
302     This->release_dc(This, hDC);
303     return hr;
304 }
305
306 HRESULT
307 Main_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl* This,
308                                       DWORD dwFlags,
309                                       LPDDGAMMARAMP lpGammaRamp)
310 {
311     HDC hDC;
312     HRESULT hr;
313     hr = This->get_dc(This, &hDC);
314     if (FAILED(hr)) return hr;
315     hr = SetDeviceGammaRamp(hDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
316     This->release_dc(This, hDC);
317     return hr;
318 }
319
320
321 /*** Interface functions */
322
323 HRESULT WINAPI
324 Main_DirectDrawSurface_AddAttachedSurface(LPDIRECTDRAWSURFACE7 iface,
325                                           LPDIRECTDRAWSURFACE7 pAttach)
326 {
327     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
328     IDirectDrawSurfaceImpl* surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
329                                                IDirectDrawSurface7, pAttach);
330
331     TRACE("(%p)->(%p)\n",This,pAttach);
332
333     /* Does windows check this? */
334     if (surf == This)
335         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
336
337     /* Does windows check this? */
338     if (surf->ddraw_owner != This->ddraw_owner)
339         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
340
341     if (surf->surface_owner != NULL)
342         return DDERR_SURFACEALREADYATTACHED; /* unchecked */
343
344     /* TODO MSDN: "You can attach only z-buffer surfaces with this method."
345      * But apparently backbuffers and mipmaps can be attached too. */
346
347     /* Set MIPMAPSUBLEVEL if this seems to be one */
348     if (This->surface_desc.ddsCaps.dwCaps &
349         surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
350         surf->surface_desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
351         /* FIXME: we should probably also add to dwMipMapCount of this
352          * and all parent surfaces (update create_texture if you do) */
353     }
354
355     /* Callback to allow the surface to do something special now that it is
356      * attached. (e.g. maybe the Z-buffer tells the renderer to use it.) */
357     if (!surf->attach(surf, This))
358         return DDERR_CANNOTATTACHSURFACE;
359
360     /* check: Where should it go in the chain? This puts it on the head. */
361     if (This->attached)
362         This->attached->prev_attached = surf;
363     surf->next_attached = This->attached;
364     surf->prev_attached = NULL;
365     This->attached = surf;
366     surf->surface_owner = This;
367
368     IDirectDrawSurface7_AddRef(pAttach);
369
370     return DD_OK;
371 }
372
373 /* MSDN: "not currently implemented." */
374 HRESULT WINAPI
375 Main_DirectDrawSurface_AddOverlayDirtyRect(LPDIRECTDRAWSURFACE7 iface,
376                                            LPRECT pRect)
377 {
378     TRACE("(%p)->(%p)\n",iface,pRect);
379     return DDERR_UNSUPPORTED; /* unchecked */
380 }
381
382 /* MSDN: "not currently implemented." */
383 HRESULT WINAPI
384 Main_DirectDrawSurface_BltBatch(LPDIRECTDRAWSURFACE7 iface,
385                                 LPDDBLTBATCH pBatch, DWORD dwCount,
386                                 DWORD dwFlags)
387 {
388     TRACE("(%p)->(%p,%ld,%08lx)\n",iface,pBatch,dwCount,dwFlags);
389     return DDERR_UNSUPPORTED; /* unchecked */
390 }
391
392 HRESULT WINAPI
393 Main_DirectDrawSurface_ChangeUniquenessValue(LPDIRECTDRAWSURFACE7 iface)
394 {
395     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
396     volatile IDirectDrawSurfaceImpl* vThis = This;
397
398     TRACE("(%p)\n",This);
399     /* A uniquness value of 0 is apparently special.
400      * This needs to be checked. */
401     while (1)
402     {
403         DWORD old_uniqueness_value = vThis->uniqueness_value;
404         DWORD new_uniqueness_value = old_uniqueness_value+1;
405
406         if (old_uniqueness_value == 0) break;
407         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
408
409         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
410                                        old_uniqueness_value,
411                                        new_uniqueness_value)
412             == old_uniqueness_value)
413             break;
414     }
415
416     return DD_OK;
417 }
418
419 HRESULT WINAPI
420 Main_DirectDrawSurface_DeleteAttachedSurface(LPDIRECTDRAWSURFACE7 iface,
421                                              DWORD dwFlags,
422                                              LPDIRECTDRAWSURFACE7 pAttach)
423 {
424     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
425     IDirectDrawSurfaceImpl* surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
426                                                IDirectDrawSurface7, pAttach);
427
428     TRACE("(%p)->(%08lx,%p)\n",This,dwFlags,pAttach);
429
430     if (!surf || (surf->surface_owner != This))
431         return DDERR_SURFACENOTATTACHED; /* unchecked */
432
433     surf->detach(surf);
434
435     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
436     if (This->surface_desc.ddsCaps.dwCaps &
437         surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
438         surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
439         /* FIXME: we should probably also subtract from dwMipMapCount of this
440          * and all parent surfaces */
441     }
442
443     if (surf->next_attached)
444         surf->next_attached->prev_attached = surf->prev_attached;
445     if (surf->prev_attached)
446         surf->prev_attached->next_attached = surf->next_attached;
447     if (This->attached == surf)
448         This->attached = surf->next_attached;
449
450     IDirectDrawSurface7_Release(pAttach);
451
452     return DD_OK;
453 }
454
455 HRESULT WINAPI
456 Main_DirectDrawSurface_EnumAttachedSurfaces(LPDIRECTDRAWSURFACE7 iface,
457                                             LPVOID context,
458                                             LPDDENUMSURFACESCALLBACK7 cb)
459 {
460     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
461     IDirectDrawSurfaceImpl* surf;
462
463     TRACE("(%p)->(%p,%p)\n",This,context,cb);
464
465     for (surf = This->attached; surf != NULL; surf = surf->next_attached)
466     {
467         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
468         if (cb(ICOM_INTERFACE(surf, IDirectDrawSurface7), &surf->surface_desc,
469                context) == DDENUMRET_CANCEL)
470             break;
471     }
472
473     return DD_OK;
474 }
475
476 HRESULT WINAPI
477 Main_DirectDrawSurface_EnumOverlayZOrders(LPDIRECTDRAWSURFACE7 iface,
478                                           DWORD dwFlags, LPVOID context,
479                                           LPDDENUMSURFACESCALLBACK7 cb)
480 {
481     TRACE("(%p)->(%08lx,%p,%p)\n",iface,dwFlags,context,cb);
482     return DD_OK;
483 }
484
485 BOOL Main_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
486                                       IDirectDrawSurfaceImpl* back,
487                                       DWORD dwFlags)
488 {
489     /* uniqueness_value? */
490     /* This is necessary. But is it safe? */
491     {
492         HDC tmp = front->hDC;
493         front->hDC = back->hDC;
494         back->hDC = tmp;
495     }
496
497     {
498         BOOL tmp = front->dc_in_use;
499         front->dc_in_use = back->dc_in_use;
500         back->dc_in_use = tmp;
501     }
502
503     {
504         FLATPTR tmp = front->global.fpVidMem;
505         front->global.fpVidMem = back->global.fpVidMem;
506         back->global.fpVidMem = tmp;
507     }
508
509     {
510         ULONG_PTR tmp = front->global_more.hKernelSurface;
511         front->global_more.hKernelSurface = back->global_more.hKernelSurface;
512         back->global_more.hKernelSurface = tmp;
513     }
514
515     return TRUE;
516 }
517
518 /* This is unnecessarely complicated :-) */
519 #define MEASUREMENT_WINDOW 5
520 #define NUMBER_OF_WINDOWS 10
521
522 static LONGLONG perf_freq;
523 static LONGLONG perf_storage[NUMBER_OF_WINDOWS];
524 static LONGLONG prev_time = 0;
525 static unsigned int current_window;
526 static unsigned int measurements_in_window;
527 static unsigned int valid_windows;
528
529 HRESULT WINAPI
530 Main_DirectDrawSurface_Flip(LPDIRECTDRAWSURFACE7 iface,
531                             LPDIRECTDRAWSURFACE7 override, DWORD dwFlags)
532 {
533     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
534     IDirectDrawSurfaceImpl* target;
535     HRESULT hr;
536
537     TRACE("(%p)->(%p,%08lx)\n",This,override,dwFlags);
538
539     if (TRACE_ON(ddraw_fps)) {
540         LONGLONG current_time;
541         LONGLONG frame_duration;
542         QueryPerformanceCounter((LARGE_INTEGER *) &current_time);
543
544         if (prev_time != 0) {
545             LONGLONG total_time = 0;
546             int tot_meas;
547             
548             frame_duration = current_time - prev_time;
549             prev_time = current_time;
550             
551             perf_storage[current_window] += frame_duration;
552             measurements_in_window++;
553             
554             if (measurements_in_window >= MEASUREMENT_WINDOW) {
555                 current_window++;
556                 valid_windows++;
557
558                 if (valid_windows < NUMBER_OF_WINDOWS) {
559                     unsigned int i;
560                     tot_meas = valid_windows * MEASUREMENT_WINDOW;
561                     for (i = 0; i < valid_windows; i++) {
562                         total_time += perf_storage[i];
563                     }
564                 } else {
565                     int i;
566                     tot_meas = NUMBER_OF_WINDOWS * MEASUREMENT_WINDOW;
567                     for (i = 0; i < NUMBER_OF_WINDOWS; i++) {
568                         total_time += perf_storage[i];
569                     }
570                 }
571
572                 TRACE_(ddraw_fps)(" %9.5f\n", (double) (perf_freq * tot_meas) / (double) total_time);
573                 
574                 if (current_window >= NUMBER_OF_WINDOWS) {
575                     current_window = 0;
576                 }
577                 perf_storage[current_window] = 0;
578                 measurements_in_window = 0;
579             }
580         } else {
581             prev_time = current_time;
582             memset(perf_storage, 0, sizeof(perf_storage));
583             current_window = 0;
584             valid_windows = 0;
585             measurements_in_window = 0;
586             QueryPerformanceFrequency((LARGE_INTEGER *) &perf_freq);
587         }
588     }
589     
590     /* MSDN: "This method can be called only for a surface that has the
591      * DDSCAPS_FLIP and DDSCAPS_FRONTBUFFER capabilities." */
592     if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
593         != (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
594         return DDERR_NOTFLIPPABLE;
595
596     if (This->aux_flip)
597         if (This->aux_flip(This->aux_ctx, This->aux_data))
598             return DD_OK;
599
600     /* 1. find the flip target */
601     /* XXX I don't think this algorithm works for more than 1 backbuffer. */
602     if (override == NULL)
603     {
604         static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
605         LPDIRECTDRAWSURFACE7 tgt;
606
607         hr = IDirectDrawSurface7_GetAttachedSurface(iface, &back_caps, &tgt);
608         if (FAILED(hr)) return DDERR_NOTFLIPPABLE; /* unchecked */
609
610         target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7,
611                              tgt);
612         IDirectDrawSurface7_Release(tgt);
613     }
614     else
615     {
616         BOOL on_chain = FALSE;
617         IDirectDrawSurfaceImpl* surf;
618
619         /* MSDN: "The method fails if the specified [override] surface is not
620          * a member of the flipping chain." */
621
622         /* Verify that override is on this flip chain. We assume that
623          * surf is the head of the flipping chain, because it's the front
624          * buffer. */
625         target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7,
626                              override);
627
628         /* Either target is (indirectly) attached to This or This is
629          * (indirectly) attached to target. */
630         for (surf = target; surf != NULL; surf = surf->surface_owner)
631         {
632             if (surf == This)
633             {
634                 on_chain = TRUE;
635                 break;
636             }
637         }
638
639         if (!on_chain)
640             return DDERR_INVALIDPARAMS; /* unchecked */
641     }
642
643     TRACE("flip to backbuffer: %p\n",target);
644     if (TRACE_ON(ddraw_flip)) {
645         static unsigned int flip_count = 0;
646         IDirectDrawPaletteImpl *palette;
647         char buf[32];
648         FILE *f;
649
650         /* Hack for paletted games... */
651         palette = target->palette;
652         target->palette = This->palette;
653         
654         sprintf(buf, "flip_%08d.ppm", flip_count++);
655         TRACE_(ddraw_flip)("Dumping file %s to disk.\n", buf);
656         f = fopen(buf, "wb");
657         DDRAW_dump_surface_to_disk(target, f, 8);
658         target->palette = palette;
659     }
660
661     if (This->flip_data(This, target, dwFlags))
662         This->flip_update(This, dwFlags);
663     
664     return DD_OK;
665 }
666
667 static PrivateData* find_private_data(IDirectDrawSurfaceImpl *This,
668                                       REFGUID tag)
669 {
670     PrivateData* data;
671     for (data = This->private_data; data != NULL; data = data->next)
672     {
673         if (IsEqualGUID(&data->tag, tag)) break;
674     }
675
676     return data;
677 }
678
679 HRESULT WINAPI
680 Main_DirectDrawSurface_FreePrivateData(LPDIRECTDRAWSURFACE7 iface, REFGUID tag)
681 {
682     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
683     PrivateData *data;
684
685     data = find_private_data(This, tag);
686     if (data == NULL) return DDERR_NOTFOUND;
687
688     if (data->prev)
689         data->prev->next = data->next;
690     if (data->next)
691         data->next->prev = data->prev;
692
693     if (data->flags & DDSPD_IUNKNOWNPTR)
694     {
695         if (data->ptr.object != NULL)
696             IUnknown_Release(data->ptr.object);
697     }
698     else
699         HeapFree(GetProcessHeap(), 0, data->ptr.data);
700
701     HeapFree(GetProcessHeap(), 0, data);
702
703     return DD_OK;
704 }
705
706 HRESULT WINAPI
707 Main_DirectDrawSurface_GetAttachedSurface(LPDIRECTDRAWSURFACE7 iface,
708                                           LPDDSCAPS2 pCaps,
709                                           LPDIRECTDRAWSURFACE7* ppSurface)
710 {
711     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
712     IDirectDrawSurfaceImpl* surf;
713     IDirectDrawSurfaceImpl* found = NULL;
714     DDSCAPS2 our_caps;
715     
716     if (TRACE_ON(ddraw)) {
717         TRACE("(%p)->Looking for caps: %lx,%lx,%lx,%lx output: %p\n",This,pCaps->dwCaps, pCaps->dwCaps2,
718               pCaps->dwCaps3, pCaps->dwCaps4, ppSurface);
719         TRACE("   Caps are : "); DDRAW_dump_DDSCAPS2(pCaps); TRACE("\n");
720     }
721
722     our_caps = *pCaps;
723     if ((This->ddraw_owner->local.dwLocalFlags & DDRAWILCL_DIRECTDRAW7) == 0) {
724         /* As this is not a DirectDraw7 application, remove the garbage that some games
725            put in the new fields of the DDSCAPS2 structure. */
726         our_caps.dwCaps2 = 0;
727         our_caps.dwCaps3 = 0;
728         our_caps.dwCaps4 = 0;
729         if (TRACE_ON(ddraw)) {
730             TRACE("   Real caps are : "); DDRAW_dump_DDSCAPS2(&our_caps); TRACE("\n");
731         }
732     }
733     
734     for (surf = This->attached; surf != NULL; surf = surf->next_attached)
735     {
736         if (TRACE_ON(ddraw)) {
737             TRACE("Surface: (%p) caps: %lx,%lx,%lx,%lx \n",surf ,
738                   surf->surface_desc.ddsCaps.dwCaps,
739                   surf->surface_desc.ddsCaps.dwCaps2,
740                   surf->surface_desc.ddsCaps.dwCaps3,
741                   surf->surface_desc.ddsCaps.dwCaps4);
742             TRACE("   Surface caps are : "); DDRAW_dump_DDSCAPS2(&(surf->surface_desc.ddsCaps)); TRACE("\n");
743         }
744         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
745             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2))
746         {
747             /* MSDN: "This method fails if more than one surface is attached
748              * that matches the capabilities requested." */
749             if (found != NULL)
750             {
751                 FIXME("More than one attached surface matches requested caps.  What should we do here?\n");
752                 /* Previous code returned 'DDERR_NOTFOUND'.  That appears not
753                    to be correct, given what 3DMark expects from MipMapped surfaces.
754                    We shall just continue instead. */
755             }
756
757             found = surf;
758         }
759     }
760
761     if (found == NULL) {
762         TRACE("Did not find any valid surface\n");
763         return DDERR_NOTFOUND;
764     }
765
766     *ppSurface = ICOM_INTERFACE(found, IDirectDrawSurface7);
767
768     if (TRACE_ON(ddraw)) {
769         TRACE("Returning surface %p with description : \n", *ppSurface);
770         DDRAW_dump_surface_desc(&(found->surface_desc));
771     }
772     
773     /* XXX d3dframe.cpp sometimes AddRefs things that it gets from us. */
774     IDirectDrawSurface7_AddRef(ICOM_INTERFACE(found, IDirectDrawSurface7));
775     return DD_OK;
776 }
777
778 HRESULT WINAPI
779 Main_DirectDrawSurface_GetBltStatus(LPDIRECTDRAWSURFACE7 iface, DWORD dwFlags)
780 {
781     TRACE("(%p)->(%08lx)\n",iface,dwFlags);
782
783     switch (dwFlags)
784     {
785     case DDGBS_CANBLT:
786     case DDGBS_ISBLTDONE:
787         return DD_OK;
788
789     default:
790         return DDERR_INVALIDPARAMS;
791     }
792 }
793
794 HRESULT WINAPI
795 Main_DirectDrawSurface_GetCaps(LPDIRECTDRAWSURFACE7 iface, LPDDSCAPS2 pCaps)
796 {
797     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
798
799     TRACE("(%p)->(%p)\n",This,pCaps);
800     *pCaps = This->surface_desc.ddsCaps;
801     return DD_OK;
802 }
803
804 HRESULT WINAPI
805 Main_DirectDrawSurface_GetClipper(LPDIRECTDRAWSURFACE7 iface,
806                                   LPDIRECTDRAWCLIPPER* ppClipper)
807 {
808     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
809
810     TRACE("(%p)->(%p)\n",This,ppClipper);
811     if (This->clipper == NULL)
812         return DDERR_NOCLIPPERATTACHED;
813
814     *ppClipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
815     IDirectDrawClipper_AddRef(ICOM_INTERFACE(This->clipper,
816                                              IDirectDrawClipper));
817     return DD_OK;
818 }
819
820 HRESULT WINAPI
821 Main_DirectDrawSurface_GetColorKey(LPDIRECTDRAWSURFACE7 iface, DWORD dwFlags,
822                                    LPDDCOLORKEY pCKey)
823 {
824     /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
825      * isn't there? That's like saying that an int isn't there. (Which MS
826      * has done in other docs.) */
827
828     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
829
830     TRACE("(%p)->(%08lx,%p)\n",This,dwFlags,pCKey);
831     if (TRACE_ON(ddraw)) {
832         TRACE(" - colorkey flags : ");
833         DDRAW_dump_colorkeyflag(dwFlags);
834     }
835
836     switch (dwFlags)
837     {
838     case DDCKEY_DESTBLT:
839         *pCKey = This->surface_desc.ddckCKDestBlt;
840         break;
841
842     case DDCKEY_DESTOVERLAY:
843         *pCKey = This->surface_desc.u3.ddckCKDestOverlay;
844         break;
845
846     case DDCKEY_SRCBLT:
847         *pCKey = This->surface_desc.ddckCKSrcBlt;
848         break;
849
850     case DDCKEY_SRCOVERLAY:
851         *pCKey = This->surface_desc.ddckCKSrcOverlay;
852         break;
853
854     default:
855         return DDERR_INVALIDPARAMS;
856     }
857
858     return DD_OK;
859 }
860
861 /* XXX We need to do something with the DC if the surface gets lost. */
862 HRESULT WINAPI
863 Main_DirectDrawSurface_GetDC(LPDIRECTDRAWSURFACE7 iface, HDC *phDC)
864 {
865     DDSURFACEDESC2 ddsd;
866     HRESULT hr;
867     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
868
869     TRACE("(%p)->(%p)\n",This,phDC);
870     CHECK_LOST(This);
871
872     LOCK_OBJECT(This);
873
874     if (This->dc_in_use)
875     {
876         UNLOCK_OBJECT(This);
877         return DDERR_DCALREADYCREATED;
878     }
879
880     /* Lock as per MSDN.
881      * Strange: Lock lists DDERR_SURFACEBUSY as an error, meaning that another
882      * thread has it locked, but GetDC does not. */
883     ddsd.dwSize = sizeof(ddsd);
884     hr = IDirectDrawSurface7_Lock(iface, NULL, &ddsd, 0, 0);
885     if (FAILED(hr))
886     {
887         UNLOCK_OBJECT(This);
888         return hr;
889     }
890
891     hr = This->get_dc(This, &This->hDC);
892     if (SUCCEEDED(hr))
893     {
894         TRACE("returning %p\n",This->hDC);
895
896         *phDC = This->hDC;
897         This->dc_in_use = TRUE;
898     }
899     else WARN("No DC! Prepare for trouble\n");
900
901     UNLOCK_OBJECT(This);
902     return hr;
903 }
904
905 HRESULT WINAPI
906 Main_DirectDrawSurface_GetDDInterface(LPDIRECTDRAWSURFACE7 iface, LPVOID* pDD)
907 {
908     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
909
910     TRACE("(%p)->(%p)\n",This,pDD);
911     *pDD = ICOM_INTERFACE(This->ddraw_owner, IDirectDraw7);
912     IDirectDraw7_AddRef(ICOM_INTERFACE(This->ddraw_owner, IDirectDraw7));
913     return DD_OK;
914 }
915
916 HRESULT WINAPI
917 Main_DirectDrawSurface_GetFlipStatus(LPDIRECTDRAWSURFACE7 iface, DWORD dwFlags)
918 {
919     /* XXX: DDERR_INVALIDSURFACETYPE */
920
921     TRACE("(%p)->(%08lx)\n",iface,dwFlags);
922     switch (dwFlags)
923     {
924     case DDGFS_CANFLIP:
925     case DDGFS_ISFLIPDONE:
926         return DD_OK;
927
928     default:
929         return DDERR_INVALIDPARAMS;
930     }
931 }
932
933 HRESULT WINAPI
934 Main_DirectDrawSurface_GetLOD(LPDIRECTDRAWSURFACE7 iface, LPDWORD pdwMaxLOD)
935 {
936     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
937
938     TRACE("(%p)->(%p)\n",This,pdwMaxLOD);
939     CHECK_TEXTURE(This);
940
941     *pdwMaxLOD = This->max_lod;
942     return DD_OK;
943 }
944
945 HRESULT WINAPI
946 Main_DirectDrawSurface_GetOverlayPosition(LPDIRECTDRAWSURFACE7 iface,
947                                           LPLONG pX, LPLONG pY)
948 {
949     return DDERR_NOTAOVERLAYSURFACE;
950 }
951
952 HRESULT WINAPI
953 Main_DirectDrawSurface_GetPalette(LPDIRECTDRAWSURFACE7 iface,
954                                   LPDIRECTDRAWPALETTE* ppPalette)
955 {
956     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
957
958     TRACE("(%p)->(%p)\n",This,ppPalette);
959     if (This->palette == NULL)
960         return DDERR_NOPALETTEATTACHED;
961
962     *ppPalette = ICOM_INTERFACE(This->palette, IDirectDrawPalette);
963     IDirectDrawPalette_AddRef(ICOM_INTERFACE(This->palette,
964                                              IDirectDrawPalette));
965     return DD_OK;
966 }
967
968 HRESULT WINAPI
969 Main_DirectDrawSurface_GetPixelFormat(LPDIRECTDRAWSURFACE7 iface,
970                                       LPDDPIXELFORMAT pDDPixelFormat)
971 {
972     /* What is DDERR_INVALIDSURFACETYPE for here? */
973     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
974
975     TRACE("(%p)->(%p)\n",This,pDDPixelFormat);
976     DD_STRUCT_COPY_BYSIZE(pDDPixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
977     return DD_OK;
978 }
979
980 HRESULT WINAPI
981 Main_DirectDrawSurface_GetPriority(LPDIRECTDRAWSURFACE7 iface,
982                                    LPDWORD pdwPriority)
983 {
984     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
985
986     TRACE("(%p)->(%p)\n",This,pdwPriority);
987     CHECK_TEXTURE(This);
988
989     *pdwPriority = This->priority;
990     return DD_OK;
991 }
992
993 HRESULT WINAPI
994 Main_DirectDrawSurface_GetPrivateData(LPDIRECTDRAWSURFACE7 iface,
995                                       REFGUID tag, LPVOID pBuffer,
996                                       LPDWORD pcbBufferSize)
997 {
998     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
999     PrivateData* data;
1000
1001     TRACE("(%p)->(%p), size = %ld\n", This, pBuffer, *pcbBufferSize);
1002
1003     data = find_private_data(This, tag);
1004     if (data == NULL) return DDERR_NOTFOUND;
1005
1006     /* This may not be right. */
1007     if ((data->flags & DDSPD_VOLATILE)
1008         && data->uniqueness_value != This->uniqueness_value)
1009         return DDERR_EXPIRED;
1010
1011     if (*pcbBufferSize < data->size)
1012     {
1013         *pcbBufferSize = data->size;
1014         return DDERR_MOREDATA;
1015     }
1016
1017     if (data->flags & DDSPD_IUNKNOWNPTR)
1018     {
1019         *(LPUNKNOWN *)pBuffer = data->ptr.object;
1020         IUnknown_AddRef(data->ptr.object);
1021     }
1022     else
1023     {
1024         memcpy(pBuffer, data->ptr.data, data->size);
1025     }
1026
1027     return DD_OK;
1028 }
1029
1030 HRESULT WINAPI
1031 Main_DirectDrawSurface_GetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1032                                       LPDDSURFACEDESC2 pDDSD)
1033 {
1034     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1035
1036     TRACE("(%p)->(%p)\n",This,pDDSD);
1037     if ((pDDSD->dwSize < sizeof(DDSURFACEDESC)) ||
1038         (pDDSD->dwSize > sizeof(DDSURFACEDESC2))) {
1039         ERR("Impossible/Strange struct size %ld.\n",pDDSD->dwSize);
1040         return DDERR_GENERIC;
1041     }
1042
1043     DD_STRUCT_COPY_BYSIZE(pDDSD,&This->surface_desc);
1044     if (TRACE_ON(ddraw)) {
1045       DDRAW_dump_surface_desc(pDDSD);
1046     }
1047     return DD_OK;
1048 }
1049
1050 HRESULT WINAPI
1051 Main_DirectDrawSurface_GetUniquenessValue(LPDIRECTDRAWSURFACE7 iface,
1052                                           LPDWORD pValue)
1053 {
1054     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1055
1056     TRACE("(%p)->(%p)\n",This,pValue);
1057     *pValue = This->uniqueness_value;
1058     return DD_OK;
1059 }
1060
1061 HRESULT WINAPI
1062 Main_DirectDrawSurface_Initialize(LPDIRECTDRAWSURFACE7 iface,
1063                                   LPDIRECTDRAW pDD, LPDDSURFACEDESC2 pDDSD)
1064 {
1065     TRACE("(%p)->(%p,%p)\n",iface,pDD,pDDSD);
1066     return DDERR_ALREADYINITIALIZED;
1067 }
1068
1069 HRESULT WINAPI
1070 Main_DirectDrawSurface_IsLost(LPDIRECTDRAWSURFACE7 iface)
1071 {
1072     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1073
1074     TRACE("(%p) is%s lost\n",This, (This->lost ? "" : " not"));
1075     return This->lost ? DDERR_SURFACELOST : DD_OK;
1076 }
1077
1078
1079 /* XXX This doesn't actually do any locking or keep track of the locked
1080  * rectangles. The behaviour is poorly documented. */
1081 HRESULT WINAPI
1082 Main_DirectDrawSurface_Lock(LPDIRECTDRAWSURFACE7 iface, LPRECT prect,
1083                             LPDDSURFACEDESC2 pDDSD, DWORD flags, HANDLE h)
1084 {
1085     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1086
1087     if (TRACE_ON(ddraw)) {
1088         TRACE("(%p)->Lock(%p,%p,%08lx,%p)\n",This,prect,pDDSD,flags,h);
1089         TRACE(" - locking flags : "); DDRAW_dump_lockflag(flags);
1090     }
1091     if (WARN_ON(ddraw)) {
1092         if (flags & ~(DDLOCK_WAIT|DDLOCK_READONLY|DDLOCK_WRITEONLY)) {
1093             WARN(" - unsupported locking flag : "); DDRAW_dump_lockflag(flags & ~(DDLOCK_WAIT|DDLOCK_READONLY|DDLOCK_WRITEONLY));
1094         }
1095     }
1096
1097     /* If the surface is already locked, return busy */
1098     if (This->locked) {
1099         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1100         return DDERR_SURFACEBUSY;
1101     }
1102
1103     /* First, copy the Surface description */
1104     DD_STRUCT_COPY_BYSIZE(pDDSD,&(This->surface_desc));
1105
1106     /* Used to optimize the D3D Device locking */
1107     This->lastlocktype = flags & (DDLOCK_READONLY|DDLOCK_WRITEONLY);
1108     
1109     /* If asked only for a part, change the surface pointer.
1110      * (Not documented.) */
1111     if (prect != NULL) {
1112         TRACE(" lprect: %ldx%ld-%ldx%ld\n",
1113                 prect->left,prect->top,prect->right,prect->bottom);
1114         /* First do some sanity checkings on the rectangle we receive.
1115            DungeonSiege seems to gives us once a very bad rectangle for example */
1116         if ((prect->top < 0) ||
1117             (prect->left < 0) ||
1118             (prect->bottom < 0) ||
1119             (prect->right < 0) ||
1120             (prect->left >= prect->right) ||
1121             (prect->top >= prect->bottom) ||
1122             (prect->left >= This->surface_desc.dwWidth) ||
1123             (prect->right > This->surface_desc.dwWidth) ||
1124             (prect->top >= This->surface_desc.dwHeight) ||
1125             (prect->bottom > This->surface_desc.dwHeight)) {
1126             ERR(" Invalid values in LPRECT !!!\n");
1127             return DDERR_INVALIDPARAMS;
1128         }
1129
1130         This->lock_update(This, prect, flags);
1131
1132         if (pDDSD->u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) {
1133             int blksize;
1134             switch(pDDSD->u4.ddpfPixelFormat.dwFourCC) {
1135                 case MAKE_FOURCC('D','X','T','1') : blksize = 8; break;
1136                 case MAKE_FOURCC('D','X','T','3') : blksize = 16; break;
1137                 case MAKE_FOURCC('D','X','T','5') : blksize = 16; break;
1138                 default: return DDERR_INVALIDPIXELFORMAT;
1139             }
1140             pDDSD->lpSurface = (char *)This->surface_desc.lpSurface
1141                 + prect->top/4 * (pDDSD->dwWidth+3)/4 * blksize
1142                 + prect->left/4 * blksize;
1143         } else
1144             pDDSD->lpSurface = (char *)This->surface_desc.lpSurface
1145                 + prect->top * This->surface_desc.u1.lPitch
1146                 + prect->left * GET_BPP(This->surface_desc);
1147     } else {
1148         This->lock_update(This, NULL, flags);
1149     }
1150
1151     This->locked = TRUE;
1152
1153     TRACE("locked surface returning description : \n");
1154     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(pDDSD);
1155     
1156     return DD_OK;
1157 }
1158
1159 HRESULT WINAPI
1160 Main_DirectDrawSurface_PageLock(LPDIRECTDRAWSURFACE7 iface, DWORD dwFlags)
1161 {
1162     /* Some surface types should return DDERR_CANTPAGELOCK. */
1163     return DD_OK;
1164 }
1165
1166 HRESULT WINAPI
1167 Main_DirectDrawSurface_PageUnlock(LPDIRECTDRAWSURFACE7 iface, DWORD dwFlags)
1168 {
1169     /* Some surface types should return DDERR_CANTPAGEUNLOCK, and we should
1170      * keep track so we can return DDERR_NOTPAGELOCKED as appropriate. */
1171     return DD_OK;
1172 }
1173
1174 HRESULT WINAPI
1175 Main_DirectDrawSurface_ReleaseDC(LPDIRECTDRAWSURFACE7 iface, HDC hDC)
1176 {
1177     HRESULT hr;
1178     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1179
1180     TRACE("(%p)->(%p)\n",This,hDC);
1181
1182     if (!This->dc_in_use || This->hDC != hDC)
1183         return DDERR_INVALIDPARAMS;
1184
1185     This->release_dc(This, hDC);
1186
1187     hr = IDirectDrawSurface7_Unlock(iface, NULL);
1188     if (FAILED(hr)) return hr;
1189
1190     This->dc_in_use = FALSE;
1191     This->hDC = 0;
1192
1193     return DD_OK;
1194 }
1195
1196 /* Restore */
1197
1198 HRESULT WINAPI
1199 Main_DirectDrawSurface_SetClipper(LPDIRECTDRAWSURFACE7 iface,
1200                                   LPDIRECTDRAWCLIPPER pDDClipper)
1201 {
1202     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1203
1204     TRACE("(%p)->(%p)\n",This,pDDClipper);
1205     if (pDDClipper == ICOM_INTERFACE(This->clipper, IDirectDrawClipper))
1206         return DD_OK;
1207
1208     if (This->clipper != NULL)
1209         IDirectDrawClipper_Release(ICOM_INTERFACE(This->clipper,
1210                                                   IDirectDrawClipper));
1211
1212     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper,
1213                                 pDDClipper);
1214     if (pDDClipper != NULL)
1215         IDirectDrawClipper_AddRef(pDDClipper);
1216
1217     return DD_OK;
1218 }
1219
1220 HRESULT WINAPI
1221 Main_DirectDrawSurface_SetColorKey(LPDIRECTDRAWSURFACE7 iface,
1222                                    DWORD dwFlags, LPDDCOLORKEY pCKey)
1223 {
1224     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1225     
1226     TRACE("(%p)->(%08lx,%p)\n",This,dwFlags,pCKey);
1227
1228     if (TRACE_ON(ddraw)) {
1229         TRACE(" - colorkey flags : ");
1230         DDRAW_dump_colorkeyflag(dwFlags);
1231     }
1232
1233     if ((dwFlags & DDCKEY_COLORSPACE) != 0) {
1234         FIXME(" colorkey value not supported (%08lx) !\n", dwFlags);
1235         return DDERR_INVALIDPARAMS;
1236     }
1237     
1238     /* TODO: investigate if this function can take multiple bits set at the same
1239              time (ie setting multiple colorkey values at the same time with only
1240              one API call).
1241     */
1242     if (pCKey) {
1243         switch (dwFlags & ~DDCKEY_COLORSPACE) {
1244             case DDCKEY_DESTBLT:
1245                 This->surface_desc.ddckCKDestBlt = *pCKey;
1246                 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
1247                 break;
1248
1249             case DDCKEY_DESTOVERLAY:
1250                 This->surface_desc.u3.ddckCKDestOverlay = *pCKey;
1251                 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
1252                 break;
1253
1254             case DDCKEY_SRCOVERLAY:
1255                 This->surface_desc.ddckCKSrcOverlay = *pCKey;
1256                 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
1257                 break;
1258
1259             case DDCKEY_SRCBLT:
1260                 This->surface_desc.ddckCKSrcBlt = *pCKey;
1261                 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
1262                 break;
1263
1264             default:
1265                 return DDERR_INVALIDPARAMS;
1266         }
1267     } else {
1268         switch (dwFlags & ~DDCKEY_COLORSPACE) {
1269             case DDCKEY_DESTBLT:
1270                 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
1271                 break;
1272
1273             case DDCKEY_DESTOVERLAY:
1274                 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
1275                 break;
1276
1277             case DDCKEY_SRCOVERLAY:
1278                 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
1279                 break;
1280
1281             case DDCKEY_SRCBLT:
1282                 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
1283                 break;
1284
1285             default:
1286                 return DDERR_INVALIDPARAMS;
1287         }
1288     }
1289
1290     if (This->aux_setcolorkey_cb) This->aux_setcolorkey_cb(This, dwFlags, pCKey);
1291
1292     return DD_OK;
1293 }
1294
1295 HRESULT WINAPI
1296 Main_DirectDrawSurface_SetLOD(LPDIRECTDRAWSURFACE7 iface, DWORD dwMaxLOD)
1297 {
1298     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1299
1300     TRACE("(%p)->(%08lx)\n",This,dwMaxLOD);
1301     CHECK_TEXTURE(This);
1302
1303     This->max_lod = dwMaxLOD;
1304     return DD_OK;
1305 }
1306
1307 HRESULT WINAPI
1308 Main_DirectDrawSurface_SetOverlayPosition(LPDIRECTDRAWSURFACE7 iface,
1309                                           LONG X, LONG Y)
1310 {
1311     return DDERR_NOTAOVERLAYSURFACE;
1312 }
1313
1314 HRESULT WINAPI
1315 Main_DirectDrawSurface_SetPalette(LPDIRECTDRAWSURFACE7 iface,
1316                                   LPDIRECTDRAWPALETTE pPalette)
1317 {
1318     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1319     IDirectDrawPalette *pal_to_rel = NULL;
1320
1321     TRACE("(%p)->(%p)\n",This,pPalette);
1322     if (pPalette == ICOM_INTERFACE(This->palette, IDirectDrawPalette))
1323         return DD_OK;
1324
1325     if (This->palette != NULL) {
1326         if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1327             This->palette->global.dwFlags &= ~DDPCAPS_PRIMARYSURFACE;
1328         pal_to_rel = ICOM_INTERFACE(This->palette, IDirectDrawPalette);
1329     }
1330
1331     This->palette = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette,
1332                                 pPalette);
1333     if (pPalette != NULL) {
1334         IDirectDrawPalette_AddRef(pPalette);
1335         if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1336             This->palette->global.dwFlags |= DDPCAPS_PRIMARYSURFACE;
1337     }
1338
1339     This->set_palette(This, This->palette);
1340
1341     /* Do the palette release at the end to prevent doing some 'loop' when removing
1342      * the surface maintaining the last reference on a palette.
1343      */
1344     if (pal_to_rel != NULL)
1345         IDirectDrawPalette_Release(pal_to_rel);
1346
1347     return DD_OK;
1348 }
1349
1350 HRESULT WINAPI
1351 Main_DirectDrawSurface_SetPriority(LPDIRECTDRAWSURFACE7 iface,
1352                                    DWORD dwPriority)
1353 {
1354     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1355
1356     TRACE("(%p)->(%08lx)\n",This,dwPriority);
1357     CHECK_TEXTURE(This);
1358
1359     This->priority = dwPriority;
1360     return DD_OK;
1361 }
1362
1363 /* Be careful when locking this: it is risky to call the object's AddRef
1364  * or Release holding a lock. */
1365 HRESULT WINAPI
1366 Main_DirectDrawSurface_SetPrivateData(LPDIRECTDRAWSURFACE7 iface,
1367                                       REFGUID tag, LPVOID pData,
1368                                       DWORD cbSize, DWORD dwFlags)
1369 {
1370     PrivateData* data;
1371     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1372
1373     TRACE("(%p)->(%p), size=%ld\n", This, pData, cbSize);
1374
1375     data = find_private_data(This, tag);
1376     if (data == NULL)
1377     {
1378         data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
1379         if (data == NULL) return DDERR_OUTOFMEMORY;
1380
1381         data->tag = *tag;
1382         data->flags = dwFlags;
1383         data->uniqueness_value = This->uniqueness_value;
1384
1385         if (dwFlags & DDSPD_IUNKNOWNPTR)
1386         {
1387             data->ptr.object = (LPUNKNOWN)pData;
1388             data->size = sizeof(LPUNKNOWN);
1389             IUnknown_AddRef(data->ptr.object);
1390         }
1391         else
1392         {
1393             data->ptr.data = HeapAlloc(GetProcessHeap(), 0, cbSize);
1394             if (data->ptr.data == NULL)
1395             {
1396                 HeapFree(GetProcessHeap(), 0, data);
1397                 return DDERR_OUTOFMEMORY;
1398             }
1399
1400             data->size = cbSize;
1401             memcpy(data->ptr.data, pData, data->size);
1402         }
1403
1404         /* link it in */
1405         data->next = This->private_data;
1406         data->prev = NULL;
1407         if (This->private_data)
1408             This->private_data->prev = data;
1409         This->private_data = data;
1410
1411         return DD_OK;
1412     }
1413     else
1414     {
1415         /* I don't actually know how windows handles this case. The only
1416          * reason I don't just call FreePrivateData is because I want to
1417          * guarantee SetPrivateData working when using LPUNKNOWN or data
1418          * that is no larger than the old data. */
1419
1420         FIXME("Replacing existing private data not implemented yet.\n");
1421         return E_FAIL;
1422     }
1423 }
1424
1425 /* SetSurfaceDesc */
1426
1427 HRESULT WINAPI
1428 Main_DirectDrawSurface_Unlock(LPDIRECTDRAWSURFACE7 iface, LPRECT pRect)
1429 {
1430     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1431
1432     TRACE("(%p)->Unlock(%p)\n",This,pRect);
1433
1434     if (!This->locked) {
1435         WARN("Surface not locked - returing DDERR_NOTLOCKED\n");
1436         return DDERR_NOTLOCKED;
1437     }
1438
1439     This->locked = FALSE;
1440     This->unlock_update(This, pRect);
1441     if (This->aux_unlock)
1442         This->aux_unlock(This->aux_ctx, This->aux_data, pRect);
1443
1444     return DD_OK;
1445 }
1446
1447 HRESULT WINAPI
1448 Main_DirectDrawSurface_UpdateOverlay(LPDIRECTDRAWSURFACE7 iface,
1449                                      LPRECT pSrcRect,
1450                                      LPDIRECTDRAWSURFACE7 pDstSurface,
1451                                      LPRECT pDstRect, DWORD dwFlags,
1452                                      LPDDOVERLAYFX pFX)
1453 {
1454     return DDERR_UNSUPPORTED;
1455 }
1456
1457 /* MSDN: "not currently implemented." */
1458 HRESULT WINAPI
1459 Main_DirectDrawSurface_UpdateOverlayDisplay(LPDIRECTDRAWSURFACE7 iface,
1460                                             DWORD dwFlags)
1461 {
1462     return DDERR_UNSUPPORTED;
1463 }
1464
1465 HRESULT WINAPI
1466 Main_DirectDrawSurface_UpdateOverlayZOrder(LPDIRECTDRAWSURFACE7 iface,
1467                                            DWORD dwFlags,
1468                                            LPDIRECTDRAWSURFACE7 pDDSRef)
1469 {
1470     return DDERR_NOTAOVERLAYSURFACE;
1471 }