wined3d: Only update the swizzle and use map for streams that are actually used in...
[wine] / dlls / ddrawex / surface.c
1 /*
2  * Copyright 2008 Stefan Dösinger for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "wine/debug.h"
20
21 #define COBJMACROS
22
23 #include "winbase.h"
24 #include "wingdi.h"
25
26 #include "ddraw.h"
27 #include "d3d.h"
28
29 #include "ddrawex_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ddrawex);
32
33 /******************************************************************************
34  * Helper functions for COM management
35  ******************************************************************************/
36 static IDirectDrawSurfaceImpl *impl_from_dds3(IDirectDrawSurface3 *iface)
37 {
38     if(!iface) return NULL;
39     return (IDirectDrawSurfaceImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawSurface3_Vtbl));
40 }
41 static IDirectDrawSurface3 *dds3_from_impl(IDirectDrawSurfaceImpl *This)
42 {
43     if(!This) return NULL;
44     return (IDirectDrawSurface3 *) &This->IDirectDrawSurface3_Vtbl;
45 }
46
47 static IDirectDrawSurfaceImpl *impl_from_dds4(IDirectDrawSurface4 *iface)
48 {
49     if(!iface) return NULL;
50     return (IDirectDrawSurfaceImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawSurface4_Vtbl));
51 }
52 static IDirectDrawSurface4 *dds4_from_impl(IDirectDrawSurfaceImpl *This)
53 {
54     if(!This) return NULL;
55     return (IDirectDrawSurface4 *) &This->IDirectDrawSurface4_Vtbl;
56 }
57
58 static HRESULT WINAPI
59 IDirectDrawSurface4Impl_QueryInterface(IDirectDrawSurface4 *iface,
60                                        REFIID riid,
61                                        void **obj)
62 {
63     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
64
65     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
66     *obj = NULL;
67
68     if(!riid)
69         return DDERR_INVALIDPARAMS;
70
71     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
72     if (IsEqualGUID(riid, &IID_IUnknown)
73      || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
74     {
75         *obj = dds4_from_impl(This);
76         IDirectDrawSurface4_AddRef((IDirectDrawSurface4 *) *obj);
77         TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
78         return S_OK;
79     }
80     else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
81           || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
82           || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
83     {
84         *obj = dds3_from_impl(This);
85         IDirectDrawSurface3_AddRef((IDirectDrawSurface3 *) *obj);
86         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
87         return S_OK;
88     }
89     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
90     {
91         FIXME("Implement IDirectDrawGammaControl in ddrawex\n");
92     }
93     else if( IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
94              IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
95     {
96         /* Most likely not supported */
97         FIXME("Test IDirect3DDevice in ddrawex\n");
98     }
99     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
100              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
101     {
102         FIXME("Implement IDirect3dTexture in ddrawex\n");
103     }
104     else
105     {
106         WARN("No interface\n");
107     }
108
109     return E_NOINTERFACE;
110 }
111
112 static HRESULT WINAPI
113 IDirectDrawSurface3Impl_QueryInterface(IDirectDrawSurface3 *iface,
114                                        REFIID riid,
115                                        void **obj)
116 {
117     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
118     TRACE("(%p)->(%s,%p): Thunking to IDirectDrawSurface4\n",This,debugstr_guid(riid),obj);
119     return IDirectDrawSurface4_QueryInterface(dds4_from_impl(This), riid, obj);
120 }
121
122 static ULONG WINAPI
123 IDirectDrawSurface4Impl_AddRef(IDirectDrawSurface4 *iface)
124 {
125     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
126     ULONG ref = InterlockedIncrement(&This->ref);
127
128     TRACE("(%p) : incrementing refcount from %u.\n", This, ref - 1);
129
130     return ref;
131 }
132
133 static ULONG WINAPI
134 IDirectDrawSurface3Impl_AddRef(IDirectDrawSurface3 *iface)
135 {
136     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
137     TRACE("(%p): Thunking to IDirectDrawSurface4\n", This);
138     return IDirectDrawSurface4_AddRef(dds4_from_impl(This));
139 }
140
141 static ULONG WINAPI
142 IDirectDrawSurface4Impl_Release(IDirectDrawSurface4 *iface)
143 {
144     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
145     ULONG ref = InterlockedDecrement(&This->ref);
146
147     TRACE("(%p) : decrementing refcount to %u.\n", This, ref);
148
149     if(ref == 0)
150     {
151         TRACE("Destroying object\n");
152         IDirectDrawSurface4_FreePrivateData(This->parent, &IID_DDrawexPriv);
153         IDirectDrawSurface4_Release(This->parent);
154         HeapFree(GetProcessHeap(), 0, This);
155     }
156     return ref;
157 }
158
159 static ULONG WINAPI
160 IDirectDrawSurface3Impl_Release(IDirectDrawSurface3 *iface)
161 {
162     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
163     TRACE("(%p): Thunking to IDirectDrawSurface4\n", This);
164     return IDirectDrawSurface4_Release(dds4_from_impl(This));
165 }
166
167 static HRESULT WINAPI
168 IDirectDrawSurface4Impl_AddAttachedSurface(IDirectDrawSurface4 *iface,
169                                            IDirectDrawSurface4 *Attach_iface)
170 {
171     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
172     IDirectDrawSurfaceImpl *attach = impl_from_dds4(Attach_iface);
173     TRACE("(%p)->(%p)\n", This, attach);
174     return IDirectDrawSurface4_AddAttachedSurface(This->parent, attach->parent);
175 }
176
177 static HRESULT WINAPI
178 IDirectDrawSurface3Impl_AddAttachedSurface(IDirectDrawSurface3 *iface,
179                                            IDirectDrawSurface3 *Attach_iface)
180 {
181     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
182     IDirectDrawSurfaceImpl *attach = impl_from_dds3(Attach_iface);
183     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, attach);
184     return IDirectDrawSurface4_AddAttachedSurface(dds4_from_impl(This), dds4_from_impl(attach));
185 }
186
187 static HRESULT WINAPI
188 IDirectDrawSurface4Impl_AddOverlayDirtyRect(IDirectDrawSurface4 *iface,
189                                             RECT *Rect)
190 {
191     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
192     TRACE("(%p)->(%p)\n", This, Rect);
193     return IDirectDrawSurface4_AddOverlayDirtyRect(This->parent, Rect);
194 }
195
196 static HRESULT WINAPI
197 IDirectDrawSurface3Impl_AddOverlayDirtyRect(IDirectDrawSurface3 *iface,
198                                             RECT *Rect)
199 {
200     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
201     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Rect);
202     return IDirectDrawSurface4_AddOverlayDirtyRect(dds4_from_impl(This), Rect);
203 }
204
205 static HRESULT WINAPI
206 IDirectDrawSurface4Impl_Blt(IDirectDrawSurface4 *iface,
207                             RECT *DestRect,
208                             IDirectDrawSurface4 *SrcSurface,
209                             RECT *SrcRect,
210                             DWORD Flags,
211                             DDBLTFX *DDBltFx)
212 {
213     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
214     IDirectDrawSurfaceImpl *Src = impl_from_dds4(SrcSurface);
215     TRACE("(%p)->(%p,%p,%p,0x%08x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
216     return IDirectDrawSurface4_Blt(This->parent, DestRect, Src ? Src->parent : NULL,
217                                    SrcRect, Flags, DDBltFx);
218 }
219
220 static HRESULT WINAPI
221 IDirectDrawSurface3Impl_Blt(IDirectDrawSurface3 *iface,
222                             RECT *DestRect,
223                             IDirectDrawSurface3 *SrcSurface,
224                             RECT *SrcRect,
225                             DWORD Flags,
226                             DDBLTFX *DDBltFx)
227 {
228     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
229     IDirectDrawSurfaceImpl *Src = impl_from_dds3(SrcSurface);
230     TRACE("(%p)->(%p,%p,%p,0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
231     return IDirectDrawSurface4_Blt(dds4_from_impl(This), DestRect, dds4_from_impl(Src),
232                                    SrcRect, Flags, DDBltFx);
233 }
234
235 static HRESULT WINAPI
236 IDirectDrawSurface4Impl_BltBatch(IDirectDrawSurface4 *iface,
237                                  DDBLTBATCH *Batch,
238                                  DWORD Count,
239                                  DWORD Flags)
240 {
241     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
242     TRACE("(%p)->(%p,%u,0x%08x)\n", This, Batch, Count, Flags);
243     return IDirectDrawSurface4_BltBatch(This->parent, Batch, Count, Flags);
244 }
245
246 static HRESULT WINAPI
247 IDirectDrawSurface3Impl_BltBatch(IDirectDrawSurface3 *iface,
248                                  DDBLTBATCH *Batch,
249                                  DWORD Count,
250                                  DWORD Flags)
251 {
252     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
253     TRACE("(%p)->(%p,%u,0x%08x): Thunking to IDirectDrawSurface4\n", This, Batch, Count, Flags);
254     return IDirectDrawSurface4_BltBatch(dds4_from_impl(This), Batch, Count, Flags);
255 }
256
257 static HRESULT WINAPI
258 IDirectDrawSurface4Impl_BltFast(IDirectDrawSurface4 *iface,
259                                 DWORD dstx,
260                                 DWORD dsty,
261                                 IDirectDrawSurface4 *Source,
262                                 RECT *rsrc,
263                                 DWORD trans)
264 {
265     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
266     IDirectDrawSurfaceImpl *Src = impl_from_dds4(Source);
267     TRACE("(%p)->(%u,%u,%p,%p,0x%08x)\n", This, dstx, dsty, Src, rsrc, trans);
268     return IDirectDrawSurface4_BltFast(This->parent, dstx, dsty, Src ? Src->parent : NULL,
269                                        rsrc, trans);
270 }
271
272 static HRESULT WINAPI
273 IDirectDrawSurface3Impl_BltFast(IDirectDrawSurface3 *iface,
274                                 DWORD dstx,
275                                 DWORD dsty,
276                                 IDirectDrawSurface3 *Source,
277                                 RECT *rsrc,
278                                 DWORD trans)
279 {
280     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
281     IDirectDrawSurfaceImpl *Src = impl_from_dds3(Source);
282     TRACE("(%p)->(%u,%u,%p,%p,0x%08x): Thunking to IDirectDrawSurface4\n", This, dstx, dsty, Src, rsrc, trans);
283     return IDirectDrawSurface4_BltFast(dds4_from_impl(This), dstx, dsty, dds4_from_impl(Src),
284                                        rsrc, trans);
285 }
286
287 static HRESULT WINAPI
288 IDirectDrawSurface4Impl_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
289                                               DWORD Flags,
290                                               IDirectDrawSurface4 *Attach)
291 {
292     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
293     IDirectDrawSurfaceImpl *Att = impl_from_dds4(Attach);
294     TRACE("(%p)->(0x%08x,%p)\n", This, Flags, Att);
295     return IDirectDrawSurface4_DeleteAttachedSurface(This->parent, Flags,
296                                                      Att ? Att->parent : NULL);
297 }
298
299 static HRESULT WINAPI
300 IDirectDrawSurface3Impl_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
301                                               DWORD Flags,
302                                               IDirectDrawSurface3 *Attach)
303 {
304     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
305     IDirectDrawSurfaceImpl *Att = impl_from_dds3(Attach);
306     TRACE("(%p)->(0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, Flags, Att);
307     return IDirectDrawSurface4_DeleteAttachedSurface(dds4_from_impl(This), Flags,
308                                                      dds4_from_impl(Att));
309 }
310
311 struct enumsurfaces_wrap
312 {
313     LPDDENUMSURFACESCALLBACK2 orig_cb;
314     void *orig_ctx;
315 };
316
317 static HRESULT WINAPI
318 enumsurfaces_wrap_cb(IDirectDrawSurface4 *surf, DDSURFACEDESC2 *desc, void *vctx)
319 {
320     struct enumsurfaces_wrap *ctx = vctx;
321     IDirectDrawSurface4 *outer = dds_get_outer(surf);
322
323     TRACE("Returning outer surface %p for inner surface %p\n", outer, surf);
324     IDirectDrawSurface4_AddRef(outer);
325     IDirectDrawSurface4_Release(surf);
326     return ctx->orig_cb(outer, desc, vctx);
327 }
328
329 static HRESULT WINAPI
330 IDirectDrawSurface4Impl_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
331                                              void *context,
332                                              LPDDENUMSURFACESCALLBACK2 cb)
333 {
334     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
335     struct enumsurfaces_wrap ctx;
336     TRACE("(%p)->(%p,%p)\n", This, context, cb);
337
338     ctx.orig_cb = cb;
339     ctx.orig_ctx = context;
340     return IDirectDrawSurface4_EnumAttachedSurfaces(This->parent, &ctx, enumsurfaces_wrap_cb);
341 }
342
343 struct enumsurfaces_thunk
344 {
345     LPDDENUMSURFACESCALLBACK orig_cb;
346     void *orig_ctx;
347 };
348
349 static HRESULT WINAPI
350 enumsurfaces_thunk_cb(IDirectDrawSurface4 *surf, DDSURFACEDESC2 *desc2, void *vctx)
351 {
352     IDirectDrawSurfaceImpl *This = impl_from_dds4(surf);
353     struct enumsurfaces_thunk *ctx = vctx;
354     DDSURFACEDESC desc;
355
356     TRACE("Thunking back to IDirectDrawSurface3\n");
357     IDirectDrawSurface3_AddRef(dds3_from_impl(This));
358     IDirectDrawSurface3_Release(surf);
359     DDSD2_to_DDSD(desc2, &desc);
360     return ctx->orig_cb((IDirectDrawSurface *) dds3_from_impl(This), &desc, ctx->orig_ctx);
361 }
362
363 static HRESULT WINAPI
364 IDirectDrawSurface3Impl_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
365                                              void *context,
366                                              LPDDENUMSURFACESCALLBACK cb)
367 {
368     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
369     struct enumsurfaces_thunk ctx;
370     TRACE("(%p)->(%p,%p): Thunking to IDirectDraw4\n", This, context, cb);
371
372     ctx.orig_cb = cb;
373     ctx.orig_ctx = context;
374     return IDirectDrawSurface4_EnumAttachedSurfaces(dds4_from_impl(This), &ctx, enumsurfaces_thunk_cb);
375 }
376
377 static HRESULT WINAPI
378 IDirectDrawSurface4Impl_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
379                                            DWORD Flags,
380                                            void *context,
381                                            LPDDENUMSURFACESCALLBACK2 cb)
382 {
383     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
384     struct enumsurfaces_wrap ctx;
385     TRACE("(%p)->(0x%08x,%p,%p)\n", This, Flags, context, cb);
386
387     ctx.orig_cb = cb;
388     ctx.orig_ctx = context;
389     return IDirectDrawSurface4_EnumOverlayZOrders(This->parent, Flags, &ctx, enumsurfaces_wrap_cb);
390 }
391
392 static HRESULT WINAPI
393 IDirectDrawSurface3Impl_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
394                                            DWORD Flags,
395                                            void *context,
396                                            LPDDENUMSURFACESCALLBACK cb)
397 {
398     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
399     struct enumsurfaces_thunk ctx;
400     TRACE("(%p)->(0x%08x,%p,%p): Thunking to IDirectDraw4\n", This, Flags, context, cb);
401
402     ctx.orig_cb = cb;
403     ctx.orig_ctx = context;
404     return IDirectDrawSurface4_EnumOverlayZOrders(dds4_from_impl(This), Flags, &ctx, enumsurfaces_thunk_cb);
405 }
406
407 static HRESULT WINAPI
408 IDirectDrawSurface4Impl_Flip(IDirectDrawSurface4 *iface,
409                              IDirectDrawSurface4 *DestOverride,
410                              DWORD Flags)
411 {
412     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
413     IDirectDrawSurfaceImpl *Dest = impl_from_dds4(DestOverride);
414     TRACE("(%p)->(%p,0x%08x)\n", This, Dest, Flags);
415     return IDirectDrawSurface4_Flip(This->parent, Dest ? Dest->parent : NULL, Flags);
416 }
417
418 static HRESULT WINAPI
419 IDirectDrawSurface3Impl_Flip(IDirectDrawSurface3 *iface,
420                              IDirectDrawSurface3 *DestOverride,
421                              DWORD Flags)
422 {
423     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
424     IDirectDrawSurfaceImpl *Dest = impl_from_dds3(DestOverride);
425     TRACE("(%p)->(%p,0x%08x): Thunking to IDirectDrawSurface4\n", This, Dest, Flags);
426     return IDirectDrawSurface4_Flip(dds4_from_impl(This), dds4_from_impl(Dest), Flags);
427 }
428
429 static HRESULT WINAPI
430 IDirectDrawSurface4Impl_GetAttachedSurface(IDirectDrawSurface4 *iface,
431                                            DDSCAPS2 *Caps,
432                                            IDirectDrawSurface4 **Surface)
433 {
434     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
435     IDirectDrawSurface4 *inner = NULL;
436     HRESULT hr;
437     TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
438
439     hr = IDirectDrawSurface4_GetAttachedSurface(dds4_from_impl(This), Caps, &inner);
440     if(SUCCEEDED(hr))
441     {
442         *Surface = dds_get_outer(inner);
443         IDirectDrawSurface4_AddRef(*Surface);
444         IDirectDrawSurface4_Release(inner);
445     }
446     else
447     {
448         *Surface = NULL;
449     }
450     return hr;
451 }
452
453 static HRESULT WINAPI
454 IDirectDrawSurface3Impl_GetAttachedSurface(IDirectDrawSurface3 *iface,
455                                            DDSCAPS *Caps,
456                                            IDirectDrawSurface3 **Surface)
457 {
458     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
459     IDirectDrawSurface4 *surf4;
460     DDSCAPS2 caps2;
461     HRESULT hr;
462     TRACE("(%p)->(%p,%p): Thunking to IDirectDrawSurface4\n", This, Caps, Surface);
463
464     memset(&caps2, 0, sizeof(caps2));
465     caps2.dwCaps = Caps->dwCaps;
466     hr = IDirectDrawSurface4_GetAttachedSurface(dds4_from_impl(This), &caps2, &surf4);
467     if(SUCCEEDED(hr))
468     {
469         IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface3, (void **) Surface);
470         IDirectDrawSurface4_Release(surf4);
471     }
472     else
473     {
474         *Surface = NULL;
475     }
476     return hr;
477 }
478
479 static HRESULT WINAPI
480 IDirectDrawSurface4Impl_GetBltStatus(IDirectDrawSurface4 *iface,
481                                      DWORD Flags)
482 {
483     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
484     TRACE("(%p)->(0x%08x)\n", This, Flags);
485     return IDirectDrawSurface4_GetBltStatus(This->parent, Flags);
486 }
487
488 static HRESULT WINAPI
489 IDirectDrawSurface3Impl_GetBltStatus(IDirectDrawSurface3 *iface,
490                                      DWORD Flags)
491 {
492     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
493     TRACE("(%p)->(0x%08x): Thunking to IDirectDrawSurface4\n", This, Flags);
494     return IDirectDrawSurface4_GetBltStatus(dds4_from_impl(This), Flags);
495 }
496
497 static HRESULT WINAPI
498 IDirectDrawSurface4Impl_GetCaps(IDirectDrawSurface4 *iface,
499                                 DDSCAPS2 *Caps)
500 {
501     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
502     TRACE("(%p)->(%p)\n", This, Caps);
503     return IDirectDrawSurface4_GetCaps(This->parent, Caps);
504 }
505
506 static HRESULT WINAPI
507 IDirectDrawSurface3Impl_GetCaps(IDirectDrawSurface3 *iface,
508                                 DDSCAPS *Caps)
509 {
510     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
511     DDSCAPS2 caps2;
512     HRESULT hr;
513     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Caps);
514
515     memset(&caps2, 0, sizeof(caps2));
516     memset(Caps, 0, sizeof(*Caps));
517     hr = IDirectDrawSurface4_GetCaps(dds4_from_impl(This), &caps2);
518     Caps->dwCaps = caps2.dwCaps;
519     return hr;
520 }
521
522 static HRESULT WINAPI
523 IDirectDrawSurface4Impl_GetClipper(IDirectDrawSurface4 *iface,
524                                    IDirectDrawClipper **Clipper)
525 {
526     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
527     TRACE("(%p)->(%p)\n", This, Clipper);
528     return IDirectDrawSurface4_GetClipper(This->parent, Clipper);
529 }
530
531 static HRESULT WINAPI
532 IDirectDrawSurface3Impl_GetClipper(IDirectDrawSurface3 *iface,
533                                    IDirectDrawClipper **Clipper)
534 {
535     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
536     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Clipper);
537     return IDirectDrawSurface4_GetClipper(dds4_from_impl(This), Clipper);
538 }
539
540 static HRESULT WINAPI
541 IDirectDrawSurface4Impl_GetColorKey(IDirectDrawSurface4 *iface,
542                                     DWORD Flags,
543                                     DDCOLORKEY *CKey)
544 {
545     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
546     TRACE("(%p)->(0x%08x,%p)\n", This, Flags, CKey);
547     return IDirectDrawSurface4_GetColorKey(This->parent, Flags, CKey);
548 }
549
550 static HRESULT WINAPI
551 IDirectDrawSurface3Impl_GetColorKey(IDirectDrawSurface3 *iface,
552                                     DWORD Flags,
553                                     DDCOLORKEY *CKey)
554 {
555     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
556     TRACE("(%p)->(0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, Flags, CKey);
557     return IDirectDrawSurface4_GetColorKey(dds4_from_impl(This), Flags, CKey);
558 }
559
560 static HRESULT WINAPI
561 IDirectDrawSurface4Impl_GetDC(IDirectDrawSurface4 *iface,
562                              HDC *hdc)
563 {
564     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
565     TRACE("(%p)->(%p)\n", This, hdc);
566     if(This->permanent_dc)
567     {
568         TRACE("Returning stored dc %p\n", This->hdc);
569         *hdc = This->hdc;
570         return DD_OK;
571     }
572     else
573     {
574         return IDirectDrawSurface4_GetDC(This->parent, hdc);
575     }
576 }
577
578 static HRESULT WINAPI
579 IDirectDrawSurface3Impl_GetDC(IDirectDrawSurface3 *iface,
580                              HDC *hdc)
581 {
582     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
583     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, hdc);
584     return IDirectDrawSurface4_GetDC(dds4_from_impl(This), hdc);
585 }
586
587 static HRESULT WINAPI
588 IDirectDrawSurface4Impl_GetFlipStatus(IDirectDrawSurface4 *iface,
589                                       DWORD Flags)
590 {
591     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
592     TRACE("(%p)->(0x%08x)\n", This, Flags);
593     return IDirectDrawSurface4_GetFlipStatus(This->parent, Flags);
594 }
595
596 static HRESULT WINAPI
597 IDirectDrawSurface3Impl_GetFlipStatus(IDirectDrawSurface3 *iface,
598                                       DWORD Flags)
599 {
600     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
601     TRACE("(%p)->(0x%08x): Thunking to IDirectDrawSurface4\n", This, Flags);
602     return IDirectDrawSurface4_GetFlipStatus(dds4_from_impl(This), Flags);
603 }
604
605 static HRESULT WINAPI
606 IDirectDrawSurface4Impl_GetOverlayPosition(IDirectDrawSurface4 *iface,
607                                            LONG *X,
608                                            LONG *Y)
609 {
610     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
611     TRACE("(%p)->(%p,%p)\n", This, X, Y);
612     return IDirectDrawSurface4_GetOverlayPosition(This->parent, X, Y);
613 }
614
615 static HRESULT WINAPI
616 IDirectDrawSurface3Impl_GetOverlayPosition(IDirectDrawSurface3 *iface,
617                                            LONG *X,
618                                            LONG *Y)
619 {
620     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
621     TRACE("(%p)->(%p,%p): Thunking to IDirectDrawSurface4\n", This, X, Y);
622     return IDirectDrawSurface4_GetOverlayPosition(dds4_from_impl(This), X, Y);
623 }
624
625 static HRESULT WINAPI
626 IDirectDrawSurface4Impl_GetPalette(IDirectDrawSurface4 *iface,
627                                    IDirectDrawPalette **Pal)
628 {
629     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
630     TRACE("(%p)->(%p)\n", This, Pal);
631     return IDirectDrawSurface4_GetPalette(This->parent, Pal);
632 }
633
634 static HRESULT WINAPI
635 IDirectDrawSurface3Impl_GetPalette(IDirectDrawSurface3 *iface,
636                                    IDirectDrawPalette **Pal)
637 {
638     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
639     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Pal);
640     return IDirectDrawSurface4_GetPalette(dds4_from_impl(This), Pal);
641 }
642
643 static HRESULT WINAPI
644 IDirectDrawSurface4Impl_GetPixelFormat(IDirectDrawSurface4 *iface,
645                                        DDPIXELFORMAT *PixelFormat)
646 {
647     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
648     TRACE("(%p)->(%p)\n", This, PixelFormat);
649     return IDirectDrawSurface4_GetPixelFormat(This->parent, PixelFormat);
650 }
651
652 static HRESULT WINAPI
653 IDirectDrawSurface3Impl_GetPixelFormat(IDirectDrawSurface3 *iface,
654                                        DDPIXELFORMAT *PixelFormat)
655 {
656     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
657     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, PixelFormat);
658     return IDirectDrawSurface4_GetPixelFormat(dds4_from_impl(This), PixelFormat);
659 }
660
661 static HRESULT WINAPI
662 IDirectDrawSurface4Impl_GetSurfaceDesc(IDirectDrawSurface4 *iface,
663                                        DDSURFACEDESC2 *DDSD)
664 {
665     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
666     HRESULT hr;
667     TRACE("(%p)->(%p)\n", This, DDSD);
668     hr = IDirectDrawSurface4_GetSurfaceDesc(This->parent, DDSD);
669
670     if(SUCCEEDED(hr) && This->permanent_dc)
671     {
672         DDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
673         DDSD->ddsCaps.dwCaps &= ~DDSCAPS_OWNDC;
674     }
675
676     return hr;
677 }
678
679 static HRESULT WINAPI
680 IDirectDrawSurface3Impl_GetSurfaceDesc(IDirectDrawSurface3 *iface,
681                                        DDSURFACEDESC *DDSD)
682 {
683     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
684     DDSURFACEDESC2 ddsd2;
685     HRESULT hr;
686     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, DDSD);
687
688     memset(&ddsd2, 0, sizeof(ddsd2));
689     ddsd2.dwSize = sizeof(ddsd2);
690     hr = IDirectDrawSurface4_GetSurfaceDesc(dds4_from_impl(This), &ddsd2);
691     DDSD2_to_DDSD(&ddsd2, DDSD);
692     return hr;
693 }
694
695 static HRESULT WINAPI
696 IDirectDrawSurface4Impl_Initialize(IDirectDrawSurface4 *iface,
697                                    IDirectDraw *DD,
698                                    DDSURFACEDESC2 *DDSD)
699 {
700     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
701     IDirectDraw4 *outer_DD4;
702     IDirectDraw4 *inner_DD4;
703     IDirectDraw *inner_DD;
704     HRESULT hr;
705     TRACE("(%p)->(%p,%p)\n", This, DD, DDSD);
706
707     IDirectDraw_QueryInterface(DD, &IID_IDirectDraw4, (void **) &outer_DD4);
708     inner_DD4 = dd_get_inner(outer_DD4);
709     IDirectDraw4_Release(outer_DD4);
710     IDirectDraw4_QueryInterface(inner_DD4, &IID_IDirectDraw4, (void **) &inner_DD);
711     hr = IDirectDrawSurface4_Initialize(This->parent, inner_DD, DDSD);
712     IDirectDraw_Release(inner_DD);
713     return hr;
714 }
715
716 static HRESULT WINAPI
717 IDirectDrawSurface3Impl_Initialize(IDirectDrawSurface3 *iface,
718                                    IDirectDraw *DD,
719                                    DDSURFACEDESC *DDSD)
720 {
721     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
722     DDSURFACEDESC2 ddsd2;
723     TRACE("(%p)->(%p,%p): Thunking to IDirectDrawSurface4\n", This, DD, DDSD);
724     DDSD_to_DDSD2(DDSD, &ddsd2);
725     return IDirectDrawSurface4_Initialize(dds4_from_impl(This), DD, &ddsd2);
726 }
727
728 static HRESULT WINAPI
729 IDirectDrawSurface4Impl_IsLost(IDirectDrawSurface4 *iface)
730 {
731     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
732     TRACE("(%p)\n", This);
733     return IDirectDrawSurface4_IsLost(This->parent);
734 }
735
736 static HRESULT WINAPI
737 IDirectDrawSurface3Impl_IsLost(IDirectDrawSurface3 *iface)
738 {
739     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
740     TRACE("(%p): Thunking to IDirectDrawSurface4\n", This);
741     return IDirectDrawSurface4_IsLost(dds4_from_impl(This));
742 }
743
744 static HRESULT WINAPI
745 IDirectDrawSurface4Impl_Lock(IDirectDrawSurface4 *iface,
746                              RECT *Rect,
747                              DDSURFACEDESC2 *DDSD,
748                              DWORD Flags,
749                              HANDLE h)
750 {
751     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
752     HRESULT hr;
753     TRACE("(%p)->(%p,%p,0x%08x,%p)\n", This, Rect, DDSD, Flags, h);
754     hr = IDirectDrawSurface4_Lock(This->parent, Rect, DDSD, Flags, h);
755
756     if(SUCCEEDED(hr) && This->permanent_dc)
757     {
758         DDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
759         DDSD->ddsCaps.dwCaps &= ~DDSCAPS_OWNDC;
760     }
761
762     return hr;
763 }
764
765 static HRESULT WINAPI
766 IDirectDrawSurface3Impl_Lock(IDirectDrawSurface3 *iface,
767                              RECT *Rect,
768                              DDSURFACEDESC *DDSD,
769                              DWORD Flags,
770                              HANDLE h)
771 {
772     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
773     DDSURFACEDESC2 ddsd2;
774     HRESULT hr;
775     TRACE("(%p)->(%p,%p,0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, Rect, DDSD, Flags, h);
776     memset(&ddsd2, 0, sizeof(ddsd2));
777     ddsd2.dwSize = sizeof(ddsd2);
778     hr = IDirectDrawSurface4_Lock(dds4_from_impl(This), Rect, &ddsd2, Flags, h);
779     DDSD2_to_DDSD(&ddsd2, DDSD);
780     return hr;
781 }
782
783 static HRESULT WINAPI
784 IDirectDrawSurface4Impl_ReleaseDC(IDirectDrawSurface4 *iface,
785                                   HDC hdc)
786 {
787     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
788     TRACE("(%p)->(%p)\n", This, hdc);
789     if(This->permanent_dc)
790     {
791         TRACE("Surface has a permanent DC, not doing anything\n");
792         return DD_OK;
793     }
794     else
795     {
796         return IDirectDrawSurface4_ReleaseDC(This->parent, hdc);
797     }
798 }
799
800 static HRESULT WINAPI
801 IDirectDrawSurface3Impl_ReleaseDC(IDirectDrawSurface3 *iface,
802                                   HDC hdc)
803 {
804     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
805     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, hdc);
806     return IDirectDrawSurface4_ReleaseDC(dds4_from_impl(This), hdc);
807 }
808
809 static HRESULT WINAPI
810 IDirectDrawSurface4Impl_Restore(IDirectDrawSurface4 *iface)
811 {
812     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
813     TRACE("(%p)\n", This);
814     return IDirectDrawSurface4_Restore(This->parent);
815 }
816
817 static HRESULT WINAPI
818 IDirectDrawSurface3Impl_Restore(IDirectDrawSurface3 *iface)
819 {
820     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
821     TRACE("(%p): Thunking to IDirectDrawSurface4\n", This);
822     return IDirectDrawSurface4_Restore(dds4_from_impl(This));
823 }
824
825 static HRESULT WINAPI
826 IDirectDrawSurface4Impl_SetClipper(IDirectDrawSurface4 *iface,
827                                    IDirectDrawClipper *Clipper)
828 {
829     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
830     TRACE("(%p)->(%p)\n", This, Clipper);
831     return IDirectDrawSurface4_SetClipper(This->parent, Clipper);
832 }
833
834 static HRESULT WINAPI
835 IDirectDrawSurface3Impl_SetClipper(IDirectDrawSurface3 *iface,
836                                    IDirectDrawClipper *Clipper)
837 {
838     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
839     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Clipper);
840     return IDirectDrawSurface4_SetClipper(dds4_from_impl(This), Clipper);
841 }
842
843 static HRESULT WINAPI
844 IDirectDrawSurface4Impl_SetColorKey(IDirectDrawSurface4 *iface,
845                                     DWORD Flags,
846                                     DDCOLORKEY *CKey)
847 {
848     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
849     TRACE("(%p)->(0x%08x,%p)\n", This, Flags, CKey);
850     return IDirectDrawSurface4_SetColorKey(This->parent, Flags, CKey);
851 }
852
853 static HRESULT WINAPI
854 IDirectDrawSurface3Impl_SetColorKey(IDirectDrawSurface3 *iface,
855                                     DWORD Flags,
856                                     DDCOLORKEY *CKey)
857 {
858     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
859     TRACE("(%p)->(0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, Flags, CKey);
860     return IDirectDrawSurface4_SetColorKey(dds4_from_impl(This), Flags, CKey);
861 }
862
863 static HRESULT WINAPI
864 IDirectDrawSurface4Impl_SetOverlayPosition(IDirectDrawSurface4 *iface,
865                                            LONG X,
866                                            LONG Y)
867 {
868     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
869     TRACE("(%p)->(%u,%u)\n", This, X, Y);
870     return IDirectDrawSurface4_SetOverlayPosition(This->parent, X, Y);
871 }
872
873 static HRESULT WINAPI
874 IDirectDrawSurface3Impl_SetOverlayPosition(IDirectDrawSurface3 *iface,
875                                            LONG X,
876                                            LONG Y)
877 {
878     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
879     TRACE("(%p)->(%u,%u): Thunking to IDirectDrawSurface4\n", This, X, Y);
880     return IDirectDrawSurface4_SetOverlayPosition(dds4_from_impl(This), X, Y);
881 }
882
883 static HRESULT WINAPI
884 IDirectDrawSurface4Impl_SetPalette(IDirectDrawSurface4 *iface,
885                                    IDirectDrawPalette *Pal)
886 {
887     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
888     TRACE("(%p)->(%p)\n", This, Pal);
889     return IDirectDrawSurface4_SetPalette(This->parent, Pal);
890 }
891
892 static HRESULT WINAPI
893 IDirectDrawSurface3Impl_SetPalette(IDirectDrawSurface3 *iface,
894                                    IDirectDrawPalette *Pal)
895 {
896     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
897     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, Pal);
898     return IDirectDrawSurface4_SetPalette(dds4_from_impl(This), Pal);
899 }
900
901 static HRESULT WINAPI
902 IDirectDrawSurface4Impl_Unlock(IDirectDrawSurface4 *iface,
903                                RECT *pRect)
904 {
905     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
906     TRACE("(%p)->(%p)\n", This, pRect);
907     return IDirectDrawSurface4_Unlock(This->parent, pRect);
908 }
909
910 static HRESULT WINAPI
911 IDirectDrawSurface3Impl_Unlock(IDirectDrawSurface3 *iface,
912                                void *data)
913 {
914     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
915     TRACE("(%p)->(%p): Thunking to IDirectDrawSurface4\n", This, data);
916     return IDirectDrawSurface4_Unlock(dds4_from_impl(This), NULL);
917 }
918
919 static HRESULT WINAPI
920 IDirectDrawSurface4Impl_UpdateOverlay(IDirectDrawSurface4 *iface,
921                                       LPRECT SrcRect,
922                                       IDirectDrawSurface4 *DstSurface,
923                                       LPRECT DstRect,
924                                       DWORD Flags,
925                                       LPDDOVERLAYFX FX)
926 {
927     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
928     IDirectDrawSurfaceImpl *Dst = impl_from_dds4(DstSurface);
929     TRACE("(%p)->(%p,%p,%p,0x%08x,%p)\n", This, SrcRect, Dst, DstRect, Flags, FX);
930     return IDirectDrawSurface4_UpdateOverlay(This->parent, SrcRect, Dst ? Dst->parent : NULL,
931                                              DstRect, Flags, FX);
932 }
933
934 static HRESULT WINAPI
935 IDirectDrawSurface3Impl_UpdateOverlay(IDirectDrawSurface3 *iface,
936                                       LPRECT SrcRect,
937                                       IDirectDrawSurface3 *DstSurface,
938                                       LPRECT DstRect,
939                                       DWORD Flags,
940                                       LPDDOVERLAYFX FX)
941 {
942     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
943     IDirectDrawSurfaceImpl *Dst = impl_from_dds3(DstSurface);
944     TRACE("(%p)->(%p,%p,%p,0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, SrcRect, Dst, DstRect, Flags, FX);
945     return IDirectDrawSurface4_UpdateOverlay(dds4_from_impl(This), SrcRect, dds4_from_impl(Dst),
946                                              DstRect, Flags, FX);
947 }
948
949 static HRESULT WINAPI
950 IDirectDrawSurface4Impl_UpdateOverlayDisplay(IDirectDrawSurface4 *iface,
951                                              DWORD Flags)
952 {
953     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
954     TRACE("(%p)->(0x%08x)\n", This, Flags);
955     return IDirectDrawSurface4_UpdateOverlayDisplay(This->parent, Flags);
956 }
957
958 static HRESULT WINAPI
959 IDirectDrawSurface3Impl_UpdateOverlayDisplay(IDirectDrawSurface3 *iface,
960                                              DWORD Flags)
961 {
962     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
963     TRACE("(%p)->(0x%08x): Thunking to IDirectDrawSurface4\n", This, Flags);
964     return IDirectDrawSurface4_UpdateOverlayDisplay(dds4_from_impl(This), Flags);
965 }
966
967 static HRESULT WINAPI
968 IDirectDrawSurface4Impl_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
969                                             DWORD Flags,
970                                             IDirectDrawSurface4 *DDSRef)
971 {
972     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
973     IDirectDrawSurfaceImpl *Ref = impl_from_dds4(DDSRef);
974     TRACE("(%p)->(0x%08x,%p)\n", This, Flags, Ref);
975     return IDirectDrawSurface4_UpdateOverlayZOrder(This->parent, Flags, Ref ? Ref->parent : NULL);
976 }
977
978 static HRESULT WINAPI
979 IDirectDrawSurface3Impl_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
980                                             DWORD Flags,
981                                             IDirectDrawSurface3 *DDSRef)
982 {
983     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
984     IDirectDrawSurfaceImpl *Ref = impl_from_dds3(DDSRef);
985     TRACE("(%p)->(0x%08x,%p): Thunking to IDirectDrawSurface4\n", This, Flags, Ref);
986     return IDirectDrawSurface4_UpdateOverlayZOrder(dds4_from_impl(This), Flags, dds4_from_impl(Ref));
987 }
988
989 static HRESULT WINAPI
990 IDirectDrawSurface4Impl_GetDDInterface(IDirectDrawSurface4 *iface,
991                                        void **DD)
992 {
993     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
994     FIXME("(%p)->(%p)\n", This, DD);
995     /* This has to be implemented in ddrawex, DDraw's interface can't be used because it is pretty
996      * hard to tell which version of the DD interface is returned
997      */
998     *DD = NULL;
999     return E_FAIL;
1000 }
1001
1002 static HRESULT WINAPI
1003 IDirectDrawSurface3Impl_GetDDInterface(IDirectDrawSurface3 *iface,
1004                                        void **DD)
1005 {
1006     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
1007     FIXME("(%p)->(%p)\n", This, DD);
1008     /* A thunk it pretty pointless because of the same reason relaying to ddraw.dll works badly
1009      */
1010     *DD = NULL;
1011     return E_FAIL;
1012 }
1013
1014 static HRESULT WINAPI
1015 IDirectDrawSurface4Impl_PageLock(IDirectDrawSurface4 *iface,
1016                                  DWORD Flags)
1017 {
1018     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1019     TRACE("(%p)->(%x)\n", iface, Flags);
1020     return IDirectDrawSurface4_PageLock(This->parent, Flags);
1021 }
1022
1023 static HRESULT WINAPI
1024 IDirectDrawSurface3Impl_PageLock(IDirectDrawSurface3 *iface,
1025                                  DWORD Flags)
1026 {
1027     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
1028     TRACE("(%p)->(%x): Thunking to IDirectDrawSurface4\n", iface, Flags);
1029     return IDirectDrawSurface4_PageLock(dds4_from_impl(This), Flags);
1030 }
1031
1032 static HRESULT WINAPI
1033 IDirectDrawSurface4Impl_PageUnlock(IDirectDrawSurface4 *iface,
1034                                   DWORD Flags)
1035 {
1036     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1037     TRACE("(%p)->(%x)\n", iface, Flags);
1038     return IDirectDrawSurface4_PageUnlock(This->parent, Flags);
1039 }
1040
1041 static HRESULT WINAPI
1042 IDirectDrawSurface3Impl_PageUnlock(IDirectDrawSurface3 *iface,
1043                                    DWORD Flags)
1044 {
1045     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
1046     TRACE("(%p)->(%x): Thunking to IDirectDrawSurface4\n", iface, Flags);
1047     return IDirectDrawSurface4_PageUnlock(dds4_from_impl(This), Flags);
1048 }
1049
1050 static HRESULT WINAPI
1051 IDirectDrawSurface4Impl_SetSurfaceDesc(IDirectDrawSurface4 *iface,
1052                                        DDSURFACEDESC2 *DDSD,
1053                                        DWORD Flags)
1054 {
1055     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1056     TRACE("(%p)->(%p,0x%08x)\n", This, DDSD, Flags);
1057     return IDirectDrawSurface4_SetSurfaceDesc(This->parent, DDSD, Flags);
1058 }
1059
1060 static HRESULT WINAPI
1061 IDirectDrawSurface3Impl_SetSurfaceDesc(IDirectDrawSurface3 *iface,
1062                                        DDSURFACEDESC *DDSD,
1063                                        DWORD Flags)
1064 {
1065     IDirectDrawSurfaceImpl *This = impl_from_dds3(iface);
1066     DDSURFACEDESC2 ddsd;
1067     TRACE("(%p)->(%p,0x%08x): Thunking to IDirectDrawSurface4\n", This, DDSD, Flags);
1068
1069     DDSD_to_DDSD2(DDSD, &ddsd);
1070     return IDirectDrawSurface4_SetSurfaceDesc(dds4_from_impl(This), &ddsd, Flags);
1071 }
1072
1073 static HRESULT WINAPI
1074 IDirectDrawSurface4Impl_SetPrivateData(IDirectDrawSurface4 *iface,
1075                                        REFGUID tag,
1076                                        void *Data,
1077                                        DWORD Size,
1078                                        DWORD Flags)
1079 {
1080     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1081     TRACE("(%p)->(%s,%p,%u,0x%08x)\n", iface, debugstr_guid(tag), Data, Size, Flags);
1082
1083     /* To completely avoid this we'd have to clone the private data API in ddrawex */
1084     if(IsEqualGUID(&IID_DDrawexPriv, tag)) {
1085         FIXME("Application uses ddrawex's private guid\n");
1086     }
1087
1088     return IDirectDrawSurface4_SetPrivateData(This->parent, tag, Data, Size, Flags);
1089 }
1090
1091 static HRESULT WINAPI
1092 IDirectDrawSurface4Impl_GetPrivateData(IDirectDrawSurface4 *iface,
1093                                        REFGUID tag,
1094                                        void *Data,
1095                                        DWORD *Size)
1096 {
1097     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1098     TRACE("(%p)->(%s,%p,%p)\n", iface, debugstr_guid(tag), Data, Size);
1099
1100     /* To completely avoid this we'd have to clone the private data API in ddrawex */
1101     if(IsEqualGUID(&IID_DDrawexPriv, tag)) {
1102         FIXME("Application uses ddrawex's private guid\n");
1103     }
1104
1105     return IDirectDrawSurface4_GetPrivateData(This->parent, tag, Data, Size);
1106 }
1107
1108 static HRESULT WINAPI
1109 IDirectDrawSurface4Impl_FreePrivateData(IDirectDrawSurface4 *iface,
1110                                         REFGUID tag)
1111 {
1112     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1113     TRACE("(%p)->(%s)\n", iface, debugstr_guid(tag));
1114
1115     /* To completely avoid this we'd have to clone the private data API in ddrawex */
1116     if(IsEqualGUID(&IID_DDrawexPriv, tag)) {
1117         FIXME("Application uses ddrawex's private guid\n");
1118     }
1119
1120     return IDirectDrawSurface4_FreePrivateData(This->parent, tag);
1121 }
1122
1123 static HRESULT WINAPI
1124 IDirectDrawSurface4Impl_GetUniquenessValue(IDirectDrawSurface4 *iface,
1125                                            LPDWORD pValue)
1126 {
1127     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1128     TRACE("(%p)->(%p)\n", This, pValue);
1129     return IDirectDrawSurface4_GetUniquenessValue(This->parent, pValue);
1130 }
1131
1132 static HRESULT WINAPI
1133 IDirectDrawSurface4Impl_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
1134 {
1135     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1136     TRACE("(%p)\n", This);
1137     return IDirectDrawSurface4_ChangeUniquenessValue(This->parent);
1138 }
1139
1140 const IDirectDrawSurface3Vtbl IDirectDrawSurface3_Vtbl =
1141 {
1142     /* IUnknown */
1143     IDirectDrawSurface3Impl_QueryInterface,
1144     IDirectDrawSurface3Impl_AddRef,
1145     IDirectDrawSurface3Impl_Release,
1146     /* IDirectDrawSurface */
1147     IDirectDrawSurface3Impl_AddAttachedSurface,
1148     IDirectDrawSurface3Impl_AddOverlayDirtyRect,
1149     IDirectDrawSurface3Impl_Blt,
1150     IDirectDrawSurface3Impl_BltBatch,
1151     IDirectDrawSurface3Impl_BltFast,
1152     IDirectDrawSurface3Impl_DeleteAttachedSurface,
1153     IDirectDrawSurface3Impl_EnumAttachedSurfaces,
1154     IDirectDrawSurface3Impl_EnumOverlayZOrders,
1155     IDirectDrawSurface3Impl_Flip,
1156     IDirectDrawSurface3Impl_GetAttachedSurface,
1157     IDirectDrawSurface3Impl_GetBltStatus,
1158     IDirectDrawSurface3Impl_GetCaps,
1159     IDirectDrawSurface3Impl_GetClipper,
1160     IDirectDrawSurface3Impl_GetColorKey,
1161     IDirectDrawSurface3Impl_GetDC,
1162     IDirectDrawSurface3Impl_GetFlipStatus,
1163     IDirectDrawSurface3Impl_GetOverlayPosition,
1164     IDirectDrawSurface3Impl_GetPalette,
1165     IDirectDrawSurface3Impl_GetPixelFormat,
1166     IDirectDrawSurface3Impl_GetSurfaceDesc,
1167     IDirectDrawSurface3Impl_Initialize,
1168     IDirectDrawSurface3Impl_IsLost,
1169     IDirectDrawSurface3Impl_Lock,
1170     IDirectDrawSurface3Impl_ReleaseDC,
1171     IDirectDrawSurface3Impl_Restore,
1172     IDirectDrawSurface3Impl_SetClipper,
1173     IDirectDrawSurface3Impl_SetColorKey,
1174     IDirectDrawSurface3Impl_SetOverlayPosition,
1175     IDirectDrawSurface3Impl_SetPalette,
1176     IDirectDrawSurface3Impl_Unlock,
1177     IDirectDrawSurface3Impl_UpdateOverlay,
1178     IDirectDrawSurface3Impl_UpdateOverlayDisplay,
1179     IDirectDrawSurface3Impl_UpdateOverlayZOrder,
1180     /* IDirectDrawSurface 2 */
1181     IDirectDrawSurface3Impl_GetDDInterface,
1182     IDirectDrawSurface3Impl_PageLock,
1183     IDirectDrawSurface3Impl_PageUnlock,
1184     /* IDirectDrawSurface 3 */
1185     IDirectDrawSurface3Impl_SetSurfaceDesc
1186 };
1187
1188 const IDirectDrawSurface4Vtbl IDirectDrawSurface4_Vtbl =
1189 {
1190     /*** IUnknown ***/
1191     IDirectDrawSurface4Impl_QueryInterface,
1192     IDirectDrawSurface4Impl_AddRef,
1193     IDirectDrawSurface4Impl_Release,
1194     /*** IDirectDrawSurface ***/
1195     IDirectDrawSurface4Impl_AddAttachedSurface,
1196     IDirectDrawSurface4Impl_AddOverlayDirtyRect,
1197     IDirectDrawSurface4Impl_Blt,
1198     IDirectDrawSurface4Impl_BltBatch,
1199     IDirectDrawSurface4Impl_BltFast,
1200     IDirectDrawSurface4Impl_DeleteAttachedSurface,
1201     IDirectDrawSurface4Impl_EnumAttachedSurfaces,
1202     IDirectDrawSurface4Impl_EnumOverlayZOrders,
1203     IDirectDrawSurface4Impl_Flip,
1204     IDirectDrawSurface4Impl_GetAttachedSurface,
1205     IDirectDrawSurface4Impl_GetBltStatus,
1206     IDirectDrawSurface4Impl_GetCaps,
1207     IDirectDrawSurface4Impl_GetClipper,
1208     IDirectDrawSurface4Impl_GetColorKey,
1209     IDirectDrawSurface4Impl_GetDC,
1210     IDirectDrawSurface4Impl_GetFlipStatus,
1211     IDirectDrawSurface4Impl_GetOverlayPosition,
1212     IDirectDrawSurface4Impl_GetPalette,
1213     IDirectDrawSurface4Impl_GetPixelFormat,
1214     IDirectDrawSurface4Impl_GetSurfaceDesc,
1215     IDirectDrawSurface4Impl_Initialize,
1216     IDirectDrawSurface4Impl_IsLost,
1217     IDirectDrawSurface4Impl_Lock,
1218     IDirectDrawSurface4Impl_ReleaseDC,
1219     IDirectDrawSurface4Impl_Restore,
1220     IDirectDrawSurface4Impl_SetClipper,
1221     IDirectDrawSurface4Impl_SetColorKey,
1222     IDirectDrawSurface4Impl_SetOverlayPosition,
1223     IDirectDrawSurface4Impl_SetPalette,
1224     IDirectDrawSurface4Impl_Unlock,
1225     IDirectDrawSurface4Impl_UpdateOverlay,
1226     IDirectDrawSurface4Impl_UpdateOverlayDisplay,
1227     IDirectDrawSurface4Impl_UpdateOverlayZOrder,
1228     /*** IDirectDrawSurface2 ***/
1229     IDirectDrawSurface4Impl_GetDDInterface,
1230     IDirectDrawSurface4Impl_PageLock,
1231     IDirectDrawSurface4Impl_PageUnlock,
1232     /*** IDirectDrawSurface3 ***/
1233     IDirectDrawSurface4Impl_SetSurfaceDesc,
1234     /*** IDirectDrawSurface4 ***/
1235     IDirectDrawSurface4Impl_SetPrivateData,
1236     IDirectDrawSurface4Impl_GetPrivateData,
1237     IDirectDrawSurface4Impl_FreePrivateData,
1238     IDirectDrawSurface4Impl_GetUniquenessValue,
1239     IDirectDrawSurface4Impl_ChangeUniquenessValue,
1240 };
1241
1242 /* dds_get_outer
1243  *
1244  * Given a surface from ddraw.dll it retrieves the pointer to the ddrawex.dll wrapper around it
1245  *
1246  * Parameters:
1247  *  inner: ddraw.dll surface to retrieve the outer surface from
1248  *
1249  * Returns:
1250  *  The surface wrapper. If there is none yet, a new one is created
1251  */
1252 IDirectDrawSurface4 *dds_get_outer(IDirectDrawSurface4 *inner)
1253 {
1254     IDirectDrawSurface4 *outer = NULL;
1255     DWORD size = sizeof(outer);
1256     HRESULT hr;
1257     if(!inner) return NULL;
1258
1259     hr = IDirectDrawSurface4_GetPrivateData(inner,
1260                                             &IID_DDrawexPriv,
1261                                             &outer,
1262                                             &size);
1263     if(FAILED(hr) || outer == NULL)
1264     {
1265         IDirectDrawSurfaceImpl *impl;
1266
1267         TRACE("Creating new ddrawex surface wrapper for surface %p\n", inner);
1268         impl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*impl));
1269         impl->ref = 1;
1270         impl->IDirectDrawSurface3_Vtbl = &IDirectDrawSurface3_Vtbl;
1271         impl->IDirectDrawSurface4_Vtbl = &IDirectDrawSurface4_Vtbl;
1272         IDirectDrawSurface4_AddRef(inner);
1273         impl->parent = inner;
1274
1275         outer = dds4_from_impl(impl);
1276
1277         hr = IDirectDrawSurface4_SetPrivateData(inner,
1278                                                 &IID_DDrawexPriv,
1279                                                 &outer,
1280                                                 sizeof(outer),
1281                                                 0 /* Flags */);
1282         if(FAILED(hr))
1283         {
1284             ERR("IDirectDrawSurface4_SetPrivateData failed\n");
1285         }
1286     }
1287
1288     return outer;
1289 }
1290
1291 IDirectDrawSurface4 *dds_get_inner(IDirectDrawSurface4 *outer)
1292 {
1293     IDirectDrawSurfaceImpl *This = impl_from_dds4(outer);
1294     if(This == NULL) return NULL;
1295     return This->parent;
1296 }
1297
1298 HRESULT prepare_permanent_dc(IDirectDrawSurface4 *iface)
1299 {
1300     IDirectDrawSurfaceImpl *This = impl_from_dds4(iface);
1301     HRESULT hr;
1302     This->permanent_dc = TRUE;
1303
1304     hr = IDirectDrawSurface4_GetDC(This->parent, &This->hdc);
1305     if(FAILED(hr)) return hr;
1306     hr = IDirectDrawSurface4_ReleaseDC(This->parent, This->hdc);
1307     return hr;
1308 }