1 /*
2 * Copyright 2018 Google
3 * SPDX-License-Identifier: MIT
4 */
5 #include "HostVisibleMemoryVirtualization.h"
6 #include "util/detect_os.h"
7
8 #include <set>
9
10 #include "ResourceTracker.h"
11 #include "Resources.h"
12 #include "VkEncoder.h"
13
14 namespace gfxstream {
15 namespace vk {
16
CoherentMemory(VirtGpuResourceMappingPtr blobMapping,uint64_t size,VkDevice device,VkDeviceMemory memory)17 CoherentMemory::CoherentMemory(VirtGpuResourceMappingPtr blobMapping, uint64_t size,
18 VkDevice device, VkDeviceMemory memory)
19 : mSize(size), mBlobMapping(blobMapping), mDevice(device), mMemory(memory) {
20 mHeap = u_mmInit(0, kHostVisibleHeapSize);
21 mBaseAddr = blobMapping->asRawPtr();
22 }
23
24 #if DETECT_OS_ANDROID
CoherentMemory(GoldfishAddressSpaceBlockPtr block,uint64_t gpuAddr,uint64_t size,VkDevice device,VkDeviceMemory memory)25 CoherentMemory::CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size,
26 VkDevice device, VkDeviceMemory memory)
27 : mSize(size), mBlock(block), mDevice(device), mMemory(memory) {
28 mHeap = u_mmInit(0, kHostVisibleHeapSize);
29 mBaseAddr = (uint8_t*)block->mmap(gpuAddr);
30 }
31 #endif // DETECT_OS_ANDROID
32
~CoherentMemory()33 CoherentMemory::~CoherentMemory() {
34 ResourceTracker::getThreadLocalEncoder()->vkFreeMemorySyncGOOGLE(mDevice, mMemory, nullptr,
35 false);
36 u_mmDestroy(mHeap);
37 }
38
getDeviceMemory() const39 VkDeviceMemory CoherentMemory::getDeviceMemory() const { return mMemory; }
40
subAllocate(uint64_t size,uint8_t ** ptr,uint64_t & offset)41 bool CoherentMemory::subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset) {
42 auto block = u_mmAllocMem(mHeap, (int)size, 0, 0);
43 if (!block) return false;
44
45 *ptr = mBaseAddr + block->ofs;
46 offset = block->ofs;
47 return true;
48 }
49
release(uint8_t * ptr)50 bool CoherentMemory::release(uint8_t* ptr) {
51 int offset = ptr - mBaseAddr;
52 auto block = u_mmFindBlock(mHeap, offset);
53 if (block) {
54 u_mmFreeMem(block);
55 return true;
56 }
57
58 return false;
59 }
60
61 } // namespace vk
62 } // namespace gfxstream
63