lalloc.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <memdraw.h>
  5. #include <memlayer.h>
  6. Memimage*
  7. memlalloc(Memscreen *s, Rectangle screenr, Refreshfn refreshfn, void *refreshptr, ulong val)
  8. {
  9. Memlayer *l;
  10. Memimage *n;
  11. static Memimage *paint;
  12. if(paint == nil){
  13. paint = allocmemimage(Rect(0,0,1,1), RGBA32);
  14. if(paint == nil)
  15. return nil;
  16. paint->flags |= Frepl;
  17. paint->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
  18. }
  19. n = allocmemimaged(screenr, s->image->chan, s->image->data);
  20. if(n == nil)
  21. return nil;
  22. l = malloc(sizeof(Memlayer));
  23. if(l == nil){
  24. free(n);
  25. return nil;
  26. }
  27. l->screen = s;
  28. if(refreshfn)
  29. l->save = nil;
  30. else{
  31. l->save = allocmemimage(screenr, s->image->chan);
  32. if(l->save == nil){
  33. free(l);
  34. free(n);
  35. return nil;
  36. }
  37. /* allocmemimage doesn't initialize memory; this paints save area */
  38. if(val != DNofill)
  39. memfillcolor(l->save, val);
  40. }
  41. l->refreshfn = refreshfn;
  42. l->refreshptr = nil; /* don't set it until we're done */
  43. l->screenr = screenr;
  44. l->delta = Pt(0,0);
  45. n->data->ref++;
  46. n->zero = s->image->zero;
  47. n->width = s->image->width;
  48. n->layer = l;
  49. /* start with new window behind all existing ones */
  50. l->front = s->rearmost;
  51. l->rear = nil;
  52. if(s->rearmost)
  53. s->rearmost->layer->rear = n;
  54. s->rearmost = n;
  55. if(s->frontmost == nil)
  56. s->frontmost = n;
  57. l->clear = 0;
  58. /* now pull new window to front */
  59. memltofront(n);
  60. l->refreshptr = refreshptr;
  61. /*
  62. * paint with requested color; previously exposed areas are already right
  63. * if this window has backing store, but just painting the whole thing is simplest.
  64. */
  65. if(val != DNofill){
  66. memsetchan(paint, n->chan);
  67. memfillcolor(paint, val);
  68. memdraw(n, n->r, paint, n->r.min, nil, n->r.min, S);
  69. }
  70. return n;
  71. }