d2d1: Implement d2d_d3d_render_target_GetTransform().
[wine.git] / dlls / wined3d / swapchain.c
blobbf0bbc67b903ad73f1de5687b74d6ca2cbc1f3da
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 surface_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_surface_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 surface_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_surface_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,
137 const RGNDATA *dirty_region, DWORD flags)
139 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
140 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
141 dst_window_override, dirty_region, flags);
143 if (flags)
144 FIXME("Ignoring flags %#x.\n", flags);
146 if (!swapchain->back_buffers)
148 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
149 return WINED3DERR_INVALIDCALL;
152 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
153 dst_rect, dst_window_override, dirty_region, flags);
155 return WINED3D_OK;
158 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
159 struct wined3d_surface *dst_surface)
161 struct wined3d_surface *src_surface;
162 RECT src_rect, dst_rect;
164 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
166 src_surface = swapchain->front_buffer;
167 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
168 dst_rect = src_rect;
170 if (swapchain->desc.windowed)
172 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
173 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
174 wine_dbgstr_rect(&dst_rect));
177 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
180 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
181 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
183 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
184 swapchain, back_buffer_idx, type);
186 /* Return invalid if there is no backbuffer array, otherwise it will
187 * crash when ddraw is used (there swapchain->back_buffers is always
188 * NULL). We need this because this function is called from
189 * stateblock_init_default_state() to get the default scissorrect
190 * dimensions. */
191 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
193 WARN("Invalid back buffer index.\n");
194 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
195 * here in wined3d to avoid problems in other libs. */
196 return NULL;
199 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
201 return swapchain->back_buffers[back_buffer_idx];
204 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
205 struct wined3d_raster_status *raster_status)
207 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
209 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
210 swapchain->device->adapter->ordinal, raster_status);
213 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
214 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
216 HRESULT hr;
218 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
220 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
221 swapchain->device->adapter->ordinal, mode, rotation);
223 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
224 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
226 return hr;
229 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
231 TRACE("swapchain %p.\n", swapchain);
233 return swapchain->device;
236 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
237 struct wined3d_swapchain_desc *desc)
239 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
241 *desc = swapchain->desc;
244 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
245 DWORD flags, const struct wined3d_gamma_ramp *ramp)
247 HDC dc;
249 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
251 if (flags)
252 FIXME("Ignoring flags %#x.\n", flags);
254 dc = GetDC(swapchain->device_window);
255 SetDeviceGammaRamp(dc, (void *)ramp);
256 ReleaseDC(swapchain->device_window, dc);
258 return WINED3D_OK;
261 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
263 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
264 swapchain->palette = palette;
267 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
268 struct wined3d_gamma_ramp *ramp)
270 HDC dc;
272 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
274 dc = GetDC(swapchain->device_window);
275 GetDeviceGammaRamp(dc, ramp);
276 ReleaseDC(swapchain->device_window, dc);
278 return WINED3D_OK;
281 /* A GL context is provided by the caller */
282 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
283 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
285 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
286 UINT src_w = src_rect->right - src_rect->left;
287 UINT src_h = src_rect->bottom - src_rect->top;
288 GLenum gl_filter;
289 const struct wined3d_gl_info *gl_info = context->gl_info;
290 RECT win_rect;
291 UINT win_h;
293 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
294 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
296 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
297 gl_filter = GL_NEAREST;
298 else
299 gl_filter = GL_LINEAR;
301 GetClientRect(swapchain->win_handle, &win_rect);
302 win_h = win_rect.bottom - win_rect.top;
304 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
306 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
308 if (backbuffer->resource.multisample_type)
310 location = WINED3D_LOCATION_RB_RESOLVED;
311 surface_load_location(backbuffer, location);
314 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
315 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
316 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
318 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer,
319 NULL, WINED3D_LOCATION_DRAWABLE);
320 context_set_draw_buffer(context, GL_BACK);
321 context_invalidate_state(context, STATE_FRAMEBUFFER);
323 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
324 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
325 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
327 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
329 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
330 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
332 /* Note that the texture is upside down */
333 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
334 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
335 GL_COLOR_BUFFER_BIT, gl_filter);
336 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
338 else
340 struct wined3d_device *device = swapchain->device;
341 struct wined3d_context *context2;
342 float tex_left = src_rect->left;
343 float tex_top = src_rect->top;
344 float tex_right = src_rect->right;
345 float tex_bottom = src_rect->bottom;
347 context2 = context_acquire(device, swapchain->back_buffers[0]);
348 context_apply_blit_state(context2, device);
350 if (backbuffer->flags & SFLAG_NORMCOORD)
352 tex_left /= src_w;
353 tex_right /= src_w;
354 tex_top /= src_h;
355 tex_bottom /= src_h;
358 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
359 gl_filter = GL_NEAREST;
361 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer,
362 NULL, WINED3D_LOCATION_DRAWABLE);
363 context_bind_texture(context2, backbuffer->texture_target, backbuffer->container->texture_rgb.name);
365 /* Set up the texture. The surface is not in a wined3d_texture
366 * container, so there are no D3D texture settings to dirtify. */
367 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
368 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
369 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
371 context_set_draw_buffer(context, GL_BACK);
373 /* Set the viewport to the destination rectandle, disable any projection
374 * transformation set up by context_apply_blit_state(), and draw a
375 * (-1,-1)-(1,1) quad.
377 * Back up viewport and matrix to avoid breaking last_was_blit
379 * Note that context_apply_blit_state() set up viewport and ortho to
380 * match the surface size - we want the GL drawable(=window) size. */
381 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
382 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
383 dst_rect->right, win_h - dst_rect->top);
384 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
385 gl_info->gl_ops.gl.p_glPushMatrix();
386 gl_info->gl_ops.gl.p_glLoadIdentity();
388 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
389 /* bottom left */
390 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
391 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
393 /* top left */
394 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
395 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
397 /* top right */
398 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
399 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
401 /* bottom right */
402 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
403 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
404 gl_info->gl_ops.gl.p_glEnd();
406 gl_info->gl_ops.gl.p_glPopMatrix();
407 gl_info->gl_ops.gl.p_glPopAttrib();
409 device->blitter->unset_shader(context->gl_info);
410 checkGLcall("Swapchain present blit(manual)\n");
412 context_release(context2);
416 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
417 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
419 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
420 const struct wined3d_fb_state *fb = &swapchain->device->fb;
421 const struct wined3d_gl_info *gl_info;
422 struct wined3d_context *context;
423 RECT src_rect, dst_rect;
424 BOOL render_to_fbo;
426 context = context_acquire(swapchain->device, back_buffer);
427 if (!context->valid)
429 context_release(context);
430 WARN("Invalid context, skipping present.\n");
431 return;
434 gl_info = context->gl_info;
436 if (swapchain->device->logo_texture)
438 struct wined3d_surface *src_surface = surface_from_resource(
439 wined3d_texture_get_sub_resource(swapchain->device->logo_texture, 0));
440 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
442 /* Blit the logo into the upper left corner of the drawable. */
443 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_ALPHATEST,
444 NULL, WINED3D_TEXF_POINT);
447 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
448 && !swapchain->device->hardwareCursor)
450 struct wined3d_surface *cursor = surface_from_resource(
451 wined3d_texture_get_sub_resource(swapchain->device->cursor_texture, 0));
452 RECT destRect =
454 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
455 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
456 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
457 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
460 TRACE("Rendering the software cursor.\n");
462 if (swapchain->desc.windowed)
463 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
464 wined3d_surface_blt(back_buffer, &destRect, cursor, NULL, WINEDDBLT_ALPHATEST,
465 NULL, WINED3D_TEXF_POINT);
468 TRACE("Presenting HDC %p.\n", context->hdc);
470 render_to_fbo = swapchain->render_to_fbo;
472 if (src_rect_in)
474 src_rect = *src_rect_in;
475 if (!render_to_fbo && (src_rect.left || src_rect.top
476 || src_rect.right != swapchain->desc.backbuffer_width
477 || src_rect.bottom != swapchain->desc.backbuffer_height))
479 render_to_fbo = TRUE;
482 else
484 src_rect.left = 0;
485 src_rect.top = 0;
486 src_rect.right = swapchain->desc.backbuffer_width;
487 src_rect.bottom = swapchain->desc.backbuffer_height;
490 if (dst_rect_in)
491 dst_rect = *dst_rect_in;
492 else
493 GetClientRect(swapchain->win_handle, &dst_rect);
495 if (!render_to_fbo && (dst_rect.left || dst_rect.top
496 || dst_rect.right != swapchain->desc.backbuffer_width
497 || dst_rect.bottom != swapchain->desc.backbuffer_height))
498 render_to_fbo = TRUE;
500 /* Rendering to a window of different size, presenting partial rectangles,
501 * or rendering to a different window needs help from FBO_blit or a textured
502 * draw. Render the swapchain to a FBO in the future.
504 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
505 * all these issues - this fails if the window is smaller than the backbuffer.
507 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
509 surface_load_location(back_buffer, WINED3D_LOCATION_TEXTURE_RGB);
510 surface_invalidate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
511 swapchain->render_to_fbo = TRUE;
512 swapchain_update_draw_bindings(swapchain);
514 else
516 surface_load_location(back_buffer, back_buffer->draw_binding);
519 if (swapchain->render_to_fbo)
521 static unsigned int once;
523 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
524 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
526 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
529 if (swapchain->num_contexts > 1)
530 gl_info->gl_ops.gl.p_glFinish();
532 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
533 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
535 TRACE("SwapBuffers called, Starting new frame\n");
536 /* FPS support */
537 if (TRACE_ON(fps))
539 DWORD time = GetTickCount();
540 ++swapchain->frames;
542 /* every 1.5 seconds */
543 if (time - swapchain->prev_time > 1500)
545 TRACE_(fps)("%p @ approx %.2ffps\n",
546 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
547 swapchain->prev_time = time;
548 swapchain->frames = 0;
552 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->locations & WINED3D_LOCATION_SYSMEM)
553 || (back_buffer->locations & WINED3D_LOCATION_SYSMEM)))
555 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
556 * Doesn't work with render_to_fbo because we're not flipping
558 struct wined3d_surface *front = swapchain->front_buffer;
560 if (front->resource.size == back_buffer->resource.size)
562 flip_surface(front, back_buffer);
564 /* Tell the front buffer surface that is has been modified. However,
565 * the other locations were preserved during that, so keep the flags.
566 * This serves to update the emulated overlay, if any. */
567 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
569 else
571 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
572 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
573 surface_validate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
574 surface_invalidate_location(back_buffer, ~WINED3D_LOCATION_DRAWABLE);
577 else
579 surface_validate_location(swapchain->front_buffer, WINED3D_LOCATION_DRAWABLE);
580 surface_invalidate_location(swapchain->front_buffer, ~WINED3D_LOCATION_DRAWABLE);
581 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
582 * and INTEXTURE copies can keep their old content if they have any defined content.
583 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
584 * the texture / sysmem copy needs to be reloaded from the drawable
586 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
588 surface_validate_location(back_buffer, back_buffer->draw_binding);
589 surface_invalidate_location(back_buffer, ~back_buffer->draw_binding);
593 if (fb->depth_stencil)
595 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
596 || fb->depth_stencil->flags & SFLAG_DISCARD)
598 surface_modify_ds_location(fb->depth_stencil, WINED3D_LOCATION_DISCARDED,
599 fb->depth_stencil->resource.width,
600 fb->depth_stencil->resource.height);
601 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
603 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
604 swapchain->device->onscreen_depth_stencil = NULL;
609 context_release(context);
612 static const struct wined3d_swapchain_ops swapchain_gl_ops =
614 swapchain_gl_present,
617 /* Helper function that blits the front buffer contents to the target window. */
618 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
620 struct wined3d_surface *front;
621 POINT offset = {0, 0};
622 HDC src_dc, dst_dc;
623 RECT draw_rect;
624 HWND window;
626 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
628 if (swapchain->palette)
629 wined3d_palette_apply_to_dc(swapchain->palette, swapchain->front_buffer->hDC);
631 front = swapchain->front_buffer;
632 if (front->resource.map_count)
633 ERR("Trying to blit a mapped surface.\n");
635 TRACE("Copying surface %p to screen.\n", front);
637 surface_load_location(front, WINED3D_LOCATION_DIB);
639 src_dc = front->hDC;
640 window = swapchain->win_handle;
641 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
643 /* Front buffer coordinates are screen coordinates. Map them to the
644 * destination window if not fullscreened. */
645 if (swapchain->desc.windowed)
646 ClientToScreen(window, &offset);
648 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
650 draw_rect.left = 0;
651 draw_rect.right = front->resource.width;
652 draw_rect.top = 0;
653 draw_rect.bottom = front->resource.height;
655 if (rect)
656 IntersectRect(&draw_rect, &draw_rect, rect);
658 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
659 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
660 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
661 ReleaseDC(window, dst_dc);
664 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
665 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
667 struct wined3d_surface *front, *back;
669 front = swapchain->front_buffer;
670 back = swapchain->back_buffers[0];
672 /* Flip the DC. */
674 HDC tmp;
675 tmp = front->hDC;
676 front->hDC = back->hDC;
677 back->hDC = tmp;
680 /* Flip the DIBsection. */
682 HBITMAP tmp;
683 tmp = front->dib.DIBsection;
684 front->dib.DIBsection = back->dib.DIBsection;
685 back->dib.DIBsection = tmp;
688 /* Flip the surface data. */
690 void *tmp;
692 tmp = front->dib.bitmap_data;
693 front->dib.bitmap_data = back->dib.bitmap_data;
694 back->dib.bitmap_data = tmp;
696 if (front->resource.heap_memory)
697 ERR("GDI Surface %p has heap memory allocated.\n", front);
699 if (back->resource.heap_memory)
700 ERR("GDI Surface %p has heap memory allocated.\n", back);
703 /* FPS support */
704 if (TRACE_ON(fps))
706 static LONG prev_time, frames;
707 DWORD time = GetTickCount();
709 ++frames;
711 /* every 1.5 seconds */
712 if (time - prev_time > 1500)
714 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
715 prev_time = time;
716 frames = 0;
720 x11_copy_to_screen(swapchain, NULL);
723 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
725 swapchain_gdi_present,
728 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
730 RECT client_rect;
732 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
733 return;
735 if (!swapchain->desc.backbuffer_count)
737 TRACE("Single buffered rendering.\n");
738 swapchain->render_to_fbo = FALSE;
739 return;
742 GetClientRect(swapchain->win_handle, &client_rect);
744 TRACE("Backbuffer %ux%u, window %ux%u.\n",
745 swapchain->desc.backbuffer_width,
746 swapchain->desc.backbuffer_height,
747 client_rect.right, client_rect.bottom);
748 TRACE("Multisample type %#x, quality %#x.\n",
749 swapchain->desc.multisample_type,
750 swapchain->desc.multisample_quality);
752 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
753 && swapchain->desc.backbuffer_width == client_rect.right
754 && swapchain->desc.backbuffer_height == client_rect.bottom)
756 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
757 swapchain->render_to_fbo = FALSE;
758 return;
761 TRACE("Rendering to FBO.\n");
762 swapchain->render_to_fbo = TRUE;
765 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
766 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
768 const struct wined3d_adapter *adapter = device->adapter;
769 struct wined3d_resource_desc surface_desc;
770 BOOL displaymode_set = FALSE;
771 RECT client_rect;
772 HWND window;
773 HRESULT hr;
774 UINT i;
776 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
778 FIXME("The application requested %u back buffers, this is not supported.\n",
779 desc->backbuffer_count);
780 return WINED3DERR_INVALIDCALL;
783 if (desc->backbuffer_count > 1)
785 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
786 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
789 if (device->wined3d->flags & WINED3D_NO3D)
790 swapchain->swapchain_ops = &swapchain_gdi_ops;
791 else
792 swapchain->swapchain_ops = &swapchain_gl_ops;
794 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
796 swapchain->device = device;
797 swapchain->parent = parent;
798 swapchain->parent_ops = parent_ops;
799 swapchain->ref = 1;
800 swapchain->win_handle = window;
801 swapchain->device_window = window;
803 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
804 adapter->ordinal, &swapchain->original_mode, NULL)))
806 ERR("Failed to get current display mode, hr %#x.\n", hr);
807 goto err;
810 GetClientRect(window, &client_rect);
811 if (desc->windowed
812 && (!desc->backbuffer_width || !desc->backbuffer_height
813 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
816 if (!desc->backbuffer_width)
818 desc->backbuffer_width = client_rect.right;
819 TRACE("Updating width to %u.\n", desc->backbuffer_width);
822 if (!desc->backbuffer_height)
824 desc->backbuffer_height = client_rect.bottom;
825 TRACE("Updating height to %u.\n", desc->backbuffer_height);
828 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
830 desc->backbuffer_format = swapchain->original_mode.format_id;
831 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
834 swapchain->desc = *desc;
835 swapchain_update_render_to_fbo(swapchain);
837 TRACE("Creating front buffer.\n");
839 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
840 surface_desc.format = swapchain->desc.backbuffer_format;
841 surface_desc.multisample_type = swapchain->desc.multisample_type;
842 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
843 surface_desc.usage = 0;
844 surface_desc.pool = WINED3D_POOL_DEFAULT;
845 surface_desc.width = swapchain->desc.backbuffer_width;
846 surface_desc.height = swapchain->desc.backbuffer_height;
847 surface_desc.depth = 1;
848 surface_desc.size = 0;
850 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
851 parent, &surface_desc, &swapchain->front_buffer)))
853 WARN("Failed to create front buffer, hr %#x.\n", hr);
854 goto err;
857 surface_set_swapchain(swapchain->front_buffer, swapchain);
858 if (!(device->wined3d->flags & WINED3D_NO3D))
860 surface_validate_location(swapchain->front_buffer, WINED3D_LOCATION_DRAWABLE);
861 surface_invalidate_location(swapchain->front_buffer, ~WINED3D_LOCATION_DRAWABLE);
864 /* MSDN says we're only allowed a single fullscreen swapchain per device,
865 * so we should really check to see if there is a fullscreen swapchain
866 * already. Does a single head count as full screen? */
868 if (!desc->windowed)
870 struct wined3d_display_mode mode;
872 /* Change the display settings */
873 mode.width = desc->backbuffer_width;
874 mode.height = desc->backbuffer_height;
875 mode.format_id = desc->backbuffer_format;
876 mode.refresh_rate = desc->refresh_rate;
877 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
879 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
881 WARN("Failed to set display mode, hr %#x.\n", hr);
882 goto err;
884 displaymode_set = TRUE;
887 if (!(device->wined3d->flags & WINED3D_NO3D))
889 static const enum wined3d_format_id formats[] =
891 WINED3DFMT_D24_UNORM_S8_UINT,
892 WINED3DFMT_D32_UNORM,
893 WINED3DFMT_R24_UNORM_X8_TYPELESS,
894 WINED3DFMT_D16_UNORM,
895 WINED3DFMT_S1_UINT_D15_UNORM
898 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
900 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
901 if (!swapchain->context)
903 ERR("Failed to create the context array.\n");
904 hr = E_OUTOFMEMORY;
905 goto err;
907 swapchain->num_contexts = 1;
909 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
910 * You are able to add a depth + stencil surface at a later stage when you need it.
911 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
912 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
913 * context, need torecreate shaders, textures and other resources.
915 * The context manager already takes care of the state problem and for the other tasks code from Reset
916 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
917 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
918 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
919 * issue needs to be fixed. */
920 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
922 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
923 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
924 if (swapchain->context[0]) break;
925 TRACE("Depth stencil format %s is not supported, trying next format\n",
926 debug_d3dformat(formats[i]));
929 if (!swapchain->context[0])
931 WARN("Failed to create context.\n");
932 hr = WINED3DERR_NOTAVAILABLE;
933 goto err;
936 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
937 && (!desc->enable_auto_depth_stencil
938 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
940 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
942 context_release(swapchain->context[0]);
945 if (swapchain->desc.backbuffer_count > 0)
947 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
948 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
949 if (!swapchain->back_buffers)
951 ERR("Failed to allocate backbuffer array memory.\n");
952 hr = E_OUTOFMEMORY;
953 goto err;
956 surface_desc.usage |= WINED3DUSAGE_RENDERTARGET;
957 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
959 TRACE("Creating back buffer %u.\n", i);
960 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
961 parent, &surface_desc, &swapchain->back_buffers[i])))
963 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
964 swapchain->desc.backbuffer_count = i;
965 goto err;
967 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
971 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
972 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
974 TRACE("Creating depth/stencil buffer.\n");
975 if (!device->auto_depth_stencil)
977 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
978 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
980 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
981 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
983 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
984 goto err;
989 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
991 return WINED3D_OK;
993 err:
994 if (displaymode_set)
996 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
997 adapter->ordinal, &swapchain->original_mode)))
998 ERR("Failed to restore display mode.\n");
999 ClipCursor(NULL);
1002 if (swapchain->back_buffers)
1004 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1006 if (swapchain->back_buffers[i])
1008 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1009 wined3d_surface_decref(swapchain->back_buffers[i]);
1012 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1015 if (swapchain->context)
1017 if (swapchain->context[0])
1019 context_release(swapchain->context[0]);
1020 context_destroy(device, swapchain->context[0]);
1021 swapchain->num_contexts = 0;
1023 HeapFree(GetProcessHeap(), 0, swapchain->context);
1026 if (swapchain->front_buffer)
1028 surface_set_swapchain(swapchain->front_buffer, NULL);
1029 wined3d_surface_decref(swapchain->front_buffer);
1032 return hr;
1035 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1036 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1038 struct wined3d_swapchain *object;
1039 HRESULT hr;
1041 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1042 device, desc, parent, parent_ops, swapchain);
1044 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1045 if (!object)
1046 return E_OUTOFMEMORY;
1048 hr = swapchain_init(object, device, desc, parent, parent_ops);
1049 if (FAILED(hr))
1051 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1052 HeapFree(GetProcessHeap(), 0, object);
1053 return hr;
1056 TRACE("Created swapchain %p.\n", object);
1057 *swapchain = object;
1059 return WINED3D_OK;
1062 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1064 struct wined3d_context **newArray;
1065 struct wined3d_context *ctx;
1067 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1069 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1071 ERR("Failed to create a new context for the swapchain\n");
1072 return NULL;
1074 context_release(ctx);
1076 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1077 if(!newArray) {
1078 ERR("Out of memory when trying to allocate a new context array\n");
1079 context_destroy(swapchain->device, ctx);
1080 return NULL;
1082 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1083 HeapFree(GetProcessHeap(), 0, swapchain->context);
1084 newArray[swapchain->num_contexts] = ctx;
1085 swapchain->context = newArray;
1086 swapchain->num_contexts++;
1088 TRACE("Returning context %p\n", ctx);
1089 return ctx;
1092 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1094 unsigned int i;
1096 for (i = 0; i < swapchain->num_contexts; ++i)
1098 context_destroy(swapchain->device, swapchain->context[i]);
1100 swapchain->num_contexts = 0;
1103 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1105 DWORD tid = GetCurrentThreadId();
1106 unsigned int i;
1108 for (i = 0; i < swapchain->num_contexts; ++i)
1110 if (swapchain->context[i]->tid == tid)
1111 return swapchain->context[i];
1114 /* Create a new context for the thread */
1115 return swapchain_create_context(swapchain);
1118 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1120 /* The drawable size of an onscreen drawable is the surface size.
1121 * (Actually: The window size, but the surface is created in window size) */
1122 *width = context->current_rt->resource.width;
1123 *height = context->current_rt->resource.height;
1126 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1128 if (!swapchain->backup_dc)
1130 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1132 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1133 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1135 ERR("Failed to create a window.\n");
1136 return NULL;
1139 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1141 ERR("Failed to get a DC.\n");
1142 DestroyWindow(swapchain->backup_wnd);
1143 swapchain->backup_wnd = NULL;
1144 return NULL;
1148 return swapchain->backup_dc;
1151 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1153 UINT i;
1155 surface_update_draw_binding(swapchain->front_buffer);
1157 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1159 surface_update_draw_binding(swapchain->back_buffers[i]);