File: test_in_process_context_provider.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 (212 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download | duplicates (5)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/test/test_in_process_context_provider.h"

#include <stdint.h>

#include <memory>
#include <utility>

#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "components/viz/common/gpu/context_cache_controller.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "components/viz/service/gl/gpu_service_impl.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/shared_memory_limits.h"
#include "gpu/command_buffer/common/context_creation_attribs.h"
#include "gpu/config/skia_limits.h"
#include "gpu/ipc/gl_in_process_context.h"
#include "gpu/ipc/raster_in_process_context.h"
#include "gpu/skia_bindings/grcontext_for_gles2_interface.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"

namespace viz {

TestInProcessContextProvider::TestInProcessContextProvider(
    TestContextType type,
    bool support_locking,
    gpu::raster::GrShaderCache* gr_shader_cache,
    gpu::GpuProcessShmCount* use_shader_cache_shm_count)
    : type_(type), use_shader_cache_shm_count_(use_shader_cache_shm_count) {
  CHECK(main_thread_checker_.CalledOnValidThread());
  context_thread_checker_.DetachFromThread();

  if (support_locking) {
    context_lock_.emplace();
  }
}

TestInProcessContextProvider::~TestInProcessContextProvider() {
  CHECK(main_thread_checker_.CalledOnValidThread() ||
        context_thread_checker_.CalledOnValidThread());
}

void TestInProcessContextProvider::AddRef() const {
  base::RefCountedThreadSafe<TestInProcessContextProvider>::AddRef();
}

void TestInProcessContextProvider::Release() const {
  base::RefCountedThreadSafe<TestInProcessContextProvider>::Release();
}

gpu::ContextResult TestInProcessContextProvider::BindToCurrentSequence() {
  CHECK(context_thread_checker_.CalledOnValidThread());

  if (is_bound_) {
    return gpu::ContextResult::kSuccess;
  }

  auto* holder = TestGpuServiceHolder::GetInstance();

  gpu::ContextCreationAttribs attribs;
  attribs.bind_generates_resource = false;

  if (type_ == TestContextType::kGLES2) {
    attribs.enable_gles2_interface = true;
    attribs.enable_raster_interface = false;
    attribs.enable_gpu_rasterization = false;

    gles2_context_ = std::make_unique<gpu::GLInProcessContext>();
    auto result = gles2_context_->Initialize(
        TestGpuServiceHolder::GetInstance()->task_executor(), attribs,
        gpu::SharedMemoryLimits());
    CHECK_EQ(result, gpu::ContextResult::kSuccess);

    caps_ = gles2_context_->GetCapabilities();
  } else {
    bool is_gpu_raster = type_ == TestContextType::kGpuRaster;

    attribs.enable_gles2_interface = false;
    attribs.enable_raster_interface = true;
    attribs.enable_gpu_rasterization = is_gpu_raster;

    raster_context_ = std::make_unique<gpu::RasterInProcessContext>();
    auto result = raster_context_->Initialize(
        holder->task_executor(), attribs, gpu::SharedMemoryLimits(),
        holder->gpu_service()->gr_shader_cache(), use_shader_cache_shm_count_);
    CHECK_EQ(result, gpu::ContextResult::kSuccess);

    caps_ = raster_context_->GetCapabilities();
    CHECK_EQ(caps_.gpu_rasterization, is_gpu_raster);
  }

  cache_controller_ = std::make_unique<ContextCacheController>(
      ContextSupport(), base::SingleThreadTaskRunner::GetCurrentDefault());
  cache_controller_->SetLock(GetLock());

  is_bound_ = true;
  return gpu::ContextResult::kSuccess;
}

gpu::gles2::GLES2Interface* TestInProcessContextProvider::ContextGL() {
  CheckValidThreadOrLockAcquired();
  CHECK(gles2_context_);
  return gles2_context_->GetImplementation();
}

gpu::raster::RasterInterface* TestInProcessContextProvider::RasterInterface() {
  CheckValidThreadOrLockAcquired();
  CHECK(raster_context_);
  return raster_context_->GetImplementation();
}

gpu::ContextSupport* TestInProcessContextProvider::ContextSupport() {
  return gles2_context_ ? gles2_context_->GetImplementation()
                        : raster_context_->GetContextSupport();
}

class GrDirectContext* TestInProcessContextProvider::GrContext() {
  CheckValidThreadOrLockAcquired();
  if (gr_context_) {
    return gr_context_->get();
  }

  if (!gles2_context_) {
    return nullptr;
  }

  size_t max_resource_cache_bytes;
  size_t max_glyph_cache_texture_bytes;
  gpu::DefaultGrCacheLimitsForTests(&max_resource_cache_bytes,
                                    &max_glyph_cache_texture_bytes);
  gr_context_ = std::make_unique<skia_bindings::GrContextForGLES2Interface>(
      ContextGL(), ContextSupport(), ContextCapabilities(),
      max_resource_cache_bytes, max_glyph_cache_texture_bytes);
  cache_controller_->SetGrContext(gr_context_->get());
  return gr_context_->get();
}

gpu::SharedImageInterface*
TestInProcessContextProvider::SharedImageInterface() {
  return gles2_context_ ? gles2_context_->GetSharedImageInterface()
                        : raster_context_->GetSharedImageInterface();
}

ContextCacheController* TestInProcessContextProvider::CacheController() {
  CheckValidThreadOrLockAcquired();
  return cache_controller_.get();
}

base::Lock* TestInProcessContextProvider::GetLock() {
  return base::OptionalToPtr(context_lock_);
}

const gpu::Capabilities& TestInProcessContextProvider::ContextCapabilities()
    const {
  CheckValidThreadOrLockAcquired();
  return caps_;
}

const gpu::GpuFeatureInfo& TestInProcessContextProvider::GetGpuFeatureInfo()
    const {
  CheckValidThreadOrLockAcquired();
  return gles2_context_ ? gles2_context_->GetGpuFeatureInfo()
                        : raster_context_->GetGpuFeatureInfo();
}

void TestInProcessContextProvider::AddObserver(ContextLostObserver* obs) {
  observers_.AddObserver(obs);
}

void TestInProcessContextProvider::RemoveObserver(ContextLostObserver* obs) {
  observers_.RemoveObserver(obs);
}

void TestInProcessContextProvider::SendOnContextLost() {
  for (auto& observer : observers_) {
    observer.OnContextLost();
  }
}

void TestInProcessContextProvider::ExecuteOnGpuThread(base::OnceClosure task) {
  CHECK(raster_context_);
  raster_context_->GetCommandBufferForTest()
      ->service_for_testing()
      ->ScheduleOutOfOrderTask(std::move(task));
}

void TestInProcessContextProvider::CheckValidThreadOrLockAcquired() const {
#if DCHECK_IS_ON()
  if (context_lock_) {
    context_lock_->AssertAcquired();
  } else {
    DCHECK(context_thread_checker_.CalledOnValidThread());
  }
#endif
}

unsigned int TestInProcessContextProvider::GetGrGLTextureFormat(
    SharedImageFormat format) const {
  return SharedImageFormatRestrictedSinglePlaneUtils::ToGLTextureStorageFormat(
      format, ContextCapabilities().angle_rgbx_internal_format);
}

GpuServiceImpl* TestInProcessContextProvider::GpuService() {
  return TestGpuServiceHolder::GetInstance()->gpu_service();
}

}  // namespace viz