- Handle failures to allocate storage more gracefully.
[wine/multimedia.git] / dlls / wined3d / surface.c
blob8381f03e5468fa9590e2e69b97250ac1526ec6fe
1 /*
2 * IWineD3DSurface Implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
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.
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.
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
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29 /* *******************************************
30 IWineD3DSurface IUnknown parts follow
31 ******************************************* */
32 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
34 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
35 WARN("(%p)->(%s,%p) should not be called\n",This,debugstr_guid(riid),ppobj);
36 return E_NOINTERFACE;
39 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface) {
40 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
41 TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
42 IUnknown_AddRef(This->resource.parent);
43 return InterlockedIncrement(&This->resource.ref);
46 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
47 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
48 ULONG ref;
49 TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
50 ref = InterlockedDecrement(&This->resource.ref);
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 */
58 return ref;
61 /* ****************************************************
62 IWineD3DSurface IWineD3DResource parts follow
63 **************************************************** */
64 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
65 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
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);
72 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
73 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
76 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
77 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
80 DWORD WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
81 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
84 DWORD WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
85 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
88 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
89 return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
92 D3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface) {
93 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
96 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
97 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
100 /* ******************************************************
101 IWineD3DSurface IWineD3DSurface parts follow
102 ****************************************************** */
104 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface *iface, REFIID riid, void** ppContainer) {
105 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
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.
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;
119 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
120 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
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;
135 HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
136 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
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;
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; */
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);
163 } else {
164 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
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 */
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);
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);
192 This->lockedRect.left = pRect->left;
193 This->lockedRect.top = pRect->top;
194 This->lockedRect.right = pRect->right;
195 This->lockedRect.bottom = pRect->bottom;
199 if (0 == This->currentDesc.Usage) { /* classic surface */
201 /* Nothing to do ;) */
203 } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage && !(Flags&D3DLOCK_DISCARD)) { /* render surfaces */
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;
209 ENTER_GL();
212 * for render->surface copy begin to begin of allocatedMemory
213 * unlock can be more easy
215 pLockedRect->pBits = This->allocatedMemory;
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");
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");
231 vcheckGLcall("glReadBuffer");
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,
242 format,
243 type,
244 (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
245 vcheckGLcall("glReadPixels");
249 glReadBuffer(prev_read);
250 vcheckGLcall("glReadBuffer");
252 LEAVE_GL();
254 } else {
255 FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
258 } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
260 FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
262 } else {
263 FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
266 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
267 /* Don't dirtify */
268 } else {
270 * Dirtify on lock
271 * as seen in msdn docs
273 IWineD3DSurface_AddDirtyRect(iface, &This->lockedRect);
275 /** Dirtify Container if needed */
276 if ((NULL != This->container) && ((IWineD3DDeviceImpl *)This->container != This->resource.wineD3DDevice)) {
277 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)This->container, TRUE);
281 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
283 This->locked = TRUE;
284 return D3D_OK;
287 HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
288 GLint skipBytes = 0;
289 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
291 if (FALSE == This->locked) {
292 ERR("trying to Unlock an unlocked surf@%p\n", This);
293 return D3DERR_INVALIDCALL;
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);
306 } else {
307 TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
310 if (FALSE == This->Dirty) {
311 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
312 goto unlock_end;
315 if (0 == This->currentDesc.Usage) { /* classic surface */
317 * nothing to do
318 * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
320 } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage) { /* render surfaces */
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];
327 ENTER_GL();
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");
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) {
345 double X, Y, height, width, minZ, maxZ;
346 This->resource.wineD3DDevice->last_was_rhw = TRUE;
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");
356 glMatrixMode(GL_PROJECTION);
357 checkGLcall("glMatrixMode");
358 glLoadIdentity();
359 checkGLcall("glLoadIdentity");
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");
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)");
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);
383 vcheckGLcall("glDrawBuffer");
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);
389 /* And back buffers are not blended */
390 glDisable(GL_BLEND);
392 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
393 vcheckGLcall("glRasterPos2f");
394 switch (This->currentDesc.Format) {
395 case D3DFMT_R5G6B5:
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");
401 break;
402 case D3DFMT_R8G8B8:
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");
408 break;
409 case D3DFMT_A8R8G8B8:
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");
419 break;
420 default:
421 FIXME("Unsupported Format %u in locking func\n", This->currentDesc.Format);
424 glPixelZoom(1.0,1.0);
425 vcheckGLcall("glPixelZoom");
426 glDrawBuffer(prev_draw);
427 vcheckGLcall("glDrawBuffer");
428 glRasterPos3iv(&prev_rasterpos[0]);
429 vcheckGLcall("glRasterPos3iv");
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);
435 LEAVE_GL();
437 /** restore clean dirty state */
438 IWineD3DSurface_CleanDirtyRect(iface);
440 } else {
441 FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
444 } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
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);
452 } else {
453 FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
456 unlock_end:
457 This->locked = FALSE;
458 memset(&This->lockedRect, 0, sizeof(RECT));
459 return D3D_OK;
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;
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;
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;
480 if (This->inTexture)
481 return D3D_OK;
483 if (This->inPBuffer) {
484 ENTER_GL();
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,
495 D3DFmt2GLIntFmt(This->resource.wineD3DDevice,
496 This->currentDesc.Format),
499 This->currentDesc.Width,
500 This->currentDesc.Height,
502 TRACE("Updating target %d\n", gl_target);
503 This->inTexture = TRUE;
505 LEAVE_GL();
506 return D3D_OK;
509 if ((This->currentDesc.Format == D3DFMT_P8 || This->currentDesc.Format == D3DFMT_A8P8) &&
510 !GL_SUPPORT(EXT_PALETTED_TEXTURE)) {
512 * wanted a paletted texture and not really support it in HW
513 * so software emulation code begin
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;
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;
532 ENTER_GL();
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,
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,
550 GL_RGBA,
551 GL_UNSIGNED_BYTE,
552 surface);
553 checkGLcall("glTexImage2D");
554 HeapFree(GetProcessHeap(), 0, surface);
556 LEAVE_GL();
558 return D3D_OK;
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,
572 This->currentDesc_size,
573 This->allocatedMemory);
575 ENTER_GL();
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,
583 This->currentDesc_size,
584 This->allocatedMemory);
585 checkGLcall("glCommpressedTexTexImage2D");
587 LEAVE_GL();
588 } else {
589 FIXME("Using DXT1/3/5 without advertized support\n");
591 } else {
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,
601 D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
602 D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
603 This->allocatedMemory);
605 ENTER_GL();
607 glTexImage2D(gl_target,
608 gl_level,
609 D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
610 This->currentDesc.Width,
611 This->currentDesc.Height,
613 D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
614 D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
615 This->allocatedMemory);
616 checkGLcall("glTexImage2D");
618 LEAVE_GL();
620 #if 0
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);
630 * debugging crash code
631 if (gen == 250) {
632 void** test = NULL;
633 *test = 0;
637 #endif
640 return D3D_OK;
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;
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;
656 TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->currentDesc.Format));
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:
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);
671 break;
672 case D3DFMT_R8G8B8:
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);
682 break;
683 case D3DFMT_A1R5G5B5:
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);
693 break;
694 case D3DFMT_A4R4G4B4:
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);
704 break;
706 case D3DFMT_R5G6B5:
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);
716 break;
717 default:
718 FIXME("Unimplemented dump mode format(%u,%s)\n", This->currentDesc.Format, debug_d3dformat(This->currentDesc.Format));
720 fclose(f);
721 return D3D_OK;
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;
737 * Slightly inefficient way to handle multiple dirty rects but it works :)
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;
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;
758 IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
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