VirtualBox

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

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

wine/new: export

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

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