xref: /reactos/dll/directx/wine/wined3d/swapchain.c (revision 81cffd76)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Copyright 2002-2003 Jason Edmeades
3c2c66affSColin Finck  * Copyright 2002-2003 Raphael Junqueira
4c2c66affSColin Finck  * Copyright 2005 Oliver Stieber
5c2c66affSColin Finck  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6c2c66affSColin Finck  * Copyright 2011 Henri Verbeet for CodeWeavers
7c2c66affSColin Finck  *
8c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
9c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
10c2c66affSColin Finck  * License as published by the Free Software Foundation; either
11c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
12c2c66affSColin Finck  *
13c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
14c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16c2c66affSColin Finck  * Lesser General Public License for more details.
17c2c66affSColin Finck  *
18c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
19c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
20c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21c2c66affSColin Finck  */
22c2c66affSColin Finck 
239987f029SAmine Khaldi #include "config.h"
249987f029SAmine Khaldi #include "wine/port.h"
25c2c66affSColin Finck #include "wined3d_private.h"
26c2c66affSColin Finck 
279987f029SAmine Khaldi #ifdef __REACTOS__
289987f029SAmine Khaldi #include <reactos/undocuser.h>
299987f029SAmine Khaldi #endif
309987f029SAmine Khaldi 
31c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(fps);
33c2c66affSColin Finck 
wined3d_swapchain_destroy_object(void * object)34c2c66affSColin Finck static void wined3d_swapchain_destroy_object(void *object)
35c2c66affSColin Finck {
36c2c66affSColin Finck     swapchain_destroy_contexts(object);
37c2c66affSColin Finck }
38c2c66affSColin Finck 
swapchain_cleanup(struct wined3d_swapchain * swapchain)39c2c66affSColin Finck static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
40c2c66affSColin Finck {
41c2c66affSColin Finck     HRESULT hr;
42c2c66affSColin Finck     UINT i;
43c2c66affSColin Finck 
44c2c66affSColin Finck     TRACE("Destroying swapchain %p.\n", swapchain);
45c2c66affSColin Finck 
46c2c66affSColin Finck     wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
47c2c66affSColin Finck 
48c2c66affSColin Finck     /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
49c2c66affSColin Finck      * is the last buffer to be destroyed, FindContext() depends on that. */
50c2c66affSColin Finck     if (swapchain->front_buffer)
51c2c66affSColin Finck     {
52c2c66affSColin Finck         wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
53c2c66affSColin Finck         if (wined3d_texture_decref(swapchain->front_buffer))
54c2c66affSColin Finck             WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
55c2c66affSColin Finck         swapchain->front_buffer = NULL;
56c2c66affSColin Finck     }
57c2c66affSColin Finck 
58c2c66affSColin Finck     if (swapchain->back_buffers)
59c2c66affSColin Finck     {
60c2c66affSColin Finck         i = swapchain->desc.backbuffer_count;
61c2c66affSColin Finck 
62c2c66affSColin Finck         while (i--)
63c2c66affSColin Finck         {
64c2c66affSColin Finck             wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
65c2c66affSColin Finck             if (wined3d_texture_decref(swapchain->back_buffers[i]))
66c2c66affSColin Finck                 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
67c2c66affSColin Finck         }
689987f029SAmine Khaldi         heap_free(swapchain->back_buffers);
69c2c66affSColin Finck         swapchain->back_buffers = NULL;
70c2c66affSColin Finck     }
71c2c66affSColin Finck 
72c2c66affSColin Finck     wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
73c2c66affSColin Finck     swapchain->device->cs->ops->finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
74c2c66affSColin Finck 
75c2c66affSColin Finck     /* Restore the screen resolution if we rendered in fullscreen.
76c2c66affSColin Finck      * This will restore the screen resolution to what it was before creating
77c2c66affSColin Finck      * the swapchain. In case of d3d8 and d3d9 this will be the original
78c2c66affSColin Finck      * desktop resolution. In case of d3d7 this will be a NOP because ddraw
79c2c66affSColin Finck      * sets the resolution before starting up Direct3D, thus orig_width and
80c2c66affSColin Finck      * orig_height will be equal to the modes in the presentation params. */
81c2c66affSColin Finck     if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
82c2c66affSColin Finck     {
83c2c66affSColin Finck         if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
84c2c66affSColin Finck                 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
85c2c66affSColin Finck             ERR("Failed to restore display mode, hr %#x.\n", hr);
86c2c66affSColin Finck 
87c2c66affSColin Finck         if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
88c2c66affSColin Finck         {
89c2c66affSColin Finck             wined3d_device_restore_fullscreen_window(swapchain->device, swapchain->device_window,
90c2c66affSColin Finck                     &swapchain->original_window_rect);
91c2c66affSColin Finck             wined3d_device_release_focus_window(swapchain->device);
92c2c66affSColin Finck         }
93c2c66affSColin Finck     }
94c2c66affSColin Finck 
95c2c66affSColin Finck     if (swapchain->backup_dc)
96c2c66affSColin Finck     {
97c2c66affSColin Finck         TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
98c2c66affSColin Finck 
99c2c66affSColin Finck         wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
100c2c66affSColin Finck         DestroyWindow(swapchain->backup_wnd);
101c2c66affSColin Finck     }
102c2c66affSColin Finck }
103c2c66affSColin Finck 
wined3d_swapchain_incref(struct wined3d_swapchain * swapchain)104c2c66affSColin Finck ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
105c2c66affSColin Finck {
106c2c66affSColin Finck     ULONG refcount = InterlockedIncrement(&swapchain->ref);
107c2c66affSColin Finck 
108c2c66affSColin Finck     TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
109c2c66affSColin Finck 
110c2c66affSColin Finck     return refcount;
111c2c66affSColin Finck }
112c2c66affSColin Finck 
wined3d_swapchain_decref(struct wined3d_swapchain * swapchain)113c2c66affSColin Finck ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
114c2c66affSColin Finck {
115c2c66affSColin Finck     ULONG refcount = InterlockedDecrement(&swapchain->ref);
116c2c66affSColin Finck 
117c2c66affSColin Finck     TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
118c2c66affSColin Finck 
119c2c66affSColin Finck     if (!refcount)
120c2c66affSColin Finck     {
121c2c66affSColin Finck         struct wined3d_device *device = swapchain->device;
122c2c66affSColin Finck 
123c2c66affSColin Finck         device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
124c2c66affSColin Finck 
125c2c66affSColin Finck         swapchain_cleanup(swapchain);
126c2c66affSColin Finck         swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
1279987f029SAmine Khaldi         heap_free(swapchain);
128c2c66affSColin Finck     }
129c2c66affSColin Finck 
130c2c66affSColin Finck     return refcount;
131c2c66affSColin Finck }
132c2c66affSColin Finck 
wined3d_swapchain_get_parent(const struct wined3d_swapchain * swapchain)133c2c66affSColin Finck void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
134c2c66affSColin Finck {
135c2c66affSColin Finck     TRACE("swapchain %p.\n", swapchain);
136c2c66affSColin Finck 
137c2c66affSColin Finck     return swapchain->parent;
138c2c66affSColin Finck }
139c2c66affSColin Finck 
wined3d_swapchain_set_window(struct wined3d_swapchain * swapchain,HWND window)140c2c66affSColin Finck void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
141c2c66affSColin Finck {
142c2c66affSColin Finck     if (!window)
143c2c66affSColin Finck         window = swapchain->device_window;
144c2c66affSColin Finck     if (window == swapchain->win_handle)
145c2c66affSColin Finck         return;
146c2c66affSColin Finck 
147c2c66affSColin Finck     TRACE("Setting swapchain %p window from %p to %p.\n",
148c2c66affSColin Finck             swapchain, swapchain->win_handle, window);
149c2c66affSColin Finck     swapchain->win_handle = window;
150c2c66affSColin Finck }
151c2c66affSColin Finck 
wined3d_swapchain_present(struct wined3d_swapchain * swapchain,const RECT * src_rect,const RECT * dst_rect,HWND dst_window_override,DWORD swap_interval,DWORD flags)152c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
1539987f029SAmine Khaldi         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
154*81cffd76SJoachim Henze         DWORD swap_interval, DWORD flags)
155c2c66affSColin Finck {
156*81cffd76SJoachim Henze     static DWORD notified_flags = 0;
157c2c66affSColin Finck     RECT s, d;
158c2c66affSColin Finck 
159c2c66affSColin Finck     TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
160c2c66affSColin Finck             swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
161c2c66affSColin Finck             dst_window_override, flags);
162c2c66affSColin Finck 
163*81cffd76SJoachim Henze     if (flags & ~notified_flags)
164*81cffd76SJoachim Henze     {
165*81cffd76SJoachim Henze         FIXME("Ignoring flags %#x.\n", flags & ~notified_flags);
166*81cffd76SJoachim Henze         notified_flags |= flags;
167*81cffd76SJoachim Henze     }
168c2c66affSColin Finck 
169c2c66affSColin Finck     if (!swapchain->back_buffers)
170c2c66affSColin Finck     {
171*81cffd76SJoachim Henze         WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
172c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
173c2c66affSColin Finck     }
174c2c66affSColin Finck 
175c2c66affSColin Finck     if (!src_rect)
176c2c66affSColin Finck     {
177c2c66affSColin Finck         SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
178c2c66affSColin Finck                 swapchain->desc.backbuffer_height);
179c2c66affSColin Finck         src_rect = &s;
180c2c66affSColin Finck     }
181c2c66affSColin Finck 
182c2c66affSColin Finck     if (!dst_rect)
183c2c66affSColin Finck     {
184c2c66affSColin Finck         GetClientRect(swapchain->win_handle, &d);
185c2c66affSColin Finck         dst_rect = &d;
186c2c66affSColin Finck     }
187c2c66affSColin Finck 
188c2c66affSColin Finck     wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
1899987f029SAmine Khaldi             dst_rect, dst_window_override, swap_interval, flags);
190c2c66affSColin Finck 
191c2c66affSColin Finck     return WINED3D_OK;
192c2c66affSColin Finck }
193c2c66affSColin Finck 
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain * swapchain,struct wined3d_texture * dst_texture,unsigned int sub_resource_idx)194c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
195c2c66affSColin Finck         struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
196c2c66affSColin Finck {
197c2c66affSColin Finck     RECT src_rect, dst_rect;
198c2c66affSColin Finck 
199c2c66affSColin Finck     TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
200c2c66affSColin Finck 
201c2c66affSColin Finck     SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
202c2c66affSColin Finck     dst_rect = src_rect;
203c2c66affSColin Finck 
204c2c66affSColin Finck     if (swapchain->desc.windowed)
205c2c66affSColin Finck     {
206c2c66affSColin Finck         MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
207c2c66affSColin Finck         FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
208c2c66affSColin Finck                 wine_dbgstr_rect(&dst_rect));
209c2c66affSColin Finck     }
210c2c66affSColin Finck 
211c2c66affSColin Finck     return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
212c2c66affSColin Finck             swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
213c2c66affSColin Finck }
214c2c66affSColin Finck 
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain * swapchain,UINT back_buffer_idx)215c2c66affSColin Finck struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
216c2c66affSColin Finck         UINT back_buffer_idx)
217c2c66affSColin Finck {
218c2c66affSColin Finck     TRACE("swapchain %p, back_buffer_idx %u.\n",
219c2c66affSColin Finck             swapchain, back_buffer_idx);
220c2c66affSColin Finck 
221c2c66affSColin Finck     /* Return invalid if there is no backbuffer array, otherwise it will
222c2c66affSColin Finck      * crash when ddraw is used (there swapchain->back_buffers is always
223c2c66affSColin Finck      * NULL). We need this because this function is called from
224c2c66affSColin Finck      * stateblock_init_default_state() to get the default scissorrect
225c2c66affSColin Finck      * dimensions. */
226c2c66affSColin Finck     if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
227c2c66affSColin Finck     {
228c2c66affSColin Finck         WARN("Invalid back buffer index.\n");
229c2c66affSColin Finck         /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
230c2c66affSColin Finck          * here in wined3d to avoid problems in other libs. */
231c2c66affSColin Finck         return NULL;
232c2c66affSColin Finck     }
233c2c66affSColin Finck 
234c2c66affSColin Finck     TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
235c2c66affSColin Finck 
236c2c66affSColin Finck     return swapchain->back_buffers[back_buffer_idx];
237c2c66affSColin Finck }
238c2c66affSColin Finck 
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain * swapchain,struct wined3d_raster_status * raster_status)239c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
240c2c66affSColin Finck         struct wined3d_raster_status *raster_status)
241c2c66affSColin Finck {
242c2c66affSColin Finck     TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
243c2c66affSColin Finck 
244c2c66affSColin Finck     return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
245c2c66affSColin Finck             swapchain->device->adapter->ordinal, raster_status);
246c2c66affSColin Finck }
247c2c66affSColin Finck 
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain * swapchain,struct wined3d_display_mode * mode,enum wined3d_display_rotation * rotation)248c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
249c2c66affSColin Finck         struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
250c2c66affSColin Finck {
251c2c66affSColin Finck     HRESULT hr;
252c2c66affSColin Finck 
253c2c66affSColin Finck     TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
254c2c66affSColin Finck 
255c2c66affSColin Finck     hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
256c2c66affSColin Finck             swapchain->device->adapter->ordinal, mode, rotation);
257c2c66affSColin Finck 
258c2c66affSColin Finck     TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
259c2c66affSColin Finck             mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
260c2c66affSColin Finck 
261c2c66affSColin Finck     return hr;
262c2c66affSColin Finck }
263c2c66affSColin Finck 
wined3d_swapchain_get_device(const struct wined3d_swapchain * swapchain)264c2c66affSColin Finck struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
265c2c66affSColin Finck {
266c2c66affSColin Finck     TRACE("swapchain %p.\n", swapchain);
267c2c66affSColin Finck 
268c2c66affSColin Finck     return swapchain->device;
269c2c66affSColin Finck }
270c2c66affSColin Finck 
wined3d_swapchain_get_desc(const struct wined3d_swapchain * swapchain,struct wined3d_swapchain_desc * desc)271c2c66affSColin Finck void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
272c2c66affSColin Finck         struct wined3d_swapchain_desc *desc)
273c2c66affSColin Finck {
274c2c66affSColin Finck     TRACE("swapchain %p, desc %p.\n", swapchain, desc);
275c2c66affSColin Finck 
276c2c66affSColin Finck     *desc = swapchain->desc;
277c2c66affSColin Finck }
278c2c66affSColin Finck 
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain * swapchain,DWORD flags,const struct wined3d_gamma_ramp * ramp)279c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
280c2c66affSColin Finck         DWORD flags, const struct wined3d_gamma_ramp *ramp)
281c2c66affSColin Finck {
282c2c66affSColin Finck     HDC dc;
283c2c66affSColin Finck 
284c2c66affSColin Finck     TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
285c2c66affSColin Finck 
286c2c66affSColin Finck     if (flags)
287c2c66affSColin Finck         FIXME("Ignoring flags %#x.\n", flags);
288c2c66affSColin Finck 
289c2c66affSColin Finck     dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
290c2c66affSColin Finck     SetDeviceGammaRamp(dc, (void *)ramp);
291c2c66affSColin Finck     ReleaseDC(swapchain->device_window, dc);
292c2c66affSColin Finck 
293c2c66affSColin Finck     return WINED3D_OK;
294c2c66affSColin Finck }
295c2c66affSColin Finck 
wined3d_swapchain_set_palette(struct wined3d_swapchain * swapchain,struct wined3d_palette * palette)296c2c66affSColin Finck void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
297c2c66affSColin Finck {
298c2c66affSColin Finck     TRACE("swapchain %p, palette %p.\n", swapchain, palette);
299c2c66affSColin Finck     swapchain->palette = palette;
300c2c66affSColin Finck }
301c2c66affSColin Finck 
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain * swapchain,struct wined3d_gamma_ramp * ramp)302c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
303c2c66affSColin Finck         struct wined3d_gamma_ramp *ramp)
304c2c66affSColin Finck {
305c2c66affSColin Finck     HDC dc;
306c2c66affSColin Finck 
307c2c66affSColin Finck     TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
308c2c66affSColin Finck 
309c2c66affSColin Finck     dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
310c2c66affSColin Finck     GetDeviceGammaRamp(dc, ramp);
311c2c66affSColin Finck     ReleaseDC(swapchain->device_window, dc);
312c2c66affSColin Finck 
313c2c66affSColin Finck     return WINED3D_OK;
314c2c66affSColin Finck }
315c2c66affSColin Finck 
316c2c66affSColin Finck /* A GL context is provided by the caller */
swapchain_blit(const struct wined3d_swapchain * swapchain,struct wined3d_context * context,const RECT * src_rect,const RECT * dst_rect)317c2c66affSColin Finck static void swapchain_blit(const struct wined3d_swapchain *swapchain,
318c2c66affSColin Finck         struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
319c2c66affSColin Finck {
320c2c66affSColin Finck     struct wined3d_texture *texture = swapchain->back_buffers[0];
321*81cffd76SJoachim Henze     struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
322c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
323c2c66affSColin Finck     enum wined3d_texture_filter_type filter;
324c2c66affSColin Finck     DWORD location;
325c2c66affSColin Finck 
326c2c66affSColin Finck     TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
327c2c66affSColin Finck             swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
328c2c66affSColin Finck 
329c2c66affSColin Finck     if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
330c2c66affSColin Finck             && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
331c2c66affSColin Finck             || is_complex_fixup(texture->resource.format->color_fixup))
332c2c66affSColin Finck         filter = WINED3D_TEXF_NONE;
333c2c66affSColin Finck     else
334c2c66affSColin Finck         filter = WINED3D_TEXF_LINEAR;
335c2c66affSColin Finck 
336c2c66affSColin Finck     location = WINED3D_LOCATION_TEXTURE_RGB;
337c2c66affSColin Finck     if (texture->resource.multisample_type)
338c2c66affSColin Finck         location = WINED3D_LOCATION_RB_RESOLVED;
339c2c66affSColin Finck 
340c2c66affSColin Finck     wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
341*81cffd76SJoachim Henze     device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, back_buffer,
342*81cffd76SJoachim Henze             location, src_rect, back_buffer, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
343c2c66affSColin Finck     wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
344c2c66affSColin Finck }
345c2c66affSColin Finck 
346c2c66affSColin Finck /* Context activation is done by the caller. */
wined3d_swapchain_rotate(struct wined3d_swapchain * swapchain,struct wined3d_context * context)347c2c66affSColin Finck static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
348c2c66affSColin Finck {
349c2c66affSColin Finck     struct wined3d_texture_sub_resource *sub_resource;
350c2c66affSColin Finck     struct wined3d_texture *texture, *texture_prev;
351c2c66affSColin Finck     struct gl_texture tex0;
352c2c66affSColin Finck     GLuint rb0;
353c2c66affSColin Finck     DWORD locations0;
354c2c66affSColin Finck     unsigned int i;
355c2c66affSColin Finck     static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
356c2c66affSColin Finck 
357c2c66affSColin Finck     if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
358c2c66affSColin Finck         return;
359c2c66affSColin Finck 
360c2c66affSColin Finck     texture_prev = swapchain->back_buffers[0];
361c2c66affSColin Finck 
362c2c66affSColin Finck     /* Back buffer 0 is already in the draw binding. */
363c2c66affSColin Finck     tex0 = texture_prev->texture_rgb;
364c2c66affSColin Finck     rb0 = texture_prev->rb_multisample;
365c2c66affSColin Finck     locations0 = texture_prev->sub_resources[0].locations;
366c2c66affSColin Finck 
367c2c66affSColin Finck     for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
368c2c66affSColin Finck     {
369c2c66affSColin Finck         texture = swapchain->back_buffers[i];
370c2c66affSColin Finck         sub_resource = &texture->sub_resources[0];
371c2c66affSColin Finck 
372c2c66affSColin Finck         if (!(sub_resource->locations & supported_locations))
373c2c66affSColin Finck             wined3d_texture_load_location(texture, 0, context, texture->resource.draw_binding);
374c2c66affSColin Finck 
375c2c66affSColin Finck         texture_prev->texture_rgb = texture->texture_rgb;
376c2c66affSColin Finck         texture_prev->rb_multisample = texture->rb_multisample;
377c2c66affSColin Finck 
378c2c66affSColin Finck         wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
379c2c66affSColin Finck         wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
380c2c66affSColin Finck 
381c2c66affSColin Finck         texture_prev = texture;
382c2c66affSColin Finck     }
383c2c66affSColin Finck 
384c2c66affSColin Finck     texture_prev->texture_rgb = tex0;
385c2c66affSColin Finck     texture_prev->rb_multisample = rb0;
386c2c66affSColin Finck 
387c2c66affSColin Finck     wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
388c2c66affSColin Finck     wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
389c2c66affSColin Finck 
390c2c66affSColin Finck     device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
391c2c66affSColin Finck }
392c2c66affSColin Finck 
swapchain_gl_present(struct wined3d_swapchain * swapchain,const RECT * src_rect,const RECT * dst_rect,DWORD flags)393c2c66affSColin Finck static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
394c2c66affSColin Finck         const RECT *src_rect, const RECT *dst_rect, DWORD flags)
395c2c66affSColin Finck {
396c2c66affSColin Finck     struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
397c2c66affSColin Finck     const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
398c2c66affSColin Finck     const struct wined3d_gl_info *gl_info;
399c2c66affSColin Finck     struct wined3d_texture *logo_texture;
400c2c66affSColin Finck     struct wined3d_context *context;
401c2c66affSColin Finck     BOOL render_to_fbo;
402c2c66affSColin Finck 
403c2c66affSColin Finck     context = context_acquire(swapchain->device, back_buffer, 0);
404c2c66affSColin Finck     if (!context->valid)
405c2c66affSColin Finck     {
406c2c66affSColin Finck         context_release(context);
407c2c66affSColin Finck         WARN("Invalid context, skipping present.\n");
408c2c66affSColin Finck         return;
409c2c66affSColin Finck     }
410c2c66affSColin Finck 
411c2c66affSColin Finck     gl_info = context->gl_info;
412c2c66affSColin Finck 
413c2c66affSColin Finck     if ((logo_texture = swapchain->device->logo_texture))
414c2c66affSColin Finck     {
415c2c66affSColin Finck         RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
416c2c66affSColin Finck 
417c2c66affSColin Finck         /* Blit the logo into the upper left corner of the drawable. */
418c2c66affSColin Finck         wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
419c2c66affSColin Finck                 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
420c2c66affSColin Finck     }
421c2c66affSColin Finck 
422c2c66affSColin Finck     if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
423c2c66affSColin Finck             && !swapchain->device->hardwareCursor)
424c2c66affSColin Finck     {
425c2c66affSColin Finck         RECT dst_rect =
426c2c66affSColin Finck         {
427c2c66affSColin Finck             swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
428c2c66affSColin Finck             swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
429c2c66affSColin Finck             swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
430c2c66affSColin Finck             swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
431c2c66affSColin Finck         };
432c2c66affSColin Finck         RECT src_rect =
433c2c66affSColin Finck         {
434c2c66affSColin Finck             0, 0,
435c2c66affSColin Finck             swapchain->device->cursor_texture->resource.width,
436c2c66affSColin Finck             swapchain->device->cursor_texture->resource.height
437c2c66affSColin Finck         };
438c2c66affSColin Finck         const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
439c2c66affSColin Finck 
440c2c66affSColin Finck         TRACE("Rendering the software cursor.\n");
441c2c66affSColin Finck 
442c2c66affSColin Finck         if (swapchain->desc.windowed)
443c2c66affSColin Finck             MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
444c2c66affSColin Finck         if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
445c2c66affSColin Finck             wined3d_texture_blt(back_buffer, 0, &dst_rect,
446c2c66affSColin Finck                     swapchain->device->cursor_texture, 0, &src_rect,
447c2c66affSColin Finck                     WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
448c2c66affSColin Finck     }
449c2c66affSColin Finck 
450c2c66affSColin Finck     TRACE("Presenting HDC %p.\n", context->hdc);
451c2c66affSColin Finck 
452c2c66affSColin Finck     if (!(render_to_fbo = swapchain->render_to_fbo)
453c2c66affSColin Finck             && (src_rect->left || src_rect->top
454c2c66affSColin Finck             || src_rect->right != swapchain->desc.backbuffer_width
455c2c66affSColin Finck             || src_rect->bottom != swapchain->desc.backbuffer_height
456c2c66affSColin Finck             || dst_rect->left || dst_rect->top
457c2c66affSColin Finck             || dst_rect->right != swapchain->desc.backbuffer_width
458c2c66affSColin Finck             || dst_rect->bottom != swapchain->desc.backbuffer_height))
459c2c66affSColin Finck         render_to_fbo = TRUE;
460c2c66affSColin Finck 
461c2c66affSColin Finck     /* Rendering to a window of different size, presenting partial rectangles,
462c2c66affSColin Finck      * or rendering to a different window needs help from FBO_blit or a textured
463c2c66affSColin Finck      * draw. Render the swapchain to a FBO in the future.
464c2c66affSColin Finck      *
465c2c66affSColin Finck      * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
466c2c66affSColin Finck      * all these issues - this fails if the window is smaller than the backbuffer.
467c2c66affSColin Finck      */
468c2c66affSColin Finck     if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
469c2c66affSColin Finck     {
470c2c66affSColin Finck         wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
471c2c66affSColin Finck         wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
472c2c66affSColin Finck         swapchain->render_to_fbo = TRUE;
473c2c66affSColin Finck         swapchain_update_draw_bindings(swapchain);
474c2c66affSColin Finck     }
475c2c66affSColin Finck     else
476c2c66affSColin Finck     {
477c2c66affSColin Finck         wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
478c2c66affSColin Finck     }
479c2c66affSColin Finck 
480c2c66affSColin Finck     if (swapchain->render_to_fbo)
481c2c66affSColin Finck         swapchain_blit(swapchain, context, src_rect, dst_rect);
482c2c66affSColin Finck 
483*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
484c2c66affSColin Finck     if (swapchain->num_contexts > 1)
485*81cffd76SJoachim Henze #else  /* STAGING_CSMT */
486*81cffd76SJoachim Henze     if (swapchain->num_contexts > 1 && !wined3d_settings.cs_multithreaded)
487*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
488c2c66affSColin Finck         gl_info->gl_ops.gl.p_glFinish();
489c2c66affSColin Finck 
490c2c66affSColin Finck     /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
491c2c66affSColin Finck     gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
492c2c66affSColin Finck 
493c2c66affSColin Finck     wined3d_swapchain_rotate(swapchain, context);
494c2c66affSColin Finck 
495c2c66affSColin Finck     TRACE("SwapBuffers called, Starting new frame\n");
496c2c66affSColin Finck     /* FPS support */
497c2c66affSColin Finck     if (TRACE_ON(fps))
498c2c66affSColin Finck     {
499c2c66affSColin Finck         DWORD time = GetTickCount();
500c2c66affSColin Finck         ++swapchain->frames;
501c2c66affSColin Finck 
502c2c66affSColin Finck         /* every 1.5 seconds */
503c2c66affSColin Finck         if (time - swapchain->prev_time > 1500)
504c2c66affSColin Finck         {
505c2c66affSColin Finck             TRACE_(fps)("%p @ approx %.2ffps\n",
506c2c66affSColin Finck                     swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
507c2c66affSColin Finck             swapchain->prev_time = time;
508c2c66affSColin Finck             swapchain->frames = 0;
509c2c66affSColin Finck         }
510c2c66affSColin Finck     }
511c2c66affSColin Finck 
512c2c66affSColin Finck     wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
513c2c66affSColin Finck     wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
514c2c66affSColin Finck     /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
515c2c66affSColin Finck      * and INTEXTURE copies can keep their old content if they have any defined content.
516c2c66affSColin Finck      * If the swapeffect is COPY, the content remains the same.
517c2c66affSColin Finck      *
518c2c66affSColin Finck      * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
519c2c66affSColin Finck      * up to date and hope WGL flipped front and back buffers and read this data into
520c2c66affSColin Finck      * the FBO. Don't bother about this for now. */
5219987f029SAmine Khaldi     if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD
5229987f029SAmine Khaldi             || swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
523c2c66affSColin Finck         wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
524c2c66affSColin Finck                 0, WINED3D_LOCATION_DISCARDED);
525c2c66affSColin Finck 
526*81cffd76SJoachim Henze     if (fb->depth_stencil)
527c2c66affSColin Finck     {
528*81cffd76SJoachim Henze         struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
529c2c66affSColin Finck 
530*81cffd76SJoachim Henze         if (ds && (swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
531*81cffd76SJoachim Henze                 || ds->container->flags & WINED3D_TEXTURE_DISCARD))
532*81cffd76SJoachim Henze             wined3d_texture_validate_location(ds->container,
533*81cffd76SJoachim Henze                     fb->depth_stencil->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
534c2c66affSColin Finck     }
535c2c66affSColin Finck 
536c2c66affSColin Finck     context_release(context);
537c2c66affSColin Finck }
538c2c66affSColin Finck 
swapchain_gl_frontbuffer_updated(struct wined3d_swapchain * swapchain)539c2c66affSColin Finck static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
540c2c66affSColin Finck {
541c2c66affSColin Finck     struct wined3d_texture *front_buffer = swapchain->front_buffer;
542c2c66affSColin Finck     struct wined3d_context *context;
543c2c66affSColin Finck 
544c2c66affSColin Finck     context = context_acquire(swapchain->device, front_buffer, 0);
545c2c66affSColin Finck     wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
546c2c66affSColin Finck     context_release(context);
547c2c66affSColin Finck     SetRectEmpty(&swapchain->front_buffer_update);
548c2c66affSColin Finck }
549c2c66affSColin Finck 
550c2c66affSColin Finck static const struct wined3d_swapchain_ops swapchain_gl_ops =
551c2c66affSColin Finck {
552c2c66affSColin Finck     swapchain_gl_present,
553c2c66affSColin Finck     swapchain_gl_frontbuffer_updated,
554c2c66affSColin Finck };
555c2c66affSColin Finck 
swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain * swapchain)556c2c66affSColin Finck static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
557c2c66affSColin Finck {
558*81cffd76SJoachim Henze     struct wined3d_surface *front;
559c2c66affSColin Finck     POINT offset = {0, 0};
560c2c66affSColin Finck     HDC src_dc, dst_dc;
561c2c66affSColin Finck     RECT draw_rect;
562c2c66affSColin Finck     HWND window;
563c2c66affSColin Finck 
564c2c66affSColin Finck     TRACE("swapchain %p.\n", swapchain);
565c2c66affSColin Finck 
566*81cffd76SJoachim Henze     front = swapchain->front_buffer->sub_resources[0].u.surface;
567c2c66affSColin Finck     if (swapchain->palette)
568c2c66affSColin Finck         wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
569c2c66affSColin Finck 
570*81cffd76SJoachim Henze     if (front->container->resource.map_count)
571c2c66affSColin Finck         ERR("Trying to blit a mapped surface.\n");
572c2c66affSColin Finck 
573c2c66affSColin Finck     TRACE("Copying surface %p to screen.\n", front);
574c2c66affSColin Finck 
575c2c66affSColin Finck     src_dc = front->dc;
576c2c66affSColin Finck     window = swapchain->win_handle;
577c2c66affSColin Finck     dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
578c2c66affSColin Finck 
579c2c66affSColin Finck     /* Front buffer coordinates are screen coordinates. Map them to the
580c2c66affSColin Finck      * destination window if not fullscreened. */
581c2c66affSColin Finck     if (swapchain->desc.windowed)
582c2c66affSColin Finck         ClientToScreen(window, &offset);
583c2c66affSColin Finck 
584c2c66affSColin Finck     TRACE("offset %s.\n", wine_dbgstr_point(&offset));
585c2c66affSColin Finck 
586c2c66affSColin Finck     SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
587c2c66affSColin Finck             swapchain->front_buffer->resource.height);
588c2c66affSColin Finck     IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
589c2c66affSColin Finck 
590c2c66affSColin Finck     BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
591c2c66affSColin Finck             draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
592c2c66affSColin Finck             src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
593c2c66affSColin Finck     ReleaseDC(window, dst_dc);
594c2c66affSColin Finck 
595c2c66affSColin Finck     SetRectEmpty(&swapchain->front_buffer_update);
596c2c66affSColin Finck }
597c2c66affSColin Finck 
swapchain_gdi_present(struct wined3d_swapchain * swapchain,const RECT * src_rect,const RECT * dst_rect,DWORD flags)598c2c66affSColin Finck static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
599c2c66affSColin Finck         const RECT *src_rect, const RECT *dst_rect, DWORD flags)
600c2c66affSColin Finck {
601*81cffd76SJoachim Henze     struct wined3d_surface *front, *back;
602c2c66affSColin Finck     HBITMAP bitmap;
603c2c66affSColin Finck     void *data;
604c2c66affSColin Finck     HDC dc;
605c2c66affSColin Finck 
606*81cffd76SJoachim Henze     front = swapchain->front_buffer->sub_resources[0].u.surface;
607*81cffd76SJoachim Henze     back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
608c2c66affSColin Finck 
609c2c66affSColin Finck     /* Flip the surface data. */
610c2c66affSColin Finck     dc = front->dc;
611c2c66affSColin Finck     bitmap = front->bitmap;
612*81cffd76SJoachim Henze     data = front->container->resource.heap_memory;
613c2c66affSColin Finck 
614c2c66affSColin Finck     front->dc = back->dc;
615c2c66affSColin Finck     front->bitmap = back->bitmap;
616*81cffd76SJoachim Henze     front->container->resource.heap_memory = back->container->resource.heap_memory;
617c2c66affSColin Finck 
618c2c66affSColin Finck     back->dc = dc;
619c2c66affSColin Finck     back->bitmap = bitmap;
620*81cffd76SJoachim Henze     back->container->resource.heap_memory = data;
621c2c66affSColin Finck 
622c2c66affSColin Finck     /* FPS support */
623c2c66affSColin Finck     if (TRACE_ON(fps))
624c2c66affSColin Finck     {
625c2c66affSColin Finck         static LONG prev_time, frames;
626c2c66affSColin Finck         DWORD time = GetTickCount();
627c2c66affSColin Finck 
628c2c66affSColin Finck         ++frames;
629c2c66affSColin Finck 
630c2c66affSColin Finck         /* every 1.5 seconds */
631c2c66affSColin Finck         if (time - prev_time > 1500)
632c2c66affSColin Finck         {
633c2c66affSColin Finck             TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
634c2c66affSColin Finck             prev_time = time;
635c2c66affSColin Finck             frames = 0;
636c2c66affSColin Finck         }
637c2c66affSColin Finck     }
638c2c66affSColin Finck 
639c2c66affSColin Finck     SetRect(&swapchain->front_buffer_update, 0, 0,
640c2c66affSColin Finck             swapchain->front_buffer->resource.width,
641c2c66affSColin Finck             swapchain->front_buffer->resource.height);
642c2c66affSColin Finck     swapchain_gdi_frontbuffer_updated(swapchain);
643c2c66affSColin Finck }
644c2c66affSColin Finck 
645c2c66affSColin Finck static const struct wined3d_swapchain_ops swapchain_gdi_ops =
646c2c66affSColin Finck {
647c2c66affSColin Finck     swapchain_gdi_present,
648c2c66affSColin Finck     swapchain_gdi_frontbuffer_updated,
649c2c66affSColin Finck };
650c2c66affSColin Finck 
swapchain_update_render_to_fbo(struct wined3d_swapchain * swapchain)651c2c66affSColin Finck static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
652c2c66affSColin Finck {
653c2c66affSColin Finck     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
654c2c66affSColin Finck         return;
655c2c66affSColin Finck 
656c2c66affSColin Finck     if (!swapchain->desc.backbuffer_count)
657c2c66affSColin Finck     {
658c2c66affSColin Finck         TRACE("Single buffered rendering.\n");
659c2c66affSColin Finck         swapchain->render_to_fbo = FALSE;
660c2c66affSColin Finck         return;
661c2c66affSColin Finck     }
662c2c66affSColin Finck 
663c2c66affSColin Finck     TRACE("Rendering to FBO.\n");
664c2c66affSColin Finck     swapchain->render_to_fbo = TRUE;
665c2c66affSColin Finck }
666c2c66affSColin Finck 
wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain * swapchain,enum wined3d_format_id format_id,enum wined3d_multisample_type * type,DWORD * quality)667c2c66affSColin Finck static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
668c2c66affSColin Finck         enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
669c2c66affSColin Finck {
670c2c66affSColin Finck     const struct wined3d_gl_info *gl_info;
671c2c66affSColin Finck     const struct wined3d_format *format;
672c2c66affSColin Finck     enum wined3d_multisample_type t;
673c2c66affSColin Finck 
674c2c66affSColin Finck     if (wined3d_settings.sample_count == ~0u)
675c2c66affSColin Finck         return;
676c2c66affSColin Finck 
677c2c66affSColin Finck     gl_info = &swapchain->device->adapter->gl_info;
678c2c66affSColin Finck     if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
679c2c66affSColin Finck         return;
680c2c66affSColin Finck 
681c2c66affSColin Finck     if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
682c2c66affSColin Finck         while (!(format->multisample_types & 1u << (t - 1)))
683c2c66affSColin Finck             ++t;
684c2c66affSColin Finck     TRACE("Using sample count %u.\n", t);
685c2c66affSColin Finck     *type = t;
686c2c66affSColin Finck     *quality = 0;
687c2c66affSColin Finck }
688c2c66affSColin Finck 
wined3d_swapchain_update_swap_interval_cs(void * object)689*81cffd76SJoachim Henze static void wined3d_swapchain_update_swap_interval_cs(void *object)
690c2c66affSColin Finck {
691*81cffd76SJoachim Henze     struct wined3d_swapchain *swapchain = object;
692c2c66affSColin Finck     const struct wined3d_gl_info *gl_info;
693c2c66affSColin Finck     struct wined3d_context *context;
694*81cffd76SJoachim Henze     int swap_interval;
695c2c66affSColin Finck 
696c2c66affSColin Finck     context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
697c2c66affSColin Finck     gl_info = context->gl_info;
698c2c66affSColin Finck 
699*81cffd76SJoachim Henze     switch (swapchain->desc.swap_interval)
700*81cffd76SJoachim Henze     {
701*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_IMMEDIATE:
702*81cffd76SJoachim Henze             swap_interval = 0;
703*81cffd76SJoachim Henze             break;
704*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_DEFAULT:
705*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_ONE:
706*81cffd76SJoachim Henze             swap_interval = 1;
707*81cffd76SJoachim Henze             break;
708*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_TWO:
709*81cffd76SJoachim Henze             swap_interval = 2;
710*81cffd76SJoachim Henze             break;
711*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_THREE:
712*81cffd76SJoachim Henze             swap_interval = 3;
713*81cffd76SJoachim Henze             break;
714*81cffd76SJoachim Henze         case WINED3DPRESENT_INTERVAL_FOUR:
715*81cffd76SJoachim Henze             swap_interval = 4;
716*81cffd76SJoachim Henze             break;
717*81cffd76SJoachim Henze         default:
718*81cffd76SJoachim Henze             FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
719*81cffd76SJoachim Henze             swap_interval = 1;
720*81cffd76SJoachim Henze     }
721*81cffd76SJoachim Henze 
722c2c66affSColin Finck     if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
723c2c66affSColin Finck     {
724c2c66affSColin Finck         if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
725*81cffd76SJoachim Henze             ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
726c2c66affSColin Finck                 swap_interval, context, GetLastError());
727c2c66affSColin Finck     }
728c2c66affSColin Finck 
729c2c66affSColin Finck     context_release(context);
730c2c66affSColin Finck }
731c2c66affSColin Finck 
wined3d_swapchain_cs_init(void * object)732c2c66affSColin Finck static void wined3d_swapchain_cs_init(void *object)
733c2c66affSColin Finck {
734c2c66affSColin Finck     struct wined3d_swapchain *swapchain = object;
735c2c66affSColin Finck     const struct wined3d_gl_info *gl_info;
736c2c66affSColin Finck     unsigned int i;
737c2c66affSColin Finck 
738c2c66affSColin Finck     static const enum wined3d_format_id formats[] =
739c2c66affSColin Finck     {
740c2c66affSColin Finck         WINED3DFMT_D24_UNORM_S8_UINT,
741c2c66affSColin Finck         WINED3DFMT_D32_UNORM,
742c2c66affSColin Finck         WINED3DFMT_R24_UNORM_X8_TYPELESS,
743c2c66affSColin Finck         WINED3DFMT_D16_UNORM,
744c2c66affSColin Finck         WINED3DFMT_S1_UINT_D15_UNORM,
745c2c66affSColin Finck     };
746c2c66affSColin Finck 
747c2c66affSColin Finck     gl_info = &swapchain->device->adapter->gl_info;
748c2c66affSColin Finck 
749c2c66affSColin Finck     /* Without ORM_FBO, switching the depth/stencil format is hard. Always
750c2c66affSColin Finck      * request a depth/stencil buffer in the likely case it's needed later. */
751c2c66affSColin Finck     for (i = 0; i < ARRAY_SIZE(formats); ++i)
752c2c66affSColin Finck     {
753c2c66affSColin Finck         swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
754c2c66affSColin Finck         if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
755c2c66affSColin Finck             break;
756c2c66affSColin Finck         TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
757c2c66affSColin Finck     }
758c2c66affSColin Finck 
759c2c66affSColin Finck     if (!swapchain->context[0])
760c2c66affSColin Finck     {
761c2c66affSColin Finck         WARN("Failed to create context.\n");
762c2c66affSColin Finck         return;
763c2c66affSColin Finck     }
764c2c66affSColin Finck     swapchain->num_contexts = 1;
765c2c66affSColin Finck 
766c2c66affSColin Finck     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
767c2c66affSColin Finck             && (!swapchain->desc.enable_auto_depth_stencil
768c2c66affSColin Finck             || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
769c2c66affSColin Finck         FIXME("Add OpenGL context recreation support.\n");
770c2c66affSColin Finck 
771c2c66affSColin Finck     context_release(swapchain->context[0]);
772c2c66affSColin Finck 
773*81cffd76SJoachim Henze     wined3d_swapchain_update_swap_interval_cs(swapchain);
774c2c66affSColin Finck }
775c2c66affSColin Finck 
swapchain_init(struct wined3d_swapchain * swapchain,struct wined3d_device * device,struct wined3d_swapchain_desc * desc,void * parent,const struct wined3d_parent_ops * parent_ops)776c2c66affSColin Finck static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
777c2c66affSColin Finck         struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
778c2c66affSColin Finck {
779c2c66affSColin Finck     const struct wined3d_adapter *adapter = device->adapter;
780c2c66affSColin Finck     struct wined3d_resource_desc texture_desc;
781c2c66affSColin Finck     BOOL displaymode_set = FALSE;
782c2c66affSColin Finck     DWORD texture_flags = 0;
783c2c66affSColin Finck     RECT client_rect;
784c2c66affSColin Finck     HWND window;
785c2c66affSColin Finck     HRESULT hr;
786*81cffd76SJoachim Henze     UINT i;
787c2c66affSColin Finck 
788c2c66affSColin Finck     if (desc->backbuffer_count > 1)
789c2c66affSColin Finck     {
790c2c66affSColin Finck         FIXME("The application requested more than one back buffer, this is not properly supported.\n"
791c2c66affSColin Finck                 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
792c2c66affSColin Finck     }
793c2c66affSColin Finck 
7949987f029SAmine Khaldi     if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
7959987f029SAmine Khaldi             && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
7969987f029SAmine Khaldi             && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
7979987f029SAmine Khaldi         FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
7989987f029SAmine Khaldi 
799c2c66affSColin Finck     if (device->wined3d->flags & WINED3D_NO3D)
800c2c66affSColin Finck         swapchain->swapchain_ops = &swapchain_gdi_ops;
801c2c66affSColin Finck     else
802c2c66affSColin Finck         swapchain->swapchain_ops = &swapchain_gl_ops;
803c2c66affSColin Finck 
804c2c66affSColin Finck     window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
805c2c66affSColin Finck 
806c2c66affSColin Finck     swapchain->device = device;
807c2c66affSColin Finck     swapchain->parent = parent;
808c2c66affSColin Finck     swapchain->parent_ops = parent_ops;
809c2c66affSColin Finck     swapchain->ref = 1;
810c2c66affSColin Finck     swapchain->win_handle = window;
811c2c66affSColin Finck     swapchain->device_window = window;
812c2c66affSColin Finck 
813c2c66affSColin Finck     if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
814c2c66affSColin Finck             adapter->ordinal, &swapchain->original_mode, NULL)))
815c2c66affSColin Finck     {
816c2c66affSColin Finck         ERR("Failed to get current display mode, hr %#x.\n", hr);
817c2c66affSColin Finck         goto err;
818c2c66affSColin Finck     }
819c2c66affSColin Finck     GetWindowRect(window, &swapchain->original_window_rect);
820c2c66affSColin Finck 
821c2c66affSColin Finck     GetClientRect(window, &client_rect);
822c2c66affSColin Finck     if (desc->windowed)
823c2c66affSColin Finck     {
824c2c66affSColin Finck         if (!desc->backbuffer_width)
825c2c66affSColin Finck         {
826*81cffd76SJoachim Henze             desc->backbuffer_width = client_rect.right;
827c2c66affSColin Finck             TRACE("Updating width to %u.\n", desc->backbuffer_width);
828c2c66affSColin Finck         }
829*81cffd76SJoachim Henze 
830c2c66affSColin Finck         if (!desc->backbuffer_height)
831c2c66affSColin Finck         {
832*81cffd76SJoachim Henze             desc->backbuffer_height = client_rect.bottom;
833c2c66affSColin Finck             TRACE("Updating height to %u.\n", desc->backbuffer_height);
834c2c66affSColin Finck         }
835c2c66affSColin Finck 
836c2c66affSColin Finck         if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
837c2c66affSColin Finck         {
838c2c66affSColin Finck             desc->backbuffer_format = swapchain->original_mode.format_id;
839c2c66affSColin Finck             TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
840c2c66affSColin Finck         }
841c2c66affSColin Finck     }
842c2c66affSColin Finck     swapchain->desc = *desc;
843c2c66affSColin Finck     wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
844c2c66affSColin Finck             &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
845c2c66affSColin Finck     swapchain_update_render_to_fbo(swapchain);
846c2c66affSColin Finck 
847c2c66affSColin Finck     TRACE("Creating front buffer.\n");
848c2c66affSColin Finck 
849c2c66affSColin Finck     texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
850c2c66affSColin Finck     texture_desc.format = swapchain->desc.backbuffer_format;
851c2c66affSColin Finck     texture_desc.multisample_type = swapchain->desc.multisample_type;
852c2c66affSColin Finck     texture_desc.multisample_quality = swapchain->desc.multisample_quality;
853c2c66affSColin Finck     texture_desc.usage = 0;
8549987f029SAmine Khaldi     texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
855c2c66affSColin Finck     texture_desc.width = swapchain->desc.backbuffer_width;
856c2c66affSColin Finck     texture_desc.height = swapchain->desc.backbuffer_height;
857c2c66affSColin Finck     texture_desc.depth = 1;
858c2c66affSColin Finck     texture_desc.size = 0;
859c2c66affSColin Finck 
860c2c66affSColin Finck     if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
861c2c66affSColin Finck         texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
862c2c66affSColin Finck 
863c2c66affSColin Finck     if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
864c2c66affSColin Finck             parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
865c2c66affSColin Finck     {
866c2c66affSColin Finck         WARN("Failed to create front buffer, hr %#x.\n", hr);
867c2c66affSColin Finck         goto err;
868c2c66affSColin Finck     }
869c2c66affSColin Finck 
870c2c66affSColin Finck     wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
871c2c66affSColin Finck     if (!(device->wined3d->flags & WINED3D_NO3D))
872c2c66affSColin Finck     {
873c2c66affSColin Finck         wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
874c2c66affSColin Finck         wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
875c2c66affSColin Finck     }
876c2c66affSColin Finck 
877c2c66affSColin Finck     /* MSDN says we're only allowed a single fullscreen swapchain per device,
878c2c66affSColin Finck      * so we should really check to see if there is a fullscreen swapchain
879c2c66affSColin Finck      * already. Does a single head count as full screen? */
880c2c66affSColin Finck     if (!desc->windowed)
881c2c66affSColin Finck     {
882c2c66affSColin Finck         if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
883c2c66affSColin Finck         {
884c2c66affSColin Finck             /* Change the display settings */
885c2c66affSColin Finck             swapchain->d3d_mode.width = desc->backbuffer_width;
886c2c66affSColin Finck             swapchain->d3d_mode.height = desc->backbuffer_height;
887c2c66affSColin Finck             swapchain->d3d_mode.format_id = desc->backbuffer_format;
888c2c66affSColin Finck             swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
889c2c66affSColin Finck             swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
890c2c66affSColin Finck 
891c2c66affSColin Finck             if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
892c2c66affSColin Finck                     adapter->ordinal, &swapchain->d3d_mode)))
893c2c66affSColin Finck             {
894c2c66affSColin Finck                 WARN("Failed to set display mode, hr %#x.\n", hr);
895c2c66affSColin Finck                 goto err;
896c2c66affSColin Finck             }
897c2c66affSColin Finck             displaymode_set = TRUE;
898c2c66affSColin Finck         }
899c2c66affSColin Finck         else
900c2c66affSColin Finck         {
901c2c66affSColin Finck             swapchain->d3d_mode = swapchain->original_mode;
902c2c66affSColin Finck         }
903c2c66affSColin Finck     }
904c2c66affSColin Finck 
905c2c66affSColin Finck     if (!(device->wined3d->flags & WINED3D_NO3D))
906c2c66affSColin Finck     {
9079987f029SAmine Khaldi         if (!(swapchain->context = heap_alloc(sizeof(*swapchain->context))))
908c2c66affSColin Finck         {
909c2c66affSColin Finck             ERR("Failed to create the context array.\n");
910c2c66affSColin Finck             hr = E_OUTOFMEMORY;
911c2c66affSColin Finck             goto err;
912c2c66affSColin Finck         }
913c2c66affSColin Finck 
914c2c66affSColin Finck         wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
915c2c66affSColin Finck         device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
916c2c66affSColin Finck 
917c2c66affSColin Finck         if (!swapchain->context[0])
918c2c66affSColin Finck         {
919c2c66affSColin Finck             hr = WINED3DERR_NOTAVAILABLE;
920c2c66affSColin Finck             goto err;
921c2c66affSColin Finck         }
922c2c66affSColin Finck     }
923c2c66affSColin Finck 
924c2c66affSColin Finck     if (swapchain->desc.backbuffer_count > 0)
925c2c66affSColin Finck     {
9269987f029SAmine Khaldi         if (!(swapchain->back_buffers = heap_calloc(swapchain->desc.backbuffer_count,
927c2c66affSColin Finck                 sizeof(*swapchain->back_buffers))))
928c2c66affSColin Finck         {
929c2c66affSColin Finck             ERR("Failed to allocate backbuffer array memory.\n");
930c2c66affSColin Finck             hr = E_OUTOFMEMORY;
931c2c66affSColin Finck             goto err;
932c2c66affSColin Finck         }
933c2c66affSColin Finck 
9349987f029SAmine Khaldi         texture_desc.usage = swapchain->desc.backbuffer_usage;
935c2c66affSColin Finck         for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
936c2c66affSColin Finck         {
937c2c66affSColin Finck             TRACE("Creating back buffer %u.\n", i);
938c2c66affSColin Finck             if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
939c2c66affSColin Finck                     parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
940c2c66affSColin Finck             {
941c2c66affSColin Finck                 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
942c2c66affSColin Finck                 swapchain->desc.backbuffer_count = i;
943c2c66affSColin Finck                 goto err;
944c2c66affSColin Finck             }
945c2c66affSColin Finck             wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
946c2c66affSColin Finck         }
947c2c66affSColin Finck     }
948c2c66affSColin Finck 
949c2c66affSColin Finck     /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
950c2c66affSColin Finck     if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
951c2c66affSColin Finck     {
952c2c66affSColin Finck         TRACE("Creating depth/stencil buffer.\n");
953c2c66affSColin Finck         if (!device->auto_depth_stencil_view)
954c2c66affSColin Finck         {
955c2c66affSColin Finck             struct wined3d_view_desc desc;
956c2c66affSColin Finck             struct wined3d_texture *ds;
957c2c66affSColin Finck 
958c2c66affSColin Finck             texture_desc.format = swapchain->desc.auto_depth_stencil_format;
959c2c66affSColin Finck             texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
960c2c66affSColin Finck 
961c2c66affSColin Finck             if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
962c2c66affSColin Finck                     device->device_parent, &texture_desc, texture_flags, &ds)))
963c2c66affSColin Finck             {
964c2c66affSColin Finck                 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
965c2c66affSColin Finck                 goto err;
966c2c66affSColin Finck             }
967c2c66affSColin Finck 
968c2c66affSColin Finck             desc.format_id = ds->resource.format->id;
969c2c66affSColin Finck             desc.flags = 0;
970c2c66affSColin Finck             desc.u.texture.level_idx = 0;
971c2c66affSColin Finck             desc.u.texture.level_count = 1;
972c2c66affSColin Finck             desc.u.texture.layer_idx = 0;
973c2c66affSColin Finck             desc.u.texture.layer_count = 1;
974c2c66affSColin Finck             hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
975c2c66affSColin Finck                     &device->auto_depth_stencil_view);
976c2c66affSColin Finck             wined3d_texture_decref(ds);
977c2c66affSColin Finck             if (FAILED(hr))
978c2c66affSColin Finck             {
979c2c66affSColin Finck                 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
980c2c66affSColin Finck                 goto err;
981c2c66affSColin Finck             }
982c2c66affSColin Finck         }
983c2c66affSColin Finck     }
984c2c66affSColin Finck 
985c2c66affSColin Finck     wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
986c2c66affSColin Finck 
987c2c66affSColin Finck     return WINED3D_OK;
988c2c66affSColin Finck 
989c2c66affSColin Finck err:
990c2c66affSColin Finck     if (displaymode_set)
991c2c66affSColin Finck     {
992c2c66affSColin Finck         if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
993c2c66affSColin Finck                 adapter->ordinal, &swapchain->original_mode)))
994c2c66affSColin Finck             ERR("Failed to restore display mode.\n");
995c2c66affSColin Finck         ClipCursor(NULL);
996c2c66affSColin Finck     }
997c2c66affSColin Finck 
998c2c66affSColin Finck     if (swapchain->back_buffers)
999c2c66affSColin Finck     {
1000c2c66affSColin Finck         for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1001c2c66affSColin Finck         {
1002c2c66affSColin Finck             if (swapchain->back_buffers[i])
1003c2c66affSColin Finck             {
1004c2c66affSColin Finck                 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1005c2c66affSColin Finck                 wined3d_texture_decref(swapchain->back_buffers[i]);
1006c2c66affSColin Finck             }
1007c2c66affSColin Finck         }
10089987f029SAmine Khaldi         heap_free(swapchain->back_buffers);
1009c2c66affSColin Finck     }
1010c2c66affSColin Finck 
1011c2c66affSColin Finck     wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
1012c2c66affSColin Finck     swapchain->device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1013c2c66affSColin Finck 
1014c2c66affSColin Finck     if (swapchain->front_buffer)
1015c2c66affSColin Finck     {
1016c2c66affSColin Finck         wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1017c2c66affSColin Finck         wined3d_texture_decref(swapchain->front_buffer);
1018c2c66affSColin Finck     }
1019c2c66affSColin Finck 
1020c2c66affSColin Finck     return hr;
1021c2c66affSColin Finck }
1022c2c66affSColin Finck 
wined3d_swapchain_create(struct wined3d_device * device,struct wined3d_swapchain_desc * desc,void * parent,const struct wined3d_parent_ops * parent_ops,struct wined3d_swapchain ** swapchain)1023c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1024c2c66affSColin Finck         void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1025c2c66affSColin Finck {
1026c2c66affSColin Finck     struct wined3d_swapchain *object;
1027c2c66affSColin Finck     HRESULT hr;
1028c2c66affSColin Finck 
1029c2c66affSColin Finck     TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1030c2c66affSColin Finck             device, desc, parent, parent_ops, swapchain);
1031c2c66affSColin Finck 
10329987f029SAmine Khaldi     if (!(object = heap_alloc_zero(sizeof(*object))))
1033c2c66affSColin Finck         return E_OUTOFMEMORY;
1034c2c66affSColin Finck 
1035c2c66affSColin Finck     hr = swapchain_init(object, device, desc, parent, parent_ops);
1036c2c66affSColin Finck     if (FAILED(hr))
1037c2c66affSColin Finck     {
1038c2c66affSColin Finck         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
10399987f029SAmine Khaldi         heap_free(object);
1040c2c66affSColin Finck         return hr;
1041c2c66affSColin Finck     }
1042c2c66affSColin Finck 
1043c2c66affSColin Finck     TRACE("Created swapchain %p.\n", object);
1044c2c66affSColin Finck     *swapchain = object;
1045c2c66affSColin Finck 
1046c2c66affSColin Finck     return WINED3D_OK;
1047c2c66affSColin Finck }
1048c2c66affSColin Finck 
swapchain_create_context(struct wined3d_swapchain * swapchain)1049c2c66affSColin Finck static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1050c2c66affSColin Finck {
1051c2c66affSColin Finck     struct wined3d_context **ctx_array;
1052c2c66affSColin Finck     struct wined3d_context *ctx;
1053c2c66affSColin Finck 
1054c2c66affSColin Finck     TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1055c2c66affSColin Finck 
1056c2c66affSColin Finck     if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1057c2c66affSColin Finck     {
1058c2c66affSColin Finck         ERR("Failed to create a new context for the swapchain\n");
1059c2c66affSColin Finck         return NULL;
1060c2c66affSColin Finck     }
1061c2c66affSColin Finck     context_release(ctx);
1062c2c66affSColin Finck 
10639987f029SAmine Khaldi     if (!(ctx_array = heap_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1064c2c66affSColin Finck     {
1065c2c66affSColin Finck         ERR("Out of memory when trying to allocate a new context array\n");
1066c2c66affSColin Finck         context_destroy(swapchain->device, ctx);
1067c2c66affSColin Finck         return NULL;
1068c2c66affSColin Finck     }
1069c2c66affSColin Finck     memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
10709987f029SAmine Khaldi     heap_free(swapchain->context);
1071c2c66affSColin Finck     ctx_array[swapchain->num_contexts] = ctx;
1072c2c66affSColin Finck     swapchain->context = ctx_array;
1073c2c66affSColin Finck     swapchain->num_contexts++;
1074c2c66affSColin Finck 
1075c2c66affSColin Finck     TRACE("Returning context %p\n", ctx);
1076c2c66affSColin Finck     return ctx;
1077c2c66affSColin Finck }
1078c2c66affSColin Finck 
swapchain_destroy_contexts(struct wined3d_swapchain * swapchain)1079c2c66affSColin Finck void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1080c2c66affSColin Finck {
1081c2c66affSColin Finck     unsigned int i;
1082c2c66affSColin Finck 
1083c2c66affSColin Finck     for (i = 0; i < swapchain->num_contexts; ++i)
1084c2c66affSColin Finck     {
1085c2c66affSColin Finck         context_destroy(swapchain->device, swapchain->context[i]);
1086c2c66affSColin Finck     }
10879987f029SAmine Khaldi     heap_free(swapchain->context);
1088c2c66affSColin Finck     swapchain->num_contexts = 0;
1089c2c66affSColin Finck     swapchain->context = NULL;
1090c2c66affSColin Finck }
1091c2c66affSColin Finck 
swapchain_get_context(struct wined3d_swapchain * swapchain)1092c2c66affSColin Finck struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1093c2c66affSColin Finck {
1094c2c66affSColin Finck     DWORD tid = GetCurrentThreadId();
1095c2c66affSColin Finck     unsigned int i;
1096c2c66affSColin Finck 
1097c2c66affSColin Finck     for (i = 0; i < swapchain->num_contexts; ++i)
1098c2c66affSColin Finck     {
1099c2c66affSColin Finck         if (swapchain->context[i]->tid == tid)
1100c2c66affSColin Finck             return swapchain->context[i];
1101c2c66affSColin Finck     }
1102c2c66affSColin Finck 
1103c2c66affSColin Finck     /* Create a new context for the thread */
1104c2c66affSColin Finck     return swapchain_create_context(swapchain);
1105c2c66affSColin Finck }
1106c2c66affSColin Finck 
swapchain_get_backup_dc(struct wined3d_swapchain * swapchain)1107c2c66affSColin Finck HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1108c2c66affSColin Finck {
1109c2c66affSColin Finck     if (!swapchain->backup_dc)
1110c2c66affSColin Finck     {
1111c2c66affSColin Finck         TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1112c2c66affSColin Finck 
1113c2c66affSColin Finck         if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1114c2c66affSColin Finck                 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1115c2c66affSColin Finck         {
1116c2c66affSColin Finck             ERR("Failed to create a window.\n");
1117c2c66affSColin Finck             return NULL;
1118c2c66affSColin Finck         }
1119c2c66affSColin Finck 
1120c2c66affSColin Finck         if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1121c2c66affSColin Finck         {
1122c2c66affSColin Finck             ERR("Failed to get a DC.\n");
1123c2c66affSColin Finck             DestroyWindow(swapchain->backup_wnd);
1124c2c66affSColin Finck             swapchain->backup_wnd = NULL;
1125c2c66affSColin Finck             return NULL;
1126c2c66affSColin Finck         }
1127c2c66affSColin Finck     }
1128c2c66affSColin Finck 
1129c2c66affSColin Finck     return swapchain->backup_dc;
1130c2c66affSColin Finck }
1131c2c66affSColin Finck 
swapchain_update_draw_bindings(struct wined3d_swapchain * swapchain)1132c2c66affSColin Finck void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1133c2c66affSColin Finck {
1134c2c66affSColin Finck     UINT i;
1135c2c66affSColin Finck 
1136c2c66affSColin Finck     wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1137c2c66affSColin Finck 
1138c2c66affSColin Finck     for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1139c2c66affSColin Finck     {
1140c2c66affSColin Finck         wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1141c2c66affSColin Finck     }
1142c2c66affSColin Finck }
1143c2c66affSColin Finck 
swapchain_update_swap_interval(struct wined3d_swapchain * swapchain)1144*81cffd76SJoachim Henze void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1145*81cffd76SJoachim Henze {
1146*81cffd76SJoachim Henze     wined3d_cs_init_object(swapchain->device->cs, wined3d_swapchain_update_swap_interval_cs, swapchain);
1147*81cffd76SJoachim Henze }
1148*81cffd76SJoachim Henze 
wined3d_swapchain_activate(struct wined3d_swapchain * swapchain,BOOL activate)1149c2c66affSColin Finck void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1150c2c66affSColin Finck {
1151c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
1152c2c66affSColin Finck     BOOL filter_messages = device->filter_messages;
1153c2c66affSColin Finck 
1154c2c66affSColin Finck     /* This code is not protected by the wined3d mutex, so it may run while
1155c2c66affSColin Finck      * wined3d_device_reset is active. Testing on Windows shows that changing
1156c2c66affSColin Finck      * focus during resets and resetting during focus change events causes
1157c2c66affSColin Finck      * the application to crash with an invalid memory access. */
1158c2c66affSColin Finck 
1159c2c66affSColin Finck     device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1160c2c66affSColin Finck 
1161c2c66affSColin Finck     if (activate)
1162c2c66affSColin Finck     {
1163c2c66affSColin Finck         if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1164c2c66affSColin Finck         {
1165c2c66affSColin Finck             /* The d3d versions do not agree on the exact messages here. D3d8 restores
1166c2c66affSColin Finck              * the window but leaves the size untouched, d3d9 sets the size on an
1167c2c66affSColin Finck              * invisible window, generates messages but doesn't change the window
1168c2c66affSColin Finck              * properties. The implementation follows d3d9.
1169c2c66affSColin Finck              *
1170c2c66affSColin Finck              * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1171c2c66affSColin Finck              * resume drawing after a focus loss. */
1172c2c66affSColin Finck             SetWindowPos(swapchain->device_window, NULL, 0, 0,
1173c2c66affSColin Finck                     swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1174c2c66affSColin Finck                     SWP_NOACTIVATE | SWP_NOZORDER);
1175c2c66affSColin Finck         }
1176c2c66affSColin Finck 
1177c2c66affSColin Finck         if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1178c2c66affSColin Finck         {
1179c2c66affSColin Finck             if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1180c2c66affSColin Finck                     device->adapter->ordinal, &swapchain->d3d_mode)))
1181c2c66affSColin Finck                 ERR("Failed to set display mode.\n");
1182c2c66affSColin Finck         }
1183c2c66affSColin Finck     }
1184c2c66affSColin Finck     else
1185c2c66affSColin Finck     {
1186c2c66affSColin Finck         if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1187c2c66affSColin Finck                 device->adapter->ordinal, NULL)))
1188c2c66affSColin Finck             ERR("Failed to set display mode.\n");
1189c2c66affSColin Finck 
1190c2c66affSColin Finck         swapchain->reapply_mode = TRUE;
1191c2c66affSColin Finck 
1192c2c66affSColin Finck         if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1193c2c66affSColin Finck                 && IsWindowVisible(swapchain->device_window))
1194c2c66affSColin Finck             ShowWindow(swapchain->device_window, SW_MINIMIZE);
1195c2c66affSColin Finck     }
1196c2c66affSColin Finck 
1197c2c66affSColin Finck     device->filter_messages = filter_messages;
1198c2c66affSColin Finck }
1199c2c66affSColin Finck 
wined3d_swapchain_resize_buffers(struct wined3d_swapchain * swapchain,unsigned int buffer_count,unsigned int width,unsigned int height,enum wined3d_format_id format_id,enum wined3d_multisample_type multisample_type,unsigned int multisample_quality)1200c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1201c2c66affSColin Finck         unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1202c2c66affSColin Finck         enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1203c2c66affSColin Finck {
1204c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
1205c2c66affSColin Finck     BOOL update_desc = FALSE;
1206c2c66affSColin Finck 
1207c2c66affSColin Finck     TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1208c2c66affSColin Finck             "multisample_type %#x, multisample_quality %#x.\n",
1209c2c66affSColin Finck             swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1210c2c66affSColin Finck             multisample_type, multisample_quality);
1211c2c66affSColin Finck 
1212c2c66affSColin Finck     wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1213c2c66affSColin Finck 
1214c2c66affSColin Finck     if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1215c2c66affSColin Finck         FIXME("Cannot change the back buffer count yet.\n");
1216c2c66affSColin Finck 
1217c2c66affSColin Finck     device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1218c2c66affSColin Finck 
1219c2c66affSColin Finck     if (!width || !height)
1220c2c66affSColin Finck     {
1221c2c66affSColin Finck         /* The application is requesting that either the swapchain width or
1222c2c66affSColin Finck          * height be set to the corresponding dimension in the window's
1223c2c66affSColin Finck          * client rect. */
1224c2c66affSColin Finck 
1225c2c66affSColin Finck         RECT client_rect;
1226c2c66affSColin Finck 
1227c2c66affSColin Finck         if (!swapchain->desc.windowed)
1228c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1229c2c66affSColin Finck 
1230c2c66affSColin Finck         if (!GetClientRect(swapchain->device_window, &client_rect))
1231c2c66affSColin Finck         {
1232c2c66affSColin Finck             ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1233c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1234c2c66affSColin Finck         }
1235c2c66affSColin Finck 
1236c2c66affSColin Finck         if (!width)
1237c2c66affSColin Finck             width = client_rect.right;
1238c2c66affSColin Finck 
1239c2c66affSColin Finck         if (!height)
1240c2c66affSColin Finck             height = client_rect.bottom;
1241c2c66affSColin Finck     }
1242c2c66affSColin Finck 
1243c2c66affSColin Finck     if (width != swapchain->desc.backbuffer_width
1244c2c66affSColin Finck             || height != swapchain->desc.backbuffer_height)
1245c2c66affSColin Finck     {
1246c2c66affSColin Finck         swapchain->desc.backbuffer_width = width;
1247c2c66affSColin Finck         swapchain->desc.backbuffer_height = height;
1248c2c66affSColin Finck         update_desc = TRUE;
1249c2c66affSColin Finck     }
1250c2c66affSColin Finck 
1251c2c66affSColin Finck     if (format_id == WINED3DFMT_UNKNOWN)
1252c2c66affSColin Finck     {
1253c2c66affSColin Finck         if (!swapchain->desc.windowed)
1254c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1255c2c66affSColin Finck         format_id = swapchain->original_mode.format_id;
1256c2c66affSColin Finck     }
1257c2c66affSColin Finck 
1258c2c66affSColin Finck     if (format_id != swapchain->desc.backbuffer_format)
1259c2c66affSColin Finck     {
1260c2c66affSColin Finck         swapchain->desc.backbuffer_format = format_id;
1261c2c66affSColin Finck         update_desc = TRUE;
1262c2c66affSColin Finck     }
1263c2c66affSColin Finck 
1264c2c66affSColin Finck     if (multisample_type != swapchain->desc.multisample_type
1265c2c66affSColin Finck             || multisample_quality != swapchain->desc.multisample_quality)
1266c2c66affSColin Finck     {
1267c2c66affSColin Finck         swapchain->desc.multisample_type = multisample_type;
1268c2c66affSColin Finck         swapchain->desc.multisample_quality = multisample_quality;
1269c2c66affSColin Finck         update_desc = TRUE;
1270c2c66affSColin Finck     }
1271c2c66affSColin Finck 
1272c2c66affSColin Finck     if (update_desc)
1273c2c66affSColin Finck     {
1274c2c66affSColin Finck         HRESULT hr;
1275c2c66affSColin Finck         UINT i;
1276c2c66affSColin Finck 
1277c2c66affSColin Finck         if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1278c2c66affSColin Finck                 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1279c2c66affSColin Finck                 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1280c2c66affSColin Finck             return hr;
1281c2c66affSColin Finck 
1282c2c66affSColin Finck         for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1283c2c66affSColin Finck         {
1284c2c66affSColin Finck             if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1285c2c66affSColin Finck                     swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1286c2c66affSColin Finck                     swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1287c2c66affSColin Finck                 return hr;
1288c2c66affSColin Finck         }
1289c2c66affSColin Finck     }
1290c2c66affSColin Finck 
1291c2c66affSColin Finck     swapchain_update_render_to_fbo(swapchain);
1292c2c66affSColin Finck     swapchain_update_draw_bindings(swapchain);
1293c2c66affSColin Finck 
1294c2c66affSColin Finck     return WINED3D_OK;
1295c2c66affSColin Finck }
1296c2c66affSColin Finck 
wined3d_swapchain_set_display_mode(struct wined3d_swapchain * swapchain,struct wined3d_display_mode * mode)1297c2c66affSColin Finck static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1298c2c66affSColin Finck         struct wined3d_display_mode *mode)
1299c2c66affSColin Finck {
1300c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
1301c2c66affSColin Finck     HRESULT hr;
1302c2c66affSColin Finck 
1303c2c66affSColin Finck     if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1304c2c66affSColin Finck     {
1305c2c66affSColin Finck         if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1306c2c66affSColin Finck                 device->adapter->ordinal, mode)))
1307c2c66affSColin Finck         {
1308c2c66affSColin Finck             WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1309c2c66affSColin Finck         }
1310c2c66affSColin Finck     }
1311c2c66affSColin Finck 
1312c2c66affSColin Finck     if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1313c2c66affSColin Finck             device->adapter->ordinal, mode)))
1314c2c66affSColin Finck     {
1315c2c66affSColin Finck         WARN("Failed to set display mode, hr %#x.\n", hr);
1316c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1317c2c66affSColin Finck     }
1318c2c66affSColin Finck 
1319c2c66affSColin Finck     return WINED3D_OK;
1320c2c66affSColin Finck }
1321c2c66affSColin Finck 
wined3d_swapchain_resize_target(struct wined3d_swapchain * swapchain,const struct wined3d_display_mode * mode)1322c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1323c2c66affSColin Finck         const struct wined3d_display_mode *mode)
1324c2c66affSColin Finck {
1325c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
1326c2c66affSColin Finck     struct wined3d_display_mode actual_mode;
1327c2c66affSColin Finck     RECT original_window_rect, window_rect;
1328c2c66affSColin Finck     HRESULT hr;
1329c2c66affSColin Finck 
1330c2c66affSColin Finck     TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1331c2c66affSColin Finck 
1332c2c66affSColin Finck     if (swapchain->desc.windowed)
1333c2c66affSColin Finck     {
1334c2c66affSColin Finck         SetRect(&window_rect, 0, 0, mode->width, mode->height);
1335c2c66affSColin Finck         AdjustWindowRectEx(&window_rect,
1336c2c66affSColin Finck                 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1337c2c66affSColin Finck                 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1338c2c66affSColin Finck         SetRect(&window_rect, 0, 0,
1339c2c66affSColin Finck                 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1340c2c66affSColin Finck         GetWindowRect(swapchain->device_window, &original_window_rect);
1341c2c66affSColin Finck         OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1342c2c66affSColin Finck     }
1343c2c66affSColin Finck     else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1344c2c66affSColin Finck     {
1345c2c66affSColin Finck         actual_mode = *mode;
1346c2c66affSColin Finck         if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1347c2c66affSColin Finck             return hr;
1348c2c66affSColin Finck         SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1349c2c66affSColin Finck     }
1350c2c66affSColin Finck     else
1351c2c66affSColin Finck     {
1352c2c66affSColin Finck         if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1353c2c66affSColin Finck                 &actual_mode, NULL)))
1354c2c66affSColin Finck         {
1355c2c66affSColin Finck             ERR("Failed to get display mode, hr %#x.\n", hr);
1356c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1357c2c66affSColin Finck         }
1358c2c66affSColin Finck 
1359c2c66affSColin Finck         SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1360c2c66affSColin Finck     }
1361c2c66affSColin Finck 
1362c2c66affSColin Finck     MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1363c2c66affSColin Finck             window_rect.right - window_rect.left,
1364c2c66affSColin Finck             window_rect.bottom - window_rect.top, TRUE);
1365c2c66affSColin Finck 
1366c2c66affSColin Finck     return WINED3D_OK;
1367c2c66affSColin Finck }
1368c2c66affSColin Finck 
wined3d_swapchain_set_fullscreen(struct wined3d_swapchain * swapchain,const struct wined3d_swapchain_desc * swapchain_desc,const struct wined3d_display_mode * mode)1369c2c66affSColin Finck HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1370c2c66affSColin Finck         const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1371c2c66affSColin Finck {
1372c2c66affSColin Finck     struct wined3d_device *device = swapchain->device;
1373c2c66affSColin Finck     struct wined3d_display_mode actual_mode;
1374c2c66affSColin Finck     HRESULT hr;
1375c2c66affSColin Finck 
1376c2c66affSColin Finck     TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1377c2c66affSColin Finck 
1378c2c66affSColin Finck     if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1379c2c66affSColin Finck     {
1380c2c66affSColin Finck         if (mode)
1381c2c66affSColin Finck         {
1382c2c66affSColin Finck             actual_mode = *mode;
1383c2c66affSColin Finck         }
1384c2c66affSColin Finck         else
1385c2c66affSColin Finck         {
1386c2c66affSColin Finck             if (!swapchain_desc->windowed)
1387c2c66affSColin Finck             {
1388c2c66affSColin Finck                 actual_mode.width = swapchain_desc->backbuffer_width;
1389c2c66affSColin Finck                 actual_mode.height = swapchain_desc->backbuffer_height;
1390c2c66affSColin Finck                 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1391c2c66affSColin Finck                 actual_mode.format_id = swapchain_desc->backbuffer_format;
1392c2c66affSColin Finck                 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1393c2c66affSColin Finck             }
1394c2c66affSColin Finck             else
1395c2c66affSColin Finck             {
1396c2c66affSColin Finck                 actual_mode = swapchain->original_mode;
1397c2c66affSColin Finck             }
1398c2c66affSColin Finck         }
1399c2c66affSColin Finck 
1400c2c66affSColin Finck         if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1401c2c66affSColin Finck             return hr;
1402c2c66affSColin Finck     }
1403c2c66affSColin Finck     else
1404c2c66affSColin Finck     {
1405c2c66affSColin Finck         if (mode)
1406c2c66affSColin Finck             WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1407c2c66affSColin Finck 
1408c2c66affSColin Finck         if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1409c2c66affSColin Finck                 &actual_mode, NULL)))
1410c2c66affSColin Finck         {
1411c2c66affSColin Finck             ERR("Failed to get display mode, hr %#x.\n", hr);
1412c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1413c2c66affSColin Finck         }
1414c2c66affSColin Finck     }
1415c2c66affSColin Finck 
1416c2c66affSColin Finck     if (!swapchain_desc->windowed)
1417c2c66affSColin Finck     {
1418c2c66affSColin Finck         unsigned int width = actual_mode.width;
1419c2c66affSColin Finck         unsigned int height = actual_mode.height;
1420c2c66affSColin Finck 
1421c2c66affSColin Finck         if (swapchain->desc.windowed)
1422c2c66affSColin Finck         {
1423c2c66affSColin Finck             /* Switch from windowed to fullscreen */
1424c2c66affSColin Finck             HWND focus_window = device->create_parms.focus_window;
1425c2c66affSColin Finck             if (!focus_window)
1426c2c66affSColin Finck                 focus_window = swapchain->device_window;
1427c2c66affSColin Finck             if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1428c2c66affSColin Finck             {
1429c2c66affSColin Finck                 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1430c2c66affSColin Finck                 return hr;
1431c2c66affSColin Finck             }
1432c2c66affSColin Finck 
1433c2c66affSColin Finck             wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1434c2c66affSColin Finck         }
1435c2c66affSColin Finck         else
1436c2c66affSColin Finck         {
1437c2c66affSColin Finck             /* Fullscreen -> fullscreen mode change */
1438c2c66affSColin Finck             BOOL filter_messages = device->filter_messages;
1439c2c66affSColin Finck             device->filter_messages = TRUE;
1440c2c66affSColin Finck 
1441c2c66affSColin Finck             MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1442d6ac0a71SAmine Khaldi             ShowWindow(swapchain->device_window, SW_SHOW);
1443c2c66affSColin Finck 
1444c2c66affSColin Finck             device->filter_messages = filter_messages;
1445c2c66affSColin Finck         }
1446c2c66affSColin Finck         swapchain->d3d_mode = actual_mode;
1447c2c66affSColin Finck     }
1448c2c66affSColin Finck     else if (!swapchain->desc.windowed)
1449c2c66affSColin Finck     {
1450c2c66affSColin Finck         /* Fullscreen -> windowed switch */
1451c2c66affSColin Finck         RECT *window_rect = NULL;
1452c2c66affSColin Finck         if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1453c2c66affSColin Finck             window_rect = &swapchain->original_window_rect;
1454c2c66affSColin Finck         wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1455c2c66affSColin Finck         wined3d_device_release_focus_window(device);
1456c2c66affSColin Finck     }
1457c2c66affSColin Finck 
1458c2c66affSColin Finck     swapchain->desc.windowed = swapchain_desc->windowed;
1459c2c66affSColin Finck 
1460c2c66affSColin Finck     return WINED3D_OK;
1461c2c66affSColin Finck }
1462