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 = (ULONG_PTR)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;
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 /* First check for the validity of source / destination rectangles. This was
399 verified using a test application + by MSDN.
402 ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
403 (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
404 (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
405 (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
406 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
407 WARN("Application gave us bad source rectangle for Blt.\n");
408 return DDERR_INVALIDRECT;
410 /* For the Destination rect, it can be out of bounds on the condition that a clipper
411 is set for the given surface.
413 if ((This->clipper == NULL) &&
414 ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
415 (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
416 (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
417 (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
418 (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
419 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
420 return DDERR_INVALIDRECT;
423 /* Now handle negative values in the rectangles. Warning: only supported for now
424 in the 'simple' cases (ie not in any stretching / rotation cases).
426 First, the case where nothing is to be done.
428 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
430 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
432 TRACE("Nothing to be done !\n");
436 /* The easy case : the source-less blits.... */
439 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
443 full_rect.right = ddesc.dwWidth;
444 full_rect.bottom = ddesc.dwHeight;
445 IntersectRect(&temp_rect, &full_rect, &xdst);
448 /* Only handle clipping on the destination rectangle */
449 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth );
450 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight);
451 if (clip_vert || clip_horiz) {
452 /* Now check if this is a special case or not... */
453 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
454 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
455 (dwFlags & DDBLT_DDFX)) {
456 WARN("Out of screen rectangle in special case. Not handled right now.\n");
461 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
462 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
465 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
466 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
468 /* And check if after clipping something is still to be done... */
469 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
470 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
471 TRACE("Nothing to be done after clipping !\n");
477 bpp = GET_BPP(ddesc);
478 srcheight = xsrc.bottom - xsrc.top;
479 srcwidth = xsrc.right - xsrc.left;
480 dstheight = xdst.bottom - xdst.top;
481 dstwidth = xdst.right - xdst.left;
482 width = (xdst.right - xdst.left) * bpp;
484 assert(width <= ddesc.u1.lPitch);
486 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
488 if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
490 static BOOL displayed = FALSE;
493 FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
496 dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
499 /* First, all the 'source-less' blits */
500 if (dwFlags & DDBLT_COLORFILL) {
501 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
502 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
503 dwFlags &= ~DDBLT_COLORFILL;
506 if (dwFlags & DDBLT_DEPTHFILL)
507 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
508 if (dwFlags & DDBLT_ROP) {
509 /* Catch some degenerate cases here */
510 switch(lpbltfx->dwROP) {
512 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
514 case 0xAA0029: /* No-op */
517 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
519 case SRCCOPY: /* well, we do that below ? */
522 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
525 dwFlags &= ~DDBLT_ROP;
527 if (dwFlags & DDBLT_DDROPS) {
528 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
530 /* Now the 'with source' blits */
533 int sx, xinc, sy, yinc;
535 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
537 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
538 xinc = (srcwidth << 16) / dstwidth;
539 yinc = (srcheight << 16) / dstheight;
542 /* No effects, we can cheat here */
543 if (dstwidth == srcwidth) {
544 if (dstheight == srcheight) {
545 /* No stretching in either direction. This needs to be as
546 * fast as possible */
549 /* check for overlapping surfaces */
550 if (src != iface || xdst.top < xsrc.top ||
551 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
553 /* no overlap, or dst above src, so copy from top downwards */
554 for (y = 0; y < dstheight; y++)
556 memcpy(dbuf, sbuf, width);
557 sbuf += sdesc.u1.lPitch;
558 dbuf += ddesc.u1.lPitch;
561 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
563 sbuf += (sdesc.u1.lPitch*dstheight);
564 dbuf += (ddesc.u1.lPitch*dstheight);
565 for (y = 0; y < dstheight; y++)
567 sbuf -= sdesc.u1.lPitch;
568 dbuf -= ddesc.u1.lPitch;
569 memcpy(dbuf, sbuf, width);
572 else /* src and dst overlapping on the same line, use memmove */
574 for (y = 0; y < dstheight; y++)
576 memmove(dbuf, sbuf, width);
577 sbuf += sdesc.u1.lPitch;
578 dbuf += ddesc.u1.lPitch;
582 /* Stretching in Y direction only */
583 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
584 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
585 memcpy(dbuf, sbuf, width);
586 dbuf += ddesc.u1.lPitch;
590 /* Stretching in X direction */
592 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
593 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
595 if ((sy >> 16) == (last_sy >> 16)) {
596 /* this sourcerow is the same as last sourcerow -
597 * copy already stretched row
599 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
601 #define STRETCH_ROW(type) { \
602 type *s = (type *) sbuf, *d = (type *) dbuf; \
603 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
604 d[x] = s[sx >> 16]; \
608 case 1: STRETCH_ROW(BYTE)
609 case 2: STRETCH_ROW(WORD)
610 case 4: STRETCH_ROW(DWORD)
613 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
617 pixel = (s[0]<<16)|(s[1]<<8)|s[2];
618 d[0] = (pixel>>16)&0xff;
619 d[1] = (pixel>> 8)&0xff;
620 d[2] = (pixel )&0xff;
626 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
627 ret = DDERR_UNSUPPORTED;
632 dbuf += ddesc.u1.lPitch;
637 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
638 DWORD keylow = 0, keyhigh = 0;
639 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
641 if (dwFlags & DDBLT_KEYSRC) {
642 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
643 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
644 } else if (dwFlags & DDBLT_KEYDEST){
645 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
646 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
647 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
648 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
649 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
651 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
652 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
654 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
657 if (dwFlags & DDBLT_DDFX) {
658 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
661 dTopRight = dbuf+((dstwidth-1)*bpp);
662 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
663 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
665 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
666 /* I don't think we need to do anything about this flag */
667 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
669 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
671 dTopRight = dTopLeft;
674 dBottomRight = dBottomLeft;
676 dstxinc = dstxinc *-1;
678 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
680 dTopLeft = dBottomLeft;
683 dTopRight = dBottomRight;
685 dstyinc = dstyinc *-1;
687 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
688 /* I don't think we need to do anything about this flag */
689 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
691 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
693 dBottomRight = dTopLeft;
696 dBottomLeft = dTopRight;
698 dstxinc = dstxinc * -1;
699 dstyinc = dstyinc * -1;
701 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
703 dTopLeft = dBottomLeft;
704 dBottomLeft = dBottomRight;
705 dBottomRight = dTopRight;
710 dstxinc = dstxinc * -1;
712 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
714 dTopLeft = dTopRight;
715 dTopRight = dBottomRight;
716 dBottomRight = dBottomLeft;
721 dstyinc = dstyinc * -1;
723 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
724 /* I don't think we need to do anything about this flag */
725 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
728 dwFlags &= ~(DDBLT_DDFX);
731 #define COPY_COLORKEY_FX(type) { \
732 type *s = (type *) sbuf, *d = (type *) dbuf, *dx, tmp; \
733 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
734 (LPBYTE)s = sbase + (sy >> 16) * sdesc.u1.lPitch; \
736 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
738 if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
739 (LPBYTE)dx += dstxinc; \
741 (LPBYTE)d += dstyinc; \
746 case 1: COPY_COLORKEY_FX(BYTE)
747 case 2: COPY_COLORKEY_FX(WORD)
748 case 4: COPY_COLORKEY_FX(DWORD)
749 case 3: {LPBYTE s,d = dbuf, dx;
750 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
751 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
753 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
756 pixel = (s[0]<<16)|(s[1]<<8)|s[2];
757 if (pixel < keylow || pixel > keyhigh){
758 dx[0] = (pixel>>16)&0xff;
759 dx[1] = (pixel>> 8)&0xff;
760 dx[2] = (pixel )&0xff;
768 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
769 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
770 ret = DDERR_UNSUPPORTED;
772 #undef COPY_COLORKEY_FX
778 if (dwFlags && FIXME_ON(ddraw)) {
779 FIXME("\tUnsupported flags: ");
780 DDRAW_dump_DDBLT(dwFlags);
784 IDirectDrawSurface7_Unlock(iface,NULL);
785 if (src) IDirectDrawSurface7_Unlock(src,NULL);
789 /* BltBatch: generic, unimplemented */
792 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
793 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
794 LPRECT rsrc, DWORD trans)
796 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
798 DDSURFACEDESC2 ddesc,sdesc;
804 if (TRACE_ON(ddraw)) {
805 FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
806 This,dstx,dsty,src,rsrc,trans
810 DDRAW_dump_DDBLTFAST(trans);
812 FIXME("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
814 FIXME(" srcrect: NULL\n");
817 /* We need to lock the surfaces, or we won't get refreshes when done. */
818 sdesc.dwSize = sizeof(sdesc);
819 IDirectDrawSurface7_Lock(src, NULL,&sdesc,DDLOCK_READONLY, 0);
820 ddesc.dwSize = sizeof(ddesc);
821 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
824 WARN("rsrc is NULL!\n");
826 rsrc->left = rsrc->top = 0;
827 rsrc->right = sdesc.dwWidth;
828 rsrc->bottom = sdesc.dwHeight;
831 bpp = GET_BPP(This->surface_desc);
832 sbuf = (BYTE *)sdesc.lpSurface+(rsrc->top*sdesc.u1.lPitch)+rsrc->left*bpp;
833 dbuf = (BYTE *)ddesc.lpSurface+(dsty*ddesc.u1.lPitch)+dstx* bpp;
836 h=rsrc->bottom-rsrc->top;
837 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
838 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
841 w=rsrc->right-rsrc->left;
842 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
843 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
846 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
847 DWORD keylow, keyhigh;
848 if (trans & DDBLTFAST_SRCCOLORKEY) {
849 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
850 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
852 /* I'm not sure if this is correct */
853 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
854 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
855 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
858 #define COPYBOX_COLORKEY(type) { \
859 type *d = (type *)dbuf, *s = (type *)sbuf, tmp; \
860 s = (type *) ((BYTE *) sdesc.lpSurface + (rsrc->top * sdesc.u1.lPitch) + rsrc->left * bpp); \
861 d = (type *) ((BYTE *) ddesc.lpSurface + (dsty * ddesc.u1.lPitch) + dstx * bpp); \
862 for (y = 0; y < h; y++) { \
863 for (x = 0; x < w; x++) { \
865 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
867 (LPBYTE)s += sdesc.u1.lPitch; \
868 (LPBYTE)d += ddesc.u1.lPitch; \
874 case 1: COPYBOX_COLORKEY(BYTE)
875 case 2: COPYBOX_COLORKEY(WORD)
876 case 4: COPYBOX_COLORKEY(DWORD)
878 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
879 ret = DDERR_UNSUPPORTED;
882 #undef COPYBOX_COLORKEY
886 for (y = 0; y < h; y++) {
887 memcpy(dbuf, sbuf, width);
888 sbuf += sdesc.u1.lPitch;
889 dbuf += ddesc.u1.lPitch;
893 IDirectDrawSurface7_Unlock(iface, NULL);
894 IDirectDrawSurface7_Unlock(src, NULL);
898 /* ChangeUniquenessValue: generic */
899 /* DeleteAttachedSurface: generic */
900 /* EnumAttachedSurfaces: generic */
901 /* EnumOverlayZOrders: generic, unimplemented */
903 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
904 IDirectDrawSurfaceImpl* back,
907 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
908 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
910 TRACE("(%p,%p)\n",front,back);
914 tmp = front_priv->dib.DIBsection;
915 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
916 back_priv->dib.DIBsection = tmp;
921 tmp = front_priv->dib.bitmap_data;
922 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
923 back_priv->dib.bitmap_data = tmp;
925 tmp = front->surface_desc.lpSurface;
926 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
927 back->surface_desc.lpSurface = tmp;
930 /* client_memory should not be different, but just in case */
933 tmp = front_priv->dib.client_memory;
934 front_priv->dib.client_memory = back_priv->dib.client_memory;
935 back_priv->dib.client_memory = tmp;
938 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
942 /* FreePrivateData: generic */
943 /* GetAttachedSurface: generic */
944 /* GetBltStatus: generic */
945 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
946 /* GetClipper: generic */
947 /* GetColorKey: generic */
949 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
951 DIB_PRIV_VAR(priv, This);
954 TRACE("Grabbing a DC for surface: %p\n", This);
956 hDC = CreateCompatibleDC(0);
957 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
959 SelectPalette(hDC, This->palette->hpal, FALSE);
966 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
968 DIB_PRIV_VAR(priv, This);
970 TRACE("Releasing DC for surface: %p\n", This);
972 SelectObject(hDC, priv->dib.holdbitmap);
978 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
980 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
983 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
985 return DIB_DirectDrawSurface_free_dc(This, hDC);
988 /* GetDDInterface: generic */
989 /* GetFlipStatus: generic */
990 /* GetLOD: generic */
991 /* GetOverlayPosition: generic */
992 /* GetPalette: generic */
993 /* GetPixelFormat: generic */
994 /* GetPriority: generic */
995 /* GetPrivateData: generic */
996 /* GetSurfaceDesc: generic */
997 /* GetUniquenessValue: generic */
998 /* Initialize: generic */
999 /* IsLost: generic */
1000 /* Lock: generic with callback? */
1001 /* PageLock: generic */
1002 /* PageUnlock: generic */
1005 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1007 TRACE("(%p)\n",iface);
1008 return DD_OK; /* ??? */
1011 /* SetClipper: generic */
1012 /* SetColorKey: generic */
1013 /* SetLOD: generic */
1014 /* SetOverlayPosition: generic */
1016 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1017 IDirectDrawPaletteImpl* pal)
1020 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1021 This->update_palette(This, pal,
1022 0, pal->palNumEntries,
1026 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1027 IDirectDrawPaletteImpl* pal,
1028 DWORD dwStart, DWORD dwCount,
1029 LPPALETTEENTRY palent)
1035 TRACE("updating primary palette\n");
1036 for (n=0; n<dwCount; n++) {
1037 col[n].rgbRed = palent[n].peRed;
1038 col[n].rgbGreen = palent[n].peGreen;
1039 col[n].rgbBlue = palent[n].peBlue;
1040 col[n].rgbReserved = 0;
1042 This->get_dc(This, &dc);
1043 SetDIBColorTable(dc, dwStart, dwCount, col);
1044 This->release_dc(This, dc);
1046 /* Propagate change to backbuffers if there are any */
1047 /* Basically this is a modification of the Flip code to find the backbuffer */
1048 /* and duplicate the palette update there as well */
1049 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1050 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1052 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1053 LPDIRECTDRAWSURFACE7 tgt;
1055 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1059 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1060 IDirectDrawSurface7,tgt);
1061 IDirectDrawSurface7_Release(tgt);
1062 target->get_dc(target, &dc);
1063 SetDIBColorTable(dc, dwStart, dwCount, col);
1064 target->release_dc(target, dc);
1069 /* SetPalette: generic */
1070 /* SetPriority: generic */
1071 /* SetPrivateData: generic */
1074 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1075 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1077 ICOM_THIS(IDirectDrawSurfaceImpl,iface);
1078 DIB_PRIV_VAR(priv, This);
1080 DWORD flags = pDDSD->dwFlags;
1082 if (TRACE_ON(ddraw)) {
1083 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1084 DDRAW_dump_surface_desc(pDDSD);
1087 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1088 flags &= ~DDSD_PIXELFORMAT;
1089 if (flags & DDSD_LPSURFACE) {
1090 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1092 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1095 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1096 HBITMAP oldbmp = priv->dib.DIBsection;
1097 LPVOID oldsurf = This->surface_desc.lpSurface;
1098 BOOL oldc = priv->dib.client_memory;
1100 flags &= ~DDSD_LPSURFACE;
1102 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1103 This->surface_desc.lpSurface = pDDSD->lpSurface;
1104 priv->dib.client_memory = TRUE;
1106 hr = create_dib(This);
1109 priv->dib.DIBsection = oldbmp;
1110 This->surface_desc.lpSurface = oldsurf;
1111 priv->dib.client_memory = oldc;
1115 DeleteObject(oldbmp);
1118 VirtualFree(oldsurf, 0, MEM_RELEASE);
1121 WARN("Unhandled flags : %08lx\n", flags);
1126 /* Unlock: ???, need callback */
1127 /* UpdateOverlay: generic */
1128 /* UpdateOverlayDisplay: generic */
1129 /* UpdateOverlayZOrder: generic */
1131 static ICOM_VTABLE(IDirectDrawSurface7) DIB_IDirectDrawSurface7_VTable =
1133 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1134 Main_DirectDrawSurface_QueryInterface,
1135 Main_DirectDrawSurface_AddRef,
1136 Main_DirectDrawSurface_Release,
1137 Main_DirectDrawSurface_AddAttachedSurface,
1138 Main_DirectDrawSurface_AddOverlayDirtyRect,
1139 DIB_DirectDrawSurface_Blt,
1140 Main_DirectDrawSurface_BltBatch,
1141 DIB_DirectDrawSurface_BltFast,
1142 Main_DirectDrawSurface_DeleteAttachedSurface,
1143 Main_DirectDrawSurface_EnumAttachedSurfaces,
1144 Main_DirectDrawSurface_EnumOverlayZOrders,
1145 Main_DirectDrawSurface_Flip,
1146 Main_DirectDrawSurface_GetAttachedSurface,
1147 Main_DirectDrawSurface_GetBltStatus,
1148 Main_DirectDrawSurface_GetCaps,
1149 Main_DirectDrawSurface_GetClipper,
1150 Main_DirectDrawSurface_GetColorKey,
1151 Main_DirectDrawSurface_GetDC,
1152 Main_DirectDrawSurface_GetFlipStatus,
1153 Main_DirectDrawSurface_GetOverlayPosition,
1154 Main_DirectDrawSurface_GetPalette,
1155 Main_DirectDrawSurface_GetPixelFormat,
1156 Main_DirectDrawSurface_GetSurfaceDesc,
1157 Main_DirectDrawSurface_Initialize,
1158 Main_DirectDrawSurface_IsLost,
1159 Main_DirectDrawSurface_Lock,
1160 Main_DirectDrawSurface_ReleaseDC,
1161 DIB_DirectDrawSurface_Restore,
1162 Main_DirectDrawSurface_SetClipper,
1163 Main_DirectDrawSurface_SetColorKey,
1164 Main_DirectDrawSurface_SetOverlayPosition,
1165 Main_DirectDrawSurface_SetPalette,
1166 Main_DirectDrawSurface_Unlock,
1167 Main_DirectDrawSurface_UpdateOverlay,
1168 Main_DirectDrawSurface_UpdateOverlayDisplay,
1169 Main_DirectDrawSurface_UpdateOverlayZOrder,
1170 Main_DirectDrawSurface_GetDDInterface,
1171 Main_DirectDrawSurface_PageLock,
1172 Main_DirectDrawSurface_PageUnlock,
1173 DIB_DirectDrawSurface_SetSurfaceDesc,
1174 Main_DirectDrawSurface_SetPrivateData,
1175 Main_DirectDrawSurface_GetPrivateData,
1176 Main_DirectDrawSurface_FreePrivateData,
1177 Main_DirectDrawSurface_GetUniquenessValue,
1178 Main_DirectDrawSurface_ChangeUniquenessValue,
1179 Main_DirectDrawSurface_SetPriority,
1180 Main_DirectDrawSurface_GetPriority,
1181 Main_DirectDrawSurface_SetLOD,
1182 Main_DirectDrawSurface_GetLOD