blob: 509ffbf526c76f0d593f098056a35b9c0aa90160 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRCb4a83232011-08-19 04:57:18 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19#include <stdio.h>
20#include <assert.h>
21#include <rdr/OutStream.h>
22#include <rfb/msgTypes.h>
23#include <rfb/ColourMap.h>
24#include <rfb/ConnParams.h>
25#include <rfb/UpdateTracker.h>
26#include <rfb/SMsgWriter.h>
27#include <rfb/LogWriter.h>
28
29using namespace rfb;
30
31static LogWriter vlog("SMsgWriter");
32
33SMsgWriter::SMsgWriter(ConnParams* cp_, rdr::OutStream* os_)
34 : imageBufIdealSize(0), cp(cp_), os(os_), lenBeforeRect(0),
35 currentEncoding(0), updatesSent(0), rawBytesEquivalent(0),
36 imageBuf(0), imageBufSize(0)
37{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000038 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039 encoders[i] = 0;
40 bytesSent[i] = 0;
41 rectsSent[i] = 0;
42 }
43}
44
45SMsgWriter::~SMsgWriter()
46{
47 vlog.info("framebuffer updates %d",updatesSent);
48 int bytes = 0;
Peter Åstrand98fe98c2010-02-10 07:43:02 +000049 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050 delete encoders[i];
51 if (i != encodingCopyRect)
52 bytes += bytesSent[i];
53 if (rectsSent[i])
54 vlog.info(" %s rects %d, bytes %d",
55 encodingName(i), rectsSent[i], bytesSent[i]);
56 }
DRC887c5fd2011-08-19 03:13:47 +000057 vlog.info(" raw bytes equivalent %llu, compression ratio %f",
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058 rawBytesEquivalent, (double)rawBytesEquivalent / bytes);
59 delete [] imageBuf;
60}
61
62void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
63 ColourMap* cm)
64{
65 startMsg(msgTypeSetColourMapEntries);
66 os->pad(1);
67 os->writeU16(firstColour);
68 os->writeU16(nColours);
69 for (int i = firstColour; i < firstColour+nColours; i++) {
70 int r, g, b;
71 cm->lookup(i, &r, &g, &b);
72 os->writeU16(r);
73 os->writeU16(g);
74 os->writeU16(b);
75 }
76 endMsg();
77}
78
79void SMsgWriter::writeBell()
80{
81 startMsg(msgTypeBell);
82 endMsg();
83}
84
85void SMsgWriter::writeServerCutText(const char* str, int len)
86{
87 startMsg(msgTypeServerCutText);
88 os->pad(3);
89 os->writeU32(len);
90 os->writeBytes(str, len);
91 endMsg();
92}
93
94void SMsgWriter::setupCurrentEncoder()
95{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000096 int encoding = cp->currentEncoding();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097
98 // FIXME: Code duplication, see writeRect().
99 if (!encoders[encoding]) {
100 encoders[encoding] = Encoder::createEncoder(encoding, this);
101 assert(encoders[encoding]);
102 }
103
104 encoders[encoding]->setCompressLevel(cp->compressLevel);
105 encoders[encoding]->setQualityLevel(cp->qualityLevel);
DRCb4a83232011-08-19 04:57:18 +0000106 encoders[encoding]->setFineQualityLevel(cp->fineQualityLevel,
107 cp->subsampling);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108}
109
110int SMsgWriter::getNumRects(const Rect &r)
111{
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000112 int encoding = cp->currentEncoding();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113
114 if (!encoders[encoding])
115 setupCurrentEncoder();
116
117 return encoders[encoding]->getNumRects(r);
118}
119
Pierre Ossmane9962f72009-04-23 12:31:42 +0000120bool SMsgWriter::needFakeUpdate()
121{
122 return false;
123}
124
Pierre Ossmane9962f72009-04-23 12:31:42 +0000125bool SMsgWriter::needNoDataUpdate()
126{
127 return false;
128}
129
130void SMsgWriter::writeNoDataUpdate()
131{
132 // This class has no pseudo-rectangles so there is nothing to do here
133 vlog.error("writeNoDataUpdate() called");
134}
135
DRCffe09d62011-08-17 02:27:59 +0000136void SMsgWriter::writeRects(const UpdateInfo& ui, TransImageGetter* ig,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 Region* updatedRegion)
138{
139 std::vector<Rect> rects;
140 std::vector<Rect>::const_iterator i;
141 updatedRegion->copyFrom(ui.changed);
142 updatedRegion->assign_union(ui.copied);
143
144 ui.copied.get_rects(&rects, ui.copy_delta.x <= 0, ui.copy_delta.y <= 0);
145 for (i = rects.begin(); i != rects.end(); i++)
146 writeCopyRect(*i, i->tl.x - ui.copy_delta.x, i->tl.y - ui.copy_delta.y);
147
148 ui.changed.get_rects(&rects);
149 for (i = rects.begin(); i != rects.end(); i++) {
150 Rect actual;
151 if (!writeRect(*i, ig, &actual)) {
152 updatedRegion->assign_subtract(*i);
153 updatedRegion->assign_union(actual);
154 }
155 }
156}
157
DRCffe09d62011-08-17 02:27:59 +0000158bool SMsgWriter::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159{
160 return writeRect(r, cp->currentEncoding(), ig, actual);
161}
162
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000163bool SMsgWriter::writeRect(const Rect& r, int encoding,
DRCffe09d62011-08-17 02:27:59 +0000164 TransImageGetter* ig, Rect* actual)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165{
166 if (!encoders[encoding]) {
167 encoders[encoding] = Encoder::createEncoder(encoding, this);
168 assert(encoders[encoding]);
169 }
170 return encoders[encoding]->writeRect(r, ig, actual);
171}
172
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
174{
175 startRect(r,encodingCopyRect);
176 os->writeU16(srcX);
177 os->writeU16(srcY);
178 endRect();
179}
180
181rdr::U8* SMsgWriter::getImageBuf(int required, int requested, int* nPixels)
182{
183 int requiredBytes = required * (cp->pf().bpp / 8);
184 int requestedBytes = requested * (cp->pf().bpp / 8);
185 int size = requestedBytes;
186 if (size > imageBufIdealSize) size = imageBufIdealSize;
187
188 if (size < requiredBytes)
189 size = requiredBytes;
190
191 if (imageBufSize < size) {
192 imageBufSize = size;
193 delete [] imageBuf;
194 imageBuf = new rdr::U8[imageBufSize];
195 }
196 if (nPixels)
197 *nPixels = imageBufSize / (cp->pf().bpp / 8);
198 return imageBuf;
199}
200
201int SMsgWriter::bpp()
202{
203 return cp->pf().bpp;
204}