1 // Copyright (C) 2019 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include "VulkanHandleMapping.h" 17 #include "VulkanHandles.h" 18 #include "aemu/base/containers/EntityManager.h" 19 #include "aemu/base/HealthMonitor.h" 20 #include "aemu/base/files/Stream.h" 21 #include "common/goldfish_vk_marshaling.h" 22 #include "utils/GfxApiLogger.h" 23 24 namespace gfxstream { 25 namespace vk { 26 27 // A class that captures all important data structures for 28 // reconstructing a Vulkan system state via trimmed API record and replay. 29 class VkReconstruction { 30 public: 31 VkReconstruction(); 32 33 void save(android::base::Stream* stream); 34 void load(android::base::Stream* stream, emugl::GfxApiLogger& gfxLogger, 35 emugl::HealthMonitor<>* healthMonitor); 36 37 struct ApiInfo { 38 // Fast 39 uint32_t opCode; 40 std::vector<uint8_t> trace; 41 size_t traceBytes = 0; 42 // Book-keeping for which handles were created by this API 43 std::vector<uint64_t> createdHandles; 44 }; 45 46 using ApiTrace = android::base::EntityManager<32, 16, 16, ApiInfo>; 47 using ApiHandle = ApiTrace::EntityHandle; 48 49 struct HandleReconstruction { 50 std::vector<ApiHandle> apiRefs; 51 std::vector<uint64_t> childHandles; 52 }; 53 54 using HandleReconstructions = 55 android::base::UnpackedComponentManager<32, 16, 16, HandleReconstruction>; 56 57 struct HandleModification { 58 std::vector<ApiHandle> apiRefs; 59 uint32_t order = 0; 60 }; 61 62 using HandleModifications = 63 android::base::UnpackedComponentManager<32, 16, 16, HandleModification>; 64 65 ApiHandle createApiInfo(); 66 void destroyApiInfo(ApiHandle h); 67 68 ApiInfo* getApiInfo(ApiHandle h); 69 70 void setApiTrace(ApiInfo* apiInfo, uint32_t opcode, const uint8_t* traceBegin, 71 size_t traceBytes); 72 73 void dump(); 74 75 void addHandles(const uint64_t* toAdd, uint32_t count); 76 void removeHandles(const uint64_t* toRemove, uint32_t count); 77 78 void forEachHandleAddApi(const uint64_t* toProcess, uint32_t count, uint64_t apiHandle); 79 void forEachHandleDeleteApi(const uint64_t* toProcess, uint32_t count); 80 81 void addHandleDependency(const uint64_t* handles, uint32_t count, uint64_t parentHandle); 82 83 void setCreatedHandlesForApi(uint64_t apiHandle, const uint64_t* created, uint32_t count); 84 85 void forEachHandleAddModifyApi(const uint64_t* toProcess, uint32_t count, uint64_t apiHandle); 86 87 void setModifiedHandlesForApi(uint64_t apiHandle, const uint64_t* modified, uint32_t count); 88 89 private: 90 std::vector<uint64_t> getOrderedUniqueModifyApis() const; 91 92 ApiTrace mApiTrace; 93 94 HandleReconstructions mHandleReconstructions; 95 HandleModifications mHandleModifications; 96 97 std::vector<uint8_t> mLoadedTrace; 98 }; 99 100 } // namespace vk 101 } // namespace gfxstream 102