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
29 #include "wine/debug.h"
30 #include "ddraw_private.h"
31 #include "dsurface/main.h"
32 #include "dsurface/dib.h"
33 #include "dsurface/user.h"
34 #include "dsurface/wndproc.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38 /* if you use OWN_WINDOW, don't use SYNC_UPDATE, or you may get trouble */
39 /* #define SYNC_UPDATE */
41 * FIXME: This does not work any more because the created window has its own
42 * thread queue that cannot be manipulated by application threads.
47 static void User_create_own_window(IDirectDrawSurfaceImpl* This);
48 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This);
51 static DWORD CALLBACK User_update_thread(LPVOID);
53 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc);
55 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt);
57 static const IDirectDrawSurface7Vtbl User_IDirectDrawSurface7_VTable;
60 User_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
62 const DDSURFACEDESC2* pDDSD)
64 USER_PRIV_VAR(priv, This);
67 TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);
68 hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
69 if (FAILED(hr)) return hr;
71 ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
72 User_IDirectDrawSurface7_VTable);
74 This->final_release = User_DirectDrawSurface_final_release;
75 This->duplicate_surface = User_DirectDrawSurface_duplicate_surface;
77 This->lock_update = User_DirectDrawSurface_lock_update;
78 This->unlock_update = User_DirectDrawSurface_unlock_update;
80 This->flip_data = User_DirectDrawSurface_flip_data;
81 This->flip_update = User_DirectDrawSurface_flip_update;
83 This->get_dc = User_DirectDrawSurface_get_dc;
84 This->release_dc = User_DirectDrawSurface_release_dc;
86 This->set_palette = User_DirectDrawSurface_set_palette;
87 This->update_palette = User_DirectDrawSurface_update_palette;
89 This->get_gamma_ramp = User_DirectDrawSurface_get_gamma_ramp;
90 This->set_gamma_ramp = User_DirectDrawSurface_set_gamma_ramp;
92 This->get_display_window = User_DirectDrawSurface_get_display_window;
94 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
97 DirectDrawSurface_RegisterClass();
100 InitializeCriticalSection(&priv->user.crit);
101 priv->user.refresh_event = CreateEventW(NULL, TRUE, FALSE, NULL);
102 priv->user.update_event = CreateEventW(NULL, FALSE, FALSE, NULL);
103 priv->user.update_thread = CreateThread(NULL, 0, User_update_thread, This, 0, NULL);
105 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN) {
106 /* wait for window creation (or update thread destruction) */
107 while (WaitForMultipleObjects(1, &priv->user.update_thread, FALSE, 100) == WAIT_TIMEOUT)
108 if (This->more.lpDDRAWReserved) break;
109 if (!This->more.lpDDRAWReserved) {
110 ERR("window creation failed\n");
116 User_create_own_window(This);
119 if (!This->more.lpDDRAWReserved)
120 This->more.lpDDRAWReserved = (LPVOID)pDD->window;
123 return DIB_DirectDrawSurface_alloc_dc(This, &priv->user.cached_dc);
127 User_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
128 const DDSURFACEDESC2 *pDDSD,
129 LPDIRECTDRAWSURFACE7 *ppSurf,
132 IDirectDrawSurfaceImpl* This;
134 assert(pUnkOuter == NULL);
136 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
137 sizeof(*This) + sizeof(User_DirectDrawSurfaceImpl));
138 if (This == NULL) return E_OUTOFMEMORY;
140 This->private = (User_DirectDrawSurfaceImpl*)(This+1);
142 hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
144 HeapFree(GetProcessHeap(), 0, This);
146 *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
151 void User_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
153 USER_PRIV_VAR(priv, This);
155 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
158 HANDLE event = priv->user.update_event;
159 priv->user.update_event = 0;
161 TRACE("waiting for update thread to terminate...\n");
163 /* dispatch any waiting sendmessages */
166 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
168 /* to avoid deadlocks, allow SendMessages from update thread
169 * through while we wait for it */
170 while (MsgWaitForMultipleObjects(1, &priv->user.update_thread, FALSE,
171 INFINITE, QS_SENDMESSAGE) == WAIT_OBJECT_0+1)
174 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
177 WaitForSingleObject(priv->user.update_thread,INFINITE);
179 TRACE("update thread terminated\n");
181 CloseHandle(priv->user.update_thread);
182 CloseHandle(priv->user.refresh_event);
183 DeleteCriticalSection(&priv->user.crit);
186 User_destroy_own_window(This);
189 This->more.lpDDRAWReserved = 0;
191 DirectDrawSurface_UnregisterClass();
194 DIB_DirectDrawSurface_free_dc(This, priv->user.cached_dc);
195 DIB_DirectDrawSurface_final_release(This);
198 static int User_DirectDrawSurface_init_wait(IDirectDrawSurfaceImpl* This)
200 USER_PRIV_VAR(priv, This);
202 EnterCriticalSection(&priv->user.crit);
203 priv->user.wait_count++;
204 need_wait = priv->user.in_refresh;
205 LeaveCriticalSection(&priv->user.crit);
209 static void User_DirectDrawSurface_end_wait(IDirectDrawSurfaceImpl* This)
211 USER_PRIV_VAR(priv, This);
212 EnterCriticalSection(&priv->user.crit);
213 if (!--priv->user.wait_count)
214 ResetEvent(priv->user.refresh_event);
215 LeaveCriticalSection(&priv->user.crit);
218 static void User_DirectDrawSurface_wait_update(IDirectDrawSurfaceImpl* This)
220 USER_PRIV_VAR(priv, This);
221 if (priv->user.in_refresh) {
222 if (User_DirectDrawSurface_init_wait(This))
223 WaitForSingleObject(priv->user.refresh_event, 2);
224 User_DirectDrawSurface_end_wait(This);
228 void User_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
229 LPCRECT pRect, DWORD dwFlags)
232 if (!(dwFlags & DDLOCK_WRITEONLY))
233 User_copy_from_screen(This, pRect);
235 if (dwFlags & DDLOCK_WAIT) User_DirectDrawSurface_wait_update(This);
238 This->lastlockrect = *pRect;
240 This->lastlockrect.left = This->lastlockrect.right = 0;
244 void User_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
247 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
250 User_copy_to_screen(This, pRect);
252 USER_PRIV_VAR(priv, This);
253 SetEvent(priv->user.update_event);
258 void User_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
259 IDirectDrawPaletteImpl* pal)
261 USER_PRIV_VAR(priv, This);
264 FIXME("selecting null palette\n");
265 /* I don't think selecting GDI object 0 will work, we should select
266 * the original palette, returned by the first SelectPalette */
267 SelectPalette(priv->user.cached_dc, 0, FALSE);
271 SelectPalette(priv->user.cached_dc, pal->hpal, FALSE);
273 DIB_DirectDrawSurface_set_palette(This, pal);
276 void User_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl* This,
277 IDirectDrawPaletteImpl* pal,
278 DWORD dwStart, DWORD dwCount,
279 LPPALETTEENTRY palent)
282 USER_PRIV_VAR(priv, This);
285 DIB_DirectDrawSurface_update_palette(This, pal, dwStart, dwCount, palent);
286 /* FIXME: realize palette on display window */
289 /* with async updates, it's probably cool to force an update */
290 SetEvent(priv->user.update_event);
294 HRESULT User_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
295 LPDIRECTDRAWSURFACE7* ppDup)
297 return User_DirectDrawSurface_Create(This->ddraw_owner,
298 &This->surface_desc, ppDup, NULL);
301 BOOL User_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
302 IDirectDrawSurfaceImpl* back,
305 USER_PRIV_VAR(front_priv, front);
306 USER_PRIV_VAR(back_priv, back);
310 tmp = front_priv->user.cached_dc;
311 front_priv->user.cached_dc = back_priv->user.cached_dc;
312 back_priv->user.cached_dc = tmp;
315 return DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
318 void User_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
321 This->lastlockrect.left = This->lastlockrect.right = 0;
322 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
323 User_copy_to_screen(This,NULL);
325 USER_PRIV_VAR(priv, This);
326 assert(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE);
327 if (dwFlags & DDFLIP_WAIT) User_DirectDrawSurface_wait_update(This);
328 This->lastlockrect.left = This->lastlockrect.right = 0;
329 SetEvent(priv->user.update_event);
333 HRESULT User_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
335 USER_PRIV_VAR(priv, This);
337 *phDC = priv->user.cached_dc;
341 HRESULT User_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This,
347 HRESULT User_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl* This,
349 LPDDGAMMARAMP lpGammaRamp)
351 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
357 hDisplayWnd = get_display_window(This, &offset);
358 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
359 hr = GetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
360 ReleaseDC(hDisplayWnd, hDisplayDC);
363 return Main_DirectDrawSurface_get_gamma_ramp(This, dwFlags, lpGammaRamp);
366 HRESULT User_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl* This,
368 LPDDGAMMARAMP lpGammaRamp)
370 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
376 hDisplayWnd = get_display_window(This, &offset);
377 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
378 hr = SetDeviceGammaRamp(hDisplayDC, lpGammaRamp) ? DD_OK : DDERR_UNSUPPORTED;
379 ReleaseDC(hDisplayWnd, hDisplayDC);
382 return Main_DirectDrawSurface_set_gamma_ramp(This, dwFlags, lpGammaRamp);
385 /* Returns the window that hosts the display.
386 * *pt is set to the upper left position of the window relative to the
387 * upper left corner of the surface. */
388 static HWND get_display_window(IDirectDrawSurfaceImpl* This, LPPOINT pt)
390 memset(pt, 0, sizeof(*pt));
392 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
395 USER_PRIV_VAR(priv, This);
397 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
398 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
399 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE);
401 return priv->user.window;
403 return This->ddraw_owner->window;
408 if (This->clipper != NULL)
410 /* looks silly, but since we don't have the clipper locked */
411 HWND hWnd = This->clipper->hWnd;
415 ClientToScreen(hWnd, pt);
420 static BOOL warn = 0;
421 if (!warn++) FIXME("clipper clip lists not supported\n");
423 return GetDesktopWindow();
428 static BOOL warn = 0;
429 if (!warn++) WARN("hosting on root\n");
431 return GetDesktopWindow();
436 HWND User_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
439 return get_display_window(This, &offset);
443 static void User_create_own_window(IDirectDrawSurfaceImpl* This)
445 USER_PRIV_VAR(priv, This);
447 if (This->ddraw_owner->cooperative_level & DDSCL_FULLSCREEN)
449 priv->user.window = CreateWindowExA(WS_EX_TOPMOST |
452 "WINE_DDRAW", "DirectDraw",
455 This->surface_desc.dwWidth,
456 This->surface_desc.dwHeight,
459 This->more.lpDDRAWReserved = (LPVOID)priv->user.window;
460 SetWindowPos(priv->user.window, HWND_TOP, 0, 0, 0, 0,
461 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|
462 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_SHOWWINDOW);
463 UpdateWindow(priv->user.window);
467 static void User_destroy_own_window(IDirectDrawSurfaceImpl* This)
469 USER_PRIV_VAR(priv, This);
471 if (priv->user.window)
473 SetWindowPos(priv->user.window, 0, 0, 0, 0, 0,
474 SWP_DEFERERASE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOZORDER|
475 SWP_NOREDRAW|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_HIDEWINDOW);
476 This->more.lpDDRAWReserved = NULL;
477 DestroyWindow(priv->user.window);
478 priv->user.window = 0;
484 static DWORD CALLBACK User_update_thread(LPVOID arg)
486 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)arg;
487 USER_PRIV_VAR(priv, This);
488 volatile HANDLE *pActive = (volatile HANDLE *)&priv->user.update_event;
489 HANDLE event = *pActive;
492 User_create_own_window(This);
495 /* the point of this is that many games lock the primary surface
496 * multiple times per frame; this thread will then simply copy as
497 * often as it can without keeping the main thread waiting for
498 * each unlock, thus keeping the frame rate high */
501 DWORD ret = MsgWaitForMultipleObjects(1, &event, FALSE, INFINITE, QS_ALLINPUT);
504 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
506 switch (msg.message) {
508 DispatchMessageA(&msg);
511 /* since we risk getting keyboard messages posted to us,
512 * repost posted messages to cooperative window */
513 PostMessageA(This->ddraw_owner->window, msg.message, msg.wParam, msg.lParam);
517 DWORD ret = WaitForSingleObject(event, INFINITE);
519 if (ret == WAIT_OBJECT_0)
522 priv->user.in_refresh = TRUE;
523 User_copy_to_screen(This, NULL);
524 EnterCriticalSection(&priv->user.crit);
525 priv->user.in_refresh = FALSE;
526 if (priv->user.wait_count)
527 SetEvent(priv->user.refresh_event);
528 LeaveCriticalSection(&priv->user.crit);
532 else if (ret != WAIT_OBJECT_0+1) break;
535 SetEvent(priv->user.refresh_event);
537 User_destroy_own_window(This);
544 static void User_copy_to_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
546 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
554 if (FAILED(This->get_dc(This, &hSurfaceDC)))
557 hDisplayWnd = get_display_window(This, &offset);
558 hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
560 /* FIXME: this doesn't work... if users really want to run
561 * X in 8bpp, then we need to call directly into display.drv
562 * (or Wine's equivalent), and force a private colormap
563 * without default entries. */
565 SelectPalette(hDisplayDC, This->palette->hpal, FALSE);
566 RealizePalette(hDisplayDC); /* sends messages => deadlocks */
570 drawrect.right = This->surface_desc.dwWidth;
572 drawrect.bottom = This->surface_desc.dwHeight;
576 HWND hwnd = This->clipper->hWnd;
577 if (hwnd && GetClientRect(hwnd,&xrc)) {
578 OffsetRect(&xrc,offset.x,offset.y);
579 IntersectRect(&drawrect,&drawrect,&xrc);
583 IntersectRect(&drawrect,&drawrect,rc);
585 /* Only use this if the caller did not pass a rectangle, since
586 * due to double locking this could be the wrong one ... */
587 if (This->lastlockrect.left != This->lastlockrect.right)
588 IntersectRect(&drawrect,&drawrect,&This->lastlockrect);
591 drawrect.left-offset.x, drawrect.top-offset.y,
592 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
594 drawrect.left, drawrect.top,
597 ReleaseDC(hDisplayWnd, hDisplayDC);
602 static void User_copy_from_screen(IDirectDrawSurfaceImpl* This, LPCRECT rc)
604 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
607 HWND hDisplayWnd = get_display_window(This, &offset);
608 HDC hDisplayDC = GetDC(hDisplayWnd);
612 drawrect.right = This->surface_desc.dwWidth;
614 drawrect.bottom = This->surface_desc.dwHeight;
616 IntersectRect(&drawrect,&drawrect,rc);
619 drawrect.left, drawrect.top,
620 drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
622 drawrect.left+offset.x, drawrect.top+offset.y,
625 ReleaseDC(hDisplayWnd, hDisplayDC);
630 static const IDirectDrawSurface7Vtbl User_IDirectDrawSurface7_VTable =
632 Main_DirectDrawSurface_QueryInterface,
633 Main_DirectDrawSurface_AddRef,
634 Main_DirectDrawSurface_Release,
635 Main_DirectDrawSurface_AddAttachedSurface,
636 Main_DirectDrawSurface_AddOverlayDirtyRect,
637 DIB_DirectDrawSurface_Blt,
638 Main_DirectDrawSurface_BltBatch,
639 DIB_DirectDrawSurface_BltFast,
640 Main_DirectDrawSurface_DeleteAttachedSurface,
641 Main_DirectDrawSurface_EnumAttachedSurfaces,
642 Main_DirectDrawSurface_EnumOverlayZOrders,
643 Main_DirectDrawSurface_Flip,
644 Main_DirectDrawSurface_GetAttachedSurface,
645 Main_DirectDrawSurface_GetBltStatus,
646 Main_DirectDrawSurface_GetCaps,
647 Main_DirectDrawSurface_GetClipper,
648 Main_DirectDrawSurface_GetColorKey,
649 Main_DirectDrawSurface_GetDC,
650 Main_DirectDrawSurface_GetFlipStatus,
651 Main_DirectDrawSurface_GetOverlayPosition,
652 Main_DirectDrawSurface_GetPalette,
653 Main_DirectDrawSurface_GetPixelFormat,
654 Main_DirectDrawSurface_GetSurfaceDesc,
655 Main_DirectDrawSurface_Initialize,
656 Main_DirectDrawSurface_IsLost,
657 Main_DirectDrawSurface_Lock,
658 Main_DirectDrawSurface_ReleaseDC,
659 DIB_DirectDrawSurface_Restore,
660 Main_DirectDrawSurface_SetClipper,
661 Main_DirectDrawSurface_SetColorKey,
662 Main_DirectDrawSurface_SetOverlayPosition,
663 Main_DirectDrawSurface_SetPalette,
664 Main_DirectDrawSurface_Unlock,
665 Main_DirectDrawSurface_UpdateOverlay,
666 Main_DirectDrawSurface_UpdateOverlayDisplay,
667 Main_DirectDrawSurface_UpdateOverlayZOrder,
668 Main_DirectDrawSurface_GetDDInterface,
669 Main_DirectDrawSurface_PageLock,
670 Main_DirectDrawSurface_PageUnlock,
671 DIB_DirectDrawSurface_SetSurfaceDesc,
672 Main_DirectDrawSurface_SetPrivateData,
673 Main_DirectDrawSurface_GetPrivateData,
674 Main_DirectDrawSurface_FreePrivateData,
675 Main_DirectDrawSurface_GetUniquenessValue,
676 Main_DirectDrawSurface_ChangeUniquenessValue,
677 Main_DirectDrawSurface_SetPriority,
678 Main_DirectDrawSurface_GetPriority,
679 Main_DirectDrawSurface_SetLOD,
680 Main_DirectDrawSurface_GetLOD