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
29 #include "wine/debug.h"
30 #include "ddraw_private.h"
31 #include "dsurface/main.h"
32 #include "dsurface/dib.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
36 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable;
38 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
39 inline static int get_dib_width_bytes( int width, int depth )
45 case 1: words = (width + 31) / 32; break;
46 case 4: words = (width + 7) / 8; break;
47 case 8: words = (width + 3) / 4; break;
49 case 16: words = (width + 1) / 2; break;
50 case 24: words = (width * 3 + 3)/4; break;
52 WARN("(%d): Unsupported depth\n", depth );
54 case 32: words = width; break;
60 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
65 DIB_DirectDrawSurfaceImpl* priv = This->private;
67 assert(This->surface_desc.lpSurface != NULL);
69 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
73 /* Allocate extra space to store the RGB bit masks. */
74 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
75 sizeof(BITMAPINFOHEADER)
80 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
81 sizeof(BITMAPINFOHEADER));
85 /* Allocate extra space for a palette. */
86 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
87 sizeof(BITMAPINFOHEADER)
89 * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
93 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
94 b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
95 b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
96 b_info->bmiHeader.biPlanes = 1;
97 b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
99 if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
100 && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
101 b_info->bmiHeader.biCompression = BI_RGB;
103 b_info->bmiHeader.biCompression = BI_BITFIELDS;
105 b_info->bmiHeader.biSizeImage
106 = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
107 * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
109 b_info->bmiHeader.biXPelsPerMeter = 0;
110 b_info->bmiHeader.biYPelsPerMeter = 0;
111 b_info->bmiHeader.biClrUsed = 0;
112 b_info->bmiHeader.biClrImportant = 0;
114 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
119 DWORD *masks = (DWORD *) &(b_info->bmiColors);
122 masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
123 masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
124 masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
130 usage = DIB_RGB_COLORS;
134 /* Don't know palette */
139 ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
142 HeapFree(GetProcessHeap(), 0, b_info);
143 return HRESULT_FROM_WIN32(GetLastError());
147 = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
148 (DWORD)This->surface_desc.lpSurface,
149 This->surface_desc.u1.lPitch);
151 if (!priv->dib.DIBsection) {
152 ERR("CreateDIBSection failed!\n");
153 HeapFree(GetProcessHeap(), 0, b_info);
154 return HRESULT_FROM_WIN32(GetLastError());
157 TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
158 if (!This->surface_desc.u1.lPitch) {
159 /* This can't happen, right? */
160 /* or use GDI_GetObj to get it from the created DIB? */
161 This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
162 This->surface_desc.dwFlags |= DDSD_PITCH;
165 if (!This->surface_desc.lpSurface) {
166 This->surface_desc.lpSurface = priv->dib.bitmap_data;
167 This->surface_desc.dwFlags |= DDSD_LPSURFACE;
170 HeapFree(GetProcessHeap(), 0, b_info);
172 /* I don't think it's worth checking for this. */
173 if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
174 ERR("unexpected error creating DirectDrawSurface DIB section\n");
176 /* this seems like a good place to put the handle for HAL driver use */
177 This->global_more.hKernelSurface = priv->dib.DIBsection;
182 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
184 DIB_DirectDrawSurfaceImpl* priv = This->private;
186 DeleteObject(priv->dib.DIBsection);
188 if (!priv->dib.client_memory)
189 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
191 Main_DirectDrawSurface_final_release(This);
194 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
195 LPDIRECTDRAWSURFACE7* ppDup)
197 return DIB_DirectDrawSurface_Create(This->ddraw_owner,
198 &This->surface_desc, ppDup, NULL);
201 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
202 IDirectDrawImpl *pDD,
203 const DDSURFACEDESC2 *pDDSD)
206 DIB_DirectDrawSurfaceImpl* priv = This->private;
208 TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
209 hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
210 if (FAILED(hr)) return hr;
212 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
213 DIB_IDirectDrawSurface7_VTable);
215 This->final_release = DIB_DirectDrawSurface_final_release;
216 This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
217 This->flip_data = DIB_DirectDrawSurface_flip_data;
219 This->get_dc = DIB_DirectDrawSurface_get_dc;
220 This->release_dc = DIB_DirectDrawSurface_release_dc;
221 This->hDC = (HDC)NULL;
223 This->set_palette = DIB_DirectDrawSurface_set_palette;
224 This->update_palette = DIB_DirectDrawSurface_update_palette;
226 TRACE("(%ldx%ld, pitch=%ld)\n",
227 This->surface_desc.dwWidth, This->surface_desc.dwHeight,
228 This->surface_desc.u1.lPitch);
229 /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
231 if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
233 /* "Client memory": it is managed by the application. */
234 /* XXX What if lPitch is not set? Use dwWidth or fail? */
236 priv->dib.client_memory = TRUE;
240 if (!(This->surface_desc.dwFlags & DDSD_PITCH))
242 int pitch = This->surface_desc.u1.lPitch;
244 pitch += 8 - (pitch % 8);
246 /* XXX else: how should lPitch be verified? */
248 This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
250 This->surface_desc.lpSurface
251 = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
252 * This->surface_desc.dwHeight,
253 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
255 if (This->surface_desc.lpSurface == NULL)
257 Main_DirectDrawSurface_final_release(This);
258 return HRESULT_FROM_WIN32(GetLastError());
261 priv->dib.client_memory = FALSE;
264 hr = create_dib(This);
267 if (!priv->dib.client_memory)
268 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
270 Main_DirectDrawSurface_final_release(This);
279 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
280 const DDSURFACEDESC2 *pDDSD,
281 LPDIRECTDRAWSURFACE7 *ppSurf,
284 IDirectDrawSurfaceImpl* This;
286 assert(pUnkOuter == NULL);
288 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
289 sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
290 if (This == NULL) return E_OUTOFMEMORY;
292 This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
294 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
296 HeapFree(GetProcessHeap(), 0, This);
298 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
304 /* AddAttachedSurface: generic */
305 /* AddOverlayDirtyRect: generic, unimplemented */
307 static HRESULT _Blt_ColorFill(
308 LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
315 #define COLORFILL_ROW(type) { \
316 type *d = (type *) buf; \
317 for (x = 0; x < width; x++) \
318 d[x] = (type) color; \
323 case 1: COLORFILL_ROW(BYTE)
324 case 2: COLORFILL_ROW(WORD)
325 case 4: COLORFILL_ROW(DWORD)
327 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
328 return DDERR_UNSUPPORTED;
333 /* Now copy first row */
335 for (y = 1; y < height; y++) {
337 memcpy(buf, first, width * bpp);
343 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
344 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
345 DWORD dwFlags, LPDDBLTFX lpbltfx)
347 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
349 DDSURFACEDESC2 ddesc,sdesc;
351 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
355 TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
357 DD_STRUCT_INIT(&ddesc);
358 DD_STRUCT_INIT(&sdesc);
360 sdesc.dwSize = sizeof(sdesc);
361 if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
362 ddesc.dwSize = sizeof(ddesc);
363 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
365 if (TRACE_ON(ddraw)) {
366 if (rdst) TRACE("\tdestrect :%dx%d-%dx%d\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
367 if (rsrc) TRACE("\tsrcrect :%dx%d-%dx%d\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
369 DDRAW_dump_DDBLT(dwFlags);
370 if (dwFlags & DDBLT_DDFX) {
372 DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
377 memcpy(&xdst,rdst,sizeof(xdst));
380 xdst.bottom = ddesc.dwHeight;
382 xdst.right = ddesc.dwWidth;
386 memcpy(&xsrc,rsrc,sizeof(xsrc));
390 xsrc.bottom = sdesc.dwHeight;
392 xsrc.right = sdesc.dwWidth;
394 memset(&xsrc,0,sizeof(xsrc));
398 if (src) assert((xsrc.bottom-xsrc.top) <= sdesc.dwHeight);
399 assert((xdst.bottom-xdst.top) <= ddesc.dwHeight);
401 /* Now handle negative values in the rectangles. Warning: only supported for now
402 in the 'simple' cases (ie not in any stretching / rotation cases).
404 First, the case where nothing is to be done.
406 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
408 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
410 TRACE("Nothing to be done !\n");
414 /* The easy case : the source-less blits.... */
417 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
421 full_rect.right = ddesc.dwWidth;
422 full_rect.bottom = ddesc.dwHeight;
423 IntersectRect(&temp_rect, &full_rect, &xdst);
426 /* This is trickier as any update to one rectangle need to be propagated to the other */
427 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth ) || (xsrc.left < 0) || (xsrc.right > (int) sdesc.dwWidth );
428 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight) || (xsrc.top < 0) || (xsrc.bottom > (int) sdesc.dwHeight);
429 if (clip_vert || clip_horiz) {
430 /* Now check if this is a special case or not... */
431 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
432 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
433 (dwFlags & DDBLT_DDFX)) {
434 WARN("Out of screen rectangle in special case. Not handled right now.\n");
439 if (xsrc.left < 0) { xdst.left -= xsrc.left; xsrc.left = 0; }
440 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
441 if (xsrc.right > sdesc.dwWidth) { xdst.right -= (xsrc.right - (int) sdesc.dwWidth); xsrc.right = (int) sdesc.dwWidth; }
442 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
445 if (xsrc.top < 0) { xdst.top -= xsrc.top; xsrc.top = 0; }
446 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
447 if (xsrc.bottom > sdesc.dwHeight) { xdst.bottom -= (xsrc.bottom - (int) sdesc.dwHeight); xsrc.bottom = (int) sdesc.dwHeight; }
448 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
450 /* And check if after clipping something is still to be done... */
451 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
452 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
453 TRACE("Nothing to be done after clipping !\n");
459 bpp = GET_BPP(ddesc);
460 srcheight = xsrc.bottom - xsrc.top;
461 srcwidth = xsrc.right - xsrc.left;
462 dstheight = xdst.bottom - xdst.top;
463 dstwidth = xdst.right - xdst.left;
464 width = (xdst.right - xdst.left) * bpp;
466 assert(width <= ddesc.u1.lPitch);
468 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
470 if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
472 static BOOL displayed = FALSE;
475 FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
478 dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
481 /* First, all the 'source-less' blits */
482 if (dwFlags & DDBLT_COLORFILL) {
483 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
484 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
485 dwFlags &= ~DDBLT_COLORFILL;
488 if (dwFlags & DDBLT_DEPTHFILL)
489 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
490 if (dwFlags & DDBLT_ROP) {
491 /* Catch some degenerate cases here */
492 switch(lpbltfx->dwROP) {
494 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
496 case 0xAA0029: /* No-op */
499 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
501 case SRCCOPY: /* well, we do that below ? */
504 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
507 dwFlags &= ~DDBLT_ROP;
509 if (dwFlags & DDBLT_DDROPS) {
510 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
512 /* Now the 'with source' blits */
515 int sx, xinc, sy, yinc;
517 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
519 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
520 xinc = (srcwidth << 16) / dstwidth;
521 yinc = (srcheight << 16) / dstheight;
524 /* No effects, we can cheat here */
525 if (dstwidth == srcwidth) {
526 if (dstheight == srcheight) {
527 /* No stretching in either direction. This needs to be as
528 * fast as possible */
531 /* check for overlapping surfaces */
532 if (src != iface || xdst.top < xsrc.top ||
533 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
535 /* no overlap, or dst above src, so copy from top downwards */
536 for (y = 0; y < dstheight; y++)
538 memcpy(dbuf, sbuf, width);
539 sbuf += sdesc.u1.lPitch;
540 dbuf += ddesc.u1.lPitch;
543 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
545 sbuf += (sdesc.u1.lPitch*dstheight);
546 dbuf += (ddesc.u1.lPitch*dstheight);
547 for (y = 0; y < dstheight; y++)
549 sbuf -= sdesc.u1.lPitch;
550 dbuf -= ddesc.u1.lPitch;
551 memcpy(dbuf, sbuf, width);
554 else /* src and dst overlapping on the same line, use memmove */
556 for (y = 0; y < dstheight; y++)
558 memmove(dbuf, sbuf, width);
559 sbuf += sdesc.u1.lPitch;
560 dbuf += ddesc.u1.lPitch;
564 /* Stretching in Y direction only */
565 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
566 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
567 memcpy(dbuf, sbuf, width);
568 dbuf += ddesc.u1.lPitch;
572 /* Stretching in X direction */
574 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
575 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
577 if ((sy >> 16) == (last_sy >> 16)) {
578 /* this sourcerow is the same as last sourcerow -
579 * copy already stretched row
581 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
583 #define STRETCH_ROW(type) { \
584 type *s = (type *) sbuf, *d = (type *) dbuf; \
585 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
586 d[x] = s[sx >> 16]; \
590 case 1: STRETCH_ROW(BYTE)
591 case 2: STRETCH_ROW(WORD)
592 case 4: STRETCH_ROW(DWORD)
595 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
599 pixel = (s[0]<<16)|(s[1]<<8)|s[2];
600 d[0] = (pixel>>16)&0xff;
601 d[1] = (pixel>> 8)&0xff;
602 d[2] = (pixel )&0xff;
608 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
609 ret = DDERR_UNSUPPORTED;
614 dbuf += ddesc.u1.lPitch;
619 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
620 DWORD keylow = 0, keyhigh = 0;
621 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
623 if (dwFlags & DDBLT_KEYSRC) {
624 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
625 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
626 } else if (dwFlags & DDBLT_KEYDEST){
627 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
628 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
629 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
630 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
631 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
633 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
634 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
636 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
639 if (dwFlags & DDBLT_DDFX) {
640 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
643 dTopRight = dbuf+((dstwidth-1)*bpp);
644 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
645 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
647 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
648 /* I don't think we need to do anything about this flag */
649 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
651 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
653 dTopRight = dTopLeft;
656 dBottomRight = dBottomLeft;
658 dstxinc = dstxinc *-1;
660 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
662 dTopLeft = dBottomLeft;
665 dTopRight = dBottomRight;
667 dstyinc = dstyinc *-1;
669 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
670 /* I don't think we need to do anything about this flag */
671 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
673 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
675 dBottomRight = dTopLeft;
678 dBottomLeft = dTopRight;
680 dstxinc = dstxinc * -1;
681 dstyinc = dstyinc * -1;
683 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
685 dTopLeft = dBottomLeft;
686 dBottomLeft = dBottomRight;
687 dBottomRight = dTopRight;
692 dstxinc = dstxinc * -1;
694 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
696 dTopLeft = dTopRight;
697 dTopRight = dBottomRight;
698 dBottomRight = dBottomLeft;
703 dstyinc = dstyinc * -1;
705 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
706 /* I don't think we need to do anything about this flag */
707 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
710 dwFlags &= ~(DDBLT_DDFX);
713 #define COPY_COLORKEY_FX(type) { \
714 type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
715 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
716 (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
718 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
720 if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
721 (LPBYTE)dx += dstxinc; \
723 (LPBYTE)d += dstyinc; \
728 case 1: COPY_COLORKEY_FX(BYTE)
729 case 2: COPY_COLORKEY_FX(WORD)
730 case 4: COPY_COLORKEY_FX(DWORD)
731 case 3: {LPBYTE s,d = dbuf, dx;
732 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
733 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
735 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
738 pixel = (s[0]<<16)|(s[1]<<8)|s[2];
739 if (pixel < keylow || pixel > keyhigh){
740 dx[0] = (pixel>>16)&0xff;
741 dx[1] = (pixel>> 8)&0xff;
742 dx[2] = (pixel )&0xff;
750 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
751 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
752 ret = DDERR_UNSUPPORTED;
754 #undef COPY_COLORKEY_FX
760 if (dwFlags && FIXME_ON(ddraw)) {
761 FIXME("\tUnsupported flags: ");
762 DDRAW_dump_DDBLT(dwFlags);
766 IDirectDrawSurface7_Unlock(iface,NULL);
767 if (src) IDirectDrawSurface7_Unlock(src,NULL);
771 /* BltBatch: generic, unimplemented */
774 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
775 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
776 LPRECT rsrc, DWORD trans)
778 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
780 DDSURFACEDESC2 ddesc,sdesc;
786 if (TRACE_ON(ddraw)) {
787 FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
788 This,dstx,dsty,src,rsrc,trans
792 DDRAW_dump_DDBLTFAST(trans);
794 FIXME("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
796 FIXME(" srcrect: NULL\n");
799 /* We need to lock the surfaces, or we won't get refreshes when done. */
800 sdesc.dwSize = sizeof(sdesc);
801 IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
802 ddesc.dwSize = sizeof(ddesc);
803 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
806 WARN("rsrc is NULL!\n");
808 rsrc->left = rsrc->top = 0;
809 rsrc->right = sdesc.dwWidth;
810 rsrc->bottom = sdesc.dwHeight;
813 bpp = GET_BPP(This->surface_desc);
814 sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
815 dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
818 h=rsrc->bottom-rsrc->top;
819 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
820 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
823 w=rsrc->right-rsrc->left;
824 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
825 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
828 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
829 DWORD keylow, keyhigh;
830 if (trans & DDBLTFAST_SRCCOLORKEY) {
831 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
832 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
834 /* I'm not sure if this is correct */
835 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
836 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
837 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
840 #define COPYBOX_COLORKEY(type) { \
841 type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
842 s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
843 d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
844 for (y = 0; y < h; y++) { \
845 for (x = 0; x < w; x++) { \
847 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
849 (LPBYTE)s += sdesc.u1.lPitch; \
850 (LPBYTE)d += ddesc.u1.lPitch; \
856 case 1: COPYBOX_COLORKEY(BYTE)
857 case 2: COPYBOX_COLORKEY(WORD)
858 case 4: COPYBOX_COLORKEY(DWORD)
860 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
861 ret = DDERR_UNSUPPORTED;
864 #undef COPYBOX_COLORKEY
868 for (y = 0; y < h; y++) {
869 memcpy(dbuf, sbuf, width);
870 sbuf += sdesc.u1.lPitch;
871 dbuf += ddesc.u1.lPitch;
875 IDirectDrawSurface7_Unlock(iface, NULL);
876 IDirectDrawSurface7_Unlock(src, NULL);
880 /* ChangeUniquenessValue: generic */
881 /* DeleteAttachedSurface: generic */
882 /* EnumAttachedSurfaces: generic */
883 /* EnumOverlayZOrders: generic, unimplemented */
885 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
886 IDirectDrawSurfaceImpl* back,
889 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
890 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
892 TRACE("(%p,%p)\n",front,back);
896 tmp = front_priv->dib.DIBsection;
897 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
898 back_priv->dib.DIBsection = tmp;
903 tmp = front_priv->dib.bitmap_data;
904 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
905 back_priv->dib.bitmap_data = tmp;
907 tmp = front->surface_desc.lpSurface;
908 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
909 back->surface_desc.lpSurface = tmp;
912 /* client_memory should not be different, but just in case */
915 tmp = front_priv->dib.client_memory;
916 front_priv->dib.client_memory = back_priv->dib.client_memory;
917 back_priv->dib.client_memory = tmp;
920 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
924 /* FreePrivateData: generic */
925 /* GetAttachedSurface: generic */
926 /* GetBltStatus: generic */
927 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
928 /* GetClipper: generic */
929 /* GetColorKey: generic */
931 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
933 DIB_PRIV_VAR(priv, This);
936 TRACE("Grabbing a DC for surface: %p\n", This);
938 hDC = CreateCompatibleDC(0);
939 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
941 SelectPalette(hDC, This->palette->hpal, FALSE);
948 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
950 DIB_PRIV_VAR(priv, This);
952 TRACE("Releasing DC for surface: %p\n", This);
954 SelectObject(hDC, priv->dib.holdbitmap);
960 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
962 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
965 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
967 return DIB_DirectDrawSurface_free_dc(This, hDC);
970 /* GetDDInterface: generic */
971 /* GetFlipStatus: generic */
972 /* GetLOD: generic */
973 /* GetOverlayPosition: generic */
974 /* GetPalette: generic */
975 /* GetPixelFormat: generic */
976 /* GetPriority: generic */
977 /* GetPrivateData: generic */
978 /* GetSurfaceDesc: generic */
979 /* GetUniquenessValue: generic */
980 /* Initialize: generic */
981 /* IsLost: generic */
982 /* Lock: generic with callback? */
983 /* PageLock: generic */
984 /* PageUnlock: generic */
987 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
989 TRACE("(%p)\n",iface);
990 return DD_OK; /* ??? */
993 /* SetClipper: generic */
994 /* SetColorKey: generic */
995 /* SetLOD: generic */
996 /* SetOverlayPosition: generic */
998 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
999 IDirectDrawPaletteImpl* pal)
1002 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1003 This->update_palette(This, pal,
1004 0, pal->palNumEntries,
1008 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1009 IDirectDrawPaletteImpl* pal,
1010 DWORD dwStart, DWORD dwCount,
1011 LPPALETTEENTRY palent)
1017 TRACE("updating primary palette\n");
1018 for (n=0; n<dwCount; n++) {
1019 col[n].rgbRed = palent[n].peRed;
1020 col[n].rgbGreen = palent[n].peGreen;
1021 col[n].rgbBlue = palent[n].peBlue;
1022 col[n].rgbReserved = 0;
1024 This->get_dc(This, &dc);
1025 SetDIBColorTable(dc, dwStart, dwCount, col);
1026 This->release_dc(This, dc);
1028 /* Propagate change to backbuffers if there are any */
1029 /* Basically this is a modification of the Flip code to find the backbuffer */
1030 /* and duplicate the palette update there as well */
1031 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1032 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1034 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1035 LPDIRECTDRAWSURFACE7 tgt;
1037 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1041 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1042 IDirectDrawSurface7,tgt);
1043 IDirectDrawSurface7_Release(tgt);
1044 target->get_dc(target, &dc);
1045 SetDIBColorTable(dc, dwStart, dwCount, col);
1046 target->release_dc(target, dc);
1051 /* SetPalette: generic */
1052 /* SetPriority: generic */
1053 /* SetPrivateData: generic */
1056 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1057 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1059 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1060 DIB_PRIV_VAR(priv, This);
1062 DWORD flags = pDDSD->dwFlags;
1064 if (TRACE_ON(ddraw)) {
1065 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1066 DDRAW_dump_surface_desc(pDDSD);
1069 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1070 flags &= ~DDSD_PIXELFORMAT;
1071 if (flags & DDSD_LPSURFACE) {
1072 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1074 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1077 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1078 HBITMAP oldbmp = priv->dib.DIBsection;
1079 LPVOID oldsurf = This->surface_desc.lpSurface;
1080 BOOL oldc = priv->dib.client_memory;
1082 flags &= ~DDSD_LPSURFACE;
1084 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1085 This->surface_desc.lpSurface = pDDSD->lpSurface;
1086 priv->dib.client_memory = TRUE;
1088 hr = create_dib(This);
1091 priv->dib.DIBsection = oldbmp;
1092 This->surface_desc.lpSurface = oldsurf;
1093 priv->dib.client_memory = oldc;
1097 DeleteObject(oldbmp);
1100 VirtualFree(oldsurf, 0, MEM_RELEASE);
1103 WARN("Unhandled flags : %08lx\n", flags);
1108 /* Unlock: ???, need callback */
1109 /* UpdateOverlay: generic */
1110 /* UpdateOverlayDisplay: generic */
1111 /* UpdateOverlayZOrder: generic */
1113 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1115 Main_DirectDrawSurface_QueryInterface,
1116 Main_DirectDrawSurface_AddRef,
1117 Main_DirectDrawSurface_Release,
1118 Main_DirectDrawSurface_AddAttachedSurface,
1119 Main_DirectDrawSurface_AddOverlayDirtyRect,
1120 DIB_DirectDrawSurface_Blt,
1121 Main_DirectDrawSurface_BltBatch,
1122 DIB_DirectDrawSurface_BltFast,
1123 Main_DirectDrawSurface_DeleteAttachedSurface,
1124 Main_DirectDrawSurface_EnumAttachedSurfaces,
1125 Main_DirectDrawSurface_EnumOverlayZOrders,
1126 Main_DirectDrawSurface_Flip,
1127 Main_DirectDrawSurface_GetAttachedSurface,
1128 Main_DirectDrawSurface_GetBltStatus,
1129 Main_DirectDrawSurface_GetCaps,
1130 Main_DirectDrawSurface_GetClipper,
1131 Main_DirectDrawSurface_GetColorKey,
1132 Main_DirectDrawSurface_GetDC,
1133 Main_DirectDrawSurface_GetFlipStatus,
1134 Main_DirectDrawSurface_GetOverlayPosition,
1135 Main_DirectDrawSurface_GetPalette,
1136 Main_DirectDrawSurface_GetPixelFormat,
1137 Main_DirectDrawSurface_GetSurfaceDesc,
1138 Main_DirectDrawSurface_Initialize,
1139 Main_DirectDrawSurface_IsLost,
1140 Main_DirectDrawSurface_Lock,
1141 Main_DirectDrawSurface_ReleaseDC,
1142 DIB_DirectDrawSurface_Restore,
1143 Main_DirectDrawSurface_SetClipper,
1144 Main_DirectDrawSurface_SetColorKey,
1145 Main_DirectDrawSurface_SetOverlayPosition,
1146 Main_DirectDrawSurface_SetPalette,
1147 Main_DirectDrawSurface_Unlock,
1148 Main_DirectDrawSurface_UpdateOverlay,
1149 Main_DirectDrawSurface_UpdateOverlayDisplay,
1150 Main_DirectDrawSurface_UpdateOverlayZOrder,
1151 Main_DirectDrawSurface_GetDDInterface,
1152 Main_DirectDrawSurface_PageLock,
1153 Main_DirectDrawSurface_PageUnlock,
1154 DIB_DirectDrawSurface_SetSurfaceDesc,
1155 Main_DirectDrawSurface_SetPrivateData,
1156 Main_DirectDrawSurface_GetPrivateData,
1157 Main_DirectDrawSurface_FreePrivateData,
1158 Main_DirectDrawSurface_GetUniquenessValue,
1159 Main_DirectDrawSurface_ChangeUniquenessValue,
1160 Main_DirectDrawSurface_SetPriority,
1161 Main_DirectDrawSurface_GetPriority,
1162 Main_DirectDrawSurface_SetLOD,
1163 Main_DirectDrawSurface_GetLOD