blob: a91c544eec21765a918696552b2cf814000fcf54 [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>
Pierre Ossmanc0397262014-03-14 15:59:46 +010028
29namespace rfb {
30 class SConnection;
31 class Encoder;
32 class UpdateInfo;
33 class PixelBuffer;
34 class RenderedCursor;
Steve Kondik1f5d4b12017-07-08 02:00:49 -070035 struct Rect;
Pierre Ossmanc0397262014-03-14 15:59:46 +010036
37 struct RectInfo;
38
39 class EncodeManager {
40 public:
41 EncodeManager(SConnection* conn);
42 ~EncodeManager();
43
Pierre Ossman20dd2a92015-02-11 17:43:15 +010044 void logStats();
45
Pierre Ossmanc0397262014-03-14 15:59:46 +010046 // Hack to let ConnParams calculate the client's preferred encoding
47 static bool supported(int encoding);
48
Pierre Ossman6b2f1132016-11-30 08:03:35 +010049 bool needsLosslessRefresh(const Region& req);
50 void pruneLosslessRefresh(const Region& limits);
51
Pierre Ossmanc0397262014-03-14 15:59:46 +010052 void writeUpdate(const UpdateInfo& ui, const PixelBuffer* pb,
53 const RenderedCursor* renderedCursor);
54
Pierre Ossman6b2f1132016-11-30 08:03:35 +010055 void writeLosslessRefresh(const Region& req, const PixelBuffer* pb,
Pierre Ossmana2b80d62018-03-23 09:30:09 +010056 const RenderedCursor* renderedCursor,
57 size_t maxUpdateSize);
Pierre Ossman6b2f1132016-11-30 08:03:35 +010058
Pierre Ossmanc0397262014-03-14 15:59:46 +010059 protected:
Pierre Ossman6b2f1132016-11-30 08:03:35 +010060 void doUpdate(bool allowLossy, const Region& changed,
61 const Region& copied, const Point& copy_delta,
62 const PixelBuffer* pb,
63 const RenderedCursor* renderedCursor);
64 void prepareEncoders(bool allowLossy);
65
Pierre Ossmana2b80d62018-03-23 09:30:09 +010066 Region getLosslessRefresh(const Region& req, size_t maxUpdateSize);
Pierre Ossmanc0397262014-03-14 15:59:46 +010067
68 int computeNumRects(const Region& changed);
69
Pierre Ossman20dd2a92015-02-11 17:43:15 +010070 Encoder *startRect(const Rect& rect, int type);
71 void endRect();
72
Pierre Ossman6b2f1132016-11-30 08:03:35 +010073 void writeCopyRects(const Region& copied, const Point& delta);
Pierre Ossmanc0397262014-03-14 15:59:46 +010074 void writeSolidRects(Region *changed, const PixelBuffer* pb);
Pierre Ossmaneef55162015-02-12 13:44:22 +010075 void findSolidRect(const Rect& rect, Region *changed, const PixelBuffer* pb);
Pierre Ossmanc0397262014-03-14 15:59:46 +010076 void writeRects(const Region& changed, const PixelBuffer* pb);
77
78 void writeSubRect(const Rect& rect, const PixelBuffer *pb);
79
80 bool checkSolidTile(const Rect& r, const rdr::U8* colourValue,
81 const PixelBuffer *pb);
82 void extendSolidAreaByBlock(const Rect& r, const rdr::U8* colourValue,
83 const PixelBuffer *pb, Rect* er);
84 void extendSolidAreaByPixel(const Rect& r, const Rect& sr,
85 const rdr::U8* colourValue,
86 const PixelBuffer *pb, Rect* er);
87
88 PixelBuffer* preparePixelBuffer(const Rect& rect,
89 const PixelBuffer *pb, bool convert);
90
91 bool analyseRect(const PixelBuffer *pb,
92 struct RectInfo *info, int maxColours);
93
94 protected:
95 // Preprocessor generated, optimised methods
96 inline bool checkSolidTile(const Rect& r, rdr::U8 colourValue,
97 const PixelBuffer *pb);
98 inline bool checkSolidTile(const Rect& r, rdr::U16 colourValue,
99 const PixelBuffer *pb);
100 inline bool checkSolidTile(const Rect& r, rdr::U32 colourValue,
101 const PixelBuffer *pb);
102
103 inline bool analyseRect(int width, int height,
104 const rdr::U8* buffer, int stride,
105 struct RectInfo *info, int maxColours);
106 inline bool analyseRect(int width, int height,
107 const rdr::U16* buffer, int stride,
108 struct RectInfo *info, int maxColours);
109 inline bool analyseRect(int width, int height,
110 const rdr::U32* buffer, int stride,
111 struct RectInfo *info, int maxColours);
112
113 protected:
114 SConnection *conn;
115
116 std::vector<Encoder*> encoders;
117 std::vector<int> activeEncoders;
118
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100119 Region lossyRegion;
120
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100121 struct EncoderStats {
122 unsigned rects;
123 unsigned long long bytes;
124 unsigned long long pixels;
125 unsigned long long equivalent;
126 };
127 typedef std::vector< std::vector<struct EncoderStats> > StatsVector;
128
129 unsigned updates;
Pierre Ossmane539cb82015-09-22 11:09:00 +0200130 EncoderStats copyStats;
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100131 StatsVector stats;
132 int activeType;
133 int beforeLength;
134
Pierre Ossmanc0397262014-03-14 15:59:46 +0100135 class OffsetPixelBuffer : public FullFramePixelBuffer {
136 public:
137 OffsetPixelBuffer() {}
138 virtual ~OffsetPixelBuffer() {}
139
140 void update(const PixelFormat& pf, int width, int height,
141 const rdr::U8* data_, int stride);
142 };
143
144 OffsetPixelBuffer offsetPixelBuffer;
145 ManagedPixelBuffer convertedPixelBuffer;
146 };
147}
148
149#endif