VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c@ 37608

最後變更 在這個檔案從37608是 37300,由 vboxsync 提交於 14 年 前

wddm: memory leaks fixes, some cleanup

  • 屬性 svn:eol-style 設為 native
檔案大小: 39.8 KB
 
1/*
2 *IDirect3DSwapChain9 implementation
3 *
4 *Copyright 2002-2003 Jason Edmeades
5 *Copyright 2002-2003 Raphael Junqueira
6 *Copyright 2005 Oliver Stieber
7 *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 *
9 *This library is free software; you can redistribute it and/or
10 *modify it under the terms of the GNU Lesser General Public
11 *License as published by the Free Software Foundation; either
12 *version 2.1 of the License, or (at your option) any later version.
13 *
14 *This library is distributed in the hope that it will be useful,
15 *but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 *Lesser General Public License for more details.
18 *
19 *You should have received a copy of the GNU Lesser General Public
20 *License along with this library; if not, write to the Free Software
21 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24/*
25 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
27 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
28 * a choice of LGPL license versions is made available with the language indicating
29 * that LGPLv2 or any later version may be used, or where a choice of which version
30 * of the LGPL is applied is otherwise unspecified.
31 */
32
33#include "config.h"
34#include "wined3d_private.h"
35
36
37/*TODO: some of the additional parameters may be required to
38 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
39 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
40
41
42WINE_DEFAULT_DEBUG_CHANNEL(d3d);
43WINE_DECLARE_DEBUG_CHANNEL(fps);
44
45#define GLINFO_LOCATION This->device->adapter->gl_info
46
47/*IWineD3DSwapChain parts follow: */
48static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface)
49{
50 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
51 WINED3DDISPLAYMODE mode;
52 unsigned int i;
53
54 TRACE("Destroying swapchain %p\n", iface);
55
56 IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
57
58 /* Release the swapchain's draw buffers. Make sure This->backBuffer[0] is
59 * the last buffer to be destroyed, FindContext() depends on that. */
60 if (This->frontBuffer)
61 {
62 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
63 if (IWineD3DSurface_Release(This->frontBuffer))
64 {
65 WARN("(%p) Something's still holding the front buffer (%p).\n",
66 This, This->frontBuffer);
67 }
68 This->frontBuffer = NULL;
69 }
70
71 if (This->backBuffer)
72 {
73 UINT i = This->presentParms.BackBufferCount;
74
75 while (i--)
76 {
77 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
78 if (IWineD3DSurface_Release(This->backBuffer[i]))
79 WARN("(%p) Something's still holding back buffer %u (%p).\n",
80 This, i, This->backBuffer[i]);
81 }
82 HeapFree(GetProcessHeap(), 0, This->backBuffer);
83 This->backBuffer = NULL;
84 }
85#ifndef VBOX_WITH_WDDM
86 for (i = 0; i < This->num_contexts; ++i)
87 {
88 context_destroy(This->device, This->context[i]);
89 }
90#else
91 IWineD3DDevice_RemoveSwapChain(This->device, This);
92 if (!This->device->NumberOfSwapChains)
93#endif
94 {
95 /* Restore the screen resolution if we rendered in fullscreen
96 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
97 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
98 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
99 */
100 if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
101 mode.Width = This->orig_width;
102 mode.Height = This->orig_height;
103 mode.RefreshRate = 0;
104 mode.Format = This->orig_fmt;
105 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)This->device, 0, &mode);
106 }
107 }
108#ifdef VBOX_WITH_WDDM
109 if(This->device_window) {
110 ReleaseDC(This->device_window, This->hDC);
111 }
112#else
113 HeapFree(GetProcessHeap(), 0, This->context);
114#endif
115 HeapFree(GetProcessHeap(), 0, This);
116}
117
118/* A GL context is provided by the caller */
119static void swapchain_blit(IWineD3DSwapChainImpl *This, struct wined3d_context *context,
120 const RECT *src_rect, const RECT *dst_rect)
121{
122 IWineD3DDeviceImpl *device = This->device;
123 IWineD3DSurfaceImpl *backbuffer = ((IWineD3DSurfaceImpl *) This->backBuffer[0]);
124 UINT src_w = src_rect->right - src_rect->left;
125 UINT src_h = src_rect->bottom - src_rect->top;
126 GLenum gl_filter;
127 const struct wined3d_gl_info *gl_info = context->gl_info;
128
129 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
130 This, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
131
132 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
133 gl_filter = GL_NEAREST;
134 else
135 gl_filter = GL_LINEAR;
136
137 if (0 && gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format_desc->color_fixup))
138 {
139 ENTER_GL();
140 context_bind_fbo(context, GL_READ_FRAMEBUFFER, &context->src_fbo);
141 context_attach_surface_fbo(context, GL_READ_FRAMEBUFFER, 0, backbuffer);
142 context_attach_depth_stencil_fbo(context, GL_READ_FRAMEBUFFER, NULL, FALSE);
143
144 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
145 context_set_draw_buffer(context, GL_BACK);
146
147 glDisable(GL_SCISSOR_TEST);
148 IWineD3DDeviceImpl_MarkStateDirty(This->device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
149
150 /* Note that the texture is upside down */
151 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
152 dst_rect->left, dst_rect->bottom, dst_rect->right, dst_rect->top,
153 GL_COLOR_BUFFER_BIT, gl_filter);
154 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
155 LEAVE_GL();
156 }
157 else
158 {
159 struct wined3d_context *context2;
160 float tex_left = src_rect->left;
161 float tex_top = src_rect->top;
162 float tex_right = src_rect->right;
163 float tex_bottom = src_rect->bottom;
164
165 context2 = context_acquire(This->device, This->backBuffer[0], CTXUSAGE_BLIT);
166
167 if(backbuffer->Flags & SFLAG_NORMCOORD)
168 {
169 tex_left /= src_w;
170 tex_right /= src_w;
171 tex_top /= src_h;
172 tex_bottom /= src_h;
173 }
174
175 if (is_complex_fixup(backbuffer->resource.format_desc->color_fixup))
176 gl_filter = GL_NEAREST;
177
178 ENTER_GL();
179 context_bind_fbo(context2, GL_DRAW_FRAMEBUFFER, NULL);
180
181 /* Set up the texture. The surface is not in a IWineD3D*Texture container,
182 * so there are no d3d texture settings to dirtify
183 */
184 device->blitter->set_shader((IWineD3DDevice *) device, backbuffer);
185 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
186 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
187
188 context_set_draw_buffer(context, GL_BACK);
189
190 /* Set the viewport to the destination rectandle, disable any projection
191 * transformation set up by CTXUSAGE_BLIT, and draw a (-1,-1)-(1,1) quad.
192 *
193 * Back up viewport and matrix to avoid breaking last_was_blit
194 *
195 * Note that CTXUSAGE_BLIT set up viewport and ortho to match the surface
196 * size - we want the GL drawable(=window) size.
197 */
198 glPushAttrib(GL_VIEWPORT_BIT);
199 glViewport(dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom);
200 glMatrixMode(GL_PROJECTION);
201 glPushMatrix();
202 glLoadIdentity();
203
204 glBegin(GL_QUADS);
205 /* bottom left */
206 glTexCoord2f(tex_left, tex_bottom);
207 glVertex2i(-1, -1);
208
209 /* top left */
210 glTexCoord2f(tex_left, tex_top);
211 glVertex2i(-1, 1);
212
213 /* top right */
214 glTexCoord2f(tex_right, tex_top);
215 glVertex2i(1, 1);
216
217 /* bottom right */
218 glTexCoord2f(tex_right, tex_bottom);
219 glVertex2i(1, -1);
220 glEnd();
221
222 glPopMatrix();
223 glPopAttrib();
224
225 device->blitter->unset_shader((IWineD3DDevice *) device);
226 checkGLcall("Swapchain present blit(manual)\n");
227 LEAVE_GL();
228
229 context_release(context2);
230 }
231}
232
233static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
234 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
235 struct wined3d_context *context;
236 RECT src_rect, dst_rect;
237 BOOL render_to_fbo;
238 unsigned int sync;
239 int retval;
240
241 IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
242
243 context = context_acquire(This->device, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
244 if (!context->valid)
245 {
246 context_release(context);
247 WARN("Invalid context, skipping present.\n");
248 return WINED3D_OK;
249 }
250
251 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
252 if (This->device->bCursorVisible && This->device->cursorTexture)
253 {
254 IWineD3DSurfaceImpl cursor;
255 RECT destRect =
256 {
257 This->device->xScreenSpace - This->device->xHotSpot,
258 This->device->yScreenSpace - This->device->yHotSpot,
259 This->device->xScreenSpace + This->device->cursorWidth - This->device->xHotSpot,
260 This->device->yScreenSpace + This->device->cursorHeight - This->device->yHotSpot,
261 };
262 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
263 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
264 * the application because we are only supposed to copy the information out. Using a fake surface
265 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
266 */
267 memset(&cursor, 0, sizeof(cursor));
268 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
269 cursor.resource.ref = 1;
270 cursor.resource.device = This->device;
271 cursor.resource.pool = WINED3DPOOL_SCRATCH;
272 cursor.resource.format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, context->gl_info);
273 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
274 cursor.texture_name = This->device->cursorTexture;
275 cursor.texture_target = GL_TEXTURE_2D;
276 cursor.texture_level = 0;
277 cursor.currentDesc.Width = This->device->cursorWidth;
278 cursor.currentDesc.Height = This->device->cursorHeight;
279 /* The cursor must have pow2 sizes */
280 cursor.pow2Width = cursor.currentDesc.Width;
281 cursor.pow2Height = cursor.currentDesc.Height;
282 /* The surface is in the texture */
283 cursor.Flags |= SFLAG_INTEXTURE;
284 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
285 * which is exactly what we want :-)
286 */
287 if (This->presentParms.Windowed) {
288 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
289 }
290 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *)&cursor,
291 NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
292 }
293
294 if (This->device->logo_surface)
295 {
296 /* Blit the logo into the upper left corner of the drawable. */
297 IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->device->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
298 }
299
300 TRACE("Presenting HDC %p.\n", context->hdc);
301
302 render_to_fbo = This->render_to_fbo;
303
304 if (pSourceRect)
305 {
306 src_rect = *pSourceRect;
307 if (!render_to_fbo && (src_rect.left || src_rect.top
308 || src_rect.right != This->presentParms.BackBufferWidth
309 || src_rect.bottom != This->presentParms.BackBufferHeight))
310 {
311 render_to_fbo = TRUE;
312 }
313 }
314 else
315 {
316 src_rect.left = 0;
317 src_rect.top = 0;
318 src_rect.right = This->presentParms.BackBufferWidth;
319 src_rect.bottom = This->presentParms.BackBufferHeight;
320 }
321
322 if (pDestRect) dst_rect = *pDestRect;
323 else GetClientRect(This->win_handle, &dst_rect);
324
325 if (!render_to_fbo && (dst_rect.left || dst_rect.top
326 || dst_rect.right != This->presentParms.BackBufferWidth
327 || dst_rect.bottom != This->presentParms.BackBufferHeight))
328 {
329 render_to_fbo = TRUE;
330 }
331
332 /* Rendering to a window of different size, presenting partial rectangles,
333 * or rendering to a different window needs help from FBO_blit or a textured
334 * draw. Render the swapchain to a FBO in the future.
335 *
336 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
337 * all these issues - this fails if the window is smaller than the backbuffer.
338 */
339 if (!This->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
340 {
341 IWineD3DSurface_LoadLocation(This->backBuffer[0], SFLAG_INTEXTURE, NULL);
342 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, FALSE);
343 This->render_to_fbo = TRUE;
344
345 /* Force the context manager to update the render target configuration next draw. */
346 context->current_rt = NULL;
347 }
348
349 if(This->render_to_fbo)
350 {
351 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
352 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
353 * not allowed(they need the COPY swapeffect)
354 *
355 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
356 * the swap
357 */
358 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP )
359 {
360 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
361 }
362
363 swapchain_blit(This, context, &src_rect, &dst_rect);
364 }
365
366#ifdef VBOX_WITH_WDDM
367 if (This->device->numContexts > 1) wglFinish();
368#else
369 if (This->num_contexts > 1) wglFinish();
370#endif
371
372#if defined(VBOX_WITH_WDDM) && defined(DEBUG)
373 {
374 HWND wnd = WindowFromDC(context->hdc);
375 Assert(context->currentSwapchain && context->win_handle==context->currentSwapchain->win_handle);
376 Assert(wnd==context->win_handle);
377 Assert(IsWindow(context->win_handle));
378
379 if (wnd != context->win_handle)
380 {
381 extern void context_update_window(struct wined3d_context *context, IWineD3DSwapChainImpl *swapchain);
382
383 context->valid = 0;
384 Assert(context->currentSwapchain == This);
385 context_update_window(context, context->currentSwapchain);
386 }
387 }
388#endif
389
390#ifdef VBOX_WITH_WDDM
391 /* We're directly using wglMakeCurrent calls skipping GDI layer, which causes GDI SwapBuffers to fail trying to
392 * call glFinish, which doesn't have any context set. So we use wglSwapLayerBuffers directly as well.
393 */
394 pwglSwapLayerBuffers(context->hdc, WGL_SWAP_MAIN_PLANE);
395#else
396 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
397#endif
398
399 TRACE("SwapBuffers called, Starting new frame\n");
400 /* FPS support */
401 if (TRACE_ON(fps))
402 {
403 DWORD time = GetTickCount();
404 This->frames++;
405 /* every 1.5 seconds */
406 if (time - This->prev_time > 1500) {
407 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
408 This->prev_time = time;
409 This->frames = 0;
410 }
411 }
412
413#if defined(FRAME_DEBUGGING)
414{
415 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
416 if (!isOn) {
417 isOn = TRUE;
418 FIXME("Enabling D3D Trace\n");
419 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
420#if defined(SHOW_FRAME_MAKEUP)
421 FIXME("Singe Frame snapshots Starting\n");
422 isDumpingFrames = TRUE;
423 ENTER_GL();
424 glClear(GL_COLOR_BUFFER_BIT);
425 LEAVE_GL();
426#endif
427
428#if defined(SINGLE_FRAME_DEBUGGING)
429 } else {
430#if defined(SHOW_FRAME_MAKEUP)
431 FIXME("Singe Frame snapshots Finishing\n");
432 isDumpingFrames = FALSE;
433#endif
434 FIXME("Singe Frame trace complete\n");
435 DeleteFileA("C:\\D3DTRACE");
436 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
437#endif
438 }
439 } else {
440 if (isOn) {
441 isOn = FALSE;
442#if defined(SHOW_FRAME_MAKEUP)
443 FIXME("Single Frame snapshots Finishing\n");
444 isDumpingFrames = FALSE;
445#endif
446 FIXME("Disabling D3D Trace\n");
447 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
448 }
449 }
450}
451#endif
452
453 /* This is disabled, but the code left in for debug purposes.
454 *
455 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
456 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
457 * The Debug runtime does the same on Windows. However, a few games do not redraw the
458 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
459 *
460 * Tests show that the content of the back buffer after a discard flip is indeed not
461 * reliable, so no game can depend on the exact content. However, it resembles the
462 * old contents in some way, for example by showing fragments at other locations. In
463 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
464 * gets a dark background image. If we clear it with a bright ugly color, the game's
465 * bug shows up much more than it does on Windows, and the players see single pixels
466 * with wrong colors.
467 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
468 */
469 if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
470 TRACE("Clearing the color buffer with cyan color\n");
471
472 IWineD3DDevice_Clear((IWineD3DDevice *)This->device, 0, NULL,
473 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
474 }
475
476 if(!This->render_to_fbo &&
477 ( ((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_INSYSMEM ||
478 ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) ) {
479 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
480 * Doesn't work with render_to_fbo because we're not flipping
481 */
482 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
483 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
484
485 if(front->resource.size == back->resource.size) {
486 DWORD fbflags;
487 flip_surface(front, back);
488
489 /* Tell the front buffer surface that is has been modified. However,
490 * the other locations were preserved during that, so keep the flags.
491 * This serves to update the emulated overlay, if any
492 */
493 fbflags = front->Flags;
494 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
495 front->Flags = fbflags;
496 } else {
497 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
498 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
499 }
500 } else {
501 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
502 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
503 * and INTEXTURE copies can keep their old content if they have any defined content.
504 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
505 * the texture / sysmem copy needs to be reloaded from the drawable
506 */
507 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP) {
508 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, TRUE);
509 }
510 }
511
512 if (This->device->stencilBufferTarget)
513 {
514 if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
515 || ((IWineD3DSurfaceImpl *)This->device->stencilBufferTarget)->Flags & SFLAG_DISCARD)
516 {
517 surface_modify_ds_location(This->device->stencilBufferTarget, SFLAG_DS_DISCARDED);
518 }
519 }
520
521 if (This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE
522 && context->gl_info->supported[SGI_VIDEO_SYNC])
523 {
524 retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
525 if(retval != 0) {
526 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
527 }
528
529 switch(This->presentParms.PresentationInterval) {
530 case WINED3DPRESENT_INTERVAL_DEFAULT:
531 case WINED3DPRESENT_INTERVAL_ONE:
532 if(sync <= This->vSyncCounter) {
533 retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
534 } else {
535 This->vSyncCounter = sync;
536 }
537 break;
538 case WINED3DPRESENT_INTERVAL_TWO:
539 if(sync <= This->vSyncCounter + 1) {
540 retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
541 } else {
542 This->vSyncCounter = sync;
543 }
544 break;
545 case WINED3DPRESENT_INTERVAL_THREE:
546 if(sync <= This->vSyncCounter + 2) {
547 retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
548 } else {
549 This->vSyncCounter = sync;
550 }
551 break;
552 case WINED3DPRESENT_INTERVAL_FOUR:
553 if(sync <= This->vSyncCounter + 3) {
554 retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
555 } else {
556 This->vSyncCounter = sync;
557 }
558 break;
559 default:
560 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
561 }
562 }
563
564 context_release(context);
565
566 TRACE("returning\n");
567 return WINED3D_OK;
568}
569
570static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window)
571{
572 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)iface;
573
574 if (!window) window = swapchain->device_window;
575 if (window == swapchain->win_handle) return WINED3D_OK;
576
577 TRACE("Setting swapchain %p window from %p to %p\n", swapchain, swapchain->win_handle, window);
578 swapchain->win_handle = window;
579
580 return WINED3D_OK;
581}
582
583static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
584{
585 /* IUnknown */
586 IWineD3DBaseSwapChainImpl_QueryInterface,
587 IWineD3DBaseSwapChainImpl_AddRef,
588 IWineD3DBaseSwapChainImpl_Release,
589 /* IWineD3DSwapChain */
590 IWineD3DBaseSwapChainImpl_GetParent,
591 IWineD3DSwapChainImpl_Destroy,
592 IWineD3DBaseSwapChainImpl_GetDevice,
593 IWineD3DSwapChainImpl_Present,
594 IWineD3DSwapChainImpl_SetDestWindowOverride,
595 IWineD3DBaseSwapChainImpl_GetFrontBufferData,
596 IWineD3DBaseSwapChainImpl_GetBackBuffer,
597 IWineD3DBaseSwapChainImpl_GetRasterStatus,
598 IWineD3DBaseSwapChainImpl_GetDisplayMode,
599 IWineD3DBaseSwapChainImpl_GetPresentParameters,
600 IWineD3DBaseSwapChainImpl_SetGammaRamp,
601 IWineD3DBaseSwapChainImpl_GetGammaRamp,
602};
603
604static LONG fullscreen_style(LONG style)
605{
606 /* Make sure the window is managed, otherwise we won't get keyboard input. */
607 style |= WS_POPUP | WS_SYSMENU;
608 style &= ~(WS_CAPTION | WS_THICKFRAME);
609
610 return style;
611}
612
613static LONG fullscreen_exstyle(LONG exstyle)
614{
615 /* Filter out window decorations. */
616 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
617
618 return exstyle;
619}
620
621void swapchain_setup_fullscreen_window(IWineD3DSwapChainImpl *swapchain, UINT w, UINT h)
622{
623 IWineD3DDeviceImpl *device = swapchain->device;
624 HWND window = swapchain->device_window;
625 BOOL filter_messages;
626 LONG style, exstyle;
627
628 TRACE("Setting up window %p for fullscreen mode.\n", window);
629
630 if (device->style || device->exStyle)
631 {
632 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
633 window, device->style, device->exStyle);
634 }
635
636 device->style = GetWindowLongW(window, GWL_STYLE);
637 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
638
639 style = fullscreen_style(device->style);
640 exstyle = fullscreen_exstyle(device->exStyle);
641
642 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
643 device->style, device->exStyle, style, exstyle);
644
645 filter_messages = device->filter_messages;
646 device->filter_messages = TRUE;
647
648 SetWindowLongW(window, GWL_STYLE, style);
649 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
650 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
651
652 device->filter_messages = filter_messages;
653}
654
655void swapchain_restore_fullscreen_window(IWineD3DSwapChainImpl *swapchain)
656{
657 IWineD3DDeviceImpl *device = swapchain->device;
658 HWND window = swapchain->device_window;
659 BOOL filter_messages;
660 LONG style, exstyle;
661
662 if (!device->style && !device->exStyle) return;
663
664 TRACE("Restoring window style of window %p to %08x, %08x.\n",
665 window, device->style, device->exStyle);
666
667 style = GetWindowLongW(window, GWL_STYLE);
668 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
669
670 filter_messages = device->filter_messages;
671 device->filter_messages = TRUE;
672
673 /* Only restore the style if the application didn't modify it during the
674 * fullscreen phase. Some applications change it before calling Reset()
675 * when switching between windowed and fullscreen modes (HL2), some
676 * depend on the original style (Eve Online). */
677 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
678 {
679 SetWindowLongW(window, GWL_STYLE, device->style);
680 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
681 }
682 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
683
684 device->filter_messages = filter_messages;
685
686 /* Delete the old values. */
687 device->style = 0;
688 device->exStyle = 0;
689}
690
691
692HRESULT swapchain_init(IWineD3DSwapChainImpl *swapchain, WINED3DSURFTYPE surface_type,
693 IWineD3DDeviceImpl *device, WINED3DPRESENT_PARAMETERS *present_parameters, IUnknown *parent)
694{
695 const struct wined3d_adapter *adapter = device->adapter;
696 const struct wined3d_format_desc *format_desc;
697 BOOL displaymode_set = FALSE;
698 WINED3DDISPLAYMODE mode;
699 RECT client_rect;
700 HWND window;
701 HRESULT hr;
702 UINT i;
703
704 if (present_parameters->BackBufferCount > WINED3DPRESENT_BACK_BUFFER_MAX)
705 {
706 FIXME("The application requested %u back buffers, this is not supported.\n",
707 present_parameters->BackBufferCount);
708 return WINED3DERR_INVALIDCALL;
709 }
710
711 if (present_parameters->BackBufferCount > 1)
712 {
713 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
714 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
715 }
716
717 switch (surface_type)
718 {
719 case SURFACE_GDI:
720 swapchain->lpVtbl = &IWineGDISwapChain_Vtbl;
721 break;
722
723 case SURFACE_OPENGL:
724 swapchain->lpVtbl = &IWineD3DSwapChain_Vtbl;
725 break;
726
727 case SURFACE_UNKNOWN:
728 FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
729 return WINED3DERR_INVALIDCALL;
730 }
731
732 window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
733
734 swapchain->device = device;
735 swapchain->parent = parent;
736 swapchain->ref = 1;
737 swapchain->win_handle = window;
738 swapchain->device_window = window;
739#ifdef VBOX_WITH_WDDM
740 Assert(window);
741 swapchain->hDC = GetDC(window);
742 if (!swapchain->hDC)
743 {
744 DWORD winEr = GetLastError();
745 WARN("Failed to get a window DC, winEr %d.\n", winEr);
746 Assert(0);
747 goto err;
748 }
749#endif
750
751 if (!present_parameters->Windowed && window)
752 {
753 swapchain_setup_fullscreen_window(swapchain, present_parameters->BackBufferWidth,
754 present_parameters->BackBufferHeight);
755 }
756
757 IWineD3D_GetAdapterDisplayMode(device->wined3d, adapter->ordinal, &mode);
758 swapchain->orig_width = mode.Width;
759 swapchain->orig_height = mode.Height;
760 swapchain->orig_fmt = mode.Format;
761 format_desc = getFormatDescEntry(mode.Format, &adapter->gl_info);
762
763 GetClientRect(window, &client_rect);
764 if (present_parameters->Windowed
765 && (!present_parameters->BackBufferWidth || !present_parameters->BackBufferHeight
766 || present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN))
767 {
768
769 if (!present_parameters->BackBufferWidth)
770 {
771 present_parameters->BackBufferWidth = client_rect.right;
772 TRACE("Updating width to %u.\n", present_parameters->BackBufferWidth);
773 }
774
775 if (!present_parameters->BackBufferHeight)
776 {
777 present_parameters->BackBufferHeight = client_rect.bottom;
778 TRACE("Updating height to %u.\n", present_parameters->BackBufferHeight);
779 }
780
781 if (present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN)
782 {
783 present_parameters->BackBufferFormat = swapchain->orig_fmt;
784 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
785 }
786 }
787 swapchain->presentParms = *present_parameters;
788
789 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
790 && present_parameters->BackBufferCount
791 && (present_parameters->BackBufferWidth != client_rect.right
792 || present_parameters->BackBufferHeight != client_rect.bottom))
793 {
794 TRACE("Rendering to FBO. Backbuffer %ux%u, window %ux%u.\n",
795 present_parameters->BackBufferWidth,
796 present_parameters->BackBufferHeight,
797 client_rect.right, client_rect.bottom);
798 swapchain->render_to_fbo = TRUE;
799 }
800
801 TRACE("Creating front buffer.\n");
802 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
803 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
804 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
805 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */, &swapchain->frontBuffer);
806 if (FAILED(hr))
807 {
808 WARN("Failed to create front buffer, hr %#x.\n", hr);
809 goto err;
810 }
811
812 IWineD3DSurface_SetContainer(swapchain->frontBuffer, (IWineD3DBase *)swapchain);
813 ((IWineD3DSurfaceImpl *)swapchain->frontBuffer)->Flags |= SFLAG_SWAPCHAIN;
814 if (surface_type == SURFACE_OPENGL)
815 {
816 IWineD3DSurface_ModifyLocation(swapchain->frontBuffer, SFLAG_INDRAWABLE, TRUE);
817 }
818
819 /* MSDN says we're only allowed a single fullscreen swapchain per device,
820 * so we should really check to see if there is a fullscreen swapchain
821 * already. Does a single head count as full screen? */
822
823 if (!present_parameters->Windowed)
824 {
825 WINED3DDISPLAYMODE mode;
826
827 /* Change the display settings */
828 mode.Width = present_parameters->BackBufferWidth;
829 mode.Height = present_parameters->BackBufferHeight;
830 mode.Format = present_parameters->BackBufferFormat;
831 mode.RefreshRate = present_parameters->FullScreen_RefreshRateInHz;
832
833 hr = IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)device, 0, &mode);
834 if (FAILED(hr))
835 {
836 WARN("Failed to set display mode, hr %#x.\n", hr);
837 goto err;
838 }
839 displaymode_set = TRUE;
840 }
841
842#ifndef VBOX_WITH_WDDM
843 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain->context));
844 if (!swapchain->context)
845 {
846 ERR("Failed to create the context array.\n");
847 hr = E_OUTOFMEMORY;
848 goto err;
849 }
850 swapchain->num_contexts = 1;
851#endif
852
853 if (surface_type == SURFACE_OPENGL)
854 {
855 struct wined3d_context * swapchainContext;
856 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
857
858 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
859 * You are able to add a depth + stencil surface at a later stage when you need it.
860 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
861 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
862 * context, need torecreate shaders, textures and other resources.
863 *
864 * The context manager already takes care of the state problem and for the other tasks code from Reset
865 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
866 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
867 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
868 * issue needs to be fixed. */
869 if (!present_parameters->EnableAutoDepthStencil
870 || swapchain->presentParms.AutoDepthStencilFormat != WINED3DFMT_D24_UNORM_S8_UINT)
871 {
872 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
873 }
874 swapchain->ds_format = getFormatDescEntry(WINED3DFMT_D24_UNORM_S8_UINT, gl_info);
875
876#ifdef VBOX_WITH_WDDM
877 swapchainContext = context_find_create(device, swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
878 swapchain->ds_format);
879 if (!swapchainContext)
880#else
881 swapchain->context[0] = context_create(swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
882 swapchain->ds_format);
883 if (!swapchain->context[0])
884#endif
885 {
886 WARN("Failed to create context.\n");
887 hr = WINED3DERR_NOTAVAILABLE;
888 goto err;
889 }
890#ifdef VBOX_WITH_WDDM
891 context_release(swapchainContext);
892#else
893 context_release(swapchain->context[0]);
894#endif
895 }
896 else
897 {
898#ifndef VBOX_WITH_WDDM
899 swapchain->context[0] = NULL;
900#endif
901 }
902
903 if (swapchain->presentParms.BackBufferCount > 0)
904 {
905 swapchain->backBuffer = HeapAlloc(GetProcessHeap(), 0,
906 sizeof(*swapchain->backBuffer) * swapchain->presentParms.BackBufferCount);
907 if (!swapchain->backBuffer)
908 {
909 ERR("Failed to allocate backbuffer array memory.\n");
910 hr = E_OUTOFMEMORY;
911 goto err;
912 }
913
914 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
915 {
916 TRACE("Creating back buffer %u.\n", i);
917 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
918 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
919 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
920 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */, &swapchain->backBuffer[i]);
921 if (FAILED(hr))
922 {
923 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
924 goto err;
925 }
926
927 IWineD3DSurface_SetContainer(swapchain->backBuffer[i], (IWineD3DBase *)swapchain);
928 ((IWineD3DSurfaceImpl *)swapchain->backBuffer[i])->Flags |= SFLAG_SWAPCHAIN;
929 }
930 }
931
932 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
933 if (present_parameters->EnableAutoDepthStencil && surface_type == SURFACE_OPENGL)
934 {
935 TRACE("Creating depth/stencil buffer.\n");
936 if (!device->auto_depth_stencil_buffer)
937 {
938 hr = IWineD3DDeviceParent_CreateDepthStencilSurface(device->device_parent, parent,
939 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
940 swapchain->presentParms.AutoDepthStencilFormat, swapchain->presentParms.MultiSampleType,
941 swapchain->presentParms.MultiSampleQuality, FALSE /* FIXME: Discard */,
942 &device->auto_depth_stencil_buffer);
943 if (FAILED(hr))
944 {
945 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
946 goto err;
947 }
948
949 IWineD3DSurface_SetContainer(device->auto_depth_stencil_buffer, NULL);
950 }
951 }
952
953 IWineD3DSwapChain_GetGammaRamp((IWineD3DSwapChain *)swapchain, &swapchain->orig_gamma);
954
955 return WINED3D_OK;
956
957err:
958 if (displaymode_set)
959 {
960 DEVMODEW devmode;
961
962 ClipCursor(NULL);
963
964 /* Change the display settings */
965 memset(&devmode, 0, sizeof(devmode));
966 devmode.dmSize = sizeof(devmode);
967 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
968 devmode.dmBitsPerPel = format_desc->byte_count * 8;
969 devmode.dmPelsWidth = swapchain->orig_width;
970 devmode.dmPelsHeight = swapchain->orig_height;
971 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
972 }
973
974 if (swapchain->backBuffer)
975 {
976 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
977 {
978 if (swapchain->backBuffer[i]) IWineD3DSurface_Release(swapchain->backBuffer[i]);
979 }
980 HeapFree(GetProcessHeap(), 0, swapchain->backBuffer);
981 }
982
983#ifdef VBOX_WITH_WDDM
984 if (!device->NumberOfSwapChains)
985 {
986 while (device->numContexts)
987 {
988 context_destroy(device, device->contexts[0]);
989 }
990 }
991#else
992 if (swapchain->context)
993 {
994 if (swapchain->context[0])
995 {
996 context_release(swapchain->context[0]);
997 context_destroy(device, swapchain->context[0]);
998 swapchain->num_contexts = 0;
999 }
1000 HeapFree(GetProcessHeap(), 0, swapchain->context);
1001 }
1002#endif
1003
1004 if (swapchain->frontBuffer) IWineD3DSurface_Release(swapchain->frontBuffer);
1005
1006 return hr;
1007}
1008
1009struct wined3d_context *swapchain_create_context_for_thread(IWineD3DSwapChain *iface)
1010{
1011 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
1012 struct wined3d_context **newArray;
1013 struct wined3d_context *ctx;
1014
1015 TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
1016
1017 if (!(ctx = context_create(This, (IWineD3DSurfaceImpl *)This->frontBuffer, This->ds_format)))
1018 {
1019 ERR("Failed to create a new context for the swapchain\n");
1020 return NULL;
1021 }
1022 context_release(ctx);
1023#ifdef VBOX_WITH_WDDM
1024 /* no need to do anything since context gets added to the device context list within the context_create call */
1025#else
1026 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
1027 if(!newArray) {
1028 ERR("Out of memory when trying to allocate a new context array\n");
1029 context_destroy(This->device, ctx);
1030 return NULL;
1031 }
1032 memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
1033 HeapFree(GetProcessHeap(), 0, This->context);
1034 newArray[This->num_contexts] = ctx;
1035 This->context = newArray;
1036 This->num_contexts++;
1037#endif
1038 TRACE("Returning context %p\n", ctx);
1039 return ctx;
1040}
1041
1042void get_drawable_size_swapchain(struct wined3d_context *context, UINT *width, UINT *height)
1043{
1044 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)context->current_rt;
1045 /* The drawable size of an onscreen drawable is the surface size.
1046 * (Actually: The window size, but the surface is created in window size) */
1047 *width = surface->currentDesc.Width;
1048 *height = surface->currentDesc.Height;
1049}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette