nxglib_moverectangle.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. * graphics/nxglib/fb/nxglib_moverectangle.c
  3. *
  4. * Copyright (C) 2008-2012 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_moverectangle_*bpp
  102. *
  103. * Descripton:
  104. * Move a rectangular region from location to another in the
  105. * framebuffer memory. The source is expressed as a rectangle; the
  106. * destination position is expressed as a point corresponding to the
  107. * translation of the upper, left-hand corner.
  108. *
  109. ****************************************************************************/
  110. void NXGL_FUNCNAME(nxgl_moverectangle,NXGLIB_SUFFIX)
  111. (FAR struct fb_planeinfo_s *pinfo, FAR const struct nxgl_rect_s *rect,
  112. FAR struct nxgl_point_s *offset)
  113. {
  114. FAR const uint8_t *sline;
  115. FAR uint8_t *dline;
  116. unsigned int width;
  117. unsigned int stride;
  118. unsigned int rows;
  119. #if NXGLIB_BITSPERPIXEL < 8
  120. uint8_t leadmask;
  121. uint8_t tailmask;
  122. #endif
  123. /* Get the width of the framebuffer in bytes */
  124. stride = pinfo->stride;
  125. /* Get the dimensions of the rectange to fill: width in pixels, height
  126. * in rows
  127. */
  128. width = rect->pt2.x - rect->pt1.x + 1;
  129. rows = rect->pt2.y - rect->pt1.y + 1;
  130. #if NXGLIB_BITSPERPIXEL < 8
  131. # ifdef CONFIG_NX_PACKEDMSFIRST
  132. /* Get the mask for pixels that are ordered so that they pack from the
  133. * MS byte down.
  134. */
  135. leadmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x)));
  136. tailmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt2.x-1)));
  137. # else
  138. /* Get the mask for pixels that are ordered so that they pack from the
  139. * LS byte up.
  140. */
  141. leadmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(rect->pt1.x)));
  142. tailmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(rect->pt1.x-1)));
  143. # endif
  144. #endif
  145. /* sline = address of the first pixel in the top row of the source in
  146. * framebuffer memory
  147. */
  148. sline = pinfo->fbmem + rect->pt1.y * stride + NXGL_SCALEX(rect->pt1.x);
  149. /* dline = address of the first pixel in the top row of the destination
  150. * in framebuffer memory. We get dline by subtract the offset from the
  151. * source position.
  152. */
  153. dline = pinfo->fbmem + offset->y * stride + NXGL_SCALEX(offset->x);
  154. /* Case 1: Is the destination position above the displayed position?
  155. * If the destination position is less then then the src address, then the
  156. * destination is offset to a postion below (and or to the left) of the
  157. * source in framebuffer memory.
  158. */
  159. if (offset->y < rect->pt1.y ||
  160. (offset->y < rect->pt1.y && offset->x <= rect->pt1.x))
  161. {
  162. /* Yes.. Copy the rectangle from top down (i.e., adding the stride
  163. * to move to the next, lower row) */
  164. while (rows--)
  165. {
  166. /* Copy the row */
  167. #if NXGLIB_BITSPERPIXEL < 8
  168. nxgl_lowresmemcpy(dline, sline, width, leadmask, tailmask);
  169. #else
  170. NXGL_MEMCPY(dline, sline, width);
  171. #endif
  172. /* Point to the next source/dest row below the current one */
  173. dline += stride;
  174. sline += stride;
  175. }
  176. }
  177. /* Case 2: No.. the destination position is above (or to the left of)
  178. * the displayed source position
  179. */
  180. else
  181. {
  182. /* Adjust sline and dline to point to the bottom row (+1) of the
  183. * source and destination rectangles in framebuffer memory.
  184. */
  185. unsigned int hoffset = rows * stride;
  186. sline += hoffset;
  187. dline += hoffset;
  188. /* Copy the rectangle from the bottom up (i.e., subtracting stride
  189. * to re-position to the previous, higher row)
  190. */
  191. while (rows--)
  192. {
  193. /* Point to the next source/dest row above the current one */
  194. dline -= stride;
  195. sline -= stride;
  196. /* Copy the row */
  197. #if NXGLIB_BITSPERPIXEL < 8
  198. nxgl_lowresmemcpy(dline, sline, width, leadmask, tailmask);
  199. #else
  200. NXGL_MEMCPY(dline, sline, width);
  201. #endif
  202. }
  203. }
  204. }