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
|
// Copyright 2017 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/service/gl/gpu_service_impl.h"
#include <memory>
#include <tuple>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "components/viz/common/resources/peak_gpu_memory_tracker_util.h"
#include "components/viz/service/gl/mock_gpu_service_impl.h"
#include "components/viz/service/input/peak_gpu_memory_tracker_impl.h"
#include "gpu/config/gpu_info.h"
#include "gpu/ipc/service/gpu_watchdog_thread.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/viz/public/mojom/gpu.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/init/gl_factory.h"
namespace viz {
namespace {
const uint64_t kPeakMemoryMB = 42u;
const uint64_t kPeakMemory = kPeakMemoryMB * 1048576u;
} // namespace
class GpuServiceTest : public testing::Test {
public:
GpuServiceTest()
: io_thread_("TestIOThread"),
wait_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
GpuServiceTest(const GpuServiceTest&) = delete;
GpuServiceTest& operator=(const GpuServiceTest&) = delete;
~GpuServiceTest() override = default;
MockGpuServiceImpl* mock_gpu_service() {
return static_cast<MockGpuServiceImpl*>(gpu_service_.get());
}
GpuServiceImpl* gpu_service() { return gpu_service_.get(); }
void DestroyService() { gpu_service_ = nullptr; }
void BlockIOThread() {
wait_.Reset();
io_runner()->PostTask(FROM_HERE, base::BindOnce(&base::WaitableEvent::Wait,
base::Unretained(&wait_)));
}
void UnblockIOThread() {
DCHECK(!wait_.IsSignaled());
wait_.Signal();
}
scoped_refptr<base::SingleThreadTaskRunner> io_runner() {
return io_thread_.task_runner();
}
// testing::Test
void SetUp() override {
ASSERT_TRUE(io_thread_.Start());
gpu::GPUInfo gpu_info;
gpu_info.in_process_gpu = false;
GpuServiceImpl::InitParams init_params;
init_params.io_runner = io_thread_.task_runner();
gpu_service_ = std::make_unique<testing::NiceMock<MockGpuServiceImpl>>(
gpu::GpuPreferences(), gpu_info, gpu::GpuFeatureInfo(), gpu::GPUInfo(),
gpu::GpuFeatureInfo(), gfx::GpuExtraInfo(), std::move(init_params));
}
void TearDown() override {
DestroyService();
base::RunLoop runloop;
runloop.RunUntilIdle();
io_thread_.Stop();
}
std::optional<bool> visible_;
private:
base::Thread io_thread_;
std::unique_ptr<GpuServiceImpl> gpu_service_;
base::WaitableEvent wait_;
};
// Tests that GpuServiceImpl can be destroyed before Bind() succeeds on the IO
// thread.
TEST_F(GpuServiceTest, ServiceDestroyedBeforeBind) {
// Block the IO thread to make sure that the GpuServiceImpl is destroyed
// before the binding happens on the IO thread.
mojo::Remote<mojom::GpuService> gpu_service_remote;
BlockIOThread();
gpu_service()->Bind(gpu_service_remote.BindNewPipeAndPassReceiver());
UnblockIOThread();
DestroyService();
}
// Tests that GpuServiceImpl can be destroyed after Bind() succeeds on the IO
// thread.
TEST_F(GpuServiceTest, ServiceDestroyedAfterBind) {
mojo::Remote<mojom::GpuService> gpu_service_remote;
gpu_service()->Bind(gpu_service_remote.BindNewPipeAndPassReceiver());
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
io_runner()->PostTask(FROM_HERE, base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait)));
wait.Wait();
DestroyService();
}
// Tests that the visibility callback gets called when visibility changes.
TEST_F(GpuServiceTest, VisibilityCallbackCalled) {
mojo::Remote<mojom::GpuService> gpu_service_remote;
gpu_service()->Bind(gpu_service_remote.BindNewPipeAndPassReceiver());
mojo::PendingRemote<mojom::GpuHost> gpu_host_proxy;
std::ignore = gpu_host_proxy.InitWithNewPipeAndPassReceiver();
gpu_service()->InitializeWithHost(
std::move(gpu_host_proxy), gpu::GpuProcessShmCount(),
gl::init::CreateOffscreenGLSurface(gl::GetDefaultDisplay(), gfx::Size()),
mojom::GpuServiceCreationParams::New());
gpu_service_remote.FlushForTesting();
gpu_service()->SetVisibilityChangedCallback(base::BindRepeating(
[](GpuServiceTest* test, bool visible) { test->visible_ = visible; },
base::Unretained(this)));
EXPECT_FALSE(visible_.has_value());
gpu_service_remote->OnForegrounded();
gpu_service_remote.FlushForTesting();
EXPECT_TRUE(visible_.has_value());
EXPECT_TRUE(*visible_);
gpu_service_remote->OnBackgrounded();
gpu_service_remote.FlushForTesting();
EXPECT_TRUE(visible_.has_value());
EXPECT_FALSE(*visible_);
}
// Tests that when a PeakGpuMemoryTracker is destroyed, GpuService properly
// updates the histograms.
TEST_F(GpuServiceTest, PeakGpuMemoryCallback) {
mojo::Remote<mojom::GpuService> gpu_service_remote;
gpu_service()->Bind(gpu_service_remote.BindNewPipeAndPassReceiver());
mojo::PendingRemote<mojom::GpuHost> gpu_host_proxy;
std::ignore = gpu_host_proxy.InitWithNewPipeAndPassReceiver();
gpu_service()->InitializeWithHost(
std::move(gpu_host_proxy), gpu::GpuProcessShmCount(),
gl::init::CreateOffscreenGLSurface(gl::GetDefaultDisplay(), gfx::Size()),
mojom::GpuServiceCreationParams::New());
gpu_service_remote.FlushForTesting();
ON_CALL(*mock_gpu_service(), GetPeakMemoryUsageOnMainThread)
.WillByDefault(testing::Invoke(
[](uint32_t sequence_num,
GpuServiceImpl::GetPeakMemoryUsageCallback callback) {
ASSERT_EQ(GetPeakMemoryUsageRequestLocation(sequence_num),
SequenceLocation::kGpuProcess);
base::flat_map<gpu::GpuPeakMemoryAllocationSource, uint64_t>
allocation_per_source;
allocation_per_source[gpu::GpuPeakMemoryAllocationSource::UNKNOWN] =
kPeakMemory;
std::move(callback).Run(kPeakMemory, allocation_per_source);
}));
base::HistogramTester histogram;
auto tracker = std::make_unique<PeakGpuMemoryTrackerImpl>(
PeakGpuMemoryTracker::Usage::PAGE_LOAD, gpu_service());
// No report in response to creation.
histogram.ExpectTotalCount("Memory.GPU.PeakMemoryUsage2.PageLoad", 0);
histogram.ExpectTotalCount(
"Memory.GPU.PeakMemoryAllocationSource.PageLoad.Unknown", 0);
// Deleting the tracker should start a request for peak Gpu memory usage,
// with the callback being a posted task.
tracker.reset();
gpu_service_remote.FlushForTesting();
histogram.ExpectUniqueSample("Memory.GPU.PeakMemoryUsage2.PageLoad",
kPeakMemoryMB, 1);
histogram.ExpectUniqueSample(
"Memory.GPU.PeakMemoryAllocationSource2.PageLoad.Unknown", kPeakMemoryMB,
1);
}
} // namespace viz
|