blob: bdff04b1bec57a0acb9ea510dc7ad1f126e370db [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,
56 const RenderedCursor* renderedCursor);
57
Pierre Ossmanc0397262014-03-14 15:59:46 +010058 protected:
Pierre Ossman6b2f1132016-11-30 08:03:35 +010059 void doUpdate(bool allowLossy, const Region& changed,
60 const Region& copied, const Point& copy_delta,
61 const PixelBuffer* pb,
62 const RenderedCursor* renderedCursor);
63 void prepareEncoders(bool allowLossy);
64
65 Region getLosslessRefresh(const Region& req);
Pierre Ossmanc0397262014-03-14 15:59:46 +010066
67 int computeNumRects(const Region& changed);
68
Pierre Ossman20dd2a92015-02-11 17:43:15 +010069 Encoder *startRect(const Rect& rect, int type);
70 void endRect();
71
Pierre Ossman6b2f1132016-11-30 08:03:35 +010072 void writeCopyRects(const Region& copied, const Point& delta);
Pierre Ossmanc0397262014-03-14 15:59:46 +010073 void writeSolidRects(Region *changed, const PixelBuffer* pb);
Pierre Ossmaneef55162015-02-12 13:44:22 +010074 void findSolidRect(const Rect& rect, Region *changed, const PixelBuffer* pb);
Pierre Ossmanc0397262014-03-14 15:59:46 +010075 void writeRects(const Region& changed, const PixelBuffer* pb);
76
77 void writeSubRect(const Rect& rect, const PixelBuffer *pb);
78
79 bool checkSolidTile(const Rect& r, const rdr::U8* colourValue,
80 const PixelBuffer *pb);
81 void extendSolidAreaByBlock(const Rect& r, const rdr::U8* colourValue,
82 const PixelBuffer *pb, Rect* er);
83 void extendSolidAreaByPixel(const Rect& r, const Rect& sr,
84 const rdr::U8* colourValue,
85 const PixelBuffer *pb, Rect* er);
86
87 PixelBuffer* preparePixelBuffer(const Rect& rect,
88 const PixelBuffer *pb, bool convert);
89
90 bool analyseRect(const PixelBuffer *pb,
91 struct RectInfo *info, int maxColours);
92
93 protected:
94 // Preprocessor generated, optimised methods
95 inline bool checkSolidTile(const Rect& r, rdr::U8 colourValue,
96 const PixelBuffer *pb);
97 inline bool checkSolidTile(const Rect& r, rdr::U16 colourValue,
98 const PixelBuffer *pb);
99 inline bool checkSolidTile(const Rect& r, rdr::U32 colourValue,
100 const PixelBuffer *pb);
101
102 inline bool analyseRect(int width, int height,
103 const rdr::U8* buffer, int stride,
104 struct RectInfo *info, int maxColours);
105 inline bool analyseRect(int width, int height,
106 const rdr::U16* buffer, int stride,
107 struct RectInfo *info, int maxColours);
108 inline bool analyseRect(int width, int height,
109 const rdr::U32* buffer, int stride,
110 struct RectInfo *info, int maxColours);
111
112 protected:
113 SConnection *conn;
114
115 std::vector<Encoder*> encoders;
116 std::vector<int> activeEncoders;
117
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100118 Region lossyRegion;
119
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100120 struct EncoderStats {
121 unsigned rects;
122 unsigned long long bytes;
123 unsigned long long pixels;
124 unsigned long long equivalent;
125 };
126 typedef std::vector< std::vector<struct EncoderStats> > StatsVector;
127
128 unsigned updates;
Pierre Ossmane539cb82015-09-22 11:09:00 +0200129 EncoderStats copyStats;
Pierre Ossman20dd2a92015-02-11 17:43:15 +0100130 StatsVector stats;
131 int activeType;
132 int beforeLength;
133
Pierre Ossmanc0397262014-03-14 15:59:46 +0100134 class OffsetPixelBuffer : public FullFramePixelBuffer {
135 public:
136 OffsetPixelBuffer() {}
137 virtual ~OffsetPixelBuffer() {}
138
139 void update(const PixelFormat& pf, int width, int height,
140 const rdr::U8* data_, int stride);
141 };
142
143 OffsetPixelBuffer offsetPixelBuffer;
144 ManagedPixelBuffer convertedPixelBuffer;
145 };
146}
147
148#endif