| 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 "gpu/ipc/service/direct_composition_surface_win.h" | 5 #include "gpu/ipc/service/direct_composition_surface_win.h" |
| 6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/base/win/hidden_window.h" | 10 #include "ui/base/win/hidden_window.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 void RunPendingTasks(scoped_refptr<base::TaskRunner> task_runner) { | 37 void RunPendingTasks(scoped_refptr<base::TaskRunner> task_runner) { |
| 38 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 38 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 39 base::WaitableEvent::InitialState::NOT_SIGNALED); | 39 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 40 task_runner->PostTask(FROM_HERE, | 40 task_runner->PostTask(FROM_HERE, |
| 41 Bind(&base::WaitableEvent::Signal, Unretained(&done))); | 41 Bind(&base::WaitableEvent::Signal, Unretained(&done))); |
| 42 done.Wait(); | 42 done.Wait(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void DestroySurface(scoped_refptr<DirectCompositionSurfaceWin> surface) { |
| 46 scoped_refptr<base::TaskRunner> task_runner = |
| 47 surface->GetWindowTaskRunnerForTesting(); |
| 48 DCHECK(surface->HasOneRef()); |
| 49 |
| 50 surface = nullptr; |
| 51 |
| 52 // Ensure that the ChildWindowWin posts the task to delete the thread to the |
| 53 // main loop before doing RunUntilIdle. Otherwise the child threads could |
| 54 // outlive the main thread. |
| 55 RunPendingTasks(task_runner); |
| 56 |
| 57 base::RunLoop().RunUntilIdle(); |
| 58 } |
| 59 |
| 45 TEST(DirectCompositionSurfaceTest, TestMakeCurrent) { | 60 TEST(DirectCompositionSurfaceTest, TestMakeCurrent) { |
| 46 if (!gl::QueryDirectCompositionDevice( | 61 if (!gl::QueryDirectCompositionDevice( |
| 47 gl::QueryD3D11DeviceObjectFromANGLE())) { | 62 gl::QueryD3D11DeviceObjectFromANGLE())) { |
| 48 LOG(WARNING) | 63 LOG(WARNING) |
| 49 << "GL implementation not using DirectComposition, skipping test."; | 64 << "GL implementation not using DirectComposition, skipping test."; |
| 50 return; | 65 return; |
| 51 } | 66 } |
| 52 | 67 |
| 53 TestImageTransportSurfaceDelegate delegate; | 68 TestImageTransportSurfaceDelegate delegate; |
| 54 | 69 |
| 55 scoped_refptr<DirectCompositionSurfaceWin> surface( | 70 scoped_refptr<DirectCompositionSurfaceWin> surface1( |
| 56 new DirectCompositionSurfaceWin(delegate.AsWeakPtr(), | 71 new DirectCompositionSurfaceWin(delegate.AsWeakPtr(), |
| 57 ui::GetHiddenWindow())); | 72 ui::GetHiddenWindow())); |
| 58 EXPECT_TRUE(surface->Initialize()); | 73 EXPECT_TRUE(surface1->Initialize()); |
| 74 surface1->SetEnableDCLayers(true); |
| 59 | 75 |
| 60 scoped_refptr<gl::GLContext> context = | 76 scoped_refptr<gl::GLContext> context1 = gl::init::CreateGLContext( |
| 61 gl::init::CreateGLContext(nullptr, surface.get(), gl::GLContextAttribs()); | 77 nullptr, surface1.get(), gl::GLContextAttribs()); |
| 62 EXPECT_TRUE(surface->Resize(gfx::Size(100, 100), 1.0, true)); | 78 EXPECT_TRUE(surface1->Resize(gfx::Size(100, 100), 1.0, true)); |
| 63 | 79 |
| 64 // First SetDrawRectangle must be full size of surface. | 80 // First SetDrawRectangle must be full size of surface. |
| 65 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); | 81 EXPECT_FALSE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); |
| 66 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); | 82 EXPECT_TRUE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 67 | 83 |
| 68 // SetDrawRectangle can't be called again until swap. | 84 // SetDrawRectangle can't be called again until swap. |
| 69 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); | 85 EXPECT_FALSE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 70 | 86 |
| 71 EXPECT_TRUE(context->MakeCurrent(surface.get())); | 87 EXPECT_TRUE(context1->MakeCurrent(surface1.get())); |
| 72 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); | 88 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface1->SwapBuffers()); |
| 73 | 89 |
| 74 EXPECT_TRUE(context->IsCurrent(surface.get())); | 90 EXPECT_TRUE(context1->IsCurrent(surface1.get())); |
| 75 | 91 |
| 76 // SetDrawRectangle must be contained within surface. | 92 // SetDrawRectangle must be contained within surface. |
| 77 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 101, 101))); | 93 EXPECT_FALSE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 101, 101))); |
| 78 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); | 94 EXPECT_TRUE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 79 EXPECT_TRUE(context->IsCurrent(surface.get())); | 95 EXPECT_TRUE(context1->IsCurrent(surface1.get())); |
| 80 | 96 |
| 81 EXPECT_TRUE(surface->Resize(gfx::Size(50, 50), 1.0, true)); | 97 EXPECT_TRUE(surface1->Resize(gfx::Size(50, 50), 1.0, true)); |
| 82 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); | 98 EXPECT_TRUE(context1->IsCurrent(surface1.get())); |
| 83 EXPECT_TRUE(context->IsCurrent(surface.get())); | 99 EXPECT_TRUE(surface1->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); |
| 100 EXPECT_TRUE(context1->IsCurrent(surface1.get())); |
| 84 | 101 |
| 85 scoped_refptr<DirectCompositionSurfaceWin> surface2( | 102 scoped_refptr<DirectCompositionSurfaceWin> surface2( |
| 86 new DirectCompositionSurfaceWin(delegate.AsWeakPtr(), | 103 new DirectCompositionSurfaceWin(delegate.AsWeakPtr(), |
| 87 ui::GetHiddenWindow())); | 104 ui::GetHiddenWindow())); |
| 88 EXPECT_TRUE(surface2->Initialize()); | 105 EXPECT_TRUE(surface2->Initialize()); |
| 89 | 106 |
| 90 scoped_refptr<gl::GLContext> context2 = gl::init::CreateGLContext( | 107 scoped_refptr<gl::GLContext> context2 = gl::init::CreateGLContext( |
| 91 nullptr, surface2.get(), gl::GLContextAttribs()); | 108 nullptr, surface2.get(), gl::GLContextAttribs()); |
| 109 surface2->SetEnableDCLayers(true); |
| 92 EXPECT_TRUE(context2->MakeCurrent(surface2.get())); | 110 EXPECT_TRUE(context2->MakeCurrent(surface2.get())); |
| 93 EXPECT_TRUE(surface2->Resize(gfx::Size(100, 100), 1.0, true)); | 111 EXPECT_TRUE(surface2->Resize(gfx::Size(100, 100), 1.0, true)); |
| 94 // The previous IDCompositionSurface should be suspended when another | 112 // The previous IDCompositionSurface should be suspended when another |
| 95 // surface is being drawn to. | 113 // surface is being drawn to. |
| 96 EXPECT_TRUE(surface2->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); | 114 EXPECT_TRUE(surface2->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 97 EXPECT_TRUE(context2->IsCurrent(surface2.get())); | 115 EXPECT_TRUE(context2->IsCurrent(surface2.get())); |
| 98 | 116 |
| 99 // It should be possible to switch back to the previous surface and | 117 // It should be possible to switch back to the previous surface and |
| 100 // unsuspend it. | 118 // unsuspend it. |
| 101 EXPECT_TRUE(context->MakeCurrent(surface.get())); | 119 EXPECT_TRUE(context1->MakeCurrent(surface1.get())); |
| 102 scoped_refptr<base::TaskRunner> task_runner1 = | 120 context2 = nullptr; |
| 103 surface->GetWindowTaskRunnerForTesting(); | 121 context1 = nullptr; |
| 104 scoped_refptr<base::TaskRunner> task_runner2 = | |
| 105 surface2->GetWindowTaskRunnerForTesting(); | |
| 106 | 122 |
| 107 context2 = nullptr; | 123 DestroySurface(std::move(surface1)); |
| 108 surface2 = nullptr; | 124 DestroySurface(std::move(surface2)); |
| 109 context = nullptr; | |
| 110 surface = nullptr; | |
| 111 | |
| 112 // Ensure that the ChildWindowWin posts the task to delete the thread to the | |
| 113 // main loop before doing RunUntilIdle. Otherwise the child threads could | |
| 114 // outlive the main thread. | |
| 115 RunPendingTasks(task_runner1); | |
| 116 RunPendingTasks(task_runner2); | |
| 117 | |
| 118 base::RunLoop().RunUntilIdle(); | |
| 119 } | 125 } |
| 120 | 126 |
| 127 // Tests that switching using EnableDCLayers works. |
| 128 TEST(DirectCompositionSurfaceTest, DXGIDCLayerSwitch) { |
| 129 if (!gl::QueryDirectCompositionDevice( |
| 130 gl::QueryD3D11DeviceObjectFromANGLE())) { |
| 131 LOG(WARNING) |
| 132 << "GL implementation not using DirectComposition, skipping test."; |
| 133 return; |
| 134 } |
| 135 |
| 136 TestImageTransportSurfaceDelegate delegate; |
| 137 |
| 138 scoped_refptr<DirectCompositionSurfaceWin> surface( |
| 139 new DirectCompositionSurfaceWin(delegate.AsWeakPtr(), |
| 140 ui::GetHiddenWindow())); |
| 141 EXPECT_TRUE(surface->Initialize()); |
| 142 |
| 143 scoped_refptr<gl::GLContext> context = |
| 144 gl::init::CreateGLContext(nullptr, surface.get(), gl::GLContextAttribs()); |
| 145 EXPECT_TRUE(surface->Resize(gfx::Size(100, 100), 1.0, true)); |
| 146 EXPECT_FALSE(surface->swap_chain()); |
| 147 |
| 148 // First SetDrawRectangle must be full size of surface for DXGI |
| 149 // swapchain. |
| 150 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); |
| 151 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 152 EXPECT_TRUE(surface->swap_chain()); |
| 153 |
| 154 // SetDrawRectangle can't be called again until swap. |
| 155 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 156 |
| 157 EXPECT_TRUE(context->MakeCurrent(surface.get())); |
| 158 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); |
| 159 |
| 160 EXPECT_TRUE(context->IsCurrent(surface.get())); |
| 161 |
| 162 surface->SetEnableDCLayers(true); |
| 163 |
| 164 // Surface switched to use IDCompositionSurface, so must draw to |
| 165 // entire surface. |
| 166 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); |
| 167 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 168 EXPECT_TRUE(context->IsCurrent(surface.get())); |
| 169 EXPECT_FALSE(surface->swap_chain()); |
| 170 |
| 171 surface->SetEnableDCLayers(false); |
| 172 |
| 173 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); |
| 174 |
| 175 // Surface switched to use IDXGISwapChain, so must draw to entire |
| 176 // surface. |
| 177 EXPECT_FALSE(surface->SetDrawRectangle(gfx::Rect(0, 0, 50, 50))); |
| 178 EXPECT_TRUE(surface->SetDrawRectangle(gfx::Rect(0, 0, 100, 100))); |
| 179 EXPECT_TRUE(surface->swap_chain()); |
| 180 |
| 181 context = nullptr; |
| 182 DestroySurface(std::move(surface)); |
| 183 } |
| 121 } // namespace | 184 } // namespace |
| 122 } // namespace gpu | 185 } // namespace gpu |
| OLD | NEW |