push 739d3025d82fc820f7a9c38697877dd3d18e8c13
[wine/hacks.git] / dlls / wined3d / surface_gdi.c
blobf3a3feaa6c1128ab23bd0093bc152830f65ed5af
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);
72 /* Front buffer coordinates are screen coordinates. Map them to the destination
73 * window if not fullscreened
75 if(!This->resource.wineD3DDevice->ddraw_fullscreen) {
76 ClientToScreen(hDisplayWnd, &offset);
78 #if 0
79 /* FIXME: this doesn't work... if users really want to run
80 * X in 8bpp, then we need to call directly into display.drv
81 * (or Wine's equivalent), and force a private colormap
82 * without default entries. */
83 if (This->palette) {
84 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
85 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
87 #endif
88 drawrect.left = 0;
89 drawrect.right = This->currentDesc.Width;
90 drawrect.top = 0;
91 drawrect.bottom = This->currentDesc.Height;
93 #if 0
94 /* TODO: Support clippers */
95 if (This->clipper)
97 RECT xrc;
98 HWND hwnd = ((IWineD3DClipperImpl *) This->clipper)->hWnd;
99 if (hwnd && GetClientRect(hwnd,&xrc))
101 OffsetRect(&xrc,offset.x,offset.y);
102 IntersectRect(&drawrect,&drawrect,&xrc);
105 #endif
106 if (rc)
108 IntersectRect(&drawrect,&drawrect,rc);
110 else
112 /* Only use this if the caller did not pass a rectangle, since
113 * due to double locking this could be the wrong one ...
115 if (This->lockedRect.left != This->lockedRect.right)
117 IntersectRect(&drawrect,&drawrect,&This->lockedRect);
121 BitBlt(hDisplayDC,
122 drawrect.left-offset.x, drawrect.top-offset.y,
123 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
124 hSurfaceDC,
125 drawrect.left, drawrect.top,
126 SRCCOPY);
127 ReleaseDC(hDisplayWnd, hDisplayDC);
131 /*****************************************************************************
132 * IWineD3DSurface::PreLoad, GDI version
134 * This call is unsupported on GDI surfaces, if it's called something went
135 * wrong in the parent library. Write an informative warning
137 *****************************************************************************/
138 static void WINAPI
139 IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
141 ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
142 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
143 ERR("(%p): Please report to wine-devel\n", iface);
146 /*****************************************************************************
147 * IWineD3DSurface::LockRect, GDI version
149 * Locks the surface and returns a pointer to the surface memory
151 * Params:
152 * pLockedRect: Address to return the locking info at
153 * pRect: Rectangle to lock
154 * Flags: Some flags
156 * Returns:
157 * WINED3D_OK on success
158 * WINED3DERR_INVALIDCALL on errors
160 *****************************************************************************/
161 static HRESULT WINAPI
162 IWineGDISurfaceImpl_LockRect(IWineD3DSurface *iface,
163 WINED3DLOCKED_RECT* pLockedRect,
164 CONST RECT* pRect,
165 DWORD Flags)
167 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
169 /* Already locked? */
170 if(This->Flags & SFLAG_LOCKED)
172 ERR("(%p) Surface already locked\n", This);
173 /* What should I return here? */
174 return WINED3DERR_INVALIDCALL;
177 if (!(This->Flags & SFLAG_LOCKABLE))
179 /* This is some GL specific thing, see the OpenGL version of
180 * this method, but check for the flag and write a trace
182 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
185 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
186 This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
188 if(!This->resource.allocatedMemory) {
189 HDC hdc;
190 HRESULT hr;
191 /* This happens on gdi surfaces if the application set a user pointer and resets it.
192 * Recreate the DIB section
194 hr = IWineD3DSurface_GetDC(iface, &hdc); /* will recursively call lockrect, do not set the LOCKED flag to this line */
195 if(hr != WINED3D_OK) return hr;
196 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
197 if(hr != WINED3D_OK) return hr;
200 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
202 if (NULL == pRect)
204 pLockedRect->pBits = This->resource.allocatedMemory;
205 This->lockedRect.left = 0;
206 This->lockedRect.top = 0;
207 This->lockedRect.right = This->currentDesc.Width;
208 This->lockedRect.bottom = This->currentDesc.Height;
210 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
211 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
212 This->lockedRect.right, This->lockedRect.bottom);
214 else
216 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
217 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
219 if (This->resource.format == WINED3DFMT_DXT1)
221 /* DXT1 is half byte per pixel */
222 pLockedRect->pBits = This->resource.allocatedMemory +
223 (pLockedRect->Pitch * pRect->top) +
224 ((pRect->left * This->bytesPerPixel / 2));
226 else
228 pLockedRect->pBits = This->resource.allocatedMemory +
229 (pLockedRect->Pitch * pRect->top) +
230 (pRect->left * This->bytesPerPixel);
232 This->lockedRect.left = pRect->left;
233 This->lockedRect.top = pRect->top;
234 This->lockedRect.right = pRect->right;
235 This->lockedRect.bottom = pRect->bottom;
238 /* No dirtifying is needed for this surface implementation */
239 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
241 This->Flags |= SFLAG_LOCKED;
242 return WINED3D_OK;
245 /*****************************************************************************
246 * IWineD3DSurface::UnlockRect, GDI version
248 * Unlocks a surface. This implementation doesn't do much, except updating
249 * the window if the front buffer is unlocked
251 * Returns:
252 * WINED3D_OK on success
253 * WINED3DERR_INVALIDCALL on failure
255 *****************************************************************************/
256 static HRESULT WINAPI
257 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
259 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
260 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
261 TRACE("(%p)\n", This);
263 if (!(This->Flags & SFLAG_LOCKED))
265 WARN("trying to Unlock an unlocked surf@%p\n", This);
266 return WINED3DERR_INVALIDCALL;
269 /* Can be useful for debugging */
270 #if 0
272 static unsigned int gen = 0;
273 char buffer[4096];
274 ++gen;
275 if ((gen % 10) == 0) {
276 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
277 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
280 * debugging crash code
281 if (gen == 250) {
282 void** test = NULL;
283 *test = 0;
287 #endif
289 /* Update the screen */
290 if(This == (IWineD3DSurfaceImpl *) dev->ddraw_primary)
292 x11_copy_to_screen(This, &This->lockedRect);
295 This->Flags &= ~SFLAG_LOCKED;
296 memset(&This->lockedRect, 0, sizeof(RECT));
297 return WINED3D_OK;
300 /*****************************************************************************
301 * IWineD3DSurface::Flip, GDI version
303 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
304 * the parent library. This implementation changes the data pointers of the
305 * surfaces and copies the new front buffer content to the screen
307 * Params:
308 * override: Flipping target(e.g. back buffer)
310 * Returns:
311 * WINED3D_OK on success
313 *****************************************************************************/
314 static HRESULT WINAPI
315 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
316 IWineD3DSurface *override,
317 DWORD Flags)
319 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
320 IWineD3DSurfaceImpl *Target = (IWineD3DSurfaceImpl *) override;
321 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
323 TRACE("(%p) Flipping to surface %p\n", This, Target);
325 if(Target == NULL)
327 ERR("(%p): Can't flip without a target\n", This);
328 return WINED3DERR_INVALIDCALL;
331 /* Flip the DC */
333 HDC tmp;
334 tmp = This->hDC;
335 This->hDC = Target->hDC;
336 Target->hDC = tmp;
339 /* Flip the DIBsection */
341 HBITMAP tmp;
342 tmp = This->dib.DIBsection;
343 This->dib.DIBsection = Target->dib.DIBsection;
344 Target->dib.DIBsection = tmp;
347 /* Flip the surface data */
349 void* tmp;
351 tmp = This->dib.bitmap_data;
352 This->dib.bitmap_data = Target->dib.bitmap_data;
353 Target->dib.bitmap_data = tmp;
355 tmp = This->resource.allocatedMemory;
356 This->resource.allocatedMemory = Target->resource.allocatedMemory;
357 Target->resource.allocatedMemory = tmp;
360 /* client_memory should not be different, but just in case */
362 BOOL tmp;
363 tmp = This->dib.client_memory;
364 This->dib.client_memory = Target->dib.client_memory;
365 Target->dib.client_memory = tmp;
368 /* Useful for debugging */
369 #if 0
371 static unsigned int gen = 0;
372 char buffer[4096];
373 ++gen;
374 if ((gen % 10) == 0) {
375 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
376 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
379 * debugging crash code
380 if (gen == 250) {
381 void** test = NULL;
382 *test = 0;
386 #endif
388 /* Update the screen */
389 x11_copy_to_screen(This, NULL);
391 /* FPS support */
392 if (TRACE_ON(fps))
394 static long prev_time, frames;
396 DWORD time = GetTickCount();
397 frames++;
398 /* every 1.5 seconds */
399 if (time - prev_time > 1500) {
400 TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
401 prev_time = time;
402 frames = 0;
406 return WINED3D_OK;
409 /*****************************************************************************
410 * _Blt_ColorFill
412 * Helper function that fills a memory area with a specific color
414 * Params:
415 * buf: memory address to start filling at
416 * width, height: Dimensions of the area to fill
417 * bpp: Bit depth of the surface
418 * lPitch: pitch of the surface
419 * color: Color to fill with
421 *****************************************************************************/
422 static HRESULT
423 _Blt_ColorFill(BYTE *buf,
424 int width, int height,
425 int bpp, LONG lPitch,
426 DWORD color)
428 int x, y;
429 LPBYTE first;
431 /* Do first row */
433 #define COLORFILL_ROW(type) \
435 type *d = (type *) buf; \
436 for (x = 0; x < width; x++) \
437 d[x] = (type) color; \
438 break; \
440 switch(bpp)
442 case 1: COLORFILL_ROW(BYTE)
443 case 2: COLORFILL_ROW(WORD)
444 case 3:
446 BYTE *d = (BYTE *) buf;
447 for (x = 0; x < width; x++,d+=3)
449 d[0] = (color ) & 0xFF;
450 d[1] = (color>> 8) & 0xFF;
451 d[2] = (color>>16) & 0xFF;
453 break;
455 case 4: COLORFILL_ROW(DWORD)
456 default:
457 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
458 return WINED3DERR_NOTAVAILABLE;
461 #undef COLORFILL_ROW
463 /* Now copy first row */
464 first = buf;
465 for (y = 1; y < height; y++)
467 buf += lPitch;
468 memcpy(buf, first, width * bpp);
470 return WINED3D_OK;
473 /*****************************************************************************
474 * IWineD3DSurface::Blt, GDI version
476 * Performs blits to a surface, eigher from a source of source-less blts
477 * This is the main functionality of DirectDraw
479 * Params:
480 * DestRect: Destination rectangle to write to
481 * SrcSurface: Source surface, can be NULL
482 * SrcRect: Source rectangle
483 *****************************************************************************/
484 HRESULT WINAPI
485 IWineGDISurfaceImpl_Blt(IWineD3DSurface *iface,
486 RECT *DestRect,
487 IWineD3DSurface *SrcSurface,
488 RECT *SrcRect,
489 DWORD Flags,
490 WINEDDBLTFX *DDBltFx,
491 WINED3DTEXTUREFILTERTYPE Filter)
493 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
494 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
495 RECT xdst,xsrc;
496 HRESULT ret = WINED3D_OK;
497 WINED3DLOCKED_RECT dlock, slock;
498 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
499 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
500 int x, y;
501 const PixelFormatDesc *sEntry, *dEntry;
502 LPBYTE dbuf, sbuf;
503 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
505 if (TRACE_ON(d3d_surface))
507 if (DestRect) TRACE("\tdestrect :%dx%d-%dx%d\n",
508 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
509 if (SrcRect) TRACE("\tsrcrect :%dx%d-%dx%d\n",
510 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
511 #if 0
512 TRACE("\tflags: ");
513 DDRAW_dump_DDBLT(Flags);
514 if (Flags & WINEDDBLT_DDFX)
516 TRACE("\tblitfx: ");
517 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
519 #endif
522 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
524 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
525 return WINEDDERR_SURFACEBUSY;
528 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
529 /* Can happen when d3d9 apps do a StretchRect call which isn't handled in gl */
530 FIXME("Filters not supported in software blit\n");
533 if (Src == This)
535 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
536 dfmt = This->resource.format;
537 slock = dlock;
538 sfmt = dfmt;
539 sEntry = getFormatDescEntry(sfmt);
540 dEntry = sEntry;
542 else
544 if (Src)
546 IWineD3DSurface_LockRect(SrcSurface, &slock, NULL, WINED3DLOCK_READONLY);
547 sfmt = Src->resource.format;
549 sEntry = getFormatDescEntry(sfmt);
550 dfmt = This->resource.format;
551 dEntry = getFormatDescEntry(dfmt);
552 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
555 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~WINEDDBLT_DDFX;
557 if (sEntry->isFourcc && dEntry->isFourcc)
559 if (sfmt != dfmt)
561 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
562 ret = WINED3DERR_WRONGTEXTUREFORMAT;
563 goto release;
565 memcpy(dlock.pBits, slock.pBits, This->resource.size);
566 goto release;
569 if (sEntry->isFourcc && !dEntry->isFourcc)
571 FIXME("DXTC decompression not supported right now\n");
572 goto release;
575 if (DestRect)
577 memcpy(&xdst,DestRect,sizeof(xdst));
579 else
581 xdst.top = 0;
582 xdst.bottom = This->currentDesc.Height;
583 xdst.left = 0;
584 xdst.right = This->currentDesc.Width;
587 if (SrcRect)
589 memcpy(&xsrc,SrcRect,sizeof(xsrc));
591 else
593 if (Src)
595 xsrc.top = 0;
596 xsrc.bottom = Src->currentDesc.Height;
597 xsrc.left = 0;
598 xsrc.right = Src->currentDesc.Width;
600 else
602 memset(&xsrc,0,sizeof(xsrc));
606 /* First check for the validity of source / destination rectangles. This was
607 verified using a test application + by MSDN.
609 if ((Src != NULL) &&
610 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
611 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
612 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
613 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
614 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
616 WARN("Application gave us bad source rectangle for Blt.\n");
617 ret = WINEDDERR_INVALIDRECT;
618 goto release;
620 /* For the Destination rect, it can be out of bounds on the condition that a clipper
621 is set for the given surface.
623 if ((/*This->clipper == NULL*/ TRUE) &&
624 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
625 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
626 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
627 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
628 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
630 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
631 ret = WINEDDERR_INVALIDRECT;
632 goto release;
635 /* Now handle negative values in the rectangles. Warning: only supported for now
636 in the 'simple' cases (ie not in any stretching / rotation cases).
638 First, the case where nothing is to be done.
640 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
641 (xdst.top >= (int) This->currentDesc.Height) ||
642 (xdst.left >= (int) This->currentDesc.Width)) ||
643 ((Src != NULL) &&
644 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
645 (xsrc.top >= (int) Src->currentDesc.Height) ||
646 (xsrc.left >= (int) Src->currentDesc.Width)) ))
648 TRACE("Nothing to be done !\n");
649 goto release;
652 /* The easy case : the source-less blits.... */
653 if (Src == NULL)
655 RECT full_rect;
656 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
658 full_rect.left = 0;
659 full_rect.top = 0;
660 full_rect.right = This->currentDesc.Width;
661 full_rect.bottom = This->currentDesc.Height;
662 IntersectRect(&temp_rect, &full_rect, &xdst);
663 xdst = temp_rect;
665 else
667 /* Only handle clipping on the destination rectangle */
668 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
669 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
670 if (clip_vert || clip_horiz)
672 /* Now check if this is a special case or not... */
673 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
674 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
675 (Flags & WINEDDBLT_DDFX))
677 WARN("Out of screen rectangle in special case. Not handled right now.\n");
678 goto release;
681 if (clip_horiz)
683 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
684 if (xdst.right > This->currentDesc.Width)
686 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
687 xdst.right = (int) This->currentDesc.Width;
690 if (clip_vert)
692 if (xdst.top < 0)
694 xsrc.top -= xdst.top;
695 xdst.top = 0;
697 if (xdst.bottom > This->currentDesc.Height)
699 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
700 xdst.bottom = (int) This->currentDesc.Height;
703 /* And check if after clipping something is still to be done... */
704 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
705 (xdst.top >= (int) This->currentDesc.Height) ||
706 (xdst.left >= (int) This->currentDesc.Width) ||
707 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
708 (xsrc.top >= (int) Src->currentDesc.Height) ||
709 (xsrc.left >= (int) Src->currentDesc.Width))
711 TRACE("Nothing to be done after clipping !\n");
712 goto release;
717 bpp = This->bytesPerPixel;
718 srcheight = xsrc.bottom - xsrc.top;
719 srcwidth = xsrc.right - xsrc.left;
720 dstheight = xdst.bottom - xdst.top;
721 dstwidth = xdst.right - xdst.left;
722 width = (xdst.right - xdst.left) * bpp;
724 assert(width <= dlock.Pitch);
726 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
728 if (Flags & WINEDDBLT_WAIT)
730 Flags &= ~WINEDDBLT_WAIT;
732 if (Flags & WINEDDBLT_ASYNC)
734 static BOOL displayed = FALSE;
735 if (!displayed)
736 FIXME("Can't handle WINEDDBLT_ASYNC flag right now.\n");
737 displayed = TRUE;
738 Flags &= ~WINEDDBLT_ASYNC;
740 if (Flags & WINEDDBLT_DONOTWAIT)
742 /* WINEDDBLT_DONOTWAIT appeared in DX7 */
743 static BOOL displayed = FALSE;
744 if (!displayed)
745 FIXME("Can't handle WINEDDBLT_DONOTWAIT flag right now.\n");
746 displayed = TRUE;
747 Flags &= ~WINEDDBLT_DONOTWAIT;
750 /* First, all the 'source-less' blits */
751 if (Flags & WINEDDBLT_COLORFILL)
753 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
754 dlock.Pitch, DDBltFx->u5.dwFillColor);
755 Flags &= ~WINEDDBLT_COLORFILL;
758 if (Flags & WINEDDBLT_DEPTHFILL)
760 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
762 if (Flags & WINEDDBLT_ROP)
764 /* Catch some degenerate cases here */
765 switch(DDBltFx->dwROP)
767 case BLACKNESS:
768 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
769 break;
770 case 0xAA0029: /* No-op */
771 break;
772 case WHITENESS:
773 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
774 break;
775 case SRCCOPY: /* well, we do that below ? */
776 break;
777 default:
778 FIXME("Unsupported raster op: %08x Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
779 goto error;
781 Flags &= ~WINEDDBLT_ROP;
783 if (Flags & WINEDDBLT_DDROPS)
785 FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
787 /* Now the 'with source' blits */
788 if (Src)
790 LPBYTE sbase;
791 int sx, xinc, sy, yinc;
793 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
794 goto release;
795 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
796 xinc = (srcwidth << 16) / dstwidth;
797 yinc = (srcheight << 16) / dstheight;
799 if (!Flags)
801 /* No effects, we can cheat here */
802 if (dstwidth == srcwidth)
804 if (dstheight == srcheight)
806 /* No stretching in either direction. This needs to be as
807 * fast as possible */
808 sbuf = sbase;
810 /* check for overlapping surfaces */
811 if (SrcSurface != iface || xdst.top < xsrc.top ||
812 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
814 /* no overlap, or dst above src, so copy from top downwards */
815 for (y = 0; y < dstheight; y++)
817 memcpy(dbuf, sbuf, width);
818 sbuf += slock.Pitch;
819 dbuf += dlock.Pitch;
822 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
824 sbuf += (slock.Pitch*dstheight);
825 dbuf += (dlock.Pitch*dstheight);
826 for (y = 0; y < dstheight; y++)
828 sbuf -= slock.Pitch;
829 dbuf -= dlock.Pitch;
830 memcpy(dbuf, sbuf, width);
833 else /* src and dst overlapping on the same line, use memmove */
835 for (y = 0; y < dstheight; y++)
837 memmove(dbuf, sbuf, width);
838 sbuf += slock.Pitch;
839 dbuf += dlock.Pitch;
842 } else {
843 /* Stretching in Y direction only */
844 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
845 sbuf = sbase + (sy >> 16) * slock.Pitch;
846 memcpy(dbuf, sbuf, width);
847 dbuf += dlock.Pitch;
851 else
853 /* Stretching in X direction */
854 int last_sy = -1;
855 for (y = sy = 0; y < dstheight; y++, sy += yinc)
857 sbuf = sbase + (sy >> 16) * slock.Pitch;
859 if ((sy >> 16) == (last_sy >> 16))
861 /* this sourcerow is the same as last sourcerow -
862 * copy already stretched row
864 memcpy(dbuf, dbuf - dlock.Pitch, width);
866 else
868 #define STRETCH_ROW(type) { \
869 type *s = (type *) sbuf, *d = (type *) dbuf; \
870 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
871 d[x] = s[sx >> 16]; \
872 break; }
874 switch(bpp)
876 case 1: STRETCH_ROW(BYTE)
877 case 2: STRETCH_ROW(WORD)
878 case 4: STRETCH_ROW(DWORD)
879 case 3:
881 LPBYTE s,d = dbuf;
882 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
884 DWORD pixel;
886 s = sbuf+3*(sx>>16);
887 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
888 d[0] = (pixel )&0xff;
889 d[1] = (pixel>> 8)&0xff;
890 d[2] = (pixel>>16)&0xff;
891 d+=3;
893 break;
895 default:
896 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
897 ret = WINED3DERR_NOTAVAILABLE;
898 goto error;
900 #undef STRETCH_ROW
902 dbuf += dlock.Pitch;
903 last_sy = sy;
907 else
909 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
910 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
911 DWORD destkeylow = 0x0, destkeyhigh = 0xFFFFFFFF, destkeymask = 0xFFFFFFFF;
912 if (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE))
914 /* The color keying flags are checked for correctness in ddraw */
915 if (Flags & WINEDDBLT_KEYSRC)
917 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
918 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
920 else if (Flags & WINEDDBLT_KEYSRCOVERRIDE)
922 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
923 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
926 if (Flags & WINEDDBLT_KEYDEST)
928 /* Destination color keys are taken from the source surface ! */
929 destkeylow = Src->DestBltCKey.dwColorSpaceLowValue;
930 destkeyhigh = Src->DestBltCKey.dwColorSpaceHighValue;
932 else if (Flags & WINEDDBLT_KEYDESTOVERRIDE)
934 destkeylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
935 destkeyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
938 if(bpp == 1)
940 keymask = 0xff;
942 else
944 keymask = sEntry->redMask |
945 sEntry->greenMask |
946 sEntry->blueMask;
948 Flags &= ~(WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE);
951 if (Flags & WINEDDBLT_DDFX)
953 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
954 LONG tmpxy;
955 dTopLeft = dbuf;
956 dTopRight = dbuf+((dstwidth-1)*bpp);
957 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
958 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
960 if (DDBltFx->dwDDFX & WINEDDBLTFX_ARITHSTRETCHY)
962 /* I don't think we need to do anything about this flag */
963 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
965 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORLEFTRIGHT)
967 tmp = dTopRight;
968 dTopRight = dTopLeft;
969 dTopLeft = tmp;
970 tmp = dBottomRight;
971 dBottomRight = dBottomLeft;
972 dBottomLeft = tmp;
973 dstxinc = dstxinc *-1;
975 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORUPDOWN)
977 tmp = dTopLeft;
978 dTopLeft = dBottomLeft;
979 dBottomLeft = tmp;
980 tmp = dTopRight;
981 dTopRight = dBottomRight;
982 dBottomRight = tmp;
983 dstyinc = dstyinc *-1;
985 if (DDBltFx->dwDDFX & WINEDDBLTFX_NOTEARING)
987 /* I don't think we need to do anything about this flag */
988 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
990 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE180)
992 tmp = dBottomRight;
993 dBottomRight = dTopLeft;
994 dTopLeft = tmp;
995 tmp = dBottomLeft;
996 dBottomLeft = dTopRight;
997 dTopRight = tmp;
998 dstxinc = dstxinc * -1;
999 dstyinc = dstyinc * -1;
1001 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE270)
1003 tmp = dTopLeft;
1004 dTopLeft = dBottomLeft;
1005 dBottomLeft = dBottomRight;
1006 dBottomRight = dTopRight;
1007 dTopRight = tmp;
1008 tmpxy = dstxinc;
1009 dstxinc = dstyinc;
1010 dstyinc = tmpxy;
1011 dstxinc = dstxinc * -1;
1013 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE90)
1015 tmp = dTopLeft;
1016 dTopLeft = dTopRight;
1017 dTopRight = dBottomRight;
1018 dBottomRight = dBottomLeft;
1019 dBottomLeft = tmp;
1020 tmpxy = dstxinc;
1021 dstxinc = dstyinc;
1022 dstyinc = tmpxy;
1023 dstyinc = dstyinc * -1;
1025 if (DDBltFx->dwDDFX & WINEDDBLTFX_ZBUFFERBASEDEST)
1027 /* I don't think we need to do anything about this flag */
1028 WARN("Flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
1030 dbuf = dTopLeft;
1031 Flags &= ~(WINEDDBLT_DDFX);
1034 #define COPY_COLORKEY_FX(type) { \
1035 type *s, *d = (type *) dbuf, *dx, tmp; \
1036 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1037 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1038 dx = d; \
1039 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1040 tmp = s[sx >> 16]; \
1041 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1042 ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1043 dx[0] = tmp; \
1045 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1047 d = (type*)(((LPBYTE)d)+dstyinc); \
1049 break; }
1051 switch (bpp) {
1052 case 1: COPY_COLORKEY_FX(BYTE)
1053 case 2: COPY_COLORKEY_FX(WORD)
1054 case 4: COPY_COLORKEY_FX(DWORD)
1055 case 3:
1057 LPBYTE s,d = dbuf, dx;
1058 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1060 sbuf = sbase + (sy >> 16) * slock.Pitch;
1061 dx = d;
1062 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1064 DWORD pixel, dpixel = 0;
1065 s = sbuf+3*(sx>>16);
1066 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1067 dpixel = dx[0]|(dx[1]<<8)|(dx[2]<<16);
1068 if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) &&
1069 ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
1071 dx[0] = (pixel )&0xff;
1072 dx[1] = (pixel>> 8)&0xff;
1073 dx[2] = (pixel>>16)&0xff;
1075 dx+= dstxinc;
1077 d += dstyinc;
1079 break;
1081 default:
1082 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1083 (Flags & WINEDDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1084 ret = WINED3DERR_NOTAVAILABLE;
1085 goto error;
1086 #undef COPY_COLORKEY_FX
1091 error:
1092 if (Flags && FIXME_ON(d3d_surface))
1094 FIXME("\tUnsupported flags: %08x\n", Flags);
1097 release:
1098 IWineD3DSurface_UnlockRect(iface);
1099 if (SrcSurface && SrcSurface != iface) IWineD3DSurface_UnlockRect(SrcSurface);
1100 return ret;
1103 /*****************************************************************************
1104 * IWineD3DSurface::BltFast, GDI version
1106 * This is the software implementation of BltFast, as used by GDI surfaces
1107 * and as a fallback for OpenGL surfaces. This code is taken from the old
1108 * DirectDraw code, and was originally written by TransGaming.
1110 * Params:
1111 * dstx:
1112 * dsty:
1113 * Source: Source surface to copy from
1114 * rsrc: Source rectangle
1115 * trans: Some Flags
1117 * Returns:
1118 * WINED3D_OK on success
1120 *****************************************************************************/
1121 HRESULT WINAPI
1122 IWineGDISurfaceImpl_BltFast(IWineD3DSurface *iface,
1123 DWORD dstx,
1124 DWORD dsty,
1125 IWineD3DSurface *Source,
1126 RECT *rsrc,
1127 DWORD trans)
1129 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1130 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1132 int bpp, w, h, x, y;
1133 WINED3DLOCKED_RECT dlock,slock;
1134 HRESULT ret = WINED3D_OK;
1135 RECT rsrc2;
1136 RECT lock_src, lock_dst, lock_union;
1137 BYTE *sbuf, *dbuf;
1138 const PixelFormatDesc *sEntry, *dEntry;
1140 if (TRACE_ON(d3d_surface))
1142 TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This,dstx,dsty,Src,rsrc,trans);
1144 if (rsrc)
1146 TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,
1147 rsrc->right,rsrc->bottom);
1149 else
1151 TRACE(" srcrect: NULL\n");
1155 if ((This->Flags & SFLAG_LOCKED) ||
1156 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1158 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1159 return WINEDDERR_SURFACEBUSY;
1162 if (!rsrc)
1164 WARN("rsrc is NULL!\n");
1165 rsrc = &rsrc2;
1166 rsrc->left = 0;
1167 rsrc->top = 0;
1168 rsrc->right = Src->currentDesc.Width;
1169 rsrc->bottom = Src->currentDesc.Height;
1172 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1173 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1174 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1175 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1176 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1177 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1179 WARN("Application gave us bad source rectangle for BltFast.\n");
1180 return WINEDDERR_INVALIDRECT;
1183 h = rsrc->bottom - rsrc->top;
1184 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1185 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1186 if (h <= 0) return WINEDDERR_INVALIDRECT;
1188 w = rsrc->right - rsrc->left;
1189 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1190 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1191 if (w <= 0) return WINEDDERR_INVALIDRECT;
1193 /* Now compute the locking rectangle... */
1194 lock_src.left = rsrc->left;
1195 lock_src.top = rsrc->top;
1196 lock_src.right = lock_src.left + w;
1197 lock_src.bottom = lock_src.top + h;
1199 lock_dst.left = dstx;
1200 lock_dst.top = dsty;
1201 lock_dst.right = dstx + w;
1202 lock_dst.bottom = dsty + h;
1204 bpp = This->bytesPerPixel;
1206 /* We need to lock the surfaces, or we won't get refreshes when done. */
1207 if (Src == This)
1209 int pitch;
1211 UnionRect(&lock_union, &lock_src, &lock_dst);
1213 /* Lock the union of the two rectangles */
1214 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1215 if(ret != WINED3D_OK) goto error;
1217 pitch = dlock.Pitch;
1218 slock.Pitch = dlock.Pitch;
1220 /* Since slock was originally copied from this surface's description, we can just reuse it */
1221 assert(This->resource.allocatedMemory != NULL);
1222 sbuf = (BYTE *)This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1223 dbuf = (BYTE *)This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1224 sEntry = getFormatDescEntry(Src->resource.format);
1225 dEntry = sEntry;
1227 else
1229 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, WINED3DLOCK_READONLY);
1230 if(ret != WINED3D_OK) goto error;
1231 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1232 if(ret != WINED3D_OK) goto error;
1234 sbuf = slock.pBits;
1235 dbuf = dlock.pBits;
1236 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1238 sEntry = getFormatDescEntry(Src->resource.format);
1239 dEntry = getFormatDescEntry(This->resource.format);
1242 /* Handle first the FOURCC surfaces... */
1243 if (sEntry->isFourcc && dEntry->isFourcc)
1245 TRACE("Fourcc -> Fourcc copy\n");
1246 if (trans)
1247 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1248 if (dstx || dsty)
1249 FIXME("offset for destination surface is not supported\n");
1250 if (Src->resource.format != This->resource.format)
1252 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1253 ret = WINED3DERR_WRONGTEXTUREFORMAT;
1254 goto error;
1256 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1257 memcpy(dbuf, sbuf, This->resource.size);
1258 goto error;
1260 if (sEntry->isFourcc && !dEntry->isFourcc)
1262 /* TODO: Use the libtxc_dxtn.so shared library to do
1263 * software decompression
1265 ERR("DXTC decompression not supported by now\n");
1266 goto error;
1269 if (trans & (WINEDDBLTFAST_SRCCOLORKEY | WINEDDBLTFAST_DESTCOLORKEY))
1271 DWORD keylow, keyhigh;
1272 TRACE("Color keyed copy\n");
1273 if (trans & WINEDDBLTFAST_SRCCOLORKEY)
1275 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1276 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1278 else
1280 /* I'm not sure if this is correct */
1281 FIXME("WINEDDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1282 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1283 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1286 #define COPYBOX_COLORKEY(type) { \
1287 type *d, *s, tmp; \
1288 s = (type *) sbuf; \
1289 d = (type *) dbuf; \
1290 for (y = 0; y < h; y++) { \
1291 for (x = 0; x < w; x++) { \
1292 tmp = s[x]; \
1293 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1295 s = (type *)((BYTE *)s + slock.Pitch); \
1296 d = (type *)((BYTE *)d + dlock.Pitch); \
1298 break; \
1301 switch (bpp) {
1302 case 1: COPYBOX_COLORKEY(BYTE)
1303 case 2: COPYBOX_COLORKEY(WORD)
1304 case 4: COPYBOX_COLORKEY(DWORD)
1305 case 3:
1307 BYTE *d, *s;
1308 DWORD tmp;
1309 s = (BYTE *) sbuf;
1310 d = (BYTE *) dbuf;
1311 for (y = 0; y < h; y++)
1313 for (x = 0; x < w * 3; x += 3)
1315 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1316 if (tmp < keylow || tmp > keyhigh)
1318 d[x + 0] = s[x + 0];
1319 d[x + 1] = s[x + 1];
1320 d[x + 2] = s[x + 2];
1323 s += slock.Pitch;
1324 d += dlock.Pitch;
1326 break;
1328 default:
1329 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1330 ret = WINED3DERR_NOTAVAILABLE;
1331 goto error;
1333 #undef COPYBOX_COLORKEY
1334 TRACE("Copy Done\n");
1336 else
1338 int width = w * bpp;
1339 TRACE("NO color key copy\n");
1340 for (y = 0; y < h; y++)
1342 /* This is pretty easy, a line for line memcpy */
1343 memcpy(dbuf, sbuf, width);
1344 sbuf += slock.Pitch;
1345 dbuf += dlock.Pitch;
1347 TRACE("Copy done\n");
1350 error:
1351 if (Src == This)
1353 IWineD3DSurface_UnlockRect(iface);
1355 else
1357 IWineD3DSurface_UnlockRect(iface);
1358 IWineD3DSurface_UnlockRect(Source);
1361 return ret;
1364 /*****************************************************************************
1365 * IWineD3DSurface::LoadTexture, GDI version
1367 * This is mutually unsupported by GDI surfaces
1369 * Returns:
1370 * D3DERR_INVALIDCALL
1372 *****************************************************************************/
1373 HRESULT WINAPI
1374 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode)
1376 ERR("Unsupported on X11 surfaces\n");
1377 return WINED3DERR_INVALIDCALL;
1380 /*****************************************************************************
1381 * IWineD3DSurface::SaveSnapshot, GDI version
1383 * This method writes the surface's contents to the in tga format to the
1384 * file specified in filename.
1386 * Params:
1387 * filename: File to write to
1389 * Returns:
1390 * WINED3DERR_INVALIDCALL if the file couldn't be opened
1391 * WINED3D_OK on success
1393 *****************************************************************************/
1394 static int get_shift(DWORD color_mask) {
1395 int shift = 0;
1396 while (color_mask > 0xFF) {
1397 color_mask >>= 1;
1398 shift += 1;
1400 while ((color_mask & 0x80) == 0) {
1401 color_mask <<= 1;
1402 shift -= 1;
1404 return shift;
1408 HRESULT WINAPI
1409 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
1410 const char* filename)
1412 FILE* f = NULL;
1413 UINT y = 0, x = 0;
1414 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1415 static char *output = NULL;
1416 static int size = 0;
1417 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1419 if (This->pow2Width > size) {
1420 output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
1421 size = This->pow2Width;
1425 f = fopen(filename, "w+");
1426 if (NULL == f) {
1427 ERR("opening of %s failed with\n", filename);
1428 return WINED3DERR_INVALIDCALL;
1430 fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
1432 if (This->resource.format == WINED3DFMT_P8) {
1433 unsigned char table[256][3];
1434 int i;
1436 if (This->palette == NULL) {
1437 fclose(f);
1438 return WINED3DERR_INVALIDCALL;
1440 for (i = 0; i < 256; i++) {
1441 table[i][0] = This->palette->palents[i].peRed;
1442 table[i][1] = This->palette->palents[i].peGreen;
1443 table[i][2] = This->palette->palents[i].peBlue;
1445 for (y = 0; y < This->pow2Height; y++) {
1446 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1447 for (x = 0; x < This->pow2Width; x++) {
1448 unsigned char color = *src;
1449 src += 1;
1451 output[3 * x + 0] = table[color][0];
1452 output[3 * x + 1] = table[color][1];
1453 output[3 * x + 2] = table[color][2];
1455 fwrite(output, 3 * This->pow2Width, 1, f);
1457 } else {
1458 int red_shift, green_shift, blue_shift, pix_width;
1460 pix_width = This->bytesPerPixel;
1462 red_shift = get_shift(formatEntry->redMask);
1463 green_shift = get_shift(formatEntry->greenMask);
1464 blue_shift = get_shift(formatEntry->blueMask);
1466 for (y = 0; y < This->pow2Height; y++) {
1467 unsigned char *src = (unsigned char *) This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
1468 for (x = 0; x < This->pow2Width; x++) {
1469 unsigned int color;
1470 unsigned int comp;
1471 int i;
1473 color = 0;
1474 for (i = 0; i < pix_width; i++) {
1475 color |= src[i] << (8 * i);
1477 src += 1 * pix_width;
1479 comp = color & formatEntry->redMask;
1480 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
1481 comp = color & formatEntry->greenMask;
1482 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
1483 comp = color & formatEntry->blueMask;
1484 output[3 * x + 2] = blue_shift > 0 ? comp >> blue_shift : comp << -blue_shift;
1486 fwrite(output, 3 * This->pow2Width, 1, f);
1489 fclose(f);
1490 return WINED3D_OK;
1493 /*****************************************************************************
1494 * IWineD3DSurface::PrivateSetup, GDI version
1496 * Initializes the GDI surface, aka creates the DIB section we render to
1497 * The DIB section creation is done by calling GetDC, which will create the
1498 * section and releasing the dc to allow the app to use it. The dib section
1499 * will stay until the surface is released
1501 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
1502 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
1503 * avoid confusion in the shared surface code.
1505 * Returns:
1506 * WINED3D_OK on success
1507 * The return values of called methods on failure
1509 *****************************************************************************/
1510 HRESULT WINAPI
1511 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
1513 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1514 HRESULT hr;
1515 HDC hdc;
1517 if(This->resource.usage & WINED3DUSAGE_OVERLAY)
1519 ERR("(%p) Overlays not yet supported by GDI surfaces\n", This);
1520 return WINED3DERR_INVALIDCALL;
1522 /* Sysmem textures have memory already allocated -
1523 * release it, this avoids an unnecessary memcpy
1525 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
1526 This->resource.allocatedMemory = NULL;
1528 /* We don't mind the nonpow2 stuff in GDI */
1529 This->pow2Width = This->currentDesc.Width;
1530 This->pow2Height = This->currentDesc.Height;
1532 /* Call GetDC to create a DIB section. We will use that
1533 * DIB section for rendering
1535 * Release the DC afterwards to allow the app to use it
1537 hr = IWineD3DSurface_GetDC(iface, &hdc);
1538 if(FAILED(hr))
1540 ERR("(%p) IWineD3DSurface::GetDC failed with hr %08x\n", This, hr);
1541 return hr;
1543 hr = IWineD3DSurface_ReleaseDC(iface, hdc);
1544 if(FAILED(hr))
1546 ERR("(%p) IWineD3DSurface::ReleaseDC failed with hr %08x\n", This, hr);
1547 return hr;
1550 return WINED3D_OK;
1553 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
1555 /* IUnknown */
1556 IWineD3DSurfaceImpl_QueryInterface,
1557 IWineD3DSurfaceImpl_AddRef,
1558 IWineD3DSurfaceImpl_Release,
1559 /* IWineD3DResource */
1560 IWineD3DSurfaceImpl_GetParent,
1561 IWineD3DSurfaceImpl_GetDevice,
1562 IWineD3DSurfaceImpl_SetPrivateData,
1563 IWineD3DSurfaceImpl_GetPrivateData,
1564 IWineD3DSurfaceImpl_FreePrivateData,
1565 IWineD3DSurfaceImpl_SetPriority,
1566 IWineD3DSurfaceImpl_GetPriority,
1567 IWineGDISurfaceImpl_PreLoad,
1568 IWineD3DSurfaceImpl_GetType,
1569 /* IWineD3DSurface */
1570 IWineD3DSurfaceImpl_GetContainer,
1571 IWineD3DSurfaceImpl_GetDesc,
1572 IWineGDISurfaceImpl_LockRect,
1573 IWineGDISurfaceImpl_UnlockRect,
1574 IWineD3DSurfaceImpl_GetDC,
1575 IWineD3DSurfaceImpl_ReleaseDC,
1576 IWineGDISurfaceImpl_Flip,
1577 IWineGDISurfaceImpl_Blt,
1578 IWineD3DSurfaceImpl_GetBltStatus,
1579 IWineD3DSurfaceImpl_GetFlipStatus,
1580 IWineD3DSurfaceImpl_IsLost,
1581 IWineD3DSurfaceImpl_Restore,
1582 IWineGDISurfaceImpl_BltFast,
1583 IWineD3DSurfaceImpl_GetPalette,
1584 IWineD3DSurfaceImpl_SetPalette,
1585 IWineD3DSurfaceImpl_RealizePalette,
1586 IWineD3DSurfaceImpl_SetColorKey,
1587 IWineD3DSurfaceImpl_GetPitch,
1588 IWineD3DSurfaceImpl_SetMem,
1589 IWineD3DSurfaceImpl_SetOverlayPosition,
1590 IWineD3DSurfaceImpl_GetOverlayPosition,
1591 IWineD3DSurfaceImpl_UpdateOverlayZOrder,
1592 IWineD3DSurfaceImpl_UpdateOverlay,
1593 IWineD3DSurfaceImpl_SetClipper,
1594 IWineD3DSurfaceImpl_GetClipper,
1595 /* Internal use: */
1596 IWineD3DSurfaceImpl_AddDirtyRect,
1597 IWineGDISurfaceImpl_LoadTexture,
1598 IWineGDISurfaceImpl_SaveSnapshot,
1599 IWineD3DSurfaceImpl_SetContainer,
1600 IWineD3DSurfaceImpl_SetGlTextureDesc,
1601 IWineD3DSurfaceImpl_GetGlDesc,
1602 IWineD3DSurfaceImpl_GetData,
1603 IWineD3DSurfaceImpl_SetFormat,
1604 IWineGDISurfaceImpl_PrivateSetup