• 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 #pragma once
15 
16 #include <inttypes.h>
17 
18 #include <atomic>
19 #include <mutex>
20 #include <optional>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "aemu/base/Compiler.h"
25 #include "aemu/base/ManagedDescriptor.hpp"
26 
27 // A global mapping from opaque host memory IDs to host virtual
28 // addresses/sizes.  This is so that the guest doesn't have to know the host
29 // virtual address to be able to map them. However, we do also provide a
30 // mechanism for obtaining the offsets into page for such buffers (as the guest
31 // does need to know those).
32 //
33 // This is currently used only in conjunction with virtio-gpu-next and Vulkan /
34 // address space device, though there are possible other consumers of this, so
35 // it becomes a global object. It exports methods into VmOperations.
36 
37 using android::base::ManagedDescriptor;
38 
39 namespace gfxstream {
40 
41 // Caching types
42 #define MAP_CACHE_MASK 0x0f
43 #define MAP_CACHE_NONE 0x00
44 #define MAP_CACHE_CACHED 0x01
45 #define MAP_CACHE_UNCACHED 0x02
46 #define MAP_CACHE_WC 0x03
47 
48 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_FD 0x1
49 #define STREAM_MEM_HANDLE_TYPE_DMABUF 0x2
50 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_WIN32 0x3
51 #define STREAM_MEM_HANDLE_TYPE_SHM 0x4
52 #define STREAM_MEM_HANDLE_TYPE_ZIRCON 0x5
53 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_FD 0x6
54 #define STREAM_FENCE_HANDLE_TYPE_SYNC_FD 0x7
55 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_WIN32 0x8
56 #define STREAM_FENCE_HANDLE_TYPE_ZIRCON 0x9
57 
58 // A struct describing the information about host memory associated
59 // with a host memory id. Used with virtio-gpu-next.
60 struct HostMemInfo {
61     void* addr;
62     uint32_t caching;
63 };
64 
65 struct VulkanInfo {
66     uint32_t memoryIndex;
67     uint8_t deviceUUID[16];
68     uint8_t driverUUID[16];
69 };
70 
71 struct ManagedDescriptorInfo {
72     ManagedDescriptor descriptor;
73     uint32_t handleType;
74     uint32_t caching;
75     std::optional<VulkanInfo> vulkanInfoOpt;
76 };
77 
78 class BlobManager {
79    public:
80     BlobManager() = default;
81 
82     static BlobManager* get();
83 
84     void addMapping(uint32_t ctx_id, uint64_t blobId, void* addr, uint32_t caching);
85     std::optional<HostMemInfo> removeMapping(uint32_t ctx_id, uint64_t blobId);
86 
87     void addDescriptorInfo(uint32_t ctx_id, uint64_t blobId, ManagedDescriptor descriptor,
88                            uint32_t handleType, uint32_t caching,
89                            std::optional<VulkanInfo> vulkanInfoOpt);
90     std::optional<ManagedDescriptorInfo> removeDescriptorInfo(uint32_t ctx_id, uint64_t blobId);
91 
92    private:
93     // Only for pairs of std::hash-able types for simplicity.
94     // You can of course template this struct to allow other hash functions
95     struct pair_hash {
96         template <class T1, class T2>
operatorpair_hash97         std::size_t operator()(const std::pair<T1, T2>& p) const {
98             auto h1 = std::hash<T1>{}(p.first);
99             auto h2 = std::hash<T2>{}(p.second);
100 
101             // Mainly for demonstration purposes, i.e. works but is overly simple
102             // In the real world, use sth. like boost.hash_combine
103             return h1 ^ h2;
104         }
105     };
106 
107     std::mutex mLock;
108     std::unordered_map<std::pair<uint32_t, uint64_t>, HostMemInfo, pair_hash> mHostMemInfos;
109     std::unordered_map<std::pair<uint32_t, uint64_t>, ManagedDescriptorInfo, pair_hash>
110         mDescriptorInfos;
111     DISALLOW_COPY_ASSIGN_AND_MOVE(BlobManager);
112 };
113 
114 }  // namespace gfxstream
115