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"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32 /* if you use OWN_WINDOW, don't use SYNC_UPDATE, or you may get trouble */
33 /* #define SYNC_UPDATE */
35 * FIXME: This does not work any more because the created window has its own
36 * thread queue that cannot be manipulated by application threads.
41 static void User_create_own_window(IDirectDrawSurfaceImpl* This);
42 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This);
45 static DWORD CALLBACK User_update_thread(LPVOID);
47 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc);
49 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt);
51 static const IDirectDrawSurface7Vtbl User_IDirectDrawSurface7_VTable;
54 User_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
56 const DDSURFACEDESC2* pDDSD)
58 USER_PRIV_VAR(priv, This);
61 TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);
62 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
63 if (FAILED(hr)) return hr;
65 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
66 User_IDirectDrawSurface7_VTable);
68 This->final_release = User_DirectDrawSurface_final_release;
69 This->duplicate_surface = User_DirectDrawSurface_duplicate_surface;
71 This->lock_update = User_DirectDrawSurface_lock_update;
72 This->unlock_update = User_DirectDrawSurface_unlock_update;
74 This->flip_data = User_DirectDrawSurface_flip_data;
75 This->flip_update = User_DirectDrawSurface_flip_update;
77 This->get_dc = User_DirectDrawSurface_get_dc;
78 This->release_dc = User_DirectDrawSurface_release_dc;
80 This->set_palette = User_DirectDrawSurface_set_palette;
81 This->update_palette = User_DirectDrawSurface_update_palette;
83 This->get_gamma_ramp = User_DirectDrawSurface_get_gamma_ramp;
84 This->set_gamma_ramp = User_DirectDrawSurface_set_gamma_ramp;
86 This->get_display_window = User_DirectDrawSurface_get_display_window;
88 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
91 DirectDrawSurface_RegisterClass();
94 InitializeCriticalSection(&priv->user.crit);
95 priv->user.refresh_event = CreateEventW(NULL, TRUE, FALSE, NULL);
96 priv->user.update_event = CreateEventW(NULL, FALSE, FALSE, NULL);
97 priv->user.update_thread = CreateThread(NULL, 0, User_update_thread, This, 0, NULL);
99 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN) {
100 /* wait for window creation (or update thread destruction) */
101 while (WaitForMultipleObjects(1, &priv->user.update_thread, FALSE, 100) == WAIT_TIMEOUT)
102 if (This->more.lpDDRAWReserved) break;
103 if (!This->more.lpDDRAWReserved) {
104 ERR("window creation failed\n");
110 User_create_own_window(This);
113 if (!This->more.lpDDRAWReserved)
114 This->more.lpDDRAWReserved = (LPVOID)pDD->window;
117 return DIB_DirectDrawSurface_alloc_dc(This, &priv->user.cached_dc);
121 User_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
122 const DDSURFACEDESC2 *pDDSD,
123 LPDIRECTDRAWSURFACE7 *ppSurf,
126 IDirectDrawSurfaceImpl* This;
128 assert(pUnkOuter == NULL);
130 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
131 sizeof(*This) + sizeof(User_DirectDrawSurfaceImpl));
132 if (This == NULL) return E_OUTOFMEMORY;
134 This->private = (User_DirectDrawSurfaceImpl*)(This+1);
136 hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
138 HeapFree(GetProcessHeap(), 0, This);
140 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
145 void User_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
147 USER_PRIV_VAR(priv, This);
149 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
152 HANDLE event = priv->user.update_event;
153 priv->user.update_event = 0;
155 TRACE("waiting for update thread to terminate...\n");
157 /* dispatch any waiting sendmessages */
160 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
162 /* to avoid deadlocks, allow SendMessages from update thread
163 * through while we wait for it */
164 while (MsgWaitForMultipleObjects(1, &priv->user.update_thread, FALSE,
165 INFINITE, QS_SENDMESSAGE) == WAIT_OBJECT_0+1)
168 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
171 WaitForSingleObject(priv->user.update_thread,INFINITE);
173 TRACE("update thread terminated\n");
175 CloseHandle(priv->user.update_thread);
176 CloseHandle(priv->user.refresh_event);
177 DeleteCriticalSection(&priv->user.crit);
180 User_destroy_own_window(This);
183 This->more.lpDDRAWReserved = 0;
185 DirectDrawSurface_UnregisterClass();
188 DIB_DirectDrawSurface_free_dc(This, priv->user.cached_dc);
189 DIB_DirectDrawSurface_final_release(This);
192 static int User_DirectDrawSurface_init_wait(IDirectDrawSurfaceImpl* This)
194 USER_PRIV_VAR(priv, This);
196 EnterCriticalSection(&priv->user.crit);
197 priv->user.wait_count++;
198 need_wait = priv->user.in_refresh;
199 LeaveCriticalSection(&priv->user.crit);
203 static void User_DirectDrawSurface_end_wait(IDirectDrawSurfaceImpl* This)
205 USER_PRIV_VAR(priv, This);
206 EnterCriticalSection(&priv->user.crit);
207 if (!--priv->user.wait_count)
208 ResetEvent(priv->user.refresh_event);
209 LeaveCriticalSection(&priv->user.crit);
212 static void User_DirectDrawSurface_wait_update(IDirectDrawSurfaceImpl* This)
214 USER_PRIV_VAR(priv, This);
215 if (priv->user.in_refresh) {
216 if (User_DirectDrawSurface_init_wait(This))
217 WaitForSingleObject(priv->user.refresh_event, 2);
218 User_DirectDrawSurface_end_wait(This);
222 void User_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
223 LPCRECT pRect, DWORD dwFlags)
226 if (!(dwFlags & DDLOCK_WRITEONLY))
227 User_copy_from_screen(This, pRect);
229 if (dwFlags & DDLOCK_WAIT) User_DirectDrawSurface_wait_update(This);
232 This->lastlockrect = *pRect;
234 This->lastlockrect.left = This->lastlockrect.right = 0;
238 void User_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
241 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
244 User_copy_to_screen(This, pRect);
246 USER_PRIV_VAR(priv, This);
247 SetEvent(priv->user.update_event);
252 void User_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
253 IDirectDrawPaletteImpl* pal)
255 USER_PRIV_VAR(priv, This);
258 FIXME("selecting null palette\n");
259 /* I don't think selecting GDI object 0 will work, we should select
260 * the original palette, returned by the first SelectPalette */
261 SelectPalette(priv->user.cached_dc, 0, FALSE);
265 SelectPalette(priv->user.cached_dc, pal->hpal, FALSE);
267 DIB_DirectDrawSurface_set_palette(This, pal);
270 void User_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
271 IDirectDrawPaletteImpl* pal,
272 DWORD dwStart, DWORD dwCount,
273 LPPALETTEENTRY palent)
276 USER_PRIV_VAR(priv, This);
279 DIB_DirectDrawSurface_update_palette(This, pal, dwStart, dwCount, palent);
280 /* FIXME: realize palette on display window */
283 /* with async updates, it's probably cool to force an update */
284 SetEvent(priv->user.update_event);
288 HRESULT User_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
289 LPDIRECTDRAWSURFACE7* ppDup)
291 return User_DirectDrawSurface_Create(This->ddraw_owner,
292 &This->surface_desc, ppDup, NULL);
295 BOOL User_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
296 IDirectDrawSurfaceImpl* back,
299 USER_PRIV_VAR(front_priv, front);
300 USER_PRIV_VAR(back_priv, back);
304 tmp = front_priv->user.cached_dc;
305 front_priv->user.cached_dc = back_priv->user.cached_dc;
306 back_priv->user.cached_dc = tmp;
309 return DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
312 void User_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
315 This->lastlockrect.left = This->lastlockrect.right = 0;
316 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
317 User_copy_to_screen(This,NULL);
319 USER_PRIV_VAR(priv, This);
320 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
321 if (dwFlags & DDFLIP_WAIT) User_DirectDrawSurface_wait_update(This);
322 This->lastlockrect.left = This->lastlockrect.right = 0;
323 SetEvent(priv->user.update_event);
327 HRESULT User_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
329 USER_PRIV_VAR(priv, This);
331 *phDC = priv->user.cached_dc;
335 HRESULT User_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This,
341 HRESULT User_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl* This,
343 LPDDGAMMARAMP lpGammaRamp)
345 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
351 hDisplayWnd = get_display_window(This, &offset);
352 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
353 hr = GetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
354 ReleaseDC(hDisplayWnd, hDisplayDC);
357 return Main_DirectDrawSurface_get_gamma_ramp(This, dwFlags, lpGammaRamp);
360 HRESULT User_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl* This,
362 LPDDGAMMARAMP lpGammaRamp)
364 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
370 hDisplayWnd = get_display_window(This, &offset);
371 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
372 hr = SetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
373 ReleaseDC(hDisplayWnd, hDisplayDC);
376 return Main_DirectDrawSurface_set_gamma_ramp(This, dwFlags, lpGammaRamp);
379 /* Returns the window that hosts the display.
380 * *pt is set to the upper left position of the window relative to the
381 * upper left corner of the surface. */
382 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt)
384 memset(pt, 0, sizeof(*pt));
386 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
389 USER_PRIV_VAR(priv, This);
391 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
392 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
393 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE);
395 return priv->user.window;
397 return This->ddraw_owner->window;
402 if (This->clipper != NULL)
404 /* looks silly, but since we don't have the clipper locked */
405 HWND hWnd = This->clipper->hWnd;
409 ClientToScreen(hWnd, pt);
414 static BOOL warn = 0;
415 if (!warn++) FIXME("clipper clip lists not supported\n");
417 return GetDesktopWindow();
422 static BOOL warn = 0;
423 if (!warn++) WARN("hosting on root\n");
425 return GetDesktopWindow();
430 HWND User_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
433 return get_display_window(This, &offset);
437 static void User_create_own_window(IDirectDrawSurfaceImpl* This)
439 USER_PRIV_VAR(priv, This);
441 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
443 priv->user.window = CreateWindowExA(WS_EX_TOPMOST |
446 "WINE_DDRAW", "DirectDraw",
449 This->surface_desc.dwWidth,
450 This->surface_desc.dwHeight,
453 This->more.lpDDRAWReserved = (LPVOID)priv->user.window;
454 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
455 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
456 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_SHOWWINDOW);
457 UpdateWindow(priv->user.window);
461 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This)
463 USER_PRIV_VAR(priv, This);
465 if (priv->user.window)
467 SetWindowPos(priv->user.window, 0, 0, 0, 0, 0,
468 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOZORDER|
469 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_HIDEWINDOW);
470 This->more.lpDDRAWReserved = NULL;
471 DestroyWindow(priv->user.window);
472 priv->user.window = 0;
478 static DWORD CALLBACK User_update_thread(LPVOID arg)
480 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)arg;
481 USER_PRIV_VAR(priv, This);
482 volatile HANDLE *pActive = (volatile HANDLE *)&priv->user.update_event;
483 HANDLE event = *pActive;
486 User_create_own_window(This);
489 /* the point of this is that many games lock the primary surface
490 * multiple times per frame; this thread will then simply copy as
491 * often as it can without keeping the main thread waiting for
492 * each unlock, thus keeping the frame rate high */
495 DWORD ret = MsgWaitForMultipleObjects(1, &event, FALSE, INFINITE, QS_ALLINPUT);
498 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
500 switch (msg.message) {
502 DispatchMessageA(&msg);
505 /* since we risk getting keyboard messages posted to us,
506 * repost posted messages to cooperative window */
507 PostMessageA(This->ddraw_owner->window, msg.message, msg.wParam, msg.lParam);
511 DWORD ret = WaitForSingleObject(event, INFINITE);
513 if (ret == WAIT_OBJECT_0)
516 priv->user.in_refresh = TRUE;
517 User_copy_to_screen(This, NULL);
518 EnterCriticalSection(&priv->user.crit);
519 priv->user.in_refresh = FALSE;
520 if (priv->user.wait_count)
521 SetEvent(priv->user.refresh_event);
522 LeaveCriticalSection(&priv->user.crit);
526 else if (ret != WAIT_OBJECT_0+1) break;
529 SetEvent(priv->user.refresh_event);
531 User_destroy_own_window(This);
538 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
540 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
548 if (FAILED(This->get_dc(This, &hSurfaceDC)))
551 hDisplayWnd = get_display_window(This, &offset);
552 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
554 /* FIXME: this doesn't work... if users really want to run
555 * X in 8bpp, then we need to call directly into display.drv
556 * (or Wine's equivalent), and force a private colormap
557 * without default entries. */
559 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
560 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
564 drawrect.right = This->surface_desc.dwWidth;
566 drawrect.bottom = This->surface_desc.dwHeight;
570 HWND hwnd = This->clipper->hWnd;
571 if (hwnd && GetClientRect(hwnd,&xrc)) {
572 OffsetRect(&xrc,offset.x,offset.y);
573 IntersectRect(&drawrect,&drawrect,&xrc);
577 IntersectRect(&drawrect,&drawrect,rc);
579 /* Only use this if the caller did not pass a rectangle, since
580 * due to double locking this could be the wrong one ... */
581 if (This->lastlockrect.left != This->lastlockrect.right)
582 IntersectRect(&drawrect,&drawrect,&This->lastlockrect);
585 drawrect.left-offset.x, drawrect.top-offset.y,
586 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
588 drawrect.left, drawrect.top,
591 ReleaseDC(hDisplayWnd, hDisplayDC);
596 static void User_copy_from_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
598 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
601 HWND hDisplayWnd = get_display_window(This, &offset);
602 HDC hDisplayDC = GetDC(hDisplayWnd);
606 drawrect.right = This->surface_desc.dwWidth;
608 drawrect.bottom = This->surface_desc.dwHeight;
610 IntersectRect(&drawrect,&drawrect,rc);
613 drawrect.left, drawrect.top,
614 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
616 drawrect.left+offset.x, drawrect.top+offset.y,
619 ReleaseDC(hDisplayWnd, hDisplayDC);
624 static const IDirectDrawSurface7Vtbl User_IDirectDrawSurface7_VTable =
626 Main_DirectDrawSurface_QueryInterface,
627 Main_DirectDrawSurface_AddRef,
628 Main_DirectDrawSurface_Release,
629 Main_DirectDrawSurface_AddAttachedSurface,
630 Main_DirectDrawSurface_AddOverlayDirtyRect,
631 DIB_DirectDrawSurface_Blt,
632 Main_DirectDrawSurface_BltBatch,
633 DIB_DirectDrawSurface_BltFast,
634 Main_DirectDrawSurface_DeleteAttachedSurface,
635 Main_DirectDrawSurface_EnumAttachedSurfaces,
636 Main_DirectDrawSurface_EnumOverlayZOrders,
637 Main_DirectDrawSurface_Flip,
638 Main_DirectDrawSurface_GetAttachedSurface,
639 Main_DirectDrawSurface_GetBltStatus,
640 Main_DirectDrawSurface_GetCaps,
641 Main_DirectDrawSurface_GetClipper,
642 Main_DirectDrawSurface_GetColorKey,
643 Main_DirectDrawSurface_GetDC,
644 Main_DirectDrawSurface_GetFlipStatus,
645 Main_DirectDrawSurface_GetOverlayPosition,
646 Main_DirectDrawSurface_GetPalette,
647 Main_DirectDrawSurface_GetPixelFormat,
648 Main_DirectDrawSurface_GetSurfaceDesc,
649 Main_DirectDrawSurface_Initialize,
650 Main_DirectDrawSurface_IsLost,
651 Main_DirectDrawSurface_Lock,
652 Main_DirectDrawSurface_ReleaseDC,
653 DIB_DirectDrawSurface_Restore,
654 Main_DirectDrawSurface_SetClipper,
655 Main_DirectDrawSurface_SetColorKey,
656 Main_DirectDrawSurface_SetOverlayPosition,
657 Main_DirectDrawSurface_SetPalette,
658 Main_DirectDrawSurface_Unlock,
659 Main_DirectDrawSurface_UpdateOverlay,
660 Main_DirectDrawSurface_UpdateOverlayDisplay,
661 Main_DirectDrawSurface_UpdateOverlayZOrder,
662 Main_DirectDrawSurface_GetDDInterface,
663 Main_DirectDrawSurface_PageLock,
664 Main_DirectDrawSurface_PageUnlock,
665 DIB_DirectDrawSurface_SetSurfaceDesc,
666 Main_DirectDrawSurface_SetPrivateData,
667 Main_DirectDrawSurface_GetPrivateData,
668 Main_DirectDrawSurface_FreePrivateData,
669 Main_DirectDrawSurface_GetUniquenessValue,
670 Main_DirectDrawSurface_ChangeUniquenessValue,
671 Main_DirectDrawSurface_SetPriority,
672 Main_DirectDrawSurface_GetPriority,
673 Main_DirectDrawSurface_SetLOD,
674 Main_DirectDrawSurface_GetLOD