blob: 42a9e192758c422a2b6ed55394f653da4f85ce2a [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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>
Adam Tkac20e0d712008-11-14 14:48:21 +000019#include <string.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <vector>
21#include <rdr/types.h>
22#include <rfb/Exception.h>
23#include <rfb/ComparingUpdateTracker.h>
24
25using namespace rfb;
26
27ComparingUpdateTracker::ComparingUpdateTracker(PixelBuffer* buffer)
28 : fb(buffer), oldFb(fb->getPF(), 0, 0), firstCompare(true)
29{
30 changed.assign_union(fb->getRect());
31}
32
33ComparingUpdateTracker::~ComparingUpdateTracker()
34{
35}
36
37
38#define BLOCK_SIZE 16
39
40void ComparingUpdateTracker::compare()
41{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 std::vector<Rect> rects;
43 std::vector<Rect>::iterator i;
44
45 if (firstCompare) {
46 // NB: We leave the change region untouched on this iteration,
47 // since in effect the entire framebuffer has changed.
48 oldFb.setSize(fb->width(), fb->height());
49 for (int y=0; y<fb->height(); y+=BLOCK_SIZE) {
50 Rect pos(0, y, fb->width(), __rfbmin(fb->height(), y+BLOCK_SIZE));
51 int srcStride;
52 const rdr::U8* srcData = fb->getPixelsR(pos, &srcStride);
53 oldFb.imageRect(pos, srcData, srcStride);
54 }
55 firstCompare = false;
56 } else {
57 copied.get_rects(&rects, copy_delta.x<=0, copy_delta.y<=0);
58 for (i = rects.begin(); i != rects.end(); i++)
59 oldFb.copyRect(*i, copy_delta);
60
Pierre Ossman939dcd12011-11-10 12:37:39 +000061 changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062
63 Region newChanged;
64 for (i = rects.begin(); i != rects.end(); i++)
65 compareRect(*i, &newChanged);
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 changed = newChanged;
68 }
69}
70
71void ComparingUpdateTracker::compareRect(const Rect& r, Region* newChanged)
72{
73 if (!r.enclosed_by(fb->getRect())) {
Pierre Ossmand88895d2009-06-09 14:18:36 +000074 Rect safe;
75 // Crop the rect and try again
76 safe = r.intersect(fb->getRect());
77 if (!safe.is_empty())
78 compareRect(safe, newChanged);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079 return;
80 }
81
82 int bytesPerPixel = fb->getPF().bpp/8;
83 int oldStride;
84 rdr::U8* oldData = oldFb.getPixelsRW(r, &oldStride);
85 int oldStrideBytes = oldStride * bytesPerPixel;
86
87 std::vector<Rect> changedBlocks;
88
89 for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
90 {
91 // Get a strip of the source buffer
92 Rect pos(r.tl.x, blockTop, r.br.x, __rfbmin(r.br.y, blockTop+BLOCK_SIZE));
93 int fbStride;
94 const rdr::U8* newBlockPtr = fb->getPixelsR(pos, &fbStride);
95 int newStrideBytes = fbStride * bytesPerPixel;
96
97 rdr::U8* oldBlockPtr = oldData;
98 int blockBottom = __rfbmin(blockTop+BLOCK_SIZE, r.br.y);
99
100 for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
101 {
102 const rdr::U8* newPtr = newBlockPtr;
103 rdr::U8* oldPtr = oldBlockPtr;
104
105 int blockRight = __rfbmin(blockLeft+BLOCK_SIZE, r.br.x);
106 int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
107
108 for (int y = blockTop; y < blockBottom; y++)
109 {
110 if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
111 {
112 // A block has changed - copy the remainder to the oldFb
113 changedBlocks.push_back(Rect(blockLeft, blockTop,
114 blockRight, blockBottom));
115 for (int y2 = y; y2 < blockBottom; y2++)
116 {
117 memcpy(oldPtr, newPtr, blockWidthInBytes);
118 newPtr += newStrideBytes;
119 oldPtr += oldStrideBytes;
120 }
121 break;
122 }
123
124 newPtr += newStrideBytes;
125 oldPtr += oldStrideBytes;
126 }
127
128 oldBlockPtr += blockWidthInBytes;
129 newBlockPtr += blockWidthInBytes;
130 }
131
132 oldData += oldStrideBytes * BLOCK_SIZE;
133 }
134
135 if (!changedBlocks.empty()) {
136 Region temp;
137 temp.setOrderedRects(changedBlocks);
138 newChanged->assign_union(temp);
139 }
140}