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
32 #include "wine/debug.h"
33 #include "ddraw_private.h"
34 #include "dsurface/main.h"
35 #include "dsurface/dib.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable;
41 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
42 inline static int get_dib_width_bytes( int width, int depth )
48 case 1: words = (width + 31) / 32; break;
49 case 4: words = (width + 7) / 8; break;
50 case 8: words = (width + 3) / 4; break;
52 case 16: words = (width + 1) / 2; break;
53 case 24: words = (width * 3 + 3)/4; break;
55 WARN("(%d): Unsupported depth\n", depth );
57 case 32: words = width; break;
63 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
68 DIB_DirectDrawSurfaceImpl* priv = This->private;
70 assert(This->surface_desc.lpSurface != NULL);
72 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
76 /* Allocate extra space to store the RGB bit masks. */
77 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
78 sizeof(BITMAPINFOHEADER)
83 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
84 sizeof(BITMAPINFOHEADER));
88 /* Allocate extra space for a palette. */
89 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
90 sizeof(BITMAPINFOHEADER)
92 * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
96 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
97 b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
98 b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
99 b_info->bmiHeader.biPlanes = 1;
100 b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
102 if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
103 && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
104 b_info->bmiHeader.biCompression = BI_RGB;
106 b_info->bmiHeader.biCompression = BI_BITFIELDS;
108 b_info->bmiHeader.biSizeImage
109 = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
110 * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
112 b_info->bmiHeader.biXPelsPerMeter = 0;
113 b_info->bmiHeader.biYPelsPerMeter = 0;
114 b_info->bmiHeader.biClrUsed = 0;
115 b_info->bmiHeader.biClrImportant = 0;
117 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
122 DWORD *masks = (DWORD *) &(b_info->bmiColors);
125 masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
126 masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
127 masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
133 usage = DIB_RGB_COLORS;
137 /* Don't know palette */
142 ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
145 HeapFree(GetProcessHeap(), 0, b_info);
146 return HRESULT_FROM_WIN32(GetLastError());
150 = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
151 (DWORD)This->surface_desc.lpSurface,
152 This->surface_desc.u1.lPitch);
154 if (!priv->dib.DIBsection) {
155 ERR("CreateDIBSection failed!\n");
156 HeapFree(GetProcessHeap(), 0, b_info);
157 return HRESULT_FROM_WIN32(GetLastError());
160 TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
161 if (!This->surface_desc.u1.lPitch) {
162 /* This can't happen, right? */
163 /* or use GDI_GetObj to get it from the created DIB? */
164 This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
165 This->surface_desc.dwFlags |= DDSD_PITCH;
168 if (!This->surface_desc.lpSurface) {
169 This->surface_desc.lpSurface = priv->dib.bitmap_data;
170 This->surface_desc.dwFlags |= DDSD_LPSURFACE;
173 HeapFree(GetProcessHeap(), 0, b_info);
175 /* I don't think it's worth checking for this. */
176 if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
177 ERR("unexpected error creating DirectDrawSurface DIB section\n");
179 /* this seems like a good place to put the handle for HAL driver use */
180 This->global_more.hKernelSurface = (ULONG_PTR)priv->dib.DIBsection;
185 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
187 DIB_DirectDrawSurfaceImpl* priv = This->private;
189 DeleteObject(priv->dib.DIBsection);
191 if (!priv->dib.client_memory)
192 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
194 Main_DirectDrawSurface_final_release(This);
197 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
198 LPDIRECTDRAWSURFACE7* ppDup)
200 return DIB_DirectDrawSurface_Create(This->ddraw_owner,
201 &This->surface_desc, ppDup, NULL);
204 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
205 IDirectDrawImpl *pDD,
206 const DDSURFACEDESC2 *pDDSD)
209 DIB_DirectDrawSurfaceImpl* priv = This->private;
211 TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
212 hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
213 if (FAILED(hr)) return hr;
215 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
216 DIB_IDirectDrawSurface7_VTable);
218 This->final_release = DIB_DirectDrawSurface_final_release;
219 This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
220 This->flip_data = DIB_DirectDrawSurface_flip_data;
222 This->get_dc = DIB_DirectDrawSurface_get_dc;
223 This->release_dc = DIB_DirectDrawSurface_release_dc;
226 This->set_palette = DIB_DirectDrawSurface_set_palette;
227 This->update_palette = DIB_DirectDrawSurface_update_palette;
229 TRACE("(%ldx%ld, pitch=%ld)\n",
230 This->surface_desc.dwWidth, This->surface_desc.dwHeight,
231 This->surface_desc.u1.lPitch);
232 /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
234 if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
236 /* "Client memory": it is managed by the application. */
237 /* XXX What if lPitch is not set? Use dwWidth or fail? */
239 priv->dib.client_memory = TRUE;
243 if (!(This->surface_desc.dwFlags & DDSD_PITCH))
245 int pitch = This->surface_desc.u1.lPitch;
247 pitch += 8 - (pitch % 8);
249 /* XXX else: how should lPitch be verified? */
251 This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
253 This->surface_desc.lpSurface
254 = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
255 * This->surface_desc.dwHeight + 4, /* The + 4 here is for dumb games reading after the end of the surface
256 when reading the last byte / half using word access */
257 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
259 if (This->surface_desc.lpSurface == NULL)
261 Main_DirectDrawSurface_final_release(This);
262 return HRESULT_FROM_WIN32(GetLastError());
265 priv->dib.client_memory = FALSE;
268 hr = create_dib(This);
271 if (!priv->dib.client_memory)
272 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
274 Main_DirectDrawSurface_final_release(This);
283 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
284 const DDSURFACEDESC2 *pDDSD,
285 LPDIRECTDRAWSURFACE7 *ppSurf,
288 IDirectDrawSurfaceImpl* This;
290 assert(pUnkOuter == NULL);
292 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
293 sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
294 if (This == NULL) return E_OUTOFMEMORY;
296 This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
298 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
300 HeapFree(GetProcessHeap(), 0, This);
302 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
308 /* AddAttachedSurface: generic */
309 /* AddOverlayDirtyRect: generic, unimplemented */
311 static HRESULT _Blt_ColorFill(
312 LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
319 #define COLORFILL_ROW(type) { \
320 type *d = (type *) buf; \
321 for (x = 0; x < width; x++) \
322 d[x] = (type) color; \
327 case 1: COLORFILL_ROW(BYTE)
328 case 2: COLORFILL_ROW(WORD)
329 case 3: { BYTE *d = (BYTE *) buf;
330 for (x = 0; x < width; x++,d+=3) {
331 d[0] = (color ) & 0xFF;
332 d[1] = (color>> 8) & 0xFF;
333 d[2] = (color>>16) & 0xFF;
336 case 4: COLORFILL_ROW(DWORD)
338 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
339 return DDERR_UNSUPPORTED;
344 /* Now copy first row */
346 for (y = 1; y < height; y++) {
348 memcpy(buf, first, width * bpp);
354 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
355 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
356 DWORD dwFlags, LPDDBLTFX lpbltfx)
358 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
360 DDSURFACEDESC2 ddesc,sdesc;
362 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
366 TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
368 if (TRACE_ON(ddraw)) {
369 if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
370 if (rsrc) TRACE("\tsrcrect :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
372 DDRAW_dump_DDBLT(dwFlags);
373 if (dwFlags & DDBLT_DDFX) {
375 DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
379 /* First, check if the possible override function handles this case */
380 if (This->aux_blt != NULL) {
381 if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
384 DD_STRUCT_INIT(&ddesc);
385 DD_STRUCT_INIT(&sdesc);
387 sdesc.dwSize = sizeof(sdesc);
388 if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
389 ddesc.dwSize = sizeof(ddesc);
390 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
393 memcpy(&xdst,rdst,sizeof(xdst));
396 xdst.bottom = ddesc.dwHeight;
398 xdst.right = ddesc.dwWidth;
402 memcpy(&xsrc,rsrc,sizeof(xsrc));
406 xsrc.bottom = sdesc.dwHeight;
408 xsrc.right = sdesc.dwWidth;
410 memset(&xsrc,0,sizeof(xsrc));
414 /* First check for the validity of source / destination rectangles. This was
415 verified using a test application + by MSDN.
418 ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
419 (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
420 (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
421 (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
422 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
423 WARN("Application gave us bad source rectangle for Blt.\n");
424 return DDERR_INVALIDRECT;
426 /* For the Destination rect, it can be out of bounds on the condition that a clipper
427 is set for the given surface.
429 if ((This->clipper == NULL) &&
430 ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
431 (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
432 (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
433 (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
434 (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
435 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
436 return DDERR_INVALIDRECT;
439 /* Now handle negative values in the rectangles. Warning: only supported for now
440 in the 'simple' cases (ie not in any stretching / rotation cases).
442 First, the case where nothing is to be done.
444 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
446 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
448 TRACE("Nothing to be done !\n");
452 /* The easy case : the source-less blits.... */
455 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
459 full_rect.right = ddesc.dwWidth;
460 full_rect.bottom = ddesc.dwHeight;
461 IntersectRect(&temp_rect, &full_rect, &xdst);
464 /* Only handle clipping on the destination rectangle */
465 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth );
466 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight);
467 if (clip_vert || clip_horiz) {
468 /* Now check if this is a special case or not... */
469 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
470 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
471 (dwFlags & DDBLT_DDFX)) {
472 WARN("Out of screen rectangle in special case. Not handled right now.\n");
477 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
478 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
481 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
482 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
484 /* And check if after clipping something is still to be done... */
485 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
486 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
487 TRACE("Nothing to be done after clipping !\n");
493 bpp = GET_BPP(ddesc);
494 srcheight = xsrc.bottom - xsrc.top;
495 srcwidth = xsrc.right - xsrc.left;
496 dstheight = xdst.bottom - xdst.top;
497 dstwidth = xdst.right - xdst.left;
498 width = (xdst.right - xdst.left) * bpp;
500 assert(width <= ddesc.u1.lPitch);
502 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
504 if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
506 static BOOL displayed = FALSE;
509 FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
512 dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
515 /* First, all the 'source-less' blits */
516 if (dwFlags & DDBLT_COLORFILL) {
517 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
518 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
519 dwFlags &= ~DDBLT_COLORFILL;
522 if (dwFlags & DDBLT_DEPTHFILL)
523 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
524 if (dwFlags & DDBLT_ROP) {
525 /* Catch some degenerate cases here */
526 switch(lpbltfx->dwROP) {
528 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
530 case 0xAA0029: /* No-op */
533 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
535 case SRCCOPY: /* well, we do that below ? */
538 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
541 dwFlags &= ~DDBLT_ROP;
543 if (dwFlags & DDBLT_DDROPS) {
544 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
546 /* Now the 'with source' blits */
549 int sx, xinc, sy, yinc;
551 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
553 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
554 xinc = (srcwidth << 16) / dstwidth;
555 yinc = (srcheight << 16) / dstheight;
558 /* No effects, we can cheat here */
559 if (dstwidth == srcwidth) {
560 if (dstheight == srcheight) {
561 /* No stretching in either direction. This needs to be as
562 * fast as possible */
565 /* check for overlapping surfaces */
566 if (src != iface || xdst.top < xsrc.top ||
567 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
569 /* no overlap, or dst above src, so copy from top downwards */
570 for (y = 0; y < dstheight; y++)
572 memcpy(dbuf, sbuf, width);
573 sbuf += sdesc.u1.lPitch;
574 dbuf += ddesc.u1.lPitch;
577 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
579 sbuf += (sdesc.u1.lPitch*dstheight);
580 dbuf += (ddesc.u1.lPitch*dstheight);
581 for (y = 0; y < dstheight; y++)
583 sbuf -= sdesc.u1.lPitch;
584 dbuf -= ddesc.u1.lPitch;
585 memcpy(dbuf, sbuf, width);
588 else /* src and dst overlapping on the same line, use memmove */
590 for (y = 0; y < dstheight; y++)
592 memmove(dbuf, sbuf, width);
593 sbuf += sdesc.u1.lPitch;
594 dbuf += ddesc.u1.lPitch;
598 /* Stretching in Y direction only */
599 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
600 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
601 memcpy(dbuf, sbuf, width);
602 dbuf += ddesc.u1.lPitch;
606 /* Stretching in X direction */
608 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
609 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
611 if ((sy >> 16) == (last_sy >> 16)) {
612 /* this sourcerow is the same as last sourcerow -
613 * copy already stretched row
615 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
617 #define STRETCH_ROW(type) { \
618 type *s = (type *) sbuf, *d = (type *) dbuf; \
619 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
620 d[x] = s[sx >> 16]; \
624 case 1: STRETCH_ROW(BYTE)
625 case 2: STRETCH_ROW(WORD)
626 case 4: STRETCH_ROW(DWORD)
629 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
633 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
634 d[0] = (pixel )&0xff;
635 d[1] = (pixel>> 8)&0xff;
636 d[2] = (pixel>>16)&0xff;
642 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
643 ret = DDERR_UNSUPPORTED;
648 dbuf += ddesc.u1.lPitch;
653 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
654 DWORD keylow = 0, keyhigh = 0;
655 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
657 if (dwFlags & DDBLT_KEYSRC) {
658 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
659 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
660 } else if (dwFlags & DDBLT_KEYDEST){
661 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
662 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
663 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
664 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
665 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
667 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
668 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
670 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
673 if (dwFlags & DDBLT_DDFX) {
674 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
677 dTopRight = dbuf+((dstwidth-1)*bpp);
678 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
679 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
681 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
682 /* I don't think we need to do anything about this flag */
683 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
685 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
687 dTopRight = dTopLeft;
690 dBottomRight = dBottomLeft;
692 dstxinc = dstxinc *-1;
694 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
696 dTopLeft = dBottomLeft;
699 dTopRight = dBottomRight;
701 dstyinc = dstyinc *-1;
703 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
704 /* I don't think we need to do anything about this flag */
705 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
707 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
709 dBottomRight = dTopLeft;
712 dBottomLeft = dTopRight;
714 dstxinc = dstxinc * -1;
715 dstyinc = dstyinc * -1;
717 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
719 dTopLeft = dBottomLeft;
720 dBottomLeft = dBottomRight;
721 dBottomRight = dTopRight;
726 dstxinc = dstxinc * -1;
728 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
730 dTopLeft = dTopRight;
731 dTopRight = dBottomRight;
732 dBottomRight = dBottomLeft;
737 dstyinc = dstyinc * -1;
739 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
740 /* I don't think we need to do anything about this flag */
741 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
744 dwFlags &= ~(DDBLT_DDFX);
747 #define COPY_COLORKEY_FX(type) { \
748 type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
749 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
750 (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
752 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
754 if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
755 (LPBYTE)dx += dstxinc; \
757 (LPBYTE)d += dstyinc; \
762 case 1: COPY_COLORKEY_FX(BYTE)
763 case 2: COPY_COLORKEY_FX(WORD)
764 case 4: COPY_COLORKEY_FX(DWORD)
765 case 3: {LPBYTE s,d = dbuf, dx;
766 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
767 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
769 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
772 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
773 if (pixel < keylow || pixel > keyhigh){
774 dx[0] = (pixel )&0xff;
775 dx[1] = (pixel>> 8)&0xff;
776 dx[2] = (pixel>>16)&0xff;
784 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
785 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
786 ret = DDERR_UNSUPPORTED;
788 #undef COPY_COLORKEY_FX
794 if (dwFlags && FIXME_ON(ddraw)) {
795 FIXME("\tUnsupported flags: ");
796 DDRAW_dump_DDBLT(dwFlags);
800 IDirectDrawSurface7_Unlock(iface,NULL);
801 if (src) IDirectDrawSurface7_Unlock(src,NULL);
805 /* BltBatch: generic, unimplemented */
808 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
809 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
810 LPRECT rsrc, DWORD trans)
812 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
814 DDSURFACEDESC2 ddesc,sdesc;
820 if (TRACE_ON(ddraw)) {
821 FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
822 This,dstx,dsty,src,rsrc,trans
826 DDRAW_dump_DDBLTFAST(trans);
828 FIXME("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
830 FIXME(" srcrect: NULL\n");
833 /* First, check if the possible override function handles this case */
834 if (This->aux_bltfast != NULL) {
835 if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
838 /* We need to lock the surfaces, or we won't get refreshes when done. */
839 sdesc.dwSize = sizeof(sdesc);
840 IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
841 ddesc.dwSize = sizeof(ddesc);
842 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
845 WARN("rsrc is NULL!\n");
847 rsrc->left = rsrc->top = 0;
848 rsrc->right = sdesc.dwWidth;
849 rsrc->bottom = sdesc.dwHeight;
852 bpp = GET_BPP(This->surface_desc);
853 sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
854 dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
857 h=rsrc->bottom-rsrc->top;
858 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
859 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
862 w=rsrc->right-rsrc->left;
863 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
864 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
867 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
868 DWORD keylow, keyhigh;
869 if (trans & DDBLTFAST_SRCCOLORKEY) {
870 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
871 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
873 /* I'm not sure if this is correct */
874 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
875 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
876 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
879 #define COPYBOX_COLORKEY(type) { \
880 type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
881 s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
882 d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
883 for (y = 0; y < h; y++) { \
884 for (x = 0; x < w; x++) { \
886 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
888 (LPBYTE)s += sdesc.u1.lPitch; \
889 (LPBYTE)d += ddesc.u1.lPitch; \
895 case 1: COPYBOX_COLORKEY(BYTE)
896 case 2: COPYBOX_COLORKEY(WORD)
897 case 4: COPYBOX_COLORKEY(DWORD)
899 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
900 ret = DDERR_UNSUPPORTED;
903 #undef COPYBOX_COLORKEY
907 for (y = 0; y < h; y++) {
908 memcpy(dbuf, sbuf, width);
909 sbuf += sdesc.u1.lPitch;
910 dbuf += ddesc.u1.lPitch;
914 IDirectDrawSurface7_Unlock(iface, NULL);
915 IDirectDrawSurface7_Unlock(src, NULL);
919 /* ChangeUniquenessValue: generic */
920 /* DeleteAttachedSurface: generic */
921 /* EnumAttachedSurfaces: generic */
922 /* EnumOverlayZOrders: generic, unimplemented */
924 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
925 IDirectDrawSurfaceImpl* back,
928 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
929 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
931 TRACE("(%p,%p)\n",front,back);
935 tmp = front_priv->dib.DIBsection;
936 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
937 back_priv->dib.DIBsection = tmp;
942 tmp = front_priv->dib.bitmap_data;
943 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
944 back_priv->dib.bitmap_data = tmp;
946 tmp = front->surface_desc.lpSurface;
947 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
948 back->surface_desc.lpSurface = tmp;
951 /* client_memory should not be different, but just in case */
954 tmp = front_priv->dib.client_memory;
955 front_priv->dib.client_memory = back_priv->dib.client_memory;
956 back_priv->dib.client_memory = tmp;
959 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
963 /* FreePrivateData: generic */
964 /* GetAttachedSurface: generic */
965 /* GetBltStatus: generic */
966 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
967 /* GetClipper: generic */
968 /* GetColorKey: generic */
970 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
972 DIB_PRIV_VAR(priv, This);
975 TRACE("Grabbing a DC for surface: %p\n", This);
977 hDC = CreateCompatibleDC(0);
978 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
980 SelectPalette(hDC, This->palette->hpal, FALSE);
987 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
989 DIB_PRIV_VAR(priv, This);
991 TRACE("Releasing DC for surface: %p\n", This);
993 SelectObject(hDC, priv->dib.holdbitmap);
999 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1001 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1004 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1006 return DIB_DirectDrawSurface_free_dc(This, hDC);
1009 /* GetDDInterface: generic */
1010 /* GetFlipStatus: generic */
1011 /* GetLOD: generic */
1012 /* GetOverlayPosition: generic */
1013 /* GetPalette: generic */
1014 /* GetPixelFormat: generic */
1015 /* GetPriority: generic */
1016 /* GetPrivateData: generic */
1017 /* GetSurfaceDesc: generic */
1018 /* GetUniquenessValue: generic */
1019 /* Initialize: generic */
1020 /* IsLost: generic */
1021 /* Lock: generic with callback? */
1022 /* PageLock: generic */
1023 /* PageUnlock: generic */
1026 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1028 TRACE("(%p)\n",iface);
1029 return DD_OK; /* ??? */
1032 /* SetClipper: generic */
1033 /* SetColorKey: generic */
1034 /* SetLOD: generic */
1035 /* SetOverlayPosition: generic */
1037 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1038 IDirectDrawPaletteImpl* pal)
1041 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1042 This->update_palette(This, pal,
1043 0, pal->palNumEntries,
1047 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1048 IDirectDrawPaletteImpl* pal,
1049 DWORD dwStart, DWORD dwCount,
1050 LPPALETTEENTRY palent)
1056 TRACE("updating primary palette\n");
1057 for (n=0; n<dwCount; n++) {
1058 col[n].rgbRed = palent[n].peRed;
1059 col[n].rgbGreen = palent[n].peGreen;
1060 col[n].rgbBlue = palent[n].peBlue;
1061 col[n].rgbReserved = 0;
1063 This->get_dc(This, &dc);
1064 SetDIBColorTable(dc, dwStart, dwCount, col);
1065 This->release_dc(This, dc);
1067 /* Propagate change to backbuffers if there are any */
1068 /* Basically this is a modification of the Flip code to find the backbuffer */
1069 /* and duplicate the palette update there as well */
1070 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1071 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1073 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1074 LPDIRECTDRAWSURFACE7 tgt;
1076 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1080 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1081 IDirectDrawSurface7,tgt);
1082 IDirectDrawSurface7_Release(tgt);
1083 target->get_dc(target, &dc);
1084 SetDIBColorTable(dc, dwStart, dwCount, col);
1085 target->release_dc(target, dc);
1090 /* SetPalette: generic */
1091 /* SetPriority: generic */
1092 /* SetPrivateData: generic */
1095 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1096 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1098 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1099 DIB_PRIV_VAR(priv, This);
1101 DWORD flags = pDDSD->dwFlags;
1103 if (TRACE_ON(ddraw)) {
1104 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1105 DDRAW_dump_surface_desc(pDDSD);
1108 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1109 flags &= ~DDSD_PIXELFORMAT;
1110 if (flags & DDSD_LPSURFACE) {
1111 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1113 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1116 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1117 HBITMAP oldbmp = priv->dib.DIBsection;
1118 LPVOID oldsurf = This->surface_desc.lpSurface;
1119 BOOL oldc = priv->dib.client_memory;
1121 flags &= ~DDSD_LPSURFACE;
1123 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1124 This->surface_desc.lpSurface = pDDSD->lpSurface;
1125 priv->dib.client_memory = TRUE;
1127 hr = create_dib(This);
1130 priv->dib.DIBsection = oldbmp;
1131 This->surface_desc.lpSurface = oldsurf;
1132 priv->dib.client_memory = oldc;
1136 DeleteObject(oldbmp);
1139 VirtualFree(oldsurf, 0, MEM_RELEASE);
1142 WARN("Unhandled flags : %08lx\n", flags);
1147 /* Unlock: ???, need callback */
1148 /* UpdateOverlay: generic */
1149 /* UpdateOverlayDisplay: generic */
1150 /* UpdateOverlayZOrder: generic */
1152 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1154 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1155 Main_DirectDrawSurface_QueryInterface,
1156 Main_DirectDrawSurface_AddRef,
1157 Main_DirectDrawSurface_Release,
1158 Main_DirectDrawSurface_AddAttachedSurface,
1159 Main_DirectDrawSurface_AddOverlayDirtyRect,
1160 DIB_DirectDrawSurface_Blt,
1161 Main_DirectDrawSurface_BltBatch,
1162 DIB_DirectDrawSurface_BltFast,
1163 Main_DirectDrawSurface_DeleteAttachedSurface,
1164 Main_DirectDrawSurface_EnumAttachedSurfaces,
1165 Main_DirectDrawSurface_EnumOverlayZOrders,
1166 Main_DirectDrawSurface_Flip,
1167 Main_DirectDrawSurface_GetAttachedSurface,
1168 Main_DirectDrawSurface_GetBltStatus,
1169 Main_DirectDrawSurface_GetCaps,
1170 Main_DirectDrawSurface_GetClipper,
1171 Main_DirectDrawSurface_GetColorKey,
1172 Main_DirectDrawSurface_GetDC,
1173 Main_DirectDrawSurface_GetFlipStatus,
1174 Main_DirectDrawSurface_GetOverlayPosition,
1175 Main_DirectDrawSurface_GetPalette,
1176 Main_DirectDrawSurface_GetPixelFormat,
1177 Main_DirectDrawSurface_GetSurfaceDesc,
1178 Main_DirectDrawSurface_Initialize,
1179 Main_DirectDrawSurface_IsLost,
1180 Main_DirectDrawSurface_Lock,
1181 Main_DirectDrawSurface_ReleaseDC,
1182 DIB_DirectDrawSurface_Restore,
1183 Main_DirectDrawSurface_SetClipper,
1184 Main_DirectDrawSurface_SetColorKey,
1185 Main_DirectDrawSurface_SetOverlayPosition,
1186 Main_DirectDrawSurface_SetPalette,
1187 Main_DirectDrawSurface_Unlock,
1188 Main_DirectDrawSurface_UpdateOverlay,
1189 Main_DirectDrawSurface_UpdateOverlayDisplay,
1190 Main_DirectDrawSurface_UpdateOverlayZOrder,
1191 Main_DirectDrawSurface_GetDDInterface,
1192 Main_DirectDrawSurface_PageLock,
1193 Main_DirectDrawSurface_PageUnlock,
1194 DIB_DirectDrawSurface_SetSurfaceDesc,
1195 Main_DirectDrawSurface_SetPrivateData,
1196 Main_DirectDrawSurface_GetPrivateData,
1197 Main_DirectDrawSurface_FreePrivateData,
1198 Main_DirectDrawSurface_GetUniquenessValue,
1199 Main_DirectDrawSurface_ChangeUniquenessValue,
1200 Main_DirectDrawSurface_SetPriority,
1201 Main_DirectDrawSurface_GetPriority,
1202 Main_DirectDrawSurface_SetLOD,
1203 Main_DirectDrawSurface_GetLOD