| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <unordered_map> | 7 #include <unordered_map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "cc/surfaces/surface.h" | 11 #include "cc/surfaces/surface.h" |
| 12 #include "cc/surfaces/surface_factory.h" | 12 #include "cc/surfaces/surface_factory.h" |
| 13 #include "cc/surfaces/surface_factory_client.h" | |
| 14 #include "cc/surfaces/surface_id.h" | 13 #include "cc/surfaces/surface_id.h" |
| 15 #include "cc/surfaces/surface_manager.h" | 14 #include "cc/surfaces/surface_manager.h" |
| 16 #include "cc/surfaces/surface_sequence_generator.h" | 15 #include "cc/surfaces/surface_sequence_generator.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace cc { | 18 namespace cc { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 constexpr FrameSinkId kFrameSink1(1, 0); | 21 constexpr FrameSinkId kFrameSink1(1, 0); |
| 22 constexpr FrameSinkId kFrameSink2(2, 0); | 22 constexpr FrameSinkId kFrameSink2(2, 0); |
| 23 constexpr FrameSinkId kFrameSink3(3, 0); | 23 constexpr FrameSinkId kFrameSink3(3, 0); |
| 24 const LocalFrameId kLocalFrame1(1, base::UnguessableToken::Create()); | 24 const LocalFrameId kLocalFrame1(1, base::UnguessableToken::Create()); |
| 25 const LocalFrameId kLocalFrame2(2, base::UnguessableToken::Create()); | 25 const LocalFrameId kLocalFrame2(2, base::UnguessableToken::Create()); |
| 26 | 26 |
| 27 class StubSurfaceFactoryClient : public SurfaceFactoryClient { | |
| 28 public: | |
| 29 void ReturnResources(const ReturnedResourceArray& resources) override {} | |
| 30 void SetBeginFrameSource(BeginFrameSource* begin_frame_source) override {} | |
| 31 }; | |
| 32 | |
| 33 // Tests for reference tracking in SurfaceManager. | 27 // Tests for reference tracking in SurfaceManager. |
| 34 class SurfaceManagerRefTest : public testing::Test { | 28 class SurfaceManagerRefTest : public testing::Test { |
| 35 public: | 29 public: |
| 36 SurfaceManager& manager() { return *manager_; } | 30 SurfaceManager& manager() { return *manager_; } |
| 37 | 31 |
| 38 // Creates a new Surface with the provided SurfaceId. Will first create the | 32 // Creates a new Surface with the provided SurfaceId. Will first create the |
| 39 // SurfaceFactory for |frame_sink_id| if necessary. | 33 // SurfaceFactory for |frame_sink_id| if necessary. |
| 40 SurfaceId CreateSurface(const FrameSinkId& frame_sink_id, | 34 SurfaceId CreateSurface(const FrameSinkId& frame_sink_id, |
| 41 const LocalFrameId& local_frame_id) { | 35 const LocalFrameId& local_frame_id) { |
| 42 GetFactory(frame_sink_id) | 36 GetFactory(frame_sink_id).Create(local_frame_id); |
| 43 .SubmitCompositorFrame(local_frame_id, CompositorFrame(), | |
| 44 SurfaceFactory::DrawCallback()); | |
| 45 return SurfaceId(frame_sink_id, local_frame_id); | 37 return SurfaceId(frame_sink_id, local_frame_id); |
| 46 } | 38 } |
| 47 | 39 |
| 48 // Destroy Surface with |surface_id|. | 40 // Destroy Surface with |surface_id|. |
| 49 void DestroySurface(const SurfaceId& surface_id) { | 41 void DestroySurface(const SurfaceId& surface_id) { |
| 50 GetFactory(surface_id.frame_sink_id()).EvictSurface(); | 42 GetFactory(surface_id.frame_sink_id()).Destroy(surface_id.local_frame_id()); |
| 51 } | 43 } |
| 52 | 44 |
| 53 protected: | 45 protected: |
| 54 SurfaceFactory& GetFactory(const FrameSinkId& frame_sink_id) { | 46 SurfaceFactory& GetFactory(const FrameSinkId& frame_sink_id) { |
| 55 auto& factory_ptr = factories_[frame_sink_id]; | 47 auto& factory_ptr = factories_[frame_sink_id]; |
| 56 if (!factory_ptr) | 48 if (!factory_ptr) |
| 57 factory_ptr = base::MakeUnique<SurfaceFactory>(frame_sink_id, | 49 factory_ptr = base::MakeUnique<SurfaceFactory>(frame_sink_id, |
| 58 manager_.get(), &client_); | 50 manager_.get(), nullptr); |
| 59 return *factory_ptr; | 51 return *factory_ptr; |
| 60 } | 52 } |
| 61 | 53 |
| 62 // testing::Test: | 54 // testing::Test: |
| 63 void SetUp() override { | 55 void SetUp() override { |
| 64 // Start each test with a fresh SurfaceManager instance. | 56 // Start each test with a fresh SurfaceManager instance. |
| 65 manager_ = base::MakeUnique<SurfaceManager>(); | 57 manager_ = base::MakeUnique<SurfaceManager>(); |
| 66 } | 58 } |
| 67 void TearDown() override { | 59 void TearDown() override { |
| 68 for (auto& factory : factories_) | |
| 69 factory.second->EvictSurface(); | |
| 70 factories_.clear(); | 60 factories_.clear(); |
| 71 manager_.reset(); | 61 manager_.reset(); |
| 72 } | 62 } |
| 73 | 63 |
| 74 std::unordered_map<FrameSinkId, | 64 std::unordered_map<FrameSinkId, |
| 75 std::unique_ptr<SurfaceFactory>, | 65 std::unique_ptr<SurfaceFactory>, |
| 76 FrameSinkIdHash> | 66 FrameSinkIdHash> |
| 77 factories_; | 67 factories_; |
| 78 std::unique_ptr<SurfaceManager> manager_; | 68 std::unique_ptr<SurfaceManager> manager_; |
| 79 StubSurfaceFactoryClient client_; | |
| 80 }; | 69 }; |
| 81 | 70 |
| 82 } // namespace | 71 } // namespace |
| 83 | 72 |
| 84 TEST_F(SurfaceManagerRefTest, AddReference) { | 73 TEST_F(SurfaceManagerRefTest, AddReference) { |
| 85 SurfaceId id1 = CreateSurface(kFrameSink1, kLocalFrame1); | 74 SurfaceId id1 = CreateSurface(kFrameSink1, kLocalFrame1); |
| 86 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 75 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 87 | 76 |
| 88 EXPECT_EQ(manager().GetSurfaceReferenceCount(id1), 1u); | 77 EXPECT_EQ(manager().GetSurfaceReferenceCount(id1), 1u); |
| 89 EXPECT_EQ(manager().GetReferencedSurfaceCount(id1), 0u); | 78 EXPECT_EQ(manager().GetReferencedSurfaceCount(id1), 0u); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 SurfaceId id2 = CreateSurface(kFrameSink2, kLocalFrame1); | 285 SurfaceId id2 = CreateSurface(kFrameSink2, kLocalFrame1); |
| 297 | 286 |
| 298 // Removing non-existent reference should be ignored. | 287 // Removing non-existent reference should be ignored. |
| 299 manager().AddSurfaceReference(id1, id2); | 288 manager().AddSurfaceReference(id1, id2); |
| 300 manager().RemoveSurfaceReference(id2, id1); | 289 manager().RemoveSurfaceReference(id2, id1); |
| 301 EXPECT_EQ(manager().GetReferencedSurfaceCount(id1), 1u); | 290 EXPECT_EQ(manager().GetReferencedSurfaceCount(id1), 1u); |
| 302 EXPECT_EQ(manager().GetSurfaceReferenceCount(id2), 1u); | 291 EXPECT_EQ(manager().GetSurfaceReferenceCount(id2), 1u); |
| 303 } | 292 } |
| 304 | 293 |
| 305 } // namespace cc | 294 } // namespace cc |
| OLD | NEW |