You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EncodeManager.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2014-2018 Pierre Ossman for Cendio AB
  4. *
  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. #include <vector>
  23. #include <rdr/types.h>
  24. #include <rfb/PixelBuffer.h>
  25. #include <rfb/Region.h>
  26. #include <rfb/Timer.h>
  27. namespace rfb {
  28. class SConnection;
  29. class Encoder;
  30. class UpdateInfo;
  31. class PixelBuffer;
  32. class RenderedCursor;
  33. struct Rect;
  34. struct RectInfo;
  35. class EncodeManager : public Timer::Callback {
  36. public:
  37. EncodeManager(SConnection* conn);
  38. ~EncodeManager();
  39. void logStats();
  40. // Hack to let ConnParams calculate the client's preferred encoding
  41. static bool supported(int encoding);
  42. bool needsLosslessRefresh(const Region& req);
  43. int getNextLosslessRefresh(const Region& req);
  44. void pruneLosslessRefresh(const Region& limits);
  45. void writeUpdate(const UpdateInfo& ui, const PixelBuffer* pb,
  46. const RenderedCursor* renderedCursor);
  47. void writeLosslessRefresh(const Region& req, const PixelBuffer* pb,
  48. const RenderedCursor* renderedCursor,
  49. size_t maxUpdateSize);
  50. protected:
  51. virtual bool handleTimeout(Timer* t);
  52. void doUpdate(bool allowLossy, const Region& changed,
  53. const Region& copied, const Point& copy_delta,
  54. const PixelBuffer* pb,
  55. const RenderedCursor* renderedCursor);
  56. void prepareEncoders(bool allowLossy);
  57. Region getLosslessRefresh(const Region& req, size_t maxUpdateSize);
  58. int computeNumRects(const Region& changed);
  59. Encoder *startRect(const Rect& rect, int type);
  60. void endRect();
  61. void writeCopyRects(const Region& copied, const Point& delta);
  62. void writeSolidRects(Region *changed, const PixelBuffer* pb);
  63. void findSolidRect(const Rect& rect, Region *changed, const PixelBuffer* pb);
  64. void writeRects(const Region& changed, const PixelBuffer* pb);
  65. void writeSubRect(const Rect& rect, const PixelBuffer *pb);
  66. bool checkSolidTile(const Rect& r, const rdr::U8* colourValue,
  67. const PixelBuffer *pb);
  68. void extendSolidAreaByBlock(const Rect& r, const rdr::U8* colourValue,
  69. const PixelBuffer *pb, Rect* er);
  70. void extendSolidAreaByPixel(const Rect& r, const Rect& sr,
  71. const rdr::U8* colourValue,
  72. const PixelBuffer *pb, Rect* er);
  73. PixelBuffer* preparePixelBuffer(const Rect& rect,
  74. const PixelBuffer *pb, bool convert);
  75. bool analyseRect(const PixelBuffer *pb,
  76. struct RectInfo *info, int maxColours);
  77. protected:
  78. // Preprocessor generated, optimised methods
  79. inline bool checkSolidTile(const Rect& r, rdr::U8 colourValue,
  80. const PixelBuffer *pb);
  81. inline bool checkSolidTile(const Rect& r, rdr::U16 colourValue,
  82. const PixelBuffer *pb);
  83. inline bool checkSolidTile(const Rect& r, rdr::U32 colourValue,
  84. const PixelBuffer *pb);
  85. inline bool analyseRect(int width, int height,
  86. const rdr::U8* buffer, int stride,
  87. struct RectInfo *info, int maxColours);
  88. inline bool analyseRect(int width, int height,
  89. const rdr::U16* buffer, int stride,
  90. struct RectInfo *info, int maxColours);
  91. inline bool analyseRect(int width, int height,
  92. const rdr::U32* buffer, int stride,
  93. struct RectInfo *info, int maxColours);
  94. protected:
  95. SConnection *conn;
  96. std::vector<Encoder*> encoders;
  97. std::vector<int> activeEncoders;
  98. Region lossyRegion;
  99. Region recentlyChangedRegion;
  100. Region pendingRefreshRegion;
  101. Timer recentChangeTimer;
  102. struct EncoderStats {
  103. unsigned rects;
  104. unsigned long long bytes;
  105. unsigned long long pixels;
  106. unsigned long long equivalent;
  107. };
  108. typedef std::vector< std::vector<struct EncoderStats> > StatsVector;
  109. unsigned updates;
  110. EncoderStats copyStats;
  111. StatsVector stats;
  112. int activeType;
  113. int beforeLength;
  114. class OffsetPixelBuffer : public FullFramePixelBuffer {
  115. public:
  116. OffsetPixelBuffer() {}
  117. virtual ~OffsetPixelBuffer() {}
  118. void update(const PixelFormat& pf, int width, int height,
  119. const rdr::U8* data_, int stride);
  120. private:
  121. virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
  122. };
  123. OffsetPixelBuffer offsetPixelBuffer;
  124. ManagedPixelBuffer convertedPixelBuffer;
  125. };
  126. }
  127. #endif