Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / wined3d / surface_gdi.c
blobe43c7906b891fade6291be20650d597804bcf27b
1 /*
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
28 #include "config.h"
29 #include "wine/port.h"
30 #include "wined3d_private.h"
32 #include <assert.h>
33 #include <stdio.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 /*****************************************************************************
40 * x11_copy_to_screen
42 * Helper function that blts the front buffer contents to the target window
44 * Params:
45 * This: Surface to copy from
46 * rc: Rectangle to copy
48 *****************************************************************************/
49 static void
50 x11_copy_to_screen(IWineD3DSurfaceImpl *This,
51 LPRECT rc)
53 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
55 POINT offset = {0,0};
56 HWND hDisplayWnd;
57 HDC hDisplayDC;
58 HDC hSurfaceDC = 0;
59 RECT drawrect;
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);
66 if(rc)
68 TRACE(" copying rect (%d,%d)->(%d,%d), offset (%d,%d)\n",
69 rc->left, rc->top, rc->right, rc->bottom, offset.x, offset.y);
71 #if 0
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. */
76 if (This->palette) {
77 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
78 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
80 #endif
81 drawrect.left = 0;
82 drawrect.right = This->currentDesc.Width;
83 drawrect.top = 0;
84 drawrect.bottom = This->currentDesc.Height;
86 #if 0
87 /* TODO: Support clippers */
88 if (This->clipper)
90 RECT xrc;
91 HWND hwnd = This->clipper->hWnd;
92 if (hwnd && GetClientRect(hwnd,&xrc))
94 OffsetRect(&xrc,offset.x,offset.y);
95 IntersectRect(&drawrect,&drawrect,&xrc);
98 #endif
99 if (rc)
101 IntersectRect(&drawrect,&drawrect,rc);
103 else
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);
114 BitBlt(hDisplayDC,
115 drawrect.left-offset.x, drawrect.top-offset.y,
116 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
117 hSurfaceDC,
118 drawrect.left, drawrect.top,
119 SRCCOPY);
120 ReleaseDC(hDisplayWnd, hDisplayDC);
124 /*****************************************************************************
125 * x11_copy_from_screen
127 * Reads the contents of a window into the front buffer
129 *****************************************************************************/
130 static void x11_copy_from_screen(IWineD3DSurfaceImpl* This,
131 const RECT *rc)
133 if (This->resource.usage & WINED3DUSAGE_RENDERTARGET)
135 HWND hDisplayWnd = This->resource.wineD3DDevice->ddraw_window;
136 HDC hDisplayDC = GetDC(hDisplayWnd);
137 RECT drawrect;
139 drawrect.left = 0;
140 drawrect.right = This->currentDesc.Width;
141 drawrect.top = 0;
142 drawrect.bottom = This->currentDesc.Height;
143 if (rc)
144 IntersectRect(&drawrect,&drawrect,rc);
146 BitBlt(This->hDC,
147 drawrect.left, drawrect.top,
148 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
149 hDisplayDC,
150 drawrect.left, drawrect.top,
151 SRCCOPY
153 ReleaseDC(hDisplayWnd, hDisplayDC);
157 /*****************************************************************************
158 * IWineD3DSurface::PreLoad, GDI version
160 * This call is unsupported on GDI surfaces, if it's called something went
161 * wrong in the parent library. Write an informative warning
163 *****************************************************************************/
164 static void WINAPI
165 IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
167 ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
168 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
169 ERR("(%p): Please report to wine-devel\n", iface);
172 /*****************************************************************************
173 * IWineD3DSurface::LockRect, GDI version
175 * Locks the surface and returns a pointer to the surface memory
177 * Params:
178 * pLockedRect: Address to return the locking info at
179 * pRect: Rectangle to lock
180 * Flags: Some flags
182 * Returns:
183 * WINED3D_OK on success
184 * WINED3DERR_INVALIDCALL on errors
186 *****************************************************************************/
187 static HRESULT WINAPI
188 IWineGDISurfaceImpl_LockRect(IWineD3DSurface *iface,
189 WINED3DLOCKED_RECT* pLockedRect,
190 CONST RECT* pRect,
191 DWORD Flags)
193 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
195 /* Already locked? */
196 if(This->Flags & SFLAG_LOCKED)
198 ERR("(%p) Surface already locked\n", This);
199 /* What should I return here? */
200 return WINED3DERR_INVALIDCALL;
203 if (!(This->Flags & SFLAG_LOCKABLE))
205 /* This is some GL specific thing, see the OpenGL version of
206 * this method, but check for the flag and write a trace
208 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
211 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
212 This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
214 if(!This->resource.allocatedMemory) {
215 HDC hdc;
216 HRESULT hr;
217 /* This happens on gdi surfaces if the application set a user pointer and resets it.
218 * Recreate the DIB section
220 hr = IWineD3DSurface_GetDC(iface, &hdc); /* will recursively call lockrect, do not set the LOCKED flag to this line */
221 if(hr != WINED3D_OK) return hr;
222 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
223 if(hr != WINED3D_OK) return hr;
226 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
228 if (NULL == pRect)
230 pLockedRect->pBits = This->resource.allocatedMemory;
231 This->lockedRect.left = 0;
232 This->lockedRect.top = 0;
233 This->lockedRect.right = This->currentDesc.Width;
234 This->lockedRect.bottom = This->currentDesc.Height;
236 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
237 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
238 This->lockedRect.right, This->lockedRect.bottom);
240 else
242 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
243 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
245 if ((pRect->top < 0) ||
246 (pRect->left < 0) ||
247 (pRect->left >= pRect->right) ||
248 (pRect->top >= pRect->bottom) ||
249 (pRect->right > This->currentDesc.Width) ||
250 (pRect->bottom > This->currentDesc.Height))
252 WARN(" Invalid values in pRect !!!\n");
253 return WINED3DERR_INVALIDCALL;
256 if (This->resource.format == WINED3DFMT_DXT1)
258 /* DXT1 is half byte per pixel */
259 pLockedRect->pBits = This->resource.allocatedMemory +
260 (pLockedRect->Pitch * pRect->top) +
261 ((pRect->left * This->bytesPerPixel / 2));
263 else
265 pLockedRect->pBits = This->resource.allocatedMemory +
266 (pLockedRect->Pitch * pRect->top) +
267 (pRect->left * This->bytesPerPixel);
269 This->lockedRect.left = pRect->left;
270 This->lockedRect.top = pRect->top;
271 This->lockedRect.right = pRect->right;
272 This->lockedRect.bottom = pRect->bottom;
275 /* DDraw apps can share ddraw operation with GDI if not in exclusive mode. This surface
276 * implementation BitBlts the front buffer's content onto the GDI device context. In the
277 * same way as we write to GDI we have to read from there when we lock the primary surface.
279 * Reading from GDI is slow, so do it only when the app locks the whole surface. If the app
280 * specifies a rectangle to lock it is assumed that the app knows what part it wants to lock.
282 * There is no need to take care for gdi readback for back buffers, or in the Flip method,
283 * because double buffered surfaces can only be created in DDSCL_EXCLUSIVE mode, and in
284 * this mode gdi stuff can't be done anyway.
286 if(!(Flags & DDLOCK_WRITEONLY) && iface == This->resource.wineD3DDevice->ddraw_primary &&
287 !This->resource.wineD3DDevice->ddraw_fullscreen)
289 x11_copy_from_screen(This, NULL);
292 /* No dirtifying is needed for this surface implementation */
293 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
295 This->Flags |= SFLAG_LOCKED;
296 return WINED3D_OK;
299 /*****************************************************************************
300 * IWineD3DSurface::UnlockRect, GDI version
302 * Unlocks a surface. This implementation doesn't do much, except updating
303 * the window if the front buffer is unlocked
305 * Returns:
306 * WINED3D_OK on success
307 * WINED3DERR_INVALIDCALL on failure
309 *****************************************************************************/
310 static HRESULT WINAPI
311 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
313 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
314 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
315 TRACE("(%p)\n", This);
317 if (!(This->Flags & SFLAG_LOCKED))
319 WARN("trying to Unlock an unlocked surf@%p\n", This);
320 return WINED3DERR_INVALIDCALL;
323 /* Can be useful for debugging */
324 #if 0
326 static unsigned int gen = 0;
327 char buffer[4096];
328 ++gen;
329 if ((gen % 10) == 0) {
330 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
331 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
334 * debugging crash code
335 if (gen == 250) {
336 void** test = NULL;
337 *test = 0;
341 #endif
343 /* Update the screen */
344 if(This == (IWineD3DSurfaceImpl *) dev->ddraw_primary)
346 x11_copy_to_screen(This, &This->lockedRect);
349 This->Flags &= ~SFLAG_LOCKED;
350 memset(&This->lockedRect, 0, sizeof(RECT));
351 return WINED3D_OK;
354 /*****************************************************************************
355 * IWineD3DSurface::Flip, GDI version
357 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
358 * the parent library. This implementation changes the data pointers of the
359 * surfaces and copies the new front buffer content to the screen
361 * Params:
362 * override: Flipping target(e.g. back buffer)
364 * Returns:
365 * WINED3D_OK on success
367 *****************************************************************************/
368 static HRESULT WINAPI
369 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
370 IWineD3DSurface *override,
371 DWORD Flags)
373 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
374 IWineD3DSurfaceImpl *Target = (IWineD3DSurfaceImpl *) override;
375 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
377 TRACE("(%p) Flipping to surface %p\n", This, Target);
379 if(Target == NULL)
381 ERR("(%p): Can't flip without a target\n", This);
382 return WINED3DERR_INVALIDCALL;
385 /* Flip the DC */
387 HDC tmp;
388 tmp = This->hDC;
389 This->hDC = Target->hDC;
390 Target->hDC = tmp;
393 /* Flip the DIBsection */
395 HBITMAP tmp;
396 tmp = This->dib.DIBsection;
397 This->dib.DIBsection = Target->dib.DIBsection;
398 Target->dib.DIBsection = tmp;
401 /* Flip the surface data */
403 void* tmp;
405 tmp = This->dib.bitmap_data;
406 This->dib.bitmap_data = Target->dib.bitmap_data;
407 Target->dib.bitmap_data = tmp;
409 tmp = This->resource.allocatedMemory;
410 This->resource.allocatedMemory = Target->resource.allocatedMemory;
411 Target->resource.allocatedMemory = tmp;
414 /* client_memory should not be different, but just in case */
416 BOOL tmp;
417 tmp = This->dib.client_memory;
418 This->dib.client_memory = Target->dib.client_memory;
419 Target->dib.client_memory = tmp;
422 /* Useful for debugging */
423 #if 0
425 static unsigned int gen = 0;
426 char buffer[4096];
427 ++gen;
428 if ((gen % 10) == 0) {
429 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
430 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
433 * debugging crash code
434 if (gen == 250) {
435 void** test = NULL;
436 *test = 0;
440 #endif
442 /* Update the screen */
443 x11_copy_to_screen(This, NULL);
445 /* FPS support */
446 if (TRACE_ON(fps))
448 static long prev_time, frames;
450 DWORD time = GetTickCount();
451 frames++;
452 /* every 1.5 seconds */
453 if (time - prev_time > 1500) {
454 TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
455 prev_time = time;
456 frames = 0;
460 return WINED3D_OK;
463 /*****************************************************************************
464 * _Blt_ColorFill
466 * Helper function that fills a memory area with a specific color
468 * Params:
469 * buf: memory address to start filling at
470 * width, height: Dimensions of the area to fill
471 * bpp: Bit depth of the surface
472 * lPitch: pitch of the surface
473 * color: Color to fill with
475 *****************************************************************************/
476 static HRESULT
477 _Blt_ColorFill(BYTE *buf,
478 int width, int height,
479 int bpp, LONG lPitch,
480 DWORD color)
482 int x, y;
483 LPBYTE first;
485 /* Do first row */
487 #define COLORFILL_ROW(type) \
489 type *d = (type *) buf; \
490 for (x = 0; x < width; x++) \
491 d[x] = (type) color; \
492 break; \
494 switch(bpp)
496 case 1: COLORFILL_ROW(BYTE)
497 case 2: COLORFILL_ROW(WORD)
498 case 3:
500 BYTE *d = (BYTE *) buf;
501 for (x = 0; x < width; x++,d+=3)
503 d[0] = (color ) & 0xFF;
504 d[1] = (color>> 8) & 0xFF;
505 d[2] = (color>>16) & 0xFF;
507 break;
509 case 4: COLORFILL_ROW(DWORD)
510 default:
511 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
512 return DDERR_UNSUPPORTED;
515 #undef COLORFILL_ROW
517 /* Now copy first row */
518 first = buf;
519 for (y = 1; y < height; y++)
521 buf += lPitch;
522 memcpy(buf, first, width * bpp);
524 return DD_OK;
527 /*****************************************************************************
528 * IWineD3DSurface::Blt, GDI version
530 * Performs blits to a surface, eigher from a source of source-less blts
531 * This is the main functionality of DirectDraw
533 * Params:
534 * DestRect: Destination rectangle to write to
535 * SrcSurface: Source surface, can be NULL
536 * SrcRect: Source rectangle
537 *****************************************************************************/
538 HRESULT WINAPI
539 IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface,
540 RECT *DestRect,
541 IWineD3DSurface *SrcSurface,
542 RECT *SrcRect,
543 DWORD Flags,
544 DDBLTFX *DDBltFx)
546 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
547 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
548 RECT xdst,xsrc;
549 HRESULT ret = DD_OK;
550 WINED3DLOCKED_RECT dlock, slock;
551 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
552 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
553 int x, y;
554 const PixelFormatDesc *sEntry, *dEntry;
555 LPBYTE dbuf, sbuf;
556 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
558 if (TRACE_ON(d3d_surface))
560 if (DestRect) TRACE("\tdestrect :%dx%d-%dx%d\n",
561 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
562 if (SrcRect) TRACE("\tsrcrect :%dx%d-%dx%d\n",
563 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
564 #if 0
565 TRACE("\tflags: ");
566 DDRAW_dump_DDBLT(Flags);
567 if (Flags & DDBLT_DDFX)
569 TRACE("\tblitfx: ");
570 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
572 #endif
575 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
577 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
578 return DDERR_SURFACEBUSY;
581 if (Src == This)
583 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
584 dfmt = This->resource.format;
585 slock = dlock;
586 sfmt = dfmt;
587 sEntry = getFormatDescEntry(sfmt);
588 dEntry = sEntry;
590 else
592 if (Src)
594 IWineD3DSurface_LockRect(SrcSurface, &slock, NULL, WINED3DLOCK_READONLY);
595 sfmt = Src->resource.format;
597 sEntry = getFormatDescEntry(sfmt);
598 dfmt = This->resource.format;
599 dEntry = getFormatDescEntry(dfmt);
600 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
603 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~DDBLT_DDFX;
605 if (sEntry->isFourcc && dEntry->isFourcc)
607 if (sfmt != dfmt)
609 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
610 ret = DDERR_INVALIDPIXELFORMAT;
611 goto release;
613 TRACE("Fourcc->Fourcc copy)\n");
614 memcpy(dlock.pBits, slock.pBits, This->currentDesc.Height * dlock.Pitch);
615 goto release;
618 if (sEntry->isFourcc && !dEntry->isFourcc)
620 FIXME("DXTC decompression not supported right now\n");
621 goto release;
624 if (DestRect)
626 memcpy(&xdst,DestRect,sizeof(xdst));
628 else
630 xdst.top = 0;
631 xdst.bottom = This->currentDesc.Height;
632 xdst.left = 0;
633 xdst.right = This->currentDesc.Width;
636 if (SrcRect)
638 memcpy(&xsrc,SrcRect,sizeof(xsrc));
640 else
642 if (Src)
644 xsrc.top = 0;
645 xsrc.bottom = Src->currentDesc.Height;
646 xsrc.left = 0;
647 xsrc.right = Src->currentDesc.Width;
649 else
651 memset(&xsrc,0,sizeof(xsrc));
655 /* First check for the validity of source / destination rectangles. This was
656 verified using a test application + by MSDN.
658 if ((Src != NULL) &&
659 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
660 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
661 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
662 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
663 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
665 WARN("Application gave us bad source rectangle for Blt.\n");
666 ret = DDERR_INVALIDRECT;
667 goto release;
669 /* For the Destination rect, it can be out of bounds on the condition that a clipper
670 is set for the given surface.
672 if ((/*This->clipper == NULL*/ TRUE) &&
673 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
674 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
675 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
676 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
677 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
679 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
680 ret = DDERR_INVALIDRECT;
681 goto release;
684 /* Now handle negative values in the rectangles. Warning: only supported for now
685 in the 'simple' cases (ie not in any stretching / rotation cases).
687 First, the case where nothing is to be done.
689 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
690 (xdst.top >= (int) This->currentDesc.Height) ||
691 (xdst.left >= (int) This->currentDesc.Width)) ||
692 ((Src != NULL) &&
693 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
694 (xsrc.top >= (int) Src->currentDesc.Height) ||
695 (xsrc.left >= (int) Src->currentDesc.Width)) ))
697 TRACE("Nothing to be done !\n");
698 goto release;
701 /* The easy case : the source-less blits.... */
702 if (Src == NULL)
704 RECT full_rect;
705 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
707 full_rect.left = 0;
708 full_rect.top = 0;
709 full_rect.right = This->currentDesc.Width;
710 full_rect.bottom = This->currentDesc.Height;
711 IntersectRect(&temp_rect, &full_rect, &xdst);
712 xdst = temp_rect;
714 else
716 /* Only handle clipping on the destination rectangle */
717 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
718 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
719 if (clip_vert || clip_horiz)
721 /* Now check if this is a special case or not... */
722 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
723 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
724 (Flags & DDBLT_DDFX))
726 WARN("Out of screen rectangle in special case. Not handled right now.\n");
727 goto release;
730 if (clip_horiz)
732 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
733 if (xdst.right > This->currentDesc.Width)
735 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
736 xdst.right = (int) This->currentDesc.Width;
739 if (clip_vert)
741 if (xdst.top < 0)
743 xsrc.top -= xdst.top;
744 xdst.top = 0;
746 if (xdst.bottom > This->currentDesc.Height)
748 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
749 xdst.bottom = (int) This->currentDesc.Height;
752 /* And check if after clipping something is still to be done... */
753 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
754 (xdst.top >= (int) This->currentDesc.Height) ||
755 (xdst.left >= (int) This->currentDesc.Width) ||
756 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
757 (xsrc.top >= (int) Src->currentDesc.Height) ||
758 (xsrc.left >= (int) Src->currentDesc.Width))
760 TRACE("Nothing to be done after clipping !\n");
761 goto release;
766 bpp = This->bytesPerPixel;
767 srcheight = xsrc.bottom - xsrc.top;
768 srcwidth = xsrc.right - xsrc.left;
769 dstheight = xdst.bottom - xdst.top;
770 dstwidth = xdst.right - xdst.left;
771 width = (xdst.right - xdst.left) * bpp;
773 assert(width <= dlock.Pitch);
775 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
777 if (Flags & DDBLT_WAIT)
779 static BOOL displayed = FALSE;
780 if (!displayed)
781 FIXME("Can't handle DDBLT_WAIT flag right now.\n");
782 displayed = TRUE;
783 Flags &= ~DDBLT_WAIT;
785 if (Flags & DDBLT_ASYNC)
787 static BOOL displayed = FALSE;
788 if (!displayed)
789 FIXME("Can't handle DDBLT_ASYNC flag right now.\n");
790 displayed = TRUE;
791 Flags &= ~DDBLT_ASYNC;
793 if (Flags & DDBLT_DONOTWAIT)
795 /* DDBLT_DONOTWAIT appeared in DX7 */
796 static BOOL displayed = FALSE;
797 if (!displayed)
798 FIXME("Can't handle DDBLT_DONOTWAIT flag right now.\n");
799 displayed = TRUE;
800 Flags &= ~DDBLT_DONOTWAIT;
803 /* First, all the 'source-less' blits */
804 if (Flags & DDBLT_COLORFILL)
806 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
807 dlock.Pitch, DDBltFx->u5.dwFillColor);
808 Flags &= ~DDBLT_COLORFILL;
811 if (Flags & DDBLT_DEPTHFILL)
813 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
815 if (Flags & DDBLT_ROP)
817 /* Catch some degenerate cases here */
818 switch(DDBltFx->dwROP)
820 case BLACKNESS:
821 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
822 break;
823 case 0xAA0029: /* No-op */
824 break;
825 case WHITENESS:
826 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
827 break;
828 case SRCCOPY: /* well, we do that below ? */
829 break;
830 default:
831 FIXME("Unsupported raster op: %08x Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
832 goto error;
834 Flags &= ~DDBLT_ROP;
836 if (Flags & DDBLT_DDROPS)
838 FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
840 /* Now the 'with source' blits */
841 if (Src)
843 LPBYTE sbase;
844 int sx, xinc, sy, yinc;
846 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
847 goto release;
848 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
849 xinc = (srcwidth << 16) / dstwidth;
850 yinc = (srcheight << 16) / dstheight;
852 if (!Flags)
854 /* No effects, we can cheat here */
855 if (dstwidth == srcwidth)
857 if (dstheight == srcheight)
859 /* No stretching in either direction. This needs to be as
860 * fast as possible */
861 sbuf = sbase;
863 /* check for overlapping surfaces */
864 if (SrcSurface != iface || xdst.top < xsrc.top ||
865 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
867 /* no overlap, or dst above src, so copy from top downwards */
868 for (y = 0; y < dstheight; y++)
870 memcpy(dbuf, sbuf, width);
871 sbuf += slock.Pitch;
872 dbuf += dlock.Pitch;
875 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
877 sbuf += (slock.Pitch*dstheight);
878 dbuf += (dlock.Pitch*dstheight);
879 for (y = 0; y < dstheight; y++)
881 sbuf -= slock.Pitch;
882 dbuf -= dlock.Pitch;
883 memcpy(dbuf, sbuf, width);
886 else /* src and dst overlapping on the same line, use memmove */
888 for (y = 0; y < dstheight; y++)
890 memmove(dbuf, sbuf, width);
891 sbuf += slock.Pitch;
892 dbuf += dlock.Pitch;
895 } else {
896 /* Stretching in Y direction only */
897 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
898 sbuf = sbase + (sy >> 16) * slock.Pitch;
899 memcpy(dbuf, sbuf, width);
900 dbuf += dlock.Pitch;
904 else
906 /* Stretching in X direction */
907 int last_sy = -1;
908 for (y = sy = 0; y < dstheight; y++, sy += yinc)
910 sbuf = sbase + (sy >> 16) * slock.Pitch;
912 if ((sy >> 16) == (last_sy >> 16))
914 /* this sourcerow is the same as last sourcerow -
915 * copy already stretched row
917 memcpy(dbuf, dbuf - dlock.Pitch, width);
919 else
921 #define STRETCH_ROW(type) { \
922 type *s = (type *) sbuf, *d = (type *) dbuf; \
923 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
924 d[x] = s[sx >> 16]; \
925 break; }
927 switch(bpp)
929 case 1: STRETCH_ROW(BYTE)
930 case 2: STRETCH_ROW(WORD)
931 case 4: STRETCH_ROW(DWORD)
932 case 3:
934 LPBYTE s,d = dbuf;
935 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
937 DWORD pixel;
939 s = sbuf+3*(sx>>16);
940 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
941 d[0] = (pixel )&0xff;
942 d[1] = (pixel>> 8)&0xff;
943 d[2] = (pixel>>16)&0xff;
944 d+=3;
946 break;
948 default:
949 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
950 ret = DDERR_UNSUPPORTED;
951 goto error;
953 #undef STRETCH_ROW
955 dbuf += dlock.Pitch;
956 last_sy = sy;
960 else
962 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
963 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
964 DWORD destkeylow = 0x0, destkeyhigh = 0xFFFFFFFF, destkeymask = 0xFFFFFFFF;
965 if (Flags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE))
967 /* The color keying flags are checked for correctness in ddraw */
968 if (Flags & DDBLT_KEYSRC)
970 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
971 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
973 else if (Flags & DDBLT_KEYSRCOVERRIDE)
975 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
976 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
979 if (Flags & DDBLT_KEYDEST)
981 /* Destination color keys are taken from the source surface ! */
982 destkeylow = Src->DestBltCKey.dwColorSpaceLowValue;
983 destkeyhigh = Src->DestBltCKey.dwColorSpaceHighValue;
985 else if (Flags & DDBLT_KEYDESTOVERRIDE)
987 destkeylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
988 destkeyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
991 if(bpp == 1)
993 keymask = 0xff;
995 else
997 keymask = sEntry->redMask |
998 sEntry->greenMask |
999 sEntry->blueMask;
1001 Flags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
1004 if (Flags & DDBLT_DDFX)
1006 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
1007 LONG tmpxy;
1008 dTopLeft = dbuf;
1009 dTopRight = dbuf+((dstwidth-1)*bpp);
1010 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
1011 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
1013 if (DDBltFx->dwDDFX & DDBLTFX_ARITHSTRETCHY)
1015 /* I don't think we need to do anything about this flag */
1016 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
1018 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT)
1020 tmp = dTopRight;
1021 dTopRight = dTopLeft;
1022 dTopLeft = tmp;
1023 tmp = dBottomRight;
1024 dBottomRight = dBottomLeft;
1025 dBottomLeft = tmp;
1026 dstxinc = dstxinc *-1;
1028 if (DDBltFx->dwDDFX & DDBLTFX_MIRRORUPDOWN)
1030 tmp = dTopLeft;
1031 dTopLeft = dBottomLeft;
1032 dBottomLeft = tmp;
1033 tmp = dTopRight;
1034 dTopRight = dBottomRight;
1035 dBottomRight = tmp;
1036 dstyinc = dstyinc *-1;
1038 if (DDBltFx->dwDDFX & DDBLTFX_NOTEARING)
1040 /* I don't think we need to do anything about this flag */
1041 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
1043 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE180)
1045 tmp = dBottomRight;
1046 dBottomRight = dTopLeft;
1047 dTopLeft = tmp;
1048 tmp = dBottomLeft;
1049 dBottomLeft = dTopRight;
1050 dTopRight = tmp;
1051 dstxinc = dstxinc * -1;
1052 dstyinc = dstyinc * -1;
1054 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE270)
1056 tmp = dTopLeft;
1057 dTopLeft = dBottomLeft;
1058 dBottomLeft = dBottomRight;
1059 dBottomRight = dTopRight;
1060 dTopRight = tmp;
1061 tmpxy = dstxinc;
1062 dstxinc = dstyinc;
1063 dstyinc = tmpxy;
1064 dstxinc = dstxinc * -1;
1066 if (DDBltFx->dwDDFX & DDBLTFX_ROTATE90)
1068 tmp = dTopLeft;
1069 dTopLeft = dTopRight;
1070 dTopRight = dBottomRight;
1071 dBottomRight = dBottomLeft;
1072 dBottomLeft = tmp;
1073 tmpxy = dstxinc;
1074 dstxinc = dstyinc;
1075 dstyinc = tmpxy;
1076 dstyinc = dstyinc * -1;
1078 if (DDBltFx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST)
1080 /* I don't think we need to do anything about this flag */
1081 WARN("Flags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
1083 dbuf = dTopLeft;
1084 Flags &= ~(DDBLT_DDFX);
1087 #define COPY_COLORKEY_FX(type) { \
1088 type *s, *d = (type *) dbuf, *dx, tmp; \
1089 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1090 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1091 dx = d; \
1092 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1093 tmp = s[sx >> 16]; \
1094 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1095 ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1096 dx[0] = tmp; \
1098 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1100 d = (type*)(((LPBYTE)d)+dstyinc); \
1102 break; }
1104 switch (bpp) {
1105 case 1: COPY_COLORKEY_FX(BYTE)
1106 case 2: COPY_COLORKEY_FX(WORD)
1107 case 4: COPY_COLORKEY_FX(DWORD)
1108 case 3:
1110 LPBYTE s,d = dbuf, dx;
1111 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1113 sbuf = sbase + (sy >> 16) * slock.Pitch;
1114 dx = d;
1115 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1117 DWORD pixel, dpixel = 0;
1118 s = sbuf+3*(sx>>16);
1119 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1120 dpixel = dx[0]|(dx[1]<<8)|(dx[2]<<16);
1121 if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) &&
1122 ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
1124 dx[0] = (pixel )&0xff;
1125 dx[1] = (pixel>> 8)&0xff;
1126 dx[2] = (pixel>>16)&0xff;
1128 dx+= dstxinc;
1130 d += dstyinc;
1132 break;
1134 default:
1135 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1136 (Flags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1137 ret = DDERR_UNSUPPORTED;
1138 goto error;
1139 #undef COPY_COLORKEY_FX
1144 error:
1145 if (Flags && FIXME_ON(d3d_surface))
1147 FIXME("\tUnsupported flags: %08x\n", Flags);
1150 release:
1151 IWineD3DSurface_UnlockRect(iface);
1152 if (SrcSurface && SrcSurface != iface) IWineD3DSurface_UnlockRect(SrcSurface);
1153 return ret;
1156 /*****************************************************************************
1157 * IWineD3DSurface::BltFast, GDI version
1159 * This is the software implementation of BltFast, as used by GDI surfaces
1160 * and as a fallback for OpenGL surfaces. This code is taken from the old
1161 * DirectDraw code, and was originally written by TransGaming.
1163 * Params:
1164 * dstx:
1165 * dsty:
1166 * Source: Source surface to copy from
1167 * rsrc: Source rectangle
1168 * trans: Some Flags
1170 * Returns:
1171 * WINED3D_OK on success
1173 *****************************************************************************/
1174 HRESULT WINAPI
1175 IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface,
1176 DWORD dstx,
1177 DWORD dsty,
1178 IWineD3DSurface *Source,
1179 RECT *rsrc,
1180 DWORD trans)
1182 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1183 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1185 int bpp, w, h, x, y;
1186 WINED3DLOCKED_RECT dlock,slock;
1187 HRESULT ret = DD_OK;
1188 RECT rsrc2;
1189 RECT lock_src, lock_dst, lock_union;
1190 BYTE *sbuf, *dbuf;
1191 const PixelFormatDesc *sEntry, *dEntry;
1193 if (TRACE_ON(d3d_surface))
1195 TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This,dstx,dsty,Src,rsrc,trans);
1197 if (rsrc)
1199 TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,
1200 rsrc->right,rsrc->bottom);
1202 else
1204 TRACE(" srcrect: NULL\n");
1208 if ((This->Flags & SFLAG_LOCKED) ||
1209 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1211 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1212 return DDERR_SURFACEBUSY;
1215 if (!rsrc)
1217 WARN("rsrc is NULL!\n");
1218 rsrc = &rsrc2;
1219 rsrc->left = 0;
1220 rsrc->top = 0;
1221 rsrc->right = Src->currentDesc.Width;
1222 rsrc->bottom = Src->currentDesc.Height;
1225 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1226 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1227 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1228 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1229 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1230 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1232 WARN("Application gave us bad source rectangle for BltFast.\n");
1233 return DDERR_INVALIDRECT;
1236 h = rsrc->bottom - rsrc->top;
1237 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1238 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1239 if (h <= 0) return DDERR_INVALIDRECT;
1241 w = rsrc->right - rsrc->left;
1242 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1243 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1244 if (w <= 0) return DDERR_INVALIDRECT;
1246 /* Now compute the locking rectangle... */
1247 lock_src.left = rsrc->left;
1248 lock_src.top = rsrc->top;
1249 lock_src.right = lock_src.left + w;
1250 lock_src.bottom = lock_src.top + h;
1252 lock_dst.left = dstx;
1253 lock_dst.top = dsty;
1254 lock_dst.right = dstx + w;
1255 lock_dst.bottom = dsty + h;
1257 bpp = This->bytesPerPixel;
1259 /* We need to lock the surfaces, or we won't get refreshes when done. */
1260 if (Src == This)
1262 int pitch;
1264 UnionRect(&lock_union, &lock_src, &lock_dst);
1266 /* Lock the union of the two rectangles */
1267 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1268 if(ret != WINED3D_OK) goto error;
1270 pitch = dlock.Pitch;
1271 slock.Pitch = dlock.Pitch;
1273 /* Since slock was originally copied from this surface's description, we can just reuse it */
1274 assert(This->resource.allocatedMemory != NULL);
1275 sbuf = (BYTE *)This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1276 dbuf = (BYTE *)This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1277 sEntry = getFormatDescEntry(Src->resource.format);
1278 dEntry = sEntry;
1280 else
1282 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, WINED3DLOCK_READONLY);
1283 if(ret != WINED3D_OK) goto error;
1284 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1285 if(ret != WINED3D_OK) goto error;
1287 sbuf = slock.pBits;
1288 dbuf = dlock.pBits;
1289 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1291 sEntry = getFormatDescEntry(Src->resource.format);
1292 dEntry = getFormatDescEntry(This->resource.format);
1295 /* Handle first the FOURCC surfaces... */
1296 if (sEntry->isFourcc && dEntry->isFourcc)
1298 TRACE("Fourcc -> Fourcc copy\n");
1299 if (trans)
1300 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1301 if (dstx || dsty)
1302 FIXME("offset for destination surface is not supported\n");
1303 if (Src->resource.format != This->resource.format)
1305 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1306 ret = DDERR_INVALIDPIXELFORMAT;
1307 goto error;
1309 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1310 memcpy(dbuf, sbuf, This->resource.size);
1311 goto error;
1313 if (sEntry->isFourcc && !dEntry->isFourcc)
1315 /* TODO: Use the libtxc_dxtn.so shared library to do
1316 * software decompression
1318 ERR("DXTC decompression not supported by now\n");
1319 goto error;
1322 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY))
1324 DWORD keylow, keyhigh;
1325 TRACE("Color keyed copy\n");
1326 if (trans & DDBLTFAST_SRCCOLORKEY)
1328 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1329 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1331 else
1333 /* I'm not sure if this is correct */
1334 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1335 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1336 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1339 #define COPYBOX_COLORKEY(type) { \
1340 type *d, *s, tmp; \
1341 s = (type *) sbuf; \
1342 d = (type *) dbuf; \
1343 for (y = 0; y < h; y++) { \
1344 for (x = 0; x < w; x++) { \
1345 tmp = s[x]; \
1346 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1348 s = (type *)((BYTE *)s + slock.Pitch); \
1349 d = (type *)((BYTE *)d + dlock.Pitch); \
1351 break; \
1354 switch (bpp) {
1355 case 1: COPYBOX_COLORKEY(BYTE)
1356 case 2: COPYBOX_COLORKEY(WORD)
1357 case 4: COPYBOX_COLORKEY(DWORD)
1358 case 3:
1360 BYTE *d, *s;
1361 DWORD tmp;
1362 s = (BYTE *) sbuf;
1363 d = (BYTE *) dbuf;
1364 for (y = 0; y < h; y++)
1366 for (x = 0; x < w * 3; x += 3)
1368 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1369 if (tmp < keylow || tmp > keyhigh)
1371 d[x + 0] = s[x + 0];
1372 d[x + 1] = s[x + 1];
1373 d[x + 2] = s[x + 2];
1376 s += slock.Pitch;
1377 d += dlock.Pitch;
1379 break;
1381 default:
1382 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1383 ret = DDERR_UNSUPPORTED;
1384 goto error;
1386 #undef COPYBOX_COLORKEY
1387 TRACE("Copy Done\n");
1389 else
1391 int width = w * bpp;
1392 TRACE("NO color key copy\n");
1393 for (y = 0; y < h; y++)
1395 /* This is pretty easy, a line for line memcpy */
1396 memcpy(dbuf, sbuf, width);
1397 sbuf += slock.Pitch;
1398 dbuf += dlock.Pitch;
1400 TRACE("Copy done\n");
1403 error:
1404 if (Src == This)
1406 IWineD3DSurface_UnlockRect(iface);
1408 else
1410 IWineD3DSurface_UnlockRect(iface);
1411 IWineD3DSurface_UnlockRect(Source);
1414 return ret;
1417 /*****************************************************************************
1418 * IWineD3DSurface::LoadTexture, GDI version
1420 * This is mutually unsupported by GDI surfaces
1422 * Returns:
1423 * D3DERR_INVALIDCALL
1425 *****************************************************************************/
1426 HRESULT WINAPI
1427 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface)
1429 ERR("Unsupported on X11 surfaces\n");
1430 return WINED3DERR_INVALIDCALL;
1433 /*****************************************************************************
1434 * IWineD3DSurface::SaveSnapshot, GDI version
1436 * This method writes the surface's contents to the in tga format to the
1437 * file specified in filename.
1439 * Params:
1440 * filename: File to write to
1442 * Returns:
1443 * WINED3DERR_INVALIDCALL if the file couldn't be opened
1444 * WINED3D_OK on success
1446 *****************************************************************************/
1447 static int get_shift(DWORD color_mask) {
1448 int shift = 0;
1449 while (color_mask > 0xFF) {
1450 color_mask >>= 1;
1451 shift += 1;
1453 while ((color_mask & 0x80) == 0) {
1454 color_mask <<= 1;
1455 shift -= 1;
1457 return shift;
1461 HRESULT WINAPI
1462 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
1463 const char* filename)
1465 FILE* f = NULL;
1466 UINT y = 0, x = 0;
1467 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1468 static char *output = NULL;
1469 static int size = 0;
1470 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1472 if (This->pow2Width > size) {
1473 output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
1474 size = This->pow2Width;
1478 f = fopen(filename, "w+");
1479 if (NULL == f) {
1480 ERR("opening of %s failed with\n", filename);
1481 return WINED3DERR_INVALIDCALL;
1483 fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
1485 if (This->resource.format == WINED3DFMT_P8) {
1486 unsigned char table[256][3];
1487 int i;
1489 if (This->palette == NULL) {
1490 fclose(f);
1491 return WINED3DERR_INVALIDCALL;
1493 for (i = 0; i < 256; i++) {
1494 table[i][0] = This->palette->palents[i].peRed;
1495 table[i][1] = This->palette->palents[i].peGreen;
1496 table[i][2] = This->palette->palents[i].peBlue;
1498 for (y = 0; y < This->pow2Height; y++) {
1499 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1500 for (x = 0; x < This->pow2Width; x++) {
1501 unsigned char color = *src;
1502 src += 1;
1504 output[3 * x + 0] = table[color][0];
1505 output[3 * x + 1] = table[color][1];
1506 output[3 * x + 2] = table[color][2];
1508 fwrite(output, 3 * This->pow2Width, 1, f);
1510 } else {
1511 int red_shift, green_shift, blue_shift, pix_width;
1513 pix_width = This->bytesPerPixel;
1515 red_shift = get_shift(formatEntry->redMask);
1516 green_shift = get_shift(formatEntry->greenMask);
1517 blue_shift = get_shift(formatEntry->blueMask);
1519 for (y = 0; y < This->pow2Height; y++) {
1520 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1521 for (x = 0; x < This->pow2Width; x++) {
1522 unsigned int color;
1523 unsigned int comp;
1524 int i;
1526 color = 0;
1527 for (i = 0; i < pix_width; i++) {
1528 color |= src[i] << (8 * i);
1530 src += 1 * pix_width;
1532 comp = color & formatEntry->redMask;
1533 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
1534 comp = color & formatEntry->greenMask;
1535 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
1536 comp = color & formatEntry->blueMask;
1537 output[3 * x + 2] = blue_shift > 0 ? comp >> blue_shift : comp << -blue_shift;
1539 fwrite(output, 3 * This->pow2Width, 1, f);
1542 fclose(f);
1543 return WINED3D_OK;
1546 /*****************************************************************************
1547 * IWineD3DSurface::PrivateSetup, GDI version
1549 * Initializes the GDI surface, aka creates the DIB section we render to
1550 * The DIB section creation is done by calling GetDC, which will create the
1551 * section and releasing the dc to allow the app to use it. The dib section
1552 * will stay until the surface is released
1554 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
1555 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
1556 * avoid confusion in the shared surface code.
1558 * Returns:
1559 * WINED3D_OK on success
1560 * The return values of called methods on failure
1562 *****************************************************************************/
1563 HRESULT WINAPI
1564 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
1566 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1567 HRESULT hr;
1568 HDC hdc;
1569 long oldsize = This->resource.size;
1571 if(This->resource.usage & WINED3DUSAGE_OVERLAY)
1573 ERR("(%p) Overlays not yet supported by GDI surfaces\n", This);
1574 return WINED3DERR_INVALIDCALL;
1576 /* Sysmem textures have memory already allocated -
1577 * release it, this avoids an unnecessary memcpy
1579 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
1580 This->resource.allocatedMemory = NULL;
1582 /* We don't mind the nonpow2 stuff in GDI */
1583 This->resource.size = IWineD3DSurface_GetPitch(iface) * This->currentDesc.Height;
1584 This->pow2Width = This->currentDesc.Width;
1585 This->pow2Height = This->currentDesc.Height;
1586 This->Flags &= ~SFLAG_NONPOW2;
1588 /* Adjust the opengl mem counter */
1589 globalChangeGlRam(This->resource.size - oldsize);
1591 /* Call GetDC to create a DIB section. We will use that
1592 * DIB section for rendering
1594 * Release the DC afterwards to allow the app to use it
1596 hr = IWineD3DSurface_GetDC(iface, &hdc);
1597 if(FAILED(hr))
1599 ERR("(%p) IWineD3DSurface::GetDC failed with hr %08x\n", This, hr);
1600 return hr;
1602 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
1603 if(FAILED(hr))
1605 ERR("(%p) IWineD3DSurface::ReleaseDC failed with hr %08x\n", This, hr);
1606 return hr;
1609 return WINED3D_OK;
1612 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
1614 /* IUnknown */
1615 IWineD3DSurfaceImpl_QueryInterface,
1616 IWineD3DSurfaceImpl_AddRef,
1617 IWineD3DSurfaceImpl_Release,
1618 /* IWineD3DResource */
1619 IWineD3DSurfaceImpl_GetParent,
1620 IWineD3DSurfaceImpl_GetDevice,
1621 IWineD3DSurfaceImpl_SetPrivateData,
1622 IWineD3DSurfaceImpl_GetPrivateData,
1623 IWineD3DSurfaceImpl_FreePrivateData,
1624 IWineD3DSurfaceImpl_SetPriority,
1625 IWineD3DSurfaceImpl_GetPriority,
1626 IWineGDISurfaceImpl_PreLoad,
1627 IWineD3DSurfaceImpl_GetType,
1628 /* IWineD3DSurface */
1629 IWineD3DSurfaceImpl_GetContainer,
1630 IWineD3DSurfaceImpl_GetDesc,
1631 IWineGDISurfaceImpl_LockRect,
1632 IWineGDISurfaceImpl_UnlockRect,
1633 IWineD3DSurfaceImpl_GetDC,
1634 IWineD3DSurfaceImpl_ReleaseDC,
1635 IWineGDISurfaceImpl_Flip,
1636 IWineGDISurfaceImpl_Blt,
1637 IWineD3DSurfaceImpl_GetBltStatus,
1638 IWineD3DSurfaceImpl_GetFlipStatus,
1639 IWineD3DSurfaceImpl_IsLost,
1640 IWineD3DSurfaceImpl_Restore,
1641 IWineGDISurfaceImpl_BltFast,
1642 IWineD3DSurfaceImpl_GetPalette,
1643 IWineD3DSurfaceImpl_SetPalette,
1644 IWineD3DSurfaceImpl_RealizePalette,
1645 IWineD3DSurfaceImpl_SetColorKey,
1646 IWineD3DSurfaceImpl_GetPitch,
1647 IWineD3DSurfaceImpl_SetMem,
1648 IWineD3DSurfaceImpl_SetOverlayPosition,
1649 IWineD3DSurfaceImpl_GetOverlayPosition,
1650 IWineD3DSurfaceImpl_UpdateOverlayZOrder,
1651 IWineD3DSurfaceImpl_UpdateOverlay,
1652 /* Internal use: */
1653 IWineD3DSurfaceImpl_AddDirtyRect,
1654 IWineGDISurfaceImpl_LoadTexture,
1655 IWineGDISurfaceImpl_SaveSnapshot,
1656 IWineD3DSurfaceImpl_SetContainer,
1657 IWineD3DSurfaceImpl_SetGlTextureDesc,
1658 IWineD3DSurfaceImpl_GetGlDesc,
1659 IWineD3DSurfaceImpl_GetData,
1660 IWineD3DSurfaceImpl_SetFormat,
1661 IWineGDISurfaceImpl_PrivateSetup