blob: ac74382678af1fcf191ba1fb8355390ce05b0d39 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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>
19#include <assert.h>
20#include <rdr/OutStream.h>
21#include <rfb/msgTypes.h>
22#include <rfb/ColourMap.h>
23#include <rfb/ConnParams.h>
24#include <rfb/UpdateTracker.h>
25#include <rfb/SMsgWriter.h>
26#include <rfb/LogWriter.h>
27
28using namespace rfb;
29
30static LogWriter vlog("SMsgWriter");
31
32SMsgWriter::SMsgWriter(ConnParams* cp_, rdr::OutStream* os_)
33 : imageBufIdealSize(0), cp(cp_), os(os_), lenBeforeRect(0),
34 currentEncoding(0), updatesSent(0), rawBytesEquivalent(0),
35 imageBuf(0), imageBufSize(0)
36{
37 for (unsigned int i = 0; i <= encodingMax; i++) {
38 encoders[i] = 0;
39 bytesSent[i] = 0;
40 rectsSent[i] = 0;
41 }
42}
43
44SMsgWriter::~SMsgWriter()
45{
46 vlog.info("framebuffer updates %d",updatesSent);
47 int bytes = 0;
48 for (unsigned int i = 0; i <= encodingMax; i++) {
49 delete encoders[i];
50 if (i != encodingCopyRect)
51 bytes += bytesSent[i];
52 if (rectsSent[i])
53 vlog.info(" %s rects %d, bytes %d",
54 encodingName(i), rectsSent[i], bytesSent[i]);
55 }
56 vlog.info(" raw bytes equivalent %d, compression ratio %f",
57 rawBytesEquivalent, (double)rawBytesEquivalent / bytes);
58 delete [] imageBuf;
59}
60
61void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
62 ColourMap* cm)
63{
64 startMsg(msgTypeSetColourMapEntries);
65 os->pad(1);
66 os->writeU16(firstColour);
67 os->writeU16(nColours);
68 for (int i = firstColour; i < firstColour+nColours; i++) {
69 int r, g, b;
70 cm->lookup(i, &r, &g, &b);
71 os->writeU16(r);
72 os->writeU16(g);
73 os->writeU16(b);
74 }
75 endMsg();
76}
77
78void SMsgWriter::writeBell()
79{
80 startMsg(msgTypeBell);
81 endMsg();
82}
83
84void SMsgWriter::writeServerCutText(const char* str, int len)
85{
86 startMsg(msgTypeServerCutText);
87 os->pad(3);
88 os->writeU32(len);
89 os->writeBytes(str, len);
90 endMsg();
91}
92
93void SMsgWriter::writeFramebufferUpdate(const UpdateInfo& ui, ImageGetter* ig,
94 Region* updatedRegion)
95{
96 writeFramebufferUpdateStart(ui.numRects());
97 writeRects(ui, ig, updatedRegion);
98 writeFramebufferUpdateEnd();
99}
100
101void SMsgWriter::writeRects(const UpdateInfo& ui, ImageGetter* ig,
102 Region* updatedRegion)
103{
104 std::vector<Rect> rects;
105 std::vector<Rect>::const_iterator i;
106 updatedRegion->copyFrom(ui.changed);
107 updatedRegion->assign_union(ui.copied);
108
109 ui.copied.get_rects(&rects, ui.copy_delta.x <= 0, ui.copy_delta.y <= 0);
110 for (i = rects.begin(); i != rects.end(); i++)
111 writeCopyRect(*i, i->tl.x - ui.copy_delta.x, i->tl.y - ui.copy_delta.y);
112
113 ui.changed.get_rects(&rects);
114 for (i = rects.begin(); i != rects.end(); i++) {
115 Rect actual;
116 if (!writeRect(*i, ig, &actual)) {
117 updatedRegion->assign_subtract(*i);
118 updatedRegion->assign_union(actual);
119 }
120 }
121}
122
123
124bool SMsgWriter::needFakeUpdate()
125{
126 return false;
127}
128
129bool SMsgWriter::writeRect(const Rect& r, ImageGetter* ig, Rect* actual)
130{
131 return writeRect(r, cp->currentEncoding(), ig, actual);
132}
133
134bool SMsgWriter::writeRect(const Rect& r, unsigned int encoding,
135 ImageGetter* ig, Rect* actual)
136{
137 if (!encoders[encoding]) {
138 encoders[encoding] = Encoder::createEncoder(encoding, this);
139 assert(encoders[encoding]);
140 }
141 return encoders[encoding]->writeRect(r, ig, actual);
142}
143
144void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
145{
146 startRect(r,encodingCopyRect);
147 os->writeU16(srcX);
148 os->writeU16(srcY);
149 endRect();
150}
151
152void SMsgWriter::setOutStream(rdr::OutStream* os_)
153{
154 os = os_;
155}
156
157rdr::U8* SMsgWriter::getImageBuf(int required, int requested, int* nPixels)
158{
159 int requiredBytes = required * (cp->pf().bpp / 8);
160 int requestedBytes = requested * (cp->pf().bpp / 8);
161 int size = requestedBytes;
162 if (size > imageBufIdealSize) size = imageBufIdealSize;
163
164 if (size < requiredBytes)
165 size = requiredBytes;
166
167 if (imageBufSize < size) {
168 imageBufSize = size;
169 delete [] imageBuf;
170 imageBuf = new rdr::U8[imageBufSize];
171 }
172 if (nPixels)
173 *nPixels = imageBufSize / (cp->pf().bpp / 8);
174 return imageBuf;
175}
176
177int SMsgWriter::bpp()
178{
179 return cp->pf().bpp;
180}