msxml3/tests: Fix the expected values in some ok() messages.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blob6600806b9141f475a84e52d416b7bd74ef83f2b4
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 UINT i;
36 TRACE("Destroying swapchain %p.\n", swapchain);
38 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain->front_buffer)
44 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
45 if (wined3d_surface_decref(swapchain->front_buffer))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
47 swapchain->front_buffer = NULL;
50 if (swapchain->back_buffers)
52 i = swapchain->desc.backbuffer_count;
54 while (i--)
56 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
57 if (wined3d_surface_decref(swapchain->back_buffers[i]))
58 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
60 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
61 swapchain->back_buffers = NULL;
64 for (i = 0; i < swapchain->num_contexts; ++i)
66 context_destroy(swapchain->device, swapchain->context[i]);
68 HeapFree(GetProcessHeap(), 0, swapchain->context);
70 /* Restore the screen resolution if we rendered in fullscreen.
71 * This will restore the screen resolution to what it was before creating
72 * the swapchain. In case of d3d8 and d3d9 this will be the original
73 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
74 * sets the resolution before starting up Direct3D, thus orig_width and
75 * orig_height will be equal to the modes in the presentation params. */
76 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
78 mode.width = swapchain->orig_width;
79 mode.height = swapchain->orig_height;
80 mode.refresh_rate = 0;
81 mode.format_id = swapchain->orig_fmt;
82 wined3d_device_set_display_mode(swapchain->device, 0, &mode);
85 if (swapchain->backup_dc)
87 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
89 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
90 DestroyWindow(swapchain->backup_wnd);
94 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
96 ULONG refcount = InterlockedIncrement(&swapchain->ref);
98 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
100 return refcount;
103 /* Do not call while under the GL lock. */
104 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
106 ULONG refcount = InterlockedDecrement(&swapchain->ref);
108 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
110 if (!refcount)
112 swapchain_cleanup(swapchain);
113 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
114 HeapFree(GetProcessHeap(), 0, swapchain);
117 return refcount;
120 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
122 TRACE("swapchain %p.\n", swapchain);
124 return swapchain->parent;
127 HRESULT CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
129 if (!window)
130 window = swapchain->device_window;
131 if (window == swapchain->win_handle)
132 return WINED3D_OK;
134 TRACE("Setting swapchain %p window from %p to %p.\n",
135 swapchain, swapchain->win_handle, window);
136 swapchain->win_handle = window;
138 return WINED3D_OK;
141 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
142 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
143 const RGNDATA *dirty_region, DWORD flags)
145 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
146 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
147 dst_window_override, dirty_region, flags);
149 if (!swapchain->back_buffers)
151 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
152 return WINED3DERR_INVALIDCALL;
155 wined3d_swapchain_set_window(swapchain, dst_window_override);
157 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
159 return WINED3D_OK;
162 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
163 struct wined3d_surface *dst_surface)
165 struct wined3d_surface *src_surface;
166 RECT src_rect, dst_rect;
168 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
170 src_surface = swapchain->front_buffer;
171 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
172 dst_rect = src_rect;
174 if (swapchain->desc.windowed)
176 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
177 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
178 wine_dbgstr_rect(&dst_rect));
181 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
184 HRESULT CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
185 UINT back_buffer_idx, enum wined3d_backbuffer_type type, struct wined3d_surface **back_buffer)
187 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
188 swapchain, back_buffer_idx, type, back_buffer);
190 /* Return invalid if there is no backbuffer array, otherwise it will
191 * crash when ddraw is used (there swapchain->back_buffers is always
192 * NULL). We need this because this function is called from
193 * stateblock_init_default_state() to get the default scissorrect
194 * dimensions. */
195 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
197 WARN("Invalid back buffer index.\n");
198 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
199 * here in wined3d to avoid problems in other libs. */
200 *back_buffer = NULL;
201 return WINED3DERR_INVALIDCALL;
204 *back_buffer = swapchain->back_buffers[back_buffer_idx];
205 if (*back_buffer)
206 wined3d_surface_incref(*back_buffer);
208 TRACE("Returning back buffer %p.\n", *back_buffer);
210 return WINED3D_OK;
213 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
214 struct wined3d_raster_status *raster_status)
216 static BOOL warned;
217 LARGE_INTEGER counter, freq_per_sec;
218 LONGLONG freq_per_frame, freq_per_line;
219 struct wined3d_display_mode mode;
221 /* No OpenGL equivalent */
222 if (!warned)
224 FIXME("swapchain %p, raster_status %p semi-stub!\n", swapchain, raster_status);
225 warned = TRUE;
228 /* Obtaining the raster status is a widely implemented but optional
229 * feature. When this method returns OK StarCraft 2 expects the
230 * raster_status->InVBlank value to actually change over time.
231 * And Endless Alice Crysis doesn't care even if this method fails.
232 * Thus this method returns OK and fakes raster_status by
233 * QueryPerformanceCounter. */
235 if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&freq_per_sec))
236 return WINED3DERR_INVALIDCALL;
238 if (FAILED(wined3d_swapchain_get_display_mode(swapchain, &mode)))
239 return WINED3DERR_INVALIDCALL;
240 if (mode.refresh_rate == DEFAULT_REFRESH_RATE)
241 mode.refresh_rate = 60;
243 freq_per_frame = freq_per_sec.QuadPart / mode.refresh_rate;
244 /* Assume 20 scan lines in the vertical blank */
245 freq_per_line = freq_per_frame / (mode.height + 20);
246 raster_status->scan_line = (counter.QuadPart % freq_per_frame) / freq_per_line;
247 if (raster_status->scan_line < mode.height)
248 raster_status->in_vblank = FALSE;
249 else
251 raster_status->scan_line = 0;
252 raster_status->in_vblank = TRUE;
254 TRACE("Returning fake value, in_vblank %u, scan_line %u.\n",
255 raster_status->in_vblank, raster_status->scan_line);
256 return WINED3D_OK;
259 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
260 struct wined3d_display_mode *mode)
262 HRESULT hr;
264 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
266 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d, swapchain->device->adapter->ordinal, mode);
268 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
269 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
271 return hr;
274 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
276 TRACE("swapchain %p.\n", swapchain);
278 return swapchain->device;
281 HRESULT CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
282 struct wined3d_swapchain_desc *desc)
284 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
286 *desc = swapchain->desc;
288 return WINED3D_OK;
291 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
292 DWORD flags, const struct wined3d_gamma_ramp *ramp)
294 HDC dc;
296 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
298 if (flags)
299 FIXME("Ignoring flags %#x.\n", flags);
301 dc = GetDC(swapchain->device_window);
302 SetDeviceGammaRamp(dc, (void *)ramp);
303 ReleaseDC(swapchain->device_window, dc);
305 return WINED3D_OK;
308 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
309 struct wined3d_gamma_ramp *ramp)
311 HDC dc;
313 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
315 dc = GetDC(swapchain->device_window);
316 GetDeviceGammaRamp(dc, ramp);
317 ReleaseDC(swapchain->device_window, dc);
319 return WINED3D_OK;
322 /* A GL context is provided by the caller */
323 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
324 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
326 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
327 UINT src_w = src_rect->right - src_rect->left;
328 UINT src_h = src_rect->bottom - src_rect->top;
329 GLenum gl_filter;
330 const struct wined3d_gl_info *gl_info = context->gl_info;
331 RECT win_rect;
332 UINT win_h;
334 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
335 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
337 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
338 gl_filter = GL_NEAREST;
339 else
340 gl_filter = GL_LINEAR;
342 GetClientRect(swapchain->win_handle, &win_rect);
343 win_h = win_rect.bottom - win_rect.top;
345 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
347 DWORD location = SFLAG_INTEXTURE;
349 if (backbuffer->resource.multisample_type)
351 location = SFLAG_INRB_RESOLVED;
352 surface_load_location(backbuffer, location, NULL);
355 ENTER_GL();
356 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
357 glReadBuffer(GL_COLOR_ATTACHMENT0);
358 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
360 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
361 context_set_draw_buffer(context, GL_BACK);
362 context_invalidate_state(context, STATE_FRAMEBUFFER);
364 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
366 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
367 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
370 glDisable(GL_SCISSOR_TEST);
371 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
373 /* Note that the texture is upside down */
374 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
375 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
376 GL_COLOR_BUFFER_BIT, gl_filter);
377 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
378 LEAVE_GL();
380 else
382 struct wined3d_device *device = swapchain->device;
383 struct wined3d_context *context2;
384 float tex_left = src_rect->left;
385 float tex_top = src_rect->top;
386 float tex_right = src_rect->right;
387 float tex_bottom = src_rect->bottom;
389 context2 = context_acquire(device, swapchain->back_buffers[0]);
390 context_apply_blit_state(context2, device);
392 if (backbuffer->flags & SFLAG_NORMCOORD)
394 tex_left /= src_w;
395 tex_right /= src_w;
396 tex_top /= src_h;
397 tex_bottom /= src_h;
400 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
401 gl_filter = GL_NEAREST;
403 ENTER_GL();
404 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
406 /* Set up the texture. The surface is not in a wined3d_texture
407 * container, so there are no D3D texture settings to dirtify. */
408 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
409 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
410 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
412 context_set_draw_buffer(context, GL_BACK);
414 /* Set the viewport to the destination rectandle, disable any projection
415 * transformation set up by context_apply_blit_state(), and draw a
416 * (-1,-1)-(1,1) quad.
418 * Back up viewport and matrix to avoid breaking last_was_blit
420 * Note that context_apply_blit_state() set up viewport and ortho to
421 * match the surface size - we want the GL drawable(=window) size. */
422 glPushAttrib(GL_VIEWPORT_BIT);
423 glViewport(dst_rect->left, win_h - dst_rect->bottom, dst_rect->right, win_h - dst_rect->top);
424 glMatrixMode(GL_PROJECTION);
425 glPushMatrix();
426 glLoadIdentity();
428 glBegin(GL_QUADS);
429 /* bottom left */
430 glTexCoord2f(tex_left, tex_bottom);
431 glVertex2i(-1, -1);
433 /* top left */
434 glTexCoord2f(tex_left, tex_top);
435 glVertex2i(-1, 1);
437 /* top right */
438 glTexCoord2f(tex_right, tex_top);
439 glVertex2i(1, 1);
441 /* bottom right */
442 glTexCoord2f(tex_right, tex_bottom);
443 glVertex2i(1, -1);
444 glEnd();
446 glPopMatrix();
447 glPopAttrib();
449 device->blitter->unset_shader(context->gl_info);
450 checkGLcall("Swapchain present blit(manual)\n");
451 LEAVE_GL();
453 context_release(context2);
457 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
458 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
460 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
461 const struct wined3d_fb_state *fb = &swapchain->device->fb;
462 const struct wined3d_gl_info *gl_info;
463 struct wined3d_context *context;
464 RECT src_rect, dst_rect;
465 BOOL render_to_fbo;
467 context = context_acquire(swapchain->device, back_buffer);
468 if (!context->valid)
470 context_release(context);
471 WARN("Invalid context, skipping present.\n");
472 return;
475 gl_info = context->gl_info;
477 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
478 if (swapchain->device->bCursorVisible &&
479 swapchain->device->cursorTexture &&
480 !swapchain->device->hardwareCursor)
482 struct wined3d_surface cursor;
483 RECT destRect =
485 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
486 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
487 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
488 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
490 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
491 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
492 * the application because we are only supposed to copy the information out. Using a fake surface
493 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
495 memset(&cursor, 0, sizeof(cursor));
496 cursor.resource.ref = 1;
497 cursor.resource.device = swapchain->device;
498 cursor.resource.pool = WINED3D_POOL_SCRATCH;
499 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
500 cursor.resource.type = WINED3D_RTYPE_SURFACE;
501 cursor.texture_name = swapchain->device->cursorTexture;
502 cursor.texture_target = GL_TEXTURE_2D;
503 cursor.texture_level = 0;
504 cursor.resource.width = swapchain->device->cursorWidth;
505 cursor.resource.height = swapchain->device->cursorHeight;
506 /* The cursor must have pow2 sizes */
507 cursor.pow2Width = cursor.resource.width;
508 cursor.pow2Height = cursor.resource.height;
509 /* The surface is in the texture */
510 cursor.flags |= SFLAG_INTEXTURE;
511 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
512 * which is exactly what we want :-)
514 if (swapchain->desc.windowed)
515 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
516 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
517 NULL, WINED3D_TEXF_POINT);
520 if (swapchain->device->logo_surface)
522 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
523 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
525 /* Blit the logo into the upper left corner of the drawable. */
526 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
527 NULL, WINED3D_TEXF_POINT);
530 TRACE("Presenting HDC %p.\n", context->hdc);
532 render_to_fbo = swapchain->render_to_fbo;
534 if (src_rect_in)
536 src_rect = *src_rect_in;
537 if (!render_to_fbo && (src_rect.left || src_rect.top
538 || src_rect.right != swapchain->desc.backbuffer_width
539 || src_rect.bottom != swapchain->desc.backbuffer_height))
541 render_to_fbo = TRUE;
544 else
546 src_rect.left = 0;
547 src_rect.top = 0;
548 src_rect.right = swapchain->desc.backbuffer_width;
549 src_rect.bottom = swapchain->desc.backbuffer_height;
552 if (dst_rect_in)
553 dst_rect = *dst_rect_in;
554 else
555 GetClientRect(swapchain->win_handle, &dst_rect);
557 if (!render_to_fbo && (dst_rect.left || dst_rect.top
558 || dst_rect.right != swapchain->desc.backbuffer_width
559 || dst_rect.bottom != swapchain->desc.backbuffer_height))
560 render_to_fbo = TRUE;
562 /* Rendering to a window of different size, presenting partial rectangles,
563 * or rendering to a different window needs help from FBO_blit or a textured
564 * draw. Render the swapchain to a FBO in the future.
566 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
567 * all these issues - this fails if the window is smaller than the backbuffer.
569 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
571 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
572 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
573 swapchain->render_to_fbo = TRUE;
574 swapchain_update_draw_bindings(swapchain);
576 else
578 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
581 if (swapchain->render_to_fbo)
583 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
584 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
585 * not allowed(they need the COPY swapeffect)
587 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
588 * the swap. */
589 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
590 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
592 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
595 if (swapchain->num_contexts > 1)
596 wglFinish();
597 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
599 TRACE("SwapBuffers called, Starting new frame\n");
600 /* FPS support */
601 if (TRACE_ON(fps))
603 DWORD time = GetTickCount();
604 ++swapchain->frames;
606 /* every 1.5 seconds */
607 if (time - swapchain->prev_time > 1500)
609 TRACE_(fps)("%p @ approx %.2ffps\n",
610 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
611 swapchain->prev_time = time;
612 swapchain->frames = 0;
616 /* This is disabled, but the code left in for debug purposes.
618 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
619 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
620 * The Debug runtime does the same on Windows. However, a few games do not redraw the
621 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
623 * Tests show that the content of the back buffer after a discard flip is indeed not
624 * reliable, so no game can depend on the exact content. However, it resembles the
625 * old contents in some way, for example by showing fragments at other locations. In
626 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
627 * gets a dark background image. If we clear it with a bright ugly color, the game's
628 * bug shows up much more than it does on Windows, and the players see single pixels
629 * with wrong colors.
630 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
631 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
633 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
635 TRACE("Clearing the color buffer with cyan color\n");
637 wined3d_device_clear(swapchain->device, 0, NULL,
638 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
641 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
642 || (back_buffer->flags & SFLAG_INSYSMEM)))
644 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
645 * Doesn't work with render_to_fbo because we're not flipping
647 struct wined3d_surface *front = swapchain->front_buffer;
649 if (front->resource.size == back_buffer->resource.size)
651 DWORD fbflags;
652 flip_surface(front, back_buffer);
654 /* Tell the front buffer surface that is has been modified. However,
655 * the other locations were preserved during that, so keep the flags.
656 * This serves to update the emulated overlay, if any. */
657 fbflags = front->flags;
658 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
659 front->flags = fbflags;
661 else
663 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
664 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
667 else
669 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
670 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
671 * and INTEXTURE copies can keep their old content if they have any defined content.
672 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
673 * the texture / sysmem copy needs to be reloaded from the drawable
675 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
676 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
679 if (fb->depth_stencil)
681 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
682 || fb->depth_stencil->flags & SFLAG_DISCARD)
684 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
685 fb->depth_stencil->resource.width,
686 fb->depth_stencil->resource.height);
687 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
689 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
690 swapchain->device->onscreen_depth_stencil = NULL;
695 context_release(context);
698 static const struct wined3d_swapchain_ops swapchain_gl_ops =
700 swapchain_gl_present,
703 /* Helper function that blits the front buffer contents to the target window. */
704 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
706 const struct wined3d_surface *front;
707 POINT offset = {0, 0};
708 HDC src_dc, dst_dc;
709 RECT draw_rect;
710 HWND window;
712 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
714 front = swapchain->front_buffer;
715 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
716 return;
718 if (front->resource.map_count)
719 ERR("Trying to blit a mapped surface.\n");
721 TRACE("Copying surface %p to screen.\n", front);
723 src_dc = front->hDC;
724 window = swapchain->win_handle;
725 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
727 /* Front buffer coordinates are screen coordinates. Map them to the
728 * destination window if not fullscreened. */
729 if (swapchain->desc.windowed)
730 ClientToScreen(window, &offset);
732 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
734 draw_rect.left = 0;
735 draw_rect.right = front->resource.width;
736 draw_rect.top = 0;
737 draw_rect.bottom = front->resource.height;
739 if (rect)
740 IntersectRect(&draw_rect, &draw_rect, rect);
742 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
743 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
744 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
745 ReleaseDC(window, dst_dc);
748 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
749 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
751 struct wined3d_surface *front, *back;
753 front = swapchain->front_buffer;
754 back = swapchain->back_buffers[0];
756 /* Flip the DC. */
758 HDC tmp;
759 tmp = front->hDC;
760 front->hDC = back->hDC;
761 back->hDC = tmp;
764 /* Flip the DIBsection. */
766 HBITMAP tmp;
767 tmp = front->dib.DIBsection;
768 front->dib.DIBsection = back->dib.DIBsection;
769 back->dib.DIBsection = tmp;
772 /* Flip the surface data. */
774 void *tmp;
776 tmp = front->dib.bitmap_data;
777 front->dib.bitmap_data = back->dib.bitmap_data;
778 back->dib.bitmap_data = tmp;
780 tmp = front->resource.allocatedMemory;
781 front->resource.allocatedMemory = back->resource.allocatedMemory;
782 back->resource.allocatedMemory = tmp;
784 if (front->resource.heapMemory)
785 ERR("GDI Surface %p has heap memory allocated.\n", front);
787 if (back->resource.heapMemory)
788 ERR("GDI Surface %p has heap memory allocated.\n", back);
791 /* FPS support */
792 if (TRACE_ON(fps))
794 static LONG prev_time, frames;
795 DWORD time = GetTickCount();
797 ++frames;
799 /* every 1.5 seconds */
800 if (time - prev_time > 1500)
802 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
803 prev_time = time;
804 frames = 0;
808 x11_copy_to_screen(swapchain, NULL);
811 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
813 swapchain_gdi_present,
816 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
818 RECT client_rect;
820 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
821 return;
823 if (!swapchain->desc.backbuffer_count)
825 TRACE("Single buffered rendering.\n");
826 swapchain->render_to_fbo = FALSE;
827 return;
830 GetClientRect(swapchain->win_handle, &client_rect);
832 TRACE("Backbuffer %ux%u, window %ux%u.\n",
833 swapchain->desc.backbuffer_width,
834 swapchain->desc.backbuffer_height,
835 client_rect.right, client_rect.bottom);
836 TRACE("Multisample type %#x, quality %#x.\n",
837 swapchain->desc.multisample_type,
838 swapchain->desc.multisample_quality);
840 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
841 && swapchain->desc.backbuffer_width == client_rect.right
842 && swapchain->desc.backbuffer_height == client_rect.bottom)
844 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
845 swapchain->render_to_fbo = FALSE;
846 return;
849 TRACE("Rendering to FBO.\n");
850 swapchain->render_to_fbo = TRUE;
853 /* Do not call while under the GL lock. */
854 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, enum wined3d_surface_type surface_type,
855 struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
856 void *parent, const struct wined3d_parent_ops *parent_ops)
858 const struct wined3d_adapter *adapter = device->adapter;
859 const struct wined3d_format *format;
860 struct wined3d_display_mode mode;
861 BOOL displaymode_set = FALSE;
862 RECT client_rect;
863 HWND window;
864 HRESULT hr;
865 UINT i;
867 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
869 FIXME("The application requested %u back buffers, this is not supported.\n",
870 desc->backbuffer_count);
871 return WINED3DERR_INVALIDCALL;
874 if (desc->backbuffer_count > 1)
876 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
877 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
880 switch (surface_type)
882 case WINED3D_SURFACE_TYPE_GDI:
883 swapchain->swapchain_ops = &swapchain_gdi_ops;
884 break;
886 case WINED3D_SURFACE_TYPE_OPENGL:
887 swapchain->swapchain_ops = &swapchain_gl_ops;
888 break;
890 default:
891 ERR("Invalid surface type %#x.\n", surface_type);
892 return WINED3DERR_INVALIDCALL;
895 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
897 swapchain->device = device;
898 swapchain->parent = parent;
899 swapchain->parent_ops = parent_ops;
900 swapchain->ref = 1;
901 swapchain->win_handle = window;
902 swapchain->device_window = window;
904 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode);
905 swapchain->orig_width = mode.width;
906 swapchain->orig_height = mode.height;
907 swapchain->orig_fmt = mode.format_id;
908 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
910 GetClientRect(window, &client_rect);
911 if (desc->windowed
912 && (!desc->backbuffer_width || !desc->backbuffer_height
913 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
916 if (!desc->backbuffer_width)
918 desc->backbuffer_width = client_rect.right;
919 TRACE("Updating width to %u.\n", desc->backbuffer_width);
922 if (!desc->backbuffer_height)
924 desc->backbuffer_height = client_rect.bottom;
925 TRACE("Updating height to %u.\n", desc->backbuffer_height);
928 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
930 desc->backbuffer_format = swapchain->orig_fmt;
931 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
934 swapchain->desc = *desc;
935 swapchain_update_render_to_fbo(swapchain);
937 TRACE("Creating front buffer.\n");
938 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
939 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
940 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
941 swapchain->desc.multisample_quality, TRUE /* Lockable */,
942 &swapchain->front_buffer);
943 if (FAILED(hr))
945 WARN("Failed to create front buffer, hr %#x.\n", hr);
946 goto err;
949 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_SWAPCHAIN, swapchain);
950 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
951 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
953 /* MSDN says we're only allowed a single fullscreen swapchain per device,
954 * so we should really check to see if there is a fullscreen swapchain
955 * already. Does a single head count as full screen? */
957 if (!desc->windowed)
959 struct wined3d_display_mode mode;
961 /* Change the display settings */
962 mode.width = desc->backbuffer_width;
963 mode.height = desc->backbuffer_height;
964 mode.format_id = desc->backbuffer_format;
965 mode.refresh_rate = desc->refresh_rate;
967 hr = wined3d_device_set_display_mode(device, 0, &mode);
968 if (FAILED(hr))
970 WARN("Failed to set display mode, hr %#x.\n", hr);
971 goto err;
973 displaymode_set = TRUE;
976 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
978 static const enum wined3d_format_id formats[] =
980 WINED3DFMT_D24_UNORM_S8_UINT,
981 WINED3DFMT_D32_UNORM,
982 WINED3DFMT_R24_UNORM_X8_TYPELESS,
983 WINED3DFMT_D16_UNORM,
984 WINED3DFMT_S1_UINT_D15_UNORM
987 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
989 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
990 if (!swapchain->context)
992 ERR("Failed to create the context array.\n");
993 hr = E_OUTOFMEMORY;
994 goto err;
996 swapchain->num_contexts = 1;
998 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
999 * You are able to add a depth + stencil surface at a later stage when you need it.
1000 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1001 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1002 * context, need torecreate shaders, textures and other resources.
1004 * The context manager already takes care of the state problem and for the other tasks code from Reset
1005 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1006 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1007 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1008 * issue needs to be fixed. */
1009 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
1011 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
1012 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
1013 if (swapchain->context[0]) break;
1014 TRACE("Depth stencil format %s is not supported, trying next format\n",
1015 debug_d3dformat(formats[i]));
1018 if (!swapchain->context[0])
1020 WARN("Failed to create context.\n");
1021 hr = WINED3DERR_NOTAVAILABLE;
1022 goto err;
1025 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
1026 && (!desc->enable_auto_depth_stencil
1027 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
1029 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1031 context_release(swapchain->context[0]);
1034 if (swapchain->desc.backbuffer_count > 0)
1036 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1037 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1038 if (!swapchain->back_buffers)
1040 ERR("Failed to allocate backbuffer array memory.\n");
1041 hr = E_OUTOFMEMORY;
1042 goto err;
1045 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1047 TRACE("Creating back buffer %u.\n", i);
1048 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
1049 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1050 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
1051 swapchain->desc.multisample_quality, TRUE /* Lockable */,
1052 &swapchain->back_buffers[i]);
1053 if (FAILED(hr))
1055 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1056 goto err;
1059 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_SWAPCHAIN, swapchain);
1063 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1064 if (desc->enable_auto_depth_stencil && surface_type == WINED3D_SURFACE_TYPE_OPENGL)
1066 TRACE("Creating depth/stencil buffer.\n");
1067 if (!device->auto_depth_stencil)
1069 hr = device->device_parent->ops->create_depth_stencil(device->device_parent,
1070 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1071 swapchain->desc.auto_depth_stencil_format, swapchain->desc.multisample_type,
1072 swapchain->desc.multisample_quality, FALSE /* FIXME: Discard */,
1073 &device->auto_depth_stencil);
1074 if (FAILED(hr))
1076 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1077 goto err;
1080 surface_set_container(device->auto_depth_stencil, WINED3D_CONTAINER_NONE, NULL);
1084 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1086 return WINED3D_OK;
1088 err:
1089 if (displaymode_set)
1091 DEVMODEW devmode;
1093 ClipCursor(NULL);
1095 /* Change the display settings */
1096 memset(&devmode, 0, sizeof(devmode));
1097 devmode.dmSize = sizeof(devmode);
1098 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1099 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1100 devmode.dmPelsWidth = swapchain->orig_width;
1101 devmode.dmPelsHeight = swapchain->orig_height;
1102 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1105 if (swapchain->back_buffers)
1107 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1109 if (swapchain->back_buffers[i])
1111 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
1112 wined3d_surface_decref(swapchain->back_buffers[i]);
1115 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1118 if (swapchain->context)
1120 if (swapchain->context[0])
1122 context_release(swapchain->context[0]);
1123 context_destroy(device, swapchain->context[0]);
1124 swapchain->num_contexts = 0;
1126 HeapFree(GetProcessHeap(), 0, swapchain->context);
1129 if (swapchain->front_buffer)
1131 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
1132 wined3d_surface_decref(swapchain->front_buffer);
1135 return hr;
1138 /* Do not call while under the GL lock. */
1139 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1140 struct wined3d_swapchain_desc *desc, enum wined3d_surface_type surface_type,
1141 void *parent, const struct wined3d_parent_ops *parent_ops,
1142 struct wined3d_swapchain **swapchain)
1144 struct wined3d_swapchain *object;
1145 HRESULT hr;
1147 TRACE("device %p, desc %p, swapchain %p, parent %p, surface_type %#x.\n",
1148 device, desc, swapchain, parent, surface_type);
1150 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1151 if (!object)
1153 ERR("Failed to allocate swapchain memory.\n");
1154 return E_OUTOFMEMORY;
1157 hr = swapchain_init(object, surface_type, device, desc, parent, parent_ops);
1158 if (FAILED(hr))
1160 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1161 HeapFree(GetProcessHeap(), 0, object);
1162 return hr;
1165 TRACE("Created swapchain %p.\n", object);
1166 *swapchain = object;
1168 return WINED3D_OK;
1171 /* Do not call while under the GL lock. */
1172 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1174 struct wined3d_context **newArray;
1175 struct wined3d_context *ctx;
1177 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1179 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1181 ERR("Failed to create a new context for the swapchain\n");
1182 return NULL;
1184 context_release(ctx);
1186 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1187 if(!newArray) {
1188 ERR("Out of memory when trying to allocate a new context array\n");
1189 context_destroy(swapchain->device, ctx);
1190 return NULL;
1192 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1193 HeapFree(GetProcessHeap(), 0, swapchain->context);
1194 newArray[swapchain->num_contexts] = ctx;
1195 swapchain->context = newArray;
1196 swapchain->num_contexts++;
1198 TRACE("Returning context %p\n", ctx);
1199 return ctx;
1202 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1204 unsigned int i;
1206 for (i = 0; i < swapchain->num_contexts; ++i)
1208 context_destroy(swapchain->device, swapchain->context[i]);
1210 swapchain->num_contexts = 0;
1213 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1215 DWORD tid = GetCurrentThreadId();
1216 unsigned int i;
1218 for (i = 0; i < swapchain->num_contexts; ++i)
1220 if (swapchain->context[i]->tid == tid)
1221 return swapchain->context[i];
1224 /* Create a new context for the thread */
1225 return swapchain_create_context(swapchain);
1228 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1230 /* The drawable size of an onscreen drawable is the surface size.
1231 * (Actually: The window size, but the surface is created in window size) */
1232 *width = context->current_rt->resource.width;
1233 *height = context->current_rt->resource.height;
1236 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1238 if (!swapchain->backup_dc)
1240 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1242 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1243 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1245 ERR("Failed to create a window.\n");
1246 return NULL;
1249 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1251 ERR("Failed to get a DC.\n");
1252 DestroyWindow(swapchain->backup_wnd);
1253 swapchain->backup_wnd = NULL;
1254 return NULL;
1258 return swapchain->backup_dc;
1261 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1263 UINT i;
1265 surface_update_draw_binding(swapchain->front_buffer);
1267 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1269 surface_update_draw_binding(swapchain->back_buffers[i]);