nxglib_fillrectangle.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /****************************************************************************
  2. * graphics/nxglib/fb/nxglib_fillrectangle.c
  3. *
  4. * Copyright (C) 2008-2011 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <nuttx/fb.h>
  41. #include <nuttx/nx/nxglib.h>
  42. #include "nxglib_bitblit.h"
  43. /****************************************************************************
  44. * Pre-Processor Definitions
  45. ****************************************************************************/
  46. #ifndef NXGLIB_SUFFIX
  47. # error "NXGLIB_SUFFIX must be defined before including this header file"
  48. #endif
  49. /****************************************************************************
  50. * Private Types
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Data
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Public Data
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Private Functions
  60. ****************************************************************************/
  61. /****************************************************************************
  62. * Public Functions
  63. ****************************************************************************/
  64. /****************************************************************************
  65. * Name: nxgl_fillrectangle_*bpp
  66. *
  67. * Descripton:
  68. * Fill a rectangle region in the framebuffer memory with a fixed color
  69. *
  70. ****************************************************************************/
  71. void NXGL_FUNCNAME(nxgl_fillrectangle,NXGLIB_SUFFIX)
  72. (FAR struct fb_planeinfo_s *pinfo,
  73. FAR const struct nxgl_rect_s *rect,
  74. NXGL_PIXEL_T color)
  75. {
  76. FAR uint8_t *line;
  77. unsigned int width;
  78. unsigned int stride;
  79. int rows;
  80. #if NXGLIB_BITSPERPIXEL < 8
  81. FAR uint8_t *dest;
  82. uint8_t mpixel = NXGL_MULTIPIXEL(color);
  83. uint8_t leadmask;
  84. uint8_t tailmask;
  85. uint8_t mask;
  86. int lnlen;
  87. #endif
  88. /* Get the width of the framebuffer in bytes */
  89. stride = pinfo->stride;
  90. /* Get the dimensions of the rectange to fill in pixels */
  91. width = rect->pt2.x - rect->pt1.x + 1;
  92. rows = rect->pt2.y - rect->pt1.y + 1;
  93. /* Get the address of the first byte in the first line to write */
  94. line = pinfo->fbmem + rect->pt1.y * stride + NXGL_SCALEX(rect->pt1.x);
  95. #if NXGLIB_BITSPERPIXEL < 8
  96. # ifdef CONFIG_NX_PACKEDMSFIRST
  97. /* Get the mask for pixels that are ordered so that they pack from the
  98. * MS byte down.
  99. */
  100. leadmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x)));
  101. tailmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt2.x-1)));
  102. # else
  103. /* Get the mask for pixels that are ordered so that they pack from the
  104. * LS byte up.
  105. */
  106. leadmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt1.x)));
  107. tailmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x-1)));
  108. # endif
  109. #endif
  110. /* Then fill the rectangle line-by-line */
  111. while (rows-- > 0)
  112. {
  113. #if NXGLIB_BITSPERPIXEL < 8
  114. /* Handle masking of the fractional initial byte */
  115. mask = leadmask;
  116. dest = line;
  117. lnlen = width;
  118. if (lnlen > 1 && mask)
  119. {
  120. dest[0] = (dest[0] & ~mask) | (mpixel & mask);
  121. mask = 0xff;
  122. dest++;
  123. lnlen--;
  124. }
  125. /* Handle masking of the fractional final byte */
  126. mask &= tailmask;
  127. if (lnlen > 0 && mask)
  128. {
  129. dest[lnlen-1] = (dest[lnlen-1] & ~mask) | (mpixel & mask);
  130. lnlen--;
  131. }
  132. /* Handle all of the unmasked bytes in-between */
  133. if (lnlen > 0)
  134. {
  135. NXGL_MEMSET(dest, (NXGL_PIXEL_T)color, lnlen);
  136. }
  137. #else
  138. /* Draw the entire raster line */
  139. NXGL_MEMSET(line, (NXGL_PIXEL_T)color, width);
  140. #endif
  141. line += stride;
  142. }
  143. }