Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ComparingUpdateTracker.cxx 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <vector>
  24. #include <rdr/types.h>
  25. #include <rfb/Exception.h>
  26. #include <rfb/LogWriter.h>
  27. #include <rfb/ComparingUpdateTracker.h>
  28. using namespace rfb;
  29. static LogWriter vlog("ComparingUpdateTracker");
  30. ComparingUpdateTracker::ComparingUpdateTracker(PixelBuffer* buffer)
  31. : fb(buffer), oldFb(fb->getPF(), 0, 0), firstCompare(true),
  32. enabled(true), totalPixels(0), missedPixels(0)
  33. {
  34. changed.assign_union(fb->getRect());
  35. }
  36. ComparingUpdateTracker::~ComparingUpdateTracker()
  37. {
  38. }
  39. #define BLOCK_SIZE 64
  40. bool ComparingUpdateTracker::compare()
  41. {
  42. std::vector<Rect> rects;
  43. std::vector<Rect>::iterator i;
  44. if (!enabled)
  45. return false;
  46. if (firstCompare) {
  47. // NB: We leave the change region untouched on this iteration,
  48. // since in effect the entire framebuffer has changed.
  49. oldFb.setSize(fb->width(), fb->height());
  50. for (int y=0; y<fb->height(); y+=BLOCK_SIZE) {
  51. Rect pos(0, y, fb->width(), __rfbmin(fb->height(), y+BLOCK_SIZE));
  52. int srcStride;
  53. const rdr::U8* srcData = fb->getBuffer(pos, &srcStride);
  54. oldFb.imageRect(pos, srcData, srcStride);
  55. }
  56. firstCompare = false;
  57. return false;
  58. }
  59. copied.get_rects(&rects, copy_delta.x<=0, copy_delta.y<=0);
  60. for (i = rects.begin(); i != rects.end(); i++)
  61. oldFb.copyRect(*i, copy_delta);
  62. changed.get_rects(&rects);
  63. Region newChanged;
  64. for (i = rects.begin(); i != rects.end(); i++)
  65. compareRect(*i, &newChanged);
  66. changed.get_rects(&rects);
  67. for (i = rects.begin(); i != rects.end(); i++)
  68. totalPixels += i->area();
  69. newChanged.get_rects(&rects);
  70. for (i = rects.begin(); i != rects.end(); i++)
  71. missedPixels += i->area();
  72. if (changed.equals(newChanged))
  73. return false;
  74. changed = newChanged;
  75. return true;
  76. }
  77. void ComparingUpdateTracker::enable()
  78. {
  79. enabled = true;
  80. }
  81. void ComparingUpdateTracker::disable()
  82. {
  83. enabled = false;
  84. // Make sure we update the framebuffer next time we get enabled
  85. firstCompare = true;
  86. }
  87. void ComparingUpdateTracker::compareRect(const Rect& r, Region* newChanged)
  88. {
  89. if (!r.enclosed_by(fb->getRect())) {
  90. Rect safe;
  91. // Crop the rect and try again
  92. safe = r.intersect(fb->getRect());
  93. if (!safe.is_empty())
  94. compareRect(safe, newChanged);
  95. return;
  96. }
  97. int bytesPerPixel = fb->getPF().bpp/8;
  98. int oldStride;
  99. rdr::U8* oldData = oldFb.getBufferRW(r, &oldStride);
  100. int oldStrideBytes = oldStride * bytesPerPixel;
  101. // Used to efficiently crop the left and right of the change rectangle
  102. int minCompareWidthInPixels = BLOCK_SIZE / 8;
  103. int minCompareWidthInBytes = minCompareWidthInPixels * bytesPerPixel;
  104. for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
  105. {
  106. // Get a strip of the source buffer
  107. Rect pos(r.tl.x, blockTop, r.br.x, __rfbmin(r.br.y, blockTop+BLOCK_SIZE));
  108. int fbStride;
  109. const rdr::U8* newBlockPtr = fb->getBuffer(pos, &fbStride);
  110. int newStrideBytes = fbStride * bytesPerPixel;
  111. rdr::U8* oldBlockPtr = oldData;
  112. int blockBottom = __rfbmin(blockTop+BLOCK_SIZE, r.br.y);
  113. for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
  114. {
  115. const rdr::U8* newPtr = newBlockPtr;
  116. rdr::U8* oldPtr = oldBlockPtr;
  117. int blockRight = __rfbmin(blockLeft+BLOCK_SIZE, r.br.x);
  118. int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
  119. // Scan the block top to bottom, to identify the first row of change
  120. for (int y = blockTop; y < blockBottom; y++)
  121. {
  122. if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
  123. {
  124. // Define the change rectangle using pessimistic values to start
  125. int changeHeight = blockBottom - y;
  126. int changeLeft = blockLeft;
  127. int changeRight = blockRight;
  128. // For every unchanged row at the bottom of the block, decrement change height
  129. {
  130. const rdr::U8* newRowPtr = newPtr + ((changeHeight - 1) * newStrideBytes);
  131. const rdr::U8* oldRowPtr = oldPtr + ((changeHeight - 1) * oldStrideBytes);
  132. while (changeHeight > 1 && memcmp(oldRowPtr, newRowPtr, blockWidthInBytes) == 0)
  133. {
  134. newRowPtr -= newStrideBytes;
  135. oldRowPtr -= oldStrideBytes;
  136. changeHeight--;
  137. }
  138. }
  139. // For every unchanged column at the left of the block, increment change left
  140. {
  141. const rdr::U8* newColumnPtr = newPtr;
  142. const rdr::U8* oldColumnPtr = oldPtr;
  143. while (changeLeft + minCompareWidthInPixels < changeRight)
  144. {
  145. const rdr::U8* newRowPtr = newColumnPtr;
  146. const rdr::U8* oldRowPtr = oldColumnPtr;
  147. for (int row = 0; row < changeHeight; row++)
  148. {
  149. if (memcmp(oldRowPtr, newRowPtr, minCompareWidthInBytes) != 0)
  150. goto endOfChangeLeft;
  151. newRowPtr += newStrideBytes;
  152. oldRowPtr += oldStrideBytes;
  153. }
  154. newColumnPtr += minCompareWidthInBytes;
  155. oldColumnPtr += minCompareWidthInBytes;
  156. changeLeft += minCompareWidthInPixels;
  157. }
  158. }
  159. endOfChangeLeft:
  160. // For every unchanged column at the right of the block, decrement change right
  161. {
  162. const rdr::U8* newColumnPtr = newPtr + blockWidthInBytes;
  163. const rdr::U8* oldColumnPtr = oldPtr + blockWidthInBytes;
  164. while (changeLeft + minCompareWidthInPixels < changeRight)
  165. {
  166. newColumnPtr -= minCompareWidthInBytes;
  167. oldColumnPtr -= minCompareWidthInBytes;
  168. const rdr::U8* newRowPtr = newColumnPtr;
  169. const rdr::U8* oldRowPtr = oldColumnPtr;
  170. for (int row = 0; row < changeHeight; row++)
  171. {
  172. if (memcmp(oldRowPtr, newRowPtr, minCompareWidthInBytes) != 0)
  173. goto endOfChangeRight;
  174. newRowPtr += newStrideBytes;
  175. oldRowPtr += oldStrideBytes;
  176. }
  177. changeRight -= minCompareWidthInPixels;
  178. }
  179. }
  180. endOfChangeRight:
  181. // Block change extends from (changeLeft, y) to (changeRight, y + changeHeight)
  182. newChanged->assign_union(Region(Rect(changeLeft, y, changeRight, y + changeHeight)));
  183. // Copy the change from fb to oldFb to allow future changes to be identified
  184. for (int row = 0; row < changeHeight; row++)
  185. {
  186. memcpy(oldPtr, newPtr, blockWidthInBytes);
  187. newPtr += newStrideBytes;
  188. oldPtr += oldStrideBytes;
  189. }
  190. // No further processing is required for this block
  191. break;
  192. }
  193. newPtr += newStrideBytes;
  194. oldPtr += oldStrideBytes;
  195. }
  196. oldBlockPtr += blockWidthInBytes;
  197. newBlockPtr += blockWidthInBytes;
  198. }
  199. oldData += oldStrideBytes * BLOCK_SIZE;
  200. }
  201. oldFb.commitBufferRW(r);
  202. }
  203. void ComparingUpdateTracker::logStats()
  204. {
  205. double ratio;
  206. char a[1024], b[1024];
  207. siPrefix(totalPixels, "pixels", a, sizeof(a));
  208. siPrefix(missedPixels, "pixels", b, sizeof(b));
  209. ratio = (double)totalPixels / missedPixels;
  210. vlog.info("%s in / %s out", a, b);
  211. vlog.info("(1:%g ratio)", ratio);
  212. totalPixels = missedPixels = 0;
  213. }