| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "services/ui/ws/gpu_compositor_frame_sink.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "cc/output/compositor_frame.h" | |
| 12 #include "cc/output/output_surface.h" | |
| 13 #include "cc/output/texture_mailbox_deleter.h" | |
| 14 #include "cc/quads/shared_quad_state.h" | |
| 15 #include "cc/quads/surface_draw_quad.h" | |
| 16 #include "cc/scheduler/begin_frame_source.h" | |
| 17 #include "cc/surfaces/display_scheduler.h" | |
| 18 #include "services/ui/surfaces/direct_output_surface.h" | |
| 19 #include "services/ui/surfaces/display_compositor.h" | |
| 20 | |
| 21 #if defined(USE_OZONE) | |
| 22 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 23 #include "services/ui/surfaces/direct_output_surface_ozone.h" | |
| 24 #endif | |
| 25 | |
| 26 namespace ui { | |
| 27 namespace ws { | |
| 28 | |
| 29 GpuCompositorFrameSink::GpuCompositorFrameSink( | |
| 30 scoped_refptr<DisplayCompositor> display_compositor, | |
| 31 const cc::FrameSinkId& frame_sink_id, | |
| 32 gfx::AcceleratedWidget widget, | |
| 33 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 34 scoped_refptr<SurfacesContextProvider> context_provider, | |
| 35 cc::mojom::MojoCompositorFrameSinkRequest request, | |
| 36 cc::mojom::MojoCompositorFrameSinkPrivateRequest private_request, | |
| 37 cc::mojom::MojoCompositorFrameSinkClientPtr client) | |
| 38 : frame_sink_id_(frame_sink_id), | |
| 39 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 40 display_compositor_(display_compositor), | |
| 41 surface_factory_(frame_sink_id_, display_compositor_->manager(), this), | |
| 42 client_(std::move(client)), | |
| 43 binding_(this, std::move(request)), | |
| 44 private_binding_(this, std::move(private_request)) { | |
| 45 display_compositor_->manager()->RegisterFrameSinkId(frame_sink_id_); | |
| 46 display_compositor_->manager()->RegisterSurfaceFactoryClient(frame_sink_id_, | |
| 47 this); | |
| 48 | |
| 49 if (widget != gfx::kNullAcceleratedWidget) | |
| 50 InitDisplay(widget, gpu_memory_buffer_manager, std::move(context_provider)); | |
| 51 } | |
| 52 | |
| 53 GpuCompositorFrameSink::~GpuCompositorFrameSink() { | |
| 54 // SurfaceFactory's destructor will attempt to return resources which will | |
| 55 // call back into here and access |client_| so we should destroy | |
| 56 // |surface_factory_|'s resources early on. | |
| 57 surface_factory_.DestroyAll(); | |
| 58 display_compositor_->manager()->UnregisterSurfaceFactoryClient( | |
| 59 frame_sink_id_); | |
| 60 display_compositor_->manager()->InvalidateFrameSinkId(frame_sink_id_); | |
| 61 } | |
| 62 | |
| 63 void GpuCompositorFrameSink::SetNeedsBeginFrame(bool needs_begin_frame) { | |
| 64 needs_begin_frame_ = needs_begin_frame; | |
| 65 UpdateNeedsBeginFramesInternal(); | |
| 66 } | |
| 67 | |
| 68 void GpuCompositorFrameSink::SubmitCompositorFrame(cc::CompositorFrame frame) { | |
| 69 gfx::Size frame_size = frame.render_pass_list[0]->output_rect.size(); | |
| 70 // If the size of the CompostiorFrame has changed then destroy the existing | |
| 71 // Surface and create a new one of the appropriate size. | |
| 72 if (!local_frame_id_.is_valid() || frame_size != last_submitted_frame_size_) { | |
| 73 if (local_frame_id_.is_valid()) | |
| 74 surface_factory_.Destroy(local_frame_id_); | |
| 75 local_frame_id_ = surface_id_allocator_.GenerateId(); | |
| 76 surface_factory_.Create(local_frame_id_); | |
| 77 if (display_) | |
| 78 display_->Resize(frame_size); | |
| 79 } | |
| 80 ++ack_pending_count_; | |
| 81 surface_factory_.SubmitCompositorFrame( | |
| 82 local_frame_id_, std::move(frame), | |
| 83 base::Bind(&GpuCompositorFrameSink::DidReceiveCompositorFrameAck, | |
| 84 base::Unretained(this))); | |
| 85 if (display_) { | |
| 86 display_->SetLocalFrameId(local_frame_id_, | |
| 87 frame.metadata.device_scale_factor); | |
| 88 } | |
| 89 last_submitted_frame_size_ = frame_size; | |
| 90 } | |
| 91 | |
| 92 void GpuCompositorFrameSink::DidReceiveCompositorFrameAck() { | |
| 93 if (!client_) | |
| 94 return; | |
| 95 client_->DidReceiveCompositorFrameAck(); | |
| 96 DCHECK_GT(ack_pending_count_, 0); | |
| 97 if (!surface_returned_resources_.empty()) { | |
| 98 client_->ReclaimResources(surface_returned_resources_); | |
| 99 surface_returned_resources_.clear(); | |
| 100 } | |
| 101 ack_pending_count_--; | |
| 102 } | |
| 103 | |
| 104 void GpuCompositorFrameSink::AddChildFrameSink( | |
| 105 const cc::FrameSinkId& child_frame_sink_id) { | |
| 106 cc::SurfaceManager* surface_manager = display_compositor_->manager(); | |
| 107 surface_manager->RegisterFrameSinkHierarchy(frame_sink_id_, | |
| 108 child_frame_sink_id); | |
| 109 } | |
| 110 | |
| 111 void GpuCompositorFrameSink::RemoveChildFrameSink( | |
| 112 const cc::FrameSinkId& child_frame_sink_id) { | |
| 113 cc::SurfaceManager* surface_manager = display_compositor_->manager(); | |
| 114 surface_manager->UnregisterFrameSinkHierarchy(frame_sink_id_, | |
| 115 child_frame_sink_id); | |
| 116 } | |
| 117 | |
| 118 void GpuCompositorFrameSink::InitDisplay( | |
| 119 gfx::AcceleratedWidget widget, | |
| 120 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 121 scoped_refptr<SurfacesContextProvider> context_provider) { | |
| 122 // TODO(rjkroege): If there is something better to do than CHECK, add it. | |
| 123 CHECK(context_provider->BindToCurrentThread()); | |
| 124 | |
| 125 std::unique_ptr<cc::SyntheticBeginFrameSource> synthetic_begin_frame_source( | |
| 126 new cc::DelayBasedBeginFrameSource( | |
| 127 base::MakeUnique<cc::DelayBasedTimeSource>(task_runner_.get()))); | |
| 128 | |
| 129 std::unique_ptr<cc::OutputSurface> display_output_surface; | |
| 130 if (context_provider->ContextCapabilities().surfaceless) { | |
| 131 #if defined(USE_OZONE) | |
| 132 display_output_surface = base::MakeUnique<DirectOutputSurfaceOzone>( | |
| 133 context_provider, widget, synthetic_begin_frame_source.get(), | |
| 134 gpu_memory_buffer_manager, GL_TEXTURE_2D, GL_RGB); | |
| 135 #else | |
| 136 NOTREACHED(); | |
| 137 #endif | |
| 138 } else { | |
| 139 display_output_surface = base::MakeUnique<DirectOutputSurface>( | |
| 140 context_provider, synthetic_begin_frame_source.get()); | |
| 141 } | |
| 142 | |
| 143 int max_frames_pending = | |
| 144 display_output_surface->capabilities().max_frames_pending; | |
| 145 DCHECK_GT(max_frames_pending, 0); | |
| 146 | |
| 147 std::unique_ptr<cc::DisplayScheduler> scheduler( | |
| 148 new cc::DisplayScheduler(synthetic_begin_frame_source.get(), | |
| 149 task_runner_.get(), max_frames_pending)); | |
| 150 | |
| 151 display_.reset(new cc::Display( | |
| 152 nullptr /* bitmap_manager */, gpu_memory_buffer_manager, | |
| 153 cc::RendererSettings(), frame_sink_id_, | |
| 154 std::move(synthetic_begin_frame_source), | |
| 155 std::move(display_output_surface), std::move(scheduler), | |
| 156 base::MakeUnique<cc::TextureMailboxDeleter>(task_runner_.get()))); | |
| 157 display_->Initialize(this, display_compositor_->manager()); | |
| 158 display_->SetVisible(true); | |
| 159 } | |
| 160 | |
| 161 void GpuCompositorFrameSink::DisplayOutputSurfaceLost() {} | |
| 162 | |
| 163 void GpuCompositorFrameSink::DisplayWillDrawAndSwap( | |
| 164 bool will_draw_and_swap, | |
| 165 const cc::RenderPassList& render_passes) {} | |
| 166 | |
| 167 void GpuCompositorFrameSink::DisplayDidDrawAndSwap() {} | |
| 168 | |
| 169 void GpuCompositorFrameSink::ReturnResources( | |
| 170 const cc::ReturnedResourceArray& resources) { | |
| 171 if (resources.empty()) | |
| 172 return; | |
| 173 | |
| 174 if (!ack_pending_count_ && client_) { | |
| 175 client_->ReclaimResources(resources); | |
| 176 return; | |
| 177 } | |
| 178 | |
| 179 std::copy(resources.begin(), resources.end(), | |
| 180 std::back_inserter(surface_returned_resources_)); | |
| 181 } | |
| 182 | |
| 183 void GpuCompositorFrameSink::SetBeginFrameSource( | |
| 184 cc::BeginFrameSource* begin_frame_source) { | |
| 185 // TODO(tansell): Implement this. | |
| 186 if (begin_frame_source_ && added_frame_observer_) { | |
| 187 begin_frame_source_->RemoveObserver(this); | |
| 188 added_frame_observer_ = false; | |
| 189 } | |
| 190 begin_frame_source_ = begin_frame_source; | |
| 191 UpdateNeedsBeginFramesInternal(); | |
| 192 } | |
| 193 | |
| 194 void GpuCompositorFrameSink::OnBeginFrame(const cc::BeginFrameArgs& args) { | |
| 195 UpdateNeedsBeginFramesInternal(); | |
| 196 last_begin_frame_args_ = args; | |
| 197 if (client_) | |
| 198 client_->OnBeginFrame(args); | |
| 199 } | |
| 200 | |
| 201 const cc::BeginFrameArgs& GpuCompositorFrameSink::LastUsedBeginFrameArgs() | |
| 202 const { | |
| 203 return last_begin_frame_args_; | |
| 204 } | |
| 205 | |
| 206 void GpuCompositorFrameSink::OnBeginFrameSourcePausedChanged(bool paused) {} | |
| 207 | |
| 208 void GpuCompositorFrameSink::UpdateNeedsBeginFramesInternal() { | |
| 209 if (!begin_frame_source_) | |
| 210 return; | |
| 211 | |
| 212 if (needs_begin_frame_ == added_frame_observer_) | |
| 213 return; | |
| 214 | |
| 215 added_frame_observer_ = needs_begin_frame_; | |
| 216 if (needs_begin_frame_) | |
| 217 begin_frame_source_->AddObserver(this); | |
| 218 else | |
| 219 begin_frame_source_->RemoveObserver(this); | |
| 220 } | |
| 221 | |
| 222 } // namespace ws | |
| 223 } // namespace ui | |
| OLD | NEW |