fbutil.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2009 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/fbutil.h>
  26. #include <grub/types.h>
  27. #include <grub/video.h>
  28. grub_video_color_t
  29. get_pixel (struct grub_video_fbblit_info *source,
  30. unsigned int x, unsigned int y)
  31. {
  32. grub_video_color_t color = 0;
  33. switch (source->mode_info->bpp)
  34. {
  35. case 32:
  36. color = *(grub_uint32_t *)grub_video_fb_get_video_ptr (source, x, y);
  37. break;
  38. case 24:
  39. {
  40. grub_uint8_t *ptr;
  41. ptr = grub_video_fb_get_video_ptr (source, x, y);
  42. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  43. color = ptr[2] | (ptr[1] << 8) | (ptr[0] << 16);
  44. #else
  45. color = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16);
  46. #endif
  47. }
  48. break;
  49. case 16:
  50. case 15:
  51. color = *(grub_uint16_t *)grub_video_fb_get_video_ptr (source, x, y);
  52. break;
  53. case 8:
  54. color = *(grub_uint8_t *)grub_video_fb_get_video_ptr (source, x, y);
  55. break;
  56. case 1:
  57. if (source->mode_info->blit_format == GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED)
  58. {
  59. int bit_index = y * source->mode_info->width + x;
  60. grub_uint8_t *ptr = source->data + bit_index / 8;
  61. int bit_pos = 7 - bit_index % 8;
  62. color = (*ptr >> bit_pos) & 0x01;
  63. }
  64. break;
  65. default:
  66. break;
  67. }
  68. return color;
  69. }
  70. void
  71. set_pixel (struct grub_video_fbblit_info *source,
  72. unsigned int x, unsigned int y, grub_video_color_t color)
  73. {
  74. switch (source->mode_info->bpp)
  75. {
  76. case 32:
  77. {
  78. grub_uint32_t *ptr;
  79. ptr = (grub_uint32_t *)grub_video_fb_get_video_ptr (source, x, y);
  80. *ptr = color;
  81. }
  82. break;
  83. case 24:
  84. {
  85. grub_uint8_t *ptr;
  86. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  87. grub_uint8_t *colorptr = ((grub_uint8_t *)&color) + 1;
  88. #else
  89. grub_uint8_t *colorptr = (grub_uint8_t *)&color;
  90. #endif
  91. ptr = grub_video_fb_get_video_ptr (source, x, y);
  92. ptr[0] = colorptr[0];
  93. ptr[1] = colorptr[1];
  94. ptr[2] = colorptr[2];
  95. }
  96. break;
  97. case 16:
  98. case 15:
  99. {
  100. grub_uint16_t *ptr;
  101. ptr = (grub_uint16_t *)grub_video_fb_get_video_ptr (source, x, y);
  102. *ptr = (grub_uint16_t) (color & 0xFFFF);
  103. }
  104. break;
  105. case 8:
  106. {
  107. grub_uint8_t *ptr;
  108. ptr = (grub_uint8_t *)grub_video_fb_get_video_ptr (source, x, y);
  109. *ptr = (grub_uint8_t) (color & 0xFF);
  110. }
  111. break;
  112. case 1:
  113. if (source->mode_info->blit_format == GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED)
  114. {
  115. int bit_index = y * source->mode_info->width + x;
  116. grub_uint8_t *ptr = source->data + bit_index / 8;
  117. int bit_pos = 7 - bit_index % 8;
  118. *ptr = (*ptr & ~(1 << bit_pos)) | ((color & 0x01) << bit_pos);
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }