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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "wine/debug.h"
32 #include "ddraw_private.h"
33 #include "dsurface/main.h"
34 #include "dsurface/dib.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 extern HBITMAP DIB_CreateDIBSection( HDC hdc, BITMAPINFO *bmi, UINT usage, LPVOID *bits,
40 HANDLE section, DWORD offset, DWORD ovr_pitch );
42 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable;
44 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
45 inline static int get_dib_width_bytes( int width, int depth )
51 case 1: words = (width + 31) / 32; break;
52 case 4: words = (width + 7) / 8; break;
53 case 8: words = (width + 3) / 4; break;
55 case 16: words = (width + 1) / 2; break;
56 case 24: words = (width * 3 + 3)/4; break;
58 WARN("(%d): Unsupported depth\n", depth );
60 case 32: words = width; break;
66 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
71 DIB_DirectDrawSurfaceImpl* priv = This->private;
73 assert(This->surface_desc.lpSurface != NULL);
75 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
79 /* Allocate extra space to store the RGB bit masks. */
80 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
81 sizeof(BITMAPINFOHEADER)
86 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
87 sizeof(BITMAPINFOHEADER));
91 /* Allocate extra space for a palette. */
92 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
93 sizeof(BITMAPINFOHEADER)
95 * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
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;
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)
132 DWORD *masks = (DWORD *) &(b_info->bmiColors);
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;
143 usage = DIB_RGB_COLORS;
147 /* Don't know palette */
152 ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
155 HeapFree(GetProcessHeap(), 0, b_info);
156 return HRESULT_FROM_WIN32(GetLastError());
160 = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
161 (DWORD)This->surface_desc.lpSurface,
162 This->surface_desc.u1.lPitch);
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;
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)
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;
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;
247 if (!(This->surface_desc.dwFlags & DDSD_PITCH))
249 int pitch = This->surface_desc.u1.lPitch;
251 pitch += 8 - (pitch % 8);
253 /* XXX else: how should lPitch be verified? */
255 This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
257 This->surface_desc.lpSurface
258 = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
259 * This->surface_desc.dwHeight + 4, /* The + 4 here is for dumb games reading after the end of the surface
260 when reading the last byte / half using word access */
261 MEM_COMMIT, PAGE_READWRITE);
263 if (This->surface_desc.lpSurface == NULL)
265 Main_DirectDrawSurface_final_release(This);
266 return HRESULT_FROM_WIN32(GetLastError());
269 priv->dib.client_memory = FALSE;
272 hr = create_dib(This);
275 if (!priv->dib.client_memory)
276 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
278 Main_DirectDrawSurface_final_release(This);
287 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
288 const DDSURFACEDESC2 *pDDSD,
289 LPDIRECTDRAWSURFACE7 *ppSurf,
292 IDirectDrawSurfaceImpl* This;
294 assert(pUnkOuter == NULL);
296 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
297 sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
298 if (This == NULL) return E_OUTOFMEMORY;
300 This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
302 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
304 HeapFree(GetProcessHeap(), 0, This);
306 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
312 /* AddAttachedSurface: generic */
313 /* AddOverlayDirtyRect: generic, unimplemented */
315 static HRESULT _Blt_ColorFill(
316 LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
323 #define COLORFILL_ROW(type) { \
324 type *d = (type *) buf; \
325 for (x = 0; x < width; x++) \
326 d[x] = (type) color; \
331 case 1: COLORFILL_ROW(BYTE)
332 case 2: COLORFILL_ROW(WORD)
333 case 3: { BYTE *d = (BYTE *) buf;
334 for (x = 0; x < width; x++,d+=3) {
335 d[0] = (color ) & 0xFF;
336 d[1] = (color>> 8) & 0xFF;
337 d[2] = (color>>16) & 0xFF;
340 case 4: COLORFILL_ROW(DWORD)
342 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
343 return DDERR_UNSUPPORTED;
348 /* Now copy first row */
350 for (y = 1; y < height; y++) {
352 memcpy(buf, first, width * bpp);
358 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
359 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
360 DWORD dwFlags, LPDDBLTFX lpbltfx)
362 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
364 DDSURFACEDESC2 ddesc,sdesc;
366 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
370 TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
372 if (TRACE_ON(ddraw)) {
373 if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
374 if (rsrc) TRACE("\tsrcrect :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
376 DDRAW_dump_DDBLT(dwFlags);
377 if (dwFlags & DDBLT_DDFX) {
379 DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
383 /* First, check if the possible override function handles this case */
384 if (This->aux_blt != NULL) {
385 if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
388 DD_STRUCT_INIT(&ddesc);
389 DD_STRUCT_INIT(&sdesc);
391 sdesc.dwSize = sizeof(sdesc);
392 if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
393 ddesc.dwSize = sizeof(ddesc);
394 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
397 memcpy(&xdst,rdst,sizeof(xdst));
400 xdst.bottom = ddesc.dwHeight;
402 xdst.right = ddesc.dwWidth;
406 memcpy(&xsrc,rsrc,sizeof(xsrc));
410 xsrc.bottom = sdesc.dwHeight;
412 xsrc.right = sdesc.dwWidth;
414 memset(&xsrc,0,sizeof(xsrc));
418 /* First check for the validity of source / destination rectangles. This was
419 verified using a test application + by MSDN.
422 ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
423 (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
424 (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
425 (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
426 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
427 WARN("Application gave us bad source rectangle for Blt.\n");
428 return DDERR_INVALIDRECT;
430 /* For the Destination rect, it can be out of bounds on the condition that a clipper
431 is set for the given surface.
433 if ((This->clipper == NULL) &&
434 ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
435 (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
436 (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
437 (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
438 (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
439 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
440 return DDERR_INVALIDRECT;
443 /* Now handle negative values in the rectangles. Warning: only supported for now
444 in the 'simple' cases (ie not in any stretching / rotation cases).
446 First, the case where nothing is to be done.
448 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
450 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
452 TRACE("Nothing to be done !\n");
456 /* The easy case : the source-less blits.... */
459 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
463 full_rect.right = ddesc.dwWidth;
464 full_rect.bottom = ddesc.dwHeight;
465 IntersectRect(&temp_rect, &full_rect, &xdst);
468 /* Only handle clipping on the destination rectangle */
469 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth );
470 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight);
471 if (clip_vert || clip_horiz) {
472 /* Now check if this is a special case or not... */
473 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
474 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
475 (dwFlags & DDBLT_DDFX)) {
476 WARN("Out of screen rectangle in special case. Not handled right now.\n");
481 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
482 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
485 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
486 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
488 /* And check if after clipping something is still to be done... */
489 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
490 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
491 TRACE("Nothing to be done after clipping !\n");
497 bpp = GET_BPP(ddesc);
498 srcheight = xsrc.bottom - xsrc.top;
499 srcwidth = xsrc.right - xsrc.left;
500 dstheight = xdst.bottom - xdst.top;
501 dstwidth = xdst.right - xdst.left;
502 width = (xdst.right - xdst.left) * bpp;
504 assert(width <= ddesc.u1.lPitch);
506 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
508 if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
510 static BOOL displayed = FALSE;
513 FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
516 dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
519 /* First, all the 'source-less' blits */
520 if (dwFlags & DDBLT_COLORFILL) {
521 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
522 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
523 dwFlags &= ~DDBLT_COLORFILL;
526 if (dwFlags & DDBLT_DEPTHFILL)
527 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
528 if (dwFlags & DDBLT_ROP) {
529 /* Catch some degenerate cases here */
530 switch(lpbltfx->dwROP) {
532 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
534 case 0xAA0029: /* No-op */
537 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
539 case SRCCOPY: /* well, we do that below ? */
542 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
545 dwFlags &= ~DDBLT_ROP;
547 if (dwFlags & DDBLT_DDROPS) {
548 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
550 /* Now the 'with source' blits */
553 int sx, xinc, sy, yinc;
555 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
557 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
558 xinc = (srcwidth << 16) / dstwidth;
559 yinc = (srcheight << 16) / dstheight;
562 /* No effects, we can cheat here */
563 if (dstwidth == srcwidth) {
564 if (dstheight == srcheight) {
565 /* No stretching in either direction. This needs to be as
566 * fast as possible */
569 /* check for overlapping surfaces */
570 if (src != iface || xdst.top < xsrc.top ||
571 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
573 /* no overlap, or dst above src, so copy from top downwards */
574 for (y = 0; y < dstheight; y++)
576 memcpy(dbuf, sbuf, width);
577 sbuf += sdesc.u1.lPitch;
578 dbuf += ddesc.u1.lPitch;
581 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
583 sbuf += (sdesc.u1.lPitch*dstheight);
584 dbuf += (ddesc.u1.lPitch*dstheight);
585 for (y = 0; y < dstheight; y++)
587 sbuf -= sdesc.u1.lPitch;
588 dbuf -= ddesc.u1.lPitch;
589 memcpy(dbuf, sbuf, width);
592 else /* src and dst overlapping on the same line, use memmove */
594 for (y = 0; y < dstheight; y++)
596 memmove(dbuf, sbuf, width);
597 sbuf += sdesc.u1.lPitch;
598 dbuf += ddesc.u1.lPitch;
602 /* Stretching in Y direction only */
603 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
604 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
605 memcpy(dbuf, sbuf, width);
606 dbuf += ddesc.u1.lPitch;
610 /* Stretching in X direction */
612 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
613 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
615 if ((sy >> 16) == (last_sy >> 16)) {
616 /* this sourcerow is the same as last sourcerow -
617 * copy already stretched row
619 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
621 #define STRETCH_ROW(type) { \
622 type *s = (type *) sbuf, *d = (type *) dbuf; \
623 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
624 d[x] = s[sx >> 16]; \
628 case 1: STRETCH_ROW(BYTE)
629 case 2: STRETCH_ROW(WORD)
630 case 4: STRETCH_ROW(DWORD)
633 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
637 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
638 d[0] = (pixel )&0xff;
639 d[1] = (pixel>> 8)&0xff;
640 d[2] = (pixel>>16)&0xff;
646 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
647 ret = DDERR_UNSUPPORTED;
652 dbuf += ddesc.u1.lPitch;
657 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
658 DWORD keylow = 0, keyhigh = 0;
659 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
661 if (dwFlags & DDBLT_KEYSRC) {
662 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
663 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
664 } else if (dwFlags & DDBLT_KEYDEST){
665 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
666 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
667 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
668 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
669 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
671 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
672 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
674 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
677 if (dwFlags & DDBLT_DDFX) {
678 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
681 dTopRight = dbuf+((dstwidth-1)*bpp);
682 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
683 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
685 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
686 /* I don't think we need to do anything about this flag */
687 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
689 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
691 dTopRight = dTopLeft;
694 dBottomRight = dBottomLeft;
696 dstxinc = dstxinc *-1;
698 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
700 dTopLeft = dBottomLeft;
703 dTopRight = dBottomRight;
705 dstyinc = dstyinc *-1;
707 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
708 /* I don't think we need to do anything about this flag */
709 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
711 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
713 dBottomRight = dTopLeft;
716 dBottomLeft = dTopRight;
718 dstxinc = dstxinc * -1;
719 dstyinc = dstyinc * -1;
721 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
723 dTopLeft = dBottomLeft;
724 dBottomLeft = dBottomRight;
725 dBottomRight = dTopRight;
730 dstxinc = dstxinc * -1;
732 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
734 dTopLeft = dTopRight;
735 dTopRight = dBottomRight;
736 dBottomRight = dBottomLeft;
741 dstyinc = dstyinc * -1;
743 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
744 /* I don't think we need to do anything about this flag */
745 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
748 dwFlags &= ~(DDBLT_DDFX);
751 #define COPY_COLORKEY_FX(type) { \
752 type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
753 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
754 (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
756 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
758 if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
759 (LPBYTE)dx += dstxinc; \
761 (LPBYTE)d += dstyinc; \
766 case 1: COPY_COLORKEY_FX(BYTE)
767 case 2: COPY_COLORKEY_FX(WORD)
768 case 4: COPY_COLORKEY_FX(DWORD)
769 case 3: {LPBYTE s,d = dbuf, dx;
770 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
771 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
773 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
776 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
777 if (pixel < keylow || pixel > keyhigh){
778 dx[0] = (pixel )&0xff;
779 dx[1] = (pixel>> 8)&0xff;
780 dx[2] = (pixel>>16)&0xff;
788 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
789 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
790 ret = DDERR_UNSUPPORTED;
792 #undef COPY_COLORKEY_FX
798 if (dwFlags && FIXME_ON(ddraw)) {
799 FIXME("\tUnsupported flags: ");
800 DDRAW_dump_DDBLT(dwFlags);
804 IDirectDrawSurface7_Unlock(iface,NULL);
805 if (src) IDirectDrawSurface7_Unlock(src,NULL);
809 /* BltBatch: generic, unimplemented */
812 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
813 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
814 LPRECT rsrc, DWORD trans)
816 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
818 DDSURFACEDESC2 ddesc,sdesc;
822 RECT lock_src, lock_dst;
824 if (TRACE_ON(ddraw)) {
825 TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
826 This,dstx,dsty,src,rsrc,trans
830 DDRAW_dump_DDBLTFAST(trans);
832 TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
834 TRACE(" srcrect: NULL\n");
837 /* First, check if the possible override function handles this case */
838 if (This->aux_bltfast != NULL) {
839 if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
842 /* Get the surface description without locking to first compute the width / height */
843 ddesc = This->surface_desc;
844 sdesc = (ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src))->surface_desc;
847 WARN("rsrc is NULL!\n");
849 rsrc->left = rsrc->top = 0;
850 rsrc->right = sdesc.dwWidth;
851 rsrc->bottom = sdesc.dwHeight;
854 h=rsrc->bottom-rsrc->top;
855 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
856 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
857 if (h<=0) return DDERR_INVALIDRECT;
859 w=rsrc->right-rsrc->left;
860 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
861 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
862 if (w<=0) return DDERR_INVALIDRECT;
864 /* Now compute the locking rectangle... */
865 lock_src.left = rsrc->left;
866 lock_src.top = rsrc->top;
867 lock_src.right = lock_src.left + w;
868 lock_src.bottom = lock_src.top + h;
870 lock_dst.left = dstx;
872 lock_dst.right = dstx + w;
873 lock_dst.bottom = dsty + h;
875 /* We need to lock the surfaces, or we won't get refreshes when done. */
876 sdesc.dwSize = sizeof(sdesc);
877 IDirectDrawSurface7_Lock(src, &lock_src, &sdesc, DDLOCK_READONLY, 0);
878 ddesc.dwSize = sizeof(ddesc);
879 IDirectDrawSurface7_Lock(iface, &lock_dst, &ddesc, DDLOCK_WRITEONLY, 0);
881 bpp = GET_BPP(This->surface_desc);
882 sbuf = (BYTE *) sdesc.lpSurface;
883 dbuf = (BYTE *) ddesc.lpSurface;
885 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
886 DWORD keylow, keyhigh;
887 if (trans & DDBLTFAST_SRCCOLORKEY) {
888 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
889 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
891 /* I'm not sure if this is correct */
892 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
893 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
894 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
897 #define COPYBOX_COLORKEY(type) { \
899 s = (type *) sdesc.lpSurface; \
900 d = (type *) ddesc.lpSurface; \
901 for (y = 0; y < h; y++) { \
902 for (x = 0; x < w; x++) { \
904 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
906 (LPBYTE)s += sdesc.u1.lPitch; \
907 (LPBYTE)d += ddesc.u1.lPitch; \
913 case 1: COPYBOX_COLORKEY(BYTE)
914 case 2: COPYBOX_COLORKEY(WORD)
915 case 4: COPYBOX_COLORKEY(DWORD)
917 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
918 ret = DDERR_UNSUPPORTED;
921 #undef COPYBOX_COLORKEY
925 for (y = 0; y < h; y++) {
926 memcpy(dbuf, sbuf, width);
927 sbuf += sdesc.u1.lPitch;
928 dbuf += ddesc.u1.lPitch;
933 IDirectDrawSurface7_Unlock(iface, &lock_dst);
934 IDirectDrawSurface7_Unlock(src, &lock_src);
938 /* ChangeUniquenessValue: generic */
939 /* DeleteAttachedSurface: generic */
940 /* EnumAttachedSurfaces: generic */
941 /* EnumOverlayZOrders: generic, unimplemented */
943 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
944 IDirectDrawSurfaceImpl* back,
947 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
948 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
950 TRACE("(%p,%p)\n",front,back);
954 tmp = front_priv->dib.DIBsection;
955 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
956 back_priv->dib.DIBsection = tmp;
961 tmp = front_priv->dib.bitmap_data;
962 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
963 back_priv->dib.bitmap_data = tmp;
965 tmp = front->surface_desc.lpSurface;
966 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
967 back->surface_desc.lpSurface = tmp;
970 /* client_memory should not be different, but just in case */
973 tmp = front_priv->dib.client_memory;
974 front_priv->dib.client_memory = back_priv->dib.client_memory;
975 back_priv->dib.client_memory = tmp;
978 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
982 /* FreePrivateData: generic */
983 /* GetAttachedSurface: generic */
984 /* GetBltStatus: generic */
985 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
986 /* GetClipper: generic */
987 /* GetColorKey: generic */
989 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
991 DIB_PRIV_VAR(priv, This);
994 TRACE("Grabbing a DC for surface: %p\n", This);
996 hDC = CreateCompatibleDC(0);
997 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
999 SelectPalette(hDC, This->palette->hpal, FALSE);
1006 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1008 DIB_PRIV_VAR(priv, This);
1010 TRACE("Releasing DC for surface: %p\n", This);
1012 SelectObject(hDC, priv->dib.holdbitmap);
1018 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1020 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1023 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1025 return DIB_DirectDrawSurface_free_dc(This, hDC);
1028 /* GetDDInterface: generic */
1029 /* GetFlipStatus: generic */
1030 /* GetLOD: generic */
1031 /* GetOverlayPosition: generic */
1032 /* GetPalette: generic */
1033 /* GetPixelFormat: generic */
1034 /* GetPriority: generic */
1035 /* GetPrivateData: generic */
1036 /* GetSurfaceDesc: generic */
1037 /* GetUniquenessValue: generic */
1038 /* Initialize: generic */
1039 /* IsLost: generic */
1040 /* Lock: generic with callback? */
1041 /* PageLock: generic */
1042 /* PageUnlock: generic */
1045 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1047 TRACE("(%p)\n",iface);
1048 return DD_OK; /* ??? */
1051 /* SetClipper: generic */
1052 /* SetColorKey: generic */
1053 /* SetLOD: generic */
1054 /* SetOverlayPosition: generic */
1056 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1057 IDirectDrawPaletteImpl* pal)
1060 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1061 This->update_palette(This, pal,
1062 0, pal->palNumEntries,
1066 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1067 IDirectDrawPaletteImpl* pal,
1068 DWORD dwStart, DWORD dwCount,
1069 LPPALETTEENTRY palent)
1075 TRACE("updating primary palette\n");
1076 for (n=0; n<dwCount; n++) {
1077 col[n].rgbRed = palent[n].peRed;
1078 col[n].rgbGreen = palent[n].peGreen;
1079 col[n].rgbBlue = palent[n].peBlue;
1080 col[n].rgbReserved = 0;
1082 This->get_dc(This, &dc);
1083 SetDIBColorTable(dc, dwStart, dwCount, col);
1084 This->release_dc(This, dc);
1086 /* Propagate change to backbuffers if there are any */
1087 /* Basically this is a modification of the Flip code to find the backbuffer */
1088 /* and duplicate the palette update there as well */
1089 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1090 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1092 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1093 LPDIRECTDRAWSURFACE7 tgt;
1095 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1099 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1100 IDirectDrawSurface7,tgt);
1101 IDirectDrawSurface7_Release(tgt);
1102 target->get_dc(target, &dc);
1103 SetDIBColorTable(dc, dwStart, dwCount, col);
1104 target->release_dc(target, dc);
1109 /* SetPalette: generic */
1110 /* SetPriority: generic */
1111 /* SetPrivateData: generic */
1114 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1115 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1117 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1118 DIB_PRIV_VAR(priv, This);
1120 DWORD flags = pDDSD->dwFlags;
1122 if (TRACE_ON(ddraw)) {
1123 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1124 DDRAW_dump_surface_desc(pDDSD);
1127 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1128 flags &= ~DDSD_PIXELFORMAT;
1129 if (flags & DDSD_LPSURFACE) {
1130 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1132 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1135 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1136 HBITMAP oldbmp = priv->dib.DIBsection;
1137 LPVOID oldsurf = This->surface_desc.lpSurface;
1138 BOOL oldc = priv->dib.client_memory;
1140 flags &= ~DDSD_LPSURFACE;
1142 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1143 This->surface_desc.lpSurface = pDDSD->lpSurface;
1144 priv->dib.client_memory = TRUE;
1146 hr = create_dib(This);
1149 priv->dib.DIBsection = oldbmp;
1150 This->surface_desc.lpSurface = oldsurf;
1151 priv->dib.client_memory = oldc;
1155 DeleteObject(oldbmp);
1158 VirtualFree(oldsurf, 0, MEM_RELEASE);
1161 WARN("Unhandled flags : %08lx\n", flags);
1166 /* Unlock: ???, need callback */
1167 /* UpdateOverlay: generic */
1168 /* UpdateOverlayDisplay: generic */
1169 /* UpdateOverlayZOrder: generic */
1171 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1173 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1174 Main_DirectDrawSurface_QueryInterface,
1175 Main_DirectDrawSurface_AddRef,
1176 Main_DirectDrawSurface_Release,
1177 Main_DirectDrawSurface_AddAttachedSurface,
1178 Main_DirectDrawSurface_AddOverlayDirtyRect,
1179 DIB_DirectDrawSurface_Blt,
1180 Main_DirectDrawSurface_BltBatch,
1181 DIB_DirectDrawSurface_BltFast,
1182 Main_DirectDrawSurface_DeleteAttachedSurface,
1183 Main_DirectDrawSurface_EnumAttachedSurfaces,
1184 Main_DirectDrawSurface_EnumOverlayZOrders,
1185 Main_DirectDrawSurface_Flip,
1186 Main_DirectDrawSurface_GetAttachedSurface,
1187 Main_DirectDrawSurface_GetBltStatus,
1188 Main_DirectDrawSurface_GetCaps,
1189 Main_DirectDrawSurface_GetClipper,
1190 Main_DirectDrawSurface_GetColorKey,
1191 Main_DirectDrawSurface_GetDC,
1192 Main_DirectDrawSurface_GetFlipStatus,
1193 Main_DirectDrawSurface_GetOverlayPosition,
1194 Main_DirectDrawSurface_GetPalette,
1195 Main_DirectDrawSurface_GetPixelFormat,
1196 Main_DirectDrawSurface_GetSurfaceDesc,
1197 Main_DirectDrawSurface_Initialize,
1198 Main_DirectDrawSurface_IsLost,
1199 Main_DirectDrawSurface_Lock,
1200 Main_DirectDrawSurface_ReleaseDC,
1201 DIB_DirectDrawSurface_Restore,
1202 Main_DirectDrawSurface_SetClipper,
1203 Main_DirectDrawSurface_SetColorKey,
1204 Main_DirectDrawSurface_SetOverlayPosition,
1205 Main_DirectDrawSurface_SetPalette,
1206 Main_DirectDrawSurface_Unlock,
1207 Main_DirectDrawSurface_UpdateOverlay,
1208 Main_DirectDrawSurface_UpdateOverlayDisplay,
1209 Main_DirectDrawSurface_UpdateOverlayZOrder,
1210 Main_DirectDrawSurface_GetDDInterface,
1211 Main_DirectDrawSurface_PageLock,
1212 Main_DirectDrawSurface_PageUnlock,
1213 DIB_DirectDrawSurface_SetSurfaceDesc,
1214 Main_DirectDrawSurface_SetPrivateData,
1215 Main_DirectDrawSurface_GetPrivateData,
1216 Main_DirectDrawSurface_FreePrivateData,
1217 Main_DirectDrawSurface_GetUniquenessValue,
1218 Main_DirectDrawSurface_ChangeUniquenessValue,
1219 Main_DirectDrawSurface_SetPriority,
1220 Main_DirectDrawSurface_GetPriority,
1221 Main_DirectDrawSurface_SetLOD,
1222 Main_DirectDrawSurface_GetLOD