winex11: Create contexts at initialization time to avoid the need for locks.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blobc4fc409cf396f5f4180ded32733b9b7c056ae902
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 /* Do not call while under the GL lock. */
31 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
33 struct wined3d_display_mode mode;
34 HRESULT hr;
35 UINT i;
37 TRACE("Destroying swapchain %p.\n", swapchain);
39 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
41 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
42 * is the last buffer to be destroyed, FindContext() depends on that. */
43 if (swapchain->front_buffer)
45 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
46 if (wined3d_surface_decref(swapchain->front_buffer))
47 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
48 swapchain->front_buffer = NULL;
51 if (swapchain->back_buffers)
53 i = swapchain->desc.backbuffer_count;
55 while (i--)
57 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
58 if (wined3d_surface_decref(swapchain->back_buffers[i]))
59 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
61 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
62 swapchain->back_buffers = NULL;
65 for (i = 0; i < swapchain->num_contexts; ++i)
67 context_destroy(swapchain->device, swapchain->context[i]);
69 HeapFree(GetProcessHeap(), 0, swapchain->context);
71 /* Restore the screen resolution if we rendered in fullscreen.
72 * This will restore the screen resolution to what it was before creating
73 * the swapchain. In case of d3d8 and d3d9 this will be the original
74 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
75 * sets the resolution before starting up Direct3D, thus orig_width and
76 * orig_height will be equal to the modes in the presentation params. */
77 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
79 mode.width = swapchain->orig_width;
80 mode.height = swapchain->orig_height;
81 mode.refresh_rate = 0;
82 mode.format_id = swapchain->orig_fmt;
83 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
84 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
85 swapchain->device->adapter->ordinal, &mode)))
86 ERR("Failed to restore display mode, hr %#x.\n", hr);
89 if (swapchain->backup_dc)
91 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
93 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
94 DestroyWindow(swapchain->backup_wnd);
98 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
100 ULONG refcount = InterlockedIncrement(&swapchain->ref);
102 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
104 return refcount;
107 /* Do not call while under the GL lock. */
108 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
110 ULONG refcount = InterlockedDecrement(&swapchain->ref);
112 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
114 if (!refcount)
116 swapchain_cleanup(swapchain);
117 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
118 HeapFree(GetProcessHeap(), 0, swapchain);
121 return refcount;
124 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
126 TRACE("swapchain %p.\n", swapchain);
128 return swapchain->parent;
131 HRESULT CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
133 if (!window)
134 window = swapchain->device_window;
135 if (window == swapchain->win_handle)
136 return WINED3D_OK;
138 TRACE("Setting swapchain %p window from %p to %p.\n",
139 swapchain, swapchain->win_handle, window);
140 swapchain->win_handle = window;
142 return WINED3D_OK;
145 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
146 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
147 const RGNDATA *dirty_region, DWORD flags)
149 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
150 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
151 dst_window_override, dirty_region, flags);
153 if (flags)
154 FIXME("Ignoring flags %#x.\n", flags);
156 if (!swapchain->back_buffers)
158 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
159 return WINED3DERR_INVALIDCALL;
162 wined3d_swapchain_set_window(swapchain, dst_window_override);
164 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
166 return WINED3D_OK;
169 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
170 struct wined3d_surface *dst_surface)
172 struct wined3d_surface *src_surface;
173 RECT src_rect, dst_rect;
175 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
177 src_surface = swapchain->front_buffer;
178 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
179 dst_rect = src_rect;
181 if (swapchain->desc.windowed)
183 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
184 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
185 wine_dbgstr_rect(&dst_rect));
188 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
191 HRESULT CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
192 UINT back_buffer_idx, enum wined3d_backbuffer_type type, struct wined3d_surface **back_buffer)
194 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
195 swapchain, back_buffer_idx, type, back_buffer);
197 /* Return invalid if there is no backbuffer array, otherwise it will
198 * crash when ddraw is used (there swapchain->back_buffers is always
199 * NULL). We need this because this function is called from
200 * stateblock_init_default_state() to get the default scissorrect
201 * dimensions. */
202 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
204 WARN("Invalid back buffer index.\n");
205 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
206 * here in wined3d to avoid problems in other libs. */
207 *back_buffer = NULL;
208 return WINED3DERR_INVALIDCALL;
211 *back_buffer = swapchain->back_buffers[back_buffer_idx];
212 if (*back_buffer)
213 wined3d_surface_incref(*back_buffer);
215 TRACE("Returning back buffer %p.\n", *back_buffer);
217 return WINED3D_OK;
220 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
221 struct wined3d_raster_status *raster_status)
223 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
225 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
226 swapchain->device->adapter->ordinal, raster_status);
229 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
230 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
232 HRESULT hr;
234 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
236 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
237 swapchain->device->adapter->ordinal, mode, rotation);
239 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
240 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
242 return hr;
245 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
247 TRACE("swapchain %p.\n", swapchain);
249 return swapchain->device;
252 HRESULT CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
253 struct wined3d_swapchain_desc *desc)
255 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
257 *desc = swapchain->desc;
259 return WINED3D_OK;
262 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
263 DWORD flags, const struct wined3d_gamma_ramp *ramp)
265 HDC dc;
267 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
269 if (flags)
270 FIXME("Ignoring flags %#x.\n", flags);
272 dc = GetDC(swapchain->device_window);
273 SetDeviceGammaRamp(dc, (void *)ramp);
274 ReleaseDC(swapchain->device_window, dc);
276 return WINED3D_OK;
279 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
280 struct wined3d_gamma_ramp *ramp)
282 HDC dc;
284 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
286 dc = GetDC(swapchain->device_window);
287 GetDeviceGammaRamp(dc, ramp);
288 ReleaseDC(swapchain->device_window, dc);
290 return WINED3D_OK;
293 /* A GL context is provided by the caller */
294 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
295 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
297 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
298 UINT src_w = src_rect->right - src_rect->left;
299 UINT src_h = src_rect->bottom - src_rect->top;
300 GLenum gl_filter;
301 const struct wined3d_gl_info *gl_info = context->gl_info;
302 RECT win_rect;
303 UINT win_h;
305 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
306 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
308 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
309 gl_filter = GL_NEAREST;
310 else
311 gl_filter = GL_LINEAR;
313 GetClientRect(swapchain->win_handle, &win_rect);
314 win_h = win_rect.bottom - win_rect.top;
316 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
318 DWORD location = SFLAG_INTEXTURE;
320 if (backbuffer->resource.multisample_type)
322 location = SFLAG_INRB_RESOLVED;
323 surface_load_location(backbuffer, location, NULL);
326 ENTER_GL();
327 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
328 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
329 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
331 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
332 context_set_draw_buffer(context, GL_BACK);
333 context_invalidate_state(context, STATE_FRAMEBUFFER);
335 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
336 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
337 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
338 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
339 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
341 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
342 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
344 /* Note that the texture is upside down */
345 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
346 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
347 GL_COLOR_BUFFER_BIT, gl_filter);
348 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
349 LEAVE_GL();
351 else
353 struct wined3d_device *device = swapchain->device;
354 struct wined3d_context *context2;
355 float tex_left = src_rect->left;
356 float tex_top = src_rect->top;
357 float tex_right = src_rect->right;
358 float tex_bottom = src_rect->bottom;
360 context2 = context_acquire(device, swapchain->back_buffers[0]);
361 context_apply_blit_state(context2, device);
363 if (backbuffer->flags & SFLAG_NORMCOORD)
365 tex_left /= src_w;
366 tex_right /= src_w;
367 tex_top /= src_h;
368 tex_bottom /= src_h;
371 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
372 gl_filter = GL_NEAREST;
374 ENTER_GL();
375 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
377 /* Set up the texture. The surface is not in a wined3d_texture
378 * container, so there are no D3D texture settings to dirtify. */
379 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
380 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
381 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
383 context_set_draw_buffer(context, GL_BACK);
385 /* Set the viewport to the destination rectandle, disable any projection
386 * transformation set up by context_apply_blit_state(), and draw a
387 * (-1,-1)-(1,1) quad.
389 * Back up viewport and matrix to avoid breaking last_was_blit
391 * Note that context_apply_blit_state() set up viewport and ortho to
392 * match the surface size - we want the GL drawable(=window) size. */
393 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
394 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
395 dst_rect->right, win_h - dst_rect->top);
396 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
397 gl_info->gl_ops.gl.p_glPushMatrix();
398 gl_info->gl_ops.gl.p_glLoadIdentity();
400 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
401 /* bottom left */
402 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
403 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
405 /* top left */
406 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
407 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
409 /* top right */
410 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
411 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
413 /* bottom right */
414 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
415 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
416 gl_info->gl_ops.gl.p_glEnd();
418 gl_info->gl_ops.gl.p_glPopMatrix();
419 gl_info->gl_ops.gl.p_glPopAttrib();
421 device->blitter->unset_shader(context->gl_info);
422 checkGLcall("Swapchain present blit(manual)\n");
423 LEAVE_GL();
425 context_release(context2);
429 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
430 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
432 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
433 const struct wined3d_fb_state *fb = &swapchain->device->fb;
434 const struct wined3d_gl_info *gl_info;
435 struct wined3d_context *context;
436 RECT src_rect, dst_rect;
437 BOOL render_to_fbo;
439 context = context_acquire(swapchain->device, back_buffer);
440 if (!context->valid)
442 context_release(context);
443 WARN("Invalid context, skipping present.\n");
444 return;
447 gl_info = context->gl_info;
449 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
450 if (swapchain->device->bCursorVisible &&
451 swapchain->device->cursorTexture &&
452 !swapchain->device->hardwareCursor)
454 struct wined3d_surface cursor;
455 RECT destRect =
457 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
458 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
459 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
460 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
462 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
463 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
464 * the application because we are only supposed to copy the information out. Using a fake surface
465 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
467 memset(&cursor, 0, sizeof(cursor));
468 cursor.resource.ref = 1;
469 cursor.resource.device = swapchain->device;
470 cursor.resource.pool = WINED3D_POOL_SCRATCH;
471 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
472 cursor.resource.type = WINED3D_RTYPE_SURFACE;
473 cursor.texture_name = swapchain->device->cursorTexture;
474 cursor.texture_target = GL_TEXTURE_2D;
475 cursor.texture_level = 0;
476 cursor.resource.width = swapchain->device->cursorWidth;
477 cursor.resource.height = swapchain->device->cursorHeight;
478 /* The cursor must have pow2 sizes */
479 cursor.pow2Width = cursor.resource.width;
480 cursor.pow2Height = cursor.resource.height;
481 /* The surface is in the texture */
482 cursor.flags |= SFLAG_INTEXTURE;
483 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
484 * which is exactly what we want :-)
486 if (swapchain->desc.windowed)
487 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
488 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
489 NULL, WINED3D_TEXF_POINT);
492 if (swapchain->device->logo_surface)
494 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
495 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
497 /* Blit the logo into the upper left corner of the drawable. */
498 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
499 NULL, WINED3D_TEXF_POINT);
502 TRACE("Presenting HDC %p.\n", context->hdc);
504 render_to_fbo = swapchain->render_to_fbo;
506 if (src_rect_in)
508 src_rect = *src_rect_in;
509 if (!render_to_fbo && (src_rect.left || src_rect.top
510 || src_rect.right != swapchain->desc.backbuffer_width
511 || src_rect.bottom != swapchain->desc.backbuffer_height))
513 render_to_fbo = TRUE;
516 else
518 src_rect.left = 0;
519 src_rect.top = 0;
520 src_rect.right = swapchain->desc.backbuffer_width;
521 src_rect.bottom = swapchain->desc.backbuffer_height;
524 if (dst_rect_in)
525 dst_rect = *dst_rect_in;
526 else
527 GetClientRect(swapchain->win_handle, &dst_rect);
529 if (!render_to_fbo && (dst_rect.left || dst_rect.top
530 || dst_rect.right != swapchain->desc.backbuffer_width
531 || dst_rect.bottom != swapchain->desc.backbuffer_height))
532 render_to_fbo = TRUE;
534 /* Rendering to a window of different size, presenting partial rectangles,
535 * or rendering to a different window needs help from FBO_blit or a textured
536 * draw. Render the swapchain to a FBO in the future.
538 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
539 * all these issues - this fails if the window is smaller than the backbuffer.
541 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
543 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
544 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
545 swapchain->render_to_fbo = TRUE;
546 swapchain_update_draw_bindings(swapchain);
548 else
550 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
553 if (swapchain->render_to_fbo)
555 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
556 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
557 * not allowed(they need the COPY swapeffect)
559 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
560 * the swap. */
561 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
562 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
564 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
567 if (swapchain->num_contexts > 1)
568 gl_info->gl_ops.gl.p_glFinish();
569 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
571 TRACE("SwapBuffers called, Starting new frame\n");
572 /* FPS support */
573 if (TRACE_ON(fps))
575 DWORD time = GetTickCount();
576 ++swapchain->frames;
578 /* every 1.5 seconds */
579 if (time - swapchain->prev_time > 1500)
581 TRACE_(fps)("%p @ approx %.2ffps\n",
582 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
583 swapchain->prev_time = time;
584 swapchain->frames = 0;
588 /* This is disabled, but the code left in for debug purposes.
590 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
591 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
592 * The Debug runtime does the same on Windows. However, a few games do not redraw the
593 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
595 * Tests show that the content of the back buffer after a discard flip is indeed not
596 * reliable, so no game can depend on the exact content. However, it resembles the
597 * old contents in some way, for example by showing fragments at other locations. In
598 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
599 * gets a dark background image. If we clear it with a bright ugly color, the game's
600 * bug shows up much more than it does on Windows, and the players see single pixels
601 * with wrong colors.
602 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
603 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
605 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
607 TRACE("Clearing the color buffer with cyan color\n");
609 wined3d_device_clear(swapchain->device, 0, NULL,
610 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
613 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
614 || (back_buffer->flags & SFLAG_INSYSMEM)))
616 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
617 * Doesn't work with render_to_fbo because we're not flipping
619 struct wined3d_surface *front = swapchain->front_buffer;
621 if (front->resource.size == back_buffer->resource.size)
623 DWORD fbflags;
624 flip_surface(front, back_buffer);
626 /* Tell the front buffer surface that is has been modified. However,
627 * the other locations were preserved during that, so keep the flags.
628 * This serves to update the emulated overlay, if any. */
629 fbflags = front->flags;
630 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
631 front->flags = fbflags;
633 else
635 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
636 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
639 else
641 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
642 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
643 * and INTEXTURE copies can keep their old content if they have any defined content.
644 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
645 * the texture / sysmem copy needs to be reloaded from the drawable
647 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
648 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
651 if (fb->depth_stencil)
653 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
654 || fb->depth_stencil->flags & SFLAG_DISCARD)
656 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
657 fb->depth_stencil->resource.width,
658 fb->depth_stencil->resource.height);
659 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
661 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
662 swapchain->device->onscreen_depth_stencil = NULL;
667 context_release(context);
670 static const struct wined3d_swapchain_ops swapchain_gl_ops =
672 swapchain_gl_present,
675 /* Helper function that blits the front buffer contents to the target window. */
676 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
678 const struct wined3d_surface *front;
679 POINT offset = {0, 0};
680 HDC src_dc, dst_dc;
681 RECT draw_rect;
682 HWND window;
684 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
686 front = swapchain->front_buffer;
687 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
688 return;
690 if (front->resource.map_count)
691 ERR("Trying to blit a mapped surface.\n");
693 TRACE("Copying surface %p to screen.\n", front);
695 src_dc = front->hDC;
696 window = swapchain->win_handle;
697 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
699 /* Front buffer coordinates are screen coordinates. Map them to the
700 * destination window if not fullscreened. */
701 if (swapchain->desc.windowed)
702 ClientToScreen(window, &offset);
704 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
706 draw_rect.left = 0;
707 draw_rect.right = front->resource.width;
708 draw_rect.top = 0;
709 draw_rect.bottom = front->resource.height;
711 if (rect)
712 IntersectRect(&draw_rect, &draw_rect, rect);
714 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
715 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
716 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
717 ReleaseDC(window, dst_dc);
720 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
721 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
723 struct wined3d_surface *front, *back;
725 front = swapchain->front_buffer;
726 back = swapchain->back_buffers[0];
728 /* Flip the DC. */
730 HDC tmp;
731 tmp = front->hDC;
732 front->hDC = back->hDC;
733 back->hDC = tmp;
736 /* Flip the DIBsection. */
738 HBITMAP tmp;
739 tmp = front->dib.DIBsection;
740 front->dib.DIBsection = back->dib.DIBsection;
741 back->dib.DIBsection = tmp;
744 /* Flip the surface data. */
746 void *tmp;
748 tmp = front->dib.bitmap_data;
749 front->dib.bitmap_data = back->dib.bitmap_data;
750 back->dib.bitmap_data = tmp;
752 tmp = front->resource.allocatedMemory;
753 front->resource.allocatedMemory = back->resource.allocatedMemory;
754 back->resource.allocatedMemory = tmp;
756 if (front->resource.heapMemory)
757 ERR("GDI Surface %p has heap memory allocated.\n", front);
759 if (back->resource.heapMemory)
760 ERR("GDI Surface %p has heap memory allocated.\n", back);
763 /* FPS support */
764 if (TRACE_ON(fps))
766 static LONG prev_time, frames;
767 DWORD time = GetTickCount();
769 ++frames;
771 /* every 1.5 seconds */
772 if (time - prev_time > 1500)
774 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
775 prev_time = time;
776 frames = 0;
780 x11_copy_to_screen(swapchain, NULL);
783 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
785 swapchain_gdi_present,
788 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
790 RECT client_rect;
792 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
793 return;
795 if (!swapchain->desc.backbuffer_count)
797 TRACE("Single buffered rendering.\n");
798 swapchain->render_to_fbo = FALSE;
799 return;
802 GetClientRect(swapchain->win_handle, &client_rect);
804 TRACE("Backbuffer %ux%u, window %ux%u.\n",
805 swapchain->desc.backbuffer_width,
806 swapchain->desc.backbuffer_height,
807 client_rect.right, client_rect.bottom);
808 TRACE("Multisample type %#x, quality %#x.\n",
809 swapchain->desc.multisample_type,
810 swapchain->desc.multisample_quality);
812 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
813 && swapchain->desc.backbuffer_width == client_rect.right
814 && swapchain->desc.backbuffer_height == client_rect.bottom)
816 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
817 swapchain->render_to_fbo = FALSE;
818 return;
821 TRACE("Rendering to FBO.\n");
822 swapchain->render_to_fbo = TRUE;
825 /* Do not call while under the GL lock. */
826 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, enum wined3d_surface_type surface_type,
827 struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
828 void *parent, const struct wined3d_parent_ops *parent_ops)
830 const struct wined3d_adapter *adapter = device->adapter;
831 const struct wined3d_format *format;
832 struct wined3d_display_mode mode;
833 BOOL displaymode_set = FALSE;
834 RECT client_rect;
835 HWND window;
836 HRESULT hr;
837 UINT i;
839 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
841 FIXME("The application requested %u back buffers, this is not supported.\n",
842 desc->backbuffer_count);
843 return WINED3DERR_INVALIDCALL;
846 if (desc->backbuffer_count > 1)
848 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
849 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
852 switch (surface_type)
854 case WINED3D_SURFACE_TYPE_GDI:
855 swapchain->swapchain_ops = &swapchain_gdi_ops;
856 break;
858 case WINED3D_SURFACE_TYPE_OPENGL:
859 swapchain->swapchain_ops = &swapchain_gl_ops;
860 break;
862 default:
863 ERR("Invalid surface type %#x.\n", surface_type);
864 return WINED3DERR_INVALIDCALL;
867 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
869 swapchain->device = device;
870 swapchain->parent = parent;
871 swapchain->parent_ops = parent_ops;
872 swapchain->ref = 1;
873 swapchain->win_handle = window;
874 swapchain->device_window = window;
876 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode, NULL);
877 swapchain->orig_width = mode.width;
878 swapchain->orig_height = mode.height;
879 swapchain->orig_fmt = mode.format_id;
880 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
882 GetClientRect(window, &client_rect);
883 if (desc->windowed
884 && (!desc->backbuffer_width || !desc->backbuffer_height
885 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
888 if (!desc->backbuffer_width)
890 desc->backbuffer_width = client_rect.right;
891 TRACE("Updating width to %u.\n", desc->backbuffer_width);
894 if (!desc->backbuffer_height)
896 desc->backbuffer_height = client_rect.bottom;
897 TRACE("Updating height to %u.\n", desc->backbuffer_height);
900 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
902 desc->backbuffer_format = swapchain->orig_fmt;
903 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
906 swapchain->desc = *desc;
907 swapchain_update_render_to_fbo(swapchain);
909 TRACE("Creating front buffer.\n");
910 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent, parent,
911 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
912 swapchain->desc.backbuffer_format, WINED3DUSAGE_RENDERTARGET,
913 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
914 &swapchain->front_buffer)))
916 WARN("Failed to create front buffer, hr %#x.\n", hr);
917 goto err;
920 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_SWAPCHAIN, swapchain);
921 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
922 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
924 /* MSDN says we're only allowed a single fullscreen swapchain per device,
925 * so we should really check to see if there is a fullscreen swapchain
926 * already. Does a single head count as full screen? */
928 if (!desc->windowed)
930 struct wined3d_display_mode mode;
932 /* Change the display settings */
933 mode.width = desc->backbuffer_width;
934 mode.height = desc->backbuffer_height;
935 mode.format_id = desc->backbuffer_format;
936 mode.refresh_rate = desc->refresh_rate;
937 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
939 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode)))
941 WARN("Failed to set display mode, hr %#x.\n", hr);
942 goto err;
944 displaymode_set = TRUE;
947 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
949 static const enum wined3d_format_id formats[] =
951 WINED3DFMT_D24_UNORM_S8_UINT,
952 WINED3DFMT_D32_UNORM,
953 WINED3DFMT_R24_UNORM_X8_TYPELESS,
954 WINED3DFMT_D16_UNORM,
955 WINED3DFMT_S1_UINT_D15_UNORM
958 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
960 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
961 if (!swapchain->context)
963 ERR("Failed to create the context array.\n");
964 hr = E_OUTOFMEMORY;
965 goto err;
967 swapchain->num_contexts = 1;
969 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
970 * You are able to add a depth + stencil surface at a later stage when you need it.
971 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
972 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
973 * context, need torecreate shaders, textures and other resources.
975 * The context manager already takes care of the state problem and for the other tasks code from Reset
976 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
977 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
978 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
979 * issue needs to be fixed. */
980 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
982 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
983 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
984 if (swapchain->context[0]) break;
985 TRACE("Depth stencil format %s is not supported, trying next format\n",
986 debug_d3dformat(formats[i]));
989 if (!swapchain->context[0])
991 WARN("Failed to create context.\n");
992 hr = WINED3DERR_NOTAVAILABLE;
993 goto err;
996 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
997 && (!desc->enable_auto_depth_stencil
998 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
1000 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1002 context_release(swapchain->context[0]);
1005 if (swapchain->desc.backbuffer_count > 0)
1007 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1008 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1009 if (!swapchain->back_buffers)
1011 ERR("Failed to allocate backbuffer array memory.\n");
1012 hr = E_OUTOFMEMORY;
1013 goto err;
1016 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1018 TRACE("Creating back buffer %u.\n", i);
1019 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent, parent,
1020 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1021 swapchain->desc.backbuffer_format, WINED3DUSAGE_RENDERTARGET,
1022 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
1023 &swapchain->back_buffers[i])))
1025 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1026 goto err;
1029 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_SWAPCHAIN, swapchain);
1033 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1034 if (desc->enable_auto_depth_stencil && surface_type == WINED3D_SURFACE_TYPE_OPENGL)
1036 TRACE("Creating depth/stencil buffer.\n");
1037 if (!device->auto_depth_stencil)
1039 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1040 device->device_parent, swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1041 swapchain->desc.auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
1042 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
1043 &device->auto_depth_stencil)))
1045 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1046 goto err;
1049 surface_set_container(device->auto_depth_stencil, WINED3D_CONTAINER_NONE, NULL);
1053 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1055 return WINED3D_OK;
1057 err:
1058 if (displaymode_set)
1060 DEVMODEW devmode;
1062 ClipCursor(NULL);
1064 /* Change the display settings */
1065 memset(&devmode, 0, sizeof(devmode));
1066 devmode.dmSize = sizeof(devmode);
1067 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1068 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1069 devmode.dmPelsWidth = swapchain->orig_width;
1070 devmode.dmPelsHeight = swapchain->orig_height;
1071 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1074 if (swapchain->back_buffers)
1076 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1078 if (swapchain->back_buffers[i])
1080 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
1081 wined3d_surface_decref(swapchain->back_buffers[i]);
1084 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1087 if (swapchain->context)
1089 if (swapchain->context[0])
1091 context_release(swapchain->context[0]);
1092 context_destroy(device, swapchain->context[0]);
1093 swapchain->num_contexts = 0;
1095 HeapFree(GetProcessHeap(), 0, swapchain->context);
1098 if (swapchain->front_buffer)
1100 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
1101 wined3d_surface_decref(swapchain->front_buffer);
1104 return hr;
1107 /* Do not call while under the GL lock. */
1108 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1109 struct wined3d_swapchain_desc *desc, enum wined3d_surface_type surface_type,
1110 void *parent, const struct wined3d_parent_ops *parent_ops,
1111 struct wined3d_swapchain **swapchain)
1113 struct wined3d_swapchain *object;
1114 HRESULT hr;
1116 TRACE("device %p, desc %p, swapchain %p, parent %p, surface_type %#x.\n",
1117 device, desc, swapchain, parent, surface_type);
1119 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1120 if (!object)
1122 ERR("Failed to allocate swapchain memory.\n");
1123 return E_OUTOFMEMORY;
1126 hr = swapchain_init(object, surface_type, device, desc, parent, parent_ops);
1127 if (FAILED(hr))
1129 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1130 HeapFree(GetProcessHeap(), 0, object);
1131 return hr;
1134 TRACE("Created swapchain %p.\n", object);
1135 *swapchain = object;
1137 return WINED3D_OK;
1140 /* Do not call while under the GL lock. */
1141 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1143 struct wined3d_context **newArray;
1144 struct wined3d_context *ctx;
1146 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1148 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1150 ERR("Failed to create a new context for the swapchain\n");
1151 return NULL;
1153 context_release(ctx);
1155 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1156 if(!newArray) {
1157 ERR("Out of memory when trying to allocate a new context array\n");
1158 context_destroy(swapchain->device, ctx);
1159 return NULL;
1161 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1162 HeapFree(GetProcessHeap(), 0, swapchain->context);
1163 newArray[swapchain->num_contexts] = ctx;
1164 swapchain->context = newArray;
1165 swapchain->num_contexts++;
1167 TRACE("Returning context %p\n", ctx);
1168 return ctx;
1171 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1173 unsigned int i;
1175 for (i = 0; i < swapchain->num_contexts; ++i)
1177 context_destroy(swapchain->device, swapchain->context[i]);
1179 swapchain->num_contexts = 0;
1182 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1184 DWORD tid = GetCurrentThreadId();
1185 unsigned int i;
1187 for (i = 0; i < swapchain->num_contexts; ++i)
1189 if (swapchain->context[i]->tid == tid)
1190 return swapchain->context[i];
1193 /* Create a new context for the thread */
1194 return swapchain_create_context(swapchain);
1197 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1199 /* The drawable size of an onscreen drawable is the surface size.
1200 * (Actually: The window size, but the surface is created in window size) */
1201 *width = context->current_rt->resource.width;
1202 *height = context->current_rt->resource.height;
1205 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1207 if (!swapchain->backup_dc)
1209 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1211 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1212 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1214 ERR("Failed to create a window.\n");
1215 return NULL;
1218 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1220 ERR("Failed to get a DC.\n");
1221 DestroyWindow(swapchain->backup_wnd);
1222 swapchain->backup_wnd = NULL;
1223 return NULL;
1227 return swapchain->backup_dc;
1230 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1232 UINT i;
1234 surface_update_draw_binding(swapchain->front_buffer);
1236 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1238 surface_update_draw_binding(swapchain->back_buffers[i]);