6060a84fb599fc7fdb45a2887aa0201c806cf2d6
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / buffer_queue.h
1 // Copyright (C) 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ANBOX_GRAPHICS_BUFFER_QUEUE_H_
16 #define ANBOX_GRAPHICS_BUFFER_QUEUE_H_
17
18 #include "anbox/common/small_vector.h"
19
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23
24 namespace anbox {
25 namespace graphics {
26 using Buffer = anbox::common::SmallFixedVector<char, 512>;
27
28 class BufferQueue {
29  public:
30   BufferQueue(size_t capacity);
31
32   bool can_push_locked() const { return !closed_ && (count_ < capacity_); }
33   bool can_pop_locked() const { return count_ > 0U; }
34   bool is_closed_locked() const { return closed_; }
35
36   int wait_until_not_empty_locked(std::unique_lock<std::mutex> &lock);
37
38   int try_push_locked(Buffer &&buffer);
39   int push_locked(Buffer &&buffer, std::unique_lock<std::mutex> &lock);
40   int try_pop_locked(Buffer *buffer);
41   int pop_locked(Buffer *buffer, std::unique_lock<std::mutex> &lock);
42   void close_locked();
43
44  private:
45   size_t capacity_ = 0;
46   size_t pos_ = 0;
47   size_t count_ = 0;
48   bool closed_ = false;
49   std::unique_ptr<Buffer[]> buffers_;
50
51   std::condition_variable can_push_;
52   std::condition_variable can_pop_;
53 };
54
55 }  // namespace graphics
56 }  // namespace anbox
57
58 #endif