winemac: Rename some confusingly-named variables.
[wine.git] / dlls / wined3d / swapchain.c
blob73243a98d947ed1dd1a225c719b919a1b170b855
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 ReleaseDC(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 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
262 struct wined3d_gamma_ramp *ramp)
264 HDC dc;
266 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
268 dc = GetDC(swapchain->device_window);
269 GetDeviceGammaRamp(dc, ramp);
270 ReleaseDC(swapchain->device_window, dc);
272 return WINED3D_OK;
275 /* A GL context is provided by the caller */
276 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
277 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
279 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
280 UINT src_w = src_rect->right - src_rect->left;
281 UINT src_h = src_rect->bottom - src_rect->top;
282 GLenum gl_filter;
283 const struct wined3d_gl_info *gl_info = context->gl_info;
284 RECT win_rect;
285 UINT win_h;
287 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
288 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
290 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
291 gl_filter = GL_NEAREST;
292 else
293 gl_filter = GL_LINEAR;
295 GetClientRect(swapchain->win_handle, &win_rect);
296 win_h = win_rect.bottom - win_rect.top;
298 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
300 DWORD location = SFLAG_INTEXTURE;
302 if (backbuffer->resource.multisample_type)
304 location = SFLAG_INRB_RESOLVED;
305 surface_load_location(backbuffer, location, NULL);
308 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
309 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
310 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
312 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
313 context_set_draw_buffer(context, GL_BACK);
314 context_invalidate_state(context, STATE_FRAMEBUFFER);
316 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
317 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
318 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
319 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
320 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
322 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
323 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
325 /* Note that the texture is upside down */
326 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
327 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
328 GL_COLOR_BUFFER_BIT, gl_filter);
329 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
331 else
333 struct wined3d_device *device = swapchain->device;
334 struct wined3d_context *context2;
335 float tex_left = src_rect->left;
336 float tex_top = src_rect->top;
337 float tex_right = src_rect->right;
338 float tex_bottom = src_rect->bottom;
340 context2 = context_acquire(device, swapchain->back_buffers[0]);
341 context_apply_blit_state(context2, device);
343 if (backbuffer->flags & SFLAG_NORMCOORD)
345 tex_left /= src_w;
346 tex_right /= src_w;
347 tex_top /= src_h;
348 tex_bottom /= src_h;
351 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
352 gl_filter = GL_NEAREST;
354 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
355 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
357 /* Set up the texture. The surface is not in a wined3d_texture
358 * container, so there are no D3D texture settings to dirtify. */
359 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
360 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
361 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
363 context_set_draw_buffer(context, GL_BACK);
365 /* Set the viewport to the destination rectandle, disable any projection
366 * transformation set up by context_apply_blit_state(), and draw a
367 * (-1,-1)-(1,1) quad.
369 * Back up viewport and matrix to avoid breaking last_was_blit
371 * Note that context_apply_blit_state() set up viewport and ortho to
372 * match the surface size - we want the GL drawable(=window) size. */
373 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
374 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
375 dst_rect->right, win_h - dst_rect->top);
376 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
377 gl_info->gl_ops.gl.p_glPushMatrix();
378 gl_info->gl_ops.gl.p_glLoadIdentity();
380 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
381 /* bottom left */
382 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
383 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
385 /* top left */
386 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
387 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
389 /* top right */
390 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
391 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
393 /* bottom right */
394 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
395 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
396 gl_info->gl_ops.gl.p_glEnd();
398 gl_info->gl_ops.gl.p_glPopMatrix();
399 gl_info->gl_ops.gl.p_glPopAttrib();
401 device->blitter->unset_shader(context->gl_info);
402 checkGLcall("Swapchain present blit(manual)\n");
404 context_release(context2);
408 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
409 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
411 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
412 const struct wined3d_fb_state *fb = &swapchain->device->fb;
413 const struct wined3d_gl_info *gl_info;
414 struct wined3d_context *context;
415 RECT src_rect, dst_rect;
416 BOOL render_to_fbo;
418 context = context_acquire(swapchain->device, back_buffer);
419 if (!context->valid)
421 context_release(context);
422 WARN("Invalid context, skipping present.\n");
423 return;
426 gl_info = context->gl_info;
428 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
429 if (swapchain->device->bCursorVisible &&
430 swapchain->device->cursorTexture &&
431 !swapchain->device->hardwareCursor)
433 struct wined3d_surface cursor;
434 RECT destRect =
436 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
437 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
438 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
439 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
441 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
442 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
443 * the application because we are only supposed to copy the information out. Using a fake surface
444 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
446 memset(&cursor, 0, sizeof(cursor));
447 cursor.resource.ref = 1;
448 cursor.resource.device = swapchain->device;
449 cursor.resource.pool = WINED3D_POOL_SCRATCH;
450 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
451 cursor.resource.type = WINED3D_RTYPE_SURFACE;
452 cursor.texture_name = swapchain->device->cursorTexture;
453 cursor.texture_target = GL_TEXTURE_2D;
454 cursor.texture_level = 0;
455 cursor.resource.width = swapchain->device->cursorWidth;
456 cursor.resource.height = swapchain->device->cursorHeight;
457 /* The cursor must have pow2 sizes */
458 cursor.pow2Width = cursor.resource.width;
459 cursor.pow2Height = cursor.resource.height;
460 /* The surface is in the texture */
461 cursor.flags |= SFLAG_INTEXTURE;
462 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
463 * which is exactly what we want :-)
465 if (swapchain->desc.windowed)
466 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
467 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
468 NULL, WINED3D_TEXF_POINT);
471 if (swapchain->device->logo_surface)
473 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
474 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
476 /* Blit the logo into the upper left corner of the drawable. */
477 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
478 NULL, WINED3D_TEXF_POINT);
481 TRACE("Presenting HDC %p.\n", context->hdc);
483 render_to_fbo = swapchain->render_to_fbo;
485 if (src_rect_in)
487 src_rect = *src_rect_in;
488 if (!render_to_fbo && (src_rect.left || src_rect.top
489 || src_rect.right != swapchain->desc.backbuffer_width
490 || src_rect.bottom != swapchain->desc.backbuffer_height))
492 render_to_fbo = TRUE;
495 else
497 src_rect.left = 0;
498 src_rect.top = 0;
499 src_rect.right = swapchain->desc.backbuffer_width;
500 src_rect.bottom = swapchain->desc.backbuffer_height;
503 if (dst_rect_in)
504 dst_rect = *dst_rect_in;
505 else
506 GetClientRect(swapchain->win_handle, &dst_rect);
508 if (!render_to_fbo && (dst_rect.left || dst_rect.top
509 || dst_rect.right != swapchain->desc.backbuffer_width
510 || dst_rect.bottom != swapchain->desc.backbuffer_height))
511 render_to_fbo = TRUE;
513 /* Rendering to a window of different size, presenting partial rectangles,
514 * or rendering to a different window needs help from FBO_blit or a textured
515 * draw. Render the swapchain to a FBO in the future.
517 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
518 * all these issues - this fails if the window is smaller than the backbuffer.
520 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
522 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
523 surface_invalidate_location(back_buffer, SFLAG_INDRAWABLE);
524 swapchain->render_to_fbo = TRUE;
525 swapchain_update_draw_bindings(swapchain);
527 else
529 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
532 if (swapchain->render_to_fbo)
534 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
535 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
536 * not allowed(they need the COPY swapeffect)
538 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
539 * the swap. */
540 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
541 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
543 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
546 if (swapchain->num_contexts > 1)
547 gl_info->gl_ops.gl.p_glFinish();
549 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
550 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
552 TRACE("SwapBuffers called, Starting new frame\n");
553 /* FPS support */
554 if (TRACE_ON(fps))
556 DWORD time = GetTickCount();
557 ++swapchain->frames;
559 /* every 1.5 seconds */
560 if (time - swapchain->prev_time > 1500)
562 TRACE_(fps)("%p @ approx %.2ffps\n",
563 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
564 swapchain->prev_time = time;
565 swapchain->frames = 0;
569 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
570 || (back_buffer->flags & SFLAG_INSYSMEM)))
572 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
573 * Doesn't work with render_to_fbo because we're not flipping
575 struct wined3d_surface *front = swapchain->front_buffer;
577 if (front->resource.size == back_buffer->resource.size)
579 flip_surface(front, back_buffer);
581 /* Tell the front buffer surface that is has been modified. However,
582 * the other locations were preserved during that, so keep the flags.
583 * This serves to update the emulated overlay, if any. */
584 surface_validate_location(front, SFLAG_INDRAWABLE);
586 else
588 surface_validate_location(front, SFLAG_INDRAWABLE);
589 surface_invalidate_location(front, ~SFLAG_INDRAWABLE);
590 surface_validate_location(back_buffer, SFLAG_INDRAWABLE);
591 surface_invalidate_location(back_buffer, ~SFLAG_INDRAWABLE);
594 else
596 surface_validate_location(swapchain->front_buffer, SFLAG_INDRAWABLE);
597 surface_invalidate_location(swapchain->front_buffer, ~SFLAG_INDRAWABLE);
598 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
599 * and INTEXTURE copies can keep their old content if they have any defined content.
600 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
601 * the texture / sysmem copy needs to be reloaded from the drawable
603 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
605 surface_validate_location(back_buffer, back_buffer->draw_binding);
606 surface_invalidate_location(back_buffer, ~back_buffer->draw_binding);
610 if (fb->depth_stencil)
612 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
613 || fb->depth_stencil->flags & SFLAG_DISCARD)
615 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
616 fb->depth_stencil->resource.width,
617 fb->depth_stencil->resource.height);
618 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
620 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
621 swapchain->device->onscreen_depth_stencil = NULL;
626 context_release(context);
629 static const struct wined3d_swapchain_ops swapchain_gl_ops =
631 swapchain_gl_present,
634 /* Helper function that blits the front buffer contents to the target window. */
635 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
637 const struct wined3d_surface *front;
638 POINT offset = {0, 0};
639 HDC src_dc, dst_dc;
640 RECT draw_rect;
641 HWND window;
643 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
645 front = swapchain->front_buffer;
646 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
647 return;
649 if (front->resource.map_count)
650 ERR("Trying to blit a mapped surface.\n");
652 TRACE("Copying surface %p to screen.\n", front);
654 src_dc = front->hDC;
655 window = swapchain->win_handle;
656 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
658 /* Front buffer coordinates are screen coordinates. Map them to the
659 * destination window if not fullscreened. */
660 if (swapchain->desc.windowed)
661 ClientToScreen(window, &offset);
663 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
665 draw_rect.left = 0;
666 draw_rect.right = front->resource.width;
667 draw_rect.top = 0;
668 draw_rect.bottom = front->resource.height;
670 if (rect)
671 IntersectRect(&draw_rect, &draw_rect, rect);
673 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
674 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
675 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
676 ReleaseDC(window, dst_dc);
679 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
680 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
682 struct wined3d_surface *front, *back;
684 front = swapchain->front_buffer;
685 back = swapchain->back_buffers[0];
687 /* Flip the DC. */
689 HDC tmp;
690 tmp = front->hDC;
691 front->hDC = back->hDC;
692 back->hDC = tmp;
695 /* Flip the DIBsection. */
697 HBITMAP tmp;
698 tmp = front->dib.DIBsection;
699 front->dib.DIBsection = back->dib.DIBsection;
700 back->dib.DIBsection = tmp;
703 /* Flip the surface data. */
705 void *tmp;
707 tmp = front->dib.bitmap_data;
708 front->dib.bitmap_data = back->dib.bitmap_data;
709 back->dib.bitmap_data = tmp;
711 tmp = front->resource.allocatedMemory;
712 front->resource.allocatedMemory = back->resource.allocatedMemory;
713 back->resource.allocatedMemory = tmp;
715 if (front->resource.heap_memory)
716 ERR("GDI Surface %p has heap memory allocated.\n", front);
718 if (back->resource.heap_memory)
719 ERR("GDI Surface %p has heap memory allocated.\n", back);
722 /* FPS support */
723 if (TRACE_ON(fps))
725 static LONG prev_time, frames;
726 DWORD time = GetTickCount();
728 ++frames;
730 /* every 1.5 seconds */
731 if (time - prev_time > 1500)
733 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
734 prev_time = time;
735 frames = 0;
739 x11_copy_to_screen(swapchain, NULL);
742 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
744 swapchain_gdi_present,
747 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
749 RECT client_rect;
751 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
752 return;
754 if (!swapchain->desc.backbuffer_count)
756 TRACE("Single buffered rendering.\n");
757 swapchain->render_to_fbo = FALSE;
758 return;
761 GetClientRect(swapchain->win_handle, &client_rect);
763 TRACE("Backbuffer %ux%u, window %ux%u.\n",
764 swapchain->desc.backbuffer_width,
765 swapchain->desc.backbuffer_height,
766 client_rect.right, client_rect.bottom);
767 TRACE("Multisample type %#x, quality %#x.\n",
768 swapchain->desc.multisample_type,
769 swapchain->desc.multisample_quality);
771 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
772 && swapchain->desc.backbuffer_width == client_rect.right
773 && swapchain->desc.backbuffer_height == client_rect.bottom)
775 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
776 swapchain->render_to_fbo = FALSE;
777 return;
780 TRACE("Rendering to FBO.\n");
781 swapchain->render_to_fbo = TRUE;
784 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
785 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
787 const struct wined3d_adapter *adapter = device->adapter;
788 struct wined3d_resource_desc surface_desc;
789 BOOL displaymode_set = FALSE;
790 RECT client_rect;
791 HWND window;
792 HRESULT hr;
793 UINT i;
795 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
797 FIXME("The application requested %u back buffers, this is not supported.\n",
798 desc->backbuffer_count);
799 return WINED3DERR_INVALIDCALL;
802 if (desc->backbuffer_count > 1)
804 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
805 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
808 if (device->wined3d->flags & WINED3D_NO3D)
809 swapchain->swapchain_ops = &swapchain_gdi_ops;
810 else
811 swapchain->swapchain_ops = &swapchain_gl_ops;
813 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
815 swapchain->device = device;
816 swapchain->parent = parent;
817 swapchain->parent_ops = parent_ops;
818 swapchain->ref = 1;
819 swapchain->win_handle = window;
820 swapchain->device_window = window;
822 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
823 adapter->ordinal, &swapchain->original_mode, NULL)))
825 ERR("Failed to get current display mode, hr %#x.\n", hr);
826 goto err;
829 GetClientRect(window, &client_rect);
830 if (desc->windowed
831 && (!desc->backbuffer_width || !desc->backbuffer_height
832 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
835 if (!desc->backbuffer_width)
837 desc->backbuffer_width = client_rect.right;
838 TRACE("Updating width to %u.\n", desc->backbuffer_width);
841 if (!desc->backbuffer_height)
843 desc->backbuffer_height = client_rect.bottom;
844 TRACE("Updating height to %u.\n", desc->backbuffer_height);
847 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
849 desc->backbuffer_format = swapchain->original_mode.format_id;
850 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
853 swapchain->desc = *desc;
854 swapchain_update_render_to_fbo(swapchain);
856 TRACE("Creating front buffer.\n");
858 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
859 surface_desc.format = swapchain->desc.backbuffer_format;
860 surface_desc.multisample_type = swapchain->desc.multisample_type;
861 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
862 surface_desc.usage = 0;
863 surface_desc.pool = WINED3D_POOL_DEFAULT;
864 surface_desc.width = swapchain->desc.backbuffer_width;
865 surface_desc.height = swapchain->desc.backbuffer_height;
866 surface_desc.depth = 1;
867 surface_desc.size = 0;
869 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
870 parent, &surface_desc, &swapchain->front_buffer)))
872 WARN("Failed to create front buffer, hr %#x.\n", hr);
873 goto err;
876 surface_set_swapchain(swapchain->front_buffer, swapchain);
877 if (!(device->wined3d->flags & WINED3D_NO3D))
879 surface_validate_location(swapchain->front_buffer, SFLAG_INDRAWABLE);
880 surface_invalidate_location(swapchain->front_buffer, ~SFLAG_INDRAWABLE);
883 /* MSDN says we're only allowed a single fullscreen swapchain per device,
884 * so we should really check to see if there is a fullscreen swapchain
885 * already. Does a single head count as full screen? */
887 if (!desc->windowed)
889 struct wined3d_display_mode mode;
891 /* Change the display settings */
892 mode.width = desc->backbuffer_width;
893 mode.height = desc->backbuffer_height;
894 mode.format_id = desc->backbuffer_format;
895 mode.refresh_rate = desc->refresh_rate;
896 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
898 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
900 WARN("Failed to set display mode, hr %#x.\n", hr);
901 goto err;
903 displaymode_set = TRUE;
906 if (!(device->wined3d->flags & WINED3D_NO3D))
908 static const enum wined3d_format_id formats[] =
910 WINED3DFMT_D24_UNORM_S8_UINT,
911 WINED3DFMT_D32_UNORM,
912 WINED3DFMT_R24_UNORM_X8_TYPELESS,
913 WINED3DFMT_D16_UNORM,
914 WINED3DFMT_S1_UINT_D15_UNORM
917 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
919 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
920 if (!swapchain->context)
922 ERR("Failed to create the context array.\n");
923 hr = E_OUTOFMEMORY;
924 goto err;
926 swapchain->num_contexts = 1;
928 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
929 * You are able to add a depth + stencil surface at a later stage when you need it.
930 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
931 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
932 * context, need torecreate shaders, textures and other resources.
934 * The context manager already takes care of the state problem and for the other tasks code from Reset
935 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
936 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
937 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
938 * issue needs to be fixed. */
939 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
941 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
942 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
943 if (swapchain->context[0]) break;
944 TRACE("Depth stencil format %s is not supported, trying next format\n",
945 debug_d3dformat(formats[i]));
948 if (!swapchain->context[0])
950 WARN("Failed to create context.\n");
951 hr = WINED3DERR_NOTAVAILABLE;
952 goto err;
955 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
956 && (!desc->enable_auto_depth_stencil
957 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
959 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
961 context_release(swapchain->context[0]);
964 if (swapchain->desc.backbuffer_count > 0)
966 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
967 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
968 if (!swapchain->back_buffers)
970 ERR("Failed to allocate backbuffer array memory.\n");
971 hr = E_OUTOFMEMORY;
972 goto err;
975 surface_desc.usage |= WINED3DUSAGE_RENDERTARGET;
976 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
978 TRACE("Creating back buffer %u.\n", i);
979 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
980 parent, &surface_desc, &swapchain->back_buffers[i])))
982 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
983 goto err;
985 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
989 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
990 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
992 TRACE("Creating depth/stencil buffer.\n");
993 if (!device->auto_depth_stencil)
995 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
996 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
998 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
999 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1001 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1002 goto err;
1007 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1009 return WINED3D_OK;
1011 err:
1012 if (displaymode_set)
1014 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1015 adapter->ordinal, &swapchain->original_mode)))
1016 ERR("Failed to restore display mode.\n");
1017 ClipCursor(NULL);
1020 if (swapchain->back_buffers)
1022 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1024 if (swapchain->back_buffers[i])
1026 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1027 wined3d_surface_decref(swapchain->back_buffers[i]);
1030 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1033 if (swapchain->context)
1035 if (swapchain->context[0])
1037 context_release(swapchain->context[0]);
1038 context_destroy(device, swapchain->context[0]);
1039 swapchain->num_contexts = 0;
1041 HeapFree(GetProcessHeap(), 0, swapchain->context);
1044 if (swapchain->front_buffer)
1046 surface_set_swapchain(swapchain->front_buffer, NULL);
1047 wined3d_surface_decref(swapchain->front_buffer);
1050 return hr;
1053 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1054 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1056 struct wined3d_swapchain *object;
1057 HRESULT hr;
1059 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1060 device, desc, parent, parent_ops, swapchain);
1062 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1063 if (!object)
1064 return E_OUTOFMEMORY;
1066 hr = swapchain_init(object, device, desc, parent, parent_ops);
1067 if (FAILED(hr))
1069 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1070 HeapFree(GetProcessHeap(), 0, object);
1071 return hr;
1074 TRACE("Created swapchain %p.\n", object);
1075 *swapchain = object;
1077 return WINED3D_OK;
1080 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1082 struct wined3d_context **newArray;
1083 struct wined3d_context *ctx;
1085 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1087 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1089 ERR("Failed to create a new context for the swapchain\n");
1090 return NULL;
1092 context_release(ctx);
1094 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1095 if(!newArray) {
1096 ERR("Out of memory when trying to allocate a new context array\n");
1097 context_destroy(swapchain->device, ctx);
1098 return NULL;
1100 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1101 HeapFree(GetProcessHeap(), 0, swapchain->context);
1102 newArray[swapchain->num_contexts] = ctx;
1103 swapchain->context = newArray;
1104 swapchain->num_contexts++;
1106 TRACE("Returning context %p\n", ctx);
1107 return ctx;
1110 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1112 unsigned int i;
1114 for (i = 0; i < swapchain->num_contexts; ++i)
1116 context_destroy(swapchain->device, swapchain->context[i]);
1118 swapchain->num_contexts = 0;
1121 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1123 DWORD tid = GetCurrentThreadId();
1124 unsigned int i;
1126 for (i = 0; i < swapchain->num_contexts; ++i)
1128 if (swapchain->context[i]->tid == tid)
1129 return swapchain->context[i];
1132 /* Create a new context for the thread */
1133 return swapchain_create_context(swapchain);
1136 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1138 /* The drawable size of an onscreen drawable is the surface size.
1139 * (Actually: The window size, but the surface is created in window size) */
1140 *width = context->current_rt->resource.width;
1141 *height = context->current_rt->resource.height;
1144 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1146 if (!swapchain->backup_dc)
1148 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1150 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1151 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1153 ERR("Failed to create a window.\n");
1154 return NULL;
1157 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1159 ERR("Failed to get a DC.\n");
1160 DestroyWindow(swapchain->backup_wnd);
1161 swapchain->backup_wnd = NULL;
1162 return NULL;
1166 return swapchain->backup_dc;
1169 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1171 UINT i;
1173 surface_update_draw_binding(swapchain->front_buffer);
1175 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1177 surface_update_draw_binding(swapchain->back_buffers[i]);