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

gpu / ipc / service / built_in_shader_cache_loader.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_IPC_SERVICE_BUILT_IN_SHADER_CACHE_LOADER_H_
#define GPU_IPC_SERVICE_BUILT_IN_SHADER_CACHE_LOADER_H_

#include <memory>
#include <vector>

#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/synchronization/waitable_event.h"
#include "gpu/ipc/service/gpu_ipc_service_export.h"

namespace gpu {

// BuiltInShaderCacheLoader loads the metal shaders that are packaged with
// chrome. Loading happens in the background. StartLoading() should be called
// early in startup to start the loading, and later on the values taken by way
// of TakeEntries().
class GPU_IPC_SERVICE_EXPORT BuiltInShaderCacheLoader {
 public:
  struct CacheEntry {
    CacheEntry();
    CacheEntry(CacheEntry&& other);
    ~CacheEntry();

    std::vector<uint8_t> key;
    std::vector<uint8_t> value;
  };

  // Starts loading the cache.
  static void StartLoading();

  // Returns the entries from the cache and deletes it. This blocks until the
  // cache is loaded.
  static std::unique_ptr<std::vector<CacheEntry>> TakeEntries();

 private:
  FRIEND_TEST_ALL_PREFIXES(BuiltInShaderCacheTest, Basic);

  BuiltInShaderCacheLoader();
  ~BuiltInShaderCacheLoader();

  // Called on the background thread to load the cache.
  void Load(const base::FilePath& path = base::FilePath());

  // Does the real work of loading the cache.
  void LoadImpl(const base::FilePath& path);

  // Returns the entries from the cache and deletes it. This blocks until the
  // cache is loaded.
  std::unique_ptr<std::vector<CacheEntry>> TakeEntriesImpl();

  std::vector<BuiltInShaderCacheLoader::CacheEntry> entries_;

  // Signaled when the cache is finished loading.
  base::WaitableEvent loaded_signaler_;
};

}  // namespace gpu

#endif  // GPU_IPC_SERVICE_BUILT_IN_SHADER_CACHE_LOADER_H_