Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ComparingUpdateTracker.cxx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. std::vector<Rect> changedBlocks;
  99. for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
  100. {
  101. // Get a strip of the source buffer
  102. Rect pos(r.tl.x, blockTop, r.br.x, __rfbmin(r.br.y, blockTop+BLOCK_SIZE));
  103. int fbStride;
  104. const rdr::U8* newBlockPtr = fb->getBuffer(pos, &fbStride);
  105. int newStrideBytes = fbStride * bytesPerPixel;
  106. rdr::U8* oldBlockPtr = oldData;
  107. int blockBottom = __rfbmin(blockTop+BLOCK_SIZE, r.br.y);
  108. for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
  109. {
  110. const rdr::U8* newPtr = newBlockPtr;
  111. rdr::U8* oldPtr = oldBlockPtr;
  112. int blockRight = __rfbmin(blockLeft+BLOCK_SIZE, r.br.x);
  113. int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
  114. for (int y = blockTop; y < blockBottom; y++)
  115. {
  116. if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
  117. {
  118. // A block has changed - copy the remainder to the oldFb
  119. changedBlocks.push_back(Rect(blockLeft, blockTop,
  120. blockRight, blockBottom));
  121. for (int y2 = y; y2 < blockBottom; y2++)
  122. {
  123. memcpy(oldPtr, newPtr, blockWidthInBytes);
  124. newPtr += newStrideBytes;
  125. oldPtr += oldStrideBytes;
  126. }
  127. break;
  128. }
  129. newPtr += newStrideBytes;
  130. oldPtr += oldStrideBytes;
  131. }
  132. oldBlockPtr += blockWidthInBytes;
  133. newBlockPtr += blockWidthInBytes;
  134. }
  135. oldData += oldStrideBytes * BLOCK_SIZE;
  136. }
  137. oldFb.commitBufferRW(r);
  138. if (!changedBlocks.empty()) {
  139. Region temp;
  140. temp.setOrderedRects(changedBlocks);
  141. newChanged->assign_union(temp);
  142. }
  143. }
  144. void ComparingUpdateTracker::logStats()
  145. {
  146. double ratio;
  147. char a[1024], b[1024];
  148. siPrefix(totalPixels, "pixels", a, sizeof(a));
  149. siPrefix(missedPixels, "pixels", b, sizeof(b));
  150. ratio = (double)totalPixels / missedPixels;
  151. vlog.info("%s in / %s out", a, b);
  152. vlog.info("(1:%g ratio)", ratio);
  153. totalPixels = missedPixels = 0;
  154. }