VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/swapchain.c@ 58152

Last change on this file since 58152 was 51270, checked in by vboxsync, 11 years ago

wine: update to 1.6.2

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.7 KB
Line 
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/*
24 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
25 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
26 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
27 * a choice of LGPL license versions is made available with the language indicating
28 * that LGPLv2 or any later version may be used, or where a choice of which version
29 * of the LGPL is applied is otherwise unspecified.
30 */
31
32#include "config.h"
33#include "wine/port.h"
34#include "wined3d_private.h"
35
36WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37WINE_DECLARE_DEBUG_CHANNEL(fps);
38
39#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
40static VOID swapchain_cleanup_rt_refs(struct wined3d_swapchain *swapchain, struct wined3d_surface * rt, int iBb)
41{
42 struct wined3d_device * device = swapchain->device;
43 struct wined3d_context *context;
44 UINT i;
45 for (i = 0; i < device->context_count; ++i)
46 {
47 context = device->contexts[i];
48
49 if (rt == context->current_rt)
50 {
51 context->current_rt = NULL;
52 }
53 }
54
55 if (device->swapchain_count)
56 {
57 struct wined3d_swapchain *default_swapchain = (struct wined3d_swapchain*)device->swapchains[0];
58 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
59 {
60 if (device->fb.render_targets[i] == rt)
61 {
62 struct wined3d_surface *new_rt;
63 if (i)
64 new_rt = NULL;
65 else if (iBb == -1) /* front buffer */
66 new_rt = default_swapchain->front_buffer;
67 else
68 new_rt = default_swapchain->back_buffers ? default_swapchain->back_buffers[0] : default_swapchain->front_buffer;
69
70 wined3d_device_set_render_target(device, i, new_rt, TRUE);
71 }
72 }
73 }
74}
75
76static VOID swapchain_cleanup_refs(struct wined3d_swapchain *swapchain)
77{
78 /* first make sure the swapchain is not used by anyone */
79 struct wined3d_device * device = swapchain->device;
80 struct wined3d_context *context;
81 UINT i;
82
83 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
84 * is the last buffer to be destroyed, FindContext() depends on that. */
85 if (swapchain->front_buffer)
86 {
87 surface_set_swapchain(swapchain->front_buffer, NULL);
88 if (wined3d_surface_decref(swapchain->front_buffer))
89 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
90 swapchain->front_buffer = NULL;
91 }
92
93 if (swapchain->back_buffers)
94 {
95 i = swapchain->desc.backbuffer_count;
96
97 while (i--)
98 {
99 surface_set_swapchain(swapchain->back_buffers[i], NULL);
100 if (wined3d_surface_decref(swapchain->back_buffers[i]))
101 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
102 }
103 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
104 swapchain->back_buffers = NULL;
105 }
106
107 for (i = 0; i < device->context_count; ++i)
108 {
109 context = device->contexts[i];
110 /* pretty hacky, @todo: check if the context is acquired and re-acquire it with a new swapchain */
111 if (context->swapchain == swapchain)
112 {
113 context->swapchain = NULL;
114 }
115 }
116//
117// if (swapchain->front_buffer)
118// swapchain_cleanup_rt_refs(swapchain, swapchain->front_buffer, -1);
119//
120// if (swapchain->back_buffers)
121// {
122// for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
123// {
124// swapchain_cleanup_rt_refs(swapchain, swapchain->back_buffers[i], i);
125// }
126// }
127}
128#endif
129
130#ifdef VBOX_WITH_WDDM
131
132struct wined3d_swapchain * swapchain_find(struct wined3d_device * device, HWND hWnd)
133{
134 UINT i;
135 for (i = 0; i < device->swapchain_count; ++i)
136 {
137 struct wined3d_swapchain *swapchain = device->swapchains[i];
138 if (swapchain->win_handle == hWnd)
139 {
140 return swapchain;
141 }
142 }
143
144 return NULL;
145}
146
147static VOID swapchain_invalidate(struct wined3d_swapchain *swapchain)
148{
149 swapchain_cleanup_refs(swapchain);
150
151 swapchain->win_handle = NULL;
152 swapchain->hDC = NULL;
153}
154#endif
155
156/* Do not call while under the GL lock. */
157static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
158{
159#ifndef VBOX_WITH_WDDM
160 HRESULT hr;
161#endif
162 UINT i;
163
164 TRACE("Destroying swapchain %p.\n", swapchain);
165
166#ifndef VBOX_WITH_WDDM
167 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
168#endif
169
170 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
171 * is the last buffer to be destroyed, FindContext() depends on that. */
172 if (swapchain->front_buffer)
173 {
174 surface_set_swapchain(swapchain->front_buffer, NULL);
175 if (wined3d_surface_decref(swapchain->front_buffer))
176 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
177 swapchain->front_buffer = NULL;
178 }
179
180 if (swapchain->back_buffers)
181 {
182 i = swapchain->desc.backbuffer_count;
183
184 while (i--)
185 {
186 surface_set_swapchain(swapchain->back_buffers[i], NULL);
187 if (wined3d_surface_decref(swapchain->back_buffers[i]))
188 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
189 }
190 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
191 swapchain->back_buffers = NULL;
192 }
193
194#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
195 swapchain_cleanup_refs(swapchain);
196#endif
197
198#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
199 for (i = 0; i < swapchain->num_contexts; ++i)
200 {
201 context_destroy(swapchain->device, swapchain->context[i]);
202 }
203 HeapFree(GetProcessHeap(), 0, swapchain->context);
204#endif
205#ifndef VBOX_WITH_WDDM
206 /* Restore the screen resolution if we rendered in fullscreen.
207 * This will restore the screen resolution to what it was before creating
208 * the swapchain. In case of d3d8 and d3d9 this will be the original
209 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
210 * sets the resolution before starting up Direct3D, thus orig_width and
211 * orig_height will be equal to the modes in the presentation params. */
212 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
213 {
214 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
215 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
216 ERR("Failed to restore display mode, hr %#x.\n", hr);
217 }
218
219 if (swapchain->backup_dc)
220 {
221 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
222
223 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
224 DestroyWindow(swapchain->backup_wnd);
225 }
226#endif
227#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
228# ifdef VBOX_WITH_WDDM
229 if(swapchain->win_handle) {
230 VBoxExtWndDestroy(swapchain->win_handle, swapchain->hDC);
231 swapchain_invalidate(swapchain);
232 }
233# endif
234#endif
235}
236
237ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
238{
239 ULONG refcount = InterlockedIncrement(&swapchain->ref);
240
241 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
242
243 return refcount;
244}
245
246/* Do not call while under the GL lock. */
247ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
248{
249 ULONG refcount = InterlockedDecrement(&swapchain->ref);
250
251 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
252
253 if (!refcount)
254 {
255 swapchain_cleanup(swapchain);
256 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
257 HeapFree(GetProcessHeap(), 0, swapchain);
258 }
259
260 return refcount;
261}
262
263void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
264{
265 TRACE("swapchain %p.\n", swapchain);
266
267 return swapchain->parent;
268}
269
270void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
271{
272#ifdef VBOX_WITH_WDDM
273 if (window)
274 ERR("not expected!");
275#else
276 if (!window)
277 window = swapchain->device_window;
278 if (window == swapchain->win_handle)
279 return;
280
281 TRACE("Setting swapchain %p window from %p to %p.\n",
282 swapchain, swapchain->win_handle, window);
283 swapchain->win_handle = window;
284#endif
285}
286
287HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
288 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
289 const RGNDATA *dirty_region, DWORD flags)
290{
291 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
292 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
293 dst_window_override, dirty_region, flags);
294
295 if (flags)
296 FIXME("Ignoring flags %#x.\n", flags);
297
298 if (!swapchain->back_buffers)
299 {
300 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
301 return WINED3DERR_INVALIDCALL;
302 }
303
304 wined3d_swapchain_set_window(swapchain, dst_window_override);
305
306 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
307
308 return WINED3D_OK;
309}
310
311HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
312 struct wined3d_surface *dst_surface)
313{
314 struct wined3d_surface *src_surface;
315 RECT src_rect, dst_rect;
316
317 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
318
319 src_surface = swapchain->front_buffer;
320 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
321 dst_rect = src_rect;
322
323#ifndef VBOX_WITH_WINE_FIXES
324 if (swapchain->desc.windowed)
325 {
326 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
327 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
328 wine_dbgstr_rect(&dst_rect));
329 }
330#endif
331
332 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
333}
334
335struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
336 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
337{
338 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
339 swapchain, back_buffer_idx, type);
340
341#ifdef VBOX_WITH_WDDM
342 if (back_buffer_idx == ~0UL)
343 {
344 return swapchain->front_buffer;
345 }
346#endif
347
348 /* Return invalid if there is no backbuffer array, otherwise it will
349 * crash when ddraw is used (there swapchain->back_buffers is always
350 * NULL). We need this because this function is called from
351 * stateblock_init_default_state() to get the default scissorrect
352 * dimensions. */
353 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
354 {
355 WARN("Invalid back buffer index.\n");
356 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
357 * here in wined3d to avoid problems in other libs. */
358 return NULL;
359 }
360
361 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
362
363 return swapchain->back_buffers[back_buffer_idx];
364}
365
366HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
367 struct wined3d_raster_status *raster_status)
368{
369 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
370
371 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
372 swapchain->device->adapter->ordinal, raster_status);
373}
374
375HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
376 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
377{
378 HRESULT hr;
379
380 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
381
382 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
383 swapchain->device->adapter->ordinal, mode, rotation);
384
385 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
386 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
387
388 return hr;
389}
390
391struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
392{
393 TRACE("swapchain %p.\n", swapchain);
394
395 return swapchain->device;
396}
397
398void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
399 struct wined3d_swapchain_desc *desc)
400{
401 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
402
403 *desc = swapchain->desc;
404}
405
406HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
407 DWORD flags, const struct wined3d_gamma_ramp *ramp)
408{
409 HDC dc;
410
411 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
412
413 if (flags)
414 FIXME("Ignoring flags %#x.\n", flags);
415
416#ifdef VBOX_WITH_WDDM
417 dc = GetDC(swapchain->win_handle);
418#else
419 dc = GetDC(swapchain->device_window);
420#endif
421 SetDeviceGammaRamp(dc, (void *)ramp);
422#ifdef VBOX_WITH_WDDM
423 ReleaseDC(swapchain->win_handle, dc);
424#else
425 ReleaseDC(swapchain->device_window, dc);
426#endif
427
428 return WINED3D_OK;
429}
430
431HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
432 struct wined3d_gamma_ramp *ramp)
433{
434 HDC dc;
435
436 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
437
438#ifdef VBOX_WITH_WDDM
439 dc = GetDC(swapchain->win_handle);
440#else
441 dc = GetDC(swapchain->device_window);
442#endif
443 GetDeviceGammaRamp(dc, ramp);
444#ifdef VBOX_WITH_WDDM
445 ReleaseDC(swapchain->win_handle, dc);
446#else
447 ReleaseDC(swapchain->device_window, dc);
448#endif
449
450 return WINED3D_OK;
451}
452
453/* A GL context is provided by the caller */
454static void swapchain_blit(const struct wined3d_swapchain *swapchain,
455 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
456{
457 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
458 UINT src_w = src_rect->right - src_rect->left;
459 UINT src_h = src_rect->bottom - src_rect->top;
460 GLenum gl_filter;
461 const struct wined3d_gl_info *gl_info = context->gl_info;
462 RECT win_rect;
463 UINT win_h;
464
465 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
466 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
467
468 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
469 gl_filter = GL_NEAREST;
470 else
471 gl_filter = GL_LINEAR;
472
473 GetClientRect(swapchain->win_handle, &win_rect);
474 win_h = win_rect.bottom - win_rect.top;
475
476 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
477 {
478 DWORD location = SFLAG_INTEXTURE;
479
480 if (backbuffer->resource.multisample_type)
481 {
482 location = SFLAG_INRB_RESOLVED;
483 surface_load_location(backbuffer, location, NULL);
484 }
485
486 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
487 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
488 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
489
490 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
491 context_set_draw_buffer(context, GL_BACK);
492 context_invalidate_state(context, STATE_FRAMEBUFFER);
493
494 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
495 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
496 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
497 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
498 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
499
500 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
501 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
502
503 /* Note that the texture is upside down */
504 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
505 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
506 GL_COLOR_BUFFER_BIT, gl_filter);
507 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
508 }
509 else
510 {
511 struct wined3d_device *device = swapchain->device;
512 struct wined3d_context *context2;
513 float tex_left = src_rect->left;
514 float tex_top = src_rect->top;
515 float tex_right = src_rect->right;
516 float tex_bottom = src_rect->bottom;
517
518 context2 = context_acquire(device, swapchain->back_buffers[0]);
519 context_apply_blit_state(context2, device);
520
521 if (backbuffer->flags & SFLAG_NORMCOORD)
522 {
523 tex_left /= src_w;
524 tex_right /= src_w;
525 tex_top /= src_h;
526 tex_bottom /= src_h;
527 }
528
529 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
530 gl_filter = GL_NEAREST;
531
532 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
533 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
534
535 /* Set up the texture. The surface is not in a wined3d_texture
536 * container, so there are no D3D texture settings to dirtify. */
537 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
538 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
539 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
540
541 context_set_draw_buffer(context, GL_BACK);
542
543 /* Set the viewport to the destination rectandle, disable any projection
544 * transformation set up by context_apply_blit_state(), and draw a
545 * (-1,-1)-(1,1) quad.
546 *
547 * Back up viewport and matrix to avoid breaking last_was_blit
548 *
549 * Note that context_apply_blit_state() set up viewport and ortho to
550 * match the surface size - we want the GL drawable(=window) size. */
551 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
552 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
553 dst_rect->right, win_h - dst_rect->top);
554 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
555 gl_info->gl_ops.gl.p_glPushMatrix();
556 gl_info->gl_ops.gl.p_glLoadIdentity();
557
558 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
559 /* bottom left */
560 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
561 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
562
563 /* top left */
564 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
565 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
566
567 /* top right */
568 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
569 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
570
571 /* bottom right */
572 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
573 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
574 gl_info->gl_ops.gl.p_glEnd();
575
576 gl_info->gl_ops.gl.p_glPopMatrix();
577 gl_info->gl_ops.gl.p_glPopAttrib();
578
579 device->blitter->unset_shader(context->gl_info);
580 checkGLcall("Swapchain present blit(manual)\n");
581
582 context_release(context2);
583 }
584}
585
586static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
587 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
588{
589 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
590 const struct wined3d_fb_state *fb = &swapchain->device->fb;
591 const struct wined3d_gl_info *gl_info;
592 struct wined3d_context *context;
593 RECT src_rect, dst_rect;
594 BOOL render_to_fbo;
595
596 context = context_acquire(swapchain->device, back_buffer);
597 if (!context->valid)
598 {
599 context_release(context);
600 WARN("Invalid context, skipping present.\n");
601 return;
602 }
603
604 gl_info = context->gl_info;
605
606 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
607 if (swapchain->device->bCursorVisible &&
608 swapchain->device->cursorTexture &&
609 !swapchain->device->hardwareCursor)
610 {
611 struct wined3d_surface cursor;
612 RECT destRect =
613 {
614 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
615 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
616 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
617 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
618 };
619 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
620 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
621 * the application because we are only supposed to copy the information out. Using a fake surface
622 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
623 */
624 memset(&cursor, 0, sizeof(cursor));
625 cursor.resource.ref = 1;
626 cursor.resource.device = swapchain->device;
627 cursor.resource.pool = WINED3D_POOL_SCRATCH;
628 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
629 cursor.resource.type = WINED3D_RTYPE_SURFACE;
630 cursor.texture_name = swapchain->device->cursorTexture;
631 cursor.texture_target = GL_TEXTURE_2D;
632 cursor.texture_level = 0;
633 cursor.resource.width = swapchain->device->cursorWidth;
634 cursor.resource.height = swapchain->device->cursorHeight;
635 /* The cursor must have pow2 sizes */
636 cursor.pow2Width = cursor.resource.width;
637 cursor.pow2Height = cursor.resource.height;
638 /* The surface is in the texture */
639 cursor.flags |= SFLAG_INTEXTURE;
640 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
641 * which is exactly what we want :-)
642 */
643#ifndef VBOX_WITH_WINE_FIXES
644 if (swapchain->desc.windowed)
645 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
646#endif
647 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
648 NULL, WINED3D_TEXF_POINT);
649 }
650
651 if (swapchain->device->logo_surface)
652 {
653 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
654 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
655
656 /* Blit the logo into the upper left corner of the drawable. */
657 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
658 NULL, WINED3D_TEXF_POINT);
659 }
660
661#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
662 TRACE("Presenting HDC %p.\n", context->hdc);
663#else
664 TRACE("Presenting HDC %p.\n", context->swapchain->hDC);
665#endif
666
667 render_to_fbo = swapchain->render_to_fbo;
668
669 if (src_rect_in)
670 {
671 src_rect = *src_rect_in;
672 if (!render_to_fbo && (src_rect.left || src_rect.top
673 || src_rect.right != swapchain->desc.backbuffer_width
674 || src_rect.bottom != swapchain->desc.backbuffer_height))
675 {
676 render_to_fbo = TRUE;
677 }
678 }
679 else
680 {
681 src_rect.left = 0;
682 src_rect.top = 0;
683 src_rect.right = swapchain->desc.backbuffer_width;
684 src_rect.bottom = swapchain->desc.backbuffer_height;
685 }
686
687 if (dst_rect_in)
688 dst_rect = *dst_rect_in;
689 else
690 GetClientRect(swapchain->win_handle, &dst_rect);
691
692 if (!render_to_fbo && (dst_rect.left || dst_rect.top
693 || dst_rect.right != swapchain->desc.backbuffer_width
694 || dst_rect.bottom != swapchain->desc.backbuffer_height))
695 render_to_fbo = TRUE;
696
697 /* Rendering to a window of different size, presenting partial rectangles,
698 * or rendering to a different window needs help from FBO_blit or a textured
699 * draw. Render the swapchain to a FBO in the future.
700 *
701 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
702 * all these issues - this fails if the window is smaller than the backbuffer.
703 */
704 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
705 {
706 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
707 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
708 swapchain->render_to_fbo = TRUE;
709 swapchain_update_draw_bindings(swapchain);
710 }
711 else
712 {
713 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
714 }
715
716 if (swapchain->render_to_fbo)
717 {
718 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
719 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
720 * not allowed(they need the COPY swapeffect)
721 *
722 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
723 * the swap. */
724 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
725 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
726
727 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
728 }
729
730#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
731 if (swapchain->num_contexts > 1)
732 gl_info->gl_ops.gl.p_glFinish();
733#endif
734
735#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
736# ifdef DEBUG
737 {
738 HWND wnd = WindowFromDC(context->swapchain->hDC);
739 Assert(wnd == context->swapchain->win_handle);
740 }
741# endif
742 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
743 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->swapchain->hDC); /* TODO: cycle through the swapchain buffers */
744#else
745
746 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
747 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
748#endif
749
750 TRACE("SwapBuffers called, Starting new frame\n");
751 /* FPS support */
752 if (TRACE_ON(fps))
753 {
754 DWORD time = GetTickCount();
755 ++swapchain->frames;
756
757 /* every 1.5 seconds */
758 if (time - swapchain->prev_time > 1500)
759 {
760 TRACE_(fps)("%p @ approx %.2ffps\n",
761 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
762 swapchain->prev_time = time;
763 swapchain->frames = 0;
764 }
765 }
766
767 /* This is disabled, but the code left in for debug purposes.
768 *
769 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
770 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
771 * The Debug runtime does the same on Windows. However, a few games do not redraw the
772 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
773 *
774 * Tests show that the content of the back buffer after a discard flip is indeed not
775 * reliable, so no game can depend on the exact content. However, it resembles the
776 * old contents in some way, for example by showing fragments at other locations. In
777 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
778 * gets a dark background image. If we clear it with a bright ugly color, the game's
779 * bug shows up much more than it does on Windows, and the players see single pixels
780 * with wrong colors.
781 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
782 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
783 {
784 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
785
786 TRACE("Clearing the color buffer with cyan color\n");
787
788 wined3d_device_clear(swapchain->device, 0, NULL,
789 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
790 }
791
792 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
793 || (back_buffer->flags & SFLAG_INSYSMEM)))
794 {
795 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
796 * Doesn't work with render_to_fbo because we're not flipping
797 */
798 struct wined3d_surface *front = swapchain->front_buffer;
799
800 if (front->resource.size == back_buffer->resource.size)
801 {
802 DWORD fbflags;
803 flip_surface(front, back_buffer);
804
805 /* Tell the front buffer surface that is has been modified. However,
806 * the other locations were preserved during that, so keep the flags.
807 * This serves to update the emulated overlay, if any. */
808 fbflags = front->flags;
809 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
810 front->flags = fbflags;
811 }
812 else
813 {
814 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
815 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
816 }
817 }
818 else
819 {
820 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
821 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
822 * and INTEXTURE copies can keep their old content if they have any defined content.
823 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
824 * the texture / sysmem copy needs to be reloaded from the drawable
825 */
826 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
827 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
828 }
829
830 if (fb->depth_stencil)
831 {
832 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
833 || fb->depth_stencil->flags & SFLAG_DISCARD)
834 {
835 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
836 fb->depth_stencil->resource.width,
837 fb->depth_stencil->resource.height);
838 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
839 {
840 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
841 swapchain->device->onscreen_depth_stencil = NULL;
842 }
843 }
844 }
845
846 context_release(context);
847}
848
849static const struct wined3d_swapchain_ops swapchain_gl_ops =
850{
851 swapchain_gl_present,
852};
853
854/* Helper function that blits the front buffer contents to the target window. */
855void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
856{
857 const struct wined3d_surface *front;
858 POINT offset = {0, 0};
859 HDC src_dc, dst_dc;
860 RECT draw_rect;
861 HWND window;
862
863 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
864
865 front = swapchain->front_buffer;
866 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
867 return;
868
869 if (front->resource.map_count)
870 ERR("Trying to blit a mapped surface.\n");
871
872 TRACE("Copying surface %p to screen.\n", front);
873
874 src_dc = front->hDC;
875 window = swapchain->win_handle;
876 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
877
878 /* Front buffer coordinates are screen coordinates. Map them to the
879 * destination window if not fullscreened. */
880 if (swapchain->desc.windowed)
881 {
882#ifndef VBOX_WITH_WINE_FIXES
883 ClientToScreen(window, &offset);
884#else
885 ERR("should not be here!");
886#endif
887 }
888
889 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
890
891 draw_rect.left = 0;
892 draw_rect.right = front->resource.width;
893 draw_rect.top = 0;
894 draw_rect.bottom = front->resource.height;
895
896 if (rect)
897 IntersectRect(&draw_rect, &draw_rect, rect);
898
899 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
900 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
901 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
902 ReleaseDC(window, dst_dc);
903}
904
905static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
906 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
907{
908 struct wined3d_surface *front, *back;
909
910 front = swapchain->front_buffer;
911 back = swapchain->back_buffers[0];
912
913 /* Flip the DC. */
914 {
915 HDC tmp;
916 tmp = front->hDC;
917 front->hDC = back->hDC;
918 back->hDC = tmp;
919 }
920
921 /* Flip the DIBsection. */
922 {
923 HBITMAP tmp;
924 tmp = front->dib.DIBsection;
925 front->dib.DIBsection = back->dib.DIBsection;
926 back->dib.DIBsection = tmp;
927 }
928
929 /* Flip the surface data. */
930 {
931 void *tmp;
932
933 tmp = front->dib.bitmap_data;
934 front->dib.bitmap_data = back->dib.bitmap_data;
935 back->dib.bitmap_data = tmp;
936
937 tmp = front->resource.allocatedMemory;
938 front->resource.allocatedMemory = back->resource.allocatedMemory;
939 back->resource.allocatedMemory = tmp;
940
941 if (front->resource.heapMemory)
942 ERR("GDI Surface %p has heap memory allocated.\n", front);
943
944 if (back->resource.heapMemory)
945 ERR("GDI Surface %p has heap memory allocated.\n", back);
946 }
947
948 /* FPS support */
949 if (TRACE_ON(fps))
950 {
951 static LONG prev_time, frames;
952 DWORD time = GetTickCount();
953
954 ++frames;
955
956 /* every 1.5 seconds */
957 if (time - prev_time > 1500)
958 {
959 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
960 prev_time = time;
961 frames = 0;
962 }
963 }
964
965 x11_copy_to_screen(swapchain, NULL);
966}
967
968static const struct wined3d_swapchain_ops swapchain_gdi_ops =
969{
970 swapchain_gdi_present,
971};
972
973void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
974{
975 RECT client_rect;
976
977 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
978 return;
979
980 if (!swapchain->desc.backbuffer_count)
981 {
982 TRACE("Single buffered rendering.\n");
983 swapchain->render_to_fbo = FALSE;
984 return;
985 }
986
987 GetClientRect(swapchain->win_handle, &client_rect);
988
989 TRACE("Backbuffer %ux%u, window %ux%u.\n",
990 swapchain->desc.backbuffer_width,
991 swapchain->desc.backbuffer_height,
992 client_rect.right, client_rect.bottom);
993 TRACE("Multisample type %#x, quality %#x.\n",
994 swapchain->desc.multisample_type,
995 swapchain->desc.multisample_quality);
996
997 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
998 && swapchain->desc.backbuffer_width == client_rect.right
999 && swapchain->desc.backbuffer_height == client_rect.bottom)
1000 {
1001 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
1002 swapchain->render_to_fbo = FALSE;
1003 return;
1004 }
1005
1006 TRACE("Rendering to FBO.\n");
1007 swapchain->render_to_fbo = TRUE;
1008}
1009
1010/* Do not call while under the GL lock. */
1011static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
1012 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
1013{
1014 const struct wined3d_adapter *adapter = device->adapter;
1015 struct wined3d_resource_desc surface_desc;
1016#ifndef VBOX_WITH_WDDM
1017 BOOL displaymode_set = FALSE;
1018#endif
1019 RECT client_rect;
1020 HWND window;
1021 HRESULT hr;
1022 UINT i;
1023#ifdef VBOX_WITH_WDDM
1024 struct wined3d_swapchain *overriden_swapchain = NULL;
1025 HDC hDC = NULL;
1026#endif
1027
1028 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
1029 {
1030 FIXME("The application requested %u back buffers, this is not supported.\n",
1031 desc->backbuffer_count);
1032 return WINED3DERR_INVALIDCALL;
1033 }
1034
1035 if (desc->backbuffer_count > 1)
1036 {
1037 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1038 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1039 }
1040
1041 if (device->wined3d->flags & WINED3D_NO3D)
1042 swapchain->swapchain_ops = &swapchain_gdi_ops;
1043 else
1044 swapchain->swapchain_ops = &swapchain_gl_ops;
1045
1046#ifndef VBOX_WITH_WDDM
1047 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
1048#else
1049 if (desc->device_window)
1050 {
1051 overriden_swapchain = swapchain_find(device, desc->device_window);
1052 if (!overriden_swapchain)
1053 {
1054 ERR("invalid window handle supplied");
1055 return E_FAIL;
1056 }
1057
1058 window = overriden_swapchain->win_handle;
1059 hDC = overriden_swapchain->hDC;
1060 }
1061 else
1062 {
1063 hr = VBoxExtWndCreate(desc->backbuffer_width, desc->backbuffer_height, &window, &hDC);
1064 if (FAILED(hr))
1065 {
1066 ERR("VBoxExtWndCreate failed, hr 0x%x", hr);
1067 return hr;
1068 }
1069 }
1070 Assert(window);
1071 Assert(hDC);
1072 desc->device_window = window;
1073#endif
1074
1075 swapchain->device = device;
1076 swapchain->parent = parent;
1077 swapchain->parent_ops = parent_ops;
1078 swapchain->ref = 1;
1079 swapchain->win_handle = window;
1080#ifndef VBOX_WITH_WDDM
1081 swapchain->device_window = window;
1082# ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1083 swapchain->hDC = VBoxExtGetDC(window);
1084 if (!swapchain->hDC)
1085 {
1086 ERR("failed to get window DC");
1087 return E_FAIL;
1088 }
1089# endif
1090#else
1091 Assert(window);
1092 swapchain->hDC = hDC;
1093#endif
1094
1095#ifndef VBOX_WITH_WDDM
1096 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
1097 adapter->ordinal, &swapchain->original_mode, NULL)))
1098 {
1099 ERR("Failed to get current display mode, hr %#x.\n", hr);
1100 goto err;
1101 }
1102#endif
1103
1104 GetClientRect(window, &client_rect);
1105 if (desc->windowed
1106 && (!desc->backbuffer_width || !desc->backbuffer_height
1107 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
1108 {
1109
1110 if (!desc->backbuffer_width)
1111 {
1112 desc->backbuffer_width = client_rect.right;
1113 TRACE("Updating width to %u.\n", desc->backbuffer_width);
1114 }
1115
1116 if (!desc->backbuffer_height)
1117 {
1118 desc->backbuffer_height = client_rect.bottom;
1119 TRACE("Updating height to %u.\n", desc->backbuffer_height);
1120 }
1121#ifndef VBOX_WITH_WDDM
1122 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
1123 {
1124 desc->backbuffer_format = swapchain->original_mode.format_id;
1125 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
1126 }
1127#endif
1128 }
1129 swapchain->desc = *desc;
1130 swapchain_update_render_to_fbo(swapchain);
1131
1132 TRACE("Creating front buffer.\n");
1133
1134 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1135 surface_desc.format = swapchain->desc.backbuffer_format;
1136 surface_desc.multisample_type = swapchain->desc.multisample_type;
1137 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
1138 surface_desc.usage = WINED3DUSAGE_RENDERTARGET;
1139 surface_desc.pool = WINED3D_POOL_DEFAULT;
1140 surface_desc.width = swapchain->desc.backbuffer_width;
1141 surface_desc.height = swapchain->desc.backbuffer_height;
1142 surface_desc.depth = 1;
1143 surface_desc.size = 0;
1144
1145 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1146 parent, &surface_desc, &swapchain->front_buffer)))
1147 {
1148 WARN("Failed to create front buffer, hr %#x.\n", hr);
1149 goto err;
1150 }
1151
1152 surface_set_swapchain(swapchain->front_buffer, swapchain);
1153 if (!(device->wined3d->flags & WINED3D_NO3D))
1154 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
1155
1156 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1157 * so we should really check to see if there is a fullscreen swapchain
1158 * already. Does a single head count as full screen? */
1159
1160#ifndef VBOX_WITH_WDDM
1161 if (!desc->windowed)
1162 {
1163 struct wined3d_display_mode mode;
1164
1165 /* Change the display settings */
1166 mode.width = desc->backbuffer_width;
1167 mode.height = desc->backbuffer_height;
1168 mode.format_id = desc->backbuffer_format;
1169 mode.refresh_rate = desc->refresh_rate;
1170 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1171
1172 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
1173 {
1174 WARN("Failed to set display mode, hr %#x.\n", hr);
1175 goto err;
1176 }
1177 displaymode_set = TRUE;
1178 }
1179#endif
1180
1181 if (!(device->wined3d->flags & WINED3D_NO3D))
1182 {
1183#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1184 static const enum wined3d_format_id formats[] =
1185 {
1186 WINED3DFMT_D24_UNORM_S8_UINT,
1187 WINED3DFMT_D32_UNORM,
1188 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1189 WINED3DFMT_D16_UNORM,
1190 WINED3DFMT_S1_UINT_D15_UNORM
1191 };
1192
1193 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
1194
1195 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
1196 if (!swapchain->context)
1197 {
1198 ERR("Failed to create the context array.\n");
1199 hr = E_OUTOFMEMORY;
1200 goto err;
1201 }
1202 swapchain->num_contexts = 1;
1203
1204 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
1205 * You are able to add a depth + stencil surface at a later stage when you need it.
1206 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1207 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1208 * context, need torecreate shaders, textures and other resources.
1209 *
1210 * The context manager already takes care of the state problem and for the other tasks code from Reset
1211 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1212 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1213 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1214 * issue needs to be fixed. */
1215 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
1216 {
1217 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
1218 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
1219 if (swapchain->context[0]) break;
1220 TRACE("Depth stencil format %s is not supported, trying next format\n",
1221 debug_d3dformat(formats[i]));
1222 }
1223
1224 if (!swapchain->context[0])
1225#else
1226 struct wined3d_context * swapchain_context;
1227 swapchain->ds_format = wined3d_get_format(&device->adapter->gl_info, WINED3DFMT_D24_UNORM_S8_UINT);
1228 swapchain_context = context_find_create(device, swapchain, swapchain->ds_format);
1229 if (!swapchain_context)
1230#endif
1231 {
1232 WARN("Failed to create context.\n");
1233 hr = WINED3DERR_NOTAVAILABLE;
1234 goto err;
1235 }
1236
1237 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
1238 && (!desc->enable_auto_depth_stencil
1239 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
1240 {
1241 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1242 }
1243#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1244 context_release(swapchain->context[0]);
1245#else
1246 context_release(swapchain_context);
1247#endif
1248 }
1249
1250 if (swapchain->desc.backbuffer_count > 0)
1251 {
1252 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1253 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1254 if (!swapchain->back_buffers)
1255 {
1256 ERR("Failed to allocate backbuffer array memory.\n");
1257 hr = E_OUTOFMEMORY;
1258 goto err;
1259 }
1260
1261 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1262 {
1263 TRACE("Creating back buffer %u.\n", i);
1264 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1265 parent, &surface_desc, &swapchain->back_buffers[i])))
1266 {
1267 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1268 goto err;
1269 }
1270 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
1271 }
1272 }
1273
1274 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1275 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1276 {
1277 TRACE("Creating depth/stencil buffer.\n");
1278 if (!device->auto_depth_stencil)
1279 {
1280 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
1281 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1282
1283 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1284 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1285 {
1286 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1287 goto err;
1288 }
1289 }
1290 }
1291
1292#ifndef VBOX_WITH_WDDM
1293 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1294#else
1295 if (overriden_swapchain)
1296 {
1297 swapchain_invalidate(overriden_swapchain);
1298 }
1299#endif
1300
1301 return WINED3D_OK;
1302
1303err:
1304#ifndef VBOX_WITH_WDDM
1305 if (displaymode_set)
1306 {
1307 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1308 adapter->ordinal, &swapchain->original_mode)))
1309 ERR("Failed to restore display mode.\n");
1310 ClipCursor(NULL);
1311 }
1312#endif
1313 if (swapchain->back_buffers)
1314 {
1315 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1316 {
1317 if (swapchain->back_buffers[i])
1318 {
1319 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1320 wined3d_surface_decref(swapchain->back_buffers[i]);
1321 }
1322 }
1323 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1324#ifdef VBOX_WITH_WINE_FIX_INITCLEAR
1325 swapchain->back_buffers = NULL;
1326#endif
1327 }
1328#ifdef VBOX_WITH_WINE_FIX_INITCLEAR
1329 swapchain->desc.backbuffer_count = 0;
1330#endif
1331#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1332 if (swapchain->context)
1333 {
1334 if (swapchain->context[0])
1335 {
1336 context_release(swapchain->context[0]);
1337 context_destroy(device, swapchain->context[0]);
1338 swapchain->num_contexts = 0;
1339 }
1340 HeapFree(GetProcessHeap(), 0, swapchain->context);
1341 }
1342#else
1343 if (!device->swapchain_count)
1344 {
1345 while (device->context_count)
1346 {
1347 context_destroy(device, device->contexts[0]);
1348 }
1349 }
1350#endif
1351 if (swapchain->front_buffer)
1352 {
1353 surface_set_swapchain(swapchain->front_buffer, NULL);
1354 wined3d_surface_decref(swapchain->front_buffer);
1355 }
1356
1357#ifdef VBOX_WITH_WDDM
1358 if (!overriden_swapchain && swapchain->win_handle)
1359 {
1360 VBoxExtWndDestroy(swapchain->win_handle, swapchain->hDC);
1361 }
1362 swapchain_invalidate(swapchain);
1363#endif
1364
1365 return hr;
1366}
1367
1368/* Do not call while under the GL lock. */
1369HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1370 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1371{
1372 struct wined3d_swapchain *object;
1373 HRESULT hr;
1374
1375 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1376 device, desc, parent, parent_ops, swapchain);
1377
1378 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1379 if (!object)
1380 return E_OUTOFMEMORY;
1381
1382 hr = swapchain_init(object, device, desc, parent, parent_ops);
1383 if (FAILED(hr))
1384 {
1385 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1386 HeapFree(GetProcessHeap(), 0, object);
1387 return hr;
1388 }
1389
1390 TRACE("Created swapchain %p.\n", object);
1391 *swapchain = object;
1392
1393 return WINED3D_OK;
1394}
1395
1396#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1397/* Do not call while under the GL lock. */
1398static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1399{
1400 struct wined3d_context **newArray;
1401 struct wined3d_context *ctx;
1402
1403 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1404
1405 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1406 {
1407 ERR("Failed to create a new context for the swapchain\n");
1408 return NULL;
1409 }
1410 context_release(ctx);
1411
1412 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1413 if(!newArray) {
1414 ERR("Out of memory when trying to allocate a new context array\n");
1415 context_destroy(swapchain->device, ctx);
1416 return NULL;
1417 }
1418 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1419 HeapFree(GetProcessHeap(), 0, swapchain->context);
1420 newArray[swapchain->num_contexts] = ctx;
1421 swapchain->context = newArray;
1422 swapchain->num_contexts++;
1423
1424 TRACE("Returning context %p\n", ctx);
1425 return ctx;
1426}
1427
1428void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1429{
1430 unsigned int i;
1431
1432 for (i = 0; i < swapchain->num_contexts; ++i)
1433 {
1434 context_destroy(swapchain->device, swapchain->context[i]);
1435 }
1436 swapchain->num_contexts = 0;
1437}
1438
1439struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1440{
1441 DWORD tid = GetCurrentThreadId();
1442 unsigned int i;
1443
1444 for (i = 0; i < swapchain->num_contexts; ++i)
1445 {
1446# ifdef VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT
1447 if (VBoxTlsRefIsFunctional(swapchain->context[i]))
1448# else
1449 if (swapchain->context[i]->tid == tid)
1450#endif
1451 return swapchain->context[i];
1452 }
1453
1454 /* Create a new context for the thread */
1455 return swapchain_create_context(swapchain);
1456}
1457#else /* if defined VBOX_WINE_WITH_SINGLE_CONTEXT */
1458struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1459{
1460 struct wined3d_context * context = swapchain->device->contexts[0];
1461 return context;
1462}
1463#endif
1464
1465void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1466{
1467 /* The drawable size of an onscreen drawable is the surface size.
1468 * (Actually: The window size, but the surface is created in window size) */
1469 *width = context->current_rt->resource.width;
1470 *height = context->current_rt->resource.height;
1471}
1472
1473#ifndef VBOX_WITH_WDDM
1474HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1475{
1476 if (!swapchain->backup_dc)
1477 {
1478 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1479
1480 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1481 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1482 {
1483 ERR("Failed to create a window.\n");
1484 return NULL;
1485 }
1486
1487 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1488 {
1489 ERR("Failed to get a DC.\n");
1490 DestroyWindow(swapchain->backup_wnd);
1491 swapchain->backup_wnd = NULL;
1492 return NULL;
1493 }
1494 }
1495
1496 return swapchain->backup_dc;
1497}
1498#endif
1499void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1500{
1501 UINT i;
1502
1503 surface_update_draw_binding(swapchain->front_buffer);
1504
1505 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1506 {
1507 surface_update_draw_binding(swapchain->back_buffers[i]);
1508 }
1509}
1510
1511#ifdef VBOX_WITH_WDDM
1512HRESULT CDECL wined3d_swapchain_present_rt(struct wined3d_swapchain *swapchain, struct wined3d_surface *rt)
1513{
1514 struct wined3d_surface *bb = swapchain->back_buffers[0];
1515 HRESULT hr = wined3d_surface_blt(bb, NULL, rt, NULL, 0, NULL, 0);
1516 if (FAILED(hr))
1517 {
1518 ERR("wined3d_surface_blt failed with hr(%d)", hr);
1519 return hr;
1520 }
1521
1522 hr = wined3d_swapchain_present(swapchain, NULL, NULL, NULL, NULL, 0);
1523 if (FAILED(hr))
1524 {
1525 ERR("wined3d_swapchain_present failed with hr(%d)", hr);
1526 return hr;
1527 }
1528
1529 return S_OK;
1530}
1531
1532HRESULT CDECL wined3d_swapchain_get_host_win_id(struct wined3d_swapchain *swapchain, int32_t *pi32Id)
1533{
1534 int32_t id = pVBoxGetWindowId(swapchain->hDC);
1535 if (!id)
1536 {
1537 *pi32Id = 0;
1538 ERR("failed to get id for hdc 0x%x", swapchain->hDC);
1539 return E_FAIL;
1540 }
1541 *pi32Id = id;
1542 return S_OK;
1543}
1544#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette