present.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * Copyright © 2013 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #ifdef HAVE_XORG_CONFIG_H
  23. #include <xorg-config.h>
  24. #endif
  25. #include "present_priv.h"
  26. #include <gcstruct.h>
  27. #include <misync.h>
  28. #include <misyncstr.h>
  29. #ifdef MONOTONIC_CLOCK
  30. #include <time.h>
  31. #endif
  32. static uint64_t present_event_id;
  33. static struct xorg_list present_exec_queue;
  34. static struct xorg_list present_flip_queue;
  35. #if 0
  36. #define DebugPresent(x) ErrorF x
  37. #else
  38. #define DebugPresent(x)
  39. #endif
  40. static void
  41. present_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);
  42. /*
  43. * Copies the update region from a pixmap to the target drawable
  44. */
  45. static void
  46. present_copy_region(DrawablePtr drawable,
  47. PixmapPtr pixmap,
  48. RegionPtr update,
  49. int16_t x_off,
  50. int16_t y_off)
  51. {
  52. ScreenPtr screen = drawable->pScreen;
  53. GCPtr gc;
  54. gc = GetScratchGC(drawable->depth, screen);
  55. if (update) {
  56. ChangeGCVal changes[2];
  57. changes[0].val = x_off;
  58. changes[1].val = y_off;
  59. ChangeGC(serverClient, gc,
  60. GCClipXOrigin|GCClipYOrigin,
  61. changes);
  62. (*gc->funcs->ChangeClip)(gc, CT_REGION, update, 0);
  63. }
  64. ValidateGC(drawable, gc);
  65. (*gc->ops->CopyArea)(&pixmap->drawable,
  66. drawable,
  67. gc,
  68. 0, 0,
  69. pixmap->drawable.width, pixmap->drawable.height,
  70. x_off, y_off);
  71. if (update)
  72. (*gc->funcs->ChangeClip)(gc, CT_NONE, NULL, 0);
  73. FreeScratchGC(gc);
  74. }
  75. static inline PixmapPtr
  76. present_flip_pending_pixmap(ScreenPtr screen)
  77. {
  78. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  79. if (!screen_priv)
  80. return NULL;
  81. if (!screen_priv->flip_pending)
  82. return NULL;
  83. return screen_priv->flip_pending->pixmap;
  84. }
  85. static Bool
  86. present_check_flip(RRCrtcPtr crtc,
  87. WindowPtr window,
  88. PixmapPtr pixmap,
  89. Bool sync_flip,
  90. RegionPtr valid,
  91. int16_t x_off,
  92. int16_t y_off)
  93. {
  94. ScreenPtr screen = window->drawable.pScreen;
  95. PixmapPtr window_pixmap;
  96. WindowPtr root = screen->root;
  97. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  98. if (!screen_priv)
  99. return FALSE;
  100. if (!screen_priv->info)
  101. return FALSE;
  102. if (!crtc)
  103. return FALSE;
  104. /* Check to see if the driver supports flips at all */
  105. if (!screen_priv->info->flip)
  106. return FALSE;
  107. /* Make sure the window hasn't been redirected with Composite */
  108. window_pixmap = screen->GetWindowPixmap(window);
  109. if (window_pixmap != screen->GetScreenPixmap(screen) &&
  110. window_pixmap != screen_priv->flip_pixmap &&
  111. window_pixmap != present_flip_pending_pixmap(screen))
  112. return FALSE;
  113. /* Check for full-screen window */
  114. if (!RegionEqual(&window->clipList, &root->winSize)) {
  115. return FALSE;
  116. }
  117. /* Source pixmap must align with window exactly */
  118. if (x_off || y_off) {
  119. return FALSE;
  120. }
  121. /* Make sure the area marked as valid fills the screen */
  122. if (valid && !RegionEqual(valid, &root->winSize)) {
  123. return FALSE;
  124. }
  125. /* Does the window match the pixmap exactly? */
  126. if (window->drawable.x != 0 || window->drawable.y != 0 ||
  127. #ifdef COMPOSITE
  128. window->drawable.x != pixmap->screen_x || window->drawable.y != pixmap->screen_y ||
  129. #endif
  130. window->drawable.width != pixmap->drawable.width ||
  131. window->drawable.height != pixmap->drawable.height) {
  132. return FALSE;
  133. }
  134. /* Ask the driver for permission */
  135. if (screen_priv->info->check_flip) {
  136. if (!(*screen_priv->info->check_flip) (crtc, window, pixmap, sync_flip)) {
  137. DebugPresent(("\td %08lx -> %08lx\n", window->drawable.id, pixmap ? pixmap->drawable.id : 0));
  138. return FALSE;
  139. }
  140. }
  141. return TRUE;
  142. }
  143. static Bool
  144. present_flip(RRCrtcPtr crtc,
  145. uint64_t event_id,
  146. uint64_t target_msc,
  147. PixmapPtr pixmap,
  148. Bool sync_flip)
  149. {
  150. ScreenPtr screen = crtc->pScreen;
  151. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  152. return (*screen_priv->info->flip) (crtc, event_id, target_msc, pixmap, sync_flip);
  153. }
  154. static void
  155. present_vblank_notify(present_vblank_ptr vblank, CARD8 kind, CARD8 mode, uint64_t ust, uint64_t crtc_msc)
  156. {
  157. int n;
  158. if (vblank->window)
  159. present_send_complete_notify(vblank->window, kind, mode, vblank->serial, ust, crtc_msc - vblank->msc_offset);
  160. for (n = 0; n < vblank->num_notifies; n++) {
  161. WindowPtr window = vblank->notifies[n].window;
  162. CARD32 serial = vblank->notifies[n].serial;
  163. if (window)
  164. present_send_complete_notify(window, kind, mode, serial, ust, crtc_msc - vblank->msc_offset);
  165. }
  166. }
  167. static void
  168. present_pixmap_idle(PixmapPtr pixmap, WindowPtr window, CARD32 serial, struct present_fence *present_fence)
  169. {
  170. if (present_fence)
  171. present_fence_set_triggered(present_fence);
  172. if (window) {
  173. DebugPresent(("\ti %08lx\n", pixmap ? pixmap->drawable.id : 0));
  174. present_send_idle_notify(window, serial, pixmap, present_fence);
  175. }
  176. }
  177. RRCrtcPtr
  178. present_get_crtc(WindowPtr window)
  179. {
  180. ScreenPtr screen = window->drawable.pScreen;
  181. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  182. if (!screen_priv)
  183. return NULL;
  184. if (!screen_priv->info)
  185. return NULL;
  186. return (*screen_priv->info->get_crtc)(window);
  187. }
  188. uint32_t
  189. present_query_capabilities(RRCrtcPtr crtc)
  190. {
  191. present_screen_priv_ptr screen_priv;
  192. if (!crtc)
  193. return 0;
  194. screen_priv = present_screen_priv(crtc->pScreen);
  195. if (!screen_priv)
  196. return 0;
  197. if (!screen_priv->info)
  198. return 0;
  199. return screen_priv->info->capabilities;
  200. }
  201. static int
  202. present_get_ust_msc(ScreenPtr screen, RRCrtcPtr crtc, uint64_t *ust, uint64_t *msc)
  203. {
  204. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  205. if (crtc == NULL)
  206. return present_fake_get_ust_msc(screen, ust, msc);
  207. else
  208. return (*screen_priv->info->get_ust_msc)(crtc, ust, msc);
  209. }
  210. static void
  211. present_flush(WindowPtr window)
  212. {
  213. ScreenPtr screen = window->drawable.pScreen;
  214. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  215. if (!screen_priv)
  216. return;
  217. if (!screen_priv->info)
  218. return;
  219. (*screen_priv->info->flush) (window);
  220. }
  221. static int
  222. present_queue_vblank(ScreenPtr screen,
  223. RRCrtcPtr crtc,
  224. uint64_t event_id,
  225. uint64_t msc)
  226. {
  227. Bool ret;
  228. if (crtc == NULL)
  229. ret = present_fake_queue_vblank(screen, event_id, msc);
  230. else
  231. {
  232. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  233. ret = (*screen_priv->info->queue_vblank) (crtc, event_id, msc);
  234. }
  235. return ret;
  236. }
  237. static uint64_t
  238. present_window_to_crtc_msc(WindowPtr window, RRCrtcPtr crtc, uint64_t window_msc, uint64_t new_msc)
  239. {
  240. present_window_priv_ptr window_priv = present_get_window_priv(window, TRUE);
  241. if (crtc != window_priv->crtc) {
  242. uint64_t old_ust, old_msc;
  243. /* The old CRTC may have been turned off, in which case
  244. * we'll just use whatever previous MSC we'd seen from this CRTC
  245. */
  246. if (present_get_ust_msc(window->drawable.pScreen, window_priv->crtc, &old_ust, &old_msc) != Success)
  247. old_msc = window_priv->msc;
  248. window_priv->msc_offset += new_msc - old_msc;
  249. window_priv->crtc = crtc;
  250. }
  251. return window_msc + window_priv->msc_offset;
  252. }
  253. /*
  254. * When the wait fence or previous flip is completed, it's time
  255. * to re-try the request
  256. */
  257. static void
  258. present_re_execute(present_vblank_ptr vblank)
  259. {
  260. uint64_t ust = 0, crtc_msc = 0;
  261. if (vblank->crtc)
  262. (void) present_get_ust_msc(vblank->screen, vblank->crtc, &ust, &crtc_msc);
  263. present_execute(vblank, ust, crtc_msc);
  264. }
  265. static void
  266. present_flip_try_ready(ScreenPtr screen)
  267. {
  268. present_vblank_ptr vblank, tmp;
  269. xorg_list_for_each_entry_safe(vblank, tmp, &present_exec_queue, event_queue) {
  270. if (vblank->flip_ready) {
  271. present_re_execute(vblank);
  272. return;
  273. }
  274. }
  275. }
  276. static void
  277. present_flip_idle(ScreenPtr screen)
  278. {
  279. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  280. if (screen_priv->flip_pixmap) {
  281. present_pixmap_idle(screen_priv->flip_pixmap, screen_priv->flip_window,
  282. screen_priv->flip_serial, screen_priv->flip_idle_fence);
  283. if (screen_priv->flip_idle_fence)
  284. present_fence_destroy(screen_priv->flip_idle_fence);
  285. dixDestroyPixmap(screen_priv->flip_pixmap, screen_priv->flip_pixmap->drawable.id);
  286. screen_priv->flip_crtc = NULL;
  287. screen_priv->flip_window = NULL;
  288. screen_priv->flip_serial = 0;
  289. screen_priv->flip_pixmap = NULL;
  290. screen_priv->flip_idle_fence = NULL;
  291. }
  292. }
  293. struct pixmap_visit {
  294. PixmapPtr old;
  295. PixmapPtr new;
  296. };
  297. static int
  298. present_set_tree_pixmap_visit(WindowPtr window, void *data)
  299. {
  300. struct pixmap_visit *visit = data;
  301. ScreenPtr screen = window->drawable.pScreen;
  302. if ((*screen->GetWindowPixmap)(window) != visit->old)
  303. return WT_DONTWALKCHILDREN;
  304. (*screen->SetWindowPixmap)(window, visit->new);
  305. return WT_WALKCHILDREN;
  306. }
  307. static void
  308. present_set_tree_pixmap(WindowPtr window, PixmapPtr pixmap)
  309. {
  310. struct pixmap_visit visit;
  311. ScreenPtr screen = window->drawable.pScreen;
  312. visit.old = (*screen->GetWindowPixmap)(window);
  313. visit.new = pixmap;
  314. if (visit.old == visit.new)
  315. return;
  316. TraverseTree(window, present_set_tree_pixmap_visit, &visit);
  317. }
  318. static void
  319. present_set_abort_flip(ScreenPtr screen)
  320. {
  321. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  322. /* Switch back to using the screen pixmap now to avoid
  323. * 2D applications drawing to the wrong pixmap.
  324. */
  325. if (screen_priv->flip_window)
  326. present_set_tree_pixmap(screen_priv->flip_window,
  327. (*screen->GetScreenPixmap)(screen));
  328. present_set_tree_pixmap(screen->root, (*screen->GetScreenPixmap)(screen));
  329. screen_priv->flip_pending->abort_flip = TRUE;
  330. }
  331. static void
  332. present_unflip(ScreenPtr screen)
  333. {
  334. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  335. assert (!screen_priv->unflip_event_id);
  336. assert (!screen_priv->flip_pending);
  337. if (screen_priv->flip_window)
  338. present_set_tree_pixmap(screen_priv->flip_window,
  339. (*screen->GetScreenPixmap)(screen));
  340. present_set_tree_pixmap(screen->root, (*screen->GetScreenPixmap)(screen));
  341. /* Update the screen pixmap with the current flip pixmap contents
  342. */
  343. if (screen_priv->flip_pixmap && screen_priv->flip_window) {
  344. present_copy_region(&screen_priv->flip_window->drawable,
  345. screen_priv->flip_pixmap,
  346. NULL, 0, 0);
  347. }
  348. screen_priv->unflip_event_id = ++present_event_id;
  349. DebugPresent(("u %lld\n", screen_priv->unflip_event_id));
  350. (*screen_priv->info->unflip) (screen, screen_priv->unflip_event_id);
  351. }
  352. static void
  353. present_flip_notify(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
  354. {
  355. ScreenPtr screen = vblank->screen;
  356. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  357. DebugPresent(("\tn %lld %p %8lld: %08lx -> %08lx\n",
  358. vblank->event_id, vblank, vblank->target_msc,
  359. vblank->pixmap ? vblank->pixmap->drawable.id : 0,
  360. vblank->window ? vblank->window->drawable.id : 0));
  361. assert (vblank == screen_priv->flip_pending);
  362. present_flip_idle(screen);
  363. xorg_list_del(&vblank->event_queue);
  364. /* Transfer reference for pixmap and fence from vblank to screen_priv */
  365. screen_priv->flip_crtc = vblank->crtc;
  366. screen_priv->flip_window = vblank->window;
  367. screen_priv->flip_serial = vblank->serial;
  368. screen_priv->flip_pixmap = vblank->pixmap;
  369. screen_priv->flip_idle_fence = vblank->idle_fence;
  370. vblank->pixmap = NULL;
  371. vblank->idle_fence = NULL;
  372. screen_priv->flip_pending = NULL;
  373. if (vblank->abort_flip)
  374. present_unflip(screen);
  375. present_vblank_notify(vblank, PresentCompleteKindPixmap, PresentCompleteModeFlip, ust, crtc_msc);
  376. present_vblank_destroy(vblank);
  377. present_flip_try_ready(screen);
  378. }
  379. void
  380. present_event_notify(uint64_t event_id, uint64_t ust, uint64_t msc)
  381. {
  382. present_vblank_ptr vblank, tmp;
  383. int s;
  384. if (!event_id)
  385. return;
  386. DebugPresent(("\te %lld ust %lld msc %lld\n", event_id, ust, msc));
  387. xorg_list_for_each_entry_safe(vblank, tmp, &present_exec_queue, event_queue) {
  388. if (vblank->event_id == event_id) {
  389. present_execute(vblank, ust, msc);
  390. return;
  391. }
  392. }
  393. xorg_list_for_each_entry_safe(vblank, tmp, &present_flip_queue, event_queue) {
  394. if (vblank->event_id == event_id) {
  395. present_flip_notify(vblank, ust, msc);
  396. return;
  397. }
  398. }
  399. for (s = 0; s < screenInfo.numScreens; s++) {
  400. ScreenPtr screen = screenInfo.screens[s];
  401. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  402. if (event_id == screen_priv->unflip_event_id) {
  403. DebugPresent(("\tun %lld\n", event_id));
  404. screen_priv->unflip_event_id = 0;
  405. present_flip_idle(screen);
  406. present_flip_try_ready(screen);
  407. return;
  408. }
  409. }
  410. }
  411. /*
  412. * 'window' is being reconfigured. Check to see if it is involved
  413. * in flipping and clean up as necessary
  414. */
  415. void
  416. present_check_flip_window (WindowPtr window)
  417. {
  418. ScreenPtr screen = window->drawable.pScreen;
  419. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  420. present_window_priv_ptr window_priv = present_window_priv(window);
  421. present_vblank_ptr flip_pending = screen_priv->flip_pending;
  422. present_vblank_ptr vblank;
  423. /* If this window hasn't ever been used with Present, it can't be
  424. * flipping
  425. */
  426. if (!window_priv)
  427. return;
  428. if (screen_priv->unflip_event_id)
  429. return;
  430. if (flip_pending) {
  431. /*
  432. * Check pending flip
  433. */
  434. if (flip_pending->window == window) {
  435. if (!present_check_flip(flip_pending->crtc, window, flip_pending->pixmap,
  436. flip_pending->sync_flip, NULL, 0, 0))
  437. present_set_abort_flip(screen);
  438. }
  439. } else {
  440. /*
  441. * Check current flip
  442. */
  443. if (window == screen_priv->flip_window) {
  444. if (!present_check_flip(screen_priv->flip_crtc, window, screen_priv->flip_pixmap, FALSE, NULL, 0, 0))
  445. present_unflip(screen);
  446. }
  447. }
  448. /* Now check any queued vblanks */
  449. xorg_list_for_each_entry(vblank, &window_priv->vblank, window_list) {
  450. if (vblank->queued && vblank->flip && !present_check_flip(vblank->crtc, window, vblank->pixmap, FALSE, NULL, 0, 0))
  451. vblank->flip = FALSE;
  452. }
  453. }
  454. /*
  455. * Called when the wait fence is triggered; just gets the current msc/ust and
  456. * calls present_execute again. That will re-check the fence and pend the
  457. * request again if it's still not actually ready
  458. */
  459. static void
  460. present_wait_fence_triggered(void *param)
  461. {
  462. present_vblank_ptr vblank = param;
  463. present_re_execute(vblank);
  464. }
  465. /*
  466. * Once the required MSC has been reached, execute the pending request.
  467. *
  468. * For requests to actually present something, either blt contents to
  469. * the screen or queue a frame buffer swap.
  470. *
  471. * For requests to just get the current MSC/UST combo, skip that part and
  472. * go straight to event delivery
  473. */
  474. static void
  475. present_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
  476. {
  477. WindowPtr window = vblank->window;
  478. ScreenPtr screen = window->drawable.pScreen;
  479. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  480. uint8_t mode;
  481. if (vblank->wait_fence) {
  482. if (!present_fence_check_triggered(vblank->wait_fence)) {
  483. present_fence_set_callback(vblank->wait_fence, present_wait_fence_triggered, vblank);
  484. return;
  485. }
  486. }
  487. if (vblank->flip && vblank->pixmap && vblank->window) {
  488. if (screen_priv->flip_pending || screen_priv->unflip_event_id) {
  489. DebugPresent(("\tr %lld %p (pending %p unflip %lld)\n",
  490. vblank->event_id, vblank,
  491. screen_priv->flip_pending, screen_priv->unflip_event_id));
  492. vblank->flip_ready = TRUE;
  493. return;
  494. }
  495. }
  496. xorg_list_del(&vblank->event_queue);
  497. xorg_list_del(&vblank->window_list);
  498. vblank->queued = FALSE;
  499. if (vblank->pixmap && vblank->window) {
  500. if (vblank->flip) {
  501. DebugPresent(("\tf %lld %p %8lld: %08lx -> %08lx\n",
  502. vblank->event_id, vblank, crtc_msc,
  503. vblank->pixmap->drawable.id, vblank->window->drawable.id));
  504. /* Prepare to flip by placing it in the flip queue and
  505. * and sticking it into the flip_pending field
  506. */
  507. screen_priv->flip_pending = vblank;
  508. xorg_list_add(&vblank->event_queue, &present_flip_queue);
  509. /* Try to flip
  510. */
  511. if (present_flip(vblank->crtc, vblank->event_id, vblank->target_msc, vblank->pixmap, vblank->sync_flip)) {
  512. RegionPtr damage;
  513. /* Fix window pixmaps:
  514. * 1) Restore previous flip window pixmap
  515. * 2) Set current flip window pixmap to the new pixmap
  516. */
  517. if (screen_priv->flip_window && screen_priv->flip_window != window)
  518. present_set_tree_pixmap(screen_priv->flip_window,
  519. (*screen->GetScreenPixmap)(screen));
  520. present_set_tree_pixmap(vblank->window, vblank->pixmap);
  521. present_set_tree_pixmap(screen->root, vblank->pixmap);
  522. /* Report update region as damaged
  523. */
  524. if (vblank->update) {
  525. damage = vblank->update;
  526. RegionIntersect(damage, damage, &window->clipList);
  527. } else
  528. damage = &window->clipList;
  529. DamageDamageRegion(&vblank->window->drawable, damage);
  530. return;
  531. }
  532. xorg_list_del(&vblank->event_queue);
  533. /* Oops, flip failed. Clear the flip_pending field
  534. */
  535. screen_priv->flip_pending = NULL;
  536. vblank->flip = FALSE;
  537. }
  538. DebugPresent(("\tc %p %8lld: %08lx -> %08lx\n", vblank, crtc_msc, vblank->pixmap->drawable.id, vblank->window->drawable.id));
  539. if (screen_priv->flip_pending) {
  540. /* Check pending flip
  541. */
  542. if (window == screen_priv->flip_pending->window)
  543. present_set_abort_flip(screen);
  544. } else if (!screen_priv->unflip_event_id) {
  545. /* Check current flip
  546. */
  547. if (window == screen_priv->flip_window)
  548. present_unflip(screen);
  549. }
  550. present_copy_region(&window->drawable, vblank->pixmap, vblank->update, vblank->x_off, vblank->y_off);
  551. /* present_copy_region sticks the region into a scratch GC,
  552. * which is then freed, freeing the region
  553. */
  554. vblank->update = NULL;
  555. present_flush(window);
  556. present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
  557. }
  558. /* Compute correct CompleteMode
  559. */
  560. if (vblank->kind == PresentCompleteKindPixmap) {
  561. if (vblank->pixmap && vblank->window)
  562. mode = PresentCompleteModeCopy;
  563. else
  564. mode = PresentCompleteModeSkip;
  565. }
  566. else
  567. mode = PresentCompleteModeCopy;
  568. present_vblank_notify(vblank, vblank->kind, mode, ust, crtc_msc);
  569. present_vblank_destroy(vblank);
  570. }
  571. int
  572. present_pixmap(WindowPtr window,
  573. PixmapPtr pixmap,
  574. CARD32 serial,
  575. RegionPtr valid,
  576. RegionPtr update,
  577. int16_t x_off,
  578. int16_t y_off,
  579. RRCrtcPtr target_crtc,
  580. SyncFence *wait_fence,
  581. SyncFence *idle_fence,
  582. uint32_t options,
  583. uint64_t window_msc,
  584. uint64_t divisor,
  585. uint64_t remainder,
  586. present_notify_ptr notifies,
  587. int num_notifies)
  588. {
  589. uint64_t ust;
  590. uint64_t target_msc;
  591. uint64_t crtc_msc;
  592. int ret;
  593. present_vblank_ptr vblank, tmp;
  594. ScreenPtr screen = window->drawable.pScreen;
  595. present_window_priv_ptr window_priv = present_get_window_priv(window, TRUE);
  596. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  597. if (!window_priv)
  598. return BadAlloc;
  599. if (!screen_priv || !screen_priv->info)
  600. target_crtc = NULL;
  601. else if (!target_crtc) {
  602. /* Update the CRTC if we have a pixmap or we don't have a CRTC
  603. */
  604. if (!pixmap)
  605. target_crtc = window_priv->crtc;
  606. if (!target_crtc)
  607. target_crtc = present_get_crtc(window);
  608. }
  609. present_get_ust_msc(screen, target_crtc, &ust, &crtc_msc);
  610. target_msc = present_window_to_crtc_msc(window, target_crtc, window_msc, crtc_msc);
  611. /* Stash the current MSC away in case we need it later
  612. */
  613. window_priv->msc = crtc_msc;
  614. /* Adjust target_msc to match modulus
  615. */
  616. if (crtc_msc >= target_msc) {
  617. if (divisor != 0) {
  618. target_msc = crtc_msc - (crtc_msc % divisor) + remainder;
  619. if (options & PresentOptionAsync) {
  620. if (target_msc < crtc_msc)
  621. target_msc += divisor;
  622. } else {
  623. if (target_msc <= crtc_msc)
  624. target_msc += divisor;
  625. }
  626. } else {
  627. target_msc = crtc_msc;
  628. if (!(options & PresentOptionAsync))
  629. target_msc++;
  630. }
  631. }
  632. /*
  633. * Look for a matching presentation already on the list and
  634. * don't bother doing the previous one if this one will overwrite it
  635. * in the same frame
  636. */
  637. if (!update && pixmap) {
  638. xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->vblank, window_list) {
  639. if (!vblank->pixmap)
  640. continue;
  641. if (!vblank->queued)
  642. continue;
  643. if (vblank->crtc != target_crtc || vblank->target_msc != target_msc)
  644. continue;
  645. DebugPresent(("\tx %lld %p %8lld: %08lx -> %08lx (crtc %p)\n",
  646. vblank->event_id, vblank, vblank->target_msc,
  647. vblank->pixmap->drawable.id, vblank->window->drawable.id,
  648. vblank->crtc));
  649. present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
  650. present_fence_destroy(vblank->idle_fence);
  651. dixDestroyPixmap(vblank->pixmap, vblank->pixmap->drawable.id);
  652. vblank->pixmap = NULL;
  653. vblank->idle_fence = NULL;
  654. vblank->flip = FALSE;
  655. if (vblank->flip_ready)
  656. present_re_execute(vblank);
  657. }
  658. }
  659. vblank = calloc (1, sizeof (present_vblank_rec));
  660. if (!vblank)
  661. return BadAlloc;
  662. xorg_list_append(&vblank->window_list, &window_priv->vblank);
  663. xorg_list_init(&vblank->event_queue);
  664. vblank->screen = screen;
  665. vblank->window = window;
  666. vblank->pixmap = pixmap;
  667. vblank->event_id = ++present_event_id;
  668. if (pixmap) {
  669. vblank->kind = PresentCompleteKindPixmap;
  670. pixmap->refcnt++;
  671. } else
  672. vblank->kind = PresentCompleteKindNotifyMSC;
  673. vblank->serial = serial;
  674. if (valid) {
  675. vblank->valid = RegionDuplicate(valid);
  676. if (!vblank->valid)
  677. goto no_mem;
  678. }
  679. if (update) {
  680. vblank->update = RegionDuplicate(update);
  681. if (!vblank->update)
  682. goto no_mem;
  683. }
  684. vblank->x_off = x_off;
  685. vblank->y_off = y_off;
  686. vblank->target_msc = target_msc;
  687. vblank->crtc = target_crtc;
  688. vblank->msc_offset = window_priv->msc_offset;
  689. vblank->notifies = notifies;
  690. vblank->num_notifies = num_notifies;
  691. if (!(options & PresentOptionAsync))
  692. vblank->sync_flip = TRUE;
  693. if (!(options & PresentOptionCopy) &&
  694. !((options & PresentOptionAsync) &&
  695. (!screen_priv->info ||
  696. !(screen_priv->info->capabilities & PresentCapabilityAsync))) &&
  697. pixmap != NULL &&
  698. present_check_flip (target_crtc, window, pixmap, vblank->sync_flip, valid, x_off, y_off))
  699. {
  700. vblank->flip = TRUE;
  701. if (vblank->sync_flip)
  702. target_msc--;
  703. }
  704. if (wait_fence) {
  705. vblank->wait_fence = present_fence_create(wait_fence);
  706. if (!vblank->wait_fence)
  707. goto no_mem;
  708. }
  709. if (idle_fence) {
  710. vblank->idle_fence = present_fence_create(idle_fence);
  711. if (!vblank->idle_fence)
  712. goto no_mem;
  713. }
  714. if (pixmap)
  715. DebugPresent(("q %lld %p %8lld: %08lx -> %08lx (crtc %p) flip %d vsync %d serial %d\n",
  716. vblank->event_id, vblank, target_msc,
  717. vblank->pixmap->drawable.id, vblank->window->drawable.id,
  718. target_crtc, vblank->flip, vblank->sync_flip, vblank->serial));
  719. xorg_list_add(&vblank->event_queue, &present_exec_queue);
  720. vblank->queued = TRUE;
  721. if ((pixmap && target_msc >= crtc_msc) || (!pixmap && target_msc > crtc_msc)) {
  722. ret = present_queue_vblank(screen, target_crtc, vblank->event_id, target_msc);
  723. if (ret != Success) {
  724. xorg_list_del(&vblank->event_queue);
  725. vblank->queued = FALSE;
  726. goto failure;
  727. }
  728. } else
  729. present_execute(vblank, ust, crtc_msc);
  730. return Success;
  731. no_mem:
  732. ret = BadAlloc;
  733. failure:
  734. vblank->notifies = NULL;
  735. present_vblank_destroy(vblank);
  736. return ret;
  737. }
  738. void
  739. present_abort_vblank(ScreenPtr screen, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc)
  740. {
  741. present_vblank_ptr vblank, tmp;
  742. if (crtc == NULL)
  743. present_fake_abort_vblank(screen, event_id, msc);
  744. else
  745. {
  746. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  747. (*screen_priv->info->abort_vblank) (crtc, event_id, msc);
  748. }
  749. xorg_list_for_each_entry_safe(vblank, tmp, &present_exec_queue, event_queue) {
  750. if (vblank->event_id == event_id) {
  751. xorg_list_del(&vblank->event_queue);
  752. vblank->queued = FALSE;
  753. return;
  754. }
  755. }
  756. xorg_list_for_each_entry_safe(vblank, tmp, &present_flip_queue, event_queue) {
  757. if (vblank->event_id == event_id) {
  758. xorg_list_del(&vblank->event_queue);
  759. return;
  760. }
  761. }
  762. }
  763. int
  764. present_notify_msc(WindowPtr window,
  765. CARD32 serial,
  766. uint64_t target_msc,
  767. uint64_t divisor,
  768. uint64_t remainder)
  769. {
  770. return present_pixmap(window,
  771. NULL,
  772. serial,
  773. NULL, NULL,
  774. 0, 0,
  775. NULL,
  776. NULL, NULL,
  777. PresentOptionAsync,
  778. target_msc, divisor, remainder, NULL, 0);
  779. }
  780. void
  781. present_flip_destroy(ScreenPtr screen)
  782. {
  783. present_screen_priv_ptr screen_priv = present_screen_priv(screen);
  784. /* XXX this needs to be synchronous for server reset */
  785. /* Do the actual cleanup once the flip has been performed by the hardware */
  786. if (screen_priv->flip_pending)
  787. present_set_abort_flip(screen);
  788. }
  789. void
  790. present_vblank_destroy(present_vblank_ptr vblank)
  791. {
  792. /* Remove vblank from window and screen lists */
  793. xorg_list_del(&vblank->window_list);
  794. DebugPresent(("\td %lld %p %8lld: %08lx -> %08lx\n",
  795. vblank->event_id, vblank, vblank->target_msc,
  796. vblank->pixmap ? vblank->pixmap->drawable.id : 0,
  797. vblank->window ? vblank->window->drawable.id : 0));
  798. /* Drop pixmap reference */
  799. if (vblank->pixmap)
  800. dixDestroyPixmap(vblank->pixmap, vblank->pixmap->drawable.id);
  801. /* Free regions */
  802. if (vblank->valid)
  803. RegionDestroy(vblank->valid);
  804. if (vblank->update)
  805. RegionDestroy(vblank->update);
  806. if (vblank->wait_fence)
  807. present_fence_destroy(vblank->wait_fence);
  808. if (vblank->idle_fence)
  809. present_fence_destroy(vblank->idle_fence);
  810. if (vblank->notifies)
  811. present_destroy_notifies(vblank->notifies, vblank->num_notifies);
  812. free(vblank);
  813. }
  814. Bool
  815. present_init(void)
  816. {
  817. xorg_list_init(&present_exec_queue);
  818. xorg_list_init(&present_flip_queue);
  819. present_fake_queue_init();
  820. return TRUE;
  821. }