File: gr_cache_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 4,768 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 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/command_buffer/service/gr_cache_controller.h"

#include <chrono>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/ipc/common/gpu_client_ids.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"

namespace gpu {
namespace raster {

GrCacheController::GrCacheController(SharedContextState* context_state)
    : context_state_(context_state),
      task_runner_(base::SingleThreadTaskRunner::HasCurrentDefault()
                       ? base::SingleThreadTaskRunner::GetCurrentDefault()
                       : nullptr) {}

GrCacheController::GrCacheController(
    SharedContextState* context_state,
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : context_state_(context_state), task_runner_(std::move(task_runner)) {}

GrCacheController::~GrCacheController() = default;

void GrCacheController::ScheduleGrContextCleanup() {
  DCHECK(context_state_->IsCurrent(nullptr));

  if (!context_state_->gr_context())
    return;

  if (!task_runner_) {
    // No way to estimate idle. Just periodically call PerformDeferredCleanup.
    PerformDeferredCleanupThrottled();
    return;
  }

  DCHECK(task_runner_->BelongsToCurrentThread());

  current_idle_id_++;
  if (!purge_gr_cache_cb_.IsCancelled())
    return;

  PerformDeferredCleanup();

  constexpr int kIdleCleanupDelaySeconds = 1;
  purge_gr_cache_cb_.Reset(base::BindOnce(&GrCacheController::PurgeGrCache,
                                          base::Unretained(this),
                                          current_idle_id_));
  task_runner_->PostDelayedTask(FROM_HERE, purge_gr_cache_cb_.callback(),
                                base::Seconds(kIdleCleanupDelaySeconds));
}

void GrCacheController::PerformDeferredCleanupThrottled() {
  constexpr base::TimeDelta kTimeout = base::Seconds(1);
  base::TimeTicks now = base::TimeTicks::Now();
  if (now - last_cleanup_timestamp_ < kTimeout) {
    return;
  }
  last_cleanup_timestamp_ = now;
  PerformDeferredCleanup();
}

void GrCacheController::PerformDeferredCleanup() {
#if BUILDFLAG(IS_MAC)
  constexpr int kOldResourceDelaySeconds = 5;
#else
  constexpr int kOldResourceDelaySeconds = 1;
#endif
  // Here we ask GrContext to free any resources that haven't been used in
  // a long while even if it is under budget. Below we set a call back to
  // purge all possible GrContext resources if the context itself is not being
  // used.
  context_state_->set_need_context_state_reset(true);
  context_state_->gr_context()->performDeferredCleanup(
      std::chrono::seconds(kOldResourceDelaySeconds));
}

void GrCacheController::PurgeGrCache(uint64_t idle_id) {
  purge_gr_cache_cb_.Cancel();

  // We don't care which surface is current. This improves
  // performance. https://crbug.com/457431
  if (!context_state_->MakeCurrent(nullptr))
    return;

  // If the idle id changed, the context was used after this callback was
  // posted. Schedule another one.
  if (idle_id != current_idle_id_) {
    ScheduleGrContextCleanup();
    return;
  }

  context_state_->set_need_context_state_reset(true);

  // Force Skia to check fences to determine what can be freed.
  context_state_->gr_context()->checkAsyncWorkCompletion();
  {
    std::optional<gpu::raster::GrShaderCache::ScopedCacheUse> cache_use;
    // ScopedCacheUse is to avoid the empty/invalid client id DCHECKS caused
    // while accessing GrShaderCache. Note that since the actual client_id here
    // does not matter, we are using gpu::kDisplayCompositorClientId.
    context_state_->UseShaderCache(cache_use, gpu::kDisplayCompositorClientId);
    context_state_->gr_context()->freeGpuResources();
  }

  // Skia may have released resources, but the driver may not process that
  // without a flush.
  if (features::EnablePruneOldTransferCacheEntries()) {
    GrFlushInfo flush_info;
    gpu::AddVulkanCleanupTaskForSkiaFlush(context_state_->vk_context_provider(),
                                          &flush_info);
    context_state_->gr_context()->flush(flush_info);
    context_state_->gr_context()->submit();
  }
  if (context_state_->GrContextIsGL()) {
    auto* api = gl::g_current_gl_context;
    api->glFlushFn();
  }

  // Skia store VkPipeline cache only on demand. We do it when we're idle idle
  // as it might take time.
  context_state_->StoreVkPipelineCacheIfNeeded();
}

}  // namespace raster
}  // namespace gpu