rfbUpdateTracker.cpp
6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
// Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check http://www.realvnc.com/ or contact
// the authors on info@realvnc.com for information on obtaining it.
// -=- rfbUpdateTracker.cpp
//
// Tracks updated regions and a region-copy event, too
//
#include "stdhdrs.h"
#include <assert.h>
#include "rfbUpdateTracker.h"
using namespace rfb;
// ClippedUpdateTracker
void ClippedUpdateTracker::add_changed(const Region2D ®ion) {
child.add_changed(region.intersect(cliprgn));
}
void ClippedUpdateTracker::add_cached(const Region2D ®ion) {
child.add_cached(region.intersect(cliprgn));
}
void ClippedUpdateTracker::add_copied(const Region2D &dest, const Point &delta) {
// Clip the destination to the display area
Region2D tmp = dest.intersect(cliprgn);
if (tmp.is_empty()) return;
// Clip the source to the screen
tmp.translate(delta.negate());
tmp = tmp.intersect(cliprgn);
if (!tmp.is_empty()) {
// Translate the source back to a destination region
tmp.translate(delta);
// Pass the copy region to the child tracker
child.add_copied(tmp, delta);
}
// And add any bits that we had to remove to the changed region
tmp = dest.subtract(tmp);
if (!tmp.is_empty()) {
child.add_changed(tmp);
}
}
// SimpleUpdateTracker
SimpleUpdateTracker::SimpleUpdateTracker(bool use_copyrect) {
copy_enabled = use_copyrect;
}
SimpleUpdateTracker::~SimpleUpdateTracker() {
}
void SimpleUpdateTracker::add_changed(const Region2D ®ion) {
changed = changed.union_(region);
}
void SimpleUpdateTracker::add_cached(const Region2D ®ion) {
cached = cached.union_(region);
}
void SimpleUpdateTracker::add_copied(const Region2D &dest, const Point &delta) {
// Do we support copyrect?
if (!copy_enabled) {
add_changed(dest);
return;
}
// Is there anything to do?
if (dest.is_empty()) return;
// Calculate whether any of this copy can be treated as a continuation
// of an earlier one
Region2D src = dest;
src.translate(delta.negate());
Region2D overlap = src.intersect(copied);
if (overlap.is_empty()) {
// There is no overlap
Rect newbr = dest.get_bounding_rect();
Rect oldbr = copied.get_bounding_rect();
if (oldbr.area() > newbr.area() && !copied.is_empty()) {
// Old copyrect is (probably) bigger - use it
changed = changed.union_(dest);
} else {
// New copyrect is probably bigger
// RealVNC 336 change
Region2D invalid = src.intersect(changed);
invalid.translate(delta);
changed = changed.union_(invalid).union_(copied);
copied = dest.subtract(invalid);
copy_delta = delta;
/*
// Use the new one
// But be careful not to copy stuff that still needs
// to be updated.
Region2D invalid_src = src.intersect(changed);
invalid_src.translate(delta);
changed = changed.union_(invalid_src).union_(copied);
copied = dest;
copy_delta = delta;
*/
}
return;
}
// RealVNC 336 change
Region2D valid = overlap.subtract(changed);
valid.translate(delta);
changed = changed.union_(copied).union_(dest).subtract(valid);
copied = valid;
copy_delta = copy_delta.translate(delta);
/*
Region2D invalid_src = overlap.intersect(changed);
invalid_src.translate(delta.negate());
changed = changed.union_(invalid_src);
overlap.translate(delta);
Region2D nonoverlapped_copied = dest.union_(copied).subtract(overlap);
changed = changed.union_(nonoverlapped_copied);
copied = overlap;
copy_delta = copy_delta.translate(delta);
*/
return;
}
void SimpleUpdateTracker::flush_update(UpdateInfo &info, const Region2D &cliprgn) {
copied = copied.subtract(changed);
// Ensure the UpdateInfo structure is empty
info.copied.clear();
info.changed.clear();
info.cached.clear();
// Clip the changed region to the clip region
Region2D updatergn = changed.intersect(cliprgn);
changed = changed.subtract(updatergn);
// Clip the copyrect region to the display
Region2D copyrgn = copied.intersect(cliprgn);
copied = copied.subtract(copyrgn);
// Clip the cacherect region to the display
Region2D cachedrgn = cached.intersect(cliprgn);
cached = cached.subtract(cachedrgn);
// Save the update and copyrect rectangles info the UpdateInfo
updatergn.get_rects(info.changed, 1, 1);
cachedrgn.get_rects(info.cached, 1, 1);
copyrgn.get_rects(info.copied, copy_delta.x <= 0, copy_delta.y <= 0);
info.copy_delta = copy_delta;
}
void SimpleUpdateTracker::flush_update(UpdateTracker &info, const Region2D &cliprgn) {
Region2D copied_clipped = copied.intersect(cliprgn);
Region2D changed_clipped = changed.intersect(cliprgn);
Region2D cached_clipped = cached.intersect(cliprgn);
copied = copied.subtract(copied_clipped);
changed = changed.subtract(changed_clipped);
cached = cached.subtract(cached_clipped);
if (!copied_clipped.is_empty()) {
info.add_copied(copied_clipped, copy_delta);
}
if (!changed_clipped.is_empty())
info.add_changed(changed_clipped);
if (!cached_clipped.is_empty())
info.add_cached(cached_clipped);
}
void SimpleUpdateTracker::get_update(UpdateInfo &info) const {
info.copied.clear();
info.changed.clear();
info.cached.clear();
info.copy_delta = copy_delta;
Region2D copied_dest = copied.subtract(changed);
copied_dest.get_rects(info.copied, copy_delta.x <= 0, copy_delta.y <= 0);
changed.get_rects(info.changed, 1, 1);
cached.get_rects(info.cached, 1, 1);
}
void SimpleUpdateTracker::get_update(UpdateTracker &to) const {
if (!copied.is_empty()) {
to.add_copied(copied, copy_delta);
}
if (!changed.is_empty()) {
to.add_changed(changed);
}
if (!cached.is_empty()) {
to.add_cached(cached);
}
}