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 #include <deque> 26 27 #include "common/libs/concurrency/thread_annotations.h" 28 #include "common/libs/concurrency/thread_safe_queue.h" 29 #include "host/frontend/vnc_server/blackboard.h" 30 #include "host/frontend/vnc_server/vnc_utils.h" 31 #include "host/libs/config/cuttlefish_config.h" 32 #include "host/libs/screen_connector/screen_connector.h" 33 34 namespace cuttlefish { 35 namespace vnc { 36 class SimulatedHWComposer { 37 public: 38 using GenerateProcessedFrameCallback = ScreenConnector::GenerateProcessedFrameCallback; 39 40 SimulatedHWComposer(BlackBoard* bb, ScreenConnector& screen_connector); 41 SimulatedHWComposer(const SimulatedHWComposer&) = delete; 42 SimulatedHWComposer& operator=(const SimulatedHWComposer&) = delete; 43 ~SimulatedHWComposer(); 44 45 Stripe GetNewStripe(); 46 47 void ReportClientsConnected(); 48 49 // NOTE not constexpr on purpose 50 static int NumberOfStripes(); 51 52 private: 53 bool closed(); 54 void close(); 55 static void EraseHalfOfElements(ThreadSafeQueue<Stripe>::QueueImpl* q); 56 void MakeStripes(); 57 GenerateProcessedFrameCallback GetScreenConnectorCallback(); 58 59 #ifdef FUZZ_TEST_VNC 60 std::default_random_engine engine_; 61 std::uniform_int_distribution<int> random_ = 62 std::uniform_int_distribution<int>{0, 2}; 63 #endif 64 static constexpr int kNumStripes = 8; 65 constexpr static std::size_t kMaxQueueElements = 64; GUARDED_BY(m_)66 bool closed_ GUARDED_BY(m_){}; 67 std::mutex m_; 68 BlackBoard* bb_{}; 69 ThreadSafeQueue<Stripe> stripes_; 70 std::thread stripe_maker_; 71 ScreenConnector& screen_connector_; 72 }; 73 } // namespace vnc 74 } // namespace cuttlefish 75