0007-Speed-up-console-framebuffer-imageblit-function.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. From 91fca9dd33742a38061dc9e949e4e88b9d5f645b Mon Sep 17 00:00:00 2001
  2. From: Harm Hanemaaijer <fgenfb@yahoo.com>
  3. Date: Thu, 20 Jun 2013 20:21:39 +0200
  4. Subject: [PATCH 007/114] Speed up console framebuffer imageblit function
  5. Especially on platforms with a slower CPU but a relatively high
  6. framebuffer fill bandwidth, like current ARM devices, the existing
  7. console monochrome imageblit function used to draw console text is
  8. suboptimal for common pixel depths such as 16bpp and 32bpp. The existing
  9. code is quite general and can deal with several pixel depths. By creating
  10. special case functions for 16bpp and 32bpp, by far the most common pixel
  11. formats used on modern systems, a significant speed-up is attained
  12. which can be readily felt on ARM-based devices like the Raspberry Pi
  13. and the Allwinner platform, but should help any platform using the
  14. fb layer.
  15. The special case functions allow constant folding, eliminating a number
  16. of instructions including divide operations, and allow the use of an
  17. unrolled loop, eliminating instructions with a variable shift size,
  18. reducing source memory access instructions, and eliminating excessive
  19. branching. These unrolled loops also allow much better code optimization
  20. by the C compiler. The code that selects which optimized variant is used
  21. is also simplified, eliminating integer divide instructions.
  22. The speed-up, measured by timing 'cat file.txt' in the console, varies
  23. between 40% and 70%, when testing on the Raspberry Pi and Allwinner
  24. ARM-based platforms, depending on font size and the pixel depth, with
  25. the greater benefit for 32bpp.
  26. Signed-off-by: Harm Hanemaaijer <fgenfb@yahoo.com>
  27. ---
  28. drivers/video/fbdev/core/cfbimgblt.c | 152 +++++++++++++++++++++++++++++++++--
  29. 1 file changed, 147 insertions(+), 5 deletions(-)
  30. --- a/drivers/video/fbdev/core/cfbimgblt.c
  31. +++ b/drivers/video/fbdev/core/cfbimgblt.c
  32. @@ -28,6 +28,11 @@
  33. *
  34. * Also need to add code to deal with cards endians that are different than
  35. * the native cpu endians. I also need to deal with MSB position in the word.
  36. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  37. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  38. + * significantly faster than the previous implementation.
  39. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  40. + * divides.
  41. */
  42. #include <linux/module.h>
  43. #include <linux/string.h>
  44. @@ -262,6 +267,133 @@ static inline void fast_imageblit(const
  45. }
  46. }
  47. +/*
  48. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  49. + * into the code, main loop unrolled.
  50. + */
  51. +
  52. +static inline void fast_imageblit16(const struct fb_image *image,
  53. + struct fb_info *p, u8 __iomem * dst1,
  54. + u32 fgcolor, u32 bgcolor)
  55. +{
  56. + u32 fgx = fgcolor, bgx = bgcolor;
  57. + u32 spitch = (image->width + 7) / 8;
  58. + u32 end_mask, eorx;
  59. + const char *s = image->data, *src;
  60. + u32 __iomem *dst;
  61. + const u32 *tab = NULL;
  62. + int i, j, k;
  63. +
  64. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  65. +
  66. + fgx <<= 16;
  67. + bgx <<= 16;
  68. + fgx |= fgcolor;
  69. + bgx |= bgcolor;
  70. +
  71. + eorx = fgx ^ bgx;
  72. + k = image->width / 2;
  73. +
  74. + for (i = image->height; i--;) {
  75. + dst = (u32 __iomem *) dst1;
  76. + src = s;
  77. +
  78. + j = k;
  79. + while (j >= 4) {
  80. + u8 bits = *src;
  81. + end_mask = tab[(bits >> 6) & 3];
  82. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  83. + end_mask = tab[(bits >> 4) & 3];
  84. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  85. + end_mask = tab[(bits >> 2) & 3];
  86. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  87. + end_mask = tab[bits & 3];
  88. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  89. + src++;
  90. + j -= 4;
  91. + }
  92. + if (j != 0) {
  93. + u8 bits = *src;
  94. + end_mask = tab[(bits >> 6) & 3];
  95. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  96. + if (j >= 2) {
  97. + end_mask = tab[(bits >> 4) & 3];
  98. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  99. + if (j == 3) {
  100. + end_mask = tab[(bits >> 2) & 3];
  101. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  102. + }
  103. + }
  104. + }
  105. + dst1 += p->fix.line_length;
  106. + s += spitch;
  107. + }
  108. +}
  109. +
  110. +/*
  111. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  112. + * into the code, main loop unrolled.
  113. + */
  114. +
  115. +static inline void fast_imageblit32(const struct fb_image *image,
  116. + struct fb_info *p, u8 __iomem * dst1,
  117. + u32 fgcolor, u32 bgcolor)
  118. +{
  119. + u32 fgx = fgcolor, bgx = bgcolor;
  120. + u32 spitch = (image->width + 7) / 8;
  121. + u32 end_mask, eorx;
  122. + const char *s = image->data, *src;
  123. + u32 __iomem *dst;
  124. + const u32 *tab = NULL;
  125. + int i, j, k;
  126. +
  127. + tab = cfb_tab32;
  128. +
  129. + eorx = fgx ^ bgx;
  130. + k = image->width;
  131. +
  132. + for (i = image->height; i--;) {
  133. + dst = (u32 __iomem *) dst1;
  134. + src = s;
  135. +
  136. + j = k;
  137. + while (j >= 8) {
  138. + u8 bits = *src;
  139. + end_mask = tab[(bits >> 7) & 1];
  140. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  141. + end_mask = tab[(bits >> 6) & 1];
  142. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  143. + end_mask = tab[(bits >> 5) & 1];
  144. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  145. + end_mask = tab[(bits >> 4) & 1];
  146. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  147. + end_mask = tab[(bits >> 3) & 1];
  148. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  149. + end_mask = tab[(bits >> 2) & 1];
  150. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  151. + end_mask = tab[(bits >> 1) & 1];
  152. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  153. + end_mask = tab[bits & 1];
  154. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  155. + src++;
  156. + j -= 8;
  157. + }
  158. + if (j != 0) {
  159. + u32 bits = (u32) * src;
  160. + while (j > 1) {
  161. + end_mask = tab[(bits >> 7) & 1];
  162. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  163. + bits <<= 1;
  164. + j--;
  165. + }
  166. + end_mask = tab[(bits >> 7) & 1];
  167. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  168. + }
  169. + dst1 += p->fix.line_length;
  170. + s += spitch;
  171. + }
  172. +}
  173. +
  174. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  175. {
  176. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  177. @@ -294,11 +426,21 @@ void cfb_imageblit(struct fb_info *p, co
  178. bgcolor = image->bg_color;
  179. }
  180. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  181. - ((width & (32/bpp-1)) == 0) &&
  182. - bpp >= 8 && bpp <= 32)
  183. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  184. - else
  185. + if (!start_index && !pitch_index) {
  186. + if (bpp == 32)
  187. + fast_imageblit32(image, p, dst1, fgcolor,
  188. + bgcolor);
  189. + else if (bpp == 16 && (width & 1) == 0)
  190. + fast_imageblit16(image, p, dst1, fgcolor,
  191. + bgcolor);
  192. + else if (bpp == 8 && (width & 3) == 0)
  193. + fast_imageblit(image, p, dst1, fgcolor,
  194. + bgcolor);
  195. + else
  196. + slow_imageblit(image, p, dst1, fgcolor,
  197. + bgcolor,
  198. + start_index, pitch_index);
  199. + } else
  200. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  201. start_index, pitch_index);
  202. } else