layerop.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <memdraw.h>
  5. #include <memlayer.h>
  6. #define RECUR(a,b,c,d) _layerop(fn, i, Rect(a.x, b.y, c.x, d.y), clipr, etc, front->layer->rear);
  7. static void
  8. _layerop(
  9. void (*fn)(Memimage*, Rectangle, Rectangle, void*, int),
  10. Memimage *i,
  11. Rectangle r,
  12. Rectangle clipr,
  13. void *etc,
  14. Memimage *front)
  15. {
  16. Rectangle fr;
  17. Top:
  18. if(front == i){
  19. /* no one is in front of this part of window; use the screen */
  20. fn(i->layer->screen->image, r, clipr, etc, 0);
  21. return;
  22. }
  23. fr = front->layer->screenr;
  24. if(rectXrect(r, fr) == 0){
  25. /* r doesn't touch this window; continue on next rearmost */
  26. // assert(front && front->layer && front->layer->screen && front->layer->rear);
  27. front = front->layer->rear;
  28. goto Top;
  29. }
  30. if(fr.max.y < r.max.y){
  31. RECUR(r.min, fr.max, r.max, r.max);
  32. r.max.y = fr.max.y;
  33. }
  34. if(r.min.y < fr.min.y){
  35. RECUR(r.min, r.min, r.max, fr.min);
  36. r.min.y = fr.min.y;
  37. }
  38. if(fr.max.x < r.max.x){
  39. RECUR(fr.max, r.min, r.max, r.max);
  40. r.max.x = fr.max.x;
  41. }
  42. if(r.min.x < fr.min.x){
  43. RECUR(r.min, r.min, fr.min, r.max);
  44. r.min.x = fr.min.x;
  45. }
  46. /* r is covered by front, so put in save area */
  47. (*fn)(i->layer->save, r, clipr, etc, 1);
  48. }
  49. /*
  50. * Assumes incoming rectangle has already been clipped to i's logical r and clipr
  51. */
  52. void
  53. _memlayerop(
  54. void (*fn)(Memimage*, Rectangle, Rectangle, void*, int),
  55. Memimage *i,
  56. Rectangle screenr, /* clipped to window boundaries */
  57. Rectangle clipr, /* clipped also to clipping rectangles of hierarchy */
  58. void *etc)
  59. {
  60. Memlayer *l;
  61. Rectangle r, scr;
  62. l = i->layer;
  63. if(!rectclip(&screenr, l->screenr))
  64. return;
  65. if(l->clear){
  66. fn(l->screen->image, screenr, clipr, etc, 0);
  67. return;
  68. }
  69. r = screenr;
  70. scr = l->screen->image->clipr;
  71. /*
  72. * Do the piece on the screen
  73. */
  74. if(rectclip(&screenr, scr))
  75. _layerop(fn, i, screenr, clipr, etc, l->screen->frontmost);
  76. if(rectinrect(r, scr))
  77. return;
  78. /*
  79. * Do the piece off the screen
  80. */
  81. if(!rectXrect(r, scr)){
  82. /* completely offscreen; easy */
  83. fn(l->save, r, clipr, etc, 1);
  84. return;
  85. }
  86. if(r.min.y < scr.min.y){
  87. /* above screen */
  88. fn(l->save, Rect(r.min.x, r.min.y, r.max.x, scr.min.y), clipr, etc, 1);
  89. r.min.y = scr.min.y;
  90. }
  91. if(r.max.y > scr.max.y){
  92. /* below screen */
  93. fn(l->save, Rect(r.min.x, scr.max.y, r.max.x, r.max.y), clipr, etc, 1);
  94. r.max.y = scr.max.y;
  95. }
  96. if(r.min.x < scr.min.x){
  97. /* left of screen */
  98. fn(l->save, Rect(r.min.x, r.min.y, scr.min.x, r.max.y), clipr, etc, 1);
  99. r.min.x = scr.min.x;
  100. }
  101. if(r.max.x > scr.max.x){
  102. /* right of screen */
  103. fn(l->save, Rect(scr.max.x, r.min.y, r.max.x, r.max.y), clipr, etc, 1);
  104. }
  105. }