4996cf5fa13c3f1aba5ca27a65dfd47007959044
[reactos.git] / reactos / dll / directx / wine / wined3d / swapchain.c
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
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <config.h>
24 #include <wine/port.h>
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
29
30 /* Do not call while under the GL lock. */
31 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 {
33 HRESULT hr;
34 UINT i;
35
36 TRACE("Destroying swapchain %p.\n", swapchain);
37
38 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
39
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)
43 {
44 surface_set_swapchain(swapchain->front_buffer, 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;
48 }
49
50 if (swapchain->back_buffers)
51 {
52 i = swapchain->desc.backbuffer_count;
53
54 while (i--)
55 {
56 surface_set_swapchain(swapchain->back_buffers[i], 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]);
59 }
60 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
61 swapchain->back_buffers = NULL;
62 }
63
64 for (i = 0; i < swapchain->num_contexts; ++i)
65 {
66 context_destroy(swapchain->device, swapchain->context[i]);
67 }
68 HeapFree(GetProcessHeap(), 0, swapchain->context);
69
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)
77 {
78 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
79 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
80 ERR("Failed to restore display mode, hr %#x.\n", hr);
81 }
82
83 if (swapchain->backup_dc)
84 {
85 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
86
87 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
88 DestroyWindow(swapchain->backup_wnd);
89 }
90 }
91
92 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
93 {
94 ULONG refcount = InterlockedIncrement(&swapchain->ref);
95
96 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
97
98 return refcount;
99 }
100
101 /* Do not call while under the GL lock. */
102 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
103 {
104 ULONG refcount = InterlockedDecrement(&swapchain->ref);
105
106 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
107
108 if (!refcount)
109 {
110 swapchain_cleanup(swapchain);
111 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
112 HeapFree(GetProcessHeap(), 0, swapchain);
113 }
114
115 return refcount;
116 }
117
118 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
119 {
120 TRACE("swapchain %p.\n", swapchain);
121
122 return swapchain->parent;
123 }
124
125 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
126 {
127 if (!window)
128 window = swapchain->device_window;
129 if (window == swapchain->win_handle)
130 return;
131
132 TRACE("Setting swapchain %p window from %p to %p.\n",
133 swapchain, swapchain->win_handle, window);
134 swapchain->win_handle = window;
135 }
136
137 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
138 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
139 const RGNDATA *dirty_region, DWORD flags)
140 {
141 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
142 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
143 dst_window_override, dirty_region, flags);
144
145 if (flags)
146 FIXME("Ignoring flags %#x.\n", flags);
147
148 if (!swapchain->back_buffers)
149 {
150 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
151 return WINED3DERR_INVALIDCALL;
152 }
153
154 wined3d_swapchain_set_window(swapchain, dst_window_override);
155
156 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
157
158 return WINED3D_OK;
159 }
160
161 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
162 struct wined3d_surface *dst_surface)
163 {
164 struct wined3d_surface *src_surface;
165 RECT src_rect, dst_rect;
166
167 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
168
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;
172
173 if (swapchain->desc.windowed)
174 {
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));
178 }
179
180 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
181 }
182
183 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
184 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
185 {
186 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
187 swapchain, back_buffer_idx, type);
188
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)
195 {
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 return NULL;
200 }
201
202 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
203
204 return swapchain->back_buffers[back_buffer_idx];
205 }
206
207 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
208 struct wined3d_raster_status *raster_status)
209 {
210 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
211
212 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
213 swapchain->device->adapter->ordinal, raster_status);
214 }
215
216 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
217 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
218 {
219 HRESULT hr;
220
221 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
222
223 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
224 swapchain->device->adapter->ordinal, mode, rotation);
225
226 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
227 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
228
229 return hr;
230 }
231
232 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
233 {
234 TRACE("swapchain %p.\n", swapchain);
235
236 return swapchain->device;
237 }
238
239 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
240 struct wined3d_swapchain_desc *desc)
241 {
242 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
243
244 *desc = swapchain->desc;
245 }
246
247 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
248 DWORD flags, const struct wined3d_gamma_ramp *ramp)
249 {
250 HDC dc;
251
252 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
253
254 if (flags)
255 FIXME("Ignoring flags %#x.\n", flags);
256
257 dc = GetDC(swapchain->device_window);
258 SetDeviceGammaRamp(dc, (void *)ramp);
259 ReleaseDC(swapchain->device_window, dc);
260
261 return WINED3D_OK;
262 }
263
264 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
265 struct wined3d_gamma_ramp *ramp)
266 {
267 HDC dc;
268
269 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
270
271 dc = GetDC(swapchain->device_window);
272 GetDeviceGammaRamp(dc, ramp);
273 ReleaseDC(swapchain->device_window, dc);
274
275 return WINED3D_OK;
276 }
277
278 /* A GL context is provided by the caller */
279 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
280 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
281 {
282 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
283 UINT src_w = src_rect->right - src_rect->left;
284 UINT src_h = src_rect->bottom - src_rect->top;
285 GLenum gl_filter;
286 const struct wined3d_gl_info *gl_info = context->gl_info;
287 RECT win_rect;
288 UINT win_h;
289
290 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
291 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
292
293 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
294 gl_filter = GL_NEAREST;
295 else
296 gl_filter = GL_LINEAR;
297
298 GetClientRect(swapchain->win_handle, &win_rect);
299 win_h = win_rect.bottom - win_rect.top;
300
301 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
302 {
303 DWORD location = SFLAG_INTEXTURE;
304
305 if (backbuffer->resource.multisample_type)
306 {
307 location = SFLAG_INRB_RESOLVED;
308 surface_load_location(backbuffer, location, NULL);
309 }
310
311 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
312 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
313 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
314
315 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
316 context_set_draw_buffer(context, GL_BACK);
317 context_invalidate_state(context, STATE_FRAMEBUFFER);
318
319 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
320 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
321 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
322 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
323 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
324
325 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
327
328 /* Note that the texture is upside down */
329 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
330 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
331 GL_COLOR_BUFFER_BIT, gl_filter);
332 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
333 }
334 else
335 {
336 struct wined3d_device *device = swapchain->device;
337 struct wined3d_context *context2;
338 float tex_left = src_rect->left;
339 float tex_top = src_rect->top;
340 float tex_right = src_rect->right;
341 float tex_bottom = src_rect->bottom;
342
343 context2 = context_acquire(device, swapchain->back_buffers[0]);
344 context_apply_blit_state(context2, device);
345
346 if (backbuffer->flags & SFLAG_NORMCOORD)
347 {
348 tex_left /= src_w;
349 tex_right /= src_w;
350 tex_top /= src_h;
351 tex_bottom /= src_h;
352 }
353
354 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
355 gl_filter = GL_NEAREST;
356
357 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
358 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
359
360 /* Set up the texture. The surface is not in a wined3d_texture
361 * container, so there are no D3D texture settings to dirtify. */
362 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
363 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
364 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
365
366 context_set_draw_buffer(context, GL_BACK);
367
368 /* Set the viewport to the destination rectandle, disable any projection
369 * transformation set up by context_apply_blit_state(), and draw a
370 * (-1,-1)-(1,1) quad.
371 *
372 * Back up viewport and matrix to avoid breaking last_was_blit
373 *
374 * Note that context_apply_blit_state() set up viewport and ortho to
375 * match the surface size - we want the GL drawable(=window) size. */
376 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
377 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
378 dst_rect->right, win_h - dst_rect->top);
379 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
380 gl_info->gl_ops.gl.p_glPushMatrix();
381 gl_info->gl_ops.gl.p_glLoadIdentity();
382
383 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
384 /* bottom left */
385 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
386 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
387
388 /* top left */
389 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
390 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
391
392 /* top right */
393 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
394 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
395
396 /* bottom right */
397 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
398 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
399 gl_info->gl_ops.gl.p_glEnd();
400
401 gl_info->gl_ops.gl.p_glPopMatrix();
402 gl_info->gl_ops.gl.p_glPopAttrib();
403
404 device->blitter->unset_shader(context->gl_info);
405 checkGLcall("Swapchain present blit(manual)\n");
406
407 context_release(context2);
408 }
409 }
410
411 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
412 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
413 {
414 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
415 const struct wined3d_fb_state *fb = &swapchain->device->fb;
416 const struct wined3d_gl_info *gl_info;
417 struct wined3d_context *context;
418 RECT src_rect, dst_rect;
419 BOOL render_to_fbo;
420
421 context = context_acquire(swapchain->device, back_buffer);
422 if (!context->valid)
423 {
424 context_release(context);
425 WARN("Invalid context, skipping present.\n");
426 return;
427 }
428
429 gl_info = context->gl_info;
430
431 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
432 if (swapchain->device->bCursorVisible &&
433 swapchain->device->cursorTexture &&
434 !swapchain->device->hardwareCursor)
435 {
436 struct wined3d_surface cursor;
437 RECT destRect =
438 {
439 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
440 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
441 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
442 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
443 };
444 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
445 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
446 * the application because we are only supposed to copy the information out. Using a fake surface
447 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
448 */
449 memset(&cursor, 0, sizeof(cursor));
450 cursor.resource.ref = 1;
451 cursor.resource.device = swapchain->device;
452 cursor.resource.pool = WINED3D_POOL_SCRATCH;
453 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
454 cursor.resource.type = WINED3D_RTYPE_SURFACE;
455 cursor.texture_name = swapchain->device->cursorTexture;
456 cursor.texture_target = GL_TEXTURE_2D;
457 cursor.texture_level = 0;
458 cursor.resource.width = swapchain->device->cursorWidth;
459 cursor.resource.height = swapchain->device->cursorHeight;
460 /* The cursor must have pow2 sizes */
461 cursor.pow2Width = cursor.resource.width;
462 cursor.pow2Height = cursor.resource.height;
463 /* The surface is in the texture */
464 cursor.flags |= SFLAG_INTEXTURE;
465 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
466 * which is exactly what we want :-)
467 */
468 if (swapchain->desc.windowed)
469 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
470 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
471 NULL, WINED3D_TEXF_POINT);
472 }
473
474 if (swapchain->device->logo_surface)
475 {
476 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
477 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
478
479 /* Blit the logo into the upper left corner of the drawable. */
480 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
481 NULL, WINED3D_TEXF_POINT);
482 }
483
484 TRACE("Presenting HDC %p.\n", context->hdc);
485
486 render_to_fbo = swapchain->render_to_fbo;
487
488 if (src_rect_in)
489 {
490 src_rect = *src_rect_in;
491 if (!render_to_fbo && (src_rect.left || src_rect.top
492 || src_rect.right != swapchain->desc.backbuffer_width
493 || src_rect.bottom != swapchain->desc.backbuffer_height))
494 {
495 render_to_fbo = TRUE;
496 }
497 }
498 else
499 {
500 src_rect.left = 0;
501 src_rect.top = 0;
502 src_rect.right = swapchain->desc.backbuffer_width;
503 src_rect.bottom = swapchain->desc.backbuffer_height;
504 }
505
506 if (dst_rect_in)
507 dst_rect = *dst_rect_in;
508 else
509 GetClientRect(swapchain->win_handle, &dst_rect);
510
511 if (!render_to_fbo && (dst_rect.left || dst_rect.top
512 || dst_rect.right != swapchain->desc.backbuffer_width
513 || dst_rect.bottom != swapchain->desc.backbuffer_height))
514 render_to_fbo = TRUE;
515
516 /* Rendering to a window of different size, presenting partial rectangles,
517 * or rendering to a different window needs help from FBO_blit or a textured
518 * draw. Render the swapchain to a FBO in the future.
519 *
520 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
521 * all these issues - this fails if the window is smaller than the backbuffer.
522 */
523 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
524 {
525 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
526 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
527 swapchain->render_to_fbo = TRUE;
528 swapchain_update_draw_bindings(swapchain);
529 }
530 else
531 {
532 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
533 }
534
535 if (swapchain->render_to_fbo)
536 {
537 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
538 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
539 * not allowed(they need the COPY swapeffect)
540 *
541 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
542 * the swap. */
543 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
544 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
545
546 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
547 }
548
549 if (swapchain->num_contexts > 1)
550 gl_info->gl_ops.gl.p_glFinish();
551
552 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
553 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
554
555 TRACE("SwapBuffers called, Starting new frame\n");
556 /* FPS support */
557 if (TRACE_ON(fps))
558 {
559 DWORD time = GetTickCount();
560 ++swapchain->frames;
561
562 /* every 1.5 seconds */
563 if (time - swapchain->prev_time > 1500)
564 {
565 TRACE_(fps)("%p @ approx %.2ffps\n",
566 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
567 swapchain->prev_time = time;
568 swapchain->frames = 0;
569 }
570 }
571
572 /* This is disabled, but the code left in for debug purposes.
573 *
574 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
575 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
576 * The Debug runtime does the same on Windows. However, a few games do not redraw the
577 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
578 *
579 * Tests show that the content of the back buffer after a discard flip is indeed not
580 * reliable, so no game can depend on the exact content. However, it resembles the
581 * old contents in some way, for example by showing fragments at other locations. In
582 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
583 * gets a dark background image. If we clear it with a bright ugly color, the game's
584 * bug shows up much more than it does on Windows, and the players see single pixels
585 * with wrong colors.
586 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
587 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
588 {
589 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
590
591 TRACE("Clearing the color buffer with cyan color\n");
592
593 wined3d_device_clear(swapchain->device, 0, NULL,
594 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
595 }
596
597 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
598 || (back_buffer->flags & SFLAG_INSYSMEM)))
599 {
600 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
601 * Doesn't work with render_to_fbo because we're not flipping
602 */
603 struct wined3d_surface *front = swapchain->front_buffer;
604
605 if (front->resource.size == back_buffer->resource.size)
606 {
607 DWORD fbflags;
608 flip_surface(front, back_buffer);
609
610 /* Tell the front buffer surface that is has been modified. However,
611 * the other locations were preserved during that, so keep the flags.
612 * This serves to update the emulated overlay, if any. */
613 fbflags = front->flags;
614 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
615 front->flags = fbflags;
616 }
617 else
618 {
619 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
620 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
621 }
622 }
623 else
624 {
625 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
626 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
627 * and INTEXTURE copies can keep their old content if they have any defined content.
628 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
629 * the texture / sysmem copy needs to be reloaded from the drawable
630 */
631 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
632 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
633 }
634
635 if (fb->depth_stencil)
636 {
637 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
638 || fb->depth_stencil->flags & SFLAG_DISCARD)
639 {
640 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
641 fb->depth_stencil->resource.width,
642 fb->depth_stencil->resource.height);
643 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
644 {
645 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
646 swapchain->device->onscreen_depth_stencil = NULL;
647 }
648 }
649 }
650
651 context_release(context);
652 }
653
654 static const struct wined3d_swapchain_ops swapchain_gl_ops =
655 {
656 swapchain_gl_present,
657 };
658
659 /* Helper function that blits the front buffer contents to the target window. */
660 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
661 {
662 const struct wined3d_surface *front;
663 POINT offset = {0, 0};
664 HDC src_dc, dst_dc;
665 RECT draw_rect;
666 HWND window;
667
668 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
669
670 front = swapchain->front_buffer;
671 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
672 return;
673
674 if (front->resource.map_count)
675 ERR("Trying to blit a mapped surface.\n");
676
677 TRACE("Copying surface %p to screen.\n", front);
678
679 src_dc = front->hDC;
680 window = swapchain->win_handle;
681 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
682
683 /* Front buffer coordinates are screen coordinates. Map them to the
684 * destination window if not fullscreened. */
685 if (swapchain->desc.windowed)
686 ClientToScreen(window, &offset);
687
688 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
689
690 draw_rect.left = 0;
691 draw_rect.right = front->resource.width;
692 draw_rect.top = 0;
693 draw_rect.bottom = front->resource.height;
694
695 if (rect)
696 IntersectRect(&draw_rect, &draw_rect, rect);
697
698 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
699 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
700 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
701 ReleaseDC(window, dst_dc);
702 }
703
704 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
705 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
706 {
707 struct wined3d_surface *front, *back;
708
709 front = swapchain->front_buffer;
710 back = swapchain->back_buffers[0];
711
712 /* Flip the DC. */
713 {
714 HDC tmp;
715 tmp = front->hDC;
716 front->hDC = back->hDC;
717 back->hDC = tmp;
718 }
719
720 /* Flip the DIBsection. */
721 {
722 HBITMAP tmp;
723 tmp = front->dib.DIBsection;
724 front->dib.DIBsection = back->dib.DIBsection;
725 back->dib.DIBsection = tmp;
726 }
727
728 /* Flip the surface data. */
729 {
730 void *tmp;
731
732 tmp = front->dib.bitmap_data;
733 front->dib.bitmap_data = back->dib.bitmap_data;
734 back->dib.bitmap_data = tmp;
735
736 tmp = front->resource.allocatedMemory;
737 front->resource.allocatedMemory = back->resource.allocatedMemory;
738 back->resource.allocatedMemory = tmp;
739
740 if (front->resource.heap_memory)
741 ERR("GDI Surface %p has heap memory allocated.\n", front);
742
743 if (back->resource.heap_memory)
744 ERR("GDI Surface %p has heap memory allocated.\n", back);
745 }
746
747 /* FPS support */
748 if (TRACE_ON(fps))
749 {
750 static LONG prev_time, frames;
751 DWORD time = GetTickCount();
752
753 ++frames;
754
755 /* every 1.5 seconds */
756 if (time - prev_time > 1500)
757 {
758 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
759 prev_time = time;
760 frames = 0;
761 }
762 }
763
764 x11_copy_to_screen(swapchain, NULL);
765 }
766
767 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
768 {
769 swapchain_gdi_present,
770 };
771
772 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
773 {
774 RECT client_rect;
775
776 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
777 return;
778
779 if (!swapchain->desc.backbuffer_count)
780 {
781 TRACE("Single buffered rendering.\n");
782 swapchain->render_to_fbo = FALSE;
783 return;
784 }
785
786 GetClientRect(swapchain->win_handle, &client_rect);
787
788 TRACE("Backbuffer %ux%u, window %ux%u.\n",
789 swapchain->desc.backbuffer_width,
790 swapchain->desc.backbuffer_height,
791 client_rect.right, client_rect.bottom);
792 TRACE("Multisample type %#x, quality %#x.\n",
793 swapchain->desc.multisample_type,
794 swapchain->desc.multisample_quality);
795
796 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
797 && swapchain->desc.backbuffer_width == client_rect.right
798 && swapchain->desc.backbuffer_height == client_rect.bottom)
799 {
800 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
801 swapchain->render_to_fbo = FALSE;
802 return;
803 }
804
805 TRACE("Rendering to FBO.\n");
806 swapchain->render_to_fbo = TRUE;
807 }
808
809 /* Do not call while under the GL lock. */
810 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
811 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
812 {
813 const struct wined3d_adapter *adapter = device->adapter;
814 struct wined3d_resource_desc surface_desc;
815 BOOL displaymode_set = FALSE;
816 RECT client_rect;
817 HWND window;
818 HRESULT hr;
819 UINT i;
820
821 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
822 {
823 FIXME("The application requested %u back buffers, this is not supported.\n",
824 desc->backbuffer_count);
825 return WINED3DERR_INVALIDCALL;
826 }
827
828 if (desc->backbuffer_count > 1)
829 {
830 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
831 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
832 }
833
834 if (device->wined3d->flags & WINED3D_NO3D)
835 swapchain->swapchain_ops = &swapchain_gdi_ops;
836 else
837 swapchain->swapchain_ops = &swapchain_gl_ops;
838
839 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
840
841 swapchain->device = device;
842 swapchain->parent = parent;
843 swapchain->parent_ops = parent_ops;
844 swapchain->ref = 1;
845 swapchain->win_handle = window;
846 swapchain->device_window = window;
847
848 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
849 adapter->ordinal, &swapchain->original_mode, NULL)))
850 {
851 ERR("Failed to get current display mode, hr %#x.\n", hr);
852 goto err;
853 }
854
855 GetClientRect(window, &client_rect);
856 if (desc->windowed
857 && (!desc->backbuffer_width || !desc->backbuffer_height
858 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
859 {
860
861 if (!desc->backbuffer_width)
862 {
863 desc->backbuffer_width = client_rect.right;
864 TRACE("Updating width to %u.\n", desc->backbuffer_width);
865 }
866
867 if (!desc->backbuffer_height)
868 {
869 desc->backbuffer_height = client_rect.bottom;
870 TRACE("Updating height to %u.\n", desc->backbuffer_height);
871 }
872
873 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
874 {
875 desc->backbuffer_format = swapchain->original_mode.format_id;
876 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
877 }
878 }
879 swapchain->desc = *desc;
880 swapchain_update_render_to_fbo(swapchain);
881
882 TRACE("Creating front buffer.\n");
883
884 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
885 surface_desc.format = swapchain->desc.backbuffer_format;
886 surface_desc.multisample_type = swapchain->desc.multisample_type;
887 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
888 surface_desc.usage = WINED3DUSAGE_RENDERTARGET;
889 surface_desc.pool = WINED3D_POOL_DEFAULT;
890 surface_desc.width = swapchain->desc.backbuffer_width;
891 surface_desc.height = swapchain->desc.backbuffer_height;
892 surface_desc.depth = 1;
893 surface_desc.size = 0;
894
895 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
896 parent, &surface_desc, &swapchain->front_buffer)))
897 {
898 WARN("Failed to create front buffer, hr %#x.\n", hr);
899 goto err;
900 }
901
902 surface_set_swapchain(swapchain->front_buffer, swapchain);
903 if (!(device->wined3d->flags & WINED3D_NO3D))
904 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
905
906 /* MSDN says we're only allowed a single fullscreen swapchain per device,
907 * so we should really check to see if there is a fullscreen swapchain
908 * already. Does a single head count as full screen? */
909
910 if (!desc->windowed)
911 {
912 struct wined3d_display_mode mode;
913
914 /* Change the display settings */
915 mode.width = desc->backbuffer_width;
916 mode.height = desc->backbuffer_height;
917 mode.format_id = desc->backbuffer_format;
918 mode.refresh_rate = desc->refresh_rate;
919 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
920
921 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
922 {
923 WARN("Failed to set display mode, hr %#x.\n", hr);
924 goto err;
925 }
926 displaymode_set = TRUE;
927 }
928
929 if (!(device->wined3d->flags & WINED3D_NO3D))
930 {
931 static const enum wined3d_format_id formats[] =
932 {
933 WINED3DFMT_D24_UNORM_S8_UINT,
934 WINED3DFMT_D32_UNORM,
935 WINED3DFMT_R24_UNORM_X8_TYPELESS,
936 WINED3DFMT_D16_UNORM,
937 WINED3DFMT_S1_UINT_D15_UNORM
938 };
939
940 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
941
942 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
943 if (!swapchain->context)
944 {
945 ERR("Failed to create the context array.\n");
946 hr = E_OUTOFMEMORY;
947 goto err;
948 }
949 swapchain->num_contexts = 1;
950
951 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
952 * You are able to add a depth + stencil surface at a later stage when you need it.
953 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
954 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
955 * context, need torecreate shaders, textures and other resources.
956 *
957 * The context manager already takes care of the state problem and for the other tasks code from Reset
958 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
959 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
960 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
961 * issue needs to be fixed. */
962 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
963 {
964 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
965 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
966 if (swapchain->context[0]) break;
967 TRACE("Depth stencil format %s is not supported, trying next format\n",
968 debug_d3dformat(formats[i]));
969 }
970
971 if (!swapchain->context[0])
972 {
973 WARN("Failed to create context.\n");
974 hr = WINED3DERR_NOTAVAILABLE;
975 goto err;
976 }
977
978 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
979 && (!desc->enable_auto_depth_stencil
980 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
981 {
982 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
983 }
984 context_release(swapchain->context[0]);
985 }
986
987 if (swapchain->desc.backbuffer_count > 0)
988 {
989 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
990 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
991 if (!swapchain->back_buffers)
992 {
993 ERR("Failed to allocate backbuffer array memory.\n");
994 hr = E_OUTOFMEMORY;
995 goto err;
996 }
997
998 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
999 {
1000 TRACE("Creating back buffer %u.\n", i);
1001 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1002 parent, &surface_desc, &swapchain->back_buffers[i])))
1003 {
1004 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1005 goto err;
1006 }
1007 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
1008 }
1009 }
1010
1011 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1012 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1013 {
1014 TRACE("Creating depth/stencil buffer.\n");
1015 if (!device->auto_depth_stencil)
1016 {
1017 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
1018 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1019
1020 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1021 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1022 {
1023 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1024 goto err;
1025 }
1026 }
1027 }
1028
1029 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1030
1031 return WINED3D_OK;
1032
1033 err:
1034 if (displaymode_set)
1035 {
1036 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1037 adapter->ordinal, &swapchain->original_mode)))
1038 ERR("Failed to restore display mode.\n");
1039 ClipCursor(NULL);
1040 }
1041
1042 if (swapchain->back_buffers)
1043 {
1044 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1045 {
1046 if (swapchain->back_buffers[i])
1047 {
1048 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1049 wined3d_surface_decref(swapchain->back_buffers[i]);
1050 }
1051 }
1052 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1053 }
1054
1055 if (swapchain->context)
1056 {
1057 if (swapchain->context[0])
1058 {
1059 context_release(swapchain->context[0]);
1060 context_destroy(device, swapchain->context[0]);
1061 swapchain->num_contexts = 0;
1062 }
1063 HeapFree(GetProcessHeap(), 0, swapchain->context);
1064 }
1065
1066 if (swapchain->front_buffer)
1067 {
1068 surface_set_swapchain(swapchain->front_buffer, NULL);
1069 wined3d_surface_decref(swapchain->front_buffer);
1070 }
1071
1072 return hr;
1073 }
1074
1075 /* Do not call while under the GL lock. */
1076 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1077 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1078 {
1079 struct wined3d_swapchain *object;
1080 HRESULT hr;
1081
1082 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1083 device, desc, parent, parent_ops, swapchain);
1084
1085 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1086 if (!object)
1087 return E_OUTOFMEMORY;
1088
1089 hr = swapchain_init(object, device, desc, parent, parent_ops);
1090 if (FAILED(hr))
1091 {
1092 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1093 HeapFree(GetProcessHeap(), 0, object);
1094 return hr;
1095 }
1096
1097 TRACE("Created swapchain %p.\n", object);
1098 *swapchain = object;
1099
1100 return WINED3D_OK;
1101 }
1102
1103 /* Do not call while under the GL lock. */
1104 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1105 {
1106 struct wined3d_context **newArray;
1107 struct wined3d_context *ctx;
1108
1109 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1110
1111 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1112 {
1113 ERR("Failed to create a new context for the swapchain\n");
1114 return NULL;
1115 }
1116 context_release(ctx);
1117
1118 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1119 if(!newArray) {
1120 ERR("Out of memory when trying to allocate a new context array\n");
1121 context_destroy(swapchain->device, ctx);
1122 return NULL;
1123 }
1124 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1125 HeapFree(GetProcessHeap(), 0, swapchain->context);
1126 newArray[swapchain->num_contexts] = ctx;
1127 swapchain->context = newArray;
1128 swapchain->num_contexts++;
1129
1130 TRACE("Returning context %p\n", ctx);
1131 return ctx;
1132 }
1133
1134 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1135 {
1136 unsigned int i;
1137
1138 for (i = 0; i < swapchain->num_contexts; ++i)
1139 {
1140 context_destroy(swapchain->device, swapchain->context[i]);
1141 }
1142 swapchain->num_contexts = 0;
1143 }
1144
1145 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1146 {
1147 DWORD tid = GetCurrentThreadId();
1148 unsigned int i;
1149
1150 for (i = 0; i < swapchain->num_contexts; ++i)
1151 {
1152 if (swapchain->context[i]->tid == tid)
1153 return swapchain->context[i];
1154 }
1155
1156 /* Create a new context for the thread */
1157 return swapchain_create_context(swapchain);
1158 }
1159
1160 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1161 {
1162 /* The drawable size of an onscreen drawable is the surface size.
1163 * (Actually: The window size, but the surface is created in window size) */
1164 *width = context->current_rt->resource.width;
1165 *height = context->current_rt->resource.height;
1166 }
1167
1168 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1169 {
1170 if (!swapchain->backup_dc)
1171 {
1172 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1173
1174 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1175 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1176 {
1177 ERR("Failed to create a window.\n");
1178 return NULL;
1179 }
1180
1181 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1182 {
1183 ERR("Failed to get a DC.\n");
1184 DestroyWindow(swapchain->backup_wnd);
1185 swapchain->backup_wnd = NULL;
1186 return NULL;
1187 }
1188 }
1189
1190 return swapchain->backup_dc;
1191 }
1192
1193 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1194 {
1195 UINT i;
1196
1197 surface_update_draw_binding(swapchain->front_buffer);
1198
1199 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1200 {
1201 surface_update_draw_binding(swapchain->back_buffers[i]);
1202 }
1203 }