OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gpu/ipc/service/gpu_vsync_provider.h" | 5 #include "gpu/ipc/service/gpu_vsync_provider_win.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "gpu/ipc/common/gpu_messages.h" |
| 14 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_message_macros.h" |
| 16 #include "ipc/message_filter.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "ui/base/win/hidden_window.h" | 18 #include "ui/base/win/hidden_window.h" |
14 | 19 |
15 namespace gpu { | 20 namespace gpu { |
16 | 21 |
| 22 namespace { |
| 23 |
| 24 class FakeChannel : public base::Thread, public IPC::Channel { |
| 25 public: |
| 26 explicit FakeChannel(const gfx::VSyncProvider::UpdateVSyncCallback& callback) |
| 27 : base::Thread("io"), |
| 28 callback_(callback), |
| 29 io_done_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 30 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 31 |
| 32 ~FakeChannel() override { |
| 33 task_runner()->PostTask(FROM_HERE, |
| 34 base::Bind(&FakeChannel::RemoveFilterOnIOThread, |
| 35 base::Unretained(this))); |
| 36 io_done_event_.Wait(); |
| 37 Stop(); |
| 38 } |
| 39 |
| 40 void SetNeedsVSync(bool needs_vsync) { |
| 41 task_runner()->PostTask(FROM_HERE, |
| 42 base::Bind(&FakeChannel::SetNeedsVSyncOnIOThread, |
| 43 base::Unretained(this), needs_vsync)); |
| 44 io_done_event_.Wait(); |
| 45 } |
| 46 |
| 47 void AddFilter(IPC::MessageFilter* message_filter) { |
| 48 message_filter_ = message_filter; |
| 49 |
| 50 // Start the thread. |
| 51 Start(); |
| 52 |
| 53 task_runner()->PostTask( |
| 54 FROM_HERE, |
| 55 base::Bind(&FakeChannel::AddFilterOnIOThread, base::Unretained(this))); |
| 56 io_done_event_.Wait(); |
| 57 } |
| 58 |
| 59 // IPC::Channel implementation |
| 60 bool Send(IPC::Message* msg) override { |
| 61 IPC_BEGIN_MESSAGE_MAP(FakeChannel, *msg) |
| 62 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_UpdateVSyncParameters, |
| 63 OnUpdateVSyncParameters); |
| 64 IPC_MESSAGE_UNHANDLED(return false) |
| 65 IPC_END_MESSAGE_MAP() |
| 66 return true; |
| 67 } |
| 68 |
| 69 bool Connect() override { |
| 70 NOTREACHED(); |
| 71 return false; |
| 72 } |
| 73 |
| 74 void Close() override { NOTREACHED(); } |
| 75 |
| 76 private: |
| 77 void SetNeedsVSyncOnIOThread(bool needs_vsync) { |
| 78 GpuCommandBufferMsg_SetNeedsVSync msg(0, needs_vsync); |
| 79 message_filter_->OnMessageReceived(msg); |
| 80 io_done_event_.Signal(); |
| 81 } |
| 82 |
| 83 void AddFilterOnIOThread() { |
| 84 message_filter_->OnFilterAdded(this); |
| 85 io_done_event_.Signal(); |
| 86 } |
| 87 |
| 88 void RemoveFilterOnIOThread() { |
| 89 message_filter_->OnFilterRemoved(); |
| 90 io_done_event_.Signal(); |
| 91 } |
| 92 |
| 93 void OnUpdateVSyncParameters(base::TimeTicks timestamp, |
| 94 base::TimeDelta interval) { |
| 95 callback_.Run(timestamp, interval); |
| 96 } |
| 97 |
| 98 gfx::VSyncProvider::UpdateVSyncCallback callback_; |
| 99 base::WaitableEvent io_done_event_; |
| 100 |
| 101 scoped_refptr<IPC::MessageFilter> message_filter_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(FakeChannel); |
| 104 }; |
| 105 |
| 106 class FakeDelegate : public ImageTransportSurfaceDelegate, |
| 107 public base::SupportsWeakPtr<FakeDelegate> { |
| 108 public: |
| 109 explicit FakeDelegate(FakeChannel* channel) : channel_(channel) {} |
| 110 |
| 111 void DidCreateAcceleratedSurfaceChildWindow( |
| 112 SurfaceHandle parent_window, |
| 113 SurfaceHandle child_window) override {} |
| 114 void DidSwapBuffersComplete(SwapBuffersCompleteParams params) override {} |
| 115 const gles2::FeatureInfo* GetFeatureInfo() const override { return nullptr; } |
| 116 void SetLatencyInfoCallback(const LatencyInfoCallback& callback) override {} |
| 117 void UpdateVSyncParameters(base::TimeTicks timebase, |
| 118 base::TimeDelta interval) override { |
| 119 // This shouldn't be called by GpuVSyncProviderWin |
| 120 NOTREACHED(); |
| 121 } |
| 122 |
| 123 void AddFilter(IPC::MessageFilter* message_filter) override { |
| 124 channel_->AddFilter(message_filter); |
| 125 } |
| 126 |
| 127 int32_t GetRouteID() const override { return 0; } |
| 128 |
| 129 private: |
| 130 FakeChannel* channel_; |
| 131 DISALLOW_COPY_AND_ASSIGN(FakeDelegate); |
| 132 }; |
| 133 |
| 134 } // namespace |
| 135 |
17 class GpuVSyncProviderTest : public testing::Test { | 136 class GpuVSyncProviderTest : public testing::Test { |
18 public: | 137 public: |
19 GpuVSyncProviderTest() | 138 GpuVSyncProviderTest() |
20 : vsync_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 139 : vsync_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
21 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 140 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
22 ~GpuVSyncProviderTest() override {} | 141 ~GpuVSyncProviderTest() override {} |
23 | 142 |
24 void SetUp() override {} | 143 void SetUp() override { |
| 144 channel_.reset(new FakeChannel( |
| 145 base::Bind(&GpuVSyncProviderTest::OnVSync, base::Unretained(this)))); |
| 146 delegate_.reset(new FakeDelegate(channel_.get())); |
| 147 provider_.reset( |
| 148 new GpuVSyncProviderWin(delegate_->AsWeakPtr(), ui::GetHiddenWindow())); |
| 149 } |
25 | 150 |
26 void TearDown() override {} | 151 void TearDown() override {} |
27 | 152 |
28 void OnVSync(base::TimeTicks timestamp) { | |
29 // This is called on VSync worker thread. | |
30 base::AutoLock lock(lock_); | |
31 if (++vsync_count_ == 3) | |
32 vsync_event_.Signal(); | |
33 } | |
34 | |
35 int vsync_count() { | 153 int vsync_count() { |
36 base::AutoLock lock(lock_); | 154 base::AutoLock lock(lock_); |
37 return vsync_count_; | 155 return vsync_count_; |
38 } | 156 } |
39 | 157 |
40 void reset_vsync_count() { | 158 void reset_vsync_count() { |
41 base::AutoLock lock(lock_); | 159 base::AutoLock lock(lock_); |
42 vsync_count_ = 0; | 160 vsync_count_ = 0; |
43 } | 161 } |
44 | 162 |
| 163 void set_vsync_stop_count(int value) { vsync_stop_count_ = value; } |
| 164 |
| 165 void SetNeedsVSync(bool needs_vsync) { channel_->SetNeedsVSync(needs_vsync); } |
| 166 |
45 protected: | 167 protected: |
46 base::WaitableEvent vsync_event_; | 168 base::WaitableEvent vsync_event_; |
47 | 169 |
48 private: | 170 private: |
| 171 void OnVSync(base::TimeTicks timestamp, base::TimeDelta interval) { |
| 172 // This is called on VSync worker thread. |
| 173 EXPECT_GT(timestamp, previous_vsync_timestamp_); |
| 174 previous_vsync_timestamp_ = timestamp; |
| 175 |
| 176 base::AutoLock lock(lock_); |
| 177 if (++vsync_count_ == vsync_stop_count_) |
| 178 vsync_event_.Signal(); |
| 179 } |
| 180 |
49 base::Lock lock_; | 181 base::Lock lock_; |
50 int vsync_count_ = 0; | 182 int vsync_count_ = 0; |
| 183 int vsync_stop_count_ = 0; |
| 184 base::TimeTicks previous_vsync_timestamp_; |
| 185 std::unique_ptr<FakeChannel> channel_; |
| 186 std::unique_ptr<FakeDelegate> delegate_; |
| 187 std::unique_ptr<GpuVSyncProviderWin> provider_; |
51 }; | 188 }; |
52 | 189 |
| 190 // Tests that VSync signal production is controlled by SetNeedsVSync. |
53 TEST_F(GpuVSyncProviderTest, VSyncSignalTest) { | 191 TEST_F(GpuVSyncProviderTest, VSyncSignalTest) { |
54 SurfaceHandle window = ui::GetHiddenWindow(); | 192 set_vsync_stop_count(3); |
55 | |
56 std::unique_ptr<GpuVSyncProvider> provider = GpuVSyncProvider::Create( | |
57 base::Bind(&GpuVSyncProviderTest::OnVSync, base::Unretained(this)), | |
58 window); | |
59 | 193 |
60 constexpr base::TimeDelta wait_timeout = | 194 constexpr base::TimeDelta wait_timeout = |
61 base::TimeDelta::FromMilliseconds(300); | 195 base::TimeDelta::FromMilliseconds(300); |
62 | 196 |
63 // Verify that there are no VSync signals before provider is enabled | 197 // Verify that there are no VSync signals before provider is enabled |
64 bool wait_result = vsync_event_.TimedWait(wait_timeout); | 198 bool wait_result = vsync_event_.TimedWait(wait_timeout); |
65 EXPECT_FALSE(wait_result); | 199 EXPECT_FALSE(wait_result); |
66 EXPECT_EQ(0, vsync_count()); | 200 EXPECT_EQ(0, vsync_count()); |
67 | 201 |
68 provider->EnableVSync(true); | 202 SetNeedsVSync(true); |
69 | 203 |
70 vsync_event_.Wait(); | 204 vsync_event_.Wait(); |
71 | 205 |
72 provider->EnableVSync(false); | 206 SetNeedsVSync(false); |
73 | 207 |
74 // Verify that VSync callbacks stop coming after disabling. | 208 // Verify that VSync callbacks stop coming after disabling. |
75 // Please note that it might still be possible for one | 209 // Please note that it might still be possible for one |
76 // callback to be in flight on VSync worker thread, so |vsync_count_| | 210 // callback to be in flight on VSync worker thread, so |vsync_count_| |
77 // could still be incremented once, but not enough times to trigger | 211 // could still be incremented once, but not enough times to trigger |
78 // |vsync_event_|. | 212 // |vsync_event_|. |
79 reset_vsync_count(); | 213 reset_vsync_count(); |
80 wait_result = vsync_event_.TimedWait(wait_timeout); | 214 wait_result = vsync_event_.TimedWait(wait_timeout); |
81 EXPECT_FALSE(wait_result); | 215 EXPECT_FALSE(wait_result); |
82 } | 216 } |
83 | 217 |
| 218 // Verifies that VSync timestamp is monotonic. |
| 219 TEST_F(GpuVSyncProviderTest, VSyncMonotonicTimestampTest) { |
| 220 set_vsync_stop_count(60); |
| 221 SetNeedsVSync(true); |
| 222 // Make sure this doesn't run for longer than 1 second in case VSync |
| 223 // callbacks are slowed by running multiple tests in parallel. |
| 224 vsync_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000)); |
| 225 } |
| 226 |
84 } // namespace gpu | 227 } // namespace gpu |
OLD | NEW |