webservices: Add support for union types in the writer.
[wine.git] / dlls / wined3d / swapchain.c
blobe23e99b67bb4907f2f441a57546f769501ce3a38
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 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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, DWORD flags)
151 RECT s, d;
153 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
154 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
155 dst_window_override, flags);
157 if (flags)
158 FIXME("Ignoring flags %#x.\n", flags);
160 if (!swapchain->back_buffers)
162 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
163 return WINED3DERR_INVALIDCALL;
166 if (!src_rect)
168 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
169 swapchain->desc.backbuffer_height);
170 src_rect = &s;
173 if (!dst_rect)
175 GetClientRect(swapchain->win_handle, &d);
176 dst_rect = &d;
179 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
180 dst_rect, dst_window_override, flags);
182 return WINED3D_OK;
185 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
186 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
188 RECT src_rect, dst_rect;
190 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
192 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
193 dst_rect = src_rect;
195 if (swapchain->desc.windowed)
197 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
198 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
199 wine_dbgstr_rect(&dst_rect));
202 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
203 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
206 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
207 UINT back_buffer_idx)
209 TRACE("swapchain %p, back_buffer_idx %u.\n",
210 swapchain, back_buffer_idx);
212 /* Return invalid if there is no backbuffer array, otherwise it will
213 * crash when ddraw is used (there swapchain->back_buffers is always
214 * NULL). We need this because this function is called from
215 * stateblock_init_default_state() to get the default scissorrect
216 * dimensions. */
217 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
219 WARN("Invalid back buffer index.\n");
220 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
221 * here in wined3d to avoid problems in other libs. */
222 return NULL;
225 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
227 return swapchain->back_buffers[back_buffer_idx];
230 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
231 struct wined3d_raster_status *raster_status)
233 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
235 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
236 swapchain->device->adapter->ordinal, raster_status);
239 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
240 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
242 HRESULT hr;
244 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
246 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
247 swapchain->device->adapter->ordinal, mode, rotation);
249 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
250 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
252 return hr;
255 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
257 TRACE("swapchain %p.\n", swapchain);
259 return swapchain->device;
262 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
263 struct wined3d_swapchain_desc *desc)
265 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
267 *desc = swapchain->desc;
270 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
271 DWORD flags, const struct wined3d_gamma_ramp *ramp)
273 HDC dc;
275 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
277 if (flags)
278 FIXME("Ignoring flags %#x.\n", flags);
280 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
281 SetDeviceGammaRamp(dc, (void *)ramp);
282 ReleaseDC(swapchain->device_window, dc);
284 return WINED3D_OK;
287 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
289 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
290 swapchain->palette = palette;
293 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
294 struct wined3d_gamma_ramp *ramp)
296 HDC dc;
298 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
300 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
301 GetDeviceGammaRamp(dc, ramp);
302 ReleaseDC(swapchain->device_window, dc);
304 return WINED3D_OK;
307 /* A GL context is provided by the caller */
308 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
309 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
311 struct wined3d_texture *texture = swapchain->back_buffers[0];
312 struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
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, back_buffer,
333 location, src_rect, back_buffer, 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 const struct wined3d_gl_info *gl_info;
390 struct wined3d_texture *logo_texture;
391 struct wined3d_context *context;
392 BOOL render_to_fbo;
394 context = context_acquire(swapchain->device, back_buffer, 0);
395 if (!context->valid)
397 context_release(context);
398 WARN("Invalid context, skipping present.\n");
399 return;
402 gl_info = context->gl_info;
404 if ((logo_texture = swapchain->device->logo_texture))
406 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
408 /* Blit the logo into the upper left corner of the drawable. */
409 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
410 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
413 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
414 && !swapchain->device->hardwareCursor)
416 RECT dst_rect =
418 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
419 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
420 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
421 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
423 RECT src_rect =
425 0, 0,
426 swapchain->device->cursor_texture->resource.width,
427 swapchain->device->cursor_texture->resource.height
429 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
431 TRACE("Rendering the software cursor.\n");
433 if (swapchain->desc.windowed)
434 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
435 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
436 wined3d_texture_blt(back_buffer, 0, &dst_rect,
437 swapchain->device->cursor_texture, 0, &src_rect,
438 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
441 TRACE("Presenting HDC %p.\n", context->hdc);
443 if (!(render_to_fbo = swapchain->render_to_fbo)
444 && (src_rect->left || src_rect->top
445 || src_rect->right != swapchain->desc.backbuffer_width
446 || src_rect->bottom != swapchain->desc.backbuffer_height
447 || dst_rect->left || dst_rect->top
448 || dst_rect->right != swapchain->desc.backbuffer_width
449 || dst_rect->bottom != swapchain->desc.backbuffer_height))
450 render_to_fbo = TRUE;
452 /* Rendering to a window of different size, presenting partial rectangles,
453 * or rendering to a different window needs help from FBO_blit or a textured
454 * draw. Render the swapchain to a FBO in the future.
456 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
457 * all these issues - this fails if the window is smaller than the backbuffer.
459 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
461 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
462 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
463 swapchain->render_to_fbo = TRUE;
464 swapchain_update_draw_bindings(swapchain);
466 else
468 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
471 if (swapchain->render_to_fbo)
473 static unsigned int once;
475 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
476 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
478 swapchain_blit(swapchain, context, src_rect, dst_rect);
481 if (swapchain->num_contexts > 1)
482 gl_info->gl_ops.gl.p_glFinish();
484 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
485 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
487 wined3d_swapchain_rotate(swapchain, context);
489 TRACE("SwapBuffers called, Starting new frame\n");
490 /* FPS support */
491 if (TRACE_ON(fps))
493 DWORD time = GetTickCount();
494 ++swapchain->frames;
496 /* every 1.5 seconds */
497 if (time - swapchain->prev_time > 1500)
499 TRACE_(fps)("%p @ approx %.2ffps\n",
500 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
501 swapchain->prev_time = time;
502 swapchain->frames = 0;
506 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
507 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
508 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
509 * and INTEXTURE copies can keep their old content if they have any defined content.
510 * If the swapeffect is COPY, the content remains the same.
512 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
513 * up to date and hope WGL flipped front and back buffers and read this data into
514 * the FBO. Don't bother about this for now. */
515 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
516 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
517 0, WINED3D_LOCATION_DISCARDED);
519 if (fb->depth_stencil)
521 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
523 if (ds && (swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
524 || ds->container->flags & WINED3D_TEXTURE_DISCARD))
525 wined3d_texture_validate_location(ds->container,
526 fb->depth_stencil->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
529 context_release(context);
532 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
534 struct wined3d_texture *front_buffer = swapchain->front_buffer;
535 struct wined3d_context *context;
537 context = context_acquire(swapchain->device, front_buffer, 0);
538 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
539 context_release(context);
540 SetRectEmpty(&swapchain->front_buffer_update);
543 static const struct wined3d_swapchain_ops swapchain_gl_ops =
545 swapchain_gl_present,
546 swapchain_gl_frontbuffer_updated,
549 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
551 struct wined3d_surface *front;
552 POINT offset = {0, 0};
553 HDC src_dc, dst_dc;
554 RECT draw_rect;
555 HWND window;
557 TRACE("swapchain %p.\n", swapchain);
559 front = swapchain->front_buffer->sub_resources[0].u.surface;
560 if (swapchain->palette)
561 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
563 if (front->container->resource.map_count)
564 ERR("Trying to blit a mapped surface.\n");
566 TRACE("Copying surface %p to screen.\n", front);
568 src_dc = front->dc;
569 window = swapchain->win_handle;
570 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
572 /* Front buffer coordinates are screen coordinates. Map them to the
573 * destination window if not fullscreened. */
574 if (swapchain->desc.windowed)
575 ClientToScreen(window, &offset);
577 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
579 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
580 swapchain->front_buffer->resource.height);
581 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
583 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
584 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
585 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
586 ReleaseDC(window, dst_dc);
588 SetRectEmpty(&swapchain->front_buffer_update);
591 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
592 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
594 struct wined3d_surface *front, *back;
595 HBITMAP bitmap;
596 void *data;
597 HDC dc;
599 front = swapchain->front_buffer->sub_resources[0].u.surface;
600 back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
602 /* Flip the surface data. */
603 dc = front->dc;
604 bitmap = front->bitmap;
605 data = front->container->resource.heap_memory;
607 front->dc = back->dc;
608 front->bitmap = back->bitmap;
609 front->container->resource.heap_memory = back->container->resource.heap_memory;
611 back->dc = dc;
612 back->bitmap = bitmap;
613 back->container->resource.heap_memory = data;
615 /* FPS support */
616 if (TRACE_ON(fps))
618 static LONG prev_time, frames;
619 DWORD time = GetTickCount();
621 ++frames;
623 /* every 1.5 seconds */
624 if (time - prev_time > 1500)
626 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
627 prev_time = time;
628 frames = 0;
632 SetRect(&swapchain->front_buffer_update, 0, 0,
633 swapchain->front_buffer->resource.width,
634 swapchain->front_buffer->resource.height);
635 swapchain_gdi_frontbuffer_updated(swapchain);
638 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
640 swapchain_gdi_present,
641 swapchain_gdi_frontbuffer_updated,
644 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
646 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
647 return;
649 if (!swapchain->desc.backbuffer_count)
651 TRACE("Single buffered rendering.\n");
652 swapchain->render_to_fbo = FALSE;
653 return;
656 TRACE("Rendering to FBO.\n");
657 swapchain->render_to_fbo = TRUE;
660 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
661 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
663 const struct wined3d_gl_info *gl_info;
664 const struct wined3d_format *format;
665 enum wined3d_multisample_type t;
667 if (wined3d_settings.sample_count == ~0u)
668 return;
670 gl_info = &swapchain->device->adapter->gl_info;
671 if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
672 return;
674 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
675 while (!(format->multisample_types & 1u << (t - 1)))
676 ++t;
677 TRACE("Using sample count %u.\n", t);
678 *type = t;
679 *quality = 0;
682 static void wined3d_swapchain_update_swap_interval_cs(void *object)
684 struct wined3d_swapchain *swapchain = object;
685 const struct wined3d_gl_info *gl_info;
686 struct wined3d_context *context;
687 int swap_interval;
689 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
690 gl_info = context->gl_info;
692 switch (swapchain->desc.swap_interval)
694 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
695 swap_interval = 0;
696 break;
697 case WINED3DPRESENT_INTERVAL_DEFAULT:
698 case WINED3DPRESENT_INTERVAL_ONE:
699 swap_interval = 1;
700 break;
701 case WINED3DPRESENT_INTERVAL_TWO:
702 swap_interval = 2;
703 break;
704 case WINED3DPRESENT_INTERVAL_THREE:
705 swap_interval = 3;
706 break;
707 case WINED3DPRESENT_INTERVAL_FOUR:
708 swap_interval = 4;
709 break;
710 default:
711 FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
712 swap_interval = 1;
715 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
717 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
718 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
719 swap_interval, context, GetLastError());
722 context_release(context);
725 static void wined3d_swapchain_cs_init(void *object)
727 struct wined3d_swapchain *swapchain = object;
728 const struct wined3d_gl_info *gl_info;
729 unsigned int i;
731 static const enum wined3d_format_id formats[] =
733 WINED3DFMT_D24_UNORM_S8_UINT,
734 WINED3DFMT_D32_UNORM,
735 WINED3DFMT_R24_UNORM_X8_TYPELESS,
736 WINED3DFMT_D16_UNORM,
737 WINED3DFMT_S1_UINT_D15_UNORM,
740 gl_info = &swapchain->device->adapter->gl_info;
742 /* Without ORM_FBO, switching the depth/stencil format is hard. Always
743 * request a depth/stencil buffer in the likely case it's needed later. */
744 for (i = 0; i < ARRAY_SIZE(formats); ++i)
746 swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
747 if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
748 break;
749 TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
752 if (!swapchain->context[0])
754 WARN("Failed to create context.\n");
755 return;
757 swapchain->num_contexts = 1;
759 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
760 && (!swapchain->desc.enable_auto_depth_stencil
761 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
762 FIXME("Add OpenGL context recreation support.\n");
764 context_release(swapchain->context[0]);
766 wined3d_swapchain_update_swap_interval_cs(swapchain);
769 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
770 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
772 const struct wined3d_adapter *adapter = device->adapter;
773 struct wined3d_resource_desc texture_desc;
774 BOOL displaymode_set = FALSE;
775 DWORD texture_flags = 0;
776 RECT client_rect;
777 HWND window;
778 HRESULT hr;
779 UINT i;
781 if (desc->backbuffer_count > 1)
783 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
784 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
787 if (device->wined3d->flags & WINED3D_NO3D)
788 swapchain->swapchain_ops = &swapchain_gdi_ops;
789 else
790 swapchain->swapchain_ops = &swapchain_gl_ops;
792 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
794 swapchain->device = device;
795 swapchain->parent = parent;
796 swapchain->parent_ops = parent_ops;
797 swapchain->ref = 1;
798 swapchain->win_handle = window;
799 swapchain->device_window = window;
801 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
802 adapter->ordinal, &swapchain->original_mode, NULL)))
804 ERR("Failed to get current display mode, hr %#x.\n", hr);
805 goto err;
807 GetWindowRect(window, &swapchain->original_window_rect);
809 GetClientRect(window, &client_rect);
810 if (desc->windowed)
812 if (!desc->backbuffer_width)
814 desc->backbuffer_width = client_rect.right;
815 TRACE("Updating width to %u.\n", desc->backbuffer_width);
818 if (!desc->backbuffer_height)
820 desc->backbuffer_height = client_rect.bottom;
821 TRACE("Updating height to %u.\n", desc->backbuffer_height);
824 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
826 desc->backbuffer_format = swapchain->original_mode.format_id;
827 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
830 swapchain->desc = *desc;
831 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
832 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
833 swapchain_update_render_to_fbo(swapchain);
835 TRACE("Creating front buffer.\n");
837 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
838 texture_desc.format = swapchain->desc.backbuffer_format;
839 texture_desc.multisample_type = swapchain->desc.multisample_type;
840 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
841 texture_desc.usage = 0;
842 texture_desc.pool = WINED3D_POOL_DEFAULT;
843 texture_desc.width = swapchain->desc.backbuffer_width;
844 texture_desc.height = swapchain->desc.backbuffer_height;
845 texture_desc.depth = 1;
846 texture_desc.size = 0;
848 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
849 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
851 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
852 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
854 WARN("Failed to create front buffer, hr %#x.\n", hr);
855 goto err;
858 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
859 if (!(device->wined3d->flags & WINED3D_NO3D))
861 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
862 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
865 /* MSDN says we're only allowed a single fullscreen swapchain per device,
866 * so we should really check to see if there is a fullscreen swapchain
867 * already. Does a single head count as full screen? */
868 if (!desc->windowed)
870 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
872 /* Change the display settings */
873 swapchain->d3d_mode.width = desc->backbuffer_width;
874 swapchain->d3d_mode.height = desc->backbuffer_height;
875 swapchain->d3d_mode.format_id = desc->backbuffer_format;
876 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
877 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
879 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
880 adapter->ordinal, &swapchain->d3d_mode)))
882 WARN("Failed to set display mode, hr %#x.\n", hr);
883 goto err;
885 displaymode_set = TRUE;
887 else
889 swapchain->d3d_mode = swapchain->original_mode;
893 if (!(device->wined3d->flags & WINED3D_NO3D))
895 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
896 if (!swapchain->context)
898 ERR("Failed to create the context array.\n");
899 hr = E_OUTOFMEMORY;
900 goto err;
903 wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
904 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
906 if (!swapchain->context[0])
908 hr = WINED3DERR_NOTAVAILABLE;
909 goto err;
913 if (swapchain->desc.backbuffer_count > 0)
915 if (!(swapchain->back_buffers = wined3d_calloc(swapchain->desc.backbuffer_count,
916 sizeof(*swapchain->back_buffers))))
918 ERR("Failed to allocate backbuffer array memory.\n");
919 hr = E_OUTOFMEMORY;
920 goto err;
923 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
924 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
926 TRACE("Creating back buffer %u.\n", i);
927 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
928 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
930 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
931 swapchain->desc.backbuffer_count = i;
932 goto err;
934 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
938 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
939 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
941 TRACE("Creating depth/stencil buffer.\n");
942 if (!device->auto_depth_stencil_view)
944 struct wined3d_view_desc desc;
945 struct wined3d_texture *ds;
947 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
948 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
950 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
951 device->device_parent, &texture_desc, texture_flags, &ds)))
953 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
954 goto err;
957 desc.format_id = ds->resource.format->id;
958 desc.flags = 0;
959 desc.u.texture.level_idx = 0;
960 desc.u.texture.level_count = 1;
961 desc.u.texture.layer_idx = 0;
962 desc.u.texture.layer_count = 1;
963 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
964 &device->auto_depth_stencil_view);
965 wined3d_texture_decref(ds);
966 if (FAILED(hr))
968 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
969 goto err;
974 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
976 return WINED3D_OK;
978 err:
979 if (displaymode_set)
981 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
982 adapter->ordinal, &swapchain->original_mode)))
983 ERR("Failed to restore display mode.\n");
984 ClipCursor(NULL);
987 if (swapchain->back_buffers)
989 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
991 if (swapchain->back_buffers[i])
993 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
994 wined3d_texture_decref(swapchain->back_buffers[i]);
997 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1000 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
1001 swapchain->device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1003 if (swapchain->front_buffer)
1005 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1006 wined3d_texture_decref(swapchain->front_buffer);
1009 return hr;
1012 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1013 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1015 struct wined3d_swapchain *object;
1016 HRESULT hr;
1018 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1019 device, desc, parent, parent_ops, swapchain);
1021 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1022 if (!object)
1023 return E_OUTOFMEMORY;
1025 hr = swapchain_init(object, device, desc, parent, parent_ops);
1026 if (FAILED(hr))
1028 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1029 HeapFree(GetProcessHeap(), 0, object);
1030 return hr;
1033 TRACE("Created swapchain %p.\n", object);
1034 *swapchain = object;
1036 return WINED3D_OK;
1039 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1041 struct wined3d_context **ctx_array;
1042 struct wined3d_context *ctx;
1044 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1046 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1048 ERR("Failed to create a new context for the swapchain\n");
1049 return NULL;
1051 context_release(ctx);
1053 if (!(ctx_array = wined3d_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1055 ERR("Out of memory when trying to allocate a new context array\n");
1056 context_destroy(swapchain->device, ctx);
1057 return NULL;
1059 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1060 HeapFree(GetProcessHeap(), 0, swapchain->context);
1061 ctx_array[swapchain->num_contexts] = ctx;
1062 swapchain->context = ctx_array;
1063 swapchain->num_contexts++;
1065 TRACE("Returning context %p\n", ctx);
1066 return ctx;
1069 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1071 unsigned int i;
1073 for (i = 0; i < swapchain->num_contexts; ++i)
1075 context_destroy(swapchain->device, swapchain->context[i]);
1077 HeapFree(GetProcessHeap(), 0, swapchain->context);
1078 swapchain->num_contexts = 0;
1079 swapchain->context = NULL;
1082 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1084 DWORD tid = GetCurrentThreadId();
1085 unsigned int i;
1087 for (i = 0; i < swapchain->num_contexts; ++i)
1089 if (swapchain->context[i]->tid == tid)
1090 return swapchain->context[i];
1093 /* Create a new context for the thread */
1094 return swapchain_create_context(swapchain);
1097 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1099 if (!swapchain->backup_dc)
1101 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1103 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1104 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1106 ERR("Failed to create a window.\n");
1107 return NULL;
1110 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1112 ERR("Failed to get a DC.\n");
1113 DestroyWindow(swapchain->backup_wnd);
1114 swapchain->backup_wnd = NULL;
1115 return NULL;
1119 return swapchain->backup_dc;
1122 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1124 UINT i;
1126 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1128 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1130 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1134 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1136 wined3d_cs_init_object(swapchain->device->cs, wined3d_swapchain_update_swap_interval_cs, swapchain);
1139 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1141 struct wined3d_device *device = swapchain->device;
1142 BOOL filter_messages = device->filter_messages;
1144 /* This code is not protected by the wined3d mutex, so it may run while
1145 * wined3d_device_reset is active. Testing on Windows shows that changing
1146 * focus during resets and resetting during focus change events causes
1147 * the application to crash with an invalid memory access. */
1149 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1151 if (activate)
1153 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1155 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1156 * the window but leaves the size untouched, d3d9 sets the size on an
1157 * invisible window, generates messages but doesn't change the window
1158 * properties. The implementation follows d3d9.
1160 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1161 * resume drawing after a focus loss. */
1162 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1163 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1164 SWP_NOACTIVATE | SWP_NOZORDER);
1167 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1169 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1170 device->adapter->ordinal, &swapchain->d3d_mode)))
1171 ERR("Failed to set display mode.\n");
1174 else
1176 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1177 device->adapter->ordinal, NULL)))
1178 ERR("Failed to set display mode.\n");
1180 swapchain->reapply_mode = TRUE;
1182 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1183 && IsWindowVisible(swapchain->device_window))
1184 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1187 device->filter_messages = filter_messages;
1190 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1191 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1192 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1194 struct wined3d_device *device = swapchain->device;
1195 BOOL update_desc = FALSE;
1197 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1198 "multisample_type %#x, multisample_quality %#x.\n",
1199 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1200 multisample_type, multisample_quality);
1202 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1204 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1205 FIXME("Cannot change the back buffer count yet.\n");
1207 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1209 if (!width || !height)
1211 /* The application is requesting that either the swapchain width or
1212 * height be set to the corresponding dimension in the window's
1213 * client rect. */
1215 RECT client_rect;
1217 if (!swapchain->desc.windowed)
1218 return WINED3DERR_INVALIDCALL;
1220 if (!GetClientRect(swapchain->device_window, &client_rect))
1222 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1223 return WINED3DERR_INVALIDCALL;
1226 if (!width)
1227 width = client_rect.right;
1229 if (!height)
1230 height = client_rect.bottom;
1233 if (width != swapchain->desc.backbuffer_width
1234 || height != swapchain->desc.backbuffer_height)
1236 swapchain->desc.backbuffer_width = width;
1237 swapchain->desc.backbuffer_height = height;
1238 update_desc = TRUE;
1241 if (format_id == WINED3DFMT_UNKNOWN)
1243 if (!swapchain->desc.windowed)
1244 return WINED3DERR_INVALIDCALL;
1245 format_id = swapchain->original_mode.format_id;
1248 if (format_id != swapchain->desc.backbuffer_format)
1250 swapchain->desc.backbuffer_format = format_id;
1251 update_desc = TRUE;
1254 if (multisample_type != swapchain->desc.multisample_type
1255 || multisample_quality != swapchain->desc.multisample_quality)
1257 swapchain->desc.multisample_type = multisample_type;
1258 swapchain->desc.multisample_quality = multisample_quality;
1259 update_desc = TRUE;
1262 if (update_desc)
1264 HRESULT hr;
1265 UINT i;
1267 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1268 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1269 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1270 return hr;
1272 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1274 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1275 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1276 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1277 return hr;
1281 swapchain_update_render_to_fbo(swapchain);
1282 swapchain_update_draw_bindings(swapchain);
1284 return WINED3D_OK;
1287 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1288 struct wined3d_display_mode *mode)
1290 struct wined3d_device *device = swapchain->device;
1291 HRESULT hr;
1293 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1295 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1296 device->adapter->ordinal, mode)))
1298 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1302 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1303 device->adapter->ordinal, mode)))
1305 WARN("Failed to set display mode, hr %#x.\n", hr);
1306 return WINED3DERR_INVALIDCALL;
1309 return WINED3D_OK;
1312 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1313 const struct wined3d_display_mode *mode)
1315 struct wined3d_device *device = swapchain->device;
1316 struct wined3d_display_mode actual_mode;
1317 RECT original_window_rect, window_rect;
1318 HRESULT hr;
1320 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1322 if (swapchain->desc.windowed)
1324 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1325 AdjustWindowRectEx(&window_rect,
1326 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1327 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1328 SetRect(&window_rect, 0, 0,
1329 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1330 GetWindowRect(swapchain->device_window, &original_window_rect);
1331 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1333 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1335 actual_mode = *mode;
1336 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1337 return hr;
1338 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1340 else
1342 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1343 &actual_mode, NULL)))
1345 ERR("Failed to get display mode, hr %#x.\n", hr);
1346 return WINED3DERR_INVALIDCALL;
1349 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1352 MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1353 window_rect.right - window_rect.left,
1354 window_rect.bottom - window_rect.top, TRUE);
1356 return WINED3D_OK;
1359 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1360 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1362 struct wined3d_device *device = swapchain->device;
1363 struct wined3d_display_mode actual_mode;
1364 HRESULT hr;
1366 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1368 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1370 if (mode)
1372 actual_mode = *mode;
1374 else
1376 if (!swapchain_desc->windowed)
1378 actual_mode.width = swapchain_desc->backbuffer_width;
1379 actual_mode.height = swapchain_desc->backbuffer_height;
1380 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1381 actual_mode.format_id = swapchain_desc->backbuffer_format;
1382 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1384 else
1386 actual_mode = swapchain->original_mode;
1390 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1391 return hr;
1393 else
1395 if (mode)
1396 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1398 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1399 &actual_mode, NULL)))
1401 ERR("Failed to get display mode, hr %#x.\n", hr);
1402 return WINED3DERR_INVALIDCALL;
1406 if (!swapchain_desc->windowed)
1408 unsigned int width = actual_mode.width;
1409 unsigned int height = actual_mode.height;
1411 if (swapchain->desc.windowed)
1413 /* Switch from windowed to fullscreen */
1414 HWND focus_window = device->create_parms.focus_window;
1415 if (!focus_window)
1416 focus_window = swapchain->device_window;
1417 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1419 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1420 return hr;
1423 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1425 else
1427 /* Fullscreen -> fullscreen mode change */
1428 BOOL filter_messages = device->filter_messages;
1429 device->filter_messages = TRUE;
1431 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1433 device->filter_messages = filter_messages;
1435 swapchain->d3d_mode = actual_mode;
1437 else if (!swapchain->desc.windowed)
1439 /* Fullscreen -> windowed switch */
1440 RECT *window_rect = NULL;
1441 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1442 window_rect = &swapchain->original_window_rect;
1443 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1444 wined3d_device_release_focus_window(device);
1447 swapchain->desc.windowed = swapchain_desc->windowed;
1449 return WINED3D_OK;