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