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 if(!This->resource.allocatedMemory) {
184 /* This happens on gdi surfaces if the application set a user pointer and resets it.
185 * Recreate the DIB section
187 hr = IWineD3DSurface_GetDC(iface, &hdc); /* will recursively call lockrect, do not set the LOCKED flag to this line */
188 if(hr != WINED3D_OK) return hr;
189 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
190 if(hr != WINED3D_OK) return hr;
193 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
197 pLockedRect->pBits = This->resource.allocatedMemory;
198 This->lockedRect.left = 0;
199 This->lockedRect.top = 0;
200 This->lockedRect.right = This->currentDesc.Width;
201 This->lockedRect.bottom = This->currentDesc.Height;
203 TRACE("Locked Rect (%p) = l %ld, t %ld, r %ld, b %ld\n",
204 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
205 This->lockedRect.right, This->lockedRect.bottom);
209 TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n",
210 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
212 if ((pRect->top < 0) ||
214 (pRect->left >= pRect->right) ||
215 (pRect->top >= pRect->bottom) ||
216 (pRect->right > This->currentDesc.Width) ||
217 (pRect->bottom > This->currentDesc.Height))
219 WARN(" Invalid values in pRect !!!\n");
220 return D3DERR_INVALIDCALL;
223 if (This->resource.format == WINED3DFMT_DXT1)
225 /* DXT1 is half byte per pixel */
226 pLockedRect->pBits = This->resource.allocatedMemory +
227 (pLockedRect->Pitch * pRect->top) +
228 ((pRect->left * This->bytesPerPixel / 2));
232 pLockedRect->pBits = This->resource.allocatedMemory +
233 (pLockedRect->Pitch * pRect->top) +
234 (pRect->left * This->bytesPerPixel);
236 This->lockedRect.left = pRect->left;
237 This->lockedRect.top = pRect->top;
238 This->lockedRect.right = pRect->right;
239 This->lockedRect.bottom = pRect->bottom;
242 /* No dirtifying is needed for this surface implementation */
243 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
245 This->Flags |= SFLAG_LOCKED;
249 /*****************************************************************************
250 * IWineD3DSurface::UnlockRect, GDI version
252 * Unlocks a surface. This implementation doesn't do much, except updating
253 * the window if the front buffer is unlocked
256 * WINED3D_OK on success
257 * WINED3DERR_INVALIDCALL on failure
259 *****************************************************************************/
260 static HRESULT WINAPI
261 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
263 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
264 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
265 TRACE("(%p)\n", This);
267 if (!(This->Flags & SFLAG_LOCKED))
269 WARN("trying to Unlock an unlocked surf@%p\n", This);
270 return WINED3DERR_INVALIDCALL;
273 /* Can be useful for debugging */
276 static unsigned int gen = 0;
279 if ((gen % 10) == 0) {
280 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
281 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
284 * debugging crash code
293 /* Update the screen */
294 if(This == (IWineD3DSurfaceImpl *) dev->ddraw_primary)
296 x11_copy_to_screen(This, &This->lockedRect);
299 This->Flags &= ~SFLAG_LOCKED;
300 memset(&This->lockedRect, 0, sizeof(RECT));
304 /*****************************************************************************
305 * IWineD3DSurface::Flip, GDI version
307 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
308 * the parent library. This implementation changes the data pointers of the
309 * surfaces and copies the new front buffer content to the screen
312 * override: Flipping target(e.g. back buffer)
315 * WINED3D_OK on success
317 *****************************************************************************/
318 static HRESULT WINAPI
319 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
320 IWineD3DSurface *override,
323 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
324 IWineD3DSurfaceImpl *Target = (IWineD3DSurfaceImpl *) override;
325 TRACE("(%p)->(%p,%lx)\n", This, override, Flags);
327 TRACE("(%p) Flipping to surface %p\n", This, Target);
331 ERR("(%p): Can't flip without a target\n", This);
332 return WINED3DERR_INVALIDCALL;
339 This->hDC = Target->hDC;
343 /* Flip the DIBsection */
346 tmp = This->dib.DIBsection;
347 This->dib.DIBsection = Target->dib.DIBsection;
348 Target->dib.DIBsection = tmp;
351 /* Flip the surface data */
355 tmp = This->dib.bitmap_data;
356 This->dib.bitmap_data = Target->dib.bitmap_data;
357 Target->dib.bitmap_data = tmp;
359 tmp = This->resource.allocatedMemory;
360 This->resource.allocatedMemory = Target->resource.allocatedMemory;
361 Target->resource.allocatedMemory = tmp;
364 /* client_memory should not be different, but just in case */
367 tmp = This->dib.client_memory;
368 This->dib.client_memory = Target->dib.client_memory;
369 Target->dib.client_memory = tmp;
372 /* Useful for debugging */
375 static unsigned int gen = 0;
378 if ((gen % 10) == 0) {
379 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
380 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
383 * debugging crash code
392 /* Update the screen */
393 x11_copy_to_screen(This, NULL);
398 static long prev_time, frames;
400 DWORD time = GetTickCount();
402 /* every 1.5 seconds */
403 if (time - prev_time > 1500) {
404 TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
413 /*****************************************************************************
416 * Helper function that fills a memory area with a specific color
419 * buf: memory address to start filling at
420 * width, height: Dimensions of the area to fill
421 * bpp: Bit depth of the surface
422 * lPitch: pitch of the surface
423 * color: Color to fill with
425 *****************************************************************************/
427 _Blt_ColorFill(BYTE *buf,
428 int width, int height,
429 int bpp, LONG lPitch,
437 #define COLORFILL_ROW(type) \
439 type *d = (type *) buf; \
440 for (x = 0; x < width; x++) \
441 d[x] = (type) color; \
446 case 1: COLORFILL_ROW(BYTE)
447 case 2: COLORFILL_ROW(WORD)
450 BYTE *d = (BYTE *) buf;
451 for (x = 0; x < width; x++,d+=3)
453 d[0] = (color ) & 0xFF;
454 d[1] = (color>> 8) & 0xFF;
455 d[2] = (color>>16) & 0xFF;
459 case 4: COLORFILL_ROW(DWORD)
461 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
462 return DDERR_UNSUPPORTED;
467 /* Now copy first row */
469 for (y = 1; y < height; y++)
472 memcpy(buf, first, width * bpp);
477 /*****************************************************************************
478 * IWineD3DSurface::Blt, GDI version
480 * Performs blits to a surface, eigher from a source of source-less blts
481 * This is the main functionality of DirectDraw
484 * DestRect: Destination rectangle to write to
485 * SrcSurface: Source surface, can be NULL
486 * SrcRect: Source rectangle
487 *****************************************************************************/
489 IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface,
491 IWineD3DSurface *SrcSurface,
496 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
497 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
500 WINED3DLOCKED_RECT dlock, slock;
501 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
502 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
504 const PixelFormatDesc *sEntry, *dEntry;
506 TRACE("(%p)->(%p,%p,%p,%lx,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
508 if (TRACE_ON(d3d_surface))
510 if (DestRect) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",
511 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
512 if (SrcRect) TRACE("\tsrcrect :%ldx%ld-%ldx%ld\n",
513 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
516 DDRAW_dump_DDBLT(Flags);
517 if (Flags & DDBLT_DDFX)
520 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
525 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
527 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
528 return DDERR_SURFACEBUSY;
533 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
534 dfmt = This->resource.format;
537 sEntry = getFormatDescEntry(sfmt);
544 IWineD3DSurface_LockRect(SrcSurface, &slock, NULL, D3DLOCK_READONLY);
545 sfmt = Src->resource.format;
547 sEntry = getFormatDescEntry(sfmt);
548 dfmt = This->resource.format;
549 dEntry = getFormatDescEntry(dfmt);
550 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
553 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~DDBLT_DDFX;
555 if (sEntry->isFourcc && dEntry->isFourcc)
559 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
560 ret = DDERR_INVALIDPIXELFORMAT;
563 TRACE("Fourcc->Fourcc copy)\n");
564 memcpy(dlock.pBits, slock.pBits, This->currentDesc.Height * dlock.Pitch);
568 if (sEntry->isFourcc && !dEntry->isFourcc)
570 FIXME("DXTC decompression not supported right now\n");
576 memcpy(&xdst,DestRect,sizeof(xdst));
581 xdst.bottom = This->currentDesc.Height;
583 xdst.right = This->currentDesc.Width;
588 memcpy(&xsrc,SrcRect,sizeof(xsrc));
595 xsrc.bottom = Src->currentDesc.Height;
597 xsrc.right = Src->currentDesc.Width;
601 memset(&xsrc,0,sizeof(xsrc));
605 /* First check for the validity of source / destination rectangles. This was
606 verified using a test application + by MSDN.
609 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
610 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
611 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
612 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
613 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
615 WARN("Application gave us bad source rectangle for Blt.\n");
616 ret = DDERR_INVALIDRECT;
619 /* For the Destination rect, it can be out of bounds on the condition that a clipper
620 is set for the given surface.
622 if ((/*This->clipper == NULL*/ TRUE) &&
623 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
624 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
625 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
626 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
627 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
629 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
630 ret = DDERR_INVALIDRECT;
634 /* Now handle negative values in the rectangles. Warning: only supported for now
635 in the 'simple' cases (ie not in any stretching / rotation cases).
637 First, the case where nothing is to be done.
639 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
640 (xdst.top >= (int) This->currentDesc.Height) ||
641 (xdst.left >= (int) This->currentDesc.Width)) ||
643 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
644 (xsrc.top >= (int) Src->currentDesc.Height) ||
645 (xsrc.left >= (int) Src->currentDesc.Width)) ))
647 TRACE("Nothing to be done !\n");
651 /* The easy case : the source-less blits.... */
655 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
659 full_rect.right = This->currentDesc.Width;
660 full_rect.bottom = This->currentDesc.Height;
661 IntersectRect(&temp_rect, &full_rect, &xdst);
666 /* Only handle clipping on the destination rectangle */
667 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
668 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
669 if (clip_vert || clip_horiz)
671 /* Now check if this is a special case or not... */
672 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
673 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
674 (Flags & DDBLT_DDFX))
676 WARN("Out of screen rectangle in special case. Not handled right now.\n");
682 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
683 if (xdst.right > This->currentDesc.Width)
685 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
686 xdst.right = (int) This->currentDesc.Width;
693 xsrc.top -= xdst.top;
696 if (xdst.bottom > This->currentDesc.Height)
698 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
699 xdst.bottom = (int) This->currentDesc.Height;
702 /* And check if after clipping something is still to be done... */
703 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
704 (xdst.top >= (int) This->currentDesc.Height) ||
705 (xdst.left >= (int) This->currentDesc.Width) ||
706 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
707 (xsrc.top >= (int) Src->currentDesc.Height) ||
708 (xsrc.left >= (int) Src->currentDesc.Width))
710 TRACE("Nothing to be done after clipping !\n");
716 bpp = This->bytesPerPixel;
717 srcheight = xsrc.bottom - xsrc.top;
718 srcwidth = xsrc.right - xsrc.left;
719 dstheight = xdst.bottom - xdst.top;
720 dstwidth = xdst.right - xdst.left;
721 width = (xdst.right - xdst.left) * bpp;
723 assert(width <= dlock.Pitch);
725 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
727 if (Flags & DDBLT_WAIT)
729 static BOOL displayed = FALSE;
731 FIXME("Can't handle DDBLT_WAIT flag right now.\n");
733 Flags &= ~DDBLT_WAIT;
735 if (Flags & DDBLT_ASYNC)
737 static BOOL displayed = FALSE;
739 FIXME("Can't handle DDBLT_ASYNC flag right now.\n");
741 Flags &= ~DDBLT_ASYNC;
743 if (Flags & DDBLT_DONOTWAIT)
745 /* DDBLT_DONOTWAIT appeared in DX7 */
746 static BOOL displayed = FALSE;
748 FIXME("Can't handle DDBLT_DONOTWAIT flag right now.\n");
750 Flags &= ~DDBLT_DONOTWAIT;
753 /* First, all the 'source-less' blits */
754 if (Flags & DDBLT_COLORFILL)
756 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
757 dlock.Pitch, DDBltFx->u5.dwFillColor);
758 Flags &= ~DDBLT_COLORFILL;
761 if (Flags & DDBLT_DEPTHFILL)
763 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
765 if (Flags & DDBLT_ROP)
767 /* Catch some degenerate cases here */
768 switch(DDBltFx->dwROP)
771 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
773 case 0xAA0029: /* No-op */
776 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
778 case SRCCOPY: /* well, we do that below ? */
781 FIXME("Unsupported raster op: %08lx Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
786 if (Flags & DDBLT_DDROPS)
788 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
790 /* Now the 'with source' blits */
794 int sx, xinc, sy, yinc;
796 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
798 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
799 xinc = (srcwidth << 16) / dstwidth;
800 yinc = (srcheight << 16) / dstheight;
804 /* No effects, we can cheat here */
805 if (dstwidth == srcwidth)
807 if (dstheight == srcheight)
809 /* No stretching in either direction. This needs to be as
810 * fast as possible */
813 /* check for overlapping surfaces */
814 if (SrcSurface != iface || xdst.top < xsrc.top ||
815 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
817 /* no overlap, or dst above src, so copy from top downwards */
818 for (y = 0; y < dstheight; y++)
820 memcpy(dbuf, sbuf, width);
825 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
827 sbuf += (slock.Pitch*dstheight);
828 dbuf += (dlock.Pitch*dstheight);
829 for (y = 0; y < dstheight; y++)
833 memcpy(dbuf, sbuf, width);
836 else /* src and dst overlapping on the same line, use memmove */
838 for (y = 0; y < dstheight; y++)
840 memmove(dbuf, sbuf, width);
846 /* Stretching in Y direction only */
847 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
848 sbuf = sbase + (sy >> 16) * slock.Pitch;
849 memcpy(dbuf, sbuf, width);
856 /* Stretching in X direction */
858 for (y = sy = 0; y < dstheight; y++, sy += yinc)
860 sbuf = sbase + (sy >> 16) * slock.Pitch;
862 if ((sy >> 16) == (last_sy >> 16))
864 /* this sourcerow is the same as last sourcerow -
865 * copy already stretched row
867 memcpy(dbuf, dbuf - dlock.Pitch, width);
871 #define STRETCH_ROW(type) { \
872 type *s = (type *) sbuf, *d = (type *) dbuf; \
873 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
874 d[x] = s[sx >> 16]; \
879 case 1: STRETCH_ROW(BYTE)
880 case 2: STRETCH_ROW(WORD)
881 case 4: STRETCH_ROW(DWORD)
885 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
890 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
891 d[0] = (pixel )&0xff;
892 d[1] = (pixel>> 8)&0xff;
893 d[2] = (pixel>>16)&0xff;
899 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
900 ret = DDERR_UNSUPPORTED;
912 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
913 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
914 if (Flags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE))
917 if (Flags & DDBLT_KEYSRC)
919 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
920 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
922 else if (Flags & DDBLT_KEYDEST)
924 keylow = This->DestBltCKey.dwColorSpaceLowValue;
925 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
927 else if (Flags & DDBLT_KEYSRCOVERRIDE)
929 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
930 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
934 keylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
935 keyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
943 keymask = sEntry->redMask |
947 Flags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
950 if (Flags & DDBLT_DDFX)
952 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
955 dTopRight = dbuf+((dstwidth-1)*bpp);
956 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
957 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
959 if (DDBltFx->dwDDFX & DDBLTFX_ARITHSTRETCHY)
961 /* I don't think we need to do anything about this flag */
962 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
964 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT)
967 dTopRight = dTopLeft;
970 dBottomRight = dBottomLeft;
972 dstxinc = dstxinc *-1;
974 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORUPDOWN)
977 dTopLeft = dBottomLeft;
980 dTopRight = dBottomRight;
982 dstyinc = dstyinc *-1;
984 if (DDBltFx->dwDDFX & DDBLTFX_NOTEARING)
986 /* I don't think we need to do anything about this flag */
987 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
989 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE180)
992 dBottomRight = dTopLeft;
995 dBottomLeft = dTopRight;
997 dstxinc = dstxinc * -1;
998 dstyinc = dstyinc * -1;
1000 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE270)
1003 dTopLeft = dBottomLeft;
1004 dBottomLeft = dBottomRight;
1005 dBottomRight = dTopRight;
1010 dstxinc = dstxinc * -1;
1012 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE90)
1015 dTopLeft = dTopRight;
1016 dTopRight = dBottomRight;
1017 dBottomRight = dBottomLeft;
1022 dstyinc = dstyinc * -1;
1024 if (DDBltFx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST)
1026 /* I don't think we need to do anything about this flag */
1027 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
1030 Flags &= ~(DDBLT_DDFX);
1033 #define COPY_COLORKEY_FX(type) { \
1034 type *s, *d = (type *) dbuf, *dx, tmp; \
1035 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1036 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1038 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1039 tmp = s[sx >> 16]; \
1040 if ((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) dx[0] = tmp; \
1041 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1043 d = (type*)(((LPBYTE)d)+dstyinc); \
1048 case 1: COPY_COLORKEY_FX(BYTE)
1049 case 2: COPY_COLORKEY_FX(WORD)
1050 case 4: COPY_COLORKEY_FX(DWORD)
1053 LPBYTE s,d = dbuf, dx;
1054 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1056 sbuf = sbase + (sy >> 16) * slock.Pitch;
1058 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1061 s = sbuf+3*(sx>>16);
1062 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1063 if ((pixel & keymask) < keylow || (pixel & keymask) > keyhigh)
1065 dx[0] = (pixel )&0xff;
1066 dx[1] = (pixel>> 8)&0xff;
1067 dx[2] = (pixel>>16)&0xff;
1076 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1077 (Flags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1078 ret = DDERR_UNSUPPORTED;
1080 #undef COPY_COLORKEY_FX
1086 if (Flags && FIXME_ON(d3d_surface))
1088 FIXME("\tUnsupported flags: %08lx\n", Flags);
1092 IWineD3DSurface_UnlockRect(iface);
1093 if (SrcSurface && SrcSurface != iface) IWineD3DSurface_UnlockRect(SrcSurface);
1097 /*****************************************************************************
1098 * IWineD3DSurface::BltFast, GDI version
1100 * This is the software implementation of BltFast, as used by GDI surfaces
1101 * and as a fallback for OpenGL surfaces. This code is taken from the old
1102 * DirectDraw code, and was originally written by TransGaming.
1107 * Source: Source surface to copy from
1108 * rsrc: Source rectangle
1112 * WINED3D_OK on success
1114 *****************************************************************************/
1116 IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface,
1119 IWineD3DSurface *Source,
1123 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1124 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1126 int bpp, w, h, x, y;
1127 WINED3DLOCKED_RECT dlock,slock;
1128 HRESULT ret = DD_OK;
1130 RECT lock_src, lock_dst, lock_union;
1132 const PixelFormatDesc *sEntry, *dEntry;
1134 if (TRACE_ON(d3d_surface))
1136 TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n", This,dstx,dsty,Src,rsrc,trans);
1140 TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,
1141 rsrc->right,rsrc->bottom);
1145 TRACE(" srcrect: NULL\n");
1149 if ((This->Flags & SFLAG_LOCKED) ||
1150 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1152 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1153 return DDERR_SURFACEBUSY;
1158 WARN("rsrc is NULL!\n");
1162 rsrc->right = Src->currentDesc.Width;
1163 rsrc->bottom = Src->currentDesc.Height;
1166 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1167 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1168 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1169 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1170 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1171 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1173 WARN("Application gave us bad source rectangle for BltFast.\n");
1174 return DDERR_INVALIDRECT;
1177 h = rsrc->bottom - rsrc->top;
1178 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1179 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1180 if (h <= 0) return DDERR_INVALIDRECT;
1182 w = rsrc->right - rsrc->left;
1183 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1184 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1185 if (w <= 0) return DDERR_INVALIDRECT;
1187 /* Now compute the locking rectangle... */
1188 lock_src.left = rsrc->left;
1189 lock_src.top = rsrc->top;
1190 lock_src.right = lock_src.left + w;
1191 lock_src.bottom = lock_src.top + h;
1193 lock_dst.left = dstx;
1194 lock_dst.top = dsty;
1195 lock_dst.right = dstx + w;
1196 lock_dst.bottom = dsty + h;
1198 bpp = This->bytesPerPixel;
1200 /* We need to lock the surfaces, or we won't get refreshes when done. */
1205 UnionRect(&lock_union, &lock_src, &lock_dst);
1207 /* Lock the union of the two rectangles */
1208 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1209 if(ret != D3D_OK) goto error;
1211 pitch = dlock.Pitch;
1212 slock.Pitch = dlock.Pitch;
1214 /* Since slock was originally copied from this surface's description, we can just reuse it */
1215 assert(This->resource.allocatedMemory != NULL);
1216 sbuf = (BYTE *)This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1217 dbuf = (BYTE *)This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1218 sEntry = getFormatDescEntry(Src->resource.format);
1223 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, D3DLOCK_READONLY);
1224 if(ret != D3D_OK) goto error;
1225 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1226 if(ret != D3D_OK) goto error;
1230 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1232 sEntry = getFormatDescEntry(Src->resource.format);
1233 dEntry = getFormatDescEntry(This->resource.format);
1236 /* Handle first the FOURCC surfaces... */
1237 if (sEntry->isFourcc && dEntry->isFourcc)
1239 TRACE("Fourcc -> Fourcc copy\n");
1241 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1243 FIXME("offset for destination surface is not supported\n");
1244 if (Src->resource.format != This->resource.format)
1246 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1247 ret = DDERR_INVALIDPIXELFORMAT;
1250 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1251 memcpy(dbuf, sbuf, This->resource.size);
1254 if (sEntry->isFourcc && !dEntry->isFourcc)
1256 /* TODO: Use the libtxc_dxtn.so shared library to do
1257 * software decompression
1259 ERR("DXTC decompression not supported by now\n");
1263 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY))
1265 DWORD keylow, keyhigh;
1266 TRACE("Color keyed copy\n");
1267 if (trans & DDBLTFAST_SRCCOLORKEY)
1269 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1270 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1274 /* I'm not sure if this is correct */
1275 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1276 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1277 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1280 #define COPYBOX_COLORKEY(type) { \
1282 s = (type *) sbuf; \
1283 d = (type *) dbuf; \
1284 for (y = 0; y < h; y++) { \
1285 for (x = 0; x < w; x++) { \
1287 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1289 s = (type *)((BYTE *)s + slock.Pitch); \
1290 d = (type *)((BYTE *)d + dlock.Pitch); \
1296 case 1: COPYBOX_COLORKEY(BYTE)
1297 case 2: COPYBOX_COLORKEY(WORD)
1298 case 4: COPYBOX_COLORKEY(DWORD)
1305 for (y = 0; y < h; y++)
1307 for (x = 0; x < w * 3; x += 3)
1309 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1310 if (tmp < keylow || tmp > keyhigh)
1312 d[x + 0] = s[x + 0];
1313 d[x + 1] = s[x + 1];
1314 d[x + 2] = s[x + 2];
1323 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1324 ret = DDERR_UNSUPPORTED;
1327 #undef COPYBOX_COLORKEY
1328 TRACE("Copy Done\n");
1332 int width = w * bpp;
1333 TRACE("NO color key copy\n");
1334 for (y = 0; y < h; y++)
1336 /* This is pretty easy, a line for line memcpy */
1337 memcpy(dbuf, sbuf, width);
1338 sbuf += slock.Pitch;
1339 dbuf += dlock.Pitch;
1341 TRACE("Copy done\n");
1347 IWineD3DSurface_UnlockRect(iface);
1351 IWineD3DSurface_UnlockRect(iface);
1352 IWineD3DSurface_UnlockRect(Source);
1358 /*****************************************************************************
1359 * IWineD3DSurface::LoadTexture, GDI version
1361 * This is mutually unsupported by GDI surfaces
1364 * D3DERR_INVALIDCALL
1366 *****************************************************************************/
1368 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface)
1370 ERR("Unsupported on X11 surfaces\n");
1371 return D3DERR_INVALIDCALL;
1374 /*****************************************************************************
1375 * IWineD3DSurface::SaveSnapshot, GDI version
1377 * This method writes the surface's contents to the in tga format to the
1378 * file specified in filename.
1381 * filename: File to write to
1384 * WINED3DERR_INVALIDCALL if the file couldn't be opened
1385 * WINED3D_OK on success
1387 *****************************************************************************/
1388 static int get_shift(DWORD color_mask) {
1390 while (color_mask > 0xFF) {
1394 while ((color_mask & 0x80) == 0) {
1403 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
1404 const char* filename)
1408 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1409 static char *output = NULL;
1410 static int size = 0;
1411 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1413 if (This->pow2Width > size) {
1414 output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
1415 size = This->pow2Width;
1419 f = fopen(filename, "w+");
1421 ERR("opening of %s failed with\n", filename);
1422 return WINED3DERR_INVALIDCALL;
1424 fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
1426 if (This->resource.format == WINED3DFMT_P8) {
1427 unsigned char table[256][3];
1430 if (This->palette == NULL) {
1432 return WINED3DERR_INVALIDCALL;
1434 for (i = 0; i < 256; i++) {
1435 table[i][0] = This->palette->palents[i].peRed;
1436 table[i][1] = This->palette->palents[i].peGreen;
1437 table[i][2] = This->palette->palents[i].peBlue;
1439 for (y = 0; y < This->pow2Height; y++) {
1440 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1441 for (x = 0; x < This->pow2Width; x++) {
1442 unsigned char color = *src;
1445 output[3 * x + 0] = table[color][0];
1446 output[3 * x + 1] = table[color][1];
1447 output[3 * x + 2] = table[color][2];
1449 fwrite(output, 3 * This->pow2Width, 1, f);
1452 int red_shift, green_shift, blue_shift, pix_width;
1454 pix_width = This->bytesPerPixel;
1456 red_shift = get_shift(formatEntry->redMask);
1457 green_shift = get_shift(formatEntry->greenMask);
1458 blue_shift = get_shift(formatEntry->blueMask);
1460 for (y = 0; y < This->pow2Height; y++) {
1461 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1462 for (x = 0; x < This->pow2Width; x++) {
1468 for (i = 0; i < pix_width; i++) {
1469 color |= src[i] << (8 * i);
1471 src += 1 * pix_width;
1473 comp = color & formatEntry->redMask;
1474 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
1475 comp = color & formatEntry->greenMask;
1476 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
1477 comp = color & formatEntry->blueMask;
1478 output[3 * x + 2] = blue_shift > 0 ? comp >> blue_shift : comp << -blue_shift;
1480 fwrite(output, 3 * This->pow2Width, 1, f);
1487 /*****************************************************************************
1488 * IWineD3DSurface::PrivateSetup, GDI version
1490 * Initializes the GDI surface, aka creates the DIB section we render to
1491 * The DIB section creation is done by calling GetDC, which will create the
1492 * section and releasing the dc to allow the app to use it. The dib section
1493 * will stay until the surface is released
1495 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
1496 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
1497 * avoid confusion in the shared surface code.
1501 * The return values of called methods on failure
1503 *****************************************************************************/
1505 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
1507 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1510 long oldsize = This->resource.size;
1512 /* Sysmem textures have memory already allocated -
1513 * release it, this avoids an unnecessary memcpy
1515 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
1516 This->resource.allocatedMemory = NULL;
1518 /* We don't mind the nonpow2 stuff in GDI */
1519 This->resource.size = This->currentDesc.Width * getFormatDescEntry(This->resource.format)->bpp * This->currentDesc.Width;
1520 This->pow2Size = This->resource.size;
1521 This->pow2Width = This->currentDesc.Width;
1522 This->pow2Height = This->currentDesc.Height;
1523 This->Flags &= ~SFLAG_NONPOW2;
1525 /* Adjust the opengl mem counter */
1526 globalChangeGlRam(This->resource.size - oldsize);
1528 /* Call GetDC to create a DIB section. We will use that
1529 * DIB section for rendering
1531 * Release the DC afterwards to allow the app to use it
1533 hr = IWineD3DSurface_GetDC(iface, &hdc);
1536 ERR("(%p) IWineD3DSurface::GetDC failed with hr %08lx\n", This, hr);
1539 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
1542 ERR("(%p) IWineD3DSurface::ReleaseDC failed with hr %08lx\n", This, hr);
1549 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
1552 IWineD3DSurfaceImpl_QueryInterface,
1553 IWineD3DSurfaceImpl_AddRef,
1554 IWineD3DSurfaceImpl_Release,
1555 /* IWineD3DResource */
1556 IWineD3DSurfaceImpl_GetParent,
1557 IWineD3DSurfaceImpl_GetDevice,
1558 IWineD3DSurfaceImpl_SetPrivateData,
1559 IWineD3DSurfaceImpl_GetPrivateData,
1560 IWineD3DSurfaceImpl_FreePrivateData,
1561 IWineD3DSurfaceImpl_SetPriority,
1562 IWineD3DSurfaceImpl_GetPriority,
1563 IWineGDISurfaceImpl_PreLoad,
1564 IWineD3DSurfaceImpl_GetType,
1565 /* IWineD3DSurface */
1566 IWineD3DSurfaceImpl_GetContainerParent,
1567 IWineD3DSurfaceImpl_GetContainer,
1568 IWineD3DSurfaceImpl_GetDesc,
1569 IWineGDISurfaceImpl_LockRect,
1570 IWineGDISurfaceImpl_UnlockRect,
1571 IWineD3DSurfaceImpl_GetDC,
1572 IWineD3DSurfaceImpl_ReleaseDC,
1573 IWineGDISurfaceImpl_Flip,
1574 IWineGDISurfaceImpl_Blt,
1575 IWineD3DSurfaceImpl_GetBltStatus,
1576 IWineD3DSurfaceImpl_GetFlipStatus,
1577 IWineD3DSurfaceImpl_IsLost,
1578 IWineD3DSurfaceImpl_Restore,
1579 IWineGDISurfaceImpl_BltFast,
1580 IWineD3DSurfaceImpl_GetPalette,
1581 IWineD3DSurfaceImpl_SetPalette,
1582 IWineD3DSurfaceImpl_RealizePalette,
1583 IWineD3DSurfaceImpl_SetColorKey,
1584 IWineD3DSurfaceImpl_GetPitch,
1585 IWineD3DSurfaceImpl_SetMem,
1587 IWineD3DSurfaceImpl_CleanDirtyRect,
1588 IWineD3DSurfaceImpl_AddDirtyRect,
1589 IWineGDISurfaceImpl_LoadTexture,
1590 IWineGDISurfaceImpl_SaveSnapshot,
1591 IWineD3DSurfaceImpl_SetContainer,
1592 IWineD3DSurfaceImpl_SetPBufferState,
1593 IWineD3DSurfaceImpl_SetGlTextureDesc,
1594 IWineD3DSurfaceImpl_GetGlDesc,
1595 IWineD3DSurfaceImpl_GetData,
1596 IWineD3DSurfaceImpl_SetFormat,
1597 IWineGDISurfaceImpl_PrivateSetup