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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright (C) 2011 D. R. Commander. 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.
*/
#include <stdio.h>
#include <assert.h>
#include <rdr/OutStream.h>
#include <rfb/msgTypes.h>
#include <rfb/ColourMap.h>
#include <rfb/ConnParams.h>
#include <rfb/UpdateTracker.h>
#include <rfb/SMsgWriter.h>
#include <rfb/LogWriter.h>
using namespace rfb;
static LogWriter vlog("SMsgWriter");
SMsgWriter::SMsgWriter(ConnParams* cp_, rdr::OutStream* os_)
: imageBufIdealSize(0), cp(cp_), os(os_), lenBeforeRect(0),
currentEncoding(0), updatesSent(0), rawBytesEquivalent(0),
imageBuf(0), imageBufSize(0)
{
for (int i = 0; i <= encodingMax; i++) {
encoders[i] = 0;
bytesSent[i] = 0;
rectsSent[i] = 0;
}
}
SMsgWriter::~SMsgWriter()
{
vlog.info("framebuffer updates %d",updatesSent);
int bytes = 0;
for (int i = 0; i <= encodingMax; i++) {
delete encoders[i];
if (i != encodingCopyRect)
bytes += bytesSent[i];
if (rectsSent[i])
vlog.info(" %s rects %d, bytes %d",
encodingName(i), rectsSent[i], bytesSent[i]);
}
vlog.info(" raw bytes equivalent %llu, compression ratio %f",
rawBytesEquivalent, (double)rawBytesEquivalent / bytes);
delete [] imageBuf;
}
void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
ColourMap* cm)
{
startMsg(msgTypeSetColourMapEntries);
os->pad(1);
os->writeU16(firstColour);
os->writeU16(nColours);
for (int i = firstColour; i < firstColour+nColours; i++) {
int r, g, b;
cm->lookup(i, &r, &g, &b);
os->writeU16(r);
os->writeU16(g);
os->writeU16(b);
}
endMsg();
}
void SMsgWriter::writeBell()
{
startMsg(msgTypeBell);
endMsg();
}
void SMsgWriter::writeServerCutText(const char* str, int len)
{
startMsg(msgTypeServerCutText);
os->pad(3);
os->writeU32(len);
os->writeBytes(str, len);
endMsg();
}
void SMsgWriter::setupCurrentEncoder()
{
int encoding = cp->currentEncoding();
// FIXME: Code duplication, see writeRect().
if (!encoders[encoding]) {
encoders[encoding] = Encoder::createEncoder(encoding, this);
assert(encoders[encoding]);
}
encoders[encoding]->setCompressLevel(cp->compressLevel);
encoders[encoding]->setQualityLevel(cp->qualityLevel);
encoders[encoding]->setFineQualityLevel(cp->fineQualityLevel,
cp->subsampling);
}
int SMsgWriter::getNumRects(const Rect &r)
{
int encoding = cp->currentEncoding();
if (!encoders[encoding])
setupCurrentEncoder();
return encoders[encoding]->getNumRects(r);
}
bool SMsgWriter::needFakeUpdate()
{
return false;
}
bool SMsgWriter::needNoDataUpdate()
{
return false;
}
void SMsgWriter::writeNoDataUpdate()
{
// This class has no pseudo-rectangles so there is nothing to do here
vlog.error("writeNoDataUpdate() called");
}
void SMsgWriter::writeRects(const UpdateInfo& ui, TransImageGetter* ig,
Region* updatedRegion)
{
std::vector<Rect> rects;
std::vector<Rect>::const_iterator i;
updatedRegion->copyFrom(ui.changed);
updatedRegion->assign_union(ui.copied);
ui.copied.get_rects(&rects, ui.copy_delta.x <= 0, ui.copy_delta.y <= 0);
for (i = rects.begin(); i != rects.end(); i++)
writeCopyRect(*i, i->tl.x - ui.copy_delta.x, i->tl.y - ui.copy_delta.y);
ui.changed.get_rects(&rects);
for (i = rects.begin(); i != rects.end(); i++) {
Rect actual;
if (!writeRect(*i, ig, &actual)) {
updatedRegion->assign_subtract(*i);
updatedRegion->assign_union(actual);
}
}
}
bool SMsgWriter::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual)
{
return writeRect(r, cp->currentEncoding(), ig, actual);
}
bool SMsgWriter::writeRect(const Rect& r, int encoding,
TransImageGetter* ig, Rect* actual)
{
if (!encoders[encoding]) {
encoders[encoding] = Encoder::createEncoder(encoding, this);
assert(encoders[encoding]);
}
return encoders[encoding]->writeRect(r, ig, actual);
}
void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
{
startRect(r,encodingCopyRect);
os->writeU16(srcX);
os->writeU16(srcY);
endRect();
}
rdr::U8* SMsgWriter::getImageBuf(int required, int requested, int* nPixels)
{
int requiredBytes = required * (cp->pf().bpp / 8);
int requestedBytes = requested * (cp->pf().bpp / 8);
int size = requestedBytes;
if (size > imageBufIdealSize) size = imageBufIdealSize;
if (size < requiredBytes)
size = requiredBytes;
if (imageBufSize < size) {
imageBufSize = size;
delete [] imageBuf;
imageBuf = new rdr::U8[imageBufSize];
}
if (nPixels)
*nPixels = imageBufSize / (cp->pf().bpp / 8);
return imageBuf;
}
int SMsgWriter::bpp()
{
return cp->pf().bpp;
}
|