2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "swapchain9.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29 #include "nine_dump.h"
31 #include "util/u_inlines.h"
32 #include "util/u_surface.h"
33 #include "hud/hud_context.h"
34 #include "state_tracker/drm_driver.h"
36 #include "threadpool.h"
38 #define DBG_CHANNEL DBG_SWAPCHAIN
40 #define UNTESTED(n) DBG("UNTESTED point %d. Please tell if it worked\n", n)
43 NineSwapChain9_ctor( struct NineSwapChain9
*This
,
44 struct NineUnknownParams
*pParams
,
46 ID3DPresent
*pPresent
,
47 D3DPRESENT_PARAMETERS
*pPresentationParameters
,
48 struct d3dadapter9_context
*pCTX
,
50 D3DDISPLAYMODEEX
*mode
)
54 DBG("This=%p pDevice=%p pPresent=%p pCTX=%p hFocusWindow=%p\n",
55 This
, pParams
->device
, pPresent
, pCTX
, hFocusWindow
);
57 hr
= NineUnknown_ctor(&This
->base
, pParams
);
61 This
->screen
= NineDevice9_GetScreen(This
->base
.device
);
62 This
->pipe
= NineDevice9_GetPipe(This
->base
.device
);
63 This
->cso
= NineDevice9_GetCSO(This
->base
.device
);
64 This
->implicit
= implicit
;
66 This
->present
= pPresent
;
69 ID3DPresent_AddRef(pPresent
);
70 if (!This
->actx
->thread_submit
&&
71 This
->base
.device
->minor_version_num
> 2) {
72 D3DPRESENT_PARAMETERS2 params2
;
74 memset(¶ms2
, 0, sizeof(D3DPRESENT_PARAMETERS2
));
75 params2
.AllowDISCARDDelayedRelease
= This
->actx
->discard_delayed_release
;
76 params2
.TearFreeDISCARD
= This
->actx
->tearfree_discard
;
77 ID3DPresent_SetPresentParameters2(pPresent
, ¶ms2
);
80 if (!pPresentationParameters
->hDeviceWindow
)
81 pPresentationParameters
->hDeviceWindow
= hFocusWindow
;
83 This
->rendering_done
= FALSE
;
85 return NineSwapChain9_Resize(This
, pPresentationParameters
, mode
);
88 static D3DWindowBuffer
*
89 D3DWindowBuffer_create(struct NineSwapChain9
*This
,
90 struct pipe_resource
*resource
,
92 int for_frontbuffer_reading
)
95 struct winsys_handle whandle
;
99 memset(&whandle
, 0, sizeof(whandle
));
100 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
101 This
->screen
->resource_get_handle(This
->screen
, This
->pipe
, resource
,
103 for_frontbuffer_reading
?
104 PIPE_HANDLE_USAGE_WRITE
:
105 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
|
106 PIPE_HANDLE_USAGE_READ
);
107 stride
= whandle
.stride
;
108 dmaBufFd
= whandle
.handle
;
109 hr
= ID3DPresent_NewD3DWindowBufferFromDmaBuf(This
->present
,
117 assert (SUCCEEDED(hr
));
120 ERR("Failed to create new D3DWindowBufferFromDmaBuf\n");
127 NineSwapChain9_GetBackBufferCountForParams( struct NineSwapChain9
*This
,
128 D3DPRESENT_PARAMETERS
*pParams
);
131 NineSwapChain9_Resize( struct NineSwapChain9
*This
,
132 D3DPRESENT_PARAMETERS
*pParams
,
133 D3DDISPLAYMODEEX
*mode
)
135 struct NineDevice9
*pDevice
= This
->base
.device
;
136 D3DSURFACE_DESC desc
;
138 struct pipe_resource
*resource
, tmplt
;
140 BOOL has_present_buffers
= FALSE
;
142 unsigned i
, oldBufferCount
, newBufferCount
;
143 D3DMULTISAMPLE_TYPE multisample_type
;
145 DBG("This=%p pParams=%p\n", This
, pParams
);
146 user_assert(pParams
!= NULL
, E_POINTER
);
147 user_assert(pParams
->SwapEffect
, D3DERR_INVALIDCALL
);
148 user_assert((pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
) ||
149 (pParams
->BackBufferCount
<= 1), D3DERR_INVALIDCALL
);
150 user_assert(pDevice
->ex
|| pParams
->BackBufferCount
<=
151 D3DPRESENT_BACK_BUFFERS_MAX
, D3DERR_INVALIDCALL
);
152 user_assert(!pDevice
->ex
|| pParams
->BackBufferCount
<=
153 D3DPRESENT_BACK_BUFFERS_MAX_EX
, D3DERR_INVALIDCALL
);
154 user_assert(pDevice
->ex
||
155 (pParams
->SwapEffect
== D3DSWAPEFFECT_FLIP
) ||
156 (pParams
->SwapEffect
== D3DSWAPEFFECT_COPY
) ||
157 (pParams
->SwapEffect
== D3DSWAPEFFECT_DISCARD
), D3DERR_INVALIDCALL
);
160 "BackBufferWidth: %u\n"
161 "BackBufferHeight: %u\n"
162 "BackBufferFormat: %s\n"
163 "BackBufferCount: %u\n"
164 "MultiSampleType: %u\n"
165 "MultiSampleQuality: %u\n"
167 "hDeviceWindow: %p\n"
169 "EnableAutoDepthStencil: %i\n"
170 "AutoDepthStencilFormat: %s\n"
172 "FullScreen_RefreshRateInHz: %u\n"
173 "PresentationInterval: %x\n", pParams
,
174 pParams
->BackBufferWidth
, pParams
->BackBufferHeight
,
175 d3dformat_to_string(pParams
->BackBufferFormat
),
176 pParams
->BackBufferCount
,
177 pParams
->MultiSampleType
, pParams
->MultiSampleQuality
,
178 pParams
->SwapEffect
, pParams
->hDeviceWindow
, pParams
->Windowed
,
179 pParams
->EnableAutoDepthStencil
,
180 d3dformat_to_string(pParams
->AutoDepthStencilFormat
),
181 nine_D3DPRESENTFLAG_to_str(pParams
->Flags
),
182 pParams
->FullScreen_RefreshRateInHz
,
183 pParams
->PresentationInterval
);
185 if (pParams
->BackBufferCount
== 0) {
186 pParams
->BackBufferCount
= 1;
189 if (pParams
->BackBufferFormat
== D3DFMT_UNKNOWN
) {
190 pParams
->BackBufferFormat
= D3DFMT_A8R8G8B8
;
193 This
->desired_fences
= This
->actx
->throttling
? This
->actx
->throttling_value
+ 1 : 0;
194 /* +1 because we add the fence of the current buffer before popping an old one */
195 if (This
->desired_fences
> DRI_SWAP_FENCES_MAX
)
196 This
->desired_fences
= DRI_SWAP_FENCES_MAX
;
198 if (This
->actx
->vblank_mode
== 0)
199 pParams
->PresentationInterval
= D3DPRESENT_INTERVAL_IMMEDIATE
;
200 else if (This
->actx
->vblank_mode
== 3)
201 pParams
->PresentationInterval
= D3DPRESENT_INTERVAL_ONE
;
203 if (mode
&& This
->mode
) {
204 *(This
->mode
) = *mode
;
206 This
->mode
= malloc(sizeof(D3DDISPLAYMODEEX
));
207 memcpy(This
->mode
, mode
, sizeof(D3DDISPLAYMODEEX
));
213 /* Note: It is the role of the backend to fill if necessary
214 * BackBufferWidth and BackBufferHeight */
215 hr
= ID3DPresent_SetPresentParameters(This
->present
, pParams
, This
->mode
);
219 oldBufferCount
= This
->num_back_buffers
;
220 newBufferCount
= NineSwapChain9_GetBackBufferCountForParams(This
, pParams
);
222 multisample_type
= pParams
->MultiSampleType
;
224 /* Map MultiSampleQuality to MultiSampleType */
225 hr
= d3dmultisample_type_check(This
->screen
, pParams
->BackBufferFormat
,
227 pParams
->MultiSampleQuality
,
233 pf
= d3d9_to_pipe_format_checked(This
->screen
, pParams
->BackBufferFormat
,
234 PIPE_TEXTURE_2D
, multisample_type
,
235 PIPE_BIND_RENDER_TARGET
, FALSE
, FALSE
);
237 if (This
->actx
->linear_framebuffer
||
238 (pf
!= PIPE_FORMAT_B8G8R8X8_UNORM
&&
239 pf
!= PIPE_FORMAT_B8G8R8A8_UNORM
) ||
240 pParams
->SwapEffect
!= D3DSWAPEFFECT_DISCARD
||
241 multisample_type
>= 2 ||
242 (This
->actx
->ref
&& This
->actx
->ref
== This
->screen
))
243 has_present_buffers
= TRUE
;
245 /* Note: the buffer depth has to match the window depth.
246 * In practice, ARGB buffers can be used with windows
247 * of depth 24. Windows of depth 32 are extremely rare.
248 * So even if the buffer is ARGB, say it is depth 24.
249 * It is common practice, for example that's how
250 * glamor implements depth 24.
251 * TODO: handle windows with other depths. Not possible in the short term.
252 * For example 16 bits.*/
255 memset(&tmplt
, 0, sizeof(tmplt
));
256 tmplt
.target
= PIPE_TEXTURE_2D
;
257 tmplt
.width0
= pParams
->BackBufferWidth
;
258 tmplt
.height0
= pParams
->BackBufferHeight
;
260 tmplt
.last_level
= 0;
261 tmplt
.array_size
= 1;
262 tmplt
.usage
= PIPE_USAGE_DEFAULT
;
265 desc
.Type
= D3DRTYPE_SURFACE
;
266 desc
.Pool
= D3DPOOL_DEFAULT
;
267 desc
.MultiSampleType
= pParams
->MultiSampleType
;
268 desc
.MultiSampleQuality
= pParams
->MultiSampleQuality
;
269 desc
.Width
= pParams
->BackBufferWidth
;
270 desc
.Height
= pParams
->BackBufferHeight
;
272 for (i
= 0; i
< oldBufferCount
; i
++) {
274 _mesa_threadpool_wait_for_task(This
->pool
, &(This
->tasks
[i
]));
276 memset(This
->tasks
, 0, sizeof(This
->tasks
));
279 _mesa_threadpool_destroy(This
, This
->pool
);
282 This
->enable_threadpool
= This
->actx
->thread_submit
&& (pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
);
283 if (This
->enable_threadpool
)
284 This
->pool
= _mesa_threadpool_create(This
);
286 This
->enable_threadpool
= FALSE
;
288 for (i
= 0; i
< oldBufferCount
; i
++) {
289 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, This
->present_handles
[i
]);
290 This
->present_handles
[i
] = NULL
;
291 if (This
->present_buffers
[i
])
292 pipe_resource_reference(&(This
->present_buffers
[i
]), NULL
);
295 if (newBufferCount
!= oldBufferCount
) {
296 for (i
= newBufferCount
; i
< oldBufferCount
;
298 NineUnknown_Detach(NineUnknown(This
->buffers
[i
]));
300 for (i
= oldBufferCount
; i
< newBufferCount
; ++i
) {
301 This
->buffers
[i
] = NULL
;
302 This
->present_handles
[i
] = NULL
;
305 This
->num_back_buffers
= newBufferCount
;
307 for (i
= 0; i
< newBufferCount
; ++i
) {
308 tmplt
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
309 tmplt
.nr_samples
= multisample_type
;
310 if (!has_present_buffers
)
311 tmplt
.bind
|= NINE_BIND_PRESENTBUFFER_FLAGS
;
312 tmplt
.format
= d3d9_to_pipe_format_checked(This
->screen
,
313 pParams
->BackBufferFormat
,
316 tmplt
.bind
, FALSE
, FALSE
);
317 if (tmplt
.format
== PIPE_FORMAT_NONE
)
318 return D3DERR_INVALIDCALL
;
319 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
321 DBG("Failed to create pipe_resource.\n");
322 return D3DERR_OUTOFVIDEOMEMORY
;
324 if (pParams
->Flags
& D3DPRESENTFLAG_LOCKABLE_BACKBUFFER
)
325 resource
->flags
|= NINE_RESOURCE_FLAG_LOCKABLE
;
326 if (This
->buffers
[i
]) {
327 NineSurface9_SetMultiSampleType(This
->buffers
[i
], desc
.MultiSampleType
);
328 NineSurface9_SetResourceResize(This
->buffers
[i
], resource
);
329 if (has_present_buffers
)
330 pipe_resource_reference(&resource
, NULL
);
332 desc
.Format
= pParams
->BackBufferFormat
;
333 desc
.Usage
= D3DUSAGE_RENDERTARGET
;
334 hr
= NineSurface9_new(pDevice
, NineUnknown(This
), resource
, NULL
, 0,
335 0, 0, &desc
, &This
->buffers
[i
]);
336 if (has_present_buffers
)
337 pipe_resource_reference(&resource
, NULL
);
339 DBG("Failed to create RT surface.\n");
342 This
->buffers
[i
]->base
.base
.forward
= FALSE
;
344 if (has_present_buffers
) {
345 tmplt
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
346 tmplt
.bind
= NINE_BIND_PRESENTBUFFER_FLAGS
;
347 tmplt
.nr_samples
= 0;
348 if (This
->actx
->linear_framebuffer
)
349 tmplt
.bind
|= PIPE_BIND_LINEAR
;
350 if (pParams
->SwapEffect
!= D3DSWAPEFFECT_DISCARD
)
351 tmplt
.bind
|= PIPE_BIND_RENDER_TARGET
;
352 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
353 pipe_resource_reference(&(This
->present_buffers
[i
]), resource
);
355 This
->present_handles
[i
] = D3DWindowBuffer_create(This
, resource
, depth
, false);
356 pipe_resource_reference(&resource
, NULL
);
357 if (!This
->present_handles
[i
]) {
358 return D3DERR_DRIVERINTERNALERROR
;
361 if (pParams
->EnableAutoDepthStencil
) {
362 tmplt
.bind
= d3d9_get_pipe_depth_format_bindings(pParams
->AutoDepthStencilFormat
);
363 tmplt
.nr_samples
= multisample_type
;
364 tmplt
.format
= d3d9_to_pipe_format_checked(This
->screen
,
365 pParams
->AutoDepthStencilFormat
,
371 if (tmplt
.format
== PIPE_FORMAT_NONE
)
372 return D3DERR_INVALIDCALL
;
375 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
377 DBG("Failed to create pipe_resource for depth buffer.\n");
378 return D3DERR_OUTOFVIDEOMEMORY
;
381 NineSurface9_SetMultiSampleType(This
->zsbuf
, desc
.MultiSampleType
);
382 NineSurface9_SetResourceResize(This
->zsbuf
, resource
);
383 pipe_resource_reference(&resource
, NULL
);
385 hr
= NineDevice9_CreateDepthStencilSurface(pDevice
,
386 pParams
->BackBufferWidth
,
387 pParams
->BackBufferHeight
,
388 pParams
->AutoDepthStencilFormat
,
389 pParams
->MultiSampleType
,
390 pParams
->MultiSampleQuality
,
392 (IDirect3DSurface9
**)&This
->zsbuf
,
395 DBG("Failed to create ZS surface.\n");
398 NineUnknown_ConvertRefToBind(NineUnknown(This
->zsbuf
));
402 This
->params
= *pParams
;
407 /* Throttling: code adapted from the dri state tracker */
410 * swap_fences_pop_front - pull a fence from the throttle queue
412 * If the throttle queue is filled to the desired number of fences,
413 * pull fences off the queue until the number is less than the desired
414 * number of fences, and return the last fence pulled.
416 static struct pipe_fence_handle
*
417 swap_fences_pop_front(struct NineSwapChain9
*This
)
419 struct pipe_screen
*screen
= This
->screen
;
420 struct pipe_fence_handle
*fence
= NULL
;
422 if (This
->desired_fences
== 0)
425 if (This
->cur_fences
>= This
->desired_fences
) {
426 screen
->fence_reference(screen
, &fence
, This
->swap_fences
[This
->tail
]);
427 screen
->fence_reference(screen
, &This
->swap_fences
[This
->tail
++], NULL
);
428 This
->tail
&= DRI_SWAP_FENCES_MASK
;
436 * swap_fences_see_front - same than swap_fences_pop_front without
441 static struct pipe_fence_handle
*
442 swap_fences_see_front(struct NineSwapChain9
*This
)
444 struct pipe_screen
*screen
= This
->screen
;
445 struct pipe_fence_handle
*fence
= NULL
;
447 if (This
->desired_fences
== 0)
450 if (This
->cur_fences
>= This
->desired_fences
) {
451 screen
->fence_reference(screen
, &fence
, This
->swap_fences
[This
->tail
]);
458 * swap_fences_push_back - push a fence onto the throttle queue at the back
460 * push a fence onto the throttle queue and pull fences of the queue
461 * so that the desired number of fences are on the queue.
464 swap_fences_push_back(struct NineSwapChain9
*This
,
465 struct pipe_fence_handle
*fence
)
467 struct pipe_screen
*screen
= This
->screen
;
469 if (!fence
|| This
->desired_fences
== 0)
472 while(This
->cur_fences
== This
->desired_fences
)
473 swap_fences_pop_front(This
);
476 screen
->fence_reference(screen
, &This
->swap_fences
[This
->head
++],
478 This
->head
&= DRI_SWAP_FENCES_MASK
;
483 * swap_fences_unref - empty the throttle queue
485 * pulls fences of the throttle queue until it is empty.
488 swap_fences_unref(struct NineSwapChain9
*This
)
490 struct pipe_screen
*screen
= This
->screen
;
492 while(This
->cur_fences
) {
493 screen
->fence_reference(screen
, &This
->swap_fences
[This
->tail
++], NULL
);
494 This
->tail
&= DRI_SWAP_FENCES_MASK
;
500 NineSwapChain9_dtor( struct NineSwapChain9
*This
)
504 DBG("This=%p\n", This
);
507 _mesa_threadpool_destroy(This
, This
->pool
);
509 for (i
= 0; i
< This
->num_back_buffers
; i
++) {
510 if (This
->buffers
[i
])
511 NineUnknown_Release(NineUnknown(This
->buffers
[i
]));
512 if (This
->present_handles
[i
])
513 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, This
->present_handles
[i
]);
514 if (This
->present_buffers
[i
])
515 pipe_resource_reference(&(This
->present_buffers
[i
]), NULL
);
518 NineUnknown_Unbind(NineUnknown(This
->zsbuf
));
521 ID3DPresent_Release(This
->present
);
523 swap_fences_unref(This
);
524 NineUnknown_dtor(&This
->base
);
528 create_present_buffer( struct NineSwapChain9
*This
,
529 unsigned int width
, unsigned int height
,
530 struct pipe_resource
**resource
,
531 D3DWindowBuffer
**present_handle
)
533 struct pipe_resource tmplt
;
535 memset(&tmplt
, 0, sizeof(tmplt
));
536 tmplt
.target
= PIPE_TEXTURE_2D
;
537 tmplt
.width0
= width
;
538 tmplt
.height0
= height
;
540 tmplt
.last_level
= 0;
541 tmplt
.array_size
= 1;
542 tmplt
.usage
= PIPE_USAGE_DEFAULT
;
544 tmplt
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
545 tmplt
.bind
= NINE_BIND_BACKBUFFER_FLAGS
|
546 NINE_BIND_PRESENTBUFFER_FLAGS
;
547 tmplt
.nr_samples
= 0;
548 if (This
->actx
->linear_framebuffer
)
549 tmplt
.bind
|= PIPE_BIND_LINEAR
;
550 *resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
552 *present_handle
= D3DWindowBuffer_create(This
, *resource
, 24, true);
554 if (!*present_handle
) {
555 pipe_resource_reference(resource
, NULL
);
560 handle_draw_cursor_and_hud( struct NineSwapChain9
*This
, struct pipe_resource
*resource
)
562 struct NineDevice9
*device
= This
->base
.device
;
563 struct pipe_blit_info blit
;
565 if (device
->cursor
.software
&& device
->cursor
.visible
&& device
->cursor
.w
) {
566 memset(&blit
, 0, sizeof(blit
));
567 blit
.src
.resource
= device
->cursor
.image
;
569 blit
.src
.format
= device
->cursor
.image
->format
;
573 blit
.src
.box
.depth
= 1;
574 blit
.src
.box
.width
= device
->cursor
.w
;
575 blit
.src
.box
.height
= device
->cursor
.h
;
577 blit
.dst
.resource
= resource
;
579 blit
.dst
.format
= resource
->format
;
581 blit
.dst
.box
.depth
= 1;
583 blit
.mask
= PIPE_MASK_RGBA
;
584 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
585 blit
.scissor_enable
= FALSE
;
587 /* NOTE: blit messes up when box.x + box.width < 0, fix driver
588 * NOTE2: device->cursor.pos contains coordinates relative to the screen.
589 * This happens to be also the position of the cursor when we are fullscreen.
590 * We don't use sw cursor for Windowed mode */
591 blit
.dst
.box
.x
= MAX2(device
->cursor
.pos
.x
, 0) - device
->cursor
.hotspot
.x
;
592 blit
.dst
.box
.y
= MAX2(device
->cursor
.pos
.y
, 0) - device
->cursor
.hotspot
.y
;
593 blit
.dst
.box
.width
= blit
.src
.box
.width
;
594 blit
.dst
.box
.height
= blit
.src
.box
.height
;
596 DBG("Blitting cursor(%ux%u) to (%i,%i).\n",
597 blit
.src
.box
.width
, blit
.src
.box
.height
,
598 blit
.dst
.box
.x
, blit
.dst
.box
.y
);
600 blit
.alpha_blend
= TRUE
;
601 This
->pipe
->blit(This
->pipe
, &blit
);
604 if (device
->hud
&& resource
) {
605 hud_draw(device
->hud
, resource
); /* XXX: no offset */
606 /* HUD doesn't clobber stipple */
607 nine_state_restore_non_cso(device
);
611 struct end_present_struct
{
612 struct pipe_screen
*screen
;
613 struct pipe_fence_handle
*fence_to_wait
;
614 ID3DPresent
*present
;
615 D3DWindowBuffer
*present_handle
;
616 HWND hDestWindowOverride
;
619 static void work_present(void *data
)
621 struct end_present_struct
*work
= data
;
622 if (work
->fence_to_wait
) {
623 (void) work
->screen
->fence_finish(work
->screen
, NULL
, work
->fence_to_wait
, PIPE_TIMEOUT_INFINITE
);
624 work
->screen
->fence_reference(work
->screen
, &(work
->fence_to_wait
), NULL
);
626 ID3DPresent_PresentBuffer(work
->present
, work
->present_handle
, work
->hDestWindowOverride
, NULL
, NULL
, NULL
, 0);
630 static void pend_present(struct NineSwapChain9
*This
,
631 HWND hDestWindowOverride
)
633 struct end_present_struct
*work
= calloc(1, sizeof(struct end_present_struct
));
635 work
->screen
= This
->screen
;
636 work
->fence_to_wait
= swap_fences_pop_front(This
);
637 work
->present
= This
->present
;
638 work
->present_handle
= This
->present_handles
[0];
639 work
->hDestWindowOverride
= hDestWindowOverride
;
640 This
->tasks
[0] = _mesa_threadpool_queue_task(This
->pool
, work_present
, work
);
645 static inline HRESULT
646 present( struct NineSwapChain9
*This
,
647 const RECT
*pSourceRect
,
648 const RECT
*pDestRect
,
649 HWND hDestWindowOverride
,
650 const RGNDATA
*pDirtyRegion
,
653 struct pipe_resource
*resource
;
654 struct pipe_fence_handle
*fence
;
656 struct pipe_blit_info blit
;
658 DBG("present: This=%p pSourceRect=%p pDestRect=%p "
659 "pDirtyRegion=%p hDestWindowOverride=%p"
660 "dwFlags=%d resource=%p\n",
661 This
, pSourceRect
, pDestRect
, pDirtyRegion
,
662 hDestWindowOverride
, (int)dwFlags
, This
->buffers
[0]->base
.resource
);
665 DBG("pSourceRect = (%u..%u)x(%u..%u)\n",
666 pSourceRect
->left
, pSourceRect
->right
,
667 pSourceRect
->top
, pSourceRect
->bottom
);
669 DBG("pDestRect = (%u..%u)x(%u..%u)\n",
670 pDestRect
->left
, pDestRect
->right
,
671 pDestRect
->top
, pDestRect
->bottom
);
673 /* TODO: in the case the source and destination rect have different size:
674 * We need to allocate a new buffer, and do a blit to it to resize.
675 * We can't use the present_buffer for that since when we created it,
676 * we couldn't guess which size would have been needed.
677 * If pDestRect or pSourceRect is null, we have to check the sizes
678 * from the source size, and the destination window size.
679 * In this case, either resize rngdata, or pass NULL instead
681 /* Note: This->buffers[0]->level should always be 0 */
683 if (This
->rendering_done
)
684 goto bypass_rendering
;
686 resource
= This
->buffers
[0]->base
.resource
;
688 if (This
->params
.SwapEffect
== D3DSWAPEFFECT_DISCARD
)
689 handle_draw_cursor_and_hud(This
, resource
);
691 if (This
->present_buffers
[0]) {
692 memset(&blit
, 0, sizeof(blit
));
693 blit
.src
.resource
= resource
;
695 blit
.src
.format
= resource
->format
;
697 blit
.src
.box
.depth
= 1;
700 blit
.src
.box
.width
= resource
->width0
;
701 blit
.src
.box
.height
= resource
->height0
;
703 resource
= This
->present_buffers
[0];
705 blit
.dst
.resource
= resource
;
707 blit
.dst
.format
= resource
->format
;
709 blit
.dst
.box
.depth
= 1;
712 blit
.dst
.box
.width
= resource
->width0
;
713 blit
.dst
.box
.height
= resource
->height0
;
715 blit
.mask
= PIPE_MASK_RGBA
;
716 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
717 blit
.scissor_enable
= FALSE
;
718 blit
.alpha_blend
= FALSE
;
720 This
->pipe
->blit(This
->pipe
, &blit
);
723 /* The resource we present has to resolve fast clears
724 * if needed (and other things) */
725 This
->pipe
->flush_resource(This
->pipe
, resource
);
727 if (This
->params
.SwapEffect
!= D3DSWAPEFFECT_DISCARD
)
728 handle_draw_cursor_and_hud(This
, resource
);
731 This
->pipe
->flush(This
->pipe
, &fence
, PIPE_FLUSH_END_OF_FRAME
);
733 swap_fences_push_back(This
, fence
);
734 This
->screen
->fence_reference(This
->screen
, &fence
, NULL
);
737 This
->rendering_done
= TRUE
;
740 if (dwFlags
& D3DPRESENT_DONOTWAIT
) {
742 BOOL still_draw
= FALSE
;
743 fence
= swap_fences_see_front(This
);
745 still_draw
= !This
->screen
->fence_finish(This
->screen
, NULL
, fence
, 0);
746 This
->screen
->fence_reference(This
->screen
, &fence
, NULL
);
749 return D3DERR_WASSTILLDRAWING
;
752 if (!This
->enable_threadpool
) {
754 fence
= swap_fences_pop_front(This
);
756 (void) This
->screen
->fence_finish(This
->screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
757 This
->screen
->fence_reference(This
->screen
, &fence
, NULL
);
760 hr
= ID3DPresent_PresentBuffer(This
->present
, This
->present_handles
[0], hDestWindowOverride
, pSourceRect
, pDestRect
, pDirtyRegion
, dwFlags
);
762 if (FAILED(hr
)) { UNTESTED(3);return hr
; }
764 pend_present(This
, hDestWindowOverride
);
766 This
->rendering_done
= FALSE
;
772 NineSwapChain9_Present( struct NineSwapChain9
*This
,
773 const RECT
*pSourceRect
,
774 const RECT
*pDestRect
,
775 HWND hDestWindowOverride
,
776 const RGNDATA
*pDirtyRegion
,
779 struct pipe_resource
*res
= NULL
;
780 D3DWindowBuffer
*handle_temp
;
781 struct threadpool_task
*task_temp
;
785 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p "
786 "pDirtyRegion=%p dwFlags=%d\n",
787 This
, pSourceRect
, pDestRect
, hDestWindowOverride
,
788 pDirtyRegion
,dwFlags
);
790 if (This
->base
.device
->ex
) {
791 if (NineSwapChain9_GetOccluded(This
)) {
792 DBG("Present is occluded. Returning S_PRESENT_OCCLUDED.\n");
793 return S_PRESENT_OCCLUDED
;
796 if (NineSwapChain9_GetOccluded(This
) ||
797 NineSwapChain9_ResolutionMismatch(This
)) {
798 This
->base
.device
->device_needs_reset
= TRUE
;
800 if (This
->base
.device
->device_needs_reset
) {
801 DBG("Device is lost. Returning D3DERR_DEVICELOST.\n");
802 return D3DERR_DEVICELOST
;
806 hr
= present(This
, pSourceRect
, pDestRect
,
807 hDestWindowOverride
, pDirtyRegion
, dwFlags
);
808 if (hr
== D3DERR_WASSTILLDRAWING
)
811 if (This
->base
.device
->minor_version_num
> 2 &&
812 This
->params
.SwapEffect
== D3DSWAPEFFECT_DISCARD
&&
813 This
->params
.PresentationInterval
== D3DPRESENT_INTERVAL_IMMEDIATE
&&
814 !This
->actx
->thread_submit
) {
815 int next_buffer
= -1;
817 while (next_buffer
== -1) {
818 /* Find a free backbuffer */
819 for (i
= 1; i
< This
->num_back_buffers
; i
++) {
820 if (ID3DPresent_IsBufferReleased(This
->present
, This
->present_handles
[i
])) {
821 DBG("Found buffer released: %d\n", i
);
826 if (next_buffer
== -1) {
827 DBG("Found no buffer released. Waiting for event\n");
828 ID3DPresent_WaitBufferReleaseEvent(This
->present
);
831 /* Switch with the released buffer */
832 pipe_resource_reference(&res
, This
->buffers
[0]->base
.resource
);
833 NineSurface9_SetResourceResize(
834 This
->buffers
[0], This
->buffers
[next_buffer
]->base
.resource
);
835 NineSurface9_SetResourceResize(
836 This
->buffers
[next_buffer
], res
);
837 pipe_resource_reference(&res
, NULL
);
839 if (This
->present_buffers
[0]) {
840 pipe_resource_reference(&res
, This
->present_buffers
[0]);
841 pipe_resource_reference(&This
->present_buffers
[0], This
->present_buffers
[next_buffer
]);
842 pipe_resource_reference(&This
->present_buffers
[next_buffer
], res
);
843 pipe_resource_reference(&res
, NULL
);
846 handle_temp
= This
->present_handles
[0];
847 This
->present_handles
[0] = This
->present_handles
[next_buffer
];
848 This
->present_handles
[next_buffer
] = handle_temp
;
850 /* Path not yet compatible with thread_submit */
851 assert(!This
->tasks
[0] && !This
->tasks
[next_buffer
]);
853 switch (This
->params
.SwapEffect
) {
854 case D3DSWAPEFFECT_OVERLAY
: /* Not implemented, fallback to FLIP */
855 case D3DSWAPEFFECT_FLIPEX
: /* Allows optimizations over FLIP for windowed mode. */
856 case D3DSWAPEFFECT_DISCARD
: /* Allows optimizations over FLIP */
857 case D3DSWAPEFFECT_FLIP
:
858 /* rotate the queue */
859 pipe_resource_reference(&res
, This
->buffers
[0]->base
.resource
);
860 for (i
= 1; i
< This
->num_back_buffers
; i
++) {
861 NineSurface9_SetResourceResize(This
->buffers
[i
- 1],
862 This
->buffers
[i
]->base
.resource
);
864 NineSurface9_SetResourceResize(
865 This
->buffers
[This
->num_back_buffers
- 1], res
);
866 pipe_resource_reference(&res
, NULL
);
868 if (This
->present_buffers
[0]) {
869 pipe_resource_reference(&res
, This
->present_buffers
[0]);
870 for (i
= 1; i
< This
->num_back_buffers
; i
++)
871 pipe_resource_reference(&(This
->present_buffers
[i
-1]), This
->present_buffers
[i
]);
872 pipe_resource_reference(&(This
->present_buffers
[This
->num_back_buffers
- 1]), res
);
873 pipe_resource_reference(&res
, NULL
);
876 handle_temp
= This
->present_handles
[0];
877 for (i
= 1; i
< This
->num_back_buffers
; i
++) {
878 This
->present_handles
[i
-1] = This
->present_handles
[i
];
880 This
->present_handles
[This
->num_back_buffers
- 1] = handle_temp
;
881 task_temp
= This
->tasks
[0];
882 for (i
= 1; i
< This
->num_back_buffers
; i
++) {
883 This
->tasks
[i
-1] = This
->tasks
[i
];
885 This
->tasks
[This
->num_back_buffers
- 1] = task_temp
;
888 case D3DSWAPEFFECT_COPY
:
894 _mesa_threadpool_wait_for_task(This
->pool
, &(This
->tasks
[0]));
896 ID3DPresent_WaitBufferReleased(This
->present
, This
->present_handles
[0]);
899 This
->base
.device
->state
.changed
.group
|= NINE_STATE_FB
;
905 NineSwapChain9_GetFrontBufferData( struct NineSwapChain9
*This
,
906 IDirect3DSurface9
*pDestSurface
)
908 struct NineSurface9
*dest_surface
= NineSurface9(pDestSurface
);
909 struct NineDevice9
*pDevice
= This
->base
.device
;
910 unsigned int width
, height
;
911 struct pipe_resource
*temp_resource
;
912 struct NineSurface9
*temp_surface
;
913 D3DWindowBuffer
*temp_handle
;
914 D3DSURFACE_DESC desc
;
917 DBG("GetFrontBufferData: This=%p pDestSurface=%p\n",
920 user_assert(dest_surface
->base
.pool
== D3DPOOL_SYSTEMMEM
, D3DERR_INVALIDCALL
);
922 width
= dest_surface
->desc
.Width
;
923 height
= dest_surface
->desc
.Height
;
925 /* Note: front window size and destination size are supposed
926 * to match. However it's not very clear what should get taken in Windowed
927 * mode. It may need a fix */
928 create_present_buffer(This
, width
, height
, &temp_resource
, &temp_handle
);
930 if (!temp_resource
|| !temp_handle
) {
931 return D3DERR_INVALIDCALL
;
934 desc
.Type
= D3DRTYPE_SURFACE
;
935 desc
.Pool
= D3DPOOL_DEFAULT
;
936 desc
.MultiSampleType
= D3DMULTISAMPLE_NONE
;
937 desc
.MultiSampleQuality
= 0;
939 desc
.Height
= height
;
940 /* NineSurface9_CopyDefaultToMem needs same format. */
941 desc
.Format
= dest_surface
->desc
.Format
;
942 desc
.Usage
= D3DUSAGE_RENDERTARGET
;
943 hr
= NineSurface9_new(pDevice
, NineUnknown(This
), temp_resource
, NULL
, 0,
944 0, 0, &desc
, &temp_surface
);
945 pipe_resource_reference(&temp_resource
, NULL
);
947 DBG("Failed to create temp FrontBuffer surface.\n");
951 ID3DPresent_FrontBufferCopy(This
->present
, temp_handle
);
953 NineSurface9_CopyDefaultToMem(dest_surface
, temp_surface
);
955 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, temp_handle
);
956 NineUnknown_Destroy(NineUnknown(temp_surface
));
962 NineSwapChain9_GetBackBuffer( struct NineSwapChain9
*This
,
964 D3DBACKBUFFER_TYPE Type
,
965 IDirect3DSurface9
**ppBackBuffer
)
967 DBG("GetBackBuffer: This=%p iBackBuffer=%d Type=%d ppBackBuffer=%p\n",
968 This
, iBackBuffer
, Type
, ppBackBuffer
);
969 (void)user_error(Type
== D3DBACKBUFFER_TYPE_MONO
);
970 /* don't touch ppBackBuffer on error */
971 user_assert(ppBackBuffer
!= NULL
, D3DERR_INVALIDCALL
);
972 user_assert(iBackBuffer
< This
->params
.BackBufferCount
, D3DERR_INVALIDCALL
);
974 NineUnknown_AddRef(NineUnknown(This
->buffers
[iBackBuffer
]));
975 *ppBackBuffer
= (IDirect3DSurface9
*)This
->buffers
[iBackBuffer
];
980 NineSwapChain9_GetRasterStatus( struct NineSwapChain9
*This
,
981 D3DRASTER_STATUS
*pRasterStatus
)
983 DBG("GetRasterStatus: This=%p pRasterStatus=%p\n",
984 This
, pRasterStatus
);
985 user_assert(pRasterStatus
!= NULL
, E_POINTER
);
986 return ID3DPresent_GetRasterStatus(This
->present
, pRasterStatus
);
990 NineSwapChain9_GetDisplayMode( struct NineSwapChain9
*This
,
991 D3DDISPLAYMODE
*pMode
)
993 D3DDISPLAYMODEEX mode
;
994 D3DDISPLAYROTATION rot
;
997 DBG("GetDisplayMode: This=%p pMode=%p\n",
999 user_assert(pMode
!= NULL
, E_POINTER
);
1001 hr
= ID3DPresent_GetDisplayMode(This
->present
, &mode
, &rot
);
1002 if (SUCCEEDED(hr
)) {
1003 pMode
->Width
= mode
.Width
;
1004 pMode
->Height
= mode
.Height
;
1005 pMode
->RefreshRate
= mode
.RefreshRate
;
1006 pMode
->Format
= mode
.Format
;
1012 NineSwapChain9_GetPresentParameters( struct NineSwapChain9
*This
,
1013 D3DPRESENT_PARAMETERS
*pPresentationParameters
)
1015 DBG("GetPresentParameters: This=%p pPresentationParameters=%p\n",
1016 This
, pPresentationParameters
);
1017 user_assert(pPresentationParameters
!= NULL
, E_POINTER
);
1018 *pPresentationParameters
= This
->params
;
1022 IDirect3DSwapChain9Vtbl NineSwapChain9_vtable
= {
1023 (void *)NineUnknown_QueryInterface
,
1024 (void *)NineUnknown_AddRef
,
1025 (void *)NineUnknown_Release
,
1026 (void *)NineSwapChain9_Present
,
1027 (void *)NineSwapChain9_GetFrontBufferData
,
1028 (void *)NineSwapChain9_GetBackBuffer
,
1029 (void *)NineSwapChain9_GetRasterStatus
,
1030 (void *)NineSwapChain9_GetDisplayMode
,
1031 (void *)NineUnknown_GetDevice
, /* actually part of SwapChain9 iface */
1032 (void *)NineSwapChain9_GetPresentParameters
1035 static const GUID
*NineSwapChain9_IIDs
[] = {
1036 &IID_IDirect3DSwapChain9
,
1042 NineSwapChain9_new( struct NineDevice9
*pDevice
,
1044 ID3DPresent
*pPresent
,
1045 D3DPRESENT_PARAMETERS
*pPresentationParameters
,
1046 struct d3dadapter9_context
*pCTX
,
1048 struct NineSwapChain9
**ppOut
)
1050 NINE_DEVICE_CHILD_NEW(SwapChain9
, ppOut
, pDevice
, /* args */
1051 implicit
, pPresent
, pPresentationParameters
,
1052 pCTX
, hFocusWindow
, NULL
);
1056 NineSwapChain9_GetOccluded( struct NineSwapChain9
*This
)
1058 if (This
->base
.device
->minor_version_num
> 0) {
1059 return ID3DPresent_GetWindowOccluded(This
->present
);
1066 NineSwapChain9_ResolutionMismatch( struct NineSwapChain9
*This
)
1068 if (This
->base
.device
->minor_version_num
> 1) {
1069 return ID3DPresent_ResolutionMismatch(This
->present
);
1076 NineSwapChain9_CreateThread( struct NineSwapChain9
*This
,
1080 if (This
->base
.device
->minor_version_num
> 1) {
1081 return ID3DPresent_CreateThread(This
->present
, pFuncAddress
, pParam
);
1088 NineSwapChain9_WaitForThread( struct NineSwapChain9
*This
,
1091 if (This
->base
.device
->minor_version_num
> 1) {
1092 (void) ID3DPresent_WaitForThread(This
->present
, thread
);
1097 NineSwapChain9_GetBackBufferCountForParams( struct NineSwapChain9
*This
,
1098 D3DPRESENT_PARAMETERS
*pParams
)
1100 int count
= pParams
->BackBufferCount
;
1102 /* When we have flip behaviour, d3d9 expects we get back the screen buffer when we flip.
1103 * Here we don't get back the initial content of the screen. To emulate the behaviour
1104 * we allocate an additional buffer */
1105 if (pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
)
1107 /* With DISCARD, as there is no guarantee about the buffer contents, we can use
1108 * an arbitrary number of buffers */
1109 if (pParams
->SwapEffect
== D3DSWAPEFFECT_DISCARD
) {
1110 /* thread_submit has a throttling equivalent to the throttling
1111 * with throttling_value set to count-1. Most drivers use
1112 * 2 for throttling_value. For performance use count of at least 3
1113 * for thread_submit. */
1114 if (This
->actx
->thread_submit
&& count
< 3)
1116 /* When we enable AllowDISCARDDelayedRelease, we must ensure
1117 * to have at least 4 buffers to meet INTERVAL_IMMEDIATE,
1118 * since the display server/compositor can hold 3 buffers
1119 * without releasing them:
1120 * . Buffer on screen.
1121 * . Buffer scheduled kernel side to be next on screen.
1122 * . Last buffer sent.
1123 * For some reasons, 5 buffers are actually needed, because in
1124 * case a pageflip is missed because rendering wasn't finished,
1125 * the Xserver will hold 4 buffers. */
1126 if (!This
->actx
->thread_submit
&&
1127 This
->base
.device
->minor_version_num
> 2 &&
1128 pParams
->PresentationInterval
== D3DPRESENT_INTERVAL_IMMEDIATE
&&