Added version information.
[wine] / dlls / d3d8 / surface.c
1 /*
2  * IDirect3DSurface8 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/debug.h"
28
29 #include "d3d8_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32
33 /* trace: */
34 #if 0
35 # define VTRACE(A) TRACE A
36 #else 
37 # define VTRACE(A) 
38 #endif
39
40
41 /* IDirect3DVolume IUnknown parts follow: */
42 HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface,REFIID riid,LPVOID *ppobj)
43 {
44     ICOM_THIS(IDirect3DSurface8Impl,iface);
45
46     if (IsEqualGUID(riid, &IID_IUnknown)
47         || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
48         IDirect3DSurface8Impl_AddRef(iface);
49         *ppobj = This;
50         return D3D_OK;
51     }
52
53     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
54     return E_NOINTERFACE;
55 }
56
57 ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
58     ICOM_THIS(IDirect3DSurface8Impl,iface);
59     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
60     return ++(This->ref);
61 }
62
63 ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
64     ICOM_THIS(IDirect3DSurface8Impl,iface);
65     ULONG ref = --This->ref;
66     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
67     if (ref == 0) {
68       HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
69       HeapFree(GetProcessHeap(), 0, This);
70     }
71     return ref;
72 }
73
74 /* IDirect3DSurface8: */
75 HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8** ppDevice) {
76     ICOM_THIS(IDirect3DSurface8Impl,iface);
77     TRACE("(%p) : returning %p\n", This, This->Device);
78     *ppDevice = (LPDIRECT3DDEVICE8) This->Device;
79     /**
80      * Note  Calling this method will increase the internal reference count 
81      * on the IDirect3DDevice8 interface. 
82      */
83     IDirect3DDevice8Impl_AddRef(*ppDevice);
84     return D3D_OK;
85 }
86 HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
87     ICOM_THIS(IDirect3DSurface8Impl,iface);
88     FIXME("(%p) : stub\n", This);    
89     return D3D_OK;
90 }
91 HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
92     ICOM_THIS(IDirect3DSurface8Impl,iface);
93     FIXME("(%p) : stub\n", This);    
94     return D3D_OK;
95 }
96 HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
97     ICOM_THIS(IDirect3DSurface8Impl,iface);
98     FIXME("(%p) : stub\n", This);    
99     return D3D_OK;
100 }
101 HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void** ppContainer) {
102     ICOM_THIS(IDirect3DSurface8Impl,iface);
103     HRESULT res;
104     res = IUnknown_QueryInterface(This->Container, riid, ppContainer);
105     if (E_NOINTERFACE == res) { 
106       /**
107        * If the surface is created using CreateImageSurface, CreateRenderTarget, 
108        * or CreateDepthStencilSurface, the surface is considered stand alone. In this case, 
109        * GetContainer will return the Direct3D device used to create the surface. 
110        */
111       res = IUnknown_QueryInterface(This->Container, &IID_IDirect3DDevice8, ppContainer);
112     }
113     TRACE("(%p) : returning %p\n", This, *ppContainer);
114     return res;
115 }
116 HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
117     ICOM_THIS(IDirect3DSurface8Impl,iface);
118
119     TRACE("(%p) : copying into %p\n", This, pDesc);
120     memcpy(pDesc, &This->myDesc, sizeof(D3DSURFACE_DESC));
121     return D3D_OK;
122 }
123 HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
124     HRESULT hr;
125     ICOM_THIS(IDirect3DSurface8Impl,iface);
126   
127     /* fixme: should we really lock as such? */
128
129     if (FALSE == This->lockable) {
130       ERR("trying to lock unlockable surf@%p\n", This);  
131       return D3DERR_INVALIDCALL;
132     }
133
134     TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
135
136     pLockedRect->Pitch = This->bytesPerPixel * This->myDesc.Width;  /* Bytes / row */    
137     
138     if (!pRect) {
139       pLockedRect->pBits = This->allocatedMemory;
140       This->lockedRect.left = 0;
141       This->lockedRect.top = 0;
142       This->lockedRect.right = This->myDesc.Width;
143       This->lockedRect.bottom = This->myDesc.Height;
144     } else {
145       TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
146       pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
147       This->lockedRect.left = pRect->left;
148       This->lockedRect.top = pRect->top;
149       This->lockedRect.right = pRect->right;
150       This->lockedRect.bottom = pRect->bottom;
151     }
152
153
154     if (0 == This->myDesc.Usage) { /* classic surface */
155
156       /* Nothing to do ;) */
157
158     } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
159       
160       if (This == This->Device->backBuffer || This == This->Device->frontBuffer) {
161         GLint  prev_store;
162         GLenum prev_read;
163         
164         ENTER_GL();
165
166         /**
167          * for render->surface copy begin to begin of allocatedMemory
168          * unlock can be more easy
169          */
170         pLockedRect->pBits = This->allocatedMemory;
171         
172         glFlush();
173         vcheckGLcall("glFlush");
174         glGetIntegerv(GL_READ_BUFFER, &prev_read);
175         vcheckGLcall("glIntegerv");
176         glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
177         vcheckGLcall("glIntegerv");
178
179         if (This == This->Device->backBuffer) {
180           glReadBuffer(GL_BACK);
181         } else if (This == This->Device->frontBuffer) {
182           glReadBuffer(GL_FRONT);
183         }
184         vcheckGLcall("glReadBuffer");
185
186         switch (This->myDesc.Format) { 
187         case D3DFMT_R5G6B5:
188           {
189             glReadPixels(This->lockedRect.left, This->lockedRect.top, 
190                          This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
191                          GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pLockedRect->pBits);
192             vcheckGLcall("glReadPixels");
193           }
194           break;
195         case D3DFMT_R8G8B8:
196           {
197             glReadPixels(This->lockedRect.left, This->lockedRect.top, 
198                          This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
199                          GL_RGB, GL_UNSIGNED_BYTE, pLockedRect->pBits);
200             vcheckGLcall("glReadPixels");
201           }
202           break;
203         case D3DFMT_A8R8G8B8:
204           {
205             glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
206             vcheckGLcall("glPixelStorei");
207             glReadPixels(This->lockedRect.left, This->lockedRect.top, 
208                          This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
209                          GL_BGRA, GL_UNSIGNED_BYTE, pLockedRect->pBits);
210             vcheckGLcall("glReadPixels");
211             glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
212             vcheckGLcall("glPixelStorei");
213           }
214           break;
215         default:
216           FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
217         }
218
219         glReadBuffer(prev_read);
220         vcheckGLcall("glReadBuffer");
221         LEAVE_GL();
222       } else {
223         FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
224       }
225
226     } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
227
228       FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
229
230     } else {
231       FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
232     }
233
234     if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
235       /* Dont dirtify */
236     } else {
237       /**
238        * Dirtify on lock
239        * as seen in msdn docs
240        */
241       This->Dirty = TRUE;
242       /** Dirtify Container if needed */
243       if (NULL != This->Container) {
244         IDirect3DBaseTexture8* cont = NULL;
245         hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
246         
247         if (SUCCEEDED(hr) && NULL != cont) {
248           /* Now setup the texture appropraitly */
249           D3DRESOURCETYPE containerType = IDirect3DBaseTexture8Impl_GetType(cont);
250           if (containerType == D3DRTYPE_TEXTURE) {
251             IDirect3DTexture8Impl* pTexture = (IDirect3DTexture8Impl*) cont;
252             pTexture->Dirty = TRUE;
253           } else if (containerType == D3DRTYPE_CUBETEXTURE) {
254             IDirect3DCubeTexture8Impl* pTexture = (IDirect3DCubeTexture8Impl*) cont;
255             pTexture->Dirty = TRUE;
256           } else {
257             FIXME("Set dirty on container type %d\n", containerType);
258           }
259           IDirect3DBaseTexture8_Release(cont);
260           cont = NULL;
261         }
262       }
263     }
264
265     TRACE("returning pBits=%p, pitch=%d\n", pLockedRect->pBits, pLockedRect->Pitch);
266
267     This->locked = TRUE;
268     return D3D_OK;
269 }
270 HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
271     ICOM_THIS(IDirect3DSurface8Impl,iface);
272     
273     if (FALSE == This->locked) {
274       ERR("trying to lock unlocked surf@%p\n", This);  
275       return D3DERR_INVALIDCALL;
276     }
277
278     TRACE("(%p) : see if behavior is correct\n", This);
279
280     if (FALSE == This->Dirty) {
281       TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
282       goto unlock_end;
283     }
284
285     if (0 == This->myDesc.Usage) { /* classic surface */
286 #if 0
287       if (This->Container) {
288         HRESULT hr;
289         IDirect3DBaseTexture8* cont = NULL;
290         hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
291         
292         if (SUCCEEDED(hr) && NULL != cont) {
293           /* Now setup the texture appropraitly */
294           int containerType = IDirect3DBaseTexture8Impl_GetType(cont);
295           if (containerType == D3DRTYPE_TEXTURE) {
296             IDirect3DTexture8Impl *pTexture = (IDirect3DTexture8Impl *)cont;
297             pTexture->Dirty = TRUE;
298           } else if (containerType == D3DRTYPE_CUBETEXTURE) {
299             IDirect3DCubeTexture8Impl *pTexture = (IDirect3DCubeTexture8Impl *)cont;
300             pTexture->Dirty = TRUE;
301           } else {
302             FIXME("Set dirty on container type %d\n", containerType);
303           }
304           IDirect3DBaseTexture8_Release(cont);
305           cont = NULL;
306         }
307       }
308 #endif
309     } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
310
311       if (This == This->Device->backBuffer || This == This->Device->frontBuffer) {
312         GLint  prev_store;
313         GLenum prev_draw;
314         
315         ENTER_GL();
316         
317         glFlush();
318         vcheckGLcall("glFlush");
319         glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
320         vcheckGLcall("glIntegerv");
321         glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
322         vcheckGLcall("glIntegerv");
323
324         if (This == This->Device->backBuffer) {
325           glDrawBuffer(GL_BACK);
326         } else if (This == This->Device->frontBuffer) {
327           glDrawBuffer(GL_FRONT);
328         }
329         vcheckGLcall("glDrawBuffer");
330
331         glRasterPos2i(This->lockedRect.left, This->lockedRect.top);
332         vcheckGLcall("glRasterPos2f");
333         switch (This->myDesc.Format) {
334         case D3DFMT_R5G6B5:
335           {
336             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
337                          GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
338             vcheckGLcall("glDrawPixels");
339           }
340           break;
341         case D3DFMT_R8G8B8:
342           {
343             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
344                          GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
345             vcheckGLcall("glDrawPixels");
346           }
347           break;
348         case D3DFMT_A8R8G8B8:
349           {
350             glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
351             vcheckGLcall("glPixelStorei");
352             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
353                          GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
354             vcheckGLcall("glDrawPixels");
355             glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
356             vcheckGLcall("glPixelStorei");
357           }
358           break;
359         default:
360           FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
361         }
362
363         glDrawBuffer(prev_draw);
364         vcheckGLcall("glDrawBuffer");
365         LEAVE_GL();
366       } else {
367         FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
368       }
369
370     } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
371     
372       if (This == This->Device->depthStencilBuffer) {
373         FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
374       } else {
375         FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
376       }
377
378     } else {
379       FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
380     }
381
382 unlock_end:
383     This->locked = FALSE;
384     return D3D_OK;
385 }
386
387
388 ICOM_VTABLE(IDirect3DSurface8) Direct3DSurface8_Vtbl =
389 {
390     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
391     IDirect3DSurface8Impl_QueryInterface,
392     IDirect3DSurface8Impl_AddRef,
393     IDirect3DSurface8Impl_Release,
394     IDirect3DSurface8Impl_GetDevice,
395     IDirect3DSurface8Impl_SetPrivateData,
396     IDirect3DSurface8Impl_GetPrivateData,
397     IDirect3DSurface8Impl_FreePrivateData,
398     IDirect3DSurface8Impl_GetContainer,
399     IDirect3DSurface8Impl_GetDesc,
400     IDirect3DSurface8Impl_LockRect,
401     IDirect3DSurface8Impl_UnlockRect,
402 };