• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #pragma once
17 
18 #include "base/MemStream.h"
19 #include "base/Optional.h"
20 #include "host-common/address_space_graphics_types.h"
21 #include "base/ConditionVariable.h"
22 #include "base/Lock.h"
23 #include "base/Thread.h"
24 
25 #include <atomic>
26 #include <memory>
27 
28 namespace emugl {
29 
30 class RenderChannelImpl;
31 class RendererImpl;
32 class ReadBuffer;
33 class RingStream;
34 
35 // A class used to model a thread of the RenderServer. Each one of them
36 // handles a single guest client / protocol byte stream.
37 class RenderThread : public android::base::Thread {
38     using MemStream = android::base::MemStream;
39 
40 public:
41     // Create a new RenderThread instance.
42     RenderThread(RenderChannelImpl* channel,
43                  android::base::Stream* loadStream = nullptr);
44 
45     // Create a new RenderThread instance tied to the address space device.
46     RenderThread(
47         struct asg_context context,
48         android::base::Stream* loadStream,
49         android::emulation::asg::ConsumerCallbacks callbacks);
50     virtual ~RenderThread();
51 
52     // Returns true iff the thread has finished.
isFinished()53     bool isFinished() const { return mFinished.load(std::memory_order_relaxed); }
54 
55     void pausePreSnapshot();
56     void resume();
57     void save(android::base::Stream* stream);
58 
59 private:
60     virtual intptr_t main();
61     void setFinished();
62 
63     // Snapshot support.
64     enum class SnapshotState {
65         Empty,
66         StartSaving,
67         StartLoading,
68         InProgress,
69         Finished,
70     };
71 
72     // Whether using RenderChannel or a ring buffer.
73     enum TransportMode {
74         Channel,
75         Ring,
76     };
77 
78     template <class OpImpl>
79     void snapshotOperation(android::base::AutoLock* lock, OpImpl&& impl);
80 
81     struct SnapshotObjects;
82 
83     bool doSnapshotOperation(const SnapshotObjects& objects,
84                              SnapshotState operation);
85     void waitForSnapshotCompletion(android::base::AutoLock* lock);
86     void loadImpl(android::base::AutoLock* lock, const SnapshotObjects& objects);
87     void saveImpl(android::base::AutoLock* lock, const SnapshotObjects& objects);
88 
89     bool isPausedForSnapshotLocked() const;
90 
91     RenderChannelImpl* mChannel = nullptr;
92     std::unique_ptr<RingStream> mRingStream;
93 
94     SnapshotState mState = SnapshotState::Empty;
95     std::atomic<bool> mFinished { false };
96     android::base::Lock mLock;
97     android::base::ConditionVariable mCondVar;
98     android::base::Optional<android::base::MemStream> mStream;
99 
100     bool mRunInLimitedMode = false;
101 };
102 
103 }  // namespace emugl
104