VirtualBox

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

Last change on this file since 39570 was 39486, checked in by vboxsync, 13 years ago

wine/xpdm: proper buffer allocation size

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

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