• 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 #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 gfxstream::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<gfxstream::guest::SubAllocator>(blobMapping->asRawPtr(), mSize, 4096);
41 }
42 
43 #if defined(__ANDROID__)
CoherentMemory(GoldfishAddressSpaceBlockPtr block,uint64_t gpuAddr,uint64_t size,VkDevice device,VkDeviceMemory memory)44 CoherentMemory::CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size,
45                                VkDevice device, VkDeviceMemory memory)
46     : mSize(size), mBlock(block), mDevice(device), mMemory(memory) {
47     void* address = block->mmap(gpuAddr);
48     mAllocator = std::make_unique<gfxstream::guest::SubAllocator>(address, mSize, kLargestPageSize);
49 }
50 #endif  // defined(__ANDROID__)
51 
~CoherentMemory()52 CoherentMemory::~CoherentMemory() {
53     ResourceTracker::getThreadLocalEncoder()->vkFreeMemorySyncGOOGLE(mDevice, mMemory, nullptr,
54                                                                      false);
55 }
56 
getDeviceMemory() const57 VkDeviceMemory CoherentMemory::getDeviceMemory() const { return mMemory; }
58 
subAllocate(uint64_t size,uint8_t ** ptr,uint64_t & offset)59 bool CoherentMemory::subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset) {
60     auto address = mAllocator->alloc(size);
61     if (!address) return false;
62 
63     *ptr = (uint8_t*)address;
64     offset = mAllocator->getOffset(address);
65     return true;
66 }
67 
release(uint8_t * ptr)68 bool CoherentMemory::release(uint8_t* ptr) {
69     mAllocator->free(ptr);
70     return true;
71 }
72 
73 }  // namespace vk
74 }  // namespace gfxstream
75