1 /* User-based primary surface driver
3 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/debug.h"
28 #include "ddraw_private.h"
29 #include "dsurface/main.h"
30 #include "dsurface/dib.h"
31 #include "dsurface/user.h"
32 #include "dsurface/wndproc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
36 /* if you use OWN_WINDOW, don't use SYNC_UPDATE, or you may get trouble */
37 /* #define SYNC_UPDATE */
39 * FIXME: This does not work any more because the created window has its own
40 * thread queue that cannot be manipulated by application threads.
45 static void User_create_own_window(IDirectDrawSurfaceImpl* This);
46 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This);
49 static DWORD CALLBACK User_update_thread(LPVOID);
51 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc);
53 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt);
55 static ICOM_VTABLE(IDirectDrawSurface7) User_IDirectDrawSurface7_VTable;
58 User_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
60 const DDSURFACEDESC2* pDDSD)
62 USER_PRIV_VAR(priv, This);
65 TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);
66 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
67 if (FAILED(hr)) return hr;
69 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
70 User_IDirectDrawSurface7_VTable);
72 This->final_release = User_DirectDrawSurface_final_release;
73 This->duplicate_surface = User_DirectDrawSurface_duplicate_surface;
75 This->lock_update = User_DirectDrawSurface_lock_update;
76 This->unlock_update = User_DirectDrawSurface_unlock_update;
78 This->flip_data = User_DirectDrawSurface_flip_data;
79 This->flip_update = User_DirectDrawSurface_flip_update;
81 This->get_dc = User_DirectDrawSurface_get_dc;
82 This->release_dc = User_DirectDrawSurface_release_dc;
84 This->set_palette = User_DirectDrawSurface_set_palette;
85 This->update_palette = User_DirectDrawSurface_update_palette;
87 This->get_gamma_ramp = User_DirectDrawSurface_get_gamma_ramp;
88 This->set_gamma_ramp = User_DirectDrawSurface_set_gamma_ramp;
90 This->get_display_window = User_DirectDrawSurface_get_display_window;
92 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
95 DirectDrawSurface_RegisterClass();
98 InitializeCriticalSection(&priv->user.crit);
99 priv->user.refresh_event = CreateEventA(NULL, TRUE, FALSE, NULL);
100 priv->user.update_event = CreateEventA(NULL, FALSE, FALSE, NULL);
101 priv->user.update_thread = CreateThread(NULL, 0, User_update_thread, This, 0, NULL);
103 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN) {
104 /* wait for window creation (or update thread destruction) */
105 while (WaitForMultipleObjects(1, &priv->user.update_thread, FALSE, 100) == WAIT_TIMEOUT)
106 if (This->more.lpDDRAWReserved) break;
107 if (!This->more.lpDDRAWReserved) {
108 ERR("window creation failed\n");
114 User_create_own_window(This);
117 if (!This->more.lpDDRAWReserved)
118 This->more.lpDDRAWReserved = (LPVOID)pDD->window;
121 return DIB_DirectDrawSurface_alloc_dc(This, &priv->user.cached_dc);
125 User_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
126 const DDSURFACEDESC2 *pDDSD,
127 LPDIRECTDRAWSURFACE7 *ppSurf,
130 IDirectDrawSurfaceImpl* This;
132 assert(pUnkOuter == NULL);
134 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
135 sizeof(*This) + sizeof(User_DirectDrawSurfaceImpl));
136 if (This == NULL) return E_OUTOFMEMORY;
138 This->private = (User_DirectDrawSurfaceImpl*)(This+1);
140 hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
142 HeapFree(GetProcessHeap(), 0, This);
144 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
149 void User_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
151 USER_PRIV_VAR(priv, This);
153 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
156 HANDLE event = priv->user.update_event;
157 priv->user.update_event = 0;
159 TRACE("waiting for update thread to terminate...\n");
161 /* dispatch any waiting sendmessages */
164 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
166 /* to avoid deadlocks, allow SendMessages from update thread
167 * through while we wait for it */
168 while (MsgWaitForMultipleObjects(1, &priv->user.update_thread, FALSE,
169 INFINITE, QS_SENDMESSAGE) == WAIT_OBJECT_0+1)
172 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
175 WaitForSingleObject(priv->user.update_thread,INFINITE);
177 TRACE("update thread terminated\n");
179 CloseHandle(priv->user.update_thread);
180 CloseHandle(priv->user.refresh_event);
181 DeleteCriticalSection(&priv->user.crit);
184 User_destroy_own_window(This);
187 This->more.lpDDRAWReserved = 0;
189 DirectDrawSurface_UnregisterClass();
192 DIB_DirectDrawSurface_free_dc(This, priv->user.cached_dc);
193 DIB_DirectDrawSurface_final_release(This);
196 static int User_DirectDrawSurface_init_wait(IDirectDrawSurfaceImpl* This)
198 USER_PRIV_VAR(priv, This);
200 EnterCriticalSection(&priv->user.crit);
201 priv->user.wait_count++;
202 need_wait = priv->user.in_refresh;
203 LeaveCriticalSection(&priv->user.crit);
207 static void User_DirectDrawSurface_end_wait(IDirectDrawSurfaceImpl* This)
209 USER_PRIV_VAR(priv, This);
210 EnterCriticalSection(&priv->user.crit);
211 if (!--priv->user.wait_count)
212 ResetEvent(priv->user.refresh_event);
213 LeaveCriticalSection(&priv->user.crit);
216 static void User_DirectDrawSurface_wait_update(IDirectDrawSurfaceImpl* This)
218 USER_PRIV_VAR(priv, This);
219 if (priv->user.in_refresh) {
220 if (User_DirectDrawSurface_init_wait(This))
221 WaitForSingleObject(priv->user.refresh_event, 2);
222 User_DirectDrawSurface_end_wait(This);
226 void User_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
227 LPCRECT pRect, DWORD dwFlags)
230 if (!(dwFlags & DDLOCK_WRITEONLY))
231 User_copy_from_screen(This, pRect);
233 if (dwFlags & DDLOCK_WAIT) User_DirectDrawSurface_wait_update(This);
236 This->lastlockrect = *pRect;
238 This->lastlockrect.left = This->lastlockrect.right = 0;
242 void User_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
245 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
248 User_copy_to_screen(This, pRect);
250 USER_PRIV_VAR(priv, This);
251 SetEvent(priv->user.update_event);
256 void User_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
257 IDirectDrawPaletteImpl* pal)
259 USER_PRIV_VAR(priv, This);
262 FIXME("selecting null palette\n");
263 /* I don't think selecting GDI object 0 will work, we should select
264 * the original palette, returned by the first SelectPalette */
265 SelectPalette(priv->user.cached_dc, 0, FALSE);
269 SelectPalette(priv->user.cached_dc, pal->hpal, FALSE);
271 DIB_DirectDrawSurface_set_palette(This, pal);
274 void User_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
275 IDirectDrawPaletteImpl* pal,
276 DWORD dwStart, DWORD dwCount,
277 LPPALETTEENTRY palent)
280 USER_PRIV_VAR(priv, This);
283 DIB_DirectDrawSurface_update_palette(This, pal, dwStart, dwCount, palent);
284 /* FIXME: realize palette on display window */
287 /* with async updates, it's probably cool to force an update */
288 SetEvent(priv->user.update_event);
292 HRESULT User_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
293 LPDIRECTDRAWSURFACE7* ppDup)
295 return User_DirectDrawSurface_Create(This->ddraw_owner,
296 &This->surface_desc, ppDup, NULL);
299 BOOL User_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
300 IDirectDrawSurfaceImpl* back,
303 USER_PRIV_VAR(front_priv, front);
304 USER_PRIV_VAR(back_priv, back);
308 tmp = front_priv->user.cached_dc;
309 front_priv->user.cached_dc = back_priv->user.cached_dc;
310 back_priv->user.cached_dc = tmp;
313 return DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
316 void User_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
319 This->lastlockrect.left = This->lastlockrect.right = 0;
320 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
321 User_copy_to_screen(This,NULL);
323 USER_PRIV_VAR(priv, This);
324 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
325 if (dwFlags & DDFLIP_WAIT) User_DirectDrawSurface_wait_update(This);
326 This->lastlockrect.left = This->lastlockrect.right = 0;
327 SetEvent(priv->user.update_event);
331 HRESULT User_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
333 USER_PRIV_VAR(priv, This);
335 *phDC = priv->user.cached_dc;
339 HRESULT User_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This,
345 HRESULT User_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl* This,
347 LPDDGAMMARAMP lpGammaRamp)
349 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
355 hDisplayWnd = get_display_window(This, &offset);
356 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
357 hr = GetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
358 ReleaseDC(hDisplayWnd, hDisplayDC);
361 return Main_DirectDrawSurface_get_gamma_ramp(This, dwFlags, lpGammaRamp);
364 HRESULT User_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl* This,
366 LPDDGAMMARAMP lpGammaRamp)
368 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
374 hDisplayWnd = get_display_window(This, &offset);
375 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
376 hr = SetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
377 ReleaseDC(hDisplayWnd, hDisplayDC);
380 return Main_DirectDrawSurface_set_gamma_ramp(This, dwFlags, lpGammaRamp);
383 /* Returns the window that hosts the display.
384 * *pt is set to the upper left position of the window relative to the
385 * upper left corner of the surface. */
386 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt)
388 memset(pt, 0, sizeof(*pt));
390 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
393 USER_PRIV_VAR(priv, This);
395 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
396 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
397 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE);
399 return priv->user.window;
401 return This->ddraw_owner->window;
406 if (This->clipper != NULL)
408 /* looks silly, but since we don't have the clipper locked */
409 HWND hWnd = This->clipper->hWnd;
413 ClientToScreen(hWnd, pt);
418 static BOOL warn = 0;
419 if (!warn++) FIXME("clipper clip lists not supported\n");
421 return GetDesktopWindow();
426 static BOOL warn = 0;
427 if (!warn++) WARN("hosting on root\n");
429 return GetDesktopWindow();
434 HWND User_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
437 return get_display_window(This, &offset);
441 static void User_create_own_window(IDirectDrawSurfaceImpl* This)
443 USER_PRIV_VAR(priv, This);
445 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
447 priv->user.window = CreateWindowExA(WS_EX_TOPMOST |
450 "WINE_DDRAW", "DirectDraw",
453 This->surface_desc.dwWidth,
454 This->surface_desc.dwHeight,
457 This->more.lpDDRAWReserved = (LPVOID)priv->user.window;
458 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
459 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
460 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_SHOWWINDOW);
461 UpdateWindow(priv->user.window);
465 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This)
467 USER_PRIV_VAR(priv, This);
469 if (priv->user.window)
471 SetWindowPos(priv->user.window, 0, 0, 0, 0, 0,
472 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOZORDER|
473 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_HIDEWINDOW);
474 This->more.lpDDRAWReserved = NULL;
475 DestroyWindow(priv->user.window);
476 priv->user.window = 0;
482 static DWORD CALLBACK User_update_thread(LPVOID arg)
484 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)arg;
485 USER_PRIV_VAR(priv, This);
486 volatile HANDLE *pActive = (volatile HANDLE *)&priv->user.update_event;
487 HANDLE event = *pActive;
490 User_create_own_window(This);
493 /* the point of this is that many games lock the primary surface
494 * multiple times per frame; this thread will then simply copy as
495 * often as it can without keeping the main thread waiting for
496 * each unlock, thus keeping the frame rate high */
499 DWORD ret = MsgWaitForMultipleObjects(1, &event, FALSE, INFINITE, QS_ALLINPUT);
502 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
504 switch (msg.message) {
506 DispatchMessageA(&msg);
509 /* since we risk getting keyboard messages posted to us,
510 * repost posted messages to cooperative window */
511 PostMessageA(This->ddraw_owner->window, msg.message, msg.wParam, msg.lParam);
515 DWORD ret = WaitForSingleObject(event, INFINITE);
517 if (ret == WAIT_OBJECT_0)
520 priv->user.in_refresh = TRUE;
521 User_copy_to_screen(This, NULL);
522 EnterCriticalSection(&priv->user.crit);
523 priv->user.in_refresh = FALSE;
524 if (priv->user.wait_count)
525 SetEvent(priv->user.refresh_event);
526 LeaveCriticalSection(&priv->user.crit);
530 else if (ret != WAIT_OBJECT_0+1) break;
533 SetEvent(priv->user.refresh_event);
535 User_destroy_own_window(This);
542 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
544 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
552 if (FAILED(This->get_dc(This, &hSurfaceDC)))
555 hDisplayWnd = get_display_window(This, &offset);
556 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
558 /* FIXME: this doesn't work... if users really want to run
559 * X in 8bpp, then we need to call directly into display.drv
560 * (or Wine's equivalent), and force a private colormap
561 * without default entries. */
563 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
564 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
568 drawrect.right = This->surface_desc.dwWidth;
570 drawrect.bottom = This->surface_desc.dwHeight;
574 HWND hwnd = This->clipper->hWnd;
575 if (hwnd && GetClientRect(hwnd,&xrc)) {
576 OffsetRect(&xrc,offset.x,offset.y);
577 IntersectRect(&drawrect,&drawrect,&xrc);
581 IntersectRect(&drawrect,&drawrect,rc);
583 /* Only use this if the caller did not pass a rectangle, since
584 * due to double locking this could be the wrong one ... */
585 if (This->lastlockrect.left != This->lastlockrect.right)
586 IntersectRect(&drawrect,&drawrect,&This->lastlockrect);
589 drawrect.left-offset.x, drawrect.top-offset.y,
590 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
592 drawrect.left, drawrect.top,
595 ReleaseDC(hDisplayWnd, hDisplayDC);
600 static void User_copy_from_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
602 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
605 HWND hDisplayWnd = get_display_window(This, &offset);
606 HDC hDisplayDC = GetDC(hDisplayWnd);
610 drawrect.right = This->surface_desc.dwWidth;
612 drawrect.bottom = This->surface_desc.dwHeight;
614 IntersectRect(&drawrect,&drawrect,rc);
617 drawrect.left, drawrect.top,
618 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
620 drawrect.left+offset.x, drawrect.top+offset.y,
623 ReleaseDC(hDisplayWnd, hDisplayDC);
628 static ICOM_VTABLE(IDirectDrawSurface7) User_IDirectDrawSurface7_VTable =
630 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
631 Main_DirectDrawSurface_QueryInterface,
632 Main_DirectDrawSurface_AddRef,
633 Main_DirectDrawSurface_Release,
634 Main_DirectDrawSurface_AddAttachedSurface,
635 Main_DirectDrawSurface_AddOverlayDirtyRect,
636 DIB_DirectDrawSurface_Blt,
637 Main_DirectDrawSurface_BltBatch,
638 DIB_DirectDrawSurface_BltFast,
639 Main_DirectDrawSurface_DeleteAttachedSurface,
640 Main_DirectDrawSurface_EnumAttachedSurfaces,
641 Main_DirectDrawSurface_EnumOverlayZOrders,
642 Main_DirectDrawSurface_Flip,
643 Main_DirectDrawSurface_GetAttachedSurface,
644 Main_DirectDrawSurface_GetBltStatus,
645 Main_DirectDrawSurface_GetCaps,
646 Main_DirectDrawSurface_GetClipper,
647 Main_DirectDrawSurface_GetColorKey,
648 Main_DirectDrawSurface_GetDC,
649 Main_DirectDrawSurface_GetFlipStatus,
650 Main_DirectDrawSurface_GetOverlayPosition,
651 Main_DirectDrawSurface_GetPalette,
652 Main_DirectDrawSurface_GetPixelFormat,
653 Main_DirectDrawSurface_GetSurfaceDesc,
654 Main_DirectDrawSurface_Initialize,
655 Main_DirectDrawSurface_IsLost,
656 Main_DirectDrawSurface_Lock,
657 Main_DirectDrawSurface_ReleaseDC,
658 DIB_DirectDrawSurface_Restore,
659 Main_DirectDrawSurface_SetClipper,
660 Main_DirectDrawSurface_SetColorKey,
661 Main_DirectDrawSurface_SetOverlayPosition,
662 Main_DirectDrawSurface_SetPalette,
663 Main_DirectDrawSurface_Unlock,
664 Main_DirectDrawSurface_UpdateOverlay,
665 Main_DirectDrawSurface_UpdateOverlayDisplay,
666 Main_DirectDrawSurface_UpdateOverlayZOrder,
667 Main_DirectDrawSurface_GetDDInterface,
668 Main_DirectDrawSurface_PageLock,
669 Main_DirectDrawSurface_PageUnlock,
670 DIB_DirectDrawSurface_SetSurfaceDesc,
671 Main_DirectDrawSurface_SetPrivateData,
672 Main_DirectDrawSurface_GetPrivateData,
673 Main_DirectDrawSurface_FreePrivateData,
674 Main_DirectDrawSurface_GetUniquenessValue,
675 Main_DirectDrawSurface_ChangeUniquenessValue,
676 Main_DirectDrawSurface_SetPriority,
677 Main_DirectDrawSurface_GetPriority,
678 Main_DirectDrawSurface_SetLOD,
679 Main_DirectDrawSurface_GetLOD