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