mshtml/tests: Fix a typo in ok() message.
[wine.git] / dlls / wined3d / swapchain.c
blob62bab65fcf3683fbebaaa7f92c22515df13c3fde
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 void wined3d_swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 HRESULT hr;
33 UINT i;
35 TRACE("Destroying swapchain %p.\n", swapchain);
37 wined3d_unhook_swapchain(swapchain);
38 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain->front_buffer)
44 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
45 if (wined3d_texture_decref(swapchain->front_buffer))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
47 swapchain->front_buffer = NULL;
50 if (swapchain->back_buffers)
52 i = swapchain->state.desc.backbuffer_count;
54 while (i--)
56 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
57 if (wined3d_texture_decref(swapchain->back_buffers[i]))
58 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
60 heap_free(swapchain->back_buffers);
61 swapchain->back_buffers = NULL;
64 /* Restore the screen resolution if we rendered in fullscreen.
65 * This will restore the screen resolution to what it was before creating
66 * the swapchain. In case of d3d8 and d3d9 this will be the original
67 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
68 * sets the resolution before starting up Direct3D, thus orig_width and
69 * orig_height will be equal to the modes in the presentation params. */
70 if (!swapchain->state.desc.windowed)
72 if (swapchain->state.desc.auto_restore_display_mode)
74 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
75 swapchain->device->adapter->ordinal, &swapchain->state.original_mode)))
76 ERR("Failed to restore display mode, hr %#x.\n", hr);
78 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
80 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state,
81 swapchain->state.device_window, &swapchain->state.original_window_rect);
82 wined3d_device_release_focus_window(swapchain->device);
85 else
87 wined3d_swapchain_state_restore_from_fullscreen(&swapchain->state, swapchain->state.device_window, NULL);
92 static void wined3d_swapchain_gl_destroy_object(void *object)
94 wined3d_swapchain_gl_destroy_contexts(object);
97 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl *swapchain_gl)
99 struct wined3d_cs *cs = swapchain_gl->s.device->cs;
101 wined3d_swapchain_cleanup(&swapchain_gl->s);
103 wined3d_cs_destroy_object(cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
104 wined3d_cs_finish(cs, WINED3D_CS_QUEUE_DEFAULT);
106 if (swapchain_gl->backup_dc)
108 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
110 wined3d_release_dc(swapchain_gl->backup_wnd, swapchain_gl->backup_dc);
111 DestroyWindow(swapchain_gl->backup_wnd);
115 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
117 ULONG refcount = InterlockedIncrement(&swapchain->ref);
119 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
121 return refcount;
124 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
126 ULONG refcount = InterlockedDecrement(&swapchain->ref);
128 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
130 if (!refcount)
132 struct wined3d_device *device;
134 wined3d_mutex_lock();
136 device = swapchain->device;
137 if (device->swapchain_count && device->swapchains[0] == swapchain)
138 wined3d_device_uninit_3d(device);
139 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
141 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
142 swapchain->device->adapter->adapter_ops->adapter_destroy_swapchain(swapchain);
144 wined3d_mutex_unlock();
147 return refcount;
150 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
152 TRACE("swapchain %p.\n", swapchain);
154 return swapchain->parent;
157 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
159 if (!window)
160 window = swapchain->state.device_window;
161 if (window == swapchain->win_handle)
162 return;
164 TRACE("Setting swapchain %p window from %p to %p.\n",
165 swapchain, swapchain->win_handle, window);
167 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
169 swapchain->win_handle = window;
172 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
173 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
174 unsigned int swap_interval, DWORD flags)
176 RECT s, d;
178 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
179 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
180 dst_window_override, swap_interval, flags);
182 if (flags)
183 FIXME("Ignoring flags %#x.\n", flags);
185 wined3d_mutex_lock();
187 if (!swapchain->back_buffers)
189 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
190 wined3d_mutex_unlock();
191 return WINED3DERR_INVALIDCALL;
194 if (!src_rect)
196 SetRect(&s, 0, 0, swapchain->state.desc.backbuffer_width,
197 swapchain->state.desc.backbuffer_height);
198 src_rect = &s;
201 if (!dst_rect)
203 GetClientRect(swapchain->win_handle, &d);
204 dst_rect = &d;
207 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
208 dst_rect, dst_window_override, swap_interval, flags);
210 wined3d_mutex_unlock();
212 return WINED3D_OK;
215 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
216 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
218 RECT src_rect, dst_rect;
220 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
222 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
223 dst_rect = src_rect;
225 if (swapchain->state.desc.windowed)
227 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
228 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
229 wine_dbgstr_rect(&dst_rect));
232 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
233 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
236 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
237 UINT back_buffer_idx)
239 TRACE("swapchain %p, back_buffer_idx %u.\n",
240 swapchain, back_buffer_idx);
242 /* Return invalid if there is no backbuffer array, otherwise it will
243 * crash when ddraw is used (there swapchain->back_buffers is always
244 * NULL). We need this because this function is called from
245 * stateblock_init_default_state() to get the default scissorrect
246 * dimensions. */
247 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->state.desc.backbuffer_count)
249 WARN("Invalid back buffer index.\n");
250 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
251 * here in wined3d to avoid problems in other libs. */
252 return NULL;
255 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
257 return swapchain->back_buffers[back_buffer_idx];
260 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
261 struct wined3d_raster_status *raster_status)
263 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
265 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
266 swapchain->device->adapter->ordinal, raster_status);
269 struct wined3d_swapchain_state * CDECL wined3d_swapchain_get_state(struct wined3d_swapchain *swapchain)
271 return &swapchain->state;
274 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
275 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
277 HRESULT hr;
279 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
281 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
282 swapchain->device->adapter->ordinal, mode, rotation);
284 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
285 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
287 return hr;
290 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
292 TRACE("swapchain %p.\n", swapchain);
294 return swapchain->device;
297 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
298 struct wined3d_swapchain_desc *desc)
300 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
302 *desc = swapchain->state.desc;
305 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
306 DWORD flags, const struct wined3d_gamma_ramp *ramp)
308 HDC dc;
310 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
312 if (flags)
313 FIXME("Ignoring flags %#x.\n", flags);
315 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
316 SetDeviceGammaRamp(dc, (void *)ramp);
317 ReleaseDC(swapchain->state.device_window, dc);
319 return WINED3D_OK;
322 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
324 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
326 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
328 swapchain->palette = palette;
331 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
332 struct wined3d_gamma_ramp *ramp)
334 HDC dc;
336 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
338 dc = GetDCEx(swapchain->state.device_window, 0, DCX_USESTYLE | DCX_CACHE);
339 GetDeviceGammaRamp(dc, ramp);
340 ReleaseDC(swapchain->state.device_window, dc);
342 return WINED3D_OK;
345 /* A GL context is provided by the caller */
346 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
347 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
349 struct wined3d_texture *texture = swapchain->back_buffers[0];
350 struct wined3d_device *device = swapchain->device;
351 enum wined3d_texture_filter_type filter;
352 DWORD location;
354 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
355 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
357 if ((src_rect->right - src_rect->left == dst_rect->right - dst_rect->left
358 && src_rect->bottom - src_rect->top == dst_rect->bottom - dst_rect->top)
359 || is_complex_fixup(texture->resource.format->color_fixup))
360 filter = WINED3D_TEXF_NONE;
361 else
362 filter = WINED3D_TEXF_LINEAR;
364 location = WINED3D_LOCATION_TEXTURE_RGB;
365 if (texture->resource.multisample_type)
366 location = WINED3D_LOCATION_RB_RESOLVED;
368 wined3d_texture_validate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
369 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context, texture, 0,
370 location, src_rect, texture, 0, WINED3D_LOCATION_DRAWABLE, dst_rect, NULL, filter);
371 wined3d_texture_invalidate_location(texture, 0, WINED3D_LOCATION_DRAWABLE);
374 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain *swapchain,
375 struct wined3d_context_gl *context_gl, unsigned int swap_interval)
377 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
379 swap_interval = swap_interval <= 4 ? swap_interval : 1;
380 if (swapchain->swap_interval == swap_interval)
381 return;
383 swapchain->swap_interval = swap_interval;
385 if (!gl_info->supported[WGL_EXT_SWAP_CONTROL])
386 return;
388 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
390 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
391 swap_interval, context_gl, GetLastError());
395 /* Context activation is done by the caller. */
396 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
398 struct wined3d_texture_sub_resource *sub_resource;
399 struct wined3d_texture_gl *texture, *texture_prev;
400 struct gl_texture tex0;
401 GLuint rb0;
402 DWORD locations0;
403 unsigned int i;
404 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
406 if (swapchain->state.desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
407 return;
409 texture_prev = wined3d_texture_gl(swapchain->back_buffers[0]);
411 /* Back buffer 0 is already in the draw binding. */
412 tex0 = texture_prev->texture_rgb;
413 rb0 = texture_prev->rb_multisample;
414 locations0 = texture_prev->t.sub_resources[0].locations;
416 for (i = 1; i < swapchain->state.desc.backbuffer_count; ++i)
418 texture = wined3d_texture_gl(swapchain->back_buffers[i]);
419 sub_resource = &texture->t.sub_resources[0];
421 if (!(sub_resource->locations & supported_locations))
422 wined3d_texture_load_location(&texture->t, 0, context, texture->t.resource.draw_binding);
424 texture_prev->texture_rgb = texture->texture_rgb;
425 texture_prev->rb_multisample = texture->rb_multisample;
427 wined3d_texture_validate_location(&texture_prev->t, 0, sub_resource->locations & supported_locations);
428 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(sub_resource->locations & supported_locations));
430 texture_prev = texture;
433 texture_prev->texture_rgb = tex0;
434 texture_prev->rb_multisample = rb0;
436 wined3d_texture_validate_location(&texture_prev->t, 0, locations0 & supported_locations);
437 wined3d_texture_invalidate_location(&texture_prev->t, 0, ~(locations0 & supported_locations));
439 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
442 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
443 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
445 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(swapchain);
446 const struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
447 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
448 const struct wined3d_fb_state *fb = &swapchain->device->cs->fb;
449 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
450 struct wined3d_texture *logo_texture, *cursor_texture;
451 const struct wined3d_gl_info *gl_info;
452 struct wined3d_context_gl *context_gl;
453 struct wined3d_context *context;
454 BOOL render_to_fbo;
456 context = context_acquire(swapchain->device, swapchain->front_buffer, 0);
457 context_gl = wined3d_context_gl(context);
458 if (!context_gl->valid)
460 context_release(context);
461 WARN("Invalid context, skipping present.\n");
462 return;
465 gl_info = context_gl->gl_info;
467 swapchain_gl_set_swap_interval(swapchain, context_gl, swap_interval);
469 if ((logo_texture = swapchain->device->logo_texture))
471 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
473 /* Blit the logo into the upper left corner of the drawable. */
474 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
475 WINED3D_BLT_SRC_CKEY, NULL, WINED3D_TEXF_POINT);
478 if ((cursor_texture = swapchain->device->cursor_texture)
479 && swapchain->device->bCursorVisible && !swapchain->device->hardwareCursor)
481 RECT dst_rect =
483 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
484 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
485 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
486 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
488 RECT src_rect =
490 0, 0, cursor_texture->resource.width, cursor_texture->resource.height
492 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
494 TRACE("Rendering the software cursor.\n");
496 if (desc->windowed)
497 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
498 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
499 wined3d_texture_blt(back_buffer, 0, &dst_rect, cursor_texture, 0,
500 &src_rect, WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
503 TRACE("Presenting DC %p.\n", context_gl->dc);
505 if (!(render_to_fbo = swapchain->render_to_fbo)
506 && (src_rect->left || src_rect->top
507 || src_rect->right != desc->backbuffer_width
508 || src_rect->bottom != desc->backbuffer_height
509 || dst_rect->left || dst_rect->top
510 || dst_rect->right != desc->backbuffer_width
511 || dst_rect->bottom != desc->backbuffer_height))
512 render_to_fbo = TRUE;
514 /* Rendering to a window of different size, presenting partial rectangles,
515 * or rendering to a different window needs help from FBO_blit or a textured
516 * draw. Render the swapchain to a FBO in the future.
518 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
519 * all these issues - this fails if the window is smaller than the backbuffer.
521 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
523 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
524 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
525 swapchain->render_to_fbo = TRUE;
526 swapchain_update_draw_bindings(swapchain);
528 else
530 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
533 if (swapchain->render_to_fbo)
534 swapchain_blit(swapchain, context, src_rect, dst_rect);
536 if (swapchain_gl->context_count > 1)
537 gl_info->gl_ops.gl.p_glFinish();
539 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
540 gl_info->gl_ops.wgl.p_wglSwapBuffers(context_gl->dc);
542 wined3d_swapchain_gl_rotate(swapchain, context);
544 TRACE("SwapBuffers called, Starting new frame\n");
545 /* FPS support */
546 if (TRACE_ON(fps))
548 DWORD time = GetTickCount();
549 ++swapchain->frames;
551 /* every 1.5 seconds */
552 if (time - swapchain->prev_time > 1500)
554 TRACE_(fps)("%p @ approx %.2ffps\n",
555 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
556 swapchain->prev_time = time;
557 swapchain->frames = 0;
561 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
562 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
563 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
564 * and INTEXTURE copies can keep their old content if they have any defined content.
565 * If the swapeffect is COPY, the content remains the same.
567 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
568 * up to date and hope WGL flipped front and back buffers and read this data into
569 * the FBO. Don't bother about this for now. */
570 if (desc->swap_effect == WINED3D_SWAP_EFFECT_DISCARD
571 || desc->swap_effect == WINED3D_SWAP_EFFECT_FLIP_DISCARD)
572 wined3d_texture_validate_location(swapchain->back_buffers[desc->backbuffer_count - 1],
573 0, WINED3D_LOCATION_DISCARDED);
575 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
577 struct wined3d_texture *ds = texture_from_resource(dsv->resource);
579 if ((desc->flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
580 || ds->flags & WINED3D_TEXTURE_DISCARD))
581 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
584 context_release(context);
587 static void swapchain_frontbuffer_updated(struct wined3d_swapchain *swapchain)
589 struct wined3d_texture *front_buffer = swapchain->front_buffer;
590 struct wined3d_context *context;
592 context = context_acquire(swapchain->device, front_buffer, 0);
593 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
594 context_release(context);
595 SetRectEmpty(&swapchain->front_buffer_update);
598 static const struct wined3d_swapchain_ops swapchain_gl_ops =
600 swapchain_gl_present,
601 swapchain_frontbuffer_updated,
604 static void swapchain_vk_present(struct wined3d_swapchain *swapchain, const RECT *src_rect,
605 const RECT *dst_rect, unsigned int swap_interval, uint32_t flags)
607 FIXME("Not implemented.\n");
610 static const struct wined3d_swapchain_ops swapchain_vk_ops =
612 swapchain_vk_present,
613 swapchain_frontbuffer_updated,
616 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
618 struct wined3d_dc_info *front;
619 POINT offset = {0, 0};
620 HDC src_dc, dst_dc;
621 RECT draw_rect;
622 HWND window;
624 TRACE("swapchain %p.\n", swapchain);
626 front = &swapchain->front_buffer->dc_info[0];
627 if (swapchain->palette)
628 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
630 if (swapchain->front_buffer->resource.map_count)
631 ERR("Trying to blit a mapped surface.\n");
633 TRACE("Copying surface %p to screen.\n", front);
635 src_dc = front->dc;
636 window = swapchain->win_handle;
637 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
639 /* Front buffer coordinates are screen coordinates. Map them to the
640 * destination window if not fullscreened. */
641 if (swapchain->state.desc.windowed)
642 ClientToScreen(window, &offset);
644 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
646 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
647 swapchain->front_buffer->resource.height);
648 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
650 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
651 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
652 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
653 ReleaseDC(window, dst_dc);
655 SetRectEmpty(&swapchain->front_buffer_update);
658 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
659 const RECT *src_rect, const RECT *dst_rect, unsigned int swap_interval, DWORD flags)
661 struct wined3d_dc_info *front, *back;
662 HBITMAP bitmap;
663 void *data;
664 HDC dc;
666 front = &swapchain->front_buffer->dc_info[0];
667 back = &swapchain->back_buffers[0]->dc_info[0];
669 /* Flip the surface data. */
670 dc = front->dc;
671 bitmap = front->bitmap;
672 data = swapchain->front_buffer->resource.heap_memory;
674 front->dc = back->dc;
675 front->bitmap = back->bitmap;
676 swapchain->front_buffer->resource.heap_memory = swapchain->back_buffers[0]->resource.heap_memory;
678 back->dc = dc;
679 back->bitmap = bitmap;
680 swapchain->back_buffers[0]->resource.heap_memory = data;
682 /* FPS support */
683 if (TRACE_ON(fps))
685 static LONG prev_time, frames;
686 DWORD time = GetTickCount();
688 ++frames;
690 /* every 1.5 seconds */
691 if (time - prev_time > 1500)
693 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
694 prev_time = time;
695 frames = 0;
699 SetRect(&swapchain->front_buffer_update, 0, 0,
700 swapchain->front_buffer->resource.width,
701 swapchain->front_buffer->resource.height);
702 swapchain_gdi_frontbuffer_updated(swapchain);
705 static const struct wined3d_swapchain_ops swapchain_no3d_ops =
707 swapchain_gdi_present,
708 swapchain_gdi_frontbuffer_updated,
711 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
713 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
714 return;
716 if (!swapchain->state.desc.backbuffer_count)
718 TRACE("Single buffered rendering.\n");
719 swapchain->render_to_fbo = FALSE;
720 return;
723 TRACE("Rendering to FBO.\n");
724 swapchain->render_to_fbo = TRUE;
727 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
728 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
730 const struct wined3d_adapter *adapter;
731 const struct wined3d_gl_info *gl_info;
732 const struct wined3d_format *format;
733 enum wined3d_multisample_type t;
735 if (wined3d_settings.sample_count == ~0u)
736 return;
738 adapter = swapchain->device->adapter;
739 gl_info = &adapter->gl_info;
740 if (!(format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET)))
741 return;
743 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
744 while (!(format->multisample_types & 1u << (t - 1)))
745 ++t;
746 TRACE("Using sample count %u.\n", t);
747 *type = t;
748 *quality = 0;
751 void swapchain_set_max_frame_latency(struct wined3d_swapchain *swapchain, const struct wined3d_device *device)
753 /* Subtract 1 for the implicit OpenGL latency. */
754 swapchain->max_frame_latency = device->max_frame_latency >= 2 ? device->max_frame_latency - 1 : 1;
757 static enum wined3d_format_id adapter_format_from_backbuffer_format(const struct wined3d_adapter *adapter,
758 enum wined3d_format_id format_id)
760 const struct wined3d_format *backbuffer_format;
762 backbuffer_format = wined3d_get_format(adapter, format_id, WINED3D_BIND_RENDER_TARGET);
763 return pixelformat_for_depth(backbuffer_format->byte_count * CHAR_BIT);
766 static HRESULT wined3d_swapchain_state_init(struct wined3d_swapchain_state *state,
767 const struct wined3d_swapchain_desc *desc, HWND window,
768 struct wined3d *wined3d, unsigned int adapter_idx)
770 HRESULT hr;
772 state->desc = *desc;
774 if (FAILED(hr = wined3d_get_adapter_display_mode(wined3d, adapter_idx, &state->original_mode, NULL)))
776 ERR("Failed to get current display mode, hr %#x.\n", hr);
777 return hr;
780 if (!desc->windowed)
782 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
784 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
786 state->d3d_mode.width = desc->backbuffer_width;
787 state->d3d_mode.height = desc->backbuffer_height;
788 state->d3d_mode.format_id = adapter_format_from_backbuffer_format(adapter, desc->backbuffer_format);
789 state->d3d_mode.refresh_rate = desc->refresh_rate;
790 state->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
792 else
794 state->d3d_mode = state->original_mode;
798 GetWindowRect(window, &state->original_window_rect);
799 state->device_window = window;
801 return hr;
804 static HRESULT wined3d_swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
805 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops,
806 const struct wined3d_swapchain_ops *swapchain_ops)
808 const struct wined3d_adapter *adapter = device->adapter;
809 struct wined3d_resource_desc texture_desc;
810 BOOL displaymode_set = FALSE;
811 DWORD texture_flags = 0;
812 RECT client_rect;
813 unsigned int i;
814 HWND window;
815 HRESULT hr;
817 wined3d_mutex_lock();
819 if (desc->backbuffer_count > 1)
821 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
822 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
825 if (desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
826 && desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
827 && desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
828 FIXME("Unimplemented swap effect %#x.\n", desc->swap_effect);
830 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
831 if (FAILED(hr = wined3d_swapchain_state_init(&swapchain->state, desc, window, device->wined3d, adapter->ordinal)))
832 return hr;
834 swapchain->swapchain_ops = swapchain_ops;
835 swapchain->device = device;
836 swapchain->parent = parent;
837 swapchain->parent_ops = parent_ops;
838 swapchain->ref = 1;
839 swapchain->win_handle = window;
840 swapchain->swap_interval = WINED3D_SWAP_INTERVAL_DEFAULT;
841 swapchain_set_max_frame_latency(swapchain, device);
843 GetClientRect(window, &client_rect);
844 if (desc->windowed)
846 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect));
848 if (!desc->backbuffer_width)
850 desc->backbuffer_width = client_rect.right ? client_rect.right : 8;
851 TRACE("Updating width to %u.\n", desc->backbuffer_width);
853 if (!desc->backbuffer_height)
855 desc->backbuffer_height = client_rect.bottom ? client_rect.bottom : 8;
856 TRACE("Updating height to %u.\n", desc->backbuffer_height);
859 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
861 desc->backbuffer_format = swapchain->state.original_mode.format_id;
862 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->state.original_mode.format_id));
865 else
867 wined3d_swapchain_state_setup_fullscreen(&swapchain->state,
868 window, desc->backbuffer_width, desc->backbuffer_height);
870 swapchain->state.desc = *desc;
871 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->state.desc.backbuffer_format,
872 &swapchain->state.desc.multisample_type, &swapchain->state.desc.multisample_quality);
873 swapchain_update_render_to_fbo(swapchain);
875 TRACE("Creating front buffer.\n");
877 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
878 texture_desc.format = swapchain->state.desc.backbuffer_format;
879 texture_desc.multisample_type = swapchain->state.desc.multisample_type;
880 texture_desc.multisample_quality = swapchain->state.desc.multisample_quality;
881 texture_desc.usage = 0;
882 if (device->wined3d->flags & WINED3D_NO3D)
883 texture_desc.usage |= WINED3DUSAGE_OWNDC;
884 texture_desc.bind_flags = 0;
885 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
886 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
887 texture_desc.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
888 texture_desc.width = swapchain->state.desc.backbuffer_width;
889 texture_desc.height = swapchain->state.desc.backbuffer_height;
890 texture_desc.depth = 1;
891 texture_desc.size = 0;
893 if (swapchain->state.desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
894 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
896 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
897 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
899 WARN("Failed to create front buffer, hr %#x.\n", hr);
900 goto err;
903 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
904 if (!(device->wined3d->flags & WINED3D_NO3D))
906 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
907 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
910 /* MSDN says we're only allowed a single fullscreen swapchain per device,
911 * so we should really check to see if there is a fullscreen swapchain
912 * already. Does a single head count as full screen? */
913 if (!desc->windowed && desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
915 /* Change the display settings */
916 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
917 adapter->ordinal, &swapchain->state.d3d_mode)))
919 WARN("Failed to set display mode, hr %#x.\n", hr);
920 goto err;
922 displaymode_set = TRUE;
925 if (swapchain->state.desc.backbuffer_count > 0)
927 if (!(swapchain->back_buffers = heap_calloc(swapchain->state.desc.backbuffer_count,
928 sizeof(*swapchain->back_buffers))))
930 ERR("Failed to allocate backbuffer array memory.\n");
931 hr = E_OUTOFMEMORY;
932 goto err;
935 texture_desc.bind_flags = swapchain->state.desc.backbuffer_bind_flags;
936 texture_desc.usage = 0;
937 if (device->wined3d->flags & WINED3D_NO3D)
938 texture_desc.usage |= WINED3DUSAGE_OWNDC;
939 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
941 TRACE("Creating back buffer %u.\n", i);
942 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
943 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
945 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
946 swapchain->state.desc.backbuffer_count = i;
947 goto err;
949 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
953 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
954 if (desc->enable_auto_depth_stencil)
956 TRACE("Creating depth/stencil buffer.\n");
957 if (!device->auto_depth_stencil_view)
959 struct wined3d_view_desc desc;
960 struct wined3d_texture *ds;
962 texture_desc.format = swapchain->state.desc.auto_depth_stencil_format;
963 texture_desc.usage = 0;
964 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
965 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
967 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
968 device->device_parent, &texture_desc, 0, &ds)))
970 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
971 goto err;
974 desc.format_id = ds->resource.format->id;
975 desc.flags = 0;
976 desc.u.texture.level_idx = 0;
977 desc.u.texture.level_count = 1;
978 desc.u.texture.layer_idx = 0;
979 desc.u.texture.layer_count = 1;
980 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
981 &device->auto_depth_stencil_view);
982 wined3d_texture_decref(ds);
983 if (FAILED(hr))
985 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
986 goto err;
991 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
993 wined3d_mutex_unlock();
995 return WINED3D_OK;
997 err:
998 if (displaymode_set)
1000 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1001 adapter->ordinal, &swapchain->state.original_mode)))
1002 ERR("Failed to restore display mode.\n");
1003 ClipCursor(NULL);
1006 if (swapchain->back_buffers)
1008 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1010 if (swapchain->back_buffers[i])
1012 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1013 wined3d_texture_decref(swapchain->back_buffers[i]);
1016 heap_free(swapchain->back_buffers);
1019 if (swapchain->front_buffer)
1021 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1022 wined3d_texture_decref(swapchain->front_buffer);
1025 wined3d_mutex_unlock();
1027 return hr;
1030 HRESULT wined3d_swapchain_no3d_init(struct wined3d_swapchain *swapchain_no3d, struct wined3d_device *device,
1031 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1033 TRACE("swapchain_no3d %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1034 swapchain_no3d, device, desc, parent, parent_ops);
1036 return wined3d_swapchain_init(swapchain_no3d, device, desc, parent, parent_ops, &swapchain_no3d_ops);
1039 HRESULT wined3d_swapchain_gl_init(struct wined3d_swapchain_gl *swapchain_gl, struct wined3d_device *device,
1040 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1042 HRESULT hr;
1044 TRACE("swapchain_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1045 swapchain_gl, device, desc, parent, parent_ops);
1047 if (FAILED(hr = wined3d_swapchain_init(&swapchain_gl->s, device, desc, parent, parent_ops, &swapchain_gl_ops)))
1049 /* Cleanup any context that may have been created for the swapchain. */
1050 wined3d_cs_destroy_object(device->cs, wined3d_swapchain_gl_destroy_object, swapchain_gl);
1051 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1054 return hr;
1057 HRESULT wined3d_swapchain_vk_init(struct wined3d_swapchain *swapchain_vk, struct wined3d_device *device,
1058 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1060 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1061 swapchain_vk, device, desc, parent, parent_ops);
1063 return wined3d_swapchain_init(swapchain_vk, device, desc, parent, parent_ops, &swapchain_vk_ops);
1066 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1067 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1069 struct wined3d_swapchain *object;
1070 HRESULT hr;
1072 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_swapchain(device,
1073 desc, parent, parent_ops, &object)))
1074 return hr;
1076 if (desc->flags & WINED3D_SWAPCHAIN_HOOK)
1077 wined3d_hook_swapchain(object);
1079 if (desc->flags & WINED3D_SWAPCHAIN_IMPLICIT)
1081 wined3d_mutex_lock();
1082 if (FAILED(hr = wined3d_device_set_implicit_swapchain(device, object)))
1084 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1085 device->adapter->adapter_ops->adapter_destroy_swapchain(object);
1086 wined3d_mutex_unlock();
1087 return hr;
1089 wined3d_mutex_unlock();
1092 *swapchain = object;
1094 return hr;
1097 static struct wined3d_context_gl *wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl *swapchain_gl)
1099 struct wined3d_device *device = swapchain_gl->s.device;
1100 struct wined3d_context_gl *context_gl;
1102 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl, GetCurrentThreadId());
1104 wined3d_from_cs(device->cs);
1106 if (!(context_gl = heap_alloc_zero(sizeof(*context_gl))))
1108 ERR("Failed to allocate context memory.\n");
1109 return NULL;
1112 if (FAILED(wined3d_context_gl_init(context_gl, swapchain_gl)))
1114 WARN("Failed to initialise context.\n");
1115 heap_free(context_gl);
1116 return NULL;
1119 if (!device_context_add(device, &context_gl->c))
1121 ERR("Failed to add the newly created context to the context list.\n");
1122 wined3d_context_gl_destroy(context_gl);
1123 return NULL;
1126 TRACE("Created context %p.\n", context_gl);
1128 context_release(&context_gl->c);
1130 if (!wined3d_array_reserve((void **)&swapchain_gl->contexts, &swapchain_gl->contexts_size,
1131 swapchain_gl->context_count + 1, sizeof(*swapchain_gl->contexts)))
1133 ERR("Failed to allocate new context array memory.\n");
1134 wined3d_context_gl_destroy(context_gl);
1135 return NULL;
1137 swapchain_gl->contexts[swapchain_gl->context_count++] = context_gl;
1139 return context_gl;
1142 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl *swapchain_gl)
1144 unsigned int i;
1146 for (i = 0; i < swapchain_gl->context_count; ++i)
1148 wined3d_context_gl_destroy(swapchain_gl->contexts[i]);
1150 heap_free(swapchain_gl->contexts);
1151 swapchain_gl->contexts_size = 0;
1152 swapchain_gl->context_count = 0;
1153 swapchain_gl->contexts = NULL;
1156 struct wined3d_context_gl *wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl *swapchain_gl)
1158 DWORD tid = GetCurrentThreadId();
1159 unsigned int i;
1161 for (i = 0; i < swapchain_gl->context_count; ++i)
1163 if (swapchain_gl->contexts[i]->tid == tid)
1164 return swapchain_gl->contexts[i];
1167 /* Create a new context for the thread. */
1168 return wined3d_swapchain_gl_create_context(swapchain_gl);
1171 HDC wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl *swapchain_gl)
1173 if (!swapchain_gl->backup_dc)
1175 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl);
1177 if (!(swapchain_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1178 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1180 ERR("Failed to create a window.\n");
1181 return NULL;
1184 if (!(swapchain_gl->backup_dc = GetDC(swapchain_gl->backup_wnd)))
1186 ERR("Failed to get a DC.\n");
1187 DestroyWindow(swapchain_gl->backup_wnd);
1188 swapchain_gl->backup_wnd = NULL;
1189 return NULL;
1193 return swapchain_gl->backup_dc;
1196 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1198 UINT i;
1200 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1202 for (i = 0; i < swapchain->state.desc.backbuffer_count; ++i)
1204 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1208 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1210 struct wined3d_device *device = swapchain->device;
1211 HWND window = swapchain->state.device_window;
1212 unsigned int screensaver_active;
1213 BOOL focus_messages, filter;
1215 /* This code is not protected by the wined3d mutex, so it may run while
1216 * wined3d_device_reset is active. Testing on Windows shows that changing
1217 * focus during resets and resetting during focus change events causes
1218 * the application to crash with an invalid memory access. */
1220 if (!(focus_messages = device->wined3d->flags & WINED3D_FOCUS_MESSAGES))
1221 filter = wined3d_filter_messages(window, TRUE);
1223 if (activate)
1225 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
1226 if ((device->restore_screensaver = !!screensaver_active))
1227 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
1229 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1231 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1232 * the window but leaves the size untouched, d3d9 sets the size on an
1233 * invisible window, generates messages but doesn't change the window
1234 * properties. The implementation follows d3d9.
1236 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1237 * resume drawing after a focus loss. */
1238 SetWindowPos(window, NULL, 0, 0, swapchain->state.desc.backbuffer_width,
1239 swapchain->state.desc.backbuffer_height, SWP_NOACTIVATE | SWP_NOZORDER);
1242 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1244 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1245 device->adapter->ordinal, &swapchain->state.d3d_mode)))
1246 ERR("Failed to set display mode.\n");
1249 if (swapchain == device->swapchains[0])
1250 device->device_parent->ops->activate(device->device_parent, TRUE);
1252 else
1254 if (device->restore_screensaver)
1256 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
1257 device->restore_screensaver = FALSE;
1260 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1261 device->adapter->ordinal, NULL)))
1262 ERR("Failed to set display mode.\n");
1264 swapchain->reapply_mode = TRUE;
1266 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1267 * during window minimization. Do our housekeeping now, as the device may not exist after
1268 * the ShowWindow call.
1270 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1271 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1272 * we'll have to control this via a create flag. Note that the device and swapchain are not
1273 * safe to access after the ShowWindow call. */
1274 if (swapchain == device->swapchains[0])
1275 device->device_parent->ops->activate(device->device_parent, FALSE);
1277 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES) && IsWindowVisible(window))
1278 ShowWindow(window, SW_MINIMIZE);
1281 if (!focus_messages)
1282 wined3d_filter_messages(window, filter);
1285 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1286 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1287 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1289 struct wined3d_swapchain_desc *desc = &swapchain->state.desc;
1290 BOOL update_desc = FALSE;
1292 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1293 "multisample_type %#x, multisample_quality %#x.\n",
1294 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1295 multisample_type, multisample_quality);
1297 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1299 if (buffer_count && buffer_count != desc->backbuffer_count)
1300 FIXME("Cannot change the back buffer count yet.\n");
1302 wined3d_cs_finish(swapchain->device->cs, WINED3D_CS_QUEUE_DEFAULT);
1304 if (!width || !height)
1306 /* The application is requesting that either the swapchain width or
1307 * height be set to the corresponding dimension in the window's
1308 * client rect. */
1310 RECT client_rect;
1312 if (!desc->windowed)
1313 return WINED3DERR_INVALIDCALL;
1315 if (!GetClientRect(swapchain->state.device_window, &client_rect))
1317 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1318 return WINED3DERR_INVALIDCALL;
1321 if (!width)
1322 width = client_rect.right;
1324 if (!height)
1325 height = client_rect.bottom;
1328 if (width != desc->backbuffer_width || height != desc->backbuffer_height)
1330 desc->backbuffer_width = width;
1331 desc->backbuffer_height = height;
1332 update_desc = TRUE;
1335 if (format_id == WINED3DFMT_UNKNOWN)
1337 if (!desc->windowed)
1338 return WINED3DERR_INVALIDCALL;
1339 format_id = swapchain->state.original_mode.format_id;
1342 if (format_id != desc->backbuffer_format)
1344 desc->backbuffer_format = format_id;
1345 update_desc = TRUE;
1348 if (multisample_type != desc->multisample_type
1349 || multisample_quality != desc->multisample_quality)
1351 desc->multisample_type = multisample_type;
1352 desc->multisample_quality = multisample_quality;
1353 update_desc = TRUE;
1356 if (update_desc)
1358 HRESULT hr;
1359 UINT i;
1361 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, desc->backbuffer_width,
1362 desc->backbuffer_height, desc->backbuffer_format,
1363 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1364 return hr;
1366 for (i = 0; i < desc->backbuffer_count; ++i)
1368 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], desc->backbuffer_width,
1369 desc->backbuffer_height, desc->backbuffer_format,
1370 desc->multisample_type, desc->multisample_quality, NULL, 0)))
1371 return hr;
1375 swapchain_update_render_to_fbo(swapchain);
1376 swapchain_update_draw_bindings(swapchain);
1378 return WINED3D_OK;
1381 static HRESULT wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state *state,
1382 struct wined3d *wined3d, unsigned int adapter_idx, struct wined3d_display_mode *mode)
1384 HRESULT hr;
1386 if (state->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1388 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(wined3d, adapter_idx, mode)))
1390 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1394 if (FAILED(hr = wined3d_set_adapter_display_mode(wined3d, adapter_idx, mode)))
1396 WARN("Failed to set display mode, hr %#x.\n", hr);
1397 return WINED3DERR_INVALIDCALL;
1400 return WINED3D_OK;
1403 HRESULT CDECL wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state *state,
1404 struct wined3d *wined3d, unsigned int adapter_idx, const struct wined3d_display_mode *mode)
1406 struct wined3d_display_mode actual_mode;
1407 RECT original_window_rect, window_rect;
1408 HWND window;
1409 HRESULT hr;
1411 TRACE("state %p, wined3d %p, adapter_idx %u, mode %p.\n", state, wined3d, adapter_idx, mode);
1413 wined3d_mutex_lock();
1415 window = state->device_window;
1417 if (state->desc.windowed)
1419 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1420 AdjustWindowRectEx(&window_rect,
1421 GetWindowLongW(window, GWL_STYLE), FALSE,
1422 GetWindowLongW(window, GWL_EXSTYLE));
1423 SetRect(&window_rect, 0, 0,
1424 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1425 GetWindowRect(window, &original_window_rect);
1426 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1428 else if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1430 actual_mode = *mode;
1431 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, wined3d, adapter_idx, &actual_mode)))
1433 wined3d_mutex_unlock();
1434 return hr;
1436 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1438 else
1440 if (FAILED(hr = wined3d_get_adapter_display_mode(wined3d, adapter_idx, &actual_mode, NULL)))
1442 ERR("Failed to get display mode, hr %#x.\n", hr);
1443 wined3d_mutex_unlock();
1444 return hr;
1447 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1450 wined3d_mutex_unlock();
1452 MoveWindow(window, window_rect.left, window_rect.top,
1453 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, TRUE);
1455 return WINED3D_OK;
1458 static LONG fullscreen_style(LONG style)
1460 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1461 style |= WS_POPUP | WS_SYSMENU;
1462 style &= ~(WS_CAPTION | WS_THICKFRAME);
1464 return style;
1467 static LONG fullscreen_exstyle(LONG exstyle)
1469 /* Filter out window decorations. */
1470 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1472 return exstyle;
1475 HRESULT wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state *state,
1476 HWND window, unsigned int w, unsigned int h)
1478 LONG style, exstyle;
1479 BOOL filter;
1481 TRACE("Setting up window %p for fullscreen mode.\n", window);
1483 if (!IsWindow(window))
1485 WARN("%p is not a valid window.\n", window);
1486 return WINED3DERR_NOTAVAILABLE;
1489 if (state->style || state->exstyle)
1491 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1492 window, state->style, state->exstyle);
1495 state->style = GetWindowLongW(window, GWL_STYLE);
1496 state->exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1498 style = fullscreen_style(state->style);
1499 exstyle = fullscreen_exstyle(state->exstyle);
1501 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1502 state->style, state->exstyle, style, exstyle);
1504 filter = wined3d_filter_messages(window, TRUE);
1506 SetWindowLongW(window, GWL_STYLE, style);
1507 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1508 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1510 wined3d_filter_messages(window, filter);
1512 return WINED3D_OK;
1515 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state *state,
1516 HWND window, const RECT *window_rect)
1518 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
1519 LONG style, exstyle;
1520 RECT rect = {0};
1521 BOOL filter;
1523 if (!state->style && !state->exstyle)
1524 return;
1526 style = GetWindowLongW(window, GWL_STYLE);
1527 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1529 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
1530 * application, and we want to ignore them in the test below, since it's
1531 * not the application's fault that they changed. Additionally, we want to
1532 * preserve the current status of these flags (i.e. don't restore them) to
1533 * more closely emulate the behavior of Direct3D, which leaves these flags
1534 * alone when returning to windowed mode. */
1535 state->style ^= (state->style ^ style) & WS_VISIBLE;
1536 state->exstyle ^= (state->exstyle ^ exstyle) & WS_EX_TOPMOST;
1538 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1539 window, state->style, state->exstyle);
1541 filter = wined3d_filter_messages(window, TRUE);
1543 /* Only restore the style if the application didn't modify it during the
1544 * fullscreen phase. Some applications change it before calling Reset()
1545 * when switching between windowed and fullscreen modes (HL2), some
1546 * depend on the original style (Eve Online). */
1547 if (style == fullscreen_style(state->style) && exstyle == fullscreen_exstyle(state->exstyle))
1549 SetWindowLongW(window, GWL_STYLE, state->style);
1550 SetWindowLongW(window, GWL_EXSTYLE, state->exstyle);
1553 if (window_rect)
1554 rect = *window_rect;
1555 else
1556 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
1557 SetWindowPos(window, 0, rect.left, rect.top,
1558 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
1560 wined3d_filter_messages(window, filter);
1562 /* Delete the old values. */
1563 state->style = 0;
1564 state->exstyle = 0;
1567 HRESULT CDECL wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state *state,
1568 const struct wined3d_swapchain_desc *swapchain_desc, struct wined3d *wined3d,
1569 unsigned int adapter_idx, const struct wined3d_display_mode *mode)
1571 struct wined3d_display_mode actual_mode;
1572 HRESULT hr;
1574 TRACE("state %p, swapchain_desc %p, wined3d %p, adapter_idx %u, mode %p.\n",
1575 state, swapchain_desc, wined3d, adapter_idx, mode);
1577 if (state->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1579 if (mode)
1581 actual_mode = *mode;
1583 else
1585 if (!swapchain_desc->windowed)
1587 const struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
1589 actual_mode.width = swapchain_desc->backbuffer_width;
1590 actual_mode.height = swapchain_desc->backbuffer_height;
1591 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1592 actual_mode.format_id = adapter_format_from_backbuffer_format(adapter,
1593 swapchain_desc->backbuffer_format);
1594 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1596 else
1598 actual_mode = state->original_mode;
1602 if (FAILED(hr = wined3d_swapchain_state_set_display_mode(state, wined3d, adapter_idx, &actual_mode)))
1603 return hr;
1605 else
1607 if (mode)
1608 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1610 if (FAILED(hr = wined3d_get_adapter_display_mode(wined3d, adapter_idx, &actual_mode, NULL)))
1612 ERR("Failed to get display mode, hr %#x.\n", hr);
1613 return WINED3DERR_INVALIDCALL;
1617 if (!swapchain_desc->windowed)
1619 unsigned int width = actual_mode.width;
1620 unsigned int height = actual_mode.height;
1622 if (state->desc.windowed)
1624 /* Switch from windowed to fullscreen */
1625 if (FAILED(hr = wined3d_swapchain_state_setup_fullscreen(state, state->device_window, width, height)))
1626 return hr;
1628 else
1630 HWND window = state->device_window;
1631 BOOL filter;
1633 /* Fullscreen -> fullscreen mode change */
1634 filter = wined3d_filter_messages(window, TRUE);
1635 MoveWindow(window, 0, 0, width, height, TRUE);
1636 ShowWindow(window, SW_SHOW);
1637 wined3d_filter_messages(window, filter);
1639 state->d3d_mode = actual_mode;
1641 else if (!state->desc.windowed)
1643 /* Fullscreen -> windowed switch */
1644 RECT *window_rect = NULL;
1645 if (state->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1646 window_rect = &state->original_window_rect;
1647 wined3d_swapchain_state_restore_from_fullscreen(state, state->device_window, window_rect);
1650 state->desc.windowed = swapchain_desc->windowed;
1652 return WINED3D_OK;
1655 void CDECL wined3d_swapchain_state_destroy(struct wined3d_swapchain_state *state)
1657 heap_free(state);
1660 HRESULT CDECL wined3d_swapchain_state_create(const struct wined3d_swapchain_desc *desc,
1661 HWND window, struct wined3d *wined3d, unsigned int adapter_idx, struct wined3d_swapchain_state **state)
1663 struct wined3d_swapchain_state *s;
1664 HRESULT hr;
1666 TRACE("desc %p, window %p, wined3d %p, adapter_idx %u, state %p.\n",
1667 desc, window, wined3d, adapter_idx, state);
1669 TRACE("desc %p, window %p, state %p.\n", desc, window, state);
1671 if (!(s = heap_alloc_zero(sizeof(*s))))
1672 return E_OUTOFMEMORY;
1674 if (FAILED(hr = wined3d_swapchain_state_init(s, desc, window, wined3d, adapter_idx)))
1676 heap_free(s);
1677 return hr;
1680 *state = s;
1682 return hr;