Chromium Code Reviews| 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/ref_counted.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "cc/ipc/display_compositor.mojom.h" | |
| 17 #include "cc/surfaces/surface_id.h" | |
| 18 #include "cc/surfaces/surface_observer.h" | |
| 19 #include "mojo/public/cpp/bindings/binding.h" | |
| 20 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 21 #include "services/ui/common/task_runner_test_base.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "ui/gfx/geometry/size.h" | |
| 24 | |
| 25 using cc::SurfaceId; | |
|
vmpstr
2016/12/01 21:15:23
You have a mix of SurfaceId and cc::SurfaceId in t
kylechar
2016/12/01 21:53:29
Done.
| |
| 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 SurfaceId MakeSurfaceId(uint32_t client_id, | |
| 38 uint32_t sink_id, | |
| 39 uint32_t local_id) { | |
| 40 return 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 OnSurfaceCreated(const cc::SurfaceId& surface_id, | |
| 71 const gfx::Size& frame_size, | |
| 72 float device_scale_factor) override { | |
| 73 AddEvent(base::StringPrintf("OnSurfaceCreated(%s)", | |
| 74 SurfaceIdString(surface_id).c_str())); | |
| 75 } | |
| 76 | |
| 77 mojo::Binding<cc::mojom::DisplayCompositorClient> binding_; | |
| 78 std::string events_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(TestDisplayCompositorClient); | |
| 81 }; | |
| 82 | |
| 83 // Test SurfaceReferenceManager that records AddSurfaceReference() and | |
| 84 // RemoveSurfaceReference() events. | |
| 85 class TestSurfaceReferenceManager : public cc::SurfaceReferenceManager { | |
| 86 public: | |
| 87 ~TestSurfaceReferenceManager() override {} | |
| 88 | |
| 89 const SurfaceId& GetRootSurfaceId() const override { return root_id_; } | |
| 90 | |
| 91 void AddSurfaceReference(const SurfaceId& parent_id, | |
| 92 const SurfaceId& child_id) override { | |
| 93 AddEvent(base::StringPrintf("Add(%s-%s)", | |
| 94 SurfaceIdString(parent_id).c_str(), | |
| 95 SurfaceIdString(child_id).c_str())); | |
| 96 } | |
| 97 | |
| 98 void RemoveSurfaceReference(const SurfaceId& parent_id, | |
| 99 const SurfaceId& child_id) override { | |
| 100 AddEvent(base::StringPrintf("Remove(%s-%s)", | |
| 101 SurfaceIdString(parent_id).c_str(), | |
| 102 SurfaceIdString(child_id).c_str())); | |
| 103 } | |
| 104 | |
| 105 size_t GetSurfaceReferenceCount(const SurfaceId& surface_id) const override { | |
| 106 NOTREACHED(); | |
| 107 return 0; | |
| 108 } | |
| 109 | |
| 110 size_t GetReferencedSurfaceCount(const SurfaceId& surface_id) const override { | |
| 111 NOTREACHED(); | |
| 112 return 0; | |
| 113 } | |
| 114 | |
| 115 // Returns events that have occurred and clear. | |
| 116 std::string events() { | |
| 117 std::string value = std::move(events_); | |
| 118 events_.clear(); | |
| 119 return value; | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 void AddEvent(const std::string& text) { | |
| 124 if (!events_.empty()) | |
| 125 events_ += ";"; | |
| 126 events_ += text; | |
| 127 } | |
| 128 | |
| 129 const SurfaceId root_id_ = MakeSurfaceId(0, 0, 0); | |
| 130 std::string events_; | |
| 131 }; | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 class DisplayCompositorTest : public TaskRunnerTestBase { | |
| 136 public: | |
| 137 DisplayCompositorTest() {} | |
| 138 ~DisplayCompositorTest() override {} | |
| 139 | |
| 140 cc::SurfaceObserver* surface_observer() { return display_compositor_.get(); } | |
| 141 | |
| 142 // Returns the total number of temporary references held by DisplayCompositor. | |
| 143 size_t CountTempReferences() { | |
| 144 size_t size = 0; | |
| 145 for (auto& map_entry : display_compositor_->temp_references_) { | |
| 146 size += map_entry.second.size(); | |
| 147 } | |
| 148 return size; | |
| 149 } | |
| 150 | |
| 151 // TaskRunnerTestBase: | |
| 152 void SetUp() override { | |
| 153 TaskRunnerTestBase::SetUp(); | |
| 154 display_compositor_ = base::MakeUnique<DisplayCompositor>( | |
| 155 nullptr, nullptr, nullptr, nullptr, client_.GetPtr()); | |
| 156 display_compositor_->reference_manager_ = &reference_manager_; | |
| 157 } | |
| 158 | |
| 159 void TearDown() override { | |
| 160 // Clear any events before the next test. | |
| 161 client_.events(); | |
| 162 reference_manager_.events(); | |
| 163 } | |
| 164 | |
| 165 protected: | |
| 166 TestDisplayCompositorClient client_; | |
| 167 TestSurfaceReferenceManager reference_manager_; | |
| 168 std::unique_ptr<DisplayCompositor> display_compositor_; | |
| 169 | |
| 170 private: | |
| 171 DISALLOW_COPY_AND_ASSIGN(DisplayCompositorTest); | |
| 172 }; | |
| 173 | |
| 174 TEST_F(DisplayCompositorTest, AddSurfaceThenReference) { | |
| 175 const SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 176 const SurfaceId surface_id = MakeSurfaceId(2, 1, 1); | |
| 177 surface_observer()->OnSurfaceCreated(surface_id, gfx::Size(1, 1), 1.0f); | |
| 178 RunUntilIdle(); | |
| 179 | |
| 180 // Client should get OnSurfaceCreated call and temporary reference added. | |
| 181 EXPECT_EQ("OnSurfaceCreated(2:1:1)", client_.events()); | |
| 182 EXPECT_EQ("Add(0:0:0-2:1:1)", reference_manager_.events()); | |
| 183 EXPECT_EQ(1u, CountTempReferences()); | |
| 184 | |
| 185 display_compositor_->AddSurfaceReference(parent_id, surface_id); | |
| 186 RunUntilIdle(); | |
| 187 | |
| 188 // Real reference is added then temporary reference removed. | |
| 189 EXPECT_EQ("Add(1:1:1-2:1:1);Remove(0:0:0-2:1:1)", | |
| 190 reference_manager_.events()); | |
| 191 EXPECT_EQ(0u, CountTempReferences()); | |
| 192 } | |
| 193 | |
| 194 TEST_F(DisplayCompositorTest, AddSurfaceThenRootReference) { | |
| 195 const SurfaceId surface_id = MakeSurfaceId(1, 1, 1); | |
| 196 surface_observer()->OnSurfaceCreated(surface_id, gfx::Size(1, 1), 1.0f); | |
| 197 RunUntilIdle(); | |
| 198 | |
| 199 // Temporary reference should be added. | |
| 200 EXPECT_EQ("Add(0:0:0-1:1:1)", reference_manager_.events()); | |
| 201 EXPECT_EQ(1u, CountTempReferences()); | |
| 202 | |
| 203 display_compositor_->AddRootSurfaceReference(surface_id); | |
| 204 RunUntilIdle(); | |
| 205 | |
| 206 // Adding real reference doesn't need to change anything in | |
| 207 // SurfaceReferenceManager does remove the temporary reference marker. | |
| 208 EXPECT_EQ("", reference_manager_.events()); | |
| 209 EXPECT_EQ(0u, CountTempReferences()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(DisplayCompositorTest, AddTwoSurfacesThenOneReference) { | |
| 213 const SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 214 const SurfaceId surface_id1 = MakeSurfaceId(2, 1, 1); | |
| 215 const SurfaceId surface_id2 = MakeSurfaceId(3, 1, 1); | |
| 216 | |
| 217 // Add two surfaces with different FrameSinkIds. | |
| 218 surface_observer()->OnSurfaceCreated(surface_id1, gfx::Size(1, 1), 1.0f); | |
| 219 surface_observer()->OnSurfaceCreated(surface_id2, gfx::Size(1, 1), 1.0f); | |
| 220 RunUntilIdle(); | |
| 221 | |
| 222 // Temporary reference should be added for both surfaces. | |
| 223 EXPECT_EQ("Add(0:0:0-2:1:1);Add(0:0:0-3:1:1)", reference_manager_.events()); | |
| 224 EXPECT_EQ(2u, CountTempReferences()); | |
| 225 | |
| 226 display_compositor_->AddSurfaceReference(parent_id, surface_id1); | |
| 227 RunUntilIdle(); | |
| 228 | |
| 229 // Real reference is added then temporary reference removed for 2:1:1. There | |
| 230 // should still be a temporary reference left to 3:1:1 | |
| 231 EXPECT_EQ("Add(1:1:1-2:1:1);Remove(0:0:0-2:1:1)", | |
| 232 reference_manager_.events()); | |
| 233 EXPECT_EQ(1u, CountTempReferences()); | |
| 234 } | |
| 235 | |
| 236 TEST_F(DisplayCompositorTest, AddSurfacesSkipReference) { | |
| 237 const SurfaceId parent_id = MakeSurfaceId(1, 1, 1); | |
| 238 const SurfaceId surface_id1 = MakeSurfaceId(2, 1, 1); | |
| 239 const SurfaceId surface_id2 = MakeSurfaceId(2, 1, 2); | |
| 240 | |
| 241 // Add two surfaces that have the same FrameSinkId. This would happen when a | |
| 242 // client submits two CFs before parent submits a new CF. | |
| 243 surface_observer()->OnSurfaceCreated(surface_id1, gfx::Size(1, 1), 1.0f); | |
| 244 surface_observer()->OnSurfaceCreated(surface_id2, gfx::Size(1, 1), 1.0f); | |
| 245 RunUntilIdle(); | |
| 246 | |
| 247 // Client should get OnSurfaceCreated call and temporary reference added for | |
| 248 // both surfaces. | |
| 249 EXPECT_EQ("OnSurfaceCreated(2:1:1);OnSurfaceCreated(2:1:2)", | |
| 250 client_.events()); | |
| 251 EXPECT_EQ("Add(0:0:0-2:1:1);Add(0:0:0-2:1:2)", reference_manager_.events()); | |
| 252 EXPECT_EQ(2u, CountTempReferences()); | |
| 253 | |
| 254 // Add a reference to the surface with the later LocalFrameId. | |
| 255 display_compositor_->AddSurfaceReference(parent_id, surface_id2); | |
| 256 RunUntilIdle(); | |
| 257 | |
| 258 // The real reference should be added for 2:1:2 and both temporary references | |
| 259 // should be removed. | |
| 260 EXPECT_EQ("Add(1:1:1-2:1:2);Remove(0:0:0-2:1:2);Remove(0:0:0-2:1:1)", | |
| 261 reference_manager_.events()); | |
| 262 EXPECT_EQ(0u, CountTempReferences()); | |
| 263 } | |
| 264 | |
| 265 } // namespace test | |
| 266 } // namespace ui | |
| OLD | NEW |