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 if (m_glInfo) { 78 stream->putBe32(1); 79 m_glInfo->onSave(stream); 80 } else { 81 stream->putBe32(0); 82 } 83 #endif 84 85 if (m_vkInfo) { 86 stream->putBe32(1); 87 m_vkInfo->onSave(stream); 88 } else { 89 stream->putBe32(0); 90 } 91 } 92 onLoad(Stream * stream)93bool RenderThreadInfo::onLoad(Stream* stream) { 94 // TODO(b/309858017): remove if when ready to bump snapshot version 95 if (FrameBuffer::getFB()->getFeatures().VulkanSnapshots.enabled) { 96 m_puid = stream->getBe64(); 97 } 98 99 #if GFXSTREAM_ENABLE_HOST_GLES 100 const bool loadGlInfo = stream->getBe32() == 1; 101 if (loadGlInfo) { 102 if (!m_glInfo) { 103 m_glInfo.emplace(); 104 } 105 if (!m_glInfo->onLoad(stream)) { 106 return false; 107 } 108 } 109 #endif 110 111 const bool loadVkInfo = stream->getBe32() == 1; 112 if (loadVkInfo) { 113 if (!m_vkInfo) { 114 m_vkInfo.emplace(); 115 } 116 if (!m_vkInfo->onLoad(stream)) { 117 return false; 118 } 119 } 120 121 return true; 122 } 123 postLoadRefreshCurrentContextSurfacePtrs()124void RenderThreadInfo::postLoadRefreshCurrentContextSurfacePtrs() { 125 #if GFXSTREAM_ENABLE_HOST_GLES 126 if (m_glInfo) { 127 m_glInfo->postLoadRefreshCurrentContextSurfacePtrs(); 128 } 129 #endif 130 } 131 132 } // namespace gfxstream 133