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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 2002-2004 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 <vector>
  20. #include <rdr/types.h>
  21. #include <rfb/Exception.h>
  22. #include <rfb/ComparingUpdateTracker.h>
  23. using namespace rfb;
  24. ComparingUpdateTracker::ComparingUpdateTracker(PixelBuffer* buffer)
  25. : SimpleUpdateTracker(true), fb(buffer),
  26. oldFb(fb->getPF(), 0, 0), firstCompare(true)
  27. {
  28. changed.assign_union(fb->getRect());
  29. }
  30. ComparingUpdateTracker::~ComparingUpdateTracker()
  31. {
  32. }
  33. void ComparingUpdateTracker::flush_update(UpdateInfo* info,
  34. const Region& cliprgn, int maxArea)
  35. {
  36. throw rfb::Exception("flush_update(UpdateInfo*) not implemented");
  37. }
  38. void ComparingUpdateTracker::flush_update(UpdateTracker &ut,
  39. const Region &cliprgn)
  40. {
  41. throw rfb::Exception("flush_update(UpdateTracker&) not implemented");
  42. }
  43. #define BLOCK_SIZE 16
  44. void ComparingUpdateTracker::compare()
  45. {
  46. std::vector<Rect> rects;
  47. std::vector<Rect>::iterator i;
  48. if (firstCompare) {
  49. // NB: We leave the change region untouched on this iteration,
  50. // since in effect the entire framebuffer has changed.
  51. oldFb.setSize(fb->width(), fb->height());
  52. for (int y=0; y<fb->height(); y+=BLOCK_SIZE) {
  53. Rect pos(0, y, fb->width(), min(fb->height(), y+BLOCK_SIZE));
  54. int srcStride;
  55. const rdr::U8* srcData = fb->getPixelsR(pos, &srcStride);
  56. oldFb.imageRect(pos, srcData, srcStride);
  57. }
  58. firstCompare = false;
  59. } else {
  60. copied.get_rects(&rects, copy_delta.x<=0, copy_delta.y<=0);
  61. for (i = rects.begin(); i != rects.end(); i++)
  62. oldFb.copyRect(*i, copy_delta);
  63. Region to_check = changed.union_(copied);
  64. to_check.get_rects(&rects);
  65. Region newChanged;
  66. for (i = rects.begin(); i != rects.end(); i++)
  67. compareRect(*i, &newChanged);
  68. copied.assign_subtract(newChanged);
  69. changed = newChanged;
  70. }
  71. }
  72. void ComparingUpdateTracker::compareRect(const Rect& r, Region* newChanged)
  73. {
  74. if (!r.enclosed_by(fb->getRect())) {
  75. fprintf(stderr,"ComparingUpdateTracker: rect outside fb (%d,%d-%d,%d)\n", r.tl.x, r.tl.y, r.br.x, r.br.y);
  76. return;
  77. }
  78. int bytesPerPixel = fb->getPF().bpp/8;
  79. int oldStride;
  80. rdr::U8* oldData = oldFb.getPixelsRW(r, &oldStride);
  81. int oldStrideBytes = oldStride * bytesPerPixel;
  82. std::vector<Rect> changedBlocks;
  83. for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
  84. {
  85. // Get a strip of the source buffer
  86. Rect pos(r.tl.x, blockTop, r.br.x, min(r.br.y, blockTop+BLOCK_SIZE));
  87. int fbStride;
  88. const rdr::U8* newBlockPtr = fb->getPixelsR(pos, &fbStride);
  89. int newStrideBytes = fbStride * bytesPerPixel;
  90. rdr::U8* oldBlockPtr = oldData;
  91. int blockBottom = min(blockTop+BLOCK_SIZE, r.br.y);
  92. for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
  93. {
  94. const rdr::U8* newPtr = newBlockPtr;
  95. rdr::U8* oldPtr = oldBlockPtr;
  96. int blockRight = min(blockLeft+BLOCK_SIZE, r.br.x);
  97. int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
  98. for (int y = blockTop; y < blockBottom; y++)
  99. {
  100. if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
  101. {
  102. // A block has changed - copy the remainder to the oldFb
  103. changedBlocks.push_back(Rect(blockLeft, blockTop,
  104. blockRight, blockBottom));
  105. for (int y2 = y; y2 < blockBottom; y2++)
  106. {
  107. memcpy(oldPtr, newPtr, blockWidthInBytes);
  108. newPtr += newStrideBytes;
  109. oldPtr += oldStrideBytes;
  110. }
  111. break;
  112. }
  113. newPtr += newStrideBytes;
  114. oldPtr += oldStrideBytes;
  115. }
  116. oldBlockPtr += blockWidthInBytes;
  117. newBlockPtr += blockWidthInBytes;
  118. }
  119. oldData += oldStrideBytes * BLOCK_SIZE;
  120. }
  121. if (!changedBlocks.empty()) {
  122. Region temp;
  123. temp.setOrderedRects(changedBlocks);
  124. newChanged->assign_union(temp);
  125. }
  126. }