push 2e171485cce958836fd6e3be0ba96ec62d78b416
[wine/hacks.git] / dlls / wined3d / surface_base.c
blob9b7f100436da8785403448b6d19b9a8566eab63b
1 /*
2 * IWineD3DSurface Implementation of management(non-rendering) functions
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
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.
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.
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
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 #include <assert.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
37 /* Do NOT define GLINFO_LOCATION in this file. THIS CODE MUST NOT USE IT */
39 /* *******************************************
40 IWineD3DSurface IUnknown parts follow
41 ******************************************* */
42 HRESULT WINAPI IWineD3DBaseSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
44 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
45 /* Warn ,but be nice about things */
46 TRACE("(%p)->(%s,%p)\n", This,debugstr_guid(riid),ppobj);
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;
56 *ppobj = NULL;
57 return E_NOINTERFACE;
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;
67 /* ****************************************************
68 IWineD3DSurface IWineD3DResource parts follow
69 **************************************************** */
70 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
71 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
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);
78 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
79 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
82 HRESULT WINAPI IWineD3DBaseSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
83 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
86 DWORD WINAPI IWineD3DBaseSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
87 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
90 DWORD WINAPI IWineD3DBaseSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
91 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
94 WINED3DRESOURCETYPE WINAPI IWineD3DBaseSurfaceImpl_GetType(IWineD3DSurface *iface) {
95 TRACE("(%p) : calling resourceimpl_GetType\n", iface);
96 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
99 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
100 TRACE("(%p) : calling resourceimpl_GetParent\n", iface);
101 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
104 /* ******************************************************
105 IWineD3DSurface IWineD3DSurface parts follow
106 ****************************************************** */
108 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer) {
109 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
110 IWineD3DBase *container = 0;
112 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
114 if (!ppContainer) {
115 ERR("Called without a valid ppContainer.\n");
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.
123 if (This->container) {
124 container = This->container;
125 } else {
126 container = (IWineD3DBase *)This->resource.wineD3DDevice;
129 TRACE("Relaying to QueryInterface\n");
130 return IUnknown_QueryInterface(container, riid, ppContainer);
133 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
134 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
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;
149 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags) {
150 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
151 TRACE("(%p)->(%x)\n", This, Flags);
153 switch (Flags)
155 case WINEDDGBS_CANBLT:
156 case WINEDDGBS_ISBLTDONE:
157 return WINED3D_OK;
159 default:
160 return WINED3DERR_INVALIDCALL;
164 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags) {
165 /* XXX: DDERR_INVALIDSURFACETYPE */
167 TRACE("(%p)->(%08x)\n",iface,Flags);
168 switch (Flags) {
169 case WINEDDGFS_CANFLIP:
170 case WINEDDGFS_ISFLIPDONE:
171 return WINED3D_OK;
173 default:
174 return WINED3DERR_INVALIDCALL;
178 HRESULT WINAPI IWineD3DBaseSurfaceImpl_IsLost(IWineD3DSurface *iface) {
179 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
180 TRACE("(%p)\n", This);
182 /* D3D8 and 9 loose full devices, ddraw only surfaces */
183 return This->Flags & SFLAG_LOST ? WINED3DERR_DEVICELOST : WINED3D_OK;
186 HRESULT WINAPI IWineD3DBaseSurfaceImpl_Restore(IWineD3DSurface *iface) {
187 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
188 TRACE("(%p)\n", This);
190 /* So far we don't lose anything :) */
191 This->Flags &= ~SFLAG_LOST;
192 return WINED3D_OK;
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);
200 if(This->palette == PalImpl) {
201 TRACE("Nop palette change\n");
202 return WINED3D_OK;
205 if(This->palette != NULL)
206 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
207 This->palette->Flags &= ~WINEDDPCAPS_PRIMARYSURFACE;
209 This->palette = PalImpl;
211 if(PalImpl != NULL) {
212 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
213 (PalImpl)->Flags |= WINEDDPCAPS_PRIMARYSURFACE;
216 return IWineD3DSurface_RealizePalette(iface);
218 else return WINED3D_OK;
221 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, WINEDDCOLORKEY *CKey) {
222 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
223 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
225 if ((Flags & WINEDDCKEY_COLORSPACE) != 0) {
226 FIXME(" colorkey value not supported (%08x) !\n", Flags);
227 return WINED3DERR_INVALIDCALL;
230 /* Dirtify the surface, but only if a key was changed */
231 if(CKey) {
232 switch (Flags & ~WINEDDCKEY_COLORSPACE) {
233 case WINEDDCKEY_DESTBLT:
234 This->DestBltCKey = *CKey;
235 This->CKeyFlags |= WINEDDSD_CKDESTBLT;
236 break;
238 case WINEDDCKEY_DESTOVERLAY:
239 This->DestOverlayCKey = *CKey;
240 This->CKeyFlags |= WINEDDSD_CKDESTOVERLAY;
241 break;
243 case WINEDDCKEY_SRCOVERLAY:
244 This->SrcOverlayCKey = *CKey;
245 This->CKeyFlags |= WINEDDSD_CKSRCOVERLAY;
246 break;
248 case WINEDDCKEY_SRCBLT:
249 This->SrcBltCKey = *CKey;
250 This->CKeyFlags |= WINEDDSD_CKSRCBLT;
251 break;
254 else {
255 switch (Flags & ~WINEDDCKEY_COLORSPACE) {
256 case WINEDDCKEY_DESTBLT:
257 This->CKeyFlags &= ~WINEDDSD_CKDESTBLT;
258 break;
260 case WINEDDCKEY_DESTOVERLAY:
261 This->CKeyFlags &= ~WINEDDSD_CKDESTOVERLAY;
262 break;
264 case WINEDDCKEY_SRCOVERLAY:
265 This->CKeyFlags &= ~WINEDDSD_CKSRCOVERLAY;
266 break;
268 case WINEDDCKEY_SRCBLT:
269 This->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
270 break;
274 return WINED3D_OK;
277 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal) {
278 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
279 TRACE("(%p)->(%p)\n", This, Pal);
281 *Pal = (IWineD3DPalette *) This->palette;
282 return WINED3D_OK;
285 DWORD WINAPI IWineD3DBaseSurfaceImpl_GetPitch(IWineD3DSurface *iface) {
286 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
287 DWORD ret;
288 TRACE("(%p)\n", This);
290 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
291 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt2/3/4/5)
292 ie pitch = (width/4) * bytes per block */
293 if (This->resource.format == WINED3DFMT_DXT1) /* DXT1 is 8 bytes per block */
294 ret = ((This->currentDesc.Width + 3) >> 2) << 3;
295 else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
296 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) /* DXT2/3/4/5 is 16 bytes per block */
297 ret = ((This->currentDesc.Width + 3) >> 2) << 4;
298 else {
299 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
300 ret = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
301 ret = (ret + alignment - 1) & ~(alignment - 1);
303 TRACE("(%p) Returning %d\n", This, ret);
304 return ret;
307 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y) {
308 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
310 FIXME("(%p)->(%d,%d) Stub!\n", This, X, Y);
312 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
314 TRACE("(%p): Not an overlay surface\n", This);
315 return WINEDDERR_NOTAOVERLAYSURFACE;
318 return WINED3D_OK;
321 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y) {
322 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
324 FIXME("(%p)->(%p,%p) Stub!\n", This, X, Y);
326 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
328 TRACE("(%p): Not an overlay surface\n", This);
329 return WINEDDERR_NOTAOVERLAYSURFACE;
332 return WINED3D_OK;
335 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref) {
336 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
337 IWineD3DSurfaceImpl *RefImpl = (IWineD3DSurfaceImpl *) Ref;
339 FIXME("(%p)->(%08x,%p) Stub!\n", This, Flags, RefImpl);
341 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
343 TRACE("(%p): Not an overlay surface\n", This);
344 return WINEDDERR_NOTAOVERLAYSURFACE;
347 return WINED3D_OK;
350 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, RECT *SrcRect, IWineD3DSurface *DstSurface, RECT *DstRect, DWORD Flags, WINEDDOVERLAYFX *FX) {
351 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
352 IWineD3DSurfaceImpl *Dst = (IWineD3DSurfaceImpl *) DstSurface;
353 FIXME("(%p)->(%p, %p, %p, %08x, %p)\n", This, SrcRect, Dst, DstRect, Flags, FX);
355 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
357 TRACE("(%p): Not an overlay surface\n", This);
358 return WINEDDERR_NOTAOVERLAYSURFACE;
361 return WINED3D_OK;
364 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetClipper(IWineD3DSurface *iface, IWineD3DClipper *clipper)
366 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
367 TRACE("(%p)->(%p)\n", This, clipper);
369 This->clipper = clipper;
370 return WINED3D_OK;
373 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetClipper(IWineD3DSurface *iface, IWineD3DClipper **clipper)
375 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
376 TRACE("(%p)->(%p)\n", This, clipper);
378 *clipper = This->clipper;
379 if(*clipper) {
380 IWineD3DClipper_AddRef(*clipper);
382 return WINED3D_OK;
385 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
386 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
388 TRACE("This %p, container %p\n", This, container);
390 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
392 TRACE("Setting container to %p from %p\n", container, This->container);
393 This->container = container;
395 return WINED3D_OK;
398 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
399 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
400 const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(format, NULL, NULL);
402 if (This->resource.format != WINED3DFMT_UNKNOWN) {
403 FIXME("(%p) : The format of the surface must be WINED3DFORMAT_UNKNOWN\n", This);
404 return WINED3DERR_INVALIDCALL;
407 TRACE("(%p) : Setting texture format to (%d,%s)\n", This, format, debug_d3dformat(format));
408 if (format == WINED3DFMT_UNKNOWN) {
409 This->resource.size = 0;
410 } else if (format == WINED3DFMT_DXT1) {
411 /* DXT1 is half byte per pixel */
412 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4)) >> 1;
414 } else if (format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3 ||
415 format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5) {
416 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4));
417 } else {
418 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
419 This->resource.size = ((This->pow2Width * formatEntry->bpp) + alignment - 1) & ~(alignment - 1);
420 This->resource.size *= This->pow2Height;
423 if (format != WINED3DFMT_UNKNOWN) {
424 This->bytesPerPixel = formatEntry->bpp;
425 } else {
426 This->bytesPerPixel = 0;
429 This->Flags |= (WINED3DFMT_D16_LOCKABLE == format) ? SFLAG_LOCKABLE : 0;
431 This->resource.format = format;
433 TRACE("(%p) : Size %d, bytesPerPixel %d\n", This, This->resource.size, This->bytesPerPixel);
435 return WINED3D_OK;
438 HRESULT IWineD3DBaseSurfaceImpl_CreateDIBSection(IWineD3DSurface *iface) {
439 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
440 int extraline = 0;
441 SYSTEM_INFO sysInfo;
442 BITMAPINFO* b_info;
443 HDC ddc;
444 DWORD *masks;
445 const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
446 UINT usage;
448 switch (This->bytesPerPixel) {
449 case 2:
450 case 4:
451 /* Allocate extra space to store the RGB bit masks. */
452 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
453 break;
455 case 3:
456 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
457 break;
459 default:
460 /* Allocate extra space for a palette. */
461 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
462 sizeof(BITMAPINFOHEADER)
463 + sizeof(RGBQUAD)
464 * (1 << (This->bytesPerPixel * 8)));
465 break;
468 if (!b_info)
469 return E_OUTOFMEMORY;
471 /* Some apps access the surface in via DWORDs, and do not take the necessary care at the end of the
472 * surface. So we need at least extra 4 bytes at the end of the surface. Check against the page size,
473 * if the last page used for the surface has at least 4 spare bytes we're safe, otherwise
474 * add an extra line to the dib section
476 GetSystemInfo(&sysInfo);
477 if( ((This->resource.size + 3) % sysInfo.dwPageSize) < 4) {
478 extraline = 1;
479 TRACE("Adding an extra line to the dib section\n");
482 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
483 /* TODO: Is there a nicer way to force a specific alignment? (8 byte for ddraw) */
484 b_info->bmiHeader.biWidth = IWineD3DSurface_GetPitch(iface) / This->bytesPerPixel;
485 b_info->bmiHeader.biHeight = -This->currentDesc.Height -extraline;
486 b_info->bmiHeader.biSizeImage = ( This->currentDesc.Height + extraline) * IWineD3DSurface_GetPitch(iface);
487 b_info->bmiHeader.biPlanes = 1;
488 b_info->bmiHeader.biBitCount = This->bytesPerPixel * 8;
490 b_info->bmiHeader.biXPelsPerMeter = 0;
491 b_info->bmiHeader.biYPelsPerMeter = 0;
492 b_info->bmiHeader.biClrUsed = 0;
493 b_info->bmiHeader.biClrImportant = 0;
495 /* Get the bit masks */
496 masks = (DWORD *) &(b_info->bmiColors);
497 switch (This->resource.format) {
498 case WINED3DFMT_R8G8B8:
499 usage = DIB_RGB_COLORS;
500 b_info->bmiHeader.biCompression = BI_RGB;
501 break;
503 case WINED3DFMT_X1R5G5B5:
504 case WINED3DFMT_A1R5G5B5:
505 case WINED3DFMT_A4R4G4B4:
506 case WINED3DFMT_X4R4G4B4:
507 case WINED3DFMT_R3G3B2:
508 case WINED3DFMT_A8R3G3B2:
509 case WINED3DFMT_A2B10G10R10:
510 case WINED3DFMT_A8B8G8R8:
511 case WINED3DFMT_X8B8G8R8:
512 case WINED3DFMT_A2R10G10B10:
513 case WINED3DFMT_R5G6B5:
514 case WINED3DFMT_A16B16G16R16:
515 usage = 0;
516 b_info->bmiHeader.biCompression = BI_BITFIELDS;
517 masks[0] = formatEntry->redMask;
518 masks[1] = formatEntry->greenMask;
519 masks[2] = formatEntry->blueMask;
520 break;
522 default:
523 /* Don't know palette */
524 b_info->bmiHeader.biCompression = BI_RGB;
525 usage = 0;
526 break;
529 ddc = GetDC(0);
530 if (ddc == 0) {
531 HeapFree(GetProcessHeap(), 0, b_info);
532 return HRESULT_FROM_WIN32(GetLastError());
535 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);
536 This->dib.DIBsection = CreateDIBSection(ddc, b_info, usage, &This->dib.bitmap_data, 0 /* Handle */, 0 /* Offset */);
537 ReleaseDC(0, ddc);
539 if (!This->dib.DIBsection) {
540 ERR("CreateDIBSection failed!\n");
541 HeapFree(GetProcessHeap(), 0, b_info);
542 return HRESULT_FROM_WIN32(GetLastError());
545 TRACE("DIBSection at : %p\n", This->dib.bitmap_data);
546 /* copy the existing surface to the dib section */
547 if(This->resource.allocatedMemory) {
548 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, b_info->bmiHeader.biSizeImage);
549 } else {
550 /* This is to make LockRect read the gl Texture although memory is allocated */
551 This->Flags &= ~SFLAG_INSYSMEM;
553 This->dib.bitmap_size = b_info->bmiHeader.biSizeImage;
555 HeapFree(GetProcessHeap(), 0, b_info);
557 /* Now allocate a HDC */
558 This->hDC = CreateCompatibleDC(0);
559 This->dib.holdbitmap = SelectObject(This->hDC, This->dib.DIBsection);
560 TRACE("using wined3d palette %p\n", This->palette);
561 SelectPalette(This->hDC,
562 This->palette ? This->palette->hpal : 0,
563 FALSE);
565 This->Flags |= SFLAG_DIBSECTION;
567 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
568 This->resource.heapMemory = NULL;
570 return WINED3D_OK;
573 void convert_r32f_r16f(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h) {
574 unsigned int x, y;
575 float *src_f;
576 unsigned short *dst_s;
578 TRACE("Converting %dx%d pixels, pitches %d %d\n", w, h, pitch_in, pitch_out);
579 for(y = 0; y < h; y++) {
580 src_f = (float *) (src + y * pitch_in);
581 dst_s = (unsigned short *) (dst + y * pitch_out);
582 for(x = 0; x < w; x++) {
583 dst_s[x] = float_32_to_16(src_f + x);
588 struct d3dfmt_convertor_desc {
589 WINED3DFORMAT from, to;
590 void (*convert)(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h);
593 struct d3dfmt_convertor_desc convertors[] = {
594 {WINED3DFMT_R32F, WINED3DFMT_R16F, convert_r32f_r16f},
597 static inline struct d3dfmt_convertor_desc *find_convertor(WINED3DFORMAT from, WINED3DFORMAT to) {
598 unsigned int i;
599 for(i = 0; i < (sizeof(convertors) / sizeof(convertors[0])); i++) {
600 if(convertors[i].from == from && convertors[i].to == to) {
601 return &convertors[i];
604 return NULL;
607 /*****************************************************************************
608 * surface_convert_format
610 * Creates a duplicate of a surface in a different format. Is used by Blt to
611 * blit between surfaces with different formats
613 * Parameters
614 * source: Source surface
615 * fmt: Requested destination format
617 *****************************************************************************/
618 IWineD3DSurfaceImpl *surface_convert_format(IWineD3DSurfaceImpl *source, WINED3DFORMAT to_fmt) {
619 IWineD3DSurface *ret = NULL;
620 struct d3dfmt_convertor_desc *conv;
621 WINED3DLOCKED_RECT lock_src, lock_dst;
622 HRESULT hr;
624 conv = find_convertor(source->resource.format, to_fmt);
625 if(!conv) {
626 FIXME("Cannot find a conversion function from format %s to %s\n",
627 debug_d3dformat(source->resource.format), debug_d3dformat(to_fmt));
628 return NULL;
631 IWineD3DDevice_CreateSurface((IWineD3DDevice *) source->resource.wineD3DDevice,
632 source->currentDesc.Width,
633 source->currentDesc.Height,
634 to_fmt,
635 TRUE, /* lockable */
636 TRUE, /* discard */
637 0, /* level */
638 &ret,
639 WINED3DRTYPE_SURFACE,
640 0, /* usage */
641 WINED3DPOOL_SCRATCH,
642 WINED3DMULTISAMPLE_NONE, /* TODO: Multisampled conversion */
643 0, /* multisamplequality */
644 NULL, /* sharedhandle */
645 IWineD3DSurface_GetImplType((IWineD3DSurface *) source),
646 NULL); /* parent */
647 if(!ret) {
648 ERR("Failed to create a destination surface for conversion\n");
649 return NULL;
652 memset(&lock_src, 0, sizeof(lock_src));
653 memset(&lock_dst, 0, sizeof(lock_dst));
655 hr = IWineD3DSurface_LockRect((IWineD3DSurface *) source, &lock_src, NULL, WINED3DLOCK_READONLY);
656 if(FAILED(hr)) {
657 ERR("Failed to lock the source surface\n");
658 IWineD3DSurface_Release(ret);
659 return NULL;
661 hr = IWineD3DSurface_LockRect(ret, &lock_dst, NULL, WINED3DLOCK_READONLY);
662 if(FAILED(hr)) {
663 ERR("Failed to lock the dest surface\n");
664 IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
665 IWineD3DSurface_Release(ret);
666 return NULL;
669 conv->convert(lock_src.pBits, lock_dst.pBits, lock_src.Pitch, lock_dst.Pitch,
670 source->currentDesc.Width, source->currentDesc.Height);
672 IWineD3DSurface_UnlockRect(ret);
673 IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
675 return (IWineD3DSurfaceImpl *) ret;
678 /*****************************************************************************
679 * _Blt_ColorFill
681 * Helper function that fills a memory area with a specific color
683 * Params:
684 * buf: memory address to start filling at
685 * width, height: Dimensions of the area to fill
686 * bpp: Bit depth of the surface
687 * lPitch: pitch of the surface
688 * color: Color to fill with
690 *****************************************************************************/
691 static HRESULT
692 _Blt_ColorFill(BYTE *buf,
693 int width, int height,
694 int bpp, LONG lPitch,
695 DWORD color)
697 int x, y;
698 LPBYTE first;
700 /* Do first row */
702 #define COLORFILL_ROW(type) \
704 type *d = (type *) buf; \
705 for (x = 0; x < width; x++) \
706 d[x] = (type) color; \
707 break; \
709 switch(bpp)
711 case 1: COLORFILL_ROW(BYTE)
712 case 2: COLORFILL_ROW(WORD)
713 case 3:
715 BYTE *d = buf;
716 for (x = 0; x < width; x++,d+=3)
718 d[0] = (color ) & 0xFF;
719 d[1] = (color>> 8) & 0xFF;
720 d[2] = (color>>16) & 0xFF;
722 break;
724 case 4: COLORFILL_ROW(DWORD)
725 default:
726 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
727 return WINED3DERR_NOTAVAILABLE;
730 #undef COLORFILL_ROW
732 /* Now copy first row */
733 first = buf;
734 for (y = 1; y < height; y++)
736 buf += lPitch;
737 memcpy(buf, first, width * bpp);
739 return WINED3D_OK;
742 /*****************************************************************************
743 * IWineD3DSurface::Blt, SW emulation version
745 * Performs blits to a surface, eigher from a source of source-less blts
746 * This is the main functionality of DirectDraw
748 * Params:
749 * DestRect: Destination rectangle to write to
750 * SrcSurface: Source surface, can be NULL
751 * SrcRect: Source rectangle
752 *****************************************************************************/
753 HRESULT WINAPI
754 IWineD3DBaseSurfaceImpl_Blt(IWineD3DSurface *iface,
755 RECT *DestRect,
756 IWineD3DSurface *SrcSurface,
757 RECT *SrcRect,
758 DWORD Flags,
759 WINEDDBLTFX *DDBltFx,
760 WINED3DTEXTUREFILTERTYPE Filter)
762 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
763 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
764 RECT xdst,xsrc;
765 HRESULT ret = WINED3D_OK;
766 WINED3DLOCKED_RECT dlock, slock;
767 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
768 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
769 int x, y;
770 const StaticPixelFormatDesc *sEntry, *dEntry;
771 LPBYTE dbuf, sbuf;
772 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
774 if (TRACE_ON(d3d_surface))
776 if (DestRect) TRACE("\tdestrect :%dx%d-%dx%d\n",
777 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
778 if (SrcRect) TRACE("\tsrcrect :%dx%d-%dx%d\n",
779 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
780 #if 0
781 TRACE("\tflags: ");
782 DDRAW_dump_DDBLT(Flags);
783 if (Flags & WINEDDBLT_DDFX)
785 TRACE("\tblitfx: ");
786 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
788 #endif
791 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
793 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
794 return WINEDDERR_SURFACEBUSY;
797 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
798 /* Can happen when d3d9 apps do a StretchRect call which isn't handled in gl */
799 FIXME("Filters not supported in software blit\n");
802 if (Src == This)
804 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
805 dfmt = This->resource.format;
806 slock = dlock;
807 sfmt = dfmt;
808 sEntry = getFormatDescEntry(sfmt, NULL, NULL);
809 dEntry = sEntry;
811 else
813 dfmt = This->resource.format;
814 dEntry = getFormatDescEntry(dfmt, NULL, NULL);
815 if (Src)
817 if(This->resource.format != Src->resource.format) {
818 Src = surface_convert_format(Src, dfmt);
819 if(!Src) {
820 /* The conv function writes a FIXME */
821 WARN("Cannot convert source surface format to dest format\n");
822 goto release;
825 IWineD3DSurface_LockRect((IWineD3DSurface *) Src, &slock, NULL, WINED3DLOCK_READONLY);
826 sfmt = Src->resource.format;
828 sEntry = getFormatDescEntry(sfmt, NULL, NULL);
829 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
832 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~WINEDDBLT_DDFX;
834 if (sEntry->isFourcc && dEntry->isFourcc)
836 memcpy(dlock.pBits, slock.pBits, This->resource.size);
837 goto release;
840 if (DestRect)
842 xdst = *DestRect;
844 else
846 xdst.top = 0;
847 xdst.bottom = This->currentDesc.Height;
848 xdst.left = 0;
849 xdst.right = This->currentDesc.Width;
852 if (SrcRect)
854 xsrc = *SrcRect;
856 else
858 if (Src)
860 xsrc.top = 0;
861 xsrc.bottom = Src->currentDesc.Height;
862 xsrc.left = 0;
863 xsrc.right = Src->currentDesc.Width;
865 else
867 memset(&xsrc,0,sizeof(xsrc));
871 /* First check for the validity of source / destination rectangles. This was
872 * verified using a test application + by MSDN.
874 if ((Src != NULL) &&
875 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
876 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
877 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
878 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
879 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
881 WARN("Application gave us bad source rectangle for Blt.\n");
882 ret = WINEDDERR_INVALIDRECT;
883 goto release;
885 /* For the Destination rect, it can be out of bounds on the condition that a clipper
886 * is set for the given surface.
888 if ((/*This->clipper == NULL*/ TRUE) &&
889 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
890 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
891 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
892 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
893 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
895 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
896 ret = WINEDDERR_INVALIDRECT;
897 goto release;
900 /* Now handle negative values in the rectangles. Warning: only supported for now
901 in the 'simple' cases (ie not in any stretching / rotation cases).
903 First, the case where nothing is to be done.
905 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
906 (xdst.top >= (int) This->currentDesc.Height) ||
907 (xdst.left >= (int) This->currentDesc.Width)) ||
908 ((Src != NULL) &&
909 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
910 (xsrc.top >= (int) Src->currentDesc.Height) ||
911 (xsrc.left >= (int) Src->currentDesc.Width)) ))
913 TRACE("Nothing to be done !\n");
914 goto release;
917 /* The easy case : the source-less blits.... */
918 if (Src == NULL)
920 RECT full_rect;
921 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
923 full_rect.left = 0;
924 full_rect.top = 0;
925 full_rect.right = This->currentDesc.Width;
926 full_rect.bottom = This->currentDesc.Height;
927 IntersectRect(&temp_rect, &full_rect, &xdst);
928 xdst = temp_rect;
930 else
932 /* Only handle clipping on the destination rectangle */
933 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
934 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
935 if (clip_vert || clip_horiz)
937 /* Now check if this is a special case or not... */
938 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
939 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
940 (Flags & WINEDDBLT_DDFX))
942 WARN("Out of screen rectangle in special case. Not handled right now.\n");
943 goto release;
946 if (clip_horiz)
948 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
949 if (xdst.right > This->currentDesc.Width)
951 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
952 xdst.right = (int) This->currentDesc.Width;
955 if (clip_vert)
957 if (xdst.top < 0)
959 xsrc.top -= xdst.top;
960 xdst.top = 0;
962 if (xdst.bottom > This->currentDesc.Height)
964 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
965 xdst.bottom = (int) This->currentDesc.Height;
968 /* And check if after clipping something is still to be done... */
969 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
970 (xdst.top >= (int) This->currentDesc.Height) ||
971 (xdst.left >= (int) This->currentDesc.Width) ||
972 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
973 (xsrc.top >= (int) Src->currentDesc.Height) ||
974 (xsrc.left >= (int) Src->currentDesc.Width))
976 TRACE("Nothing to be done after clipping !\n");
977 goto release;
982 bpp = This->bytesPerPixel;
983 srcheight = xsrc.bottom - xsrc.top;
984 srcwidth = xsrc.right - xsrc.left;
985 dstheight = xdst.bottom - xdst.top;
986 dstwidth = xdst.right - xdst.left;
987 width = (xdst.right - xdst.left) * bpp;
989 assert(width <= dlock.Pitch);
991 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
993 if (Flags & WINEDDBLT_WAIT)
995 Flags &= ~WINEDDBLT_WAIT;
997 if (Flags & WINEDDBLT_ASYNC)
999 static BOOL displayed = FALSE;
1000 if (!displayed)
1001 FIXME("Can't handle WINEDDBLT_ASYNC flag right now.\n");
1002 displayed = TRUE;
1003 Flags &= ~WINEDDBLT_ASYNC;
1005 if (Flags & WINEDDBLT_DONOTWAIT)
1007 /* WINEDDBLT_DONOTWAIT appeared in DX7 */
1008 static BOOL displayed = FALSE;
1009 if (!displayed)
1010 FIXME("Can't handle WINEDDBLT_DONOTWAIT flag right now.\n");
1011 displayed = TRUE;
1012 Flags &= ~WINEDDBLT_DONOTWAIT;
1015 /* First, all the 'source-less' blits */
1016 if (Flags & WINEDDBLT_COLORFILL)
1018 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
1019 dlock.Pitch, DDBltFx->u5.dwFillColor);
1020 Flags &= ~WINEDDBLT_COLORFILL;
1023 if (Flags & WINEDDBLT_DEPTHFILL)
1025 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
1027 if (Flags & WINEDDBLT_ROP)
1029 /* Catch some degenerate cases here */
1030 switch(DDBltFx->dwROP)
1032 case BLACKNESS:
1033 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
1034 break;
1035 case 0xAA0029: /* No-op */
1036 break;
1037 case WHITENESS:
1038 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
1039 break;
1040 case SRCCOPY: /* well, we do that below ? */
1041 break;
1042 default:
1043 FIXME("Unsupported raster op: %08x Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
1044 goto error;
1046 Flags &= ~WINEDDBLT_ROP;
1048 if (Flags & WINEDDBLT_DDROPS)
1050 FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
1052 /* Now the 'with source' blits */
1053 if (Src)
1055 LPBYTE sbase;
1056 int sx, xinc, sy, yinc;
1058 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
1059 goto release;
1060 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
1061 xinc = (srcwidth << 16) / dstwidth;
1062 yinc = (srcheight << 16) / dstheight;
1064 if (!Flags)
1066 /* No effects, we can cheat here */
1067 if (dstwidth == srcwidth)
1069 if (dstheight == srcheight)
1071 /* No stretching in either direction. This needs to be as
1072 * fast as possible */
1073 sbuf = sbase;
1075 /* check for overlapping surfaces */
1076 if (Src != This || xdst.top < xsrc.top ||
1077 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
1079 /* no overlap, or dst above src, so copy from top downwards */
1080 for (y = 0; y < dstheight; y++)
1082 memcpy(dbuf, sbuf, width);
1083 sbuf += slock.Pitch;
1084 dbuf += dlock.Pitch;
1087 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
1089 sbuf += (slock.Pitch*dstheight);
1090 dbuf += (dlock.Pitch*dstheight);
1091 for (y = 0; y < dstheight; y++)
1093 sbuf -= slock.Pitch;
1094 dbuf -= dlock.Pitch;
1095 memcpy(dbuf, sbuf, width);
1098 else /* src and dst overlapping on the same line, use memmove */
1100 for (y = 0; y < dstheight; y++)
1102 memmove(dbuf, sbuf, width);
1103 sbuf += slock.Pitch;
1104 dbuf += dlock.Pitch;
1107 } else {
1108 /* Stretching in Y direction only */
1109 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
1110 sbuf = sbase + (sy >> 16) * slock.Pitch;
1111 memcpy(dbuf, sbuf, width);
1112 dbuf += dlock.Pitch;
1116 else
1118 /* Stretching in X direction */
1119 int last_sy = -1;
1120 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1122 sbuf = sbase + (sy >> 16) * slock.Pitch;
1124 if ((sy >> 16) == (last_sy >> 16))
1126 /* this sourcerow is the same as last sourcerow -
1127 * copy already stretched row
1129 memcpy(dbuf, dbuf - dlock.Pitch, width);
1131 else
1133 #define STRETCH_ROW(type) { \
1134 type *s = (type *) sbuf, *d = (type *) dbuf; \
1135 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
1136 d[x] = s[sx >> 16]; \
1137 break; }
1139 switch(bpp)
1141 case 1: STRETCH_ROW(BYTE)
1142 case 2: STRETCH_ROW(WORD)
1143 case 4: STRETCH_ROW(DWORD)
1144 case 3:
1146 LPBYTE s,d = dbuf;
1147 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1149 DWORD pixel;
1151 s = sbuf+3*(sx>>16);
1152 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1153 d[0] = (pixel )&0xff;
1154 d[1] = (pixel>> 8)&0xff;
1155 d[2] = (pixel>>16)&0xff;
1156 d+=3;
1158 break;
1160 default:
1161 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
1162 ret = WINED3DERR_NOTAVAILABLE;
1163 goto error;
1165 #undef STRETCH_ROW
1167 dbuf += dlock.Pitch;
1168 last_sy = sy;
1172 else
1174 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
1175 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
1176 DWORD destkeylow = 0x0, destkeyhigh = 0xFFFFFFFF, destkeymask = 0xFFFFFFFF;
1177 if (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE))
1179 /* The color keying flags are checked for correctness in ddraw */
1180 if (Flags & WINEDDBLT_KEYSRC)
1182 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1183 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1185 else if (Flags & WINEDDBLT_KEYSRCOVERRIDE)
1187 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
1188 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
1191 if (Flags & WINEDDBLT_KEYDEST)
1193 /* Destination color keys are taken from the source surface ! */
1194 destkeylow = Src->DestBltCKey.dwColorSpaceLowValue;
1195 destkeyhigh = Src->DestBltCKey.dwColorSpaceHighValue;
1197 else if (Flags & WINEDDBLT_KEYDESTOVERRIDE)
1199 destkeylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
1200 destkeyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
1203 if(bpp == 1)
1205 keymask = 0xff;
1207 else
1209 keymask = sEntry->redMask |
1210 sEntry->greenMask |
1211 sEntry->blueMask;
1213 Flags &= ~(WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE);
1216 if (Flags & WINEDDBLT_DDFX)
1218 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
1219 LONG tmpxy;
1220 dTopLeft = dbuf;
1221 dTopRight = dbuf+((dstwidth-1)*bpp);
1222 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
1223 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
1225 if (DDBltFx->dwDDFX & WINEDDBLTFX_ARITHSTRETCHY)
1227 /* I don't think we need to do anything about this flag */
1228 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
1230 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORLEFTRIGHT)
1232 tmp = dTopRight;
1233 dTopRight = dTopLeft;
1234 dTopLeft = tmp;
1235 tmp = dBottomRight;
1236 dBottomRight = dBottomLeft;
1237 dBottomLeft = tmp;
1238 dstxinc = dstxinc *-1;
1240 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORUPDOWN)
1242 tmp = dTopLeft;
1243 dTopLeft = dBottomLeft;
1244 dBottomLeft = tmp;
1245 tmp = dTopRight;
1246 dTopRight = dBottomRight;
1247 dBottomRight = tmp;
1248 dstyinc = dstyinc *-1;
1250 if (DDBltFx->dwDDFX & WINEDDBLTFX_NOTEARING)
1252 /* I don't think we need to do anything about this flag */
1253 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
1255 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE180)
1257 tmp = dBottomRight;
1258 dBottomRight = dTopLeft;
1259 dTopLeft = tmp;
1260 tmp = dBottomLeft;
1261 dBottomLeft = dTopRight;
1262 dTopRight = tmp;
1263 dstxinc = dstxinc * -1;
1264 dstyinc = dstyinc * -1;
1266 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE270)
1268 tmp = dTopLeft;
1269 dTopLeft = dBottomLeft;
1270 dBottomLeft = dBottomRight;
1271 dBottomRight = dTopRight;
1272 dTopRight = tmp;
1273 tmpxy = dstxinc;
1274 dstxinc = dstyinc;
1275 dstyinc = tmpxy;
1276 dstxinc = dstxinc * -1;
1278 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE90)
1280 tmp = dTopLeft;
1281 dTopLeft = dTopRight;
1282 dTopRight = dBottomRight;
1283 dBottomRight = dBottomLeft;
1284 dBottomLeft = tmp;
1285 tmpxy = dstxinc;
1286 dstxinc = dstyinc;
1287 dstyinc = tmpxy;
1288 dstyinc = dstyinc * -1;
1290 if (DDBltFx->dwDDFX & WINEDDBLTFX_ZBUFFERBASEDEST)
1292 /* I don't think we need to do anything about this flag */
1293 WARN("Flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
1295 dbuf = dTopLeft;
1296 Flags &= ~(WINEDDBLT_DDFX);
1299 #define COPY_COLORKEY_FX(type) { \
1300 type *s, *d = (type *) dbuf, *dx, tmp; \
1301 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1302 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1303 dx = d; \
1304 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1305 tmp = s[sx >> 16]; \
1306 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1307 ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1308 dx[0] = tmp; \
1310 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1312 d = (type*)(((LPBYTE)d)+dstyinc); \
1314 break; }
1316 switch (bpp) {
1317 case 1: COPY_COLORKEY_FX(BYTE)
1318 case 2: COPY_COLORKEY_FX(WORD)
1319 case 4: COPY_COLORKEY_FX(DWORD)
1320 case 3:
1322 LPBYTE s,d = dbuf, dx;
1323 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1325 sbuf = sbase + (sy >> 16) * slock.Pitch;
1326 dx = d;
1327 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1329 DWORD pixel, dpixel = 0;
1330 s = sbuf+3*(sx>>16);
1331 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1332 dpixel = dx[0]|(dx[1]<<8)|(dx[2]<<16);
1333 if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) &&
1334 ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
1336 dx[0] = (pixel )&0xff;
1337 dx[1] = (pixel>> 8)&0xff;
1338 dx[2] = (pixel>>16)&0xff;
1340 dx+= dstxinc;
1342 d += dstyinc;
1344 break;
1346 default:
1347 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1348 (Flags & WINEDDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1349 ret = WINED3DERR_NOTAVAILABLE;
1350 goto error;
1351 #undef COPY_COLORKEY_FX
1356 error:
1357 if (Flags && FIXME_ON(d3d_surface))
1359 FIXME("\tUnsupported flags: %08x\n", Flags);
1362 release:
1363 IWineD3DSurface_UnlockRect(iface);
1364 if (Src && Src != This) IWineD3DSurface_UnlockRect((IWineD3DSurface *) Src);
1365 /* Release the converted surface if any */
1366 if (Src && SrcSurface != (IWineD3DSurface *) Src) IWineD3DSurface_Release((IWineD3DSurface *) Src);
1367 return ret;
1370 /*****************************************************************************
1371 * IWineD3DSurface::BltFast, SW emulation version
1373 * This is the software implementation of BltFast, as used by GDI surfaces
1374 * and as a fallback for OpenGL surfaces. This code is taken from the old
1375 * DirectDraw code, and was originally written by TransGaming.
1377 * Params:
1378 * dstx:
1379 * dsty:
1380 * Source: Source surface to copy from
1381 * rsrc: Source rectangle
1382 * trans: Some Flags
1384 * Returns:
1385 * WINED3D_OK on success
1387 *****************************************************************************/
1388 HRESULT WINAPI
1389 IWineD3DBaseSurfaceImpl_BltFast(IWineD3DSurface *iface,
1390 DWORD dstx,
1391 DWORD dsty,
1392 IWineD3DSurface *Source,
1393 RECT *rsrc,
1394 DWORD trans)
1396 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1397 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1399 int bpp, w, h, x, y;
1400 WINED3DLOCKED_RECT dlock,slock;
1401 HRESULT ret = WINED3D_OK;
1402 RECT rsrc2;
1403 RECT lock_src, lock_dst, lock_union;
1404 BYTE *sbuf, *dbuf;
1405 const StaticPixelFormatDesc *sEntry, *dEntry;
1407 if (TRACE_ON(d3d_surface))
1409 TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This,dstx,dsty,Src,rsrc,trans);
1411 if (rsrc)
1413 TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,
1414 rsrc->right,rsrc->bottom);
1416 else
1418 TRACE(" srcrect: NULL\n");
1422 if ((This->Flags & SFLAG_LOCKED) ||
1423 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1425 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1426 return WINEDDERR_SURFACEBUSY;
1429 if (!rsrc)
1431 WARN("rsrc is NULL!\n");
1432 rsrc = &rsrc2;
1433 rsrc->left = 0;
1434 rsrc->top = 0;
1435 rsrc->right = Src->currentDesc.Width;
1436 rsrc->bottom = Src->currentDesc.Height;
1439 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1440 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1441 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1442 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1443 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1444 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1446 WARN("Application gave us bad source rectangle for BltFast.\n");
1447 return WINEDDERR_INVALIDRECT;
1450 h = rsrc->bottom - rsrc->top;
1451 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1452 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1453 if (h <= 0) return WINEDDERR_INVALIDRECT;
1455 w = rsrc->right - rsrc->left;
1456 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1457 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1458 if (w <= 0) return WINEDDERR_INVALIDRECT;
1460 /* Now compute the locking rectangle... */
1461 lock_src.left = rsrc->left;
1462 lock_src.top = rsrc->top;
1463 lock_src.right = lock_src.left + w;
1464 lock_src.bottom = lock_src.top + h;
1466 lock_dst.left = dstx;
1467 lock_dst.top = dsty;
1468 lock_dst.right = dstx + w;
1469 lock_dst.bottom = dsty + h;
1471 bpp = This->bytesPerPixel;
1473 /* We need to lock the surfaces, or we won't get refreshes when done. */
1474 if (Src == This)
1476 int pitch;
1478 UnionRect(&lock_union, &lock_src, &lock_dst);
1480 /* Lock the union of the two rectangles */
1481 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1482 if(ret != WINED3D_OK) goto error;
1484 pitch = dlock.Pitch;
1485 slock.Pitch = dlock.Pitch;
1487 /* Since slock was originally copied from this surface's description, we can just reuse it */
1488 assert(This->resource.allocatedMemory != NULL);
1489 sbuf = This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1490 dbuf = This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1491 sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1492 dEntry = sEntry;
1494 else
1496 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, WINED3DLOCK_READONLY);
1497 if(ret != WINED3D_OK) goto error;
1498 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1499 if(ret != WINED3D_OK) goto error;
1501 sbuf = slock.pBits;
1502 dbuf = dlock.pBits;
1503 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1505 sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1506 dEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
1509 /* Handle first the FOURCC surfaces... */
1510 if (sEntry->isFourcc && dEntry->isFourcc)
1512 TRACE("Fourcc -> Fourcc copy\n");
1513 if (trans)
1514 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1515 if (dstx || dsty)
1516 FIXME("offset for destination surface is not supported\n");
1517 if (Src->resource.format != This->resource.format)
1519 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1520 ret = WINED3DERR_WRONGTEXTUREFORMAT;
1521 goto error;
1523 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1524 memcpy(dbuf, sbuf, This->resource.size);
1525 goto error;
1527 if (sEntry->isFourcc && !dEntry->isFourcc)
1529 /* TODO: Use the libtxc_dxtn.so shared library to do
1530 * software decompression
1532 ERR("DXTC decompression not supported by now\n");
1533 goto error;
1536 if (trans & (WINEDDBLTFAST_SRCCOLORKEY | WINEDDBLTFAST_DESTCOLORKEY))
1538 DWORD keylow, keyhigh;
1539 TRACE("Color keyed copy\n");
1540 if (trans & WINEDDBLTFAST_SRCCOLORKEY)
1542 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1543 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1545 else
1547 /* I'm not sure if this is correct */
1548 FIXME("WINEDDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1549 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1550 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1553 #define COPYBOX_COLORKEY(type) { \
1554 type *d, *s, tmp; \
1555 s = (type *) sbuf; \
1556 d = (type *) dbuf; \
1557 for (y = 0; y < h; y++) { \
1558 for (x = 0; x < w; x++) { \
1559 tmp = s[x]; \
1560 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1562 s = (type *)((BYTE *)s + slock.Pitch); \
1563 d = (type *)((BYTE *)d + dlock.Pitch); \
1565 break; \
1568 switch (bpp) {
1569 case 1: COPYBOX_COLORKEY(BYTE)
1570 case 2: COPYBOX_COLORKEY(WORD)
1571 case 4: COPYBOX_COLORKEY(DWORD)
1572 case 3:
1574 BYTE *d, *s;
1575 DWORD tmp;
1576 s = sbuf;
1577 d = dbuf;
1578 for (y = 0; y < h; y++)
1580 for (x = 0; x < w * 3; x += 3)
1582 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1583 if (tmp < keylow || tmp > keyhigh)
1585 d[x + 0] = s[x + 0];
1586 d[x + 1] = s[x + 1];
1587 d[x + 2] = s[x + 2];
1590 s += slock.Pitch;
1591 d += dlock.Pitch;
1593 break;
1595 default:
1596 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1597 ret = WINED3DERR_NOTAVAILABLE;
1598 goto error;
1600 #undef COPYBOX_COLORKEY
1601 TRACE("Copy Done\n");
1603 else
1605 int width = w * bpp;
1606 INT sbufpitch, dbufpitch;
1608 TRACE("NO color key copy\n");
1609 /* Handle overlapping surfaces */
1610 if (sbuf < dbuf)
1612 sbuf += (h - 1) * slock.Pitch;
1613 dbuf += (h - 1) * dlock.Pitch;
1614 sbufpitch = -slock.Pitch;
1615 dbufpitch = -dlock.Pitch;
1617 else
1619 sbufpitch = slock.Pitch;
1620 dbufpitch = dlock.Pitch;
1622 for (y = 0; y < h; y++)
1624 /* This is pretty easy, a line for line memcpy */
1625 memmove(dbuf, sbuf, width);
1626 sbuf += sbufpitch;
1627 dbuf += dbufpitch;
1629 TRACE("Copy done\n");
1632 error:
1633 if (Src == This)
1635 IWineD3DSurface_UnlockRect(iface);
1637 else
1639 IWineD3DSurface_UnlockRect(iface);
1640 IWineD3DSurface_UnlockRect(Source);
1643 return ret;
1646 HRESULT WINAPI IWineD3DBaseSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags)
1648 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1650 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
1651 This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1653 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
1655 if (NULL == pRect)
1657 pLockedRect->pBits = This->resource.allocatedMemory;
1658 This->lockedRect.left = 0;
1659 This->lockedRect.top = 0;
1660 This->lockedRect.right = This->currentDesc.Width;
1661 This->lockedRect.bottom = This->currentDesc.Height;
1663 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
1664 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
1665 This->lockedRect.right, This->lockedRect.bottom);
1667 else
1669 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
1670 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
1672 /* DXTn textures are based on compressed blocks of 4x4 pixels, each
1673 * 16 bytes large (8 bytes in case of DXT1). Because of that Pitch has
1674 * slightly different meaning compared to regular textures. For DXTn
1675 * textures Pitch is the size of a row of blocks, 4 high and "width"
1676 * long. The x offset is calculated differently as well, since moving 4
1677 * pixels to the right actually moves an entire 4x4 block to right, ie
1678 * 16 bytes (8 in case of DXT1). */
1679 if (This->resource.format == WINED3DFMT_DXT1)
1681 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 2);
1683 else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
1684 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5)
1686 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 4);
1688 else
1690 pLockedRect->pBits = This->resource.allocatedMemory +
1691 (pLockedRect->Pitch * pRect->top) +
1692 (pRect->left * This->bytesPerPixel);
1694 This->lockedRect.left = pRect->left;
1695 This->lockedRect.top = pRect->top;
1696 This->lockedRect.right = pRect->right;
1697 This->lockedRect.bottom = pRect->bottom;
1700 /* No dirtifying is needed for this surface implementation */
1701 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
1703 return WINED3D_OK;
1706 void WINAPI IWineD3DBaseSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
1707 ERR("Should not be called on base texture\n");
1708 return;