wined3d: Get rid of the redundant "device" parameter to context_destroy().
[wine.git] / dlls / wined3d / swapchain.c
blob4ac091f692494ba9f6ecf7c77ff2597f827c9ec7
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 wined3d_cs_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;
119 wined3d_mutex_lock();
121 device = swapchain->device;
122 if (device->swapchain_count && device->swapchains[0] == swapchain)
123 wined3d_device_uninit_3d(device);
124 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
126 swapchain_cleanup(swapchain);
127 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
129 wined3d_mutex_unlock();
131 heap_free(swapchain);
134 return refcount;
137 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
139 TRACE("swapchain %p.\n", swapchain);
141 return swapchain->parent;
144 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
146 if (!window)
147 window = swapchain->device_window;
148 if (window == swapchain->win_handle)
149 return;
151 TRACE("Setting swapchain %p window from %p to %p.\n",
152 swapchain, swapchain->win_handle, window);
154 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
156 swapchain->win_handle = window;
159 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
160 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
161 unsigned int swap_interval, DWORD flags)
163 RECT s, d;
165 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
166 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
167 dst_window_override, swap_interval, flags);
169 if (flags)
170 FIXME("Ignoring flags %#x.\n", flags);
172 wined3d_mutex_lock();
174 if (!swapchain->back_buffers)
176 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
177 wined3d_mutex_unlock();
178 return WINED3DERR_INVALIDCALL;
181 if (!src_rect)
183 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
184 swapchain->desc.backbuffer_height);
185 src_rect = &s;
188 if (!dst_rect)
190 GetClientRect(swapchain->win_handle, &d);
191 dst_rect = &d;
194 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
195 dst_rect, dst_window_override, swap_interval, flags);
197 wined3d_mutex_unlock();
199 return WINED3D_OK;
202 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
203 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
205 RECT src_rect, dst_rect;
207 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
209 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
210 dst_rect = src_rect;
212 if (swapchain->desc.windowed)
214 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
215 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
216 wine_dbgstr_rect(&dst_rect));
219 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
220 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
223 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
224 UINT back_buffer_idx)
226 TRACE("swapchain %p, back_buffer_idx %u.\n",
227 swapchain, back_buffer_idx);
229 /* Return invalid if there is no backbuffer array, otherwise it will
230 * crash when ddraw is used (there swapchain->back_buffers is always
231 * NULL). We need this because this function is called from
232 * stateblock_init_default_state() to get the default scissorrect
233 * dimensions. */
234 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
236 WARN("Invalid back buffer index.\n");
237 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
238 * here in wined3d to avoid problems in other libs. */
239 return NULL;
242 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
244 return swapchain->back_buffers[back_buffer_idx];
247 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
248 struct wined3d_raster_status *raster_status)
250 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
252 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
253 swapchain->device->adapter->ordinal, raster_status);
256 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
257 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
259 HRESULT hr;
261 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
263 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
264 swapchain->device->adapter->ordinal, mode, rotation);
266 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
267 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
269 return hr;
272 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
274 TRACE("swapchain %p.\n", swapchain);
276 return swapchain->device;
279 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
280 struct wined3d_swapchain_desc *desc)
282 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
284 *desc = swapchain->desc;
287 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
288 DWORD flags, const struct wined3d_gamma_ramp *ramp)
290 HDC dc;
292 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
294 if (flags)
295 FIXME("Ignoring flags %#x.\n", flags);
297 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
298 SetDeviceGammaRamp(dc, (void *)ramp);
299 ReleaseDC(swapchain->device_window, dc);
301 return WINED3D_OK;
304 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
306 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
308 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
310 swapchain->palette = palette;
313 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
314 struct wined3d_gamma_ramp *ramp)
316 HDC dc;
318 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
320 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
321 GetDeviceGammaRamp(dc, ramp);
322 ReleaseDC(swapchain->device_window, dc);
324 return WINED3D_OK;
327 /* A GL context is provided by the caller */
328 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
329 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
331 struct wined3d_texture *texture = swapchain->back_buffers[0];
332 struct wined3d_device *device = swapchain->device;
333 enum wined3d_texture_filter_type filter;
334 DWORD location;
336 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
337 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
339 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
340 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
341 || is_complex_fixup(texture->resource.format->color_fixup))
342 filter = WINED3D_TEXF_NONE;
343 else
344 filter = WINED3D_TEXF_LINEAR;
346 location = WINED3D_LOCATION_TEXTURE_RGB;
347 if (texture->resource.multisample_type)
348 location = WINED3D_LOCATION_RB_RESOLVED;
350 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
351 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
352 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
353 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
356 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain *swapchain,
357 struct wined3d_context *context, unsigned int swap_interval)
359 const struct wined3d_gl_info *gl_info = context->gl_info;
361 swap_interval = swap_interval <= 4 ? swap_interval : 1;
362 if (swapchain->swap_interval == swap_interval)
363 return;
365 swapchain->swap_interval = swap_interval;
367 if (!gl_info->supported[WGL_EXT_SWAP_CONTROL])
368 return;
370 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
372 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
373 swap_interval, context, GetLastError());
377 /* Context activation is done by the caller. */
378 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
380 struct wined3d_texture_sub_resource *sub_resource;
381 struct wined3d_texture_gl *texture, *texture_prev;
382 struct gl_texture tex0;
383 GLuint rb0;
384 DWORD locations0;
385 unsigned int i;
386 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
388 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
389 return;
391 texture_prev = wined3d_texture_gl(swapchain->back_buffers[0]);
393 /* Back buffer 0 is already in the draw binding. */
394 tex0 = texture_prev->texture_rgb;
395 rb0 = texture_prev->rb_multisample;
396 locations0 = texture_prev->t.sub_resources[0].locations;
398 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
400 texture = wined3d_texture_gl(swapchain->back_buffers[i]);
401 sub_resource = &texture->t.sub_resources[0];
403 if (!(sub_resource->locations & supported_locations))
404 wined3d_texture_load_location(&texture->t, 0, context, texture->t.resource.draw_binding);
406 texture_prev->texture_rgb = texture->texture_rgb;
407 texture_prev->rb_multisample = texture->rb_multisample;
409 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
410 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
412 texture_prev = texture;
415 texture_prev->texture_rgb = tex0;
416 texture_prev->rb_multisample = rb0;
418 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
419 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
421 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
424 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
425 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
427 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
428 const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
429 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
430 const struct wined3d_gl_info *gl_info;
431 struct wined3d_texture *logo_texture;
432 struct wined3d_context *context;
433 BOOL render_to_fbo;
435 context = context_acquire(swapchain->device, back_buffer, 0);
436 if (!context->valid)
438 context_release(context);
439 WARN("Invalid context, skipping present.\n");
440 return;
443 gl_info = context->gl_info;
445 swapchain_gl_set_swap_interval(swapchain, context, swap_interval);
447 if ((logo_texture = swapchain->device->logo_texture))
449 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
451 /* Blit the logo into the upper left corner of the drawable. */
452 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
453 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
456 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
457 && !swapchain->device->hardwareCursor)
459 RECT dst_rect =
461 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
462 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
463 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
464 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
466 RECT src_rect =
468 0, 0,
469 swapchain->device->cursor_texture->resource.width,
470 swapchain->device->cursor_texture->resource.height
472 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
474 TRACE("Rendering the software cursor.\n");
476 if (swapchain->desc.windowed)
477 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
478 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
479 wined3d_texture_blt(back_buffer, 0, &dst_rect,
480 swapchain->device->cursor_texture, 0, &src_rect,
481 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
484 TRACE("Presenting HDC %p.\n", context->hdc);
486 if (!(render_to_fbo = swapchain->render_to_fbo)
487 && (src_rect->left || src_rect->top
488 || src_rect->right != swapchain->desc.backbuffer_width
489 || src_rect->bottom != swapchain->desc.backbuffer_height
490 || dst_rect->left || dst_rect->top
491 || dst_rect->right != swapchain->desc.backbuffer_width
492 || dst_rect->bottom != swapchain->desc.backbuffer_height))
493 render_to_fbo = TRUE;
495 /* Rendering to a window of different size, presenting partial rectangles,
496 * or rendering to a different window needs help from FBO_blit or a textured
497 * draw. Render the swapchain to a FBO in the future.
499 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
500 * all these issues - this fails if the window is smaller than the backbuffer.
502 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
504 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
505 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
506 swapchain->render_to_fbo = TRUE;
507 swapchain_update_draw_bindings(swapchain);
509 else
511 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
514 if (swapchain->render_to_fbo)
515 swapchain_blit(swapchain, context, src_rect, dst_rect);
517 if (swapchain->num_contexts > 1)
518 gl_info->gl_ops.gl.p_glFinish();
520 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
521 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
523 wined3d_swapchain_gl_rotate(swapchain, context);
525 TRACE("SwapBuffers called, Starting new frame\n");
526 /* FPS support */
527 if (TRACE_ON(fps))
529 DWORD time = GetTickCount();
530 ++swapchain->frames;
532 /* every 1.5 seconds */
533 if (time - swapchain->prev_time > 1500)
535 TRACE_(fps)("%p @ approx %.2ffps\n",
536 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
537 swapchain->prev_time = time;
538 swapchain->frames = 0;
542 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
543 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
544 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
545 * and INTEXTURE copies can keep their old content if they have any defined content.
546 * If the swapeffect is COPY, the content remains the same.
548 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
549 * up to date and hope WGL flipped front and back buffers and read this data into
550 * the FBO. Don't bother about this for now. */
551 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD
552 || swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
553 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
554 0, WINED3D_LOCATION_DISCARDED);
556 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
558 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
560 if ((swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
561 || ds->flags & WINED3D_TEXTURE_DISCARD))
562 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
565 context_release(context);
568 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
570 struct wined3d_texture *front_buffer = swapchain->front_buffer;
571 struct wined3d_context *context;
573 context = context_acquire(swapchain->device, front_buffer, 0);
574 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
575 context_release(context);
576 SetRectEmpty(&swapchain->front_buffer_update);
579 static const struct wined3d_swapchain_ops swapchain_gl_ops =
581 swapchain_gl_present,
582 swapchain_gl_frontbuffer_updated,
585 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
587 struct wined3d_dc_info *front;
588 POINT offset = {0, 0};
589 HDC src_dc, dst_dc;
590 RECT draw_rect;
591 HWND window;
593 TRACE("swapchain %p.\n", swapchain);
595 front = &swapchain->front_buffer->dc_info[0];
596 if (swapchain->palette)
597 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
599 if (swapchain->front_buffer->resource.map_count)
600 ERR("Trying to blit a mapped surface.\n");
602 TRACE("Copying surface %p to screen.\n", front);
604 src_dc = front->dc;
605 window = swapchain->win_handle;
606 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
608 /* Front buffer coordinates are screen coordinates. Map them to the
609 * destination window if not fullscreened. */
610 if (swapchain->desc.windowed)
611 ClientToScreen(window, &offset);
613 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
615 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
616 swapchain->front_buffer->resource.height);
617 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
619 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
620 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
621 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
622 ReleaseDC(window, dst_dc);
624 SetRectEmpty(&swapchain->front_buffer_update);
627 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
628 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
630 struct wined3d_dc_info *front, *back;
631 HBITMAP bitmap;
632 void *data;
633 HDC dc;
635 front = &swapchain->front_buffer->dc_info[0];
636 back = &swapchain->back_buffers[0]->dc_info[0];
638 /* Flip the surface data. */
639 dc = front->dc;
640 bitmap = front->bitmap;
641 data = swapchain->front_buffer->resource.heap_memory;
643 front->dc = back->dc;
644 front->bitmap = back->bitmap;
645 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
647 back->dc = dc;
648 back->bitmap = bitmap;
649 swapchain->back_buffers[0]->resource.heap_memory = data;
651 /* FPS support */
652 if (TRACE_ON(fps))
654 static LONG prev_time, frames;
655 DWORD time = GetTickCount();
657 ++frames;
659 /* every 1.5 seconds */
660 if (time - prev_time > 1500)
662 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
663 prev_time = time;
664 frames = 0;
668 SetRect(&swapchain->front_buffer_update, 0, 0,
669 swapchain->front_buffer->resource.width,
670 swapchain->front_buffer->resource.height);
671 swapchain_gdi_frontbuffer_updated(swapchain);
674 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
676 swapchain_gdi_present,
677 swapchain_gdi_frontbuffer_updated,
680 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
682 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
683 return;
685 if (!swapchain->desc.backbuffer_count)
687 TRACE("Single buffered rendering.\n");
688 swapchain->render_to_fbo = FALSE;
689 return;
692 TRACE("Rendering to FBO.\n");
693 swapchain->render_to_fbo = TRUE;
696 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
697 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
699 const struct wined3d_adapter *adapter;
700 const struct wined3d_gl_info *gl_info;
701 const struct wined3d_format *format;
702 enum wined3d_multisample_type t;
704 if (wined3d_settings.sample_count == ~0u)
705 return;
707 adapter = swapchain->device->adapter;
708 gl_info = &adapter->gl_info;
709 if (!(format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET)))
710 return;
712 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
713 while (!(format->multisample_types & 1u << (t - 1)))
714 ++t;
715 TRACE("Using sample count %u.\n", t);
716 *type = t;
717 *quality = 0;
720 static void wined3d_swapchain_cs_init(void *object)
722 struct wined3d_swapchain *swapchain = object;
723 const struct wined3d_adapter *adapter;
724 unsigned int i;
726 static const enum wined3d_format_id formats[] =
728 WINED3DFMT_D24_UNORM_S8_UINT,
729 WINED3DFMT_D32_UNORM,
730 WINED3DFMT_R24_UNORM_X8_TYPELESS,
731 WINED3DFMT_D16_UNORM,
732 WINED3DFMT_S1_UINT_D15_UNORM,
735 adapter = swapchain->device->adapter;
737 /* Without ORM_FBO, switching the depth/stencil format is hard. Always
738 * request a depth/stencil buffer in the likely case it's needed later. */
739 for (i = 0; i < ARRAY_SIZE(formats); ++i)
741 swapchain->ds_format = wined3d_get_format(adapter, formats[i], WINED3D_BIND_DEPTH_STENCIL);
742 if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
743 break;
744 TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
747 if (!swapchain->context[0])
749 WARN("Failed to create context.\n");
750 return;
752 swapchain->num_contexts = 1;
754 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
755 && (!swapchain->desc.enable_auto_depth_stencil
756 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
757 FIXME("Add OpenGL context recreation support.\n");
759 context_release(swapchain->context[0]);
762 void swapchain_set_max_frame_latency(struct wined3d_swapchain *swapchain, const struct wined3d_device *device)
764 /* Subtract 1 for the implicit OpenGL latency. */
765 swapchain->max_frame_latency = device->max_frame_latency >= 2 ? device->max_frame_latency - 1 : 1;
768 static enum wined3d_format_id adapter_format_from_backbuffer_format(struct wined3d_swapchain *swapchain,
769 enum wined3d_format_id format_id)
771 const struct wined3d_adapter *adapter = swapchain->device->adapter;
772 const struct wined3d_format *backbuffer_format;
774 backbuffer_format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET);
775 return pixelformat_for_depth(backbuffer_format->byte_count * CHAR_BIT);
778 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
779 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
781 const struct wined3d_adapter *adapter = device->adapter;
782 struct wined3d_resource_desc texture_desc;
783 BOOL displaymode_set = FALSE;
784 DWORD texture_flags = 0;
785 RECT client_rect;
786 unsigned int i;
787 HWND window;
788 HRESULT hr;
790 if (desc->backbuffer_count > 1)
792 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
793 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
796 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
797 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
798 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
799 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
801 if (device->wined3d->flags & WINED3D_NO3D)
802 swapchain->swapchain_ops = &swapchain_gdi_ops;
803 else
804 swapchain->swapchain_ops = &swapchain_gl_ops;
806 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
808 swapchain->device = device;
809 swapchain->parent = parent;
810 swapchain->parent_ops = parent_ops;
811 swapchain->ref = 1;
812 swapchain->win_handle = window;
813 swapchain->device_window = window;
814 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
815 swapchain_set_max_frame_latency(swapchain, device);
817 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
818 adapter->ordinal, &swapchain->original_mode, NULL)))
820 ERR("Failed to get current display mode, hr %#x.\n", hr);
821 goto err;
823 GetWindowRect(window, &swapchain->original_window_rect);
825 GetClientRect(window, &client_rect);
826 if (desc->windowed)
828 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
830 if (!desc->backbuffer_width)
832 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
833 TRACE("Updating width to %u.\n", desc->backbuffer_width);
835 if (!desc->backbuffer_height)
837 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
838 TRACE("Updating height to %u.\n", desc->backbuffer_height);
841 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
843 desc->backbuffer_format = swapchain->original_mode.format_id;
844 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
847 swapchain->desc = *desc;
848 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
849 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
850 swapchain_update_render_to_fbo(swapchain);
852 TRACE("Creating front buffer.\n");
854 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
855 texture_desc.format = swapchain->desc.backbuffer_format;
856 texture_desc.multisample_type = swapchain->desc.multisample_type;
857 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
858 texture_desc.usage = 0;
859 if (device->wined3d->flags & WINED3D_NO3D)
860 texture_desc.usage |= WINED3DUSAGE_OWNDC;
861 texture_desc.bind_flags = 0;
862 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
863 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
864 texture_desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
865 texture_desc.width = swapchain->desc.backbuffer_width;
866 texture_desc.height = swapchain->desc.backbuffer_height;
867 texture_desc.depth = 1;
868 texture_desc.size = 0;
870 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
871 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
873 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
874 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
876 WARN("Failed to create front buffer, hr %#x.\n", hr);
877 goto err;
880 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
881 if (!(device->wined3d->flags & WINED3D_NO3D))
883 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
884 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
887 /* MSDN says we're only allowed a single fullscreen swapchain per device,
888 * so we should really check to see if there is a fullscreen swapchain
889 * already. Does a single head count as full screen? */
890 if (!desc->windowed)
892 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
894 /* Change the display settings */
895 swapchain->d3d_mode.width = desc->backbuffer_width;
896 swapchain->d3d_mode.height = desc->backbuffer_height;
897 swapchain->d3d_mode.format_id = adapter_format_from_backbuffer_format(swapchain,
898 desc->backbuffer_format);
899 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
900 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
902 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
903 adapter->ordinal, &swapchain->d3d_mode)))
905 WARN("Failed to set display mode, hr %#x.\n", hr);
906 goto err;
908 displaymode_set = TRUE;
910 else
912 swapchain->d3d_mode = swapchain->original_mode;
916 if (!(device->wined3d->flags & WINED3D_NO3D))
918 if (!(swapchain->context = heap_alloc(sizeof(*swapchain->context))))
920 ERR("Failed to create the context array.\n");
921 hr = E_OUTOFMEMORY;
922 goto err;
925 wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
926 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
928 if (!swapchain->context[0])
930 hr = WINED3DERR_NOTAVAILABLE;
931 goto err;
935 if (swapchain->desc.backbuffer_count > 0)
937 if (!(swapchain->back_buffers = heap_calloc(swapchain->desc.backbuffer_count,
938 sizeof(*swapchain->back_buffers))))
940 ERR("Failed to allocate backbuffer array memory.\n");
941 hr = E_OUTOFMEMORY;
942 goto err;
945 texture_desc.bind_flags = swapchain->desc.backbuffer_bind_flags;
946 texture_desc.usage = 0;
947 if (device->wined3d->flags & WINED3D_NO3D)
948 texture_desc.usage |= WINED3DUSAGE_OWNDC;
949 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
951 TRACE("Creating back buffer %u.\n", i);
952 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
953 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
955 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
956 swapchain->desc.backbuffer_count = i;
957 goto err;
959 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
963 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
964 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
966 TRACE("Creating depth/stencil buffer.\n");
967 if (!device->auto_depth_stencil_view)
969 struct wined3d_view_desc desc;
970 struct wined3d_texture *ds;
972 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
973 texture_desc.usage = 0;
974 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
975 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
977 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
978 device->device_parent, &texture_desc, 0, &ds)))
980 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
981 goto err;
984 desc.format_id = ds->resource.format->id;
985 desc.flags = 0;
986 desc.u.texture.level_idx = 0;
987 desc.u.texture.level_count = 1;
988 desc.u.texture.layer_idx = 0;
989 desc.u.texture.layer_count = 1;
990 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
991 &device->auto_depth_stencil_view);
992 wined3d_texture_decref(ds);
993 if (FAILED(hr))
995 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
996 goto err;
1001 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1003 return WINED3D_OK;
1005 err:
1006 if (displaymode_set)
1008 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1009 adapter->ordinal, &swapchain->original_mode)))
1010 ERR("Failed to restore display mode.\n");
1011 ClipCursor(NULL);
1014 if (swapchain->back_buffers)
1016 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1018 if (swapchain->back_buffers[i])
1020 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1021 wined3d_texture_decref(swapchain->back_buffers[i]);
1024 heap_free(swapchain->back_buffers);
1027 wined3d_cs_destroy_object(device->cs, wined3d_swapchain_destroy_object, swapchain);
1028 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1030 if (swapchain->front_buffer)
1032 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1033 wined3d_texture_decref(swapchain->front_buffer);
1036 return hr;
1039 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1040 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1042 struct wined3d_swapchain *object;
1043 HRESULT hr;
1045 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1046 device, desc, parent, parent_ops, swapchain);
1048 if (!(object = heap_alloc_zero(sizeof(*object))))
1049 return E_OUTOFMEMORY;
1051 wined3d_mutex_lock();
1052 hr = swapchain_init(object, device, desc, parent, parent_ops);
1053 wined3d_mutex_unlock();
1054 if (FAILED(hr))
1056 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1057 heap_free(object);
1058 return hr;
1061 if (desc->flags & WINED3D_SWAPCHAIN_IMPLICIT)
1063 wined3d_mutex_lock();
1064 if (FAILED(hr = wined3d_device_set_implicit_swapchain(device, object)))
1066 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1067 swapchain_cleanup(object);
1068 wined3d_mutex_unlock();
1069 heap_free(swapchain);
1070 return hr;
1072 wined3d_mutex_unlock();
1075 TRACE("Created swapchain %p.\n", object);
1076 *swapchain = object;
1078 return WINED3D_OK;
1081 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1083 struct wined3d_context **ctx_array;
1084 struct wined3d_context *ctx;
1086 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1088 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1090 ERR("Failed to create a new context for the swapchain\n");
1091 return NULL;
1093 context_release(ctx);
1095 if (!(ctx_array = heap_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1097 ERR("Out of memory when trying to allocate a new context array\n");
1098 wined3d_context_destroy(ctx);
1099 return NULL;
1101 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1102 heap_free(swapchain->context);
1103 ctx_array[swapchain->num_contexts] = ctx;
1104 swapchain->context = ctx_array;
1105 swapchain->num_contexts++;
1107 TRACE("Returning context %p\n", ctx);
1108 return ctx;
1111 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1113 unsigned int i;
1115 for (i = 0; i < swapchain->num_contexts; ++i)
1117 wined3d_context_destroy(swapchain->context[i]);
1119 heap_free(swapchain->context);
1120 swapchain->num_contexts = 0;
1121 swapchain->context = NULL;
1124 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1126 DWORD tid = GetCurrentThreadId();
1127 unsigned int i;
1129 for (i = 0; i < swapchain->num_contexts; ++i)
1131 if (swapchain->context[i]->tid == tid)
1132 return swapchain->context[i];
1135 /* Create a new context for the thread */
1136 return swapchain_create_context(swapchain);
1139 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1141 if (!swapchain->backup_dc)
1143 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1145 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1146 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1148 ERR("Failed to create a window.\n");
1149 return NULL;
1152 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1154 ERR("Failed to get a DC.\n");
1155 DestroyWindow(swapchain->backup_wnd);
1156 swapchain->backup_wnd = NULL;
1157 return NULL;
1161 return swapchain->backup_dc;
1164 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1166 UINT i;
1168 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1170 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1172 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1176 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1178 struct wined3d_device *device = swapchain->device;
1179 BOOL filter_messages = device->filter_messages;
1180 BOOL focus_messages = device->wined3d->flags & WINED3D_FOCUS_MESSAGES;
1182 /* This code is not protected by the wined3d mutex, so it may run while
1183 * wined3d_device_reset is active. Testing on Windows shows that changing
1184 * focus during resets and resetting during focus change events causes
1185 * the application to crash with an invalid memory access. */
1187 if (!focus_messages)
1188 device->filter_messages = 1;
1190 if (activate)
1192 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1194 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1195 * the window but leaves the size untouched, d3d9 sets the size on an
1196 * invisible window, generates messages but doesn't change the window
1197 * properties. The implementation follows d3d9.
1199 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1200 * resume drawing after a focus loss. */
1201 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1202 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1203 SWP_NOACTIVATE | SWP_NOZORDER);
1206 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1208 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1209 device->adapter->ordinal, &swapchain->d3d_mode)))
1210 ERR("Failed to set display mode.\n");
1213 if (swapchain == device->swapchains[0])
1214 device->device_parent->ops->activate(device->device_parent, TRUE);
1216 else
1218 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1219 device->adapter->ordinal, NULL)))
1220 ERR("Failed to set display mode.\n");
1222 swapchain->reapply_mode = TRUE;
1224 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1225 * during window minimization. Do our housekeeping now, as the device may not exist after
1226 * the ShowWindow call.
1228 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1229 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1230 * we'll have to control this via a create flag. Note that the device and swapchain are not
1231 * safe to access after the ShowWindow call. */
1232 if (swapchain == device->swapchains[0])
1233 device->device_parent->ops->activate(device->device_parent, FALSE);
1235 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1236 && IsWindowVisible(swapchain->device_window))
1237 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1240 if (!focus_messages)
1241 device->filter_messages = filter_messages;
1244 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1245 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1246 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1248 BOOL update_desc = FALSE;
1250 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1251 "multisample_type %#x, multisample_quality %#x.\n",
1252 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1253 multisample_type, multisample_quality);
1255 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1257 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1258 FIXME("Cannot change the back buffer count yet.\n");
1260 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
1262 if (!width || !height)
1264 /* The application is requesting that either the swapchain width or
1265 * height be set to the corresponding dimension in the window's
1266 * client rect. */
1268 RECT client_rect;
1270 if (!swapchain->desc.windowed)
1271 return WINED3DERR_INVALIDCALL;
1273 if (!GetClientRect(swapchain->device_window, &client_rect))
1275 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1276 return WINED3DERR_INVALIDCALL;
1279 if (!width)
1280 width = client_rect.right;
1282 if (!height)
1283 height = client_rect.bottom;
1286 if (width != swapchain->desc.backbuffer_width
1287 || height != swapchain->desc.backbuffer_height)
1289 swapchain->desc.backbuffer_width = width;
1290 swapchain->desc.backbuffer_height = height;
1291 update_desc = TRUE;
1294 if (format_id == WINED3DFMT_UNKNOWN)
1296 if (!swapchain->desc.windowed)
1297 return WINED3DERR_INVALIDCALL;
1298 format_id = swapchain->original_mode.format_id;
1301 if (format_id != swapchain->desc.backbuffer_format)
1303 swapchain->desc.backbuffer_format = format_id;
1304 update_desc = TRUE;
1307 if (multisample_type != swapchain->desc.multisample_type
1308 || multisample_quality != swapchain->desc.multisample_quality)
1310 swapchain->desc.multisample_type = multisample_type;
1311 swapchain->desc.multisample_quality = multisample_quality;
1312 update_desc = TRUE;
1315 if (update_desc)
1317 HRESULT hr;
1318 UINT i;
1320 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1321 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1322 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1323 return hr;
1325 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1327 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1328 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1329 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1330 return hr;
1334 swapchain_update_render_to_fbo(swapchain);
1335 swapchain_update_draw_bindings(swapchain);
1337 return WINED3D_OK;
1340 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1341 struct wined3d_display_mode *mode)
1343 struct wined3d_device *device = swapchain->device;
1344 HRESULT hr;
1346 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1348 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1349 device->adapter->ordinal, mode)))
1351 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1355 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1356 device->adapter->ordinal, mode)))
1358 WARN("Failed to set display mode, hr %#x.\n", hr);
1359 return WINED3DERR_INVALIDCALL;
1362 return WINED3D_OK;
1365 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1366 const struct wined3d_display_mode *mode)
1368 struct wined3d_display_mode actual_mode;
1369 RECT original_window_rect, window_rect;
1370 struct wined3d_device *device;
1371 HWND window;
1372 HRESULT hr;
1374 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1376 wined3d_mutex_lock();
1378 device = swapchain->device;
1379 window = swapchain->device_window;
1381 if (swapchain->desc.windowed)
1383 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1384 AdjustWindowRectEx(&window_rect,
1385 GetWindowLongW(window, GWL_STYLE), FALSE,
1386 GetWindowLongW(window, GWL_EXSTYLE));
1387 SetRect(&window_rect, 0, 0,
1388 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1389 GetWindowRect(window, &original_window_rect);
1390 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1392 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1394 actual_mode = *mode;
1395 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1397 wined3d_mutex_unlock();
1398 return hr;
1400 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1402 else
1404 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1405 &actual_mode, NULL)))
1407 ERR("Failed to get display mode, hr %#x.\n", hr);
1408 wined3d_mutex_unlock();
1409 return hr;
1412 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1415 wined3d_mutex_unlock();
1417 MoveWindow(window, window_rect.left, window_rect.top,
1418 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, TRUE);
1420 return WINED3D_OK;
1423 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1424 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1426 struct wined3d_device *device = swapchain->device;
1427 struct wined3d_display_mode actual_mode;
1428 HRESULT hr;
1430 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1432 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1434 if (mode)
1436 actual_mode = *mode;
1438 else
1440 if (!swapchain_desc->windowed)
1442 actual_mode.width = swapchain_desc->backbuffer_width;
1443 actual_mode.height = swapchain_desc->backbuffer_height;
1444 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1445 actual_mode.format_id = adapter_format_from_backbuffer_format(swapchain,
1446 swapchain_desc->backbuffer_format);
1447 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1449 else
1451 actual_mode = swapchain->original_mode;
1455 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1456 return hr;
1458 else
1460 if (mode)
1461 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1463 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1464 &actual_mode, NULL)))
1466 ERR("Failed to get display mode, hr %#x.\n", hr);
1467 return WINED3DERR_INVALIDCALL;
1471 if (!swapchain_desc->windowed)
1473 unsigned int width = actual_mode.width;
1474 unsigned int height = actual_mode.height;
1476 if (swapchain->desc.windowed)
1478 /* Switch from windowed to fullscreen */
1479 HWND focus_window = device->create_parms.focus_window;
1480 if (!focus_window)
1481 focus_window = swapchain->device_window;
1482 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1484 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1485 return hr;
1488 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1490 else
1492 /* Fullscreen -> fullscreen mode change */
1493 BOOL filter_messages = device->filter_messages;
1494 device->filter_messages = TRUE;
1496 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1497 ShowWindow(swapchain->device_window, SW_SHOW);
1499 device->filter_messages = filter_messages;
1501 swapchain->d3d_mode = actual_mode;
1503 else if (!swapchain->desc.windowed)
1505 /* Fullscreen -> windowed switch */
1506 RECT *window_rect = NULL;
1507 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1508 window_rect = &swapchain->original_window_rect;
1509 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1510 wined3d_device_release_focus_window(device);
1513 swapchain->desc.windowed = swapchain_desc->windowed;
1515 return WINED3D_OK;