gallium: remove PIPE_BIND_TRANSFER_READ/WRITE
[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 int for_frontbuffer_reading)
84 {
85 D3DWindowBuffer *ret;
86 struct winsys_handle whandle;
87 int stride, dmaBufFd;
88
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,
92 &whandle,
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,
100 dmaBufFd,
101 resource->width0,
102 resource->height0,
103 stride,
104 depth,
105 32,
106 &ret);
107 return ret;
108 }
109
110 HRESULT
111 NineSwapChain9_Resize( struct NineSwapChain9 *This,
112 D3DPRESENT_PARAMETERS *pParams,
113 D3DDISPLAYMODEEX *mode )
114 {
115 struct NineDevice9 *pDevice = This->base.device;
116 struct NineSurface9 **bufs;
117 D3DSURFACE_DESC desc;
118 HRESULT hr;
119 struct pipe_resource *resource, tmplt;
120 enum pipe_format pf;
121 BOOL has_present_buffers = FALSE;
122 int depth;
123 unsigned i, oldBufferCount, newBufferCount;
124
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);
135
136 DBG("pParams(%p):\n"
137 "BackBufferWidth: %u\n"
138 "BackBufferHeight: %u\n"
139 "BackBufferFormat: %s\n"
140 "BackBufferCount: %u\n"
141 "MultiSampleType: %u\n"
142 "MultiSampleQuality: %u\n"
143 "SwapEffect: %u\n"
144 "hDeviceWindow: %p\n"
145 "Windowed: %i\n"
146 "EnableAutoDepthStencil: %i\n"
147 "AutoDepthStencilFormat: %s\n"
148 "Flags: %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);
161
162 if (pParams->BackBufferCount > 3) {
163 pParams->BackBufferCount = 3;
164 }
165
166 if (pParams->BackBufferCount == 0) {
167 pParams->BackBufferCount = 1;
168 }
169
170 if (pParams->BackBufferFormat == D3DFMT_UNKNOWN) {
171 pParams->BackBufferFormat = D3DFMT_A8R8G8B8;
172 }
173
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;
178
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;
183
184 if (mode && This->mode) {
185 *(This->mode) = *mode;
186 } else if (mode) {
187 This->mode = malloc(sizeof(D3DDISPLAYMODEEX));
188 memcpy(This->mode, mode, sizeof(D3DDISPLAYMODEEX));
189 } else {
190 free(This->mode);
191 This->mode = NULL;
192 }
193
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);
197 if (hr != D3D_OK)
198 return hr;
199
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);
208
209 pf = d3d9_to_pipe_format_checked(This->screen, pParams->BackBufferFormat,
210 PIPE_TEXTURE_2D, pParams->MultiSampleType,
211 PIPE_BIND_RENDER_TARGET, FALSE, FALSE);
212
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;
220
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.*/
229 depth = 24;
230
231 tmplt.target = PIPE_TEXTURE_2D;
232 tmplt.width0 = pParams->BackBufferWidth;
233 tmplt.height0 = pParams->BackBufferHeight;
234 tmplt.depth0 = 1;
235 tmplt.last_level = 0;
236 tmplt.array_size = 1;
237 tmplt.usage = PIPE_USAGE_DEFAULT;
238 tmplt.flags = 0;
239
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;
246
247 if (This->pool) {
248 _mesa_threadpool_destroy(This, This->pool);
249 This->pool = NULL;
250 }
251 This->enable_threadpool = This->actx->thread_submit && (pParams->SwapEffect != D3DSWAPEFFECT_COPY);
252 if (This->enable_threadpool)
253 This->pool = _mesa_threadpool_create(This);
254 if (!This->pool)
255 This->enable_threadpool = FALSE;
256
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 *));
261
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);
267 }
268
269 if (!has_present_buffers && This->present_buffers) {
270 FREE(This->present_buffers);
271 This->present_buffers = NULL;
272 }
273
274 if (newBufferCount != oldBufferCount) {
275 for (i = newBufferCount; i < oldBufferCount;
276 ++i)
277 NineUnknown_Detach(NineUnknown(This->buffers[i]));
278
279 bufs = REALLOC(This->buffers,
280 oldBufferCount * sizeof(This->buffers[0]),
281 newBufferCount * sizeof(This->buffers[0]));
282 if (!bufs)
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;
291 }
292 }
293
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 *));
301 }
302
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,
310 PIPE_TEXTURE_2D,
311 tmplt.nr_samples,
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);
316 if (!resource) {
317 DBG("Failed to create pipe_resource.\n");
318 return D3DERR_OUTOFVIDEOMEMORY;
319 }
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);
326 } else {
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);
333 if (FAILED(hr)) {
334 DBG("Failed to create RT surface.\n");
335 return hr;
336 }
337 This->buffers[i]->base.base.forward = FALSE;
338 }
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);
349 }
350 This->present_handles[i] = D3DWindowBuffer_create(This, resource, depth, false);
351 pipe_resource_reference(&resource, NULL);
352 }
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,
365 PIPE_TEXTURE_2D,
366 tmplt.nr_samples,
367 tmplt.bind,
368 FALSE, FALSE);
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,
373 PIPE_TEXTURE_2D,
374 tmplt.nr_samples,
375 tmplt.bind,
376 FALSE, FALSE);
377 }
378
379 if (tmplt.format == PIPE_FORMAT_NONE)
380 return D3DERR_INVALIDCALL;
381
382 resource = This->screen->resource_create(This->screen, &tmplt);
383 if (!resource) {
384 DBG("Failed to create pipe_resource for depth buffer.\n");
385 return D3DERR_OUTOFVIDEOMEMORY;
386 }
387 if (This->zsbuf) {
388 NineSurface9_SetResourceResize(This->zsbuf, resource);
389 pipe_resource_reference(&resource, NULL);
390 } else {
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);
397 if (FAILED(hr)) {
398 DBG("Failed to create ZS surface.\n");
399 return hr;
400 }
401 This->zsbuf->base.base.forward = FALSE;
402 }
403 }
404
405 This->params = *pParams;
406
407 return D3D_OK;
408 }
409
410 /* Throttling: code adapted from the dri state tracker */
411
412 /**
413 * swap_fences_pop_front - pull a fence from the throttle queue
414 *
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.
418 */
419 static struct pipe_fence_handle *
420 swap_fences_pop_front(struct NineSwapChain9 *This)
421 {
422 struct pipe_screen *screen = This->screen;
423 struct pipe_fence_handle *fence = NULL;
424
425 if (This->desired_fences == 0)
426 return NULL;
427
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;
432 --This->cur_fences;
433 }
434 return fence;
435 }
436
437
438 /**
439 * swap_fences_see_front - same than swap_fences_pop_front without
440 * pulling
441 *
442 */
443
444 static struct pipe_fence_handle *
445 swap_fences_see_front(struct NineSwapChain9 *This)
446 {
447 struct pipe_screen *screen = This->screen;
448 struct pipe_fence_handle *fence = NULL;
449
450 if (This->desired_fences == 0)
451 return NULL;
452
453 if (This->cur_fences >= This->desired_fences) {
454 screen->fence_reference(screen, &fence, This->swap_fences[This->tail]);
455 }
456 return fence;
457 }
458
459
460 /**
461 * swap_fences_push_back - push a fence onto the throttle queue at the back
462 *
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.
465 */
466 static void
467 swap_fences_push_back(struct NineSwapChain9 *This,
468 struct pipe_fence_handle *fence)
469 {
470 struct pipe_screen *screen = This->screen;
471
472 if (!fence || This->desired_fences == 0)
473 return;
474
475 while(This->cur_fences == This->desired_fences)
476 swap_fences_pop_front(This);
477
478 This->cur_fences++;
479 screen->fence_reference(screen, &This->swap_fences[This->head++],
480 fence);
481 This->head &= DRI_SWAP_FENCES_MASK;
482 }
483
484
485 /**
486 * swap_fences_unref - empty the throttle queue
487 *
488 * pulls fences of the throttle queue until it is empty.
489 */
490 static void
491 swap_fences_unref(struct NineSwapChain9 *This)
492 {
493 struct pipe_screen *screen = This->screen;
494
495 while(This->cur_fences) {
496 screen->fence_reference(screen, &This->swap_fences[This->tail++], NULL);
497 This->tail &= DRI_SWAP_FENCES_MASK;
498 --This->cur_fences;
499 }
500 }
501
502 void
503 NineSwapChain9_dtor( struct NineSwapChain9 *This )
504 {
505 unsigned i;
506
507 DBG("This=%p\n", This);
508
509 if (This->pool)
510 _mesa_threadpool_destroy(This, This->pool);
511
512 if (This->buffers) {
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);
518 }
519 FREE(This->buffers);
520 FREE(This->present_buffers);
521 }
522 if (This->zsbuf)
523 NineUnknown_Destroy(NineUnknown(This->zsbuf));
524
525 if (This->present)
526 ID3DPresent_Release(This->present);
527
528 swap_fences_unref(This);
529 NineUnknown_dtor(&This->base);
530 }
531
532 static void
533 create_present_buffer( struct NineSwapChain9 *This,
534 unsigned int width, unsigned int height,
535 struct pipe_resource **resource,
536 D3DWindowBuffer **present_handle)
537 {
538 struct pipe_resource tmplt;
539
540 tmplt.target = PIPE_TEXTURE_2D;
541 tmplt.width0 = width;
542 tmplt.height0 = height;
543 tmplt.depth0 = 1;
544 tmplt.last_level = 0;
545 tmplt.array_size = 1;
546 tmplt.usage = PIPE_USAGE_DEFAULT;
547 tmplt.flags = 0;
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);
555
556 *present_handle = D3DWindowBuffer_create(This, *resource, 24, true);
557 }
558
559 static void
560 handle_draw_cursor_and_hud( struct NineSwapChain9 *This, struct pipe_resource *resource)
561 {
562 struct NineDevice9 *device = This->base.device;
563 struct pipe_blit_info blit;
564
565 if (device->cursor.software && device->cursor.visible && device->cursor.w) {
566 memset(&blit, 0, sizeof(blit));
567 blit.src.resource = device->cursor.image;
568 blit.src.level = 0;
569 blit.src.format = device->cursor.image->format;
570 blit.src.box.x = 0;
571 blit.src.box.y = 0;
572 blit.src.box.z = 0;
573 blit.src.box.depth = 1;
574 blit.src.box.width = device->cursor.w;
575 blit.src.box.height = device->cursor.h;
576
577 blit.dst.resource = resource;
578 blit.dst.level = 0;
579 blit.dst.format = resource->format;
580 blit.dst.box.z = 0;
581 blit.dst.box.depth = 1;
582
583 blit.mask = PIPE_MASK_RGBA;
584 blit.filter = PIPE_TEX_FILTER_NEAREST;
585 blit.scissor_enable = FALSE;
586
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;
595
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);
599
600 blit.alpha_blend = TRUE;
601 This->pipe->blit(This->pipe, &blit);
602 }
603
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);
608 }
609 }
610
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;
617 };
618
619 static void work_present(void *data)
620 {
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);
625 }
626 ID3DPresent_PresentBuffer(work->present, work->present_handle, work->hDestWindowOverride, NULL, NULL, NULL, 0);
627 free(work);
628 }
629
630 static void pend_present(struct NineSwapChain9 *This,
631 HWND hDestWindowOverride)
632 {
633 struct end_present_struct *work = calloc(1, sizeof(struct end_present_struct));
634
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);
641
642 return;
643 }
644
645 static inline HRESULT
646 present( struct NineSwapChain9 *This,
647 const RECT *pSourceRect,
648 const RECT *pDestRect,
649 HWND hDestWindowOverride,
650 const RGNDATA *pDirtyRegion,
651 DWORD dwFlags )
652 {
653 struct pipe_resource *resource;
654 struct pipe_fence_handle *fence;
655 HRESULT hr;
656 struct pipe_blit_info blit;
657
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);
663
664 if (pSourceRect)
665 DBG("pSourceRect = (%u..%u)x(%u..%u)\n",
666 pSourceRect->left, pSourceRect->right,
667 pSourceRect->top, pSourceRect->bottom);
668 if (pDestRect)
669 DBG("pDestRect = (%u..%u)x(%u..%u)\n",
670 pDestRect->left, pDestRect->right,
671 pDestRect->top, pDestRect->bottom);
672
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
680 */
681 /* Note: This->buffers[0]->level should always be 0 */
682
683 if (This->rendering_done)
684 goto bypass_rendering;
685
686 resource = This->buffers[0]->base.resource;
687
688 if (This->params.SwapEffect == D3DSWAPEFFECT_DISCARD)
689 handle_draw_cursor_and_hud(This, resource);
690
691 if (This->present_buffers) {
692 memset(&blit, 0, sizeof(blit));
693 blit.src.resource = resource;
694 blit.src.level = 0;
695 blit.src.format = resource->format;
696 blit.src.box.z = 0;
697 blit.src.box.depth = 1;
698 blit.src.box.x = 0;
699 blit.src.box.y = 0;
700 blit.src.box.width = resource->width0;
701 blit.src.box.height = resource->height0;
702
703 resource = This->present_buffers[0];
704
705 blit.dst.resource = resource;
706 blit.dst.level = 0;
707 blit.dst.format = resource->format;
708 blit.dst.box.z = 0;
709 blit.dst.box.depth = 1;
710 blit.dst.box.x = 0;
711 blit.dst.box.y = 0;
712 blit.dst.box.width = resource->width0;
713 blit.dst.box.height = resource->height0;
714
715 blit.mask = PIPE_MASK_RGBA;
716 blit.filter = PIPE_TEX_FILTER_NEAREST;
717 blit.scissor_enable = FALSE;
718 blit.alpha_blend = FALSE;
719
720 This->pipe->blit(This->pipe, &blit);
721 }
722
723 /* The resource we present has to resolve fast clears
724 * if needed (and other things) */
725 This->pipe->flush_resource(This->pipe, resource);
726
727 if (This->params.SwapEffect != D3DSWAPEFFECT_DISCARD)
728 handle_draw_cursor_and_hud(This, resource);
729
730 fence = NULL;
731 This->pipe->flush(This->pipe, &fence, PIPE_FLUSH_END_OF_FRAME);
732 if (fence) {
733 swap_fences_push_back(This, fence);
734 This->screen->fence_reference(This->screen, &fence, NULL);
735 }
736
737 This->rendering_done = TRUE;
738 bypass_rendering:
739
740 if (dwFlags & D3DPRESENT_DONOTWAIT) {
741 UNTESTED(2);
742 BOOL still_draw = FALSE;
743 fence = swap_fences_see_front(This);
744 if (fence) {
745 still_draw = !This->screen->fence_finish(This->screen, NULL, fence, 0);
746 This->screen->fence_reference(This->screen, &fence, NULL);
747 }
748 if (still_draw)
749 return D3DERR_WASSTILLDRAWING;
750 }
751
752 if (!This->enable_threadpool) {
753 This->tasks[0]=NULL;
754 fence = swap_fences_pop_front(This);
755 if (fence) {
756 (void) This->screen->fence_finish(This->screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
757 This->screen->fence_reference(This->screen, &fence, NULL);
758 }
759
760 hr = ID3DPresent_PresentBuffer(This->present, This->present_handles[0], hDestWindowOverride, pSourceRect, pDestRect, pDirtyRegion, dwFlags);
761
762 if (FAILED(hr)) { UNTESTED(3);return hr; }
763 } else {
764 pend_present(This, hDestWindowOverride);
765 }
766 This->rendering_done = FALSE;
767
768 return D3D_OK;
769 }
770
771 HRESULT NINE_WINAPI
772 NineSwapChain9_Present( struct NineSwapChain9 *This,
773 const RECT *pSourceRect,
774 const RECT *pDestRect,
775 HWND hDestWindowOverride,
776 const RGNDATA *pDirtyRegion,
777 DWORD dwFlags )
778 {
779 struct pipe_resource *res = NULL;
780 D3DWindowBuffer *handle_temp;
781 struct threadpool_task *task_temp;
782 int i;
783 HRESULT hr;
784
785 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p "
786 "pDirtyRegion=%p dwFlags=%d\n",
787 This, pSourceRect, pDestRect, hDestWindowOverride,
788 pDirtyRegion,dwFlags);
789
790 if (This->base.device->ex) {
791 if (NineSwapChain9_GetOccluded(This)) {
792 return S_PRESENT_OCCLUDED;
793 }
794 } else {
795 if (NineSwapChain9_GetOccluded(This) ||
796 NineSwapChain9_ResolutionMismatch(This)) {
797 This->base.device->device_needs_reset = TRUE;
798 }
799 if (This->base.device->device_needs_reset) {
800 return D3DERR_DEVICELOST;
801 }
802 }
803
804 hr = present(This, pSourceRect, pDestRect,
805 hDestWindowOverride, pDirtyRegion, dwFlags);
806 if (hr == D3DERR_WASSTILLDRAWING)
807 return hr;
808
809 switch (This->params.SwapEffect) {
810 case D3DSWAPEFFECT_FLIP:
811 UNTESTED(4);
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);
818 }
819 NineSurface9_SetResourceResize(
820 This->buffers[This->params.BackBufferCount], res);
821 pipe_resource_reference(&res, NULL);
822
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);
829 }
830
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];
834 }
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];
839 }
840 This->tasks[This->params.BackBufferCount] = task_temp;
841 break;
842
843 case D3DSWAPEFFECT_COPY:
844 UNTESTED(5);
845 /* do nothing */
846 break;
847
848 case D3DSWAPEFFECT_OVERLAY:
849 /* XXX not implemented */
850 break;
851
852 case D3DSWAPEFFECT_FLIPEX:
853 /* XXX not implemented */
854 break;
855 }
856
857 if (This->tasks[0])
858 _mesa_threadpool_wait_for_task(This->pool, &(This->tasks[0]));
859
860 ID3DPresent_WaitBufferReleased(This->present, This->present_handles[0]);
861
862 This->base.device->state.changed.group |= NINE_STATE_FB;
863
864 return hr;
865 }
866
867 HRESULT NINE_WINAPI
868 NineSwapChain9_GetFrontBufferData( struct NineSwapChain9 *This,
869 IDirect3DSurface9 *pDestSurface )
870 {
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;
878 HRESULT hr;
879
880 DBG("GetFrontBufferData: This=%p pDestSurface=%p\n",
881 This, pDestSurface);
882
883 user_assert(dest_surface->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
884
885 width = dest_surface->desc.Width;
886 height = dest_surface->desc.Height;
887
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);
892
893 desc.Type = D3DRTYPE_SURFACE;
894 desc.Pool = D3DPOOL_DEFAULT;
895 desc.MultiSampleType = D3DMULTISAMPLE_NONE;
896 desc.MultiSampleQuality = 0;
897 desc.Width = width;
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);
905 if (FAILED(hr)) {
906 DBG("Failed to create temp FrontBuffer surface.\n");
907 return hr;
908 }
909
910 ID3DPresent_FrontBufferCopy(This->present, temp_handle);
911
912 NineSurface9_CopyDefaultToMem(dest_surface, temp_surface);
913
914 ID3DPresent_DestroyD3DWindowBuffer(This->present, temp_handle);
915 NineUnknown_Destroy(NineUnknown(temp_surface));
916
917 return D3D_OK;
918 }
919
920 HRESULT NINE_WINAPI
921 NineSwapChain9_GetBackBuffer( struct NineSwapChain9 *This,
922 UINT iBackBuffer,
923 D3DBACKBUFFER_TYPE Type,
924 IDirect3DSurface9 **ppBackBuffer )
925 {
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);
932
933 NineUnknown_AddRef(NineUnknown(This->buffers[iBackBuffer]));
934 *ppBackBuffer = (IDirect3DSurface9 *)This->buffers[iBackBuffer];
935 return D3D_OK;
936 }
937
938 HRESULT NINE_WINAPI
939 NineSwapChain9_GetRasterStatus( struct NineSwapChain9 *This,
940 D3DRASTER_STATUS *pRasterStatus )
941 {
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);
946 }
947
948 HRESULT NINE_WINAPI
949 NineSwapChain9_GetDisplayMode( struct NineSwapChain9 *This,
950 D3DDISPLAYMODE *pMode )
951 {
952 D3DDISPLAYMODEEX mode;
953 D3DDISPLAYROTATION rot;
954 HRESULT hr;
955
956 DBG("GetDisplayMode: This=%p pMode=%p\n",
957 This, pMode);
958 user_assert(pMode != NULL, E_POINTER);
959
960 hr = ID3DPresent_GetDisplayMode(This->present, &mode, &rot);
961 if (SUCCEEDED(hr)) {
962 pMode->Width = mode.Width;
963 pMode->Height = mode.Height;
964 pMode->RefreshRate = mode.RefreshRate;
965 pMode->Format = mode.Format;
966 }
967 return hr;
968 }
969
970 HRESULT NINE_WINAPI
971 NineSwapChain9_GetPresentParameters( struct NineSwapChain9 *This,
972 D3DPRESENT_PARAMETERS *pPresentationParameters )
973 {
974 DBG("GetPresentParameters: This=%p pPresentationParameters=%p\n",
975 This, pPresentationParameters);
976 user_assert(pPresentationParameters != NULL, E_POINTER);
977 *pPresentationParameters = This->params;
978 return D3D_OK;
979 }
980
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
992 };
993
994 static const GUID *NineSwapChain9_IIDs[] = {
995 &IID_IDirect3DSwapChain9,
996 &IID_IUnknown,
997 NULL
998 };
999
1000 HRESULT
1001 NineSwapChain9_new( struct NineDevice9 *pDevice,
1002 BOOL implicit,
1003 ID3DPresent *pPresent,
1004 D3DPRESENT_PARAMETERS *pPresentationParameters,
1005 struct d3dadapter9_context *pCTX,
1006 HWND hFocusWindow,
1007 struct NineSwapChain9 **ppOut )
1008 {
1009 NINE_DEVICE_CHILD_NEW(SwapChain9, ppOut, pDevice, /* args */
1010 implicit, pPresent, pPresentationParameters,
1011 pCTX, hFocusWindow, NULL);
1012 }
1013
1014 BOOL
1015 NineSwapChain9_GetOccluded( struct NineSwapChain9 *This )
1016 {
1017 if (This->base.device->minor_version_num > 0) {
1018 return ID3DPresent_GetWindowOccluded(This->present);
1019 }
1020
1021 return FALSE;
1022 }
1023
1024 BOOL
1025 NineSwapChain9_ResolutionMismatch( struct NineSwapChain9 *This )
1026 {
1027 if (This->base.device->minor_version_num > 1) {
1028 return ID3DPresent_ResolutionMismatch(This->present);
1029 }
1030
1031 return FALSE;
1032 }
1033
1034 HANDLE
1035 NineSwapChain9_CreateThread( struct NineSwapChain9 *This,
1036 void *pFuncAddress,
1037 void *pParam )
1038 {
1039 if (This->base.device->minor_version_num > 1) {
1040 return ID3DPresent_CreateThread(This->present, pFuncAddress, pParam);
1041 }
1042
1043 return NULL;
1044 }
1045
1046 void
1047 NineSwapChain9_WaitForThread( struct NineSwapChain9 *This,
1048 HANDLE thread )
1049 {
1050 if (This->base.device->minor_version_num > 1) {
1051 (void) ID3DPresent_WaitForThread(This->present, thread);
1052 }
1053 }