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 <memory> 20 #include <mutex> 21 #include <thread> 22 #include <utility> 23 #include <vector> 24 25 #include "common/libs/concurrency/thread_annotations.h" 26 #include "host/frontend/vnc_server/blackboard.h" 27 #include "host/frontend/vnc_server/jpeg_compressor.h" 28 #include "host/frontend/vnc_server/simulated_hw_composer.h" 29 #include "host/libs/screen_connector/screen_connector.h" 30 31 namespace cuttlefish { 32 namespace vnc { 33 class FrameBufferWatcher { 34 public: 35 explicit FrameBufferWatcher(BlackBoard* bb, 36 ScreenConnector& screen_connector); 37 FrameBufferWatcher(const FrameBufferWatcher&) = delete; 38 FrameBufferWatcher& operator=(const FrameBufferWatcher&) = delete; 39 ~FrameBufferWatcher(); 40 41 StripePtrVec StripesNewerThan(ScreenOrientation orientation, 42 const SeqNumberVec& seq_num) const; 43 void IncClientCount(); 44 void DecClientCount(); 45 46 static int StripesPerFrame(); 47 48 private: 49 static Stripe Rotated(Stripe stripe); 50 51 bool closed() const; 52 bool StripeIsDifferentFromPrevious(const Stripe& stripe) const 53 REQUIRES(stripes_lock_); 54 // returns true if stripe is still considered new and seq number was updated 55 bool UpdateMostRecentSeqNumIfStripeIsNew(const Stripe& stripe) 56 REQUIRES(stripes_lock_); 57 // returns true if stripe is still considered new and was updated 58 bool UpdateStripeIfStripeIsNew(const std::shared_ptr<const Stripe>& stripe) 59 EXCLUDES(stripes_lock_); 60 // Compresses stripe->raw_data to stripe->jpeg_data 61 void CompressStripe(JpegCompressor* jpeg_compressor, Stripe* stripe); 62 void Worker(); 63 void Updater(); 64 65 StripePtrVec& Stripes(ScreenOrientation orientation) REQUIRES(stripes_lock_); 66 const StripePtrVec& Stripes(ScreenOrientation orientation) const 67 REQUIRES(stripes_lock_); 68 69 std::vector<std::thread> workers_; 70 mutable std::mutex stripes_lock_; 71 std::array<StripePtrVec, kNumOrientations> stripes_ GUARDED_BY(stripes_lock_); 72 SeqNumberVec most_recent_identical_stripe_seq_nums_ 73 GUARDED_BY(stripes_lock_) = MakeSeqNumberVec(); 74 mutable std::mutex m_; GUARDED_BY(m_)75 bool closed_ GUARDED_BY(m_){}; 76 BlackBoard* bb_{}; 77 SimulatedHWComposer hwcomposer; 78 }; 79 80 } // namespace vnc 81 } // namespace cuttlefish 82