Delegate decoder object management to a separate class
Done in preparation for multi-core decoding. Keeps the complexity
out of the other classes. This also moves ownership of the
framebuffer in to CConnection. It's the CConnection object that is
aware of the threads and how to synchronise with them. Therefore
the ownership of the framebuffer must also be there to make sure
it isn't deleted whilst threads are working.
diff --git a/common/rfb/CConnection.cxx b/common/rfb/CConnection.cxx
index 785d305..49b8a82 100644
--- a/common/rfb/CConnection.cxx
+++ b/common/rfb/CConnection.cxx
@@ -40,13 +40,15 @@
CConnection::CConnection()
: csecurity(0), is(0), os(0), reader_(0), writer_(0),
shared(false),
- state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false)
+ state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false),
+ framebuffer(NULL), decoder(this)
{
security = new SecurityClient();
}
CConnection::~CConnection()
{
+ setFramebuffer(NULL);
if (csecurity) csecurity->destroy();
delete reader_;
reader_ = 0;
@@ -60,6 +62,45 @@
os = os_;
}
+void CConnection::setFramebuffer(ModifiablePixelBuffer* fb)
+{
+ if ((framebuffer != NULL) && (fb != NULL)) {
+ Rect rect;
+
+ const rdr::U8* data;
+ int stride;
+
+ const rdr::U8 black[4] = { 0, 0, 0, 0 };
+
+ // Copy still valid area
+
+ rect.setXYWH(0, 0,
+ __rfbmin(fb->width(), framebuffer->width()),
+ __rfbmin(fb->height(), framebuffer->height()));
+ data = framebuffer->getBuffer(framebuffer->getRect(), &stride);
+ fb->imageRect(rect, data, stride);
+
+ // Black out any new areas
+
+ if (fb->width() > framebuffer->width()) {
+ rect.setXYWH(framebuffer->width(), 0,
+ fb->width() - fb->width(),
+ fb->height());
+ fb->fillRect(rect, black);
+ }
+
+ if (fb->height() > framebuffer->height()) {
+ rect.setXYWH(0, framebuffer->height(),
+ fb->width(),
+ fb->height() - framebuffer->height());
+ fb->fillRect(rect, black);
+ }
+ }
+
+ delete framebuffer;
+ framebuffer = fb;
+}
+
void CConnection::initialiseProtocol()
{
state_ = RFBSTATE_PROTOCOL_VERSION;
@@ -260,6 +301,11 @@
writer_->writeClientInit(shared);
}
+void CConnection::dataRect(const Rect& r, int encoding)
+{
+ decoder.decodeRect(r, encoding, framebuffer);
+}
+
void CConnection::authSuccess()
{
}
diff --git a/common/rfb/CConnection.h b/common/rfb/CConnection.h
index 501c761..7f8dbea 100644
--- a/common/rfb/CConnection.h
+++ b/common/rfb/CConnection.h
@@ -24,6 +24,7 @@
#define __RFB_CCONNECTION_H__
#include <rfb/CMsgHandler.h>
+#include <rfb/DecodeManager.h>
#include <rfb/util.h>
namespace rfb {
@@ -64,6 +65,13 @@
// only ever support protocol version 3.3
void setProtocol3_3(bool s) {useProtocol3_3 = s;}
+ // setFramebuffer configures the PixelBuffer that the CConnection
+ // should render all pixel data in to. Note that the CConnection
+ // takes ownership of the PixelBuffer and it must not be deleted by
+ // anyone else. Call setFramebuffer again with NULL or a different
+ // PixelBuffer to delete the previous one.
+ void setFramebuffer(ModifiablePixelBuffer* fb);
+
// initialiseProtocol() should be called once the streams and security
// types are set. Subsequently, processMsg() should be called whenever
// there is data to read on the InStream.
@@ -82,6 +90,11 @@
void processMsg();
+ // Methods overridden from CMsgHandler
+
+ virtual void dataRect(const Rect& r, int encoding);
+
+
// Methods to be overridden in a derived class
// getIdVerifier() returns the identity verifier associated with the connection.
@@ -129,6 +142,8 @@
void setReader(CMsgReader *r) { reader_ = r; }
void setWriter(CMsgWriter *w) { writer_ = w; }
+ ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
+
private:
// This is a default implementation of fences that automatically
// responds to requests, stating no support for synchronisation.
@@ -157,6 +172,9 @@
CharArray serverName;
bool useProtocol3_3;
+
+ ModifiablePixelBuffer* framebuffer;
+ DecodeManager decoder;
};
}
#endif
diff --git a/common/rfb/CMakeLists.txt b/common/rfb/CMakeLists.txt
index 14b8b72..5047e5e 100644
--- a/common/rfb/CMakeLists.txt
+++ b/common/rfb/CMakeLists.txt
@@ -15,6 +15,7 @@
ConnParams.cxx
CopyRectDecoder.cxx
Cursor.cxx
+ DecodeManager.cxx
Decoder.cxx
d3des.c
EncodeManager.cxx
diff --git a/common/rfb/DecodeManager.cxx b/common/rfb/DecodeManager.cxx
new file mode 100644
index 0000000..f6ffa4b
--- /dev/null
+++ b/common/rfb/DecodeManager.cxx
@@ -0,0 +1,63 @@
+/* Copyright 2015 Pierre Ossman for Cendio AB
+ *
+ * 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 <assert.h>
+#include <string.h>
+
+#include <rfb/DecodeManager.h>
+#include <rfb/Decoder.h>
+
+#include <rfb/LogWriter.h>
+
+#include <rdr/Exception.h>
+
+using namespace rfb;
+
+static LogWriter vlog("DecodeManager");
+
+DecodeManager::DecodeManager(CConnection *conn) :
+ conn(conn)
+{
+ memset(decoders, 0, sizeof(decoders));
+}
+
+DecodeManager::~DecodeManager()
+{
+ for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
+ delete decoders[i];
+}
+
+void DecodeManager::decodeRect(const Rect& r, int encoding,
+ ModifiablePixelBuffer* pb)
+{
+ assert(pb != NULL);
+
+ if (!Decoder::supported(encoding)) {
+ vlog.error("Unknown encoding %d", encoding);
+ throw rdr::Exception("Unknown encoding");
+ }
+
+ if (!decoders[encoding]) {
+ decoders[encoding] = Decoder::createDecoder(encoding, conn);
+ if (!decoders[encoding]) {
+ vlog.error("Unknown encoding %d", encoding);
+ throw rdr::Exception("Unknown encoding");
+ }
+ }
+ decoders[encoding]->readRect(r, pb);
+}
diff --git a/common/rfb/DecodeManager.h b/common/rfb/DecodeManager.h
new file mode 100644
index 0000000..445601d
--- /dev/null
+++ b/common/rfb/DecodeManager.h
@@ -0,0 +1,44 @@
+/* Copyright 2015 Pierre Ossman for Cendio AB
+ *
+ * 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.
+ */
+
+#ifndef __RFB_DECODEMANAGER_H__
+#define __RFB_DECODEMANAGER_H__
+
+#include <rfb/encodings.h>
+
+namespace rfb {
+ class CConnection;
+ class Decoder;
+ class ModifiablePixelBuffer;
+ class Rect;
+
+ class DecodeManager {
+ public:
+ DecodeManager(CConnection *conn);
+ ~DecodeManager();
+
+ void decodeRect(const Rect& r, int encoding,
+ ModifiablePixelBuffer* pb);
+
+ private:
+ CConnection *conn;
+ Decoder *decoders[encodingMax+1];
+ };
+}
+
+#endif