msxml3: Fix attributes formatting.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blobfd2e236b6e777339ae86ac2812fa0b354748449b
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 WINED3DDISPLAYMODE 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->presentParms.BackBufferCount;
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->presentParms.Windowed && swapchain->presentParms.AutoRestoreDisplayMode)
77 mode.Width = swapchain->orig_width;
78 mode.Height = swapchain->orig_height;
79 mode.RefreshRate = 0;
80 mode.Format = 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 wined3d_swapchain_set_window(swapchain, dst_window_override);
150 return swapchain->swapchain_ops->swapchain_present(swapchain,
151 src_rect, dst_rect, dirty_region, flags);
154 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
155 struct wined3d_surface *dst_surface)
157 POINT offset = {0, 0};
159 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
161 if (swapchain->presentParms.Windowed)
162 MapWindowPoints(swapchain->win_handle, NULL, &offset, 1);
164 wined3d_surface_bltfast(dst_surface, offset.x, offset.y, swapchain->front_buffer, NULL, 0);
166 return WINED3D_OK;
169 HRESULT CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
170 UINT back_buffer_idx, WINED3DBACKBUFFER_TYPE type, struct wined3d_surface **back_buffer)
172 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
173 swapchain, back_buffer_idx, type, back_buffer);
175 /* Return invalid if there is no backbuffer array, otherwise it will
176 * crash when ddraw is used (there swapchain->back_buffers is always
177 * NULL). We need this because this function is called from
178 * stateblock_init_default_state() to get the default scissorrect
179 * dimensions. */
180 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->presentParms.BackBufferCount)
182 WARN("Invalid back buffer index.\n");
183 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
184 * here in wined3d to avoid problems in other libs. */
185 *back_buffer = NULL;
186 return WINED3DERR_INVALIDCALL;
189 *back_buffer = swapchain->back_buffers[back_buffer_idx];
190 if (*back_buffer)
191 wined3d_surface_incref(*back_buffer);
193 TRACE("Returning back buffer %p.\n", *back_buffer);
195 return WINED3D_OK;
198 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
199 WINED3DRASTER_STATUS *raster_status)
201 static BOOL warned;
202 /* No OpenGL equivalent */
203 if (!warned)
205 FIXME("swapchain %p, raster_status %p stub!\n", swapchain, raster_status);
206 warned = TRUE;
209 /* Obtaining the raster status is a widely implemented but optional
210 * feature. When this method returns OK StarCraft 2 expects the
211 * raster_status->InVBlank value to actually change over time. To prevent
212 * StarCraft 2 from running in an infinite loop at startup this method
213 * returns INVALIDCALL. */
214 return WINED3DERR_INVALIDCALL;
217 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain, WINED3DDISPLAYMODE *mode)
219 HRESULT hr;
221 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
223 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d, swapchain->device->adapter->ordinal, mode);
225 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
226 mode->Width, mode->Height, mode->RefreshRate, debug_d3dformat(mode->Format));
228 return hr;
231 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
233 TRACE("swapchain %p.\n", swapchain);
235 return swapchain->device;
238 HRESULT CDECL wined3d_swapchain_get_present_parameters(const struct wined3d_swapchain *swapchain,
239 WINED3DPRESENT_PARAMETERS *present_parameters)
241 TRACE("swapchain %p, present_parameters %p.\n", swapchain, present_parameters);
243 *present_parameters = swapchain->presentParms;
245 return WINED3D_OK;
248 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
249 DWORD flags, const WINED3DGAMMARAMP *ramp)
251 HDC dc;
253 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
255 if (flags)
256 FIXME("Ignoring flags %#x.\n", flags);
258 dc = GetDC(swapchain->device_window);
259 SetDeviceGammaRamp(dc, (void *)ramp);
260 ReleaseDC(swapchain->device_window, dc);
262 return WINED3D_OK;
265 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
266 WINED3DGAMMARAMP *ramp)
268 HDC dc;
270 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
272 dc = GetDC(swapchain->device_window);
273 GetDeviceGammaRamp(dc, ramp);
274 ReleaseDC(swapchain->device_window, dc);
276 return WINED3D_OK;
279 /* A GL context is provided by the caller */
280 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
281 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
283 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
284 UINT src_w = src_rect->right - src_rect->left;
285 UINT src_h = src_rect->bottom - src_rect->top;
286 GLenum gl_filter;
287 const struct wined3d_gl_info *gl_info = context->gl_info;
288 RECT win_rect;
289 UINT win_h;
291 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
292 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
294 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
295 gl_filter = GL_NEAREST;
296 else
297 gl_filter = GL_LINEAR;
299 GetClientRect(swapchain->win_handle, &win_rect);
300 win_h = win_rect.bottom - win_rect.top;
302 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
304 DWORD location = SFLAG_INTEXTURE;
306 if (backbuffer->resource.multisample_type)
308 location = SFLAG_INRB_RESOLVED;
309 surface_load_location(backbuffer, location, NULL);
312 ENTER_GL();
313 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
314 glReadBuffer(GL_COLOR_ATTACHMENT0);
315 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
317 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
318 context_set_draw_buffer(context, GL_BACK);
319 context_invalidate_state(context, STATE_FRAMEBUFFER);
321 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
322 context_invalidate_state(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
323 context_invalidate_state(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
324 context_invalidate_state(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
325 context_invalidate_state(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
327 glDisable(GL_SCISSOR_TEST);
328 context_invalidate_state(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
330 /* Note that the texture is upside down */
331 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
332 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
333 GL_COLOR_BUFFER_BIT, gl_filter);
334 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
335 LEAVE_GL();
337 else
339 struct wined3d_device *device = swapchain->device;
340 struct wined3d_context *context2;
341 float tex_left = src_rect->left;
342 float tex_top = src_rect->top;
343 float tex_right = src_rect->right;
344 float tex_bottom = src_rect->bottom;
346 context2 = context_acquire(device, swapchain->back_buffers[0]);
347 context_apply_blit_state(context2, device);
349 if (backbuffer->flags & SFLAG_NORMCOORD)
351 tex_left /= src_w;
352 tex_right /= src_w;
353 tex_top /= src_h;
354 tex_bottom /= src_h;
357 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
358 gl_filter = GL_NEAREST;
360 ENTER_GL();
361 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
363 /* Set up the texture. The surface is not in a wined3d_texture
364 * container, so there are no D3D texture settings to dirtify. */
365 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
366 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
367 glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
369 context_set_draw_buffer(context, GL_BACK);
371 /* Set the viewport to the destination rectandle, disable any projection
372 * transformation set up by context_apply_blit_state(), and draw a
373 * (-1,-1)-(1,1) quad.
375 * Back up viewport and matrix to avoid breaking last_was_blit
377 * Note that context_apply_blit_state() set up viewport and ortho to
378 * match the surface size - we want the GL drawable(=window) size. */
379 glPushAttrib(GL_VIEWPORT_BIT);
380 glViewport(dst_rect->left, win_h - dst_rect->bottom, dst_rect->right, win_h - dst_rect->top);
381 glMatrixMode(GL_PROJECTION);
382 glPushMatrix();
383 glLoadIdentity();
385 glBegin(GL_QUADS);
386 /* bottom left */
387 glTexCoord2f(tex_left, tex_bottom);
388 glVertex2i(-1, -1);
390 /* top left */
391 glTexCoord2f(tex_left, tex_top);
392 glVertex2i(-1, 1);
394 /* top right */
395 glTexCoord2f(tex_right, tex_top);
396 glVertex2i(1, 1);
398 /* bottom right */
399 glTexCoord2f(tex_right, tex_bottom);
400 glVertex2i(1, -1);
401 glEnd();
403 glPopMatrix();
404 glPopAttrib();
406 device->blitter->unset_shader(context->gl_info);
407 checkGLcall("Swapchain present blit(manual)\n");
408 LEAVE_GL();
410 context_release(context2);
414 static HRESULT swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
415 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
417 const struct wined3d_fb_state *fb = &swapchain->device->fb;
418 const struct wined3d_gl_info *gl_info;
419 struct wined3d_context *context;
420 RECT src_rect, dst_rect;
421 BOOL render_to_fbo;
423 context = context_acquire(swapchain->device, swapchain->back_buffers[0]);
424 if (!context->valid)
426 context_release(context);
427 WARN("Invalid context, skipping present.\n");
428 return WINED3D_OK;
431 gl_info = context->gl_info;
433 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
434 if (swapchain->device->bCursorVisible &&
435 swapchain->device->cursorTexture &&
436 !swapchain->device->hardwareCursor)
438 struct wined3d_surface cursor;
439 RECT destRect =
441 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
442 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
443 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
444 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
446 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
447 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
448 * the application because we are only supposed to copy the information out. Using a fake surface
449 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
451 memset(&cursor, 0, sizeof(cursor));
452 cursor.resource.ref = 1;
453 cursor.resource.device = swapchain->device;
454 cursor.resource.pool = WINED3DPOOL_SCRATCH;
455 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
456 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
457 cursor.texture_name = swapchain->device->cursorTexture;
458 cursor.texture_target = GL_TEXTURE_2D;
459 cursor.texture_level = 0;
460 cursor.resource.width = swapchain->device->cursorWidth;
461 cursor.resource.height = swapchain->device->cursorHeight;
462 /* The cursor must have pow2 sizes */
463 cursor.pow2Width = cursor.resource.width;
464 cursor.pow2Height = cursor.resource.height;
465 /* The surface is in the texture */
466 cursor.flags |= SFLAG_INTEXTURE;
467 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
468 * which is exactly what we want :-)
470 if (swapchain->presentParms.Windowed)
471 MapWindowPoints(NULL, swapchain->win_handle, (LPPOINT)&destRect, 2);
472 wined3d_surface_blt(swapchain->back_buffers[0], &destRect,
473 &cursor, NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
476 if (swapchain->device->logo_surface)
478 /* Blit the logo into the upper left corner of the drawable. */
479 wined3d_surface_bltfast(swapchain->back_buffers[0], 0, 0,
480 swapchain->device->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
483 TRACE("Presenting HDC %p.\n", context->hdc);
485 render_to_fbo = swapchain->render_to_fbo;
487 if (src_rect_in)
489 src_rect = *src_rect_in;
490 if (!render_to_fbo && (src_rect.left || src_rect.top
491 || src_rect.right != swapchain->presentParms.BackBufferWidth
492 || src_rect.bottom != swapchain->presentParms.BackBufferHeight))
494 render_to_fbo = TRUE;
497 else
499 src_rect.left = 0;
500 src_rect.top = 0;
501 src_rect.right = swapchain->presentParms.BackBufferWidth;
502 src_rect.bottom = swapchain->presentParms.BackBufferHeight;
505 if (dst_rect_in)
506 dst_rect = *dst_rect_in;
507 else
508 GetClientRect(swapchain->win_handle, &dst_rect);
510 if (!render_to_fbo && (dst_rect.left || dst_rect.top
511 || dst_rect.right != swapchain->presentParms.BackBufferWidth
512 || dst_rect.bottom != swapchain->presentParms.BackBufferHeight))
514 render_to_fbo = TRUE;
517 /* Rendering to a window of different size, presenting partial rectangles,
518 * or rendering to a different window needs help from FBO_blit or a textured
519 * draw. Render the swapchain to a FBO in the future.
521 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
522 * all these issues - this fails if the window is smaller than the backbuffer.
524 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
526 surface_load_location(swapchain->back_buffers[0], SFLAG_INTEXTURE, NULL);
527 surface_modify_location(swapchain->back_buffers[0], SFLAG_INDRAWABLE, FALSE);
528 swapchain->render_to_fbo = TRUE;
529 swapchain_update_draw_bindings(swapchain);
532 if (swapchain->render_to_fbo)
534 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
535 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
536 * not allowed(they need the COPY swapeffect)
538 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
539 * the swap. */
540 if (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP)
541 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
543 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
546 if (swapchain->num_contexts > 1)
547 wglFinish();
548 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
550 TRACE("SwapBuffers called, Starting new frame\n");
551 /* FPS support */
552 if (TRACE_ON(fps))
554 DWORD time = GetTickCount();
555 ++swapchain->frames;
557 /* every 1.5 seconds */
558 if (time - swapchain->prev_time > 1500)
560 TRACE_(fps)("%p @ approx %.2ffps\n",
561 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
562 swapchain->prev_time = time;
563 swapchain->frames = 0;
567 /* This is disabled, but the code left in for debug purposes.
569 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
570 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
571 * The Debug runtime does the same on Windows. However, a few games do not redraw the
572 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
574 * Tests show that the content of the back buffer after a discard flip is indeed not
575 * reliable, so no game can depend on the exact content. However, it resembles the
576 * old contents in some way, for example by showing fragments at other locations. In
577 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
578 * gets a dark background image. If we clear it with a bright ugly color, the game's
579 * bug shows up much more than it does on Windows, and the players see single pixels
580 * with wrong colors.
581 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
582 if (FALSE && swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD)
584 TRACE("Clearing the color buffer with cyan color\n");
586 wined3d_device_clear(swapchain->device, 0, NULL,
587 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
590 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
591 || (swapchain->back_buffers[0]->flags & SFLAG_INSYSMEM)))
593 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
594 * Doesn't work with render_to_fbo because we're not flipping
596 struct wined3d_surface *front = swapchain->front_buffer;
597 struct wined3d_surface *back = swapchain->back_buffers[0];
599 if(front->resource.size == back->resource.size) {
600 DWORD fbflags;
601 flip_surface(front, back);
603 /* Tell the front buffer surface that is has been modified. However,
604 * the other locations were preserved during that, so keep the flags.
605 * This serves to update the emulated overlay, if any. */
606 fbflags = front->flags;
607 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
608 front->flags = fbflags;
610 else
612 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
613 surface_modify_location(back, SFLAG_INDRAWABLE, TRUE);
616 else
618 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
619 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
620 * and INTEXTURE copies can keep their old content if they have any defined content.
621 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
622 * the texture / sysmem copy needs to be reloaded from the drawable
624 if (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP)
625 surface_modify_location(swapchain->back_buffers[0], swapchain->back_buffers[0]->draw_binding, TRUE);
628 if (fb->depth_stencil)
630 if (swapchain->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
631 || fb->depth_stencil->flags & SFLAG_DISCARD)
633 surface_modify_ds_location(fb->depth_stencil, SFLAG_DS_DISCARDED,
634 fb->depth_stencil->resource.width,
635 fb->depth_stencil->resource.height);
636 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
638 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
639 swapchain->device->onscreen_depth_stencil = NULL;
644 context_release(context);
646 TRACE("returning\n");
647 return WINED3D_OK;
650 static const struct wined3d_swapchain_ops swapchain_gl_ops =
652 swapchain_gl_present,
655 /* Helper function that blits the front buffer contents to the target window. */
656 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
658 const struct wined3d_surface *front;
659 POINT offset = {0, 0};
660 HDC src_dc, dst_dc;
661 RECT draw_rect;
662 HWND window;
664 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
666 front = swapchain->front_buffer;
667 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
668 return;
670 TRACE("Copying surface %p to screen.\n", front);
672 src_dc = front->hDC;
673 window = swapchain->win_handle;
674 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
676 /* Front buffer coordinates are screen coordinates. Map them to the
677 * destination window if not fullscreened. */
678 if (swapchain->presentParms.Windowed)
679 ClientToScreen(window, &offset);
681 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
683 #if 0
684 /* FIXME: This doesn't work... if users really want to run
685 * X in 8bpp, then we need to call directly into display.drv
686 * (or Wine's equivalent), and force a private colormap
687 * without default entries. */
688 if (front->palette)
690 SelectPalette(dst_dc, front->palette->hpal, FALSE);
691 RealizePalette(dst_dc); /* sends messages => deadlocks */
693 #endif
695 draw_rect.left = 0;
696 draw_rect.right = front->resource.width;
697 draw_rect.top = 0;
698 draw_rect.bottom = front->resource.height;
700 #if 0
701 /* TODO: Support clippers. */
702 if (front->clipper)
704 RECT xrc;
705 HWND hwnd = front->clipper->hWnd;
706 if (hwnd && GetClientRect(hwnd,&xrc))
708 OffsetRect(&xrc, offset.x, offset.y);
709 IntersectRect(&draw_rect, &draw_rect, &xrc);
712 #endif
714 if (!rect)
716 /* Only use this if the caller did not pass a rectangle, since
717 * due to double locking this could be the wrong one... */
718 if (front->lockedRect.left != front->lockedRect.right)
719 IntersectRect(&draw_rect, &draw_rect, &front->lockedRect);
721 else
723 IntersectRect(&draw_rect, &draw_rect, rect);
726 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
727 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
728 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
729 ReleaseDC(window, dst_dc);
732 static HRESULT swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
733 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
735 struct wined3d_surface *front, *back;
737 if (!swapchain->back_buffers)
739 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
740 return WINED3DERR_INVALIDCALL;
742 front = swapchain->front_buffer;
743 back = swapchain->back_buffers[0];
745 /* Flip the DC. */
747 HDC tmp;
748 tmp = front->hDC;
749 front->hDC = back->hDC;
750 back->hDC = tmp;
753 /* Flip the DIBsection. */
755 HBITMAP tmp;
756 tmp = front->dib.DIBsection;
757 front->dib.DIBsection = back->dib.DIBsection;
758 back->dib.DIBsection = tmp;
761 /* Flip the surface data. */
763 void *tmp;
765 tmp = front->dib.bitmap_data;
766 front->dib.bitmap_data = back->dib.bitmap_data;
767 back->dib.bitmap_data = tmp;
769 tmp = front->resource.allocatedMemory;
770 front->resource.allocatedMemory = back->resource.allocatedMemory;
771 back->resource.allocatedMemory = tmp;
773 if (front->resource.heapMemory)
774 ERR("GDI Surface %p has heap memory allocated.\n", front);
776 if (back->resource.heapMemory)
777 ERR("GDI Surface %p has heap memory allocated.\n", back);
780 /* Client_memory should not be different, but just in case. */
782 BOOL tmp;
783 tmp = front->dib.client_memory;
784 front->dib.client_memory = back->dib.client_memory;
785 back->dib.client_memory = tmp;
788 /* FPS support */
789 if (TRACE_ON(fps))
791 static LONG prev_time, frames;
792 DWORD time = GetTickCount();
794 ++frames;
796 /* every 1.5 seconds */
797 if (time - prev_time > 1500)
799 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
800 prev_time = time;
801 frames = 0;
805 x11_copy_to_screen(swapchain, NULL);
807 return WINED3D_OK;
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->presentParms.BackBufferCount)
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->presentParms.BackBufferWidth,
833 swapchain->presentParms.BackBufferHeight,
834 client_rect.right, client_rect.bottom);
835 TRACE("Multisample type %#x, quality %#x.\n",
836 swapchain->presentParms.MultiSampleType,
837 swapchain->presentParms.MultiSampleQuality);
839 if (!wined3d_settings.always_offscreen && !swapchain->presentParms.MultiSampleType
840 && swapchain->presentParms.BackBufferWidth == client_rect.right
841 && swapchain->presentParms.BackBufferHeight == 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, WINED3DSURFTYPE surface_type,
854 struct wined3d_device *device, WINED3DPRESENT_PARAMETERS *present_parameters,
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 BOOL displaymode_set = FALSE;
860 WINED3DDISPLAYMODE mode;
861 RECT client_rect;
862 HWND window;
863 HRESULT hr;
864 UINT i;
866 if (present_parameters->BackBufferCount > WINED3DPRESENT_BACK_BUFFER_MAX)
868 FIXME("The application requested %u back buffers, this is not supported.\n",
869 present_parameters->BackBufferCount);
870 return WINED3DERR_INVALIDCALL;
873 if (present_parameters->BackBufferCount > 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 SURFACE_GDI:
882 swapchain->swapchain_ops = &swapchain_gdi_ops;
883 break;
885 case SURFACE_OPENGL:
886 swapchain->swapchain_ops = &swapchain_gl_ops;
887 break;
889 case SURFACE_UNKNOWN:
890 FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
891 return WINED3DERR_INVALIDCALL;
894 window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
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;
907 format = wined3d_get_format(&adapter->gl_info, mode.Format);
909 GetClientRect(window, &client_rect);
910 if (present_parameters->Windowed
911 && (!present_parameters->BackBufferWidth || !present_parameters->BackBufferHeight
912 || present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN))
915 if (!present_parameters->BackBufferWidth)
917 present_parameters->BackBufferWidth = client_rect.right;
918 TRACE("Updating width to %u.\n", present_parameters->BackBufferWidth);
921 if (!present_parameters->BackBufferHeight)
923 present_parameters->BackBufferHeight = client_rect.bottom;
924 TRACE("Updating height to %u.\n", present_parameters->BackBufferHeight);
927 if (present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN)
929 present_parameters->BackBufferFormat = swapchain->orig_fmt;
930 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
933 swapchain->presentParms = *present_parameters;
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->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
939 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
940 swapchain->presentParms.MultiSampleQuality, 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 == SURFACE_OPENGL)
951 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
954 /* MSDN says we're only allowed a single fullscreen swapchain per device,
955 * so we should really check to see if there is a fullscreen swapchain
956 * already. Does a single head count as full screen? */
958 if (!present_parameters->Windowed)
960 WINED3DDISPLAYMODE mode;
962 /* Change the display settings */
963 mode.Width = present_parameters->BackBufferWidth;
964 mode.Height = present_parameters->BackBufferHeight;
965 mode.Format = present_parameters->BackBufferFormat;
966 mode.RefreshRate = present_parameters->FullScreen_RefreshRateInHz;
968 hr = wined3d_device_set_display_mode(device, 0, &mode);
969 if (FAILED(hr))
971 WARN("Failed to set display mode, hr %#x.\n", hr);
972 goto err;
974 displaymode_set = TRUE;
977 if (surface_type == SURFACE_OPENGL)
979 static const enum wined3d_format_id formats[] =
981 WINED3DFMT_D24_UNORM_S8_UINT,
982 WINED3DFMT_D32_UNORM,
983 WINED3DFMT_R24_UNORM_X8_TYPELESS,
984 WINED3DFMT_D16_UNORM,
985 WINED3DFMT_S1_UINT_D15_UNORM
988 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
990 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain->context));
991 if (!swapchain->context)
993 ERR("Failed to create the context array.\n");
994 hr = E_OUTOFMEMORY;
995 goto err;
997 swapchain->num_contexts = 1;
999 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
1000 * You are able to add a depth + stencil surface at a later stage when you need it.
1001 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1002 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1003 * context, need torecreate shaders, textures and other resources.
1005 * The context manager already takes care of the state problem and for the other tasks code from Reset
1006 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1007 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1008 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1009 * issue needs to be fixed. */
1010 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
1012 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
1013 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
1014 if (swapchain->context[0]) break;
1015 TRACE("Depth stencil format %s is not supported, trying next format\n",
1016 debug_d3dformat(formats[i]));
1019 if (!swapchain->context[0])
1021 WARN("Failed to create context.\n");
1022 hr = WINED3DERR_NOTAVAILABLE;
1023 goto err;
1026 if (!present_parameters->EnableAutoDepthStencil
1027 || swapchain->presentParms.AutoDepthStencilFormat != 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->presentParms.BackBufferCount > 0)
1036 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1037 sizeof(*swapchain->back_buffers) * swapchain->presentParms.BackBufferCount);
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->presentParms.BackBufferCount; ++i)
1047 TRACE("Creating back buffer %u.\n", i);
1048 hr = device->device_parent->ops->create_rendertarget(device->device_parent, parent,
1049 swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
1050 swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
1051 swapchain->presentParms.MultiSampleQuality, 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 (present_parameters->EnableAutoDepthStencil && surface_type == SURFACE_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->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
1071 swapchain->presentParms.AutoDepthStencilFormat, swapchain->presentParms.MultiSampleType,
1072 swapchain->presentParms.MultiSampleQuality, 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->presentParms.BackBufferCount; ++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 WINED3DPRESENT_PARAMETERS *present_parameters, WINED3DSURFTYPE 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, present_parameters %p, swapchain %p, parent %p, surface_type %#x.\n",
1148 device, present_parameters, 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, present_parameters, 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->presentParms.BackBufferCount; ++i)
1269 surface_update_draw_binding(swapchain->back_buffers[i]);