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