Make WIN_SetStyle more thread-safe by specifying the bits to change
[wine] / dlls / wined3d / surface.c
1 /*
2  * IWineD3DSurface Implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2003 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
28
29 /* *******************************************
30    IWineD3DSurface IUnknown parts follow
31    ******************************************* */
32 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
35     WARN("(%p)->(%s,%p) should not be called\n",This,debugstr_guid(riid),ppobj);
36     return E_NOINTERFACE;
37 }
38
39 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface) {
40     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
41     ULONG ref = InterlockedIncrement(&This->resource.ref);
42     TRACE("(%p) : AddRef increasing from %ld\n", This,ref - 1);
43     IUnknown_AddRef(This->resource.parent);
44     return ref;
45 }
46
47 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
48     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
49     ULONG ref = InterlockedDecrement(&This->resource.ref);
50     TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
51     if (ref == 0) {
52         HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
53         IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
54         HeapFree(GetProcessHeap(), 0, This);
55     } else {
56         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
57     }
58     return ref;
59 }
60
61 /* ****************************************************
62    IWineD3DSurface IWineD3DResource parts follow
63    **************************************************** */
64 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
65     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
66 }
67
68 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
69     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
70 }
71
72 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
73     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
74 }
75
76 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
77     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
78 }
79
80 DWORD    WINAPI        IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
81     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
82 }
83
84 DWORD    WINAPI        IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
85     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
86 }
87
88 void     WINAPI        IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
89     return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
90 }
91
92 D3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface) {
93     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
94 }
95
96 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
97     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
98 }
99
100 /* ******************************************************
101    IWineD3DSurface IWineD3DSurface parts follow
102    ****************************************************** */
103
104 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface *iface, REFIID riid, void** ppContainer) {
105     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
106
107     /** From MSDN:
108      * If the surface is created using CreateImageSurface/CreateOffscreenPlainSurface, CreateRenderTarget, 
109      * or CreateDepthStencilSurface, the surface is considered stand alone. In this case, 
110      * GetContainer will return the Direct3D device used to create the surface. 
111      */
112     WARN("Query of container implementation currently ignores riid\n");
113     *ppContainer = This->container;
114     IUnknown_AddRef((IUnknown *)*ppContainer);
115     TRACE("(%p) : returning %p\n", This, *ppContainer);
116     return D3D_OK;
117 }
118
119 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
120     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
121
122     TRACE("(%p) : copying into %p\n", This, pDesc);
123     *(pDesc->Format)             = This->currentDesc.Format;
124     *(pDesc->Type)               = This->currentDesc.Type;
125     *(pDesc->Usage)              = This->currentDesc.Usage;
126     *(pDesc->Pool)               = This->currentDesc.Pool;
127     *(pDesc->Size)               = This->currentDesc_size;   /* dx8 only */
128     *(pDesc->MultiSampleType)    = This->currentDesc.MultiSampleType;
129     *(pDesc->MultiSampleQuality) = This->currentDesc.MultiSampleQuality;
130     *(pDesc->Width)              = This->currentDesc.Width;
131     *(pDesc->Height)             = This->currentDesc.Height;
132     return D3D_OK;
133 }
134
135 HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
136     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
137
138     /* fixme: should we really lock as such? */
139     if (This->inTexture && This->inPBuffer) {
140         FIXME("Warning: Surface is in texture memory or pbuffer\n");
141         This->inTexture = 0;
142         This->inPBuffer = 0;
143     }
144
145     if (FALSE == This->lockable) {
146         /* Note: UpdateTextures calls CopyRects which calls this routine to populate the 
147               texture regions, and since the destination is an unlockable region we need
148               to tolerate this                                                           */
149         TRACE("Warning: trying to lock unlockable surf@%p\n", This);  
150         /*return D3DERR_INVALIDCALL; */
151     }
152
153     if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->renderTarget || This == This->resource.wineD3DDevice->frontBuffer || This->resource.wineD3DDevice->depthStencilBuffer) {
154         if (This == This->resource.wineD3DDevice->backBuffer) {
155             TRACE("(%p, backBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
156         } else if (This == This->resource.wineD3DDevice->frontBuffer) {
157             TRACE("(%p, frontBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
158         } else if (This == This->resource.wineD3DDevice->renderTarget) {
159             TRACE("(%p, renderTarget) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
160         } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
161             TRACE("(%p, stencilBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
162         }
163     } else {
164         TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
165     }
166
167     /* DXTn formats don't have exact pitches as they are to the new row of blocks,
168          where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt3/5)      
169           ie pitch = (width/4) * bytes per block                                  */
170     if (This->currentDesc.Format == D3DFMT_DXT1) /* DXT1 is 8 bytes per block */
171         pLockedRect->Pitch = (This->currentDesc.Width/4) * 8;
172     else if (This->currentDesc.Format == D3DFMT_DXT3 || This->currentDesc.Format == D3DFMT_DXT5) /* DXT3/5 is 16 bytes per block */
173         pLockedRect->Pitch = (This->currentDesc.Width/4) * 16;
174     else
175         pLockedRect->Pitch = This->bytesPerPixel * This->currentDesc.Width;  /* Bytes / row */    
176
177     if (NULL == pRect) {
178         pLockedRect->pBits = This->allocatedMemory;
179         This->lockedRect.left   = 0;
180         This->lockedRect.top    = 0;
181         This->lockedRect.right  = This->currentDesc.Width;
182         This->lockedRect.bottom = This->currentDesc.Height;
183         TRACE("Locked Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", &This->lockedRect, This->lockedRect.left, This->lockedRect.top, This->lockedRect.right, This->lockedRect.bottom);
184     } else {
185         TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
186
187         if (This->currentDesc.Format == D3DFMT_DXT1) { /* DXT1 is half byte per pixel */
188             pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + ((pRect->left * This->bytesPerPixel/2));
189         } else {
190             pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
191         }
192         This->lockedRect.left   = pRect->left;
193         This->lockedRect.top    = pRect->top;
194         This->lockedRect.right  = pRect->right;
195         This->lockedRect.bottom = pRect->bottom;
196     }
197
198
199     if (0 == This->currentDesc.Usage) { /* classic surface */
200
201         /* Nothing to do ;) */
202
203     } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage && !(Flags&D3DLOCK_DISCARD)) { /* render surfaces */
204
205         if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->renderTarget || This == This->resource.wineD3DDevice->frontBuffer) {
206             GLint  prev_store;
207             GLenum prev_read;
208
209             ENTER_GL();
210
211             /**
212              * for render->surface copy begin to begin of allocatedMemory
213              * unlock can be more easy
214              */
215             pLockedRect->pBits = This->allocatedMemory;
216
217             glFlush();
218             vcheckGLcall("glFlush");
219             glGetIntegerv(GL_READ_BUFFER, &prev_read);
220             vcheckGLcall("glIntegerv");
221             glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
222             vcheckGLcall("glIntegerv");
223
224             if (This == This->resource.wineD3DDevice->backBuffer) {
225                 glReadBuffer(GL_BACK);
226             } else if (This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
227                 glReadBuffer(GL_FRONT);
228             } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
229                 ERR("Stencil Buffer lock unsupported for now\n");
230             }
231             vcheckGLcall("glReadBuffer");
232
233             {
234                 long j;
235                 GLenum format = D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format);
236                 GLenum type   = D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format);
237                 for (j = This->lockedRect.top; j < This->lockedRect.bottom - This->lockedRect.top; ++j) {
238                     glReadPixels(This->lockedRect.left, 
239                                  This->lockedRect.bottom - j - 1, 
240                                  This->lockedRect.right - This->lockedRect.left, 
241                                  1,
242                                  format, 
243                                  type, 
244                                  (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
245                     vcheckGLcall("glReadPixels");
246                 }
247             }
248
249             glReadBuffer(prev_read);
250             vcheckGLcall("glReadBuffer");
251
252             LEAVE_GL();
253
254         } else {
255             FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
256         }
257
258     } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
259
260         FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
261
262     } else {
263         FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
264     }
265
266     if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
267         /* Don't dirtify */
268     } else {
269         /**
270          * Dirtify on lock
271          * as seen in msdn docs
272          */
273         IWineD3DSurface_AddDirtyRect(iface, &This->lockedRect);
274
275         /** Dirtify Container if needed */
276         if ((NULL != This->container) && ((IWineD3DDeviceImpl *)This->container != This->resource.wineD3DDevice)) {
277             IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)This->container, TRUE);
278         }
279     }
280
281     TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
282
283     This->locked = TRUE;
284     return D3D_OK;
285 }
286
287 HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
288     GLint skipBytes = 0;
289     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
290
291     if (FALSE == This->locked) {
292         ERR("trying to Unlock an unlocked surf@%p\n", This);  
293         return D3DERR_INVALIDCALL;
294     }
295
296     if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->frontBuffer || This->resource.wineD3DDevice->depthStencilBuffer || This == This->resource.wineD3DDevice->renderTarget) {
297         if (This == This->resource.wineD3DDevice->backBuffer) {
298             TRACE("(%p, backBuffer) : dirtyfied(%d)\n", This, This->Dirty);
299         } else if (This == This->resource.wineD3DDevice->frontBuffer) {
300             TRACE("(%p, frontBuffer) : dirtyfied(%d)\n", This, This->Dirty);
301         } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
302             TRACE("(%p, stencilBuffer) : dirtyfied(%d)\n", This, This->Dirty);
303         } else if (This == This->resource.wineD3DDevice->renderTarget) {
304             TRACE("(%p, renderTarget) : dirtyfied(%d)\n", This, This->Dirty);
305         }
306     } else {
307         TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
308     }
309
310     if (FALSE == This->Dirty) {
311         TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
312         goto unlock_end;
313     }
314
315     if (0 == This->currentDesc.Usage) { /* classic surface */
316         /**
317          * nothing to do
318          * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
319          */
320     } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage) { /* render surfaces */
321
322         if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
323             GLint  prev_store;
324             GLenum prev_draw;
325             GLint  prev_rasterpos[4];
326
327             ENTER_GL();
328
329             glFlush();
330             vcheckGLcall("glFlush");
331             glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
332             vcheckGLcall("glIntegerv");
333             glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
334             vcheckGLcall("glIntegerv");
335             glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
336             vcheckGLcall("glIntegerv");
337             glPixelZoom(1.0, -1.0);
338             vcheckGLcall("glPixelZoom");
339
340             /* glDrawPixels transforms the raster position as though it was a vertex -
341                we want to draw at screen position 0,0 - Set up ortho (rhw) mode as   
342                per drawprim (and leave set - it will sort itself out due to last_was_rhw */
343             if (!This->resource.wineD3DDevice->last_was_rhw) {
344
345                 double X, Y, height, width, minZ, maxZ;
346                 This->resource.wineD3DDevice->last_was_rhw = TRUE;
347
348                 /* Transformed already into viewport coordinates, so we do not need transform
349                    matrices. Reset all matrices to identity and leave the default matrix in world 
350                    mode.                                                                         */
351                 glMatrixMode(GL_MODELVIEW);
352                 checkGLcall("glMatrixMode");
353                 glLoadIdentity();
354                 checkGLcall("glLoadIdentity");
355
356                 glMatrixMode(GL_PROJECTION);
357                 checkGLcall("glMatrixMode");
358                 glLoadIdentity();
359                 checkGLcall("glLoadIdentity");
360
361                 /* Set up the viewport to be full viewport */
362                 X      = This->resource.wineD3DDevice->stateBlock->viewport.X;
363                 Y      = This->resource.wineD3DDevice->stateBlock->viewport.Y;
364                 height = This->resource.wineD3DDevice->stateBlock->viewport.Height;
365                 width  = This->resource.wineD3DDevice->stateBlock->viewport.Width;
366                 minZ   = This->resource.wineD3DDevice->stateBlock->viewport.MinZ;
367                 maxZ   = This->resource.wineD3DDevice->stateBlock->viewport.MaxZ;
368                 TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
369                 glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
370                 checkGLcall("glOrtho");
371
372                 /* Window Coord 0 is the middle of the first pixel, so translate by half
373                    a pixel (See comment above glTranslate below)                         */
374                 glTranslatef(0.5, 0.5, 0);
375                 checkGLcall("glTranslatef(0.5, 0.5, 0)");
376             }
377
378             if (This == This->resource.wineD3DDevice->backBuffer) {
379                 glDrawBuffer(GL_BACK);
380             } else if (This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
381                 glDrawBuffer(GL_FRONT);
382             }
383             vcheckGLcall("glDrawBuffer");
384
385             /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
386             glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
387             glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
388
389             /* And back buffers are not blended */
390             glDisable(GL_BLEND);
391
392             glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
393             vcheckGLcall("glRasterPos2f");
394             switch (This->currentDesc.Format) {
395             case D3DFMT_R5G6B5:
396                 {
397                     glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
398                                  GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
399                     vcheckGLcall("glDrawPixels");
400                 }
401                 break;
402             case D3DFMT_R8G8B8:
403                 {
404                     glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
405                                  GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
406                     vcheckGLcall("glDrawPixels");
407                 }
408                 break;
409             case D3DFMT_A8R8G8B8:
410                 {
411                     glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
412                     vcheckGLcall("glPixelStorei");
413                     glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
414                                  GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
415                     vcheckGLcall("glDrawPixels");
416                     glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
417                     vcheckGLcall("glPixelStorei");
418                 }
419                 break;
420             default:
421                 FIXME("Unsupported Format %u in locking func\n", This->currentDesc.Format);
422             }
423
424             glPixelZoom(1.0,1.0);
425             vcheckGLcall("glPixelZoom");
426             glDrawBuffer(prev_draw);
427             vcheckGLcall("glDrawBuffer");
428             glRasterPos3iv(&prev_rasterpos[0]);
429             vcheckGLcall("glRasterPos3iv");
430
431             /* Reset to previous pack row length / blending state */
432             glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
433             if (This->resource.wineD3DDevice->stateBlock->renderState[D3DRS_ALPHABLENDENABLE]) glEnable(GL_BLEND);
434
435             LEAVE_GL();
436
437             /** restore clean dirty state */
438             IWineD3DSurface_CleanDirtyRect(iface);
439
440         } else {
441             FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
442         }
443
444     } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
445
446         if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
447             FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
448         } else {
449             FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
450         }
451
452     } else {
453         FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
454     }
455
456     unlock_end:
457     This->locked = FALSE;
458     memset(&This->lockedRect, 0, sizeof(RECT));
459     return D3D_OK;
460 }
461
462 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
463     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
464     FIXME("No support for GetDC yet for surface %p\n", This);
465     return D3DERR_INVALIDCALL;
466 }
467
468 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
469     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
470     FIXME("No support for ReleaseDC yet for surface %p\n", This);
471     return D3DERR_INVALIDCALL;
472 }
473
474 /* ******************************************************
475    IWineD3DSurface Internal (No mapping to directx api) parts follow
476    ****************************************************** */
477 HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, GLenum gl_target, GLenum gl_level) {
478     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
479
480     if (This->inTexture)
481         return D3D_OK;
482
483     if (This->inPBuffer) {
484         ENTER_GL();
485
486         if (gl_level != 0)
487             FIXME("Surface in texture is only supported for level 0\n");
488         else if (This->currentDesc.Format == D3DFMT_P8 || This->currentDesc.Format == D3DFMT_A8P8 ||
489                  This->currentDesc.Format == D3DFMT_DXT1 || This->currentDesc.Format == D3DFMT_DXT3 ||
490                  This->currentDesc.Format == D3DFMT_DXT5)
491             FIXME("Format %d not supported\n", This->currentDesc.Format);
492         else {
493             glCopyTexImage2D(gl_target,
494                              0,
495                              D3DFmt2GLIntFmt(This->resource.wineD3DDevice,
496                                              This->currentDesc.Format),
497                              0,
498                              0,
499                              This->currentDesc.Width,
500                              This->currentDesc.Height,
501                              0);
502             TRACE("Updating target %d\n", gl_target);
503             This->inTexture = TRUE;
504         }
505         LEAVE_GL();
506         return D3D_OK;
507     }
508
509     if ((This->currentDesc.Format == D3DFMT_P8 || This->currentDesc.Format == D3DFMT_A8P8) && 
510         !GL_SUPPORT(EXT_PALETTED_TEXTURE)) {
511         /**
512          * wanted a paletted texture and not really support it in HW 
513          * so software emulation code begin
514          */
515         UINT i;
516         PALETTEENTRY* pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
517         VOID* surface = (VOID*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->currentDesc.Width * This->currentDesc.Height * sizeof(DWORD));
518         BYTE* dst = (BYTE*) surface;
519         BYTE* src = (BYTE*) This->allocatedMemory;
520
521         for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
522             BYTE color = *src++;
523             *dst++ = pal[color].peRed;
524             *dst++ = pal[color].peGreen;
525             *dst++ = pal[color].peBlue;
526             if (This->currentDesc.Format == D3DFMT_A8P8)
527                 *dst++ = pal[color].peFlags;
528             else
529                 *dst++ = 0xFF; 
530         }
531
532         ENTER_GL();
533
534         TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
535               gl_target,
536               gl_level, 
537               GL_RGBA,
538               This->currentDesc.Width, 
539               This->currentDesc.Height, 
540               0, 
541               GL_RGBA,
542               GL_UNSIGNED_BYTE,
543               surface);
544         glTexImage2D(gl_target,
545                      gl_level, 
546                      GL_RGBA,
547                      This->currentDesc.Width,
548                      This->currentDesc.Height,
549                      0,
550                      GL_RGBA,
551                      GL_UNSIGNED_BYTE,
552                      surface);
553         checkGLcall("glTexImage2D");
554         HeapFree(GetProcessHeap(), 0, surface);
555
556         LEAVE_GL();
557
558         return D3D_OK;    
559     }
560
561     if (This->currentDesc.Format == D3DFMT_DXT1 || 
562         This->currentDesc.Format == D3DFMT_DXT3 || 
563         This->currentDesc.Format == D3DFMT_DXT5) {
564         if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
565             TRACE("Calling glCompressedTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, sz=%d, Mem=%p\n",
566                   gl_target, 
567                   gl_level, 
568                   D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format), 
569                   This->currentDesc.Width, 
570                   This->currentDesc.Height, 
571                   0, 
572                   This->currentDesc_size,
573                   This->allocatedMemory);
574
575             ENTER_GL();
576
577             GL_EXTCALL(glCompressedTexImage2DARB)(gl_target, 
578                                                   gl_level, 
579                                                   D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
580                                                   This->currentDesc.Width,
581                                                   This->currentDesc.Height,
582                                                   0,
583                                                   This->currentDesc_size,
584                                                   This->allocatedMemory);
585             checkGLcall("glCommpressedTexTexImage2D");
586
587             LEAVE_GL();
588         } else {
589             FIXME("Using DXT1/3/5 without advertized support\n");
590         }
591     } else {
592
593         TRACE("Calling glTexImage2D %x i=%d, d3dfmt=%s, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
594               gl_target, 
595               gl_level, 
596               debug_d3dformat(This->currentDesc.Format),
597               D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format), 
598               This->currentDesc.Width, 
599               This->currentDesc.Height, 
600               0, 
601               D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format), 
602               D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
603               This->allocatedMemory);
604
605         ENTER_GL();
606
607         glTexImage2D(gl_target, 
608                      gl_level,
609                      D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
610                      This->currentDesc.Width,
611                      This->currentDesc.Height,
612                      0,
613                      D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
614                      D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
615                      This->allocatedMemory);
616         checkGLcall("glTexImage2D");
617
618         LEAVE_GL();
619
620 #if 0
621         {
622             static unsigned int gen = 0;
623             char buffer[4096];
624             ++gen;
625             if ((gen % 10) == 0) {
626                 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, gl_target, gl_level, gen);
627                 IWineD3DSurfaceImpl_SaveSnapshot((LPDIRECT3DSURFACE8) This, buffer);
628             }
629             /*
630              * debugging crash code
631             if (gen == 250) {
632               void** test = NULL;
633               *test = 0;
634             }
635             */
636         }
637 #endif
638     }
639
640     return D3D_OK;
641 }
642
643 #include <errno.h>
644 #include <stdio.h>
645 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
646     FILE* f = NULL;
647     ULONG i;
648     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
649
650     f = fopen(filename, "w+");
651     if (NULL == f) {
652         ERR("opening of %s failed with: %s\n", filename, strerror(errno));
653         return D3DERR_INVALIDCALL;
654     }
655
656     TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->currentDesc.Format));
657
658     fprintf(f, "P6\n%u %u\n255\n", This->currentDesc.Width, This->currentDesc.Height);
659     switch (This->currentDesc.Format) {
660     case D3DFMT_X8R8G8B8:
661     case D3DFMT_A8R8G8B8:
662         {
663             DWORD color;
664             for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
665                 color = ((DWORD*) This->allocatedMemory)[i];
666                 fputc((color >> 16) & 0xFF, f);
667                 fputc((color >>  8) & 0xFF, f);
668                 fputc((color >>  0) & 0xFF, f);
669             }
670         }
671         break;
672     case D3DFMT_R8G8B8:
673         {
674             BYTE* color;
675             for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
676                 color = ((BYTE*) This->allocatedMemory) + (3 * i);
677                 fputc((color[0]) & 0xFF, f);
678                 fputc((color[1]) & 0xFF, f);
679                 fputc((color[2]) & 0xFF, f);
680             }
681         }
682         break;
683     case D3DFMT_A1R5G5B5: 
684         {
685             WORD color;
686             for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
687                 color = ((WORD*) This->allocatedMemory)[i];
688                 fputc(((color >> 10) & 0x1F) * 255 / 31, f);
689                 fputc(((color >>  5) & 0x1F) * 255 / 31, f);
690                 fputc(((color >>  0) & 0x1F) * 255 / 31, f);
691             }
692         }
693         break;
694     case D3DFMT_A4R4G4B4:
695         {
696             WORD color;
697             for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
698                 color = ((WORD*) This->allocatedMemory)[i];
699                 fputc(((color >>  8) & 0x0F) * 255 / 15, f);
700                 fputc(((color >>  4) & 0x0F) * 255 / 15, f);
701                 fputc(((color >>  0) & 0x0F) * 255 / 15, f);
702             }
703         }
704         break;
705
706     case D3DFMT_R5G6B5: 
707         {
708             WORD color;
709             for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
710                 color = ((WORD*) This->allocatedMemory)[i];
711                 fputc(((color >> 11) & 0x1F) * 255 / 31, f);
712                 fputc(((color >>  5) & 0x3F) * 255 / 63, f);
713                 fputc(((color >>  0) & 0x1F) * 255 / 31, f);
714             }
715         }
716         break;
717     default: 
718         FIXME("Unimplemented dump mode format(%u,%s)\n", This->currentDesc.Format, debug_d3dformat(This->currentDesc.Format));
719     }
720     fclose(f);
721     return D3D_OK;
722 }
723
724 HRESULT WINAPI IWineD3DSurfaceImpl_CleanDirtyRect(IWineD3DSurface *iface) {
725     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
726     This->Dirty = FALSE;
727     This->dirtyRect.left   = This->currentDesc.Width;
728     This->dirtyRect.top    = This->currentDesc.Height;
729     This->dirtyRect.right  = 0;
730     This->dirtyRect.bottom = 0;
731     TRACE("(%p) : Dirty?%d, Rect:(%ld,%ld,%ld,%ld)\n", This, This->Dirty, This->dirtyRect.left, 
732           This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
733     return D3D_OK;
734 }
735
736 /**
737  *   Slightly inefficient way to handle multiple dirty rects but it works :)
738  */
739 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
740     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
741     This->Dirty = TRUE;
742     if (NULL != pDirtyRect) {
743         This->dirtyRect.left   = min(This->dirtyRect.left,   pDirtyRect->left);
744         This->dirtyRect.top    = min(This->dirtyRect.top,    pDirtyRect->top);
745         This->dirtyRect.right  = max(This->dirtyRect.right,  pDirtyRect->right);
746         This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
747     } else {
748         This->dirtyRect.left   = 0;
749         This->dirtyRect.top    = 0;
750         This->dirtyRect.right  = This->currentDesc.Width;
751         This->dirtyRect.bottom = This->currentDesc.Height;
752     }
753     TRACE("(%p) : Dirty?%d, Rect:(%ld,%ld,%ld,%ld)\n", This, This->Dirty, This->dirtyRect.left, 
754           This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
755     return D3D_OK;
756 }
757
758 IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
759 {
760     IWineD3DSurfaceImpl_QueryInterface,
761     IWineD3DSurfaceImpl_AddRef,
762     IWineD3DSurfaceImpl_Release,
763     IWineD3DSurfaceImpl_GetParent,
764     IWineD3DSurfaceImpl_GetDevice,
765     IWineD3DSurfaceImpl_SetPrivateData,
766     IWineD3DSurfaceImpl_GetPrivateData,
767     IWineD3DSurfaceImpl_FreePrivateData,
768     IWineD3DSurfaceImpl_SetPriority,
769     IWineD3DSurfaceImpl_GetPriority,
770     IWineD3DSurfaceImpl_PreLoad,
771     IWineD3DSurfaceImpl_GetType,
772     IWineD3DSurfaceImpl_GetContainer,
773     IWineD3DSurfaceImpl_GetDesc,
774     IWineD3DSurfaceImpl_LockRect,
775     IWineD3DSurfaceImpl_UnlockRect,
776     IWineD3DSurfaceImpl_GetDC,
777     IWineD3DSurfaceImpl_ReleaseDC,
778     /* Internal use: */
779     IWineD3DSurfaceImpl_CleanDirtyRect,
780     IWineD3DSurfaceImpl_AddDirtyRect,
781     IWineD3DSurfaceImpl_LoadTexture,
782     IWineD3DSurfaceImpl_SaveSnapshot
783 };