VirtualBox

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

最後變更 在這個檔案從39869是 39869,由 vboxsync 提交於 13 年 前

wine/d3d: proper habdling of context destruction and tls stuff

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

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