st/nine: Refactor how user constbufs sizes are calculated
[mesa.git] / src / gallium / state_trackers / nine / swapchain9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "swapchain9.h"
24 #include "surface9.h"
25 #include "device9.h"
26
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29 #include "nine_dump.h"
30
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"
35
36 #include "threadpool.h"
37
38 #define DBG_CHANNEL DBG_SWAPCHAIN
39
40 #define UNTESTED(n) DBG("UNTESTED point %d. Please tell if it worked\n", n)
41
42 HRESULT
43 NineSwapChain9_ctor( struct NineSwapChain9 *This,
44 struct NineUnknownParams *pParams,
45 BOOL implicit,
46 ID3DPresent *pPresent,
47 D3DPRESENT_PARAMETERS *pPresentationParameters,
48 struct d3dadapter9_context *pCTX,
49 HWND hFocusWindow,
50 D3DDISPLAYMODEEX *mode )
51 {
52 HRESULT hr;
53
54 DBG("This=%p pDevice=%p pPresent=%p pCTX=%p hFocusWindow=%p\n",
55 This, pParams->device, pPresent, pCTX, hFocusWindow);
56
57 hr = NineUnknown_ctor(&This->base, pParams);
58 if (FAILED(hr))
59 return hr;
60
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;
65 This->actx = pCTX;
66 This->present = pPresent;
67 This->mode = NULL;
68
69 ID3DPresent_AddRef(pPresent);
70
71 if (!pPresentationParameters->hDeviceWindow)
72 pPresentationParameters->hDeviceWindow = hFocusWindow;
73
74 This->rendering_done = FALSE;
75 This->pool = NULL;
76 return NineSwapChain9_Resize(This, pPresentationParameters, mode);
77 }
78
79 static D3DWindowBuffer *
80 D3DWindowBuffer_create(struct NineSwapChain9 *This,
81 struct pipe_resource *resource,
82 int depth)
83 {
84 D3DWindowBuffer *ret;
85 struct winsys_handle whandle;
86 int stride, dmaBufFd;
87
88 memset(&whandle, 0, sizeof(whandle));
89 whandle.type = DRM_API_HANDLE_TYPE_FD;
90 This->screen->resource_get_handle(This->screen, resource, &whandle);
91 stride = whandle.stride;
92 dmaBufFd = whandle.handle;
93 ID3DPresent_NewD3DWindowBufferFromDmaBuf(This->present,
94 dmaBufFd,
95 resource->width0,
96 resource->height0,
97 stride,
98 depth,
99 32,
100 &ret);
101 return ret;
102 }
103
104 HRESULT
105 NineSwapChain9_Resize( struct NineSwapChain9 *This,
106 D3DPRESENT_PARAMETERS *pParams,
107 D3DDISPLAYMODEEX *mode )
108 {
109 struct NineDevice9 *pDevice = This->base.device;
110 struct NineSurface9 **bufs;
111 D3DSURFACE_DESC desc;
112 HRESULT hr;
113 struct pipe_resource *resource, tmplt;
114 enum pipe_format pf;
115 BOOL has_present_buffers = FALSE;
116 int depth;
117 unsigned i, oldBufferCount, newBufferCount;
118
119 DBG("This=%p pParams=%p\n", This, pParams);
120 user_assert(pParams != NULL, E_POINTER);
121
122 DBG("pParams(%p):\n"
123 "BackBufferWidth: %u\n"
124 "BackBufferHeight: %u\n"
125 "BackBufferFormat: %s\n"
126 "BackBufferCount: %u\n"
127 "MultiSampleType: %u\n"
128 "MultiSampleQuality: %u\n"
129 "SwapEffect: %u\n"
130 "hDeviceWindow: %p\n"
131 "Windowed: %i\n"
132 "EnableAutoDepthStencil: %i\n"
133 "AutoDepthStencilFormat: %s\n"
134 "Flags: %s\n"
135 "FullScreen_RefreshRateInHz: %u\n"
136 "PresentationInterval: %x\n", pParams,
137 pParams->BackBufferWidth, pParams->BackBufferHeight,
138 d3dformat_to_string(pParams->BackBufferFormat),
139 pParams->BackBufferCount,
140 pParams->MultiSampleType, pParams->MultiSampleQuality,
141 pParams->SwapEffect, pParams->hDeviceWindow, pParams->Windowed,
142 pParams->EnableAutoDepthStencil,
143 d3dformat_to_string(pParams->AutoDepthStencilFormat),
144 nine_D3DPRESENTFLAG_to_str(pParams->Flags),
145 pParams->FullScreen_RefreshRateInHz,
146 pParams->PresentationInterval);
147
148 if (pParams->SwapEffect == D3DSWAPEFFECT_COPY &&
149 pParams->BackBufferCount > 1) {
150 pParams->BackBufferCount = 1;
151 }
152
153 if (pParams->BackBufferCount > 3) {
154 pParams->BackBufferCount = 3;
155 }
156
157 if (pParams->BackBufferCount == 0) {
158 pParams->BackBufferCount = 1;
159 }
160
161 if (pParams->BackBufferFormat == D3DFMT_UNKNOWN) {
162 pParams->BackBufferFormat = D3DFMT_A8R8G8B8;
163 }
164
165 This->desired_fences = This->actx->throttling ? This->actx->throttling_value + 1 : 0;
166 /* +1 because we add the fence of the current buffer before popping an old one */
167 if (This->desired_fences > DRI_SWAP_FENCES_MAX)
168 This->desired_fences = DRI_SWAP_FENCES_MAX;
169
170 if (This->actx->vblank_mode == 0)
171 pParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
172 else if (This->actx->vblank_mode == 3)
173 pParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
174
175 if (mode && This->mode) {
176 *(This->mode) = *mode;
177 } else if (mode) {
178 This->mode = malloc(sizeof(D3DDISPLAYMODEEX));
179 memcpy(This->mode, mode, sizeof(D3DDISPLAYMODEEX));
180 } else {
181 free(This->mode);
182 This->mode = NULL;
183 }
184
185 /* Note: It is the role of the backend to fill if neccessary
186 * BackBufferWidth and BackBufferHeight */
187 ID3DPresent_SetPresentParameters(This->present, pParams, This->mode);
188
189 /* When we have flip behaviour, d3d9 expects we get back the screen buffer when we flip.
190 * Here we don't get back the initial content of the screen. To emulate the behaviour
191 * we allocate an additional buffer */
192 oldBufferCount = This->params.BackBufferCount ?
193 (This->params.BackBufferCount +
194 (This->params.SwapEffect != D3DSWAPEFFECT_COPY)) : 0;
195 newBufferCount = pParams->BackBufferCount +
196 (pParams->SwapEffect != D3DSWAPEFFECT_COPY);
197
198 pf = d3d9_to_pipe_format(pParams->BackBufferFormat);
199 if (This->actx->linear_framebuffer ||
200 (pf != PIPE_FORMAT_B8G8R8X8_UNORM &&
201 pf != PIPE_FORMAT_B8G8R8A8_UNORM) ||
202 pParams->SwapEffect != D3DSWAPEFFECT_DISCARD ||
203 pParams->MultiSampleType >= 2 ||
204 (This->actx->ref && This->actx->ref == This->screen))
205 has_present_buffers = TRUE;
206
207 /* Note: the buffer depth has to match the window depth.
208 * In practice, ARGB buffers can be used with windows
209 * of depth 24. Windows of depth 32 are extremely rare.
210 * So even if the buffer is ARGB, say it is depth 24.
211 * It is common practice, for example that's how
212 * glamor implements depth 24.
213 * TODO: handle windows with other depths. Not possible in the short term.
214 * For example 16 bits.*/
215 depth = 24;
216
217 tmplt.target = PIPE_TEXTURE_2D;
218 tmplt.width0 = pParams->BackBufferWidth;
219 tmplt.height0 = pParams->BackBufferHeight;
220 tmplt.depth0 = 1;
221 tmplt.last_level = 0;
222 tmplt.array_size = 1;
223 tmplt.usage = PIPE_USAGE_DEFAULT;
224 tmplt.flags = 0;
225
226 desc.Type = D3DRTYPE_SURFACE;
227 desc.Pool = D3DPOOL_DEFAULT;
228 desc.MultiSampleType = pParams->MultiSampleType;
229 desc.MultiSampleQuality = 0;
230 desc.Width = pParams->BackBufferWidth;
231 desc.Height = pParams->BackBufferHeight;
232
233 if (This->pool) {
234 _mesa_threadpool_destroy(This->pool);
235 This->pool = NULL;
236 }
237 This->enable_threadpool = This->actx->thread_submit && (pParams->SwapEffect != D3DSWAPEFFECT_COPY);
238 if (This->enable_threadpool)
239 This->pool = _mesa_threadpool_create();
240 if (!This->pool)
241 This->enable_threadpool = FALSE;
242
243 This->tasks = REALLOC(This->tasks,
244 oldBufferCount * sizeof(struct threadpool_task *),
245 newBufferCount * sizeof(struct threadpool_task *));
246 memset(This->tasks, 0, newBufferCount * sizeof(struct threadpool_task *));
247
248 for (i = 0; i < oldBufferCount; i++) {
249 ID3DPresent_DestroyD3DWindowBuffer(This->present, This->present_handles[i]);
250 This->present_handles[i] = NULL;
251 if (This->present_buffers)
252 pipe_resource_reference(&(This->present_buffers[i]), NULL);
253 }
254
255 if (!has_present_buffers && This->present_buffers) {
256 FREE(This->present_buffers);
257 This->present_buffers = NULL;
258 }
259
260 if (newBufferCount != oldBufferCount) {
261 for (i = newBufferCount; i < oldBufferCount;
262 ++i)
263 NineUnknown_Detach(NineUnknown(This->buffers[i]));
264
265 bufs = REALLOC(This->buffers,
266 oldBufferCount * sizeof(This->buffers[0]),
267 newBufferCount * sizeof(This->buffers[0]));
268 if (!bufs)
269 return E_OUTOFMEMORY;
270 This->buffers = bufs;
271 if (has_present_buffers) {
272 This->present_buffers = REALLOC(This->present_buffers,
273 This->present_buffers == NULL ? 0 : oldBufferCount * sizeof(struct pipe_resource *),
274 newBufferCount * sizeof(struct pipe_resource *));
275 memset(This->present_buffers, 0, newBufferCount * sizeof(struct pipe_resource *));
276 }
277 This->present_handles = REALLOC(This->present_handles,
278 oldBufferCount * sizeof(D3DWindowBuffer *),
279 newBufferCount * sizeof(D3DWindowBuffer *));
280 for (i = oldBufferCount; i < newBufferCount; ++i) {
281 This->buffers[i] = NULL;
282 This->present_handles[i] = NULL;
283 }
284 }
285
286 for (i = 0; i < newBufferCount; ++i) {
287 tmplt.format = d3d9_to_pipe_format(pParams->BackBufferFormat);
288 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_TRANSFER_READ |
289 PIPE_BIND_TRANSFER_WRITE | PIPE_BIND_RENDER_TARGET;
290 tmplt.nr_samples = pParams->MultiSampleType;
291 if (!has_present_buffers)
292 tmplt.bind |= PIPE_BIND_SHARED | PIPE_BIND_SCANOUT | PIPE_BIND_DISPLAY_TARGET;
293 resource = This->screen->resource_create(This->screen, &tmplt);
294 if (!resource) {
295 DBG("Failed to create pipe_resource.\n");
296 return D3DERR_OUTOFVIDEOMEMORY;
297 }
298 if (pParams->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
299 resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
300 if (This->buffers[i]) {
301 NineSurface9_SetResourceResize(This->buffers[i], resource);
302 if (has_present_buffers)
303 pipe_resource_reference(&resource, NULL);
304 } else {
305 desc.Format = pParams->BackBufferFormat;
306 desc.Usage = D3DUSAGE_RENDERTARGET;
307 hr = NineSurface9_new(pDevice, NineUnknown(This), resource, NULL, 0,
308 0, 0, &desc, &This->buffers[i]);
309 if (has_present_buffers)
310 pipe_resource_reference(&resource, NULL);
311 if (FAILED(hr)) {
312 DBG("Failed to create RT surface.\n");
313 return hr;
314 }
315 This->buffers[i]->base.base.forward = FALSE;
316 }
317 if (has_present_buffers) {
318 tmplt.format = PIPE_FORMAT_B8G8R8X8_UNORM;
319 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_SHARED | PIPE_BIND_SCANOUT | PIPE_BIND_DISPLAY_TARGET;
320 tmplt.nr_samples = 0;
321 if (This->actx->linear_framebuffer)
322 tmplt.bind |= PIPE_BIND_LINEAR;
323 if (pParams->SwapEffect != D3DSWAPEFFECT_DISCARD)
324 tmplt.bind |= PIPE_BIND_RENDER_TARGET;
325 resource = This->screen->resource_create(This->screen, &tmplt);
326 pipe_resource_reference(&(This->present_buffers[i]), resource);
327 }
328 This->present_handles[i] = D3DWindowBuffer_create(This, resource, depth);
329 if (!has_present_buffers)
330 pipe_resource_reference(&resource, NULL);
331 }
332 if (pParams->EnableAutoDepthStencil) {
333 tmplt.format = d3d9_to_pipe_format(pParams->AutoDepthStencilFormat);
334 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_TRANSFER_READ |
335 PIPE_BIND_TRANSFER_WRITE | PIPE_BIND_DEPTH_STENCIL;
336 tmplt.nr_samples = pParams->MultiSampleType;
337
338 resource = This->screen->resource_create(This->screen, &tmplt);
339 if (!resource) {
340 DBG("Failed to create pipe_resource for depth buffer.\n");
341 return D3DERR_OUTOFVIDEOMEMORY;
342 }
343 if (This->zsbuf) {
344 NineSurface9_SetResourceResize(This->zsbuf, resource);
345 pipe_resource_reference(&resource, NULL);
346 } else {
347 /* XXX wine thinks the container of this should be the device */
348 desc.Format = pParams->AutoDepthStencilFormat;
349 desc.Usage = D3DUSAGE_DEPTHSTENCIL;
350 hr = NineSurface9_new(pDevice, NineUnknown(pDevice), resource, NULL, 0,
351 0, 0, &desc, &This->zsbuf);
352 pipe_resource_reference(&resource, NULL);
353 if (FAILED(hr)) {
354 DBG("Failed to create ZS surface.\n");
355 return hr;
356 }
357 This->zsbuf->base.base.forward = FALSE;
358 }
359 }
360
361 This->params = *pParams;
362
363 return D3D_OK;
364 }
365
366 /* Throttling: code adapted from the dri state tracker */
367
368 /**
369 * swap_fences_pop_front - pull a fence from the throttle queue
370 *
371 * If the throttle queue is filled to the desired number of fences,
372 * pull fences off the queue until the number is less than the desired
373 * number of fences, and return the last fence pulled.
374 */
375 static struct pipe_fence_handle *
376 swap_fences_pop_front(struct NineSwapChain9 *This)
377 {
378 struct pipe_screen *screen = This->screen;
379 struct pipe_fence_handle *fence = NULL;
380
381 if (This->desired_fences == 0)
382 return NULL;
383
384 if (This->cur_fences >= This->desired_fences) {
385 screen->fence_reference(screen, &fence, This->swap_fences[This->tail]);
386 screen->fence_reference(screen, &This->swap_fences[This->tail++], NULL);
387 This->tail &= DRI_SWAP_FENCES_MASK;
388 --This->cur_fences;
389 }
390 return fence;
391 }
392
393
394 /**
395 * swap_fences_see_front - same than swap_fences_pop_front without
396 * pulling
397 *
398 */
399
400 static struct pipe_fence_handle *
401 swap_fences_see_front(struct NineSwapChain9 *This)
402 {
403 struct pipe_screen *screen = This->screen;
404 struct pipe_fence_handle *fence = NULL;
405
406 if (This->desired_fences == 0)
407 return NULL;
408
409 if (This->cur_fences >= This->desired_fences) {
410 screen->fence_reference(screen, &fence, This->swap_fences[This->tail]);
411 }
412 return fence;
413 }
414
415
416 /**
417 * swap_fences_push_back - push a fence onto the throttle queue at the back
418 *
419 * push a fence onto the throttle queue and pull fences of the queue
420 * so that the desired number of fences are on the queue.
421 */
422 static void
423 swap_fences_push_back(struct NineSwapChain9 *This,
424 struct pipe_fence_handle *fence)
425 {
426 struct pipe_screen *screen = This->screen;
427
428 if (!fence || This->desired_fences == 0)
429 return;
430
431 while(This->cur_fences == This->desired_fences)
432 swap_fences_pop_front(This);
433
434 This->cur_fences++;
435 screen->fence_reference(screen, &This->swap_fences[This->head++],
436 fence);
437 This->head &= DRI_SWAP_FENCES_MASK;
438 }
439
440
441 /**
442 * swap_fences_unref - empty the throttle queue
443 *
444 * pulls fences of the throttle queue until it is empty.
445 */
446 static void
447 swap_fences_unref(struct NineSwapChain9 *This)
448 {
449 struct pipe_screen *screen = This->screen;
450
451 while(This->cur_fences) {
452 screen->fence_reference(screen, &This->swap_fences[This->tail++], NULL);
453 This->tail &= DRI_SWAP_FENCES_MASK;
454 --This->cur_fences;
455 }
456 }
457
458 void
459 NineSwapChain9_dtor( struct NineSwapChain9 *This )
460 {
461 unsigned i;
462
463 DBG("This=%p\n", This);
464
465 if (This->pool)
466 _mesa_threadpool_destroy(This->pool);
467
468 if (This->buffers) {
469 for (i = 0; i < This->params.BackBufferCount; i++) {
470 NineUnknown_Release(NineUnknown(This->buffers[i]));
471 ID3DPresent_DestroyD3DWindowBuffer(This->present, This->present_handles[i]);
472 if (This->present_buffers)
473 pipe_resource_reference(&(This->present_buffers[i]), NULL);
474 }
475 FREE(This->buffers);
476 FREE(This->present_buffers);
477 }
478 if (This->zsbuf)
479 NineUnknown_Destroy(NineUnknown(This->zsbuf));
480
481 if (This->present)
482 ID3DPresent_Release(This->present);
483
484 swap_fences_unref(This);
485 NineUnknown_dtor(&This->base);
486 }
487
488 static void
489 create_present_buffer( struct NineSwapChain9 *This,
490 unsigned int width, unsigned int height,
491 struct pipe_resource **resource,
492 D3DWindowBuffer **present_handle)
493 {
494 struct pipe_resource tmplt;
495
496 tmplt.target = PIPE_TEXTURE_2D;
497 tmplt.width0 = width;
498 tmplt.height0 = height;
499 tmplt.depth0 = 1;
500 tmplt.last_level = 0;
501 tmplt.array_size = 1;
502 tmplt.usage = PIPE_USAGE_DEFAULT;
503 tmplt.flags = 0;
504 tmplt.format = PIPE_FORMAT_B8G8R8X8_UNORM;
505 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_TRANSFER_READ |
506 PIPE_BIND_TRANSFER_WRITE | PIPE_BIND_RENDER_TARGET |
507 PIPE_BIND_SHARED | PIPE_BIND_SCANOUT | PIPE_BIND_DISPLAY_TARGET;
508 tmplt.nr_samples = 0;
509 if (This->actx->linear_framebuffer)
510 tmplt.bind |= PIPE_BIND_LINEAR;
511 *resource = This->screen->resource_create(This->screen, &tmplt);
512
513 *present_handle = D3DWindowBuffer_create(This, *resource, 24);
514 }
515
516 static void
517 handle_draw_cursor_and_hud( struct NineSwapChain9 *This, struct pipe_resource *resource)
518 {
519 struct NineDevice9 *device = This->base.device;
520 struct pipe_blit_info blit;
521
522 if (device->cursor.software && device->cursor.visible && device->cursor.w) {
523 blit.src.resource = device->cursor.image;
524 blit.src.level = 0;
525 blit.src.format = device->cursor.image->format;
526 blit.src.box.x = 0;
527 blit.src.box.y = 0;
528 blit.src.box.z = 0;
529 blit.src.box.depth = 1;
530 blit.src.box.width = device->cursor.w;
531 blit.src.box.height = device->cursor.h;
532
533 blit.dst.resource = resource;
534 blit.dst.level = 0;
535 blit.dst.format = resource->format;
536 blit.dst.box.z = 0;
537 blit.dst.box.depth = 1;
538
539 blit.mask = PIPE_MASK_RGBA;
540 blit.filter = PIPE_TEX_FILTER_NEAREST;
541 blit.scissor_enable = FALSE;
542
543 ID3DPresent_GetCursorPos(This->present, &device->cursor.pos);
544
545 /* NOTE: blit messes up when box.x + box.width < 0, fix driver */
546 blit.dst.box.x = MAX2(device->cursor.pos.x, 0) - device->cursor.hotspot.x;
547 blit.dst.box.y = MAX2(device->cursor.pos.y, 0) - device->cursor.hotspot.y;
548 blit.dst.box.width = blit.src.box.width;
549 blit.dst.box.height = blit.src.box.height;
550
551 DBG("Blitting cursor(%ux%u) to (%i,%i).\n",
552 blit.src.box.width, blit.src.box.height,
553 blit.dst.box.x, blit.dst.box.y);
554
555 This->pipe->blit(This->pipe, &blit);
556 }
557
558 if (device->hud && resource) {
559 hud_draw(device->hud, resource); /* XXX: no offset */
560 /* HUD doesn't clobber stipple */
561 NineDevice9_RestoreNonCSOState(device, ~0x2);
562 }
563 }
564
565 struct end_present_struct {
566 struct pipe_screen *screen;
567 struct pipe_fence_handle *fence_to_wait;
568 ID3DPresent *present;
569 D3DWindowBuffer *present_handle;
570 HWND hDestWindowOverride;
571 };
572
573 static void work_present(void *data)
574 {
575 struct end_present_struct *work = data;
576 if (work->fence_to_wait) {
577 (void) work->screen->fence_finish(work->screen, work->fence_to_wait, PIPE_TIMEOUT_INFINITE);
578 work->screen->fence_reference(work->screen, &(work->fence_to_wait), NULL);
579 }
580 ID3DPresent_PresentBuffer(work->present, work->present_handle, work->hDestWindowOverride, NULL, NULL, NULL, 0);
581 free(work);
582 }
583
584 static void pend_present(struct NineSwapChain9 *This,
585 HWND hDestWindowOverride)
586 {
587 struct end_present_struct *work = calloc(1, sizeof(struct end_present_struct));
588
589 work->screen = This->screen;
590 work->fence_to_wait = swap_fences_pop_front(This);
591 work->present = This->present;
592 work->present_handle = This->present_handles[0];
593 work->hDestWindowOverride = hDestWindowOverride;
594 This->tasks[0] = _mesa_threadpool_queue_task(This->pool, work_present, work);
595
596 return;
597 }
598
599 static INLINE HRESULT
600 present( struct NineSwapChain9 *This,
601 const RECT *pSourceRect,
602 const RECT *pDestRect,
603 HWND hDestWindowOverride,
604 const RGNDATA *pDirtyRegion,
605 DWORD dwFlags )
606 {
607 struct pipe_resource *resource;
608 struct pipe_fence_handle *fence;
609 HRESULT hr;
610 struct pipe_blit_info blit;
611
612 DBG("present: This=%p pSourceRect=%p pDestRect=%p "
613 "pDirtyRegion=%p hDestWindowOverride=%p"
614 "dwFlags=%d resource=%p\n",
615 This, pSourceRect, pDestRect, pDirtyRegion,
616 hDestWindowOverride, (int)dwFlags, This->buffers[0]->base.resource);
617
618 if (pSourceRect)
619 DBG("pSourceRect = (%u..%u)x(%u..%u)\n",
620 pSourceRect->left, pSourceRect->right,
621 pSourceRect->top, pSourceRect->bottom);
622 if (pDestRect)
623 DBG("pDestRect = (%u..%u)x(%u..%u)\n",
624 pDestRect->left, pDestRect->right,
625 pDestRect->top, pDestRect->bottom);
626
627 /* TODO: in the case the source and destination rect have different size:
628 * We need to allocate a new buffer, and do a blit to it to resize.
629 * We can't use the present_buffer for that since when we created it,
630 * we couldn't guess which size would have been needed.
631 * If pDestRect or pSourceRect is null, we have to check the sizes
632 * from the source size, and the destination window size.
633 * In this case, either resize rngdata, or pass NULL instead
634 */
635 /* Note: This->buffers[0]->level should always be 0 */
636
637 if (This->rendering_done)
638 goto bypass_rendering;
639
640 resource = This->buffers[0]->base.resource;
641
642 if (This->params.SwapEffect == D3DSWAPEFFECT_DISCARD)
643 handle_draw_cursor_and_hud(This, resource);
644
645 if (This->present_buffers) {
646 blit.src.resource = resource;
647 blit.src.level = 0;
648 blit.src.format = resource->format;
649 blit.src.box.z = 0;
650 blit.src.box.depth = 1;
651 blit.src.box.x = 0;
652 blit.src.box.y = 0;
653 blit.src.box.width = resource->width0;
654 blit.src.box.height = resource->height0;
655
656 resource = This->present_buffers[0];
657
658 blit.dst.resource = resource;
659 blit.dst.level = 0;
660 blit.dst.format = resource->format;
661 blit.dst.box.z = 0;
662 blit.dst.box.depth = 1;
663 blit.dst.box.x = 0;
664 blit.dst.box.y = 0;
665 blit.dst.box.width = resource->width0;
666 blit.dst.box.height = resource->height0;
667
668 blit.mask = PIPE_MASK_RGBA;
669 blit.filter = PIPE_TEX_FILTER_NEAREST;
670 blit.scissor_enable = FALSE;
671
672 This->pipe->blit(This->pipe, &blit);
673 }
674
675 if (This->params.SwapEffect != D3DSWAPEFFECT_DISCARD)
676 handle_draw_cursor_and_hud(This, resource);
677
678 fence = NULL;
679 This->pipe->flush(This->pipe, &fence, PIPE_FLUSH_END_OF_FRAME);
680 if (fence) {
681 swap_fences_push_back(This, fence);
682 This->screen->fence_reference(This->screen, &fence, NULL);
683 }
684
685 This->rendering_done = TRUE;
686 bypass_rendering:
687
688 if (dwFlags & D3DPRESENT_DONOTWAIT) {
689 UNTESTED(2);
690 BOOL still_draw = FALSE;
691 fence = swap_fences_see_front(This);
692 if (fence) {
693 still_draw = !This->screen->fence_signalled(This->screen, fence);
694 This->screen->fence_reference(This->screen, &fence, NULL);
695 }
696 if (still_draw)
697 return D3DERR_WASSTILLDRAWING;
698 }
699
700 if (This->present_buffers)
701 resource = This->present_buffers[0];
702 else
703 resource = This->buffers[0]->base.resource;
704 This->pipe->flush_resource(This->pipe, resource);
705
706 if (!This->enable_threadpool) {
707 This->tasks[0]=NULL;
708 fence = swap_fences_pop_front(This);
709 if (fence) {
710 (void) This->screen->fence_finish(This->screen, fence, PIPE_TIMEOUT_INFINITE);
711 This->screen->fence_reference(This->screen, &fence, NULL);
712 }
713
714 hr = ID3DPresent_PresentBuffer(This->present, This->present_handles[0], hDestWindowOverride, pSourceRect, pDestRect, pDirtyRegion, dwFlags);
715
716 if (FAILED(hr)) { UNTESTED(3);return hr; }
717 } else {
718 pend_present(This, hDestWindowOverride);
719 }
720 This->rendering_done = FALSE;
721
722 return D3D_OK;
723 }
724
725 HRESULT WINAPI
726 NineSwapChain9_Present( struct NineSwapChain9 *This,
727 const RECT *pSourceRect,
728 const RECT *pDestRect,
729 HWND hDestWindowOverride,
730 const RGNDATA *pDirtyRegion,
731 DWORD dwFlags )
732 {
733 struct pipe_resource *res = NULL;
734 D3DWindowBuffer *handle_temp;
735 struct threadpool_task *task_temp;
736 int i;
737 HRESULT hr = present(This, pSourceRect, pDestRect,
738 hDestWindowOverride, pDirtyRegion, dwFlags);
739
740 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p "
741 "pDirtyRegion=%p dwFlags=%d\n",
742 This, pSourceRect, pDestRect, hDestWindowOverride,
743 pDirtyRegion,dwFlags);
744
745 if (hr == D3DERR_WASSTILLDRAWING)
746 return hr;
747
748 switch (This->params.SwapEffect) {
749 case D3DSWAPEFFECT_FLIP:
750 UNTESTED(4);
751 case D3DSWAPEFFECT_DISCARD:
752 /* rotate the queue */;
753 pipe_resource_reference(&res, This->buffers[0]->base.resource);
754 for (i = 1; i <= This->params.BackBufferCount; i++) {
755 NineSurface9_SetResourceResize(This->buffers[i - 1],
756 This->buffers[i]->base.resource);
757 }
758 NineSurface9_SetResourceResize(
759 This->buffers[This->params.BackBufferCount], res);
760 pipe_resource_reference(&res, NULL);
761
762 if (This->present_buffers) {
763 pipe_resource_reference(&res, This->present_buffers[0]);
764 for (i = 1; i <= This->params.BackBufferCount; i++)
765 pipe_resource_reference(&(This->present_buffers[i-1]), This->present_buffers[i]);
766 pipe_resource_reference(&(This->present_buffers[This->params.BackBufferCount]), res);
767 pipe_resource_reference(&res, NULL);
768 }
769
770 handle_temp = This->present_handles[0];
771 for (i = 1; i <= This->params.BackBufferCount; i++) {
772 This->present_handles[i-1] = This->present_handles[i];
773 }
774 This->present_handles[This->params.BackBufferCount] = handle_temp;
775 task_temp = This->tasks[0];
776 for (i = 1; i <= This->params.BackBufferCount; i++) {
777 This->tasks[i-1] = This->tasks[i];
778 }
779 This->tasks[This->params.BackBufferCount] = task_temp;
780 break;
781
782 case D3DSWAPEFFECT_COPY:
783 UNTESTED(5);
784 /* do nothing */
785 break;
786
787 case D3DSWAPEFFECT_OVERLAY:
788 /* XXX not implemented */
789 break;
790
791 case D3DSWAPEFFECT_FLIPEX:
792 /* XXX not implemented */
793 break;
794 }
795
796 if (This->tasks[0])
797 _mesa_threadpool_wait_for_task(This->pool, &(This->tasks[0]));
798
799 ID3DPresent_WaitBufferReleased(This->present, This->present_handles[0]);
800
801 This->base.device->state.changed.group |= NINE_STATE_FB;
802 nine_update_state(This->base.device, NINE_STATE_FB);
803
804 return hr;
805 }
806
807 HRESULT WINAPI
808 NineSwapChain9_GetFrontBufferData( struct NineSwapChain9 *This,
809 IDirect3DSurface9 *pDestSurface )
810 {
811 struct NineSurface9 *dest_surface = NineSurface9(pDestSurface);
812 struct NineDevice9 *pDevice = This->base.device;
813 unsigned int width, height;
814 struct pipe_resource *temp_resource;
815 struct NineSurface9 *temp_surface;
816 D3DWindowBuffer *temp_handle;
817 D3DSURFACE_DESC desc;
818 HRESULT hr;
819
820 DBG("GetFrontBufferData: This=%p pDestSurface=%p\n",
821 This, pDestSurface);
822
823 width = dest_surface->desc.Width;
824 height = dest_surface->desc.Height;
825
826 /* Note: front window size and destination size are supposed
827 * to match. However it's not very clear what should get taken in Windowed
828 * mode. It may need a fix */
829 create_present_buffer(This, width, height, &temp_resource, &temp_handle);
830
831 desc.Type = D3DRTYPE_SURFACE;
832 desc.Pool = D3DPOOL_DEFAULT;
833 desc.MultiSampleType = D3DMULTISAMPLE_NONE;
834 desc.MultiSampleQuality = 0;
835 desc.Width = width;
836 desc.Height = height;
837 /* NineSurface9_CopySurface needs same format. */
838 desc.Format = dest_surface->desc.Format;
839 desc.Usage = D3DUSAGE_RENDERTARGET;
840 hr = NineSurface9_new(pDevice, NineUnknown(This), temp_resource, NULL, 0,
841 0, 0, &desc, &temp_surface);
842 pipe_resource_reference(&temp_resource, NULL);
843 if (FAILED(hr)) {
844 DBG("Failed to create temp FrontBuffer surface.\n");
845 return hr;
846 }
847
848 ID3DPresent_FrontBufferCopy(This->present, temp_handle);
849
850 NineSurface9_CopySurface(dest_surface, temp_surface, NULL, NULL);
851
852 ID3DPresent_DestroyD3DWindowBuffer(This->present, temp_handle);
853 NineUnknown_Destroy(NineUnknown(temp_surface));
854
855 return D3D_OK;
856 }
857
858 HRESULT WINAPI
859 NineSwapChain9_GetBackBuffer( struct NineSwapChain9 *This,
860 UINT iBackBuffer,
861 D3DBACKBUFFER_TYPE Type,
862 IDirect3DSurface9 **ppBackBuffer )
863 {
864 DBG("GetBackBuffer: This=%p iBackBuffer=%d Type=%d ppBackBuffer=%p\n",
865 This, iBackBuffer, Type, ppBackBuffer);
866 (void)user_error(Type == D3DBACKBUFFER_TYPE_MONO);
867 user_assert(iBackBuffer < This->params.BackBufferCount, D3DERR_INVALIDCALL);
868 user_assert(ppBackBuffer != NULL, E_POINTER);
869
870 NineUnknown_AddRef(NineUnknown(This->buffers[iBackBuffer]));
871 *ppBackBuffer = (IDirect3DSurface9 *)This->buffers[iBackBuffer];
872 return D3D_OK;
873 }
874
875 HRESULT WINAPI
876 NineSwapChain9_GetRasterStatus( struct NineSwapChain9 *This,
877 D3DRASTER_STATUS *pRasterStatus )
878 {
879 DBG("GetRasterStatus: This=%p pRasterStatus=%p\n",
880 This, pRasterStatus);
881 user_assert(pRasterStatus != NULL, E_POINTER);
882 return ID3DPresent_GetRasterStatus(This->present, pRasterStatus);
883 }
884
885 HRESULT WINAPI
886 NineSwapChain9_GetDisplayMode( struct NineSwapChain9 *This,
887 D3DDISPLAYMODE *pMode )
888 {
889 D3DDISPLAYMODEEX mode;
890 D3DDISPLAYROTATION rot;
891 HRESULT hr;
892
893 DBG("GetDisplayMode: This=%p pMode=%p\n",
894 This, pMode);
895 user_assert(pMode != NULL, E_POINTER);
896
897 hr = ID3DPresent_GetDisplayMode(This->present, &mode, &rot);
898 if (SUCCEEDED(hr)) {
899 pMode->Width = mode.Width;
900 pMode->Height = mode.Height;
901 pMode->RefreshRate = mode.RefreshRate;
902 pMode->Format = mode.Format;
903 }
904 return hr;
905 }
906
907 HRESULT WINAPI
908 NineSwapChain9_GetPresentParameters( struct NineSwapChain9 *This,
909 D3DPRESENT_PARAMETERS *pPresentationParameters )
910 {
911 DBG("GetPresentParameters: This=%p pPresentationParameters=%p\n",
912 This, pPresentationParameters);
913 user_assert(pPresentationParameters != NULL, E_POINTER);
914 *pPresentationParameters = This->params;
915 return D3D_OK;
916 }
917
918 IDirect3DSwapChain9Vtbl NineSwapChain9_vtable = {
919 (void *)NineUnknown_QueryInterface,
920 (void *)NineUnknown_AddRef,
921 (void *)NineUnknown_Release,
922 (void *)NineSwapChain9_Present,
923 (void *)NineSwapChain9_GetFrontBufferData,
924 (void *)NineSwapChain9_GetBackBuffer,
925 (void *)NineSwapChain9_GetRasterStatus,
926 (void *)NineSwapChain9_GetDisplayMode,
927 (void *)NineUnknown_GetDevice, /* actually part of SwapChain9 iface */
928 (void *)NineSwapChain9_GetPresentParameters
929 };
930
931 static const GUID *NineSwapChain9_IIDs[] = {
932 &IID_IDirect3DSwapChain9,
933 &IID_IUnknown,
934 NULL
935 };
936
937 HRESULT
938 NineSwapChain9_new( struct NineDevice9 *pDevice,
939 BOOL implicit,
940 ID3DPresent *pPresent,
941 D3DPRESENT_PARAMETERS *pPresentationParameters,
942 struct d3dadapter9_context *pCTX,
943 HWND hFocusWindow,
944 struct NineSwapChain9 **ppOut )
945 {
946 NINE_DEVICE_CHILD_NEW(SwapChain9, ppOut, pDevice, /* args */
947 implicit, pPresent, pPresentationParameters,
948 pCTX, hFocusWindow, NULL);
949 }