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 uint32_t virtioGpuContextId = -1); 45 46 // Create a new RenderThread instance tied to the address space device. 47 RenderThread( 48 struct asg_context context, 49 android::base::Stream* loadStream, 50 android::emulation::asg::ConsumerCallbacks callbacks, 51 uint32_t contextId, uint32_t capsetId, 52 std::optional<std::string> nameOpt); 53 virtual ~RenderThread(); 54 55 // Returns true iff the thread has finished. isFinished()56 bool isFinished() const { return mFinished.load(std::memory_order_relaxed); } 57 58 void pausePreSnapshot(); 59 void resume(bool waitForSave); 60 void save(android::base::Stream* stream); 61 62 private: 63 virtual intptr_t main(); 64 void setFinished(); 65 66 // Snapshot support. 67 enum class SnapshotState { 68 Empty, 69 StartSaving, 70 StartLoading, 71 InProgress, 72 Finished, 73 }; 74 75 // Whether using RenderChannel or a ring buffer. 76 enum TransportMode { 77 Channel, 78 Ring, 79 }; 80 81 template <class OpImpl> 82 void snapshotOperation(android::base::AutoLock* lock, OpImpl&& impl); 83 84 struct SnapshotObjects; 85 86 bool doSnapshotOperation(const SnapshotObjects& objects, 87 SnapshotState operation); 88 void waitForSnapshotCompletion(android::base::AutoLock* lock); 89 void loadImpl(android::base::AutoLock* lock, const SnapshotObjects& objects); 90 void saveImpl(android::base::AutoLock* lock, const SnapshotObjects& objects); 91 92 bool isPausedForSnapshotLocked() const; 93 94 RenderChannelImpl* mChannel = nullptr; 95 std::unique_ptr<RingStream> mRingStream; 96 97 SnapshotState mState = SnapshotState::Empty; 98 std::atomic<bool> mFinished { false }; 99 android::base::Lock mLock; 100 android::base::ConditionVariable mCondVar; 101 android::base::Optional<android::base::MemStream> mStream; 102 103 bool mRunInLimitedMode = false; 104 uint32_t mContextId = 0; 105 uint32_t mCapsetId = 0; 106 // If we need to reload process resources. 107 // This happens in snapshot testing where we don't snapshot render threads. 108 bool mNeedReloadProcessResources = false; 109 }; 110 111 } // namespace gfxstream 112