1 /* Copyright 2015 Pierre Ossman for Cendio AB
2  *
3  * This is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This software is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this software; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
16  * USA.
17  */
18 
19 #ifndef __RFB_DECODEMANAGER_H__
20 #define __RFB_DECODEMANAGER_H__
21 
22 #include <list>
23 
24 #include <os/Thread.h>
25 
26 #include <rfb/Region.h>
27 #include <rfb/encodings.h>
28 
29 namespace os {
30   class Condition;
31   class Mutex;
32 }
33 
34 namespace rdr {
35   struct Exception;
36   class MemOutStream;
37 }
38 
39 namespace rfb {
40   class CConnection;
41   class Decoder;
42   class ModifiablePixelBuffer;
43   struct Rect;
44 
45   class DecodeManager {
46   public:
47     DecodeManager(CConnection *conn);
48     ~DecodeManager();
49 
50     bool decodeRect(const Rect& r, int encoding,
51                     ModifiablePixelBuffer* pb);
52 
53     void flush();
54 
55   private:
56     void setThreadException(const rdr::Exception& e);
57     void throwThreadException();
58 
59   private:
60     CConnection *conn;
61     Decoder *decoders[encodingMax+1];
62 
63     struct QueueEntry {
64       bool active;
65       Rect rect;
66       int encoding;
67       Decoder* decoder;
68       const ServerParams* server;
69       ModifiablePixelBuffer* pb;
70       rdr::MemOutStream* bufferStream;
71       Region affectedRegion;
72     };
73 
74     std::list<rdr::MemOutStream*> freeBuffers;
75     std::list<QueueEntry*> workQueue;
76 
77     os::Mutex* queueMutex;
78     os::Condition* producerCond;
79     os::Condition* consumerCond;
80 
81   private:
82     class DecodeThread : public os::Thread {
83     public:
84       DecodeThread(DecodeManager* manager);
85       ~DecodeThread();
86 
87       void stop();
88 
89     protected:
90       void worker();
91       DecodeManager::QueueEntry* findEntry();
92 
93     private:
94       DecodeManager* manager;
95 
96       bool stopRequested;
97     };
98 
99     std::list<DecodeThread*> threads;
100     rdr::Exception *threadException;
101   };
102 }
103 
104 #endif
105