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