wineconsole: Use the explicit W types/functions in dialog.c.
[wine.git] / dlls / wined3d / swapchain.c
blobe1cb2ad74a2dded7abc043f4cab2814ad9506935
1 /*
2 *IDirect3DSwapChain9 implementation
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
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.
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.
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
24 #include "config.h"
25 #include "wined3d_private.h"
28 /*TODO: some of the additional parameters may be required to
29 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
30 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 WINE_DECLARE_DEBUG_CHANNEL(fps);
36 #define GLINFO_LOCATION This->device->adapter->gl_info
38 /*IWineD3DSwapChain parts follow: */
39 static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface)
41 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
42 WINED3DDISPLAYMODE mode;
43 unsigned int i;
45 TRACE("Destroying swapchain %p\n", iface);
47 IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
49 /* Release the swapchain's draw buffers. Make sure This->back_buffers[0] is
50 * the last buffer to be destroyed, FindContext() depends on that. */
51 if (This->front_buffer)
53 IWineD3DSurface_SetContainer((IWineD3DSurface *)This->front_buffer, NULL);
54 if (IWineD3DSurface_Release((IWineD3DSurface *)This->front_buffer))
56 WARN("(%p) Something's still holding the front buffer (%p).\n",
57 This, This->front_buffer);
59 This->front_buffer = NULL;
62 if (This->back_buffers)
64 UINT i = This->presentParms.BackBufferCount;
66 while (i--)
68 IWineD3DSurface_SetContainer((IWineD3DSurface *)This->back_buffers[i], NULL);
69 if (IWineD3DSurface_Release((IWineD3DSurface *)This->back_buffers[i]))
70 WARN("(%p) Something's still holding back buffer %u (%p).\n",
71 This, i, This->back_buffers[i]);
73 HeapFree(GetProcessHeap(), 0, This->back_buffers);
74 This->back_buffers = NULL;
77 for (i = 0; i < This->num_contexts; ++i)
79 context_destroy(This->device, This->context[i]);
81 /* Restore the screen resolution if we rendered in fullscreen
82 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
83 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
84 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
86 if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
87 mode.Width = This->orig_width;
88 mode.Height = This->orig_height;
89 mode.RefreshRate = 0;
90 mode.Format = This->orig_fmt;
91 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)This->device, 0, &mode);
94 HeapFree(GetProcessHeap(), 0, This->context);
95 HeapFree(GetProcessHeap(), 0, This);
98 /* A GL context is provided by the caller */
99 static void swapchain_blit(IWineD3DSwapChainImpl *This, struct wined3d_context *context,
100 const RECT *src_rect, const RECT *dst_rect)
102 IWineD3DDeviceImpl *device = This->device;
103 IWineD3DSurfaceImpl *backbuffer = This->back_buffers[0];
104 UINT src_w = src_rect->right - src_rect->left;
105 UINT src_h = src_rect->bottom - src_rect->top;
106 GLenum gl_filter;
107 const struct wined3d_gl_info *gl_info = context->gl_info;
109 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
110 This, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
112 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
113 gl_filter = GL_NEAREST;
114 else
115 gl_filter = GL_LINEAR;
117 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format_desc->color_fixup))
119 ENTER_GL();
120 context_bind_fbo(context, GL_READ_FRAMEBUFFER, &context->src_fbo);
121 context_attach_surface_fbo(context, GL_READ_FRAMEBUFFER, 0, backbuffer);
122 context_attach_depth_stencil_fbo(context, GL_READ_FRAMEBUFFER, NULL, FALSE);
124 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
125 context_set_draw_buffer(context, GL_BACK);
127 glDisable(GL_SCISSOR_TEST);
128 IWineD3DDeviceImpl_MarkStateDirty(This->device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
130 /* Note that the texture is upside down */
131 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
132 dst_rect->left, dst_rect->bottom, dst_rect->right, dst_rect->top,
133 GL_COLOR_BUFFER_BIT, gl_filter);
134 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
135 LEAVE_GL();
137 else
139 struct wined3d_context *context2;
140 float tex_left = src_rect->left;
141 float tex_top = src_rect->top;
142 float tex_right = src_rect->right;
143 float tex_bottom = src_rect->bottom;
145 context2 = context_acquire(This->device, This->back_buffers[0], CTXUSAGE_BLIT);
147 if(backbuffer->Flags & SFLAG_NORMCOORD)
149 tex_left /= src_w;
150 tex_right /= src_w;
151 tex_top /= src_h;
152 tex_bottom /= src_h;
155 if (is_complex_fixup(backbuffer->resource.format_desc->color_fixup))
156 gl_filter = GL_NEAREST;
158 ENTER_GL();
159 context_bind_fbo(context2, GL_DRAW_FRAMEBUFFER, NULL);
161 /* Set up the texture. The surface is not in a IWineD3D*Texture container,
162 * so there are no d3d texture settings to dirtify
164 device->blitter->set_shader((IWineD3DDevice *) device, backbuffer);
165 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
166 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
168 context_set_draw_buffer(context, GL_BACK);
170 /* Set the viewport to the destination rectandle, disable any projection
171 * transformation set up by CTXUSAGE_BLIT, and draw a (-1,-1)-(1,1) quad.
173 * Back up viewport and matrix to avoid breaking last_was_blit
175 * Note that CTXUSAGE_BLIT set up viewport and ortho to match the surface
176 * size - we want the GL drawable(=window) size.
178 glPushAttrib(GL_VIEWPORT_BIT);
179 glViewport(dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom);
180 glMatrixMode(GL_PROJECTION);
181 glPushMatrix();
182 glLoadIdentity();
184 glBegin(GL_QUADS);
185 /* bottom left */
186 glTexCoord2f(tex_left, tex_bottom);
187 glVertex2i(-1, -1);
189 /* top left */
190 glTexCoord2f(tex_left, tex_top);
191 glVertex2i(-1, 1);
193 /* top right */
194 glTexCoord2f(tex_right, tex_top);
195 glVertex2i(1, 1);
197 /* bottom right */
198 glTexCoord2f(tex_right, tex_bottom);
199 glVertex2i(1, -1);
200 glEnd();
202 glPopMatrix();
203 glPopAttrib();
205 device->blitter->unset_shader((IWineD3DDevice *) device);
206 checkGLcall("Swapchain present blit(manual)\n");
207 LEAVE_GL();
209 context_release(context2);
213 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
214 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
215 struct wined3d_context *context;
216 RECT src_rect, dst_rect;
217 BOOL render_to_fbo;
218 unsigned int sync;
219 int retval;
221 IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
223 context = context_acquire(This->device, This->back_buffers[0], CTXUSAGE_RESOURCELOAD);
224 if (!context->valid)
226 context_release(context);
227 WARN("Invalid context, skipping present.\n");
228 return WINED3D_OK;
231 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
232 if (This->device->bCursorVisible && This->device->cursorTexture)
234 IWineD3DSurfaceImpl cursor;
235 RECT destRect =
237 This->device->xScreenSpace - This->device->xHotSpot,
238 This->device->yScreenSpace - This->device->yHotSpot,
239 This->device->xScreenSpace + This->device->cursorWidth - This->device->xHotSpot,
240 This->device->yScreenSpace + This->device->cursorHeight - This->device->yHotSpot,
242 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
243 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
244 * the application because we are only supposed to copy the information out. Using a fake surface
245 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
247 memset(&cursor, 0, sizeof(cursor));
248 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
249 cursor.resource.ref = 1;
250 cursor.resource.device = This->device;
251 cursor.resource.pool = WINED3DPOOL_SCRATCH;
252 cursor.resource.format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, context->gl_info);
253 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
254 cursor.texture_name = This->device->cursorTexture;
255 cursor.texture_target = GL_TEXTURE_2D;
256 cursor.texture_level = 0;
257 cursor.currentDesc.Width = This->device->cursorWidth;
258 cursor.currentDesc.Height = This->device->cursorHeight;
259 /* The cursor must have pow2 sizes */
260 cursor.pow2Width = cursor.currentDesc.Width;
261 cursor.pow2Height = cursor.currentDesc.Height;
262 /* The surface is in the texture */
263 cursor.Flags |= SFLAG_INTEXTURE;
264 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
265 * which is exactly what we want :-)
267 if (This->presentParms.Windowed) {
268 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
270 IWineD3DSurface_Blt((IWineD3DSurface *)This->back_buffers[0], &destRect, (IWineD3DSurface *)&cursor,
271 NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
274 if (This->device->logo_surface)
276 /* Blit the logo into the upper left corner of the drawable. */
277 IWineD3DSurface_BltFast((IWineD3DSurface *)This->back_buffers[0], 0, 0,
278 This->device->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
281 TRACE("Presenting HDC %p.\n", context->hdc);
283 render_to_fbo = This->render_to_fbo;
285 if (pSourceRect)
287 src_rect = *pSourceRect;
288 if (!render_to_fbo && (src_rect.left || src_rect.top
289 || src_rect.right != This->presentParms.BackBufferWidth
290 || src_rect.bottom != This->presentParms.BackBufferHeight))
292 render_to_fbo = TRUE;
295 else
297 src_rect.left = 0;
298 src_rect.top = 0;
299 src_rect.right = This->presentParms.BackBufferWidth;
300 src_rect.bottom = This->presentParms.BackBufferHeight;
303 if (pDestRect) dst_rect = *pDestRect;
304 else GetClientRect(This->win_handle, &dst_rect);
306 if (!render_to_fbo && (dst_rect.left || dst_rect.top
307 || dst_rect.right != This->presentParms.BackBufferWidth
308 || dst_rect.bottom != This->presentParms.BackBufferHeight))
310 render_to_fbo = TRUE;
313 /* Rendering to a window of different size, presenting partial rectangles,
314 * or rendering to a different window needs help from FBO_blit or a textured
315 * draw. Render the swapchain to a FBO in the future.
317 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
318 * all these issues - this fails if the window is smaller than the backbuffer.
320 if (!This->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
322 IWineD3DSurface_LoadLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INTEXTURE, NULL);
323 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INDRAWABLE, FALSE);
324 This->render_to_fbo = TRUE;
326 /* Force the context manager to update the render target configuration next draw. */
327 context->current_rt = NULL;
330 if(This->render_to_fbo)
332 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
333 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
334 * not allowed(they need the COPY swapeffect)
336 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
337 * the swap
339 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP )
341 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
344 swapchain_blit(This, context, &src_rect, &dst_rect);
347 if (This->num_contexts > 1) wglFinish();
348 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
350 TRACE("SwapBuffers called, Starting new frame\n");
351 /* FPS support */
352 if (TRACE_ON(fps))
354 DWORD time = GetTickCount();
355 This->frames++;
356 /* every 1.5 seconds */
357 if (time - This->prev_time > 1500) {
358 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
359 This->prev_time = time;
360 This->frames = 0;
364 #if defined(FRAME_DEBUGGING)
366 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
367 if (!isOn) {
368 isOn = TRUE;
369 FIXME("Enabling D3D Trace\n");
370 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
371 #if defined(SHOW_FRAME_MAKEUP)
372 FIXME("Singe Frame snapshots Starting\n");
373 isDumpingFrames = TRUE;
374 ENTER_GL();
375 glClear(GL_COLOR_BUFFER_BIT);
376 LEAVE_GL();
377 #endif
379 #if defined(SINGLE_FRAME_DEBUGGING)
380 } else {
381 #if defined(SHOW_FRAME_MAKEUP)
382 FIXME("Singe Frame snapshots Finishing\n");
383 isDumpingFrames = FALSE;
384 #endif
385 FIXME("Singe Frame trace complete\n");
386 DeleteFileA("C:\\D3DTRACE");
387 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
388 #endif
390 } else {
391 if (isOn) {
392 isOn = FALSE;
393 #if defined(SHOW_FRAME_MAKEUP)
394 FIXME("Single Frame snapshots Finishing\n");
395 isDumpingFrames = FALSE;
396 #endif
397 FIXME("Disabling D3D Trace\n");
398 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
402 #endif
404 /* This is disabled, but the code left in for debug purposes.
406 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
407 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
408 * The Debug runtime does the same on Windows. However, a few games do not redraw the
409 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
411 * Tests show that the content of the back buffer after a discard flip is indeed not
412 * reliable, so no game can depend on the exact content. However, it resembles the
413 * old contents in some way, for example by showing fragments at other locations. In
414 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
415 * gets a dark background image. If we clear it with a bright ugly color, the game's
416 * bug shows up much more than it does on Windows, and the players see single pixels
417 * with wrong colors.
418 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
420 if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
421 TRACE("Clearing the color buffer with cyan color\n");
423 IWineD3DDevice_Clear((IWineD3DDevice *)This->device, 0, NULL,
424 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
427 if (!This->render_to_fbo && ((This->front_buffer->Flags & SFLAG_INSYSMEM)
428 || (This->back_buffers[0]->Flags & SFLAG_INSYSMEM)))
430 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
431 * Doesn't work with render_to_fbo because we're not flipping
433 IWineD3DSurfaceImpl *front = This->front_buffer;
434 IWineD3DSurfaceImpl *back = This->back_buffers[0];
436 if(front->resource.size == back->resource.size) {
437 DWORD fbflags;
438 flip_surface(front, back);
440 /* Tell the front buffer surface that is has been modified. However,
441 * the other locations were preserved during that, so keep the flags.
442 * This serves to update the emulated overlay, if any
444 fbflags = front->Flags;
445 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)front, SFLAG_INDRAWABLE, TRUE);
446 front->Flags = fbflags;
447 } else {
448 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
449 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
452 else
454 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->front_buffer, SFLAG_INDRAWABLE, TRUE);
455 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
456 * and INTEXTURE copies can keep their old content if they have any defined content.
457 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
458 * the texture / sysmem copy needs to be reloaded from the drawable
460 if (This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP)
462 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INDRAWABLE, TRUE);
466 if (This->device->depth_stencil)
468 if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
469 || This->device->depth_stencil->Flags & SFLAG_DISCARD)
471 surface_modify_ds_location(This->device->depth_stencil, SFLAG_DS_DISCARDED);
472 if (This->device->depth_stencil == This->device->onscreen_depth_stencil)
474 IWineD3DSurface_Release((IWineD3DSurface *)This->device->onscreen_depth_stencil);
475 This->device->onscreen_depth_stencil = NULL;
480 if (This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE
481 && context->gl_info->supported[SGI_VIDEO_SYNC])
483 retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
484 if(retval != 0) {
485 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
488 switch(This->presentParms.PresentationInterval) {
489 case WINED3DPRESENT_INTERVAL_DEFAULT:
490 case WINED3DPRESENT_INTERVAL_ONE:
491 if(sync <= This->vSyncCounter) {
492 retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
493 } else {
494 This->vSyncCounter = sync;
496 break;
497 case WINED3DPRESENT_INTERVAL_TWO:
498 if(sync <= This->vSyncCounter + 1) {
499 retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
500 } else {
501 This->vSyncCounter = sync;
503 break;
504 case WINED3DPRESENT_INTERVAL_THREE:
505 if(sync <= This->vSyncCounter + 2) {
506 retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
507 } else {
508 This->vSyncCounter = sync;
510 break;
511 case WINED3DPRESENT_INTERVAL_FOUR:
512 if(sync <= This->vSyncCounter + 3) {
513 retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
514 } else {
515 This->vSyncCounter = sync;
517 break;
518 default:
519 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
523 context_release(context);
525 TRACE("returning\n");
526 return WINED3D_OK;
529 static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window)
531 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)iface;
533 if (!window) window = swapchain->device_window;
534 if (window == swapchain->win_handle) return WINED3D_OK;
536 TRACE("Setting swapchain %p window from %p to %p\n", swapchain, swapchain->win_handle, window);
537 swapchain->win_handle = window;
539 return WINED3D_OK;
542 static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
544 /* IUnknown */
545 IWineD3DBaseSwapChainImpl_QueryInterface,
546 IWineD3DBaseSwapChainImpl_AddRef,
547 IWineD3DBaseSwapChainImpl_Release,
548 /* IWineD3DSwapChain */
549 IWineD3DBaseSwapChainImpl_GetParent,
550 IWineD3DSwapChainImpl_Destroy,
551 IWineD3DBaseSwapChainImpl_GetDevice,
552 IWineD3DSwapChainImpl_Present,
553 IWineD3DSwapChainImpl_SetDestWindowOverride,
554 IWineD3DBaseSwapChainImpl_GetFrontBufferData,
555 IWineD3DBaseSwapChainImpl_GetBackBuffer,
556 IWineD3DBaseSwapChainImpl_GetRasterStatus,
557 IWineD3DBaseSwapChainImpl_GetDisplayMode,
558 IWineD3DBaseSwapChainImpl_GetPresentParameters,
559 IWineD3DBaseSwapChainImpl_SetGammaRamp,
560 IWineD3DBaseSwapChainImpl_GetGammaRamp
563 static LONG fullscreen_style(LONG style)
565 /* Make sure the window is managed, otherwise we won't get keyboard input. */
566 style |= WS_POPUP | WS_SYSMENU;
567 style &= ~(WS_CAPTION | WS_THICKFRAME);
569 return style;
572 static LONG fullscreen_exstyle(LONG exstyle)
574 /* Filter out window decorations. */
575 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
577 return exstyle;
580 void swapchain_setup_fullscreen_window(IWineD3DSwapChainImpl *swapchain, UINT w, UINT h)
582 IWineD3DDeviceImpl *device = swapchain->device;
583 HWND window = swapchain->device_window;
584 BOOL filter_messages;
585 LONG style, exstyle;
587 TRACE("Setting up window %p for fullscreen mode.\n", window);
589 if (device->style || device->exStyle)
591 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
592 window, device->style, device->exStyle);
595 device->style = GetWindowLongW(window, GWL_STYLE);
596 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
598 style = fullscreen_style(device->style);
599 exstyle = fullscreen_exstyle(device->exStyle);
601 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
602 device->style, device->exStyle, style, exstyle);
604 filter_messages = device->filter_messages;
605 device->filter_messages = TRUE;
607 SetWindowLongW(window, GWL_STYLE, style);
608 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
609 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
611 device->filter_messages = filter_messages;
614 void swapchain_restore_fullscreen_window(IWineD3DSwapChainImpl *swapchain)
616 IWineD3DDeviceImpl *device = swapchain->device;
617 HWND window = swapchain->device_window;
618 BOOL filter_messages;
619 LONG style, exstyle;
621 if (!device->style && !device->exStyle) return;
623 TRACE("Restoring window style of window %p to %08x, %08x.\n",
624 window, device->style, device->exStyle);
626 style = GetWindowLongW(window, GWL_STYLE);
627 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
629 filter_messages = device->filter_messages;
630 device->filter_messages = TRUE;
632 /* Only restore the style if the application didn't modify it during the
633 * fullscreen phase. Some applications change it before calling Reset()
634 * when switching between windowed and fullscreen modes (HL2), some
635 * depend on the original style (Eve Online). */
636 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
638 SetWindowLongW(window, GWL_STYLE, device->style);
639 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
641 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
643 device->filter_messages = filter_messages;
645 /* Delete the old values. */
646 device->style = 0;
647 device->exStyle = 0;
651 HRESULT swapchain_init(IWineD3DSwapChainImpl *swapchain, WINED3DSURFTYPE surface_type,
652 IWineD3DDeviceImpl *device, WINED3DPRESENT_PARAMETERS *present_parameters, IUnknown *parent)
654 const struct wined3d_adapter *adapter = device->adapter;
655 const struct wined3d_format_desc *format_desc;
656 BOOL displaymode_set = FALSE;
657 WINED3DDISPLAYMODE mode;
658 RECT client_rect;
659 HWND window;
660 HRESULT hr;
661 UINT i;
663 if (present_parameters->BackBufferCount > WINED3DPRESENT_BACK_BUFFER_MAX)
665 FIXME("The application requested %u back buffers, this is not supported.\n",
666 present_parameters->BackBufferCount);
667 return WINED3DERR_INVALIDCALL;
670 if (present_parameters->BackBufferCount > 1)
672 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
673 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
676 switch (surface_type)
678 case SURFACE_GDI:
679 swapchain->lpVtbl = &IWineGDISwapChain_Vtbl;
680 break;
682 case SURFACE_OPENGL:
683 swapchain->lpVtbl = &IWineD3DSwapChain_Vtbl;
684 break;
686 case SURFACE_UNKNOWN:
687 FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
688 return WINED3DERR_INVALIDCALL;
691 window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
693 swapchain->device = device;
694 swapchain->parent = parent;
695 swapchain->ref = 1;
696 swapchain->win_handle = window;
697 swapchain->device_window = window;
699 if (!present_parameters->Windowed && window)
701 swapchain_setup_fullscreen_window(swapchain, present_parameters->BackBufferWidth,
702 present_parameters->BackBufferHeight);
705 IWineD3D_GetAdapterDisplayMode(device->wined3d, adapter->ordinal, &mode);
706 swapchain->orig_width = mode.Width;
707 swapchain->orig_height = mode.Height;
708 swapchain->orig_fmt = mode.Format;
709 format_desc = getFormatDescEntry(mode.Format, &adapter->gl_info);
711 GetClientRect(window, &client_rect);
712 if (present_parameters->Windowed
713 && (!present_parameters->BackBufferWidth || !present_parameters->BackBufferHeight
714 || present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN))
717 if (!present_parameters->BackBufferWidth)
719 present_parameters->BackBufferWidth = client_rect.right;
720 TRACE("Updating width to %u.\n", present_parameters->BackBufferWidth);
723 if (!present_parameters->BackBufferHeight)
725 present_parameters->BackBufferHeight = client_rect.bottom;
726 TRACE("Updating height to %u.\n", present_parameters->BackBufferHeight);
729 if (present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN)
731 present_parameters->BackBufferFormat = swapchain->orig_fmt;
732 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
735 swapchain->presentParms = *present_parameters;
737 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
738 && present_parameters->BackBufferCount
739 && (present_parameters->BackBufferWidth != client_rect.right
740 || present_parameters->BackBufferHeight != client_rect.bottom))
742 TRACE("Rendering to FBO. Backbuffer %ux%u, window %ux%u.\n",
743 present_parameters->BackBufferWidth,
744 present_parameters->BackBufferHeight,
745 client_rect.right, client_rect.bottom);
746 swapchain->render_to_fbo = TRUE;
749 TRACE("Creating front buffer.\n");
750 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
751 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
752 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
753 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */,
754 (IWineD3DSurface **)&swapchain->front_buffer);
755 if (FAILED(hr))
757 WARN("Failed to create front buffer, hr %#x.\n", hr);
758 goto err;
761 IWineD3DSurface_SetContainer((IWineD3DSurface *)swapchain->front_buffer, (IWineD3DBase *)swapchain);
762 swapchain->front_buffer->Flags |= SFLAG_SWAPCHAIN;
763 if (surface_type == SURFACE_OPENGL)
765 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
768 /* MSDN says we're only allowed a single fullscreen swapchain per device,
769 * so we should really check to see if there is a fullscreen swapchain
770 * already. Does a single head count as full screen? */
772 if (!present_parameters->Windowed)
774 WINED3DDISPLAYMODE mode;
776 /* Change the display settings */
777 mode.Width = present_parameters->BackBufferWidth;
778 mode.Height = present_parameters->BackBufferHeight;
779 mode.Format = present_parameters->BackBufferFormat;
780 mode.RefreshRate = present_parameters->FullScreen_RefreshRateInHz;
782 hr = IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)device, 0, &mode);
783 if (FAILED(hr))
785 WARN("Failed to set display mode, hr %#x.\n", hr);
786 goto err;
788 displaymode_set = TRUE;
791 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain->context));
792 if (!swapchain->context)
794 ERR("Failed to create the context array.\n");
795 hr = E_OUTOFMEMORY;
796 goto err;
798 swapchain->num_contexts = 1;
800 if (surface_type == SURFACE_OPENGL)
802 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
804 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
805 * You are able to add a depth + stencil surface at a later stage when you need it.
806 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
807 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
808 * context, need torecreate shaders, textures and other resources.
810 * The context manager already takes care of the state problem and for the other tasks code from Reset
811 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
812 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
813 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
814 * issue needs to be fixed. */
815 if (!present_parameters->EnableAutoDepthStencil
816 || swapchain->presentParms.AutoDepthStencilFormat != WINED3DFMT_D24_UNORM_S8_UINT)
818 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
820 swapchain->ds_format = getFormatDescEntry(WINED3DFMT_D24_UNORM_S8_UINT, gl_info);
821 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
822 if (!swapchain->context[0])
824 WARN("Failed to create context.\n");
825 hr = WINED3DERR_NOTAVAILABLE;
826 goto err;
828 context_release(swapchain->context[0]);
830 else
832 swapchain->context[0] = NULL;
835 if (swapchain->presentParms.BackBufferCount > 0)
837 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
838 sizeof(*swapchain->back_buffers) * swapchain->presentParms.BackBufferCount);
839 if (!swapchain->back_buffers)
841 ERR("Failed to allocate backbuffer array memory.\n");
842 hr = E_OUTOFMEMORY;
843 goto err;
846 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
848 TRACE("Creating back buffer %u.\n", i);
849 hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
850 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
851 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
852 swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */,
853 (IWineD3DSurface **)&swapchain->back_buffers[i]);
854 if (FAILED(hr))
856 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
857 goto err;
860 IWineD3DSurface_SetContainer((IWineD3DSurface *)swapchain->back_buffers[i], (IWineD3DBase *)swapchain);
861 swapchain->back_buffers[i]->Flags |= SFLAG_SWAPCHAIN;
865 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
866 if (present_parameters->EnableAutoDepthStencil && surface_type == SURFACE_OPENGL)
868 TRACE("Creating depth/stencil buffer.\n");
869 if (!device->auto_depth_stencil)
871 hr = IWineD3DDeviceParent_CreateDepthStencilSurface(device->device_parent, parent,
872 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
873 swapchain->presentParms.AutoDepthStencilFormat, swapchain->presentParms.MultiSampleType,
874 swapchain->presentParms.MultiSampleQuality, FALSE /* FIXME: Discard */,
875 (IWineD3DSurface **)&device->auto_depth_stencil);
876 if (FAILED(hr))
878 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
879 goto err;
882 IWineD3DSurface_SetContainer((IWineD3DSurface *)device->auto_depth_stencil, NULL);
886 IWineD3DSwapChain_GetGammaRamp((IWineD3DSwapChain *)swapchain, &swapchain->orig_gamma);
888 return WINED3D_OK;
890 err:
891 if (displaymode_set)
893 DEVMODEW devmode;
895 ClipCursor(NULL);
897 /* Change the display settings */
898 memset(&devmode, 0, sizeof(devmode));
899 devmode.dmSize = sizeof(devmode);
900 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
901 devmode.dmBitsPerPel = format_desc->byte_count * 8;
902 devmode.dmPelsWidth = swapchain->orig_width;
903 devmode.dmPelsHeight = swapchain->orig_height;
904 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
907 if (swapchain->back_buffers)
909 for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
911 if (swapchain->back_buffers[i]) IWineD3DSurface_Release((IWineD3DSurface *)swapchain->back_buffers[i]);
913 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
916 if (swapchain->context)
918 if (swapchain->context[0])
920 context_release(swapchain->context[0]);
921 context_destroy(device, swapchain->context[0]);
922 swapchain->num_contexts = 0;
924 HeapFree(GetProcessHeap(), 0, swapchain->context);
927 if (swapchain->front_buffer) IWineD3DSurface_Release((IWineD3DSurface *)swapchain->front_buffer);
929 return hr;
932 struct wined3d_context *swapchain_create_context_for_thread(IWineD3DSwapChain *iface)
934 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
935 struct wined3d_context **newArray;
936 struct wined3d_context *ctx;
938 TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
940 if (!(ctx = context_create(This, This->front_buffer, This->ds_format)))
942 ERR("Failed to create a new context for the swapchain\n");
943 return NULL;
945 context_release(ctx);
947 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
948 if(!newArray) {
949 ERR("Out of memory when trying to allocate a new context array\n");
950 context_destroy(This->device, ctx);
951 return NULL;
953 memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
954 HeapFree(GetProcessHeap(), 0, This->context);
955 newArray[This->num_contexts] = ctx;
956 This->context = newArray;
957 This->num_contexts++;
959 TRACE("Returning context %p\n", ctx);
960 return ctx;
963 void get_drawable_size_swapchain(struct wined3d_context *context, UINT *width, UINT *height)
965 /* The drawable size of an onscreen drawable is the surface size.
966 * (Actually: The window size, but the surface is created in window size) */
967 *width = context->current_rt->currentDesc.Width;
968 *height = context->current_rt->currentDesc.Height;