VirtualBox

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

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

wine/XPDM: 1. Additional swapchain creation fixes 2. De-libwine'ize wined3d 3. Single context per swapchain 4. wine & crOgl current context sync fixes 5. Proper Get/ReleaseDC handling

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

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