lalloc.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <memdraw.h>
  13. #include <memlayer.h>
  14. Memimage*
  15. memlalloc(Memscreen *s, Rectangle screenr, Refreshfn refreshfn, void *refreshptr,
  16. uint32_t val)
  17. {
  18. Memlayer *l;
  19. Memimage *n;
  20. static Memimage *paint;
  21. if(paint == nil){
  22. paint = allocmemimage(Rect(0,0,1,1), RGBA32);
  23. if(paint == nil)
  24. return nil;
  25. paint->flags |= Frepl;
  26. paint->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
  27. }
  28. n = allocmemimaged(screenr, s->image->chan, s->image->data);
  29. if(n == nil)
  30. return nil;
  31. l = malloc(sizeof(Memlayer));
  32. if(l == nil){
  33. free(n);
  34. return nil;
  35. }
  36. l->screen = s;
  37. if(refreshfn)
  38. l->save = nil;
  39. else{
  40. l->save = allocmemimage(screenr, s->image->chan);
  41. if(l->save == nil){
  42. free(l);
  43. free(n);
  44. return nil;
  45. }
  46. /* allocmemimage doesn't initialize memory; this paints save area */
  47. if(val != DNofill)
  48. memfillcolor(l->save, val);
  49. }
  50. l->refreshfn = refreshfn;
  51. l->refreshptr = nil; /* don't set it until we're done */
  52. l->screenr = screenr;
  53. l->delta = Pt(0,0);
  54. n->data->ref++;
  55. n->zero = s->image->zero;
  56. n->width = s->image->width;
  57. n->layer = l;
  58. /* start with new window behind all existing ones */
  59. l->front = s->rearmost;
  60. l->rear = nil;
  61. if(s->rearmost)
  62. s->rearmost->layer->rear = n;
  63. s->rearmost = n;
  64. if(s->frontmost == nil)
  65. s->frontmost = n;
  66. l->clear = 0;
  67. /* now pull new window to front */
  68. _memltofrontfill(n, val != DNofill);
  69. l->refreshptr = refreshptr;
  70. /*
  71. * paint with requested color; previously exposed areas are already right
  72. * if this window has backing store, but just painting the whole thing is simplest.
  73. */
  74. if(val != DNofill){
  75. memsetchan(paint, n->chan);
  76. memfillcolor(paint, val);
  77. memdraw(n, n->r, paint, n->r.min, nil, n->r.min, S);
  78. }
  79. return n;
  80. }