sys_gfx.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 0x50000000 /* 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(procs[current_pid].pid))
  78. return -1; /* one session per process */
  79. t = wm_term_for_screen(procs[current_pid].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(procs[current_pid].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(procs[current_pid].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 = procs[current_pid].pid;
  121. s->cr3 = procs[current_pid].cr3;
  122. s->term = t;
  123. s->key_head = s->key_tail = 0;
  124. info->width = w;
  125. info->height = h;
  126. info->fb = (void *)GFX_USER_BASE;
  127. return 0;
  128. }
  129. int sys_gfx_present(void)
  130. {
  131. struct gfx_session *s = by_pid(procs[current_pid].pid);
  132. const unsigned short *src; /* the caller's own view of its canvas */
  133. unsigned int n, i;
  134. if (!s)
  135. return -1;
  136. n = s->c.w * s->c.h;
  137. /* The syscall runs on the caller's page directory, so read the canvas
  138. * through the caller's mapping: that is exactly the memory the command
  139. * drew into and presented. */
  140. src = (const unsigned short *)GFX_USER_BASE;
  141. /* Publish: copy the finished frame into the composited front buffer.
  142. * The syscall runs with interrupts off, so the WM can never observe (or
  143. * blit) a half-copied frame. */
  144. for (i = 0; i + 1 < n; i += 2)
  145. ((unsigned int *)s->c.px)[i / 2] = ((const unsigned int *)src)[i / 2];
  146. if (n & 1)
  147. s->c.px[n - 1] = src[n - 1];
  148. s->c.seq++;
  149. return 0;
  150. }
  151. int sys_gfx_end(void)
  152. {
  153. struct gfx_session *s = by_pid(procs[current_pid].pid);
  154. if (!s)
  155. return -1;
  156. unmap_all(s);
  157. gfx_free(&s->c);
  158. free_back(s);
  159. s->used = 0;
  160. wm_request_redraw(); /* the window falls back to its text cells */
  161. return 0;
  162. }
  163. int sys_gfx_key_poll(void)
  164. {
  165. struct gfx_session *s = by_pid(procs[current_pid].pid);
  166. int k;
  167. if (!s || s->key_tail == s->key_head)
  168. return 0;
  169. k = s->keys[s->key_tail];
  170. s->key_tail = (s->key_tail + 1) % GFX_KEY_RING;
  171. return k;
  172. }
  173. /* WM: the canvas to composite for a window's screen, or NULL. */
  174. struct gfx_canvas *sys_gfx_canvas_for(struct text_screen *screen)
  175. {
  176. struct gfx_session *s = by_screen(screen);
  177. return s ? &s->c : 0;
  178. }
  179. /* WM: hand a key to the focused window's gfx command. 1 = consumed. */
  180. int sys_gfx_push_key(struct text_screen *screen, int k)
  181. {
  182. struct gfx_session *s = by_screen(screen);
  183. unsigned int next;
  184. if (!s)
  185. return 0;
  186. next = (s->key_head + 1) % GFX_KEY_RING;
  187. if (next != s->key_tail) {
  188. s->keys[s->key_head] = k;
  189. s->key_head = next;
  190. }
  191. return 1; /* consumed even when the ring is full (drop, don't leak) */
  192. }
  193. /* WM: the window is closing; stop its gfx command and free its canvas. */
  194. void sys_gfx_kill_for_screen(struct text_screen *screen)
  195. {
  196. struct gfx_session *s = by_screen(screen);
  197. if (!s)
  198. return;
  199. process_kill(s->pid);
  200. s->dead = 1; /* freed by the next sys_gfx_collect() */
  201. }
  202. /* Reaper hook (schedule(), IRQ context): only flag the session. */
  203. void sys_gfx_reap(unsigned int pid)
  204. {
  205. unsigned int i;
  206. for (i = 0; i < GFX_MAX_SESSIONS; i++)
  207. if (sessions[i].used && !sessions[i].dead && sessions[i].pid == pid)
  208. sessions[i].dead = 1;
  209. }
  210. /* WM loop: free dead sessions away from the renderer (the reaper can run
  211. * while the WM is mid-blit, so freeing there would be unsafe). */
  212. void sys_gfx_collect(void)
  213. {
  214. unsigned int i;
  215. int freed = 0;
  216. for (i = 0; i < GFX_MAX_SESSIONS; i++) {
  217. if (sessions[i].used && sessions[i].dead) {
  218. gfx_free(&sessions[i].c);
  219. free_back(&sessions[i]);
  220. sessions[i].used = 0;
  221. freed = 1;
  222. }
  223. }
  224. if (freed)
  225. wm_request_redraw();
  226. }