Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 | |
| 29 | using namespace rfb; |
| 30 | |
| 31 | static LogWriter vlog("SMsgWriter"); |
| 32 | |
| 33 | SMsgWriter::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 Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 38 | for (int i = 0; i <= encodingMax; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | encoders[i] = 0; |
| 40 | bytesSent[i] = 0; |
| 41 | rectsSent[i] = 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | SMsgWriter::~SMsgWriter() |
| 46 | { |
| 47 | vlog.info("framebuffer updates %d",updatesSent); |
| 48 | int bytes = 0; |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 49 | for (int i = 0; i <= encodingMax; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | 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 | } |
DRC | 887c5fd | 2011-08-19 03:13:47 +0000 | [diff] [blame] | 57 | vlog.info(" raw bytes equivalent %llu, compression ratio %f", |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | rawBytesEquivalent, (double)rawBytesEquivalent / bytes); |
| 59 | delete [] imageBuf; |
| 60 | } |
| 61 | |
| 62 | void 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 | |
| 79 | void SMsgWriter::writeBell() |
| 80 | { |
| 81 | startMsg(msgTypeBell); |
| 82 | endMsg(); |
| 83 | } |
| 84 | |
| 85 | void 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 | |
| 94 | void SMsgWriter::setupCurrentEncoder() |
| 95 | { |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 96 | int encoding = cp->currentEncoding(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 97 | |
| 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); |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 106 | encoders[encoding]->setFineQualityLevel(cp->fineQualityLevel, |
| 107 | cp->subsampling); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int SMsgWriter::getNumRects(const Rect &r) |
| 111 | { |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 112 | int encoding = cp->currentEncoding(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | |
| 114 | if (!encoders[encoding]) |
| 115 | setupCurrentEncoder(); |
| 116 | |
| 117 | return encoders[encoding]->getNumRects(r); |
| 118 | } |
| 119 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 120 | bool SMsgWriter::needFakeUpdate() |
| 121 | { |
| 122 | return false; |
| 123 | } |
| 124 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 125 | bool SMsgWriter::needNoDataUpdate() |
| 126 | { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | void SMsgWriter::writeNoDataUpdate() |
| 131 | { |
| 132 | // This class has no pseudo-rectangles so there is nothing to do here |
| 133 | vlog.error("writeNoDataUpdate() called"); |
| 134 | } |
| 135 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 136 | void SMsgWriter::writeRects(const UpdateInfo& ui, TransImageGetter* ig, |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 137 | 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 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 158 | bool SMsgWriter::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 159 | { |
| 160 | return writeRect(r, cp->currentEncoding(), ig, actual); |
| 161 | } |
| 162 | |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 163 | bool SMsgWriter::writeRect(const Rect& r, int encoding, |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 164 | TransImageGetter* ig, Rect* actual) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 165 | { |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 173 | void 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 | |
| 181 | rdr::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 | |
| 201 | int SMsgWriter::bpp() |
| 202 | { |
| 203 | return cp->pf().bpp; |
| 204 | } |