nxglib_getrectangle.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************************
  2. * graphics/nxglib/fb/nxglib_getrectangle.c
  3. *
  4. * Copyright (C) 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. /****************************************************************************
  47. * Private Types
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Public Data
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Private Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: nxgl_lowresmemcpy
  60. ****************************************************************************/
  61. #if NXGLIB_BITSPERPIXEL < 8
  62. static inline void nxgl_lowresmemcpy(FAR uint8_t *dline, FAR const uint8_t *sline,
  63. unsigned int width,
  64. uint8_t leadmask, uint8_t tailmask)
  65. {
  66. FAR const uint8_t *sptr;
  67. FAR uint8_t *dptr;
  68. uint8_t mask;
  69. int lnlen;
  70. /* Handle masking of the fractional initial byte */
  71. mask = leadmask;
  72. sptr = sline;
  73. dptr = dline;
  74. lnlen = width;
  75. if (lnlen > 1 && mask)
  76. {
  77. dptr[0] = (dptr[0] & ~mask) | (sptr[0] & mask);
  78. mask = 0xff;
  79. dptr++;
  80. sptr++;
  81. lnlen--;
  82. }
  83. /* Handle masking of the fractional final byte */
  84. mask &= tailmask;
  85. if (lnlen > 0 && mask)
  86. {
  87. dptr[lnlen-1] = (dptr[lnlen-1] & ~mask) | (sptr[lnlen-1] & mask);
  88. lnlen--;
  89. }
  90. /* Handle all of the unmasked bytes in-between */
  91. if (lnlen > 0)
  92. {
  93. NXGL_MEMCPY(dptr, sptr, lnlen);
  94. }
  95. }
  96. #endif
  97. /****************************************************************************
  98. * Public Functions
  99. ****************************************************************************/
  100. /****************************************************************************
  101. * Name: nxgl_getrectangle_*bpp
  102. *
  103. * Descripton:
  104. * Fetch a rectangular region from framebuffer memory. The source is
  105. * expressed as a rectangle.
  106. *
  107. ****************************************************************************/
  108. void NXGL_FUNCNAME(nxgl_getrectangle,NXGLIB_SUFFIX)
  109. (FAR struct fb_planeinfo_s *pinfo, FAR const struct nxgl_rect_s *rect,
  110. FAR void *dest, unsigned int deststride)
  111. {
  112. FAR const uint8_t *sline;
  113. FAR uint8_t *dline;
  114. unsigned int width;
  115. unsigned int fbstride;
  116. unsigned int rows;
  117. #if NXGLIB_BITSPERPIXEL < 8
  118. uint8_t leadmask;
  119. uint8_t tailmask;
  120. #endif
  121. /* Get the width of the framebuffer in bytes */
  122. fbstride = pinfo->stride;
  123. /* Get the dimensions of the rectange to copy: width in pixels, height
  124. * in rows
  125. */
  126. width = rect->pt2.x - rect->pt1.x + 1;
  127. rows = rect->pt2.y - rect->pt1.y + 1;
  128. #if NXGLIB_BITSPERPIXEL < 8
  129. # ifdef CONFIG_NX_PACKEDMSFIRST
  130. /* Get the mask for pixels that are ordered so that they pack from the
  131. * MS byte down.
  132. */
  133. leadmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x)));
  134. tailmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt2.x-1)));
  135. # else
  136. /* Get the mask for pixels that are ordered so that they pack from the
  137. * LS byte up.
  138. */
  139. leadmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt1.x)));
  140. tailmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x-1)));
  141. # endif
  142. #endif
  143. /* sline = address of the first pixel in the top row of the source in
  144. * framebuffer memory
  145. */
  146. sline = pinfo->fbmem + rect->pt1.y * fbstride + NXGL_SCALEX(rect->pt1.x);
  147. /* dline = address of the first row pixel */
  148. dline = (FAR uint8_t *)dest;
  149. /* Yes.. Copy the rectangle */
  150. while (rows--)
  151. {
  152. /* Copy the row */
  153. #if NXGLIB_BITSPERPIXEL < 8
  154. nxgl_lowresmemcpy(dline, sline, width, leadmask, tailmask);
  155. #else
  156. NXGL_MEMCPY(dline, sline, width);
  157. #endif
  158. /* Point to the next source/dest row below the current one */
  159. dline += deststride;
  160. sline += fbstride;
  161. }
  162. }