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 #include "HostVisibleMemoryVirtualization.h"
16
17 #include <set>
18
19 #include "../OpenglSystemCommon/EmulatorFeatureInfo.h"
20 #include "ResourceTracker.h"
21 #include "Resources.h"
22 #include "VkEncoder.h"
23 #include "aemu/base/AndroidSubAllocator.h"
24
25 using gfxstream::guest::SubAllocator;
26
27 namespace gfxstream {
28 namespace vk {
29
isHostVisible(const VkPhysicalDeviceMemoryProperties * memoryProps,uint32_t index)30 bool isHostVisible(const VkPhysicalDeviceMemoryProperties* memoryProps, uint32_t index) {
31 return memoryProps->memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
32 }
33
CoherentMemory(VirtGpuResourceMappingPtr blobMapping,uint64_t size,VkDevice device,VkDeviceMemory memory)34 CoherentMemory::CoherentMemory(VirtGpuResourceMappingPtr blobMapping, uint64_t size,
35 VkDevice device, VkDeviceMemory memory)
36 : mSize(size), mBlobMapping(blobMapping), mDevice(device), mMemory(memory) {
37 mAllocator =
38 std::make_unique<gfxstream::guest::SubAllocator>(blobMapping->asRawPtr(), mSize, 4096);
39 }
40
41 #if defined(__ANDROID__)
CoherentMemory(GoldfishAddressSpaceBlockPtr block,uint64_t gpuAddr,uint64_t size,VkDevice device,VkDeviceMemory memory)42 CoherentMemory::CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size,
43 VkDevice device, VkDeviceMemory memory)
44 : mSize(size), mBlock(block), mDevice(device), mMemory(memory) {
45 void* address = block->mmap(gpuAddr);
46 mAllocator = std::make_unique<gfxstream::guest::SubAllocator>(address, mSize, kLargestPageSize);
47 }
48 #endif // defined(__ANDROID__)
49
~CoherentMemory()50 CoherentMemory::~CoherentMemory() {
51 ResourceTracker::getThreadLocalEncoder()->vkFreeMemorySyncGOOGLE(mDevice, mMemory, nullptr,
52 false);
53 }
54
getDeviceMemory() const55 VkDeviceMemory CoherentMemory::getDeviceMemory() const { return mMemory; }
56
subAllocate(uint64_t size,uint8_t ** ptr,uint64_t & offset)57 bool CoherentMemory::subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset) {
58 auto address = mAllocator->alloc(size);
59 if (!address) return false;
60
61 *ptr = (uint8_t*)address;
62 offset = mAllocator->getOffset(address);
63 return true;
64 }
65
release(uint8_t * ptr)66 bool CoherentMemory::release(uint8_t* ptr) {
67 mAllocator->free(ptr);
68 return true;
69 }
70
71 } // namespace vk
72 } // namespace gfxstream
73