blob: 671e6ee8543c45275b28f47bfeaf6c3725f9a4ad [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 Kaplinsky1a845d02007-08-31 21:06:53 +000042 // First of all, exclude video area from both changed and copied regions.
43 // We handle video area separately and do not compare it -- we know it's
44 // being changed continuously.
45 if (!video_area.is_empty()) {
46 changed.assign_subtract(video_area);
47 copied.assign_subtract(video_area);
48 }
49
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050 std::vector<Rect> rects;
51 std::vector<Rect>::iterator i;
52
53 if (firstCompare) {
54 // NB: We leave the change region untouched on this iteration,
55 // since in effect the entire framebuffer has changed.
56 oldFb.setSize(fb->width(), fb->height());
57 for (int y=0; y<fb->height(); y+=BLOCK_SIZE) {
58 Rect pos(0, y, fb->width(), __rfbmin(fb->height(), y+BLOCK_SIZE));
59 int srcStride;
60 const rdr::U8* srcData = fb->getPixelsR(pos, &srcStride);
61 oldFb.imageRect(pos, srcData, srcStride);
62 }
63 firstCompare = false;
64 } else {
65 copied.get_rects(&rects, copy_delta.x<=0, copy_delta.y<=0);
66 for (i = rects.begin(); i != rects.end(); i++)
67 oldFb.copyRect(*i, copy_delta);
68
69 Region to_check = changed.union_(copied);
70 to_check.get_rects(&rects);
71
72 Region newChanged;
73 for (i = rects.begin(); i != rects.end(); i++)
74 compareRect(*i, &newChanged);
75
76 copied.assign_subtract(newChanged);
77 changed = newChanged;
78 }
79}
80
81void ComparingUpdateTracker::compareRect(const Rect& r, Region* newChanged)
82{
83 if (!r.enclosed_by(fb->getRect())) {
84 fprintf(stderr,"ComparingUpdateTracker: rect outside fb (%d,%d-%d,%d)\n", r.tl.x, r.tl.y, r.br.x, r.br.y);
85 return;
86 }
87
88 int bytesPerPixel = fb->getPF().bpp/8;
89 int oldStride;
90 rdr::U8* oldData = oldFb.getPixelsRW(r, &oldStride);
91 int oldStrideBytes = oldStride * bytesPerPixel;
92
93 std::vector<Rect> changedBlocks;
94
95 for (int blockTop = r.tl.y; blockTop < r.br.y; blockTop += BLOCK_SIZE)
96 {
97 // Get a strip of the source buffer
98 Rect pos(r.tl.x, blockTop, r.br.x, __rfbmin(r.br.y, blockTop+BLOCK_SIZE));
99 int fbStride;
100 const rdr::U8* newBlockPtr = fb->getPixelsR(pos, &fbStride);
101 int newStrideBytes = fbStride * bytesPerPixel;
102
103 rdr::U8* oldBlockPtr = oldData;
104 int blockBottom = __rfbmin(blockTop+BLOCK_SIZE, r.br.y);
105
106 for (int blockLeft = r.tl.x; blockLeft < r.br.x; blockLeft += BLOCK_SIZE)
107 {
108 const rdr::U8* newPtr = newBlockPtr;
109 rdr::U8* oldPtr = oldBlockPtr;
110
111 int blockRight = __rfbmin(blockLeft+BLOCK_SIZE, r.br.x);
112 int blockWidthInBytes = (blockRight-blockLeft) * bytesPerPixel;
113
114 for (int y = blockTop; y < blockBottom; y++)
115 {
116 if (memcmp(oldPtr, newPtr, blockWidthInBytes) != 0)
117 {
118 // A block has changed - copy the remainder to the oldFb
119 changedBlocks.push_back(Rect(blockLeft, blockTop,
120 blockRight, blockBottom));
121 for (int y2 = y; y2 < blockBottom; y2++)
122 {
123 memcpy(oldPtr, newPtr, blockWidthInBytes);
124 newPtr += newStrideBytes;
125 oldPtr += oldStrideBytes;
126 }
127 break;
128 }
129
130 newPtr += newStrideBytes;
131 oldPtr += oldStrideBytes;
132 }
133
134 oldBlockPtr += blockWidthInBytes;
135 newBlockPtr += blockWidthInBytes;
136 }
137
138 oldData += oldStrideBytes * BLOCK_SIZE;
139 }
140
141 if (!changedBlocks.empty()) {
142 Region temp;
143 temp.setOrderedRects(changedBlocks);
144 newChanged->assign_union(temp);
145 }
146}