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-2007 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
30 #include "wine/port.h"
31 #include "wined3d_private.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);
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);
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);
115 ERR("Called without a valid ppContainer.\n");
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;
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;
149 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags) {
150 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
151 TRACE("(%p)->(%x)\n", This, Flags);
155 case WINEDDGBS_CANBLT:
156 case WINEDDGBS_ISBLTDONE:
160 return WINED3DERR_INVALIDCALL;
164 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags) {
165 /* XXX: DDERR_INVALIDSURFACETYPE */
167 TRACE("(%p)->(%08x)\n",iface,Flags);
169 case WINEDDGFS_CANFLIP:
170 case WINEDDGFS_ISFLIPDONE:
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;
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");
205 if(This->palette != NULL)
206 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
207 This->palette->Flags &= ~WINEDDPCAPS_PRIMARYSURFACE;
209 if(PalImpl != NULL) {
210 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
211 /* Set the device's main palette if the palette
212 * wasn't a primary palette before
214 if(!(PalImpl->Flags & WINEDDPCAPS_PRIMARYSURFACE)) {
215 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
218 for(i=0; i < 256; i++) {
219 device->palettes[device->currentPalette][i] = PalImpl->palents[i];
223 (PalImpl)->Flags |= WINEDDPCAPS_PRIMARYSURFACE;
226 This->palette = PalImpl;
228 return IWineD3DSurface_RealizePalette(iface);
231 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, WINEDDCOLORKEY *CKey) {
232 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
233 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
235 if ((Flags & WINEDDCKEY_COLORSPACE) != 0) {
236 FIXME(" colorkey value not supported (%08x) !\n", Flags);
237 return WINED3DERR_INVALIDCALL;
240 /* Dirtify the surface, but only if a key was changed */
242 switch (Flags & ~WINEDDCKEY_COLORSPACE) {
243 case WINEDDCKEY_DESTBLT:
244 This->DestBltCKey = *CKey;
245 This->CKeyFlags |= WINEDDSD_CKDESTBLT;
248 case WINEDDCKEY_DESTOVERLAY:
249 This->DestOverlayCKey = *CKey;
250 This->CKeyFlags |= WINEDDSD_CKDESTOVERLAY;
253 case WINEDDCKEY_SRCOVERLAY:
254 This->SrcOverlayCKey = *CKey;
255 This->CKeyFlags |= WINEDDSD_CKSRCOVERLAY;
258 case WINEDDCKEY_SRCBLT:
259 This->SrcBltCKey = *CKey;
260 This->CKeyFlags |= WINEDDSD_CKSRCBLT;
265 switch (Flags & ~WINEDDCKEY_COLORSPACE) {
266 case WINEDDCKEY_DESTBLT:
267 This->CKeyFlags &= ~WINEDDSD_CKDESTBLT;
270 case WINEDDCKEY_DESTOVERLAY:
271 This->CKeyFlags &= ~WINEDDSD_CKDESTOVERLAY;
274 case WINEDDCKEY_SRCOVERLAY:
275 This->CKeyFlags &= ~WINEDDSD_CKSRCOVERLAY;
278 case WINEDDCKEY_SRCBLT:
279 This->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
287 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal) {
288 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
289 TRACE("(%p)->(%p)\n", This, Pal);
291 *Pal = (IWineD3DPalette *) This->palette;
295 DWORD WINAPI IWineD3DBaseSurfaceImpl_GetPitch(IWineD3DSurface *iface) {
296 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
298 TRACE("(%p)\n", This);
300 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
301 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt2/3/4/5)
302 ie pitch = (width/4) * bytes per block */
303 if (This->resource.format == WINED3DFMT_DXT1) /* DXT1 is 8 bytes per block */
304 ret = ((This->currentDesc.Width + 3) >> 2) << 3;
305 else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
306 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) /* DXT2/3/4/5 is 16 bytes per block */
307 ret = ((This->currentDesc.Width + 3) >> 2) << 4;
309 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
310 ret = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
311 ret = (ret + alignment - 1) & ~(alignment - 1);
313 TRACE("(%p) Returning %d\n", This, ret);
317 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y) {
318 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
320 FIXME("(%p)->(%d,%d) Stub!\n", This, X, Y);
322 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
324 TRACE("(%p): Not an overlay surface\n", This);
325 return WINEDDERR_NOTAOVERLAYSURFACE;
331 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y) {
332 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
334 FIXME("(%p)->(%p,%p) Stub!\n", This, X, Y);
336 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
338 TRACE("(%p): Not an overlay surface\n", This);
339 return WINEDDERR_NOTAOVERLAYSURFACE;
345 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref) {
346 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
347 IWineD3DSurfaceImpl *RefImpl = (IWineD3DSurfaceImpl *) Ref;
349 FIXME("(%p)->(%08x,%p) Stub!\n", This, Flags, RefImpl);
351 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
353 TRACE("(%p): Not an overlay surface\n", This);
354 return WINEDDERR_NOTAOVERLAYSURFACE;
360 HRESULT WINAPI IWineD3DBaseSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, RECT *SrcRect, IWineD3DSurface *DstSurface, RECT *DstRect, DWORD Flags, WINEDDOVERLAYFX *FX) {
361 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
362 IWineD3DSurfaceImpl *Dst = (IWineD3DSurfaceImpl *) DstSurface;
363 FIXME("(%p)->(%p, %p, %p, %08x, %p)\n", This, SrcRect, Dst, DstRect, Flags, FX);
365 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
367 TRACE("(%p): Not an overlay surface\n", This);
368 return WINEDDERR_NOTAOVERLAYSURFACE;
374 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetClipper(IWineD3DSurface *iface, IWineD3DClipper *clipper)
376 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
377 TRACE("(%p)->(%p)\n", This, clipper);
379 This->clipper = clipper;
383 HRESULT WINAPI IWineD3DBaseSurfaceImpl_GetClipper(IWineD3DSurface *iface, IWineD3DClipper **clipper)
385 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
386 TRACE("(%p)->(%p)\n", This, clipper);
388 *clipper = This->clipper;
390 IWineD3DClipper_AddRef(*clipper);
395 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
396 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
398 TRACE("This %p, container %p\n", This, container);
400 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
402 TRACE("Setting container to %p from %p\n", container, This->container);
403 This->container = container;
408 HRESULT WINAPI IWineD3DBaseSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
409 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
410 const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(format, NULL, NULL);
412 if (This->resource.format != WINED3DFMT_UNKNOWN) {
413 FIXME("(%p) : The format of the surface must be WINED3DFORMAT_UNKNOWN\n", This);
414 return WINED3DERR_INVALIDCALL;
417 TRACE("(%p) : Setting texture format to (%d,%s)\n", This, format, debug_d3dformat(format));
418 if (format == WINED3DFMT_UNKNOWN) {
419 This->resource.size = 0;
420 } else if (format == WINED3DFMT_DXT1) {
421 /* DXT1 is half byte per pixel */
422 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4)) >> 1;
424 } else if (format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3 ||
425 format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5) {
426 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4));
428 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
429 This->resource.size = ((This->pow2Width * formatEntry->bpp) + alignment - 1) & ~(alignment - 1);
430 This->resource.size *= This->pow2Height;
433 if (format != WINED3DFMT_UNKNOWN) {
434 This->bytesPerPixel = formatEntry->bpp;
436 This->bytesPerPixel = 0;
439 This->Flags |= (WINED3DFMT_D16_LOCKABLE == format) ? SFLAG_LOCKABLE : 0;
441 This->resource.format = format;
443 TRACE("(%p) : Size %d, bytesPerPixel %d\n", This, This->resource.size, This->bytesPerPixel);
448 HRESULT IWineD3DBaseSurfaceImpl_CreateDIBSection(IWineD3DSurface *iface) {
449 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
455 const StaticPixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
458 switch (This->bytesPerPixel) {
461 /* Allocate extra space to store the RGB bit masks. */
462 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
466 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
470 /* Allocate extra space for a palette. */
471 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
472 sizeof(BITMAPINFOHEADER)
474 * (1 << (This->bytesPerPixel * 8)));
479 return E_OUTOFMEMORY;
481 /* Some apps access the surface in via DWORDs, and do not take the necessary care at the end of the
482 * surface. So we need at least extra 4 bytes at the end of the surface. Check against the page size,
483 * if the last page used for the surface has at least 4 spare bytes we're safe, otherwise
484 * add an extra line to the dib section
486 GetSystemInfo(&sysInfo);
487 if( ((This->resource.size + 3) % sysInfo.dwPageSize) < 4) {
489 TRACE("Adding an extra line to the dib section\n");
492 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
493 /* TODO: Is there a nicer way to force a specific alignment? (8 byte for ddraw) */
494 b_info->bmiHeader.biWidth = IWineD3DSurface_GetPitch(iface) / This->bytesPerPixel;
495 b_info->bmiHeader.biHeight = -This->currentDesc.Height -extraline;
496 b_info->bmiHeader.biSizeImage = ( This->currentDesc.Height + extraline) * IWineD3DSurface_GetPitch(iface);
497 b_info->bmiHeader.biPlanes = 1;
498 b_info->bmiHeader.biBitCount = This->bytesPerPixel * 8;
500 b_info->bmiHeader.biXPelsPerMeter = 0;
501 b_info->bmiHeader.biYPelsPerMeter = 0;
502 b_info->bmiHeader.biClrUsed = 0;
503 b_info->bmiHeader.biClrImportant = 0;
505 /* Get the bit masks */
506 masks = (DWORD *) &(b_info->bmiColors);
507 switch (This->resource.format) {
508 case WINED3DFMT_R8G8B8:
509 usage = DIB_RGB_COLORS;
510 b_info->bmiHeader.biCompression = BI_RGB;
513 case WINED3DFMT_X1R5G5B5:
514 case WINED3DFMT_A1R5G5B5:
515 case WINED3DFMT_A4R4G4B4:
516 case WINED3DFMT_X4R4G4B4:
517 case WINED3DFMT_R3G3B2:
518 case WINED3DFMT_A8R3G3B2:
519 case WINED3DFMT_A2B10G10R10:
520 case WINED3DFMT_A8B8G8R8:
521 case WINED3DFMT_X8B8G8R8:
522 case WINED3DFMT_A2R10G10B10:
523 case WINED3DFMT_R5G6B5:
524 case WINED3DFMT_A16B16G16R16:
526 b_info->bmiHeader.biCompression = BI_BITFIELDS;
527 masks[0] = formatEntry->redMask;
528 masks[1] = formatEntry->greenMask;
529 masks[2] = formatEntry->blueMask;
533 /* Don't know palette */
534 b_info->bmiHeader.biCompression = BI_RGB;
541 HeapFree(GetProcessHeap(), 0, b_info);
542 return HRESULT_FROM_WIN32(GetLastError());
545 TRACE("Creating a DIB section with size %dx%dx%d, size=%d\n", b_info->bmiHeader.biWidth, b_info->bmiHeader.biHeight, b_info->bmiHeader.biBitCount, b_info->bmiHeader.biSizeImage);
546 This->dib.DIBsection = CreateDIBSection(ddc, b_info, usage, &This->dib.bitmap_data, 0 /* Handle */, 0 /* Offset */);
549 if (!This->dib.DIBsection) {
550 ERR("CreateDIBSection failed!\n");
551 HeapFree(GetProcessHeap(), 0, b_info);
552 return HRESULT_FROM_WIN32(GetLastError());
555 TRACE("DIBSection at : %p\n", This->dib.bitmap_data);
556 /* copy the existing surface to the dib section */
557 if(This->resource.allocatedMemory) {
558 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, b_info->bmiHeader.biSizeImage);
560 /* This is to make LockRect read the gl Texture although memory is allocated */
561 This->Flags &= ~SFLAG_INSYSMEM;
563 This->dib.bitmap_size = b_info->bmiHeader.biSizeImage;
565 HeapFree(GetProcessHeap(), 0, b_info);
567 /* Now allocate a HDC */
568 This->hDC = CreateCompatibleDC(0);
569 This->dib.holdbitmap = SelectObject(This->hDC, This->dib.DIBsection);
570 TRACE("using wined3d palette %p\n", This->palette);
571 SelectPalette(This->hDC,
572 This->palette ? This->palette->hpal : 0,
575 This->Flags |= SFLAG_DIBSECTION;
577 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
578 This->resource.heapMemory = NULL;
583 void convert_r32f_r16f(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h) {
586 unsigned short *dst_s;
588 TRACE("Converting %dx%d pixels, pitches %d %d\n", w, h, pitch_in, pitch_out);
589 for(y = 0; y < h; y++) {
590 src_f = (float *) (src + y * pitch_in);
591 dst_s = (unsigned short *) (dst + y * pitch_out);
592 for(x = 0; x < w; x++) {
593 dst_s[x] = float_32_to_16(src_f + x);
598 struct d3dfmt_convertor_desc {
599 WINED3DFORMAT from, to;
600 void (*convert)(BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h);
603 struct d3dfmt_convertor_desc convertors[] = {
604 {WINED3DFMT_R32F, WINED3DFMT_R16F, convert_r32f_r16f},
607 static inline struct d3dfmt_convertor_desc *find_convertor(WINED3DFORMAT from, WINED3DFORMAT to) {
609 for(i = 0; i < (sizeof(convertors) / sizeof(convertors[0])); i++) {
610 if(convertors[i].from == from && convertors[i].to == to) {
611 return &convertors[i];
617 /*****************************************************************************
618 * surface_convert_format
620 * Creates a duplicate of a surface in a different format. Is used by Blt to
621 * blit between surfaces with different formats
624 * source: Source surface
625 * fmt: Requested destination format
627 *****************************************************************************/
628 IWineD3DSurfaceImpl *surface_convert_format(IWineD3DSurfaceImpl *source, WINED3DFORMAT to_fmt) {
629 IWineD3DSurface *ret = NULL;
630 struct d3dfmt_convertor_desc *conv;
631 WINED3DLOCKED_RECT lock_src, lock_dst;
634 conv = find_convertor(source->resource.format, to_fmt);
636 FIXME("Cannot find a conversion function from format %s to %s\n",
637 debug_d3dformat(source->resource.format), debug_d3dformat(to_fmt));
641 IWineD3DDevice_CreateSurface((IWineD3DDevice *) source->resource.wineD3DDevice,
642 source->currentDesc.Width,
643 source->currentDesc.Height,
649 WINED3DRTYPE_SURFACE,
652 WINED3DMULTISAMPLE_NONE, /* TODO: Multisampled conversion */
653 0, /* multisamplequality */
654 NULL, /* sharedhandle */
655 IWineD3DSurface_GetImplType((IWineD3DSurface *) source),
658 ERR("Failed to create a destination surface for conversion\n");
662 memset(&lock_src, 0, sizeof(lock_src));
663 memset(&lock_dst, 0, sizeof(lock_dst));
665 hr = IWineD3DSurface_LockRect((IWineD3DSurface *) source, &lock_src, NULL, WINED3DLOCK_READONLY);
667 ERR("Failed to lock the source surface\n");
668 IWineD3DSurface_Release(ret);
671 hr = IWineD3DSurface_LockRect(ret, &lock_dst, NULL, WINED3DLOCK_READONLY);
673 ERR("Failed to lock the dest surface\n");
674 IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
675 IWineD3DSurface_Release(ret);
679 conv->convert(lock_src.pBits, lock_dst.pBits, lock_src.Pitch, lock_dst.Pitch,
680 source->currentDesc.Width, source->currentDesc.Height);
682 IWineD3DSurface_UnlockRect(ret);
683 IWineD3DSurface_UnlockRect((IWineD3DSurface *) source);
685 return (IWineD3DSurfaceImpl *) ret;
688 /*****************************************************************************
691 * Helper function that fills a memory area with a specific color
694 * buf: memory address to start filling at
695 * width, height: Dimensions of the area to fill
696 * bpp: Bit depth of the surface
697 * lPitch: pitch of the surface
698 * color: Color to fill with
700 *****************************************************************************/
702 _Blt_ColorFill(BYTE *buf,
703 int width, int height,
704 int bpp, LONG lPitch,
712 #define COLORFILL_ROW(type) \
714 type *d = (type *) buf; \
715 for (x = 0; x < width; x++) \
716 d[x] = (type) color; \
721 case 1: COLORFILL_ROW(BYTE)
722 case 2: COLORFILL_ROW(WORD)
726 for (x = 0; x < width; x++,d+=3)
728 d[0] = (color ) & 0xFF;
729 d[1] = (color>> 8) & 0xFF;
730 d[2] = (color>>16) & 0xFF;
734 case 4: COLORFILL_ROW(DWORD)
736 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
737 return WINED3DERR_NOTAVAILABLE;
742 /* Now copy first row */
744 for (y = 1; y < height; y++)
747 memcpy(buf, first, width * bpp);
752 /*****************************************************************************
753 * IWineD3DSurface::Blt, SW emulation version
755 * Performs blits to a surface, eigher from a source of source-less blts
756 * This is the main functionality of DirectDraw
759 * DestRect: Destination rectangle to write to
760 * SrcSurface: Source surface, can be NULL
761 * SrcRect: Source rectangle
762 *****************************************************************************/
764 IWineD3DBaseSurfaceImpl_Blt(IWineD3DSurface *iface,
766 IWineD3DSurface *SrcSurface,
769 WINEDDBLTFX *DDBltFx,
770 WINED3DTEXTUREFILTERTYPE Filter)
772 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
773 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
775 HRESULT ret = WINED3D_OK;
776 WINED3DLOCKED_RECT dlock, slock;
777 WINED3DFORMAT dfmt = WINED3DFMT_UNKNOWN, sfmt = WINED3DFMT_UNKNOWN;
778 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
780 const StaticPixelFormatDesc *sEntry, *dEntry;
782 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
784 if (TRACE_ON(d3d_surface))
786 if (DestRect) TRACE("\tdestrect :%dx%d-%dx%d\n",
787 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
788 if (SrcRect) TRACE("\tsrcrect :%dx%d-%dx%d\n",
789 SrcRect->left, SrcRect->top, SrcRect->right, SrcRect->bottom);
792 DDRAW_dump_DDBLT(Flags);
793 if (Flags & WINEDDBLT_DDFX)
796 DDRAW_dump_DDBLTFX(DDBltFx->dwDDFX);
801 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
803 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
804 return WINEDDERR_SURFACEBUSY;
807 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
808 /* Can happen when d3d9 apps do a StretchRect call which isn't handled in gl */
809 FIXME("Filters not supported in software blit\n");
814 IWineD3DSurface_LockRect(iface, &dlock, NULL, 0);
815 dfmt = This->resource.format;
818 sEntry = getFormatDescEntry(sfmt, NULL, NULL);
823 dfmt = This->resource.format;
824 dEntry = getFormatDescEntry(dfmt, NULL, NULL);
827 if(This->resource.format != Src->resource.format) {
828 Src = surface_convert_format(Src, dfmt);
830 /* The conv function writes a FIXME */
831 WARN("Cannot convert source surface format to dest format\n");
835 IWineD3DSurface_LockRect((IWineD3DSurface *) Src, &slock, NULL, WINED3DLOCK_READONLY);
836 sfmt = Src->resource.format;
838 sEntry = getFormatDescEntry(sfmt, NULL, NULL);
839 IWineD3DSurface_LockRect(iface, &dlock,NULL,0);
842 if (!DDBltFx || !(DDBltFx->dwDDFX)) Flags &= ~WINEDDBLT_DDFX;
844 if (sEntry->isFourcc && dEntry->isFourcc)
846 memcpy(dlock.pBits, slock.pBits, This->resource.size);
857 xdst.bottom = This->currentDesc.Height;
859 xdst.right = This->currentDesc.Width;
871 xsrc.bottom = Src->currentDesc.Height;
873 xsrc.right = Src->currentDesc.Width;
877 memset(&xsrc,0,sizeof(xsrc));
881 /* First check for the validity of source / destination rectangles. This was
882 * verified using a test application + by MSDN.
885 ((xsrc.bottom > Src->currentDesc.Height) || (xsrc.bottom < 0) ||
886 (xsrc.top > Src->currentDesc.Height) || (xsrc.top < 0) ||
887 (xsrc.left > Src->currentDesc.Width) || (xsrc.left < 0) ||
888 (xsrc.right > Src->currentDesc.Width) || (xsrc.right < 0) ||
889 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top)))
891 WARN("Application gave us bad source rectangle for Blt.\n");
892 ret = WINEDDERR_INVALIDRECT;
895 /* For the Destination rect, it can be out of bounds on the condition that a clipper
896 * is set for the given surface.
898 if ((/*This->clipper == NULL*/ TRUE) &&
899 ((xdst.bottom > This->currentDesc.Height) || (xdst.bottom < 0) ||
900 (xdst.top > This->currentDesc.Height) || (xdst.top < 0) ||
901 (xdst.left > This->currentDesc.Width) || (xdst.left < 0) ||
902 (xdst.right > This->currentDesc.Width) || (xdst.right < 0) ||
903 (xdst.right < xdst.left) || (xdst.bottom < xdst.top)))
905 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
906 ret = WINEDDERR_INVALIDRECT;
910 /* Now handle negative values in the rectangles. Warning: only supported for now
911 in the 'simple' cases (ie not in any stretching / rotation cases).
913 First, the case where nothing is to be done.
915 if (((xdst.bottom <= 0) || (xdst.right <= 0) ||
916 (xdst.top >= (int) This->currentDesc.Height) ||
917 (xdst.left >= (int) This->currentDesc.Width)) ||
919 ((xsrc.bottom <= 0) || (xsrc.right <= 0) ||
920 (xsrc.top >= (int) Src->currentDesc.Height) ||
921 (xsrc.left >= (int) Src->currentDesc.Width)) ))
923 TRACE("Nothing to be done !\n");
927 /* The easy case : the source-less blits.... */
931 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
935 full_rect.right = This->currentDesc.Width;
936 full_rect.bottom = This->currentDesc.Height;
937 IntersectRect(&temp_rect, &full_rect, &xdst);
942 /* Only handle clipping on the destination rectangle */
943 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) This->currentDesc.Width );
944 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) This->currentDesc.Height);
945 if (clip_vert || clip_horiz)
947 /* Now check if this is a special case or not... */
948 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
949 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
950 (Flags & WINEDDBLT_DDFX))
952 WARN("Out of screen rectangle in special case. Not handled right now.\n");
958 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
959 if (xdst.right > This->currentDesc.Width)
961 xsrc.right -= (xdst.right - (int) This->currentDesc.Width);
962 xdst.right = (int) This->currentDesc.Width;
969 xsrc.top -= xdst.top;
972 if (xdst.bottom > This->currentDesc.Height)
974 xsrc.bottom -= (xdst.bottom - (int) This->currentDesc.Height);
975 xdst.bottom = (int) This->currentDesc.Height;
978 /* And check if after clipping something is still to be done... */
979 if ((xdst.bottom <= 0) || (xdst.right <= 0) ||
980 (xdst.top >= (int) This->currentDesc.Height) ||
981 (xdst.left >= (int) This->currentDesc.Width) ||
982 (xsrc.bottom <= 0) || (xsrc.right <= 0) ||
983 (xsrc.top >= (int) Src->currentDesc.Height) ||
984 (xsrc.left >= (int) Src->currentDesc.Width))
986 TRACE("Nothing to be done after clipping !\n");
992 bpp = This->bytesPerPixel;
993 srcheight = xsrc.bottom - xsrc.top;
994 srcwidth = xsrc.right - xsrc.left;
995 dstheight = xdst.bottom - xdst.top;
996 dstwidth = xdst.right - xdst.left;
997 width = (xdst.right - xdst.left) * bpp;
999 assert(width <= dlock.Pitch);
1001 dbuf = (BYTE*)dlock.pBits+(xdst.top*dlock.Pitch)+(xdst.left*bpp);
1003 if (Flags & WINEDDBLT_WAIT)
1005 Flags &= ~WINEDDBLT_WAIT;
1007 if (Flags & WINEDDBLT_ASYNC)
1009 static BOOL displayed = FALSE;
1011 FIXME("Can't handle WINEDDBLT_ASYNC flag right now.\n");
1013 Flags &= ~WINEDDBLT_ASYNC;
1015 if (Flags & WINEDDBLT_DONOTWAIT)
1017 /* WINEDDBLT_DONOTWAIT appeared in DX7 */
1018 static BOOL displayed = FALSE;
1020 FIXME("Can't handle WINEDDBLT_DONOTWAIT flag right now.\n");
1022 Flags &= ~WINEDDBLT_DONOTWAIT;
1025 /* First, all the 'source-less' blits */
1026 if (Flags & WINEDDBLT_COLORFILL)
1028 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
1029 dlock.Pitch, DDBltFx->u5.dwFillColor);
1030 Flags &= ~WINEDDBLT_COLORFILL;
1033 if (Flags & WINEDDBLT_DEPTHFILL)
1035 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
1037 if (Flags & WINEDDBLT_ROP)
1039 /* Catch some degenerate cases here */
1040 switch(DDBltFx->dwROP)
1043 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,0);
1045 case 0xAA0029: /* No-op */
1048 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,dlock.Pitch,~0);
1050 case SRCCOPY: /* well, we do that below ? */
1053 FIXME("Unsupported raster op: %08x Pattern: %p\n", DDBltFx->dwROP, DDBltFx->u5.lpDDSPattern);
1056 Flags &= ~WINEDDBLT_ROP;
1058 if (Flags & WINEDDBLT_DDROPS)
1060 FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", DDBltFx->dwDDROP, DDBltFx->u5.lpDDSPattern);
1062 /* Now the 'with source' blits */
1066 int sx, xinc, sy, yinc;
1068 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
1070 sbase = (BYTE*)slock.pBits+(xsrc.top*slock.Pitch)+xsrc.left*bpp;
1071 xinc = (srcwidth << 16) / dstwidth;
1072 yinc = (srcheight << 16) / dstheight;
1076 /* No effects, we can cheat here */
1077 if (dstwidth == srcwidth)
1079 if (dstheight == srcheight)
1081 /* No stretching in either direction. This needs to be as
1082 * fast as possible */
1085 /* check for overlapping surfaces */
1086 if (Src != This || xdst.top < xsrc.top ||
1087 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
1089 /* no overlap, or dst above src, so copy from top downwards */
1090 for (y = 0; y < dstheight; y++)
1092 memcpy(dbuf, sbuf, width);
1093 sbuf += slock.Pitch;
1094 dbuf += dlock.Pitch;
1097 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
1099 sbuf += (slock.Pitch*dstheight);
1100 dbuf += (dlock.Pitch*dstheight);
1101 for (y = 0; y < dstheight; y++)
1103 sbuf -= slock.Pitch;
1104 dbuf -= dlock.Pitch;
1105 memcpy(dbuf, sbuf, width);
1108 else /* src and dst overlapping on the same line, use memmove */
1110 for (y = 0; y < dstheight; y++)
1112 memmove(dbuf, sbuf, width);
1113 sbuf += slock.Pitch;
1114 dbuf += dlock.Pitch;
1118 /* Stretching in Y direction only */
1119 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
1120 sbuf = sbase + (sy >> 16) * slock.Pitch;
1121 memcpy(dbuf, sbuf, width);
1122 dbuf += dlock.Pitch;
1128 /* Stretching in X direction */
1130 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1132 sbuf = sbase + (sy >> 16) * slock.Pitch;
1134 if ((sy >> 16) == (last_sy >> 16))
1136 /* this sourcerow is the same as last sourcerow -
1137 * copy already stretched row
1139 memcpy(dbuf, dbuf - dlock.Pitch, width);
1143 #define STRETCH_ROW(type) { \
1144 type *s = (type *) sbuf, *d = (type *) dbuf; \
1145 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
1146 d[x] = s[sx >> 16]; \
1151 case 1: STRETCH_ROW(BYTE)
1152 case 2: STRETCH_ROW(WORD)
1153 case 4: STRETCH_ROW(DWORD)
1157 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1161 s = sbuf+3*(sx>>16);
1162 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1163 d[0] = (pixel )&0xff;
1164 d[1] = (pixel>> 8)&0xff;
1165 d[2] = (pixel>>16)&0xff;
1171 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
1172 ret = WINED3DERR_NOTAVAILABLE;
1177 dbuf += dlock.Pitch;
1184 LONG dstyinc = dlock.Pitch, dstxinc = bpp;
1185 DWORD keylow = 0xFFFFFFFF, keyhigh = 0, keymask = 0xFFFFFFFF;
1186 DWORD destkeylow = 0x0, destkeyhigh = 0xFFFFFFFF, destkeymask = 0xFFFFFFFF;
1187 if (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE))
1189 /* The color keying flags are checked for correctness in ddraw */
1190 if (Flags & WINEDDBLT_KEYSRC)
1192 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1193 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1195 else if (Flags & WINEDDBLT_KEYSRCOVERRIDE)
1197 keylow = DDBltFx->ddckSrcColorkey.dwColorSpaceLowValue;
1198 keyhigh = DDBltFx->ddckSrcColorkey.dwColorSpaceHighValue;
1201 if (Flags & WINEDDBLT_KEYDEST)
1203 /* Destination color keys are taken from the source surface ! */
1204 destkeylow = Src->DestBltCKey.dwColorSpaceLowValue;
1205 destkeyhigh = Src->DestBltCKey.dwColorSpaceHighValue;
1207 else if (Flags & WINEDDBLT_KEYDESTOVERRIDE)
1209 destkeylow = DDBltFx->ddckDestColorkey.dwColorSpaceLowValue;
1210 destkeyhigh = DDBltFx->ddckDestColorkey.dwColorSpaceHighValue;
1219 keymask = sEntry->redMask |
1223 Flags &= ~(WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE);
1226 if (Flags & WINEDDBLT_DDFX)
1228 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
1231 dTopRight = dbuf+((dstwidth-1)*bpp);
1232 dBottomLeft = dTopLeft+((dstheight-1)*dlock.Pitch);
1233 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
1235 if (DDBltFx->dwDDFX & WINEDDBLTFX_ARITHSTRETCHY)
1237 /* I don't think we need to do anything about this flag */
1238 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
1240 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORLEFTRIGHT)
1243 dTopRight = dTopLeft;
1246 dBottomRight = dBottomLeft;
1248 dstxinc = dstxinc *-1;
1250 if (DDBltFx->dwDDFX & WINEDDBLTFX_MIRRORUPDOWN)
1253 dTopLeft = dBottomLeft;
1256 dTopRight = dBottomRight;
1258 dstyinc = dstyinc *-1;
1260 if (DDBltFx->dwDDFX & WINEDDBLTFX_NOTEARING)
1262 /* I don't think we need to do anything about this flag */
1263 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
1265 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE180)
1268 dBottomRight = dTopLeft;
1271 dBottomLeft = dTopRight;
1273 dstxinc = dstxinc * -1;
1274 dstyinc = dstyinc * -1;
1276 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE270)
1279 dTopLeft = dBottomLeft;
1280 dBottomLeft = dBottomRight;
1281 dBottomRight = dTopRight;
1286 dstxinc = dstxinc * -1;
1288 if (DDBltFx->dwDDFX & WINEDDBLTFX_ROTATE90)
1291 dTopLeft = dTopRight;
1292 dTopRight = dBottomRight;
1293 dBottomRight = dBottomLeft;
1298 dstyinc = dstyinc * -1;
1300 if (DDBltFx->dwDDFX & WINEDDBLTFX_ZBUFFERBASEDEST)
1302 /* I don't think we need to do anything about this flag */
1303 WARN("Flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
1306 Flags &= ~(WINEDDBLT_DDFX);
1309 #define COPY_COLORKEY_FX(type) { \
1310 type *s, *d = (type *) dbuf, *dx, tmp; \
1311 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1312 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1314 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1315 tmp = s[sx >> 16]; \
1316 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1317 ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1320 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1322 d = (type*)(((LPBYTE)d)+dstyinc); \
1327 case 1: COPY_COLORKEY_FX(BYTE)
1328 case 2: COPY_COLORKEY_FX(WORD)
1329 case 4: COPY_COLORKEY_FX(DWORD)
1332 LPBYTE s,d = dbuf, dx;
1333 for (y = sy = 0; y < dstheight; y++, sy += yinc)
1335 sbuf = sbase + (sy >> 16) * slock.Pitch;
1337 for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
1339 DWORD pixel, dpixel = 0;
1340 s = sbuf+3*(sx>>16);
1341 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
1342 dpixel = dx[0]|(dx[1]<<8)|(dx[2]<<16);
1343 if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh) &&
1344 ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
1346 dx[0] = (pixel )&0xff;
1347 dx[1] = (pixel>> 8)&0xff;
1348 dx[2] = (pixel>>16)&0xff;
1357 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1358 (Flags & WINEDDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
1359 ret = WINED3DERR_NOTAVAILABLE;
1361 #undef COPY_COLORKEY_FX
1367 if (Flags && FIXME_ON(d3d_surface))
1369 FIXME("\tUnsupported flags: %08x\n", Flags);
1373 IWineD3DSurface_UnlockRect(iface);
1374 if (Src && Src != This) IWineD3DSurface_UnlockRect((IWineD3DSurface *) Src);
1375 /* Release the converted surface if any */
1376 if (Src && SrcSurface != (IWineD3DSurface *) Src) IWineD3DSurface_Release((IWineD3DSurface *) Src);
1380 /*****************************************************************************
1381 * IWineD3DSurface::BltFast, SW emulation version
1383 * This is the software implementation of BltFast, as used by GDI surfaces
1384 * and as a fallback for OpenGL surfaces. This code is taken from the old
1385 * DirectDraw code, and was originally written by TransGaming.
1390 * Source: Source surface to copy from
1391 * rsrc: Source rectangle
1395 * WINED3D_OK on success
1397 *****************************************************************************/
1399 IWineD3DBaseSurfaceImpl_BltFast(IWineD3DSurface *iface,
1402 IWineD3DSurface *Source,
1406 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1407 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) Source;
1409 int bpp, w, h, x, y;
1410 WINED3DLOCKED_RECT dlock,slock;
1411 HRESULT ret = WINED3D_OK;
1413 RECT lock_src, lock_dst, lock_union;
1415 const StaticPixelFormatDesc *sEntry, *dEntry;
1417 if (TRACE_ON(d3d_surface))
1419 TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This,dstx,dsty,Src,rsrc,trans);
1423 TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc->left,rsrc->top,
1424 rsrc->right,rsrc->bottom);
1428 TRACE(" srcrect: NULL\n");
1432 if ((This->Flags & SFLAG_LOCKED) ||
1433 ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
1435 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1436 return WINEDDERR_SURFACEBUSY;
1441 WARN("rsrc is NULL!\n");
1445 rsrc->right = Src->currentDesc.Width;
1446 rsrc->bottom = Src->currentDesc.Height;
1449 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1450 if ((rsrc->bottom > Src->currentDesc.Height) || (rsrc->bottom < 0) ||
1451 (rsrc->top > Src->currentDesc.Height) || (rsrc->top < 0) ||
1452 (rsrc->left > Src->currentDesc.Width) || (rsrc->left < 0) ||
1453 (rsrc->right > Src->currentDesc.Width) || (rsrc->right < 0) ||
1454 (rsrc->right < rsrc->left) || (rsrc->bottom < rsrc->top))
1456 WARN("Application gave us bad source rectangle for BltFast.\n");
1457 return WINEDDERR_INVALIDRECT;
1460 h = rsrc->bottom - rsrc->top;
1461 if (h > This->currentDesc.Height-dsty) h = This->currentDesc.Height-dsty;
1462 if (h > Src->currentDesc.Height-rsrc->top) h=Src->currentDesc.Height-rsrc->top;
1463 if (h <= 0) return WINEDDERR_INVALIDRECT;
1465 w = rsrc->right - rsrc->left;
1466 if (w > This->currentDesc.Width-dstx) w = This->currentDesc.Width-dstx;
1467 if (w > Src->currentDesc.Width-rsrc->left) w = Src->currentDesc.Width-rsrc->left;
1468 if (w <= 0) return WINEDDERR_INVALIDRECT;
1470 /* Now compute the locking rectangle... */
1471 lock_src.left = rsrc->left;
1472 lock_src.top = rsrc->top;
1473 lock_src.right = lock_src.left + w;
1474 lock_src.bottom = lock_src.top + h;
1476 lock_dst.left = dstx;
1477 lock_dst.top = dsty;
1478 lock_dst.right = dstx + w;
1479 lock_dst.bottom = dsty + h;
1481 bpp = This->bytesPerPixel;
1483 /* We need to lock the surfaces, or we won't get refreshes when done. */
1488 UnionRect(&lock_union, &lock_src, &lock_dst);
1490 /* Lock the union of the two rectangles */
1491 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_union, 0);
1492 if(ret != WINED3D_OK) goto error;
1494 pitch = dlock.Pitch;
1495 slock.Pitch = dlock.Pitch;
1497 /* Since slock was originally copied from this surface's description, we can just reuse it */
1498 assert(This->resource.allocatedMemory != NULL);
1499 sbuf = This->resource.allocatedMemory + lock_src.top * pitch + lock_src.left * bpp;
1500 dbuf = This->resource.allocatedMemory + lock_dst.top * pitch + lock_dst.left * bpp;
1501 sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1506 ret = IWineD3DSurface_LockRect(Source, &slock, &lock_src, WINED3DLOCK_READONLY);
1507 if(ret != WINED3D_OK) goto error;
1508 ret = IWineD3DSurface_LockRect(iface, &dlock, &lock_dst, 0);
1509 if(ret != WINED3D_OK) goto error;
1513 TRACE("Dst is at %p, Src is at %p\n", dbuf, sbuf);
1515 sEntry = getFormatDescEntry(Src->resource.format, NULL, NULL);
1516 dEntry = getFormatDescEntry(This->resource.format, NULL, NULL);
1519 /* Handle first the FOURCC surfaces... */
1520 if (sEntry->isFourcc && dEntry->isFourcc)
1522 TRACE("Fourcc -> Fourcc copy\n");
1524 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1526 FIXME("offset for destination surface is not supported\n");
1527 if (Src->resource.format != This->resource.format)
1529 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1530 ret = WINED3DERR_WRONGTEXTUREFORMAT;
1533 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1534 memcpy(dbuf, sbuf, This->resource.size);
1537 if (sEntry->isFourcc && !dEntry->isFourcc)
1539 /* TODO: Use the libtxc_dxtn.so shared library to do
1540 * software decompression
1542 ERR("DXTC decompression not supported by now\n");
1546 if (trans & (WINEDDBLTFAST_SRCCOLORKEY | WINEDDBLTFAST_DESTCOLORKEY))
1548 DWORD keylow, keyhigh;
1549 TRACE("Color keyed copy\n");
1550 if (trans & WINEDDBLTFAST_SRCCOLORKEY)
1552 keylow = Src->SrcBltCKey.dwColorSpaceLowValue;
1553 keyhigh = Src->SrcBltCKey.dwColorSpaceHighValue;
1557 /* I'm not sure if this is correct */
1558 FIXME("WINEDDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1559 keylow = This->DestBltCKey.dwColorSpaceLowValue;
1560 keyhigh = This->DestBltCKey.dwColorSpaceHighValue;
1563 #define COPYBOX_COLORKEY(type) { \
1565 s = (type *) sbuf; \
1566 d = (type *) dbuf; \
1567 for (y = 0; y < h; y++) { \
1568 for (x = 0; x < w; x++) { \
1570 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1572 s = (type *)((BYTE *)s + slock.Pitch); \
1573 d = (type *)((BYTE *)d + dlock.Pitch); \
1579 case 1: COPYBOX_COLORKEY(BYTE)
1580 case 2: COPYBOX_COLORKEY(WORD)
1581 case 4: COPYBOX_COLORKEY(DWORD)
1588 for (y = 0; y < h; y++)
1590 for (x = 0; x < w * 3; x += 3)
1592 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1593 if (tmp < keylow || tmp > keyhigh)
1595 d[x + 0] = s[x + 0];
1596 d[x + 1] = s[x + 1];
1597 d[x + 2] = s[x + 2];
1606 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1607 ret = WINED3DERR_NOTAVAILABLE;
1610 #undef COPYBOX_COLORKEY
1611 TRACE("Copy Done\n");
1615 int width = w * bpp;
1616 INT sbufpitch, dbufpitch;
1618 TRACE("NO color key copy\n");
1619 /* Handle overlapping surfaces */
1622 sbuf += (h - 1) * slock.Pitch;
1623 dbuf += (h - 1) * dlock.Pitch;
1624 sbufpitch = -slock.Pitch;
1625 dbufpitch = -dlock.Pitch;
1629 sbufpitch = slock.Pitch;
1630 dbufpitch = dlock.Pitch;
1632 for (y = 0; y < h; y++)
1634 /* This is pretty easy, a line for line memcpy */
1635 memmove(dbuf, sbuf, width);
1639 TRACE("Copy done\n");
1645 IWineD3DSurface_UnlockRect(iface);
1649 IWineD3DSurface_UnlockRect(iface);
1650 IWineD3DSurface_UnlockRect(Source);
1656 HRESULT WINAPI IWineD3DBaseSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags)
1658 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1660 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
1661 This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1663 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
1667 pLockedRect->pBits = This->resource.allocatedMemory;
1668 This->lockedRect.left = 0;
1669 This->lockedRect.top = 0;
1670 This->lockedRect.right = This->currentDesc.Width;
1671 This->lockedRect.bottom = This->currentDesc.Height;
1673 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
1674 &This->lockedRect, This->lockedRect.left, This->lockedRect.top,
1675 This->lockedRect.right, This->lockedRect.bottom);
1679 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
1680 pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
1682 /* DXTn textures are based on compressed blocks of 4x4 pixels, each
1683 * 16 bytes large (8 bytes in case of DXT1). Because of that Pitch has
1684 * slightly different meaning compared to regular textures. For DXTn
1685 * textures Pitch is the size of a row of blocks, 4 high and "width"
1686 * long. The x offset is calculated differently as well, since moving 4
1687 * pixels to the right actually moves an entire 4x4 block to right, ie
1688 * 16 bytes (8 in case of DXT1). */
1689 if (This->resource.format == WINED3DFMT_DXT1)
1691 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 2);
1693 else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
1694 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5)
1696 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 4);
1700 pLockedRect->pBits = This->resource.allocatedMemory +
1701 (pLockedRect->Pitch * pRect->top) +
1702 (pRect->left * This->bytesPerPixel);
1704 This->lockedRect.left = pRect->left;
1705 This->lockedRect.top = pRect->top;
1706 This->lockedRect.right = pRect->right;
1707 This->lockedRect.bottom = pRect->bottom;
1710 /* No dirtifying is needed for this surface implementation */
1711 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect->pBits, pLockedRect->Pitch);
1716 void WINAPI IWineD3DBaseSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
1717 ERR("Should not be called on base texture\n");