windowscodecs: Add wrapper functions for IWICImagingFactory methods.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blob0e363a5b5897059da3eacdb6265cc11dbbef0a07
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 "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 WINE_DECLARE_DEBUG_CHANNEL(fps);
29 /* Do not call while under the GL lock. */
30 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 struct wined3d_display_mode mode;
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_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, 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_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, 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 mode.width = swapchain->orig_width;
78 mode.height = swapchain->orig_height;
79 mode.refresh_rate = 0;
80 mode.format_id = swapchain->orig_fmt;
81 wined3d_device_set_display_mode(swapchain->device, 0, &mode);
84 if (swapchain->backup_dc)
86 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
88 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
89 DestroyWindow(swapchain->backup_wnd);
93 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
95 ULONG refcount = InterlockedIncrement(&swapchain->ref);
97 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
99 return refcount;
102 /* Do not call while under the GL lock. */
103 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
105 ULONG refcount = InterlockedDecrement(&swapchain->ref);
107 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
109 if (!refcount)
111 swapchain_cleanup(swapchain);
112 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
113 HeapFree(GetProcessHeap(), 0, swapchain);
116 return refcount;
119 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
121 TRACE("swapchain %p.\n", swapchain);
123 return swapchain->parent;
126 HRESULT CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
128 if (!window)
129 window = swapchain->device_window;
130 if (window == swapchain->win_handle)
131 return WINED3D_OK;
133 TRACE("Setting swapchain %p window from %p to %p.\n",
134 swapchain, swapchain->win_handle, window);
135 swapchain->win_handle = window;
137 return WINED3D_OK;
140 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
141 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
142 const RGNDATA *dirty_region, DWORD flags)
144 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
145 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
146 dst_window_override, dirty_region, flags);
148 if (!swapchain->back_buffers)
150 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
151 return WINED3DERR_INVALIDCALL;
154 wined3d_swapchain_set_window(swapchain, dst_window_override);
156 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
158 return WINED3D_OK;
161 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
162 struct wined3d_surface *dst_surface)
164 struct wined3d_surface *src_surface;
165 RECT src_rect, dst_rect;
167 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
169 src_surface = swapchain->front_buffer;
170 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
171 dst_rect = src_rect;
173 if (swapchain->desc.windowed)
175 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
176 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
177 wine_dbgstr_rect(&dst_rect));
180 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
183 HRESULT CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
184 UINT back_buffer_idx, enum wined3d_backbuffer_type type, struct wined3d_surface **back_buffer)
186 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
187 swapchain, back_buffer_idx, type, back_buffer);
189 /* Return invalid if there is no backbuffer array, otherwise it will
190 * crash when ddraw is used (there swapchain->back_buffers is always
191 * NULL). We need this because this function is called from
192 * stateblock_init_default_state() to get the default scissorrect
193 * dimensions. */
194 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
196 WARN("Invalid back buffer index.\n");
197 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
198 * here in wined3d to avoid problems in other libs. */
199 *back_buffer = NULL;
200 return WINED3DERR_INVALIDCALL;
203 *back_buffer = swapchain->back_buffers[back_buffer_idx];
204 if (*back_buffer)
205 wined3d_surface_incref(*back_buffer);
207 TRACE("Returning back buffer %p.\n", *back_buffer);
209 return WINED3D_OK;
212 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
213 struct wined3d_raster_status *raster_status)
215 static BOOL warned;
216 LARGE_INTEGER counter, freq_per_sec;
217 LONGLONG freq_per_frame, freq_per_line;
218 struct wined3d_display_mode mode;
220 /* No OpenGL equivalent */
221 if (!warned)
223 FIXME("swapchain %p, raster_status %p semi-stub!\n", swapchain, raster_status);
224 warned = TRUE;
227 /* Obtaining the raster status is a widely implemented but optional
228 * feature. When this method returns OK StarCraft 2 expects the
229 * raster_status->InVBlank value to actually change over time.
230 * And Endless Alice Crysis doesn't care even if this method fails.
231 * Thus this method returns OK and fakes raster_status by
232 * QueryPerformanceCounter. */
234 if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&freq_per_sec))
235 return WINED3DERR_INVALIDCALL;
237 if (FAILED(wined3d_swapchain_get_display_mode(swapchain, &mode)))
238 return WINED3DERR_INVALIDCALL;
239 if (mode.refresh_rate == DEFAULT_REFRESH_RATE)
240 mode.refresh_rate = 60;
242 freq_per_frame = freq_per_sec.QuadPart / mode.refresh_rate;
243 /* Assume 20 scan lines in the vertical blank */
244 freq_per_line = freq_per_frame / (mode.height + 20);
245 raster_status->scan_line = (counter.QuadPart % freq_per_frame) / freq_per_line;
246 if (raster_status->scan_line < mode.height)
247 raster_status->in_vblank = FALSE;
248 else
250 raster_status->scan_line = 0;
251 raster_status->in_vblank = TRUE;
253 TRACE("Returning fake value, in_vblank %u, scan_line %u.\n",
254 raster_status->in_vblank, raster_status->scan_line);
255 return WINED3D_OK;
258 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
259 struct wined3d_display_mode *mode)
261 HRESULT hr;
263 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
265 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d, swapchain->device->adapter->ordinal, mode);
267 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
268 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
270 return hr;
273 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
275 TRACE("swapchain %p.\n", swapchain);
277 return swapchain->device;
280 HRESULT CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
281 struct wined3d_swapchain_desc *desc)
283 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
285 *desc = swapchain->desc;
287 return WINED3D_OK;
290 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
291 DWORD flags, const struct wined3d_gamma_ramp *ramp)
293 HDC dc;
295 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
297 if (flags)
298 FIXME("Ignoring flags %#x.\n", flags);
300 dc = GetDC(swapchain->device_window);
301 SetDeviceGammaRamp(dc, (void *)ramp);
302 ReleaseDC(swapchain->device_window, dc);
304 return WINED3D_OK;
307 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
308 struct wined3d_gamma_ramp *ramp)
310 HDC dc;
312 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
314 dc = GetDC(swapchain->device_window);
315 GetDeviceGammaRamp(dc, ramp);
316 ReleaseDC(swapchain->device_window, dc);
318 return WINED3D_OK;
321 /* A GL context is provided by the caller */
322 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
323 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
325 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
326 UINT src_w = src_rect->right - src_rect->left;
327 UINT src_h = src_rect->bottom - src_rect->top;
328 GLenum gl_filter;
329 const struct wined3d_gl_info *gl_info = context->gl_info;
330 RECT win_rect;
331 UINT win_h;
333 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
334 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
336 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
337 gl_filter = GL_NEAREST;
338 else
339 gl_filter = GL_LINEAR;
341 GetClientRect(swapchain->win_handle, &win_rect);
342 win_h = win_rect.bottom - win_rect.top;
344 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
346 DWORD location = SFLAG_INTEXTURE;
348 if (backbuffer->resource.multisample_type)
350 location = SFLAG_INRB_RESOLVED;
351 surface_load_location(backbuffer, location, NULL);
354 ENTER_GL();
355 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
356 glReadBuffer(GL_COLOR_ATTACHMENT0);
357 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
359 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
360 context_set_draw_buffer(context, GL_BACK);
361 context_invalidate_state(context, STATE_FRAMEBUFFER);
363 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
364 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
366 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
367 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
369 glDisable(GL_SCISSOR_TEST);
370 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
372 /* Note that the texture is upside down */
373 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
374 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
375 GL_COLOR_BUFFER_BIT, gl_filter);
376 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
377 LEAVE_GL();
379 else
381 struct wined3d_device *device = swapchain->device;
382 struct wined3d_context *context2;
383 float tex_left = src_rect->left;
384 float tex_top = src_rect->top;
385 float tex_right = src_rect->right;
386 float tex_bottom = src_rect->bottom;
388 context2 = context_acquire(device, swapchain->back_buffers[0]);
389 context_apply_blit_state(context2, device);
391 if (backbuffer->flags & SFLAG_NORMCOORD)
393 tex_left /= src_w;
394 tex_right /= src_w;
395 tex_top /= src_h;
396 tex_bottom /= src_h;
399 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
400 gl_filter = GL_NEAREST;
402 ENTER_GL();
403 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
405 /* Set up the texture. The surface is not in a wined3d_texture
406 * container, so there are no D3D texture settings to dirtify. */
407 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
408 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
409 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
411 context_set_draw_buffer(context, GL_BACK);
413 /* Set the viewport to the destination rectandle, disable any projection
414 * transformation set up by context_apply_blit_state(), and draw a
415 * (-1,-1)-(1,1) quad.
417 * Back up viewport and matrix to avoid breaking last_was_blit
419 * Note that context_apply_blit_state() set up viewport and ortho to
420 * match the surface size - we want the GL drawable(=window) size. */
421 glPushAttrib(GL_VIEWPORT_BIT);
422 glViewport(dst_rect->left, win_h - dst_rect->bottom, dst_rect->right, win_h - dst_rect->top);
423 glMatrixMode(GL_PROJECTION);
424 glPushMatrix();
425 glLoadIdentity();
427 glBegin(GL_QUADS);
428 /* bottom left */
429 glTexCoord2f(tex_left, tex_bottom);
430 glVertex2i(-1, -1);
432 /* top left */
433 glTexCoord2f(tex_left, tex_top);
434 glVertex2i(-1, 1);
436 /* top right */
437 glTexCoord2f(tex_right, tex_top);
438 glVertex2i(1, 1);
440 /* bottom right */
441 glTexCoord2f(tex_right, tex_bottom);
442 glVertex2i(1, -1);
443 glEnd();
445 glPopMatrix();
446 glPopAttrib();
448 device->blitter->unset_shader(context->gl_info);
449 checkGLcall("Swapchain present blit(manual)\n");
450 LEAVE_GL();
452 context_release(context2);
456 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
457 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
459 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
460 const struct wined3d_fb_state *fb = &swapchain->device->fb;
461 const struct wined3d_gl_info *gl_info;
462 struct wined3d_context *context;
463 RECT src_rect, dst_rect;
464 BOOL render_to_fbo;
466 context = context_acquire(swapchain->device, back_buffer);
467 if (!context->valid)
469 context_release(context);
470 WARN("Invalid context, skipping present.\n");
471 return;
474 gl_info = context->gl_info;
476 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
477 if (swapchain->device->bCursorVisible &&
478 swapchain->device->cursorTexture &&
479 !swapchain->device->hardwareCursor)
481 struct wined3d_surface cursor;
482 RECT destRect =
484 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
485 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
486 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
487 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
489 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
490 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
491 * the application because we are only supposed to copy the information out. Using a fake surface
492 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
494 memset(&cursor, 0, sizeof(cursor));
495 cursor.resource.ref = 1;
496 cursor.resource.device = swapchain->device;
497 cursor.resource.pool = WINED3D_POOL_SCRATCH;
498 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
499 cursor.resource.type = WINED3D_RTYPE_SURFACE;
500 cursor.texture_name = swapchain->device->cursorTexture;
501 cursor.texture_target = GL_TEXTURE_2D;
502 cursor.texture_level = 0;
503 cursor.resource.width = swapchain->device->cursorWidth;
504 cursor.resource.height = swapchain->device->cursorHeight;
505 /* The cursor must have pow2 sizes */
506 cursor.pow2Width = cursor.resource.width;
507 cursor.pow2Height = cursor.resource.height;
508 /* The surface is in the texture */
509 cursor.flags |= SFLAG_INTEXTURE;
510 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
511 * which is exactly what we want :-)
513 if (swapchain->desc.windowed)
514 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
515 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
516 NULL, WINED3D_TEXF_POINT);
519 if (swapchain->device->logo_surface)
521 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
522 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
524 /* Blit the logo into the upper left corner of the drawable. */
525 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
526 NULL, WINED3D_TEXF_POINT);
529 TRACE("Presenting HDC %p.\n", context->hdc);
531 render_to_fbo = swapchain->render_to_fbo;
533 if (src_rect_in)
535 src_rect = *src_rect_in;
536 if (!render_to_fbo && (src_rect.left || src_rect.top
537 || src_rect.right != swapchain->desc.backbuffer_width
538 || src_rect.bottom != swapchain->desc.backbuffer_height))
540 render_to_fbo = TRUE;
543 else
545 src_rect.left = 0;
546 src_rect.top = 0;
547 src_rect.right = swapchain->desc.backbuffer_width;
548 src_rect.bottom = swapchain->desc.backbuffer_height;
551 if (dst_rect_in)
552 dst_rect = *dst_rect_in;
553 else
554 GetClientRect(swapchain->win_handle, &dst_rect);
556 if (!render_to_fbo && (dst_rect.left || dst_rect.top
557 || dst_rect.right != swapchain->desc.backbuffer_width
558 || dst_rect.bottom != swapchain->desc.backbuffer_height))
559 render_to_fbo = TRUE;
561 /* Rendering to a window of different size, presenting partial rectangles,
562 * or rendering to a different window needs help from FBO_blit or a textured
563 * draw. Render the swapchain to a FBO in the future.
565 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
566 * all these issues - this fails if the window is smaller than the backbuffer.
568 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
570 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
571 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
572 swapchain->render_to_fbo = TRUE;
573 swapchain_update_draw_bindings(swapchain);
575 else
577 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
580 if (swapchain->render_to_fbo)
582 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
583 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
584 * not allowed(they need the COPY swapeffect)
586 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
587 * the swap. */
588 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
589 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
591 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
594 if (swapchain->num_contexts > 1)
595 wglFinish();
596 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
598 TRACE("SwapBuffers called, Starting new frame\n");
599 /* FPS support */
600 if (TRACE_ON(fps))
602 DWORD time = GetTickCount();
603 ++swapchain->frames;
605 /* every 1.5 seconds */
606 if (time - swapchain->prev_time > 1500)
608 TRACE_(fps)("%p @ approx %.2ffps\n",
609 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
610 swapchain->prev_time = time;
611 swapchain->frames = 0;
615 /* This is disabled, but the code left in for debug purposes.
617 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
618 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
619 * The Debug runtime does the same on Windows. However, a few games do not redraw the
620 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
622 * Tests show that the content of the back buffer after a discard flip is indeed not
623 * reliable, so no game can depend on the exact content. However, it resembles the
624 * old contents in some way, for example by showing fragments at other locations. In
625 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
626 * gets a dark background image. If we clear it with a bright ugly color, the game's
627 * bug shows up much more than it does on Windows, and the players see single pixels
628 * with wrong colors.
629 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
630 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
632 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
634 TRACE("Clearing the color buffer with cyan color\n");
636 wined3d_device_clear(swapchain->device, 0, NULL,
637 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
640 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
641 || (back_buffer->flags & SFLAG_INSYSMEM)))
643 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
644 * Doesn't work with render_to_fbo because we're not flipping
646 struct wined3d_surface *front = swapchain->front_buffer;
648 if (front->resource.size == back_buffer->resource.size)
650 DWORD fbflags;
651 flip_surface(front, back_buffer);
653 /* Tell the front buffer surface that is has been modified. However,
654 * the other locations were preserved during that, so keep the flags.
655 * This serves to update the emulated overlay, if any. */
656 fbflags = front->flags;
657 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
658 front->flags = fbflags;
660 else
662 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
663 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
666 else
668 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
669 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
670 * and INTEXTURE copies can keep their old content if they have any defined content.
671 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
672 * the texture / sysmem copy needs to be reloaded from the drawable
674 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
675 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
678 if (fb->depth_stencil)
680 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
681 || fb->depth_stencil->flags & SFLAG_DISCARD)
683 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
684 fb->depth_stencil->resource.width,
685 fb->depth_stencil->resource.height);
686 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
688 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
689 swapchain->device->onscreen_depth_stencil = NULL;
694 context_release(context);
697 static const struct wined3d_swapchain_ops swapchain_gl_ops =
699 swapchain_gl_present,
702 /* Helper function that blits the front buffer contents to the target window. */
703 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
705 const struct wined3d_surface *front;
706 POINT offset = {0, 0};
707 HDC src_dc, dst_dc;
708 RECT draw_rect;
709 HWND window;
711 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
713 front = swapchain->front_buffer;
714 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
715 return;
717 if (front->flags & SFLAG_LOCKED)
718 ERR("Trying to blit a mapped surface.\n");
720 TRACE("Copying surface %p to screen.\n", front);
722 src_dc = front->hDC;
723 window = swapchain->win_handle;
724 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
726 /* Front buffer coordinates are screen coordinates. Map them to the
727 * destination window if not fullscreened. */
728 if (swapchain->desc.windowed)
729 ClientToScreen(window, &offset);
731 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
733 draw_rect.left = 0;
734 draw_rect.right = front->resource.width;
735 draw_rect.top = 0;
736 draw_rect.bottom = front->resource.height;
738 if (rect)
739 IntersectRect(&draw_rect, &draw_rect, rect);
741 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
742 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
743 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
744 ReleaseDC(window, dst_dc);
747 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
748 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
750 struct wined3d_surface *front, *back;
752 front = swapchain->front_buffer;
753 back = swapchain->back_buffers[0];
755 /* Flip the DC. */
757 HDC tmp;
758 tmp = front->hDC;
759 front->hDC = back->hDC;
760 back->hDC = tmp;
763 /* Flip the DIBsection. */
765 HBITMAP tmp;
766 tmp = front->dib.DIBsection;
767 front->dib.DIBsection = back->dib.DIBsection;
768 back->dib.DIBsection = tmp;
771 /* Flip the surface data. */
773 void *tmp;
775 tmp = front->dib.bitmap_data;
776 front->dib.bitmap_data = back->dib.bitmap_data;
777 back->dib.bitmap_data = tmp;
779 tmp = front->resource.allocatedMemory;
780 front->resource.allocatedMemory = back->resource.allocatedMemory;
781 back->resource.allocatedMemory = tmp;
783 if (front->resource.heapMemory)
784 ERR("GDI Surface %p has heap memory allocated.\n", front);
786 if (back->resource.heapMemory)
787 ERR("GDI Surface %p has heap memory allocated.\n", back);
790 /* FPS support */
791 if (TRACE_ON(fps))
793 static LONG prev_time, frames;
794 DWORD time = GetTickCount();
796 ++frames;
798 /* every 1.5 seconds */
799 if (time - prev_time > 1500)
801 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
802 prev_time = time;
803 frames = 0;
807 x11_copy_to_screen(swapchain, NULL);
810 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
812 swapchain_gdi_present,
815 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
817 RECT client_rect;
819 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
820 return;
822 if (!swapchain->desc.backbuffer_count)
824 TRACE("Single buffered rendering.\n");
825 swapchain->render_to_fbo = FALSE;
826 return;
829 GetClientRect(swapchain->win_handle, &client_rect);
831 TRACE("Backbuffer %ux%u, window %ux%u.\n",
832 swapchain->desc.backbuffer_width,
833 swapchain->desc.backbuffer_height,
834 client_rect.right, client_rect.bottom);
835 TRACE("Multisample type %#x, quality %#x.\n",
836 swapchain->desc.multisample_type,
837 swapchain->desc.multisample_quality);
839 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
840 && swapchain->desc.backbuffer_width == client_rect.right
841 && swapchain->desc.backbuffer_height == client_rect.bottom)
843 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
844 swapchain->render_to_fbo = FALSE;
845 return;
848 TRACE("Rendering to FBO.\n");
849 swapchain->render_to_fbo = TRUE;
852 /* Do not call while under the GL lock. */
853 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, enum wined3d_surface_type surface_type,
854 struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
855 void *parent, const struct wined3d_parent_ops *parent_ops)
857 const struct wined3d_adapter *adapter = device->adapter;
858 const struct wined3d_format *format;
859 struct wined3d_display_mode mode;
860 BOOL displaymode_set = FALSE;
861 RECT client_rect;
862 HWND window;
863 HRESULT hr;
864 UINT i;
866 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
868 FIXME("The application requested %u back buffers, this is not supported.\n",
869 desc->backbuffer_count);
870 return WINED3DERR_INVALIDCALL;
873 if (desc->backbuffer_count > 1)
875 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
876 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
879 switch (surface_type)
881 case WINED3D_SURFACE_TYPE_GDI:
882 swapchain->swapchain_ops = &swapchain_gdi_ops;
883 break;
885 case WINED3D_SURFACE_TYPE_OPENGL:
886 swapchain->swapchain_ops = &swapchain_gl_ops;
887 break;
889 default:
890 ERR("Invalid surface type %#x.\n", surface_type);
891 return WINED3DERR_INVALIDCALL;
894 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
896 swapchain->device = device;
897 swapchain->parent = parent;
898 swapchain->parent_ops = parent_ops;
899 swapchain->ref = 1;
900 swapchain->win_handle = window;
901 swapchain->device_window = window;
903 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode);
904 swapchain->orig_width = mode.width;
905 swapchain->orig_height = mode.height;
906 swapchain->orig_fmt = mode.format_id;
907 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
909 GetClientRect(window, &client_rect);
910 if (desc->windowed
911 && (!desc->backbuffer_width || !desc->backbuffer_height
912 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
915 if (!desc->backbuffer_width)
917 desc->backbuffer_width = client_rect.right;
918 TRACE("Updating width to %u.\n", desc->backbuffer_width);
921 if (!desc->backbuffer_height)
923 desc->backbuffer_height = client_rect.bottom;
924 TRACE("Updating height to %u.\n", desc->backbuffer_height);
927 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
929 desc->backbuffer_format = swapchain->orig_fmt;
930 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
933 swapchain->desc = *desc;
934 swapchain_update_render_to_fbo(swapchain);
936 TRACE("Creating front buffer.\n");
937 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
938 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
939 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
940 swapchain->desc.multisample_quality, TRUE /* Lockable */,
941 &swapchain->front_buffer);
942 if (FAILED(hr))
944 WARN("Failed to create front buffer, hr %#x.\n", hr);
945 goto err;
948 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_SWAPCHAIN, swapchain);
949 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
950 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
952 /* MSDN says we're only allowed a single fullscreen swapchain per device,
953 * so we should really check to see if there is a fullscreen swapchain
954 * already. Does a single head count as full screen? */
956 if (!desc->windowed)
958 struct wined3d_display_mode mode;
960 /* Change the display settings */
961 mode.width = desc->backbuffer_width;
962 mode.height = desc->backbuffer_height;
963 mode.format_id = desc->backbuffer_format;
964 mode.refresh_rate = desc->refresh_rate;
966 hr = wined3d_device_set_display_mode(device, 0, &mode);
967 if (FAILED(hr))
969 WARN("Failed to set display mode, hr %#x.\n", hr);
970 goto err;
972 displaymode_set = TRUE;
975 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
977 static const enum wined3d_format_id formats[] =
979 WINED3DFMT_D24_UNORM_S8_UINT,
980 WINED3DFMT_D32_UNORM,
981 WINED3DFMT_R24_UNORM_X8_TYPELESS,
982 WINED3DFMT_D16_UNORM,
983 WINED3DFMT_S1_UINT_D15_UNORM
986 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
988 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
989 if (!swapchain->context)
991 ERR("Failed to create the context array.\n");
992 hr = E_OUTOFMEMORY;
993 goto err;
995 swapchain->num_contexts = 1;
997 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
998 * You are able to add a depth + stencil surface at a later stage when you need it.
999 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1000 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1001 * context, need torecreate shaders, textures and other resources.
1003 * The context manager already takes care of the state problem and for the other tasks code from Reset
1004 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1005 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1006 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1007 * issue needs to be fixed. */
1008 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
1010 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
1011 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
1012 if (swapchain->context[0]) break;
1013 TRACE("Depth stencil format %s is not supported, trying next format\n",
1014 debug_d3dformat(formats[i]));
1017 if (!swapchain->context[0])
1019 WARN("Failed to create context.\n");
1020 hr = WINED3DERR_NOTAVAILABLE;
1021 goto err;
1024 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
1025 && (!desc->enable_auto_depth_stencil
1026 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
1028 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1030 context_release(swapchain->context[0]);
1033 if (swapchain->desc.backbuffer_count > 0)
1035 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1036 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1037 if (!swapchain->back_buffers)
1039 ERR("Failed to allocate backbuffer array memory.\n");
1040 hr = E_OUTOFMEMORY;
1041 goto err;
1044 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1046 TRACE("Creating back buffer %u.\n", i);
1047 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
1048 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1049 swapchain->desc.backbuffer_format, swapchain->desc.multisample_type,
1050 swapchain->desc.multisample_quality, TRUE /* Lockable */,
1051 &swapchain->back_buffers[i]);
1052 if (FAILED(hr))
1054 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1055 goto err;
1058 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_SWAPCHAIN, swapchain);
1062 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1063 if (desc->enable_auto_depth_stencil && surface_type == WINED3D_SURFACE_TYPE_OPENGL)
1065 TRACE("Creating depth/stencil buffer.\n");
1066 if (!device->auto_depth_stencil)
1068 hr = device->device_parent->ops->create_depth_stencil(device->device_parent,
1069 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1070 swapchain->desc.auto_depth_stencil_format, swapchain->desc.multisample_type,
1071 swapchain->desc.multisample_quality, FALSE /* FIXME: Discard */,
1072 &device->auto_depth_stencil);
1073 if (FAILED(hr))
1075 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1076 goto err;
1079 surface_set_container(device->auto_depth_stencil, WINED3D_CONTAINER_NONE, NULL);
1083 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1085 return WINED3D_OK;
1087 err:
1088 if (displaymode_set)
1090 DEVMODEW devmode;
1092 ClipCursor(NULL);
1094 /* Change the display settings */
1095 memset(&devmode, 0, sizeof(devmode));
1096 devmode.dmSize = sizeof(devmode);
1097 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1098 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1099 devmode.dmPelsWidth = swapchain->orig_width;
1100 devmode.dmPelsHeight = swapchain->orig_height;
1101 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1104 if (swapchain->back_buffers)
1106 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1108 if (swapchain->back_buffers[i])
1110 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
1111 wined3d_surface_decref(swapchain->back_buffers[i]);
1114 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1117 if (swapchain->context)
1119 if (swapchain->context[0])
1121 context_release(swapchain->context[0]);
1122 context_destroy(device, swapchain->context[0]);
1123 swapchain->num_contexts = 0;
1125 HeapFree(GetProcessHeap(), 0, swapchain->context);
1128 if (swapchain->front_buffer)
1130 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
1131 wined3d_surface_decref(swapchain->front_buffer);
1134 return hr;
1137 /* Do not call while under the GL lock. */
1138 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1139 struct wined3d_swapchain_desc *desc, enum wined3d_surface_type surface_type,
1140 void *parent, const struct wined3d_parent_ops *parent_ops,
1141 struct wined3d_swapchain **swapchain)
1143 struct wined3d_swapchain *object;
1144 HRESULT hr;
1146 TRACE("device %p, desc %p, swapchain %p, parent %p, surface_type %#x.\n",
1147 device, desc, swapchain, parent, surface_type);
1149 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1150 if (!object)
1152 ERR("Failed to allocate swapchain memory.\n");
1153 return E_OUTOFMEMORY;
1156 hr = swapchain_init(object, surface_type, device, desc, parent, parent_ops);
1157 if (FAILED(hr))
1159 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1160 HeapFree(GetProcessHeap(), 0, object);
1161 return hr;
1164 TRACE("Created swapchain %p.\n", object);
1165 *swapchain = object;
1167 return WINED3D_OK;
1170 /* Do not call while under the GL lock. */
1171 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1173 struct wined3d_context **newArray;
1174 struct wined3d_context *ctx;
1176 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1178 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1180 ERR("Failed to create a new context for the swapchain\n");
1181 return NULL;
1183 context_release(ctx);
1185 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1186 if(!newArray) {
1187 ERR("Out of memory when trying to allocate a new context array\n");
1188 context_destroy(swapchain->device, ctx);
1189 return NULL;
1191 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1192 HeapFree(GetProcessHeap(), 0, swapchain->context);
1193 newArray[swapchain->num_contexts] = ctx;
1194 swapchain->context = newArray;
1195 swapchain->num_contexts++;
1197 TRACE("Returning context %p\n", ctx);
1198 return ctx;
1201 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1203 unsigned int i;
1205 for (i = 0; i < swapchain->num_contexts; ++i)
1207 context_destroy(swapchain->device, swapchain->context[i]);
1209 swapchain->num_contexts = 0;
1212 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1214 DWORD tid = GetCurrentThreadId();
1215 unsigned int i;
1217 for (i = 0; i < swapchain->num_contexts; ++i)
1219 if (swapchain->context[i]->tid == tid)
1220 return swapchain->context[i];
1223 /* Create a new context for the thread */
1224 return swapchain_create_context(swapchain);
1227 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1229 /* The drawable size of an onscreen drawable is the surface size.
1230 * (Actually: The window size, but the surface is created in window size) */
1231 *width = context->current_rt->resource.width;
1232 *height = context->current_rt->resource.height;
1235 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1237 if (!swapchain->backup_dc)
1239 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1241 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1242 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1244 ERR("Failed to create a window.\n");
1245 return NULL;
1248 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1250 ERR("Failed to get a DC.\n");
1251 DestroyWindow(swapchain->backup_wnd);
1252 swapchain->backup_wnd = NULL;
1253 return NULL;
1257 return swapchain->backup_dc;
1260 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1262 UINT i;
1264 surface_update_draw_binding(swapchain->front_buffer);
1266 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1268 surface_update_draw_binding(swapchain->back_buffers[i]);