1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
cc / test / transfer_cache_test_helper.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/test/transfer_cache_test_helper.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
namespace cc {
TransferCacheTestHelper::TransferCacheTestHelper(GrDirectContext* context)
: context_(context) {
if (!context_) {
owned_context_ = GrDirectContext::MakeMock(nullptr);
context_ = owned_context_.get();
}
}
TransferCacheTestHelper::~TransferCacheTestHelper() = default;
bool TransferCacheTestHelper::LockEntryDirect(const EntryKey& key) {
return LockEntryInternal(key);
}
void TransferCacheTestHelper::CreateEntryDirect(const EntryKey& key,
base::span<uint8_t> data) {
// Deserialize into a service transfer cache entry.
std::unique_ptr<ServiceTransferCacheEntry> service_entry =
ServiceTransferCacheEntry::Create(key.first);
if (!service_entry)
return;
bool success =
service_entry->Deserialize(context_, /*graphite_recorder=*/nullptr, data);
if (!success)
return;
last_added_entry_ = key;
// Put things into the cache.
entries_.emplace(key, std::move(service_entry));
locked_entries_.insert(key);
EnforceLimits();
}
void TransferCacheTestHelper::CreateLocalEntry(
uint32_t id,
std::unique_ptr<ServiceTransferCacheEntry> entry) {
auto key = std::make_pair(entry->Type(), id);
DeleteEntryDirect(key);
entries_[key] = std::move(entry);
local_entries_.insert(key);
last_added_entry_ = key;
}
void TransferCacheTestHelper::UnlockEntriesDirect(
const std::vector<EntryKey>& keys) {
for (const auto& key : keys) {
locked_entries_.erase(key);
}
EnforceLimits();
}
void TransferCacheTestHelper::DeleteEntryDirect(const EntryKey& key) {
locked_entries_.erase(key);
local_entries_.erase(key);
entries_.erase(key);
}
void TransferCacheTestHelper::SetGrContext(GrDirectContext* context) {
context_ = context;
}
void TransferCacheTestHelper::SetCachedItemsLimit(size_t limit) {
cached_items_limit_ = limit;
EnforceLimits();
}
ServiceTransferCacheEntry* TransferCacheTestHelper::GetEntryInternal(
TransferCacheEntryType type,
uint32_t id) {
auto key = std::make_pair(type, id);
if (locked_entries_.count(key) + local_entries_.count(key) == 0) {
return nullptr;
}
if (!base::Contains(entries_, key)) {
return nullptr;
}
return entries_[key].get();
}
bool TransferCacheTestHelper::LockEntryInternal(const EntryKey& key) {
if (!base::Contains(entries_, key)) {
return false;
}
locked_entries_.insert(key);
EnforceLimits();
return true;
}
uint32_t TransferCacheTestHelper::CreateEntryInternal(
const ClientTransferCacheEntry& client_entry,
uint8_t* memory) {
auto key = std::make_pair(client_entry.Type(), client_entry.Id());
DCHECK(!base::Contains(entries_, key));
// Serialize data.
uint32_t size = client_entry.SerializedSize();
auto data = base::HeapArray<uint8_t>::Uninit(size);
auto span = base::span(data);
bool success = client_entry.Serialize(span);
DCHECK(success);
CreateEntryDirect(key, span);
return 0u;
}
void TransferCacheTestHelper::FlushEntriesInternal(std::set<EntryKey> keys) {
for (auto& key : keys)
locked_entries_.erase(key);
EnforceLimits();
}
void TransferCacheTestHelper::EnforceLimits() {
for (auto it = entries_.begin(); it != entries_.end();) {
if (entries_.size() <= cached_items_limit_)
break;
auto found = locked_entries_.find(it->first);
if (found == locked_entries_.end()) {
it = entries_.erase(it);
} else {
++it;
}
}
}
} // namespace cc