[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 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 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 5 | #include "cc/surfaces/direct_compositor_frame_sink.h" |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 6 | |
jamesr | 9b8fda3 | 2015-03-16 19:11:05 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 8 | #include "cc/output/compositor_frame.h" |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 9 | #include "cc/output/compositor_frame_sink_client.h" |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 10 | #include "cc/surfaces/display.h" |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 11 | #include "cc/surfaces/frame_sink_id.h" |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 12 | #include "cc/surfaces/surface.h" |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 13 | #include "cc/surfaces/surface_id_allocator.h" |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 14 | #include "cc/surfaces/surface_manager.h" |
| 15 | |
jbauman | f400ce53 | 2015-02-03 04:54:59 | [diff] [blame] | 16 | namespace cc { |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 17 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 18 | DirectCompositorFrameSink::DirectCompositorFrameSink( |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 19 | const FrameSinkId& frame_sink_id, |
jbauman | f400ce53 | 2015-02-03 04:54:59 | [diff] [blame] | 20 | SurfaceManager* surface_manager, |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 21 | Display* display, |
vmpstr | 4065174a | 2016-03-16 20:44:02 | [diff] [blame] | 22 | scoped_refptr<ContextProvider> context_provider, |
| 23 | scoped_refptr<ContextProvider> worker_context_provider) |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 24 | : CompositorFrameSink(std::move(context_provider), |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 25 | std::move(worker_context_provider)), |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 26 | frame_sink_id_(frame_sink_id), |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 27 | surface_manager_(surface_manager), |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 28 | display_(display), |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 29 | factory_(frame_sink_id, surface_manager, this) { |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 30 | DCHECK(thread_checker_.CalledOnValidThread()); |
jbauman | 9bfb1a5 | 2015-01-09 08:16:03 | [diff] [blame] | 31 | capabilities_.can_force_reclaim_resources = true; |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 32 | // Display and DirectCompositorFrameSink share a GL context, so sync |
jbauman | 65182a3 | 2015-04-11 00:32:12 | [diff] [blame] | 33 | // points aren't needed when passing resources between them. |
| 34 | capabilities_.delegated_sync_points_required = false; |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 35 | factory_.set_needs_sync_points(false); |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 36 | } |
| 37 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 38 | DirectCompositorFrameSink::DirectCompositorFrameSink( |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 39 | const FrameSinkId& frame_sink_id, |
dyen | b52ecd96 | 2016-04-20 23:23:44 | [diff] [blame] | 40 | SurfaceManager* surface_manager, |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 41 | Display* display, |
dyen | b52ecd96 | 2016-04-20 23:23:44 | [diff] [blame] | 42 | scoped_refptr<VulkanContextProvider> vulkan_context_provider) |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 43 | : CompositorFrameSink(std::move(vulkan_context_provider)), |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 44 | frame_sink_id_(frame_sink_id), |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 45 | surface_manager_(surface_manager), |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 46 | display_(display), |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 47 | factory_(frame_sink_id_, surface_manager, this) { |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 48 | DCHECK(thread_checker_.CalledOnValidThread()); |
dyen | b52ecd96 | 2016-04-20 23:23:44 | [diff] [blame] | 49 | capabilities_.can_force_reclaim_resources = true; |
| 50 | } |
| 51 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 52 | DirectCompositorFrameSink::~DirectCompositorFrameSink() { |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 53 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | b95a2ee | 2014-06-01 12:25:39 | [diff] [blame] | 54 | } |
| 55 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 56 | bool DirectCompositorFrameSink::BindToClient( |
| 57 | CompositorFrameSinkClient* client) { |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 58 | DCHECK(thread_checker_.CalledOnValidThread()); |
danakj | 4f26adf2 | 2016-06-16 19:38:21 | [diff] [blame] | 59 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 60 | if (!CompositorFrameSink::BindToClient(client)) |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 61 | return false; |
enne | 4e3c9d5 | 2016-03-09 00:25:12 | [diff] [blame] | 62 | |
danakj | 8fa1509 | 2016-10-11 00:48:03 | [diff] [blame] | 63 | surface_manager_->RegisterSurfaceFactoryClient(frame_sink_id_, this); |
| 64 | |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 65 | // We want the Display's output surface to hear about lost context, and since |
| 66 | // this shares a context with it, we should not be listening for lost context |
| 67 | // callbacks on the context here. |
danakj | 8fa1509 | 2016-10-11 00:48:03 | [diff] [blame] | 68 | if (auto* cp = context_provider()) |
| 69 | cp->SetLostContextCallback(base::Closure()); |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 70 | |
jbauman | a8c5cc9 | 2014-10-03 20:51:23 | [diff] [blame] | 71 | // Avoid initializing GL context here, as this should be sharing the |
| 72 | // Display's context. |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 73 | display_->Initialize(this, surface_manager_, frame_sink_id_); |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 74 | return true; |
jbauman | a8c5cc9 | 2014-10-03 20:51:23 | [diff] [blame] | 75 | } |
| 76 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 77 | void DirectCompositorFrameSink::DetachFromClient() { |
enne | 4e3c9d5 | 2016-03-09 00:25:12 | [diff] [blame] | 78 | // Unregister the SurfaceFactoryClient here instead of the dtor so that only |
| 79 | // one client is alive for this namespace at any given time. |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 80 | surface_manager_->UnregisterSurfaceFactoryClient(frame_sink_id_); |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 81 | if (!delegated_local_frame_id_.is_null()) |
| 82 | factory_.Destroy(delegated_local_frame_id_); |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 83 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 84 | CompositorFrameSink::DetachFromClient(); |
enne | 4e3c9d5 | 2016-03-09 00:25:12 | [diff] [blame] | 85 | } |
| 86 | |
danakj | 9d12442 | 2016-10-14 03:15:08 | [diff] [blame^] | 87 | void DirectCompositorFrameSink::SubmitCompositorFrame(CompositorFrame frame) { |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 88 | gfx::Size frame_size = |
| 89 | frame.delegated_frame_data->render_pass_list.back()->output_rect.size(); |
| 90 | if (frame_size.IsEmpty() || frame_size != last_swap_frame_size_) { |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 91 | if (!delegated_local_frame_id_.is_null()) { |
| 92 | factory_.Destroy(delegated_local_frame_id_); |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 93 | } |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 94 | delegated_local_frame_id_ = surface_id_allocator_.GenerateId(); |
| 95 | factory_.Create(delegated_local_frame_id_); |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 96 | last_swap_frame_size_ = frame_size; |
| 97 | } |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 98 | display_->SetSurfaceId(SurfaceId(frame_sink_id_, delegated_local_frame_id_), |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 99 | frame.metadata.device_scale_factor); |
| 100 | |
| 101 | factory_.SubmitCompositorFrame( |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 102 | delegated_local_frame_id_, std::move(frame), |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 103 | base::Bind(&DirectCompositorFrameSink::DidDrawCallback, |
| 104 | base::Unretained(this))); |
danakj | a85bd24 | 2016-06-22 22:25:49 | [diff] [blame] | 105 | } |
| 106 | |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 107 | void DirectCompositorFrameSink::ForceReclaimResources() { |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 108 | if (!delegated_local_frame_id_.is_null()) { |
| 109 | factory_.SubmitCompositorFrame(delegated_local_frame_id_, CompositorFrame(), |
danakj | e41d978a | 2016-09-19 21:09:28 | [diff] [blame] | 110 | SurfaceFactory::DrawCallback()); |
| 111 | } |
danakj | a85bd24 | 2016-06-22 22:25:49 | [diff] [blame] | 112 | } |
| 113 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 114 | void DirectCompositorFrameSink::ReturnResources( |
jbauman | f400ce53 | 2015-02-03 04:54:59 | [diff] [blame] | 115 | const ReturnedResourceArray& resources) { |
[email protected] | 72728b1 | 2014-07-18 03:39:17 | [diff] [blame] | 116 | if (client_) |
fsamuel | b62b7822 | 2016-07-15 01:14:14 | [diff] [blame] | 117 | client_->ReclaimResources(resources); |
[email protected] | 387b59d | 2014-06-27 01:17:34 | [diff] [blame] | 118 | } |
| 119 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 120 | void DirectCompositorFrameSink::SetBeginFrameSource( |
brianderson | 877996b0 | 2015-10-20 20:35:31 | [diff] [blame] | 121 | BeginFrameSource* begin_frame_source) { |
enne | 19c10858 | 2016-04-14 03:35:32 | [diff] [blame] | 122 | DCHECK(client_); |
| 123 | client_->SetBeginFrameSource(begin_frame_source); |
brianderson | 877996b0 | 2015-10-20 20:35:31 | [diff] [blame] | 124 | } |
| 125 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 126 | void DirectCompositorFrameSink::DisplayOutputSurfaceLost() { |
| 127 | is_lost_ = true; |
| 128 | client_->DidLoseCompositorFrameSink(); |
danakj | 324faea | 2016-06-11 01:39:31 | [diff] [blame] | 129 | } |
| 130 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 131 | void DirectCompositorFrameSink::DisplayWillDrawAndSwap( |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 132 | bool will_draw_and_swap, |
| 133 | const RenderPassList& render_passes) { |
| 134 | // This notification is not relevant to our client outside of tests. |
| 135 | } |
| 136 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 137 | void DirectCompositorFrameSink::DisplayDidDrawAndSwap() { |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 138 | // This notification is not relevant to our client outside of tests. We |
| 139 | // unblock the client from DidDrawCallback() when the surface is going to |
| 140 | // be drawn. |
| 141 | } |
| 142 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 143 | void DirectCompositorFrameSink::DidDrawCallback() { |
danakj | 9d12442 | 2016-10-14 03:15:08 | [diff] [blame^] | 144 | client_->DidReceiveCompositorFrameAck(); |
jbauman | 878e953 | 2014-08-23 22:10:23 | [diff] [blame] | 145 | } |
| 146 | |
jbauman | f400ce53 | 2015-02-03 04:54:59 | [diff] [blame] | 147 | } // namespace cc |