OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "cc/surfaces/surface_factory.h" | 5 #include "cc/surfaces/surface_factory.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "cc/output/compositor_frame.h" | 10 #include "cc/output/compositor_frame.h" |
11 #include "cc/output/copy_output_request.h" | 11 #include "cc/output/copy_output_request.h" |
12 #include "cc/surfaces/surface.h" | 12 #include "cc/surfaces/surface.h" |
13 #include "cc/surfaces/surface_factory_client.h" | 13 #include "cc/surfaces/surface_factory_client.h" |
14 #include "cc/surfaces/surface_manager.h" | 14 #include "cc/surfaces/surface_manager.h" |
15 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
16 | 16 |
17 namespace cc { | 17 namespace cc { |
18 SurfaceFactory::SurfaceFactory(const FrameSinkId& frame_sink_id, | 18 SurfaceFactory::SurfaceFactory(const FrameSinkId& frame_sink_id, |
19 SurfaceManager* manager, | 19 SurfaceManager* manager, |
20 SurfaceFactoryClient* client) | 20 SurfaceFactoryClient* client) |
21 : frame_sink_id_(frame_sink_id), | 21 : frame_sink_id_(frame_sink_id), |
22 manager_(manager), | 22 manager_(manager), |
23 client_(client), | 23 client_(client), |
24 holder_(client), | 24 holder_(client), |
25 needs_sync_points_(true), | 25 needs_sync_points_(true), |
26 weak_factory_(this) {} | 26 weak_factory_(this) {} |
27 | 27 |
28 SurfaceFactory::~SurfaceFactory() { | 28 SurfaceFactory::~SurfaceFactory() { |
29 // This is to prevent troubles when a factory that resides in a client is | 29 if (!surface_map_.empty()) { |
30 // being destroyed. In such cases, the factory might attempt to return | 30 LOG(ERROR) << "SurfaceFactory has " << surface_map_.size() |
31 // resources to the client while it's in the middle of destruction and this | 31 << " entries in map on destruction."; |
32 // could cause a crash or some unexpected behaviour. | 32 } |
33 DCHECK(!current_surface_) << "Please call EvictSurface before destruction"; | 33 DestroyAll(); |
34 } | 34 } |
35 | 35 |
36 void SurfaceFactory::EvictSurface() { | 36 void SurfaceFactory::DestroyAll() { |
37 if (!current_surface_) | 37 if (manager_) { |
38 return; | 38 for (auto& pair : surface_map_) |
39 if (manager_) | 39 manager_->Destroy(std::move(pair.second)); |
40 manager_->Destroy(std::move(current_surface_)); | 40 } |
41 current_surface_.reset(); | 41 surface_map_.clear(); |
42 } | 42 } |
43 | 43 |
44 void SurfaceFactory::Reset() { | 44 void SurfaceFactory::Reset() { |
45 EvictSurface(); | 45 DestroyAll(); |
46 // Disown Surfaces that are still alive so that they don't try to unref | 46 // Disown Surfaces that are still alive so that they don't try to unref |
47 // resources that we're not tracking any more. | 47 // resources that we're not tracking any more. |
48 weak_factory_.InvalidateWeakPtrs(); | 48 weak_factory_.InvalidateWeakPtrs(); |
49 holder_.Reset(); | 49 holder_.Reset(); |
50 } | 50 } |
51 | 51 |
| 52 void SurfaceFactory::Create(const LocalFrameId& local_frame_id) { |
| 53 auto surface(base::MakeUnique<Surface>( |
| 54 SurfaceId(frame_sink_id_, local_frame_id), weak_factory_.GetWeakPtr())); |
| 55 manager_->RegisterSurface(surface.get()); |
| 56 DCHECK(!surface_map_.count(local_frame_id)); |
| 57 surface_map_[local_frame_id] = std::move(surface); |
| 58 } |
| 59 |
| 60 void SurfaceFactory::Destroy(const LocalFrameId& local_frame_id) { |
| 61 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); |
| 62 DCHECK(it != surface_map_.end()); |
| 63 DCHECK(it->second->factory().get() == this); |
| 64 std::unique_ptr<Surface> surface(std::move(it->second)); |
| 65 surface_map_.erase(it); |
| 66 if (manager_) |
| 67 manager_->Destroy(std::move(surface)); |
| 68 } |
| 69 |
| 70 void SurfaceFactory::SetPreviousFrameSurface(const LocalFrameId& new_id, |
| 71 const LocalFrameId& old_id) { |
| 72 OwningSurfaceMap::iterator it = surface_map_.find(new_id); |
| 73 DCHECK(it != surface_map_.end()); |
| 74 Surface* old_surface = |
| 75 manager_->GetSurfaceForId(SurfaceId(frame_sink_id_, old_id)); |
| 76 if (old_surface) { |
| 77 it->second->SetPreviousFrameSurface(old_surface); |
| 78 } |
| 79 } |
| 80 |
52 void SurfaceFactory::SubmitCompositorFrame(const LocalFrameId& local_frame_id, | 81 void SurfaceFactory::SubmitCompositorFrame(const LocalFrameId& local_frame_id, |
53 CompositorFrame frame, | 82 CompositorFrame frame, |
54 const DrawCallback& callback) { | 83 const DrawCallback& callback) { |
55 TRACE_EVENT0("cc", "SurfaceFactory::SubmitCompositorFrame"); | 84 TRACE_EVENT0("cc", "SurfaceFactory::SubmitCompositorFrame"); |
56 DCHECK(local_frame_id.is_valid()); | 85 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); |
57 std::unique_ptr<Surface> surface; | 86 DCHECK(it != surface_map_.end()); |
58 bool create_new_surface = | 87 DCHECK(it->second->factory().get() == this); |
59 (!current_surface_ || | 88 // Tell the SurfaceManager if this is the first frame submitted with this |
60 local_frame_id != current_surface_->surface_id().local_frame_id()); | 89 // LocalFrameId. |
61 if (!create_new_surface) { | 90 if (!it->second->HasFrame()) { |
62 surface = std::move(current_surface_); | 91 float device_scale_factor = frame.metadata.device_scale_factor; |
63 } else { | |
64 surface = Create(local_frame_id); | |
65 gfx::Size frame_size; | 92 gfx::Size frame_size; |
66 // CompositorFrames may not be populated with a RenderPass in unit tests. | 93 // CompositorFrames may not be populated with a RenderPass in unit tests. |
67 if (!frame.render_pass_list.empty()) | 94 if (!frame.render_pass_list.empty()) |
68 frame_size = frame.render_pass_list[0]->output_rect.size(); | 95 frame_size = frame.render_pass_list[0]->output_rect.size(); |
69 manager_->SurfaceCreated(surface->surface_id(), frame_size, | 96 manager_->SurfaceCreated(it->second->surface_id(), frame_size, |
70 frame.metadata.device_scale_factor); | 97 device_scale_factor); |
71 } | 98 } |
72 surface->QueueFrame(std::move(frame), callback); | 99 it->second->QueueFrame(std::move(frame), callback); |
73 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) { | 100 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) { |
74 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); | 101 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); |
75 surface->RunDrawCallbacks(); | 102 it->second->RunDrawCallbacks(); |
76 } | 103 } |
77 if (current_surface_ && create_new_surface) { | |
78 surface->SetPreviousFrameSurface(current_surface_.get()); | |
79 Destroy(std::move(current_surface_)); | |
80 } | |
81 current_surface_ = std::move(surface); | |
82 } | 104 } |
83 | 105 |
84 void SurfaceFactory::RequestCopyOfSurface( | 106 void SurfaceFactory::RequestCopyOfSurface( |
| 107 const LocalFrameId& local_frame_id, |
85 std::unique_ptr<CopyOutputRequest> copy_request) { | 108 std::unique_ptr<CopyOutputRequest> copy_request) { |
86 if (!current_surface_) { | 109 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); |
| 110 if (it == surface_map_.end()) { |
87 copy_request->SendEmptyResult(); | 111 copy_request->SendEmptyResult(); |
88 return; | 112 return; |
89 } | 113 } |
90 DCHECK(current_surface_->factory().get() == this); | 114 DCHECK(it->second->factory().get() == this); |
91 current_surface_->RequestCopyOfOutput(std::move(copy_request)); | 115 it->second->RequestCopyOfOutput(std::move(copy_request)); |
92 manager_->SurfaceModified(current_surface_->surface_id()); | 116 manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id)); |
93 } | 117 } |
94 | 118 |
95 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id, | 119 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id, |
96 const gfx::Rect& damage_rect) { | 120 const gfx::Rect& damage_rect) { |
97 client_->WillDrawSurface(id, damage_rect); | 121 client_->WillDrawSurface(id, damage_rect); |
98 } | 122 } |
99 | 123 |
100 void SurfaceFactory::ReceiveFromChild( | 124 void SurfaceFactory::ReceiveFromChild( |
101 const TransferableResourceArray& resources) { | 125 const TransferableResourceArray& resources) { |
102 holder_.ReceiveFromChild(resources); | 126 holder_.ReceiveFromChild(resources); |
103 } | 127 } |
104 | 128 |
105 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { | 129 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { |
106 holder_.RefResources(resources); | 130 holder_.RefResources(resources); |
107 } | 131 } |
108 | 132 |
109 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { | 133 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { |
110 holder_.UnrefResources(resources); | 134 holder_.UnrefResources(resources); |
111 } | 135 } |
112 | 136 |
113 std::unique_ptr<Surface> SurfaceFactory::Create( | |
114 const LocalFrameId& local_frame_id) { | |
115 auto surface = base::MakeUnique<Surface>( | |
116 SurfaceId(frame_sink_id_, local_frame_id), weak_factory_.GetWeakPtr()); | |
117 manager_->RegisterSurface(surface.get()); | |
118 return surface; | |
119 } | |
120 | |
121 void SurfaceFactory::Destroy(std::unique_ptr<Surface> surface) { | |
122 if (manager_) | |
123 manager_->Destroy(std::move(surface)); | |
124 } | |
125 | |
126 } // namespace cc | 137 } // namespace cc |
OLD | NEW |