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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
gpu / command_buffer / service / passthrough_program_cache.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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/service/passthrough_program_cache.h"
#include <stddef.h>
#include <string_view>
#include <utility>
#include "base/base64.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_display.h"
#include "ui/gl/gl_surface_egl.h"
namespace gpu {
namespace gles2 {
namespace {
bool BlobCacheExtensionAvailable(gl::GLDisplayEGL* gl_display) {
// The display should be initialized if the extension is available.
return gl_display->ext->b_EGL_ANDROID_blob_cache;
}
// EGL_ANDROID_blob_cache doesn't give user pointer to the callbacks so we are
// forced to have this be global.
PassthroughProgramCache* g_program_cache = nullptr;
// Blob cache function pointers can only be set once per EGLDisplay. Using a
// single bool is not technically correct, but it's acceptable since we only
// have one EGLDisplay in practice.
bool g_blob_cache_funcs_set = false;
} // namespace
PassthroughProgramCache::PassthroughProgramCache(
size_t max_cache_size_bytes,
bool disable_gpu_shader_disk_cache,
ValueAddedHook* value_added_hook)
: ProgramCache(max_cache_size_bytes),
disable_gpu_shader_disk_cache_(disable_gpu_shader_disk_cache),
curr_size_bytes_(0),
store_(ProgramLRUCache::NO_AUTO_EVICT),
value_added_hook_(value_added_hook) {
gl::GLDisplayEGL* gl_display = gl::GLSurfaceEGL::GetGLDisplayEGL();
EGLDisplay egl_display = gl_display->GetDisplay();
DCHECK(!g_program_cache);
g_program_cache = this;
// display is EGL_NO_DISPLAY during unittests.
if (egl_display != EGL_NO_DISPLAY && !g_blob_cache_funcs_set &&
BlobCacheExtensionAvailable(gl_display)) {
// Register the blob cache callbacks.
eglSetBlobCacheFuncsANDROID(egl_display, BlobCacheSet, BlobCacheGet);
g_blob_cache_funcs_set = true;
}
}
PassthroughProgramCache::~PassthroughProgramCache() {
// Clear up the blob cache callbacks. Note that this not allowed by the
// EGL_ANDROID_blob_cache spec, so we just set the pointer to this object to
// nullptr as a workaround. The callbacks don't work with this pointer
// missing.
g_program_cache = nullptr;
}
void PassthroughProgramCache::ClearBackend() {
base::AutoLock auto_lock(lock_);
store_.Clear();
DCHECK_EQ(0U, curr_size_bytes_);
}
ProgramCache::ProgramLoadResult PassthroughProgramCache::LoadLinkedProgram(
GLuint program,
Shader* shader_a,
Shader* shader_b,
const LocationMap* bind_attrib_location_map,
const std::vector<std::string>& transform_feedback_varyings,
GLenum transform_feedback_buffer_mode,
DecoderClient* client) {
NOTREACHED();
}
void PassthroughProgramCache::SaveLinkedProgram(
GLuint program,
const Shader* shader_a,
const Shader* shader_b,
const LocationMap* bind_attrib_location_map,
const std::vector<std::string>& transform_feedback_varyings,
GLenum transform_feedback_buffer_mode,
DecoderClient* client) {
NOTREACHED();
}
void PassthroughProgramCache::LoadProgram(const std::string& key,
const std::string& program) {
base::AutoLock auto_lock(lock_);
if (!CacheEnabled()) {
return;
}
std::string key_decoded;
std::string program_decoded;
base::Base64Decode(key, &key_decoded);
base::Base64Decode(program, &program_decoded);
Key entry_key(key_decoded.begin(), key_decoded.end());
Value entry_value(program_decoded.begin(), program_decoded.end());
store_.Put(entry_key, ProgramCacheValue(std::move(entry_value), this));
}
size_t PassthroughProgramCache::Trim(size_t limit) {
base::AutoLock auto_lock(lock_);
size_t initial_size = curr_size_bytes_;
while (curr_size_bytes_ > limit) {
DCHECK(!store_.empty());
store_.Erase(store_.rbegin());
}
return initial_size - curr_size_bytes_;
}
bool PassthroughProgramCache::CacheEnabled() const {
return !disable_gpu_shader_disk_cache_;
}
void PassthroughProgramCache::Set(Key&& key,
Value&& value,
CacheProgramCallback callback) {
{
base::AutoLock auto_lock(lock_);
// If the value is so big it will never fit in the cache, throw it away.
if (value.size() > max_size_bytes()) {
return;
}
// Evict any cached program with the same key in favor of the least recently
// accessed.
ProgramLRUCache::iterator existing = store_.Peek(key);
if (existing != store_.end()) {
store_.Erase(existing);
}
// If the cache is overflowing, remove some old entries.
DCHECK(max_size_bytes() >= value.size());
}
Trim(max_size_bytes() - value.size());
// If callback is set, notify that there was a new/updated blob entry so it
// can be stored in disk. Note that this is done before the Put() call as
// that consumes `value`.
if (callback) {
// Convert the key and binary to string form.
std::string_view key_string(reinterpret_cast<const char*>(key.data()),
key.size());
std::string_view value_string(reinterpret_cast<const char*>(value.data()),
value.size());
std::string key_string_64 = base::Base64Encode(key_string);
std::string value_string_64 = base::Base64Encode(value_string);
callback.Run(key_string_64, value_string_64);
}
{
base::AutoLock auto_lock(lock_);
if (value_added_hook_) {
value_added_hook_->OnValueAddedToCache(key, value);
}
store_.Put(key, ProgramCacheValue(std::move(value), this));
}
}
size_t PassthroughProgramCache::Get(const Key& key,
void* out_value,
size_t value_size) {
// Note that the |lock_| should be held during whole time ProgramCacheValue is
// being accessed below.
base::AutoLock auto_lock(lock_);
ProgramLRUCache::iterator found = store_.Get(key);
UMA_HISTOGRAM_BOOLEAN("Gpu.PassthroughProgramCacheLoadHitInCache",
found != store_.end());
if (found == store_.end()) {
return 0;
}
const PassthroughProgramCache::Value& entry_value = found->second.data();
if (value_size > 0) {
if (static_cast<size_t>(value_size) >= entry_value.size()) {
memcpy(out_value, entry_value.data(), entry_value.size());
}
}
return entry_value.size();
}
EGLsizeiANDROID PassthroughProgramCache::BlobCacheGetImpl(
const void* key,
EGLsizeiANDROID key_size,
void* value,
EGLsizeiANDROID value_size) {
if (key_size < 0) {
return 0;
}
const uint8_t* key_begin = reinterpret_cast<const uint8_t*>(key);
PassthroughProgramCache::Key entry_key(key_begin, key_begin + key_size);
return Get(entry_key, value, value_size);
}
void PassthroughProgramCache::BlobCacheSetImpl(const void* key,
EGLsizeiANDROID key_size,
const void* value,
EGLsizeiANDROID value_size) {
if (key_size < 0 || value_size < 0) {
return;
}
const uint8_t* key_begin = reinterpret_cast<const uint8_t*>(key);
PassthroughProgramCache::Key entry_key(key_begin, key_begin + key_size);
const uint8_t* value_begin = reinterpret_cast<const uint8_t*>(value);
PassthroughProgramCache::Value entry_value(value_begin,
value_begin + value_size);
CacheProgramCallback callback;
{
base::AutoLock auto_lock(lock_);
callback = cache_program_callback_;
}
Set(std::move(entry_key), std::move(entry_value), callback);
}
void PassthroughProgramCache::BlobCacheSet(const void* key,
EGLsizeiANDROID key_size,
const void* value,
EGLsizeiANDROID value_size) {
if (!g_program_cache)
return;
g_program_cache->BlobCacheSetImpl(key, key_size, value, value_size);
}
EGLsizeiANDROID PassthroughProgramCache::BlobCacheGet(
const void* key,
EGLsizeiANDROID key_size,
void* value,
EGLsizeiANDROID value_size) {
if (!g_program_cache)
return 0;
return g_program_cache->BlobCacheGetImpl(key, key_size, value, value_size);
}
PassthroughProgramCache::ProgramCacheValue::ProgramCacheValue(
PassthroughProgramCache::Value&& program_blob,
PassthroughProgramCache* program_cache)
: program_blob_(std::move(program_blob)), program_cache_(program_cache) {
program_cache_->curr_size_bytes_ += program_blob_.size();
}
PassthroughProgramCache::ProgramCacheValue::~ProgramCacheValue() {
if (program_cache_) {
program_cache_->curr_size_bytes_ -= program_blob_.size();
}
}
PassthroughProgramCache::ProgramCacheValue::ProgramCacheValue(
ProgramCacheValue&& other)
: program_blob_(std::move(other.program_blob_)),
program_cache_(std::exchange(other.program_cache_, nullptr)) {}
PassthroughProgramCache::ProgramCacheValue&
PassthroughProgramCache::ProgramCacheValue::operator=(
ProgramCacheValue&& other) {
program_blob_ = std::move(other.program_blob_);
program_cache_ = std::exchange(other.program_cache_, nullptr);
return *this;
}
} // namespace gles2
} // namespace gpu