• 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 "aemu/base/files/MemStream.h"
19 #include "aemu/base/Optional.h"
20 #include "host-common/address_space_graphics_types.h"
21 #include "aemu/base/synchronization/ConditionVariable.h"
22 #include "aemu/base/synchronization/Lock.h"
23 #include "aemu/base/threads/Thread.h"
24 
25 #include <atomic>
26 #include <memory>
27 
28 namespace gfxstream {
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         uint32_t contextId, uint32_t capsetId,
51         std::optional<std::string> nameOpt);
52     virtual ~RenderThread();
53 
54     // Returns true iff the thread has finished.
isFinished()55     bool isFinished() const { return mFinished.load(std::memory_order_relaxed); }
56 
57     void pausePreSnapshot();
58     void resume();
59     void save(android::base::Stream* stream);
60 
61 private:
62     virtual intptr_t main();
63     void setFinished();
64 
65     // Snapshot support.
66     enum class SnapshotState {
67         Empty,
68         StartSaving,
69         StartLoading,
70         InProgress,
71         Finished,
72     };
73 
74     // Whether using RenderChannel or a ring buffer.
75     enum TransportMode {
76         Channel,
77         Ring,
78     };
79 
80     template <class OpImpl>
81     void snapshotOperation(android::base::AutoLock* lock, OpImpl&& impl);
82 
83     struct SnapshotObjects;
84 
85     bool doSnapshotOperation(const SnapshotObjects& objects,
86                              SnapshotState operation);
87     void waitForSnapshotCompletion(android::base::AutoLock* lock);
88     void loadImpl(android::base::AutoLock* lock, const SnapshotObjects& objects);
89     void saveImpl(android::base::AutoLock* lock, const SnapshotObjects& objects);
90 
91     bool isPausedForSnapshotLocked() const;
92 
93     RenderChannelImpl* mChannel = nullptr;
94     std::unique_ptr<RingStream> mRingStream;
95 
96     SnapshotState mState = SnapshotState::Empty;
97     std::atomic<bool> mFinished { false };
98     android::base::Lock mLock;
99     android::base::ConditionVariable mCondVar;
100     android::base::Optional<android::base::MemStream> mStream;
101 
102     bool mRunInLimitedMode = false;
103     uint32_t mCapsetId = 0;
104     uint32_t mContextId = 0;
105 };
106 
107 }  // namespace gfxstream
108