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

gpu / ipc / common / gpu_disk_cache_type.cc [blame]

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

#include "gpu/ipc/common/gpu_disk_cache_type.h"

#include "base/notreached.h"

namespace gpu {

std::ostream& operator<<(std::ostream& s, const GpuDiskCacheType& type) {
  switch (type) {
    case GpuDiskCacheType::kGlShaders:
      s << "gpu::GpuDiskCacheType::kGlShaders";
      break;
    case GpuDiskCacheType::kDawnWebGPU:
      s << "gpu::GpuDiskCacheType::kDawnWebGPU";
      break;
    case GpuDiskCacheType::kDawnGraphite:
      s << "gpu::GpuDiskCacheType::kDawnGraphite";
      break;
  }
  return s;
}

std::ostream& operator<<(std::ostream& s, const GpuDiskCacheHandle& handle) {
  switch (GetHandleType(handle)) {
    case GpuDiskCacheType::kGlShaders:
      s << "GlShaderHandle(" << GetHandleValue(handle) << ")";
      break;
    case GpuDiskCacheType::kDawnWebGPU:
      s << "DawnWebGPUHandle(" << GetHandleValue(handle) << ")";
      break;
    case GpuDiskCacheType::kDawnGraphite:
      s << "DawnGraphiteHandle(" << GetHandleValue(handle) << ")";
      break;
  }
  return s;
}

base::FilePath::StringType GetGpuDiskCacheSubdir(GpuDiskCacheType type) {
  switch (type) {
    case GpuDiskCacheType::kGlShaders:
      return FILE_PATH_LITERAL("GPUCache");
    case GpuDiskCacheType::kDawnWebGPU:
      return FILE_PATH_LITERAL("DawnWebGPUCache");
    case GpuDiskCacheType::kDawnGraphite:
      return FILE_PATH_LITERAL("DawnGraphiteCache");
  }
  NOTREACHED();
}

GpuDiskCacheType GetHandleType(const GpuDiskCacheHandle& handle) {
  if (absl::holds_alternative<gpu::GpuDiskCacheGlShaderHandle>(handle))
    return GpuDiskCacheType::kGlShaders;
  if (absl::holds_alternative<gpu::GpuDiskCacheDawnWebGPUHandle>(handle)) {
    return GpuDiskCacheType::kDawnWebGPU;
  }
  DCHECK(absl::holds_alternative<gpu::GpuDiskCacheDawnGraphiteHandle>(handle));
  return GpuDiskCacheType::kDawnGraphite;
}

int32_t GetHandleValue(const GpuDiskCacheHandle& handle) {
  if (absl::holds_alternative<gpu::GpuDiskCacheGlShaderHandle>(handle))
    return absl::get<gpu::GpuDiskCacheGlShaderHandle>(handle).value();
  if (absl::holds_alternative<gpu::GpuDiskCacheDawnWebGPUHandle>(handle)) {
    return absl::get<gpu::GpuDiskCacheDawnWebGPUHandle>(handle).value();
  }
  DCHECK(absl::holds_alternative<gpu::GpuDiskCacheDawnGraphiteHandle>(handle));
  return absl::get<gpu::GpuDiskCacheDawnGraphiteHandle>(handle).value();
}

bool IsReservedGpuDiskCacheHandle(const GpuDiskCacheHandle& handle) {
  if (absl::holds_alternative<gpu::GpuDiskCacheGlShaderHandle>(handle)) {
    const auto& gl_shader_handle =
        absl::get<gpu::GpuDiskCacheGlShaderHandle>(handle);
    return gl_shader_handle == kDisplayCompositorGpuDiskCacheHandle ||
           gl_shader_handle == kGrShaderGpuDiskCacheHandle;
  }
  if (absl::holds_alternative<gpu::GpuDiskCacheDawnGraphiteHandle>(handle)) {
    const auto& dawn_graphite_handle =
        absl::get<gpu::GpuDiskCacheDawnGraphiteHandle>(handle);
    return dawn_graphite_handle == kGraphiteDawnGpuDiskCacheHandle;
  }
  return false;
}

}  // namespace gpu