aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/DecodeManager.h
blob: 5435bfc1799ae1fc593fb150536d575bf1106e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright 2015 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifndef __RFB_DECODEMANAGER_H__
#define __RFB_DECODEMANAGER_H__

#include <list>

#include <os/Thread.h>

#include <rfb/Region.h>
#include <rfb/encodings.h>

namespace os {
  class Condition;
  class Mutex;
}

namespace rdr {
  struct Exception;
  class MemOutStream;
}

namespace rfb {
  class CConnection;
  class Decoder;
  class ModifiablePixelBuffer;
  struct Rect;

  class DecodeManager {
  public:
    DecodeManager(CConnection *conn);
    ~DecodeManager();

    bool decodeRect(const Rect& r, int encoding,
                    ModifiablePixelBuffer* pb);

    void flush();

  private:
    void logStats();

    void setThreadException(const rdr::Exception& e);
    void throwThreadException();

  private:
    CConnection *conn;
    Decoder *decoders[encodingMax+1];

    struct DecoderStats {
      unsigned rects;
      unsigned long long bytes;
      unsigned long long pixels;
      unsigned long long equivalent;
    };

    DecoderStats stats[encodingMax+1];

    struct QueueEntry {
      bool active;
      Rect rect;
      int encoding;
      Decoder* decoder;
      const ServerParams* server;
      ModifiablePixelBuffer* pb;
      rdr::MemOutStream* bufferStream;
      Region affectedRegion;
    };

    std::list<rdr::MemOutStream*> freeBuffers;
    std::list<QueueEntry*> workQueue;

    os::Mutex* queueMutex;
    os::Condition* producerCond;
    os::Condition* consumerCond;

  private:
    class DecodeThread : public os::Thread {
    public:
      DecodeThread(DecodeManager* manager);
      ~DecodeThread();

      void stop();

    protected:
      void worker() override;
      DecodeManager::QueueEntry* findEntry();

    private:
      DecodeManager* manager;

      bool stopRequested;
    };

    std::list<DecodeThread*> threads;
    rdr::Exception *threadException;
  };
}

#endif