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