You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComparingUpdateTracker.cxx 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <vector>
  21. #include <rdr/types.h>
  22. #include <rfb/Exception.h>
  23. #include <rfb/LogWriter.h>
  24. #include <rfb/ComparingUpdateTracker.h>
  25. using namespace rfb;
  26. static LogWriter vlog("ComparingUpdateTracker");
  27. ComparingUpdateTracker::ComparingUpdateTracker(PixelBuffer* buffer)
  28. : fb(buffer), oldFb(fb->getPF(), 0, 0), firstCompare(true),
  29. enabled(true), totalPixels(0), missedPixels(0)
  30. {
  31. changed.assign_union(fb->getRect());
  32. }
  33. ComparingUpdateTracker::~ComparingUpdateTracker()
  34. {
  35. }
  36. #define BLOCK_SIZE 64
  37. bool ComparingUpdateTracker::compare()
  38. {
  39. std::vector<Rect> rects;
  40. std::vector<Rect>::iterator i;
  41. if (!enabled)
  42. return false;
  43. if (firstCompare) {
  44. // NB: We leave the change region untouched on this iteration,
  45. // since in effect the entire framebuffer has changed.
  46. oldFb.setSize(fb->width(), fb->height());
  47. for (int y=0; y<fb->height(); y+=BLOCK_SIZE) {
  48. Rect pos(0, y, fb->width(), __rfbmin(fb->height(), y+BLOCK_SIZE));
  49. int srcStride;
  50. const rdr::U8* srcData = fb->getBuffer(pos, &srcStride);
  51. oldFb.imageRect(pos, srcData, srcStride);
  52. }
  53. firstCompare = false;
  54. return false;
  55. }
  56. copied.get_rects(&rects, copy_delta.x<=0, copy_delta.y<=0);
  57. for (i = rects.begin(); i != rects.end(); i++)
  58. oldFb.copyRect(*i, copy_delta);
  59. changed.get_rects(&rects);
  60. Region newChanged;
  61. for (i = rects.begin(); i != rects.end(); i++)
  62. compareRect(*i, &newChanged);
  63. changed.get_rects(&rects);
  64. for (i = rects.begin(); i != rects.end(); i++)
  65. totalPixels += i->area();
  66. newChanged.get_rects(&rects);
  67. for (i = rects.begin(); i != rects.end(); i++)
  68. missedPixels += i->area();
  69. if (changed.equals(newChanged))
  70. return false;
  71. changed = newChanged;
  72. return true;
  73. }
  74. void ComparingUpdateTracker::enable()
  75. {
  76. enabled = true;
  77. }
  78. void ComparingUpdateTracker::disable()
  79. {
  80. enabled = false;
  81. // Make sure we update the framebuffer next time we get enabled
  82. firstCompare = true;
  83. }
  84. void ComparingUpdateTracker::compareRect(const Rect& r, Region* newChanged)
  85. {
  86. if (!r.enclosed_by(fb->getRect())) {
  87. Rect safe;
  88. // Crop the rect and try again
  89. safe = r.intersect(fb->getRect());
  90. if (!safe.is_empty())
  91. compareRect(safe, newChanged);
  92. return;
  93. }
  94. int bytesPerPixel = fb->getPF().bpp/8;
  95. int oldStride;
  96. rdr::U8* oldData = oldFb.getBufferRW(r, &oldStride);
  97. int oldStrideBytes = oldStride * bytesPerPixel;
  98. // Used to efficiently crop the left and right of the change rectangle
  99. int minCompareWidthInPixels = BLOCK_SIZE / 8;
  100. int minCompareWidthInBytes = minCompareWidthInPixels * bytesPerPixel;
  101. for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
  102. {
  103. // Get a strip of the source buffer
  104. Rect pos(r.tl.x, blockTop, r.br.x, __rfbmin(r.br.y, blockTop+BLOCK_SIZE));
  105. int fbStride;
  106. const rdr::U8* newBlockPtr = fb->getBuffer(pos, &fbStride);
  107. int newStrideBytes = fbStride * bytesPerPixel;
  108. rdr::U8* oldBlockPtr = oldData;
  109. int blockBottom = __rfbmin(blockTop+BLOCK_SIZE, r.br.y);
  110. for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
  111. {
  112. const rdr::U8* newPtr = newBlockPtr;
  113. rdr::U8* oldPtr = oldBlockPtr;
  114. int blockRight = __rfbmin(blockLeft+BLOCK_SIZE, r.br.x);
  115. int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
  116. // Scan the block top to bottom, to identify the first row of change
  117. for (int y = blockTop; y < blockBottom; y++)
  118. {
  119. if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
  120. {
  121. // Define the change rectangle using pessimistic values to start
  122. int changeHeight = blockBottom - y;
  123. int changeLeft = blockLeft;
  124. int changeRight = blockRight;
  125. // For every unchanged row at the bottom of the block, decrement change height
  126. {
  127. const rdr::U8* newRowPtr = newPtr + ((changeHeight - 1) * newStrideBytes);
  128. const rdr::U8* oldRowPtr = oldPtr + ((changeHeight - 1) * oldStrideBytes);
  129. while (changeHeight > 1 && memcmp(oldRowPtr, newRowPtr, blockWidthInBytes) == 0)
  130. {
  131. newRowPtr -= newStrideBytes;
  132. oldRowPtr -= oldStrideBytes;
  133. changeHeight--;
  134. }
  135. }
  136. // For every unchanged column at the left of the block, increment change left
  137. {
  138. const rdr::U8* newColumnPtr = newPtr;
  139. const rdr::U8* oldColumnPtr = oldPtr;
  140. while (changeLeft + minCompareWidthInPixels < changeRight)
  141. {
  142. const rdr::U8* newRowPtr = newColumnPtr;
  143. const rdr::U8* oldRowPtr = oldColumnPtr;
  144. for (int row = 0; row < changeHeight; row++)
  145. {
  146. if (memcmp(oldRowPtr, newRowPtr, minCompareWidthInBytes) != 0)
  147. goto endOfChangeLeft;
  148. newRowPtr += newStrideBytes;
  149. oldRowPtr += oldStrideBytes;
  150. }
  151. newColumnPtr += minCompareWidthInBytes;
  152. oldColumnPtr += minCompareWidthInBytes;
  153. changeLeft += minCompareWidthInPixels;
  154. }
  155. }
  156. endOfChangeLeft:
  157. // For every unchanged column at the right of the block, decrement change right
  158. {
  159. const rdr::U8* newColumnPtr = newPtr + blockWidthInBytes;
  160. const rdr::U8* oldColumnPtr = oldPtr + blockWidthInBytes;
  161. while (changeLeft + minCompareWidthInPixels < changeRight)
  162. {
  163. newColumnPtr -= minCompareWidthInBytes;
  164. oldColumnPtr -= minCompareWidthInBytes;
  165. const rdr::U8* newRowPtr = newColumnPtr;
  166. const rdr::U8* oldRowPtr = oldColumnPtr;
  167. for (int row = 0; row < changeHeight; row++)
  168. {
  169. if (memcmp(oldRowPtr, newRowPtr, minCompareWidthInBytes) != 0)
  170. goto endOfChangeRight;
  171. newRowPtr += newStrideBytes;
  172. oldRowPtr += oldStrideBytes;
  173. }
  174. changeRight -= minCompareWidthInPixels;
  175. }
  176. }
  177. endOfChangeRight:
  178. // Block change extends from (changeLeft, y) to (changeRight, y + changeHeight)
  179. newChanged->assign_union(Region(Rect(changeLeft, y, changeRight, y + changeHeight)));
  180. // Copy the change from fb to oldFb to allow future changes to be identified
  181. for (int row = 0; row < changeHeight; row++)
  182. {
  183. memcpy(oldPtr, newPtr, blockWidthInBytes);
  184. newPtr += newStrideBytes;
  185. oldPtr += oldStrideBytes;
  186. }
  187. // No further processing is required for this block
  188. break;
  189. }
  190. newPtr += newStrideBytes;
  191. oldPtr += oldStrideBytes;
  192. }
  193. oldBlockPtr += blockWidthInBytes;
  194. newBlockPtr += blockWidthInBytes;
  195. }
  196. oldData += oldStrideBytes * BLOCK_SIZE;
  197. }
  198. oldFb.commitBufferRW(r);
  199. }
  200. void ComparingUpdateTracker::logStats()
  201. {
  202. double ratio;
  203. char a[1024], b[1024];
  204. siPrefix(totalPixels, "pixels", a, sizeof(a));
  205. siPrefix(missedPixels, "pixels", b, sizeof(b));
  206. ratio = (double)totalPixels / missedPixels;
  207. vlog.info("%s in / %s out", a, b);
  208. vlog.info("(1:%g ratio)", ratio);
  209. totalPixels = missedPixels = 0;
  210. }