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-2008 Stefan Dösinger
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
30 #include "wined3d_private.h"
34 /* Use the d3d_surface debug channel to have one channel for all surfaces */
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
37 void surface_gdi_cleanup(IWineD3DSurfaceImpl *This)
39 TRACE("(%p) : Cleaning up.\n", This);
41 if (This->Flags & SFLAG_DIBSECTION)
44 SelectObject(This->hDC, This->dib.holdbitmap);
46 /* Release the DIB section. */
47 DeleteObject(This->dib.DIBsection);
48 This->dib.bitmap_data = NULL;
49 This->resource.allocatedMemory = NULL;
52 if (This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
53 if (This->overlay_dest) list_remove(&This->overlay_entry);
55 HeapFree(GetProcessHeap(), 0, This->palette9);
57 resource_cleanup((IWineD3DResource *)This);
60 /*****************************************************************************
61 * IWineD3DSurface::Release, GDI version
63 * In general a normal COM Release method, but the GDI version doesn't have
64 * to destroy all the GL things.
66 *****************************************************************************/
67 static ULONG WINAPI IWineGDISurfaceImpl_Release(IWineD3DSurface *iface) {
68 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
69 ULONG ref = InterlockedDecrement(&This->resource.ref);
70 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
74 surface_gdi_cleanup(This);
76 TRACE("(%p) Released.\n", This);
77 HeapFree(GetProcessHeap(), 0, This);
83 /*****************************************************************************
84 * IWineD3DSurface::PreLoad, GDI version
86 * This call is unsupported on GDI surfaces, if it's called something went
87 * wrong in the parent library. Write an informative warning
89 *****************************************************************************/
91 IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
93 ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
94 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
95 ERR("(%p): Please report to wine-devel\n", iface);
98 /*****************************************************************************
99 * IWineD3DSurface::UnLoad, GDI version
101 * This call is unsupported on GDI surfaces, if it's called something went
102 * wrong in the parent library. Write an informative warning.
104 *****************************************************************************/
105 static void WINAPI IWineGDISurfaceImpl_UnLoad(IWineD3DSurface *iface)
107 ERR("(%p): UnLoad is not supported on X11 surfaces!\n", iface);
108 ERR("(%p): Most likely the parent library did something wrong.\n", iface);
109 ERR("(%p): Please report to wine-devel\n", iface);
112 /*****************************************************************************
113 * IWineD3DSurface::LockRect, GDI version
115 * Locks the surface and returns a pointer to the surface memory
118 * pLockedRect: Address to return the locking info at
119 * pRect: Rectangle to lock
123 * WINED3D_OK on success
124 * WINED3DERR_INVALIDCALL on errors
126 *****************************************************************************/
127 static HRESULT WINAPI
128 IWineGDISurfaceImpl_LockRect(IWineD3DSurface *iface,
129 WINED3DLOCKED_RECT* pLockedRect,
133 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
135 /* Already locked? */
136 if(This->Flags & SFLAG_LOCKED)
138 WARN("(%p) Surface already locked\n", This);
139 /* What should I return here? */
140 return WINED3DERR_INVALIDCALL;
142 This->Flags |= SFLAG_LOCKED;
144 if(!This->resource.allocatedMemory) {
145 /* This happens on gdi surfaces if the application set a user pointer and resets it.
146 * Recreate the DIB section
148 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
149 This->resource.allocatedMemory = This->dib.bitmap_data;
152 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
155 /*****************************************************************************
156 * IWineD3DSurface::UnlockRect, GDI version
158 * Unlocks a surface. This implementation doesn't do much, except updating
159 * the window if the front buffer is unlocked
162 * WINED3D_OK on success
163 * WINED3DERR_INVALIDCALL on failure
165 *****************************************************************************/
166 static HRESULT WINAPI
167 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
169 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
170 IWineD3DSwapChainImpl *swapchain = NULL;
171 TRACE("(%p)\n", This);
173 if (!(This->Flags & SFLAG_LOCKED))
175 WARN("trying to Unlock an unlocked surf@%p\n", This);
176 return WINEDDERR_NOTLOCKED;
179 /* Can be useful for debugging */
182 static unsigned int gen = 0;
185 if ((gen % 10) == 0) {
186 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm",
187 This, This->texture_target, This->texture_level, gen);
188 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
191 * debugging crash code
200 /* Tell the swapchain to update the screen */
201 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain)))
203 if(iface == swapchain->frontBuffer)
205 x11_copy_to_screen(swapchain, &This->lockedRect);
207 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
210 This->Flags &= ~SFLAG_LOCKED;
211 memset(&This->lockedRect, 0, sizeof(RECT));
215 /*****************************************************************************
216 * IWineD3DSurface::Flip, GDI version
218 * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
219 * the parent library. This implementation changes the data pointers of the
220 * surfaces and copies the new front buffer content to the screen
223 * override: Flipping target(e.g. back buffer)
226 * WINED3D_OK on success
228 *****************************************************************************/
229 static HRESULT WINAPI
230 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
231 IWineD3DSurface *override,
234 IWineD3DSwapChainImpl *swapchain = NULL;
237 if(FAILED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain)))
239 ERR("Flipped surface is not on a swapchain\n");
240 return WINEDDERR_NOTFLIPPABLE;
243 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
244 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
248 /*****************************************************************************
249 * IWineD3DSurface::LoadTexture, GDI version
251 * This is mutually unsupported by GDI surfaces
256 *****************************************************************************/
257 static HRESULT WINAPI
258 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode)
260 ERR("Unsupported on X11 surfaces\n");
261 return WINED3DERR_INVALIDCALL;
264 /*****************************************************************************
265 * IWineD3DSurface::SaveSnapshot, GDI version
267 * This method writes the surface's contents to the in tga format to the
268 * file specified in filename.
271 * filename: File to write to
274 * WINED3DERR_INVALIDCALL if the file couldn't be opened
275 * WINED3D_OK on success
277 *****************************************************************************/
278 static int get_shift(DWORD color_mask) {
280 while (color_mask > 0xFF) {
284 while ((color_mask & 0x80) == 0) {
292 static HRESULT WINAPI
293 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
294 const char* filename)
298 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
299 static char *output = NULL;
300 static UINT size = 0;
301 const struct GlPixelFormatDesc *format_desc = This->resource.format_desc;
303 if (This->pow2Width > size) {
304 output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
305 size = This->pow2Width;
309 f = fopen(filename, "w+");
311 ERR("opening of %s failed with\n", filename);
312 return WINED3DERR_INVALIDCALL;
314 fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
316 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT)
318 unsigned char table[256][3];
321 if (This->palette == NULL) {
323 return WINED3DERR_INVALIDCALL;
325 for (i = 0; i < 256; i++) {
326 table[i][0] = This->palette->palents[i].peRed;
327 table[i][1] = This->palette->palents[i].peGreen;
328 table[i][2] = This->palette->palents[i].peBlue;
330 for (y = 0; y < This->pow2Height; y++) {
331 unsigned char *src = This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
332 for (x = 0; x < This->pow2Width; x++) {
333 unsigned char color = *src;
336 output[3 * x + 0] = table[color][0];
337 output[3 * x + 1] = table[color][1];
338 output[3 * x + 2] = table[color][2];
340 fwrite(output, 3 * This->pow2Width, 1, f);
343 int red_shift, green_shift, blue_shift, pix_width, alpha_shift;
345 pix_width = format_desc->byte_count;
347 red_shift = get_shift(format_desc->red_mask);
348 green_shift = get_shift(format_desc->green_mask);
349 blue_shift = get_shift(format_desc->blue_mask);
350 alpha_shift = get_shift(format_desc->alpha_mask);
352 for (y = 0; y < This->pow2Height; y++) {
353 const unsigned char *src = This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
354 for (x = 0; x < This->pow2Width; x++) {
360 for (i = 0; i < pix_width; i++) {
361 color |= src[i] << (8 * i);
363 src += 1 * pix_width;
365 comp = color & format_desc->red_mask;
366 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
367 comp = color & format_desc->green_mask;
368 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
369 comp = color & format_desc->alpha_mask;
370 output[3 * x + 2] = alpha_shift > 0 ? comp >> alpha_shift : comp << -alpha_shift;
372 fwrite(output, 3 * This->pow2Width, 1, f);
379 static HRESULT WINAPI IWineGDISurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
380 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
381 WINED3DLOCKED_RECT lock;
385 TRACE("(%p)->(%p)\n",This,pHDC);
387 if(!(This->Flags & SFLAG_DIBSECTION))
389 WARN("DC not supported on this surface\n");
390 return WINED3DERR_INVALIDCALL;
393 if(This->Flags & SFLAG_USERPTR) {
394 ERR("Not supported on surfaces with an application-provided surfaces\n");
395 return WINEDDERR_NODC;
398 /* Give more detailed info for ddraw */
399 if (This->Flags & SFLAG_DCINUSE)
400 return WINEDDERR_DCALREADYCREATED;
402 /* Can't GetDC if the surface is locked */
403 if (This->Flags & SFLAG_LOCKED)
404 return WINED3DERR_INVALIDCALL;
406 memset(&lock, 0, sizeof(lock)); /* To be sure */
408 /* Should have a DIB section already */
410 /* Lock the surface */
411 hr = IWineD3DSurface_LockRect(iface,
416 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
417 /* keep the dib section */
421 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
422 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
425 const PALETTEENTRY *pal = NULL;
428 pal = This->palette->palents;
430 IWineD3DSurfaceImpl *dds_primary;
431 IWineD3DSwapChainImpl *swapchain;
432 swapchain = (IWineD3DSwapChainImpl *)This->resource.wineD3DDevice->swapchains[0];
433 dds_primary = (IWineD3DSurfaceImpl *)swapchain->frontBuffer;
434 if (dds_primary && dds_primary->palette)
435 pal = dds_primary->palette->palents;
439 for (n=0; n<256; n++) {
440 col[n].rgbRed = pal[n].peRed;
441 col[n].rgbGreen = pal[n].peGreen;
442 col[n].rgbBlue = pal[n].peBlue;
443 col[n].rgbReserved = 0;
445 SetDIBColorTable(This->hDC, 0, 256, col);
450 TRACE("returning %p\n",*pHDC);
451 This->Flags |= SFLAG_DCINUSE;
456 static HRESULT WINAPI IWineGDISurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
457 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
459 TRACE("(%p)->(%p)\n",This,hDC);
461 if (!(This->Flags & SFLAG_DCINUSE))
462 return WINEDDERR_NODC;
464 if (This->hDC !=hDC) {
465 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
466 return WINEDDERR_NODC;
469 /* we locked first, so unlock now */
470 IWineD3DSurface_UnlockRect(iface);
472 This->Flags &= ~SFLAG_DCINUSE;
477 static HRESULT WINAPI IWineGDISurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
478 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
480 IWineD3DPaletteImpl *pal = This->palette;
482 IWineD3DSwapChainImpl *swapchain;
483 TRACE("(%p)\n", This);
485 if (!pal) return WINED3D_OK;
487 if(This->Flags & SFLAG_DIBSECTION) {
488 TRACE("(%p): Updating the hdc's palette\n", This);
489 for (n=0; n<256; n++) {
490 col[n].rgbRed = pal->palents[n].peRed;
491 col[n].rgbGreen = pal->palents[n].peGreen;
492 col[n].rgbBlue = pal->palents[n].peBlue;
493 col[n].rgbReserved = 0;
495 SetDIBColorTable(This->hDC, 0, 256, col);
498 /* Update the image because of the palette change. Some games like e.g Red Alert
499 call SetEntries a lot to implement fading. */
500 /* Tell the swapchain to update the screen */
501 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain)))
503 if(iface == swapchain->frontBuffer)
505 x11_copy_to_screen(swapchain, NULL);
507 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
513 /*****************************************************************************
514 * IWineD3DSurface::PrivateSetup, GDI version
516 * Initializes the GDI surface, aka creates the DIB section we render to
517 * The DIB section creation is done by calling GetDC, which will create the
518 * section and releasing the dc to allow the app to use it. The dib section
519 * will stay until the surface is released
521 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
522 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
523 * avoid confusion in the shared surface code.
526 * WINED3D_OK on success
527 * The return values of called methods on failure
529 *****************************************************************************/
530 static HRESULT WINAPI
531 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
533 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
536 if(This->resource.usage & WINED3DUSAGE_OVERLAY)
538 ERR("(%p) Overlays not yet supported by GDI surfaces\n", This);
539 return WINED3DERR_INVALIDCALL;
542 /* Sysmem textures have memory already allocated -
543 * release it, this avoids an unnecessary memcpy
545 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
548 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
549 This->resource.heapMemory = NULL;
550 This->resource.allocatedMemory = This->dib.bitmap_data;
553 /* We don't mind the nonpow2 stuff in GDI */
554 This->pow2Width = This->currentDesc.Width;
555 This->pow2Height = This->currentDesc.Height;
560 static HRESULT WINAPI IWineGDISurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
561 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
563 /* Render targets depend on their hdc, and we can't create an hdc on a user pointer */
564 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
565 ERR("Not supported on render targets\n");
566 return WINED3DERR_INVALIDCALL;
569 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
570 WARN("Surface is locked or the HDC is in use\n");
571 return WINED3DERR_INVALIDCALL;
574 if(Mem && Mem != This->resource.allocatedMemory) {
575 void *release = NULL;
577 /* Do I have to copy the old surface content? */
578 if(This->Flags & SFLAG_DIBSECTION) {
579 /* Release the DC. No need to hold the critical section for the update
580 * Thread because this thread runs only on front buffers, but this method
581 * fails for render targets in the check above.
583 SelectObject(This->hDC, This->dib.holdbitmap);
585 /* Release the DIB section */
586 DeleteObject(This->dib.DIBsection);
587 This->dib.bitmap_data = NULL;
588 This->resource.allocatedMemory = NULL;
590 This->Flags &= ~SFLAG_DIBSECTION;
591 } else if(!(This->Flags & SFLAG_USERPTR)) {
592 release = This->resource.allocatedMemory;
594 This->resource.allocatedMemory = Mem;
595 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
597 /* Now free the old memory if any */
598 HeapFree(GetProcessHeap(), 0, release);
599 } else if(This->Flags & SFLAG_USERPTR) {
600 /* LockRect and GetDC will re-create the dib section and allocated memory */
601 This->resource.allocatedMemory = NULL;
602 This->Flags &= ~SFLAG_USERPTR;
607 /***************************
609 ***************************/
610 static void WINAPI IWineGDISurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
611 TRACE("(%p)->(%s, %s)\n", iface,
612 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
613 persistent ? "TRUE" : "FALSE");
614 /* GDI surfaces can be in system memory only */
615 if(flag != SFLAG_INSYSMEM) {
616 ERR("GDI Surface requested in gl %s memory\n", flag == SFLAG_INDRAWABLE ? "drawable" : "texture");
620 static HRESULT WINAPI IWineGDISurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
621 if(flag != SFLAG_INSYSMEM) {
622 ERR("GDI Surface requested to be copied to gl %s\n", flag == SFLAG_INTEXTURE ? "texture" : "drawable");
624 TRACE("Surface requested in surface memory\n");
629 static WINED3DSURFTYPE WINAPI IWineGDISurfaceImpl_GetImplType(IWineD3DSurface *iface) {
633 static HRESULT WINAPI IWineGDISurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
634 FIXME("GDI surfaces can't draw overlays yet\n");
638 /* FIXME: This vtable should not use any IWineD3DSurface* implementation functions,
639 * only IWineD3DBaseSurface and IWineGDISurface ones.
641 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
644 IWineD3DBaseSurfaceImpl_QueryInterface,
645 IWineD3DBaseSurfaceImpl_AddRef,
646 IWineGDISurfaceImpl_Release,
647 /* IWineD3DResource */
648 IWineD3DBaseSurfaceImpl_GetParent,
649 IWineD3DBaseSurfaceImpl_SetPrivateData,
650 IWineD3DBaseSurfaceImpl_GetPrivateData,
651 IWineD3DBaseSurfaceImpl_FreePrivateData,
652 IWineD3DBaseSurfaceImpl_SetPriority,
653 IWineD3DBaseSurfaceImpl_GetPriority,
654 IWineGDISurfaceImpl_PreLoad,
655 IWineGDISurfaceImpl_UnLoad,
656 IWineD3DBaseSurfaceImpl_GetType,
657 /* IWineD3DSurface */
658 IWineD3DBaseSurfaceImpl_GetContainer,
659 IWineD3DBaseSurfaceImpl_GetDesc,
660 IWineGDISurfaceImpl_LockRect,
661 IWineGDISurfaceImpl_UnlockRect,
662 IWineGDISurfaceImpl_GetDC,
663 IWineGDISurfaceImpl_ReleaseDC,
664 IWineGDISurfaceImpl_Flip,
665 IWineD3DBaseSurfaceImpl_Blt,
666 IWineD3DBaseSurfaceImpl_GetBltStatus,
667 IWineD3DBaseSurfaceImpl_GetFlipStatus,
668 IWineD3DBaseSurfaceImpl_IsLost,
669 IWineD3DBaseSurfaceImpl_Restore,
670 IWineD3DBaseSurfaceImpl_BltFast,
671 IWineD3DBaseSurfaceImpl_GetPalette,
672 IWineD3DBaseSurfaceImpl_SetPalette,
673 IWineGDISurfaceImpl_RealizePalette,
674 IWineD3DBaseSurfaceImpl_SetColorKey,
675 IWineD3DBaseSurfaceImpl_GetPitch,
676 IWineGDISurfaceImpl_SetMem,
677 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
678 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
679 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
680 IWineD3DBaseSurfaceImpl_UpdateOverlay,
681 IWineD3DBaseSurfaceImpl_SetClipper,
682 IWineD3DBaseSurfaceImpl_GetClipper,
684 IWineGDISurfaceImpl_LoadTexture,
685 IWineD3DBaseSurfaceImpl_BindTexture,
686 IWineGDISurfaceImpl_SaveSnapshot,
687 IWineD3DBaseSurfaceImpl_SetContainer,
688 IWineD3DBaseSurfaceImpl_GetData,
689 IWineD3DBaseSurfaceImpl_SetFormat,
690 IWineGDISurfaceImpl_PrivateSetup,
691 IWineGDISurfaceImpl_ModifyLocation,
692 IWineGDISurfaceImpl_LoadLocation,
693 IWineGDISurfaceImpl_GetImplType,
694 IWineGDISurfaceImpl_DrawOverlay