| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/surfaces/surface_factory.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/trace_event/trace_event.h" | |
| 11 #include "cc/output/compositor_frame.h" | |
| 12 #include "cc/output/copy_output_request.h" | |
| 13 #include "cc/surfaces/surface.h" | |
| 14 #include "cc/surfaces/surface_factory_client.h" | |
| 15 #include "cc/surfaces/surface_info.h" | |
| 16 #include "cc/surfaces/surface_manager.h" | |
| 17 #include "ui/gfx/geometry/size.h" | |
| 18 | |
| 19 namespace cc { | |
| 20 SurfaceFactory::SurfaceFactory(const FrameSinkId& frame_sink_id, | |
| 21 SurfaceManager* manager, | |
| 22 SurfaceFactoryClient* client) | |
| 23 : frame_sink_id_(frame_sink_id), | |
| 24 manager_(manager), | |
| 25 client_(client), | |
| 26 holder_(client), | |
| 27 needs_sync_points_(true), | |
| 28 weak_factory_(this) {} | |
| 29 | |
| 30 SurfaceFactory::~SurfaceFactory() { | |
| 31 // This is to prevent troubles when a factory that resides in a client is | |
| 32 // being destroyed. In such cases, the factory might attempt to return | |
| 33 // resources to the client while it's in the middle of destruction and this | |
| 34 // could cause a crash or some unexpected behaviour. | |
| 35 DCHECK(!current_surface_) << "Please call EvictSurface before destruction"; | |
| 36 } | |
| 37 | |
| 38 void SurfaceFactory::EvictSurface() { | |
| 39 if (!current_surface_) | |
| 40 return; | |
| 41 Destroy(std::move(current_surface_)); | |
| 42 } | |
| 43 | |
| 44 void SurfaceFactory::SubmitCompositorFrame( | |
| 45 const LocalSurfaceId& local_surface_id, | |
| 46 CompositorFrame frame, | |
| 47 const DrawCallback& callback) { | |
| 48 TRACE_EVENT0("cc", "SurfaceFactory::SubmitCompositorFrame"); | |
| 49 DCHECK(local_surface_id.is_valid()); | |
| 50 std::unique_ptr<Surface> surface; | |
| 51 bool create_new_surface = | |
| 52 (!current_surface_ || | |
| 53 local_surface_id != current_surface_->surface_id().local_surface_id()); | |
| 54 if (!create_new_surface) { | |
| 55 surface = std::move(current_surface_); | |
| 56 } else { | |
| 57 surface = Create(local_surface_id); | |
| 58 } | |
| 59 surface->QueueFrame(std::move(frame), callback); | |
| 60 | |
| 61 if (!manager_->SurfaceModified(SurfaceId(frame_sink_id_, local_surface_id))) { | |
| 62 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD); | |
| 63 surface->RunDrawCallbacks(); | |
| 64 } | |
| 65 if (current_surface_ && create_new_surface) { | |
| 66 surface->SetPreviousFrameSurface(current_surface_.get()); | |
| 67 Destroy(std::move(current_surface_)); | |
| 68 } | |
| 69 current_surface_ = std::move(surface); | |
| 70 } | |
| 71 | |
| 72 void SurfaceFactory::RequestCopyOfSurface( | |
| 73 std::unique_ptr<CopyOutputRequest> copy_request) { | |
| 74 if (!current_surface_) { | |
| 75 copy_request->SendEmptyResult(); | |
| 76 return; | |
| 77 } | |
| 78 DCHECK(current_surface_->factory().get() == this); | |
| 79 current_surface_->RequestCopyOfOutput(std::move(copy_request)); | |
| 80 manager_->SurfaceModified(current_surface_->surface_id()); | |
| 81 } | |
| 82 | |
| 83 void SurfaceFactory::ClearSurface() { | |
| 84 if (!current_surface_) | |
| 85 return; | |
| 86 current_surface_->EvictFrame(); | |
| 87 manager_->SurfaceModified(current_surface_->surface_id()); | |
| 88 } | |
| 89 | |
| 90 void SurfaceFactory::WillDrawSurface(const LocalSurfaceId& id, | |
| 91 const gfx::Rect& damage_rect) { | |
| 92 client_->WillDrawSurface(id, damage_rect); | |
| 93 } | |
| 94 | |
| 95 void SurfaceFactory::ReceiveFromChild( | |
| 96 const TransferableResourceArray& resources) { | |
| 97 holder_.ReceiveFromChild(resources); | |
| 98 } | |
| 99 | |
| 100 void SurfaceFactory::RefResources(const TransferableResourceArray& resources) { | |
| 101 holder_.RefResources(resources); | |
| 102 } | |
| 103 | |
| 104 void SurfaceFactory::UnrefResources(const ReturnedResourceArray& resources) { | |
| 105 holder_.UnrefResources(resources); | |
| 106 } | |
| 107 | |
| 108 void SurfaceFactory::OnReferencedSurfacesChanged( | |
| 109 Surface* surface, | |
| 110 const std::vector<SurfaceId>* active_referenced_surfaces, | |
| 111 const std::vector<SurfaceId>* pending_referenced_surfaces) { | |
| 112 client_->ReferencedSurfacesChanged(surface->surface_id().local_surface_id(), | |
| 113 active_referenced_surfaces, | |
| 114 pending_referenced_surfaces); | |
| 115 } | |
| 116 | |
| 117 void SurfaceFactory::OnSurfaceActivated(Surface* surface) { | |
| 118 DCHECK(surface->HasActiveFrame()); | |
| 119 if (seen_first_frame_activation_) | |
| 120 return; | |
| 121 | |
| 122 seen_first_frame_activation_ = true; | |
| 123 | |
| 124 const CompositorFrame& frame = surface->GetActiveFrame(); | |
| 125 // CompositorFrames might not be populated with a RenderPass in unit tests. | |
| 126 gfx::Size frame_size; | |
| 127 if (!frame.render_pass_list.empty()) | |
| 128 frame_size = frame.render_pass_list.back()->output_rect.size(); | |
| 129 | |
| 130 // SurfaceCreated only applies for the first Surface activation. Thus, | |
| 131 // SurfaceFactory stops observing new activations after the first one. | |
| 132 manager_->SurfaceCreated(SurfaceInfo( | |
| 133 surface->surface_id(), frame.metadata.device_scale_factor, frame_size)); | |
| 134 } | |
| 135 | |
| 136 void SurfaceFactory::OnSurfaceDependenciesChanged( | |
| 137 Surface* surface, | |
| 138 const SurfaceDependencies& added_dependencies, | |
| 139 const SurfaceDependencies& removed_dependencies) {} | |
| 140 | |
| 141 void SurfaceFactory::OnSurfaceDiscarded(Surface* surface) {} | |
| 142 | |
| 143 std::unique_ptr<Surface> SurfaceFactory::Create( | |
| 144 const LocalSurfaceId& local_surface_id) { | |
| 145 seen_first_frame_activation_ = false; | |
| 146 std::unique_ptr<Surface> surface = | |
| 147 manager_->CreateSurface(weak_factory_.GetWeakPtr(), local_surface_id); | |
| 148 surface->AddObserver(this); | |
| 149 return surface; | |
| 150 } | |
| 151 | |
| 152 void SurfaceFactory::Destroy(std::unique_ptr<Surface> surface) { | |
| 153 surface->RemoveObserver(this); | |
| 154 manager_->DestroySurface(std::move(surface)); | |
| 155 } | |
| 156 | |
| 157 } // namespace cc | |
| OLD | NEW |