2 * 2D Surface implementation without OpenGL
4 * Copyright 1997-2000 Marcus Meissner
5 * Copyright 1998-2000 Lionel Ulmer
6 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * Copyright 2002-2005 Jason Edmeades
8 * Copyright 2002-2003 Raphael Junqueira
9 * Copyright 2004 Christian Costa
10 * Copyright 2005 Oliver Stieber
11 * Copyright 2006 Stefan Dösinger
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
30 #include "wined3d_private.h"
35 /* Use the d3d_surface debug channel to have one channel for all surfaces */
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
37 WINE_DECLARE_DEBUG_CHANNEL(fps);
39 /*****************************************************************************
42 * Helper function that blts the front buffer contents to the target window
45 * This: Surface to copy from
46 * rc: Rectangle to copy
48 *****************************************************************************/
50 x11_copy_to_screen(IWineD3DSurfaceImpl *This,
53 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
60 TRACE("(%p)->(%p): Copying to screen\n", This, rc);
62 hSurfaceDC = This->hDC;
64 hDisplayWnd = This->resource.wineD3DDevice->ddraw_window;
65 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
68 TRACE(" copying rect (%ld,%ld)->(%ld,%ld), offset (%ld,%ld)\n",
69 rc->left, rc->top, rc->right, rc->bottom, offset.x, offset.y);
72 /* FIXME: this doesn't work... if users really want to run
73 * X in 8bpp, then we need to call directly into display.drv
74 * (or Wine's equivalent), and force a private colormap
75 * without default entries. */
77 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
78 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
82 drawrect.right = This->currentDesc.Width;
84 drawrect.bottom = This->currentDesc.Height;
87 /* TODO: Support clippers */
91 HWND hwnd = This->clipper->hWnd;
92 if (hwnd && GetClientRect(hwnd,&xrc))
94 OffsetRect(&xrc,offset.x,offset.y);
95 IntersectRect(&drawrect,&drawrect,&xrc);
101 IntersectRect(&drawrect,&drawrect,rc);
105 /* Only use this if the caller did not pass a rectangle, since
106 * due to double locking this could be the wrong one ...
108 if (This->lockedRect.left != This->lockedRect.right)
110 IntersectRect(&drawrect,&drawrect,&This->lockedRect);
115 drawrect.left-offset.x, drawrect.top-offset.y,
116 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
118 drawrect.left, drawrect.top,
120 ReleaseDC(hDisplayWnd, hDisplayDC);
124 /*****************************************************************************
125 * IWineD3DSurface::PreLoad, GDI version
127 * This call is unsupported on GDI surfaces, if it's called something went
128 * wrong in the parent library. Write an informative warning
130 *****************************************************************************/
132 IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
134 ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
135 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
136 ERR("(%p): Please report to wine-devel\n", iface);
139 /*****************************************************************************
140 * IWineD3DSurface::LockRect, GDI version
142 * Locks the surface and returns a pointer to the surface memory
145 * pLockedRect: Address to return the locking info at
146 * pRect: Rectangle to lock
150 * WINED3D_OK on success
151 * WINED3DERR_INVALIDCALL on errors
153 *****************************************************************************/
154 static HRESULT WINAPI
155 IWineGDISurfaceImpl_LockRect(IWineD3DSurface *iface,
156 WINED3DLOCKED_RECT* pLockedRect,
160 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
162 /* Already locked? */
163 if(This->Flags & SFLAG_LOCKED)
165 ERR("(%p) Surface already locked\n", This);
166 /* What should I return here? */
167 return D3DERR_INVALIDCALL;
170 if (!(This->Flags & SFLAG_LOCKABLE))
172 /* This is some GL specific thing, see the OpenGL version of
173 * this method, but check for the flag and write a trace
175 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
178 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n",
179 This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
181 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
185 pLockedRect->pBits = This->resource.allocatedMemory;
186 This->lockedRect.left = 0;
187 This->lockedRect.top = 0;
188 This->lockedRect.right = This->currentDesc.Width;
189 This->lockedRect.bottom = This->currentDesc.Height;
191 TRACE("Locked Rect (%p) = l %ld, t %ld, r %ld, b %ld\n",
192 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
193 This->lockedRect.right, This->lockedRect.bottom);
197 TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n",
198 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
200 if ((pRect->top < 0) ||
202 (pRect->left >= pRect->right) ||
203 (pRect->top >= pRect->bottom) ||
204 (pRect->right > This->currentDesc.Width) ||
205 (pRect->bottom > This->currentDesc.Height))
207 WARN(" Invalid values in pRect !!!\n");
208 return D3DERR_INVALIDCALL;
211 if (This->resource.format == WINED3DFMT_DXT1)
213 /* DXT1 is half byte per pixel */
214 pLockedRect->pBits = This->resource.allocatedMemory +
215 (pLockedRect->Pitch * pRect->top) +
216 ((pRect->left * This->bytesPerPixel / 2));
220 pLockedRect->pBits = This->resource.allocatedMemory +
221 (pLockedRect->Pitch * pRect->top) +
222 (pRect->left * This->bytesPerPixel);
224 This->lockedRect.left = pRect->left;
225 This->lockedRect.top = pRect->top;
226 This->lockedRect.right = pRect->right;
227 This->lockedRect.bottom = pRect->bottom;
230 /* No dirtifying is needed for this surface implementation */
231 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
233 This->Flags |= SFLAG_LOCKED;
237 /*****************************************************************************
238 * IWineD3DSurface::UnlockRect, GDI version
240 * Unlocks a surface. This implementation doesn't do much, except updating
241 * the window if the front buffer is unlocked
244 * WINED3D_OK on success
245 * WINED3DERR_INVALIDCALL on failure
247 *****************************************************************************/
248 static HRESULT WINAPI
249 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
251 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
252 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
253 TRACE("(%p)\n", This);
255 if (!(This->Flags & SFLAG_LOCKED))
257 WARN("trying to Unlock an unlocked surf@%p\n", This);
258 return WINED3DERR_INVALIDCALL;
261 /* Can be useful for debugging */
264 static unsigned int gen = 0;
267 if ((gen % 10) == 0) {
268 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
269 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
272 * debugging crash code
281 /* Update the screen */
282 if(This == (IWineD3DSurfaceImpl *) dev->ddraw_primary)
284 x11_copy_to_screen(This, &This->lockedRect);
287 This->Flags &= ~SFLAG_LOCKED;
288 memset(&This->lockedRect, 0, sizeof(RECT));
292 /*****************************************************************************
293 * IWineD3DSurface::Flip, GDI version
295 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
296 * the parent library. This implementation changes the data pointers of the
297 * surfaces and copies the new front buffer content to the screen
300 * override: Flipping target(e.g. back buffer)
303 * WINED3D_OK on success
305 *****************************************************************************/
306 static HRESULT WINAPI
307 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
308 IWineD3DSurface *override,
311 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
312 IWineD3DSurfaceImpl *Target = (IWineD3DSurfaceImpl *) override;
313 TRACE("(%p)->(%p,%lx)\n", This, override, Flags);
315 TRACE("(%p) Flipping to surface %p\n", This, Target);
319 ERR("(%p): Can't flip without a target\n", This);
320 return WINED3DERR_INVALIDCALL;
327 This->hDC = Target->hDC;
331 /* Flip the DIBsection */
334 tmp = This->dib.DIBsection;
335 This->dib.DIBsection = Target->dib.DIBsection;
336 Target->dib.DIBsection = tmp;
339 /* Flip the surface data */
343 tmp = This->dib.bitmap_data;
344 This->dib.bitmap_data = Target->dib.bitmap_data;
345 Target->dib.bitmap_data = tmp;
347 tmp = This->resource.allocatedMemory;
348 This->resource.allocatedMemory = Target->resource.allocatedMemory;
349 Target->resource.allocatedMemory = tmp;
352 /* client_memory should not be different, but just in case */
355 tmp = This->dib.client_memory;
356 This->dib.client_memory = Target->dib.client_memory;
357 Target->dib.client_memory = tmp;
360 /* Useful for debugging */
363 static unsigned int gen = 0;
366 if ((gen % 10) == 0) {
367 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
368 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
371 * debugging crash code
380 /* Update the screen */
381 x11_copy_to_screen(This, NULL);
386 static long prev_time, frames;
388 DWORD time = GetTickCount();
390 /* every 1.5 seconds */
391 if (time - prev_time > 1500) {
392 TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
401 /*****************************************************************************
404 * Helper function that fills a memory area with a specific color
407 * buf: memory address to start filling at
408 * width, height: Dimensions of the area to fill
409 * bpp: Bit depth of the surface
410 * lPitch: pitch of the surface
411 * color: Color to fill with
413 *****************************************************************************/
415 _Blt_ColorFill(BYTE *buf,
416 int width, int height,
417 int bpp, LONG lPitch,
425 #define COLORFILL_ROW(type) \
427 type *d = (type *) buf; \
428 for (x = 0; x < width; x++) \
429 d[x] = (type) color; \
434 case 1: COLORFILL_ROW(BYTE)
435 case 2: COLORFILL_ROW(WORD)
438 BYTE *d = (BYTE *) buf;
439 for (x = 0; x < width; x++,d+=3)
441 d[0] = (color ) & 0xFF;
442 d[1] = (color>> 8) & 0xFF;
443 d[2] = (color>>16) & 0xFF;
447 case 4: COLORFILL_ROW(DWORD)
449 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
450 return DDERR_UNSUPPORTED;
455 /* Now copy first row */
457 for (y = 1; y < height; y++)
460 memcpy(buf, first, width * bpp);
465 /*****************************************************************************
466 * IWineD3DSurface::Blt, GDI version
468 * Performs blits to a surface, eigher from a source of source-less blts
469 * This is the main functionality of DirectDraw
472 * DestRect: Destination rectangle to write to
473 * SrcSurface: Source surface, can be NULL
474 * SrcRect: Source rectangle
475 *****************************************************************************/
477 IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface,
479 IWineD3DSurface *SrcSurface,
484 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
485 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
488 WINED3DLOCKED_RECT dlock, slock;
489 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
490 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
492 const PixelFormatDesc *sEntry, *dEntry;
494 TRACE("(%p)->(%p,%p,%p,%lx,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
496 if (TRACE_ON(d3d_surface))
498 if (DestRect) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",
499 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
500 if (SrcRect) TRACE("\tsrcrect :%ldx%ld-%ldx%ld\n",
501 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
504 DDRAW_dump_DDBLT(Flags);
505 if (Flags & DDBLT_DDFX)
508 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
513 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
515 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
516 return DDERR_SURFACEBUSY;
521 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
522 dfmt = This->resource.format;
525 sEntry = getFormatDescEntry(sfmt);
532 IWineD3DSurface_LockRect(SrcSurface, &slock, NULL, D3DLOCK_READONLY);
533 sfmt = Src->resource.format;
535 sEntry = getFormatDescEntry(sfmt);
536 dfmt = This->resource.format;
537 dEntry = getFormatDescEntry(dfmt);
538 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
541 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~DDBLT_DDFX;
543 if (sEntry->isFourcc && dEntry->isFourcc)
547 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
548 ret = DDERR_INVALIDPIXELFORMAT;
551 TRACE("Fourcc->Fourcc copy)\n");
552 memcpy(dlock.pBits, slock.pBits, This->currentDesc.Height * dlock.Pitch);
556 if (sEntry->isFourcc && !dEntry->isFourcc)
558 FIXME("DXTC decompression not supported right now\n");
564 memcpy(&xdst,DestRect,sizeof(xdst));
569 xdst.bottom = This->currentDesc.Height;
571 xdst.right = This->currentDesc.Width;
576 memcpy(&xsrc,SrcRect,sizeof(xsrc));
583 xsrc.bottom = Src->currentDesc.Height;
585 xsrc.right = Src->currentDesc.Width;
589 memset(&xsrc,0,sizeof(xsrc));
593 /* First check for the validity of source / destination rectangles. This was
594 verified using a test application + by MSDN.
597 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
598 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
599 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
600 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
601 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
603 WARN("Application gave us bad source rectangle for Blt.\n");
604 ret = DDERR_INVALIDRECT;
607 /* For the Destination rect, it can be out of bounds on the condition that a clipper
608 is set for the given surface.
610 if ((/*This->clipper == NULL*/ TRUE) &&
611 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
612 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
613 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
614 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
615 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
617 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
618 ret = DDERR_INVALIDRECT;
622 /* Now handle negative values in the rectangles. Warning: only supported for now
623 in the 'simple' cases (ie not in any stretching / rotation cases).
625 First, the case where nothing is to be done.
627 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
628 (xdst.top >= (int) This->currentDesc.Height) ||
629 (xdst.left >= (int) This->currentDesc.Width)) ||
631 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
632 (xsrc.top >= (int) Src->currentDesc.Height) ||
633 (xsrc.left >= (int) Src->currentDesc.Width)) ))
635 TRACE("Nothing to be done !\n");
639 /* The easy case : the source-less blits.... */
643 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
647 full_rect.right = This->currentDesc.Width;
648 full_rect.bottom = This->currentDesc.Height;
649 IntersectRect(&temp_rect, &full_rect, &xdst);
654 /* Only handle clipping on the destination rectangle */
655 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
656 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
657 if (clip_vert || clip_horiz)
659 /* Now check if this is a special case or not... */
660 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
661 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
662 (Flags & DDBLT_DDFX))
664 WARN("Out of screen rectangle in special case. Not handled right now.\n");
670 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
671 if (xdst.right > This->currentDesc.Width)
673 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
674 xdst.right = (int) This->currentDesc.Width;
681 xsrc.top -= xdst.top;
684 if (xdst.bottom > This->currentDesc.Height)
686 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
687 xdst.bottom = (int) This->currentDesc.Height;
690 /* And check if after clipping something is still to be done... */
691 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
692 (xdst.top >= (int) This->currentDesc.Height) ||
693 (xdst.left >= (int) This->currentDesc.Width) ||
694 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
695 (xsrc.top >= (int) Src->currentDesc.Height) ||
696 (xsrc.left >= (int) Src->currentDesc.Width))
698 TRACE("Nothing to be done after clipping !\n");
704 bpp = This->bytesPerPixel;
705 srcheight = xsrc.bottom - xsrc.top;
706 srcwidth = xsrc.right - xsrc.left;
707 dstheight = xdst.bottom - xdst.top;
708 dstwidth = xdst.right - xdst.left;
709 width = (xdst.right - xdst.left) * bpp;
711 assert(width <= dlock.Pitch);
713 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
715 if (Flags & DDBLT_WAIT)
717 static BOOL displayed = FALSE;
719 FIXME("Can't handle DDBLT_WAIT flag right now.\n");
721 Flags &= ~DDBLT_WAIT;
723 if (Flags & DDBLT_ASYNC)
725 static BOOL displayed = FALSE;
727 FIXME("Can't handle DDBLT_ASYNC flag right now.\n");
729 Flags &= ~DDBLT_ASYNC;
731 if (Flags & DDBLT_DONOTWAIT)
733 /* DDBLT_DONOTWAIT appeared in DX7 */
734 static BOOL displayed = FALSE;
736 FIXME("Can't handle DDBLT_DONOTWAIT flag right now.\n");
738 Flags &= ~DDBLT_DONOTWAIT;
741 /* First, all the 'source-less' blits */
742 if (Flags & DDBLT_COLORFILL)
744 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
745 dlock.Pitch, DDBltFx->u5.dwFillColor);
746 Flags &= ~DDBLT_COLORFILL;
749 if (Flags & DDBLT_DEPTHFILL)
751 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
753 if (Flags & DDBLT_ROP)
755 /* Catch some degenerate cases here */
756 switch(DDBltFx->dwROP)
759 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
761 case 0xAA0029: /* No-op */
764 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
766 case SRCCOPY: /* well, we do that below ? */
769 FIXME("Unsupported raster op: %08lx Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
774 if (Flags & DDBLT_DDROPS)
776 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
778 /* Now the 'with source' blits */
782 int sx, xinc, sy, yinc;
784 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
786 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
787 xinc = (srcwidth << 16) / dstwidth;
788 yinc = (srcheight << 16) / dstheight;
792 /* No effects, we can cheat here */
793 if (dstwidth == srcwidth)
795 if (dstheight == srcheight)
797 /* No stretching in either direction. This needs to be as
798 * fast as possible */
801 /* check for overlapping surfaces */
802 if (SrcSurface != iface || xdst.top < xsrc.top ||
803 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
805 /* no overlap, or dst above src, so copy from top downwards */
806 for (y = 0; y < dstheight; y++)
808 memcpy(dbuf, sbuf, width);
813 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
815 sbuf += (slock.Pitch*dstheight);
816 dbuf += (dlock.Pitch*dstheight);
817 for (y = 0; y < dstheight; y++)
821 memcpy(dbuf, sbuf, width);
824 else /* src and dst overlapping on the same line, use memmove */
826 for (y = 0; y < dstheight; y++)
828 memmove(dbuf, sbuf, width);
834 /* Stretching in Y direction only */
835 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
836 sbuf = sbase + (sy >> 16) * slock.Pitch;
837 memcpy(dbuf, sbuf, width);
844 /* Stretching in X direction */
846 for (y = sy = 0; y < dstheight; y++, sy += yinc)
848 sbuf = sbase + (sy >> 16) * slock.Pitch;
850 if ((sy >> 16) == (last_sy >> 16))
852 /* this sourcerow is the same as last sourcerow -
853 * copy already stretched row
855 memcpy(dbuf, dbuf - dlock.Pitch, width);
859 #define STRETCH_ROW(type) { \
860 type *s = (type *) sbuf, *d = (type *) dbuf; \
861 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
862 d[x] = s[sx >> 16]; \
867 case 1: STRETCH_ROW(BYTE)
868 case 2: STRETCH_ROW(WORD)
869 case 4: STRETCH_ROW(DWORD)
873 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
878 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
879 d[0] = (pixel )&0xff;
880 d[1] = (pixel>> 8)&0xff;
881 d[2] = (pixel>>16)&0xff;
887 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
888 ret = DDERR_UNSUPPORTED;
900 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
901 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
902 if (Flags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE))
905 if (Flags & DDBLT_KEYSRC)
907 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
908 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
910 else if (Flags & DDBLT_KEYDEST)
912 keylow = This->DestBltCKey.dwColorSpaceLowValue;
913 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
915 else if (Flags & DDBLT_KEYSRCOVERRIDE)
917 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
918 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
922 keylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
923 keyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
931 keymask = sEntry->redMask |
935 Flags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
938 if (Flags & DDBLT_DDFX)
940 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
943 dTopRight = dbuf+((dstwidth-1)*bpp);
944 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
945 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
947 if (DDBltFx->dwDDFX & DDBLTFX_ARITHSTRETCHY)
949 /* I don't think we need to do anything about this flag */
950 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
952 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT)
955 dTopRight = dTopLeft;
958 dBottomRight = dBottomLeft;
960 dstxinc = dstxinc *-1;
962 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORUPDOWN)
965 dTopLeft = dBottomLeft;
968 dTopRight = dBottomRight;
970 dstyinc = dstyinc *-1;
972 if (DDBltFx->dwDDFX & DDBLTFX_NOTEARING)
974 /* I don't think we need to do anything about this flag */
975 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
977 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE180)
980 dBottomRight = dTopLeft;
983 dBottomLeft = dTopRight;
985 dstxinc = dstxinc * -1;
986 dstyinc = dstyinc * -1;
988 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE270)
991 dTopLeft = dBottomLeft;
992 dBottomLeft = dBottomRight;
993 dBottomRight = dTopRight;
998 dstxinc = dstxinc * -1;
1000 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE90)
1003 dTopLeft = dTopRight;
1004 dTopRight = dBottomRight;
1005 dBottomRight = dBottomLeft;
1010 dstyinc = dstyinc * -1;
1012 if (DDBltFx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST)
1014 /* I don't think we need to do anything about this flag */
1015 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
1018 Flags &= ~(DDBLT_DDFX);
1021 #define COPY_COLORKEY_FX(type) { \
1022 type *s, *d = (type *) dbuf, *dx, tmp; \
1023 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1024 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1026 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1027 tmp = s[sx >> 16]; \
1028 if ((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) dx[0] = tmp; \
1029 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1031 d = (type*)(((LPBYTE)d)+dstyinc); \
1036 case 1: COPY_COLORKEY_FX(BYTE)
1037 case 2: COPY_COLORKEY_FX(WORD)
1038 case 4: COPY_COLORKEY_FX(DWORD)
1041 LPBYTE s,d = dbuf, dx;
1042 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1044 sbuf = sbase + (sy >> 16) * slock.Pitch;
1046 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1049 s = sbuf+3*(sx>>16);
1050 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1051 if ((pixel & keymask) < keylow || (pixel & keymask) > keyhigh)
1053 dx[0] = (pixel )&0xff;
1054 dx[1] = (pixel>> 8)&0xff;
1055 dx[2] = (pixel>>16)&0xff;
1064 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1065 (Flags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1066 ret = DDERR_UNSUPPORTED;
1068 #undef COPY_COLORKEY_FX
1074 if (Flags && FIXME_ON(d3d_surface))
1076 FIXME("\tUnsupported flags: %08lx\n", Flags);
1080 IWineD3DSurface_UnlockRect(iface);
1081 if (SrcSurface && SrcSurface != iface) IWineD3DSurface_UnlockRect(SrcSurface);
1085 /*****************************************************************************
1086 * IWineD3DSurface::BltFast, GDI version
1088 * This is the software implementation of BltFast, as used by GDI surfaces
1089 * and as a fallback for OpenGL surfaces. This code is taken from the old
1090 * DirectDraw code, and was originally written by TransGaming.
1095 * Source: Source surface to copy from
1096 * rsrc: Source rectangle
1100 * WINED3D_OK on success
1102 *****************************************************************************/
1104 IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface,
1107 IWineD3DSurface *Source,
1111 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1112 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1114 int bpp, w, h, x, y;
1115 WINED3DLOCKED_RECT dlock,slock;
1116 HRESULT ret = DD_OK;
1118 RECT lock_src, lock_dst, lock_union;
1120 const PixelFormatDesc *sEntry, *dEntry;
1122 if (TRACE_ON(d3d_surface))
1124 TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n", This,dstx,dsty,Src,rsrc,trans);
1128 TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,
1129 rsrc->right,rsrc->bottom);
1133 TRACE(" srcrect: NULL\n");
1137 if ((This->Flags & SFLAG_LOCKED) ||
1138 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1140 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1141 return DDERR_SURFACEBUSY;
1146 WARN("rsrc is NULL!\n");
1150 rsrc->right = Src->currentDesc.Width;
1151 rsrc->bottom = Src->currentDesc.Height;
1154 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1155 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1156 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1157 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1158 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1159 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1161 WARN("Application gave us bad source rectangle for BltFast.\n");
1162 return DDERR_INVALIDRECT;
1165 h = rsrc->bottom - rsrc->top;
1166 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1167 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1168 if (h <= 0) return DDERR_INVALIDRECT;
1170 w = rsrc->right - rsrc->left;
1171 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1172 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1173 if (w <= 0) return DDERR_INVALIDRECT;
1175 /* Now compute the locking rectangle... */
1176 lock_src.left = rsrc->left;
1177 lock_src.top = rsrc->top;
1178 lock_src.right = lock_src.left + w;
1179 lock_src.bottom = lock_src.top + h;
1181 lock_dst.left = dstx;
1182 lock_dst.top = dsty;
1183 lock_dst.right = dstx + w;
1184 lock_dst.bottom = dsty + h;
1186 bpp = This->bytesPerPixel;
1188 /* We need to lock the surfaces, or we won't get refreshes when done. */
1193 UnionRect(&lock_union, &lock_src, &lock_dst);
1195 /* Lock the union of the two rectangles */
1196 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1197 if(ret != D3D_OK) goto error;
1199 pitch = dlock.Pitch;
1200 slock.Pitch = dlock.Pitch;
1202 /* Since slock was originally copied from this surface's description, we can just reuse it */
1203 assert(This->resource.allocatedMemory != NULL);
1204 sbuf = (BYTE *)This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1205 dbuf = (BYTE *)This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1206 sEntry = getFormatDescEntry(Src->resource.format);
1211 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, D3DLOCK_READONLY);
1212 if(ret != D3D_OK) goto error;
1213 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1214 if(ret != D3D_OK) goto error;
1218 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1220 sEntry = getFormatDescEntry(Src->resource.format);
1221 dEntry = getFormatDescEntry(This->resource.format);
1224 /* Handle first the FOURCC surfaces... */
1225 if (sEntry->isFourcc && dEntry->isFourcc)
1227 TRACE("Fourcc -> Fourcc copy\n");
1229 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1231 FIXME("offset for destination surface is not supported\n");
1232 if (Src->resource.format != This->resource.format)
1234 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1235 ret = DDERR_INVALIDPIXELFORMAT;
1238 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1239 memcpy(dbuf, sbuf, This->resource.size);
1242 if (sEntry->isFourcc && !dEntry->isFourcc)
1244 /* TODO: Use the libtxc_dxtn.so shared library to do
1245 * software decompression
1247 ERR("DXTC decompression not supported by now\n");
1251 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY))
1253 DWORD keylow, keyhigh;
1254 TRACE("Color keyed copy\n");
1255 if (trans & DDBLTFAST_SRCCOLORKEY)
1257 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1258 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1262 /* I'm not sure if this is correct */
1263 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1264 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1265 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1268 #define COPYBOX_COLORKEY(type) { \
1270 s = (type *) sbuf; \
1271 d = (type *) dbuf; \
1272 for (y = 0; y < h; y++) { \
1273 for (x = 0; x < w; x++) { \
1275 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1277 s = (type *)((BYTE *)s + slock.Pitch); \
1278 d = (type *)((BYTE *)d + dlock.Pitch); \
1284 case 1: COPYBOX_COLORKEY(BYTE)
1285 case 2: COPYBOX_COLORKEY(WORD)
1286 case 4: COPYBOX_COLORKEY(DWORD)
1293 for (y = 0; y < h; y++)
1295 for (x = 0; x < w * 3; x += 3)
1297 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1298 if (tmp < keylow || tmp > keyhigh)
1300 d[x + 0] = s[x + 0];
1301 d[x + 1] = s[x + 1];
1302 d[x + 2] = s[x + 2];
1311 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1312 ret = DDERR_UNSUPPORTED;
1315 #undef COPYBOX_COLORKEY
1316 TRACE("Copy Done\n");
1320 int width = w * bpp;
1321 TRACE("NO color key copy\n");
1322 for (y = 0; y < h; y++)
1324 /* This is pretty easy, a line for line memcpy */
1325 memcpy(dbuf, sbuf, width);
1326 sbuf += slock.Pitch;
1327 dbuf += dlock.Pitch;
1329 TRACE("Copy done\n");
1335 IWineD3DSurface_UnlockRect(iface);
1339 IWineD3DSurface_UnlockRect(iface);
1340 IWineD3DSurface_UnlockRect(Source);
1346 /*****************************************************************************
1347 * IWineD3DSurface::LoadTexture, GDI version
1349 * This is mutually unsupported by GDI surfaces
1352 * D3DERR_INVALIDCALL
1354 *****************************************************************************/
1356 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface)
1358 ERR("Unsupported on X11 surfaces\n");
1359 return D3DERR_INVALIDCALL;
1362 /*****************************************************************************
1363 * IWineD3DSurface::SaveSnapshot, GDI version
1365 * This method writes the surface's contents to the in tga format to the
1366 * file specified in filename.
1369 * filename: File to write to
1372 * WINED3DERR_INVALIDCALL if the file couldn't be opened
1373 * WINED3D_OK on success
1375 *****************************************************************************/
1376 static int get_shift(DWORD color_mask) {
1378 while (color_mask > 0xFF) {
1382 while ((color_mask & 0x80) == 0) {
1391 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
1392 const char* filename)
1396 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1397 static char *output = NULL;
1398 static int size = 0;
1399 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1401 if (This->pow2Width > size) {
1402 output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
1403 size = This->pow2Width;
1407 f = fopen(filename, "w+");
1409 ERR("opening of %s failed with\n", filename);
1410 return WINED3DERR_INVALIDCALL;
1412 fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
1414 if (This->resource.format == WINED3DFMT_P8) {
1415 unsigned char table[256][3];
1418 if (This->palette == NULL) {
1420 return WINED3DERR_INVALIDCALL;
1422 for (i = 0; i < 256; i++) {
1423 table[i][0] = This->palette->palents[i].peRed;
1424 table[i][1] = This->palette->palents[i].peGreen;
1425 table[i][2] = This->palette->palents[i].peBlue;
1427 for (y = 0; y < This->pow2Height; y++) {
1428 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1429 for (x = 0; x < This->pow2Width; x++) {
1430 unsigned char color = *src;
1433 output[3 * x + 0] = table[color][0];
1434 output[3 * x + 1] = table[color][1];
1435 output[3 * x + 2] = table[color][2];
1437 fwrite(output, 3 * This->pow2Width, 1, f);
1440 int red_shift, green_shift, blue_shift, pix_width;
1442 pix_width = This->bytesPerPixel;
1444 red_shift = get_shift(formatEntry->redMask);
1445 green_shift = get_shift(formatEntry->greenMask);
1446 blue_shift = get_shift(formatEntry->blueMask);
1448 for (y = 0; y < This->pow2Height; y++) {
1449 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1450 for (x = 0; x < This->pow2Width; x++) {
1456 for (i = 0; i < pix_width; i++) {
1457 color |= src[i] << (8 * i);
1459 src += 1 * pix_width;
1461 comp = color & formatEntry->redMask;
1462 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
1463 comp = color & formatEntry->greenMask;
1464 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
1465 comp = color & formatEntry->blueMask;
1466 output[3 * x + 2] = blue_shift > 0 ? comp >> blue_shift : comp << -blue_shift;
1468 fwrite(output, 3 * This->pow2Width, 1, f);
1475 /*****************************************************************************
1476 * IWineD3DSurface::PrivateSetup, GDI version
1478 * Initializes the GDI surface, aka creates the DIB section we render to
1479 * The DIB section creation is done by calling GetDC, which will create the
1480 * section and releasing the dc to allow the app to use it. The dib section
1481 * will stay until the surface is released
1483 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
1484 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
1485 * avoid confusion in the shared surface code.
1489 * The return values of called methods on failure
1491 *****************************************************************************/
1493 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
1495 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1498 long oldsize = This->resource.size;
1500 /* Sysmem textures have memory already allocated -
1501 * release it, this avoids an unnecessary memcpy
1503 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
1504 This->resource.allocatedMemory = NULL;
1506 /* We don't mind the nonpow2 stuff in GDI */
1507 This->resource.size = This->currentDesc.Width * getFormatDescEntry(This->resource.format)->bpp * This->currentDesc.Width;
1508 This->pow2Size = This->resource.size;
1509 This->pow2Width = This->currentDesc.Width;
1510 This->pow2Height = This->currentDesc.Height;
1511 This->Flags &= ~SFLAG_NONPOW2;
1513 /* Adjust the opengl mem counter */
1514 globalChangeGlRam(This->resource.size - oldsize);
1516 /* Call GetDC to create a DIB section. We will use that
1517 * DIB section for rendering
1519 * Release the DC afterwards to allow the app to use it
1521 hr = IWineD3DSurface_GetDC(iface, &hdc);
1524 ERR("(%p) IWineD3DSurface::GetDC failed with hr %08lx\n", This, hr);
1527 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
1530 ERR("(%p) IWineD3DSurface::ReleaseDC failed with hr %08lx\n", This, hr);
1537 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
1540 IWineD3DSurfaceImpl_QueryInterface,
1541 IWineD3DSurfaceImpl_AddRef,
1542 IWineD3DSurfaceImpl_Release,
1543 /* IWineD3DResource */
1544 IWineD3DSurfaceImpl_GetParent,
1545 IWineD3DSurfaceImpl_GetDevice,
1546 IWineD3DSurfaceImpl_SetPrivateData,
1547 IWineD3DSurfaceImpl_GetPrivateData,
1548 IWineD3DSurfaceImpl_FreePrivateData,
1549 IWineD3DSurfaceImpl_SetPriority,
1550 IWineD3DSurfaceImpl_GetPriority,
1551 IWineGDISurfaceImpl_PreLoad,
1552 IWineD3DSurfaceImpl_GetType,
1553 /* IWineD3DSurface */
1554 IWineD3DSurfaceImpl_GetContainerParent,
1555 IWineD3DSurfaceImpl_GetContainer,
1556 IWineD3DSurfaceImpl_GetDesc,
1557 IWineGDISurfaceImpl_LockRect,
1558 IWineGDISurfaceImpl_UnlockRect,
1559 IWineD3DSurfaceImpl_GetDC,
1560 IWineD3DSurfaceImpl_ReleaseDC,
1561 IWineGDISurfaceImpl_Flip,
1562 IWineGDISurfaceImpl_Blt,
1563 IWineD3DSurfaceImpl_GetBltStatus,
1564 IWineD3DSurfaceImpl_GetFlipStatus,
1565 IWineD3DSurfaceImpl_IsLost,
1566 IWineD3DSurfaceImpl_Restore,
1567 IWineGDISurfaceImpl_BltFast,
1568 IWineD3DSurfaceImpl_GetPalette,
1569 IWineD3DSurfaceImpl_SetPalette,
1570 IWineD3DSurfaceImpl_RealizePalette,
1571 IWineD3DSurfaceImpl_SetColorKey,
1572 IWineD3DSurfaceImpl_GetPitch,
1574 IWineD3DSurfaceImpl_CleanDirtyRect,
1575 IWineD3DSurfaceImpl_AddDirtyRect,
1576 IWineGDISurfaceImpl_LoadTexture,
1577 IWineGDISurfaceImpl_SaveSnapshot,
1578 IWineD3DSurfaceImpl_SetContainer,
1579 IWineD3DSurfaceImpl_SetPBufferState,
1580 IWineD3DSurfaceImpl_SetGlTextureDesc,
1581 IWineD3DSurfaceImpl_GetGlDesc,
1582 IWineD3DSurfaceImpl_GetData,
1583 IWineD3DSurfaceImpl_SetFormat,
1584 IWineGDISurfaceImpl_PrivateSetup