| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/ui/public/cpp/gpu/gpu.h" | 5 #include "services/ui/public/cpp/gpu/gpu.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/test/scoped_task_environment.h" | 8 #include "base/test/scoped_task_environment.h" |
| 9 #include "gpu/config/gpu_info.h" | 9 #include "gpu/config/gpu_info.h" |
| 10 #include "mojo/public/cpp/bindings/binding_set.h" | 10 #include "mojo/public/cpp/bindings/binding_set.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 } | 26 } |
| 27 | 27 |
| 28 void WaitUntilChannelRequest() { | 28 void WaitUntilChannelRequest() { |
| 29 DCHECK(quit_closure_.is_null()); | 29 DCHECK(quit_closure_.is_null()); |
| 30 base::RunLoop run_loop; | 30 base::RunLoop run_loop; |
| 31 quit_closure_ = run_loop.QuitClosure(); | 31 quit_closure_ = run_loop.QuitClosure(); |
| 32 run_loop.Run(); | 32 run_loop.Run(); |
| 33 quit_closure_ = base::Closure(); | 33 quit_closure_ = base::Closure(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool RespondToGpuChannelRequests() { | 36 bool RespondToGpuChannelRequests(bool success = false) { |
| 37 if (establish_channel_callback_.is_null()) | 37 if (establish_channel_callback_.is_null()) |
| 38 return false; | 38 return false; |
| 39 constexpr int client_id = 1; | 39 constexpr int client_id = 1; |
| 40 mojo::ScopedMessagePipeHandle handle; | 40 mojo::ScopedMessagePipeHandle handle; |
| 41 if (success) |
| 42 handle = std::move(mojo::MessagePipe().handle0); |
| 41 base::ResetAndReturn(&establish_channel_callback_) | 43 base::ResetAndReturn(&establish_channel_callback_) |
| 42 .Run(client_id, std::move(handle), gpu::GPUInfo()); | 44 .Run(client_id, std::move(handle), gpu::GPUInfo()); |
| 43 return true; | 45 return true; |
| 44 } | 46 } |
| 45 | 47 |
| 46 // ui::mojom::Gpu overrides: | 48 // ui::mojom::Gpu overrides: |
| 47 void EstablishGpuChannel( | 49 void EstablishGpuChannel( |
| 48 const EstablishGpuChannelCallback& callback) override { | 50 const EstablishGpuChannelCallback& callback) override { |
| 49 establish_channel_callback_ = callback; | 51 establish_channel_callback_ = callback; |
| 50 if (!quit_closure_.is_null()) | 52 if (!quit_closure_.is_null()) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 gpu()->EstablishGpuChannel(callback); | 116 gpu()->EstablishGpuChannel(callback); |
| 115 gpu()->EstablishGpuChannel(callback); | 117 gpu()->EstablishGpuChannel(callback); |
| 116 EXPECT_EQ(2, counter); | 118 EXPECT_EQ(2, counter); |
| 117 gpu_impl()->WaitUntilChannelRequest(); | 119 gpu_impl()->WaitUntilChannelRequest(); |
| 118 | 120 |
| 119 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests()); | 121 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests()); |
| 120 run_loop.Run(); | 122 run_loop.Run(); |
| 121 EXPECT_EQ(0, counter); | 123 EXPECT_EQ(0, counter); |
| 122 } | 124 } |
| 123 | 125 |
| 126 // Tests that a new request for establishing a gpu channel from a callback of a |
| 127 // previous callback is processed correctly. |
| 128 TEST_F(GpuTest, EstablishRequestOnFailureOnPreviousRequest) { |
| 129 base::RunLoop run_loop; |
| 130 scoped_refptr<gpu::GpuChannelHost> host; |
| 131 auto callback = base::Bind( |
| 132 [](scoped_refptr<gpu::GpuChannelHost>* out_host, |
| 133 const base::Closure& callback, |
| 134 scoped_refptr<gpu::GpuChannelHost> host) { |
| 135 callback.Run(); |
| 136 *out_host = std::move(host); |
| 137 }, |
| 138 &host, run_loop.QuitClosure()); |
| 139 gpu()->EstablishGpuChannel(base::Bind( |
| 140 [](ui::Gpu* gpu, const gpu::GpuChannelEstablishedCallback& callback, |
| 141 scoped_refptr<gpu::GpuChannelHost> host) { |
| 142 DCHECK(!host); |
| 143 gpu->EstablishGpuChannel(callback); |
| 144 }, |
| 145 gpu(), callback)); |
| 146 gpu_impl()->WaitUntilChannelRequest(); |
| 147 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests()); |
| 148 |
| 149 // Responding to the first request would issue a second request immediately. |
| 150 // So wait for that request to reach the TestGpuImpl, and have that respond |
| 151 // again. That should run quit-closure of |run_loop|. |
| 152 gpu_impl()->WaitUntilChannelRequest(); |
| 153 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests(true /* success */)); |
| 154 run_loop.Run(); |
| 155 EXPECT_TRUE(host); |
| 156 } |
| 157 |
| 158 // Tests that if a request for a gpu channel succeeded, then subsequent requests |
| 159 // are met synchronously. |
| 160 TEST_F(GpuTest, EstablishRequestResponseSynchronouslyOnSuccess) { |
| 161 scoped_refptr<gpu::GpuChannelHost> host; |
| 162 base::RunLoop run_loop; |
| 163 gpu()->EstablishGpuChannel(base::Bind( |
| 164 [](scoped_refptr<gpu::GpuChannelHost>* out_host, |
| 165 const base::Closure& callback, |
| 166 scoped_refptr<gpu::GpuChannelHost> host) { |
| 167 *out_host = std::move(host); |
| 168 callback.Run(); |
| 169 }, |
| 170 &host, run_loop.QuitClosure())); |
| 171 gpu_impl()->WaitUntilChannelRequest(); |
| 172 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests(true /* success */)); |
| 173 EXPECT_FALSE(host); |
| 174 run_loop.Run(); |
| 175 EXPECT_TRUE(host); |
| 176 |
| 177 int counter = 0; |
| 178 auto callback = |
| 179 base::Bind([](int* counter, |
| 180 scoped_refptr<gpu::GpuChannelHost> host) { ++(*counter); }, |
| 181 &counter); |
| 182 gpu()->EstablishGpuChannel(callback); |
| 183 EXPECT_EQ(1, counter); |
| 184 } |
| 185 |
| 124 } // namespace ui | 186 } // namespace ui |
| OLD | NEW |