Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: cc/surfaces/surface_factory.cc

Issue 2485473003: Remove SurfaceFactory::Create and SurfaceFactory::Destroy (Closed)
Patch Set: doc Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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";
danakj 2016/11/15 00:43:41 A++ polite code
Saman Sami 2016/11/15 21:12:19 lol
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 =
59 (!current_surface_ ||
60 local_frame_id != current_surface_->surface_id().local_frame_id());
61 if (create_new_surface)
62 surface = Create(local_frame_id);
63 else
64 surface = std::move(current_surface_);
88 // Tell the SurfaceManager if this is the first frame submitted with this 65 // Tell the SurfaceManager if this is the first frame submitted with this
89 // LocalFrameId. 66 // LocalFrameId.
90 if (!it->second->HasFrame()) { 67 if (!surface->HasFrame()) {
Fady Samuel 2016/11/15 12:46:51 nit: This check seems redundant at this point. If
Saman Sami 2016/11/15 21:12:19 Done.
91 float device_scale_factor = frame.metadata.device_scale_factor; 68 float device_scale_factor = frame.metadata.device_scale_factor;
92 gfx::Size frame_size; 69 gfx::Size frame_size;
93 // CompositorFrames may not be populated with a RenderPass in unit tests. 70 // CompositorFrames may not be populated with a RenderPass in unit tests.
94 if (!frame.render_pass_list.empty()) 71 if (!frame.render_pass_list.empty())
95 frame_size = frame.render_pass_list[0]->output_rect.size(); 72 frame_size = frame.render_pass_list[0]->output_rect.size();
96 manager_->SurfaceCreated(it->second->surface_id(), frame_size, 73 manager_->SurfaceCreated(surface->surface_id(), frame_size,
97 device_scale_factor); 74 device_scale_factor);
98 } 75 }
99 it->second->QueueFrame(std::move(frame), callback); 76 surface->QueueFrame(std::move(frame), callback);
100 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) { 77 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id))) {
101 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); 78 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD);
102 it->second->RunDrawCallbacks(); 79 surface->RunDrawCallbacks();
103 } 80 }
81 if (current_surface_ && create_new_surface) {
82 surface->SetPreviousFrameSurface(current_surface_.get());
83 Destroy(std::move(current_surface_));
84 }
85 current_surface_ = std::move(surface);
104 } 86 }
105 87
106 void SurfaceFactory::RequestCopyOfSurface( 88 void SurfaceFactory::RequestCopyOfSurface(
107 const LocalFrameId& local_frame_id,
108 std::unique_ptr<CopyOutputRequest> copy_request) { 89 std::unique_ptr<CopyOutputRequest> copy_request) {
109 OwningSurfaceMap::iterator it = surface_map_.find(local_frame_id); 90 if (!current_surface_) {
110 if (it == surface_map_.end()) {
111 copy_request->SendEmptyResult(); 91 copy_request->SendEmptyResult();
112 return; 92 return;
113 } 93 }
114 DCHECK(it->second->factory().get() == this); 94 DCHECK(current_surface_->factory().get() == this);
115 it->second->RequestCopyOfOutput(std::move(copy_request)); 95 current_surface_->RequestCopyOfOutput(std::move(copy_request));
116 manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_frame_id)); 96 manager_->SurfaceModified(current_surface_->surface_id());
117 } 97 }
118 98
119 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id, 99 void SurfaceFactory::WillDrawSurface(const LocalFrameId& id,
120 const gfx::Rect& damage_rect) { 100 const gfx::Rect& damage_rect) {
121 client_->WillDrawSurface(id, damage_rect); 101 client_->WillDrawSurface(id, damage_rect);
122 } 102 }
123 103
124 void SurfaceFactory::ReceiveFromChild( 104 void SurfaceFactory::ReceiveFromChild(
125 const TransferableResourceArray& resources) { 105 const TransferableResourceArray& resources) {
126 holder_.ReceiveFromChild(resources); 106 holder_.ReceiveFromChild(resources);
127 } 107 }
128 108
129 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { 109 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) {
130 holder_.RefResources(resources); 110 holder_.RefResources(resources);
131 } 111 }
132 112
133 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { 113 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) {
134 holder_.UnrefResources(resources); 114 holder_.UnrefResources(resources);
135 } 115 }
136 116
117 std::unique_ptr<Surface> SurfaceFactory::Create(
118 const LocalFrameId& local_frame_id) {
119 auto surface = base::MakeUnique<Surface>(
120 SurfaceId(frame_sink_id_, local_frame_id), weak_factory_.GetWeakPtr());
121 manager_->RegisterSurface(surface.get());
122 return surface;
123 }
124
125 void SurfaceFactory::Destroy(std::unique_ptr<Surface> surface) {
126 if (manager_)
127 manager_->Destroy(std::move(surface));
128 }
129
137 } // namespace cc 130 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698