Added version information.
[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 3: { BYTE *d = (BYTE *) buf;
330               for (x = 0; x < width; x++,d+=3) {
331                 d[0] = (color    ) & 0xFF;
332                 d[1] = (color>> 8) & 0xFF;
333                 d[2] = (color>>16) & 0xFF;
334               }
335               break;}
336     case 4: COLORFILL_ROW(DWORD)
337     default:
338         FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
339         return DDERR_UNSUPPORTED;
340     }
341
342 #undef COLORFILL_ROW
343
344     /* Now copy first row */
345     first = buf;
346     for (y = 1; y < height; y++) {
347         buf += lPitch;
348         memcpy(buf, first, width * bpp);
349     }
350     return DD_OK;
351 }
352
353 HRESULT WINAPI
354 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
355                           LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
356                           DWORD dwFlags, LPDDBLTFX lpbltfx)
357 {
358     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
359     RECT                xdst,xsrc;
360     DDSURFACEDESC2      ddesc,sdesc;
361     HRESULT             ret = DD_OK;
362     int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
363     int x, y;
364     LPBYTE dbuf, sbuf;
365
366     TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
367
368     if (TRACE_ON(ddraw)) {
369         if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
370         if (rsrc) TRACE("\tsrcrect  :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
371         TRACE("\tflags: ");
372         DDRAW_dump_DDBLT(dwFlags);
373         if (dwFlags & DDBLT_DDFX) {
374             TRACE("\tblitfx: ");
375             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
376         }
377     }
378
379     /* First, check if the possible override function handles this case */
380     if (This->aux_blt != NULL) {
381         if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
382     }
383
384     DD_STRUCT_INIT(&ddesc);
385     DD_STRUCT_INIT(&sdesc);
386
387     sdesc.dwSize = sizeof(sdesc);
388     if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
389     ddesc.dwSize = sizeof(ddesc);
390     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
391
392     if (rdst) {
393         memcpy(&xdst,rdst,sizeof(xdst));
394     } else {
395         xdst.top        = 0;
396         xdst.bottom     = ddesc.dwHeight;
397         xdst.left       = 0;
398         xdst.right      = ddesc.dwWidth;
399     }
400
401     if (rsrc) {
402         memcpy(&xsrc,rsrc,sizeof(xsrc));
403     } else {
404         if (src) {
405             xsrc.top    = 0;
406             xsrc.bottom = sdesc.dwHeight;
407             xsrc.left   = 0;
408             xsrc.right  = sdesc.dwWidth;
409         } else {
410             memset(&xsrc,0,sizeof(xsrc));
411         }
412     }
413
414     /* First check for the validity of source / destination rectangles. This was
415        verified using a test application + by MSDN.
416     */
417     if ((src != NULL) &&
418         ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
419          (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
420          (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
421          (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
422          (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
423         WARN("Application gave us bad source rectangle for Blt.\n");
424         return DDERR_INVALIDRECT;
425     }
426     /* For the Destination rect, it can be out of bounds on the condition that a clipper
427        is set for the given surface.
428     */
429     if ((This->clipper == NULL) &&
430         ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
431          (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
432          (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
433          (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
434          (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
435         WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
436         return DDERR_INVALIDRECT;
437     }
438     
439     /* Now handle negative values in the rectangles. Warning: only supported for now
440        in the 'simple' cases (ie not in any stretching / rotation cases).
441
442        First, the case where nothing is to be done.
443     */
444     if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
445         ((src != NULL) &&
446          ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
447     {
448         TRACE("Nothing to be done !\n");
449         goto release;
450     }
451
452     /* The easy case : the source-less blits.... */
453     if (src == NULL) {
454         RECT full_rect;
455         RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
456
457         full_rect.left   = 0;
458         full_rect.top    = 0;
459         full_rect.right  = ddesc.dwWidth;
460         full_rect.bottom = ddesc.dwHeight;
461         IntersectRect(&temp_rect, &full_rect, &xdst);
462         xdst = temp_rect;
463     } else {
464         /* Only handle clipping on the destination rectangle */
465         int clip_horiz = (xdst.left < 0) || (xdst.right  > (int) ddesc.dwWidth );
466         int clip_vert  = (xdst.top  < 0) || (xdst.bottom > (int) ddesc.dwHeight);
467         if (clip_vert || clip_horiz) {
468             /* Now check if this is a special case or not... */
469             if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
470                 (((xdst.right  - xdst.left) != (xsrc.right  - xsrc.left)) && clip_horiz) ||
471                 (dwFlags & DDBLT_DDFX)) {
472                 WARN("Out of screen rectangle in special case. Not handled right now.\n");
473                 goto release;
474             }
475
476             if (clip_horiz) {
477               if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
478               if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
479             }
480             if (clip_vert) {
481                 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
482                 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
483             }
484             /* And check if after clipping something is still to be done... */
485             if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
486                 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
487                 TRACE("Nothing to be done after clipping !\n");
488                 goto release;
489             }
490         }
491     }
492
493     bpp = GET_BPP(ddesc);
494     srcheight = xsrc.bottom - xsrc.top;
495     srcwidth = xsrc.right - xsrc.left;
496     dstheight = xdst.bottom - xdst.top;
497     dstwidth = xdst.right - xdst.left;
498     width = (xdst.right - xdst.left) * bpp;
499
500     assert(width <= ddesc.u1.lPitch);
501
502     dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
503
504     if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
505     {
506         static BOOL displayed = FALSE;
507         if (!displayed)
508         {
509             FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
510             displayed = TRUE;
511         }
512         dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
513     }
514
515     /* First, all the 'source-less' blits */
516     if (dwFlags & DDBLT_COLORFILL) {
517         ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
518                              ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
519         dwFlags &= ~DDBLT_COLORFILL;
520     }
521
522     if (dwFlags & DDBLT_DEPTHFILL)
523         FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
524     if (dwFlags & DDBLT_ROP) {
525         /* Catch some degenerate cases here */
526         switch(lpbltfx->dwROP) {
527         case BLACKNESS:
528             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
529             break;
530         case 0xAA0029: /* No-op */
531             break;
532         case WHITENESS:
533             ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
534             break;
535         case SRCCOPY: /* well, we do that below ? */
536             break;
537         default:
538             FIXME("Unsupported raster op: %08lx  Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
539             goto error;
540         }
541         dwFlags &= ~DDBLT_ROP;
542     }
543     if (dwFlags & DDBLT_DDROPS) {
544         FIXME("\tDdraw Raster Ops: %08lx  Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
545     }
546     /* Now the 'with source' blits */
547     if (src) {
548         LPBYTE sbase;
549         int sx, xinc, sy, yinc;
550
551         if (!dstwidth || !dstheight) /* hmm... stupid program ? */
552             goto release;
553         sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
554         xinc = (srcwidth << 16) / dstwidth;
555         yinc = (srcheight << 16) / dstheight;
556
557         if (!dwFlags) {
558             /* No effects, we can cheat here */
559             if (dstwidth == srcwidth) {
560                 if (dstheight == srcheight) {
561                     /* No stretching in either direction. This needs to be as
562                      * fast as possible */
563                     sbuf = sbase;
564
565                     /* check for overlapping surfaces */
566                     if (src != iface || xdst.top < xsrc.top ||
567                         xdst.right <= xsrc.left || xsrc.right <= xdst.left)
568                     {
569                         /* no overlap, or dst above src, so copy from top downwards */
570                         for (y = 0; y < dstheight; y++)
571                         {
572                             memcpy(dbuf, sbuf, width);
573                             sbuf += sdesc.u1.lPitch;
574                             dbuf += ddesc.u1.lPitch;
575                         }
576                     }
577                     else if (xdst.top > xsrc.top)  /* copy from bottom upwards */
578                     {
579                         sbuf += (sdesc.u1.lPitch*dstheight);
580                         dbuf += (ddesc.u1.lPitch*dstheight);
581                         for (y = 0; y < dstheight; y++)
582                         {
583                             sbuf -= sdesc.u1.lPitch;
584                             dbuf -= ddesc.u1.lPitch;
585                             memcpy(dbuf, sbuf, width);
586                         }
587                     }
588                     else /* src and dst overlapping on the same line, use memmove */
589                     {
590                         for (y = 0; y < dstheight; y++)
591                         {
592                             memmove(dbuf, sbuf, width);
593                             sbuf += sdesc.u1.lPitch;
594                             dbuf += ddesc.u1.lPitch;
595                         }
596                     }
597                 } else {
598                     /* Stretching in Y direction only */
599                     for (y = sy = 0; y < dstheight; y++, sy += yinc) {
600                         sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
601                         memcpy(dbuf, sbuf, width);
602                         dbuf += ddesc.u1.lPitch;
603                     }
604                 }
605             } else {
606                 /* Stretching in X direction */
607                 int last_sy = -1;
608                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
609                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
610
611                     if ((sy >> 16) == (last_sy >> 16)) {
612                         /* this sourcerow is the same as last sourcerow -
613                          * copy already stretched row
614                          */
615                         memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
616                     } else {
617 #define STRETCH_ROW(type) { \
618                     type *s = (type *) sbuf, *d = (type *) dbuf; \
619                     for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
620                     d[x] = s[sx >> 16]; \
621                     break; }
622
623                     switch(bpp) {
624                     case 1: STRETCH_ROW(BYTE)
625                     case 2: STRETCH_ROW(WORD)
626                     case 4: STRETCH_ROW(DWORD)
627                     case 3: {
628                         LPBYTE s,d = dbuf;
629                         for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
630                             DWORD pixel;
631
632                             s = sbuf+3*(sx>>16);
633                             pixel = s[0]|(s[1]<<8)|(s[2]<<16);
634                             d[0] = (pixel    )&0xff;
635                             d[1] = (pixel>> 8)&0xff;
636                             d[2] = (pixel>>16)&0xff;
637                             d+=3;
638                         }
639                         break;
640                     }
641                     default:
642                         FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
643                         ret = DDERR_UNSUPPORTED;
644                         goto error;
645                     }
646 #undef STRETCH_ROW
647                     }
648                     dbuf += ddesc.u1.lPitch;
649                     last_sy = sy;
650                 }
651             }
652         } else {
653            LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
654            DWORD keylow = 0, keyhigh = 0;
655            if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
656
657               if (dwFlags & DDBLT_KEYSRC) {
658                  keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
659                  keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
660               } else if (dwFlags & DDBLT_KEYDEST){
661                  keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
662                  keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
663               } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
664                  keylow  = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
665                  keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
666               } else {
667                  keylow  = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
668                  keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
669               }
670               dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
671            }
672
673            if (dwFlags & DDBLT_DDFX)  {
674               LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
675               LONG tmpxy;
676               dTopLeft     = dbuf;
677               dTopRight    = dbuf+((dstwidth-1)*bpp);
678               dBottomLeft  = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
679               dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
680
681               if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
682                  /* I don't think we need to do anything about this flag */
683                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
684               }
685               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
686                  tmp          = dTopRight;
687                  dTopRight    = dTopLeft;
688                  dTopLeft     = tmp;
689                  tmp          = dBottomRight;
690                  dBottomRight = dBottomLeft;
691                  dBottomLeft  = tmp;
692                  dstxinc = dstxinc *-1;
693               }
694               if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
695                  tmp          = dTopLeft;
696                  dTopLeft     = dBottomLeft;
697                  dBottomLeft  = tmp;
698                  tmp          = dTopRight;
699                  dTopRight    = dBottomRight;
700                  dBottomRight = tmp;
701                  dstyinc = dstyinc *-1;
702               }
703               if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
704                  /* I don't think we need to do anything about this flag */
705                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
706               }
707               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
708                  tmp          = dBottomRight;
709                  dBottomRight = dTopLeft;
710                  dTopLeft     = tmp;
711                  tmp          = dBottomLeft;
712                  dBottomLeft  = dTopRight;
713                  dTopRight    = tmp;
714                  dstxinc = dstxinc * -1;
715                  dstyinc = dstyinc * -1;
716               }
717               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
718                  tmp          = dTopLeft;
719                  dTopLeft     = dBottomLeft;
720                  dBottomLeft  = dBottomRight;
721                  dBottomRight = dTopRight;
722                  dTopRight    = tmp;
723                  tmpxy   = dstxinc;
724                  dstxinc = dstyinc;
725                  dstyinc = tmpxy;
726                  dstxinc = dstxinc * -1;
727               }
728               if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
729                  tmp          = dTopLeft;
730                  dTopLeft     = dTopRight;
731                  dTopRight    = dBottomRight;
732                  dBottomRight = dBottomLeft;
733                  dBottomLeft  = tmp;
734                  tmpxy   = dstxinc;
735                  dstxinc = dstyinc;
736                  dstyinc = tmpxy;
737                  dstyinc = dstyinc * -1;
738               }
739               if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
740                  /* I don't think we need to do anything about this flag */
741                  WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
742               }
743               dbuf = dTopLeft;
744               dwFlags &= ~(DDBLT_DDFX);
745            }
746
747 #define COPY_COLORKEY_FX(type) { \
748             type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
749             for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
750                (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
751                (LPBYTE)dx = d; \
752                for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
753                   tmp = s[sx >> 16]; \
754                   if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
755                   (LPBYTE)dx += dstxinc; \
756                   } \
757                (LPBYTE)d += dstyinc; \
758             } \
759             break; }
760
761             switch (bpp) {
762             case 1: COPY_COLORKEY_FX(BYTE)
763             case 2: COPY_COLORKEY_FX(WORD)
764             case 4: COPY_COLORKEY_FX(DWORD)
765             case 3: {LPBYTE s,d = dbuf, dx;
766                 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
767                     sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
768                     dx = d;
769                     for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
770                         DWORD pixel;
771                         s = sbuf+3*(sx>>16);
772                         pixel = s[0]|(s[1]<<8)|(s[2]<<16);
773                         if (pixel < keylow || pixel > keyhigh){
774                             dx[0] = (pixel    )&0xff;
775                             dx[1] = (pixel>> 8)&0xff;
776                             dx[2] = (pixel>>16)&0xff;
777                         }
778                         dx+= dstxinc;
779                     }
780                     d += dstyinc;
781                 }
782                 break;}
783             default:
784                FIXME("%s color-keyed blit not implemented for bpp %d!\n",
785                   (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
786                   ret = DDERR_UNSUPPORTED;
787                   goto error;
788 #undef COPY_COLORKEY_FX
789             }
790         }
791     }
792
793 error:
794     if (dwFlags && FIXME_ON(ddraw)) {
795         FIXME("\tUnsupported flags: ");
796         DDRAW_dump_DDBLT(dwFlags);
797     }
798
799 release:
800     IDirectDrawSurface7_Unlock(iface,NULL);
801     if (src) IDirectDrawSurface7_Unlock(src,NULL);
802     return DD_OK;
803 }
804
805 /* BltBatch: generic, unimplemented */
806
807 HRESULT WINAPI
808 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
809                               DWORD dsty, LPDIRECTDRAWSURFACE7 src,
810                               LPRECT rsrc, DWORD trans)
811 {
812     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
813     int                 bpp, w, h, x, y;
814     DDSURFACEDESC2      ddesc,sdesc;
815     HRESULT             ret = DD_OK;
816     LPBYTE              sbuf, dbuf;
817     RECT                rsrc2;
818
819
820     if (TRACE_ON(ddraw)) {
821         FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
822                 This,dstx,dsty,src,rsrc,trans
823         );
824         FIXME("\ttrans:");
825         if (FIXME_ON(ddraw))
826           DDRAW_dump_DDBLTFAST(trans);
827         if (rsrc)
828           FIXME("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
829         else
830           FIXME(" srcrect: NULL\n");
831     }
832
833     /* First, check if the possible override function handles this case */
834     if (This->aux_bltfast != NULL) {
835         if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
836     }
837
838     /* We need to lock the surfaces, or we won't get refreshes when done. */
839     sdesc.dwSize = sizeof(sdesc);
840     IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
841     ddesc.dwSize = sizeof(ddesc);
842     IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
843
844    if (!rsrc) {
845            WARN("rsrc is NULL!\n");
846            rsrc = &rsrc2;
847            rsrc->left = rsrc->top = 0;
848            rsrc->right = sdesc.dwWidth;
849            rsrc->bottom = sdesc.dwHeight;
850    }
851
852     bpp = GET_BPP(This->surface_desc);
853     sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
854     dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
855
856
857     h=rsrc->bottom-rsrc->top;
858     if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
859     if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
860     if (h<0) h=0;
861
862     w=rsrc->right-rsrc->left;
863     if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
864     if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
865     if (w<0) w=0;
866
867     if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
868         DWORD keylow, keyhigh;
869         if (trans & DDBLTFAST_SRCCOLORKEY) {
870             keylow  = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
871             keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
872         } else {
873             /* I'm not sure if this is correct */
874             FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
875             keylow  = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
876             keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
877         }
878
879 #define COPYBOX_COLORKEY(type) { \
880     type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
881     s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
882     d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
883     for (y = 0; y < h; y++) { \
884         for (x = 0; x < w; x++) { \
885             tmp = s[x]; \
886             if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
887         } \
888         (LPBYTE)s += sdesc.u1.lPitch; \
889         (LPBYTE)d += ddesc.u1.lPitch; \
890     } \
891     break; \
892 }
893
894         switch (bpp) {
895         case 1: COPYBOX_COLORKEY(BYTE)
896         case 2: COPYBOX_COLORKEY(WORD)
897         case 4: COPYBOX_COLORKEY(DWORD)
898         default:
899             FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
900             ret = DDERR_UNSUPPORTED;
901             goto error;
902         }
903 #undef COPYBOX_COLORKEY
904     } else {
905         int width = w * bpp;
906
907         for (y = 0; y < h; y++) {
908             memcpy(dbuf, sbuf, width);
909             sbuf += sdesc.u1.lPitch;
910             dbuf += ddesc.u1.lPitch;
911         }
912     }
913 error:
914     IDirectDrawSurface7_Unlock(iface, NULL);
915     IDirectDrawSurface7_Unlock(src, NULL);
916     return ret;
917 }
918
919 /* ChangeUniquenessValue: generic */
920 /* DeleteAttachedSurface: generic */
921 /* EnumAttachedSurfaces: generic */
922 /* EnumOverlayZOrders: generic, unimplemented */
923
924 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
925                                      IDirectDrawSurfaceImpl* back,
926                                      DWORD dwFlags)
927 {
928     DIB_DirectDrawSurfaceImpl* front_priv = front->private;
929     DIB_DirectDrawSurfaceImpl* back_priv = back->private;
930
931     TRACE("(%p,%p)\n",front,back);
932
933     {
934         HBITMAP tmp;
935         tmp = front_priv->dib.DIBsection;
936         front_priv->dib.DIBsection = back_priv->dib.DIBsection;
937         back_priv->dib.DIBsection = tmp;
938     }
939
940     {
941         void* tmp;
942         tmp = front_priv->dib.bitmap_data;
943         front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
944         back_priv->dib.bitmap_data = tmp;
945
946         tmp = front->surface_desc.lpSurface;
947         front->surface_desc.lpSurface = back->surface_desc.lpSurface;
948         back->surface_desc.lpSurface = tmp;
949     }
950
951     /* client_memory should not be different, but just in case */
952     {
953         BOOL tmp;
954         tmp = front_priv->dib.client_memory;
955         front_priv->dib.client_memory = back_priv->dib.client_memory;
956         back_priv->dib.client_memory = tmp;
957     }
958
959     return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
960 }
961
962 /* Flip: generic */
963 /* FreePrivateData: generic */
964 /* GetAttachedSurface: generic */
965 /* GetBltStatus: generic */
966 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
967 /* GetClipper: generic */
968 /* GetColorKey: generic */
969
970 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
971 {
972     DIB_PRIV_VAR(priv, This);
973     HDC hDC;
974
975     TRACE("Grabbing a DC for surface: %p\n", This);
976
977     hDC = CreateCompatibleDC(0);
978     priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
979     if (This->palette)
980         SelectPalette(hDC, This->palette->hpal, FALSE);
981
982     *phDC = hDC;
983
984     return S_OK;
985 }
986
987 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
988 {
989     DIB_PRIV_VAR(priv, This);
990
991     TRACE("Releasing DC for surface: %p\n", This);
992
993     SelectObject(hDC, priv->dib.holdbitmap);
994     DeleteDC(hDC);
995
996     return S_OK;
997 }
998
999 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1000 {
1001     return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1002 }
1003
1004 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1005 {
1006     return DIB_DirectDrawSurface_free_dc(This, hDC);
1007 }
1008
1009 /* GetDDInterface: generic */
1010 /* GetFlipStatus: generic */
1011 /* GetLOD: generic */
1012 /* GetOverlayPosition: generic */
1013 /* GetPalette: generic */
1014 /* GetPixelFormat: generic */
1015 /* GetPriority: generic */
1016 /* GetPrivateData: generic */
1017 /* GetSurfaceDesc: generic */
1018 /* GetUniquenessValue: generic */
1019 /* Initialize: generic */
1020 /* IsLost: generic */
1021 /* Lock: generic with callback? */
1022 /* PageLock: generic */
1023 /* PageUnlock: generic */
1024
1025 HRESULT WINAPI
1026 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1027 {
1028     TRACE("(%p)\n",iface);
1029     return DD_OK;       /* ??? */
1030 }
1031
1032 /* SetClipper: generic */
1033 /* SetColorKey: generic */
1034 /* SetLOD: generic */
1035 /* SetOverlayPosition: generic */
1036
1037 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1038                                        IDirectDrawPaletteImpl* pal)
1039 {
1040     if (!pal) return;
1041     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1042         This->update_palette(This, pal,
1043                              0, pal->palNumEntries,
1044                              pal->palents);
1045 }
1046
1047 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1048                                           IDirectDrawPaletteImpl* pal,
1049                                           DWORD dwStart, DWORD dwCount,
1050                                           LPPALETTEENTRY palent)
1051 {
1052     RGBQUAD col[256];
1053     int n;
1054     HDC dc;
1055
1056     TRACE("updating primary palette\n");
1057     for (n=0; n<dwCount; n++) {
1058       col[n].rgbRed   = palent[n].peRed;
1059       col[n].rgbGreen = palent[n].peGreen;
1060       col[n].rgbBlue  = palent[n].peBlue;
1061       col[n].rgbReserved = 0;
1062     }
1063     This->get_dc(This, &dc);
1064     SetDIBColorTable(dc, dwStart, dwCount, col);
1065     This->release_dc(This, dc);
1066
1067     /* Propagate change to backbuffers if there are any */
1068     /* Basically this is a modification of the Flip code to find the backbuffer */
1069     /* and duplicate the palette update there as well */
1070     if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1071         == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1072     {
1073         static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1074         LPDIRECTDRAWSURFACE7 tgt;
1075
1076         HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1077                                                             &back_caps, &tgt);
1078         if (!FAILED(hr))
1079         {
1080             IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1081                                                          IDirectDrawSurface7,tgt);
1082             IDirectDrawSurface7_Release(tgt);
1083             target->get_dc(target, &dc);
1084             SetDIBColorTable(dc, dwStart, dwCount, col);
1085             target->release_dc(target, dc);
1086         }
1087     }
1088 }
1089
1090 /* SetPalette: generic */
1091 /* SetPriority: generic */
1092 /* SetPrivateData: generic */
1093
1094 HRESULT WINAPI
1095 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1096                                      LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1097 {
1098     ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1099     DIB_PRIV_VAR(priv, This);
1100     HRESULT hr = DD_OK;
1101     DWORD flags = pDDSD->dwFlags;
1102
1103     if (TRACE_ON(ddraw)) {
1104         TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1105         DDRAW_dump_surface_desc(pDDSD);
1106     }
1107
1108     if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1109         flags &= ~DDSD_PIXELFORMAT;
1110         if (flags & DDSD_LPSURFACE) {
1111             This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1112         } else {
1113             FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1114         }
1115     }
1116     if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1117         HBITMAP oldbmp = priv->dib.DIBsection;
1118         LPVOID oldsurf = This->surface_desc.lpSurface;
1119         BOOL oldc = priv->dib.client_memory;
1120
1121         flags &= ~DDSD_LPSURFACE;
1122
1123         TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1124         This->surface_desc.lpSurface = pDDSD->lpSurface;
1125         priv->dib.client_memory = TRUE;
1126
1127         hr = create_dib(This);
1128         if (FAILED(hr))
1129         {
1130             priv->dib.DIBsection = oldbmp;
1131             This->surface_desc.lpSurface = oldsurf;
1132             priv->dib.client_memory = oldc;
1133             return hr;
1134         }
1135
1136         DeleteObject(oldbmp);
1137
1138         if (!oldc)
1139             VirtualFree(oldsurf, 0, MEM_RELEASE);
1140     }
1141     if (flags) {
1142         WARN("Unhandled flags : %08lx\n", flags);
1143     }
1144     return hr;
1145 }
1146
1147 /* Unlock: ???, need callback */
1148 /* UpdateOverlay: generic */
1149 /* UpdateOverlayDisplay: generic */
1150 /* UpdateOverlayZOrder: generic */
1151
1152 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1153 {
1154     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1155     Main_DirectDrawSurface_QueryInterface,
1156     Main_DirectDrawSurface_AddRef,
1157     Main_DirectDrawSurface_Release,
1158     Main_DirectDrawSurface_AddAttachedSurface,
1159     Main_DirectDrawSurface_AddOverlayDirtyRect,
1160     DIB_DirectDrawSurface_Blt,
1161     Main_DirectDrawSurface_BltBatch,
1162     DIB_DirectDrawSurface_BltFast,
1163     Main_DirectDrawSurface_DeleteAttachedSurface,
1164     Main_DirectDrawSurface_EnumAttachedSurfaces,
1165     Main_DirectDrawSurface_EnumOverlayZOrders,
1166     Main_DirectDrawSurface_Flip,
1167     Main_DirectDrawSurface_GetAttachedSurface,
1168     Main_DirectDrawSurface_GetBltStatus,
1169     Main_DirectDrawSurface_GetCaps,
1170     Main_DirectDrawSurface_GetClipper,
1171     Main_DirectDrawSurface_GetColorKey,
1172     Main_DirectDrawSurface_GetDC,
1173     Main_DirectDrawSurface_GetFlipStatus,
1174     Main_DirectDrawSurface_GetOverlayPosition,
1175     Main_DirectDrawSurface_GetPalette,
1176     Main_DirectDrawSurface_GetPixelFormat,
1177     Main_DirectDrawSurface_GetSurfaceDesc,
1178     Main_DirectDrawSurface_Initialize,
1179     Main_DirectDrawSurface_IsLost,
1180     Main_DirectDrawSurface_Lock,
1181     Main_DirectDrawSurface_ReleaseDC,
1182     DIB_DirectDrawSurface_Restore,
1183     Main_DirectDrawSurface_SetClipper,
1184     Main_DirectDrawSurface_SetColorKey,
1185     Main_DirectDrawSurface_SetOverlayPosition,
1186     Main_DirectDrawSurface_SetPalette,
1187     Main_DirectDrawSurface_Unlock,
1188     Main_DirectDrawSurface_UpdateOverlay,
1189     Main_DirectDrawSurface_UpdateOverlayDisplay,
1190     Main_DirectDrawSurface_UpdateOverlayZOrder,
1191     Main_DirectDrawSurface_GetDDInterface,
1192     Main_DirectDrawSurface_PageLock,
1193     Main_DirectDrawSurface_PageUnlock,
1194     DIB_DirectDrawSurface_SetSurfaceDesc,
1195     Main_DirectDrawSurface_SetPrivateData,
1196     Main_DirectDrawSurface_GetPrivateData,
1197     Main_DirectDrawSurface_FreePrivateData,
1198     Main_DirectDrawSurface_GetUniquenessValue,
1199     Main_DirectDrawSurface_ChangeUniquenessValue,
1200     Main_DirectDrawSurface_SetPriority,
1201     Main_DirectDrawSurface_GetPriority,
1202     Main_DirectDrawSurface_SetLOD,
1203     Main_DirectDrawSurface_GetLOD
1204 };