blob: bdae9063b7f546355429915f3cd0f4b787163dd4 [file] [log] [blame]
Pierre Ossmanc0397262014-03-14 15:59:46 +01001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
2 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman6b2f1132016-11-30 08:03:35 +01003 * Copyright 2014-2018 Pierre Ossman for Cendio AB
Pierre Ossmanc0397262014-03-14 15:59:46 +01004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20#ifndef __RFB_ENCODEMANAGER_H__
21#define __RFB_ENCODEMANAGER_H__
22
23#include <vector>
24
25#include <rdr/types.h>
26#include <rfb/PixelBuffer.h>
Pierre Ossman6b2f1132016-11-30 08:03:35 +010027#include <rfb/Region.h>
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020028#include <rfb/Timer.h>
Pierre Ossmanc0397262014-03-14 15:59:46 +010029
30namespace rfb {
31 class SConnection;
32 class Encoder;
33 class UpdateInfo;
34 class PixelBuffer;
35 class RenderedCursor;
Steve Kondik1f5d4b12017-07-08 02:00:49 -070036 struct Rect;
Pierre Ossmanc0397262014-03-14 15:59:46 +010037
38 struct RectInfo;
39
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020040 class EncodeManager : public Timer::Callback {
Pierre Ossmanc0397262014-03-14 15:59:46 +010041 public:
42 EncodeManager(SConnection* conn);
43 ~EncodeManager();
44
Pierre Ossman20dd2a92015-02-11 17:43:15 +010045 void logStats();
46
Pierre Ossmanc0397262014-03-14 15:59:46 +010047 // Hack to let ConnParams calculate the client's preferred encoding
48 static bool supported(int encoding);
49
Pierre Ossman6b2f1132016-11-30 08:03:35 +010050 bool needsLosslessRefresh(const Region& req);
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020051 int getNextLosslessRefresh(const Region& req);
52
Pierre Ossman6b2f1132016-11-30 08:03:35 +010053 void pruneLosslessRefresh(const Region& limits);
54
Pierre Ossmanc0397262014-03-14 15:59:46 +010055 void writeUpdate(const UpdateInfo& ui, const PixelBuffer* pb,
56 const RenderedCursor* renderedCursor);
57
Pierre Ossman6b2f1132016-11-30 08:03:35 +010058 void writeLosslessRefresh(const Region& req, const PixelBuffer* pb,
Pierre Ossmana2b80d62018-03-23 09:30:09 +010059 const RenderedCursor* renderedCursor,
60 size_t maxUpdateSize);
Pierre Ossman6b2f1132016-11-30 08:03:35 +010061
Pierre Ossmanc0397262014-03-14 15:59:46 +010062 protected:
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020063 virtual bool handleTimeout(Timer* t);
64
Pierre Ossman6b2f1132016-11-30 08:03:35 +010065 void doUpdate(bool allowLossy, const Region& changed,
66 const Region& copied, const Point& copy_delta,
67 const PixelBuffer* pb,
68 const RenderedCursor* renderedCursor);
69 void prepareEncoders(bool allowLossy);
70
Pierre Ossmana2b80d62018-03-23 09:30:09 +010071 Region getLosslessRefresh(const Region& req, size_t maxUpdateSize);
Pierre Ossmanc0397262014-03-14 15:59:46 +010072
73 int computeNumRects(const Region& changed);
74
Pierre Ossman20dd2a92015-02-11 17:43:15 +010075 Encoder *startRect(const Rect& rect, int type);
76 void endRect();
77
Pierre Ossman6b2f1132016-11-30 08:03:35 +010078 void writeCopyRects(const Region& copied, const Point& delta);
Pierre Ossmanc0397262014-03-14 15:59:46 +010079 void writeSolidRects(Region *changed, const PixelBuffer* pb);
Pierre Ossmaneef55162015-02-12 13:44:22 +010080 void findSolidRect(const Rect& rect, Region *changed, const PixelBuffer* pb);
Pierre Ossmanc0397262014-03-14 15:59:46 +010081 void writeRects(const Region& changed, const PixelBuffer* pb);
82
83 void writeSubRect(const Rect& rect, const PixelBuffer *pb);
84
85 bool checkSolidTile(const Rect& r, const rdr::U8* colourValue,
86 const PixelBuffer *pb);
87 void extendSolidAreaByBlock(const Rect& r, const rdr::U8* colourValue,
88 const PixelBuffer *pb, Rect* er);
89 void extendSolidAreaByPixel(const Rect& r, const Rect& sr,
90 const rdr::U8* colourValue,
91 const PixelBuffer *pb, Rect* er);
92
93 PixelBuffer* preparePixelBuffer(const Rect& rect,
94 const PixelBuffer *pb, bool convert);
95
96 bool analyseRect(const PixelBuffer *pb,
97 struct RectInfo *info, int maxColours);
98
99 protected:
100 // Preprocessor generated, optimised methods
101 inline bool checkSolidTile(const Rect& r, rdr::U8 colourValue,
102 const PixelBuffer *pb);
103 inline bool checkSolidTile(const Rect& r, rdr::U16 colourValue,
104 const PixelBuffer *pb);
105 inline bool checkSolidTile(const Rect& r, rdr::U32 colourValue,
106 const PixelBuffer *pb);
107
108 inline bool analyseRect(int width, int height,
109 const rdr::U8* buffer, int stride,
110 struct RectInfo *info, int maxColours);
111 inline bool analyseRect(int width, int height,
112 const rdr::U16* buffer, int stride,
113 struct RectInfo *info, int maxColours);
114 inline bool analyseRect(int width, int height,
115 const rdr::U32* buffer, int stride,
116 struct RectInfo *info, int maxColours);
117
118 protected:
119 SConnection *conn;
120
121 std::vector<Encoder*> encoders;
122 std::vector<int> activeEncoders;
123
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100124 Region lossyRegion;
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200125 Region recentlyChangedRegion;
126 Region pendingRefreshRegion;
127
128 Timer recentChangeTimer;
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100129
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100130 struct EncoderStats {
131 unsigned rects;
132 unsigned long long bytes;
133 unsigned long long pixels;
134 unsigned long long equivalent;
135 };
136 typedef std::vector< std::vector<struct EncoderStats> > StatsVector;
137
138 unsigned updates;
Pierre Ossmane539cb82015-09-22 11:09:00 +0200139 EncoderStats copyStats;
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100140 StatsVector stats;
141 int activeType;
142 int beforeLength;
143
Pierre Ossmanc0397262014-03-14 15:59:46 +0100144 class OffsetPixelBuffer : public FullFramePixelBuffer {
145 public:
146 OffsetPixelBuffer() {}
147 virtual ~OffsetPixelBuffer() {}
148
149 void update(const PixelFormat& pf, int width, int height,
150 const rdr::U8* data_, int stride);
151 };
152
153 OffsetPixelBuffer offsetPixelBuffer;
154 ManagedPixelBuffer convertedPixelBuffer;
155 };
156}
157
158#endif