memdraw.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #pragma src "/sys/src/libmemdraw"
  10. #pragma lib "libmemdraw.a"
  11. typedef struct Memimage Memimage;
  12. typedef struct Memdata Memdata;
  13. typedef struct Memsubfont Memsubfont;
  14. typedef struct Memlayer Memlayer;
  15. typedef struct Memcmap Memcmap;
  16. typedef struct Memdrawparam Memdrawparam;
  17. #pragma incomplete Memlayer
  18. /*
  19. * Memdata is allocated from main pool, but .data from the image pool.
  20. * Memdata is allocated separately to permit patching its pointer after
  21. * compaction when windows share the image data.
  22. * The first word of data is a back pointer to the Memdata, to find
  23. * The word to patch.
  24. */
  25. struct Memdata
  26. {
  27. uint32_t *base; /* allocated data pointer */
  28. uint8_t *bdata; /* pointer to first byte of actual data; word-aligned */
  29. int ref; /* number of Memimages using this data */
  30. void* imref;
  31. int allocd; /* is this malloc'd? */
  32. };
  33. enum {
  34. Frepl = 1<<0, /* is replicated */
  35. Fsimple = 1<<1, /* is 1x1 */
  36. Fgrey = 1<<2, /* is grey */
  37. Falpha = 1<<3, /* has explicit alpha */
  38. Fcmap = 1<<4, /* has cmap channel */
  39. Fbytes = 1<<5, /* has only 8-bit channels */
  40. };
  41. struct Memimage
  42. {
  43. Rectangle r; /* rectangle in data area, local coords */
  44. Rectangle clipr; /* clipping region */
  45. int depth; /* number of bits of storage per pixel */
  46. int nchan; /* number of channels */
  47. uint32_t chan; /* channel descriptions */
  48. Memcmap *cmap;
  49. Memdata *data; /* pointer to data; shared by windows in this image */
  50. int zero; /* data->bdata+zero==&byte containing (0,0) */
  51. uint32_t width; /* width in words of a single scan line */
  52. Memlayer *layer; /* nil if not a layer*/
  53. uint32_t flags;
  54. int shift[NChan];
  55. int mask[NChan];
  56. int nbits[NChan];
  57. };
  58. struct Memcmap
  59. {
  60. uint8_t cmap2rgb[3*256];
  61. uint8_t rgb2cmap[16*16*16];
  62. };
  63. /*
  64. * Subfonts
  65. *
  66. * given char c, Subfont *f, Fontchar *i, and Point p, one says
  67. * i = f->info+c;
  68. * draw(b, Rect(p.x+i->left, p.y+i->top,
  69. * p.x+i->left+((i+1)->x-i->x), p.y+i->bottom),
  70. * color, f->bits, Pt(i->x, i->top));
  71. * p.x += i->width;
  72. * to draw characters in the specified color (itself a Memimage) in Memimage b.
  73. */
  74. struct Memsubfont
  75. {
  76. char *name;
  77. int16_t n; /* number of chars in font */
  78. uint8_t height; /* height of bitmap */
  79. char ascent; /* top of bitmap to baseline */
  80. Fontchar *info; /* n+1 character descriptors */
  81. Memimage *bits; /* of font */
  82. };
  83. /*
  84. * Encapsulated parameters and information for sub-draw routines.
  85. */
  86. enum {
  87. Simplesrc=1<<0,
  88. Simplemask=1<<1,
  89. Replsrc=1<<2,
  90. Replmask=1<<3,
  91. Fullmask=1<<4,
  92. };
  93. struct Memdrawparam
  94. {
  95. Memimage *dst;
  96. Rectangle r;
  97. Memimage *src;
  98. Rectangle sr;
  99. Memimage *mask;
  100. Rectangle mr;
  101. int op;
  102. uint32_t state;
  103. uint32_t mval; /* if Simplemask, the mask pixel in mask format */
  104. uint32_t mrgba; /* mval in rgba */
  105. uint32_t sval; /* if Simplesrc, the source pixel in src format */
  106. uint32_t srgba; /* sval in rgba */
  107. uint32_t sdval; /* sval in dst format */
  108. };
  109. /*
  110. * Memimage management
  111. */
  112. extern Memimage* allocmemimage(Rectangle, uint32_t);
  113. extern Memimage* allocmemimaged(Rectangle, uint32_t, Memdata*);
  114. extern Memimage* readmemimage(int);
  115. extern Memimage* creadmemimage(int);
  116. extern int writememimage(int, Memimage*);
  117. extern void freememimage(Memimage*);
  118. extern int loadmemimage(Memimage*, Rectangle, uint8_t*, int);
  119. extern int cloadmemimage(Memimage*, Rectangle, uint8_t*, int);
  120. extern int unloadmemimage(Memimage*, Rectangle, uint8_t*, int);
  121. extern uint32_t* wordaddr(Memimage*, Point);
  122. extern uint8_t* byteaddr(Memimage*, Point);
  123. extern int drawclip(Memimage*, Rectangle*, Memimage*, Point*, Memimage*, Point*, Rectangle*, Rectangle*);
  124. extern void memfillcolor(Memimage*, uint32_t);
  125. extern int memsetchan(Memimage*, uint32_t);
  126. /*
  127. * Graphics
  128. */
  129. extern void memdraw(Memimage*, Rectangle, Memimage*, Point, Memimage*, Point, int);
  130. extern void memline(Memimage*, Point, Point, int, int, int, Memimage*, Point, int);
  131. extern void mempoly(Memimage*, Point*, int, int, int, int, Memimage*, Point, int);
  132. extern void memfillpoly(Memimage*, Point*, int, int, Memimage*, Point, int);
  133. extern void _memfillpolysc(Memimage*, Point*, int, int, Memimage*, Point, int, int, int, int);
  134. extern void memimagedraw(Memimage*, Rectangle, Memimage*, Point, Memimage*, Point, int);
  135. extern int hwdraw(Memdrawparam*);
  136. extern void memimageline(Memimage*, Point, Point, int, int, int, Memimage*, Point, int);
  137. extern void _memimageline(Memimage*, Point, Point, int, int, int, Memimage*, Point, Rectangle, int);
  138. extern Point memimagestring(Memimage*, Point, Memimage*, Point, Memsubfont*,
  139. char*);
  140. extern void memellipse(Memimage*, Point, int, int, int, Memimage*, Point, int);
  141. extern void memarc(Memimage*, Point, int, int, int, Memimage*, Point, int, int, int);
  142. extern Rectangle memlinebbox(Point, Point, int, int, int);
  143. extern int memlineendsize(int);
  144. extern void _memmkcmap(void);
  145. extern void memimageinit(void);
  146. /*
  147. * Subfont management
  148. */
  149. extern Memsubfont* allocmemsubfont(char*, int, int, int,
  150. Fontchar*, Memimage*);
  151. extern Memsubfont* openmemsubfont(char*);
  152. extern void freememsubfont(Memsubfont*);
  153. extern Point memsubfontwidth(Memsubfont*, char*);
  154. extern Memsubfont* getmemdefont(void);
  155. /*
  156. * Predefined
  157. */
  158. extern Memimage* memwhite;
  159. extern Memimage* memblack;
  160. extern Memimage* memopaque;
  161. extern Memimage* memtransparent;
  162. extern Memcmap *memdefcmap;
  163. /*
  164. * Kernel interface
  165. */
  166. void memimagemove(void*, void*);
  167. /*
  168. * Kernel cruft
  169. */
  170. extern void rdb(void);
  171. extern int iprint(char*, ...);
  172. #pragma varargck argpos iprint 1
  173. extern int drawdebug;
  174. /*
  175. * doprint interface: numbconv bit strings
  176. */
  177. #pragma varargck type "llb" vlong
  178. #pragma varargck type "llb" uvlong
  179. #pragma varargck type "lb" long
  180. #pragma varargck type "lb" ulong
  181. #pragma varargck type "b" int
  182. #pragma varargck type "b" uint