comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / wined3d / swapchain.c
blobf982add3fc32112143017fd0eee51b83a00c3ea6
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 swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 HRESULT hr;
33 UINT i;
35 TRACE("Destroying swapchain %p.\n", swapchain);
37 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain->front_buffer)
43 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_texture_decref(swapchain->front_buffer))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
46 swapchain->front_buffer = NULL;
49 if (swapchain->back_buffers)
51 i = swapchain->desc.backbuffer_count;
53 while (i--)
55 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_texture_decref(swapchain->back_buffers[i]))
57 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
59 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
60 swapchain->back_buffers = NULL;
63 for (i = 0; i < swapchain->num_contexts; ++i)
65 context_destroy(swapchain->device, swapchain->context[i]);
67 HeapFree(GetProcessHeap(), 0, swapchain->context);
69 /* Restore the screen resolution if we rendered in fullscreen.
70 * This will restore the screen resolution to what it was before creating
71 * the swapchain. In case of d3d8 and d3d9 this will be the original
72 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
73 * sets the resolution before starting up Direct3D, thus orig_width and
74 * orig_height will be equal to the modes in the presentation params. */
75 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
77 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
78 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
79 ERR("Failed to restore display mode, hr %#x.\n", hr);
82 if (swapchain->backup_dc)
84 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
86 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
87 DestroyWindow(swapchain->backup_wnd);
91 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
93 ULONG refcount = InterlockedIncrement(&swapchain->ref);
95 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
97 return refcount;
100 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedDecrement(&swapchain->ref);
104 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
106 if (!refcount)
108 swapchain_cleanup(swapchain);
109 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
110 HeapFree(GetProcessHeap(), 0, swapchain);
113 return refcount;
116 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
118 TRACE("swapchain %p.\n", swapchain);
120 return swapchain->parent;
123 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
125 if (!window)
126 window = swapchain->device_window;
127 if (window == swapchain->win_handle)
128 return;
130 TRACE("Setting swapchain %p window from %p to %p.\n",
131 swapchain, swapchain->win_handle, window);
132 swapchain->win_handle = window;
135 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
136 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, DWORD flags)
138 RECT s, d;
140 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
141 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
142 dst_window_override, flags);
144 if (flags)
145 FIXME("Ignoring flags %#x.\n", flags);
147 if (!swapchain->back_buffers)
149 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
150 return WINED3DERR_INVALIDCALL;
153 if (!src_rect)
155 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
156 swapchain->desc.backbuffer_height);
157 src_rect = &s;
160 if (!dst_rect)
162 GetClientRect(swapchain->win_handle, &d);
163 dst_rect = &d;
166 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
167 dst_rect, dst_window_override, flags);
169 return WINED3D_OK;
172 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
173 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
175 RECT src_rect, dst_rect;
177 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
179 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
180 dst_rect = src_rect;
182 if (swapchain->desc.windowed)
184 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
185 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
186 wine_dbgstr_rect(&dst_rect));
189 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
190 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
193 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
194 UINT back_buffer_idx)
196 TRACE("swapchain %p, back_buffer_idx %u.\n",
197 swapchain, back_buffer_idx);
199 /* Return invalid if there is no backbuffer array, otherwise it will
200 * crash when ddraw is used (there swapchain->back_buffers is always
201 * NULL). We need this because this function is called from
202 * stateblock_init_default_state() to get the default scissorrect
203 * dimensions. */
204 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
206 WARN("Invalid back buffer index.\n");
207 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
208 * here in wined3d to avoid problems in other libs. */
209 return NULL;
212 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
214 return swapchain->back_buffers[back_buffer_idx];
217 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
218 struct wined3d_raster_status *raster_status)
220 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
222 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
223 swapchain->device->adapter->ordinal, raster_status);
226 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
227 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
229 HRESULT hr;
231 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
233 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
234 swapchain->device->adapter->ordinal, mode, rotation);
236 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
237 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
239 return hr;
242 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
244 TRACE("swapchain %p.\n", swapchain);
246 return swapchain->device;
249 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
250 struct wined3d_swapchain_desc *desc)
252 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
254 *desc = swapchain->desc;
257 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
258 DWORD flags, const struct wined3d_gamma_ramp *ramp)
260 HDC dc;
262 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
264 if (flags)
265 FIXME("Ignoring flags %#x.\n", flags);
267 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
268 SetDeviceGammaRamp(dc, (void *)ramp);
269 ReleaseDC(swapchain->device_window, dc);
271 return WINED3D_OK;
274 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
276 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
277 swapchain->palette = palette;
280 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
281 struct wined3d_gamma_ramp *ramp)
283 HDC dc;
285 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
287 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
288 GetDeviceGammaRamp(dc, ramp);
289 ReleaseDC(swapchain->device_window, dc);
291 return WINED3D_OK;
294 /* A GL context is provided by the caller */
295 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
296 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
298 struct wined3d_texture *texture = swapchain->back_buffers[0];
299 struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
300 struct wined3d_surface *front_buffer = swapchain->front_buffer->sub_resources[0].u.surface;
301 UINT src_w = src_rect->right - src_rect->left;
302 UINT src_h = src_rect->bottom - src_rect->top;
303 GLenum gl_filter;
304 const struct wined3d_gl_info *gl_info = context->gl_info;
305 RECT win_rect;
306 UINT win_h;
308 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
309 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
311 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
312 gl_filter = GL_NEAREST;
313 else
314 gl_filter = GL_LINEAR;
316 GetClientRect(swapchain->win_handle, &win_rect);
317 win_h = win_rect.bottom - win_rect.top;
319 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(texture->resource.format->color_fixup))
321 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
323 if (texture->resource.multisample_type)
325 location = WINED3D_LOCATION_RB_RESOLVED;
326 surface_load_location(back_buffer, context, location);
329 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, back_buffer, NULL, location);
330 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
331 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
333 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
334 context_set_draw_buffer(context, GL_BACK);
335 context_invalidate_state(context, STATE_FRAMEBUFFER);
337 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
338 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
339 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
340 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
341 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
343 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
344 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
346 /* Note that the texture is upside down */
347 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
348 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
349 GL_COLOR_BUFFER_BIT, gl_filter);
350 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
352 else
354 struct wined3d_device *device = swapchain->device;
355 struct wined3d_context *context2;
356 float tex_left = src_rect->left;
357 float tex_top = src_rect->top;
358 float tex_right = src_rect->right;
359 float tex_bottom = src_rect->bottom;
361 context2 = context_acquire(device, back_buffer);
362 context_apply_blit_state(context2, device);
364 if (texture->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
366 tex_left /= texture->pow2_width;
367 tex_right /= texture->pow2_width;
368 tex_top /= texture->pow2_height;
369 tex_bottom /= texture->pow2_height;
372 if (is_complex_fixup(texture->resource.format->color_fixup))
373 gl_filter = GL_NEAREST;
375 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
376 context_bind_texture(context2, back_buffer->texture_target, texture->texture_rgb.name);
378 /* Set up the texture. The surface is not in a wined3d_texture
379 * container, so there are no D3D texture settings to dirtify. */
380 device->blitter->set_shader(device->blit_priv, context2, back_buffer, NULL);
381 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
382 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
384 context_set_draw_buffer(context, GL_BACK);
386 /* Set the viewport to the destination rectandle, disable any projection
387 * transformation set up by context_apply_blit_state(), and draw a
388 * (-1,-1)-(1,1) quad.
390 * Back up viewport and matrix to avoid breaking last_was_blit
392 * Note that context_apply_blit_state() set up viewport and ortho to
393 * match the surface size - we want the GL drawable(=window) size. */
394 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
395 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
396 dst_rect->right, win_h - dst_rect->top);
397 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
398 gl_info->gl_ops.gl.p_glPushMatrix();
399 gl_info->gl_ops.gl.p_glLoadIdentity();
401 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
402 /* bottom left */
403 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
404 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
406 /* top left */
407 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
408 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
410 /* top right */
411 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
412 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
414 /* bottom right */
415 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
416 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
417 gl_info->gl_ops.gl.p_glEnd();
419 gl_info->gl_ops.gl.p_glPopMatrix();
420 gl_info->gl_ops.gl.p_glPopAttrib();
422 device->blitter->unset_shader(context->gl_info);
423 checkGLcall("Swapchain present blit(manual)\n");
425 context_release(context2);
429 /* Context activation is done by the caller. */
430 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
432 struct wined3d_texture_sub_resource *sub_resource;
433 struct wined3d_texture *texture, *texture_prev;
434 struct gl_texture tex0;
435 GLuint rb0;
436 DWORD locations0;
437 unsigned int i;
438 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
440 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
441 return;
443 texture_prev = swapchain->back_buffers[0];
445 /* Back buffer 0 is already in the draw binding. */
446 tex0 = texture_prev->texture_rgb;
447 rb0 = texture_prev->rb_multisample;
448 locations0 = texture_prev->sub_resources[0].locations;
450 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
452 texture = swapchain->back_buffers[i];
453 sub_resource = &texture->sub_resources[0];
455 if (!(sub_resource->locations & supported_locations))
456 surface_load_location(sub_resource->u.surface, context, texture->resource.draw_binding);
458 texture_prev->texture_rgb = texture->texture_rgb;
459 texture_prev->rb_multisample = texture->rb_multisample;
461 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
462 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
464 texture_prev = texture;
467 texture_prev->texture_rgb = tex0;
468 texture_prev->rb_multisample = rb0;
470 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
471 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
473 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
476 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
477 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
479 struct wined3d_surface *back_buffer = swapchain->back_buffers[0]->sub_resources[0].u.surface;
480 const struct wined3d_fb_state *fb = &swapchain->device->fb;
481 const struct wined3d_gl_info *gl_info;
482 struct wined3d_texture *logo_texture;
483 struct wined3d_context *context;
484 BOOL render_to_fbo;
486 context = context_acquire(swapchain->device, back_buffer);
487 if (!context->valid)
489 context_release(context);
490 WARN("Invalid context, skipping present.\n");
491 return;
494 gl_info = context->gl_info;
496 if ((logo_texture = swapchain->device->logo_texture))
498 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
500 /* Blit the logo into the upper left corner of the drawable. */
501 wined3d_texture_blt(swapchain->back_buffers[0], 0, &rect, logo_texture, 0, &rect,
502 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
505 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
506 && !swapchain->device->hardwareCursor)
508 RECT dst_rect =
510 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
511 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
512 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
513 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
515 RECT src_rect =
517 0, 0,
518 swapchain->device->cursor_texture->resource.width,
519 swapchain->device->cursor_texture->resource.height
521 const RECT clip_rect = {0, 0,
522 swapchain->back_buffers[0]->resource.width,
523 swapchain->back_buffers[0]->resource.height};
525 TRACE("Rendering the software cursor.\n");
527 if (swapchain->desc.windowed)
528 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
529 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
530 wined3d_texture_blt(swapchain->back_buffers[0], 0, &dst_rect,
531 swapchain->device->cursor_texture, 0, &src_rect,
532 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
535 TRACE("Presenting HDC %p.\n", context->hdc);
537 if (!(render_to_fbo = swapchain->render_to_fbo)
538 && (src_rect->left || src_rect->top
539 || src_rect->right != swapchain->desc.backbuffer_width
540 || src_rect->bottom != swapchain->desc.backbuffer_height
541 || dst_rect->left || dst_rect->top
542 || dst_rect->right != swapchain->desc.backbuffer_width
543 || dst_rect->bottom != swapchain->desc.backbuffer_height))
544 render_to_fbo = TRUE;
546 /* Rendering to a window of different size, presenting partial rectangles,
547 * or rendering to a different window needs help from FBO_blit or a textured
548 * draw. Render the swapchain to a FBO in the future.
550 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
551 * all these issues - this fails if the window is smaller than the backbuffer.
553 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
555 surface_load_location(back_buffer, context, WINED3D_LOCATION_TEXTURE_RGB);
556 wined3d_texture_invalidate_location(back_buffer->container, 0, WINED3D_LOCATION_DRAWABLE);
557 swapchain->render_to_fbo = TRUE;
558 swapchain_update_draw_bindings(swapchain);
560 else
562 surface_load_location(back_buffer, context, back_buffer->container->resource.draw_binding);
565 if (swapchain->render_to_fbo)
567 static unsigned int once;
569 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
570 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
572 swapchain_blit(swapchain, context, src_rect, dst_rect);
575 if (swapchain->num_contexts > 1)
576 gl_info->gl_ops.gl.p_glFinish();
578 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
579 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
581 wined3d_swapchain_rotate(swapchain, context);
583 TRACE("SwapBuffers called, Starting new frame\n");
584 /* FPS support */
585 if (TRACE_ON(fps))
587 DWORD time = GetTickCount();
588 ++swapchain->frames;
590 /* every 1.5 seconds */
591 if (time - swapchain->prev_time > 1500)
593 TRACE_(fps)("%p @ approx %.2ffps\n",
594 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
595 swapchain->prev_time = time;
596 swapchain->frames = 0;
600 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
601 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
602 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
603 * and INTEXTURE copies can keep their old content if they have any defined content.
604 * If the swapeffect is COPY, the content remains the same.
606 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
607 * up to date and hope WGL flipped front and back buffers and read this data into
608 * the FBO. Don't bother about this for now. */
610 if (fb->depth_stencil)
612 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
614 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
615 || ds->container->flags & WINED3D_TEXTURE_DISCARD))
617 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
618 fb->depth_stencil->width, fb->depth_stencil->height);
619 if (ds == swapchain->device->onscreen_depth_stencil)
621 wined3d_texture_decref(swapchain->device->onscreen_depth_stencil->container);
622 swapchain->device->onscreen_depth_stencil = NULL;
627 context_release(context);
630 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
632 struct wined3d_surface *surface;
633 struct wined3d_context *context;
635 surface = swapchain->front_buffer->sub_resources[0].u.surface;
636 context = context_acquire(swapchain->device, surface);
637 surface_load_location(surface, context, surface->container->resource.draw_binding);
638 context_release(context);
639 SetRectEmpty(&swapchain->front_buffer_update);
642 static const struct wined3d_swapchain_ops swapchain_gl_ops =
644 swapchain_gl_present,
645 swapchain_gl_frontbuffer_updated,
648 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
650 struct wined3d_surface *front;
651 POINT offset = {0, 0};
652 HDC src_dc, dst_dc;
653 RECT draw_rect;
654 HWND window;
656 TRACE("swapchain %p.\n", swapchain);
658 front = swapchain->front_buffer->sub_resources[0].u.surface;
659 if (swapchain->palette)
660 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
662 if (front->container->resource.map_count)
663 ERR("Trying to blit a mapped surface.\n");
665 TRACE("Copying surface %p to screen.\n", front);
667 src_dc = front->dc;
668 window = swapchain->win_handle;
669 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
671 /* Front buffer coordinates are screen coordinates. Map them to the
672 * destination window if not fullscreened. */
673 if (swapchain->desc.windowed)
674 ClientToScreen(window, &offset);
676 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
678 draw_rect.left = 0;
679 draw_rect.right = swapchain->front_buffer->resource.width;
680 draw_rect.top = 0;
681 draw_rect.bottom = swapchain->front_buffer->resource.height;
682 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
684 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
685 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
686 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
687 ReleaseDC(window, dst_dc);
689 SetRectEmpty(&swapchain->front_buffer_update);
692 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
693 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
695 struct wined3d_surface *front, *back;
696 HBITMAP bitmap;
697 void *data;
698 HDC dc;
700 front = swapchain->front_buffer->sub_resources[0].u.surface;
701 back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
703 /* Flip the surface data. */
704 dc = front->dc;
705 bitmap = front->bitmap;
706 data = front->container->resource.heap_memory;
708 front->dc = back->dc;
709 front->bitmap = back->bitmap;
710 front->container->resource.heap_memory = back->container->resource.heap_memory;
712 back->dc = dc;
713 back->bitmap = bitmap;
714 back->container->resource.heap_memory = data;
716 /* FPS support */
717 if (TRACE_ON(fps))
719 static LONG prev_time, frames;
720 DWORD time = GetTickCount();
722 ++frames;
724 /* every 1.5 seconds */
725 if (time - prev_time > 1500)
727 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
728 prev_time = time;
729 frames = 0;
733 SetRect(&swapchain->front_buffer_update, 0, 0,
734 swapchain->front_buffer->resource.width,
735 swapchain->front_buffer->resource.height);
736 swapchain_gdi_frontbuffer_updated(swapchain);
739 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
741 swapchain_gdi_present,
742 swapchain_gdi_frontbuffer_updated,
745 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
747 RECT client_rect;
749 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
750 return;
752 if (!swapchain->desc.backbuffer_count)
754 TRACE("Single buffered rendering.\n");
755 swapchain->render_to_fbo = FALSE;
756 return;
759 GetClientRect(swapchain->win_handle, &client_rect);
761 TRACE("Backbuffer %ux%u, window %ux%u.\n",
762 swapchain->desc.backbuffer_width,
763 swapchain->desc.backbuffer_height,
764 client_rect.right, client_rect.bottom);
765 TRACE("Multisample type %#x, quality %#x.\n",
766 swapchain->desc.multisample_type,
767 swapchain->desc.multisample_quality);
769 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
770 && swapchain->desc.backbuffer_width == client_rect.right
771 && swapchain->desc.backbuffer_height == client_rect.bottom)
773 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
774 swapchain->render_to_fbo = FALSE;
775 return;
778 TRACE("Rendering to FBO.\n");
779 swapchain->render_to_fbo = TRUE;
782 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
783 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
785 const struct wined3d_gl_info *gl_info;
786 const struct wined3d_format *format;
787 enum wined3d_multisample_type t;
789 if (wined3d_settings.sample_count == ~0u)
790 return;
792 gl_info = &swapchain->device->adapter->gl_info;
793 if (!(format = wined3d_get_format(gl_info, format_id)))
794 return;
796 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
797 while (!(format->multisample_types & 1u << (t - 1)))
798 ++t;
799 TRACE("Using sample count %u.\n", t);
800 *type = t;
801 *quality = 0;
804 static HRESULT 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)
807 const struct wined3d_adapter *adapter = device->adapter;
808 struct wined3d_resource_desc texture_desc;
809 BOOL displaymode_set = FALSE;
810 RECT client_rect;
811 HWND window;
812 HRESULT hr;
813 UINT i;
815 if (desc->backbuffer_count > 1)
817 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
818 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
821 if (device->wined3d->flags & WINED3D_NO3D)
822 swapchain->swapchain_ops = &swapchain_gdi_ops;
823 else
824 swapchain->swapchain_ops = &swapchain_gl_ops;
826 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
828 swapchain->device = device;
829 swapchain->parent = parent;
830 swapchain->parent_ops = parent_ops;
831 swapchain->ref = 1;
832 swapchain->win_handle = window;
833 swapchain->device_window = window;
835 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
836 adapter->ordinal, &swapchain->original_mode, NULL)))
838 ERR("Failed to get current display mode, hr %#x.\n", hr);
839 goto err;
842 GetClientRect(window, &client_rect);
843 if (desc->windowed
844 && (!desc->backbuffer_width || !desc->backbuffer_height
845 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
848 if (!desc->backbuffer_width)
850 desc->backbuffer_width = client_rect.right;
851 TRACE("Updating width to %u.\n", desc->backbuffer_width);
854 if (!desc->backbuffer_height)
856 desc->backbuffer_height = client_rect.bottom;
857 TRACE("Updating height to %u.\n", desc->backbuffer_height);
860 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
862 desc->backbuffer_format = swapchain->original_mode.format_id;
863 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
866 swapchain->desc = *desc;
867 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
868 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
869 swapchain_update_render_to_fbo(swapchain);
871 TRACE("Creating front buffer.\n");
873 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
874 texture_desc.format = swapchain->desc.backbuffer_format;
875 texture_desc.multisample_type = swapchain->desc.multisample_type;
876 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
877 texture_desc.usage = 0;
878 texture_desc.pool = WINED3D_POOL_DEFAULT;
879 texture_desc.width = swapchain->desc.backbuffer_width;
880 texture_desc.height = swapchain->desc.backbuffer_height;
881 texture_desc.depth = 1;
882 texture_desc.size = 0;
884 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
885 parent, &texture_desc, &swapchain->front_buffer)))
887 WARN("Failed to create front buffer, hr %#x.\n", hr);
888 goto err;
891 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
892 if (!(device->wined3d->flags & WINED3D_NO3D))
894 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
895 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
898 /* MSDN says we're only allowed a single fullscreen swapchain per device,
899 * so we should really check to see if there is a fullscreen swapchain
900 * already. Does a single head count as full screen? */
902 if (!desc->windowed)
904 /* Change the display settings */
905 swapchain->d3d_mode.width = desc->backbuffer_width;
906 swapchain->d3d_mode.height = desc->backbuffer_height;
907 swapchain->d3d_mode.format_id = desc->backbuffer_format;
908 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
909 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
911 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
913 WARN("Failed to set display mode, hr %#x.\n", hr);
914 goto err;
916 displaymode_set = TRUE;
919 if (!(device->wined3d->flags & WINED3D_NO3D))
921 static const enum wined3d_format_id formats[] =
923 WINED3DFMT_D24_UNORM_S8_UINT,
924 WINED3DFMT_D32_UNORM,
925 WINED3DFMT_R24_UNORM_X8_TYPELESS,
926 WINED3DFMT_D16_UNORM,
927 WINED3DFMT_S1_UINT_D15_UNORM
930 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
932 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
933 if (!swapchain->context)
935 ERR("Failed to create the context array.\n");
936 hr = E_OUTOFMEMORY;
937 goto err;
939 swapchain->num_contexts = 1;
941 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
942 * You are able to add a depth + stencil surface at a later stage when you need it.
943 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
944 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
945 * context, need torecreate shaders, textures and other resources.
947 * The context manager already takes care of the state problem and for the other tasks code from Reset
948 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
949 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
950 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
951 * issue needs to be fixed. */
952 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
954 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
955 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
956 if (swapchain->context[0]) break;
957 TRACE("Depth stencil format %s is not supported, trying next format\n",
958 debug_d3dformat(formats[i]));
961 if (!swapchain->context[0])
963 WARN("Failed to create context.\n");
964 hr = WINED3DERR_NOTAVAILABLE;
965 goto err;
968 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
969 && (!desc->enable_auto_depth_stencil
970 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
972 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
974 context_release(swapchain->context[0]);
975 swapchain_update_swap_interval(swapchain);
978 if (swapchain->desc.backbuffer_count > 0)
980 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
981 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
982 if (!swapchain->back_buffers)
984 ERR("Failed to allocate backbuffer array memory.\n");
985 hr = E_OUTOFMEMORY;
986 goto err;
989 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
990 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
992 TRACE("Creating back buffer %u.\n", i);
993 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
994 parent, &texture_desc, &swapchain->back_buffers[i])))
996 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
997 swapchain->desc.backbuffer_count = i;
998 goto err;
1000 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1004 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1005 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1007 TRACE("Creating depth/stencil buffer.\n");
1008 if (!device->auto_depth_stencil_view)
1010 struct wined3d_texture *ds;
1011 struct wined3d_rendertarget_view_desc desc;
1013 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
1014 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1016 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1017 device->device_parent, &texture_desc, &ds)))
1019 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1020 goto err;
1023 desc.format_id = ds->resource.format->id;
1024 desc.u.texture.level_idx = 0;
1025 desc.u.texture.layer_idx = 0;
1026 desc.u.texture.layer_count = 1;
1027 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1028 &device->auto_depth_stencil_view);
1029 wined3d_texture_decref(ds);
1030 if (FAILED(hr))
1032 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1033 goto err;
1038 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1040 return WINED3D_OK;
1042 err:
1043 if (displaymode_set)
1045 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1046 adapter->ordinal, &swapchain->original_mode)))
1047 ERR("Failed to restore display mode.\n");
1048 ClipCursor(NULL);
1051 if (swapchain->back_buffers)
1053 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1055 if (swapchain->back_buffers[i])
1057 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1058 wined3d_texture_decref(swapchain->back_buffers[i]);
1061 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1064 if (swapchain->context)
1066 if (swapchain->context[0])
1068 context_release(swapchain->context[0]);
1069 context_destroy(device, swapchain->context[0]);
1070 swapchain->num_contexts = 0;
1072 HeapFree(GetProcessHeap(), 0, swapchain->context);
1075 if (swapchain->front_buffer)
1077 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1078 wined3d_texture_decref(swapchain->front_buffer);
1081 return hr;
1084 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1085 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1087 struct wined3d_swapchain *object;
1088 HRESULT hr;
1090 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1091 device, desc, parent, parent_ops, swapchain);
1093 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1094 if (!object)
1095 return E_OUTOFMEMORY;
1097 hr = swapchain_init(object, device, desc, parent, parent_ops);
1098 if (FAILED(hr))
1100 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1101 HeapFree(GetProcessHeap(), 0, object);
1102 return hr;
1105 TRACE("Created swapchain %p.\n", object);
1106 *swapchain = object;
1108 return WINED3D_OK;
1111 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1113 struct wined3d_context **newArray;
1114 struct wined3d_context *ctx;
1116 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1118 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1120 ERR("Failed to create a new context for the swapchain\n");
1121 return NULL;
1123 context_release(ctx);
1125 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1126 if(!newArray) {
1127 ERR("Out of memory when trying to allocate a new context array\n");
1128 context_destroy(swapchain->device, ctx);
1129 return NULL;
1131 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1132 HeapFree(GetProcessHeap(), 0, swapchain->context);
1133 newArray[swapchain->num_contexts] = ctx;
1134 swapchain->context = newArray;
1135 swapchain->num_contexts++;
1137 TRACE("Returning context %p\n", ctx);
1138 return ctx;
1141 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1143 unsigned int i;
1145 for (i = 0; i < swapchain->num_contexts; ++i)
1147 context_destroy(swapchain->device, swapchain->context[i]);
1149 swapchain->num_contexts = 0;
1152 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1154 DWORD tid = GetCurrentThreadId();
1155 unsigned int i;
1157 for (i = 0; i < swapchain->num_contexts; ++i)
1159 if (swapchain->context[i]->tid == tid)
1160 return swapchain->context[i];
1163 /* Create a new context for the thread */
1164 return swapchain_create_context(swapchain);
1167 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1169 if (!swapchain->backup_dc)
1171 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1173 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1174 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1176 ERR("Failed to create a window.\n");
1177 return NULL;
1180 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1182 ERR("Failed to get a DC.\n");
1183 DestroyWindow(swapchain->backup_wnd);
1184 swapchain->backup_wnd = NULL;
1185 return NULL;
1189 return swapchain->backup_dc;
1192 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1194 UINT i;
1196 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1198 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1200 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1204 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1206 const struct wined3d_gl_info *gl_info;
1207 struct wined3d_context *context;
1208 int swap_interval;
1210 context = context_acquire(swapchain->device, swapchain->front_buffer->sub_resources[0].u.surface);
1211 gl_info = context->gl_info;
1213 switch (swapchain->desc.swap_interval)
1215 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1216 swap_interval = 0;
1217 break;
1218 case WINED3DPRESENT_INTERVAL_DEFAULT:
1219 case WINED3DPRESENT_INTERVAL_ONE:
1220 swap_interval = 1;
1221 break;
1222 case WINED3DPRESENT_INTERVAL_TWO:
1223 swap_interval = 2;
1224 break;
1225 case WINED3DPRESENT_INTERVAL_THREE:
1226 swap_interval = 3;
1227 break;
1228 case WINED3DPRESENT_INTERVAL_FOUR:
1229 swap_interval = 4;
1230 break;
1231 default:
1232 FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
1233 swap_interval = 1;
1236 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1238 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1239 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1240 swap_interval, context, GetLastError());
1243 context_release(context);
1246 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1248 struct wined3d_device *device = swapchain->device;
1249 BOOL filter_messages = device->filter_messages;
1251 /* This code is not protected by the wined3d mutex, so it may run while
1252 * wined3d_device_reset is active. Testing on Windows shows that changing
1253 * focus during resets and resetting during focus change events causes
1254 * the application to crash with an invalid memory access. */
1256 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1258 if (activate)
1260 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1262 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1263 * the window but leaves the size untouched, d3d9 sets the size on an
1264 * invisible window, generates messages but doesn't change the window
1265 * properties. The implementation follows d3d9.
1267 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1268 * resume drawing after a focus loss. */
1269 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1270 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1271 SWP_NOACTIVATE | SWP_NOZORDER);
1274 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1276 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1277 device->adapter->ordinal, &swapchain->d3d_mode)))
1278 ERR("Failed to set display mode.\n");
1281 else
1283 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1284 device->adapter->ordinal, NULL)))
1285 ERR("Failed to set display mode.\n");
1287 swapchain->reapply_mode = TRUE;
1289 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1290 && IsWindowVisible(swapchain->device_window))
1291 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1294 device->filter_messages = filter_messages;
1297 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1298 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1299 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1301 BOOL update_desc = FALSE;
1303 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1304 "multisample_type %#x, multisample_quality %#x.\n",
1305 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1306 multisample_type, multisample_quality);
1308 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1310 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1311 FIXME("Cannot change the back buffer count yet.\n");
1313 if (!width || !height)
1315 /* The application is requesting that either the swapchain width or
1316 * height be set to the corresponding dimension in the window's
1317 * client rect. */
1319 RECT client_rect;
1321 if (!swapchain->desc.windowed)
1322 return WINED3DERR_INVALIDCALL;
1324 if (!GetClientRect(swapchain->device_window, &client_rect))
1326 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1327 return WINED3DERR_INVALIDCALL;
1330 if (!width)
1331 width = client_rect.right;
1333 if (!height)
1334 height = client_rect.bottom;
1337 if (width != swapchain->desc.backbuffer_width
1338 || height != swapchain->desc.backbuffer_height)
1340 swapchain->desc.backbuffer_width = width;
1341 swapchain->desc.backbuffer_height = height;
1342 update_desc = TRUE;
1345 if (format_id == WINED3DFMT_UNKNOWN)
1347 if (!swapchain->desc.windowed)
1348 return WINED3DERR_INVALIDCALL;
1349 format_id = swapchain->original_mode.format_id;
1352 if (format_id != swapchain->desc.backbuffer_format)
1354 swapchain->desc.backbuffer_format = format_id;
1355 update_desc = TRUE;
1358 if (multisample_type != swapchain->desc.multisample_type
1359 || multisample_quality != swapchain->desc.multisample_quality)
1361 swapchain->desc.multisample_type = multisample_type;
1362 swapchain->desc.multisample_quality = multisample_quality;
1363 update_desc = TRUE;
1366 if (update_desc)
1368 HRESULT hr;
1369 UINT i;
1371 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1372 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1373 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1374 return hr;
1376 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1378 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1379 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1380 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1381 return hr;
1385 swapchain_update_render_to_fbo(swapchain);
1386 swapchain_update_draw_bindings(swapchain);
1388 return WINED3D_OK;