Add DDBLT_DONOTWAIT flag.
[wine/wine-kai.git] / dlls / ddraw / surface_dib.c
blobe5c50774086cc39e307bc1d67dff7abaae9955b0
1 /* DIBSection DirectDrawSurface driver
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 1998-2000 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "winerror.h"
33 #include "wine/debug.h"
34 #include "ddraw_private.h"
35 #include "d3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 /* FIXME */
40 extern HBITMAP DIB_CreateDIBSection( HDC hdc, const BITMAPINFO *bmi, UINT usage, VOID **bits,
41 HANDLE section, DWORD offset, DWORD ovr_pitch );
43 static const IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable;
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 )
48 int words;
50 switch(depth)
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;
63 return 4 * words;
67 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
69 BITMAPINFO* b_info;
70 UINT usage;
71 HDC ddc;
72 DIB_DirectDrawSurfaceImpl* priv = This->private;
74 assert(This->surface_desc.lpSurface != NULL);
76 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
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;
85 case 24:
86 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
87 sizeof(BITMAPINFOHEADER));
88 break;
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;
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;
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;
111 b_info->bmiHeader.biSizeImage
112 = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
113 * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
115 b_info->bmiHeader.biXPelsPerMeter = 0;
116 b_info->bmiHeader.biYPelsPerMeter = 0;
117 b_info->bmiHeader.biClrUsed = 0;
118 b_info->bmiHeader.biClrImportant = 0;
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;
127 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
129 case 16:
130 case 32:
132 DWORD *masks = (DWORD *) &(b_info->bmiColors);
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;
139 break;
141 case 24:
142 /* Nothing to do */
143 usage = DIB_RGB_COLORS;
144 break;
146 default:
147 /* Don't know palette */
148 usage = 0;
149 break;
152 ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
153 if (ddc == 0)
155 HeapFree(GetProcessHeap(), 0, b_info);
156 return HRESULT_FROM_WIN32(GetLastError());
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());
170 TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
172 if (!This->surface_desc.lpSurface) {
173 This->surface_desc.lpSurface = priv->dib.bitmap_data;
174 This->surface_desc.dwFlags |= DDSD_LPSURFACE;
177 HeapFree(GetProcessHeap(), 0, b_info);
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");
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;
186 return S_OK;
189 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
191 DIB_DirectDrawSurfaceImpl* priv = This->private;
193 DeleteObject(priv->dib.DIBsection);
195 if (!priv->dib.client_memory)
196 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
198 Main_DirectDrawSurface_final_release(This);
201 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
202 LPDIRECTDRAWSURFACE7* ppDup)
204 return DIB_DirectDrawSurface_Create(This->ddraw_owner,
205 &This->surface_desc, ppDup, NULL);
208 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
209 IDirectDrawImpl *pDD,
210 const DDSURFACEDESC2 *pDDSD)
212 HRESULT hr;
213 DIB_DirectDrawSurfaceImpl* priv = This->private;
215 TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
216 hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
217 if (FAILED(hr)) return hr;
219 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
220 DIB_IDirectDrawSurface7_VTable);
222 This->final_release = DIB_DirectDrawSurface_final_release;
223 This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
224 This->flip_data = DIB_DirectDrawSurface_flip_data;
226 This->get_dc = DIB_DirectDrawSurface_get_dc;
227 This->release_dc = DIB_DirectDrawSurface_release_dc;
228 This->hDC = NULL;
230 This->set_palette = DIB_DirectDrawSurface_set_palette;
231 This->update_palette = DIB_DirectDrawSurface_update_palette;
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? */
238 if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
240 /* "Client memory": it is managed by the application. */
241 /* XXX What if lPitch is not set? Use dwWidth or fail? */
243 priv->dib.client_memory = TRUE;
245 else
247 if (!(This->surface_desc.dwFlags & DDSD_PITCH))
249 int pitch = This->surface_desc.u1.lPitch;
250 if (pitch % 8 != 0)
251 pitch += 8 - (pitch % 8);
253 /* XXX else: how should lPitch be verified? */
255 This->surface_desc.dwFlags |= DDSD_LPSURFACE;
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;
271 if (This->surface_desc.lpSurface == NULL)
273 Main_DirectDrawSurface_final_release(This);
274 return HRESULT_FROM_WIN32(GetLastError());
277 priv->dib.client_memory = FALSE;
280 hr = create_dib(This);
281 if (FAILED(hr))
283 if (!priv->dib.client_memory)
284 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
286 Main_DirectDrawSurface_final_release(This);
288 return hr;
291 return DD_OK;
294 /* Not an API */
295 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
296 const DDSURFACEDESC2 *pDDSD,
297 LPDIRECTDRAWSURFACE7 *ppSurf,
298 IUnknown *pUnkOuter)
300 IDirectDrawSurfaceImpl* This;
301 HRESULT hr;
302 assert(pUnkOuter == NULL);
304 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
305 sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
306 if (This == NULL) return E_OUTOFMEMORY;
308 This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
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);
316 return hr;
320 /* AddAttachedSurface: generic */
321 /* AddOverlayDirtyRect: generic, unimplemented */
323 static HRESULT _Blt_ColorFill(
324 LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
326 int x, y;
327 LPBYTE first;
329 /* Do first row */
331 #define COLORFILL_ROW(type) { \
332 type *d = (type *) buf; \
333 for (x = 0; x < width; x++) \
334 d[x] = (type) color; \
335 break; \
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;
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;
354 #undef COLORFILL_ROW
356 /* Now copy first row */
357 first = buf;
358 for (y = 1; y < height; y++) {
359 buf += lPitch;
360 memcpy(buf, first, width * bpp);
362 return DD_OK;
365 static void ComputeShifts(DWORD mask, DWORD* lshift, DWORD* rshift)
367 int pos = 0;
368 int bits = 0;
369 *lshift = 0;
370 *rshift = 0;
372 if (!mask)
373 return;
375 while(!(mask & (1 << pos)))
376 pos++;
378 while(mask & (1 << (pos+bits)))
379 bits++;
381 *lshift = pos;
382 *rshift = 8 - bits;
385 /* This is used to factorize the decompression between the Blt and BltFast code */
386 static void DoDXTCDecompression(const DDSURFACEDESC2 *sdesc, const DDSURFACEDESC2 *ddesc)
388 DWORD rs,rb,rm;
389 DWORD gs,gb,gm;
390 DWORD bs,bb,bm;
391 DWORD as,ab,am;
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;
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;
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;
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;
478 #if 0 /* Useful for debugging */
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);
488 #endif
491 HRESULT WINAPI
492 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
493 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
494 DWORD dwFlags, LPDDBLTFX lpbltfx)
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;
504 TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
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);
517 if ((This->locked) || ((src != NULL) && (((IDirectDrawSurfaceImpl *)src)->locked))) {
518 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
519 return DDERR_SURFACEBUSY;
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;
527 DD_STRUCT_INIT(&ddesc);
528 DD_STRUCT_INIT(&sdesc);
530 sdesc.dwSize = sizeof(sdesc);
531 ddesc.dwSize = sizeof(ddesc);
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);
541 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
542 (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
543 if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
544 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
545 ret = DDERR_INVALIDPIXELFORMAT;
546 goto release;
548 memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
549 goto release;
552 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
553 (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
554 DoDXTCDecompression(&sdesc, &ddesc);
555 goto release;
558 if (rdst) {
559 memcpy(&xdst,rdst,sizeof(xdst));
560 } else {
561 xdst.top = 0;
562 xdst.bottom = ddesc.dwHeight;
563 xdst.left = 0;
564 xdst.right = ddesc.dwWidth;
567 if (rsrc) {
568 memcpy(&xsrc,rsrc,sizeof(xsrc));
569 } else {
570 if (src) {
571 xsrc.top = 0;
572 xsrc.bottom = sdesc.dwHeight;
573 xsrc.left = 0;
574 xsrc.right = sdesc.dwWidth;
575 } else {
576 memset(&xsrc,0,sizeof(xsrc));
580 /* First check for the validity of source / destination rectangles. This was
581 verified using a test application + by MSDN.
583 if ((src != NULL) &&
584 ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
585 (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
586 (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
587 (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
588 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
589 WARN("Application gave us bad source rectangle for Blt.\n");
590 ret = DDERR_INVALIDRECT;
591 goto release;
593 /* For the Destination rect, it can be out of bounds on the condition that a clipper
594 is set for the given surface.
596 if ((This->clipper == NULL) &&
597 ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
598 (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
599 (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
600 (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
601 (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
602 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
603 ret = DDERR_INVALIDRECT;
604 goto release;
607 /* Now handle negative values in the rectangles. Warning: only supported for now
608 in the 'simple' cases (ie not in any stretching / rotation cases).
610 First, the case where nothing is to be done.
612 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
613 ((src != NULL) &&
614 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
616 TRACE("Nothing to be done !\n");
617 goto release;
620 /* The easy case : the source-less blits.... */
621 if (src == NULL) {
622 RECT full_rect;
623 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
625 full_rect.left = 0;
626 full_rect.top = 0;
627 full_rect.right = ddesc.dwWidth;
628 full_rect.bottom = ddesc.dwHeight;
629 IntersectRect(&temp_rect, &full_rect, &xdst);
630 xdst = temp_rect;
631 } else {
632 /* Only handle clipping on the destination rectangle */
633 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth );
634 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight);
635 if (clip_vert || clip_horiz) {
636 /* Now check if this is a special case or not... */
637 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
638 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
639 (dwFlags & DDBLT_DDFX)) {
640 WARN("Out of screen rectangle in special case. Not handled right now.\n");
641 goto release;
644 if (clip_horiz) {
645 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
646 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
648 if (clip_vert) {
649 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
650 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
652 /* And check if after clipping something is still to be done... */
653 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
654 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
655 TRACE("Nothing to be done after clipping !\n");
656 goto release;
661 bpp = GET_BPP(ddesc);
662 srcheight = xsrc.bottom - xsrc.top;
663 srcwidth = xsrc.right - xsrc.left;
664 dstheight = xdst.bottom - xdst.top;
665 dstwidth = xdst.right - xdst.left;
666 width = (xdst.right - xdst.left) * bpp;
668 assert(width <= ddesc.u1.lPitch);
670 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
672 if (dwFlags & DDBLT_WAIT) {
673 static BOOL displayed = FALSE;
674 if (!displayed)
675 FIXME("Can't handle DDBLT_WAIT flag right now.\n");
676 displayed = TRUE;
677 dwFlags &= ~DDBLT_WAIT;
679 if (dwFlags & DDBLT_ASYNC) {
680 static BOOL displayed = FALSE;
681 if (!displayed)
682 FIXME("Can't handle DDBLT_ASYNC flag right now.\n");
683 displayed = TRUE;
684 dwFlags &= ~DDBLT_ASYNC;
686 if (dwFlags & DDBLT_DONOTWAIT) {
687 /* DDBLT_DONOTWAIT appeared in DX7 */
688 static BOOL displayed = FALSE;
689 if (!displayed)
690 FIXME("Can't handle DDBLT_DONOTWAIT flag right now.\n");
691 displayed = TRUE;
692 dwFlags &= ~DDBLT_DONOTWAIT;
695 /* First, all the 'source-less' blits */
696 if (dwFlags & DDBLT_COLORFILL) {
697 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
698 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
699 dwFlags &= ~DDBLT_COLORFILL;
702 if (dwFlags & DDBLT_DEPTHFILL)
703 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
704 if (dwFlags & DDBLT_ROP) {
705 /* Catch some degenerate cases here */
706 switch(lpbltfx->dwROP) {
707 case BLACKNESS:
708 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
709 break;
710 case 0xAA0029: /* No-op */
711 break;
712 case WHITENESS:
713 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
714 break;
715 case SRCCOPY: /* well, we do that below ? */
716 break;
717 default:
718 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
719 goto error;
721 dwFlags &= ~DDBLT_ROP;
723 if (dwFlags & DDBLT_DDROPS) {
724 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
726 /* Now the 'with source' blits */
727 if (src) {
728 LPBYTE sbase;
729 int sx, xinc, sy, yinc;
731 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
732 goto release;
733 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
734 xinc = (srcwidth << 16) / dstwidth;
735 yinc = (srcheight << 16) / dstheight;
737 if (!dwFlags) {
738 /* No effects, we can cheat here */
739 if (dstwidth == srcwidth) {
740 if (dstheight == srcheight) {
741 /* No stretching in either direction. This needs to be as
742 * fast as possible */
743 sbuf = sbase;
745 /* check for overlapping surfaces */
746 if (src != iface || xdst.top < xsrc.top ||
747 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
749 /* no overlap, or dst above src, so copy from top downwards */
750 for (y = 0; y < dstheight; y++)
752 memcpy(dbuf, sbuf, width);
753 sbuf += sdesc.u1.lPitch;
754 dbuf += ddesc.u1.lPitch;
757 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
759 sbuf += (sdesc.u1.lPitch*dstheight);
760 dbuf += (ddesc.u1.lPitch*dstheight);
761 for (y = 0; y < dstheight; y++)
763 sbuf -= sdesc.u1.lPitch;
764 dbuf -= ddesc.u1.lPitch;
765 memcpy(dbuf, sbuf, width);
768 else /* src and dst overlapping on the same line, use memmove */
770 for (y = 0; y < dstheight; y++)
772 memmove(dbuf, sbuf, width);
773 sbuf += sdesc.u1.lPitch;
774 dbuf += ddesc.u1.lPitch;
777 } else {
778 /* Stretching in Y direction only */
779 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
780 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
781 memcpy(dbuf, sbuf, width);
782 dbuf += ddesc.u1.lPitch;
785 } else {
786 /* Stretching in X direction */
787 int last_sy = -1;
788 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
789 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
791 if ((sy >> 16) == (last_sy >> 16)) {
792 /* this sourcerow is the same as last sourcerow -
793 * copy already stretched row
795 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
796 } else {
797 #define STRETCH_ROW(type) { \
798 type *s = (type *) sbuf, *d = (type *) dbuf; \
799 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
800 d[x] = s[sx >> 16]; \
801 break; }
803 switch(bpp) {
804 case 1: STRETCH_ROW(BYTE)
805 case 2: STRETCH_ROW(WORD)
806 case 4: STRETCH_ROW(DWORD)
807 case 3: {
808 LPBYTE s,d = dbuf;
809 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
810 DWORD pixel;
812 s = sbuf+3*(sx>>16);
813 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
814 d[0] = (pixel )&0xff;
815 d[1] = (pixel>> 8)&0xff;
816 d[2] = (pixel>>16)&0xff;
817 d+=3;
819 break;
821 default:
822 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
823 ret = DDERR_UNSUPPORTED;
824 goto error;
826 #undef STRETCH_ROW
828 dbuf += ddesc.u1.lPitch;
829 last_sy = sy;
832 } else {
833 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
834 DWORD keylow = 0, keyhigh = 0, keymask = 0xFFFFFFFF;
835 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
837 if (dwFlags & DDBLT_KEYSRC) {
838 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
839 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
840 } else if (dwFlags & DDBLT_KEYDEST){
841 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
842 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
843 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
844 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
845 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
846 } else {
847 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
848 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
850 keymask = sdesc.u4.ddpfPixelFormat.u2.dwRBitMask | sdesc.u4.ddpfPixelFormat.u3.dwGBitMask | sdesc.u4.ddpfPixelFormat.u4.dwBBitMask;
851 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
854 if (dwFlags & DDBLT_DDFX) {
855 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
856 LONG tmpxy;
857 dTopLeft = dbuf;
858 dTopRight = dbuf+((dstwidth-1)*bpp);
859 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
860 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
862 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
863 /* I don't think we need to do anything about this flag */
864 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
866 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
867 tmp = dTopRight;
868 dTopRight = dTopLeft;
869 dTopLeft = tmp;
870 tmp = dBottomRight;
871 dBottomRight = dBottomLeft;
872 dBottomLeft = tmp;
873 dstxinc = dstxinc *-1;
875 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
876 tmp = dTopLeft;
877 dTopLeft = dBottomLeft;
878 dBottomLeft = tmp;
879 tmp = dTopRight;
880 dTopRight = dBottomRight;
881 dBottomRight = tmp;
882 dstyinc = dstyinc *-1;
884 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
885 /* I don't think we need to do anything about this flag */
886 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
888 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
889 tmp = dBottomRight;
890 dBottomRight = dTopLeft;
891 dTopLeft = tmp;
892 tmp = dBottomLeft;
893 dBottomLeft = dTopRight;
894 dTopRight = tmp;
895 dstxinc = dstxinc * -1;
896 dstyinc = dstyinc * -1;
898 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
899 tmp = dTopLeft;
900 dTopLeft = dBottomLeft;
901 dBottomLeft = dBottomRight;
902 dBottomRight = dTopRight;
903 dTopRight = tmp;
904 tmpxy = dstxinc;
905 dstxinc = dstyinc;
906 dstyinc = tmpxy;
907 dstxinc = dstxinc * -1;
909 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
910 tmp = dTopLeft;
911 dTopLeft = dTopRight;
912 dTopRight = dBottomRight;
913 dBottomRight = dBottomLeft;
914 dBottomLeft = tmp;
915 tmpxy = dstxinc;
916 dstxinc = dstyinc;
917 dstyinc = tmpxy;
918 dstyinc = dstyinc * -1;
920 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
921 /* I don't think we need to do anything about this flag */
922 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
924 dbuf = dTopLeft;
925 dwFlags &= ~(DDBLT_DDFX);
928 #define COPY_COLORKEY_FX(type) { \
929 type *s, *d = (type *) dbuf, *dx, tmp; \
930 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
931 s = (type*)(sbase + (sy >> 16) * sdesc.u1.lPitch); \
932 dx = d; \
933 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
934 tmp = s[sx >> 16]; \
935 if ((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) dx[0] = tmp; \
936 dx = (type*)(((LPBYTE)dx)+dstxinc); \
938 d = (type*)(((LPBYTE)d)+dstyinc); \
940 break; }
942 switch (bpp) {
943 case 1: COPY_COLORKEY_FX(BYTE)
944 case 2: COPY_COLORKEY_FX(WORD)
945 case 4: COPY_COLORKEY_FX(DWORD)
946 case 3: {LPBYTE s,d = dbuf, dx;
947 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
948 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
949 dx = d;
950 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
951 DWORD pixel;
952 s = sbuf+3*(sx>>16);
953 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
954 if ((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) {
955 dx[0] = (pixel )&0xff;
956 dx[1] = (pixel>> 8)&0xff;
957 dx[2] = (pixel>>16)&0xff;
959 dx+= dstxinc;
961 d += dstyinc;
963 break;}
964 default:
965 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
966 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
967 ret = DDERR_UNSUPPORTED;
968 goto error;
969 #undef COPY_COLORKEY_FX
974 error:
975 if (dwFlags && FIXME_ON(ddraw)) {
976 FIXME("\tUnsupported flags: ");
977 DDRAW_dump_DDBLT(dwFlags);
980 release:
981 IDirectDrawSurface7_Unlock(iface,NULL);
982 if (src && src != iface) IDirectDrawSurface7_Unlock(src,NULL);
983 return ret;
986 /* BltBatch: generic, unimplemented */
988 HRESULT WINAPI
989 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
990 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
991 LPRECT rsrc, DWORD trans)
993 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
994 int bpp, w, h, x, y;
995 DDSURFACEDESC2 ddesc,sdesc;
996 HRESULT ret = DD_OK;
997 LPBYTE sbuf, dbuf;
998 RECT rsrc2;
999 RECT lock_src, lock_dst, lock_union;
1001 if (TRACE_ON(ddraw)) {
1002 TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
1003 This,dstx,dsty,src,rsrc,trans
1005 TRACE("\ttrans:");
1006 if (FIXME_ON(ddraw))
1007 DDRAW_dump_DDBLTFAST(trans);
1008 if (rsrc)
1009 TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
1010 else
1011 TRACE(" srcrect: NULL\n");
1014 if ((This->locked) || ((src != NULL) && (((IDirectDrawSurfaceImpl *)src)->locked))) {
1015 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1016 return DDERR_SURFACEBUSY;
1019 /* First, check if the possible override function handles this case */
1020 if (This->aux_bltfast != NULL) {
1021 if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
1024 /* Get the surface description without locking to first compute the width / height */
1025 ddesc = This->surface_desc;
1026 sdesc = (ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src))->surface_desc;
1028 if (!rsrc) {
1029 WARN("rsrc is NULL!\n");
1030 rsrc = &rsrc2;
1031 rsrc->left = rsrc->top = 0;
1032 rsrc->right = sdesc.dwWidth;
1033 rsrc->bottom = sdesc.dwHeight;
1036 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1037 if ((rsrc->bottom > sdesc.dwHeight) || (rsrc->bottom < 0) ||
1038 (rsrc->top > sdesc.dwHeight) || (rsrc->top < 0) ||
1039 (rsrc->left > sdesc.dwWidth) || (rsrc->left < 0) ||
1040 (rsrc->right > sdesc.dwWidth) || (rsrc->right < 0) ||
1041 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top)) {
1042 WARN("Application gave us bad source rectangle for BltFast.\n");
1043 return DDERR_INVALIDRECT;
1046 h=rsrc->bottom-rsrc->top;
1047 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
1048 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
1049 if (h<=0) return DDERR_INVALIDRECT;
1051 w=rsrc->right-rsrc->left;
1052 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
1053 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
1054 if (w<=0) return DDERR_INVALIDRECT;
1056 /* Now compute the locking rectangle... */
1057 lock_src.left = rsrc->left;
1058 lock_src.top = rsrc->top;
1059 lock_src.right = lock_src.left + w;
1060 lock_src.bottom = lock_src.top + h;
1062 lock_dst.left = dstx;
1063 lock_dst.top = dsty;
1064 lock_dst.right = dstx + w;
1065 lock_dst.bottom = dsty + h;
1067 bpp = GET_BPP(This->surface_desc);
1069 /* We need to lock the surfaces, or we won't get refreshes when done. */
1070 if (src == iface) {
1071 int pitch;
1073 UnionRect(&lock_union, &lock_src, &lock_dst);
1075 /* Lock the union of the two rectangles */
1076 IDirectDrawSurface7_Lock(iface, &lock_union, &ddesc, 0, 0);
1078 pitch = This->surface_desc.u1.lPitch;
1080 /* Since sdesc was originally copied from this surface's description, we can just reuse it */
1081 sdesc.lpSurface = (BYTE *)This->surface_desc.lpSurface + lock_src.top * pitch + lock_src.left * bpp;
1082 ddesc.lpSurface = (BYTE *)This->surface_desc.lpSurface + lock_dst.top * pitch + lock_dst.left * bpp;
1083 } else {
1084 sdesc.dwSize = sizeof(sdesc);
1085 IDirectDrawSurface7_Lock(src, &lock_src, &sdesc, DDLOCK_READONLY, 0);
1086 ddesc.dwSize = sizeof(ddesc);
1087 IDirectDrawSurface7_Lock(iface, &lock_dst, &ddesc, DDLOCK_WRITEONLY, 0);
1090 /* Handle first the FOURCC surfaces... */
1091 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) && (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
1092 if (trans)
1093 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1094 if (dstx || dsty)
1095 FIXME("offset for destination surface is not supported\n");
1096 if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
1097 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1098 ret = DDERR_INVALIDPIXELFORMAT;
1099 goto error;
1101 memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
1102 goto error;
1104 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
1105 (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
1106 DoDXTCDecompression(&sdesc, &ddesc);
1107 goto error;
1110 sbuf = (BYTE *) sdesc.lpSurface;
1111 dbuf = (BYTE *) ddesc.lpSurface;
1113 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
1114 DWORD keylow, keyhigh;
1115 if (trans & DDBLTFAST_SRCCOLORKEY) {
1116 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
1117 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
1118 } else {
1119 /* I'm not sure if this is correct */
1120 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1121 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
1122 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
1125 #define COPYBOX_COLORKEY(type) { \
1126 type *d, *s, tmp; \
1127 s = (type *) sdesc.lpSurface; \
1128 d = (type *) ddesc.lpSurface; \
1129 for (y = 0; y < h; y++) { \
1130 for (x = 0; x < w; x++) { \
1131 tmp = s[x]; \
1132 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1134 s = (type *)((BYTE *)s + sdesc.u1.lPitch); \
1135 d = (type *)((BYTE *)d + ddesc.u1.lPitch); \
1137 break; \
1140 switch (bpp) {
1141 case 1: COPYBOX_COLORKEY(BYTE)
1142 case 2: COPYBOX_COLORKEY(WORD)
1143 case 4: COPYBOX_COLORKEY(DWORD)
1144 case 3:
1146 BYTE *d, *s;
1147 DWORD tmp;
1148 s = (BYTE *) sdesc.lpSurface;
1149 d = (BYTE *) ddesc.lpSurface;
1150 for (y = 0; y < h; y++) {
1151 for (x = 0; x < w * 3; x += 3) {
1152 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1153 if (tmp < keylow || tmp > keyhigh) {
1154 d[x + 0] = s[x + 0];
1155 d[x + 1] = s[x + 1];
1156 d[x + 2] = s[x + 2];
1159 s += sdesc.u1.lPitch;
1160 d += ddesc.u1.lPitch;
1162 break;
1164 default:
1165 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1166 ret = DDERR_UNSUPPORTED;
1167 goto error;
1169 #undef COPYBOX_COLORKEY
1170 } else {
1171 int width = w * bpp;
1173 for (y = 0; y < h; y++) {
1174 memcpy(dbuf, sbuf, width);
1175 sbuf += sdesc.u1.lPitch;
1176 dbuf += ddesc.u1.lPitch;
1180 error:
1181 if (src == iface) {
1182 IDirectDrawSurface7_Unlock(iface, &lock_union);
1183 } else {
1184 IDirectDrawSurface7_Unlock(iface, &lock_dst);
1185 IDirectDrawSurface7_Unlock(src, &lock_src);
1188 return ret;
1191 /* ChangeUniquenessValue: generic */
1192 /* DeleteAttachedSurface: generic */
1193 /* EnumAttachedSurfaces: generic */
1194 /* EnumOverlayZOrders: generic, unimplemented */
1196 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
1197 IDirectDrawSurfaceImpl* back,
1198 DWORD dwFlags)
1200 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
1201 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
1203 TRACE("(%p,%p)\n",front,back);
1206 HBITMAP tmp;
1207 tmp = front_priv->dib.DIBsection;
1208 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
1209 back_priv->dib.DIBsection = tmp;
1213 void* tmp;
1214 tmp = front_priv->dib.bitmap_data;
1215 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
1216 back_priv->dib.bitmap_data = tmp;
1218 tmp = front->surface_desc.lpSurface;
1219 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
1220 back->surface_desc.lpSurface = tmp;
1223 /* client_memory should not be different, but just in case */
1225 BOOL tmp;
1226 tmp = front_priv->dib.client_memory;
1227 front_priv->dib.client_memory = back_priv->dib.client_memory;
1228 back_priv->dib.client_memory = tmp;
1231 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
1234 /* Flip: generic */
1235 /* FreePrivateData: generic */
1236 /* GetAttachedSurface: generic */
1237 /* GetBltStatus: generic */
1238 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
1239 /* GetClipper: generic */
1240 /* GetColorKey: generic */
1242 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1244 DIB_PRIV_VAR(priv, This);
1245 HDC hDC;
1247 TRACE("Grabbing a DC for surface: %p\n", This);
1249 hDC = CreateCompatibleDC(0);
1250 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
1251 if (This->palette)
1252 SelectPalette(hDC, This->palette->hpal, FALSE);
1254 *phDC = hDC;
1256 return S_OK;
1259 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1261 DIB_PRIV_VAR(priv, This);
1263 TRACE("Releasing DC for surface: %p\n", This);
1265 SelectObject(hDC, priv->dib.holdbitmap);
1266 DeleteDC(hDC);
1268 return S_OK;
1271 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1273 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1276 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1278 return DIB_DirectDrawSurface_free_dc(This, hDC);
1281 /* GetDDInterface: generic */
1282 /* GetFlipStatus: generic */
1283 /* GetLOD: generic */
1284 /* GetOverlayPosition: generic */
1285 /* GetPalette: generic */
1286 /* GetPixelFormat: generic */
1287 /* GetPriority: generic */
1288 /* GetPrivateData: generic */
1289 /* GetSurfaceDesc: generic */
1290 /* GetUniquenessValue: generic */
1291 /* Initialize: generic */
1292 /* IsLost: generic */
1293 /* Lock: generic with callback? */
1294 /* PageLock: generic */
1295 /* PageUnlock: generic */
1297 HRESULT WINAPI
1298 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1300 TRACE("(%p)\n",iface);
1301 return DD_OK; /* ??? */
1304 /* SetClipper: generic */
1305 /* SetColorKey: generic */
1306 /* SetLOD: generic */
1307 /* SetOverlayPosition: generic */
1309 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1310 IDirectDrawPaletteImpl* pal)
1312 if (!pal) return;
1313 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1314 This->update_palette(This, pal,
1315 0, pal->palNumEntries,
1316 pal->palents);
1319 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1320 IDirectDrawPaletteImpl* pal,
1321 DWORD dwStart, DWORD dwCount,
1322 LPPALETTEENTRY palent)
1324 RGBQUAD col[256];
1325 unsigned int n;
1326 HDC dc;
1328 TRACE("updating primary palette\n");
1329 for (n=0; n<dwCount; n++) {
1330 col[n].rgbRed = palent[n].peRed;
1331 col[n].rgbGreen = palent[n].peGreen;
1332 col[n].rgbBlue = palent[n].peBlue;
1333 col[n].rgbReserved = 0;
1335 This->get_dc(This, &dc);
1336 SetDIBColorTable(dc, dwStart, dwCount, col);
1337 This->release_dc(This, dc);
1339 /* Propagate change to backbuffers if there are any */
1340 /* Basically this is a modification of the Flip code to find the backbuffer */
1341 /* and duplicate the palette update there as well */
1342 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1343 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1345 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1346 LPDIRECTDRAWSURFACE7 tgt;
1348 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1349 &back_caps, &tgt);
1350 if (!FAILED(hr))
1352 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1353 IDirectDrawSurface7,tgt);
1354 IDirectDrawSurface7_Release(tgt);
1355 target->get_dc(target, &dc);
1356 SetDIBColorTable(dc, dwStart, dwCount, col);
1357 target->release_dc(target, dc);
1362 /* SetPalette: generic */
1363 /* SetPriority: generic */
1364 /* SetPrivateData: generic */
1366 HRESULT WINAPI
1367 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1368 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1370 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1371 DIB_PRIV_VAR(priv, This);
1372 HRESULT hr = DD_OK;
1373 DWORD flags = pDDSD->dwFlags;
1375 if (TRACE_ON(ddraw)) {
1376 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1377 DDRAW_dump_surface_desc(pDDSD);
1380 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1381 flags &= ~DDSD_PIXELFORMAT;
1382 if (flags & DDSD_LPSURFACE) {
1383 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1384 } else {
1385 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1388 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1389 HBITMAP oldbmp = priv->dib.DIBsection;
1390 LPVOID oldsurf = This->surface_desc.lpSurface;
1391 BOOL oldc = priv->dib.client_memory;
1393 flags &= ~DDSD_LPSURFACE;
1395 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1396 This->surface_desc.lpSurface = pDDSD->lpSurface;
1397 priv->dib.client_memory = TRUE;
1399 hr = create_dib(This);
1400 if (FAILED(hr))
1402 priv->dib.DIBsection = oldbmp;
1403 This->surface_desc.lpSurface = oldsurf;
1404 priv->dib.client_memory = oldc;
1405 return hr;
1408 DeleteObject(oldbmp);
1410 if (!oldc)
1411 VirtualFree(oldsurf, 0, MEM_RELEASE);
1413 if (flags) {
1414 WARN("Unhandled flags : %08lx\n", flags);
1416 return hr;
1419 /* Unlock: ???, need callback */
1420 /* UpdateOverlay: generic */
1421 /* UpdateOverlayDisplay: generic */
1422 /* UpdateOverlayZOrder: generic */
1424 static const IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable =
1426 Main_DirectDrawSurface_QueryInterface,
1427 Main_DirectDrawSurface_AddRef,
1428 Main_DirectDrawSurface_Release,
1429 Main_DirectDrawSurface_AddAttachedSurface,
1430 Main_DirectDrawSurface_AddOverlayDirtyRect,
1431 DIB_DirectDrawSurface_Blt,
1432 Main_DirectDrawSurface_BltBatch,
1433 DIB_DirectDrawSurface_BltFast,
1434 Main_DirectDrawSurface_DeleteAttachedSurface,
1435 Main_DirectDrawSurface_EnumAttachedSurfaces,
1436 Main_DirectDrawSurface_EnumOverlayZOrders,
1437 Main_DirectDrawSurface_Flip,
1438 Main_DirectDrawSurface_GetAttachedSurface,
1439 Main_DirectDrawSurface_GetBltStatus,
1440 Main_DirectDrawSurface_GetCaps,
1441 Main_DirectDrawSurface_GetClipper,
1442 Main_DirectDrawSurface_GetColorKey,
1443 Main_DirectDrawSurface_GetDC,
1444 Main_DirectDrawSurface_GetFlipStatus,
1445 Main_DirectDrawSurface_GetOverlayPosition,
1446 Main_DirectDrawSurface_GetPalette,
1447 Main_DirectDrawSurface_GetPixelFormat,
1448 Main_DirectDrawSurface_GetSurfaceDesc,
1449 Main_DirectDrawSurface_Initialize,
1450 Main_DirectDrawSurface_IsLost,
1451 Main_DirectDrawSurface_Lock,
1452 Main_DirectDrawSurface_ReleaseDC,
1453 DIB_DirectDrawSurface_Restore,
1454 Main_DirectDrawSurface_SetClipper,
1455 Main_DirectDrawSurface_SetColorKey,
1456 Main_DirectDrawSurface_SetOverlayPosition,
1457 Main_DirectDrawSurface_SetPalette,
1458 Main_DirectDrawSurface_Unlock,
1459 Main_DirectDrawSurface_UpdateOverlay,
1460 Main_DirectDrawSurface_UpdateOverlayDisplay,
1461 Main_DirectDrawSurface_UpdateOverlayZOrder,
1462 Main_DirectDrawSurface_GetDDInterface,
1463 Main_DirectDrawSurface_PageLock,
1464 Main_DirectDrawSurface_PageUnlock,
1465 DIB_DirectDrawSurface_SetSurfaceDesc,
1466 Main_DirectDrawSurface_SetPrivateData,
1467 Main_DirectDrawSurface_GetPrivateData,
1468 Main_DirectDrawSurface_FreePrivateData,
1469 Main_DirectDrawSurface_GetUniquenessValue,
1470 Main_DirectDrawSurface_ChangeUniquenessValue,
1471 Main_DirectDrawSurface_SetPriority,
1472 Main_DirectDrawSurface_GetPriority,
1473 Main_DirectDrawSurface_SetLOD,
1474 Main_DirectDrawSurface_GetLOD