1 #pragma once
2 
3 /*
4  * Copyright (C) 2017 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <condition_variable>
20 #include <mutex>
21 #ifdef FUZZ_TEST_VNC
22 #include <random>
23 #endif
24 #include <thread>
25 
26 #include "common/libs/thread_safe_queue/thread_safe_queue.h"
27 #include "common/libs/threads/thread_annotations.h"
28 #include "host/frontend/vnc_server/blackboard.h"
29 #include "host/libs/screen_connector/screen_connector.h"
30 
31 namespace cvd {
32 namespace vnc {
33 class SimulatedHWComposer {
34  public:
35   SimulatedHWComposer(BlackBoard* bb);
36   SimulatedHWComposer(const SimulatedHWComposer&) = delete;
37   SimulatedHWComposer& operator=(const SimulatedHWComposer&) = delete;
38   ~SimulatedHWComposer();
39 
40   Stripe GetNewStripe();
41 
42   // NOTE not constexpr on purpose
43   static int NumberOfStripes();
44 
45  private:
46   bool closed();
47   void close();
48   static void EraseHalfOfElements(ThreadSafeQueue<Stripe>::QueueImpl* q);
49   void MakeStripes();
50 
51 #ifdef FUZZ_TEST_VNC
52   std::default_random_engine engine_;
53   std::uniform_int_distribution<int> random_ =
54       std::uniform_int_distribution<int>{0, 2};
55 #endif
56   static constexpr int kNumStripes = 8;
57   constexpr static std::size_t kMaxQueueElements = 64;
GUARDED_BY(m_)58   bool closed_ GUARDED_BY(m_){};
59   std::mutex m_;
60   BlackBoard* bb_{};
61   ThreadSafeQueue<Stripe> stripes_;
62   std::thread stripe_maker_;
63   std::shared_ptr<ScreenConnector> screen_connector_;
64 };
65 }  // namespace vnc
66 }  // namespace cvd
67