sys_gfx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Window graphics sessions: one double-buffered canvas per window, mapped
  2. * into the owning ring-3 process. The process draws into the mapped back
  3. * buffer; SYS_GFX_PRESENT copies it into the front buffer, which is the only
  4. * thing the WM ever composites - so a half-drawn frame can never reach the
  5. * screen (the copy runs with interrupts off). Sessions are keyed by the
  6. * owner's pid and by the terminal screen (the screen pointer is what a
  7. * spawned command inherits, so it identifies the window without any new
  8. * plumbing in process.c). */
  9. #include "sys_gfx.h"
  10. #include "mm.h"
  11. #include "paging.h"
  12. #include "process.h"
  13. #include "wm.h"
  14. #define GFX_MAX_SESSIONS MAX_WINDOWS
  15. #define GFX_USER_BASE USER_GFX_BASE /* canvas mapping for the owner */
  16. #define GFX_KEY_RING 16
  17. struct gfx_session {
  18. int used;
  19. int dead; /* owner gone: free on the next WM collect */
  20. unsigned int pid;
  21. unsigned int cr3; /* owner's page directory (to unmap on END) */
  22. struct term *term;
  23. struct gfx_canvas c; /* front buffer: what the WM composites */
  24. unsigned short *back; /* back buffer: mapped to the owner */
  25. unsigned int back_pages;
  26. int keys[GFX_KEY_RING];
  27. unsigned int key_head, key_tail;
  28. };
  29. static struct gfx_session sessions[GFX_MAX_SESSIONS];
  30. static inline void invlpg_va(unsigned int va)
  31. {
  32. __asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
  33. }
  34. static struct gfx_session *by_pid(unsigned int pid)
  35. {
  36. unsigned int i;
  37. for (i = 0; i < GFX_MAX_SESSIONS; i++)
  38. if (sessions[i].used && !sessions[i].dead && sessions[i].pid == pid)
  39. return &sessions[i];
  40. return 0;
  41. }
  42. static struct gfx_session *by_screen(struct text_screen *s)
  43. {
  44. unsigned int i;
  45. for (i = 0; i < GFX_MAX_SESSIONS; i++)
  46. if (sessions[i].used && !sessions[i].dead &&
  47. &sessions[i].term->screen == s)
  48. return &sessions[i];
  49. return 0;
  50. }
  51. static void free_back(struct gfx_session *s)
  52. {
  53. unsigned int i;
  54. if (!s->back)
  55. return;
  56. for (i = 0; i < s->back_pages; i++)
  57. mm_free_page((unsigned int)s->back + i * PAGE_SIZE);
  58. s->back = 0;
  59. s->back_pages = 0;
  60. }
  61. static void unmap_all(struct gfx_session *s)
  62. {
  63. unsigned int i;
  64. if (!s->cr3 || !s->back)
  65. return;
  66. for (i = 0; i < s->back_pages; i++)
  67. paging_unmap_page(s->cr3, GFX_USER_BASE + i * PAGE_SIZE);
  68. s->cr3 = 0;
  69. }
  70. int sys_gfx_begin(struct sys_gfx_info *info)
  71. {
  72. struct gfx_session *s = 0;
  73. struct term *t;
  74. unsigned int w, h, i, pages;
  75. if (!info)
  76. return -1;
  77. if (by_pid(current_proc()->pid))
  78. return -1; /* one session per process */
  79. t = wm_term_for_screen(current_proc()->screen);
  80. if (!t)
  81. return -1; /* no window (not spawned by a terminal) */
  82. if (by_screen(&t->screen))
  83. return -1; /* one gfx command per window */
  84. for (i = 0; i < GFX_MAX_SESSIONS; i++)
  85. if (!sessions[i].used) {
  86. s = &sessions[i];
  87. break;
  88. }
  89. if (!s)
  90. return -1;
  91. if (wm_content_size(t, &w, &h) != 0)
  92. return -1;
  93. if (gfx_alloc(&s->c, w, h) != 0)
  94. return -1;
  95. pages = (w * h * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
  96. s->back = (unsigned short *)mm_alloc_pages(pages);
  97. if (!s->back) {
  98. gfx_free(&s->c);
  99. return -1;
  100. }
  101. s->back_pages = pages;
  102. for (i = 0; i < w * h; i++) /* fresh memory is not zeroed */
  103. s->back[i] = 0;
  104. for (i = 0; i < pages; i++) {
  105. if (!paging_map_page(current_proc()->cr3,
  106. GFX_USER_BASE + i * PAGE_SIZE,
  107. (unsigned int)s->back + i * PAGE_SIZE,
  108. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  109. while (i--)
  110. paging_unmap_page(current_proc()->cr3,
  111. GFX_USER_BASE + i * PAGE_SIZE);
  112. free_back(s);
  113. gfx_free(&s->c);
  114. return -1;
  115. }
  116. invlpg_va(GFX_USER_BASE + i * PAGE_SIZE);
  117. }
  118. s->used = 1;
  119. s->dead = 0;
  120. s->pid = current_proc()->pid;
  121. s->cr3 = current_proc()->cr3;
  122. s->term = t;
  123. s->key_head = s->key_tail = 0;
  124. wm_content_changed(&t->screen); /* the WM switches to the canvas */
  125. info->width = w;
  126. info->height = h;
  127. info->fb = (void *)GFX_USER_BASE;
  128. return 0;
  129. }
  130. int sys_gfx_present(void)
  131. {
  132. struct gfx_session *s = by_pid(current_proc()->pid);
  133. const unsigned short *src; /* the caller's own view of its canvas */
  134. unsigned int n, i;
  135. if (!s)
  136. return -1;
  137. n = s->c.w * s->c.h;
  138. /* The syscall runs on the caller's page directory, so read the canvas
  139. * through the caller's mapping: that is exactly the memory the command
  140. * drew into and presented. */
  141. src = (const unsigned short *)GFX_USER_BASE;
  142. /* Publish: copy the finished frame into the composited front buffer.
  143. * The syscall runs with interrupts off, so the WM can never observe (or
  144. * blit) a half-copied frame. */
  145. for (i = 0; i + 1 < n; i += 2)
  146. ((unsigned int *)s->c.px)[i / 2] = ((const unsigned int *)src)[i / 2];
  147. if (n & 1)
  148. s->c.px[n - 1] = src[n - 1];
  149. s->c.seq++;
  150. return 0;
  151. }
  152. int sys_gfx_end(void)
  153. {
  154. struct gfx_session *s = by_pid(current_proc()->pid);
  155. if (!s)
  156. return -1;
  157. unmap_all(s);
  158. /* The front canvas is still mapped into the WM. Mark the session dead
  159. * and let sys_gfx_collect() unmap it from the WM before freeing it. */
  160. s->dead = 1;
  161. wm_content_changed(&s->term->screen);
  162. return 0;
  163. }
  164. int sys_gfx_key_poll(void)
  165. {
  166. struct gfx_session *s = by_pid(current_proc()->pid);
  167. int k;
  168. if (!s)
  169. return wm_server_key_poll(); /* generic canvas window */
  170. if (s->key_tail == s->key_head)
  171. return 0;
  172. k = s->keys[s->key_tail];
  173. s->key_tail = (s->key_tail + 1) % GFX_KEY_RING;
  174. return k;
  175. }
  176. /* WM: the canvas to composite for a window's screen, or NULL. */
  177. struct gfx_canvas *sys_gfx_canvas_for(struct text_screen *screen)
  178. {
  179. struct gfx_session *s = by_screen(screen);
  180. return s ? &s->c : 0;
  181. }
  182. /* WM: hand a key to the focused window's gfx command. 1 = consumed. */
  183. int sys_gfx_push_key(struct text_screen *screen, int k)
  184. {
  185. struct gfx_session *s = by_screen(screen);
  186. unsigned int next;
  187. if (!s)
  188. return 0;
  189. next = (s->key_head + 1) % GFX_KEY_RING;
  190. if (next != s->key_tail) {
  191. s->keys[s->key_head] = k;
  192. s->key_head = next;
  193. }
  194. return 1; /* consumed even when the ring is full (drop, don't leak) */
  195. }
  196. /* WM: the window is closing; stop its gfx command and free its canvas. */
  197. void sys_gfx_kill_for_screen(struct text_screen *screen)
  198. {
  199. struct gfx_session *s = by_screen(screen);
  200. if (!s)
  201. return;
  202. process_kill(s->pid);
  203. s->dead = 1; /* freed by the next sys_gfx_collect() */
  204. wm_content_changed(screen);
  205. }
  206. /* Reaper hook (schedule(), IRQ context): only flag the session. */
  207. void sys_gfx_reap(unsigned int pid)
  208. {
  209. unsigned int i;
  210. for (i = 0; i < GFX_MAX_SESSIONS; i++)
  211. if (sessions[i].used && !sessions[i].dead && sessions[i].pid == pid)
  212. sessions[i].dead = 1;
  213. }
  214. /* WM loop: free dead sessions away from the renderer (the reaper can run
  215. * while the WM is mid-blit, so freeing there would be unsafe). */
  216. void sys_gfx_collect(void)
  217. {
  218. unsigned int i;
  219. int freed = 0;
  220. /* The WM may still be composing the front canvas we are about to free.
  221. * It calls back after PRESENT; the idle loop also retries. */
  222. if (wm_server_frame_active())
  223. return;
  224. for (i = 0; i < GFX_MAX_SESSIONS; i++) {
  225. if (sessions[i].used && sessions[i].dead) {
  226. struct text_screen *screen = &sessions[i].term->screen;
  227. /* Drop the WM's read-only mapping before the canvas pages
  228. * can be reused. Only this session's canvas: a newer
  229. * session may already be mapped for the same screen. */
  230. wm_server_unmap_canvas(screen, sessions[i].c.px);
  231. gfx_free(&sessions[i].c);
  232. free_back(&sessions[i]);
  233. sessions[i].used = 0;
  234. wm_content_changed(screen);
  235. freed = 1;
  236. }
  237. }
  238. if (freed)
  239. wm_request_redraw();
  240. }