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