VirtualBox

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

最後變更 在這個檔案從48345是 48345,由 vboxsync 提交於 12 年 前

header fixes

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

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