| Flutter Impeller |

pipeline_cache_data_vk.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/renderer/backend/vulkan/pipeline_cache_data_vk.h"
6
7 #include "flutter/fml/file.h"
8 #include "impeller/base/allocation.h"
9 #include "impeller/base/validation.h"
10
11 namespace impeller {
12
13 static constexpr const char* kPipelineCacheFileName =
14 "flutter.impeller.vkcache";
15
16 bool PipelineCacheDataPersist(const fml::UniqueFD& cache_directory,
17 const VkPhysicalDeviceProperties& props,
18 const vk::UniquePipelineCache& cache) {
19 if (!cache_directory.is_valid()) {
20 return false;
21 }
22 size_t data_size = 0u;
23 if (cache.getOwner().getPipelineCacheData(*cache, &data_size, nullptr) !=
24 vk::Result::eSuccess) {
25 VALIDATION_LOG << "Could not fetch pipeline cache size.";
26 return false;
27 }
28 if (data_size == 0u) {
29 return true;
30 }
31 auto allocation = std::make_shared<Allocation>();
32 if (!allocation->Truncate(Bytes{sizeof(PipelineCacheHeaderVK) + data_size},
33 false)) {
34 VALIDATION_LOG << "Could not allocate pipeline cache data staging buffer.";
35 return false;
36 }
37 const auto header = PipelineCacheHeaderVK{props, data_size};
38 std::memcpy(allocation->GetBuffer(), &header, sizeof(header));
39 if (cache.getOwner().getPipelineCacheData(
40 *cache, &data_size, allocation->GetBuffer() + sizeof(header)) !=
41 vk::Result::eSuccess) {
42 VALIDATION_LOG << "Could not copy pipeline cache data.";
43 return false;
44 }
45
46 auto allocation_mapping = CreateMappingFromAllocation(allocation);
47 if (!allocation_mapping) {
48 return false;
49 }
50 if (!fml::WriteAtomically(cache_directory, kPipelineCacheFileName,
51 *allocation_mapping)) {
52 VALIDATION_LOG << "Could not write cache file to disk.";
53 return false;
54 }
55 return true;
56 }
57
58 std::unique_ptr<fml::Mapping> PipelineCacheDataRetrieve(
59 const fml::UniqueFD& cache_directory,
60 const VkPhysicalDeviceProperties& props) {
61 if (!cache_directory.is_valid()) {
62 return nullptr;
63 }
64 std::shared_ptr<fml::FileMapping> on_disk_data =
65 fml::FileMapping::CreateReadOnly(cache_directory, kPipelineCacheFileName);
66 if (!on_disk_data) {
67 return nullptr;
68 }
69 if (on_disk_data->GetSize() < sizeof(PipelineCacheHeaderVK)) {
70 VALIDATION_LOG << "Pipeline cache data size is too small.";
71 return nullptr;
72 }
73 auto on_disk_header = PipelineCacheHeaderVK{};
74 std::memcpy(&on_disk_header, //
75 on_disk_data->GetMapping(), //
76 sizeof(on_disk_header) //
77 );
78 const auto current_header = PipelineCacheHeaderVK{props, 0u};
79 if (!on_disk_header.IsCompatibleWith(current_header)) {
80 FML_LOG(WARNING)
81 << "Persisted pipeline cache is not compatible with current "
82 "Vulkan context. Ignoring.";
83 return nullptr;
84 }
85 // Zero sized data is known to cause issues.
86 if (on_disk_header.data_size == 0u) {
87 return nullptr;
88 }
89 return std::make_unique<fml::NonOwnedMapping>(
90 on_disk_data->GetMapping() + sizeof(on_disk_header),
91 on_disk_header.data_size, [on_disk_data](auto, auto) {});
92 }
93
94 PipelineCacheHeaderVK::PipelineCacheHeaderVK() = default;
95
96 PipelineCacheHeaderVK::PipelineCacheHeaderVK(
97 const VkPhysicalDeviceProperties& props,
98 uint64_t p_data_size)
99 : driver_version(props.driverVersion),
100 vendor_id(props.vendorID),
101 device_id(props.deviceID),
102 data_size(p_data_size) {
103 std::memcpy(uuid, props.pipelineCacheUUID, VK_UUID_SIZE);
104 }
105
106 bool PipelineCacheHeaderVK::IsCompatibleWith(
107 const PipelineCacheHeaderVK& o) const {
108 // Check for everything but the data size.
109 return magic == o.magic && //
110 driver_version == o.driver_version && //
111 vendor_id == o.vendor_id && //
112 device_id == o.device_id && //
113 abi == o.abi && //
114 std::memcmp(uuid, o.uuid, VK_UUID_SIZE) == 0;
115 }
116
117 } // namespace impeller
impeller::PipelineCacheHeaderVK::abi
uint32_t abi
Definition: pipeline_cache_data_vk.h:34
impeller::PipelineCacheDataPersist
bool PipelineCacheDataPersist(const fml::UniqueFD &cache_directory, const VkPhysicalDeviceProperties &props, const vk::UniquePipelineCache &cache)
Persist the pipeline cache to a file in the given cache directory. This function performs integrity c...
Definition: pipeline_cache_data_vk.cc:16
impeller::PipelineCacheHeaderVK::driver_version
uint32_t driver_version
Definition: pipeline_cache_data_vk.h:29
allocation.h
impeller::PipelineCacheHeaderVK::magic
uint32_t magic
Definition: pipeline_cache_data_vk.h:25
impeller::kPipelineCacheFileName
static constexpr const char * kPipelineCacheFileName
Definition: pipeline_cache_data_vk.cc:13
validation.h
impeller::CreateMappingFromAllocation
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Creates a mapping from allocation.
Definition: allocation.cc:99
pipeline_cache_data_vk.h
impeller::PipelineCacheHeaderVK::PipelineCacheHeaderVK
PipelineCacheHeaderVK()
Constructs a new empty instance.
impeller::PipelineCacheHeaderVK::IsCompatibleWith
bool IsCompatibleWith(const PipelineCacheHeaderVK &other) const
Determines whether the specified o is compatible with.
Definition: pipeline_cache_data_vk.cc:106
impeller::AllocationSize
Represents the size of an allocation in different units.
Definition: allocation_size.h:29
impeller::PipelineCacheHeaderVK
An Impeller specific header prepended to all pipeline cache information that is persisted on disk....
Definition: pipeline_cache_data_vk.h:23
impeller::PipelineCacheHeaderVK::uuid
uint8_t uuid[VK_UUID_SIZE]
Definition: pipeline_cache_data_vk.h:35
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::PipelineCacheDataRetrieve
std::unique_ptr< fml::Mapping > PipelineCacheDataRetrieve(const fml::UniqueFD &cache_directory, const VkPhysicalDeviceProperties &props)
Retrieve the previously persisted pipeline cache data. This function provides integrity checks the Vu...
Definition: pipeline_cache_data_vk.cc:58
impeller::PipelineCacheHeaderVK::device_id
uint32_t device_id
Definition: pipeline_cache_data_vk.h:31
impeller
Definition: allocation.cc:12
impeller::PipelineCacheHeaderVK::vendor_id
uint32_t vendor_id
Definition: pipeline_cache_data_vk.h:30