1 // Copyright 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "ExternalObjectManager.h"
15
16 #include <utility>
17
18 namespace gfxstream {
19
sMapping()20 static ExternalObjectManager* sMapping() {
21 static ExternalObjectManager* s = new ExternalObjectManager;
22 return s;
23 }
24
25 // static
get()26 ExternalObjectManager* ExternalObjectManager::get() { return sMapping(); }
27
addMapping(uint32_t ctxId,uint64_t blobId,void * addr,uint32_t caching)28 void ExternalObjectManager::addMapping(uint32_t ctxId, uint64_t blobId, void* addr,
29 uint32_t caching) {
30 struct HostMemInfo info = {
31 .addr = addr,
32 .caching = caching,
33 };
34
35 auto key = std::make_pair(ctxId, blobId);
36 std::lock_guard<std::mutex> lock(mMutex);
37 mHostMemInfos.insert(std::make_pair(key, info));
38 }
39
removeMapping(uint32_t ctxId,uint64_t blobId)40 std::optional<HostMemInfo> ExternalObjectManager::removeMapping(uint32_t ctxId, uint64_t blobId) {
41 auto key = std::make_pair(ctxId, blobId);
42 std::lock_guard<std::mutex> lock(mMutex);
43 auto found = mHostMemInfos.find(key);
44 if (found != mHostMemInfos.end()) {
45 std::optional<HostMemInfo> ret = found->second;
46 mHostMemInfos.erase(found);
47 return ret;
48 }
49
50 return std::nullopt;
51 }
52
addBlobDescriptorInfo(uint32_t ctxId,uint64_t blobId,ManagedDescriptor descriptor,uint32_t streamHandleType,uint32_t caching,std::optional<VulkanInfo> vulkanInfoOpt)53 void ExternalObjectManager::addBlobDescriptorInfo(uint32_t ctxId, uint64_t blobId,
54 ManagedDescriptor descriptor,
55 uint32_t streamHandleType, uint32_t caching,
56 std::optional<VulkanInfo> vulkanInfoOpt) {
57 struct BlobDescriptorInfo info = {
58 .descriptorInfo =
59 {
60 .descriptor = std::move(descriptor),
61 .streamHandleType = streamHandleType,
62 },
63 .caching = caching,
64 .vulkanInfoOpt = vulkanInfoOpt,
65 };
66
67 auto key = std::make_pair(ctxId, blobId);
68 std::lock_guard<std::mutex> lock(mMutex);
69 mBlobDescriptorInfos.insert(std::make_pair(key, std::move(info)));
70 }
71
removeBlobDescriptorInfo(uint32_t ctxId,uint64_t blobId)72 std::optional<BlobDescriptorInfo> ExternalObjectManager::removeBlobDescriptorInfo(uint32_t ctxId,
73 uint64_t blobId) {
74 auto key = std::make_pair(ctxId, blobId);
75 std::lock_guard<std::mutex> lock(mMutex);
76 auto found = mBlobDescriptorInfos.find(key);
77 if (found != mBlobDescriptorInfos.end()) {
78 std::optional<BlobDescriptorInfo> ret = std::move(found->second);
79 mBlobDescriptorInfos.erase(found);
80 return ret;
81 }
82
83 return std::nullopt;
84 }
85
addSyncDescriptorInfo(uint32_t ctxId,uint64_t syncId,ManagedDescriptor descriptor,uint32_t streamHandleType)86 void ExternalObjectManager::addSyncDescriptorInfo(uint32_t ctxId, uint64_t syncId,
87 ManagedDescriptor descriptor,
88 uint32_t streamHandleType) {
89 SyncDescriptorInfo info = {
90 .descriptor = std::move(descriptor),
91 .streamHandleType = streamHandleType,
92 };
93
94 auto key = std::make_pair(ctxId, syncId);
95 std::lock_guard<std::mutex> lock(mMutex);
96 mSyncDescriptorInfos.insert(std::make_pair(key, std::move(info)));
97 }
98
removeSyncDescriptorInfo(uint32_t ctxId,uint64_t syncId)99 std::optional<SyncDescriptorInfo> ExternalObjectManager::removeSyncDescriptorInfo(uint32_t ctxId,
100 uint64_t syncId) {
101 auto key = std::make_pair(ctxId, syncId);
102 std::lock_guard<std::mutex> lock(mMutex);
103 auto found = mSyncDescriptorInfos.find(key);
104 if (found != mSyncDescriptorInfos.end()) {
105 std::optional<SyncDescriptorInfo> ret = std::move(found->second);
106 mSyncDescriptorInfos.erase(found);
107 return ret;
108 }
109
110 return std::nullopt;
111 }
112
addResourceExternalHandleInfo(uint32_t resHandle,const ExternalHandleInfo & externalHandleInfo)113 void ExternalObjectManager::addResourceExternalHandleInfo(
114 uint32_t resHandle, const ExternalHandleInfo& externalHandleInfo) {
115 std::lock_guard<std::mutex> lock(mMutex);
116 mResourceExternalHandleInfos.insert(std::make_pair(resHandle, externalHandleInfo));
117 }
118
removeResourceExternalHandleInfo(uint32_t resHandle)119 std::optional<ExternalHandleInfo> ExternalObjectManager::removeResourceExternalHandleInfo(
120 uint32_t resHandle) {
121 std::lock_guard<std::mutex> lock(mMutex);
122 auto found = mResourceExternalHandleInfos.find(resHandle);
123 if (found != mResourceExternalHandleInfos.end()) {
124 std::optional<ExternalHandleInfo> ret = found->second;
125 mResourceExternalHandleInfos.erase(found);
126 return ret;
127 }
128
129 return std::nullopt;
130 }
131
132 } // namespace gfxstream
133