summaryrefslogtreecommitdiffstats
path: root/common/rfb/UpdateTracker.cxx
blob: 14ac49d786239edd5de0d5b569faab404b7ae0f8 (plain)
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
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This 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 software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- rfbUpdateTracker.cpp
//
// Tracks updated regions and a region-copy event, too
//

#include <assert.h>

#include <rfb/UpdateTracker.h>
#include <rfb/LogWriter.h>

using namespace rfb;

static LogWriter vlog("UpdateTracker");


// -=- ClippingUpdateTracker

void ClippingUpdateTracker::add_changed(const Region &region) {
  ut->add_changed(region.intersect(clipRect));
}

void ClippingUpdateTracker::add_copied(const Region &dest, const Point &delta) {
  // Clip the destination to the display area
  Region clipdest = dest.intersect(clipRect);
  if (clipdest.is_empty())  return;

  // Clip the source to the screen
  Region tmp = clipdest;
  tmp.translate(delta.negate());
  tmp.assign_intersect(clipRect);
  if (!tmp.is_empty()) {
    // Translate the source back to a destination region
    tmp.translate(delta);

    // Pass the copy region to the child tracker
    ut->add_copied(tmp, delta);
  }

  // And add any bits that we had to remove to the changed region
  tmp = clipdest.subtract(tmp);
  if (!tmp.is_empty())
    ut->add_changed(tmp);
}

// SimpleUpdateTracker

SimpleUpdateTracker::SimpleUpdateTracker(bool use_copyrect) {
  copy_enabled = use_copyrect;
}

SimpleUpdateTracker::~SimpleUpdateTracker() {
}

void SimpleUpdateTracker::enable_copyrect(bool enable) {
  if (!enable && copy_enabled) {
    add_changed(copied);
    copied.clear();
  }
  copy_enabled=enable;
}

void SimpleUpdateTracker::add_changed(const Region &region) {
  changed.assign_union(region);
}

void SimpleUpdateTracker::add_copied(const Region &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
  Region src = dest;
  src.translate(delta.negate());
  Region 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()) {
      // Old copyrect is (probably) bigger - use it
      changed.assign_union(dest);
    } else {
      // New copyrect is probably bigger
      // Use the new one
      // But be careful not to copy stuff that still needs
      // to be updated.
      Region invalid_src = src.intersect(changed);
      invalid_src.translate(delta);
      changed.assign_union(invalid_src);
      changed.assign_union(copied);
      copied = dest;
      copy_delta = delta;
    }
    return;
  }

  Region invalid_src = overlap.intersect(changed);
  invalid_src.translate(delta);
  changed.assign_union(invalid_src);
  
  overlap.translate(delta);

  Region nonoverlapped_copied = dest.union_(copied).subtract(overlap);
  changed.assign_union(nonoverlapped_copied);

  copied = overlap;
  copy_delta = copy_delta.translate(delta);

  return;
}

void SimpleUpdateTracker::subtract(const Region& region) {
  copied.assign_subtract(region);
  changed.assign_subtract(region);
}

void SimpleUpdateTracker::getUpdateInfo(UpdateInfo* info, const Region& clip)
{
  copied.assign_subtract(changed);
  info->changed = changed.intersect(clip);
  info->copied = copied.intersect(clip);
  info->copy_delta = copy_delta;
}

void SimpleUpdateTracker::copyTo(UpdateTracker* to) const {
  if (!copied.is_empty())
    to->add_copied(copied, copy_delta);
  if (!changed.is_empty())
    to->add_changed(changed);
}