quartz: Silence requests for ipin on filters.
[wine] / dlls / wined3d / surface_gdi.c
1 /*
2  * 2D Surface implementation without OpenGL
3  *
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
12  *
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.
17  *
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.
22  *
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
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30 #include "wined3d_private.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
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);
38
39 /*****************************************************************************
40  * x11_copy_to_screen
41  *
42  * Helper function that blts the front buffer contents to the target window
43  *
44  * Params:
45  *  This: Surface to copy from
46  *  rc: Rectangle to copy
47  *
48  *****************************************************************************/
49 static void
50 x11_copy_to_screen(IWineD3DSurfaceImpl *This,
51                    LPRECT rc)
52 {
53     if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
54     {
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);
61
62         hSurfaceDC = This->hDC;
63
64         hDisplayWnd = This->resource.wineD3DDevice->ddraw_window;
65         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
66         if(rc)
67         {
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);
70         }
71
72         /* Front buffer coordinates are screen coordinates. Map them to the destination
73          * window if not fullscreened
74          */
75         if(!This->resource.wineD3DDevice->ddraw_fullscreen) {
76             ClientToScreen(hDisplayWnd, &offset);
77         }
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 */
86         }
87 #endif
88         drawrect.left   = 0;
89         drawrect.right  = This->currentDesc.Width;
90         drawrect.top    = 0;
91         drawrect.bottom = This->currentDesc.Height;
92
93 #if 0
94         /* TODO: Support clippers */
95         if (This->clipper)
96         {
97             RECT xrc;
98             HWND hwnd = ((IWineD3DClipperImpl *) This->clipper)->hWnd;
99             if (hwnd && GetClientRect(hwnd,&xrc))
100             {
101                 OffsetRect(&xrc,offset.x,offset.y);
102                 IntersectRect(&drawrect,&drawrect,&xrc);
103             }
104         }
105 #endif
106         if (rc)
107         {
108             IntersectRect(&drawrect,&drawrect,rc);
109         }
110         else
111         {
112             /* Only use this if the caller did not pass a rectangle, since
113              * due to double locking this could be the wrong one ...
114              */
115             if (This->lockedRect.left != This->lockedRect.right)
116             {
117                 IntersectRect(&drawrect,&drawrect,&This->lockedRect);
118             }
119         }
120
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);
128     }
129 }
130
131 /*****************************************************************************
132  * IWineD3DSurface::Release, GDI version
133  *
134  * In general a normal COM Release method, but the GDI version doesn't have
135  * to destroy all the GL things.
136  *
137  *****************************************************************************/
138 ULONG WINAPI IWineGDISurfaceImpl_Release(IWineD3DSurface *iface) {
139     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
140     ULONG ref = InterlockedDecrement(&This->resource.ref);
141     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
142     if (ref == 0) {
143         IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
144         TRACE("(%p) : cleaning up\n", This);
145
146         if(This->Flags & SFLAG_DIBSECTION) {
147             /* Release the DC */
148             SelectObject(This->hDC, This->dib.holdbitmap);
149             DeleteDC(This->hDC);
150             /* Release the DIB section */
151             DeleteObject(This->dib.DIBsection);
152             This->dib.bitmap_data = NULL;
153             This->resource.allocatedMemory = NULL;
154         }
155         if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
156
157         HeapFree(GetProcessHeap(), 0, This->palette9);
158
159         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
160         if(iface == device->ddraw_primary)
161             device->ddraw_primary = NULL;
162
163         TRACE("(%p) Released\n", This);
164         HeapFree(GetProcessHeap(), 0, This);
165
166     }
167     return ref;
168 }
169
170 /*****************************************************************************
171  * IWineD3DSurface::PreLoad, GDI version
172  *
173  * This call is unsupported on GDI surfaces, if it's called something went
174  * wrong in the parent library. Write an informative warning
175  *
176  *****************************************************************************/
177 static void WINAPI
178 IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
179 {
180     ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
181     ERR("(%p): Most likely the parent library did something wrong.\n", iface);
182     ERR("(%p): Please report to wine-devel\n", iface);
183 }
184
185 /*****************************************************************************
186  * IWineD3DSurface::Unoad, GDI version
187  *
188  * This call is unsupported on GDI surfaces, if it's called something went
189  * wrong in the parent library. Write an informative warning
190  *
191  *****************************************************************************/
192 static void WINAPI IWineGDISurfaceImpl_UnLoad(IWineD3DSurface *iface)
193 {
194     ERR("(%p): UnLoad is not supported on X11 surfaces!\n", iface);
195     ERR("(%p): Most likely the parent library did something wrong.\n", iface);
196     ERR("(%p): Please report to wine-devel\n", iface);
197 }
198
199 /*****************************************************************************
200  * IWineD3DSurface::LockRect, GDI version
201  *
202  * Locks the surface and returns a pointer to the surface memory
203  *
204  * Params:
205  *  pLockedRect: Address to return the locking info at
206  *  pRect: Rectangle to lock
207  *  Flags: Some flags
208  *
209  * Returns:
210  *  WINED3D_OK on success
211  *  WINED3DERR_INVALIDCALL on errors
212  *
213  *****************************************************************************/
214 static HRESULT WINAPI
215 IWineGDISurfaceImpl_LockRect(IWineD3DSurface *iface,
216                              WINED3DLOCKED_RECT* pLockedRect,
217                              CONST RECT* pRect,
218                              DWORD Flags)
219 {
220     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
221
222     /* Already locked? */
223     if(This->Flags & SFLAG_LOCKED)
224     {
225         ERR("(%p) Surface already locked\n", This);
226         /* What should I return here? */
227         return WINED3DERR_INVALIDCALL;
228     }
229     This->Flags |= SFLAG_LOCKED;
230
231     if(!This->resource.allocatedMemory) {
232         /* This happens on gdi surfaces if the application set a user pointer and resets it.
233          * Recreate the DIB section
234          */
235         IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
236         This->resource.allocatedMemory = This->dib.bitmap_data;
237     }
238
239     return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
240 }
241
242 /*****************************************************************************
243  * IWineD3DSurface::UnlockRect, GDI version
244  *
245  * Unlocks a surface. This implementation doesn't do much, except updating
246  * the window if the front buffer is unlocked
247  *
248  * Returns:
249  *  WINED3D_OK on success
250  *  WINED3DERR_INVALIDCALL on failure
251  *
252  *****************************************************************************/
253 static HRESULT WINAPI
254 IWineGDISurfaceImpl_UnlockRect(IWineD3DSurface *iface)
255 {
256     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
257     IWineD3DDeviceImpl *dev = This->resource.wineD3DDevice;
258     TRACE("(%p)\n", This);
259
260     if (!(This->Flags & SFLAG_LOCKED))
261     {
262         WARN("trying to Unlock an unlocked surf@%p\n", This);
263         return WINED3DERR_INVALIDCALL;
264     }
265
266     /* Can be useful for debugging */
267 #if 0
268         {
269             static unsigned int gen = 0;
270             char buffer[4096];
271             ++gen;
272             if ((gen % 10) == 0) {
273                 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
274                 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
275             }
276             /*
277              * debugging crash code
278             if (gen == 250) {
279               void** test = NULL;
280               *test = 0;
281             }
282             */
283         }
284 #endif
285
286     /* Update the screen */
287     if(This == (IWineD3DSurfaceImpl *) dev->ddraw_primary)
288     {
289         x11_copy_to_screen(This, &This->lockedRect);
290     }
291
292     This->Flags &= ~SFLAG_LOCKED;
293     memset(&This->lockedRect, 0, sizeof(RECT));
294     return WINED3D_OK;
295 }
296
297 /*****************************************************************************
298  * IWineD3DSurface::Flip, GDI version
299  *
300  * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
301  * the parent library. This implementation changes the data pointers of the
302  * surfaces and copies the new front buffer content to the screen
303  *
304  * Params:
305  *  override: Flipping target(e.g. back buffer)
306  *
307  * Returns:
308  *  WINED3D_OK on success
309  *
310  *****************************************************************************/
311 static HRESULT WINAPI
312 IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface,
313                          IWineD3DSurface *override,
314                          DWORD Flags)
315 {
316     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
317     IWineD3DSurfaceImpl *Target = (IWineD3DSurfaceImpl *) override;
318     TRACE("(%p)->(%p,%x)\n", This, override, Flags);
319
320     TRACE("(%p) Flipping to surface %p\n", This, Target);
321
322     if(Target == NULL)
323     {
324         ERR("(%p): Can't flip without a target\n", This);
325         return WINED3DERR_INVALIDCALL;
326     }
327
328     /* Flip the DC */
329     {
330         HDC tmp;
331         tmp = This->hDC;
332         This->hDC = Target->hDC;
333         Target->hDC = tmp;
334     }
335
336     /* Flip the DIBsection */
337     {
338         HBITMAP tmp;
339         tmp = This->dib.DIBsection;
340         This->dib.DIBsection = Target->dib.DIBsection;
341         Target->dib.DIBsection = tmp;
342     }
343
344     /* Flip the surface data */
345     {
346         void* tmp;
347
348         tmp = This->dib.bitmap_data;
349         This->dib.bitmap_data = Target->dib.bitmap_data;
350         Target->dib.bitmap_data = tmp;
351
352         tmp = This->resource.allocatedMemory;
353         This->resource.allocatedMemory = Target->resource.allocatedMemory;
354         Target->resource.allocatedMemory = tmp;
355
356         if(This->resource.heapMemory) {
357             ERR("GDI Surface %p has heap memory allocated\n", This);
358         }
359         if(Target->resource.heapMemory) {
360             ERR("GDI Surface %p has heap memory allocated\n", Target);
361         }
362     }
363
364     /* client_memory should not be different, but just in case */
365     {
366         BOOL tmp;
367         tmp = This->dib.client_memory;
368         This->dib.client_memory = Target->dib.client_memory;
369         Target->dib.client_memory = tmp;
370     }
371
372     /* Useful for debugging */
373 #if 0
374         {
375             static unsigned int gen = 0;
376             char buffer[4096];
377             ++gen;
378             if ((gen % 10) == 0) {
379                 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
380                 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
381             }
382             /*
383              * debugging crash code
384             if (gen == 250) {
385               void** test = NULL;
386               *test = 0;
387             }
388             */
389         }
390 #endif
391
392     /* Update the screen */
393     x11_copy_to_screen(This, NULL);
394
395     /* FPS support */
396     if (TRACE_ON(fps))
397     {
398         static long prev_time, frames;
399
400         DWORD time = GetTickCount();
401         frames++;
402         /* every 1.5 seconds */
403         if (time - prev_time > 1500) {
404             TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
405             prev_time = time;
406             frames = 0;
407         }
408     }
409
410     return WINED3D_OK;
411 }
412
413 /*****************************************************************************
414  * IWineD3DSurface::LoadTexture, GDI version
415  *
416  * This is mutually unsupported by GDI surfaces
417  *
418  * Returns:
419  *  D3DERR_INVALIDCALL
420  *
421  *****************************************************************************/
422 HRESULT WINAPI
423 IWineGDISurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode)
424 {
425     ERR("Unsupported on X11 surfaces\n");
426     return WINED3DERR_INVALIDCALL;
427 }
428
429 /*****************************************************************************
430  * IWineD3DSurface::SaveSnapshot, GDI version
431  *
432  * This method writes the surface's contents to the in tga format to the
433  * file specified in filename.
434  *
435  * Params:
436  *  filename: File to write to
437  *
438  * Returns:
439  *  WINED3DERR_INVALIDCALL if the file couldn't be opened
440  *  WINED3D_OK on success
441  *
442  *****************************************************************************/
443 static int get_shift(DWORD color_mask) {
444     int shift = 0;
445     while (color_mask > 0xFF) {
446         color_mask >>= 1;
447         shift += 1;
448     }
449     while ((color_mask & 0x80) == 0) {
450         color_mask <<= 1;
451         shift -= 1;
452     }
453     return shift;
454 }
455
456
457 HRESULT WINAPI
458 IWineGDISurfaceImpl_SaveSnapshot(IWineD3DSurface *iface,
459 const char* filename)
460 {
461     FILE* f = NULL;
462     UINT y = 0, x = 0;
463     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
464     static char *output = NULL;
465     static int size = 0;
466     const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
467
468     if (This->pow2Width > size) {
469         output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pow2Width * 3);
470         size = This->pow2Width;
471     }
472
473
474     f = fopen(filename, "w+");
475     if (NULL == f) {
476         ERR("opening of %s failed with\n", filename);
477         return WINED3DERR_INVALIDCALL;
478     }
479     fprintf(f, "P6\n%d %d\n255\n", This->pow2Width, This->pow2Height);
480
481     if (This->resource.format == WINED3DFMT_P8) {
482         unsigned char table[256][3];
483         int i;
484
485         if (This->palette == NULL) {
486             fclose(f);
487             return WINED3DERR_INVALIDCALL;
488         }
489         for (i = 0; i < 256; i++) {
490             table[i][0] = This->palette->palents[i].peRed;
491             table[i][1] = This->palette->palents[i].peGreen;
492             table[i][2] = This->palette->palents[i].peBlue;
493         }
494         for (y = 0; y < This->pow2Height; y++) {
495             unsigned char *src = This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
496             for (x = 0; x < This->pow2Width; x++) {
497                 unsigned char color = *src;
498                 src += 1;
499
500                 output[3 * x + 0] = table[color][0];
501                 output[3 * x + 1] = table[color][1];
502                 output[3 * x + 2] = table[color][2];
503             }
504             fwrite(output, 3 * This->pow2Width, 1, f);
505         }
506     } else {
507         int red_shift, green_shift, blue_shift, pix_width, alpha_shift;
508
509         pix_width = This->bytesPerPixel;
510
511         red_shift = get_shift(formatEntry->redMask);
512         green_shift = get_shift(formatEntry->greenMask);
513         blue_shift = get_shift(formatEntry->blueMask);
514         alpha_shift = get_shift(formatEntry->alphaMask);
515
516         for (y = 0; y < This->pow2Height; y++) {
517             unsigned char *src = This->resource.allocatedMemory + (y * 1 * IWineD3DSurface_GetPitch(iface));
518             for (x = 0; x < This->pow2Width; x++) {         
519                 unsigned int color;
520                 unsigned int comp;
521                 int i;
522
523                 color = 0;
524                 for (i = 0; i < pix_width; i++) {
525                     color |= src[i] << (8 * i);
526                 }
527                 src += 1 * pix_width;
528
529                 comp = color & formatEntry->redMask;
530                 output[3 * x + 0] = red_shift > 0 ? comp >> red_shift : comp << -red_shift;
531                 comp = color & formatEntry->greenMask;
532                 output[3 * x + 1] = green_shift > 0 ? comp >> green_shift : comp << -green_shift;
533                 comp = color & formatEntry->alphaMask;
534                 output[3 * x + 2] = alpha_shift > 0 ? comp >> alpha_shift : comp << -alpha_shift;
535             }
536             fwrite(output, 3 * This->pow2Width, 1, f);
537         }
538     }
539     fclose(f);
540     return WINED3D_OK;
541 }
542
543 HRESULT WINAPI IWineGDISurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
544     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
545     WINED3DLOCKED_RECT lock;
546     HRESULT hr;
547     RGBQUAD col[256];
548
549     TRACE("(%p)->(%p)\n",This,pHDC);
550
551     if(This->Flags & SFLAG_USERPTR) {
552         ERR("Not supported on surfaces with an application-provided surfaces\n");
553         return WINEDDERR_NODC;
554     }
555
556     /* Give more detailed info for ddraw */
557     if (This->Flags & SFLAG_DCINUSE)
558         return WINEDDERR_DCALREADYCREATED;
559
560     /* Can't GetDC if the surface is locked */
561     if (This->Flags & SFLAG_LOCKED)
562         return WINED3DERR_INVALIDCALL;
563
564     memset(&lock, 0, sizeof(lock)); /* To be sure */
565
566     /* Should have a DIB section already */
567
568     /* Lock the surface */
569     hr = IWineD3DSurface_LockRect(iface,
570                                   &lock,
571                                   NULL,
572                                   0);
573     if(FAILED(hr)) {
574         ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
575         /* keep the dib section */
576         return hr;
577     }
578
579     if(This->resource.format == WINED3DFMT_P8 ||
580        This->resource.format == WINED3DFMT_A8P8) {
581         unsigned int n;
582         if(This->palette) {
583             PALETTEENTRY ent[256];
584
585             GetPaletteEntries(This->palette->hpal, 0, 256, ent);
586             for (n=0; n<256; n++) {
587                 col[n].rgbRed   = ent[n].peRed;
588                 col[n].rgbGreen = ent[n].peGreen;
589                 col[n].rgbBlue  = ent[n].peBlue;
590                 col[n].rgbReserved = 0;
591             }
592         } else {
593             IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
594
595             for (n=0; n<256; n++) {
596                 col[n].rgbRed   = device->palettes[device->currentPalette][n].peRed;
597                 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
598                 col[n].rgbBlue  = device->palettes[device->currentPalette][n].peBlue;
599                 col[n].rgbReserved = 0;
600             }
601
602         }
603         SetDIBColorTable(This->hDC, 0, 256, col);
604     }
605
606     *pHDC = This->hDC;
607     TRACE("returning %p\n",*pHDC);
608     This->Flags |= SFLAG_DCINUSE;
609
610     return WINED3D_OK;
611 }
612
613 HRESULT WINAPI IWineGDISurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
614     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
615
616     TRACE("(%p)->(%p)\n",This,hDC);
617
618     if (!(This->Flags & SFLAG_DCINUSE))
619         return WINED3DERR_INVALIDCALL;
620
621     if (This->hDC !=hDC) {
622         WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
623         return WINED3DERR_INVALIDCALL;
624     }
625
626     /* we locked first, so unlock now */
627     IWineD3DSurface_UnlockRect(iface);
628
629     This->Flags &= ~SFLAG_DCINUSE;
630
631     return WINED3D_OK;
632 }
633
634 HRESULT WINAPI IWineGDISurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
635     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
636     RGBQUAD col[256];
637     IWineD3DPaletteImpl *pal = This->palette;
638     unsigned int n;
639     TRACE("(%p)\n", This);
640
641     if(This->Flags & SFLAG_DIBSECTION) {
642         TRACE("(%p): Updating the hdc's palette\n", This);
643         for (n=0; n<256; n++) {
644             if(pal) {
645                 col[n].rgbRed   = pal->palents[n].peRed;
646                 col[n].rgbGreen = pal->palents[n].peGreen;
647                 col[n].rgbBlue  = pal->palents[n].peBlue;
648             } else {
649                 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
650                 /* Use the default device palette */
651                 col[n].rgbRed   = device->palettes[device->currentPalette][n].peRed;
652                 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
653                 col[n].rgbBlue  = device->palettes[device->currentPalette][n].peBlue;
654             }
655             col[n].rgbReserved = 0;
656         }
657         SetDIBColorTable(This->hDC, 0, 256, col);
658     }
659
660     /* Update the image because of the palette change. Note that this function is also
661      * called on a palette removal. In such a case we don't have to update the screen.
662      * Some games like e.g Red Alert call SetEntries a lot to implement fading.
663      */
664     if(pal && This->resource.usage & WINED3DUSAGE_RENDERTARGET)
665         x11_copy_to_screen(This, NULL);
666
667     return WINED3D_OK;
668 }
669
670 /*****************************************************************************
671  * IWineD3DSurface::PrivateSetup, GDI version
672  *
673  * Initializes the GDI surface, aka creates the DIB section we render to
674  * The DIB section creation is done by calling GetDC, which will create the
675  * section and releasing the dc to allow the app to use it. The dib section
676  * will stay until the surface is released
677  *
678  * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
679  * are set to the real sizes to save memory. The NONPOW2 flag is unset to
680  * avoid confusion in the shared surface code.
681  *
682  * Returns:
683  *  WINED3D_OK on success
684  *  The return values of called methods on failure
685  *
686  *****************************************************************************/
687 HRESULT WINAPI
688 IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
689 {
690     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
691
692     if(This->resource.usage & WINED3DUSAGE_OVERLAY)
693     {
694         ERR("(%p) Overlays not yet supported by GDI surfaces\n", This);
695         return WINED3DERR_INVALIDCALL;
696     }
697     /* Sysmem textures have memory already allocated -
698      * release it, this avoids an unnecessary memcpy
699      */
700     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
701     This->resource.allocatedMemory = NULL;
702     This->resource.heapMemory = NULL;
703
704     /* We don't mind the nonpow2 stuff in GDI */
705     This->pow2Width = This->currentDesc.Width;
706     This->pow2Height = This->currentDesc.Height;
707
708     IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
709     This->resource.allocatedMemory = This->dib.bitmap_data;
710
711     return WINED3D_OK;
712 }
713
714 void WINAPI IWineGDISurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
715     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
716
717     /* Ignore 0 textureName and target. D3D textures can be created with gdi surfaces as plain
718      * containers, but they're useless until the app creates a d3d device from a d3d point of
719      * view, it's not an implementation limitation. This avoids false warnings when the texture
720      * is destroyed and sets the description back to 0/0
721      */
722     if(textureName != 0 || target != 0) {
723         FIXME("(%p) : Should not be called on a GDI surface. textureName %u, target %i\n", This, textureName, target);
724         DebugBreak();
725     }
726 }
727
728 void WINAPI IWineGDISurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
729     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
730     FIXME("(%p) : Should not be called on a GDI surface\n", This);
731     *glDescription = NULL;
732 }
733
734 HRESULT WINAPI IWineGDISurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
735     /* GDI surface data can only be in one location, the system memory dib section. So they are
736      * always clean by definition.
737      */
738     TRACE("No dirtification in GDI surfaces\n");
739     return WINED3D_OK;
740 }
741
742 HRESULT WINAPI IWineGDISurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
743     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
744
745     /* Render targets depend on their hdc, and we can't create an hdc on a user pointer */
746     if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
747         ERR("Not supported on render targets\n");
748         return WINED3DERR_INVALIDCALL;
749     }
750
751     if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
752         WARN("Surface is locked or the HDC is in use\n");
753         return WINED3DERR_INVALIDCALL;
754     }
755
756     if(Mem && Mem != This->resource.allocatedMemory) {
757         void *release = NULL;
758
759         /* Do I have to copy the old surface content? */
760         if(This->Flags & SFLAG_DIBSECTION) {
761                 /* Release the DC. No need to hold the critical section for the update
762             * Thread because this thread runs only on front buffers, but this method
763             * fails for render targets in the check above.
764                 */
765             SelectObject(This->hDC, This->dib.holdbitmap);
766             DeleteDC(This->hDC);
767             /* Release the DIB section */
768             DeleteObject(This->dib.DIBsection);
769             This->dib.bitmap_data = NULL;
770             This->resource.allocatedMemory = NULL;
771             This->hDC = NULL;
772             This->Flags &= ~SFLAG_DIBSECTION;
773         } else if(!(This->Flags & SFLAG_USERPTR)) {
774             release = This->resource.allocatedMemory;
775         }
776         This->resource.allocatedMemory = Mem;
777         This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
778
779         /* Now free the old memory if any */
780         HeapFree(GetProcessHeap(), 0, release);
781     } else if(This->Flags & SFLAG_USERPTR) {
782         /* Lockrect and GetDC will re-create the dib section and allocated memory */
783         This->resource.allocatedMemory = NULL;
784         This->Flags &= ~SFLAG_USERPTR;
785     }
786     return WINED3D_OK;
787 }
788
789 /***************************
790  *
791  ***************************/
792 static void WINAPI IWineGDISurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
793     TRACE("(%p)->(%s, %s)\n", iface,
794           flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
795           persistent ? "TRUE" : "FALSE");
796     /* GDI surfaces can be in system memory only */
797     if(flag != SFLAG_INSYSMEM) {
798         ERR("GDI Surface requested in gl %s memory\n", flag == SFLAG_INDRAWABLE ? "drawable" : "texture");
799     }
800 }
801
802 static HRESULT WINAPI IWineGDISurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
803     if(flag != SFLAG_INSYSMEM) {
804         ERR("GDI Surface requested to be copied to gl %s\n", flag == SFLAG_INTEXTURE ? "texture" : "drawable");
805     } else {
806         TRACE("Surface requested in surface memory\n");
807     }
808     return WINED3D_OK;
809 }
810
811 static WINED3DSURFTYPE WINAPI IWineGDISurfaceImpl_GetImplType(IWineD3DSurface *iface) {
812     return SURFACE_GDI;
813 }
814
815 /* FIXME: This vtable should not use any IWineD3DSurface* implementation functions,
816  * only IWineD3DBaseSurface and IWineGDISurface ones.
817  */
818 const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
819 {
820     /* IUnknown */
821     IWineD3DBaseSurfaceImpl_QueryInterface,
822     IWineD3DBaseSurfaceImpl_AddRef,
823     IWineGDISurfaceImpl_Release,
824     /* IWineD3DResource */
825     IWineD3DBaseSurfaceImpl_GetParent,
826     IWineD3DBaseSurfaceImpl_GetDevice,
827     IWineD3DBaseSurfaceImpl_SetPrivateData,
828     IWineD3DBaseSurfaceImpl_GetPrivateData,
829     IWineD3DBaseSurfaceImpl_FreePrivateData,
830     IWineD3DBaseSurfaceImpl_SetPriority,
831     IWineD3DBaseSurfaceImpl_GetPriority,
832     IWineGDISurfaceImpl_PreLoad,
833     IWineGDISurfaceImpl_UnLoad,
834     IWineD3DBaseSurfaceImpl_GetType,
835     /* IWineD3DSurface */
836     IWineD3DBaseSurfaceImpl_GetContainer,
837     IWineD3DBaseSurfaceImpl_GetDesc,
838     IWineGDISurfaceImpl_LockRect,
839     IWineGDISurfaceImpl_UnlockRect,
840     IWineGDISurfaceImpl_GetDC,
841     IWineGDISurfaceImpl_ReleaseDC,
842     IWineGDISurfaceImpl_Flip,
843     IWineD3DBaseSurfaceImpl_Blt,
844     IWineD3DBaseSurfaceImpl_GetBltStatus,
845     IWineD3DBaseSurfaceImpl_GetFlipStatus,
846     IWineD3DBaseSurfaceImpl_IsLost,
847     IWineD3DBaseSurfaceImpl_Restore,
848     IWineD3DBaseSurfaceImpl_BltFast,
849     IWineD3DBaseSurfaceImpl_GetPalette,
850     IWineD3DBaseSurfaceImpl_SetPalette,
851     IWineGDISurfaceImpl_RealizePalette,
852     IWineD3DBaseSurfaceImpl_SetColorKey,
853     IWineD3DBaseSurfaceImpl_GetPitch,
854     IWineGDISurfaceImpl_SetMem,
855     IWineD3DBaseSurfaceImpl_SetOverlayPosition,
856     IWineD3DBaseSurfaceImpl_GetOverlayPosition,
857     IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
858     IWineD3DBaseSurfaceImpl_UpdateOverlay,
859     IWineD3DBaseSurfaceImpl_SetClipper,
860     IWineD3DBaseSurfaceImpl_GetClipper,
861     /* Internal use: */
862     IWineGDISurfaceImpl_AddDirtyRect,
863     IWineGDISurfaceImpl_LoadTexture,
864     IWineD3DBaseSurfaceImpl_BindTexture,
865     IWineGDISurfaceImpl_SaveSnapshot,
866     IWineD3DBaseSurfaceImpl_SetContainer,
867     IWineGDISurfaceImpl_SetGlTextureDesc,
868     IWineGDISurfaceImpl_GetGlDesc,
869     IWineD3DSurfaceImpl_GetData,
870     IWineD3DBaseSurfaceImpl_SetFormat,
871     IWineGDISurfaceImpl_PrivateSetup,
872     IWineGDISurfaceImpl_ModifyLocation,
873     IWineGDISurfaceImpl_LoadLocation,
874     IWineGDISurfaceImpl_GetImplType
875 };