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