Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame^] | 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 | |
| 19 | // -=- rfbUpdateTracker.cpp |
| 20 | // |
| 21 | // Tracks updated regions and a region-copy event, too |
| 22 | // |
| 23 | |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include <rfb/UpdateTracker.h> |
| 27 | #include <rfb/LogWriter.h> |
| 28 | |
| 29 | using namespace rfb; |
| 30 | |
| 31 | static LogWriter vlog("UpdateTracker"); |
| 32 | |
| 33 | |
| 34 | // -=- ClippingUpdateTracker |
| 35 | |
| 36 | void ClippingUpdateTracker::add_changed(const Region ®ion) { |
| 37 | ut->add_changed(region.intersect(clipRect)); |
| 38 | } |
| 39 | |
| 40 | void ClippingUpdateTracker::add_copied(const Region &dest, const Point &delta) { |
| 41 | // Clip the destination to the display area |
| 42 | Region clipdest = dest.intersect(clipRect); |
| 43 | if (clipdest.is_empty()) return; |
| 44 | |
| 45 | // Clip the source to the screen |
| 46 | Region tmp = clipdest; |
| 47 | tmp.translate(delta.negate()); |
| 48 | tmp.assign_intersect(clipRect); |
| 49 | if (!tmp.is_empty()) { |
| 50 | // Translate the source back to a destination region |
| 51 | tmp.translate(delta); |
| 52 | |
| 53 | // Pass the copy region to the child tracker |
| 54 | ut->add_copied(tmp, delta); |
| 55 | } |
| 56 | |
| 57 | // And add any bits that we had to remove to the changed region |
| 58 | tmp = clipdest.subtract(tmp); |
| 59 | if (!tmp.is_empty()) |
| 60 | ut->add_changed(tmp); |
| 61 | } |
| 62 | |
| 63 | // SimpleUpdateTracker |
| 64 | |
| 65 | SimpleUpdateTracker::SimpleUpdateTracker(bool use_copyrect) { |
| 66 | copy_enabled = use_copyrect; |
| 67 | } |
| 68 | |
| 69 | SimpleUpdateTracker::~SimpleUpdateTracker() { |
| 70 | } |
| 71 | |
| 72 | void SimpleUpdateTracker::enable_copyrect(bool enable) { |
| 73 | if (!enable && copy_enabled) { |
| 74 | add_changed(copied); |
| 75 | copied.clear(); |
| 76 | } |
| 77 | copy_enabled=enable; |
| 78 | } |
| 79 | |
| 80 | void SimpleUpdateTracker::add_changed(const Region ®ion) { |
| 81 | changed.assign_union(region); |
| 82 | } |
| 83 | |
| 84 | void SimpleUpdateTracker::add_copied(const Region &dest, const Point &delta) { |
| 85 | // Do we support copyrect? |
| 86 | if (!copy_enabled) { |
| 87 | add_changed(dest); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Is there anything to do? |
| 92 | if (dest.is_empty()) return; |
| 93 | |
| 94 | // Calculate whether any of this copy can be treated as a continuation |
| 95 | // of an earlier one |
| 96 | Region src = dest; |
| 97 | src.translate(delta.negate()); |
| 98 | Region overlap = src.intersect(copied); |
| 99 | |
| 100 | if (overlap.is_empty()) { |
| 101 | // There is no overlap |
| 102 | |
| 103 | Rect newbr = dest.get_bounding_rect(); |
| 104 | Rect oldbr = copied.get_bounding_rect(); |
| 105 | if (oldbr.area() > newbr.area()) { |
| 106 | // Old copyrect is (probably) bigger - use it |
| 107 | changed.assign_union(dest); |
| 108 | } else { |
| 109 | // New copyrect is probably bigger |
| 110 | // Use the new one |
| 111 | // But be careful not to copy stuff that still needs |
| 112 | // to be updated. |
| 113 | Region invalid_src = src.intersect(changed); |
| 114 | invalid_src.translate(delta); |
| 115 | changed.assign_union(invalid_src); |
| 116 | changed.assign_union(copied); |
| 117 | copied = dest; |
| 118 | copy_delta = delta; |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | Region invalid_src = overlap.intersect(changed); |
| 124 | invalid_src.translate(delta); |
| 125 | changed.assign_union(invalid_src); |
| 126 | |
| 127 | overlap.translate(delta); |
| 128 | |
| 129 | Region nonoverlapped_copied = dest.union_(copied).subtract(overlap); |
| 130 | changed.assign_union(nonoverlapped_copied); |
| 131 | |
| 132 | copied = overlap; |
| 133 | copy_delta = copy_delta.translate(delta); |
| 134 | |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | void SimpleUpdateTracker::subtract(const Region& region) { |
| 139 | copied.assign_subtract(region); |
| 140 | changed.assign_subtract(region); |
| 141 | } |
| 142 | |
| 143 | void SimpleUpdateTracker::getUpdateInfo(UpdateInfo* info, const Region& clip) |
| 144 | { |
| 145 | copied.assign_subtract(changed); |
| 146 | info->changed = changed.intersect(clip); |
| 147 | info->copied = copied.intersect(clip); |
| 148 | info->copy_delta = copy_delta; |
| 149 | } |
| 150 | |
| 151 | void SimpleUpdateTracker::copyTo(UpdateTracker* to) const { |
| 152 | if (!copied.is_empty()) |
| 153 | to->add_copied(copied, copy_delta); |
| 154 | if (!changed.is_empty()) |
| 155 | to->add_changed(changed); |
| 156 | } |