wined3d: Implement mode setting in the adapter instead of the device.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blob3f93e8390a7f12124bf6ea78b04668dc8b400cd4
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 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
84 swapchain->device->adapter->ordinal, &mode)))
85 ERR("Failed to restore display mode, hr %#x.\n", hr);
88 if (swapchain->backup_dc)
90 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
92 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
93 DestroyWindow(swapchain->backup_wnd);
97 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
99 ULONG refcount = InterlockedIncrement(&swapchain->ref);
101 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
103 return refcount;
106 /* Do not call while under the GL lock. */
107 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
109 ULONG refcount = InterlockedDecrement(&swapchain->ref);
111 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
113 if (!refcount)
115 swapchain_cleanup(swapchain);
116 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
117 HeapFree(GetProcessHeap(), 0, swapchain);
120 return refcount;
123 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
125 TRACE("swapchain %p.\n", swapchain);
127 return swapchain->parent;
130 HRESULT CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
132 if (!window)
133 window = swapchain->device_window;
134 if (window == swapchain->win_handle)
135 return WINED3D_OK;
137 TRACE("Setting swapchain %p window from %p to %p.\n",
138 swapchain, swapchain->win_handle, window);
139 swapchain->win_handle = window;
141 return WINED3D_OK;
144 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
145 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
146 const RGNDATA *dirty_region, DWORD flags)
148 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
149 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
150 dst_window_override, dirty_region, flags);
152 if (!swapchain->back_buffers)
154 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
155 return WINED3DERR_INVALIDCALL;
158 wined3d_swapchain_set_window(swapchain, dst_window_override);
160 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
162 return WINED3D_OK;
165 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
166 struct wined3d_surface *dst_surface)
168 struct wined3d_surface *src_surface;
169 RECT src_rect, dst_rect;
171 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
173 src_surface = swapchain->front_buffer;
174 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
175 dst_rect = src_rect;
177 if (swapchain->desc.windowed)
179 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
180 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
181 wine_dbgstr_rect(&dst_rect));
184 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
187 HRESULT CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
188 UINT back_buffer_idx, enum wined3d_backbuffer_type type, struct wined3d_surface **back_buffer)
190 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
191 swapchain, back_buffer_idx, type, back_buffer);
193 /* Return invalid if there is no backbuffer array, otherwise it will
194 * crash when ddraw is used (there swapchain->back_buffers is always
195 * NULL). We need this because this function is called from
196 * stateblock_init_default_state() to get the default scissorrect
197 * dimensions. */
198 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
200 WARN("Invalid back buffer index.\n");
201 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
202 * here in wined3d to avoid problems in other libs. */
203 *back_buffer = NULL;
204 return WINED3DERR_INVALIDCALL;
207 *back_buffer = swapchain->back_buffers[back_buffer_idx];
208 if (*back_buffer)
209 wined3d_surface_incref(*back_buffer);
211 TRACE("Returning back buffer %p.\n", *back_buffer);
213 return WINED3D_OK;
216 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
217 struct wined3d_raster_status *raster_status)
219 static BOOL warned;
220 LARGE_INTEGER counter, freq_per_sec;
221 LONGLONG freq_per_frame, freq_per_line;
222 struct wined3d_display_mode mode;
224 /* No OpenGL equivalent */
225 if (!warned)
227 FIXME("swapchain %p, raster_status %p semi-stub!\n", swapchain, raster_status);
228 warned = TRUE;
231 /* Obtaining the raster status is a widely implemented but optional
232 * feature. When this method returns OK StarCraft 2 expects the
233 * raster_status->InVBlank value to actually change over time.
234 * And Endless Alice Crysis doesn't care even if this method fails.
235 * Thus this method returns OK and fakes raster_status by
236 * QueryPerformanceCounter. */
238 if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&freq_per_sec))
239 return WINED3DERR_INVALIDCALL;
241 if (FAILED(wined3d_swapchain_get_display_mode(swapchain, &mode)))
242 return WINED3DERR_INVALIDCALL;
243 if (mode.refresh_rate == DEFAULT_REFRESH_RATE)
244 mode.refresh_rate = 60;
246 freq_per_frame = freq_per_sec.QuadPart / mode.refresh_rate;
247 /* Assume 20 scan lines in the vertical blank */
248 freq_per_line = freq_per_frame / (mode.height + 20);
249 raster_status->scan_line = (counter.QuadPart % freq_per_frame) / freq_per_line;
250 if (raster_status->scan_line < mode.height)
251 raster_status->in_vblank = FALSE;
252 else
254 raster_status->scan_line = 0;
255 raster_status->in_vblank = TRUE;
257 TRACE("Returning fake value, in_vblank %u, scan_line %u.\n",
258 raster_status->in_vblank, raster_status->scan_line);
259 return WINED3D_OK;
262 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
263 struct wined3d_display_mode *mode)
265 HRESULT hr;
267 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
269 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d, swapchain->device->adapter->ordinal, mode);
271 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
272 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
274 return hr;
277 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
279 TRACE("swapchain %p.\n", swapchain);
281 return swapchain->device;
284 HRESULT CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
285 struct wined3d_swapchain_desc *desc)
287 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
289 *desc = swapchain->desc;
291 return WINED3D_OK;
294 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
295 DWORD flags, const struct wined3d_gamma_ramp *ramp)
297 HDC dc;
299 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
301 if (flags)
302 FIXME("Ignoring flags %#x.\n", flags);
304 dc = GetDC(swapchain->device_window);
305 SetDeviceGammaRamp(dc, (void *)ramp);
306 ReleaseDC(swapchain->device_window, dc);
308 return WINED3D_OK;
311 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
312 struct wined3d_gamma_ramp *ramp)
314 HDC dc;
316 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
318 dc = GetDC(swapchain->device_window);
319 GetDeviceGammaRamp(dc, ramp);
320 ReleaseDC(swapchain->device_window, dc);
322 return WINED3D_OK;
325 /* A GL context is provided by the caller */
326 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
327 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
329 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
330 UINT src_w = src_rect->right - src_rect->left;
331 UINT src_h = src_rect->bottom - src_rect->top;
332 GLenum gl_filter;
333 const struct wined3d_gl_info *gl_info = context->gl_info;
334 RECT win_rect;
335 UINT win_h;
337 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
338 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
340 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
341 gl_filter = GL_NEAREST;
342 else
343 gl_filter = GL_LINEAR;
345 GetClientRect(swapchain->win_handle, &win_rect);
346 win_h = win_rect.bottom - win_rect.top;
348 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
350 DWORD location = SFLAG_INTEXTURE;
352 if (backbuffer->resource.multisample_type)
354 location = SFLAG_INRB_RESOLVED;
355 surface_load_location(backbuffer, location, NULL);
358 ENTER_GL();
359 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
360 glReadBuffer(GL_COLOR_ATTACHMENT0);
361 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
363 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
364 context_set_draw_buffer(context, GL_BACK);
365 context_invalidate_state(context, STATE_FRAMEBUFFER);
367 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
369 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
370 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
371 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
373 glDisable(GL_SCISSOR_TEST);
374 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
376 /* Note that the texture is upside down */
377 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
378 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
379 GL_COLOR_BUFFER_BIT, gl_filter);
380 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
381 LEAVE_GL();
383 else
385 struct wined3d_device *device = swapchain->device;
386 struct wined3d_context *context2;
387 float tex_left = src_rect->left;
388 float tex_top = src_rect->top;
389 float tex_right = src_rect->right;
390 float tex_bottom = src_rect->bottom;
392 context2 = context_acquire(device, swapchain->back_buffers[0]);
393 context_apply_blit_state(context2, device);
395 if (backbuffer->flags & SFLAG_NORMCOORD)
397 tex_left /= src_w;
398 tex_right /= src_w;
399 tex_top /= src_h;
400 tex_bottom /= src_h;
403 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
404 gl_filter = GL_NEAREST;
406 ENTER_GL();
407 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
409 /* Set up the texture. The surface is not in a wined3d_texture
410 * container, so there are no D3D texture settings to dirtify. */
411 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
412 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
413 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
415 context_set_draw_buffer(context, GL_BACK);
417 /* Set the viewport to the destination rectandle, disable any projection
418 * transformation set up by context_apply_blit_state(), and draw a
419 * (-1,-1)-(1,1) quad.
421 * Back up viewport and matrix to avoid breaking last_was_blit
423 * Note that context_apply_blit_state() set up viewport and ortho to
424 * match the surface size - we want the GL drawable(=window) size. */
425 glPushAttrib(GL_VIEWPORT_BIT);
426 glViewport(dst_rect->left, win_h - dst_rect->bottom, dst_rect->right, win_h - dst_rect->top);
427 glMatrixMode(GL_PROJECTION);
428 glPushMatrix();
429 glLoadIdentity();
431 glBegin(GL_QUADS);
432 /* bottom left */
433 glTexCoord2f(tex_left, tex_bottom);
434 glVertex2i(-1, -1);
436 /* top left */
437 glTexCoord2f(tex_left, tex_top);
438 glVertex2i(-1, 1);
440 /* top right */
441 glTexCoord2f(tex_right, tex_top);
442 glVertex2i(1, 1);
444 /* bottom right */
445 glTexCoord2f(tex_right, tex_bottom);
446 glVertex2i(1, -1);
447 glEnd();
449 glPopMatrix();
450 glPopAttrib();
452 device->blitter->unset_shader(context->gl_info);
453 checkGLcall("Swapchain present blit(manual)\n");
454 LEAVE_GL();
456 context_release(context2);
460 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
461 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
463 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
464 const struct wined3d_fb_state *fb = &swapchain->device->fb;
465 const struct wined3d_gl_info *gl_info;
466 struct wined3d_context *context;
467 RECT src_rect, dst_rect;
468 BOOL render_to_fbo;
470 context = context_acquire(swapchain->device, back_buffer);
471 if (!context->valid)
473 context_release(context);
474 WARN("Invalid context, skipping present.\n");
475 return;
478 gl_info = context->gl_info;
480 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
481 if (swapchain->device->bCursorVisible &&
482 swapchain->device->cursorTexture &&
483 !swapchain->device->hardwareCursor)
485 struct wined3d_surface cursor;
486 RECT destRect =
488 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
489 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
490 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
491 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
493 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
494 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
495 * the application because we are only supposed to copy the information out. Using a fake surface
496 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
498 memset(&cursor, 0, sizeof(cursor));
499 cursor.resource.ref = 1;
500 cursor.resource.device = swapchain->device;
501 cursor.resource.pool = WINED3D_POOL_SCRATCH;
502 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
503 cursor.resource.type = WINED3D_RTYPE_SURFACE;
504 cursor.texture_name = swapchain->device->cursorTexture;
505 cursor.texture_target = GL_TEXTURE_2D;
506 cursor.texture_level = 0;
507 cursor.resource.width = swapchain->device->cursorWidth;
508 cursor.resource.height = swapchain->device->cursorHeight;
509 /* The cursor must have pow2 sizes */
510 cursor.pow2Width = cursor.resource.width;
511 cursor.pow2Height = cursor.resource.height;
512 /* The surface is in the texture */
513 cursor.flags |= SFLAG_INTEXTURE;
514 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
515 * which is exactly what we want :-)
517 if (swapchain->desc.windowed)
518 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
519 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
520 NULL, WINED3D_TEXF_POINT);
523 if (swapchain->device->logo_surface)
525 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
526 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
528 /* Blit the logo into the upper left corner of the drawable. */
529 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
530 NULL, WINED3D_TEXF_POINT);
533 TRACE("Presenting HDC %p.\n", context->hdc);
535 render_to_fbo = swapchain->render_to_fbo;
537 if (src_rect_in)
539 src_rect = *src_rect_in;
540 if (!render_to_fbo && (src_rect.left || src_rect.top
541 || src_rect.right != swapchain->desc.backbuffer_width
542 || src_rect.bottom != swapchain->desc.backbuffer_height))
544 render_to_fbo = TRUE;
547 else
549 src_rect.left = 0;
550 src_rect.top = 0;
551 src_rect.right = swapchain->desc.backbuffer_width;
552 src_rect.bottom = swapchain->desc.backbuffer_height;
555 if (dst_rect_in)
556 dst_rect = *dst_rect_in;
557 else
558 GetClientRect(swapchain->win_handle, &dst_rect);
560 if (!render_to_fbo && (dst_rect.left || dst_rect.top
561 || dst_rect.right != swapchain->desc.backbuffer_width
562 || dst_rect.bottom != swapchain->desc.backbuffer_height))
563 render_to_fbo = TRUE;
565 /* Rendering to a window of different size, presenting partial rectangles,
566 * or rendering to a different window needs help from FBO_blit or a textured
567 * draw. Render the swapchain to a FBO in the future.
569 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
570 * all these issues - this fails if the window is smaller than the backbuffer.
572 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
574 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
575 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
576 swapchain->render_to_fbo = TRUE;
577 swapchain_update_draw_bindings(swapchain);
579 else
581 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
584 if (swapchain->render_to_fbo)
586 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
587 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
588 * not allowed(they need the COPY swapeffect)
590 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
591 * the swap. */
592 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
593 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
595 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
598 if (swapchain->num_contexts > 1)
599 wglFinish();
600 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
602 TRACE("SwapBuffers called, Starting new frame\n");
603 /* FPS support */
604 if (TRACE_ON(fps))
606 DWORD time = GetTickCount();
607 ++swapchain->frames;
609 /* every 1.5 seconds */
610 if (time - swapchain->prev_time > 1500)
612 TRACE_(fps)("%p @ approx %.2ffps\n",
613 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
614 swapchain->prev_time = time;
615 swapchain->frames = 0;
619 /* This is disabled, but the code left in for debug purposes.
621 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
622 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
623 * The Debug runtime does the same on Windows. However, a few games do not redraw the
624 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
626 * Tests show that the content of the back buffer after a discard flip is indeed not
627 * reliable, so no game can depend on the exact content. However, it resembles the
628 * old contents in some way, for example by showing fragments at other locations. In
629 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
630 * gets a dark background image. If we clear it with a bright ugly color, the game's
631 * bug shows up much more than it does on Windows, and the players see single pixels
632 * with wrong colors.
633 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
634 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
636 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
638 TRACE("Clearing the color buffer with cyan color\n");
640 wined3d_device_clear(swapchain->device, 0, NULL,
641 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
644 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
645 || (back_buffer->flags & SFLAG_INSYSMEM)))
647 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
648 * Doesn't work with render_to_fbo because we're not flipping
650 struct wined3d_surface *front = swapchain->front_buffer;
652 if (front->resource.size == back_buffer->resource.size)
654 DWORD fbflags;
655 flip_surface(front, back_buffer);
657 /* Tell the front buffer surface that is has been modified. However,
658 * the other locations were preserved during that, so keep the flags.
659 * This serves to update the emulated overlay, if any. */
660 fbflags = front->flags;
661 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
662 front->flags = fbflags;
664 else
666 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
667 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
670 else
672 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
673 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
674 * and INTEXTURE copies can keep their old content if they have any defined content.
675 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
676 * the texture / sysmem copy needs to be reloaded from the drawable
678 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
679 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
682 if (fb->depth_stencil)
684 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
685 || fb->depth_stencil->flags & SFLAG_DISCARD)
687 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
688 fb->depth_stencil->resource.width,
689 fb->depth_stencil->resource.height);
690 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
692 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
693 swapchain->device->onscreen_depth_stencil = NULL;
698 context_release(context);
701 static const struct wined3d_swapchain_ops swapchain_gl_ops =
703 swapchain_gl_present,
706 /* Helper function that blits the front buffer contents to the target window. */
707 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
709 const struct wined3d_surface *front;
710 POINT offset = {0, 0};
711 HDC src_dc, dst_dc;
712 RECT draw_rect;
713 HWND window;
715 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
717 front = swapchain->front_buffer;
718 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
719 return;
721 if (front->resource.map_count)
722 ERR("Trying to blit a mapped surface.\n");
724 TRACE("Copying surface %p to screen.\n", front);
726 src_dc = front->hDC;
727 window = swapchain->win_handle;
728 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
730 /* Front buffer coordinates are screen coordinates. Map them to the
731 * destination window if not fullscreened. */
732 if (swapchain->desc.windowed)
733 ClientToScreen(window, &offset);
735 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
737 draw_rect.left = 0;
738 draw_rect.right = front->resource.width;
739 draw_rect.top = 0;
740 draw_rect.bottom = front->resource.height;
742 if (rect)
743 IntersectRect(&draw_rect, &draw_rect, rect);
745 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
746 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
747 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
748 ReleaseDC(window, dst_dc);
751 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
752 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
754 struct wined3d_surface *front, *back;
756 front = swapchain->front_buffer;
757 back = swapchain->back_buffers[0];
759 /* Flip the DC. */
761 HDC tmp;
762 tmp = front->hDC;
763 front->hDC = back->hDC;
764 back->hDC = tmp;
767 /* Flip the DIBsection. */
769 HBITMAP tmp;
770 tmp = front->dib.DIBsection;
771 front->dib.DIBsection = back->dib.DIBsection;
772 back->dib.DIBsection = tmp;
775 /* Flip the surface data. */
777 void *tmp;
779 tmp = front->dib.bitmap_data;
780 front->dib.bitmap_data = back->dib.bitmap_data;
781 back->dib.bitmap_data = tmp;
783 tmp = front->resource.allocatedMemory;
784 front->resource.allocatedMemory = back->resource.allocatedMemory;
785 back->resource.allocatedMemory = tmp;
787 if (front->resource.heapMemory)
788 ERR("GDI Surface %p has heap memory allocated.\n", front);
790 if (back->resource.heapMemory)
791 ERR("GDI Surface %p has heap memory allocated.\n", back);
794 /* FPS support */
795 if (TRACE_ON(fps))
797 static LONG prev_time, frames;
798 DWORD time = GetTickCount();
800 ++frames;
802 /* every 1.5 seconds */
803 if (time - prev_time > 1500)
805 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
806 prev_time = time;
807 frames = 0;
811 x11_copy_to_screen(swapchain, NULL);
814 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
816 swapchain_gdi_present,
819 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
821 RECT client_rect;
823 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
824 return;
826 if (!swapchain->desc.backbuffer_count)
828 TRACE("Single buffered rendering.\n");
829 swapchain->render_to_fbo = FALSE;
830 return;
833 GetClientRect(swapchain->win_handle, &client_rect);
835 TRACE("Backbuffer %ux%u, window %ux%u.\n",
836 swapchain->desc.backbuffer_width,
837 swapchain->desc.backbuffer_height,
838 client_rect.right, client_rect.bottom);
839 TRACE("Multisample type %#x, quality %#x.\n",
840 swapchain->desc.multisample_type,
841 swapchain->desc.multisample_quality);
843 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
844 && swapchain->desc.backbuffer_width == client_rect.right
845 && swapchain->desc.backbuffer_height == client_rect.bottom)
847 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
848 swapchain->render_to_fbo = FALSE;
849 return;
852 TRACE("Rendering to FBO.\n");
853 swapchain->render_to_fbo = TRUE;
856 /* Do not call while under the GL lock. */
857 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, enum wined3d_surface_type surface_type,
858 struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
859 void *parent, const struct wined3d_parent_ops *parent_ops)
861 const struct wined3d_adapter *adapter = device->adapter;
862 const struct wined3d_format *format;
863 struct wined3d_display_mode mode;
864 BOOL displaymode_set = FALSE;
865 RECT client_rect;
866 HWND window;
867 HRESULT hr;
868 UINT i;
870 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
872 FIXME("The application requested %u back buffers, this is not supported.\n",
873 desc->backbuffer_count);
874 return WINED3DERR_INVALIDCALL;
877 if (desc->backbuffer_count > 1)
879 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
880 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
883 switch (surface_type)
885 case WINED3D_SURFACE_TYPE_GDI:
886 swapchain->swapchain_ops = &swapchain_gdi_ops;
887 break;
889 case WINED3D_SURFACE_TYPE_OPENGL:
890 swapchain->swapchain_ops = &swapchain_gl_ops;
891 break;
893 default:
894 ERR("Invalid surface type %#x.\n", surface_type);
895 return WINED3DERR_INVALIDCALL;
898 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
900 swapchain->device = device;
901 swapchain->parent = parent;
902 swapchain->parent_ops = parent_ops;
903 swapchain->ref = 1;
904 swapchain->win_handle = window;
905 swapchain->device_window = window;
907 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode);
908 swapchain->orig_width = mode.width;
909 swapchain->orig_height = mode.height;
910 swapchain->orig_fmt = mode.format_id;
911 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
913 GetClientRect(window, &client_rect);
914 if (desc->windowed
915 && (!desc->backbuffer_width || !desc->backbuffer_height
916 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
919 if (!desc->backbuffer_width)
921 desc->backbuffer_width = client_rect.right;
922 TRACE("Updating width to %u.\n", desc->backbuffer_width);
925 if (!desc->backbuffer_height)
927 desc->backbuffer_height = client_rect.bottom;
928 TRACE("Updating height to %u.\n", desc->backbuffer_height);
931 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
933 desc->backbuffer_format = swapchain->orig_fmt;
934 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
937 swapchain->desc = *desc;
938 swapchain_update_render_to_fbo(swapchain);
940 TRACE("Creating front buffer.\n");
941 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
942 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
943 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
944 swapchain->desc.multisample_quality, TRUE /* Lockable */,
945 &swapchain->front_buffer);
946 if (FAILED(hr))
948 WARN("Failed to create front buffer, hr %#x.\n", hr);
949 goto err;
952 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_SWAPCHAIN, swapchain);
953 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
954 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
956 /* MSDN says we're only allowed a single fullscreen swapchain per device,
957 * so we should really check to see if there is a fullscreen swapchain
958 * already. Does a single head count as full screen? */
960 if (!desc->windowed)
962 struct wined3d_display_mode mode;
964 /* Change the display settings */
965 mode.width = desc->backbuffer_width;
966 mode.height = desc->backbuffer_height;
967 mode.format_id = desc->backbuffer_format;
968 mode.refresh_rate = desc->refresh_rate;
970 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode)))
972 WARN("Failed to set display mode, hr %#x.\n", hr);
973 goto err;
975 displaymode_set = TRUE;
978 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
980 static const enum wined3d_format_id formats[] =
982 WINED3DFMT_D24_UNORM_S8_UINT,
983 WINED3DFMT_D32_UNORM,
984 WINED3DFMT_R24_UNORM_X8_TYPELESS,
985 WINED3DFMT_D16_UNORM,
986 WINED3DFMT_S1_UINT_D15_UNORM
989 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
991 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
992 if (!swapchain->context)
994 ERR("Failed to create the context array.\n");
995 hr = E_OUTOFMEMORY;
996 goto err;
998 swapchain->num_contexts = 1;
1000 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
1001 * You are able to add a depth + stencil surface at a later stage when you need it.
1002 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1003 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1004 * context, need torecreate shaders, textures and other resources.
1006 * The context manager already takes care of the state problem and for the other tasks code from Reset
1007 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1008 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1009 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1010 * issue needs to be fixed. */
1011 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
1013 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
1014 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
1015 if (swapchain->context[0]) break;
1016 TRACE("Depth stencil format %s is not supported, trying next format\n",
1017 debug_d3dformat(formats[i]));
1020 if (!swapchain->context[0])
1022 WARN("Failed to create context.\n");
1023 hr = WINED3DERR_NOTAVAILABLE;
1024 goto err;
1027 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
1028 && (!desc->enable_auto_depth_stencil
1029 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
1031 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1033 context_release(swapchain->context[0]);
1036 if (swapchain->desc.backbuffer_count > 0)
1038 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1039 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1040 if (!swapchain->back_buffers)
1042 ERR("Failed to allocate backbuffer array memory.\n");
1043 hr = E_OUTOFMEMORY;
1044 goto err;
1047 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1049 TRACE("Creating back buffer %u.\n", i);
1050 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
1051 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1052 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
1053 swapchain->desc.multisample_quality, TRUE /* Lockable */,
1054 &swapchain->back_buffers[i]);
1055 if (FAILED(hr))
1057 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1058 goto err;
1061 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_SWAPCHAIN, swapchain);
1065 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1066 if (desc->enable_auto_depth_stencil && surface_type == WINED3D_SURFACE_TYPE_OPENGL)
1068 TRACE("Creating depth/stencil buffer.\n");
1069 if (!device->auto_depth_stencil)
1071 hr = device->device_parent->ops->create_depth_stencil(device->device_parent,
1072 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1073 swapchain->desc.auto_depth_stencil_format, swapchain->desc.multisample_type,
1074 swapchain->desc.multisample_quality, FALSE /* FIXME: Discard */,
1075 &device->auto_depth_stencil);
1076 if (FAILED(hr))
1078 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1079 goto err;
1082 surface_set_container(device->auto_depth_stencil, WINED3D_CONTAINER_NONE, NULL);
1086 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1088 return WINED3D_OK;
1090 err:
1091 if (displaymode_set)
1093 DEVMODEW devmode;
1095 ClipCursor(NULL);
1097 /* Change the display settings */
1098 memset(&devmode, 0, sizeof(devmode));
1099 devmode.dmSize = sizeof(devmode);
1100 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1101 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1102 devmode.dmPelsWidth = swapchain->orig_width;
1103 devmode.dmPelsHeight = swapchain->orig_height;
1104 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1107 if (swapchain->back_buffers)
1109 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1111 if (swapchain->back_buffers[i])
1113 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
1114 wined3d_surface_decref(swapchain->back_buffers[i]);
1117 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1120 if (swapchain->context)
1122 if (swapchain->context[0])
1124 context_release(swapchain->context[0]);
1125 context_destroy(device, swapchain->context[0]);
1126 swapchain->num_contexts = 0;
1128 HeapFree(GetProcessHeap(), 0, swapchain->context);
1131 if (swapchain->front_buffer)
1133 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
1134 wined3d_surface_decref(swapchain->front_buffer);
1137 return hr;
1140 /* Do not call while under the GL lock. */
1141 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1142 struct wined3d_swapchain_desc *desc, enum wined3d_surface_type surface_type,
1143 void *parent, const struct wined3d_parent_ops *parent_ops,
1144 struct wined3d_swapchain **swapchain)
1146 struct wined3d_swapchain *object;
1147 HRESULT hr;
1149 TRACE("device %p, desc %p, swapchain %p, parent %p, surface_type %#x.\n",
1150 device, desc, swapchain, parent, surface_type);
1152 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1153 if (!object)
1155 ERR("Failed to allocate swapchain memory.\n");
1156 return E_OUTOFMEMORY;
1159 hr = swapchain_init(object, surface_type, device, desc, parent, parent_ops);
1160 if (FAILED(hr))
1162 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1163 HeapFree(GetProcessHeap(), 0, object);
1164 return hr;
1167 TRACE("Created swapchain %p.\n", object);
1168 *swapchain = object;
1170 return WINED3D_OK;
1173 /* Do not call while under the GL lock. */
1174 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1176 struct wined3d_context **newArray;
1177 struct wined3d_context *ctx;
1179 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1181 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1183 ERR("Failed to create a new context for the swapchain\n");
1184 return NULL;
1186 context_release(ctx);
1188 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1189 if(!newArray) {
1190 ERR("Out of memory when trying to allocate a new context array\n");
1191 context_destroy(swapchain->device, ctx);
1192 return NULL;
1194 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1195 HeapFree(GetProcessHeap(), 0, swapchain->context);
1196 newArray[swapchain->num_contexts] = ctx;
1197 swapchain->context = newArray;
1198 swapchain->num_contexts++;
1200 TRACE("Returning context %p\n", ctx);
1201 return ctx;
1204 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1206 unsigned int i;
1208 for (i = 0; i < swapchain->num_contexts; ++i)
1210 context_destroy(swapchain->device, swapchain->context[i]);
1212 swapchain->num_contexts = 0;
1215 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1217 DWORD tid = GetCurrentThreadId();
1218 unsigned int i;
1220 for (i = 0; i < swapchain->num_contexts; ++i)
1222 if (swapchain->context[i]->tid == tid)
1223 return swapchain->context[i];
1226 /* Create a new context for the thread */
1227 return swapchain_create_context(swapchain);
1230 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1232 /* The drawable size of an onscreen drawable is the surface size.
1233 * (Actually: The window size, but the surface is created in window size) */
1234 *width = context->current_rt->resource.width;
1235 *height = context->current_rt->resource.height;
1238 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1240 if (!swapchain->backup_dc)
1242 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1244 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1245 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1247 ERR("Failed to create a window.\n");
1248 return NULL;
1251 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1253 ERR("Failed to get a DC.\n");
1254 DestroyWindow(swapchain->backup_wnd);
1255 swapchain->backup_wnd = NULL;
1256 return NULL;
1260 return swapchain->backup_dc;
1263 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1265 UINT i;
1267 surface_update_draw_binding(swapchain->front_buffer);
1269 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1271 surface_update_draw_binding(swapchain->back_buffers[i]);