467dded274c24c6fb81ad6e750ebf5284174c8a1
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / buffered_io_stream.h
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #ifndef ANBOX_GRAPHICS_BUFFERED_IO_STREAM_H_
19 #define ANBOX_GRAPHICS_BUFFERED_IO_STREAM_H_
20
21 #include "external/android-emugl/host/include/libOpenglRender/IOStream.h"
22
23 #include "anbox/graphics/buffer_queue.h"
24 #include "anbox/network/socket_messenger.h"
25
26 #include <memory>
27 #include <thread>
28
29 namespace anbox {
30 namespace graphics {
31 class BufferedIOStream : public IOStream {
32  public:
33   static const size_t default_buffer_size{384};
34
35   explicit BufferedIOStream(
36       const std::shared_ptr<anbox::network::SocketMessenger> &messenger,
37       size_t buffer_size = default_buffer_size);
38
39   virtual ~BufferedIOStream();
40
41   void *allocBuffer(size_t min_size) override;
42   size_t commitBuffer(size_t size) override;
43   const unsigned char *read(void *buf, size_t *inout_len) override;
44   void forceStop() override;
45   void post_data(Buffer &&data);
46
47   bool needs_data();
48
49  private:
50   void thread_main();
51
52   std::shared_ptr<anbox::network::SocketMessenger> messenger_;
53   std::mutex lock_;
54   std::mutex out_lock_;
55   Buffer write_buffer_;
56   Buffer read_buffer_;
57   size_t read_buffer_left_ = 0;
58   BufferQueue in_queue_;
59   BufferQueue out_queue_;
60   std::thread worker_thread_;
61 };
62 }  // namespace graphics
63 }  // namespace anbox
64
65 #endif