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
);
71 if (!pPresentationParameters
->hDeviceWindow
)
72 pPresentationParameters
->hDeviceWindow
= hFocusWindow
;
74 This
->rendering_done
= FALSE
;
76 return NineSwapChain9_Resize(This
, pPresentationParameters
, mode
);
79 static D3DWindowBuffer
*
80 D3DWindowBuffer_create(struct NineSwapChain9
*This
,
81 struct pipe_resource
*resource
,
83 int for_frontbuffer_reading
)
86 struct winsys_handle whandle
;
89 memset(&whandle
, 0, sizeof(whandle
));
90 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
91 This
->screen
->resource_get_handle(This
->screen
, This
->pipe
, resource
,
93 for_frontbuffer_reading
?
94 PIPE_HANDLE_USAGE_WRITE
:
95 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
|
96 PIPE_HANDLE_USAGE_READ
);
97 stride
= whandle
.stride
;
98 dmaBufFd
= whandle
.handle
;
99 ID3DPresent_NewD3DWindowBufferFromDmaBuf(This
->present
,
111 NineSwapChain9_Resize( struct NineSwapChain9
*This
,
112 D3DPRESENT_PARAMETERS
*pParams
,
113 D3DDISPLAYMODEEX
*mode
)
115 struct NineDevice9
*pDevice
= This
->base
.device
;
116 struct NineSurface9
**bufs
;
117 D3DSURFACE_DESC desc
;
119 struct pipe_resource
*resource
, tmplt
;
121 BOOL has_present_buffers
= FALSE
;
123 unsigned i
, oldBufferCount
, newBufferCount
;
125 DBG("This=%p pParams=%p\n", This
, pParams
);
126 user_assert(pParams
!= NULL
, E_POINTER
);
127 user_assert(pParams
->SwapEffect
, D3DERR_INVALIDCALL
);
128 user_assert((pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
) ||
129 (pParams
->BackBufferCount
<= 1), D3DERR_INVALIDCALL
);
130 user_assert(pDevice
->ex
|| pParams
->BackBufferCount
<= 3, D3DERR_INVALIDCALL
);
131 user_assert(pDevice
->ex
||
132 (pParams
->SwapEffect
== D3DSWAPEFFECT_FLIP
) ||
133 (pParams
->SwapEffect
== D3DSWAPEFFECT_COPY
) ||
134 (pParams
->SwapEffect
== D3DSWAPEFFECT_DISCARD
), D3DERR_INVALIDCALL
);
137 "BackBufferWidth: %u\n"
138 "BackBufferHeight: %u\n"
139 "BackBufferFormat: %s\n"
140 "BackBufferCount: %u\n"
141 "MultiSampleType: %u\n"
142 "MultiSampleQuality: %u\n"
144 "hDeviceWindow: %p\n"
146 "EnableAutoDepthStencil: %i\n"
147 "AutoDepthStencilFormat: %s\n"
149 "FullScreen_RefreshRateInHz: %u\n"
150 "PresentationInterval: %x\n", pParams
,
151 pParams
->BackBufferWidth
, pParams
->BackBufferHeight
,
152 d3dformat_to_string(pParams
->BackBufferFormat
),
153 pParams
->BackBufferCount
,
154 pParams
->MultiSampleType
, pParams
->MultiSampleQuality
,
155 pParams
->SwapEffect
, pParams
->hDeviceWindow
, pParams
->Windowed
,
156 pParams
->EnableAutoDepthStencil
,
157 d3dformat_to_string(pParams
->AutoDepthStencilFormat
),
158 nine_D3DPRESENTFLAG_to_str(pParams
->Flags
),
159 pParams
->FullScreen_RefreshRateInHz
,
160 pParams
->PresentationInterval
);
162 if (pParams
->BackBufferCount
> 3) {
163 pParams
->BackBufferCount
= 3;
166 if (pParams
->BackBufferCount
== 0) {
167 pParams
->BackBufferCount
= 1;
170 if (pParams
->BackBufferFormat
== D3DFMT_UNKNOWN
) {
171 pParams
->BackBufferFormat
= D3DFMT_A8R8G8B8
;
174 This
->desired_fences
= This
->actx
->throttling
? This
->actx
->throttling_value
+ 1 : 0;
175 /* +1 because we add the fence of the current buffer before popping an old one */
176 if (This
->desired_fences
> DRI_SWAP_FENCES_MAX
)
177 This
->desired_fences
= DRI_SWAP_FENCES_MAX
;
179 if (This
->actx
->vblank_mode
== 0)
180 pParams
->PresentationInterval
= D3DPRESENT_INTERVAL_IMMEDIATE
;
181 else if (This
->actx
->vblank_mode
== 3)
182 pParams
->PresentationInterval
= D3DPRESENT_INTERVAL_ONE
;
184 if (mode
&& This
->mode
) {
185 *(This
->mode
) = *mode
;
187 This
->mode
= malloc(sizeof(D3DDISPLAYMODEEX
));
188 memcpy(This
->mode
, mode
, sizeof(D3DDISPLAYMODEEX
));
194 /* Note: It is the role of the backend to fill if necessary
195 * BackBufferWidth and BackBufferHeight */
196 hr
= ID3DPresent_SetPresentParameters(This
->present
, pParams
, This
->mode
);
200 /* When we have flip behaviour, d3d9 expects we get back the screen buffer when we flip.
201 * Here we don't get back the initial content of the screen. To emulate the behaviour
202 * we allocate an additional buffer */
203 oldBufferCount
= This
->params
.BackBufferCount
?
204 (This
->params
.BackBufferCount
+
205 (This
->params
.SwapEffect
!= D3DSWAPEFFECT_COPY
)) : 0;
206 newBufferCount
= pParams
->BackBufferCount
+
207 (pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
);
209 pf
= d3d9_to_pipe_format_checked(This
->screen
, pParams
->BackBufferFormat
,
210 PIPE_TEXTURE_2D
, pParams
->MultiSampleType
,
211 PIPE_BIND_RENDER_TARGET
, FALSE
, FALSE
);
213 if (This
->actx
->linear_framebuffer
||
214 (pf
!= PIPE_FORMAT_B8G8R8X8_UNORM
&&
215 pf
!= PIPE_FORMAT_B8G8R8A8_UNORM
) ||
216 pParams
->SwapEffect
!= D3DSWAPEFFECT_DISCARD
||
217 pParams
->MultiSampleType
>= 2 ||
218 (This
->actx
->ref
&& This
->actx
->ref
== This
->screen
))
219 has_present_buffers
= TRUE
;
221 /* Note: the buffer depth has to match the window depth.
222 * In practice, ARGB buffers can be used with windows
223 * of depth 24. Windows of depth 32 are extremely rare.
224 * So even if the buffer is ARGB, say it is depth 24.
225 * It is common practice, for example that's how
226 * glamor implements depth 24.
227 * TODO: handle windows with other depths. Not possible in the short term.
228 * For example 16 bits.*/
231 tmplt
.target
= PIPE_TEXTURE_2D
;
232 tmplt
.width0
= pParams
->BackBufferWidth
;
233 tmplt
.height0
= pParams
->BackBufferHeight
;
235 tmplt
.last_level
= 0;
236 tmplt
.array_size
= 1;
237 tmplt
.usage
= PIPE_USAGE_DEFAULT
;
240 desc
.Type
= D3DRTYPE_SURFACE
;
241 desc
.Pool
= D3DPOOL_DEFAULT
;
242 desc
.MultiSampleType
= pParams
->MultiSampleType
;
243 desc
.MultiSampleQuality
= 0;
244 desc
.Width
= pParams
->BackBufferWidth
;
245 desc
.Height
= pParams
->BackBufferHeight
;
248 _mesa_threadpool_destroy(This
, This
->pool
);
251 This
->enable_threadpool
= This
->actx
->thread_submit
&& (pParams
->SwapEffect
!= D3DSWAPEFFECT_COPY
);
252 if (This
->enable_threadpool
)
253 This
->pool
= _mesa_threadpool_create(This
);
255 This
->enable_threadpool
= FALSE
;
257 This
->tasks
= REALLOC(This
->tasks
,
258 oldBufferCount
* sizeof(struct threadpool_task
*),
259 newBufferCount
* sizeof(struct threadpool_task
*));
260 memset(This
->tasks
, 0, newBufferCount
* sizeof(struct threadpool_task
*));
262 for (i
= 0; i
< oldBufferCount
; i
++) {
263 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, This
->present_handles
[i
]);
264 This
->present_handles
[i
] = NULL
;
265 if (This
->present_buffers
)
266 pipe_resource_reference(&(This
->present_buffers
[i
]), NULL
);
269 if (!has_present_buffers
&& This
->present_buffers
) {
270 FREE(This
->present_buffers
);
271 This
->present_buffers
= NULL
;
274 if (newBufferCount
!= oldBufferCount
) {
275 for (i
= newBufferCount
; i
< oldBufferCount
;
277 NineUnknown_Detach(NineUnknown(This
->buffers
[i
]));
279 bufs
= REALLOC(This
->buffers
,
280 oldBufferCount
* sizeof(This
->buffers
[0]),
281 newBufferCount
* sizeof(This
->buffers
[0]));
283 return E_OUTOFMEMORY
;
284 This
->buffers
= bufs
;
285 This
->present_handles
= REALLOC(This
->present_handles
,
286 oldBufferCount
* sizeof(D3DWindowBuffer
*),
287 newBufferCount
* sizeof(D3DWindowBuffer
*));
288 for (i
= oldBufferCount
; i
< newBufferCount
; ++i
) {
289 This
->buffers
[i
] = NULL
;
290 This
->present_handles
[i
] = NULL
;
294 if (has_present_buffers
&&
295 (newBufferCount
!= oldBufferCount
|| !This
->present_buffers
)) {
296 This
->present_buffers
= REALLOC(This
->present_buffers
,
297 This
->present_buffers
== NULL
? 0 :
298 oldBufferCount
* sizeof(struct pipe_resource
*),
299 newBufferCount
* sizeof(struct pipe_resource
*));
300 memset(This
->present_buffers
, 0, newBufferCount
* sizeof(struct pipe_resource
*));
303 for (i
= 0; i
< newBufferCount
; ++i
) {
304 tmplt
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
305 tmplt
.nr_samples
= pParams
->MultiSampleType
;
306 if (!has_present_buffers
)
307 tmplt
.bind
|= PIPE_BIND_SHARED
| PIPE_BIND_SCANOUT
| PIPE_BIND_DISPLAY_TARGET
;
308 tmplt
.format
= d3d9_to_pipe_format_checked(This
->screen
,
309 pParams
->BackBufferFormat
,
312 tmplt
.bind
, FALSE
, FALSE
);
313 if (tmplt
.format
== PIPE_FORMAT_NONE
)
314 return D3DERR_INVALIDCALL
;
315 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
317 DBG("Failed to create pipe_resource.\n");
318 return D3DERR_OUTOFVIDEOMEMORY
;
320 if (pParams
->Flags
& D3DPRESENTFLAG_LOCKABLE_BACKBUFFER
)
321 resource
->flags
|= NINE_RESOURCE_FLAG_LOCKABLE
;
322 if (This
->buffers
[i
]) {
323 NineSurface9_SetResourceResize(This
->buffers
[i
], resource
);
324 if (has_present_buffers
)
325 pipe_resource_reference(&resource
, NULL
);
327 desc
.Format
= pParams
->BackBufferFormat
;
328 desc
.Usage
= D3DUSAGE_RENDERTARGET
;
329 hr
= NineSurface9_new(pDevice
, NineUnknown(This
), resource
, NULL
, 0,
330 0, 0, &desc
, &This
->buffers
[i
]);
331 if (has_present_buffers
)
332 pipe_resource_reference(&resource
, NULL
);
334 DBG("Failed to create RT surface.\n");
337 This
->buffers
[i
]->base
.base
.forward
= FALSE
;
339 if (has_present_buffers
) {
340 tmplt
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
341 tmplt
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_SHARED
| PIPE_BIND_SCANOUT
| PIPE_BIND_DISPLAY_TARGET
;
342 tmplt
.nr_samples
= 0;
343 if (This
->actx
->linear_framebuffer
)
344 tmplt
.bind
|= PIPE_BIND_LINEAR
;
345 if (pParams
->SwapEffect
!= D3DSWAPEFFECT_DISCARD
)
346 tmplt
.bind
|= PIPE_BIND_RENDER_TARGET
;
347 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
348 pipe_resource_reference(&(This
->present_buffers
[i
]), resource
);
350 This
->present_handles
[i
] = D3DWindowBuffer_create(This
, resource
, depth
, false);
351 pipe_resource_reference(&resource
, NULL
);
353 if (pParams
->EnableAutoDepthStencil
) {
354 tmplt
.bind
= d3d9_get_pipe_depth_format_bindings(pParams
->AutoDepthStencilFormat
);
355 /* Checking the d3d9 depth format for texture support indicates the app if it can use
356 * the format for shadow mapping or texturing. If the check returns true, then the app
357 * is allowed to use this functionnality, so try first to create the buffer
358 * with PIPE_BIND_SAMPLER_VIEW. If the format can't be created with it, try without.
359 * If it fails with PIPE_BIND_SAMPLER_VIEW, then the app check for texture support
360 * would fail too, so we are fine. */
361 tmplt
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
362 tmplt
.nr_samples
= pParams
->MultiSampleType
;
363 tmplt
.format
= d3d9_to_pipe_format_checked(This
->screen
,
364 pParams
->AutoDepthStencilFormat
,
369 if (tmplt
.format
== PIPE_FORMAT_NONE
) {
370 tmplt
.bind
&= ~PIPE_BIND_SAMPLER_VIEW
;
371 tmplt
.format
= d3d9_to_pipe_format_checked(This
->screen
,
372 pParams
->AutoDepthStencilFormat
,
379 if (tmplt
.format
== PIPE_FORMAT_NONE
)
380 return D3DERR_INVALIDCALL
;
382 resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
384 DBG("Failed to create pipe_resource for depth buffer.\n");
385 return D3DERR_OUTOFVIDEOMEMORY
;
388 NineSurface9_SetResourceResize(This
->zsbuf
, resource
);
389 pipe_resource_reference(&resource
, NULL
);
391 /* XXX wine thinks the container of this should be the device */
392 desc
.Format
= pParams
->AutoDepthStencilFormat
;
393 desc
.Usage
= D3DUSAGE_DEPTHSTENCIL
;
394 hr
= NineSurface9_new(pDevice
, NineUnknown(pDevice
), resource
, NULL
, 0,
395 0, 0, &desc
, &This
->zsbuf
);
396 pipe_resource_reference(&resource
, NULL
);
398 DBG("Failed to create ZS surface.\n");
401 This
->zsbuf
->base
.base
.forward
= FALSE
;
405 This
->params
= *pParams
;
410 /* Throttling: code adapted from the dri state tracker */
413 * swap_fences_pop_front - pull a fence from the throttle queue
415 * If the throttle queue is filled to the desired number of fences,
416 * pull fences off the queue until the number is less than the desired
417 * number of fences, and return the last fence pulled.
419 static struct pipe_fence_handle
*
420 swap_fences_pop_front(struct NineSwapChain9
*This
)
422 struct pipe_screen
*screen
= This
->screen
;
423 struct pipe_fence_handle
*fence
= NULL
;
425 if (This
->desired_fences
== 0)
428 if (This
->cur_fences
>= This
->desired_fences
) {
429 screen
->fence_reference(screen
, &fence
, This
->swap_fences
[This
->tail
]);
430 screen
->fence_reference(screen
, &This
->swap_fences
[This
->tail
++], NULL
);
431 This
->tail
&= DRI_SWAP_FENCES_MASK
;
439 * swap_fences_see_front - same than swap_fences_pop_front without
444 static struct pipe_fence_handle
*
445 swap_fences_see_front(struct NineSwapChain9
*This
)
447 struct pipe_screen
*screen
= This
->screen
;
448 struct pipe_fence_handle
*fence
= NULL
;
450 if (This
->desired_fences
== 0)
453 if (This
->cur_fences
>= This
->desired_fences
) {
454 screen
->fence_reference(screen
, &fence
, This
->swap_fences
[This
->tail
]);
461 * swap_fences_push_back - push a fence onto the throttle queue at the back
463 * push a fence onto the throttle queue and pull fences of the queue
464 * so that the desired number of fences are on the queue.
467 swap_fences_push_back(struct NineSwapChain9
*This
,
468 struct pipe_fence_handle
*fence
)
470 struct pipe_screen
*screen
= This
->screen
;
472 if (!fence
|| This
->desired_fences
== 0)
475 while(This
->cur_fences
== This
->desired_fences
)
476 swap_fences_pop_front(This
);
479 screen
->fence_reference(screen
, &This
->swap_fences
[This
->head
++],
481 This
->head
&= DRI_SWAP_FENCES_MASK
;
486 * swap_fences_unref - empty the throttle queue
488 * pulls fences of the throttle queue until it is empty.
491 swap_fences_unref(struct NineSwapChain9
*This
)
493 struct pipe_screen
*screen
= This
->screen
;
495 while(This
->cur_fences
) {
496 screen
->fence_reference(screen
, &This
->swap_fences
[This
->tail
++], NULL
);
497 This
->tail
&= DRI_SWAP_FENCES_MASK
;
503 NineSwapChain9_dtor( struct NineSwapChain9
*This
)
507 DBG("This=%p\n", This
);
510 _mesa_threadpool_destroy(This
, This
->pool
);
513 for (i
= 0; i
< This
->params
.BackBufferCount
; i
++) {
514 NineUnknown_Release(NineUnknown(This
->buffers
[i
]));
515 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, This
->present_handles
[i
]);
516 if (This
->present_buffers
)
517 pipe_resource_reference(&(This
->present_buffers
[i
]), NULL
);
520 FREE(This
->present_buffers
);
523 NineUnknown_Destroy(NineUnknown(This
->zsbuf
));
526 ID3DPresent_Release(This
->present
);
528 swap_fences_unref(This
);
529 NineUnknown_dtor(&This
->base
);
533 create_present_buffer( struct NineSwapChain9
*This
,
534 unsigned int width
, unsigned int height
,
535 struct pipe_resource
**resource
,
536 D3DWindowBuffer
**present_handle
)
538 struct pipe_resource tmplt
;
540 tmplt
.target
= PIPE_TEXTURE_2D
;
541 tmplt
.width0
= width
;
542 tmplt
.height0
= height
;
544 tmplt
.last_level
= 0;
545 tmplt
.array_size
= 1;
546 tmplt
.usage
= PIPE_USAGE_DEFAULT
;
548 tmplt
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
549 tmplt
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
|
550 PIPE_BIND_SHARED
| PIPE_BIND_SCANOUT
| PIPE_BIND_DISPLAY_TARGET
;
551 tmplt
.nr_samples
= 0;
552 if (This
->actx
->linear_framebuffer
)
553 tmplt
.bind
|= PIPE_BIND_LINEAR
;
554 *resource
= This
->screen
->resource_create(This
->screen
, &tmplt
);
556 *present_handle
= D3DWindowBuffer_create(This
, *resource
, 24, true);
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
) {
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 return S_PRESENT_OCCLUDED
;
795 if (NineSwapChain9_GetOccluded(This
) ||
796 NineSwapChain9_ResolutionMismatch(This
)) {
797 This
->base
.device
->device_needs_reset
= TRUE
;
799 if (This
->base
.device
->device_needs_reset
) {
800 return D3DERR_DEVICELOST
;
804 hr
= present(This
, pSourceRect
, pDestRect
,
805 hDestWindowOverride
, pDirtyRegion
, dwFlags
);
806 if (hr
== D3DERR_WASSTILLDRAWING
)
809 switch (This
->params
.SwapEffect
) {
810 case D3DSWAPEFFECT_FLIP
:
812 case D3DSWAPEFFECT_DISCARD
:
813 /* rotate the queue */
814 pipe_resource_reference(&res
, This
->buffers
[0]->base
.resource
);
815 for (i
= 1; i
<= This
->params
.BackBufferCount
; i
++) {
816 NineSurface9_SetResourceResize(This
->buffers
[i
- 1],
817 This
->buffers
[i
]->base
.resource
);
819 NineSurface9_SetResourceResize(
820 This
->buffers
[This
->params
.BackBufferCount
], res
);
821 pipe_resource_reference(&res
, NULL
);
823 if (This
->present_buffers
) {
824 pipe_resource_reference(&res
, This
->present_buffers
[0]);
825 for (i
= 1; i
<= This
->params
.BackBufferCount
; i
++)
826 pipe_resource_reference(&(This
->present_buffers
[i
-1]), This
->present_buffers
[i
]);
827 pipe_resource_reference(&(This
->present_buffers
[This
->params
.BackBufferCount
]), res
);
828 pipe_resource_reference(&res
, NULL
);
831 handle_temp
= This
->present_handles
[0];
832 for (i
= 1; i
<= This
->params
.BackBufferCount
; i
++) {
833 This
->present_handles
[i
-1] = This
->present_handles
[i
];
835 This
->present_handles
[This
->params
.BackBufferCount
] = handle_temp
;
836 task_temp
= This
->tasks
[0];
837 for (i
= 1; i
<= This
->params
.BackBufferCount
; i
++) {
838 This
->tasks
[i
-1] = This
->tasks
[i
];
840 This
->tasks
[This
->params
.BackBufferCount
] = task_temp
;
843 case D3DSWAPEFFECT_COPY
:
848 case D3DSWAPEFFECT_OVERLAY
:
849 /* XXX not implemented */
852 case D3DSWAPEFFECT_FLIPEX
:
853 /* XXX not implemented */
858 _mesa_threadpool_wait_for_task(This
->pool
, &(This
->tasks
[0]));
860 ID3DPresent_WaitBufferReleased(This
->present
, This
->present_handles
[0]);
862 This
->base
.device
->state
.changed
.group
|= NINE_STATE_FB
;
868 NineSwapChain9_GetFrontBufferData( struct NineSwapChain9
*This
,
869 IDirect3DSurface9
*pDestSurface
)
871 struct NineSurface9
*dest_surface
= NineSurface9(pDestSurface
);
872 struct NineDevice9
*pDevice
= This
->base
.device
;
873 unsigned int width
, height
;
874 struct pipe_resource
*temp_resource
;
875 struct NineSurface9
*temp_surface
;
876 D3DWindowBuffer
*temp_handle
;
877 D3DSURFACE_DESC desc
;
880 DBG("GetFrontBufferData: This=%p pDestSurface=%p\n",
883 user_assert(dest_surface
->base
.pool
== D3DPOOL_SYSTEMMEM
, D3DERR_INVALIDCALL
);
885 width
= dest_surface
->desc
.Width
;
886 height
= dest_surface
->desc
.Height
;
888 /* Note: front window size and destination size are supposed
889 * to match. However it's not very clear what should get taken in Windowed
890 * mode. It may need a fix */
891 create_present_buffer(This
, width
, height
, &temp_resource
, &temp_handle
);
893 desc
.Type
= D3DRTYPE_SURFACE
;
894 desc
.Pool
= D3DPOOL_DEFAULT
;
895 desc
.MultiSampleType
= D3DMULTISAMPLE_NONE
;
896 desc
.MultiSampleQuality
= 0;
898 desc
.Height
= height
;
899 /* NineSurface9_CopyDefaultToMem needs same format. */
900 desc
.Format
= dest_surface
->desc
.Format
;
901 desc
.Usage
= D3DUSAGE_RENDERTARGET
;
902 hr
= NineSurface9_new(pDevice
, NineUnknown(This
), temp_resource
, NULL
, 0,
903 0, 0, &desc
, &temp_surface
);
904 pipe_resource_reference(&temp_resource
, NULL
);
906 DBG("Failed to create temp FrontBuffer surface.\n");
910 ID3DPresent_FrontBufferCopy(This
->present
, temp_handle
);
912 NineSurface9_CopyDefaultToMem(dest_surface
, temp_surface
);
914 ID3DPresent_DestroyD3DWindowBuffer(This
->present
, temp_handle
);
915 NineUnknown_Destroy(NineUnknown(temp_surface
));
921 NineSwapChain9_GetBackBuffer( struct NineSwapChain9
*This
,
923 D3DBACKBUFFER_TYPE Type
,
924 IDirect3DSurface9
**ppBackBuffer
)
926 DBG("GetBackBuffer: This=%p iBackBuffer=%d Type=%d ppBackBuffer=%p\n",
927 This
, iBackBuffer
, Type
, ppBackBuffer
);
928 (void)user_error(Type
== D3DBACKBUFFER_TYPE_MONO
);
929 /* don't touch ppBackBuffer on error */
930 user_assert(ppBackBuffer
!= NULL
, D3DERR_INVALIDCALL
);
931 user_assert(iBackBuffer
< This
->params
.BackBufferCount
, D3DERR_INVALIDCALL
);
933 NineUnknown_AddRef(NineUnknown(This
->buffers
[iBackBuffer
]));
934 *ppBackBuffer
= (IDirect3DSurface9
*)This
->buffers
[iBackBuffer
];
939 NineSwapChain9_GetRasterStatus( struct NineSwapChain9
*This
,
940 D3DRASTER_STATUS
*pRasterStatus
)
942 DBG("GetRasterStatus: This=%p pRasterStatus=%p\n",
943 This
, pRasterStatus
);
944 user_assert(pRasterStatus
!= NULL
, E_POINTER
);
945 return ID3DPresent_GetRasterStatus(This
->present
, pRasterStatus
);
949 NineSwapChain9_GetDisplayMode( struct NineSwapChain9
*This
,
950 D3DDISPLAYMODE
*pMode
)
952 D3DDISPLAYMODEEX mode
;
953 D3DDISPLAYROTATION rot
;
956 DBG("GetDisplayMode: This=%p pMode=%p\n",
958 user_assert(pMode
!= NULL
, E_POINTER
);
960 hr
= ID3DPresent_GetDisplayMode(This
->present
, &mode
, &rot
);
962 pMode
->Width
= mode
.Width
;
963 pMode
->Height
= mode
.Height
;
964 pMode
->RefreshRate
= mode
.RefreshRate
;
965 pMode
->Format
= mode
.Format
;
971 NineSwapChain9_GetPresentParameters( struct NineSwapChain9
*This
,
972 D3DPRESENT_PARAMETERS
*pPresentationParameters
)
974 DBG("GetPresentParameters: This=%p pPresentationParameters=%p\n",
975 This
, pPresentationParameters
);
976 user_assert(pPresentationParameters
!= NULL
, E_POINTER
);
977 *pPresentationParameters
= This
->params
;
981 IDirect3DSwapChain9Vtbl NineSwapChain9_vtable
= {
982 (void *)NineUnknown_QueryInterface
,
983 (void *)NineUnknown_AddRef
,
984 (void *)NineUnknown_Release
,
985 (void *)NineSwapChain9_Present
,
986 (void *)NineSwapChain9_GetFrontBufferData
,
987 (void *)NineSwapChain9_GetBackBuffer
,
988 (void *)NineSwapChain9_GetRasterStatus
,
989 (void *)NineSwapChain9_GetDisplayMode
,
990 (void *)NineUnknown_GetDevice
, /* actually part of SwapChain9 iface */
991 (void *)NineSwapChain9_GetPresentParameters
994 static const GUID
*NineSwapChain9_IIDs
[] = {
995 &IID_IDirect3DSwapChain9
,
1001 NineSwapChain9_new( struct NineDevice9
*pDevice
,
1003 ID3DPresent
*pPresent
,
1004 D3DPRESENT_PARAMETERS
*pPresentationParameters
,
1005 struct d3dadapter9_context
*pCTX
,
1007 struct NineSwapChain9
**ppOut
)
1009 NINE_DEVICE_CHILD_NEW(SwapChain9
, ppOut
, pDevice
, /* args */
1010 implicit
, pPresent
, pPresentationParameters
,
1011 pCTX
, hFocusWindow
, NULL
);
1015 NineSwapChain9_GetOccluded( struct NineSwapChain9
*This
)
1017 if (This
->base
.device
->minor_version_num
> 0) {
1018 return ID3DPresent_GetWindowOccluded(This
->present
);
1025 NineSwapChain9_ResolutionMismatch( struct NineSwapChain9
*This
)
1027 if (This
->base
.device
->minor_version_num
> 1) {
1028 return ID3DPresent_ResolutionMismatch(This
->present
);
1035 NineSwapChain9_CreateThread( struct NineSwapChain9
*This
,
1039 if (This
->base
.device
->minor_version_num
> 1) {
1040 return ID3DPresent_CreateThread(This
->present
, pFuncAddress
, pParam
);
1047 NineSwapChain9_WaitForThread( struct NineSwapChain9
*This
,
1050 if (This
->base
.device
->minor_version_num
> 1) {
1051 (void) ID3DPresent_WaitForThread(This
->present
, thread
);