• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "BlobManager.h"
15 
16 #include <utility>
17 
18 using android::base::ManagedDescriptor;
19 
20 namespace gfxstream {
21 
sMapping()22 static BlobManager* sMapping() {
23     static BlobManager* s = new BlobManager;
24     return s;
25 }
26 
27 // static
get()28 BlobManager* BlobManager::get() { return sMapping(); }
29 
addMapping(uint32_t ctxId,uint64_t blobId,void * addr,uint32_t caching)30 void BlobManager::addMapping(uint32_t ctxId, uint64_t blobId, void* addr, uint32_t caching) {
31     struct HostMemInfo info = {
32         .addr = addr,
33         .caching = caching,
34     };
35 
36     auto key = std::make_pair(ctxId, blobId);
37     std::lock_guard<std::mutex> lock(mLock);
38     mHostMemInfos.insert(std::make_pair(key, info));
39 }
40 
removeMapping(uint32_t ctxId,uint64_t blobId)41 std::optional<HostMemInfo> BlobManager::removeMapping(uint32_t ctxId, uint64_t blobId) {
42     auto key = std::make_pair(ctxId, blobId);
43     std::lock_guard<std::mutex> lock(mLock);
44     auto found = mHostMemInfos.find(key);
45     if (found != mHostMemInfos.end()) {
46         std::optional<HostMemInfo> ret = found->second;
47         mHostMemInfos.erase(found);
48         return ret;
49     }
50 
51     return std::nullopt;
52 }
53 
addDescriptorInfo(uint32_t ctxId,uint64_t blobId,ManagedDescriptor descriptor,uint32_t handleType,uint32_t caching,std::optional<VulkanInfo> vulkanInfoOpt)54 void BlobManager::addDescriptorInfo(uint32_t ctxId, uint64_t blobId, ManagedDescriptor descriptor,
55                                     uint32_t handleType, uint32_t caching,
56                                     std::optional<VulkanInfo> vulkanInfoOpt) {
57     struct ManagedDescriptorInfo info = {
58         .descriptor = std::move(descriptor),
59         .handleType = handleType,
60         .caching = caching,
61         .vulkanInfoOpt = vulkanInfoOpt,
62     };
63 
64     auto key = std::make_pair(ctxId, blobId);
65     std::lock_guard<std::mutex> lock(mLock);
66     mDescriptorInfos.insert(std::make_pair(key, std::move(info)));
67 }
68 
removeDescriptorInfo(uint32_t ctxId,uint64_t blobId)69 std::optional<ManagedDescriptorInfo> BlobManager::removeDescriptorInfo(uint32_t ctxId,
70                                                                        uint64_t blobId) {
71     auto key = std::make_pair(ctxId, blobId);
72     std::lock_guard<std::mutex> lock(mLock);
73     auto found = mDescriptorInfos.find(key);
74     if (found != mDescriptorInfos.end()) {
75         std::optional<ManagedDescriptorInfo> ret = std::move(found->second);
76         mDescriptorInfos.erase(found);
77         return ret;
78     }
79 
80     return std::nullopt;
81 }
82 
83 }  // namespace gfxstream
84