• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018 The Android Open Source Project
2 // Copyright (C) 2018 Google Inc.
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 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include "VirtGpu.h"
20 #include "aemu/base/AndroidSubAllocator.h"
21 #include "goldfish_address_space.h"
22 
23 #define ALIGN(A, B) (((A) + (B)-1) & ~((B)-1))
24 
25 constexpr uint64_t kMegaByte = 1048576;
26 
27 // This needs to be a power of 2 that is at least the min alignment needed
28 // in HostVisibleMemoryVirtualization.cpp.
29 // Some Windows drivers require a 64KB alignment for suballocated memory (b:152769369) for YUV
30 // images.
31 constexpr uint64_t kLargestPageSize = 65536;
32 
33 constexpr uint64_t kDefaultHostMemBlockSize = 16 * kMegaByte;  // 16 mb
34 constexpr uint64_t kHostVisibleHeapSize = 512 * kMegaByte;     // 512 mb
35 
36 namespace gfxstream {
37 namespace vk {
38 
39 bool isHostVisible(const VkPhysicalDeviceMemoryProperties* memoryProps, uint32_t index);
40 
41 using GoldfishAddressSpaceBlockPtr = std::shared_ptr<GoldfishAddressSpaceBlock>;
42 using SubAllocatorPtr = std::unique_ptr<android::base::guest::SubAllocator>;
43 
44 class CoherentMemory {
45    public:
46     CoherentMemory(VirtGpuBlobMappingPtr blobMapping, uint64_t size, VkDevice device,
47                    VkDeviceMemory memory);
48     CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size,
49                    VkDevice device, VkDeviceMemory memory);
50     ~CoherentMemory();
51 
52     VkDeviceMemory getDeviceMemory() const;
53 
54     bool subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset);
55     bool release(uint8_t* ptr);
56 
57    private:
58     CoherentMemory(CoherentMemory const&);
59     void operator=(CoherentMemory const&);
60 
61     uint64_t mSize;
62     VirtGpuBlobMappingPtr mBlobMapping = nullptr;
63     GoldfishAddressSpaceBlockPtr mBlock = nullptr;
64     VkDevice mDevice;
65     VkDeviceMemory mMemory;
66     SubAllocatorPtr mAllocator;
67 };
68 
69 using CoherentMemoryPtr = std::shared_ptr<CoherentMemory>;
70 
71 }  // namespace vk
72 }  // namespace gfxstream
73