fbfill.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* SPECIAL NOTES!
  19. Please note following when reading the code below:
  20. - In this driver we assume that every memory can be accessed by same memory
  21. bus. If there are different address spaces do not use this code as a base
  22. code for other archs.
  23. - Every function in this code assumes that bounds checking has been done in
  24. previous phase and they are opted out in here. */
  25. #include <grub/video_fb.h>
  26. #include <grub/fbfill.h>
  27. #include <grub/fbutil.h>
  28. #include <grub/types.h>
  29. #include <grub/video.h>
  30. /* Generic filler that works for every supported mode. */
  31. static void
  32. grub_video_fbfill (struct grub_video_fbblit_info *dst,
  33. grub_video_color_t color, int x, int y,
  34. int width, int height)
  35. {
  36. int i;
  37. int j;
  38. for (j = 0; j < height; j++)
  39. for (i = 0; i < width; i++)
  40. set_pixel (dst, x + i, y + j, color);
  41. }
  42. /* Optimized filler for direct color 32 bit modes. It is assumed that color
  43. is already mapped to destination format. */
  44. static void
  45. grub_video_fbfill_direct32 (struct grub_video_fbblit_info *dst,
  46. grub_video_color_t color, int x, int y,
  47. int width, int height)
  48. {
  49. int i;
  50. int j;
  51. grub_uint32_t *dstptr;
  52. grub_size_t rowskip;
  53. /* Calculate the number of bytes to advance from the end of one line
  54. to the beginning of the next line. */
  55. rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
  56. /* Get the start address. */
  57. dstptr = grub_video_fb_get_video_ptr (dst, x, y);
  58. for (j = 0; j < height; j++)
  59. {
  60. for (i = 0; i < width; i++)
  61. *dstptr++ = color;
  62. /* Advance the dest pointer to the right location on the next line. */
  63. GRUB_VIDEO_FB_ADVANCE_POINTER (dstptr, rowskip);
  64. }
  65. }
  66. /* Optimized filler for direct color 24 bit modes. It is assumed that color
  67. is already mapped to destination format. */
  68. static void
  69. grub_video_fbfill_direct24 (struct grub_video_fbblit_info *dst,
  70. grub_video_color_t color, int x, int y,
  71. int width, int height)
  72. {
  73. int i;
  74. int j;
  75. grub_size_t rowskip;
  76. grub_uint8_t *dstptr;
  77. #ifndef GRUB_CPU_WORDS_BIGENDIAN
  78. grub_uint8_t fill0 = (grub_uint8_t)((color >> 0) & 0xFF);
  79. grub_uint8_t fill1 = (grub_uint8_t)((color >> 8) & 0xFF);
  80. grub_uint8_t fill2 = (grub_uint8_t)((color >> 16) & 0xFF);
  81. #else
  82. grub_uint8_t fill2 = (grub_uint8_t)((color >> 0) & 0xFF);
  83. grub_uint8_t fill1 = (grub_uint8_t)((color >> 8) & 0xFF);
  84. grub_uint8_t fill0 = (grub_uint8_t)((color >> 16) & 0xFF);
  85. #endif
  86. /* Calculate the number of bytes to advance from the end of one line
  87. to the beginning of the next line. */
  88. rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
  89. /* Get the start address. */
  90. dstptr = grub_video_fb_get_video_ptr (dst, x, y);
  91. for (j = 0; j < height; j++)
  92. {
  93. for (i = 0; i < width; i++)
  94. {
  95. *dstptr++ = fill0;
  96. *dstptr++ = fill1;
  97. *dstptr++ = fill2;
  98. }
  99. /* Advance the dest pointer to the right location on the next line. */
  100. dstptr += rowskip;
  101. }
  102. }
  103. /* Optimized filler for direct color 16 bit modes. It is assumed that color
  104. is already mapped to destination format. */
  105. static void
  106. grub_video_fbfill_direct16 (struct grub_video_fbblit_info *dst,
  107. grub_video_color_t color, int x, int y,
  108. int width, int height)
  109. {
  110. int i;
  111. int j;
  112. grub_size_t rowskip;
  113. grub_uint16_t *dstptr;
  114. /* Calculate the number of bytes to advance from the end of one line
  115. to the beginning of the next line. */
  116. rowskip = (dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width);
  117. /* Get the start address. */
  118. dstptr = grub_video_fb_get_video_ptr (dst, x, y);
  119. for (j = 0; j < height; j++)
  120. {
  121. for (i = 0; i < width; i++)
  122. *dstptr++ = color;
  123. /* Advance the dest pointer to the right location on the next line. */
  124. GRUB_VIDEO_FB_ADVANCE_POINTER (dstptr, rowskip);
  125. }
  126. }
  127. /* Optimized filler for index color. It is assumed that color
  128. is already mapped to destination format. */
  129. static void
  130. grub_video_fbfill_direct8 (struct grub_video_fbblit_info *dst,
  131. grub_video_color_t color, int x, int y,
  132. int width, int height)
  133. {
  134. int i;
  135. int j;
  136. grub_size_t rowskip;
  137. grub_uint8_t *dstptr;
  138. grub_uint8_t fill = (grub_uint8_t)color & 0xFF;
  139. /* Calculate the number of bytes to advance from the end of one line
  140. to the beginning of the next line. */
  141. rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
  142. /* Get the start address. */
  143. dstptr = grub_video_fb_get_video_ptr (dst, x, y);
  144. for (j = 0; j < height; j++)
  145. {
  146. for (i = 0; i < width; i++)
  147. *dstptr++ = fill;
  148. /* Advance the dest pointer to the right location on the next line. */
  149. dstptr += rowskip;
  150. }
  151. }
  152. void
  153. grub_video_fb_fill_dispatch (struct grub_video_fbblit_info *target,
  154. grub_video_color_t color, int x, int y,
  155. unsigned int width, unsigned int height)
  156. {
  157. /* Try to figure out more optimized version. Note that color is already
  158. mapped to target format so we can make assumptions based on that. */
  159. switch (target->mode_info->bytes_per_pixel)
  160. {
  161. case 4:
  162. grub_video_fbfill_direct32 (target, color, x, y,
  163. width, height);
  164. return;
  165. case 3:
  166. grub_video_fbfill_direct24 (target, color, x, y,
  167. width, height);
  168. return;
  169. case 2:
  170. grub_video_fbfill_direct16 (target, color, x, y,
  171. width, height);
  172. return;
  173. case 1:
  174. grub_video_fbfill_direct8 (target, color, x, y,
  175. width, height);
  176. return;
  177. }
  178. /* No optimized version found, use default (slow) filler. */
  179. grub_video_fbfill (target, color, x, y, width, height);
  180. }