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