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 if (!surface_map_.empty()) { | 29 // This is to prevent troubles when a factory that resides in a client is |
30 LOG(ERROR) << "SurfaceFactory has " << surface_map_.size() | 30 // being destroyed. In such cases, the factory might attempt to return |
31 << " entries in map on destruction."; | 31 // resources to the client while it's in the middle of destruction and this |
32 } | 32 // could cause a crash or some unexpected behaviour. |
33 DestroyAll(); | 33 DCHECK(!current_surface_) << "Please call EvictSurface before destruction"; |
34 } | 34 } |
35 | 35 |
36 void SurfaceFactory::DestroyAll() { | 36 void SurfaceFactory::EvictSurface() { |
37 if (manager_) { | 37 if (!current_surface_) |
38 for (auto& pair : surface_map_) | 38 return; |
39 manager_->Destroy(std::move(pair.second)); | 39 if (manager_) |
40 } | 40 manager_->Destroy(std::move(current_surface_)); |
41 surface_map_.clear(); | 41 current_surface_.reset(); |
42 } | 42 } |
43 | 43 |
44 void SurfaceFactory::Reset() { | 44 void SurfaceFactory::Reset() { |
45 DestroyAll(); | 45 EvictSurface(); |
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 | |
81 void SurfaceFactory::SubmitCompositorFrame(const LocalFrameId& local_frame_id, | 52 void SurfaceFactory::SubmitCompositorFrame(const LocalFrameId& local_frame_id, |
82 CompositorFrame frame, | 53 CompositorFrame frame, |
83 const DrawCallback& callback) { | 54 const DrawCallback& callback) { |
84 TRACE_EVENT0("cc", "SurfaceFactory::SubmitCompositorFrame"); | 55 TRACE_EVENT0("cc", "SurfaceFactory::SubmitCompositorFrame"); |
85 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); | 56 DCHECK(local_frame_id.is_valid()); |
86 DCHECK(it != surface_map_.end()); | 57 std::unique_ptr<Surface> surface; |
87 DCHECK(it->second->factory().get() == this); | 58 bool create_new_surface = |
88 // Tell the SurfaceManager if this is the first frame submitted with this | 59 (!current_surface_ || |
89 // LocalFrameId. | 60 local_frame_id != current_surface_->surface_id().local_frame_id()); |
90 if (!it->second->HasFrame()) { | 61 if (!create_new_surface) { |
91 float device_scale_factor = frame.metadata.device_scale_factor; | 62 surface = std::move(current_surface_); |
| 63 } else { |
| 64 surface = Create(local_frame_id); |
92 gfx::Size frame_size; | 65 gfx::Size frame_size; |
93 // CompositorFrames may not be populated with a RenderPass in unit tests. | 66 // CompositorFrames may not be populated with a RenderPass in unit tests. |
94 if (!frame.render_pass_list.empty()) | 67 if (!frame.render_pass_list.empty()) |
95 frame_size = frame.render_pass_list[0]->output_rect.size(); | 68 frame_size = frame.render_pass_list[0]->output_rect.size(); |
96 manager_->SurfaceCreated(it->second->surface_id(), frame_size, | 69 manager_->SurfaceCreated(surface->surface_id(), frame_size, |
97 device_scale_factor); | 70 frame.metadata.device_scale_factor); |
98 } | 71 } |
99 it->second->QueueFrame(std::move(frame), callback); | 72 surface->QueueFrame(std::move(frame), callback); |
100 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) { | 73 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) { |
101 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); | 74 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); |
102 it->second->RunDrawCallbacks(); | 75 surface->RunDrawCallbacks(); |
103 } | 76 } |
| 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); |
104 } | 82 } |
105 | 83 |
106 void SurfaceFactory::RequestCopyOfSurface( | 84 void SurfaceFactory::RequestCopyOfSurface( |
107 const LocalFrameId& local_frame_id, | |
108 std::unique_ptr<CopyOutputRequest> copy_request) { | 85 std::unique_ptr<CopyOutputRequest> copy_request) { |
109 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); | 86 if (!current_surface_) { |
110 if (it == surface_map_.end()) { | |
111 copy_request->SendEmptyResult(); | 87 copy_request->SendEmptyResult(); |
112 return; | 88 return; |
113 } | 89 } |
114 DCHECK(it->second->factory().get() == this); | 90 DCHECK(current_surface_->factory().get() == this); |
115 it->second->RequestCopyOfOutput(std::move(copy_request)); | 91 current_surface_->RequestCopyOfOutput(std::move(copy_request)); |
116 manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id)); | 92 manager_->SurfaceModified(current_surface_->surface_id()); |
117 } | 93 } |
118 | 94 |
119 void SurfaceFactory::ClearSurface(const LocalFrameId& local_frame_id) { | 95 void SurfaceFactory::ClearSurface() { |
120 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); | 96 if (!current_surface_) |
121 DCHECK(it != surface_map_.end()); | 97 return; |
122 DCHECK(it->second->factory().get() == this); | 98 current_surface_->EvictFrame(); |
123 it->second->EvictFrame(); | 99 manager_->SurfaceModified(current_surface_->surface_id()); |
124 manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id)); | |
125 } | 100 } |
126 | 101 |
127 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id, | 102 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id, |
128 const gfx::Rect& damage_rect) { | 103 const gfx::Rect& damage_rect) { |
129 client_->WillDrawSurface(id, damage_rect); | 104 client_->WillDrawSurface(id, damage_rect); |
130 } | 105 } |
131 | 106 |
132 void SurfaceFactory::ReceiveFromChild( | 107 void SurfaceFactory::ReceiveFromChild( |
133 const TransferableResourceArray& resources) { | 108 const TransferableResourceArray& resources) { |
134 holder_.ReceiveFromChild(resources); | 109 holder_.ReceiveFromChild(resources); |
135 } | 110 } |
136 | 111 |
137 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { | 112 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { |
138 holder_.RefResources(resources); | 113 holder_.RefResources(resources); |
139 } | 114 } |
140 | 115 |
141 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { | 116 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { |
142 holder_.UnrefResources(resources); | 117 holder_.UnrefResources(resources); |
143 } | 118 } |
144 | 119 |
| 120 std::unique_ptr<Surface> SurfaceFactory::Create( |
| 121 const LocalFrameId& local_frame_id) { |
| 122 auto surface = base::MakeUnique<Surface>( |
| 123 SurfaceId(frame_sink_id_, local_frame_id), weak_factory_.GetWeakPtr()); |
| 124 manager_->RegisterSurface(surface.get()); |
| 125 return surface; |
| 126 } |
| 127 |
| 128 void SurfaceFactory::Destroy(std::unique_ptr<Surface> surface) { |
| 129 if (manager_) |
| 130 manager_->Destroy(std::move(surface)); |
| 131 } |
| 132 |
145 } // namespace cc | 133 } // namespace cc |
OLD | NEW |