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