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 17 #include "RenderThreadInfo.h" 18 19 #include "aemu/base/synchronization/Lock.h" 20 #include "FrameBuffer.h" 21 22 #include <unordered_map> 23 #include <unordered_set> 24 25 namespace gfxstream { 26 27 using android::base::AutoLock; 28 using android::base::Stream; 29 using android::base::Lock; 30 31 static thread_local RenderThreadInfo* s_threadInfoPtr; 32 33 struct RenderThreadRegistry { 34 Lock lock; 35 std::unordered_set<RenderThreadInfo*> threadInfos; 36 }; 37 38 static RenderThreadRegistry sRegistry; 39 RenderThreadInfo()40RenderThreadInfo::RenderThreadInfo() { 41 s_threadInfoPtr = this; 42 AutoLock lock(sRegistry.lock); 43 sRegistry.threadInfos.insert(this); 44 } 45 ~RenderThreadInfo()46RenderThreadInfo::~RenderThreadInfo() { 47 s_threadInfoPtr = nullptr; 48 AutoLock lock(sRegistry.lock); 49 sRegistry.threadInfos.erase(this); 50 } 51 get()52RenderThreadInfo* RenderThreadInfo::get() { 53 return s_threadInfoPtr; 54 } 55 56 // Loop over all active render thread infos. Takes the global render thread info lock. forAllRenderThreadInfos(std::function<void (RenderThreadInfo *)> f)57void RenderThreadInfo::forAllRenderThreadInfos(std::function<void(RenderThreadInfo*)> f) { 58 AutoLock lock(sRegistry.lock); 59 for (auto info: sRegistry.threadInfos) { 60 f(info); 61 } 62 } 63 64 #if GFXSTREAM_ENABLE_HOST_GLES initGl()65void RenderThreadInfo::initGl() { 66 m_glInfo.emplace(); 67 } 68 #endif 69 onSave(Stream * stream)70void RenderThreadInfo::onSave(Stream* stream) { 71 // TODO(b/309858017): remove if when ready to bump snapshot version 72 if (FrameBuffer::getFB()->getFeatures().VulkanSnapshots.enabled) { 73 stream->putBe64(m_puid); 74 } 75 76 #if GFXSTREAM_ENABLE_HOST_GLES 77 m_glInfo->onSave(stream); 78 #endif 79 } 80 onLoad(Stream * stream)81bool RenderThreadInfo::onLoad(Stream* stream) { 82 // TODO(b/309858017): remove if when ready to bump snapshot version 83 if (FrameBuffer::getFB()->getFeatures().VulkanSnapshots.enabled) { 84 m_puid = stream->getBe64(); 85 } 86 87 #if GFXSTREAM_ENABLE_HOST_GLES 88 return m_glInfo->onLoad(stream); 89 #else 90 // Functions only work with GLES for now. 91 return false; 92 #endif 93 } 94 postLoadRefreshCurrentContextSurfacePtrs()95void RenderThreadInfo::postLoadRefreshCurrentContextSurfacePtrs() { 96 #if GFXSTREAM_ENABLE_HOST_GLES 97 return m_glInfo->postLoadRefreshCurrentContextSurfacePtrs(); 98 #endif 99 } 100 101 } // namespace gfxstream 102