quartz: Silence requests for ipin on filters.
[wine] / dlls / wined3d / surface_base.c
1 /*
2  * IWineD3DSurface Implementation of management(non-rendering) functions
3  *
4  * Copyright 1998 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  * Copyright 2002-2005 Jason Edmeades
7  * Copyright 2002-2003 Raphael Junqueira
8  * Copyright 2004 Christian Costa
9  * Copyright 2005 Oliver Stieber
10  * Copyright 2006-2007 Stefan Dösinger for CodeWeavers
11  * Copyright 2007 Henri Verbeet
12  * Copyright 2006-2007 Roderick Colenbrander
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
32
33 #include <assert.h>
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
36
37 /* Do NOT define GLINFO_LOCATION in this file. THIS CODE MUST NOT USE IT */
38
39 /* *******************************************
40    IWineD3DSurface IUnknown parts follow
41    ******************************************* */
42 HRESULT WINAPI IWineD3DBaseSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
43 {
44     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
45     /* Warn ,but be nice about things */
46     TRACE("(%p)->(%s,%p)\n", This,debugstr_guid(riid),ppobj);
47
48     if (IsEqualGUID(riid, &IID_IUnknown)
49         || IsEqualGUID(riid, &IID_IWineD3DBase)
50         || IsEqualGUID(riid, &IID_IWineD3DResource)
51         || IsEqualGUID(riid, &IID_IWineD3DSurface)) {
52         IUnknown_AddRef((IUnknown*)iface);
53         *ppobj = This;
54         return S_OK;
55         }
56         *ppobj = NULL;
57         return E_NOINTERFACE;
58 }
59
60 ULONG WINAPI IWineD3DBaseSurfaceImpl_AddRef(IWineD3DSurface *iface) {
61     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
62     ULONG ref = InterlockedIncrement(&This->resource.ref);
63     TRACE("(%p) : AddRef increasing from %d\n", This,ref - 1);
64     return ref;
65 }
66
67 /* ****************************************************
68    IWineD3DSurface IWineD3DResource parts follow
69    **************************************************** */
70 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
71     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
72 }
73
74 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
75     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
76 }
77
78 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
79     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
80 }
81
82 HRESULT WINAPI IWineD3DBaseSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
83     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
84 }
85
86 DWORD   WINAPI IWineD3DBaseSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
87     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
88 }
89
90 DWORD   WINAPI IWineD3DBaseSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
91     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
92 }
93
94 WINED3DRESOURCETYPE WINAPI IWineD3DBaseSurfaceImpl_GetType(IWineD3DSurface *iface) {
95     TRACE("(%p) : calling resourceimpl_GetType\n", iface);
96     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
97 }
98
99 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
100     TRACE("(%p) : calling resourceimpl_GetParent\n", iface);
101     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
102 }
103
104 /* ******************************************************
105    IWineD3DSurface IWineD3DSurface parts follow
106    ****************************************************** */
107
108 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer) {
109     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
110     IWineD3DBase *container = 0;
111
112     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
113
114     if (!ppContainer) {
115         ERR("Called without a valid ppContainer.\n");
116     }
117
118     /** From MSDN:
119      * If the surface is created using CreateImageSurface/CreateOffscreenPlainSurface, CreateRenderTarget,
120      * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
121      * GetContainer will return the Direct3D device used to create the surface.
122      */
123     if (This->container) {
124         container = This->container;
125     } else {
126         container = (IWineD3DBase *)This->resource.wineD3DDevice;
127     }
128
129     TRACE("Relaying to QueryInterface\n");
130     return IUnknown_QueryInterface(container, riid, ppContainer);
131 }
132
133 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
134     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
135
136     TRACE("(%p) : copying into %p\n", This, pDesc);
137     if(pDesc->Format != NULL)             *(pDesc->Format) = This->resource.format;
138     if(pDesc->Type != NULL)               *(pDesc->Type)   = This->resource.resourceType;
139     if(pDesc->Usage != NULL)              *(pDesc->Usage)              = This->resource.usage;
140     if(pDesc->Pool != NULL)               *(pDesc->Pool)               = This->resource.pool;
141     if(pDesc->Size != NULL)               *(pDesc->Size)               = This->resource.size;   /* dx8 only */
142     if(pDesc->MultiSampleType != NULL)    *(pDesc->MultiSampleType)    = This->currentDesc.MultiSampleType;
143     if(pDesc->MultiSampleQuality != NULL) *(pDesc->MultiSampleQuality) = This->currentDesc.MultiSampleQuality;
144     if(pDesc->Width != NULL)              *(pDesc->Width)              = This->currentDesc.Width;
145     if(pDesc->Height != NULL)             *(pDesc->Height)             = This->currentDesc.Height;
146     return WINED3D_OK;
147 }
148
149 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags) {
150     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
151     TRACE("(%p)->(%x)\n", This, Flags);
152
153     switch (Flags)
154     {
155         case WINEDDGBS_CANBLT:
156         case WINEDDGBS_ISBLTDONE:
157             return WINED3D_OK;
158
159         default:
160             return WINED3DERR_INVALIDCALL;
161     }
162 }
163
164 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags) {
165     /* XXX: DDERR_INVALIDSURFACETYPE */
166
167     TRACE("(%p)->(%08x)\n",iface,Flags);
168     switch (Flags) {
169         case WINEDDGFS_CANFLIP:
170         case WINEDDGFS_ISFLIPDONE:
171             return WINED3D_OK;
172
173         default:
174             return WINED3DERR_INVALIDCALL;
175     }
176 }
177
178 HRESULT WINAPI IWineD3DBaseSurfaceImpl_IsLost(IWineD3DSurface *iface) {
179     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
180     TRACE("(%p)\n", This);
181
182     /* D3D8 and 9 loose full devices, ddraw only surfaces */
183     return This->Flags & SFLAG_LOST ? WINED3DERR_DEVICELOST : WINED3D_OK;
184 }
185
186 HRESULT WINAPI IWineD3DBaseSurfaceImpl_Restore(IWineD3DSurface *iface) {
187     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
188     TRACE("(%p)\n", This);
189
190     /* So far we don't lose anything :) */
191     This->Flags &= ~SFLAG_LOST;
192     return WINED3D_OK;
193 }
194
195 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal) {
196     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
197     IWineD3DPaletteImpl *PalImpl = (IWineD3DPaletteImpl *) Pal;
198     TRACE("(%p)->(%p)\n", This, Pal);
199
200     if(This->palette == PalImpl) {
201         TRACE("Nop palette change\n");
202         return WINED3D_OK;
203     }
204
205     if(This->palette != NULL)
206         if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
207             This->palette->Flags &= ~WINEDDPCAPS_PRIMARYSURFACE;
208
209     if(PalImpl != NULL) {
210         if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
211             /* Set the device's main palette if the palette
212             * wasn't a primary palette before
213             */
214             if(!(PalImpl->Flags & WINEDDPCAPS_PRIMARYSURFACE)) {
215                 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
216                 unsigned int i;
217
218                 for(i=0; i < 256; i++) {
219                     device->palettes[device->currentPalette][i] = PalImpl->palents[i];
220                 }
221             }
222
223             (PalImpl)->Flags |= WINEDDPCAPS_PRIMARYSURFACE;
224         }
225     }
226     This->palette = PalImpl;
227
228     return IWineD3DSurface_RealizePalette(iface);
229 }
230
231 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, WINEDDCOLORKEY *CKey) {
232     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
233     TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
234
235     if ((Flags & WINEDDCKEY_COLORSPACE) != 0) {
236         FIXME(" colorkey value not supported (%08x) !\n", Flags);
237         return WINED3DERR_INVALIDCALL;
238     }
239
240     /* Dirtify the surface, but only if a key was changed */
241     if(CKey) {
242         switch (Flags & ~WINEDDCKEY_COLORSPACE) {
243             case WINEDDCKEY_DESTBLT:
244                 This->DestBltCKey = *CKey;
245                 This->CKeyFlags |= WINEDDSD_CKDESTBLT;
246                 break;
247
248             case WINEDDCKEY_DESTOVERLAY:
249                 This->DestOverlayCKey = *CKey;
250                 This->CKeyFlags |= WINEDDSD_CKDESTOVERLAY;
251                 break;
252
253             case WINEDDCKEY_SRCOVERLAY:
254                 This->SrcOverlayCKey = *CKey;
255                 This->CKeyFlags |= WINEDDSD_CKSRCOVERLAY;
256                 break;
257
258             case WINEDDCKEY_SRCBLT:
259                 This->SrcBltCKey = *CKey;
260                 This->CKeyFlags |= WINEDDSD_CKSRCBLT;
261                 break;
262         }
263     }
264     else {
265         switch (Flags & ~WINEDDCKEY_COLORSPACE) {
266             case WINEDDCKEY_DESTBLT:
267                 This->CKeyFlags &= ~WINEDDSD_CKDESTBLT;
268                 break;
269
270             case WINEDDCKEY_DESTOVERLAY:
271                 This->CKeyFlags &= ~WINEDDSD_CKDESTOVERLAY;
272                 break;
273
274             case WINEDDCKEY_SRCOVERLAY:
275                 This->CKeyFlags &= ~WINEDDSD_CKSRCOVERLAY;
276                 break;
277
278             case WINEDDCKEY_SRCBLT:
279                 This->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
280                 break;
281         }
282     }
283
284     return WINED3D_OK;
285 }
286
287 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal) {
288     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
289     TRACE("(%p)->(%p)\n", This, Pal);
290
291     *Pal = (IWineD3DPalette *) This->palette;
292     return WINED3D_OK;
293 }
294
295 DWORD WINAPI IWineD3DBaseSurfaceImpl_GetPitch(IWineD3DSurface *iface) {
296     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
297     DWORD ret;
298     TRACE("(%p)\n", This);
299
300     /* DXTn formats don't have exact pitches as they are to the new row of blocks,
301     where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt2/3/4/5)
302     ie pitch = (width/4) * bytes per block                                  */
303     if (This->resource.format == WINED3DFMT_DXT1) /* DXT1 is 8 bytes per block */
304         ret = ((This->currentDesc.Width + 3) >> 2) << 3;
305     else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
306              This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) /* DXT2/3/4/5 is 16 bytes per block */
307         ret = ((This->currentDesc.Width + 3) >> 2) << 4;
308     else {
309         unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
310         ret = This->bytesPerPixel * This->currentDesc.Width;  /* Bytes / row */
311         ret = (ret + alignment - 1) & ~(alignment - 1);
312     }
313     TRACE("(%p) Returning %d\n", This, ret);
314     return ret;
315 }
316
317 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y) {
318     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
319
320     FIXME("(%p)->(%d,%d) Stub!\n", This, X, Y);
321
322     if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
323     {
324         TRACE("(%p): Not an overlay surface\n", This);
325         return WINEDDERR_NOTAOVERLAYSURFACE;
326     }
327
328     return WINED3D_OK;
329 }
330
331 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y) {
332     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
333
334     FIXME("(%p)->(%p,%p) Stub!\n", This, X, Y);
335
336     if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
337     {
338         TRACE("(%p): Not an overlay surface\n", This);
339         return WINEDDERR_NOTAOVERLAYSURFACE;
340     }
341
342     return WINED3D_OK;
343 }
344
345 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref) {
346     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
347     IWineD3DSurfaceImpl *RefImpl = (IWineD3DSurfaceImpl *) Ref;
348
349     FIXME("(%p)->(%08x,%p) Stub!\n", This, Flags, RefImpl);
350
351     if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
352     {
353         TRACE("(%p): Not an overlay surface\n", This);
354         return WINEDDERR_NOTAOVERLAYSURFACE;
355     }
356
357     return WINED3D_OK;
358 }
359
360 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, RECT *SrcRect, IWineD3DSurface *DstSurface, RECT *DstRect, DWORD Flags, WINEDDOVERLAYFX *FX) {
361     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
362     IWineD3DSurfaceImpl *Dst = (IWineD3DSurfaceImpl *) DstSurface;
363     FIXME("(%p)->(%p, %p, %p, %08x, %p)\n", This, SrcRect, Dst, DstRect, Flags, FX);
364
365     if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
366     {
367         TRACE("(%p): Not an overlay surface\n", This);
368         return WINEDDERR_NOTAOVERLAYSURFACE;
369     }
370
371     return WINED3D_OK;
372 }
373
374 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetClipper(IWineD3DSurface *iface, IWineD3DClipper *clipper)
375 {
376     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
377     TRACE("(%p)->(%p)\n", This, clipper);
378
379     This->clipper = clipper;
380     return WINED3D_OK;
381 }
382
383 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetClipper(IWineD3DSurface *iface, IWineD3DClipper **clipper)
384 {
385     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
386     TRACE("(%p)->(%p)\n", This, clipper);
387
388     *clipper = This->clipper;
389     if(*clipper) {
390         IWineD3DClipper_AddRef(*clipper);
391     }
392     return WINED3D_OK;
393 }
394
395 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
396     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
397
398     TRACE("This %p, container %p\n", This, container);
399
400     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
401
402     TRACE("Setting container to %p from %p\n", container, This->container);
403     This->container = container;
404
405     return WINED3D_OK;
406 }
407
408 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
409     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
410     const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(format, NULL, NULL);
411
412     if (This->resource.format != WINED3DFMT_UNKNOWN) {
413         FIXME("(%p) : The format of the surface must be WINED3DFORMAT_UNKNOWN\n", This);
414         return WINED3DERR_INVALIDCALL;
415     }
416
417     TRACE("(%p) : Setting texture format to (%d,%s)\n", This, format, debug_d3dformat(format));
418     if (format == WINED3DFMT_UNKNOWN) {
419         This->resource.size = 0;
420     } else if (format == WINED3DFMT_DXT1) {
421         /* DXT1 is half byte per pixel */
422         This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4)) >> 1;
423
424     } else if (format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3 ||
425                format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5) {
426         This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4));
427     } else {
428         unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
429         This->resource.size = ((This->pow2Width * formatEntry->bpp) + alignment - 1) & ~(alignment - 1);
430         This->resource.size *= This->pow2Height;
431     }
432
433     if (format != WINED3DFMT_UNKNOWN) {
434         This->bytesPerPixel = formatEntry->bpp;
435     } else {
436         This->bytesPerPixel = 0;
437     }
438
439     This->Flags |= (WINED3DFMT_D16_LOCKABLE == format) ? SFLAG_LOCKABLE : 0;
440
441     This->resource.format = format;
442
443     TRACE("(%p) : Size %d, bytesPerPixel %d\n", This, This->resource.size, This->bytesPerPixel);
444
445     return WINED3D_OK;
446 }
447
448 HRESULT IWineD3DBaseSurfaceImpl_CreateDIBSection(IWineD3DSurface *iface) {
449     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
450     int extraline = 0;
451     SYSTEM_INFO sysInfo;
452     BITMAPINFO* b_info;
453     HDC ddc;
454     DWORD *masks;
455     const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
456     UINT usage;
457
458     switch (This->bytesPerPixel) {
459         case 2:
460         case 4:
461             /* Allocate extra space to store the RGB bit masks. */
462             b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
463             break;
464
465         case 3:
466             b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
467             break;
468
469         default:
470             /* Allocate extra space for a palette. */
471             b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
472                                sizeof(BITMAPINFOHEADER)
473                                + sizeof(RGBQUAD)
474                                * (1 << (This->bytesPerPixel * 8)));
475             break;
476     }
477
478     if (!b_info)
479         return E_OUTOFMEMORY;
480
481         /* Some apps access the surface in via DWORDs, and do not take the necessary care at the end of the
482     * surface. So we need at least extra 4 bytes at the end of the surface. Check against the page size,
483     * if the last page used for the surface has at least 4 spare bytes we're safe, otherwise
484     * add an extra line to the dib section
485         */
486     GetSystemInfo(&sysInfo);
487     if( ((This->resource.size + 3) % sysInfo.dwPageSize) < 4) {
488         extraline = 1;
489         TRACE("Adding an extra line to the dib section\n");
490     }
491
492     b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
493     /* TODO: Is there a nicer way to force a specific alignment? (8 byte for ddraw) */
494     b_info->bmiHeader.biWidth = IWineD3DSurface_GetPitch(iface) / This->bytesPerPixel;
495     b_info->bmiHeader.biHeight = -This->currentDesc.Height -extraline;
496     b_info->bmiHeader.biSizeImage = ( This->currentDesc.Height + extraline) * IWineD3DSurface_GetPitch(iface);
497     b_info->bmiHeader.biPlanes = 1;
498     b_info->bmiHeader.biBitCount = This->bytesPerPixel * 8;
499
500     b_info->bmiHeader.biXPelsPerMeter = 0;
501     b_info->bmiHeader.biYPelsPerMeter = 0;
502     b_info->bmiHeader.biClrUsed = 0;
503     b_info->bmiHeader.biClrImportant = 0;
504
505     /* Get the bit masks */
506     masks = (DWORD *) &(b_info->bmiColors);
507     switch (This->resource.format) {
508         case WINED3DFMT_R8G8B8:
509             usage = DIB_RGB_COLORS;
510             b_info->bmiHeader.biCompression = BI_RGB;
511             break;
512
513         case WINED3DFMT_X1R5G5B5:
514         case WINED3DFMT_A1R5G5B5:
515         case WINED3DFMT_A4R4G4B4:
516         case WINED3DFMT_X4R4G4B4:
517         case WINED3DFMT_R3G3B2:
518         case WINED3DFMT_A8R3G3B2:
519         case WINED3DFMT_A2B10G10R10:
520         case WINED3DFMT_A8B8G8R8:
521         case WINED3DFMT_X8B8G8R8:
522         case WINED3DFMT_A2R10G10B10:
523         case WINED3DFMT_R5G6B5:
524         case WINED3DFMT_A16B16G16R16:
525             usage = 0;
526             b_info->bmiHeader.biCompression = BI_BITFIELDS;
527             masks[0] = formatEntry->redMask;
528             masks[1] = formatEntry->greenMask;
529             masks[2] = formatEntry->blueMask;
530             break;
531
532         default:
533             /* Don't know palette */
534             b_info->bmiHeader.biCompression = BI_RGB;
535             usage = 0;
536             break;
537     }
538
539     ddc = GetDC(0);
540     if (ddc == 0) {
541         HeapFree(GetProcessHeap(), 0, b_info);
542         return HRESULT_FROM_WIN32(GetLastError());
543     }
544
545     TRACE("Creating a DIB section with size %dx%dx%d, size=%d\n", b_info->bmiHeader.biWidth, b_info->bmiHeader.biHeight, b_info->bmiHeader.biBitCount, b_info->bmiHeader.biSizeImage);
546     This->dib.DIBsection = CreateDIBSection(ddc, b_info, usage, &This->dib.bitmap_data, 0 /* Handle */, 0 /* Offset */);
547     ReleaseDC(0, ddc);
548
549     if (!This->dib.DIBsection) {
550         ERR("CreateDIBSection failed!\n");
551         HeapFree(GetProcessHeap(), 0, b_info);
552         return HRESULT_FROM_WIN32(GetLastError());
553     }
554
555     TRACE("DIBSection at : %p\n", This->dib.bitmap_data);
556     /* copy the existing surface to the dib section */
557     if(This->resource.allocatedMemory) {
558         memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, b_info->bmiHeader.biSizeImage);
559     } else {
560         /* This is to make LockRect read the gl Texture although memory is allocated */
561         This->Flags &= ~SFLAG_INSYSMEM;
562     }
563     This->dib.bitmap_size = b_info->bmiHeader.biSizeImage;
564
565     HeapFree(GetProcessHeap(), 0, b_info);
566
567     /* Now allocate a HDC */
568     This->hDC = CreateCompatibleDC(0);
569     This->dib.holdbitmap = SelectObject(This->hDC, This->dib.DIBsection);
570     TRACE("using wined3d palette %p\n", This->palette);
571     SelectPalette(This->hDC,
572                   This->palette ? This->palette->hpal : 0,
573                   FALSE);
574
575     This->Flags |= SFLAG_DIBSECTION;
576
577     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
578     This->resource.heapMemory = NULL;
579
580     return WINED3D_OK;
581 }
582
583 void convert_r32f_r16f(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h) {
584     unsigned int x, y;
585     float *src_f;
586     unsigned short *dst_s;
587
588     TRACE("Converting %dx%d pixels, pitches %d %d\n", w, h, pitch_in, pitch_out);
589     for(y = 0; y < h; y++) {
590         src_f = (float *) (src + y * pitch_in);
591         dst_s = (unsigned short *) (dst + y * pitch_out);
592         for(x = 0; x < w; x++) {
593             dst_s[x] = float_32_to_16(src_f + x);
594         }
595     }
596 }
597
598 struct d3dfmt_convertor_desc {
599     WINED3DFORMAT from, to;
600     void (*convert)(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h);
601 };
602
603 struct d3dfmt_convertor_desc convertors[] = {
604     {WINED3DFMT_R32F,       WINED3DFMT_R16F,        convert_r32f_r16f},
605 };
606
607 static inline struct d3dfmt_convertor_desc *find_convertor(WINED3DFORMAT from, WINED3DFORMAT to) {
608     unsigned int i;
609     for(i = 0; i < (sizeof(convertors) / sizeof(convertors[0])); i++) {
610         if(convertors[i].from == from && convertors[i].to == to) {
611             return &convertors[i];
612         }
613     }
614     return NULL;
615 }
616
617 /*****************************************************************************
618  * surface_convert_format
619  *
620  * Creates a duplicate of a surface in a different format. Is used by Blt to
621  * blit between surfaces with different formats
622  *
623  * Parameters
624  *  source: Source surface
625  *  fmt: Requested destination format
626  *
627  *****************************************************************************/
628 IWineD3DSurfaceImpl *surface_convert_format(IWineD3DSurfaceImpl *source, WINED3DFORMAT to_fmt) {
629     IWineD3DSurface *ret = NULL;
630     struct d3dfmt_convertor_desc *conv;
631     WINED3DLOCKED_RECT lock_src, lock_dst;
632     HRESULT hr;
633
634     conv = find_convertor(source->resource.format, to_fmt);
635     if(!conv) {
636         FIXME("Cannot find a conversion function from format %s to %s\n",
637               debug_d3dformat(source->resource.format), debug_d3dformat(to_fmt));
638         return NULL;
639     }
640
641     IWineD3DDevice_CreateSurface((IWineD3DDevice *) source->resource.wineD3DDevice,
642                                  source->currentDesc.Width,
643                                  source->currentDesc.Height,
644                                  to_fmt,
645                                  TRUE,  /* lockable */
646                                  TRUE,  /* discard  */
647                                  0,     /* level */
648                                  &ret,
649                                  WINED3DRTYPE_SURFACE,
650                                  0,     /* usage */
651                                  WINED3DPOOL_SCRATCH,
652                                  WINED3DMULTISAMPLE_NONE,   /* TODO: Multisampled conversion */
653                                  0,     /* multisamplequality */
654                                  NULL,  /* sharedhandle */
655                                  IWineD3DSurface_GetImplType((IWineD3DSurface *) source),
656                                  NULL); /* parent */
657     if(!ret) {
658         ERR("Failed to create a destination surface for conversion\n");
659         return NULL;
660     }
661
662     memset(&lock_src, 0, sizeof(lock_src));
663     memset(&lock_dst, 0, sizeof(lock_dst));
664
665     hr = IWineD3DSurface_LockRect((IWineD3DSurface *) source, &lock_src, NULL, WINED3DLOCK_READONLY);
666     if(FAILED(hr)) {
667         ERR("Failed to lock the source surface\n");
668         IWineD3DSurface_Release(ret);
669         return NULL;
670     }
671     hr = IWineD3DSurface_LockRect(ret, &lock_dst, NULL, WINED3DLOCK_READONLY);
672     if(FAILED(hr)) {
673         ERR("Failed to lock the dest surface\n");
674         IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
675         IWineD3DSurface_Release(ret);
676         return NULL;
677     }
678
679     conv->convert(lock_src.pBits, lock_dst.pBits, lock_src.Pitch, lock_dst.Pitch,
680                   source->currentDesc.Width, source->currentDesc.Height);
681
682     IWineD3DSurface_UnlockRect(ret);
683     IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
684
685     return (IWineD3DSurfaceImpl *) ret;
686 }
687
688 /*****************************************************************************
689  * _Blt_ColorFill
690  *
691  * Helper function that fills a memory area with a specific color
692  *
693  * Params:
694  *  buf: memory address to start filling at
695  *  width, height: Dimensions of the area to fill
696  *  bpp: Bit depth of the surface
697  *  lPitch: pitch of the surface
698  *  color: Color to fill with
699  *
700  *****************************************************************************/
701 static HRESULT
702         _Blt_ColorFill(BYTE *buf,
703                        int width, int height,
704                        int bpp, LONG lPitch,
705                        DWORD color)
706 {
707     int x, y;
708     LPBYTE first;
709
710     /* Do first row */
711
712 #define COLORFILL_ROW(type) \
713     { \
714     type *d = (type *) buf; \
715     for (x = 0; x < width; x++) \
716     d[x] = (type) color; \
717     break; \
718 }
719     switch(bpp)
720     {
721         case 1: COLORFILL_ROW(BYTE)
722                 case 2: COLORFILL_ROW(WORD)
723         case 3:
724         {
725             BYTE *d = buf;
726             for (x = 0; x < width; x++,d+=3)
727             {
728                 d[0] = (color    ) & 0xFF;
729                 d[1] = (color>> 8) & 0xFF;
730                 d[2] = (color>>16) & 0xFF;
731             }
732             break;
733         }
734         case 4: COLORFILL_ROW(DWORD)
735         default:
736             FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
737             return WINED3DERR_NOTAVAILABLE;
738     }
739
740 #undef COLORFILL_ROW
741
742     /* Now copy first row */
743     first = buf;
744     for (y = 1; y < height; y++)
745     {
746         buf += lPitch;
747         memcpy(buf, first, width * bpp);
748     }
749     return WINED3D_OK;
750 }
751
752 /*****************************************************************************
753  * IWineD3DSurface::Blt, SW emulation version
754  *
755  * Performs blits to a surface, eigher from a source of source-less blts
756  * This is the main functionality of DirectDraw
757  *
758  * Params:
759  *  DestRect: Destination rectangle to write to
760  *  SrcSurface: Source surface, can be NULL
761  *  SrcRect: Source rectangle
762  *****************************************************************************/
763 HRESULT WINAPI
764 IWineD3DBaseSurfaceImpl_Blt(IWineD3DSurface *iface,
765                             RECT *DestRect,
766                             IWineD3DSurface *SrcSurface,
767                             RECT *SrcRect,
768                             DWORD Flags,
769                             WINEDDBLTFX *DDBltFx,
770                             WINED3DTEXTUREFILTERTYPE Filter)
771 {
772     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
773     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
774     RECT        xdst,xsrc;
775     HRESULT     ret = WINED3D_OK;
776     WINED3DLOCKED_RECT  dlock, slock;
777     WINED3DFORMAT       dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
778     int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
779     int x, y;
780     const StaticPixelFormatDesc *sEntry, *dEntry;
781     LPBYTE dbuf, sbuf;
782     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
783
784     if (TRACE_ON(d3d_surface))
785     {
786         if (DestRect) TRACE("\tdestrect :%dx%d-%dx%d\n",
787             DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
788         if (SrcRect) TRACE("\tsrcrect  :%dx%d-%dx%d\n",
789             SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
790 #if 0
791         TRACE("\tflags: ");
792                       DDRAW_dump_DDBLT(Flags);
793                       if (Flags & WINEDDBLT_DDFX)
794               {
795                       TRACE("\tblitfx: ");
796                       DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
797     }
798 #endif
799     }
800
801     if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
802     {
803         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
804         return WINEDDERR_SURFACEBUSY;
805     }
806
807     if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
808         /* Can happen when d3d9 apps do a StretchRect call which isn't handled in gl */
809         FIXME("Filters not supported in software blit\n");
810     }
811
812     if (Src == This)
813     {
814         IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
815         dfmt = This->resource.format;
816         slock = dlock;
817         sfmt = dfmt;
818         sEntry = getFormatDescEntry(sfmt, NULL, NULL);
819         dEntry = sEntry;
820     }
821     else
822     {
823         dfmt = This->resource.format;
824         dEntry = getFormatDescEntry(dfmt, NULL, NULL);
825         if (Src)
826         {
827             if(This->resource.format != Src->resource.format) {
828                 Src = surface_convert_format(Src, dfmt);
829                 if(!Src) {
830                     /* The conv function writes a FIXME */
831                     WARN("Cannot convert source surface format to dest format\n");
832                     goto release;
833                 }
834             }
835             IWineD3DSurface_LockRect((IWineD3DSurface *) Src, &slock, NULL, WINED3DLOCK_READONLY);
836             sfmt = Src->resource.format;
837         }
838         sEntry = getFormatDescEntry(sfmt, NULL, NULL);
839         IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
840     }
841
842     if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~WINEDDBLT_DDFX;
843
844     if (sEntry->isFourcc && dEntry->isFourcc)
845     {
846         memcpy(dlock.pBits, slock.pBits, This->resource.size);
847         goto release;
848     }
849
850     if (DestRect)
851     {
852         xdst = *DestRect;
853     }
854     else
855     {
856         xdst.top    = 0;
857         xdst.bottom = This->currentDesc.Height;
858         xdst.left   = 0;
859         xdst.right  = This->currentDesc.Width;
860     }
861
862     if (SrcRect)
863     {
864         xsrc = *SrcRect;
865     }
866     else
867     {
868         if (Src)
869         {
870             xsrc.top    = 0;
871             xsrc.bottom = Src->currentDesc.Height;
872             xsrc.left   = 0;
873             xsrc.right  = Src->currentDesc.Width;
874         }
875         else
876         {
877             memset(&xsrc,0,sizeof(xsrc));
878         }
879     }
880
881     /* First check for the validity of source / destination rectangles. This was
882      * verified using a test application + by MSDN.
883      */
884     if ((Src != NULL) &&
885          ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
886          (xsrc.top     > Src->currentDesc.Height) || (xsrc.top    < 0) ||
887          (xsrc.left    > Src->currentDesc.Width)  || (xsrc.left   < 0) ||
888          (xsrc.right   > Src->currentDesc.Width)  || (xsrc.right  < 0) ||
889          (xsrc.right   < xsrc.left)               || (xsrc.bottom < xsrc.top)))
890     {
891         WARN("Application gave us bad source rectangle for Blt.\n");
892         ret = WINEDDERR_INVALIDRECT;
893         goto release;
894     }
895     /* For the Destination rect, it can be out of bounds on the condition that a clipper
896      * is set for the given surface.
897      */
898     if ((/*This->clipper == NULL*/ TRUE) &&
899          ((xdst.bottom  > This->currentDesc.Height) || (xdst.bottom < 0) ||
900          (xdst.top      > This->currentDesc.Height) || (xdst.top    < 0) ||
901          (xdst.left     > This->currentDesc.Width)  || (xdst.left   < 0) ||
902          (xdst.right    > This->currentDesc.Width)  || (xdst.right  < 0) ||
903          (xdst.right    < xdst.left)                || (xdst.bottom < xdst.top)))
904     {
905         WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
906         ret = WINEDDERR_INVALIDRECT;
907         goto release;
908     }
909
910     /* Now handle negative values in the rectangles. Warning: only supported for now
911     in the 'simple' cases (ie not in any stretching / rotation cases).
912
913     First, the case where nothing is to be done.
914     */
915     if (((xdst.bottom <= 0) || (xdst.right <= 0)         ||
916           (xdst.top    >= (int) This->currentDesc.Height) ||
917           (xdst.left   >= (int) This->currentDesc.Width)) ||
918           ((Src != NULL) &&
919           ((xsrc.bottom <= 0) || (xsrc.right <= 0)     ||
920           (xsrc.top >= (int) Src->currentDesc.Height) ||
921           (xsrc.left >= (int) Src->currentDesc.Width))  ))
922     {
923         TRACE("Nothing to be done !\n");
924         goto release;
925     }
926
927     /* The easy case : the source-less blits.... */
928     if (Src == NULL)
929     {
930         RECT full_rect;
931         RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
932
933         full_rect.left   = 0;
934         full_rect.top    = 0;
935         full_rect.right  = This->currentDesc.Width;
936         full_rect.bottom = This->currentDesc.Height;
937         IntersectRect(&temp_rect, &full_rect, &xdst);
938         xdst = temp_rect;
939     }
940     else
941     {
942         /* Only handle clipping on the destination rectangle */
943         int clip_horiz = (xdst.left < 0) || (xdst.right  > (int) This->currentDesc.Width );
944         int clip_vert  = (xdst.top  < 0) || (xdst.bottom > (int) This->currentDesc.Height);
945         if (clip_vert || clip_horiz)
946         {
947             /* Now check if this is a special case or not... */
948             if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
949                    (((xdst.right  - xdst.left) != (xsrc.right  - xsrc.left)) && clip_horiz) ||
950                    (Flags & WINEDDBLT_DDFX))
951             {
952                 WARN("Out of screen rectangle in special case. Not handled right now.\n");
953                 goto release;
954             }
955
956             if (clip_horiz)
957             {
958                 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
959                 if (xdst.right > This->currentDesc.Width)
960                 {
961                     xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
962                     xdst.right = (int) This->currentDesc.Width;
963                 }
964             }
965             if (clip_vert)
966             {
967                 if (xdst.top < 0)
968                 {
969                     xsrc.top -= xdst.top;
970                     xdst.top = 0;
971                 }
972                 if (xdst.bottom > This->currentDesc.Height)
973                 {
974                     xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
975                     xdst.bottom = (int) This->currentDesc.Height;
976                 }
977             }
978             /* And check if after clipping something is still to be done... */
979             if ((xdst.bottom <= 0)   || (xdst.right <= 0)       ||
980                  (xdst.top   >= (int) This->currentDesc.Height)  ||
981                  (xdst.left  >= (int) This->currentDesc.Width)   ||
982                  (xsrc.bottom <= 0)   || (xsrc.right <= 0)       ||
983                  (xsrc.top >= (int) Src->currentDesc.Height)     ||
984                  (xsrc.left >= (int) Src->currentDesc.Width))
985             {
986                 TRACE("Nothing to be done after clipping !\n");
987                 goto release;
988             }
989         }
990     }
991
992     bpp = This->bytesPerPixel;
993     srcheight = xsrc.bottom - xsrc.top;
994     srcwidth = xsrc.right - xsrc.left;
995     dstheight = xdst.bottom - xdst.top;
996     dstwidth = xdst.right - xdst.left;
997     width = (xdst.right - xdst.left) * bpp;
998
999     assert(width <= dlock.Pitch);
1000
1001     dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
1002
1003     if (Flags & WINEDDBLT_WAIT)
1004     {
1005         Flags &= ~WINEDDBLT_WAIT;
1006     }
1007     if (Flags & WINEDDBLT_ASYNC)
1008     {
1009         static BOOL displayed = FALSE;
1010         if (!displayed)
1011             FIXME("Can't handle WINEDDBLT_ASYNC flag right now.\n");
1012         displayed = TRUE;
1013         Flags &= ~WINEDDBLT_ASYNC;
1014     }
1015     if (Flags & WINEDDBLT_DONOTWAIT)
1016     {
1017         /* WINEDDBLT_DONOTWAIT appeared in DX7 */
1018         static BOOL displayed = FALSE;
1019         if (!displayed)
1020             FIXME("Can't handle WINEDDBLT_DONOTWAIT flag right now.\n");
1021         displayed = TRUE;
1022         Flags &= ~WINEDDBLT_DONOTWAIT;
1023     }
1024
1025     /* First, all the 'source-less' blits */
1026     if (Flags & WINEDDBLT_COLORFILL)
1027     {
1028         ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
1029                              dlock.Pitch, DDBltFx->u5.dwFillColor);
1030         Flags &= ~WINEDDBLT_COLORFILL;
1031     }
1032
1033     if (Flags & WINEDDBLT_DEPTHFILL)
1034     {
1035         FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
1036     }
1037     if (Flags & WINEDDBLT_ROP)
1038     {
1039         /* Catch some degenerate cases here */
1040         switch(DDBltFx->dwROP)
1041         {
1042             case BLACKNESS:
1043                 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
1044                 break;
1045                 case 0xAA0029: /* No-op */
1046                     break;
1047             case WHITENESS:
1048                 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
1049                 break;
1050                 case SRCCOPY: /* well, we do that below ? */
1051                     break;
1052             default:
1053                 FIXME("Unsupported raster op: %08x  Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
1054                 goto error;
1055         }
1056         Flags &= ~WINEDDBLT_ROP;
1057     }
1058     if (Flags & WINEDDBLT_DDROPS)
1059     {
1060         FIXME("\tDdraw Raster Ops: %08x  Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
1061     }
1062     /* Now the 'with source' blits */
1063     if (Src)
1064     {
1065         LPBYTE sbase;
1066         int sx, xinc, sy, yinc;
1067
1068         if (!dstwidth || !dstheight) /* hmm... stupid program ? */
1069             goto release;
1070         sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
1071         xinc = (srcwidth << 16) / dstwidth;
1072         yinc = (srcheight << 16) / dstheight;
1073
1074         if (!Flags)
1075         {
1076             /* No effects, we can cheat here */
1077             if (dstwidth == srcwidth)
1078             {
1079                 if (dstheight == srcheight)
1080                 {
1081                     /* No stretching in either direction. This needs to be as
1082                     * fast as possible */
1083                     sbuf = sbase;
1084
1085                     /* check for overlapping surfaces */
1086                     if (Src != This || xdst.top < xsrc.top ||
1087                         xdst.right <= xsrc.left || xsrc.right <= xdst.left)
1088                     {
1089                         /* no overlap, or dst above src, so copy from top downwards */
1090                         for (y = 0; y < dstheight; y++)
1091                         {
1092                             memcpy(dbuf, sbuf, width);
1093                             sbuf += slock.Pitch;
1094                             dbuf += dlock.Pitch;
1095                         }
1096                     }
1097                     else if (xdst.top > xsrc.top)  /* copy from bottom upwards */
1098                     {
1099                         sbuf += (slock.Pitch*dstheight);
1100                         dbuf += (dlock.Pitch*dstheight);
1101                         for (y = 0; y < dstheight; y++)
1102                         {
1103                             sbuf -= slock.Pitch;
1104                             dbuf -= dlock.Pitch;
1105                             memcpy(dbuf, sbuf, width);
1106                         }
1107                     }
1108                     else /* src and dst overlapping on the same line, use memmove */
1109                     {
1110                         for (y = 0; y < dstheight; y++)
1111                         {
1112                             memmove(dbuf, sbuf, width);
1113                             sbuf += slock.Pitch;
1114                             dbuf += dlock.Pitch;
1115                         }
1116                     }
1117                 } else {
1118                     /* Stretching in Y direction only */
1119                     for (y = sy = 0; y < dstheight; y++, sy += yinc) {
1120                         sbuf = sbase + (sy >> 16) * slock.Pitch;
1121                         memcpy(dbuf, sbuf, width);
1122                         dbuf += dlock.Pitch;
1123                     }
1124                 }
1125             }
1126             else
1127             {
1128                 /* Stretching in X direction */
1129                 int last_sy = -1;
1130                 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1131                 {
1132                     sbuf = sbase + (sy >> 16) * slock.Pitch;
1133
1134                     if ((sy >> 16) == (last_sy >> 16))
1135                     {
1136                         /* this sourcerow is the same as last sourcerow -
1137                         * copy already stretched row
1138                         */
1139                         memcpy(dbuf, dbuf - dlock.Pitch, width);
1140                     }
1141                     else
1142                     {
1143 #define STRETCH_ROW(type) { \
1144                         type *s = (type *) sbuf, *d = (type *) dbuf; \
1145                         for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
1146                         d[x] = s[sx >> 16]; \
1147                         break; }
1148
1149                         switch(bpp)
1150                         {
1151                             case 1: STRETCH_ROW(BYTE)
1152                                     case 2: STRETCH_ROW(WORD)
1153                                             case 4: STRETCH_ROW(DWORD)
1154                             case 3:
1155                             {
1156                                 LPBYTE s,d = dbuf;
1157                                 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1158                                 {
1159                                     DWORD pixel;
1160
1161                                     s = sbuf+3*(sx>>16);
1162                                     pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1163                                     d[0] = (pixel    )&0xff;
1164                                     d[1] = (pixel>> 8)&0xff;
1165                                     d[2] = (pixel>>16)&0xff;
1166                                     d+=3;
1167                                 }
1168                                 break;
1169                             }
1170                             default:
1171                                 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
1172                                 ret = WINED3DERR_NOTAVAILABLE;
1173                                 goto error;
1174                         }
1175 #undef STRETCH_ROW
1176                     }
1177                     dbuf += dlock.Pitch;
1178                     last_sy = sy;
1179                 }
1180             }
1181         }
1182         else
1183         {
1184             LONG dstyinc = dlock.Pitch, dstxinc = bpp;
1185             DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
1186             DWORD destkeylow = 0x0, destkeyhigh = 0xFFFFFFFF, destkeymask = 0xFFFFFFFF;
1187             if (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE))
1188             {
1189                 /* The color keying flags are checked for correctness in ddraw */
1190                 if (Flags & WINEDDBLT_KEYSRC)
1191                 {
1192                     keylow  = Src->SrcBltCKey.dwColorSpaceLowValue;
1193                     keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1194                 }
1195                 else  if (Flags & WINEDDBLT_KEYSRCOVERRIDE)
1196                 {
1197                     keylow  = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
1198                     keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
1199                 }
1200
1201                 if (Flags & WINEDDBLT_KEYDEST)
1202                 {
1203                     /* Destination color keys are taken from the source surface ! */
1204                     destkeylow  = Src->DestBltCKey.dwColorSpaceLowValue;
1205                     destkeyhigh = Src->DestBltCKey.dwColorSpaceHighValue;
1206                 }
1207                 else if (Flags & WINEDDBLT_KEYDESTOVERRIDE)
1208                 {
1209                     destkeylow  = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
1210                     destkeyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
1211                 }
1212
1213                 if(bpp == 1)
1214                 {
1215                     keymask = 0xff;
1216                 }
1217                 else
1218                 {
1219                     keymask = sEntry->redMask   |
1220                             sEntry->greenMask |
1221                             sEntry->blueMask;
1222                 }
1223                 Flags &= ~(WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE);
1224             }
1225
1226             if (Flags & WINEDDBLT_DDFX)
1227             {
1228                 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
1229                 LONG tmpxy;
1230                 dTopLeft     = dbuf;
1231                 dTopRight    = dbuf+((dstwidth-1)*bpp);
1232                 dBottomLeft  = dTopLeft+((dstheight-1)*dlock.Pitch);
1233                 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
1234
1235                 if (DDBltFx->dwDDFX & WINEDDBLTFX_ARITHSTRETCHY)
1236                 {
1237                     /* I don't think we need to do anything about this flag */
1238                     WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
1239                 }
1240                 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORLEFTRIGHT)
1241                 {
1242                     tmp          = dTopRight;
1243                     dTopRight    = dTopLeft;
1244                     dTopLeft     = tmp;
1245                     tmp          = dBottomRight;
1246                     dBottomRight = dBottomLeft;
1247                     dBottomLeft  = tmp;
1248                     dstxinc = dstxinc *-1;
1249                 }
1250                 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORUPDOWN)
1251                 {
1252                     tmp          = dTopLeft;
1253                     dTopLeft     = dBottomLeft;
1254                     dBottomLeft  = tmp;
1255                     tmp          = dTopRight;
1256                     dTopRight    = dBottomRight;
1257                     dBottomRight = tmp;
1258                     dstyinc = dstyinc *-1;
1259                 }
1260                 if (DDBltFx->dwDDFX & WINEDDBLTFX_NOTEARING)
1261                 {
1262                     /* I don't think we need to do anything about this flag */
1263                     WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
1264                 }
1265                 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE180)
1266                 {
1267                     tmp          = dBottomRight;
1268                     dBottomRight = dTopLeft;
1269                     dTopLeft     = tmp;
1270                     tmp          = dBottomLeft;
1271                     dBottomLeft  = dTopRight;
1272                     dTopRight    = tmp;
1273                     dstxinc = dstxinc * -1;
1274                     dstyinc = dstyinc * -1;
1275                 }
1276                 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE270)
1277                 {
1278                     tmp          = dTopLeft;
1279                     dTopLeft     = dBottomLeft;
1280                     dBottomLeft  = dBottomRight;
1281                     dBottomRight = dTopRight;
1282                     dTopRight    = tmp;
1283                     tmpxy   = dstxinc;
1284                     dstxinc = dstyinc;
1285                     dstyinc = tmpxy;
1286                     dstxinc = dstxinc * -1;
1287                 }
1288                 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE90)
1289                 {
1290                     tmp          = dTopLeft;
1291                     dTopLeft     = dTopRight;
1292                     dTopRight    = dBottomRight;
1293                     dBottomRight = dBottomLeft;
1294                     dBottomLeft  = tmp;
1295                     tmpxy   = dstxinc;
1296                     dstxinc = dstyinc;
1297                     dstyinc = tmpxy;
1298                     dstyinc = dstyinc * -1;
1299                 }
1300                 if (DDBltFx->dwDDFX & WINEDDBLTFX_ZBUFFERBASEDEST)
1301                 {
1302                     /* I don't think we need to do anything about this flag */
1303                     WARN("Flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
1304                 }
1305                 dbuf = dTopLeft;
1306                 Flags &= ~(WINEDDBLT_DDFX);
1307             }
1308
1309 #define COPY_COLORKEY_FX(type) { \
1310             type *s, *d = (type *) dbuf, *dx, tmp; \
1311             for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1312             s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1313             dx = d; \
1314             for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1315             tmp = s[sx >> 16]; \
1316             if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1317             ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1318             dx[0] = tmp; \
1319         } \
1320             dx = (type*)(((LPBYTE)dx)+dstxinc); \
1321         } \
1322             d = (type*)(((LPBYTE)d)+dstyinc); \
1323         } \
1324             break; }
1325
1326             switch (bpp) {
1327                 case 1: COPY_COLORKEY_FX(BYTE)
1328                         case 2: COPY_COLORKEY_FX(WORD)
1329                                 case 4: COPY_COLORKEY_FX(DWORD)
1330                 case 3:
1331                 {
1332                     LPBYTE s,d = dbuf, dx;
1333                     for (y = sy = 0; y < dstheight; y++, sy += yinc)
1334                     {
1335                         sbuf = sbase + (sy >> 16) * slock.Pitch;
1336                         dx = d;
1337                         for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1338                         {
1339                             DWORD pixel, dpixel = 0;
1340                             s = sbuf+3*(sx>>16);
1341                             pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1342                             dpixel = dx[0]|(dx[1]<<8)|(dx[2]<<16);
1343                             if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) &&
1344                                   ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
1345                             {
1346                                 dx[0] = (pixel    )&0xff;
1347                                 dx[1] = (pixel>> 8)&0xff;
1348                                 dx[2] = (pixel>>16)&0xff;
1349                             }
1350                             dx+= dstxinc;
1351                         }
1352                         d += dstyinc;
1353                     }
1354                     break;
1355                 }
1356                 default:
1357                     FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1358                           (Flags & WINEDDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1359                     ret = WINED3DERR_NOTAVAILABLE;
1360                     goto error;
1361 #undef COPY_COLORKEY_FX
1362             }
1363         }
1364     }
1365
1366 error:
1367     if (Flags && FIXME_ON(d3d_surface))
1368     {
1369         FIXME("\tUnsupported flags: %08x\n", Flags);
1370     }
1371
1372 release:
1373     IWineD3DSurface_UnlockRect(iface);
1374     if (Src && Src != This) IWineD3DSurface_UnlockRect((IWineD3DSurface *) Src);
1375     /* Release the converted surface if any */
1376     if (Src && SrcSurface != (IWineD3DSurface *) Src) IWineD3DSurface_Release((IWineD3DSurface *) Src);
1377     return ret;
1378 }
1379
1380 /*****************************************************************************
1381  * IWineD3DSurface::BltFast, SW emulation version
1382  *
1383  * This is the software implementation of BltFast, as used by GDI surfaces
1384  * and as a fallback for OpenGL surfaces. This code is taken from the old
1385  * DirectDraw code, and was originally written by TransGaming.
1386  *
1387  * Params:
1388  *  dstx:
1389  *  dsty:
1390  *  Source: Source surface to copy from
1391  *  rsrc: Source rectangle
1392  *  trans: Some Flags
1393  *
1394  * Returns:
1395  *  WINED3D_OK on success
1396  *
1397  *****************************************************************************/
1398 HRESULT WINAPI
1399 IWineD3DBaseSurfaceImpl_BltFast(IWineD3DSurface *iface,
1400                                 DWORD dstx,
1401                                 DWORD dsty,
1402                                 IWineD3DSurface *Source,
1403                                 RECT *rsrc,
1404                                 DWORD trans)
1405 {
1406     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1407     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1408
1409     int                 bpp, w, h, x, y;
1410     WINED3DLOCKED_RECT  dlock,slock;
1411     HRESULT             ret = WINED3D_OK;
1412     RECT                rsrc2;
1413     RECT                lock_src, lock_dst, lock_union;
1414     BYTE                *sbuf, *dbuf;
1415     const StaticPixelFormatDesc *sEntry, *dEntry;
1416
1417     if (TRACE_ON(d3d_surface))
1418     {
1419         TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This,dstx,dsty,Src,rsrc,trans);
1420
1421         if (rsrc)
1422         {
1423             TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,
1424                   rsrc->right,rsrc->bottom);
1425         }
1426         else
1427         {
1428             TRACE(" srcrect: NULL\n");
1429         }
1430     }
1431
1432     if ((This->Flags & SFLAG_LOCKED) ||
1433          ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1434     {
1435         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1436         return WINEDDERR_SURFACEBUSY;
1437     }
1438
1439     if (!rsrc)
1440     {
1441         WARN("rsrc is NULL!\n");
1442         rsrc = &rsrc2;
1443         rsrc->left = 0;
1444         rsrc->top = 0;
1445         rsrc->right = Src->currentDesc.Width;
1446         rsrc->bottom = Src->currentDesc.Height;
1447     }
1448
1449     /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1450     if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1451          (rsrc->top    > Src->currentDesc.Height) || (rsrc->top    < 0) ||
1452          (rsrc->left   > Src->currentDesc.Width)  || (rsrc->left   < 0) ||
1453          (rsrc->right  > Src->currentDesc.Width)  || (rsrc->right  < 0) ||
1454          (rsrc->right  < rsrc->left)              || (rsrc->bottom < rsrc->top))
1455     {
1456         WARN("Application gave us bad source rectangle for BltFast.\n");
1457         return WINEDDERR_INVALIDRECT;
1458     }
1459
1460     h = rsrc->bottom - rsrc->top;
1461     if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1462     if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1463     if (h <= 0) return WINEDDERR_INVALIDRECT;
1464
1465     w = rsrc->right - rsrc->left;
1466     if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1467     if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1468     if (w <= 0) return WINEDDERR_INVALIDRECT;
1469
1470     /* Now compute the locking rectangle... */
1471     lock_src.left = rsrc->left;
1472     lock_src.top = rsrc->top;
1473     lock_src.right = lock_src.left + w;
1474     lock_src.bottom = lock_src.top + h;
1475
1476     lock_dst.left = dstx;
1477     lock_dst.top = dsty;
1478     lock_dst.right = dstx + w;
1479     lock_dst.bottom = dsty + h;
1480
1481     bpp = This->bytesPerPixel;
1482
1483     /* We need to lock the surfaces, or we won't get refreshes when done. */
1484     if (Src == This)
1485     {
1486         int pitch;
1487
1488         UnionRect(&lock_union, &lock_src, &lock_dst);
1489
1490         /* Lock the union of the two rectangles */
1491         ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1492         if(ret != WINED3D_OK) goto error;
1493
1494         pitch = dlock.Pitch;
1495         slock.Pitch = dlock.Pitch;
1496
1497         /* Since slock was originally copied from this surface's description, we can just reuse it */
1498         assert(This->resource.allocatedMemory != NULL);
1499         sbuf = This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1500         dbuf = This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1501         sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1502         dEntry = sEntry;
1503     }
1504     else
1505     {
1506         ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, WINED3DLOCK_READONLY);
1507         if(ret != WINED3D_OK) goto error;
1508         ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1509         if(ret != WINED3D_OK) goto error;
1510
1511         sbuf = slock.pBits;
1512         dbuf = dlock.pBits;
1513         TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1514
1515         sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1516         dEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
1517     }
1518
1519     /* Handle first the FOURCC surfaces... */
1520     if (sEntry->isFourcc && dEntry->isFourcc)
1521     {
1522         TRACE("Fourcc -> Fourcc copy\n");
1523         if (trans)
1524             FIXME("trans arg not supported when a FOURCC surface is involved\n");
1525         if (dstx || dsty)
1526             FIXME("offset for destination surface is not supported\n");
1527         if (Src->resource.format != This->resource.format)
1528         {
1529             FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1530             ret = WINED3DERR_WRONGTEXTUREFORMAT;
1531             goto error;
1532         }
1533         /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1534         memcpy(dbuf, sbuf, This->resource.size);
1535         goto error;
1536     }
1537     if (sEntry->isFourcc && !dEntry->isFourcc)
1538     {
1539         /* TODO: Use the libtxc_dxtn.so shared library to do
1540          * software decompression
1541          */
1542         ERR("DXTC decompression not supported by now\n");
1543         goto error;
1544     }
1545
1546     if (trans & (WINEDDBLTFAST_SRCCOLORKEY | WINEDDBLTFAST_DESTCOLORKEY))
1547     {
1548         DWORD keylow, keyhigh;
1549         TRACE("Color keyed copy\n");
1550         if (trans & WINEDDBLTFAST_SRCCOLORKEY)
1551         {
1552             keylow  = Src->SrcBltCKey.dwColorSpaceLowValue;
1553             keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1554         }
1555         else
1556         {
1557             /* I'm not sure if this is correct */
1558             FIXME("WINEDDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1559             keylow  = This->DestBltCKey.dwColorSpaceLowValue;
1560             keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1561         }
1562
1563 #define COPYBOX_COLORKEY(type) { \
1564         type *d, *s, tmp; \
1565         s = (type *) sbuf; \
1566         d = (type *) dbuf; \
1567         for (y = 0; y < h; y++) { \
1568         for (x = 0; x < w; x++) { \
1569         tmp = s[x]; \
1570         if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1571     } \
1572         s = (type *)((BYTE *)s + slock.Pitch); \
1573         d = (type *)((BYTE *)d + dlock.Pitch); \
1574     } \
1575         break; \
1576     }
1577
1578         switch (bpp) {
1579             case 1: COPYBOX_COLORKEY(BYTE)
1580                     case 2: COPYBOX_COLORKEY(WORD)
1581                             case 4: COPYBOX_COLORKEY(DWORD)
1582             case 3:
1583             {
1584                 BYTE *d, *s;
1585                 DWORD tmp;
1586                 s = sbuf;
1587                 d = dbuf;
1588                 for (y = 0; y < h; y++)
1589                 {
1590                     for (x = 0; x < w * 3; x += 3)
1591                     {
1592                         tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1593                         if (tmp < keylow || tmp > keyhigh)
1594                         {
1595                             d[x + 0] = s[x + 0];
1596                             d[x + 1] = s[x + 1];
1597                             d[x + 2] = s[x + 2];
1598                         }
1599                     }
1600                     s += slock.Pitch;
1601                     d += dlock.Pitch;
1602                 }
1603                 break;
1604             }
1605             default:
1606                 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1607                 ret = WINED3DERR_NOTAVAILABLE;
1608                 goto error;
1609         }
1610 #undef COPYBOX_COLORKEY
1611         TRACE("Copy Done\n");
1612     }
1613     else
1614     {
1615         int width = w * bpp;
1616         INT sbufpitch, dbufpitch;
1617
1618         TRACE("NO color key copy\n");
1619         /* Handle overlapping surfaces */
1620         if (sbuf < dbuf)
1621         {
1622             sbuf += (h - 1) * slock.Pitch;
1623             dbuf += (h - 1) * dlock.Pitch;
1624             sbufpitch = -slock.Pitch;
1625             dbufpitch = -dlock.Pitch;
1626         }
1627         else
1628         {
1629             sbufpitch = slock.Pitch;
1630             dbufpitch = dlock.Pitch;
1631         }
1632         for (y = 0; y < h; y++)
1633         {
1634             /* This is pretty easy, a line for line memcpy */
1635             memmove(dbuf, sbuf, width);
1636             sbuf += sbufpitch;
1637             dbuf += dbufpitch;
1638         }
1639         TRACE("Copy done\n");
1640     }
1641
1642 error:
1643     if (Src == This)
1644     {
1645         IWineD3DSurface_UnlockRect(iface);
1646     }
1647     else
1648     {
1649         IWineD3DSurface_UnlockRect(iface);
1650         IWineD3DSurface_UnlockRect(Source);
1651     }
1652
1653     return ret;
1654 }
1655
1656 HRESULT WINAPI IWineD3DBaseSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags)
1657 {
1658     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1659
1660     TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
1661           This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1662
1663     pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
1664
1665     if (NULL == pRect)
1666     {
1667         pLockedRect->pBits = This->resource.allocatedMemory;
1668         This->lockedRect.left   = 0;
1669         This->lockedRect.top    = 0;
1670         This->lockedRect.right  = This->currentDesc.Width;
1671         This->lockedRect.bottom = This->currentDesc.Height;
1672
1673         TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
1674               &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
1675               This->lockedRect.right, This->lockedRect.bottom);
1676     }
1677     else
1678     {
1679         TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
1680               pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
1681
1682         /* DXTn textures are based on compressed blocks of 4x4 pixels, each
1683          * 16 bytes large (8 bytes in case of DXT1). Because of that Pitch has
1684          * slightly different meaning compared to regular textures. For DXTn
1685          * textures Pitch is the size of a row of blocks, 4 high and "width"
1686          * long. The x offset is calculated differently as well, since moving 4
1687          * pixels to the right actually moves an entire 4x4 block to right, ie
1688          * 16 bytes (8 in case of DXT1). */
1689         if (This->resource.format == WINED3DFMT_DXT1)
1690         {
1691             pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 2);
1692         }
1693         else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
1694                     This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5)
1695         {
1696             pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 4);
1697         }
1698         else
1699         {
1700             pLockedRect->pBits = This->resource.allocatedMemory +
1701                     (pLockedRect->Pitch * pRect->top) +
1702                     (pRect->left * This->bytesPerPixel);
1703         }
1704         This->lockedRect.left   = pRect->left;
1705         This->lockedRect.top    = pRect->top;
1706         This->lockedRect.right  = pRect->right;
1707         This->lockedRect.bottom = pRect->bottom;
1708     }
1709
1710     /* No dirtifying is needed for this surface implementation */
1711     TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
1712
1713     return WINED3D_OK;
1714 }
1715
1716 void WINAPI IWineD3DBaseSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
1717     ERR("Should not be called on base texture\n");
1718     return;
1719 }