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