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