wined3d: Implement updating swap interval through wined3d_swapchain_present().
[wine.git] / dlls / wined3d / swapchain.c
blob6bbba4d8f53d9725c9238339c287f622c3123187
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 if (!window)
139 window = swapchain->device_window;
140 if (window == swapchain->win_handle)
141 return;
143 TRACE("Setting swapchain %p window from %p to %p.\n",
144 swapchain, swapchain->win_handle, window);
145 swapchain->win_handle = window;
148 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
149 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
150 unsigned int swap_interval, DWORD flags)
152 RECT s, d;
154 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
155 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
156 dst_window_override, flags);
158 if (flags)
159 FIXME("Ignoring flags %#x.\n", flags);
161 if (!swapchain->back_buffers)
163 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
164 return WINED3DERR_INVALIDCALL;
167 if (!src_rect)
169 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
170 swapchain->desc.backbuffer_height);
171 src_rect = &s;
174 if (!dst_rect)
176 GetClientRect(swapchain->win_handle, &d);
177 dst_rect = &d;
180 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
181 dst_rect, dst_window_override, swap_interval, flags);
183 return WINED3D_OK;
186 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
187 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
189 RECT src_rect, dst_rect;
191 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
193 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
194 dst_rect = src_rect;
196 if (swapchain->desc.windowed)
198 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
199 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
200 wine_dbgstr_rect(&dst_rect));
203 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
204 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
207 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
208 UINT back_buffer_idx)
210 TRACE("swapchain %p, back_buffer_idx %u.\n",
211 swapchain, back_buffer_idx);
213 /* Return invalid if there is no backbuffer array, otherwise it will
214 * crash when ddraw is used (there swapchain->back_buffers is always
215 * NULL). We need this because this function is called from
216 * stateblock_init_default_state() to get the default scissorrect
217 * dimensions. */
218 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
220 WARN("Invalid back buffer index.\n");
221 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
222 * here in wined3d to avoid problems in other libs. */
223 return NULL;
226 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
228 return swapchain->back_buffers[back_buffer_idx];
231 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
232 struct wined3d_raster_status *raster_status)
234 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
236 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
237 swapchain->device->adapter->ordinal, raster_status);
240 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
241 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
243 HRESULT hr;
245 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
247 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
248 swapchain->device->adapter->ordinal, mode, rotation);
250 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
251 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
253 return hr;
256 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
258 TRACE("swapchain %p.\n", swapchain);
260 return swapchain->device;
263 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
264 struct wined3d_swapchain_desc *desc)
266 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
268 *desc = swapchain->desc;
271 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
272 DWORD flags, const struct wined3d_gamma_ramp *ramp)
274 HDC dc;
276 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
278 if (flags)
279 FIXME("Ignoring flags %#x.\n", flags);
281 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
282 SetDeviceGammaRamp(dc, (void *)ramp);
283 ReleaseDC(swapchain->device_window, dc);
285 return WINED3D_OK;
288 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
290 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
291 swapchain->palette = palette;
294 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
295 struct wined3d_gamma_ramp *ramp)
297 HDC dc;
299 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
301 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
302 GetDeviceGammaRamp(dc, ramp);
303 ReleaseDC(swapchain->device_window, dc);
305 return WINED3D_OK;
308 /* A GL context is provided by the caller */
309 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
310 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
312 struct wined3d_texture *texture = swapchain->back_buffers[0];
313 struct wined3d_device *device = swapchain->device;
314 enum wined3d_texture_filter_type filter;
315 DWORD location;
317 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
318 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
320 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
321 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
322 || is_complex_fixup(texture->resource.format->color_fixup))
323 filter = WINED3D_TEXF_NONE;
324 else
325 filter = WINED3D_TEXF_LINEAR;
327 location = WINED3D_LOCATION_TEXTURE_RGB;
328 if (texture->resource.multisample_type)
329 location = WINED3D_LOCATION_RB_RESOLVED;
331 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
332 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
333 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
334 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
337 /* Context activation is done by the caller. */
338 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
340 struct wined3d_texture_sub_resource *sub_resource;
341 struct wined3d_texture *texture, *texture_prev;
342 struct gl_texture tex0;
343 GLuint rb0;
344 DWORD locations0;
345 unsigned int i;
346 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
348 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
349 return;
351 texture_prev = swapchain->back_buffers[0];
353 /* Back buffer 0 is already in the draw binding. */
354 tex0 = texture_prev->texture_rgb;
355 rb0 = texture_prev->rb_multisample;
356 locations0 = texture_prev->sub_resources[0].locations;
358 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
360 texture = swapchain->back_buffers[i];
361 sub_resource = &texture->sub_resources[0];
363 if (!(sub_resource->locations & supported_locations))
364 wined3d_texture_load_location(texture, 0, context, texture->resource.draw_binding);
366 texture_prev->texture_rgb = texture->texture_rgb;
367 texture_prev->rb_multisample = texture->rb_multisample;
369 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
370 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
372 texture_prev = texture;
375 texture_prev->texture_rgb = tex0;
376 texture_prev->rb_multisample = rb0;
378 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
379 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
381 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
384 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
385 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
387 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
388 const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
389 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
390 const struct wined3d_gl_info *gl_info;
391 struct wined3d_texture *logo_texture;
392 struct wined3d_context *context;
393 BOOL render_to_fbo;
395 context = context_acquire(swapchain->device, back_buffer, 0);
396 if (!context->valid)
398 context_release(context);
399 WARN("Invalid context, skipping present.\n");
400 return;
403 gl_info = context->gl_info;
405 if ((logo_texture = swapchain->device->logo_texture))
407 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
409 /* Blit the logo into the upper left corner of the drawable. */
410 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
411 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
414 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
415 && !swapchain->device->hardwareCursor)
417 RECT dst_rect =
419 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
420 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
421 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
422 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
424 RECT src_rect =
426 0, 0,
427 swapchain->device->cursor_texture->resource.width,
428 swapchain->device->cursor_texture->resource.height
430 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
432 TRACE("Rendering the software cursor.\n");
434 if (swapchain->desc.windowed)
435 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
436 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
437 wined3d_texture_blt(back_buffer, 0, &dst_rect,
438 swapchain->device->cursor_texture, 0, &src_rect,
439 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
442 TRACE("Presenting HDC %p.\n", context->hdc);
444 if (!(render_to_fbo = swapchain->render_to_fbo)
445 && (src_rect->left || src_rect->top
446 || src_rect->right != swapchain->desc.backbuffer_width
447 || src_rect->bottom != swapchain->desc.backbuffer_height
448 || dst_rect->left || dst_rect->top
449 || dst_rect->right != swapchain->desc.backbuffer_width
450 || dst_rect->bottom != swapchain->desc.backbuffer_height))
451 render_to_fbo = TRUE;
453 /* Rendering to a window of different size, presenting partial rectangles,
454 * or rendering to a different window needs help from FBO_blit or a textured
455 * draw. Render the swapchain to a FBO in the future.
457 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
458 * all these issues - this fails if the window is smaller than the backbuffer.
460 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
462 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
463 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
464 swapchain->render_to_fbo = TRUE;
465 swapchain_update_draw_bindings(swapchain);
467 else
469 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
472 if (swapchain->render_to_fbo)
473 swapchain_blit(swapchain, context, src_rect, dst_rect);
475 if (swapchain->num_contexts > 1)
476 gl_info->gl_ops.gl.p_glFinish();
478 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
479 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
481 wined3d_swapchain_rotate(swapchain, context);
483 TRACE("SwapBuffers called, Starting new frame\n");
484 /* FPS support */
485 if (TRACE_ON(fps))
487 DWORD time = GetTickCount();
488 ++swapchain->frames;
490 /* every 1.5 seconds */
491 if (time - swapchain->prev_time > 1500)
493 TRACE_(fps)("%p @ approx %.2ffps\n",
494 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
495 swapchain->prev_time = time;
496 swapchain->frames = 0;
500 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
501 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
502 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
503 * and INTEXTURE copies can keep their old content if they have any defined content.
504 * If the swapeffect is COPY, the content remains the same.
506 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
507 * up to date and hope WGL flipped front and back buffers and read this data into
508 * the FBO. Don't bother about this for now. */
509 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD
510 || swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
511 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
512 0, WINED3D_LOCATION_DISCARDED);
514 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
516 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
518 if ((swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
519 || ds->flags & WINED3D_TEXTURE_DISCARD))
520 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
523 context_release(context);
526 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
528 struct wined3d_texture *front_buffer = swapchain->front_buffer;
529 struct wined3d_context *context;
531 context = context_acquire(swapchain->device, front_buffer, 0);
532 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
533 context_release(context);
534 SetRectEmpty(&swapchain->front_buffer_update);
537 static const struct wined3d_swapchain_ops swapchain_gl_ops =
539 swapchain_gl_present,
540 swapchain_gl_frontbuffer_updated,
543 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
545 struct wined3d_dc_info *front;
546 POINT offset = {0, 0};
547 HDC src_dc, dst_dc;
548 RECT draw_rect;
549 HWND window;
551 TRACE("swapchain %p.\n", swapchain);
553 front = &swapchain->front_buffer->dc_info[0];
554 if (swapchain->palette)
555 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
557 if (swapchain->front_buffer->resource.map_count)
558 ERR("Trying to blit a mapped surface.\n");
560 TRACE("Copying surface %p to screen.\n", front);
562 src_dc = front->dc;
563 window = swapchain->win_handle;
564 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
566 /* Front buffer coordinates are screen coordinates. Map them to the
567 * destination window if not fullscreened. */
568 if (swapchain->desc.windowed)
569 ClientToScreen(window, &offset);
571 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
573 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
574 swapchain->front_buffer->resource.height);
575 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
577 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
578 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
579 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
580 ReleaseDC(window, dst_dc);
582 SetRectEmpty(&swapchain->front_buffer_update);
585 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
586 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
588 struct wined3d_dc_info *front, *back;
589 HBITMAP bitmap;
590 void *data;
591 HDC dc;
593 front = &swapchain->front_buffer->dc_info[0];
594 back = &swapchain->back_buffers[0]->dc_info[0];
596 /* Flip the surface data. */
597 dc = front->dc;
598 bitmap = front->bitmap;
599 data = swapchain->front_buffer->resource.heap_memory;
601 front->dc = back->dc;
602 front->bitmap = back->bitmap;
603 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
605 back->dc = dc;
606 back->bitmap = bitmap;
607 swapchain->back_buffers[0]->resource.heap_memory = data;
609 /* FPS support */
610 if (TRACE_ON(fps))
612 static LONG prev_time, frames;
613 DWORD time = GetTickCount();
615 ++frames;
617 /* every 1.5 seconds */
618 if (time - prev_time > 1500)
620 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
621 prev_time = time;
622 frames = 0;
626 SetRect(&swapchain->front_buffer_update, 0, 0,
627 swapchain->front_buffer->resource.width,
628 swapchain->front_buffer->resource.height);
629 swapchain_gdi_frontbuffer_updated(swapchain);
632 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
634 swapchain_gdi_present,
635 swapchain_gdi_frontbuffer_updated,
638 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
640 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
641 return;
643 if (!swapchain->desc.backbuffer_count)
645 TRACE("Single buffered rendering.\n");
646 swapchain->render_to_fbo = FALSE;
647 return;
650 TRACE("Rendering to FBO.\n");
651 swapchain->render_to_fbo = TRUE;
654 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
655 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
657 const struct wined3d_gl_info *gl_info;
658 const struct wined3d_format *format;
659 enum wined3d_multisample_type t;
661 if (wined3d_settings.sample_count == ~0u)
662 return;
664 gl_info = &swapchain->device->adapter->gl_info;
665 if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
666 return;
668 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
669 while (!(format->multisample_types & 1u << (t - 1)))
670 ++t;
671 TRACE("Using sample count %u.\n", t);
672 *type = t;
673 *quality = 0;
676 static void wined3d_swapchain_update_swap_interval_cs(void *object)
678 struct wined3d_swapchain *swapchain = object;
679 const struct wined3d_gl_info *gl_info;
680 struct wined3d_context *context;
681 int swap_interval;
683 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
684 gl_info = context->gl_info;
686 swap_interval = swapchain->desc.swap_interval > 4 ? 1 : swapchain->desc.swap_interval;
688 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
690 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
691 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x.\n",
692 swap_interval, context, GetLastError());
695 context_release(context);
698 static void wined3d_swapchain_cs_init(void *object)
700 struct wined3d_swapchain *swapchain = object;
701 const struct wined3d_gl_info *gl_info;
702 unsigned int i;
704 static const enum wined3d_format_id formats[] =
706 WINED3DFMT_D24_UNORM_S8_UINT,
707 WINED3DFMT_D32_UNORM,
708 WINED3DFMT_R24_UNORM_X8_TYPELESS,
709 WINED3DFMT_D16_UNORM,
710 WINED3DFMT_S1_UINT_D15_UNORM,
713 gl_info = &swapchain->device->adapter->gl_info;
715 /* Without ORM_FBO, switching the depth/stencil format is hard. Always
716 * request a depth/stencil buffer in the likely case it's needed later. */
717 for (i = 0; i < ARRAY_SIZE(formats); ++i)
719 swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
720 if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
721 break;
722 TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
725 if (!swapchain->context[0])
727 WARN("Failed to create context.\n");
728 return;
730 swapchain->num_contexts = 1;
732 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
733 && (!swapchain->desc.enable_auto_depth_stencil
734 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
735 FIXME("Add OpenGL context recreation support.\n");
737 context_release(swapchain->context[0]);
739 wined3d_swapchain_update_swap_interval_cs(swapchain);
742 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
743 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
745 const struct wined3d_adapter *adapter = device->adapter;
746 struct wined3d_resource_desc texture_desc;
747 BOOL displaymode_set = FALSE;
748 DWORD texture_flags = 0;
749 RECT client_rect;
750 HWND window;
751 HRESULT hr;
752 UINT i;
754 if (desc->backbuffer_count > 1)
756 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
757 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
760 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
761 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
762 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
763 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
765 if (device->wined3d->flags & WINED3D_NO3D)
766 swapchain->swapchain_ops = &swapchain_gdi_ops;
767 else
768 swapchain->swapchain_ops = &swapchain_gl_ops;
770 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
772 swapchain->device = device;
773 swapchain->parent = parent;
774 swapchain->parent_ops = parent_ops;
775 swapchain->ref = 1;
776 swapchain->win_handle = window;
777 swapchain->device_window = window;
779 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
780 adapter->ordinal, &swapchain->original_mode, NULL)))
782 ERR("Failed to get current display mode, hr %#x.\n", hr);
783 goto err;
785 GetWindowRect(window, &swapchain->original_window_rect);
787 GetClientRect(window, &client_rect);
788 if (desc->windowed)
790 if (!desc->backbuffer_width)
792 desc->backbuffer_width = client_rect.right;
793 TRACE("Updating width to %u.\n", desc->backbuffer_width);
796 if (!desc->backbuffer_height)
798 desc->backbuffer_height = client_rect.bottom;
799 TRACE("Updating height to %u.\n", desc->backbuffer_height);
802 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
804 desc->backbuffer_format = swapchain->original_mode.format_id;
805 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
808 swapchain->desc = *desc;
809 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
810 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
811 swapchain_update_render_to_fbo(swapchain);
813 TRACE("Creating front buffer.\n");
815 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
816 texture_desc.format = swapchain->desc.backbuffer_format;
817 texture_desc.multisample_type = swapchain->desc.multisample_type;
818 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
819 texture_desc.usage = 0;
820 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
821 texture_desc.width = swapchain->desc.backbuffer_width;
822 texture_desc.height = swapchain->desc.backbuffer_height;
823 texture_desc.depth = 1;
824 texture_desc.size = 0;
826 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
827 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
829 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
830 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
832 WARN("Failed to create front buffer, hr %#x.\n", hr);
833 goto err;
836 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
837 if (!(device->wined3d->flags & WINED3D_NO3D))
839 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
840 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
843 /* MSDN says we're only allowed a single fullscreen swapchain per device,
844 * so we should really check to see if there is a fullscreen swapchain
845 * already. Does a single head count as full screen? */
846 if (!desc->windowed)
848 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
850 /* Change the display settings */
851 swapchain->d3d_mode.width = desc->backbuffer_width;
852 swapchain->d3d_mode.height = desc->backbuffer_height;
853 swapchain->d3d_mode.format_id = desc->backbuffer_format;
854 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
855 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
857 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
858 adapter->ordinal, &swapchain->d3d_mode)))
860 WARN("Failed to set display mode, hr %#x.\n", hr);
861 goto err;
863 displaymode_set = TRUE;
865 else
867 swapchain->d3d_mode = swapchain->original_mode;
871 if (!(device->wined3d->flags & WINED3D_NO3D))
873 if (!(swapchain->context = heap_alloc(sizeof(*swapchain->context))))
875 ERR("Failed to create the context array.\n");
876 hr = E_OUTOFMEMORY;
877 goto err;
880 wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
881 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
883 if (!swapchain->context[0])
885 hr = WINED3DERR_NOTAVAILABLE;
886 goto err;
890 if (swapchain->desc.backbuffer_count > 0)
892 if (!(swapchain->back_buffers = heap_calloc(swapchain->desc.backbuffer_count,
893 sizeof(*swapchain->back_buffers))))
895 ERR("Failed to allocate backbuffer array memory.\n");
896 hr = E_OUTOFMEMORY;
897 goto err;
900 texture_desc.usage = swapchain->desc.backbuffer_usage;
901 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
903 TRACE("Creating back buffer %u.\n", i);
904 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
905 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
907 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
908 swapchain->desc.backbuffer_count = i;
909 goto err;
911 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
915 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
916 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
918 TRACE("Creating depth/stencil buffer.\n");
919 if (!device->auto_depth_stencil_view)
921 struct wined3d_view_desc desc;
922 struct wined3d_texture *ds;
924 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
925 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
927 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
928 device->device_parent, &texture_desc, texture_flags, &ds)))
930 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
931 goto err;
934 desc.format_id = ds->resource.format->id;
935 desc.flags = 0;
936 desc.u.texture.level_idx = 0;
937 desc.u.texture.level_count = 1;
938 desc.u.texture.layer_idx = 0;
939 desc.u.texture.layer_count = 1;
940 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
941 &device->auto_depth_stencil_view);
942 wined3d_texture_decref(ds);
943 if (FAILED(hr))
945 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
946 goto err;
951 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
953 return WINED3D_OK;
955 err:
956 if (displaymode_set)
958 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
959 adapter->ordinal, &swapchain->original_mode)))
960 ERR("Failed to restore display mode.\n");
961 ClipCursor(NULL);
964 if (swapchain->back_buffers)
966 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
968 if (swapchain->back_buffers[i])
970 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
971 wined3d_texture_decref(swapchain->back_buffers[i]);
974 heap_free(swapchain->back_buffers);
977 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
978 swapchain->device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
980 if (swapchain->front_buffer)
982 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
983 wined3d_texture_decref(swapchain->front_buffer);
986 return hr;
989 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
990 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
992 struct wined3d_swapchain *object;
993 HRESULT hr;
995 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
996 device, desc, parent, parent_ops, swapchain);
998 if (!(object = heap_alloc_zero(sizeof(*object))))
999 return E_OUTOFMEMORY;
1001 hr = swapchain_init(object, device, desc, parent, parent_ops);
1002 if (FAILED(hr))
1004 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1005 heap_free(object);
1006 return hr;
1009 TRACE("Created swapchain %p.\n", object);
1010 *swapchain = object;
1012 return WINED3D_OK;
1015 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1017 struct wined3d_context **ctx_array;
1018 struct wined3d_context *ctx;
1020 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1022 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1024 ERR("Failed to create a new context for the swapchain\n");
1025 return NULL;
1027 context_release(ctx);
1029 if (!(ctx_array = heap_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1031 ERR("Out of memory when trying to allocate a new context array\n");
1032 context_destroy(swapchain->device, ctx);
1033 return NULL;
1035 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1036 heap_free(swapchain->context);
1037 ctx_array[swapchain->num_contexts] = ctx;
1038 swapchain->context = ctx_array;
1039 swapchain->num_contexts++;
1041 TRACE("Returning context %p\n", ctx);
1042 return ctx;
1045 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1047 unsigned int i;
1049 for (i = 0; i < swapchain->num_contexts; ++i)
1051 context_destroy(swapchain->device, swapchain->context[i]);
1053 heap_free(swapchain->context);
1054 swapchain->num_contexts = 0;
1055 swapchain->context = NULL;
1058 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1060 DWORD tid = GetCurrentThreadId();
1061 unsigned int i;
1063 for (i = 0; i < swapchain->num_contexts; ++i)
1065 if (swapchain->context[i]->tid == tid)
1066 return swapchain->context[i];
1069 /* Create a new context for the thread */
1070 return swapchain_create_context(swapchain);
1073 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1075 if (!swapchain->backup_dc)
1077 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1079 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1080 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1082 ERR("Failed to create a window.\n");
1083 return NULL;
1086 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1088 ERR("Failed to get a DC.\n");
1089 DestroyWindow(swapchain->backup_wnd);
1090 swapchain->backup_wnd = NULL;
1091 return NULL;
1095 return swapchain->backup_dc;
1098 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1100 UINT i;
1102 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1104 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1106 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1110 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1112 wined3d_cs_init_object(swapchain->device->cs, wined3d_swapchain_update_swap_interval_cs, swapchain);
1115 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1117 struct wined3d_device *device = swapchain->device;
1118 BOOL filter_messages = device->filter_messages;
1120 /* This code is not protected by the wined3d mutex, so it may run while
1121 * wined3d_device_reset is active. Testing on Windows shows that changing
1122 * focus during resets and resetting during focus change events causes
1123 * the application to crash with an invalid memory access. */
1125 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1127 if (activate)
1129 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1131 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1132 * the window but leaves the size untouched, d3d9 sets the size on an
1133 * invisible window, generates messages but doesn't change the window
1134 * properties. The implementation follows d3d9.
1136 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1137 * resume drawing after a focus loss. */
1138 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1139 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1140 SWP_NOACTIVATE | SWP_NOZORDER);
1143 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1145 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1146 device->adapter->ordinal, &swapchain->d3d_mode)))
1147 ERR("Failed to set display mode.\n");
1150 else
1152 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1153 device->adapter->ordinal, NULL)))
1154 ERR("Failed to set display mode.\n");
1156 swapchain->reapply_mode = TRUE;
1158 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1159 && IsWindowVisible(swapchain->device_window))
1160 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1163 device->filter_messages = filter_messages;
1166 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1167 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1168 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1170 struct wined3d_device *device = swapchain->device;
1171 BOOL update_desc = FALSE;
1173 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1174 "multisample_type %#x, multisample_quality %#x.\n",
1175 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1176 multisample_type, multisample_quality);
1178 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1180 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1181 FIXME("Cannot change the back buffer count yet.\n");
1183 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1185 if (!width || !height)
1187 /* The application is requesting that either the swapchain width or
1188 * height be set to the corresponding dimension in the window's
1189 * client rect. */
1191 RECT client_rect;
1193 if (!swapchain->desc.windowed)
1194 return WINED3DERR_INVALIDCALL;
1196 if (!GetClientRect(swapchain->device_window, &client_rect))
1198 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1199 return WINED3DERR_INVALIDCALL;
1202 if (!width)
1203 width = client_rect.right;
1205 if (!height)
1206 height = client_rect.bottom;
1209 if (width != swapchain->desc.backbuffer_width
1210 || height != swapchain->desc.backbuffer_height)
1212 swapchain->desc.backbuffer_width = width;
1213 swapchain->desc.backbuffer_height = height;
1214 update_desc = TRUE;
1217 if (format_id == WINED3DFMT_UNKNOWN)
1219 if (!swapchain->desc.windowed)
1220 return WINED3DERR_INVALIDCALL;
1221 format_id = swapchain->original_mode.format_id;
1224 if (format_id != swapchain->desc.backbuffer_format)
1226 swapchain->desc.backbuffer_format = format_id;
1227 update_desc = TRUE;
1230 if (multisample_type != swapchain->desc.multisample_type
1231 || multisample_quality != swapchain->desc.multisample_quality)
1233 swapchain->desc.multisample_type = multisample_type;
1234 swapchain->desc.multisample_quality = multisample_quality;
1235 update_desc = TRUE;
1238 if (update_desc)
1240 HRESULT hr;
1241 UINT i;
1243 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1244 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1245 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1246 return hr;
1248 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1250 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1251 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1252 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1253 return hr;
1257 swapchain_update_render_to_fbo(swapchain);
1258 swapchain_update_draw_bindings(swapchain);
1260 return WINED3D_OK;
1263 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1264 struct wined3d_display_mode *mode)
1266 struct wined3d_device *device = swapchain->device;
1267 HRESULT hr;
1269 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1271 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1272 device->adapter->ordinal, mode)))
1274 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1278 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1279 device->adapter->ordinal, mode)))
1281 WARN("Failed to set display mode, hr %#x.\n", hr);
1282 return WINED3DERR_INVALIDCALL;
1285 return WINED3D_OK;
1288 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1289 const struct wined3d_display_mode *mode)
1291 struct wined3d_device *device = swapchain->device;
1292 struct wined3d_display_mode actual_mode;
1293 RECT original_window_rect, window_rect;
1294 HRESULT hr;
1296 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1298 if (swapchain->desc.windowed)
1300 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1301 AdjustWindowRectEx(&window_rect,
1302 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1303 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1304 SetRect(&window_rect, 0, 0,
1305 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1306 GetWindowRect(swapchain->device_window, &original_window_rect);
1307 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1309 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1311 actual_mode = *mode;
1312 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1313 return hr;
1314 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1316 else
1318 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1319 &actual_mode, NULL)))
1321 ERR("Failed to get display mode, hr %#x.\n", hr);
1322 return WINED3DERR_INVALIDCALL;
1325 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1328 MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1329 window_rect.right - window_rect.left,
1330 window_rect.bottom - window_rect.top, TRUE);
1332 return WINED3D_OK;
1335 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1336 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1338 struct wined3d_device *device = swapchain->device;
1339 struct wined3d_display_mode actual_mode;
1340 HRESULT hr;
1342 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1344 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1346 if (mode)
1348 actual_mode = *mode;
1350 else
1352 if (!swapchain_desc->windowed)
1354 actual_mode.width = swapchain_desc->backbuffer_width;
1355 actual_mode.height = swapchain_desc->backbuffer_height;
1356 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1357 actual_mode.format_id = swapchain_desc->backbuffer_format;
1358 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1360 else
1362 actual_mode = swapchain->original_mode;
1366 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1367 return hr;
1369 else
1371 if (mode)
1372 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1374 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1375 &actual_mode, NULL)))
1377 ERR("Failed to get display mode, hr %#x.\n", hr);
1378 return WINED3DERR_INVALIDCALL;
1382 if (!swapchain_desc->windowed)
1384 unsigned int width = actual_mode.width;
1385 unsigned int height = actual_mode.height;
1387 if (swapchain->desc.windowed)
1389 /* Switch from windowed to fullscreen */
1390 HWND focus_window = device->create_parms.focus_window;
1391 if (!focus_window)
1392 focus_window = swapchain->device_window;
1393 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1395 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1396 return hr;
1399 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1401 else
1403 /* Fullscreen -> fullscreen mode change */
1404 BOOL filter_messages = device->filter_messages;
1405 device->filter_messages = TRUE;
1407 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1408 ShowWindow(swapchain->device_window, SW_SHOW);
1410 device->filter_messages = filter_messages;
1412 swapchain->d3d_mode = actual_mode;
1414 else if (!swapchain->desc.windowed)
1416 /* Fullscreen -> windowed switch */
1417 RECT *window_rect = NULL;
1418 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1419 window_rect = &swapchain->original_window_rect;
1420 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1421 wined3d_device_release_focus_window(device);
1424 swapchain->desc.windowed = swapchain_desc->windowed;
1426 return WINED3D_OK;