Skip to content

Commit 60b6958

Browse files
vkconfig: Fix layer schema validation caching
1 parent 7df1088 commit 60b6958

6 files changed

Lines changed: 132 additions & 37 deletions

File tree

vkconfig_core/configurator.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,11 +1143,21 @@ bool Configurator::Load() {
11431143
display.id.api_version = Version::NONE;
11441144
display.descriptor.enabled = json_descriptor_object.value("enabled").toBool();
11451145
display.descriptor.removed = json_descriptor_object.value("removed").toBool();
1146-
display.descriptor.validated = json_descriptor_object.value("validated").toBool();
1146+
display.descriptor.validated =
1147+
::GetLayerValidated(json_descriptor_object.value("validated").toString().toStdString().c_str());
1148+
if (json_descriptor_object.value("last_modified") != QJsonValue::Undefined) {
1149+
display.descriptor.last_modified =
1150+
json_descriptor_object.value("last_modified").toString().toStdString();
1151+
}
11471152
descriptors.push_back(display);
11481153
}
11491154

1150-
this->layers.AppendInit(Path(keys[i].toStdString()), descriptors);
1155+
Path path(keys[i].toStdString());
1156+
if (!path.Exists()) {
1157+
continue;
1158+
}
1159+
1160+
this->layers.AppendInit(path, descriptors);
11511161
}
11521162
}
11531163
if (json_object.value("paths") != QJsonValue::Undefined) {
@@ -1379,10 +1389,10 @@ bool Configurator::Save() const {
13791389

13801390
QJsonObject json_descriptor;
13811391
json_descriptor.insert("key", jt->first.c_str());
1382-
json_descriptor.insert("validated", jt->second.descriptor.validated);
1392+
json_descriptor.insert("validated", ::GetToken(jt->second.descriptor.validated));
13831393
json_descriptor.insert("enabled", jt->second.descriptor.enabled);
13841394
json_descriptor.insert("removed", jt->second.descriptor.removed);
1385-
1395+
json_descriptor.insert("last_modified", jt->second.descriptor.last_modified.c_str());
13861396
json_layer_descriptors.append(json_descriptor);
13871397
}
13881398

vkconfig_core/layer.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@
4646
#include <string>
4747
#include <algorithm>
4848

49+
#include <array>
50+
#include <cstring>
51+
52+
const char* GetToken(LayerValidated status) {
53+
static const char* TOKENS[] = {
54+
"NONE", // LAYER_VALIDATE_NONE
55+
"PASS", // LAYER_VALIDATE_PASS
56+
"FAIL", // LAYER_VALIDATE_FAIL
57+
};
58+
static_assert(std::size(TOKENS) == LAYER_VALIDATE_COUNT);
59+
60+
return TOKENS[status];
61+
}
62+
63+
LayerValidated GetLayerValidated(const char* token) {
64+
for (int i = LAYER_VALIDATE_FIRST, l = LAYER_VALIDATE_LAST; i <= l; ++i) {
65+
const LayerValidated status = static_cast<LayerValidated>(i);
66+
if (std::strcmp(::GetToken(status), token) == 0) {
67+
return status;
68+
}
69+
}
70+
71+
return LAYER_VALIDATE_FIRST;
72+
}
73+
4974
Layer::Layer() : status(STATUS_STABLE), platforms(PLATFORM_DESKTOP_BIT) {}
5075

5176
Layer::Layer(const std::string& key) : key(key), status(STATUS_STABLE), platforms(PLATFORM_DESKTOP_BIT) {}

vkconfig_core/layer.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,30 @@ enum LayerLoadStatus {
4949

5050
enum { LAYER_LOAD_COUNT = LAYER_LOAD_LAST - LAYER_LOAD_FIRST + 1 };
5151

52+
enum LayerValidated {
53+
LAYER_VALIDATE_NONE = 0,
54+
LAYER_VALIDATE_PASS,
55+
LAYER_VALIDATE_FAIL,
56+
57+
LAYER_VALIDATE_FIRST = LAYER_VALIDATE_NONE,
58+
LAYER_VALIDATE_LAST = LAYER_VALIDATE_FAIL,
59+
};
60+
61+
enum { LAYER_VALIDATE_COUNT = LAYER_VALIDATE_LAST - LAYER_VALIDATE_FIRST + 1 };
62+
63+
const char* GetToken(LayerValidated status);
64+
65+
LayerValidated GetLayerValidated(const char* token);
66+
5267
struct LayerId {
5368
Path manifest_path;
5469
std::string key;
5570
Version api_version;
5671
};
5772

5873
struct LayerDescriptor {
59-
bool validated = false;
74+
std::string last_modified;
75+
LayerValidated validated = LAYER_VALIDATE_NONE;
6076
bool enabled = true;
6177
bool removed = false;
6278
bool recent = false;
@@ -94,7 +110,7 @@ class Layer {
94110
Path binary_path;
95111
Version api_version;
96112
std::string implementation_version;
97-
std::string last_modified;
113+
// std::string last_modified;
98114
StatusType status;
99115
std::string description;
100116
std::string introduction;

vkconfig_core/layer_manager.cpp

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ bool LayerManager::Load(const QJsonObject &json_root_object, ConfiguratorMode co
175175
LayerDisplay layer;
176176
layer.id.manifest_path = json_layers_found_keys[i].toStdString();
177177
layer.descriptor.enabled = !json_status_object.value("disabled").toBool();
178-
layer.descriptor.validated = json_status_object.value("validated").toBool();
178+
layer.descriptor.validated = LAYER_VALIDATE_NONE;
179179

180180
std::vector<LayerDisplay> layers;
181181
layers.push_back(layer);
@@ -391,7 +391,7 @@ const Layer *LayerManager::FindLastModified(const std::string &layer_name, const
391391
}
392392

393393
if (result != nullptr) {
394-
if (result->last_modified > this->available_layers[i].last_modified) {
394+
if (result->descriptor.last_modified > this->available_layers[i].descriptor.last_modified) {
395395
continue;
396396
}
397397
}
@@ -450,7 +450,7 @@ void LayerManager::ApplyLayerDescriptor() {
450450
}
451451
}
452452

453-
layer.descriptor = init.descriptor;
453+
layer.descriptor.removed = init.descriptor.removed;
454454
}
455455
}
456456
}
@@ -497,10 +497,10 @@ void LayerManager::LoadAllInstalledLayers(ConfiguratorMode configurator_mode) {
497497
}
498498
}
499499

500-
this->ApplyLayerDescriptor();
500+
// this->ApplyLayerDescriptor();
501501
}
502502

503-
bool LayerManager::Validate(const Path &layer_path, QString json_text, ConfiguratorMode configurator_mode) const {
503+
LayerValidated LayerManager::Validate(const Path &layer_path, QString json_text, ConfiguratorMode configurator_mode) const {
504504
JsonValidator validator;
505505
bool result = validator.Check(json_text);
506506

@@ -543,13 +543,25 @@ bool LayerManager::Validate(const Path &layer_path, QString json_text, Configura
543543
}
544544
}
545545

546-
return result;
546+
return result ? LAYER_VALIDATE_PASS : LAYER_VALIDATE_FAIL;
547547
}
548548

549-
LayerLoadStatus LayerManager::LoadLayers(const Path &layer_path, LayerType type, ConfiguratorMode configurator_mode) {
550-
const std::string &last_modified = layer_path.LastModified();
551-
LayerDescriptor descriptor;
549+
LayerDescriptor LayerManager::GetDescriptor(const Path &layer_path, const std::string &layer_key) const {
550+
auto it = layer_init.find(layer_path);
551+
if (it != layer_init.end()) {
552+
const std::vector<LayerDisplay> &data = it->second;
553+
for (std::size_t i = 0, n = data.size(); i < n; ++i) {
554+
const LayerDisplay &display = data[i];
555+
if (display.id.key == layer_key) {
556+
return display.descriptor;
557+
}
558+
}
559+
}
560+
561+
return LayerDescriptor();
562+
}
552563

564+
LayerLoadStatus LayerManager::LoadLayers(const Path &layer_path, LayerType type, ConfiguratorMode configurator_mode) {
553565
QFile file(layer_path.AbsolutePath().c_str());
554566
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
555567
assert(0);
@@ -579,6 +591,7 @@ LayerLoadStatus LayerManager::LoadLayers(const Path &layer_path, LayerType type,
579591
}
580592

581593
LayerLoadStatus status = LAYER_LOAD_ADDED;
594+
const std::string &last_modified = layer_path.LastModified();
582595

583596
if (json_root_object.value("layers") != QJsonValue::Undefined) {
584597
const QJsonArray &json_layers_array = json_root_object.value("layers").toArray();
@@ -589,32 +602,37 @@ LayerLoadStatus LayerManager::LoadLayers(const Path &layer_path, LayerType type,
589602
std::string key = ReadStringValue(json_layer_object, "name");
590603

591604
if (key == "VK_LAYER_LUNARG_override" || !(key.rfind("VK_", 0) == 0)) {
592-
return LAYER_LOAD_IGNORED;
605+
status = LAYER_LOAD_IGNORED;
606+
continue;
593607
}
594-
}
595608

596-
if (this->validate_manifests) {
597-
descriptor.validated = this->Validate(layer_path, json_text, configurator_mode);
598-
}
609+
LayerDescriptor descriptor = this->GetDescriptor(layer_path, key);
599610

600-
for (int i = 0, n = json_layers_array.size(); i < n; ++i) {
601-
const QJsonObject &json_layer_object = json_layers_array[i].toObject();
602-
status = this->LoadLayer(json_layer_object, layer_path, type, last_modified, file_format_version, descriptor);
611+
if (this->validate_manifests && (descriptor.last_modified != last_modified) || !descriptor.validated) {
612+
descriptor.validated = this->Validate(layer_path, json_text, configurator_mode);
613+
descriptor.last_modified = last_modified;
614+
}
615+
616+
status = this->LoadLayer(json_layer_object, layer_path, type, file_format_version, descriptor);
603617
}
604618
} else if (json_root_object.value("layer") != QJsonValue::Undefined) {
605619
const QJsonObject &json_layer_object = json_root_object.value("layer").toObject();
606620

607-
std::string key = ReadStringValue(json_layer_object, "name");
621+
std::string layer_key = ReadStringValue(json_layer_object, "name");
608622

609-
if (key == "VK_LAYER_LUNARG_override" || !(key.rfind("VK_", 0) == 0)) {
623+
if (layer_key == "VK_LAYER_LUNARG_override" || !(layer_key.rfind("VK_", 0) == 0)) {
610624
return LAYER_LOAD_IGNORED;
611625
}
612626

613-
if (this->validate_manifests) {
627+
LayerDescriptor descriptor = this->GetDescriptor(layer_path, layer_key);
628+
629+
if (this->validate_manifests &&
630+
(descriptor.last_modified != last_modified || descriptor.validated == LAYER_VALIDATE_NONE)) {
614631
descriptor.validated = this->Validate(layer_path, json_text, configurator_mode);
632+
descriptor.last_modified = last_modified;
615633
}
616634

617-
status = this->LoadLayer(json_layer_object, layer_path, type, last_modified, file_format_version, descriptor);
635+
status = this->LoadLayer(json_layer_object, layer_path, type, file_format_version, descriptor);
618636
} else {
619637
assert(0);
620638
}
@@ -623,11 +641,10 @@ LayerLoadStatus LayerManager::LoadLayers(const Path &layer_path, LayerType type,
623641
}
624642

625643
LayerLoadStatus LayerManager::LoadLayer(const QJsonObject &json_layer_object, const Path &layer_path, LayerType type,
626-
const std::string &last_modified, Version file_format_version, LayerDescriptor descriptor) {
644+
Version file_format_version, LayerDescriptor descriptor) {
627645
Layer layer;
628646
layer.type = type;
629647
layer.manifest_path = layer_path;
630-
layer.last_modified = last_modified;
631648
layer.file_format_version = file_format_version;
632649
layer.descriptor = descriptor;
633650

@@ -641,7 +658,7 @@ LayerLoadStatus LayerManager::LoadLayer(const QJsonObject &json_layer_object, co
641658
if (duplicated_layer->descriptor.removed) {
642659
duplicated_layer->descriptor.removed = false;
643660
duplicated_layer->descriptor.enabled = true;
644-
} else if (duplicated_layer->last_modified != layer.last_modified) {
661+
} else if (duplicated_layer->descriptor.last_modified != layer.descriptor.last_modified) {
645662
// Reload when the manifest was updated
646663
LayerLoadStatus reloaded_status = duplicated_layer->Load(json_layer_object);
647664
status = reloaded_status == LAYER_LOAD_ADDED ? LAYER_LOAD_RELOADED : reloaded_status;
@@ -704,10 +721,11 @@ std::map<Path, std::map<std::string, LayerDisplay>> LayerManager::BuildLayerStor
704721
if (layer == nullptr) {
705722
continue;
706723
}
724+
/*
707725
if (layer->descriptor.removed) {
708726
continue;
709727
}
710-
728+
*/
711729
LayerDisplay layer_display;
712730
layer_display.id.manifest_path = layer->manifest_path;
713731
layer_display.id.key = layer->key;
@@ -729,6 +747,20 @@ std::map<Path, std::map<std::string, LayerDisplay>> LayerManager::BuildLayerStor
729747
}
730748
}
731749

750+
for (auto it = this->layer_init.begin(), end = this->layer_init.end(); it != end; ++it) {
751+
auto jt = result.find(it->first);
752+
if (jt == result.end()) {
753+
const std::vector<LayerDisplay> &display = it->second;
754+
std::map<std::string, LayerDisplay> entry;
755+
756+
for (std::size_t i = 0, n = display.size(); i < n; ++i) {
757+
entry.insert(std::make_pair(display[i].id.key, display[i]));
758+
}
759+
760+
result.insert(std::make_pair(it->first, entry));
761+
}
762+
}
763+
732764
return result;
733765
}
734766

@@ -759,7 +791,18 @@ std::vector<Path> LayerManager::BuildLayerPaths() const {
759791
std::vector<Path> result;
760792

761793
for (auto it = this->layer_init.begin(); it != this->layer_init.end(); ++it) {
762-
result.push_back(it->first);
794+
bool keep = false;
795+
796+
for (auto jt = it->second.begin(); jt != it->second.end(); ++jt) {
797+
if (!jt->descriptor.removed) {
798+
keep = true;
799+
break;
800+
}
801+
}
802+
803+
if (keep) {
804+
result.push_back(it->first);
805+
}
763806
}
764807

765808
return result;

vkconfig_core/layer_manager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class LayerManager : public Serialize {
6969
std::set<LayerDisplay> BuildLayerDisplayList() const;
7070
std::map<Path, std::map<std::string, LayerDisplay>> BuildLayerStoreList() const;
7171
std::vector<Path> BuildLayerPaths() const;
72+
LayerDescriptor GetDescriptor(const Path& layer_path, const std::string& layer_key) const;
7273

7374
std::vector<std::string> GatherLayerNames() const;
7475

@@ -83,10 +84,10 @@ class LayerManager : public Serialize {
8384
}
8485

8586
private:
86-
bool Validate(const Path& layer_path, QString json_text, ConfiguratorMode configurator_mode) const;
87+
LayerValidated Validate(const Path& layer_path, QString json_text, ConfiguratorMode configurator_mode) const;
8788

8889
LayerLoadStatus LoadLayer(const QJsonObject& json_layer_object, const Path& layer_path, LayerType type,
89-
const std::string& last_modified, Version file_format_version, LayerDescriptor descriptor);
90+
Version file_format_version, LayerDescriptor descriptor);
9091

9192
void ApplyLayerDescriptor();
9293

vkconfig_core/test/test_layer_manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ TEST(test_layer_manager, FindLastModified) {
185185
const std::size_t initial_count = layer_manager.Size();
186186

187187
Layer* layer204_modified = layer_manager.FindFromManifest(":/layers/VK_LAYER_LUNARG_version_204.json", false);
188-
layer204_modified->last_modified = "1";
188+
layer204_modified->descriptor.last_modified = "1";
189189
Path modified_path = layer204_modified->manifest_path;
190190
layer204_modified->manifest_path = ":/layers/VK_LAYER_LUNARG_version_204_copy.json";
191191

@@ -194,15 +194,15 @@ TEST(test_layer_manager, FindLastModified) {
194194
EXPECT_EQ(initial_count + 1, reloaded_count);
195195

196196
Layer* layer204 = layer_manager.FindFromManifest(":/layers/VK_LAYER_LUNARG_version_204.json", false);
197-
layer204->last_modified = "0";
197+
layer204->descriptor.last_modified = "0";
198198

199199
Layer* layer204_first = layer_manager.FindFromManifest(":/layers/VK_LAYER_LUNARG_version_204_copy.json", false);
200200
Layer* layer204_copy = layer_manager.FindFromManifest(":/layers/VK_LAYER_LUNARG_version_204.json", false);
201201

202202
const Layer* last0 = layer_manager.FindLastModified("VK_LAYER_LUNARG_version", Version(1, 3, 204));
203203
EXPECT_EQ(layer204_first, last0);
204204

205-
layer204_copy->last_modified = "2";
205+
layer204_copy->descriptor.last_modified = "2";
206206
const Layer* last1 = layer_manager.FindLastModified("VK_LAYER_LUNARG_version", Version(1, 3, 204));
207207
EXPECT_EQ(layer204_copy, last1);
208208
}

0 commit comments

Comments
 (0)