Automatic lossless refresh

Resend pixel perfect copies of areas that were previously sent
using a lossy encoder. This is done when there is no normal update
to send, and no congestion.
diff --git a/common/rfb/EncodeManager.h b/common/rfb/EncodeManager.h
index 4319b42..bdff04b 100644
--- a/common/rfb/EncodeManager.h
+++ b/common/rfb/EncodeManager.h
@@ -1,6 +1,6 @@
 /* Copyright (C) 2000-2003 Constantin Kaplinsky.  All Rights Reserved.
  * Copyright (C) 2011 D. R. Commander.  All Rights Reserved.
- * Copyright 2014 Pierre Ossman for Cendio AB
+ * Copyright 2014-2018 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
@@ -24,6 +24,7 @@
 
 #include <rdr/types.h>
 #include <rfb/PixelBuffer.h>
+#include <rfb/Region.h>
 
 namespace rfb {
   class SConnection;
@@ -31,7 +32,6 @@
   class UpdateInfo;
   class PixelBuffer;
   class RenderedCursor;
-  class Region;
   struct Rect;
 
   struct RectInfo;
@@ -46,18 +46,30 @@
     // Hack to let ConnParams calculate the client's preferred encoding
     static bool supported(int encoding);
 
+    bool needsLosslessRefresh(const Region& req);
+    void pruneLosslessRefresh(const Region& limits);
+
     void writeUpdate(const UpdateInfo& ui, const PixelBuffer* pb,
                      const RenderedCursor* renderedCursor);
 
+    void writeLosslessRefresh(const Region& req, const PixelBuffer* pb,
+                              const RenderedCursor* renderedCursor);
+
   protected:
-    void prepareEncoders();
+    void doUpdate(bool allowLossy, const Region& changed,
+                  const Region& copied, const Point& copy_delta,
+                  const PixelBuffer* pb,
+                  const RenderedCursor* renderedCursor);
+    void prepareEncoders(bool allowLossy);
+
+    Region getLosslessRefresh(const Region& req);
 
     int computeNumRects(const Region& changed);
 
     Encoder *startRect(const Rect& rect, int type);
     void endRect();
 
-    void writeCopyRects(const UpdateInfo& ui);
+    void writeCopyRects(const Region& copied, const Point& delta);
     void writeSolidRects(Region *changed, const PixelBuffer* pb);
     void findSolidRect(const Rect& rect, Region *changed, const PixelBuffer* pb);
     void writeRects(const Region& changed, const PixelBuffer* pb);
@@ -103,6 +115,8 @@
     std::vector<Encoder*> encoders;
     std::vector<int> activeEncoders;
 
+    Region lossyRegion;
+
     struct EncoderStats {
       unsigned rects;
       unsigned long long bytes;