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 #pragma once 16 17 #include <vulkan/vulkan.h> 18 19 #include "VirtGpu.h" 20 #include "aemu/base/AndroidSubAllocator.h" 21 #include "goldfish_address_space.h" 22 23 constexpr uint64_t kMegaByte = 1048576; 24 25 // This needs to be a power of 2 that is at least the min alignment needed 26 // in HostVisibleMemoryVirtualization.cpp. 27 // Some Windows drivers require a 64KB alignment for suballocated memory (b:152769369) for YUV 28 // images. 29 constexpr uint64_t kLargestPageSize = 65536; 30 31 constexpr uint64_t kDefaultHostMemBlockSize = 16 * kMegaByte; // 16 mb 32 constexpr uint64_t kHostVisibleHeapSize = 512 * kMegaByte; // 512 mb 33 34 namespace gfxstream { 35 namespace vk { 36 37 bool isHostVisible(const VkPhysicalDeviceMemoryProperties* memoryProps, uint32_t index); 38 39 using GoldfishAddressSpaceBlockPtr = std::shared_ptr<GoldfishAddressSpaceBlock>; 40 using SubAllocatorPtr = std::unique_ptr<gfxstream::guest::SubAllocator>; 41 42 class CoherentMemory { 43 public: 44 CoherentMemory(VirtGpuResourceMappingPtr blobMapping, uint64_t size, VkDevice device, 45 VkDeviceMemory memory); 46 47 #if defined(__ANDROID__) 48 CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size, 49 VkDevice device, VkDeviceMemory memory); 50 #endif // defined(__ANDROID__) 51 52 ~CoherentMemory(); 53 54 VkDeviceMemory getDeviceMemory() const; 55 56 bool subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset); 57 bool release(uint8_t* ptr); 58 59 private: 60 CoherentMemory(CoherentMemory const&); 61 void operator=(CoherentMemory const&); 62 63 uint64_t mSize; 64 VirtGpuResourceMappingPtr mBlobMapping = nullptr; 65 GoldfishAddressSpaceBlockPtr mBlock = nullptr; 66 VkDevice mDevice; 67 VkDeviceMemory mMemory; 68 SubAllocatorPtr mAllocator; 69 }; 70 71 using CoherentMemoryPtr = std::shared_ptr<CoherentMemory>; 72 73 } // namespace vk 74 } // namespace gfxstream 75