dmusic: Sync up the dmobject.[ch] files.
[wine.git] / dlls / wined3d / swapchain.c
blob9df40abc70ce495750e9c486596edf7dcbe9244a
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 static void wined3d_swapchain_destroy_object(void *object)
32 swapchain_destroy_contexts(object);
35 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
37 HRESULT hr;
38 UINT i;
40 TRACE("Destroying swapchain %p.\n", swapchain);
42 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
44 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
45 * is the last buffer to be destroyed, FindContext() depends on that. */
46 if (swapchain->front_buffer)
48 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
49 if (wined3d_texture_decref(swapchain->front_buffer))
50 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
51 swapchain->front_buffer = NULL;
54 if (swapchain->back_buffers)
56 i = swapchain->desc.backbuffer_count;
58 while (i--)
60 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
61 if (wined3d_texture_decref(swapchain->back_buffers[i]))
62 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
64 heap_free(swapchain->back_buffers);
65 swapchain->back_buffers = NULL;
68 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
69 swapchain->device->cs->ops->finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
71 /* Restore the screen resolution if we rendered in fullscreen.
72 * This will restore the screen resolution to what it was before creating
73 * the swapchain. In case of d3d8 and d3d9 this will be the original
74 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
75 * sets the resolution before starting up Direct3D, thus orig_width and
76 * orig_height will be equal to the modes in the presentation params. */
77 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
79 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
80 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
81 ERR("Failed to restore display mode, hr %#x.\n", hr);
83 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
85 wined3d_device_restore_fullscreen_window(swapchain->device, swapchain->device_window,
86 &swapchain->original_window_rect);
87 wined3d_device_release_focus_window(swapchain->device);
91 if (swapchain->backup_dc)
93 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
95 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
96 DestroyWindow(swapchain->backup_wnd);
100 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedIncrement(&swapchain->ref);
104 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
106 return refcount;
109 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
111 ULONG refcount = InterlockedDecrement(&swapchain->ref);
113 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
115 if (!refcount)
117 struct wined3d_device *device = swapchain->device;
119 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
121 swapchain_cleanup(swapchain);
122 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
123 heap_free(swapchain);
126 return refcount;
129 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
131 TRACE("swapchain %p.\n", swapchain);
133 return swapchain->parent;
136 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
138 struct wined3d_device *device = swapchain->device;
140 if (!window)
141 window = swapchain->device_window;
142 if (window == swapchain->win_handle)
143 return;
145 TRACE("Setting swapchain %p window from %p to %p.\n",
146 swapchain, swapchain->win_handle, window);
148 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
150 swapchain->win_handle = window;
153 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
154 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
155 unsigned int swap_interval, DWORD flags)
157 RECT s, d;
159 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
160 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
161 dst_window_override, flags);
163 if (flags)
164 FIXME("Ignoring flags %#x.\n", flags);
166 if (!swapchain->back_buffers)
168 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
169 return WINED3DERR_INVALIDCALL;
172 if (!src_rect)
174 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
175 swapchain->desc.backbuffer_height);
176 src_rect = &s;
179 if (!dst_rect)
181 GetClientRect(swapchain->win_handle, &d);
182 dst_rect = &d;
185 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
186 dst_rect, dst_window_override, swap_interval, flags);
188 return WINED3D_OK;
191 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
192 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
194 RECT src_rect, dst_rect;
196 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
198 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
199 dst_rect = src_rect;
201 if (swapchain->desc.windowed)
203 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
204 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
205 wine_dbgstr_rect(&dst_rect));
208 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
209 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
212 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
213 UINT back_buffer_idx)
215 TRACE("swapchain %p, back_buffer_idx %u.\n",
216 swapchain, back_buffer_idx);
218 /* Return invalid if there is no backbuffer array, otherwise it will
219 * crash when ddraw is used (there swapchain->back_buffers is always
220 * NULL). We need this because this function is called from
221 * stateblock_init_default_state() to get the default scissorrect
222 * dimensions. */
223 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
225 WARN("Invalid back buffer index.\n");
226 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
227 * here in wined3d to avoid problems in other libs. */
228 return NULL;
231 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
233 return swapchain->back_buffers[back_buffer_idx];
236 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
237 struct wined3d_raster_status *raster_status)
239 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
241 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
242 swapchain->device->adapter->ordinal, raster_status);
245 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
246 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
248 HRESULT hr;
250 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
252 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
253 swapchain->device->adapter->ordinal, mode, rotation);
255 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
256 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
258 return hr;
261 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
263 TRACE("swapchain %p.\n", swapchain);
265 return swapchain->device;
268 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
269 struct wined3d_swapchain_desc *desc)
271 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
273 *desc = swapchain->desc;
276 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
277 DWORD flags, const struct wined3d_gamma_ramp *ramp)
279 HDC dc;
281 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
283 if (flags)
284 FIXME("Ignoring flags %#x.\n", flags);
286 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
287 SetDeviceGammaRamp(dc, (void *)ramp);
288 ReleaseDC(swapchain->device_window, dc);
290 return WINED3D_OK;
293 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
295 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
296 swapchain->palette = palette;
299 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
300 struct wined3d_gamma_ramp *ramp)
302 HDC dc;
304 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
306 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
307 GetDeviceGammaRamp(dc, ramp);
308 ReleaseDC(swapchain->device_window, dc);
310 return WINED3D_OK;
313 /* A GL context is provided by the caller */
314 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
315 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
317 struct wined3d_texture *texture = swapchain->back_buffers[0];
318 struct wined3d_device *device = swapchain->device;
319 enum wined3d_texture_filter_type filter;
320 DWORD location;
322 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
323 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
325 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
326 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
327 || is_complex_fixup(texture->resource.format->color_fixup))
328 filter = WINED3D_TEXF_NONE;
329 else
330 filter = WINED3D_TEXF_LINEAR;
332 location = WINED3D_LOCATION_TEXTURE_RGB;
333 if (texture->resource.multisample_type)
334 location = WINED3D_LOCATION_RB_RESOLVED;
336 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
337 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
338 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
339 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
342 /* Context activation is done by the caller. */
343 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
345 struct wined3d_texture_sub_resource *sub_resource;
346 struct wined3d_texture *texture, *texture_prev;
347 struct gl_texture tex0;
348 GLuint rb0;
349 DWORD locations0;
350 unsigned int i;
351 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
353 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
354 return;
356 texture_prev = swapchain->back_buffers[0];
358 /* Back buffer 0 is already in the draw binding. */
359 tex0 = texture_prev->texture_rgb;
360 rb0 = texture_prev->rb_multisample;
361 locations0 = texture_prev->sub_resources[0].locations;
363 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
365 texture = swapchain->back_buffers[i];
366 sub_resource = &texture->sub_resources[0];
368 if (!(sub_resource->locations & supported_locations))
369 wined3d_texture_load_location(texture, 0, context, texture->resource.draw_binding);
371 texture_prev->texture_rgb = texture->texture_rgb;
372 texture_prev->rb_multisample = texture->rb_multisample;
374 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
375 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
377 texture_prev = texture;
380 texture_prev->texture_rgb = tex0;
381 texture_prev->rb_multisample = rb0;
383 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
384 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
386 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
389 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
390 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
392 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
393 const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
394 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
395 const struct wined3d_gl_info *gl_info;
396 struct wined3d_texture *logo_texture;
397 struct wined3d_context *context;
398 BOOL render_to_fbo;
400 context = context_acquire(swapchain->device, back_buffer, 0);
401 if (!context->valid)
403 context_release(context);
404 WARN("Invalid context, skipping present.\n");
405 return;
408 gl_info = context->gl_info;
410 if ((logo_texture = swapchain->device->logo_texture))
412 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
414 /* Blit the logo into the upper left corner of the drawable. */
415 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
416 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
419 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
420 && !swapchain->device->hardwareCursor)
422 RECT dst_rect =
424 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
425 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
426 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
427 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
429 RECT src_rect =
431 0, 0,
432 swapchain->device->cursor_texture->resource.width,
433 swapchain->device->cursor_texture->resource.height
435 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
437 TRACE("Rendering the software cursor.\n");
439 if (swapchain->desc.windowed)
440 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
441 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
442 wined3d_texture_blt(back_buffer, 0, &dst_rect,
443 swapchain->device->cursor_texture, 0, &src_rect,
444 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
447 TRACE("Presenting HDC %p.\n", context->hdc);
449 if (!(render_to_fbo = swapchain->render_to_fbo)
450 && (src_rect->left || src_rect->top
451 || src_rect->right != swapchain->desc.backbuffer_width
452 || src_rect->bottom != swapchain->desc.backbuffer_height
453 || dst_rect->left || dst_rect->top
454 || dst_rect->right != swapchain->desc.backbuffer_width
455 || dst_rect->bottom != swapchain->desc.backbuffer_height))
456 render_to_fbo = TRUE;
458 /* Rendering to a window of different size, presenting partial rectangles,
459 * or rendering to a different window needs help from FBO_blit or a textured
460 * draw. Render the swapchain to a FBO in the future.
462 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
463 * all these issues - this fails if the window is smaller than the backbuffer.
465 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
467 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
468 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
469 swapchain->render_to_fbo = TRUE;
470 swapchain_update_draw_bindings(swapchain);
472 else
474 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
477 if (swapchain->render_to_fbo)
478 swapchain_blit(swapchain, context, src_rect, dst_rect);
480 if (swapchain->num_contexts > 1)
481 gl_info->gl_ops.gl.p_glFinish();
483 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
484 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
486 wined3d_swapchain_rotate(swapchain, context);
488 TRACE("SwapBuffers called, Starting new frame\n");
489 /* FPS support */
490 if (TRACE_ON(fps))
492 DWORD time = GetTickCount();
493 ++swapchain->frames;
495 /* every 1.5 seconds */
496 if (time - swapchain->prev_time > 1500)
498 TRACE_(fps)("%p @ approx %.2ffps\n",
499 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
500 swapchain->prev_time = time;
501 swapchain->frames = 0;
505 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
506 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
507 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
508 * and INTEXTURE copies can keep their old content if they have any defined content.
509 * If the swapeffect is COPY, the content remains the same.
511 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
512 * up to date and hope WGL flipped front and back buffers and read this data into
513 * the FBO. Don't bother about this for now. */
514 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD
515 || swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
516 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
517 0, WINED3D_LOCATION_DISCARDED);
519 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
521 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
523 if ((swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
524 || ds->flags & WINED3D_TEXTURE_DISCARD))
525 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
528 context_release(context);
531 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
533 struct wined3d_texture *front_buffer = swapchain->front_buffer;
534 struct wined3d_context *context;
536 context = context_acquire(swapchain->device, front_buffer, 0);
537 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
538 context_release(context);
539 SetRectEmpty(&swapchain->front_buffer_update);
542 static const struct wined3d_swapchain_ops swapchain_gl_ops =
544 swapchain_gl_present,
545 swapchain_gl_frontbuffer_updated,
548 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
550 struct wined3d_dc_info *front;
551 POINT offset = {0, 0};
552 HDC src_dc, dst_dc;
553 RECT draw_rect;
554 HWND window;
556 TRACE("swapchain %p.\n", swapchain);
558 front = &swapchain->front_buffer->dc_info[0];
559 if (swapchain->palette)
560 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
562 if (swapchain->front_buffer->resource.map_count)
563 ERR("Trying to blit a mapped surface.\n");
565 TRACE("Copying surface %p to screen.\n", front);
567 src_dc = front->dc;
568 window = swapchain->win_handle;
569 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
571 /* Front buffer coordinates are screen coordinates. Map them to the
572 * destination window if not fullscreened. */
573 if (swapchain->desc.windowed)
574 ClientToScreen(window, &offset);
576 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
578 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
579 swapchain->front_buffer->resource.height);
580 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
582 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
583 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
584 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
585 ReleaseDC(window, dst_dc);
587 SetRectEmpty(&swapchain->front_buffer_update);
590 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
591 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
593 struct wined3d_dc_info *front, *back;
594 HBITMAP bitmap;
595 void *data;
596 HDC dc;
598 front = &swapchain->front_buffer->dc_info[0];
599 back = &swapchain->back_buffers[0]->dc_info[0];
601 /* Flip the surface data. */
602 dc = front->dc;
603 bitmap = front->bitmap;
604 data = swapchain->front_buffer->resource.heap_memory;
606 front->dc = back->dc;
607 front->bitmap = back->bitmap;
608 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
610 back->dc = dc;
611 back->bitmap = bitmap;
612 swapchain->back_buffers[0]->resource.heap_memory = data;
614 /* FPS support */
615 if (TRACE_ON(fps))
617 static LONG prev_time, frames;
618 DWORD time = GetTickCount();
620 ++frames;
622 /* every 1.5 seconds */
623 if (time - prev_time > 1500)
625 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
626 prev_time = time;
627 frames = 0;
631 SetRect(&swapchain->front_buffer_update, 0, 0,
632 swapchain->front_buffer->resource.width,
633 swapchain->front_buffer->resource.height);
634 swapchain_gdi_frontbuffer_updated(swapchain);
637 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
639 swapchain_gdi_present,
640 swapchain_gdi_frontbuffer_updated,
643 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
645 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
646 return;
648 if (!swapchain->desc.backbuffer_count)
650 TRACE("Single buffered rendering.\n");
651 swapchain->render_to_fbo = FALSE;
652 return;
655 TRACE("Rendering to FBO.\n");
656 swapchain->render_to_fbo = TRUE;
659 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
660 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
662 const struct wined3d_gl_info *gl_info;
663 const struct wined3d_format *format;
664 enum wined3d_multisample_type t;
666 if (wined3d_settings.sample_count == ~0u)
667 return;
669 gl_info = &swapchain->device->adapter->gl_info;
670 if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
671 return;
673 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
674 while (!(format->multisample_types & 1u << (t - 1)))
675 ++t;
676 TRACE("Using sample count %u.\n", t);
677 *type = t;
678 *quality = 0;
681 void wined3d_swapchain_set_swap_interval(struct wined3d_swapchain *swapchain,
682 unsigned int swap_interval)
684 const struct wined3d_gl_info *gl_info;
685 struct wined3d_context *context;
687 swap_interval = swap_interval <= 4 ? swap_interval : 1;
688 if (swapchain->swap_interval == swap_interval)
689 return;
691 swapchain->swap_interval = swap_interval;
693 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
694 gl_info = context->gl_info;
696 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
698 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
699 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x.\n",
700 swap_interval, context, GetLastError());
703 context_release(context);
706 static void wined3d_swapchain_cs_init(void *object)
708 struct wined3d_swapchain *swapchain = object;
709 const struct wined3d_gl_info *gl_info;
710 unsigned int i;
712 static const enum wined3d_format_id formats[] =
714 WINED3DFMT_D24_UNORM_S8_UINT,
715 WINED3DFMT_D32_UNORM,
716 WINED3DFMT_R24_UNORM_X8_TYPELESS,
717 WINED3DFMT_D16_UNORM,
718 WINED3DFMT_S1_UINT_D15_UNORM,
721 gl_info = &swapchain->device->adapter->gl_info;
723 /* Without ORM_FBO, switching the depth/stencil format is hard. Always
724 * request a depth/stencil buffer in the likely case it's needed later. */
725 for (i = 0; i < ARRAY_SIZE(formats); ++i)
727 swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
728 if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
729 break;
730 TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
733 if (!swapchain->context[0])
735 WARN("Failed to create context.\n");
736 return;
738 swapchain->num_contexts = 1;
740 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
741 && (!swapchain->desc.enable_auto_depth_stencil
742 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
743 FIXME("Add OpenGL context recreation support.\n");
745 context_release(swapchain->context[0]);
748 void swapchain_set_max_frame_latency(struct wined3d_swapchain *swapchain, const struct wined3d_device *device)
750 /* Subtract 1 for the implicit OpenGL latency. */
751 swapchain->max_frame_latency = device->max_frame_latency >= 2 ? device->max_frame_latency - 1 : 1;
754 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
755 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
757 const struct wined3d_adapter *adapter = device->adapter;
758 struct wined3d_resource_desc texture_desc;
759 BOOL displaymode_set = FALSE;
760 DWORD texture_flags = 0;
761 RECT client_rect;
762 unsigned int i;
763 HWND window;
764 HRESULT hr;
766 if (desc->backbuffer_count > 1)
768 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
769 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
772 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
773 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
774 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
775 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
777 if (device->wined3d->flags & WINED3D_NO3D)
778 swapchain->swapchain_ops = &swapchain_gdi_ops;
779 else
780 swapchain->swapchain_ops = &swapchain_gl_ops;
782 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
784 swapchain->device = device;
785 swapchain->parent = parent;
786 swapchain->parent_ops = parent_ops;
787 swapchain->ref = 1;
788 swapchain->win_handle = window;
789 swapchain->device_window = window;
790 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
791 swapchain_set_max_frame_latency(swapchain, device);
793 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
794 adapter->ordinal, &swapchain->original_mode, NULL)))
796 ERR("Failed to get current display mode, hr %#x.\n", hr);
797 goto err;
799 GetWindowRect(window, &swapchain->original_window_rect);
801 GetClientRect(window, &client_rect);
802 if (desc->windowed)
804 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
806 if (!desc->backbuffer_width)
808 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
809 TRACE("Updating width to %u.\n", desc->backbuffer_width);
811 if (!desc->backbuffer_height)
813 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
814 TRACE("Updating height to %u.\n", desc->backbuffer_height);
817 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
819 desc->backbuffer_format = swapchain->original_mode.format_id;
820 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
823 swapchain->desc = *desc;
824 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
825 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
826 swapchain_update_render_to_fbo(swapchain);
828 TRACE("Creating front buffer.\n");
830 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
831 texture_desc.format = swapchain->desc.backbuffer_format;
832 texture_desc.multisample_type = swapchain->desc.multisample_type;
833 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
834 texture_desc.usage = 0;
835 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
836 texture_desc.width = swapchain->desc.backbuffer_width;
837 texture_desc.height = swapchain->desc.backbuffer_height;
838 texture_desc.depth = 1;
839 texture_desc.size = 0;
841 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
842 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
844 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
845 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
847 WARN("Failed to create front buffer, hr %#x.\n", hr);
848 goto err;
851 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
852 if (!(device->wined3d->flags & WINED3D_NO3D))
854 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
855 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
858 /* MSDN says we're only allowed a single fullscreen swapchain per device,
859 * so we should really check to see if there is a fullscreen swapchain
860 * already. Does a single head count as full screen? */
861 if (!desc->windowed)
863 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
865 /* Change the display settings */
866 swapchain->d3d_mode.width = desc->backbuffer_width;
867 swapchain->d3d_mode.height = desc->backbuffer_height;
868 swapchain->d3d_mode.format_id = desc->backbuffer_format;
869 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
870 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
872 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
873 adapter->ordinal, &swapchain->d3d_mode)))
875 WARN("Failed to set display mode, hr %#x.\n", hr);
876 goto err;
878 displaymode_set = TRUE;
880 else
882 swapchain->d3d_mode = swapchain->original_mode;
886 if (!(device->wined3d->flags & WINED3D_NO3D))
888 if (!(swapchain->context = heap_alloc(sizeof(*swapchain->context))))
890 ERR("Failed to create the context array.\n");
891 hr = E_OUTOFMEMORY;
892 goto err;
895 wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
896 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
898 if (!swapchain->context[0])
900 hr = WINED3DERR_NOTAVAILABLE;
901 goto err;
905 if (swapchain->desc.backbuffer_count > 0)
907 if (!(swapchain->back_buffers = heap_calloc(swapchain->desc.backbuffer_count,
908 sizeof(*swapchain->back_buffers))))
910 ERR("Failed to allocate backbuffer array memory.\n");
911 hr = E_OUTOFMEMORY;
912 goto err;
915 texture_desc.usage = swapchain->desc.backbuffer_usage;
916 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
918 TRACE("Creating back buffer %u.\n", i);
919 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
920 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
922 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
923 swapchain->desc.backbuffer_count = i;
924 goto err;
926 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
930 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
931 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
933 TRACE("Creating depth/stencil buffer.\n");
934 if (!device->auto_depth_stencil_view)
936 struct wined3d_view_desc desc;
937 struct wined3d_texture *ds;
939 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
940 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
942 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
943 device->device_parent, &texture_desc, texture_flags, &ds)))
945 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
946 goto err;
949 desc.format_id = ds->resource.format->id;
950 desc.flags = 0;
951 desc.u.texture.level_idx = 0;
952 desc.u.texture.level_count = 1;
953 desc.u.texture.layer_idx = 0;
954 desc.u.texture.layer_count = 1;
955 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
956 &device->auto_depth_stencil_view);
957 wined3d_texture_decref(ds);
958 if (FAILED(hr))
960 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
961 goto err;
966 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
968 return WINED3D_OK;
970 err:
971 if (displaymode_set)
973 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
974 adapter->ordinal, &swapchain->original_mode)))
975 ERR("Failed to restore display mode.\n");
976 ClipCursor(NULL);
979 if (swapchain->back_buffers)
981 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
983 if (swapchain->back_buffers[i])
985 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
986 wined3d_texture_decref(swapchain->back_buffers[i]);
989 heap_free(swapchain->back_buffers);
992 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
993 swapchain->device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
995 if (swapchain->front_buffer)
997 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
998 wined3d_texture_decref(swapchain->front_buffer);
1001 return hr;
1004 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1005 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1007 struct wined3d_swapchain *object;
1008 HRESULT hr;
1010 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1011 device, desc, parent, parent_ops, swapchain);
1013 if (!(object = heap_alloc_zero(sizeof(*object))))
1014 return E_OUTOFMEMORY;
1016 hr = swapchain_init(object, device, desc, parent, parent_ops);
1017 if (FAILED(hr))
1019 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1020 heap_free(object);
1021 return hr;
1024 TRACE("Created swapchain %p.\n", object);
1025 *swapchain = object;
1027 return WINED3D_OK;
1030 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1032 struct wined3d_context **ctx_array;
1033 struct wined3d_context *ctx;
1035 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1037 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1039 ERR("Failed to create a new context for the swapchain\n");
1040 return NULL;
1042 context_release(ctx);
1044 if (!(ctx_array = heap_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1046 ERR("Out of memory when trying to allocate a new context array\n");
1047 context_destroy(swapchain->device, ctx);
1048 return NULL;
1050 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1051 heap_free(swapchain->context);
1052 ctx_array[swapchain->num_contexts] = ctx;
1053 swapchain->context = ctx_array;
1054 swapchain->num_contexts++;
1056 TRACE("Returning context %p\n", ctx);
1057 return ctx;
1060 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1062 unsigned int i;
1064 for (i = 0; i < swapchain->num_contexts; ++i)
1066 context_destroy(swapchain->device, swapchain->context[i]);
1068 heap_free(swapchain->context);
1069 swapchain->num_contexts = 0;
1070 swapchain->context = NULL;
1073 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1075 DWORD tid = GetCurrentThreadId();
1076 unsigned int i;
1078 for (i = 0; i < swapchain->num_contexts; ++i)
1080 if (swapchain->context[i]->tid == tid)
1081 return swapchain->context[i];
1084 /* Create a new context for the thread */
1085 return swapchain_create_context(swapchain);
1088 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1090 if (!swapchain->backup_dc)
1092 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1094 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1095 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1097 ERR("Failed to create a window.\n");
1098 return NULL;
1101 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1103 ERR("Failed to get a DC.\n");
1104 DestroyWindow(swapchain->backup_wnd);
1105 swapchain->backup_wnd = NULL;
1106 return NULL;
1110 return swapchain->backup_dc;
1113 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1115 UINT i;
1117 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1119 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1121 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1125 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1127 struct wined3d_device *device = swapchain->device;
1128 BOOL filter_messages = device->filter_messages;
1130 /* This code is not protected by the wined3d mutex, so it may run while
1131 * wined3d_device_reset is active. Testing on Windows shows that changing
1132 * focus during resets and resetting during focus change events causes
1133 * the application to crash with an invalid memory access. */
1135 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1137 if (activate)
1139 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1141 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1142 * the window but leaves the size untouched, d3d9 sets the size on an
1143 * invisible window, generates messages but doesn't change the window
1144 * properties. The implementation follows d3d9.
1146 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1147 * resume drawing after a focus loss. */
1148 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1149 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1150 SWP_NOACTIVATE | SWP_NOZORDER);
1153 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1155 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1156 device->adapter->ordinal, &swapchain->d3d_mode)))
1157 ERR("Failed to set display mode.\n");
1160 else
1162 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1163 device->adapter->ordinal, NULL)))
1164 ERR("Failed to set display mode.\n");
1166 swapchain->reapply_mode = TRUE;
1168 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1169 && IsWindowVisible(swapchain->device_window))
1170 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1173 device->filter_messages = filter_messages;
1176 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1177 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1178 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1180 struct wined3d_device *device = swapchain->device;
1181 BOOL update_desc = FALSE;
1183 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1184 "multisample_type %#x, multisample_quality %#x.\n",
1185 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1186 multisample_type, multisample_quality);
1188 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1190 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1191 FIXME("Cannot change the back buffer count yet.\n");
1193 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1195 if (!width || !height)
1197 /* The application is requesting that either the swapchain width or
1198 * height be set to the corresponding dimension in the window's
1199 * client rect. */
1201 RECT client_rect;
1203 if (!swapchain->desc.windowed)
1204 return WINED3DERR_INVALIDCALL;
1206 if (!GetClientRect(swapchain->device_window, &client_rect))
1208 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1209 return WINED3DERR_INVALIDCALL;
1212 if (!width)
1213 width = client_rect.right;
1215 if (!height)
1216 height = client_rect.bottom;
1219 if (width != swapchain->desc.backbuffer_width
1220 || height != swapchain->desc.backbuffer_height)
1222 swapchain->desc.backbuffer_width = width;
1223 swapchain->desc.backbuffer_height = height;
1224 update_desc = TRUE;
1227 if (format_id == WINED3DFMT_UNKNOWN)
1229 if (!swapchain->desc.windowed)
1230 return WINED3DERR_INVALIDCALL;
1231 format_id = swapchain->original_mode.format_id;
1234 if (format_id != swapchain->desc.backbuffer_format)
1236 swapchain->desc.backbuffer_format = format_id;
1237 update_desc = TRUE;
1240 if (multisample_type != swapchain->desc.multisample_type
1241 || multisample_quality != swapchain->desc.multisample_quality)
1243 swapchain->desc.multisample_type = multisample_type;
1244 swapchain->desc.multisample_quality = multisample_quality;
1245 update_desc = TRUE;
1248 if (update_desc)
1250 HRESULT hr;
1251 UINT i;
1253 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1254 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1255 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1256 return hr;
1258 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1260 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1261 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1262 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1263 return hr;
1267 swapchain_update_render_to_fbo(swapchain);
1268 swapchain_update_draw_bindings(swapchain);
1270 return WINED3D_OK;
1273 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1274 struct wined3d_display_mode *mode)
1276 struct wined3d_device *device = swapchain->device;
1277 HRESULT hr;
1279 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1281 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1282 device->adapter->ordinal, mode)))
1284 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1288 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1289 device->adapter->ordinal, mode)))
1291 WARN("Failed to set display mode, hr %#x.\n", hr);
1292 return WINED3DERR_INVALIDCALL;
1295 return WINED3D_OK;
1298 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1299 const struct wined3d_display_mode *mode)
1301 struct wined3d_device *device = swapchain->device;
1302 struct wined3d_display_mode actual_mode;
1303 RECT original_window_rect, window_rect;
1304 HRESULT hr;
1306 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1308 if (swapchain->desc.windowed)
1310 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1311 AdjustWindowRectEx(&window_rect,
1312 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1313 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1314 SetRect(&window_rect, 0, 0,
1315 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1316 GetWindowRect(swapchain->device_window, &original_window_rect);
1317 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1319 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1321 actual_mode = *mode;
1322 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1323 return hr;
1324 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1326 else
1328 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1329 &actual_mode, NULL)))
1331 ERR("Failed to get display mode, hr %#x.\n", hr);
1332 return WINED3DERR_INVALIDCALL;
1335 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1338 MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1339 window_rect.right - window_rect.left,
1340 window_rect.bottom - window_rect.top, TRUE);
1342 return WINED3D_OK;
1345 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1346 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1348 struct wined3d_device *device = swapchain->device;
1349 struct wined3d_display_mode actual_mode;
1350 HRESULT hr;
1352 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1354 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1356 if (mode)
1358 actual_mode = *mode;
1360 else
1362 if (!swapchain_desc->windowed)
1364 actual_mode.width = swapchain_desc->backbuffer_width;
1365 actual_mode.height = swapchain_desc->backbuffer_height;
1366 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1367 actual_mode.format_id = swapchain_desc->backbuffer_format;
1368 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1370 else
1372 actual_mode = swapchain->original_mode;
1376 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1377 return hr;
1379 else
1381 if (mode)
1382 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1384 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1385 &actual_mode, NULL)))
1387 ERR("Failed to get display mode, hr %#x.\n", hr);
1388 return WINED3DERR_INVALIDCALL;
1392 if (!swapchain_desc->windowed)
1394 unsigned int width = actual_mode.width;
1395 unsigned int height = actual_mode.height;
1397 if (swapchain->desc.windowed)
1399 /* Switch from windowed to fullscreen */
1400 HWND focus_window = device->create_parms.focus_window;
1401 if (!focus_window)
1402 focus_window = swapchain->device_window;
1403 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1405 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1406 return hr;
1409 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1411 else
1413 /* Fullscreen -> fullscreen mode change */
1414 BOOL filter_messages = device->filter_messages;
1415 device->filter_messages = TRUE;
1417 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1418 ShowWindow(swapchain->device_window, SW_SHOW);
1420 device->filter_messages = filter_messages;
1422 swapchain->d3d_mode = actual_mode;
1424 else if (!swapchain->desc.windowed)
1426 /* Fullscreen -> windowed switch */
1427 RECT *window_rect = NULL;
1428 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1429 window_rect = &swapchain->original_window_rect;
1430 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1431 wined3d_device_release_focus_window(device);
1434 swapchain->desc.windowed = swapchain_desc->windowed;
1436 return WINED3D_OK;