| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/ui/surfaces/display_compositor.h" | |
| 6 | |
| 7 #include <inttypes.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/strings/stringprintf.h" | |
| 17 #include "cc/ipc/display_compositor.mojom.h" | |
| 18 #include "cc/surfaces/surface_id.h" | |
| 19 #include "cc/surfaces/surface_observer.h" | |
| 20 #include "cc/surfaces/surface_reference.h" | |
| 21 #include "mojo/public/cpp/bindings/binding.h" | |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 23 #include "services/ui/common/task_runner_test_base.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 #include "ui/gfx/geometry/size.h" | |
| 26 | |
| 27 namespace ui { | |
| 28 namespace test { | |
| 29 namespace { | |
| 30 | |
| 31 std::string SurfaceIdString(const cc::SurfaceId& surface_id) { | |
| 32 return base::StringPrintf("%u:%u:%u", surface_id.frame_sink_id().client_id(), | |
| 33 surface_id.frame_sink_id().sink_id(), | |
| 34 surface_id.local_frame_id().local_id()); | |
| 35 } | |
| 36 | |
| 37 cc::SurfaceId MakeSurfaceId(uint32_t client_id, | |
| 38 uint32_t sink_id, | |
| 39 uint32_t local_id) { | |
| 40 return cc::SurfaceId( | |
| 41 cc::FrameSinkId(client_id, sink_id), | |
| 42 cc::LocalFrameId(local_id, base::UnguessableToken::Deserialize(0, 1u))); | |
| 43 } | |
| 44 | |
| 45 // Test mojom::DisplayCompositorClient that records OnSurfaceCreated() events. | |
| 46 class TestDisplayCompositorClient : public cc::mojom::DisplayCompositorClient { | |
| 47 public: | |
| 48 TestDisplayCompositorClient() : binding_(this) {} | |
| 49 ~TestDisplayCompositorClient() override {} | |
| 50 | |
| 51 cc::mojom::DisplayCompositorClientPtr GetPtr() { | |
| 52 return binding_.CreateInterfacePtrAndBind(); | |
| 53 } | |
| 54 | |
| 55 // Returns events that have occurred and clear. | |
| 56 std::string events() { | |
| 57 std::string value = std::move(events_); | |
| 58 events_.clear(); | |
| 59 return value; | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 void AddEvent(const std::string& text) { | |
| 64 if (!events_.empty()) | |
| 65 events_ += ";"; | |
| 66 events_ += text; | |
| 67 } | |
| 68 | |
| 69 // cc::mojom::DisplayCompositorClient: | |
| 70 void OnDisplayCompositorCreated( | |
| 71 const cc::SurfaceId& root_surface_id) override { | |
| 72 got_root_surface_id_ = true; | |
| 73 } | |
| 74 | |
| 75 void OnSurfaceCreated(const cc::SurfaceInfo& surface_info) override { | |
| 76 EXPECT_TRUE(got_root_surface_id_); | |
| 77 AddEvent(base::StringPrintf("OnSurfaceCreated(%s)", | |
| 78 SurfaceIdString(surface_info.id()).c_str())); | |
| 79 } | |
| 80 | |
| 81 mojo::Binding<cc::mojom::DisplayCompositorClient> binding_; | |
| 82 std::string events_; | |
| 83 bool got_root_surface_id_ = false; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(TestDisplayCompositorClient); | |
| 86 }; | |
| 87 | |
| 88 // Test SurfaceReferenceManager that records AddSurfaceReference() and | |
| 89 // RemoveSurfaceReference() events. | |
| 90 class TestSurfaceReferenceManager : public cc::SurfaceReferenceManager { | |
| 91 public: | |
| 92 ~TestSurfaceReferenceManager() override {} | |
| 93 | |
| 94 const cc::SurfaceId& GetRootSurfaceId() const override { return root_id_; } | |
| 95 | |
| 96 void AddSurfaceReference(const cc::SurfaceId& parent_id, | |
| 97 const cc::SurfaceId& child_id) override { | |
| 98 AddEvent(base::StringPrintf("Add(%s-%s)", | |
| 99 SurfaceIdString(parent_id).c_str(), | |
| 100 SurfaceIdString(child_id).c_str())); | |
| 101 } | |
| 102 | |
| 103 void RemoveSurfaceReference(const cc::SurfaceId& parent_id, | |
| 104 const cc::SurfaceId& child_id) override { | |
| 105 AddEvent(base::StringPrintf("Remove(%s-%s)", | |
| 106 SurfaceIdString(parent_id).c_str(), | |
| 107 SurfaceIdString(child_id).c_str())); | |
| 108 } | |
| 109 | |
| 110 size_t GetSurfaceReferenceCount( | |
| 111 const cc::SurfaceId& surface_id) const override { | |
| 112 NOTREACHED(); | |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 size_t GetReferencedSurfaceCount( | |
| 117 const cc::SurfaceId& surface_id) const override { | |
| 118 NOTREACHED(); | |
| 119 return 0; | |
| 120 } | |
| 121 | |
| 122 // Returns events that have occurred and clear. | |
| 123 std::string events() { | |
| 124 std::string value = std::move(events_); | |
| 125 events_.clear(); | |
| 126 return value; | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 void AddEvent(const std::string& text) { | |
| 131 if (!events_.empty()) | |
| 132 events_ += ";"; | |
| 133 events_ += text; | |
| 134 } | |
| 135 | |
| 136 const cc::SurfaceId root_id_ = MakeSurfaceId(0, 0, 0); | |
| 137 std::string events_; | |
| 138 }; | |
| 139 | |
| 140 } // namespace | |
| 141 | |
| 142 class DisplayCompositorTest : public TaskRunnerTestBase { | |
| 143 public: | |
| 144 DisplayCompositorTest() {} | |
| 145 ~DisplayCompositorTest() override {} | |
| 146 | |
| 147 cc::SurfaceObserver* surface_observer() { return display_compositor_.get(); } | |
| 148 | |
| 149 const cc::SurfaceId& GetRootSurfaceId() const { | |
| 150 return reference_manager_.GetRootSurfaceId(); | |
| 151 } | |
| 152 | |
| 153 void AddSurfaceReference(const cc::SurfaceId& parent_id, | |
| 154 const cc::SurfaceId& child_id) { | |
| 155 display_compositor_->AddSurfaceReferences(std::vector<cc::SurfaceReference>{ | |
| 156 cc::SurfaceReference(parent_id, child_id)}); | |
| 157 } | |
| 158 | |
| 159 // Returns the total number of temporary references held by DisplayCompositor. | |
| 160 size_t CountTempReferences() { | |
| 161 size_t size = 0; | |
| 162 for (auto& map_entry : display_compositor_->temp_references_) { | |
| 163 size += map_entry.second.size(); | |
| 164 } | |
| 165 return size; | |
| 166 } | |
| 167 | |
| 168 // TaskRunnerTestBase: | |
| 169 void SetUp() override { | |
| 170 TaskRunnerTestBase::SetUp(); | |
| 171 display_compositor_ = base::MakeUnique<DisplayCompositor>( | |
| 172 nullptr, nullptr, nullptr, nullptr, client_.GetPtr()); | |
| 173 display_compositor_->reference_manager_ = &reference_manager_; | |
| 174 } | |
| 175 | |
| 176 void TearDown() override { | |
| 177 // Clear any events before the next test. | |
| 178 client_.events(); | |
| 179 reference_manager_.events(); | |
| 180 } | |
| 181 | |
| 182 protected: | |
| 183 TestDisplayCompositorClient client_; | |
| 184 TestSurfaceReferenceManager reference_manager_; | |
| 185 std::unique_ptr<DisplayCompositor> display_compositor_; | |
| 186 | |
| 187 private: | |
| 188 DISALLOW_COPY_AND_ASSIGN(DisplayCompositorTest); | |
| 189 }; | |
| 190 | |
| 191 TEST_F(DisplayCompositorTest, AddSurfaceThenReference) { | |
| 192 const cc::SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 193 const cc::SurfaceId surface_id = MakeSurfaceId(2, 1, 1); | |
| 194 surface_observer()->OnSurfaceCreated( | |
| 195 cc::SurfaceInfo(surface_id, 1.0f, gfx::Size(1, 1))); | |
| 196 RunUntilIdle(); | |
| 197 | |
| 198 // Client should get OnSurfaceCreated call and temporary reference added. | |
| 199 EXPECT_EQ("OnSurfaceCreated(2:1:1)", client_.events()); | |
| 200 EXPECT_EQ("Add(0:0:0-2:1:1)", reference_manager_.events()); | |
| 201 EXPECT_EQ(1u, CountTempReferences()); | |
| 202 | |
| 203 AddSurfaceReference(parent_id, surface_id); | |
| 204 RunUntilIdle(); | |
| 205 | |
| 206 // Real reference is added then temporary reference removed. | |
| 207 EXPECT_EQ("Add(1:1:1-2:1:1);Remove(0:0:0-2:1:1)", | |
| 208 reference_manager_.events()); | |
| 209 EXPECT_EQ(0u, CountTempReferences()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(DisplayCompositorTest, AddSurfaceThenRootReference) { | |
| 213 const cc::SurfaceId surface_id = MakeSurfaceId(1, 1, 1); | |
| 214 surface_observer()->OnSurfaceCreated( | |
| 215 cc::SurfaceInfo(surface_id, 1.0f, gfx::Size(1, 1))); | |
| 216 RunUntilIdle(); | |
| 217 | |
| 218 // Temporary reference should be added. | |
| 219 EXPECT_EQ("Add(0:0:0-1:1:1)", reference_manager_.events()); | |
| 220 EXPECT_EQ(1u, CountTempReferences()); | |
| 221 | |
| 222 AddSurfaceReference(GetRootSurfaceId(), surface_id); | |
| 223 RunUntilIdle(); | |
| 224 | |
| 225 // Adding real reference doesn't need to change anything in | |
| 226 // SurfaceReferenceManager does remove the temporary reference marker. | |
| 227 EXPECT_EQ("", reference_manager_.events()); | |
| 228 EXPECT_EQ(0u, CountTempReferences()); | |
| 229 } | |
| 230 | |
| 231 TEST_F(DisplayCompositorTest, AddTwoSurfacesThenOneReference) { | |
| 232 const cc::SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 233 const cc::SurfaceId surface_id1 = MakeSurfaceId(2, 1, 1); | |
| 234 const cc::SurfaceId surface_id2 = MakeSurfaceId(3, 1, 1); | |
| 235 | |
| 236 // Add two surfaces with different FrameSinkIds. | |
| 237 surface_observer()->OnSurfaceCreated( | |
| 238 cc::SurfaceInfo(surface_id1, 1.0f, gfx::Size(1, 1))); | |
| 239 surface_observer()->OnSurfaceCreated( | |
| 240 cc::SurfaceInfo(surface_id2, 1.0f, gfx::Size(1, 1))); | |
| 241 RunUntilIdle(); | |
| 242 | |
| 243 // Temporary reference should be added for both surfaces. | |
| 244 EXPECT_EQ("Add(0:0:0-2:1:1);Add(0:0:0-3:1:1)", reference_manager_.events()); | |
| 245 EXPECT_EQ(2u, CountTempReferences()); | |
| 246 | |
| 247 AddSurfaceReference(parent_id, surface_id1); | |
| 248 RunUntilIdle(); | |
| 249 | |
| 250 // Real reference is added then temporary reference removed for 2:1:1. There | |
| 251 // should still be a temporary reference left to 3:1:1 | |
| 252 EXPECT_EQ("Add(1:1:1-2:1:1);Remove(0:0:0-2:1:1)", | |
| 253 reference_manager_.events()); | |
| 254 EXPECT_EQ(1u, CountTempReferences()); | |
| 255 } | |
| 256 | |
| 257 TEST_F(DisplayCompositorTest, AddSurfacesSkipReference) { | |
| 258 const cc::SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 259 const cc::SurfaceId surface_id1 = MakeSurfaceId(2, 1, 1); | |
| 260 const cc::SurfaceId surface_id2 = MakeSurfaceId(2, 1, 2); | |
| 261 | |
| 262 // Add two surfaces that have the same FrameSinkId. This would happen when a | |
| 263 // client submits two CFs before parent submits a new CF. | |
| 264 surface_observer()->OnSurfaceCreated( | |
| 265 cc::SurfaceInfo(surface_id1, 1.0f, gfx::Size(1, 1))); | |
| 266 surface_observer()->OnSurfaceCreated( | |
| 267 cc::SurfaceInfo(surface_id2, 1.0f, gfx::Size(1, 1))); | |
| 268 RunUntilIdle(); | |
| 269 | |
| 270 // Client should get OnSurfaceCreated call and temporary reference added for | |
| 271 // both surfaces. | |
| 272 EXPECT_EQ("OnSurfaceCreated(2:1:1);OnSurfaceCreated(2:1:2)", | |
| 273 client_.events()); | |
| 274 EXPECT_EQ("Add(0:0:0-2:1:1);Add(0:0:0-2:1:2)", reference_manager_.events()); | |
| 275 EXPECT_EQ(2u, CountTempReferences()); | |
| 276 | |
| 277 // Add a reference to the surface with the later LocalFrameId. | |
| 278 AddSurfaceReference(parent_id, surface_id2); | |
| 279 RunUntilIdle(); | |
| 280 | |
| 281 // The real reference should be added for 2:1:2 and both temporary references | |
| 282 // should be removed. | |
| 283 EXPECT_EQ("Add(1:1:1-2:1:2);Remove(0:0:0-2:1:2);Remove(0:0:0-2:1:1)", | |
| 284 reference_manager_.events()); | |
| 285 EXPECT_EQ(0u, CountTempReferences()); | |
| 286 } | |
| 287 | |
| 288 TEST_F(DisplayCompositorTest, RemoveFirstTempRefOnly) { | |
| 289 const cc::SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 290 const cc::SurfaceId surface_id1 = MakeSurfaceId(2, 1, 1); | |
| 291 const cc::SurfaceId surface_id2 = MakeSurfaceId(2, 1, 2); | |
| 292 | |
| 293 // Add two surfaces that have the same FrameSinkId. This would happen when a | |
| 294 // client submits two CFs before parent submits a new CF. | |
| 295 surface_observer()->OnSurfaceCreated( | |
| 296 cc::SurfaceInfo(surface_id1, 1.0f, gfx::Size(1, 1))); | |
| 297 surface_observer()->OnSurfaceCreated( | |
| 298 cc::SurfaceInfo(surface_id2, 1.0f, gfx::Size(1, 1))); | |
| 299 RunUntilIdle(); | |
| 300 | |
| 301 // Client should get OnSurfaceCreated call and temporary reference added for | |
| 302 // both surfaces. | |
| 303 EXPECT_EQ("OnSurfaceCreated(2:1:1);OnSurfaceCreated(2:1:2)", | |
| 304 client_.events()); | |
| 305 EXPECT_EQ("Add(0:0:0-2:1:1);Add(0:0:0-2:1:2)", reference_manager_.events()); | |
| 306 EXPECT_EQ(2u, CountTempReferences()); | |
| 307 | |
| 308 // Add a reference to the surface with the earlier LocalFrameId. | |
| 309 AddSurfaceReference(parent_id, surface_id1); | |
| 310 RunUntilIdle(); | |
| 311 | |
| 312 // The real reference should be added for 2:1:1 and temporary reference | |
| 313 // should be removed. The temporary reference for 2:1:2 should remain. | |
| 314 EXPECT_EQ("Add(1:1:1-2:1:1);Remove(0:0:0-2:1:1)", | |
| 315 reference_manager_.events()); | |
| 316 EXPECT_EQ(1u, CountTempReferences()); | |
| 317 } | |
| 318 | |
| 319 } // namespace test | |
| 320 } // namespace ui | |
| OLD | NEW |