ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / dlls / ddraw / dsurface / dib.c
1 /*              DIBSection DirectDrawSurface driver
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 1998-2000 Lionel Ulmer
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
22 #include "config.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "winerror.h"
31 #include "bitmap.h"
32 #include "wine/debug.h"
33 #include "ddraw_private.h"
34 #include "dsurface/main.h"
35 #include "dsurface/dib.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38
39 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable;
40
41 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
42 inline static int get_dib_width_bytes( int width, int depth )
43 {
44     int words;
45
46     switch(depth)
47     {
48     case 1:  words = (width + 31) / 32; break;
49     case 4:  words = (width + 7) / 8; break;
50     case 8:  words = (width + 3) / 4; break;
51     case 15:
52     case 16: words = (width + 1) / 2; break;
53     case 24: words = (width * 3 + 3)/4; break;
54     default:
55         WARN("(%d): Unsupported depth\n", depth );
56         /* fall through */
57     case 32: words = width; break;
58     }
59     return 4 * words;
60 }
61
62
63 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
64 {
65     BITMAPINFO* b_info;
66     UINT usage;
67     HDC ddc;
68     DIB_DirectDrawSurfaceImpl* priv = This->private;
69
70     assert(This->surface_desc.lpSurface != NULL);
71
72     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
73     {
74     case 16:
75     case 32:
76         /* Allocate extra space to store the RGB bit masks. */
77         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
78                                         sizeof(BITMAPINFOHEADER)
79                                         + 3 * sizeof(DWORD));
80         break;
81
82     case 24:
83         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
84                                         sizeof(BITMAPINFOHEADER));
85         break;
86
87     default:
88         /* Allocate extra space for a palette. */
89         b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
90                                         sizeof(BITMAPINFOHEADER)
91                                         + sizeof(RGBQUAD)
92                                         * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
93         break;
94     }
95
96     b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
97     b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
98     b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
99     b_info->bmiHeader.biPlanes = 1;
100     b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
101
102     if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
103         && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
104         b_info->bmiHeader.biCompression = BI_RGB;
105     else
106         b_info->bmiHeader.biCompression = BI_BITFIELDS;
107
108     b_info->bmiHeader.biSizeImage
109         = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
110         * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
111
112     b_info->bmiHeader.biXPelsPerMeter = 0;
113     b_info->bmiHeader.biYPelsPerMeter = 0;
114     b_info->bmiHeader.biClrUsed = 0;
115     b_info->bmiHeader.biClrImportant = 0;
116
117     switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
118     {
119     case 16:
120     case 32:
121     {
122         DWORD *masks = (DWORD *) &(b_info->bmiColors);
123
124         usage = 0;
125         masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
126         masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
127         masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
128     }
129     break;
130
131     case 24:
132         /* Nothing to do */
133         usage = DIB_RGB_COLORS;
134         break;
135
136     default:
137         /* Don't know palette */
138         usage = 0;
139         break;
140     }
141
142     ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
143     if (ddc == 0)
144     {
145         HeapFree(GetProcessHeap(), 0, b_info);
146         return HRESULT_FROM_WIN32(GetLastError());
147     }
148
149     priv->dib.DIBsection
150         = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
151                                (DWORD)This->surface_desc.lpSurface,
152                                This->surface_desc.u1.lPitch);
153     DeleteDC(ddc);
154     if (!priv->dib.DIBsection) {
155         ERR("CreateDIBSection failed!\n");
156         HeapFree(GetProcessHeap(), 0, b_info);
157         return HRESULT_FROM_WIN32(GetLastError());
158     }
159
160     TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
161     if (!This->surface_desc.u1.lPitch) {
162         /* This can't happen, right? */
163         /* or use GDI_GetObj to get it from the created DIB? */
164         This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
165         This->surface_desc.dwFlags |= DDSD_PITCH;
166     }
167
168     if (!This->surface_desc.lpSurface) {
169         This->surface_desc.lpSurface = priv->dib.bitmap_data;
170         This->surface_desc.dwFlags |= DDSD_LPSURFACE;
171     }
172
173     HeapFree(GetProcessHeap(), 0, b_info);
174
175     /* I don't think it's worth checking for this. */
176     if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
177         ERR("unexpected error creating DirectDrawSurface DIB section\n");
178
179     /* this seems like a good place to put the handle for HAL driver use */
180     This->global_more.hKernelSurface = (ULONG_PTR)priv->dib.DIBsection;
181
182     return S_OK;
183 }
184
185 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
186 {
187     DIB_DirectDrawSurfaceImpl* priv = This->private;
188
189     DeleteObject(priv->dib.DIBsection);
190
191     if (!priv->dib.client_memory)
192         VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
193
194     Main_DirectDrawSurface_final_release(This);
195 }
196
197 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
198                                                 LPDIRECTDRAWSURFACE7* ppDup)
199 {
200     return DIB_DirectDrawSurface_Create(This->ddraw_owner,
201                                         &This->surface_desc, ppDup, NULL);
202 }
203
204 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
205                                         IDirectDrawImpl *pDD,
206                                         const DDSURFACEDESC2 *pDDSD)
207 {
208     HRESULT hr;
209     DIB_DirectDrawSurfaceImpl* priv = This->private;
210
211     TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
212     hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
213     if (FAILED(hr)) return hr;
214
215     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
216                         DIB_IDirectDrawSurface7_VTable);
217
218     This->final_release = DIB_DirectDrawSurface_final_release;
219     This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
220     This->flip_data = DIB_DirectDrawSurface_flip_data;
221
222     This->get_dc     = DIB_DirectDrawSurface_get_dc;
223     This->release_dc = DIB_DirectDrawSurface_release_dc;
224     This->hDC = NULL;
225
226     This->set_palette    = DIB_DirectDrawSurface_set_palette;
227     This->update_palette = DIB_DirectDrawSurface_update_palette;
228
229     TRACE("(%ldx%ld, pitch=%ld)\n",
230           This->surface_desc.dwWidth, This->surface_desc.dwHeight,
231           This->surface_desc.u1.lPitch);
232     /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
233
234     if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
235     {
236         /* "Client memory": it is managed by the application. */
237         /* XXX What if lPitch is not set? Use dwWidth or fail? */
238
239         priv->dib.client_memory = TRUE;
240     }
241     else
242     {
243         if (!(This->surface_desc.dwFlags & DDSD_PITCH))
244         {
245             int pitch = This->surface_desc.u1.lPitch;
246             if (pitch % 8 != 0)
247                 pitch += 8 - (pitch % 8);
248         }
249         /* XXX else: how should lPitch be verified? */
250
251         This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
252
253         This->surface_desc.lpSurface
254             = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
255                            * This->surface_desc.dwHeight + 4, /* The + 4 here is for dumb games reading after the end of the surface
256                                                                  when reading the last byte / half using word access */
257                            MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
258
259         if (This->surface_desc.lpSurface == NULL)
260         {
261             Main_DirectDrawSurface_final_release(This);
262             return HRESULT_FROM_WIN32(GetLastError());
263         }
264
265         priv->dib.client_memory = FALSE;
266     }
267
268     hr = create_dib(This);
269     if (FAILED(hr))
270     {
271         if (!priv->dib.client_memory)
272             VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
273
274         Main_DirectDrawSurface_final_release(This);
275
276         return hr;
277     }
278
279     return DD_OK;
280 }
281
282 /* Not an API */
283 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
284                                      const DDSURFACEDESC2 *pDDSD,
285                                      LPDIRECTDRAWSURFACE7 *ppSurf,
286                                      IUnknown *pUnkOuter)
287 {
288     IDirectDrawSurfaceImpl* This;
289     HRESULT hr;
290     assert(pUnkOuter == NULL);
291
292     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
293                      sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
294     if (This == NULL) return E_OUTOFMEMORY;
295
296     This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
297
298     hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
299     if (FAILED(hr))
300         HeapFree(GetProcessHeap(), 0, This);
301     else
302         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
303
304     return hr;
305
306 }
307
308 /* AddAttachedSurface: generic */
309 /* AddOverlayDirtyRect: generic, unimplemented */
310
311 static HRESULT _Blt_ColorFill(
312     LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
313 ) {
314     int x, y;
315     LPBYTE first;
316
317     /* Do first row */
318
319 #define COLORFILL_ROW(type) { \
320     type *d = (type *) buf; \
321     for (x = 0; x < width; x++) \
322         d[x] = (type) color; \
323     break; \
324 }
325
326     switch(bpp) {
327     case 1: COLORFILL_ROW(BYTE)
328     case 2: COLORFILL_ROW(WORD)
329     case 4: COLORFILL_ROW(DWORD)
330     default:
331         FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
332         return DDERR_UNSUPPORTED;
333     }
334
335 #undef COLORFILL_ROW
336
337     /* Now copy first row */
338     first = buf;
339     for (y = 1; y < height; y++) {
340         buf += lPitch;
341         memcpy(buf, first, width * bpp);
342     }
343     return DD_OK;
344 }
345
346 HRESULT WINAPI
347 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
348                           LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
349                           DWORD dwFlags, LPDDBLTFX lpbltfx)
350 {
351     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
352     RECT                xdst,xsrc;
353     DDSURFACEDESC2      ddesc,sdesc;
354     HRESULT             ret = DD_OK;
355     int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
356     int x, y;
357     LPBYTE dbuf, sbuf;
358
359     TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
360
361     if (TRACE_ON(ddraw)) {
362         if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
363         if (rsrc) TRACE("\tsrcrect  :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
364         TRACE("\tflags: ");
365         DDRAW_dump_DDBLT(dwFlags);
366         if (dwFlags & DDBLT_DDFX) {
367             TRACE("\tblitfx: ");
368             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
369         }
370     }
371
372     /* First, check if the possible override function handles this case */
373     if (This->aux_blt != NULL) {
374         if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
375     }
376
377     DD_STRUCT_INIT(&ddesc);
378     DD_STRUCT_INIT(&sdesc);
379
380     sdesc.dwSize = sizeof(sdesc);
381     if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
382     ddesc.dwSize = sizeof(ddesc);
383     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
384
385     if (rdst) {
386         memcpy(&xdst,rdst,sizeof(xdst));
387     } else {
388         xdst.top        = 0;
389         xdst.bottom     = ddesc.dwHeight;
390         xdst.left       = 0;
391         xdst.right      = ddesc.dwWidth;
392     }
393
394     if (rsrc) {
395         memcpy(&xsrc,rsrc,sizeof(xsrc));
396     } else {
397         if (src) {
398             xsrc.top    = 0;
399             xsrc.bottom = sdesc.dwHeight;
400             xsrc.left   = 0;
401             xsrc.right  = sdesc.dwWidth;
402         } else {
403             memset(&xsrc,0,sizeof(xsrc));
404         }
405     }
406
407     /* First check for the validity of source / destination rectangles. This was
408        verified using a test application + by MSDN.
409     */
410     if ((src != NULL) &&
411         ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
412          (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
413          (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
414          (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
415          (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
416         WARN("Application gave us bad source rectangle for Blt.\n");
417         return DDERR_INVALIDRECT;
418     }
419     /* For the Destination rect, it can be out of bounds on the condition that a clipper
420        is set for the given surface.
421     */
422     if ((This->clipper == NULL) &&
423         ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
424          (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
425          (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
426          (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
427          (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
428         WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
429         return DDERR_INVALIDRECT;
430     }
431     
432     /* Now handle negative values in the rectangles. Warning: only supported for now
433        in the 'simple' cases (ie not in any stretching / rotation cases).
434
435        First, the case where nothing is to be done.
436     */
437     if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
438         ((src != NULL) &&
439          ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
440     {
441         TRACE("Nothing to be done !\n");
442         goto release;
443     }
444
445     /* The easy case : the source-less blits.... */
446     if (src == NULL) {
447         RECT full_rect;
448         RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
449
450         full_rect.left   = 0;
451         full_rect.top    = 0;
452         full_rect.right  = ddesc.dwWidth;
453         full_rect.bottom = ddesc.dwHeight;
454         IntersectRect(&temp_rect, &full_rect, &xdst);
455         xdst = temp_rect;
456     } else {
457         /* Only handle clipping on the destination rectangle */
458         int clip_horiz = (xdst.left < 0) || (xdst.right  > (int) ddesc.dwWidth );
459         int clip_vert  = (xdst.top  < 0) || (xdst.bottom > (int) ddesc.dwHeight);
460         if (clip_vert || clip_horiz) {
461             /* Now check if this is a special case or not... */
462             if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
463                 (((xdst.right  - xdst.left) != (xsrc.right  - xsrc.left)) && clip_horiz) ||
464                 (dwFlags & DDBLT_DDFX)) {
465                 WARN("Out of screen rectangle in special case. Not handled right now.\n");
466                 goto release;
467             }
468
469             if (clip_horiz) {
470               if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
471               if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
472             }
473             if (clip_vert) {
474                 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
475                 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
476             }
477             /* And check if after clipping something is still to be done... */
478             if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
479                 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
480                 TRACE("Nothing to be done after clipping !\n");
481                 goto release;
482             }
483         }
484     }
485
486     bpp = GET_BPP(ddesc);
487     srcheight = xsrc.bottom - xsrc.top;
488     srcwidth = xsrc.right - xsrc.left;
489     dstheight = xdst.bottom - xdst.top;
490     dstwidth = xdst.right - xdst.left;
491     width = (xdst.right - xdst.left) * bpp;
492
493     assert(width <= ddesc.u1.lPitch);
494
495     dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
496
497     if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
498     {
499         static BOOL displayed = FALSE;
500         if (!displayed)
501         {
502             FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
503             displayed = TRUE;
504         }
505         dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
506     }
507
508     /* First, all the 'source-less' blits */
509     if (dwFlags & DDBLT_COLORFILL) {
510         ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
511                              ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
512         dwFlags &= ~DDBLT_COLORFILL;
513     }
514
515     if (dwFlags & DDBLT_DEPTHFILL)
516         FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
517     if (dwFlags & DDBLT_ROP) {
518         /* Catch some degenerate cases here */
519         switch(lpbltfx->dwROP) {
520         case BLACKNESS:
521             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
522             break;
523         case 0xAA0029: /* No-op */
524             break;
525         case WHITENESS:
526             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
527             break;
528         case SRCCOPY: /* well, we do that below ? */
529             break;
530         default:
531             FIXME("Unsupported raster op: %08lx  Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
532             goto error;
533         }
534         dwFlags &= ~DDBLT_ROP;
535     }
536     if (dwFlags & DDBLT_DDROPS) {
537         FIXME("\tDdraw Raster Ops: %08lx  Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
538     }
539     /* Now the 'with source' blits */
540     if (src) {
541         LPBYTE sbase;
542         int sx, xinc, sy, yinc;
543
544         if (!dstwidth || !dstheight) /* hmm... stupid program ? */
545             goto release;
546         sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
547         xinc = (srcwidth << 16) / dstwidth;
548         yinc = (srcheight << 16) / dstheight;
549
550         if (!dwFlags) {
551             /* No effects, we can cheat here */
552             if (dstwidth == srcwidth) {
553                 if (dstheight == srcheight) {
554                     /* No stretching in either direction. This needs to be as
555                      * fast as possible */
556                     sbuf = sbase;
557
558                     /* check for overlapping surfaces */
559                     if (src != iface || xdst.top < xsrc.top ||
560                         xdst.right <= xsrc.left || xsrc.right <= xdst.left)
561                     {
562                         /* no overlap, or dst above src, so copy from top downwards */
563                         for (y = 0; y < dstheight; y++)
564                         {
565                             memcpy(dbuf, sbuf, width);
566                             sbuf += sdesc.u1.lPitch;
567                             dbuf += ddesc.u1.lPitch;
568                         }
569                     }
570                     else if (xdst.top > xsrc.top)  /* copy from bottom upwards */
571                     {
572                         sbuf += (sdesc.u1.lPitch*dstheight);
573                         dbuf += (ddesc.u1.lPitch*dstheight);
574                         for (y = 0; y < dstheight; y++)
575                         {
576                             sbuf -= sdesc.u1.lPitch;
577                             dbuf -= ddesc.u1.lPitch;
578                             memcpy(dbuf, sbuf, width);
579                         }
580                     }
581                     else /* src and dst overlapping on the same line, use memmove */
582                     {
583                         for (y = 0; y < dstheight; y++)
584                         {
585                             memmove(dbuf, sbuf, width);
586                             sbuf += sdesc.u1.lPitch;
587                             dbuf += ddesc.u1.lPitch;
588                         }
589                     }
590                 } else {
591                     /* Stretching in Y direction only */
592                     for (y = sy = 0; y < dstheight; y++, sy += yinc) {
593                         sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
594                         memcpy(dbuf, sbuf, width);
595                         dbuf += ddesc.u1.lPitch;
596                     }
597                 }
598             } else {
599                 /* Stretching in X direction */
600                 int last_sy = -1;
601                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
602                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
603
604                     if ((sy >> 16) == (last_sy >> 16)) {
605                         /* this sourcerow is the same as last sourcerow -
606                          * copy already stretched row
607                          */
608                         memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
609                     } else {
610 #define STRETCH_ROW(type) { \
611                     type *s = (type *) sbuf, *d = (type *) dbuf; \
612                     for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
613                     d[x] = s[sx >> 16]; \
614                     break; }
615
616                     switch(bpp) {
617                     case 1: STRETCH_ROW(BYTE)
618                     case 2: STRETCH_ROW(WORD)
619                     case 4: STRETCH_ROW(DWORD)
620                     case 3: {
621                         LPBYTE s,d = dbuf;
622                         for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
623                             DWORD pixel;
624
625                             s = sbuf+3*(sx>>16);
626                             pixel = (s[0]<<16)|(s[1]<<8)|s[2];
627                             d[0] = (pixel>>16)&0xff;
628                             d[1] = (pixel>> 8)&0xff;
629                             d[2] = (pixel    )&0xff;
630                             d+=3;
631                         }
632                         break;
633                     }
634                     default:
635                         FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
636                         ret = DDERR_UNSUPPORTED;
637                         goto error;
638                     }
639 #undef STRETCH_ROW
640                     }
641                     dbuf += ddesc.u1.lPitch;
642                     last_sy = sy;
643                 }
644             }
645         } else {
646            LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
647            DWORD keylow = 0, keyhigh = 0;
648            if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
649
650               if (dwFlags & DDBLT_KEYSRC) {
651                  keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
652                  keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
653               } else if (dwFlags & DDBLT_KEYDEST){
654                  keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
655                  keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
656               } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
657                  keylow  = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
658                  keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
659               } else {
660                  keylow  = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
661                  keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
662               }
663               dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
664            }
665
666            if (dwFlags & DDBLT_DDFX)  {
667               LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
668               LONG tmpxy;
669               dTopLeft     = dbuf;
670               dTopRight    = dbuf+((dstwidth-1)*bpp);
671               dBottomLeft  = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
672               dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
673
674               if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
675                  /* I don't think we need to do anything about this flag */
676                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
677               }
678               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
679                  tmp          = dTopRight;
680                  dTopRight    = dTopLeft;
681                  dTopLeft     = tmp;
682                  tmp          = dBottomRight;
683                  dBottomRight = dBottomLeft;
684                  dBottomLeft  = tmp;
685                  dstxinc = dstxinc *-1;
686               }
687               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
688                  tmp          = dTopLeft;
689                  dTopLeft     = dBottomLeft;
690                  dBottomLeft  = tmp;
691                  tmp          = dTopRight;
692                  dTopRight    = dBottomRight;
693                  dBottomRight = tmp;
694                  dstyinc = dstyinc *-1;
695               }
696               if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
697                  /* I don't think we need to do anything about this flag */
698                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
699               }
700               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
701                  tmp          = dBottomRight;
702                  dBottomRight = dTopLeft;
703                  dTopLeft     = tmp;
704                  tmp          = dBottomLeft;
705                  dBottomLeft  = dTopRight;
706                  dTopRight    = tmp;
707                  dstxinc = dstxinc * -1;
708                  dstyinc = dstyinc * -1;
709               }
710               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
711                  tmp          = dTopLeft;
712                  dTopLeft     = dBottomLeft;
713                  dBottomLeft  = dBottomRight;
714                  dBottomRight = dTopRight;
715                  dTopRight    = tmp;
716                  tmpxy   = dstxinc;
717                  dstxinc = dstyinc;
718                  dstyinc = tmpxy;
719                  dstxinc = dstxinc * -1;
720               }
721               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
722                  tmp          = dTopLeft;
723                  dTopLeft     = dTopRight;
724                  dTopRight    = dBottomRight;
725                  dBottomRight = dBottomLeft;
726                  dBottomLeft  = tmp;
727                  tmpxy   = dstxinc;
728                  dstxinc = dstyinc;
729                  dstyinc = tmpxy;
730                  dstyinc = dstyinc * -1;
731               }
732               if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
733                  /* I don't think we need to do anything about this flag */
734                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
735               }
736               dbuf = dTopLeft;
737               dwFlags &= ~(DDBLT_DDFX);
738            }
739
740 #define COPY_COLORKEY_FX(type) { \
741             type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
742             for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
743                (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
744                (LPBYTE)dx = d; \
745                for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
746                   tmp = s[sx >> 16]; \
747                   if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
748                   (LPBYTE)dx += dstxinc; \
749                   } \
750                (LPBYTE)d += dstyinc; \
751             } \
752             break; }
753
754             switch (bpp) {
755             case 1: COPY_COLORKEY_FX(BYTE)
756             case 2: COPY_COLORKEY_FX(WORD)
757             case 4: COPY_COLORKEY_FX(DWORD)
758             case 3: {LPBYTE s,d = dbuf, dx;
759                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
760                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
761                     dx = d;
762                     for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
763                         DWORD pixel;
764                         s = sbuf+3*(sx>>16);
765                         pixel = (s[0]<<16)|(s[1]<<8)|s[2];
766                         if (pixel < keylow || pixel > keyhigh){
767                             dx[0] = (pixel>>16)&0xff;
768                             dx[1] = (pixel>> 8)&0xff;
769                             dx[2] = (pixel    )&0xff;
770                         }
771                         dx+= dstxinc;
772                     }
773                     d += dstyinc;
774                 }
775                 break;}
776             default:
777                FIXME("%s color-keyed blit not implemented for bpp %d!\n",
778                   (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
779                   ret = DDERR_UNSUPPORTED;
780                   goto error;
781 #undef COPY_COLORKEY_FX
782             }
783         }
784     }
785
786 error:
787     if (dwFlags && FIXME_ON(ddraw)) {
788         FIXME("\tUnsupported flags: ");
789         DDRAW_dump_DDBLT(dwFlags);
790     }
791
792 release:
793     IDirectDrawSurface7_Unlock(iface,NULL);
794     if (src) IDirectDrawSurface7_Unlock(src,NULL);
795     return DD_OK;
796 }
797
798 /* BltBatch: generic, unimplemented */
799
800 HRESULT WINAPI
801 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
802                               DWORD dsty, LPDIRECTDRAWSURFACE7 src,
803                               LPRECT rsrc, DWORD trans)
804 {
805     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
806     int                 bpp, w, h, x, y;
807     DDSURFACEDESC2      ddesc,sdesc;
808     HRESULT             ret = DD_OK;
809     LPBYTE              sbuf, dbuf;
810     RECT                rsrc2;
811
812
813     if (TRACE_ON(ddraw)) {
814         FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
815                 This,dstx,dsty,src,rsrc,trans
816         );
817         FIXME("\ttrans:");
818         if (FIXME_ON(ddraw))
819           DDRAW_dump_DDBLTFAST(trans);
820         if (rsrc)
821           FIXME("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
822         else
823           FIXME(" srcrect: NULL\n");
824     }
825
826     /* First, check if the possible override function handles this case */
827     if (This->aux_bltfast != NULL) {
828         if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
829     }
830
831     /* We need to lock the surfaces, or we won't get refreshes when done. */
832     sdesc.dwSize = sizeof(sdesc);
833     IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
834     ddesc.dwSize = sizeof(ddesc);
835     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
836
837    if (!rsrc) {
838            WARN("rsrc is NULL!\n");
839            rsrc = &rsrc2;
840            rsrc->left = rsrc->top = 0;
841            rsrc->right = sdesc.dwWidth;
842            rsrc->bottom = sdesc.dwHeight;
843    }
844
845     bpp = GET_BPP(This->surface_desc);
846     sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
847     dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
848
849
850     h=rsrc->bottom-rsrc->top;
851     if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
852     if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
853     if (h<0) h=0;
854
855     w=rsrc->right-rsrc->left;
856     if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
857     if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
858     if (w<0) w=0;
859
860     if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
861         DWORD keylow, keyhigh;
862         if (trans & DDBLTFAST_SRCCOLORKEY) {
863             keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
864             keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
865         } else {
866             /* I'm not sure if this is correct */
867             FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
868             keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
869             keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
870         }
871
872 #define COPYBOX_COLORKEY(type) { \
873     type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
874     s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
875     d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
876     for (y = 0; y < h; y++) { \
877         for (x = 0; x < w; x++) { \
878             tmp = s[x]; \
879             if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
880         } \
881         (LPBYTE)s += sdesc.u1.lPitch; \
882         (LPBYTE)d += ddesc.u1.lPitch; \
883     } \
884     break; \
885 }
886
887         switch (bpp) {
888         case 1: COPYBOX_COLORKEY(BYTE)
889         case 2: COPYBOX_COLORKEY(WORD)
890         case 4: COPYBOX_COLORKEY(DWORD)
891         default:
892             FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
893             ret = DDERR_UNSUPPORTED;
894             goto error;
895         }
896 #undef COPYBOX_COLORKEY
897     } else {
898         int width = w * bpp;
899
900         for (y = 0; y < h; y++) {
901             memcpy(dbuf, sbuf, width);
902             sbuf += sdesc.u1.lPitch;
903             dbuf += ddesc.u1.lPitch;
904         }
905     }
906 error:
907     IDirectDrawSurface7_Unlock(iface, NULL);
908     IDirectDrawSurface7_Unlock(src, NULL);
909     return ret;
910 }
911
912 /* ChangeUniquenessValue: generic */
913 /* DeleteAttachedSurface: generic */
914 /* EnumAttachedSurfaces: generic */
915 /* EnumOverlayZOrders: generic, unimplemented */
916
917 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
918                                      IDirectDrawSurfaceImpl* back,
919                                      DWORD dwFlags)
920 {
921     DIB_DirectDrawSurfaceImpl* front_priv = front->private;
922     DIB_DirectDrawSurfaceImpl* back_priv = back->private;
923
924     TRACE("(%p,%p)\n",front,back);
925
926     {
927         HBITMAP tmp;
928         tmp = front_priv->dib.DIBsection;
929         front_priv->dib.DIBsection = back_priv->dib.DIBsection;
930         back_priv->dib.DIBsection = tmp;
931     }
932
933     {
934         void* tmp;
935         tmp = front_priv->dib.bitmap_data;
936         front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
937         back_priv->dib.bitmap_data = tmp;
938
939         tmp = front->surface_desc.lpSurface;
940         front->surface_desc.lpSurface = back->surface_desc.lpSurface;
941         back->surface_desc.lpSurface = tmp;
942     }
943
944     /* client_memory should not be different, but just in case */
945     {
946         BOOL tmp;
947         tmp = front_priv->dib.client_memory;
948         front_priv->dib.client_memory = back_priv->dib.client_memory;
949         back_priv->dib.client_memory = tmp;
950     }
951
952     return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
953 }
954
955 /* Flip: generic */
956 /* FreePrivateData: generic */
957 /* GetAttachedSurface: generic */
958 /* GetBltStatus: generic */
959 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
960 /* GetClipper: generic */
961 /* GetColorKey: generic */
962
963 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
964 {
965     DIB_PRIV_VAR(priv, This);
966     HDC hDC;
967
968     TRACE("Grabbing a DC for surface: %p\n", This);
969
970     hDC = CreateCompatibleDC(0);
971     priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
972     if (This->palette)
973         SelectPalette(hDC, This->palette->hpal, FALSE);
974
975     *phDC = hDC;
976
977     return S_OK;
978 }
979
980 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
981 {
982     DIB_PRIV_VAR(priv, This);
983
984     TRACE("Releasing DC for surface: %p\n", This);
985
986     SelectObject(hDC, priv->dib.holdbitmap);
987     DeleteDC(hDC);
988
989     return S_OK;
990 }
991
992 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
993 {
994     return DIB_DirectDrawSurface_alloc_dc(This, phDC);
995 }
996
997 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
998 {
999     return DIB_DirectDrawSurface_free_dc(This, hDC);
1000 }
1001
1002 /* GetDDInterface: generic */
1003 /* GetFlipStatus: generic */
1004 /* GetLOD: generic */
1005 /* GetOverlayPosition: generic */
1006 /* GetPalette: generic */
1007 /* GetPixelFormat: generic */
1008 /* GetPriority: generic */
1009 /* GetPrivateData: generic */
1010 /* GetSurfaceDesc: generic */
1011 /* GetUniquenessValue: generic */
1012 /* Initialize: generic */
1013 /* IsLost: generic */
1014 /* Lock: generic with callback? */
1015 /* PageLock: generic */
1016 /* PageUnlock: generic */
1017
1018 HRESULT WINAPI
1019 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1020 {
1021     TRACE("(%p)\n",iface);
1022     return DD_OK;       /* ??? */
1023 }
1024
1025 /* SetClipper: generic */
1026 /* SetColorKey: generic */
1027 /* SetLOD: generic */
1028 /* SetOverlayPosition: generic */
1029
1030 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1031                                        IDirectDrawPaletteImpl* pal)
1032 {
1033     if (!pal) return;
1034     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1035         This->update_palette(This, pal,
1036                              0, pal->palNumEntries,
1037                              pal->palents);
1038 }
1039
1040 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1041                                           IDirectDrawPaletteImpl* pal,
1042                                           DWORD dwStart, DWORD dwCount,
1043                                           LPPALETTEENTRY palent)
1044 {
1045     RGBQUAD col[256];
1046     int n;
1047     HDC dc;
1048
1049     TRACE("updating primary palette\n");
1050     for (n=0; n<dwCount; n++) {
1051       col[n].rgbRed   = palent[n].peRed;
1052       col[n].rgbGreen = palent[n].peGreen;
1053       col[n].rgbBlue  = palent[n].peBlue;
1054       col[n].rgbReserved = 0;
1055     }
1056     This->get_dc(This, &dc);
1057     SetDIBColorTable(dc, dwStart, dwCount, col);
1058     This->release_dc(This, dc);
1059
1060     /* Propagate change to backbuffers if there are any */
1061     /* Basically this is a modification of the Flip code to find the backbuffer */
1062     /* and duplicate the palette update there as well */
1063     if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1064         == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1065     {
1066         static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1067         LPDIRECTDRAWSURFACE7 tgt;
1068
1069         HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1070                                                             &back_caps, &tgt);
1071         if (!FAILED(hr))
1072         {
1073             IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1074                                                          IDirectDrawSurface7,tgt);
1075             IDirectDrawSurface7_Release(tgt);
1076             target->get_dc(target, &dc);
1077             SetDIBColorTable(dc, dwStart, dwCount, col);
1078             target->release_dc(target, dc);
1079         }
1080     }
1081 }
1082
1083 /* SetPalette: generic */
1084 /* SetPriority: generic */
1085 /* SetPrivateData: generic */
1086
1087 HRESULT WINAPI
1088 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1089                                      LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1090 {
1091     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1092     DIB_PRIV_VAR(priv, This);
1093     HRESULT hr = DD_OK;
1094     DWORD flags = pDDSD->dwFlags;
1095
1096     if (TRACE_ON(ddraw)) {
1097         TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1098         DDRAW_dump_surface_desc(pDDSD);
1099     }
1100
1101     if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1102         flags &= ~DDSD_PIXELFORMAT;
1103         if (flags & DDSD_LPSURFACE) {
1104             This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1105         } else {
1106             FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1107         }
1108     }
1109     if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1110         HBITMAP oldbmp = priv->dib.DIBsection;
1111         LPVOID oldsurf = This->surface_desc.lpSurface;
1112         BOOL oldc = priv->dib.client_memory;
1113
1114         flags &= ~DDSD_LPSURFACE;
1115
1116         TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1117         This->surface_desc.lpSurface = pDDSD->lpSurface;
1118         priv->dib.client_memory = TRUE;
1119
1120         hr = create_dib(This);
1121         if (FAILED(hr))
1122         {
1123             priv->dib.DIBsection = oldbmp;
1124             This->surface_desc.lpSurface = oldsurf;
1125             priv->dib.client_memory = oldc;
1126             return hr;
1127         }
1128
1129         DeleteObject(oldbmp);
1130
1131         if (!oldc)
1132             VirtualFree(oldsurf, 0, MEM_RELEASE);
1133     }
1134     if (flags) {
1135         WARN("Unhandled flags : %08lx\n", flags);
1136     }
1137     return hr;
1138 }
1139
1140 /* Unlock: ???, need callback */
1141 /* UpdateOverlay: generic */
1142 /* UpdateOverlayDisplay: generic */
1143 /* UpdateOverlayZOrder: generic */
1144
1145 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1146 {
1147     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1148     Main_DirectDrawSurface_QueryInterface,
1149     Main_DirectDrawSurface_AddRef,
1150     Main_DirectDrawSurface_Release,
1151     Main_DirectDrawSurface_AddAttachedSurface,
1152     Main_DirectDrawSurface_AddOverlayDirtyRect,
1153     DIB_DirectDrawSurface_Blt,
1154     Main_DirectDrawSurface_BltBatch,
1155     DIB_DirectDrawSurface_BltFast,
1156     Main_DirectDrawSurface_DeleteAttachedSurface,
1157     Main_DirectDrawSurface_EnumAttachedSurfaces,
1158     Main_DirectDrawSurface_EnumOverlayZOrders,
1159     Main_DirectDrawSurface_Flip,
1160     Main_DirectDrawSurface_GetAttachedSurface,
1161     Main_DirectDrawSurface_GetBltStatus,
1162     Main_DirectDrawSurface_GetCaps,
1163     Main_DirectDrawSurface_GetClipper,
1164     Main_DirectDrawSurface_GetColorKey,
1165     Main_DirectDrawSurface_GetDC,
1166     Main_DirectDrawSurface_GetFlipStatus,
1167     Main_DirectDrawSurface_GetOverlayPosition,
1168     Main_DirectDrawSurface_GetPalette,
1169     Main_DirectDrawSurface_GetPixelFormat,
1170     Main_DirectDrawSurface_GetSurfaceDesc,
1171     Main_DirectDrawSurface_Initialize,
1172     Main_DirectDrawSurface_IsLost,
1173     Main_DirectDrawSurface_Lock,
1174     Main_DirectDrawSurface_ReleaseDC,
1175     DIB_DirectDrawSurface_Restore,
1176     Main_DirectDrawSurface_SetClipper,
1177     Main_DirectDrawSurface_SetColorKey,
1178     Main_DirectDrawSurface_SetOverlayPosition,
1179     Main_DirectDrawSurface_SetPalette,
1180     Main_DirectDrawSurface_Unlock,
1181     Main_DirectDrawSurface_UpdateOverlay,
1182     Main_DirectDrawSurface_UpdateOverlayDisplay,
1183     Main_DirectDrawSurface_UpdateOverlayZOrder,
1184     Main_DirectDrawSurface_GetDDInterface,
1185     Main_DirectDrawSurface_PageLock,
1186     Main_DirectDrawSurface_PageUnlock,
1187     DIB_DirectDrawSurface_SetSurfaceDesc,
1188     Main_DirectDrawSurface_SetPrivateData,
1189     Main_DirectDrawSurface_GetPrivateData,
1190     Main_DirectDrawSurface_FreePrivateData,
1191     Main_DirectDrawSurface_GetUniquenessValue,
1192     Main_DirectDrawSurface_ChangeUniquenessValue,
1193     Main_DirectDrawSurface_SetPriority,
1194     Main_DirectDrawSurface_GetPriority,
1195     Main_DirectDrawSurface_SetLOD,
1196     Main_DirectDrawSurface_GetLOD
1197 };