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/threads/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 
30 namespace cuttlefish {
31 namespace vnc {
32 class FrameBufferWatcher {
33  public:
34   explicit FrameBufferWatcher(BlackBoard* bb);
35   FrameBufferWatcher(const FrameBufferWatcher&) = delete;
36   FrameBufferWatcher& operator=(const FrameBufferWatcher&) = delete;
37   ~FrameBufferWatcher();
38 
39   StripePtrVec StripesNewerThan(ScreenOrientation orientation,
40                                 const SeqNumberVec& seq_num) const;
41   static int StripesPerFrame();
42 
43  private:
44   static Stripe Rotated(Stripe stripe);
45 
46   bool closed() const;
47   bool StripeIsDifferentFromPrevious(const Stripe& stripe) const
48       REQUIRES(stripes_lock_);
49   // returns true if stripe is still considered new and seq number was updated
50   bool UpdateMostRecentSeqNumIfStripeIsNew(const Stripe& stripe)
51       REQUIRES(stripes_lock_);
52   // returns true if stripe is still considered new and was updated
53   bool UpdateStripeIfStripeIsNew(const std::shared_ptr<const Stripe>& stripe)
54       EXCLUDES(stripes_lock_);
55   // Compresses stripe->raw_data to stripe->jpeg_data
56   void CompressStripe(JpegCompressor* jpeg_compressor, Stripe* stripe);
57   void Worker();
58   void Updater();
59 
60   StripePtrVec& Stripes(ScreenOrientation orientation) REQUIRES(stripes_lock_);
61   const StripePtrVec& Stripes(ScreenOrientation orientation) const
62       REQUIRES(stripes_lock_);
63 
64   std::vector<std::thread> workers_;
65   mutable std::mutex stripes_lock_;
66   std::array<StripePtrVec, kNumOrientations> stripes_ GUARDED_BY(stripes_lock_);
67   SeqNumberVec most_recent_identical_stripe_seq_nums_
68       GUARDED_BY(stripes_lock_) = MakeSeqNumberVec();
69   mutable std::mutex m_;
GUARDED_BY(m_)70   bool closed_ GUARDED_BY(m_){};
71   BlackBoard* bb_{};
72   SimulatedHWComposer hwcomposer{bb_};
73 };
74 
75 }  // namespace vnc
76 }  // namespace cuttlefish
77