Removed some more uses of the non-standard ICOM_THIS macro.
[wine/multimedia.git] / dlls / ddraw / dsurface / dib.c
blobc01850bf8169a268b542aa784b7a75ca9e6675bb
1 /* DIBSection DirectDrawSurface driver
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 1998-2000 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "ddraw_private.h"
33 #include "d3d_private.h"
34 #include "dsurface/main.h"
35 #include "dsurface/dib.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 /* FIXME */
40 extern HBITMAP DIB_CreateDIBSection( HDC hdc, const BITMAPINFO *bmi, UINT usage, VOID **bits,
41 HANDLE section, DWORD offset, DWORD ovr_pitch );
43 static IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable;
45 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
46 inline static int get_dib_width_bytes( int width, int depth )
48 int words;
50 switch(depth)
52 case 1: words = (width + 31) / 32; break;
53 case 4: words = (width + 7) / 8; break;
54 case 8: words = (width + 3) / 4; break;
55 case 15:
56 case 16: words = (width + 1) / 2; break;
57 case 24: words = (width * 3 + 3)/4; break;
58 default:
59 WARN("(%d): Unsupported depth\n", depth );
60 /* fall through */
61 case 32: words = width; break;
63 return 4 * words;
67 static HRESULT create_dib(IDirectDrawSurfaceImpl* This)
69 BITMAPINFO* b_info;
70 UINT usage;
71 HDC ddc;
72 DIB_DirectDrawSurfaceImpl* priv = This->private;
74 assert(This->surface_desc.lpSurface != NULL);
76 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
78 case 16:
79 case 32:
80 /* Allocate extra space to store the RGB bit masks. */
81 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
82 sizeof(BITMAPINFOHEADER)
83 + 3 * sizeof(DWORD));
84 break;
86 case 24:
87 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
88 sizeof(BITMAPINFOHEADER));
89 break;
91 default:
92 /* Allocate extra space for a palette. */
93 b_info = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
94 sizeof(BITMAPINFOHEADER)
95 + sizeof(RGBQUAD)
96 * (1 << This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount));
97 break;
100 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
101 b_info->bmiHeader.biWidth = This->surface_desc.dwWidth;
102 b_info->bmiHeader.biHeight = -This->surface_desc.dwHeight;
103 b_info->bmiHeader.biPlanes = 1;
104 b_info->bmiHeader.biBitCount = This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount;
106 if ((This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 16)
107 && (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount != 32))
108 b_info->bmiHeader.biCompression = BI_RGB;
109 else
110 b_info->bmiHeader.biCompression = BI_BITFIELDS;
112 b_info->bmiHeader.biSizeImage
113 = (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8)
114 * This->surface_desc.dwWidth * This->surface_desc.dwHeight;
116 b_info->bmiHeader.biXPelsPerMeter = 0;
117 b_info->bmiHeader.biYPelsPerMeter = 0;
118 b_info->bmiHeader.biClrUsed = 0;
119 b_info->bmiHeader.biClrImportant = 0;
121 if (!This->surface_desc.u1.lPitch) {
122 /* This can't happen, right? */
123 /* or use GDI_GetObj to get it from the created DIB? */
124 This->surface_desc.u1.lPitch = get_dib_width_bytes(b_info->bmiHeader.biWidth, b_info->bmiHeader.biBitCount);
125 This->surface_desc.dwFlags |= DDSD_PITCH;
128 switch (This->surface_desc.u4.ddpfPixelFormat.u1.dwRGBBitCount)
130 case 16:
131 case 32:
133 DWORD *masks = (DWORD *) &(b_info->bmiColors);
135 usage = 0;
136 masks[0] = This->surface_desc.u4.ddpfPixelFormat.u2.dwRBitMask;
137 masks[1] = This->surface_desc.u4.ddpfPixelFormat.u3.dwGBitMask;
138 masks[2] = This->surface_desc.u4.ddpfPixelFormat.u4.dwBBitMask;
140 break;
142 case 24:
143 /* Nothing to do */
144 usage = DIB_RGB_COLORS;
145 break;
147 default:
148 /* Don't know palette */
149 usage = 0;
150 break;
153 ddc = CreateDCA("DISPLAY", NULL, NULL, NULL);
154 if (ddc == 0)
156 HeapFree(GetProcessHeap(), 0, b_info);
157 return HRESULT_FROM_WIN32(GetLastError());
160 priv->dib.DIBsection
161 = DIB_CreateDIBSection(ddc, b_info, usage, &(priv->dib.bitmap_data), 0,
162 (DWORD)This->surface_desc.lpSurface,
163 This->surface_desc.u1.lPitch);
164 DeleteDC(ddc);
165 if (!priv->dib.DIBsection) {
166 ERR("CreateDIBSection failed!\n");
167 HeapFree(GetProcessHeap(), 0, b_info);
168 return HRESULT_FROM_WIN32(GetLastError());
171 TRACE("DIBSection at : %p\n", priv->dib.bitmap_data);
173 if (!This->surface_desc.lpSurface) {
174 This->surface_desc.lpSurface = priv->dib.bitmap_data;
175 This->surface_desc.dwFlags |= DDSD_LPSURFACE;
178 HeapFree(GetProcessHeap(), 0, b_info);
180 /* I don't think it's worth checking for this. */
181 if (priv->dib.bitmap_data != This->surface_desc.lpSurface)
182 ERR("unexpected error creating DirectDrawSurface DIB section\n");
184 /* this seems like a good place to put the handle for HAL driver use */
185 This->global_more.hKernelSurface = (ULONG_PTR)priv->dib.DIBsection;
187 return S_OK;
190 void DIB_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
192 DIB_DirectDrawSurfaceImpl* priv = This->private;
194 DeleteObject(priv->dib.DIBsection);
196 if (!priv->dib.client_memory)
197 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
199 Main_DirectDrawSurface_final_release(This);
202 HRESULT DIB_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
203 LPDIRECTDRAWSURFACE7* ppDup)
205 return DIB_DirectDrawSurface_Create(This->ddraw_owner,
206 &This->surface_desc, ppDup, NULL);
209 HRESULT DIB_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
210 IDirectDrawImpl *pDD,
211 const DDSURFACEDESC2 *pDDSD)
213 HRESULT hr;
214 DIB_DirectDrawSurfaceImpl* priv = This->private;
216 TRACE("(%p)->(%p,%p)\n",This,pDD,pDDSD);
217 hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
218 if (FAILED(hr)) return hr;
220 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
221 DIB_IDirectDrawSurface7_VTable);
223 This->final_release = DIB_DirectDrawSurface_final_release;
224 This->duplicate_surface = DIB_DirectDrawSurface_duplicate_surface;
225 This->flip_data = DIB_DirectDrawSurface_flip_data;
227 This->get_dc = DIB_DirectDrawSurface_get_dc;
228 This->release_dc = DIB_DirectDrawSurface_release_dc;
229 This->hDC = NULL;
231 This->set_palette = DIB_DirectDrawSurface_set_palette;
232 This->update_palette = DIB_DirectDrawSurface_update_palette;
234 TRACE("(%ldx%ld, pitch=%ld)\n",
235 This->surface_desc.dwWidth, This->surface_desc.dwHeight,
236 This->surface_desc.u1.lPitch);
237 /* XXX load dwWidth and dwHeight from pDD if they are not specified? */
239 if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
241 /* "Client memory": it is managed by the application. */
242 /* XXX What if lPitch is not set? Use dwWidth or fail? */
244 priv->dib.client_memory = TRUE;
246 else
248 if (!(This->surface_desc.dwFlags & DDSD_PITCH))
250 int pitch = This->surface_desc.u1.lPitch;
251 if (pitch % 8 != 0)
252 pitch += 8 - (pitch % 8);
254 /* XXX else: how should lPitch be verified? */
256 This->surface_desc.dwFlags |= DDSD_PITCH|DDSD_LPSURFACE;
258 if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
259 This->surface_desc.lpSurface
260 = VirtualAlloc(NULL, This->surface_desc.u1.dwLinearSize, MEM_COMMIT, PAGE_READWRITE);
261 else
262 This->surface_desc.lpSurface
263 = VirtualAlloc(NULL, This->surface_desc.u1.lPitch
264 * This->surface_desc.dwHeight + 4, /* The + 4 here is for dumb games reading after the end of the surface
265 when reading the last byte / half using word access */
266 MEM_COMMIT, PAGE_READWRITE);
268 if (This->surface_desc.lpSurface == NULL)
270 Main_DirectDrawSurface_final_release(This);
271 return HRESULT_FROM_WIN32(GetLastError());
274 priv->dib.client_memory = FALSE;
277 hr = create_dib(This);
278 if (FAILED(hr))
280 if (!priv->dib.client_memory)
281 VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);
283 Main_DirectDrawSurface_final_release(This);
285 return hr;
288 return DD_OK;
291 /* Not an API */
292 HRESULT DIB_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
293 const DDSURFACEDESC2 *pDDSD,
294 LPDIRECTDRAWSURFACE7 *ppSurf,
295 IUnknown *pUnkOuter)
297 IDirectDrawSurfaceImpl* This;
298 HRESULT hr;
299 assert(pUnkOuter == NULL);
301 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
302 sizeof(*This) + sizeof(DIB_DirectDrawSurfaceImpl));
303 if (This == NULL) return E_OUTOFMEMORY;
305 This->private = (DIB_DirectDrawSurfaceImpl*)(This+1);
307 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
308 if (FAILED(hr))
309 HeapFree(GetProcessHeap(), 0, This);
310 else
311 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
313 return hr;
317 /* AddAttachedSurface: generic */
318 /* AddOverlayDirtyRect: generic, unimplemented */
320 static HRESULT _Blt_ColorFill(
321 LPBYTE buf, int width, int height, int bpp, LONG lPitch, DWORD color
323 int x, y;
324 LPBYTE first;
326 /* Do first row */
328 #define COLORFILL_ROW(type) { \
329 type *d = (type *) buf; \
330 for (x = 0; x < width; x++) \
331 d[x] = (type) color; \
332 break; \
335 switch(bpp) {
336 case 1: COLORFILL_ROW(BYTE)
337 case 2: COLORFILL_ROW(WORD)
338 case 3: { BYTE *d = (BYTE *) buf;
339 for (x = 0; x < width; x++,d+=3) {
340 d[0] = (color ) & 0xFF;
341 d[1] = (color>> 8) & 0xFF;
342 d[2] = (color>>16) & 0xFF;
344 break;}
345 case 4: COLORFILL_ROW(DWORD)
346 default:
347 FIXME("Color fill not implemented for bpp %d!\n", bpp*8);
348 return DDERR_UNSUPPORTED;
351 #undef COLORFILL_ROW
353 /* Now copy first row */
354 first = buf;
355 for (y = 1; y < height; y++) {
356 buf += lPitch;
357 memcpy(buf, first, width * bpp);
359 return DD_OK;
362 static void ComputeShifts(DWORD mask, DWORD* lshift, DWORD* rshift)
364 int pos = 0;
365 int bits = 0;
366 *lshift = 0;
367 *rshift = 0;
369 if (!mask)
370 return;
372 while(!(mask & (1 << pos)))
373 pos++;
375 while(mask & (1 << (pos+bits)))
376 bits++;
378 *lshift = pos;
379 *rshift = 8 - bits;
382 /* This is used to factorize the decompression between the Blt and BltFast code */
383 static void DoDXTCDecompression(const DDSURFACEDESC2 *sdesc, const DDSURFACEDESC2 *ddesc)
385 DWORD rs,rb,rm;
386 DWORD gs,gb,gm;
387 DWORD bs,bb,bm;
388 DWORD as,ab,am;
390 if (!s3tc_initialized) {
391 /* FIXME: We may fake this by rendering the texture into the framebuffer using OpenGL functions and reading back
392 * the framebuffer. This will be slow and somewhat ugly. */
393 FIXME("Manual S3TC decompression is not supported in native mode\n");
394 return;
397 rm = ddesc->u4.ddpfPixelFormat.u2.dwRBitMask;
398 ComputeShifts(rm, &rs, &rb);
399 gm = ddesc->u4.ddpfPixelFormat.u3.dwGBitMask;
400 ComputeShifts(gm, &gs, &gb);
401 bm = ddesc->u4.ddpfPixelFormat.u4.dwBBitMask;
402 ComputeShifts(bm, &bs, &bb);
403 am = ddesc->u4.ddpfPixelFormat.u5.dwRGBAlphaBitMask;
404 ComputeShifts(am, &as, &ab);
405 if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','1')) {
406 int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
407 int pitch = ddesc->u1.lPitch;
408 int width = ddesc->dwWidth;
409 int height = ddesc->dwHeight;
410 int x,y;
411 char* dst = (char*) ddesc->lpSurface;
412 char* src = (char*) sdesc->lpSurface;
413 for (x = 0; x < width; x++)
414 for (y =0; y < height; y++) {
415 DWORD pixel = 0;
416 BYTE data[4];
417 (*fetch_2d_texel_rgba_dxt1)(width, src, x, y, data);
418 pixel = 0;
419 pixel |= ((data[0] >> rb) << rs) & rm;
420 pixel |= ((data[1] >> gb) << gs) & gm;
421 pixel |= ((data[2] >> bb) << bs) & bm;
422 pixel |= ((data[3] >> ab) << as) & am;
423 if (is16)
424 *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
425 else
426 *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
428 } else if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','3')) {
429 int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
430 int pitch = ddesc->u1.lPitch;
431 int width = ddesc->dwWidth;
432 int height = ddesc->dwHeight;
433 int x,y;
434 char* dst = (char*) ddesc->lpSurface;
435 char* src = (char*) sdesc->lpSurface;
436 for (x = 0; x < width; x++)
437 for (y =0; y < height; y++) {
438 DWORD pixel = 0;
439 BYTE data[4];
440 (*fetch_2d_texel_rgba_dxt3)(width, src, x, y, data);
441 pixel = 0;
442 pixel |= ((data[0] >> rb) << rs) & rm;
443 pixel |= ((data[1] >> gb) << gs) & gm;
444 pixel |= ((data[2] >> bb) << bs) & bm;
445 pixel |= ((data[3] >> ab) << as) & am;
446 if (is16)
447 *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
448 else
449 *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
451 } else if (sdesc->u4.ddpfPixelFormat.dwFourCC == MAKE_FOURCC('D','X','T','5')) {
452 int is16 = ddesc->u4.ddpfPixelFormat.u1.dwRGBBitCount == 16;
453 int pitch = ddesc->u1.lPitch;
454 int width = ddesc->dwWidth;
455 int height = ddesc->dwHeight;
456 int x,y;
457 char* dst = (char*) ddesc->lpSurface;
458 char* src = (char*) sdesc->lpSurface;
459 for (x = 0; x < width; x++)
460 for (y =0; y < height; y++) {
461 DWORD pixel = 0;
462 BYTE data[4];
463 (*fetch_2d_texel_rgba_dxt5)(width, src, x, y, data);
464 pixel = 0;
465 pixel |= ((data[0] >> rb) << rs) & rm;
466 pixel |= ((data[1] >> gb) << gs) & gm;
467 pixel |= ((data[2] >> bb) << bs) & bm;
468 pixel |= ((data[3] >> ab) << as) & am;
469 if (is16)
470 *((WORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
471 else
472 *((DWORD*)(dst+y*pitch+x*(is16?2:4))) = pixel;
475 #if 0 /* Usefull for debugging */
477 static int idx;
478 char texname[255];
479 FILE* f;
480 sprintf(texname, "dxt_%d.pnm", idx++);
481 f = fopen(texname,"w");
482 DDRAW_dump_surface_to_disk(This, f, 1);
483 fclose(f);
485 #endif
488 HRESULT WINAPI
489 DIB_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
490 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
491 DWORD dwFlags, LPDDBLTFX lpbltfx)
493 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
494 RECT xdst,xsrc;
495 DDSURFACEDESC2 ddesc,sdesc;
496 HRESULT ret = DD_OK;
497 int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
498 int x, y;
499 LPBYTE dbuf, sbuf;
501 TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
503 if (TRACE_ON(ddraw)) {
504 if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
505 if (rsrc) TRACE("\tsrcrect :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
506 TRACE("\tflags: ");
507 DDRAW_dump_DDBLT(dwFlags);
508 if (dwFlags & DDBLT_DDFX) {
509 TRACE("\tblitfx: ");
510 DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
514 /* First, check if the possible override function handles this case */
515 if (This->aux_blt != NULL) {
516 if (This->aux_blt(This, rdst, src, rsrc, dwFlags, lpbltfx) == DD_OK) return DD_OK;
519 DD_STRUCT_INIT(&ddesc);
520 DD_STRUCT_INIT(&sdesc);
522 sdesc.dwSize = sizeof(sdesc);
523 if (src) IDirectDrawSurface7_Lock(src, NULL, &sdesc, DDLOCK_READONLY, 0);
524 ddesc.dwSize = sizeof(ddesc);
525 IDirectDrawSurface7_Lock(iface,NULL,&ddesc,DDLOCK_WRITEONLY,0);
527 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
528 (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
529 if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
530 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
531 ret = DDERR_INVALIDPIXELFORMAT;
532 goto release;
534 memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
535 goto release;
538 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
539 (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
540 DoDXTCDecompression(&sdesc, &ddesc);
541 goto release;
544 if (rdst) {
545 memcpy(&xdst,rdst,sizeof(xdst));
546 } else {
547 xdst.top = 0;
548 xdst.bottom = ddesc.dwHeight;
549 xdst.left = 0;
550 xdst.right = ddesc.dwWidth;
553 if (rsrc) {
554 memcpy(&xsrc,rsrc,sizeof(xsrc));
555 } else {
556 if (src) {
557 xsrc.top = 0;
558 xsrc.bottom = sdesc.dwHeight;
559 xsrc.left = 0;
560 xsrc.right = sdesc.dwWidth;
561 } else {
562 memset(&xsrc,0,sizeof(xsrc));
566 /* First check for the validity of source / destination rectangles. This was
567 verified using a test application + by MSDN.
569 if ((src != NULL) &&
570 ((xsrc.bottom > sdesc.dwHeight) || (xsrc.bottom < 0) ||
571 (xsrc.top > sdesc.dwHeight) || (xsrc.top < 0) ||
572 (xsrc.left > sdesc.dwWidth) || (xsrc.left < 0) ||
573 (xsrc.right > sdesc.dwWidth) || (xsrc.right < 0) ||
574 (xsrc.right < xsrc.left) || (xsrc.bottom < xsrc.top))) {
575 WARN("Application gave us bad source rectangle for Blt.\n");
576 return DDERR_INVALIDRECT;
578 /* For the Destination rect, it can be out of bounds on the condition that a clipper
579 is set for the given surface.
581 if ((This->clipper == NULL) &&
582 ((xdst.bottom > ddesc.dwHeight) || (xdst.bottom < 0) ||
583 (xdst.top > ddesc.dwHeight) || (xdst.top < 0) ||
584 (xdst.left > ddesc.dwWidth) || (xdst.left < 0) ||
585 (xdst.right > ddesc.dwWidth) || (xdst.right < 0) ||
586 (xdst.right < xdst.left) || (xdst.bottom < xdst.top))) {
587 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
588 return DDERR_INVALIDRECT;
591 /* Now handle negative values in the rectangles. Warning: only supported for now
592 in the 'simple' cases (ie not in any stretching / rotation cases).
594 First, the case where nothing is to be done.
596 if (((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth)) ||
597 ((src != NULL) &&
598 ((xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth))))
600 TRACE("Nothing to be done !\n");
601 goto release;
604 /* The easy case : the source-less blits.... */
605 if (src == NULL) {
606 RECT full_rect;
607 RECT temp_rect; /* No idea if intersect rect can be the same as one of the source rect */
609 full_rect.left = 0;
610 full_rect.top = 0;
611 full_rect.right = ddesc.dwWidth;
612 full_rect.bottom = ddesc.dwHeight;
613 IntersectRect(&temp_rect, &full_rect, &xdst);
614 xdst = temp_rect;
615 } else {
616 /* Only handle clipping on the destination rectangle */
617 int clip_horiz = (xdst.left < 0) || (xdst.right > (int) ddesc.dwWidth );
618 int clip_vert = (xdst.top < 0) || (xdst.bottom > (int) ddesc.dwHeight);
619 if (clip_vert || clip_horiz) {
620 /* Now check if this is a special case or not... */
621 if ((((xdst.bottom - xdst.top ) != (xsrc.bottom - xsrc.top )) && clip_vert ) ||
622 (((xdst.right - xdst.left) != (xsrc.right - xsrc.left)) && clip_horiz) ||
623 (dwFlags & DDBLT_DDFX)) {
624 WARN("Out of screen rectangle in special case. Not handled right now.\n");
625 goto release;
628 if (clip_horiz) {
629 if (xdst.left < 0) { xsrc.left -= xdst.left; xdst.left = 0; }
630 if (xdst.right > ddesc.dwWidth) { xsrc.right -= (xdst.right - (int) ddesc.dwWidth); xdst.right = (int) ddesc.dwWidth; }
632 if (clip_vert) {
633 if (xdst.top < 0) { xsrc.top -= xdst.top; xdst.top = 0; }
634 if (xdst.bottom > ddesc.dwHeight) { xsrc.bottom -= (xdst.bottom - (int) ddesc.dwHeight); xdst.bottom = (int) ddesc.dwHeight; }
636 /* And check if after clipping something is still to be done... */
637 if ((xdst.bottom <= 0) || (xdst.right <= 0) || (xdst.top >= (int) ddesc.dwHeight) || (xdst.left >= (int) ddesc.dwWidth) ||
638 (xsrc.bottom <= 0) || (xsrc.right <= 0) || (xsrc.top >= (int) sdesc.dwHeight) || (xsrc.left >= (int) sdesc.dwWidth)) {
639 TRACE("Nothing to be done after clipping !\n");
640 goto release;
645 bpp = GET_BPP(ddesc);
646 srcheight = xsrc.bottom - xsrc.top;
647 srcwidth = xsrc.right - xsrc.left;
648 dstheight = xdst.bottom - xdst.top;
649 dstwidth = xdst.right - xdst.left;
650 width = (xdst.right - xdst.left) * bpp;
652 assert(width <= ddesc.u1.lPitch);
654 dbuf = (BYTE*)ddesc.lpSurface+(xdst.top*ddesc.u1.lPitch)+(xdst.left*bpp);
656 if (dwFlags & (DDBLT_WAIT|DDBLT_ASYNC))
658 static BOOL displayed = FALSE;
659 if (!displayed)
661 FIXME("dwFlags DDBLT_WAIT and/or DDBLT_ASYNC: can't handle right now.\n");
662 displayed = TRUE;
664 dwFlags &= ~(DDBLT_WAIT|DDBLT_ASYNC);
667 /* First, all the 'source-less' blits */
668 if (dwFlags & DDBLT_COLORFILL) {
669 ret = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp,
670 ddesc.u1.lPitch, lpbltfx->u5.dwFillColor);
671 dwFlags &= ~DDBLT_COLORFILL;
674 if (dwFlags & DDBLT_DEPTHFILL)
675 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
676 if (dwFlags & DDBLT_ROP) {
677 /* Catch some degenerate cases here */
678 switch(lpbltfx->dwROP) {
679 case BLACKNESS:
680 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,0);
681 break;
682 case 0xAA0029: /* No-op */
683 break;
684 case WHITENESS:
685 ret = _Blt_ColorFill(dbuf,dstwidth,dstheight,bpp,ddesc.u1.lPitch,~0);
686 break;
687 case SRCCOPY: /* well, we do that below ? */
688 break;
689 default:
690 FIXME("Unsupported raster op: %08lx Pattern: %p\n", lpbltfx->dwROP, lpbltfx->u5.lpDDSPattern);
691 goto error;
693 dwFlags &= ~DDBLT_ROP;
695 if (dwFlags & DDBLT_DDROPS) {
696 FIXME("\tDdraw Raster Ops: %08lx Pattern: %p\n", lpbltfx->dwDDROP, lpbltfx->u5.lpDDSPattern);
698 /* Now the 'with source' blits */
699 if (src) {
700 LPBYTE sbase;
701 int sx, xinc, sy, yinc;
703 if (!dstwidth || !dstheight) /* hmm... stupid program ? */
704 goto release;
705 sbase = (BYTE*)sdesc.lpSurface+(xsrc.top*sdesc.u1.lPitch)+xsrc.left*bpp;
706 xinc = (srcwidth << 16) / dstwidth;
707 yinc = (srcheight << 16) / dstheight;
709 if (!dwFlags) {
710 /* No effects, we can cheat here */
711 if (dstwidth == srcwidth) {
712 if (dstheight == srcheight) {
713 /* No stretching in either direction. This needs to be as
714 * fast as possible */
715 sbuf = sbase;
717 /* check for overlapping surfaces */
718 if (src != iface || xdst.top < xsrc.top ||
719 xdst.right <= xsrc.left || xsrc.right <= xdst.left)
721 /* no overlap, or dst above src, so copy from top downwards */
722 for (y = 0; y < dstheight; y++)
724 memcpy(dbuf, sbuf, width);
725 sbuf += sdesc.u1.lPitch;
726 dbuf += ddesc.u1.lPitch;
729 else if (xdst.top > xsrc.top) /* copy from bottom upwards */
731 sbuf += (sdesc.u1.lPitch*dstheight);
732 dbuf += (ddesc.u1.lPitch*dstheight);
733 for (y = 0; y < dstheight; y++)
735 sbuf -= sdesc.u1.lPitch;
736 dbuf -= ddesc.u1.lPitch;
737 memcpy(dbuf, sbuf, width);
740 else /* src and dst overlapping on the same line, use memmove */
742 for (y = 0; y < dstheight; y++)
744 memmove(dbuf, sbuf, width);
745 sbuf += sdesc.u1.lPitch;
746 dbuf += ddesc.u1.lPitch;
749 } else {
750 /* Stretching in Y direction only */
751 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
752 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
753 memcpy(dbuf, sbuf, width);
754 dbuf += ddesc.u1.lPitch;
757 } else {
758 /* Stretching in X direction */
759 int last_sy = -1;
760 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
761 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
763 if ((sy >> 16) == (last_sy >> 16)) {
764 /* this sourcerow is the same as last sourcerow -
765 * copy already stretched row
767 memcpy(dbuf, dbuf - ddesc.u1.lPitch, width);
768 } else {
769 #define STRETCH_ROW(type) { \
770 type *s = (type *) sbuf, *d = (type *) dbuf; \
771 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
772 d[x] = s[sx >> 16]; \
773 break; }
775 switch(bpp) {
776 case 1: STRETCH_ROW(BYTE)
777 case 2: STRETCH_ROW(WORD)
778 case 4: STRETCH_ROW(DWORD)
779 case 3: {
780 LPBYTE s,d = dbuf;
781 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
782 DWORD pixel;
784 s = sbuf+3*(sx>>16);
785 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
786 d[0] = (pixel )&0xff;
787 d[1] = (pixel>> 8)&0xff;
788 d[2] = (pixel>>16)&0xff;
789 d+=3;
791 break;
793 default:
794 FIXME("Stretched blit not implemented for bpp %d!\n", bpp*8);
795 ret = DDERR_UNSUPPORTED;
796 goto error;
798 #undef STRETCH_ROW
800 dbuf += ddesc.u1.lPitch;
801 last_sy = sy;
804 } else {
805 LONG dstyinc = ddesc.u1.lPitch, dstxinc = bpp;
806 DWORD keylow = 0, keyhigh = 0;
807 if (dwFlags & (DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE)) {
809 if (dwFlags & DDBLT_KEYSRC) {
810 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
811 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
812 } else if (dwFlags & DDBLT_KEYDEST){
813 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
814 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
815 } else if (dwFlags & DDBLT_KEYSRCOVERRIDE) {
816 keylow = lpbltfx->ddckSrcColorkey.dwColorSpaceLowValue;
817 keyhigh = lpbltfx->ddckSrcColorkey.dwColorSpaceHighValue;
818 } else {
819 keylow = lpbltfx->ddckDestColorkey.dwColorSpaceLowValue;
820 keyhigh = lpbltfx->ddckDestColorkey.dwColorSpaceHighValue;
822 dwFlags &= ~(DDBLT_KEYSRC | DDBLT_KEYDEST | DDBLT_KEYSRCOVERRIDE | DDBLT_KEYDESTOVERRIDE);
825 if (dwFlags & DDBLT_DDFX) {
826 LPBYTE dTopLeft, dTopRight, dBottomLeft, dBottomRight, tmp;
827 LONG tmpxy;
828 dTopLeft = dbuf;
829 dTopRight = dbuf+((dstwidth-1)*bpp);
830 dBottomLeft = dTopLeft+((dstheight-1)*ddesc.u1.lPitch);
831 dBottomRight = dBottomLeft+((dstwidth-1)*bpp);
833 if (lpbltfx->dwDDFX & DDBLTFX_ARITHSTRETCHY){
834 /* I don't think we need to do anything about this flag */
835 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ARITHSTRETCHY\n");
837 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORLEFTRIGHT) {
838 tmp = dTopRight;
839 dTopRight = dTopLeft;
840 dTopLeft = tmp;
841 tmp = dBottomRight;
842 dBottomRight = dBottomLeft;
843 dBottomLeft = tmp;
844 dstxinc = dstxinc *-1;
846 if (lpbltfx->dwDDFX & DDBLTFX_MIRRORUPDOWN) {
847 tmp = dTopLeft;
848 dTopLeft = dBottomLeft;
849 dBottomLeft = tmp;
850 tmp = dTopRight;
851 dTopRight = dBottomRight;
852 dBottomRight = tmp;
853 dstyinc = dstyinc *-1;
855 if (lpbltfx->dwDDFX & DDBLTFX_NOTEARING) {
856 /* I don't think we need to do anything about this flag */
857 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_NOTEARING\n");
859 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE180) {
860 tmp = dBottomRight;
861 dBottomRight = dTopLeft;
862 dTopLeft = tmp;
863 tmp = dBottomLeft;
864 dBottomLeft = dTopRight;
865 dTopRight = tmp;
866 dstxinc = dstxinc * -1;
867 dstyinc = dstyinc * -1;
869 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE270) {
870 tmp = dTopLeft;
871 dTopLeft = dBottomLeft;
872 dBottomLeft = dBottomRight;
873 dBottomRight = dTopRight;
874 dTopRight = tmp;
875 tmpxy = dstxinc;
876 dstxinc = dstyinc;
877 dstyinc = tmpxy;
878 dstxinc = dstxinc * -1;
880 if (lpbltfx->dwDDFX & DDBLTFX_ROTATE90) {
881 tmp = dTopLeft;
882 dTopLeft = dTopRight;
883 dTopRight = dBottomRight;
884 dBottomRight = dBottomLeft;
885 dBottomLeft = tmp;
886 tmpxy = dstxinc;
887 dstxinc = dstyinc;
888 dstyinc = tmpxy;
889 dstyinc = dstyinc * -1;
891 if (lpbltfx->dwDDFX & DDBLTFX_ZBUFFERBASEDEST) {
892 /* I don't think we need to do anything about this flag */
893 WARN("dwflags=DDBLT_DDFX nothing done for DDBLTFX_ZBUFFERBASEDEST\n");
895 dbuf = dTopLeft;
896 dwFlags &= ~(DDBLT_DDFX);
899 #define COPY_COLORKEY_FX(type) { \
900 type *s, *d = (type *) dbuf, *dx, tmp; \
901 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
902 s = (type*)(sbase + (sy >> 16) * sdesc.u1.lPitch); \
903 dx = d; \
904 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
905 tmp = s[sx >> 16]; \
906 if (tmp < keylow || tmp > keyhigh) dx[0] = tmp; \
907 dx = (type*)(((LPBYTE)dx)+dstxinc); \
909 d = (type*)(((LPBYTE)d)+dstyinc); \
911 break; }
913 switch (bpp) {
914 case 1: COPY_COLORKEY_FX(BYTE)
915 case 2: COPY_COLORKEY_FX(WORD)
916 case 4: COPY_COLORKEY_FX(DWORD)
917 case 3: {LPBYTE s,d = dbuf, dx;
918 for (y = sy = 0; y < dstheight; y++, sy += yinc) {
919 sbuf = sbase + (sy >> 16) * sdesc.u1.lPitch;
920 dx = d;
921 for (x = sx = 0; x < dstwidth; x++, sx+= xinc) {
922 DWORD pixel;
923 s = sbuf+3*(sx>>16);
924 pixel = s[0]|(s[1]<<8)|(s[2]<<16);
925 if (pixel < keylow || pixel > keyhigh){
926 dx[0] = (pixel )&0xff;
927 dx[1] = (pixel>> 8)&0xff;
928 dx[2] = (pixel>>16)&0xff;
930 dx+= dstxinc;
932 d += dstyinc;
934 break;}
935 default:
936 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
937 (dwFlags & DDBLT_KEYSRC) ? "Source" : "Destination", bpp*8);
938 ret = DDERR_UNSUPPORTED;
939 goto error;
940 #undef COPY_COLORKEY_FX
945 error:
946 if (dwFlags && FIXME_ON(ddraw)) {
947 FIXME("\tUnsupported flags: ");
948 DDRAW_dump_DDBLT(dwFlags);
951 release:
952 IDirectDrawSurface7_Unlock(iface,NULL);
953 if (src) IDirectDrawSurface7_Unlock(src,NULL);
954 return DD_OK;
957 /* BltBatch: generic, unimplemented */
959 HRESULT WINAPI
960 DIB_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
961 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
962 LPRECT rsrc, DWORD trans)
964 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
965 int bpp, w, h, x, y;
966 DDSURFACEDESC2 ddesc,sdesc;
967 HRESULT ret = DD_OK;
968 LPBYTE sbuf, dbuf;
969 RECT rsrc2;
970 RECT lock_src, lock_dst;
972 if (TRACE_ON(ddraw)) {
973 TRACE("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
974 This,dstx,dsty,src,rsrc,trans
976 TRACE("\ttrans:");
977 if (FIXME_ON(ddraw))
978 DDRAW_dump_DDBLTFAST(trans);
979 if (rsrc)
980 TRACE("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
981 else
982 TRACE(" srcrect: NULL\n");
985 /* First, check if the possible override function handles this case */
986 if (This->aux_bltfast != NULL) {
987 if (This->aux_bltfast(This, dstx, dsty, src, rsrc, trans) == DD_OK) return DD_OK;
990 /* Get the surface description without locking to first compute the width / height */
991 ddesc = This->surface_desc;
992 sdesc = (ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src))->surface_desc;
994 if (!rsrc) {
995 WARN("rsrc is NULL!\n");
996 rsrc = &rsrc2;
997 rsrc->left = rsrc->top = 0;
998 rsrc->right = sdesc.dwWidth;
999 rsrc->bottom = sdesc.dwHeight;
1002 h=rsrc->bottom-rsrc->top;
1003 if (h>ddesc.dwHeight-dsty) h=ddesc.dwHeight-dsty;
1004 if (h>sdesc.dwHeight-rsrc->top) h=sdesc.dwHeight-rsrc->top;
1005 if (h<=0) return DDERR_INVALIDRECT;
1007 w=rsrc->right-rsrc->left;
1008 if (w>ddesc.dwWidth-dstx) w=ddesc.dwWidth-dstx;
1009 if (w>sdesc.dwWidth-rsrc->left) w=sdesc.dwWidth-rsrc->left;
1010 if (w<=0) return DDERR_INVALIDRECT;
1012 /* Now compute the locking rectangle... */
1013 lock_src.left = rsrc->left;
1014 lock_src.top = rsrc->top;
1015 lock_src.right = lock_src.left + w;
1016 lock_src.bottom = lock_src.top + h;
1018 lock_dst.left = dstx;
1019 lock_dst.top = dsty;
1020 lock_dst.right = dstx + w;
1021 lock_dst.bottom = dsty + h;
1023 /* We need to lock the surfaces, or we won't get refreshes when done. */
1024 sdesc.dwSize = sizeof(sdesc);
1025 IDirectDrawSurface7_Lock(src, &lock_src, &sdesc, DDLOCK_READONLY, 0);
1026 ddesc.dwSize = sizeof(ddesc);
1027 IDirectDrawSurface7_Lock(iface, &lock_dst, &ddesc, DDLOCK_WRITEONLY, 0);
1029 /* Handle first the FOURCC surfaces... */
1030 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) && (ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
1031 if (trans)
1032 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1033 if (dstx || dsty)
1034 FIXME("offset for destination surface is not supported\n");
1035 if (sdesc.u4.ddpfPixelFormat.dwFourCC != sdesc.u4.ddpfPixelFormat.dwFourCC) {
1036 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1037 ret = DDERR_INVALIDPIXELFORMAT;
1038 goto error;
1040 memcpy(ddesc.lpSurface, sdesc.lpSurface, ddesc.u1.dwLinearSize);
1041 goto error;
1043 if ((sdesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) &&
1044 (!(ddesc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC))) {
1045 DoDXTCDecompression(&sdesc, &ddesc);
1046 goto error;
1049 bpp = GET_BPP(This->surface_desc);
1050 sbuf = (BYTE *) sdesc.lpSurface;
1051 dbuf = (BYTE *) ddesc.lpSurface;
1053 if (trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) {
1054 DWORD keylow, keyhigh;
1055 if (trans & DDBLTFAST_SRCCOLORKEY) {
1056 keylow = sdesc.ddckCKSrcBlt.dwColorSpaceLowValue;
1057 keyhigh = sdesc.ddckCKSrcBlt.dwColorSpaceHighValue;
1058 } else {
1059 /* I'm not sure if this is correct */
1060 FIXME("DDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1061 keylow = ddesc.ddckCKDestBlt.dwColorSpaceLowValue;
1062 keyhigh = ddesc.ddckCKDestBlt.dwColorSpaceHighValue;
1065 #define COPYBOX_COLORKEY(type) { \
1066 type *d, *s, tmp; \
1067 s = (type *) sdesc.lpSurface; \
1068 d = (type *) ddesc.lpSurface; \
1069 for (y = 0; y < h; y++) { \
1070 for (x = 0; x < w; x++) { \
1071 tmp = s[x]; \
1072 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1074 s = (type *)((BYTE *)s + sdesc.u1.lPitch); \
1075 d = (type *)((BYTE *)d + ddesc.u1.lPitch); \
1077 break; \
1080 switch (bpp) {
1081 case 1: COPYBOX_COLORKEY(BYTE)
1082 case 2: COPYBOX_COLORKEY(WORD)
1083 case 4: COPYBOX_COLORKEY(DWORD)
1084 case 3:
1086 BYTE *d, *s;
1087 DWORD tmp;
1088 s = (BYTE *) sdesc.lpSurface;
1089 d = (BYTE *) ddesc.lpSurface;
1090 for (y = 0; y < h; y++) {
1091 for (x = 0; x < w * 3; x += 3) {
1092 tmp = (DWORD)s[x] + ((DWORD)s[x + 1] << 8) + ((DWORD)s[x + 2] << 16);
1093 if (tmp < keylow || tmp > keyhigh) {
1094 d[x + 0] = s[x + 0];
1095 d[x + 1] = s[x + 1];
1096 d[x + 2] = s[x + 2];
1099 s += sdesc.u1.lPitch;
1100 d += ddesc.u1.lPitch;
1102 break;
1104 default:
1105 FIXME("Source color key blitting not supported for bpp %d\n",bpp*8);
1106 ret = DDERR_UNSUPPORTED;
1107 goto error;
1109 #undef COPYBOX_COLORKEY
1110 } else {
1111 int width = w * bpp;
1113 for (y = 0; y < h; y++) {
1114 memcpy(dbuf, sbuf, width);
1115 sbuf += sdesc.u1.lPitch;
1116 dbuf += ddesc.u1.lPitch;
1120 error:
1121 IDirectDrawSurface7_Unlock(iface, &lock_dst);
1122 IDirectDrawSurface7_Unlock(src, &lock_src);
1123 return ret;
1126 /* ChangeUniquenessValue: generic */
1127 /* DeleteAttachedSurface: generic */
1128 /* EnumAttachedSurfaces: generic */
1129 /* EnumOverlayZOrders: generic, unimplemented */
1131 BOOL DIB_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
1132 IDirectDrawSurfaceImpl* back,
1133 DWORD dwFlags)
1135 DIB_DirectDrawSurfaceImpl* front_priv = front->private;
1136 DIB_DirectDrawSurfaceImpl* back_priv = back->private;
1138 TRACE("(%p,%p)\n",front,back);
1141 HBITMAP tmp;
1142 tmp = front_priv->dib.DIBsection;
1143 front_priv->dib.DIBsection = back_priv->dib.DIBsection;
1144 back_priv->dib.DIBsection = tmp;
1148 void* tmp;
1149 tmp = front_priv->dib.bitmap_data;
1150 front_priv->dib.bitmap_data = back_priv->dib.bitmap_data;
1151 back_priv->dib.bitmap_data = tmp;
1153 tmp = front->surface_desc.lpSurface;
1154 front->surface_desc.lpSurface = back->surface_desc.lpSurface;
1155 back->surface_desc.lpSurface = tmp;
1158 /* client_memory should not be different, but just in case */
1160 BOOL tmp;
1161 tmp = front_priv->dib.client_memory;
1162 front_priv->dib.client_memory = back_priv->dib.client_memory;
1163 back_priv->dib.client_memory = tmp;
1166 return Main_DirectDrawSurface_flip_data(front, back, dwFlags);
1169 /* Flip: generic */
1170 /* FreePrivateData: generic */
1171 /* GetAttachedSurface: generic */
1172 /* GetBltStatus: generic */
1173 /* GetCaps: generic (Returns the caps from This->surface_desc.) */
1174 /* GetClipper: generic */
1175 /* GetColorKey: generic */
1177 HRESULT DIB_DirectDrawSurface_alloc_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1179 DIB_PRIV_VAR(priv, This);
1180 HDC hDC;
1182 TRACE("Grabbing a DC for surface: %p\n", This);
1184 hDC = CreateCompatibleDC(0);
1185 priv->dib.holdbitmap = SelectObject(hDC, priv->dib.DIBsection);
1186 if (This->palette)
1187 SelectPalette(hDC, This->palette->hpal, FALSE);
1189 *phDC = hDC;
1191 return S_OK;
1194 HRESULT DIB_DirectDrawSurface_free_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1196 DIB_PRIV_VAR(priv, This);
1198 TRACE("Releasing DC for surface: %p\n", This);
1200 SelectObject(hDC, priv->dib.holdbitmap);
1201 DeleteDC(hDC);
1203 return S_OK;
1206 HRESULT DIB_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
1208 return DIB_DirectDrawSurface_alloc_dc(This, phDC);
1211 HRESULT DIB_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This, HDC hDC)
1213 return DIB_DirectDrawSurface_free_dc(This, hDC);
1216 /* GetDDInterface: generic */
1217 /* GetFlipStatus: generic */
1218 /* GetLOD: generic */
1219 /* GetOverlayPosition: generic */
1220 /* GetPalette: generic */
1221 /* GetPixelFormat: generic */
1222 /* GetPriority: generic */
1223 /* GetPrivateData: generic */
1224 /* GetSurfaceDesc: generic */
1225 /* GetUniquenessValue: generic */
1226 /* Initialize: generic */
1227 /* IsLost: generic */
1228 /* Lock: generic with callback? */
1229 /* PageLock: generic */
1230 /* PageUnlock: generic */
1232 HRESULT WINAPI
1233 DIB_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
1235 TRACE("(%p)\n",iface);
1236 return DD_OK; /* ??? */
1239 /* SetClipper: generic */
1240 /* SetColorKey: generic */
1241 /* SetLOD: generic */
1242 /* SetOverlayPosition: generic */
1244 void DIB_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
1245 IDirectDrawPaletteImpl* pal)
1247 if (!pal) return;
1248 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1249 This->update_palette(This, pal,
1250 0, pal->palNumEntries,
1251 pal->palents);
1254 void DIB_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
1255 IDirectDrawPaletteImpl* pal,
1256 DWORD dwStart, DWORD dwCount,
1257 LPPALETTEENTRY palent)
1259 RGBQUAD col[256];
1260 unsigned int n;
1261 HDC dc;
1263 TRACE("updating primary palette\n");
1264 for (n=0; n<dwCount; n++) {
1265 col[n].rgbRed = palent[n].peRed;
1266 col[n].rgbGreen = palent[n].peGreen;
1267 col[n].rgbBlue = palent[n].peBlue;
1268 col[n].rgbReserved = 0;
1270 This->get_dc(This, &dc);
1271 SetDIBColorTable(dc, dwStart, dwCount, col);
1272 This->release_dc(This, dc);
1274 /* Propagate change to backbuffers if there are any */
1275 /* Basically this is a modification of the Flip code to find the backbuffer */
1276 /* and duplicate the palette update there as well */
1277 if ((This->surface_desc.ddsCaps.dwCaps&(DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1278 == (DDSCAPS_FLIP|DDSCAPS_FRONTBUFFER))
1280 static DDSCAPS2 back_caps = { DDSCAPS_BACKBUFFER };
1281 LPDIRECTDRAWSURFACE7 tgt;
1283 HRESULT hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This,IDirectDrawSurface7),
1284 &back_caps, &tgt);
1285 if (!FAILED(hr))
1287 IDirectDrawSurfaceImpl* target = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1288 IDirectDrawSurface7,tgt);
1289 IDirectDrawSurface7_Release(tgt);
1290 target->get_dc(target, &dc);
1291 SetDIBColorTable(dc, dwStart, dwCount, col);
1292 target->release_dc(target, dc);
1297 /* SetPalette: generic */
1298 /* SetPriority: generic */
1299 /* SetPrivateData: generic */
1301 HRESULT WINAPI
1302 DIB_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
1303 LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
1305 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1306 DIB_PRIV_VAR(priv, This);
1307 HRESULT hr = DD_OK;
1308 DWORD flags = pDDSD->dwFlags;
1310 if (TRACE_ON(ddraw)) {
1311 TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
1312 DDRAW_dump_surface_desc(pDDSD);
1315 if (pDDSD->dwFlags & DDSD_PIXELFORMAT) {
1316 flags &= ~DDSD_PIXELFORMAT;
1317 if (flags & DDSD_LPSURFACE) {
1318 This->surface_desc.u4.ddpfPixelFormat = pDDSD->u4.ddpfPixelFormat;
1319 } else {
1320 FIXME("Change of pixel format without surface re-allocation is not supported !\n");
1323 if (pDDSD->dwFlags & DDSD_LPSURFACE) {
1324 HBITMAP oldbmp = priv->dib.DIBsection;
1325 LPVOID oldsurf = This->surface_desc.lpSurface;
1326 BOOL oldc = priv->dib.client_memory;
1328 flags &= ~DDSD_LPSURFACE;
1330 TRACE("new lpSurface=%p\n",pDDSD->lpSurface);
1331 This->surface_desc.lpSurface = pDDSD->lpSurface;
1332 priv->dib.client_memory = TRUE;
1334 hr = create_dib(This);
1335 if (FAILED(hr))
1337 priv->dib.DIBsection = oldbmp;
1338 This->surface_desc.lpSurface = oldsurf;
1339 priv->dib.client_memory = oldc;
1340 return hr;
1343 DeleteObject(oldbmp);
1345 if (!oldc)
1346 VirtualFree(oldsurf, 0, MEM_RELEASE);
1348 if (flags) {
1349 WARN("Unhandled flags : %08lx\n", flags);
1351 return hr;
1354 /* Unlock: ???, need callback */
1355 /* UpdateOverlay: generic */
1356 /* UpdateOverlayDisplay: generic */
1357 /* UpdateOverlayZOrder: generic */
1359 static IDirectDrawSurface7Vtbl DIB_IDirectDrawSurface7_VTable =
1361 Main_DirectDrawSurface_QueryInterface,
1362 Main_DirectDrawSurface_AddRef,
1363 Main_DirectDrawSurface_Release,
1364 Main_DirectDrawSurface_AddAttachedSurface,
1365 Main_DirectDrawSurface_AddOverlayDirtyRect,
1366 DIB_DirectDrawSurface_Blt,
1367 Main_DirectDrawSurface_BltBatch,
1368 DIB_DirectDrawSurface_BltFast,
1369 Main_DirectDrawSurface_DeleteAttachedSurface,
1370 Main_DirectDrawSurface_EnumAttachedSurfaces,
1371 Main_DirectDrawSurface_EnumOverlayZOrders,
1372 Main_DirectDrawSurface_Flip,
1373 Main_DirectDrawSurface_GetAttachedSurface,
1374 Main_DirectDrawSurface_GetBltStatus,
1375 Main_DirectDrawSurface_GetCaps,
1376 Main_DirectDrawSurface_GetClipper,
1377 Main_DirectDrawSurface_GetColorKey,
1378 Main_DirectDrawSurface_GetDC,
1379 Main_DirectDrawSurface_GetFlipStatus,
1380 Main_DirectDrawSurface_GetOverlayPosition,
1381 Main_DirectDrawSurface_GetPalette,
1382 Main_DirectDrawSurface_GetPixelFormat,
1383 Main_DirectDrawSurface_GetSurfaceDesc,
1384 Main_DirectDrawSurface_Initialize,
1385 Main_DirectDrawSurface_IsLost,
1386 Main_DirectDrawSurface_Lock,
1387 Main_DirectDrawSurface_ReleaseDC,
1388 DIB_DirectDrawSurface_Restore,
1389 Main_DirectDrawSurface_SetClipper,
1390 Main_DirectDrawSurface_SetColorKey,
1391 Main_DirectDrawSurface_SetOverlayPosition,
1392 Main_DirectDrawSurface_SetPalette,
1393 Main_DirectDrawSurface_Unlock,
1394 Main_DirectDrawSurface_UpdateOverlay,
1395 Main_DirectDrawSurface_UpdateOverlayDisplay,
1396 Main_DirectDrawSurface_UpdateOverlayZOrder,
1397 Main_DirectDrawSurface_GetDDInterface,
1398 Main_DirectDrawSurface_PageLock,
1399 Main_DirectDrawSurface_PageUnlock,
1400 DIB_DirectDrawSurface_SetSurfaceDesc,
1401 Main_DirectDrawSurface_SetPrivateData,
1402 Main_DirectDrawSurface_GetPrivateData,
1403 Main_DirectDrawSurface_FreePrivateData,
1404 Main_DirectDrawSurface_GetUniquenessValue,
1405 Main_DirectDrawSurface_ChangeUniquenessValue,
1406 Main_DirectDrawSurface_SetPriority,
1407 Main_DirectDrawSurface_GetPriority,
1408 Main_DirectDrawSurface_SetLOD,
1409 Main_DirectDrawSurface_GetLOD