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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/gpu/shader_disk_cache.h"
#include "base/threading/thread_checker.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/public/browser/browser_thread.h"
#include "gpu/command_buffer/common/constants.h"
#include "net/base/cache_type.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
namespace content {
namespace {
static const base::FilePath::CharType kGpuCachePath[] =
FILE_PATH_LITERAL("GPUCache");
void EntryCloser(disk_cache::Entry* entry) {
entry->Close();
}
} // namespace
// ShaderDiskCacheEntry handles the work of caching/updating the cached
// shaders.
class ShaderDiskCacheEntry
: public base::ThreadChecker,
public base::RefCounted<ShaderDiskCacheEntry> {
public:
ShaderDiskCacheEntry(base::WeakPtr<ShaderDiskCache> cache,
const std::string& key,
const std::string& shader);
void Cache();
private:
friend class base::RefCounted<ShaderDiskCacheEntry>;
enum OpType {
TERMINATE,
OPEN_ENTRY,
WRITE_DATA,
CREATE_ENTRY,
};
~ShaderDiskCacheEntry();
void OnOpComplete(int rv);
int OpenCallback(int rv);
int WriteCallback(int rv);
int IOComplete(int rv);
base::WeakPtr<ShaderDiskCache> cache_;
OpType op_type_;
std::string key_;
std::string shader_;
disk_cache::Entry* entry_;
DISALLOW_COPY_AND_ASSIGN(ShaderDiskCacheEntry);
};
// ShaderDiskReadHelper is used to load all of the cached shaders from the
// disk cache and send to the memory cache.
class ShaderDiskReadHelper
: public base::ThreadChecker,
public base::RefCounted<ShaderDiskReadHelper> {
public:
ShaderDiskReadHelper(base::WeakPtr<ShaderDiskCache> cache, int host_id);
void LoadCache();
private:
friend class base::RefCounted<ShaderDiskReadHelper>;
enum OpType {
TERMINATE,
OPEN_NEXT,
OPEN_NEXT_COMPLETE,
READ_COMPLETE,
ITERATION_FINISHED
};
~ShaderDiskReadHelper();
void OnOpComplete(int rv);
int OpenNextEntry();
int OpenNextEntryComplete(int rv);
int ReadComplete(int rv);
int IterationComplete(int rv);
base::WeakPtr<ShaderDiskCache> cache_;
OpType op_type_;
void* iter_;
scoped_refptr<net::IOBufferWithSize> buf_;
int host_id_;
disk_cache::Entry* entry_;
DISALLOW_COPY_AND_ASSIGN(ShaderDiskReadHelper);
};
class ShaderClearHelper
: public base::RefCounted<ShaderClearHelper>,
public base::SupportsWeakPtr<ShaderClearHelper> {
public:
ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache,
const base::FilePath& path,
const base::Time& delete_begin,
const base::Time& delete_end,
const base::Closure& callback);
void Clear();
private:
friend class base::RefCounted<ShaderClearHelper>;
enum OpType {
TERMINATE,
VERIFY_CACHE_SETUP,
DELETE_CACHE
};
~ShaderClearHelper();
void DoClearShaderCache(int rv);
scoped_refptr<ShaderDiskCache> cache_;
OpType op_type_;
base::FilePath path_;
base::Time delete_begin_;
base::Time delete_end_;
base::Closure callback_;
DISALLOW_COPY_AND_ASSIGN(ShaderClearHelper);
};
ShaderDiskCacheEntry::ShaderDiskCacheEntry(base::WeakPtr<ShaderDiskCache> cache,
const std::string& key,
const std::string& shader)
: cache_(cache),
op_type_(OPEN_ENTRY),
key_(key),
shader_(shader),
entry_(NULL) {
}
ShaderDiskCacheEntry::~ShaderDiskCacheEntry() {
if (entry_)
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&EntryCloser, entry_));
}
void ShaderDiskCacheEntry::Cache() {
DCHECK(CalledOnValidThread());
if (!cache_.get())
return;
int rv = cache_->backend()->OpenEntry(
key_,
&entry_,
base::Bind(&ShaderDiskCacheEntry::OnOpComplete, this));
if (rv != net::ERR_IO_PENDING)
OnOpComplete(rv);
}
void ShaderDiskCacheEntry::OnOpComplete(int rv) {
DCHECK(CalledOnValidThread());
if (!cache_.get())
return;
do {
switch (op_type_) {
case OPEN_ENTRY:
rv = OpenCallback(rv);
break;
case CREATE_ENTRY:
rv = WriteCallback(rv);
break;
case WRITE_DATA:
rv = IOComplete(rv);
break;
case TERMINATE:
rv = net::ERR_IO_PENDING; // break the loop.
break;
default:
NOTREACHED(); // Invalid op_type_ provided.
break;
}
} while (rv != net::ERR_IO_PENDING);
}
int ShaderDiskCacheEntry::OpenCallback(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
if (rv == net::OK) {
cache_->backend()->OnExternalCacheHit(key_);
cache_->EntryComplete(this);
op_type_ = TERMINATE;
return rv;
}
op_type_ = CREATE_ENTRY;
return cache_->backend()->CreateEntry(
key_,
&entry_,
base::Bind(&ShaderDiskCacheEntry::OnOpComplete, this));
}
int ShaderDiskCacheEntry::WriteCallback(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
if (rv != net::OK) {
LOG(ERROR) << "Failed to create shader cache entry: " << rv;
cache_->EntryComplete(this);
op_type_ = TERMINATE;
return rv;
}
op_type_ = WRITE_DATA;
scoped_refptr<net::StringIOBuffer> io_buf = new net::StringIOBuffer(shader_);
return entry_->WriteData(
1,
0,
io_buf.get(),
shader_.length(),
base::Bind(&ShaderDiskCacheEntry::OnOpComplete, this),
false);
}
int ShaderDiskCacheEntry::IOComplete(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
cache_->EntryComplete(this);
op_type_ = TERMINATE;
return rv;
}
ShaderDiskReadHelper::ShaderDiskReadHelper(
base::WeakPtr<ShaderDiskCache> cache,
int host_id)
: cache_(cache),
op_type_(OPEN_NEXT),
iter_(NULL),
buf_(NULL),
host_id_(host_id),
entry_(NULL) {
}
void ShaderDiskReadHelper::LoadCache() {
DCHECK(CalledOnValidThread());
if (!cache_.get())
return;
OnOpComplete(net::OK);
}
void ShaderDiskReadHelper::OnOpComplete(int rv) {
DCHECK(CalledOnValidThread());
if (!cache_.get())
return;
do {
switch (op_type_) {
case OPEN_NEXT:
rv = OpenNextEntry();
break;
case OPEN_NEXT_COMPLETE:
rv = OpenNextEntryComplete(rv);
break;
case READ_COMPLETE:
rv = ReadComplete(rv);
break;
case ITERATION_FINISHED:
rv = IterationComplete(rv);
break;
case TERMINATE:
cache_->ReadComplete();
rv = net::ERR_IO_PENDING; // break the loop
break;
default:
NOTREACHED(); // Invalid state for read helper
rv = net::ERR_FAILED;
break;
}
} while (rv != net::ERR_IO_PENDING);
}
int ShaderDiskReadHelper::OpenNextEntry() {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
op_type_ = OPEN_NEXT_COMPLETE;
return cache_->backend()->OpenNextEntry(
&iter_,
&entry_,
base::Bind(&ShaderDiskReadHelper::OnOpComplete, this));
}
int ShaderDiskReadHelper::OpenNextEntryComplete(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
if (rv == net::ERR_FAILED) {
op_type_ = ITERATION_FINISHED;
return net::OK;
}
if (rv < 0)
return rv;
op_type_ = READ_COMPLETE;
buf_ = new net::IOBufferWithSize(entry_->GetDataSize(1));
return entry_->ReadData(
1,
0,
buf_.get(),
buf_->size(),
base::Bind(&ShaderDiskReadHelper::OnOpComplete, this));
}
int ShaderDiskReadHelper::ReadComplete(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
if (rv && rv == buf_->size()) {
GpuProcessHost* host = GpuProcessHost::FromID(host_id_);
if (host)
host->LoadedShader(entry_->GetKey(), std::string(buf_->data(),
buf_->size()));
}
buf_ = NULL;
entry_->Close();
entry_ = NULL;
op_type_ = OPEN_NEXT;
return net::OK;
}
int ShaderDiskReadHelper::IterationComplete(int rv) {
DCHECK(CalledOnValidThread());
// Called through OnOpComplete, so we know |cache_| is valid.
cache_->backend()->EndEnumeration(&iter_);
iter_ = NULL;
op_type_ = TERMINATE;
return net::OK;
}
ShaderDiskReadHelper::~ShaderDiskReadHelper() {
if (entry_)
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&EntryCloser, entry_));
}
ShaderClearHelper::ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache,
const base::FilePath& path,
const base::Time& delete_begin,
const base::Time& delete_end,
const base::Closure& callback)
: cache_(cache),
op_type_(VERIFY_CACHE_SETUP),
path_(path),
delete_begin_(delete_begin),
delete_end_(delete_end),
callback_(callback) {
}
ShaderClearHelper::~ShaderClearHelper() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
void ShaderClearHelper::Clear() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DoClearShaderCache(net::OK);
}
void ShaderClearHelper::DoClearShaderCache(int rv) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Hold a ref to ourselves so when we do the CacheCleared call we don't get
// auto-deleted when our ref count drops to zero.
scoped_refptr<ShaderClearHelper> helper = this;
while (rv != net::ERR_IO_PENDING) {
switch (op_type_) {
case VERIFY_CACHE_SETUP:
rv = cache_->SetAvailableCallback(
base::Bind(&ShaderClearHelper::DoClearShaderCache, AsWeakPtr()));
op_type_ = DELETE_CACHE;
break;
case DELETE_CACHE:
rv = cache_->Clear(
delete_begin_, delete_end_,
base::Bind(&ShaderClearHelper::DoClearShaderCache, AsWeakPtr()));
op_type_ = TERMINATE;
break;
case TERMINATE:
ShaderCacheFactory::GetInstance()->CacheCleared(path_);
callback_.Run();
rv = net::ERR_IO_PENDING; // Break the loop.
break;
default:
NOTREACHED(); // Invalid state provided.
op_type_ = TERMINATE;
break;
}
}
}
// static
ShaderCacheFactory* ShaderCacheFactory::GetInstance() {
return Singleton<ShaderCacheFactory,
LeakySingletonTraits<ShaderCacheFactory> >::get();
}
ShaderCacheFactory::ShaderCacheFactory() {
}
ShaderCacheFactory::~ShaderCacheFactory() {
}
void ShaderCacheFactory::SetCacheInfo(int32 client_id,
const base::FilePath& path) {
client_id_to_path_map_[client_id] = path;
}
void ShaderCacheFactory::RemoveCacheInfo(int32 client_id) {
client_id_to_path_map_.erase(client_id);
}
scoped_refptr<ShaderDiskCache> ShaderCacheFactory::Get(int32 client_id) {
ClientIdToPathMap::iterator iter =
client_id_to_path_map_.find(client_id);
if (iter == client_id_to_path_map_.end())
return NULL;
return ShaderCacheFactory::GetByPath(iter->second);
}
scoped_refptr<ShaderDiskCache> ShaderCacheFactory::GetByPath(
const base::FilePath& path) {
ShaderCacheMap::iterator iter = shader_cache_map_.find(path);
if (iter != shader_cache_map_.end())
return iter->second;
ShaderDiskCache* cache = new ShaderDiskCache(path);
cache->Init();
return cache;
}
void ShaderCacheFactory::AddToCache(const base::FilePath& key,
ShaderDiskCache* cache) {
shader_cache_map_[key] = cache;
}
void ShaderCacheFactory::RemoveFromCache(const base::FilePath& key) {
shader_cache_map_.erase(key);
}
void ShaderCacheFactory::ClearByPath(const base::FilePath& path,
const base::Time& delete_begin,
const base::Time& delete_end,
const base::Closure& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(!callback.is_null());
scoped_refptr<ShaderClearHelper> helper = new ShaderClearHelper(
GetByPath(path), path, delete_begin, delete_end, callback);
// We could receive requests to clear the same path with different
// begin/end times. So, we keep a list of requests. If we haven't seen this
// path before we kick off the clear and add it to the list. If we have see it
// already, then we already have a clear running. We add this clear to the
// list and wait for any previous clears to finish.
ShaderClearMap::iterator iter = shader_clear_map_.find(path);
if (iter != shader_clear_map_.end()) {
iter->second.push(helper);
return;
}
shader_clear_map_.insert(
std::pair<base::FilePath, ShaderClearQueue>(path, ShaderClearQueue()));
shader_clear_map_[path].push(helper);
helper->Clear();
}
void ShaderCacheFactory::CacheCleared(const base::FilePath& path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ShaderClearMap::iterator iter = shader_clear_map_.find(path);
if (iter == shader_clear_map_.end()) {
LOG(ERROR) << "Completed clear but missing clear helper.";
return;
}
iter->second.pop();
// If there are remaining items in the list we trigger the Clear on the
// next one.
if (!iter->second.empty()) {
iter->second.front()->Clear();
return;
}
shader_clear_map_.erase(path);
}
ShaderDiskCache::ShaderDiskCache(const base::FilePath& cache_path)
: cache_available_(false),
host_id_(0),
cache_path_(cache_path),
is_initialized_(false) {
ShaderCacheFactory::GetInstance()->AddToCache(cache_path_, this);
}
ShaderDiskCache::~ShaderDiskCache() {
ShaderCacheFactory::GetInstance()->RemoveFromCache(cache_path_);
}
void ShaderDiskCache::Init() {
if (is_initialized_) {
NOTREACHED(); // can't initialize disk cache twice.
return;
}
is_initialized_ = true;
int rv = disk_cache::CreateCacheBackend(
net::SHADER_CACHE,
net::CACHE_BACKEND_BLOCKFILE,
cache_path_.Append(kGpuCachePath),
gpu::kDefaultMaxProgramCacheMemoryBytes,
true,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(),
NULL,
&backend_,
base::Bind(&ShaderDiskCache::CacheCreatedCallback, this));
if (rv == net::OK)
cache_available_ = true;
}
void ShaderDiskCache::Cache(const std::string& key, const std::string& shader) {
if (!cache_available_)
return;
ShaderDiskCacheEntry* shim =
new ShaderDiskCacheEntry(AsWeakPtr(), key, shader);
shim->Cache();
entry_map_[shim] = shim;
}
int ShaderDiskCache::Clear(
const base::Time begin_time, const base::Time end_time,
const net::CompletionCallback& completion_callback) {
int rv;
if (begin_time.is_null()) {
rv = backend_->DoomAllEntries(completion_callback);
} else {
rv = backend_->DoomEntriesBetween(begin_time, end_time,
completion_callback);
}
return rv;
}
int32 ShaderDiskCache::Size() {
if (!cache_available_)
return -1;
return backend_->GetEntryCount();
}
int ShaderDiskCache::SetAvailableCallback(
const net::CompletionCallback& callback) {
if (cache_available_)
return net::OK;
available_callback_ = callback;
return net::ERR_IO_PENDING;
}
void ShaderDiskCache::CacheCreatedCallback(int rv) {
if (rv != net::OK) {
LOG(ERROR) << "Shader Cache Creation failed: " << rv;
return;
}
helper_ = new ShaderDiskReadHelper(AsWeakPtr(), host_id_);
helper_->LoadCache();
}
void ShaderDiskCache::EntryComplete(void* entry) {
entry_map_.erase(entry);
if (entry_map_.empty() && !cache_complete_callback_.is_null())
cache_complete_callback_.Run(net::OK);
}
void ShaderDiskCache::ReadComplete() {
helper_ = NULL;
// The cache is considered available after we have finished reading any
// of the old cache values off disk. This prevents a potential race where we
// are reading from disk and execute a cache clear at the same time.
cache_available_ = true;
if (!available_callback_.is_null()) {
available_callback_.Run(net::OK);
available_callback_.Reset();
}
}
int ShaderDiskCache::SetCacheCompleteCallback(
const net::CompletionCallback& callback) {
if (entry_map_.empty()) {
return net::OK;
}
cache_complete_callback_ = callback;
return net::ERR_IO_PENDING;
}
} // namespace content
|