boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 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 "content/browser/android/synchronous_compositor_host.h" |
| 6 | |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 9 | #include "base/command_line.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 10 | #include "base/containers/hash_tables.h" |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 12 | #include "base/memory/shared_memory.h" |
| 13 | #include "base/trace_event/trace_event_argument.h" |
boliu | 2255797 | 2016-10-25 16:08:29 | [diff] [blame] | 14 | #include "content/browser/android/synchronous_compositor_browser_filter.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 15 | #include "content/browser/renderer_host/render_widget_host_view_android.h" |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 16 | #include "content/browser/web_contents/web_contents_android.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 17 | #include "content/browser/web_contents/web_contents_impl.h" |
| 18 | #include "content/common/android/sync_compositor_messages.h" |
jbroman | 801dd27 | 2016-04-22 18:54:02 | [diff] [blame] | 19 | #include "content/common/android/sync_compositor_statics.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 20 | #include "content/public/browser/android/synchronous_compositor_client.h" |
hush | 92bd35e7 | 2015-11-13 22:57:37 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 22 | #include "content/public/browser/render_view_host.h" |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 23 | #include "content/public/common/content_switches.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 24 | #include "ipc/ipc_sender.h" |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 25 | #include "third_party/skia/include/core/SkBitmap.h" |
| 26 | #include "third_party/skia/include/core/SkCanvas.h" |
| 27 | #include "third_party/skia/include/core/SkImageInfo.h" |
| 28 | #include "third_party/skia/include/core/SkRect.h" |
chongz | a8ba91fc | 2016-08-16 21:39:17 | [diff] [blame] | 29 | #include "ui/events/blink/did_overscroll_params.h" |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 30 | #include "ui/gfx/skia_util.h" |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 31 | |
| 32 | namespace content { |
| 33 | |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 34 | // static |
| 35 | void SynchronousCompositor::SetClientForWebContents( |
| 36 | WebContents* contents, |
| 37 | SynchronousCompositorClient* client) { |
| 38 | DCHECK(contents); |
| 39 | DCHECK(client); |
| 40 | WebContentsAndroid* web_contents_android = |
| 41 | static_cast<WebContentsImpl*>(contents)->GetWebContentsAndroid(); |
| 42 | DCHECK(!web_contents_android->synchronous_compositor_client()); |
| 43 | web_contents_android->set_synchronous_compositor_client(client); |
| 44 | } |
| 45 | |
| 46 | // static |
| 47 | std::unique_ptr<SynchronousCompositorHost> SynchronousCompositorHost::Create( |
| 48 | RenderWidgetHostViewAndroid* rwhva, |
| 49 | WebContents* web_contents) { |
| 50 | DCHECK(web_contents); |
| 51 | WebContentsAndroid* web_contents_android = |
| 52 | static_cast<WebContentsImpl*>(web_contents)->GetWebContentsAndroid(); |
| 53 | if (!web_contents_android->synchronous_compositor_client()) |
| 54 | return nullptr; // Not using sync compositing. |
| 55 | |
| 56 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 57 | bool use_in_proc_software_draw = |
| 58 | command_line->HasSwitch(switches::kSingleProcess); |
| 59 | return base::WrapUnique(new SynchronousCompositorHost( |
boliu | b2768db | 2016-06-03 23:35:16 | [diff] [blame] | 60 | rwhva, web_contents_android->synchronous_compositor_client(), |
boliu | c99f26c1 | 2016-04-21 21:58:59 | [diff] [blame] | 61 | use_in_proc_software_draw)); |
| 62 | } |
| 63 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 64 | SynchronousCompositorHost::SynchronousCompositorHost( |
| 65 | RenderWidgetHostViewAndroid* rwhva, |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 66 | SynchronousCompositorClient* client, |
| 67 | bool use_in_proc_software_draw) |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 68 | : rwhva_(rwhva), |
| 69 | client_(client), |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 70 | ui_task_runner_(BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)), |
hush | 284add6 | 2016-06-17 20:47:33 | [diff] [blame] | 71 | process_id_(rwhva_->GetRenderWidgetHost()->GetProcess()->GetID()), |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 72 | routing_id_(rwhva_->GetRenderWidgetHost()->GetRoutingID()), |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 73 | sender_(rwhva_->GetRenderWidgetHost()), |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 74 | use_in_process_zero_copy_software_draw_(use_in_proc_software_draw), |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 75 | bytes_limit_(0u), |
| 76 | renderer_param_version_(0u), |
| 77 | need_animate_scroll_(false), |
boliu | 17bc31f | 2016-02-23 18:46:28 | [diff] [blame] | 78 | need_invalidate_count_(0u), |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 79 | did_activate_pending_tree_count_(0u) { |
hush | 284add6 | 2016-06-17 20:47:33 | [diff] [blame] | 80 | client_->DidInitializeCompositor(this, process_id_, routing_id_); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | SynchronousCompositorHost::~SynchronousCompositorHost() { |
hush | 284add6 | 2016-06-17 20:47:33 | [diff] [blame] | 84 | client_->DidDestroyCompositor(this, process_id_, routing_id_); |
boliu | 524d4c0b | 2016-10-28 04:04:05 | [diff] [blame^] | 85 | if (registered_with_filter_) { |
| 86 | if (SynchronousCompositorBrowserFilter* filter = GetFilter()) |
| 87 | filter->UnregisterHost(this); |
| 88 | } |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool SynchronousCompositorHost::OnMessageReceived(const IPC::Message& message) { |
| 92 | bool handled = true; |
| 93 | IPC_BEGIN_MESSAGE_MAP(SynchronousCompositorHost, message) |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 94 | IPC_MESSAGE_HANDLER(SyncCompositorHostMsg_CompositorFrameSinkCreated, |
| 95 | CompositorFrameSinkCreated) |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 96 | IPC_MESSAGE_HANDLER(SyncCompositorHostMsg_UpdateState, ProcessCommonParams) |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 97 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 98 | IPC_END_MESSAGE_MAP() |
| 99 | return handled; |
| 100 | } |
| 101 | |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 102 | scoped_refptr<SynchronousCompositor::FrameFuture> |
| 103 | SynchronousCompositorHost::DemandDrawHwAsync( |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 104 | const gfx::Size& viewport_size, |
| 105 | const gfx::Rect& viewport_rect_for_tile_priority, |
| 106 | const gfx::Transform& transform_for_tile_priority) { |
| 107 | SyncCompositorDemandDrawHwParams params(viewport_size, |
| 108 | viewport_rect_for_tile_priority, |
| 109 | transform_for_tile_priority); |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 110 | scoped_refptr<FrameFuture> frame_future = new FrameFuture(); |
boliu | 2255797 | 2016-10-25 16:08:29 | [diff] [blame] | 111 | if (SynchronousCompositorBrowserFilter* filter = GetFilter()) { |
boliu | 524d4c0b | 2016-10-28 04:04:05 | [diff] [blame^] | 112 | if (!registered_with_filter_) { |
| 113 | filter->RegisterHost(this); |
| 114 | registered_with_filter_ = true; |
| 115 | } |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 116 | filter->SetFrameFuture(routing_id_, frame_future); |
| 117 | sender_->Send(new SyncCompositorMsg_DemandDrawHwAsync(routing_id_, params)); |
| 118 | } else { |
boliu | f26c9e0f | 2016-10-27 16:29:08 | [diff] [blame] | 119 | frame_future->SetFrame(nullptr); |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 120 | } |
| 121 | return frame_future; |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 122 | } |
| 123 | |
boliu | f4e57485 | 2016-03-22 19:33:17 | [diff] [blame] | 124 | SynchronousCompositor::Frame SynchronousCompositorHost::DemandDrawHw( |
boliu | 31c233ed | 2016-07-29 05:38:59 | [diff] [blame] | 125 | const gfx::Size& viewport_size, |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 126 | const gfx::Rect& viewport_rect_for_tile_priority, |
| 127 | const gfx::Transform& transform_for_tile_priority) { |
boliu | 31c233ed | 2016-07-29 05:38:59 | [diff] [blame] | 128 | SyncCompositorDemandDrawHwParams params(viewport_size, |
| 129 | viewport_rect_for_tile_priority, |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 130 | transform_for_tile_priority); |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 131 | uint32_t compositor_frame_sink_id; |
samans | 9641873 | 2016-10-27 21:24:45 | [diff] [blame] | 132 | base::Optional<cc::CompositorFrame> compositor_frame; |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 133 | SyncCompositorCommonRendererParams common_renderer_params; |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 134 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 135 | if (!sender_->Send(new SyncCompositorMsg_DemandDrawHw( |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 136 | routing_id_, params, &common_renderer_params, |
| 137 | &compositor_frame_sink_id, &compositor_frame))) { |
boliu | f4e57485 | 2016-03-22 19:33:17 | [diff] [blame] | 138 | return SynchronousCompositor::Frame(); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 139 | } |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 140 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 141 | ProcessCommonParams(common_renderer_params); |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 142 | |
samans | 9641873 | 2016-10-27 21:24:45 | [diff] [blame] | 143 | if (!compositor_frame) |
| 144 | return SynchronousCompositor::Frame(); |
| 145 | |
ojars | 5339299 | 2016-09-03 00:09:06 | [diff] [blame] | 146 | SynchronousCompositor::Frame frame; |
| 147 | frame.frame.reset(new cc::CompositorFrame); |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 148 | frame.compositor_frame_sink_id = compositor_frame_sink_id; |
boliu | 524d4c0b | 2016-10-28 04:04:05 | [diff] [blame^] | 149 | *frame.frame = std::move(*compositor_frame); |
samans | 9641873 | 2016-10-27 21:24:45 | [diff] [blame] | 150 | UpdateFrameMetaData(frame.frame->metadata.Clone()); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 151 | return frame; |
| 152 | } |
| 153 | |
| 154 | void SynchronousCompositorHost::UpdateFrameMetaData( |
fsamuel | d63137a | 2016-06-24 23:39:51 | [diff] [blame] | 155 | cc::CompositorFrameMetadata frame_metadata) { |
| 156 | rwhva_->SynchronousFrameMetadata(std::move(frame_metadata)); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 157 | } |
| 158 | |
boliu | 2255797 | 2016-10-25 16:08:29 | [diff] [blame] | 159 | SynchronousCompositorBrowserFilter* SynchronousCompositorHost::GetFilter() { |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 160 | return static_cast<RenderProcessHostImpl*>( |
| 161 | rwhva_->GetRenderWidgetHost()->GetProcess()) |
| 162 | ->synchronous_compositor_filter(); |
| 163 | } |
| 164 | |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 165 | namespace { |
| 166 | |
| 167 | class ScopedSetSkCanvas { |
| 168 | public: |
| 169 | explicit ScopedSetSkCanvas(SkCanvas* canvas) { |
| 170 | SynchronousCompositorSetSkCanvas(canvas); |
| 171 | } |
| 172 | |
| 173 | ~ScopedSetSkCanvas() { |
| 174 | SynchronousCompositorSetSkCanvas(nullptr); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | DISALLOW_COPY_AND_ASSIGN(ScopedSetSkCanvas); |
| 179 | }; |
| 180 | |
| 181 | } |
| 182 | |
| 183 | bool SynchronousCompositorHost::DemandDrawSwInProc(SkCanvas* canvas) { |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 184 | SyncCompositorCommonRendererParams common_renderer_params; |
| 185 | bool success = false; |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 186 | std::unique_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame); |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 187 | ScopedSetSkCanvas set_sk_canvas(canvas); |
| 188 | SyncCompositorDemandDrawSwParams params; // Unused. |
| 189 | if (!sender_->Send(new SyncCompositorMsg_DemandDrawSw( |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 190 | routing_id_, params, &success, &common_renderer_params, |
| 191 | frame.get()))) { |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | if (!success) |
| 195 | return false; |
| 196 | ProcessCommonParams(common_renderer_params); |
fsamuel | d63137a | 2016-06-24 23:39:51 | [diff] [blame] | 197 | UpdateFrameMetaData(std::move(frame->metadata)); |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 201 | class SynchronousCompositorHost::ScopedSendZeroMemory { |
| 202 | public: |
| 203 | ScopedSendZeroMemory(SynchronousCompositorHost* host) : host_(host) {} |
| 204 | ~ScopedSendZeroMemory() { host_->SendZeroMemory(); } |
| 205 | |
| 206 | private: |
| 207 | SynchronousCompositorHost* const host_; |
| 208 | |
| 209 | DISALLOW_COPY_AND_ASSIGN(ScopedSendZeroMemory); |
| 210 | }; |
| 211 | |
| 212 | struct SynchronousCompositorHost::SharedMemoryWithSize { |
| 213 | base::SharedMemory shm; |
| 214 | const size_t stride; |
| 215 | const size_t buffer_size; |
| 216 | |
| 217 | SharedMemoryWithSize(size_t stride, size_t buffer_size) |
| 218 | : stride(stride), buffer_size(buffer_size) {} |
| 219 | |
| 220 | private: |
| 221 | DISALLOW_COPY_AND_ASSIGN(SharedMemoryWithSize); |
| 222 | }; |
| 223 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 224 | bool SynchronousCompositorHost::DemandDrawSw(SkCanvas* canvas) { |
boliu | 4d4b5a8 | 2016-02-17 01:55:21 | [diff] [blame] | 225 | if (use_in_process_zero_copy_software_draw_) |
| 226 | return DemandDrawSwInProc(canvas); |
| 227 | |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 228 | SyncCompositorDemandDrawSwParams params; |
| 229 | params.size = gfx::Size(canvas->getBaseLayerSize().width(), |
| 230 | canvas->getBaseLayerSize().height()); |
| 231 | SkIRect canvas_clip; |
| 232 | canvas->getClipDeviceBounds(&canvas_clip); |
| 233 | params.clip = gfx::SkIRectToRect(canvas_clip); |
| 234 | params.transform.matrix() = canvas->getTotalMatrix(); |
| 235 | if (params.size.IsEmpty()) |
| 236 | return true; |
| 237 | |
| 238 | SkImageInfo info = |
| 239 | SkImageInfo::MakeN32Premul(params.size.width(), params.size.height()); |
| 240 | DCHECK_EQ(kRGBA_8888_SkColorType, info.colorType()); |
| 241 | size_t stride = info.minRowBytes(); |
| 242 | size_t buffer_size = info.getSafeSize(stride); |
| 243 | if (!buffer_size) |
| 244 | return false; // Overflow. |
| 245 | |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 246 | SetSoftwareDrawSharedMemoryIfNeeded(stride, buffer_size); |
| 247 | if (!software_draw_shm_) |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 248 | return false; |
| 249 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 250 | std::unique_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame); |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 251 | SyncCompositorCommonRendererParams common_renderer_params; |
| 252 | bool success = false; |
| 253 | if (!sender_->Send(new SyncCompositorMsg_DemandDrawSw( |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 254 | routing_id_, params, &success, &common_renderer_params, |
| 255 | frame.get()))) { |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 256 | return false; |
| 257 | } |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 258 | ScopedSendZeroMemory send_zero_memory(this); |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 259 | if (!success) |
| 260 | return false; |
| 261 | |
| 262 | ProcessCommonParams(common_renderer_params); |
fsamuel | d63137a | 2016-06-24 23:39:51 | [diff] [blame] | 263 | UpdateFrameMetaData(std::move(frame->metadata)); |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 264 | |
| 265 | SkBitmap bitmap; |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 266 | if (!bitmap.installPixels(info, software_draw_shm_->shm.memory(), stride)) |
boliu | f6d263e | 2015-11-25 03:53:58 | [diff] [blame] | 267 | return false; |
| 268 | |
| 269 | { |
| 270 | TRACE_EVENT0("browser", "DrawBitmap"); |
| 271 | canvas->save(); |
| 272 | canvas->resetMatrix(); |
| 273 | canvas->drawBitmap(bitmap, 0, 0); |
| 274 | canvas->restore(); |
| 275 | } |
| 276 | |
| 277 | return true; |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 278 | } |
| 279 | |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 280 | void SynchronousCompositorHost::SetSoftwareDrawSharedMemoryIfNeeded( |
| 281 | size_t stride, |
| 282 | size_t buffer_size) { |
| 283 | if (software_draw_shm_ && software_draw_shm_->stride == stride && |
| 284 | software_draw_shm_->buffer_size == buffer_size) |
| 285 | return; |
| 286 | software_draw_shm_.reset(); |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 287 | std::unique_ptr<SharedMemoryWithSize> software_draw_shm( |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 288 | new SharedMemoryWithSize(stride, buffer_size)); |
| 289 | { |
| 290 | TRACE_EVENT1("browser", "AllocateSharedMemory", "buffer_size", buffer_size); |
| 291 | if (!software_draw_shm->shm.CreateAndMapAnonymous(buffer_size)) |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | SyncCompositorSetSharedMemoryParams set_shm_params; |
| 296 | set_shm_params.buffer_size = buffer_size; |
| 297 | base::ProcessHandle renderer_process_handle = |
| 298 | rwhva_->GetRenderWidgetHost()->GetProcess()->GetHandle(); |
| 299 | if (!software_draw_shm->shm.ShareToProcess(renderer_process_handle, |
| 300 | &set_shm_params.shm_handle)) { |
| 301 | return; |
| 302 | } |
| 303 | |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 304 | bool success = false; |
| 305 | SyncCompositorCommonRendererParams common_renderer_params; |
| 306 | if (!sender_->Send(new SyncCompositorMsg_SetSharedMemory( |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 307 | routing_id_, set_shm_params, &success, &common_renderer_params)) || |
boliu | cafb1bf | 2015-12-29 01:40:19 | [diff] [blame] | 308 | !success) { |
| 309 | return; |
| 310 | } |
| 311 | software_draw_shm_ = std::move(software_draw_shm); |
| 312 | ProcessCommonParams(common_renderer_params); |
| 313 | } |
| 314 | |
| 315 | void SynchronousCompositorHost::SendZeroMemory() { |
| 316 | // No need to check return value. |
| 317 | sender_->Send(new SyncCompositorMsg_ZeroSharedMemory(routing_id_)); |
| 318 | } |
| 319 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 320 | void SynchronousCompositorHost::ReturnResources( |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 321 | uint32_t compositor_frame_sink_id, |
fsamuel | b62b7822 | 2016-07-15 01:14:14 | [diff] [blame] | 322 | const cc::ReturnedResourceArray& resources) { |
| 323 | DCHECK(!resources.empty()); |
boliu | ec72a2ed | 2016-05-10 00:51:56 | [diff] [blame] | 324 | sender_->Send(new SyncCompositorMsg_ReclaimResources( |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 325 | routing_id_, compositor_frame_sink_id, resources)); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void SynchronousCompositorHost::SetMemoryPolicy(size_t bytes_limit) { |
| 329 | if (bytes_limit_ == bytes_limit) |
| 330 | return; |
boliu | 42e0131 | 2016-05-12 21:47:52 | [diff] [blame] | 331 | |
| 332 | if (sender_->Send( |
| 333 | new SyncCompositorMsg_SetMemoryPolicy(routing_id_, bytes_limit))) { |
| 334 | bytes_limit_ = bytes_limit; |
| 335 | } |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void SynchronousCompositorHost::DidChangeRootLayerScrollOffset( |
| 339 | const gfx::ScrollOffset& root_offset) { |
| 340 | if (root_scroll_offset_ == root_offset) |
| 341 | return; |
| 342 | root_scroll_offset_ = root_offset; |
boliu | 22cdc6d | 2016-05-13 19:43:09 | [diff] [blame] | 343 | sender_->Send( |
| 344 | new SyncCompositorMsg_SetScroll(routing_id_, root_scroll_offset_)); |
hush | 92bd35e7 | 2015-11-13 22:57:37 | [diff] [blame] | 345 | } |
| 346 | |
boliu | 21d8ab14 | 2016-04-07 23:33:52 | [diff] [blame] | 347 | void SynchronousCompositorHost::SynchronouslyZoomBy(float zoom_delta, |
| 348 | const gfx::Point& anchor) { |
boliu | 21d8ab14 | 2016-04-07 23:33:52 | [diff] [blame] | 349 | SyncCompositorCommonRendererParams common_renderer_params; |
| 350 | if (!sender_->Send(new SyncCompositorMsg_ZoomBy( |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 351 | routing_id_, zoom_delta, anchor, &common_renderer_params))) { |
boliu | 21d8ab14 | 2016-04-07 23:33:52 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | ProcessCommonParams(common_renderer_params); |
| 355 | } |
| 356 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 357 | void SynchronousCompositorHost::OnComputeScroll( |
| 358 | base::TimeTicks animation_time) { |
| 359 | if (!need_animate_scroll_) |
| 360 | return; |
| 361 | need_animate_scroll_ = false; |
| 362 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 363 | SyncCompositorCommonRendererParams common_renderer_params; |
boliu | 8f3016e | 2016-06-04 00:59:18 | [diff] [blame] | 364 | sender_->Send( |
| 365 | new SyncCompositorMsg_ComputeScroll(routing_id_, animation_time)); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 366 | } |
| 367 | |
boliu | de5b75b | 2016-03-11 07:02:14 | [diff] [blame] | 368 | void SynchronousCompositorHost::DidOverscroll( |
chongz | a8ba91fc | 2016-08-16 21:39:17 | [diff] [blame] | 369 | const ui::DidOverscrollParams& over_scroll_params) { |
hush | 83e367c | 2016-06-18 02:37:54 | [diff] [blame] | 370 | client_->DidOverscroll(this, over_scroll_params.accumulated_overscroll, |
boliu | de5b75b | 2016-03-11 07:02:14 | [diff] [blame] | 371 | over_scroll_params.latest_overscroll_delta, |
| 372 | over_scroll_params.current_fling_velocity); |
| 373 | } |
| 374 | |
boliu | 3dd3ca7 | 2016-07-29 00:18:37 | [diff] [blame] | 375 | void SynchronousCompositorHost::DidSendBeginFrame( |
| 376 | ui::WindowAndroid* window_android) { |
boliu | 2255797 | 2016-10-25 16:08:29 | [diff] [blame] | 377 | if (SynchronousCompositorBrowserFilter* filter = GetFilter()) |
boliu | c37cd75b | 2016-10-24 17:54:39 | [diff] [blame] | 378 | filter->SyncStateAfterVSync(window_android, this); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 379 | } |
| 380 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 381 | void SynchronousCompositorHost::CompositorFrameSinkCreated() { |
| 382 | // New CompositorFrameSink is not aware of state from Browser side. So need to |
boliu | 42e0131 | 2016-05-12 21:47:52 | [diff] [blame] | 383 | // re-send all browser side state here. |
| 384 | sender_->Send( |
| 385 | new SyncCompositorMsg_SetMemoryPolicy(routing_id_, bytes_limit_)); |
| 386 | } |
| 387 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 388 | void SynchronousCompositorHost::ProcessCommonParams( |
| 389 | const SyncCompositorCommonRendererParams& params) { |
| 390 | // Ignore if |renderer_param_version_| is newer than |params.version|. This |
| 391 | // comparison takes into account when the unsigned int wraps. |
| 392 | if ((renderer_param_version_ - params.version) < 0x80000000) { |
| 393 | return; |
| 394 | } |
| 395 | renderer_param_version_ = params.version; |
| 396 | need_animate_scroll_ = params.need_animate_scroll; |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 397 | root_scroll_offset_ = params.total_scroll_offset; |
| 398 | |
boliu | 17bc31f | 2016-02-23 18:46:28 | [diff] [blame] | 399 | if (need_invalidate_count_ != params.need_invalidate_count) { |
| 400 | need_invalidate_count_ = params.need_invalidate_count; |
hush | 83e367c | 2016-06-18 02:37:54 | [diff] [blame] | 401 | client_->PostInvalidate(this); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 402 | } |
| 403 | |
boliu | 17bc31f | 2016-02-23 18:46:28 | [diff] [blame] | 404 | if (did_activate_pending_tree_count_ != |
| 405 | params.did_activate_pending_tree_count) { |
| 406 | did_activate_pending_tree_count_ = params.did_activate_pending_tree_count; |
hush | 83e367c | 2016-06-18 02:37:54 | [diff] [blame] | 407 | client_->DidUpdateContent(this); |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // Ensure only valid values from compositor are sent to client. |
| 411 | // Compositor has page_scale_factor set to 0 before initialization, so check |
| 412 | // for that case here. |
| 413 | if (params.page_scale_factor) { |
| 414 | client_->UpdateRootLayerState( |
hush | 83e367c | 2016-06-18 02:37:54 | [diff] [blame] | 415 | this, gfx::ScrollOffsetToVector2dF(params.total_scroll_offset), |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 416 | gfx::ScrollOffsetToVector2dF(params.max_scroll_offset), |
| 417 | params.scrollable_size, params.page_scale_factor, |
| 418 | params.min_page_scale_factor, params.max_page_scale_factor); |
| 419 | } |
| 420 | } |
| 421 | |
boliu | bee541f4 | 2015-11-05 00:52:53 | [diff] [blame] | 422 | } // namespace content |