rasops15.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* $OpenBSD: rasops15.c,v 1.8 2010/08/28 12:48:14 miod Exp $ */
  2. /* $NetBSD: rasops15.c,v 1.7 2000/04/12 14:22:29 pk Exp $ */
  3. /*-
  4. * Copyright (c) 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Andrew Doran.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/time.h>
  34. #include <dev/wscons/wsdisplayvar.h>
  35. #include <dev/wscons/wsconsio.h>
  36. #include <dev/rasops/rasops.h>
  37. int rasops15_putchar(void *, int, int, u_int, long attr);
  38. #ifndef RASOPS_SMALL
  39. int rasops15_putchar8(void *, int, int, u_int, long attr);
  40. int rasops15_putchar12(void *, int, int, u_int, long attr);
  41. int rasops15_putchar16(void *, int, int, u_int, long attr);
  42. void rasops15_makestamp(struct rasops_info *, long);
  43. /*
  44. * (2x2)x1 stamp for optimized character blitting
  45. */
  46. static int32_t stamp[32];
  47. static long stamp_attr;
  48. static int stamp_mutex; /* XXX see note in readme */
  49. #endif
  50. /*
  51. * XXX this confuses the hell out of gcc2 (not egcs) which always insists
  52. * that the shift count is negative.
  53. *
  54. * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
  55. * destination int32_t[0] = STAMP_READ(offset)
  56. * destination int32_t[1] = STAMP_READ(offset + 4)
  57. */
  58. #define STAMP_SHIFT(fb,n) ((n*4-3) >= 0 ? (fb)>>(n*4-3):(fb)<<-(n*4-3))
  59. #define STAMP_MASK (15 << 3)
  60. #define STAMP_READ(o) (*(int32_t *)((caddr_t)stamp + (o)))
  61. /*
  62. * Initialize rasops_info struct for this colordepth.
  63. */
  64. void
  65. rasops15_init(struct rasops_info *ri)
  66. {
  67. switch (ri->ri_font->fontwidth) {
  68. #ifndef RASOPS_SMALL
  69. case 8:
  70. ri->ri_ops.putchar = rasops15_putchar8;
  71. break;
  72. case 12:
  73. ri->ri_ops.putchar = rasops15_putchar12;
  74. break;
  75. case 16:
  76. ri->ri_ops.putchar = rasops15_putchar16;
  77. break;
  78. #endif /* !RASOPS_SMALL */
  79. default:
  80. ri->ri_ops.putchar = rasops15_putchar;
  81. break;
  82. }
  83. if (ri->ri_rnum == 0) {
  84. ri->ri_rnum = 5;
  85. ri->ri_rpos = 0;
  86. ri->ri_gnum = 5 + (ri->ri_depth == 16);
  87. ri->ri_gpos = 5;
  88. ri->ri_bnum = 5;
  89. ri->ri_bpos = 10 + (ri->ri_depth == 16);
  90. }
  91. }
  92. /*
  93. * Paint a single character.
  94. */
  95. int
  96. rasops15_putchar(void *cookie, int row, int col, u_int uc, long attr)
  97. {
  98. int fb, width, height, cnt, clr[2];
  99. struct rasops_info *ri;
  100. u_char *dp, *rp, *fr;
  101. ri = (struct rasops_info *)cookie;
  102. #ifdef RASOPS_CLIPPING
  103. /* Catches 'row < 0' case too */
  104. if ((unsigned)row >= (unsigned)ri->ri_rows)
  105. return 0;
  106. if ((unsigned)col >= (unsigned)ri->ri_cols)
  107. return 0;
  108. #endif
  109. rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
  110. height = ri->ri_font->fontheight;
  111. width = ri->ri_font->fontwidth;
  112. clr[1] = ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
  113. clr[0] = ri->ri_devcmap[((u_int)attr >> 16) & 0xf];
  114. if (uc == ' ') {
  115. int16_t c = (int16_t)clr[0];
  116. while (height--) {
  117. dp = rp;
  118. rp += ri->ri_stride;
  119. for (cnt = width; cnt; cnt--) {
  120. *(int16_t *)dp = c;
  121. dp += 2;
  122. }
  123. }
  124. } else {
  125. uc -= ri->ri_font->firstchar;
  126. fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
  127. while (height--) {
  128. dp = rp;
  129. fb = fr[3] | (fr[2] << 8) | (fr[1] << 16) | (fr[0] << 24);
  130. fr += ri->ri_font->stride;
  131. rp += ri->ri_stride;
  132. for (cnt = width; cnt; cnt--) {
  133. *(int16_t *)dp = (int16_t)clr[(fb >> 31) & 1];
  134. fb <<= 1;
  135. dp += 2;
  136. }
  137. }
  138. }
  139. /* Do underline */
  140. if ((attr & 1) != 0) {
  141. int16_t c = (int16_t)clr[1];
  142. rp -= ri->ri_stride << 1;
  143. while (width--) {
  144. *(int16_t *)rp = c;
  145. rp += 2;
  146. }
  147. }
  148. return 0;
  149. }
  150. #ifndef RASOPS_SMALL
  151. /*
  152. * Recompute the (2x2)x1 blitting stamp.
  153. */
  154. void
  155. rasops15_makestamp(struct rasops_info *ri, long attr)
  156. {
  157. int32_t fg, bg;
  158. int i;
  159. fg = ri->ri_devcmap[((u_int)attr >> 24) & 0xf] & 0xffff;
  160. bg = ri->ri_devcmap[((u_int)attr >> 16) & 0xf] & 0xffff;
  161. stamp_attr = attr;
  162. for (i = 0; i < 32; i += 2) {
  163. #if BYTE_ORDER == LITTLE_ENDIAN
  164. stamp[i] = (i & 16 ? fg : bg);
  165. stamp[i] |= ((i & 8 ? fg : bg) << 16);
  166. stamp[i + 1] = (i & 4 ? fg : bg);
  167. stamp[i + 1] |= ((i & 2 ? fg : bg) << 16);
  168. #else
  169. stamp[i + 1] = (i & 2 ? fg : bg);
  170. stamp[i + 1] |= ((i & 4 ? fg : bg) << 16);
  171. stamp[i] = (i & 8 ? fg : bg);
  172. stamp[i] |= ((i & 16 ? fg : bg) << 16);
  173. #endif
  174. }
  175. }
  176. /*
  177. * Paint a single character. This is for 8-pixel wide fonts.
  178. */
  179. int
  180. rasops15_putchar8(void *cookie, int row, int col, u_int uc, long attr)
  181. {
  182. struct rasops_info *ri;
  183. int height, so, fs;
  184. int32_t *rp;
  185. u_char *fr;
  186. /* Can't risk remaking the stamp if it's already in use */
  187. if (stamp_mutex++) {
  188. stamp_mutex--;
  189. return rasops15_putchar(cookie, row, col, uc, attr);
  190. }
  191. ri = (struct rasops_info *)cookie;
  192. #ifdef RASOPS_CLIPPING
  193. if ((unsigned)row >= (unsigned)ri->ri_rows) {
  194. stamp_mutex--;
  195. return 0;
  196. }
  197. if ((unsigned)col >= (unsigned)ri->ri_cols) {
  198. stamp_mutex--;
  199. return 0;
  200. }
  201. #endif
  202. /* Recompute stamp? */
  203. if (attr != stamp_attr)
  204. rasops15_makestamp(ri, attr);
  205. rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
  206. height = ri->ri_font->fontheight;
  207. if (uc == (u_int)-1) {
  208. int32_t c = stamp[0];
  209. while (height--) {
  210. rp[0] = rp[1] = rp[2] = rp[3] = c;
  211. DELTA(rp, ri->ri_stride, int32_t *);
  212. }
  213. } else {
  214. uc -= ri->ri_font->firstchar;
  215. fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
  216. fs = ri->ri_font->stride;
  217. while (height--) {
  218. so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
  219. rp[0] = STAMP_READ(so);
  220. rp[1] = STAMP_READ(so + 4);
  221. so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
  222. rp[2] = STAMP_READ(so);
  223. rp[3] = STAMP_READ(so + 4);
  224. fr += fs;
  225. DELTA(rp, ri->ri_stride, int32_t *);
  226. }
  227. }
  228. /* Do underline */
  229. if ((attr & 1) != 0) {
  230. int32_t c = STAMP_READ(28);
  231. DELTA(rp, -(ri->ri_stride << 1), int32_t *);
  232. rp[0] = rp[1] = rp[2] = rp[3] = c;
  233. }
  234. stamp_mutex--;
  235. return 0;
  236. }
  237. /*
  238. * Paint a single character. This is for 12-pixel wide fonts.
  239. */
  240. int
  241. rasops15_putchar12(void *cookie, int row, int col, u_int uc, long attr)
  242. {
  243. struct rasops_info *ri;
  244. int height, so, fs;
  245. int32_t *rp;
  246. u_char *fr;
  247. /* Can't risk remaking the stamp if it's already in use */
  248. if (stamp_mutex++) {
  249. stamp_mutex--;
  250. return rasops15_putchar(cookie, row, col, uc, attr);
  251. }
  252. ri = (struct rasops_info *)cookie;
  253. #ifdef RASOPS_CLIPPING
  254. if ((unsigned)row >= (unsigned)ri->ri_rows) {
  255. stamp_mutex--;
  256. return 0;
  257. }
  258. if ((unsigned)col >= (unsigned)ri->ri_cols) {
  259. stamp_mutex--;
  260. return 0;
  261. }
  262. #endif
  263. /* Recompute stamp? */
  264. if (attr != stamp_attr)
  265. rasops15_makestamp(ri, attr);
  266. rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
  267. height = ri->ri_font->fontheight;
  268. if (uc == (u_int)-1) {
  269. int32_t c = stamp[0];
  270. while (height--) {
  271. rp[0] = rp[1] = rp[2] = rp[3] = rp[4] = rp[5] = c;
  272. DELTA(rp, ri->ri_stride, int32_t *);
  273. }
  274. } else {
  275. uc -= ri->ri_font->firstchar;
  276. fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
  277. fs = ri->ri_font->stride;
  278. while (height--) {
  279. so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
  280. rp[0] = STAMP_READ(so);
  281. rp[1] = STAMP_READ(so + 4);
  282. so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
  283. rp[2] = STAMP_READ(so);
  284. rp[3] = STAMP_READ(so + 4);
  285. so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
  286. rp[4] = STAMP_READ(so);
  287. rp[5] = STAMP_READ(so + 4);
  288. fr += fs;
  289. DELTA(rp, ri->ri_stride, int32_t *);
  290. }
  291. }
  292. /* Do underline */
  293. if (attr & 1) {
  294. int32_t c = STAMP_READ(28);
  295. DELTA(rp, -(ri->ri_stride << 1), int32_t *);
  296. rp[0] = rp[1] = rp[2] = rp[3] = rp[4] = rp[5] = c;
  297. }
  298. stamp_mutex--;
  299. return 0;
  300. }
  301. /*
  302. * Paint a single character. This is for 16-pixel wide fonts.
  303. */
  304. int
  305. rasops15_putchar16(void *cookie, int row, int col, u_int uc, long attr)
  306. {
  307. struct rasops_info *ri;
  308. int height, so, fs;
  309. int32_t *rp;
  310. u_char *fr;
  311. /* Can't risk remaking the stamp if it's already in use */
  312. if (stamp_mutex++) {
  313. stamp_mutex--;
  314. return rasops15_putchar(cookie, row, col, uc, attr);
  315. }
  316. ri = (struct rasops_info *)cookie;
  317. #ifdef RASOPS_CLIPPING
  318. if ((unsigned)row >= (unsigned)ri->ri_rows) {
  319. stamp_mutex--;
  320. return 0;
  321. }
  322. if ((unsigned)col >= (unsigned)ri->ri_cols) {
  323. stamp_mutex--;
  324. return 0;
  325. }
  326. #endif
  327. /* Recompute stamp? */
  328. if (attr != stamp_attr)
  329. rasops15_makestamp(ri, attr);
  330. rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
  331. height = ri->ri_font->fontheight;
  332. if (uc == (u_int)-1) {
  333. int32_t c = stamp[0];
  334. while (height--) {
  335. rp[0] = rp[1] = rp[2] = rp[3] =
  336. rp[4] = rp[5] = rp[6] = rp[7] = c;
  337. DELTA(rp, ri->ri_stride, int32_t *);
  338. }
  339. } else {
  340. uc -= ri->ri_font->firstchar;
  341. fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
  342. fs = ri->ri_font->stride;
  343. while (height--) {
  344. so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
  345. rp[0] = STAMP_READ(so);
  346. rp[1] = STAMP_READ(so + 4);
  347. so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
  348. rp[2] = STAMP_READ(so);
  349. rp[3] = STAMP_READ(so + 4);
  350. so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
  351. rp[4] = STAMP_READ(so);
  352. rp[5] = STAMP_READ(so + 4);
  353. so = STAMP_SHIFT(fr[1], 0) & STAMP_MASK;
  354. rp[6] = STAMP_READ(so);
  355. rp[7] = STAMP_READ(so + 4);
  356. DELTA(rp, ri->ri_stride, int32_t *);
  357. fr += fs;
  358. }
  359. }
  360. /* Do underline */
  361. if (attr & 1) {
  362. int32_t c = STAMP_READ(28);
  363. DELTA(rp, -(ri->ri_stride << 1), int32_t *);
  364. rp[0] = rp[1] = rp[2] = rp[3] =
  365. rp[4] = rp[5] = rp[6] = rp[7] = c;
  366. }
  367. stamp_mutex--;
  368. return 0;
  369. }
  370. #endif /* !RASOPS_SMALL */