mstask: Implement ITask::DeleteTrigger().
[wine.git] / dlls / wined3d / swapchain.c
blob2aec612018c33161cd7f3cf14f7f258d06c7d9d3
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 static void wined3d_swapchain_destroy_object(void *object)
32 swapchain_destroy_contexts(object);
35 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
37 HRESULT hr;
38 UINT i;
40 TRACE("Destroying swapchain %p.\n", swapchain);
42 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
44 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
45 * is the last buffer to be destroyed, FindContext() depends on that. */
46 if (swapchain->front_buffer)
48 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
49 if (wined3d_texture_decref(swapchain->front_buffer))
50 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
51 swapchain->front_buffer = NULL;
54 if (swapchain->back_buffers)
56 i = swapchain->desc.backbuffer_count;
58 while (i--)
60 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
61 if (wined3d_texture_decref(swapchain->back_buffers[i]))
62 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
64 heap_free(swapchain->back_buffers);
65 swapchain->back_buffers = NULL;
68 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
69 swapchain->device->cs->ops->finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
71 /* Restore the screen resolution if we rendered in fullscreen.
72 * This will restore the screen resolution to what it was before creating
73 * the swapchain. In case of d3d8 and d3d9 this will be the original
74 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
75 * sets the resolution before starting up Direct3D, thus orig_width and
76 * orig_height will be equal to the modes in the presentation params. */
77 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
79 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
80 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
81 ERR("Failed to restore display mode, hr %#x.\n", hr);
83 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
85 wined3d_device_restore_fullscreen_window(swapchain->device, swapchain->device_window,
86 &swapchain->original_window_rect);
87 wined3d_device_release_focus_window(swapchain->device);
91 if (swapchain->backup_dc)
93 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
95 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
96 DestroyWindow(swapchain->backup_wnd);
100 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedIncrement(&swapchain->ref);
104 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
106 return refcount;
109 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
111 ULONG refcount = InterlockedDecrement(&swapchain->ref);
113 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
115 if (!refcount)
117 struct wined3d_device *device = swapchain->device;
119 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
121 swapchain_cleanup(swapchain);
122 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
123 heap_free(swapchain);
126 return refcount;
129 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
131 TRACE("swapchain %p.\n", swapchain);
133 return swapchain->parent;
136 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
138 if (!window)
139 window = swapchain->device_window;
140 if (window == swapchain->win_handle)
141 return;
143 TRACE("Setting swapchain %p window from %p to %p.\n",
144 swapchain, swapchain->win_handle, window);
145 swapchain->win_handle = window;
148 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
149 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
150 unsigned int swap_interval, DWORD flags)
152 RECT s, d;
154 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
155 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
156 dst_window_override, flags);
158 if (flags)
159 FIXME("Ignoring flags %#x.\n", flags);
161 if (!swapchain->back_buffers)
163 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
164 return WINED3DERR_INVALIDCALL;
167 if (!src_rect)
169 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
170 swapchain->desc.backbuffer_height);
171 src_rect = &s;
174 if (!dst_rect)
176 GetClientRect(swapchain->win_handle, &d);
177 dst_rect = &d;
180 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
181 dst_rect, dst_window_override, swap_interval, flags);
183 return WINED3D_OK;
186 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
187 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
189 RECT src_rect, dst_rect;
191 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
193 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
194 dst_rect = src_rect;
196 if (swapchain->desc.windowed)
198 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
199 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
200 wine_dbgstr_rect(&dst_rect));
203 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
204 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
207 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
208 UINT back_buffer_idx)
210 TRACE("swapchain %p, back_buffer_idx %u.\n",
211 swapchain, back_buffer_idx);
213 /* Return invalid if there is no backbuffer array, otherwise it will
214 * crash when ddraw is used (there swapchain->back_buffers is always
215 * NULL). We need this because this function is called from
216 * stateblock_init_default_state() to get the default scissorrect
217 * dimensions. */
218 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
220 WARN("Invalid back buffer index.\n");
221 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
222 * here in wined3d to avoid problems in other libs. */
223 return NULL;
226 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
228 return swapchain->back_buffers[back_buffer_idx];
231 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
232 struct wined3d_raster_status *raster_status)
234 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
236 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
237 swapchain->device->adapter->ordinal, raster_status);
240 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
241 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
243 HRESULT hr;
245 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
247 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
248 swapchain->device->adapter->ordinal, mode, rotation);
250 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
251 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
253 return hr;
256 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
258 TRACE("swapchain %p.\n", swapchain);
260 return swapchain->device;
263 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
264 struct wined3d_swapchain_desc *desc)
266 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
268 *desc = swapchain->desc;
271 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
272 DWORD flags, const struct wined3d_gamma_ramp *ramp)
274 HDC dc;
276 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
278 if (flags)
279 FIXME("Ignoring flags %#x.\n", flags);
281 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
282 SetDeviceGammaRamp(dc, (void *)ramp);
283 ReleaseDC(swapchain->device_window, dc);
285 return WINED3D_OK;
288 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
290 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
291 swapchain->palette = palette;
294 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
295 struct wined3d_gamma_ramp *ramp)
297 HDC dc;
299 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
301 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
302 GetDeviceGammaRamp(dc, ramp);
303 ReleaseDC(swapchain->device_window, dc);
305 return WINED3D_OK;
308 /* A GL context is provided by the caller */
309 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
310 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
312 struct wined3d_texture *texture = swapchain->back_buffers[0];
313 struct wined3d_device *device = swapchain->device;
314 enum wined3d_texture_filter_type filter;
315 DWORD location;
317 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
318 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
320 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
321 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
322 || is_complex_fixup(texture->resource.format->color_fixup))
323 filter = WINED3D_TEXF_NONE;
324 else
325 filter = WINED3D_TEXF_LINEAR;
327 location = WINED3D_LOCATION_TEXTURE_RGB;
328 if (texture->resource.multisample_type)
329 location = WINED3D_LOCATION_RB_RESOLVED;
331 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
332 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
333 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
334 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
337 /* Context activation is done by the caller. */
338 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
340 struct wined3d_texture_sub_resource *sub_resource;
341 struct wined3d_texture *texture, *texture_prev;
342 struct gl_texture tex0;
343 GLuint rb0;
344 DWORD locations0;
345 unsigned int i;
346 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
348 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
349 return;
351 texture_prev = swapchain->back_buffers[0];
353 /* Back buffer 0 is already in the draw binding. */
354 tex0 = texture_prev->texture_rgb;
355 rb0 = texture_prev->rb_multisample;
356 locations0 = texture_prev->sub_resources[0].locations;
358 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
360 texture = swapchain->back_buffers[i];
361 sub_resource = &texture->sub_resources[0];
363 if (!(sub_resource->locations & supported_locations))
364 wined3d_texture_load_location(texture, 0, context, texture->resource.draw_binding);
366 texture_prev->texture_rgb = texture->texture_rgb;
367 texture_prev->rb_multisample = texture->rb_multisample;
369 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
370 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
372 texture_prev = texture;
375 texture_prev->texture_rgb = tex0;
376 texture_prev->rb_multisample = rb0;
378 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
379 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
381 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
384 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
385 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
387 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
388 const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
389 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
390 const struct wined3d_gl_info *gl_info;
391 struct wined3d_texture *logo_texture;
392 struct wined3d_context *context;
393 BOOL render_to_fbo;
395 context = context_acquire(swapchain->device, back_buffer, 0);
396 if (!context->valid)
398 context_release(context);
399 WARN("Invalid context, skipping present.\n");
400 return;
403 gl_info = context->gl_info;
405 if ((logo_texture = swapchain->device->logo_texture))
407 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
409 /* Blit the logo into the upper left corner of the drawable. */
410 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
411 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
414 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
415 && !swapchain->device->hardwareCursor)
417 RECT dst_rect =
419 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
420 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
421 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
422 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
424 RECT src_rect =
426 0, 0,
427 swapchain->device->cursor_texture->resource.width,
428 swapchain->device->cursor_texture->resource.height
430 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
432 TRACE("Rendering the software cursor.\n");
434 if (swapchain->desc.windowed)
435 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
436 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
437 wined3d_texture_blt(back_buffer, 0, &dst_rect,
438 swapchain->device->cursor_texture, 0, &src_rect,
439 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
442 TRACE("Presenting HDC %p.\n", context->hdc);
444 if (!(render_to_fbo = swapchain->render_to_fbo)
445 && (src_rect->left || src_rect->top
446 || src_rect->right != swapchain->desc.backbuffer_width
447 || src_rect->bottom != swapchain->desc.backbuffer_height
448 || dst_rect->left || dst_rect->top
449 || dst_rect->right != swapchain->desc.backbuffer_width
450 || dst_rect->bottom != swapchain->desc.backbuffer_height))
451 render_to_fbo = TRUE;
453 /* Rendering to a window of different size, presenting partial rectangles,
454 * or rendering to a different window needs help from FBO_blit or a textured
455 * draw. Render the swapchain to a FBO in the future.
457 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
458 * all these issues - this fails if the window is smaller than the backbuffer.
460 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
462 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
463 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
464 swapchain->render_to_fbo = TRUE;
465 swapchain_update_draw_bindings(swapchain);
467 else
469 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
472 if (swapchain->render_to_fbo)
473 swapchain_blit(swapchain, context, src_rect, dst_rect);
475 if (swapchain->num_contexts > 1)
476 gl_info->gl_ops.gl.p_glFinish();
478 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
479 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
481 wined3d_swapchain_rotate(swapchain, context);
483 TRACE("SwapBuffers called, Starting new frame\n");
484 /* FPS support */
485 if (TRACE_ON(fps))
487 DWORD time = GetTickCount();
488 ++swapchain->frames;
490 /* every 1.5 seconds */
491 if (time - swapchain->prev_time > 1500)
493 TRACE_(fps)("%p @ approx %.2ffps\n",
494 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
495 swapchain->prev_time = time;
496 swapchain->frames = 0;
500 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
501 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
502 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
503 * and INTEXTURE copies can keep their old content if they have any defined content.
504 * If the swapeffect is COPY, the content remains the same.
506 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
507 * up to date and hope WGL flipped front and back buffers and read this data into
508 * the FBO. Don't bother about this for now. */
509 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD
510 || swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
511 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
512 0, WINED3D_LOCATION_DISCARDED);
514 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
516 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
518 if ((swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
519 || ds->flags & WINED3D_TEXTURE_DISCARD))
520 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
523 context_release(context);
526 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
528 struct wined3d_texture *front_buffer = swapchain->front_buffer;
529 struct wined3d_context *context;
531 context = context_acquire(swapchain->device, front_buffer, 0);
532 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
533 context_release(context);
534 SetRectEmpty(&swapchain->front_buffer_update);
537 static const struct wined3d_swapchain_ops swapchain_gl_ops =
539 swapchain_gl_present,
540 swapchain_gl_frontbuffer_updated,
543 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
545 struct wined3d_dc_info *front;
546 POINT offset = {0, 0};
547 HDC src_dc, dst_dc;
548 RECT draw_rect;
549 HWND window;
551 TRACE("swapchain %p.\n", swapchain);
553 front = &swapchain->front_buffer->dc_info[0];
554 if (swapchain->palette)
555 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
557 if (swapchain->front_buffer->resource.map_count)
558 ERR("Trying to blit a mapped surface.\n");
560 TRACE("Copying surface %p to screen.\n", front);
562 src_dc = front->dc;
563 window = swapchain->win_handle;
564 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
566 /* Front buffer coordinates are screen coordinates. Map them to the
567 * destination window if not fullscreened. */
568 if (swapchain->desc.windowed)
569 ClientToScreen(window, &offset);
571 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
573 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
574 swapchain->front_buffer->resource.height);
575 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
577 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
578 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
579 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
580 ReleaseDC(window, dst_dc);
582 SetRectEmpty(&swapchain->front_buffer_update);
585 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
586 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
588 struct wined3d_dc_info *front, *back;
589 HBITMAP bitmap;
590 void *data;
591 HDC dc;
593 front = &swapchain->front_buffer->dc_info[0];
594 back = &swapchain->back_buffers[0]->dc_info[0];
596 /* Flip the surface data. */
597 dc = front->dc;
598 bitmap = front->bitmap;
599 data = swapchain->front_buffer->resource.heap_memory;
601 front->dc = back->dc;
602 front->bitmap = back->bitmap;
603 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
605 back->dc = dc;
606 back->bitmap = bitmap;
607 swapchain->back_buffers[0]->resource.heap_memory = data;
609 /* FPS support */
610 if (TRACE_ON(fps))
612 static LONG prev_time, frames;
613 DWORD time = GetTickCount();
615 ++frames;
617 /* every 1.5 seconds */
618 if (time - prev_time > 1500)
620 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
621 prev_time = time;
622 frames = 0;
626 SetRect(&swapchain->front_buffer_update, 0, 0,
627 swapchain->front_buffer->resource.width,
628 swapchain->front_buffer->resource.height);
629 swapchain_gdi_frontbuffer_updated(swapchain);
632 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
634 swapchain_gdi_present,
635 swapchain_gdi_frontbuffer_updated,
638 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
640 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
641 return;
643 if (!swapchain->desc.backbuffer_count)
645 TRACE("Single buffered rendering.\n");
646 swapchain->render_to_fbo = FALSE;
647 return;
650 TRACE("Rendering to FBO.\n");
651 swapchain->render_to_fbo = TRUE;
654 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
655 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
657 const struct wined3d_gl_info *gl_info;
658 const struct wined3d_format *format;
659 enum wined3d_multisample_type t;
661 if (wined3d_settings.sample_count == ~0u)
662 return;
664 gl_info = &swapchain->device->adapter->gl_info;
665 if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
666 return;
668 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
669 while (!(format->multisample_types & 1u << (t - 1)))
670 ++t;
671 TRACE("Using sample count %u.\n", t);
672 *type = t;
673 *quality = 0;
676 void wined3d_swapchain_set_swap_interval(struct wined3d_swapchain *swapchain,
677 unsigned int swap_interval)
679 const struct wined3d_gl_info *gl_info;
680 struct wined3d_context *context;
682 swap_interval = swap_interval <= 4 ? swap_interval : 1;
683 if (swapchain->swap_interval == swap_interval)
684 return;
686 swapchain->swap_interval = swap_interval;
688 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
689 gl_info = context->gl_info;
691 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
693 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
694 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x.\n",
695 swap_interval, context, GetLastError());
698 context_release(context);
701 static void wined3d_swapchain_cs_init(void *object)
703 struct wined3d_swapchain *swapchain = object;
704 const struct wined3d_gl_info *gl_info;
705 unsigned int i;
707 static const enum wined3d_format_id formats[] =
709 WINED3DFMT_D24_UNORM_S8_UINT,
710 WINED3DFMT_D32_UNORM,
711 WINED3DFMT_R24_UNORM_X8_TYPELESS,
712 WINED3DFMT_D16_UNORM,
713 WINED3DFMT_S1_UINT_D15_UNORM,
716 gl_info = &swapchain->device->adapter->gl_info;
718 /* Without ORM_FBO, switching the depth/stencil format is hard. Always
719 * request a depth/stencil buffer in the likely case it's needed later. */
720 for (i = 0; i < ARRAY_SIZE(formats); ++i)
722 swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
723 if ((swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
724 break;
725 TRACE("Depth stencil format %s is not supported, trying next format.\n", debug_d3dformat(formats[i]));
728 if (!swapchain->context[0])
730 WARN("Failed to create context.\n");
731 return;
733 swapchain->num_contexts = 1;
735 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
736 && (!swapchain->desc.enable_auto_depth_stencil
737 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
738 FIXME("Add OpenGL context recreation support.\n");
740 context_release(swapchain->context[0]);
743 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
744 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
746 const struct wined3d_adapter *adapter = device->adapter;
747 struct wined3d_resource_desc texture_desc;
748 BOOL displaymode_set = FALSE;
749 DWORD texture_flags = 0;
750 RECT client_rect;
751 unsigned int i;
752 HWND window;
753 HRESULT hr;
755 if (desc->backbuffer_count > 1)
757 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
758 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
761 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
762 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
763 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
764 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
766 if (device->wined3d->flags & WINED3D_NO3D)
767 swapchain->swapchain_ops = &swapchain_gdi_ops;
768 else
769 swapchain->swapchain_ops = &swapchain_gl_ops;
771 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
773 swapchain->device = device;
774 swapchain->parent = parent;
775 swapchain->parent_ops = parent_ops;
776 swapchain->ref = 1;
777 swapchain->win_handle = window;
778 swapchain->device_window = window;
779 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
781 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
782 adapter->ordinal, &swapchain->original_mode, NULL)))
784 ERR("Failed to get current display mode, hr %#x.\n", hr);
785 goto err;
787 GetWindowRect(window, &swapchain->original_window_rect);
789 GetClientRect(window, &client_rect);
790 if (desc->windowed)
792 if (!desc->backbuffer_width)
794 desc->backbuffer_width = client_rect.right;
795 TRACE("Updating width to %u.\n", desc->backbuffer_width);
798 if (!desc->backbuffer_height)
800 desc->backbuffer_height = client_rect.bottom;
801 TRACE("Updating height to %u.\n", desc->backbuffer_height);
804 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
806 desc->backbuffer_format = swapchain->original_mode.format_id;
807 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
810 swapchain->desc = *desc;
811 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
812 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
813 swapchain_update_render_to_fbo(swapchain);
815 TRACE("Creating front buffer.\n");
817 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
818 texture_desc.format = swapchain->desc.backbuffer_format;
819 texture_desc.multisample_type = swapchain->desc.multisample_type;
820 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
821 texture_desc.usage = 0;
822 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
823 texture_desc.width = swapchain->desc.backbuffer_width;
824 texture_desc.height = swapchain->desc.backbuffer_height;
825 texture_desc.depth = 1;
826 texture_desc.size = 0;
828 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
829 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
831 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
832 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
834 WARN("Failed to create front buffer, hr %#x.\n", hr);
835 goto err;
838 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
839 if (!(device->wined3d->flags & WINED3D_NO3D))
841 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
842 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
845 /* MSDN says we're only allowed a single fullscreen swapchain per device,
846 * so we should really check to see if there is a fullscreen swapchain
847 * already. Does a single head count as full screen? */
848 if (!desc->windowed)
850 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
852 /* Change the display settings */
853 swapchain->d3d_mode.width = desc->backbuffer_width;
854 swapchain->d3d_mode.height = desc->backbuffer_height;
855 swapchain->d3d_mode.format_id = desc->backbuffer_format;
856 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
857 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
859 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
860 adapter->ordinal, &swapchain->d3d_mode)))
862 WARN("Failed to set display mode, hr %#x.\n", hr);
863 goto err;
865 displaymode_set = TRUE;
867 else
869 swapchain->d3d_mode = swapchain->original_mode;
873 if (!(device->wined3d->flags & WINED3D_NO3D))
875 if (!(swapchain->context = heap_alloc(sizeof(*swapchain->context))))
877 ERR("Failed to create the context array.\n");
878 hr = E_OUTOFMEMORY;
879 goto err;
882 wined3d_cs_init_object(device->cs, wined3d_swapchain_cs_init, swapchain);
883 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
885 if (!swapchain->context[0])
887 hr = WINED3DERR_NOTAVAILABLE;
888 goto err;
892 if (swapchain->desc.backbuffer_count > 0)
894 if (!(swapchain->back_buffers = heap_calloc(swapchain->desc.backbuffer_count,
895 sizeof(*swapchain->back_buffers))))
897 ERR("Failed to allocate backbuffer array memory.\n");
898 hr = E_OUTOFMEMORY;
899 goto err;
902 texture_desc.usage = swapchain->desc.backbuffer_usage;
903 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
905 TRACE("Creating back buffer %u.\n", i);
906 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
907 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
909 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
910 swapchain->desc.backbuffer_count = i;
911 goto err;
913 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
917 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
918 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
920 TRACE("Creating depth/stencil buffer.\n");
921 if (!device->auto_depth_stencil_view)
923 struct wined3d_view_desc desc;
924 struct wined3d_texture *ds;
926 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
927 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
929 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
930 device->device_parent, &texture_desc, texture_flags, &ds)))
932 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
933 goto err;
936 desc.format_id = ds->resource.format->id;
937 desc.flags = 0;
938 desc.u.texture.level_idx = 0;
939 desc.u.texture.level_count = 1;
940 desc.u.texture.layer_idx = 0;
941 desc.u.texture.layer_count = 1;
942 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
943 &device->auto_depth_stencil_view);
944 wined3d_texture_decref(ds);
945 if (FAILED(hr))
947 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
948 goto err;
953 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
955 return WINED3D_OK;
957 err:
958 if (displaymode_set)
960 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
961 adapter->ordinal, &swapchain->original_mode)))
962 ERR("Failed to restore display mode.\n");
963 ClipCursor(NULL);
966 if (swapchain->back_buffers)
968 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
970 if (swapchain->back_buffers[i])
972 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
973 wined3d_texture_decref(swapchain->back_buffers[i]);
976 heap_free(swapchain->back_buffers);
979 wined3d_cs_destroy_object(swapchain->device->cs, wined3d_swapchain_destroy_object, swapchain);
980 swapchain->device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
982 if (swapchain->front_buffer)
984 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
985 wined3d_texture_decref(swapchain->front_buffer);
988 return hr;
991 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
992 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
994 struct wined3d_swapchain *object;
995 HRESULT hr;
997 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
998 device, desc, parent, parent_ops, swapchain);
1000 if (!(object = heap_alloc_zero(sizeof(*object))))
1001 return E_OUTOFMEMORY;
1003 hr = swapchain_init(object, device, desc, parent, parent_ops);
1004 if (FAILED(hr))
1006 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1007 heap_free(object);
1008 return hr;
1011 TRACE("Created swapchain %p.\n", object);
1012 *swapchain = object;
1014 return WINED3D_OK;
1017 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1019 struct wined3d_context **ctx_array;
1020 struct wined3d_context *ctx;
1022 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1024 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1026 ERR("Failed to create a new context for the swapchain\n");
1027 return NULL;
1029 context_release(ctx);
1031 if (!(ctx_array = heap_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1033 ERR("Out of memory when trying to allocate a new context array\n");
1034 context_destroy(swapchain->device, ctx);
1035 return NULL;
1037 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1038 heap_free(swapchain->context);
1039 ctx_array[swapchain->num_contexts] = ctx;
1040 swapchain->context = ctx_array;
1041 swapchain->num_contexts++;
1043 TRACE("Returning context %p\n", ctx);
1044 return ctx;
1047 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1049 unsigned int i;
1051 for (i = 0; i < swapchain->num_contexts; ++i)
1053 context_destroy(swapchain->device, swapchain->context[i]);
1055 heap_free(swapchain->context);
1056 swapchain->num_contexts = 0;
1057 swapchain->context = NULL;
1060 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1062 DWORD tid = GetCurrentThreadId();
1063 unsigned int i;
1065 for (i = 0; i < swapchain->num_contexts; ++i)
1067 if (swapchain->context[i]->tid == tid)
1068 return swapchain->context[i];
1071 /* Create a new context for the thread */
1072 return swapchain_create_context(swapchain);
1075 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1077 if (!swapchain->backup_dc)
1079 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1081 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1082 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1084 ERR("Failed to create a window.\n");
1085 return NULL;
1088 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1090 ERR("Failed to get a DC.\n");
1091 DestroyWindow(swapchain->backup_wnd);
1092 swapchain->backup_wnd = NULL;
1093 return NULL;
1097 return swapchain->backup_dc;
1100 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1102 UINT i;
1104 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1106 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1108 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1112 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1114 struct wined3d_device *device = swapchain->device;
1115 BOOL filter_messages = device->filter_messages;
1117 /* This code is not protected by the wined3d mutex, so it may run while
1118 * wined3d_device_reset is active. Testing on Windows shows that changing
1119 * focus during resets and resetting during focus change events causes
1120 * the application to crash with an invalid memory access. */
1122 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1124 if (activate)
1126 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1128 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1129 * the window but leaves the size untouched, d3d9 sets the size on an
1130 * invisible window, generates messages but doesn't change the window
1131 * properties. The implementation follows d3d9.
1133 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1134 * resume drawing after a focus loss. */
1135 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1136 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1137 SWP_NOACTIVATE | SWP_NOZORDER);
1140 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1142 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1143 device->adapter->ordinal, &swapchain->d3d_mode)))
1144 ERR("Failed to set display mode.\n");
1147 else
1149 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1150 device->adapter->ordinal, NULL)))
1151 ERR("Failed to set display mode.\n");
1153 swapchain->reapply_mode = TRUE;
1155 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1156 && IsWindowVisible(swapchain->device_window))
1157 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1160 device->filter_messages = filter_messages;
1163 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1164 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1165 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1167 struct wined3d_device *device = swapchain->device;
1168 BOOL update_desc = FALSE;
1170 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1171 "multisample_type %#x, multisample_quality %#x.\n",
1172 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1173 multisample_type, multisample_quality);
1175 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1177 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1178 FIXME("Cannot change the back buffer count yet.\n");
1180 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1182 if (!width || !height)
1184 /* The application is requesting that either the swapchain width or
1185 * height be set to the corresponding dimension in the window's
1186 * client rect. */
1188 RECT client_rect;
1190 if (!swapchain->desc.windowed)
1191 return WINED3DERR_INVALIDCALL;
1193 if (!GetClientRect(swapchain->device_window, &client_rect))
1195 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1196 return WINED3DERR_INVALIDCALL;
1199 if (!width)
1200 width = client_rect.right;
1202 if (!height)
1203 height = client_rect.bottom;
1206 if (width != swapchain->desc.backbuffer_width
1207 || height != swapchain->desc.backbuffer_height)
1209 swapchain->desc.backbuffer_width = width;
1210 swapchain->desc.backbuffer_height = height;
1211 update_desc = TRUE;
1214 if (format_id == WINED3DFMT_UNKNOWN)
1216 if (!swapchain->desc.windowed)
1217 return WINED3DERR_INVALIDCALL;
1218 format_id = swapchain->original_mode.format_id;
1221 if (format_id != swapchain->desc.backbuffer_format)
1223 swapchain->desc.backbuffer_format = format_id;
1224 update_desc = TRUE;
1227 if (multisample_type != swapchain->desc.multisample_type
1228 || multisample_quality != swapchain->desc.multisample_quality)
1230 swapchain->desc.multisample_type = multisample_type;
1231 swapchain->desc.multisample_quality = multisample_quality;
1232 update_desc = TRUE;
1235 if (update_desc)
1237 HRESULT hr;
1238 UINT i;
1240 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1241 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1242 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1243 return hr;
1245 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1247 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1248 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1249 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1250 return hr;
1254 swapchain_update_render_to_fbo(swapchain);
1255 swapchain_update_draw_bindings(swapchain);
1257 return WINED3D_OK;
1260 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1261 struct wined3d_display_mode *mode)
1263 struct wined3d_device *device = swapchain->device;
1264 HRESULT hr;
1266 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1268 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1269 device->adapter->ordinal, mode)))
1271 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1275 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1276 device->adapter->ordinal, mode)))
1278 WARN("Failed to set display mode, hr %#x.\n", hr);
1279 return WINED3DERR_INVALIDCALL;
1282 return WINED3D_OK;
1285 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1286 const struct wined3d_display_mode *mode)
1288 struct wined3d_device *device = swapchain->device;
1289 struct wined3d_display_mode actual_mode;
1290 RECT original_window_rect, window_rect;
1291 HRESULT hr;
1293 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1295 if (swapchain->desc.windowed)
1297 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1298 AdjustWindowRectEx(&window_rect,
1299 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1300 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1301 SetRect(&window_rect, 0, 0,
1302 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1303 GetWindowRect(swapchain->device_window, &original_window_rect);
1304 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1306 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1308 actual_mode = *mode;
1309 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1310 return hr;
1311 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1313 else
1315 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1316 &actual_mode, NULL)))
1318 ERR("Failed to get display mode, hr %#x.\n", hr);
1319 return WINED3DERR_INVALIDCALL;
1322 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1325 MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1326 window_rect.right - window_rect.left,
1327 window_rect.bottom - window_rect.top, TRUE);
1329 return WINED3D_OK;
1332 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1333 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1335 struct wined3d_device *device = swapchain->device;
1336 struct wined3d_display_mode actual_mode;
1337 HRESULT hr;
1339 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1341 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1343 if (mode)
1345 actual_mode = *mode;
1347 else
1349 if (!swapchain_desc->windowed)
1351 actual_mode.width = swapchain_desc->backbuffer_width;
1352 actual_mode.height = swapchain_desc->backbuffer_height;
1353 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1354 actual_mode.format_id = swapchain_desc->backbuffer_format;
1355 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1357 else
1359 actual_mode = swapchain->original_mode;
1363 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1364 return hr;
1366 else
1368 if (mode)
1369 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1371 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1372 &actual_mode, NULL)))
1374 ERR("Failed to get display mode, hr %#x.\n", hr);
1375 return WINED3DERR_INVALIDCALL;
1379 if (!swapchain_desc->windowed)
1381 unsigned int width = actual_mode.width;
1382 unsigned int height = actual_mode.height;
1384 if (swapchain->desc.windowed)
1386 /* Switch from windowed to fullscreen */
1387 HWND focus_window = device->create_parms.focus_window;
1388 if (!focus_window)
1389 focus_window = swapchain->device_window;
1390 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1392 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1393 return hr;
1396 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1398 else
1400 /* Fullscreen -> fullscreen mode change */
1401 BOOL filter_messages = device->filter_messages;
1402 device->filter_messages = TRUE;
1404 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1405 ShowWindow(swapchain->device_window, SW_SHOW);
1407 device->filter_messages = filter_messages;
1409 swapchain->d3d_mode = actual_mode;
1411 else if (!swapchain->desc.windowed)
1413 /* Fullscreen -> windowed switch */
1414 RECT *window_rect = NULL;
1415 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1416 window_rect = &swapchain->original_window_rect;
1417 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1418 wined3d_device_release_focus_window(device);
1421 swapchain->desc.windowed = swapchain_desc->windowed;
1423 return WINED3D_OK;