blob: 924b11825b2c432928e7c0079c24cd6df8212d74 [file] [log] [blame]
[email protected]d3d6325d2012-01-03 21:17:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f1a29a02011-10-06 23:08:445#include "content/renderer/render_thread_impl.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]da00a2882009-03-09 17:51:197#include <algorithm>
[email protected]61a9b2d82010-02-26 00:31:088#include <limits>
[email protected]75e126b932009-09-28 19:38:499#include <map>
dcheng07945f632015-12-26 07:59:3210#include <utility>
[email protected]da00a2882009-03-09 17:51:1911#include <vector>
12
[email protected]237a14852012-04-28 02:56:3813#include "base/allocator/allocator_extension.h"
harakenbbfdd9f02017-01-12 07:14:0414#include "base/at_exit.h"
[email protected]06533c0b2009-03-05 21:39:1115#include "base/command_line.h"
[email protected]94f9a0f682009-06-15 18:30:3016#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0317#include "base/logging.h"
avi1023d012015-12-25 02:39:1418#include "base/macros.h"
reveman1af05cb32015-03-17 23:18:1619#include "base/memory/discardable_memory_allocator.h"
hajimehoshi7bb39582016-10-12 04:30:5720#include "base/memory/memory_coordinator_client_registry.h"
dchengcedca5612016-04-09 01:40:1521#include "base/memory/ptr_util.h"
[email protected]8bac37b2013-07-18 19:13:5622#include "base/memory/shared_memory.h"
gabf64a25e2017-05-12 19:42:5623#include "base/message_loop/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3824#include "base/metrics/field_trial.h"
tasaka27961a2017-05-24 07:33:2525#include "base/metrics/histogram_functions.h"
asvitkine8d51e9d2016-09-02 23:55:4326#include "base/metrics/histogram_macros.h"
[email protected]433df472012-03-07 20:33:3927#include "base/path_service.h"
tasakb46626a2016-10-18 05:54:4428#include "base/process/process_metrics.h"
fdoray2df4a9e2016-07-18 23:47:1629#include "base/run_loop.h"
[email protected]21aa99682013-06-11 07:17:0130#include "base/strings/string16.h"
[email protected]1e1d1e12014-01-17 16:14:2931#include "base/strings/string_number_conversions.h"
hablichd6a4f122015-10-28 11:34:4932#include "base/strings/string_split.h"
ccameron6728bae32015-01-09 20:18:0633#include "base/strings/sys_string_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0034#include "base/strings/utf_string_conversions.h"
fdoray50a38342016-11-21 20:46:0435#include "base/threading/sequenced_worker_pool.h"
reveman34b7a1522015-03-23 20:27:4736#include "base/threading/simple_thread.h"
[email protected]1357c322010-12-30 22:18:5637#include "base/threading/thread_local.h"
[email protected]d293572a2013-05-23 18:50:4538#include "base/threading/thread_restrictions.h"
gab30f26df2016-05-11 19:37:5539#include "base/threading/thread_task_runner_handle.h"
ccameron9c48d1d42016-09-22 01:46:0140#include "base/trace_event/memory_dump_manager.h"
primiano9e38d552015-01-28 04:18:0141#include "base/trace_event/trace_event.h"
[email protected]7a4de7a62010-08-17 18:38:2442#include "base/values.h"
avi1023d012015-12-25 02:39:1443#include "build/build_config.h"
jbroman0d302162015-09-05 05:46:2344#include "cc/base/histograms.h"
[email protected]d72493152014-01-08 17:37:4545#include "cc/base/switches.h"
[email protected]38564622014-08-19 02:47:1846#include "cc/blink/web_layer_impl.h"
danakj920156852015-05-18 20:22:2947#include "cc/raster/task_graph_runner.h"
danakjba65a0912017-09-21 16:38:4248#include "cc/trees/layer_tree_frame_sink.h"
vollick1050cc62015-12-03 07:04:5449#include "cc/trees/layer_tree_host_common.h"
loysoa6edaaff2015-05-25 03:26:4450#include "cc/trees/layer_tree_settings.h"
penghuangd81c1a62016-11-02 20:06:0651#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
dalecurtis4a9839a2017-05-04 23:40:4752#include "components/metrics/public/interfaces/single_sample_metrics.mojom.h"
53#include "components/metrics/single_sample_metrics.h"
danakjc7afae52017-06-20 21:12:4154#include "components/viz/client/client_layer_tree_frame_sink.h"
danakj92429852017-06-29 00:19:0355#include "components/viz/client/client_shared_bitmap_manager.h"
Valery Arkhangorodsky513ac8fd2017-08-02 23:09:2356#include "components/viz/client/hit_test_data_provider.h"
samans7e6675cc2017-06-05 20:16:0857#include "components/viz/client/local_surface_id_provider.h"
Fady Samuel343a74952017-11-30 14:19:2058#include "components/viz/common/features.h"
danakjf20f4502017-09-26 17:13:3159#include "components/viz/common/frame_sinks/copy_output_request.h"
Alex Zhang1f159572017-08-23 20:14:2060#include "components/viz/common/gpu/vulkan_in_process_context_provider.h"
bashia6264042016-09-15 04:49:5161#include "content/child/memory/child_memory_coordinator_impl.h"
[email protected]643255da2013-06-12 20:55:1362#include "content/child/runtime_features.h"
[email protected]57cf97882013-06-26 20:39:0163#include "content/child/thread_safe_sender.h"
[email protected]89c36f82013-07-17 06:23:2764#include "content/common/content_constants_internal.h"
[email protected]5f2aa722013-08-07 16:59:4165#include "content/common/dom_storage/dom_storage_messages.h"
thakis18e426412017-03-15 12:06:3766#include "content/common/features.h"
[email protected]82307f6b2014-08-07 03:30:1267#include "content/common/frame_messages.h"
raymesbba82b32016-07-19 00:41:3868#include "content/common/frame_owner_properties.h"
sunnyps8f9139e2017-05-12 17:53:2569#include "content/common/gpu_stream_constants.h"
[email protected]778574e2011-03-21 22:03:5070#include "content/common/view_messages.h"
[email protected]a458504b2012-07-23 19:57:0671#include "content/public/common/content_constants.h"
bashia6264042016-09-15 04:49:5172#include "content/public/common/content_features.h"
[email protected]433df472012-03-07 20:33:3973#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2974#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3175#include "content/public/common/renderer_preferences.h"
benbd9dc802017-04-19 01:37:4376#include "content/public/common/service_manager_connection.h"
ben649b3edd2017-03-23 00:32:0277#include "content/public/common/service_names.mojom.h"
benbd9dc802017-04-19 01:37:4378#include "content/public/common/simple_connection_filter.h"
[email protected]58436a12012-03-21 17:10:2679#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3480#include "content/public/renderer/content_renderer_client.h"
tyoshino832a58a2016-04-18 08:14:0881#include "content/public/renderer/render_thread_observer.h"
[email protected]64ffa0442011-10-03 22:08:3682#include "content/public/renderer/render_view_visitor.h"
John Abd-El-Malekdcf1d1372017-10-22 06:39:4183#include "content/renderer/appcache/appcache_dispatcher.h"
84#include "content/renderer/appcache/appcache_frontend_impl.h"
fsamuel6c1dfeb2014-12-18 19:21:3385#include "content/renderer/browser_plugin/browser_plugin_manager.h"
jsbell279efb42015-03-31 17:02:4686#include "content/renderer/cache_storage/cache_storage_dispatcher.h"
87#include "content/renderer/cache_storage/cache_storage_message_filter.h"
prashant.nfad657e2016-06-01 07:52:1788#include "content/renderer/categorized_worker_pool.h"
[email protected]1910fe82012-05-10 00:04:1089#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
90#include "content/renderer/dom_storage/webstoragearea_impl.h"
91#include "content/renderer/dom_storage/webstoragenamespace_impl.h"
tbansalb612c5d2017-05-25 18:53:0692#include "content/renderer/effective_connection_type_helper.h"
John Abd-El-Malekdcf1d1372017-10-22 06:39:4193#include "content/renderer/fileapi/file_system_dispatcher.h"
94#include "content/renderer/fileapi/webfilesystem_impl.h"
simonhonga7e3ac42014-11-11 20:50:2295#include "content/renderer/gpu/compositor_external_begin_frame_source.h"
96#include "content/renderer/gpu/compositor_forwarding_message_filter.h"
danakj83066a32016-06-21 02:34:4997#include "content/renderer/gpu/frame_swap_message_queue.h"
John Abd-El-Malekdcf1d1372017-10-22 06:39:4198#include "content/renderer/indexed_db/indexed_db_dispatcher.h"
[email protected]7a72d452013-12-13 10:01:1399#include "content/renderer/input/input_event_filter.h"
100#include "content/renderer/input/input_handler_manager.h"
jdduke691dd572014-12-02 20:47:52101#include "content/renderer/input/main_thread_input_event_filter.h"
John Abd-El-Malek6b56ef712017-10-21 22:52:46102#include "content/renderer/loader/resource_dispatcher.h"
Scott Violet1098538e2017-10-05 19:23:33103#include "content/renderer/mash_util.h"
[email protected]f7eb0a392011-07-12 10:19:51104#include "content/renderer/media/audio_input_message_filter.h"
105#include "content/renderer/media/audio_message_filter.h"
[email protected]3958e972012-07-17 00:25:41106#include "content/renderer/media/audio_renderer_mixer_manager.h"
Miguel Casas-Sanchezcfcca5d52017-07-07 02:32:59107#include "content/renderer/media/gpu/gpu_video_accelerator_factories_impl.h"
[email protected]d8cd8372012-03-09 10:49:51108#include "content/renderer/media/media_stream_center.h"
[email protected]a9875152013-06-22 04:03:03109#include "content/renderer/media/midi_message_filter.h"
xhwang194acae2014-11-12 22:46:33110#include "content/renderer/media/render_media_client.h"
[email protected]80b161a2011-06-27 17:42:11111#include "content/renderer/media/video_capture_impl_manager.h"
Stuart Langley849079a2017-08-28 02:02:50112#include "content/renderer/mus/render_widget_window_tree_client_factory.h"
113#include "content/renderer/mus/renderer_window_tree_client.h"
[email protected]d7ff5fb2014-05-29 19:50:25114#include "content/renderer/net_info_helper.h"
John Abd-El-Malekf9f86c12017-10-21 07:22:23115#include "content/renderer/notifications/notification_dispatcher.h"
[email protected]4761cf12012-09-12 10:37:55116#include "content/renderer/p2p/socket_dispatcher.h"
John Abd-El-Malekf75a3192017-10-22 14:23:20117#include "content/renderer/quota_dispatcher.h"
[email protected]82307f6b2014-08-07 03:30:12118#include "content/renderer/render_frame_proxy.h"
[email protected]8704f89b2011-04-15 00:30:05119#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:28120#include "content/renderer/render_view_impl.h"
tfarina556a7232014-10-05 01:02:09121#include "content/renderer/renderer_blink_platform_impl.h"
shimazu5de409e42016-09-29 08:45:28122#include "content/renderer/service_worker/embedded_worker_instance_client_impl.h"
mek27c9d742015-07-16 18:30:18123#include "content/renderer/service_worker/service_worker_context_client.h"
kinuko5af4ffe2015-06-09 03:38:46124#include "content/renderer/service_worker/service_worker_context_message_filter.h"
John Abd-El-Maleke1d4de32017-10-21 16:14:29125#include "content/renderer/service_worker/service_worker_message_filter.h"
[email protected]0ec90d522014-03-12 16:28:19126#include "content/renderer/shared_worker/embedded_shared_worker_stub.h"
Darin Fisherbcc33632017-09-26 00:57:16127#include "content/renderer/shared_worker/shared_worker_factory_impl.h"
John Abd-El-Malekf75a3192017-10-22 14:23:20128#include "content/renderer/web_database_observer_impl.h"
129#include "content/renderer/worker_thread_registry.h"
juncai2f298a82017-04-18 03:51:39130#include "device/gamepad/public/cpp/gamepads.h"
chunyang.daibe874c52014-11-14 06:45:05131#include "gin/public/debug.h"
dongseong.hwang72183b02014-12-08 10:41:55132#include "gpu/GLES2/gl2extchromium.h"
Victor Miura3a4ad4f82017-12-13 06:03:45133#include "gpu/command_buffer/client/gles2_interface.h"
134#include "gpu/command_buffer/client/raster_interface.h"
danakj6f4e1e22016-04-20 03:27:34135#include "gpu/command_buffer/client/shared_memory_limits.h"
Adrienne Walker436a7752017-08-28 23:33:09136#include "gpu/config/gpu_switches.h"
danakj870925d42016-05-03 20:07:38137#include "gpu/ipc/client/command_buffer_proxy_impl.h"
penghuang346a46f92016-03-31 21:37:52138#include "gpu/ipc/client/gpu_channel_host.h"
[email protected]46f36a492010-07-28 19:36:41139#include "ipc/ipc_channel_handle.h"
amistryd4aa70d2016-06-23 07:52:37140#include "ipc/ipc_channel_mojo.h"
[email protected]cb6037d2009-11-16 22:55:17141#include "ipc/ipc_platform_file.h"
[email protected]433df472012-03-07 20:33:39142#include "media/base/media.h"
chcunningham9a285ed2017-03-08 21:48:56143#include "media/base/media_switches.h"
Brett Wilson0748bf412016-11-22 17:55:46144#include "media/media_features.h"
Dale Curtis863022e2017-08-19 02:05:34145#include "media/video/gpu_video_accelerator_factories.h"
rockot85dce0862015-11-13 01:33:59146#include "mojo/public/cpp/bindings/strong_binding.h"
Yuki Yamada68992b02017-07-31 06:13:45147#include "mojo/public/cpp/system/message_pipe.h"
[email protected]620161e2011-03-07 18:05:26148#include "net/base/net_errors.h"
eroman9ab64842015-07-21 05:07:52149#include "net/base/port_util.h"
hablichd6a4f122015-10-28 11:34:49150#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
tfarina7a4a7fd2016-01-20 14:23:44151#include "net/base/url_util.h"
brettw4b461082016-11-19 18:55:16152#include "ppapi/features/features.h"
Khushal3e96e662017-10-30 23:16:50153#include "services/metrics/public/cpp/mojo_ukm_recorder.h"
penghuangd6843e42016-12-17 13:57:20154#include "services/service_manager/public/cpp/connector.h"
rockot734fb662016-10-15 16:41:30155#include "services/service_manager/public/cpp/interface_provider.h"
sadrul85cc5d82016-12-20 03:37:41156#include "services/ui/public/cpp/gpu/context_provider_command_buffer.h"
Stuart Langley849079a2017-08-28 02:02:50157#include "services/ui/public/cpp/gpu/gpu.h"
penghuangd6843e42016-12-17 13:57:20158#include "services/ui/public/interfaces/constants.mojom.h"
[email protected]b48c53ad2014-02-05 21:59:18159#include "skia/ext/event_tracer_impl.h"
ssid59c969162015-07-28 13:02:58160#include "skia/ext/skia_memory_dump_provider.h"
kinukoed1ed1d2017-02-08 09:13:43161#include "third_party/WebKit/public/platform/WebCache.h"
reed6e5a72222015-08-06 20:37:16162#include "third_party/WebKit/public/platform/WebImageGenerator.h"
bashi5f4c78922016-08-18 23:06:08163#include "third_party/WebKit/public/platform/WebMemoryCoordinator.h"
kinukofbfee762017-03-08 04:01:06164#include "third_party/WebKit/public/platform/WebNetworkStateNotifier.h"
nverne61d2da872017-05-24 10:15:30165#include "third_party/WebKit/public/platform/WebRuntimeFeatures.h"
[email protected]89c36f82013-07-17 06:23:27166#include "third_party/WebKit/public/platform/WebString.h"
skyostil457b0a12014-09-09 10:12:07167#include "third_party/WebKit/public/platform/WebThread.h"
altiminc7369bf2017-04-11 14:29:15168#include "third_party/WebKit/public/platform/scheduler/child/webthread_base.h"
skyostil529caa292016-08-10 17:44:51169#include "third_party/WebKit/public/platform/scheduler/renderer/renderer_scheduler.h"
[email protected]2255a9332013-06-17 05:12:31170#include "third_party/WebKit/public/web/WebDocument.h"
171#include "third_party/WebKit/public/web/WebFrame.h"
172#include "third_party/WebKit/public/web/WebKit.h"
[email protected]2255a9332013-06-17 05:12:31173#include "third_party/WebKit/public/web/WebScriptController.h"
174#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
[email protected]2255a9332013-06-17 05:12:31175#include "third_party/WebKit/public/web/WebView.h"
David Benjaminb946811c2017-06-26 23:34:29176#include "third_party/boringssl/src/include/openssl/evp.h"
[email protected]ddbb53342014-01-06 10:59:47177#include "third_party/skia/include/core/SkGraphics.h"
[email protected]c49201a2012-05-24 11:04:57178#include "ui/base/layout.h"
[email protected]18ad6772011-09-20 21:51:32179#include "ui/base/ui_base_switches.h"
Sadrul Habib Chowdhuryeeb5a942017-11-28 21:17:23180#include "ui/base/ui_base_switches_util.h"
mlamouri7c149652017-03-24 00:00:09181#include "ui/display/display_switches.h"
ennee292bdc2016-09-15 19:57:15182#include "ui/gl/gl_switches.h"
[email protected]2c62b562009-01-27 19:04:50183
[email protected]33b02992014-03-01 01:06:29184#if defined(OS_ANDROID)
185#include <cpu-features.h>
boliubee541f42015-11-05 00:52:53186#include "content/renderer/android/synchronous_compositor_filter.h"
danakjc7afae52017-06-20 21:12:41187#include "content/renderer/android/synchronous_layer_tree_frame_sink.h"
boliue81d16132016-04-26 00:54:41188#include "content/renderer/media/android/stream_texture_factory.h"
dalecurtis88af3932016-02-20 00:12:20189#include "media/base/android/media_codec_util.h"
[email protected]33b02992014-03-01 01:06:29190#endif
191
192#if defined(OS_MACOSX)
ccamerona7644752014-12-30 01:16:31193#include "base/mac/mac_util.h"
rsesek1efb3c32015-09-29 15:39:50194#include "content/renderer/theme_helper_mac.h"
[email protected]33b02992014-03-01 01:06:29195#include "content/renderer/webscrollbarbehavior_impl_mac.h"
196#endif
197
[email protected]da00a2882009-03-09 17:51:19198#if defined(OS_WIN)
199#include <windows.h>
200#include <objbase.h>
201#endif
202
Brett Wilson0748bf412016-11-22 17:55:46203#if BUILDFLAG(ENABLE_WEBRTC)
liushouqun517b1292016-09-14 05:58:59204#include "content/renderer/media/aec_dump_message_filter.h"
[email protected]22fe91d2014-08-12 17:07:12205#include "content/renderer/media/peer_connection_tracker.h"
206#include "content/renderer/media/rtc_peer_connection_handler.h"
207#include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
[email protected]22fe91d2014-08-12 17:07:12208#endif
209
chunyang.daibe874c52014-11-14 06:45:05210#ifdef ENABLE_VTUNE_JIT_INTERFACE
211#include "v8/src/third_party/vtune/v8-vtune.h"
212#endif
213
ochangdd89a1e2016-06-08 16:39:01214#if defined(ENABLE_IPC_FUZZER)
215#include "content/common/external_ipc_dumper.h"
216#endif
217
tasakb46626a2016-10-18 05:54:44218#if defined(OS_MACOSX)
219#include <malloc/malloc.h>
220#else
221#include <malloc.h>
222#endif
223
[email protected]d293572a2013-05-23 18:50:45224using base::ThreadRestrictions;
[email protected]180ef242013-11-07 06:50:46225using blink::WebDocument;
226using blink::WebFrame;
227using blink::WebNetworkStateNotifier;
228using blink::WebRuntimeFeatures;
229using blink::WebScriptController;
230using blink::WebSecurityPolicy;
231using blink::WebString;
232using blink::WebView;
[email protected]e9ff79c2012-10-19 21:31:26233
234namespace content {
initial.commit09911bf2008-07-26 23:55:29235
[email protected]42f1d7822009-07-23 18:17:55236namespace {
[email protected]da9ccfb2012-01-28 00:34:40237
avi1023d012015-12-25 02:39:14238const int64_t kInitialIdleHandlerDelayMs = 1000;
239const int64_t kLongIdleHandlerDelayMs = 30 * 1000;
[email protected]bee16aab2009-08-26 15:55:03240
[email protected]dd2c1022014-07-22 23:13:57241// Maximum allocation size allowed for image scaling filters that
242// require pre-scaling. Skia will fallback to a filter that doesn't
243// require pre-scaling if the default filter would require an
244// allocation that exceeds this limit.
245const size_t kImageCacheSingleAllocationByteLimit = 64 * 1024 * 1024;
246
samans2040988b2017-04-11 23:58:35247#if defined(OS_ANDROID)
danakj83066a32016-06-21 02:34:49248// Unique identifier for each output surface created.
danakjc7afae52017-06-20 21:12:41249uint32_t g_next_layer_tree_frame_sink_id = 1;
samans2040988b2017-04-11 23:58:35250#endif
danakj83066a32016-06-21 02:34:49251
rockot5c478a72016-09-28 23:14:18252// An implementation of mojom::RenderMessageFilter which can be mocked out
253// for tests which may indirectly send messages over this interface.
254mojom::RenderMessageFilter* g_render_message_filter_for_testing;
255
olegmax045f7fb12017-05-19 07:58:55256// An implementation of RendererBlinkPlatformImpl which can be mocked out
257// for tests.
258RendererBlinkPlatformImpl* g_current_blink_platform_impl_for_testing;
259
[email protected]f1a29a02011-10-06 23:08:44260// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16261// incorrectly from the wrong thread.
scottmg5e65e3a2017-03-08 08:48:46262base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl>>::DestructorAtExit
[email protected]6de0fd1d2011-11-15 13:31:49263 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54264
John Abd-El-Malekd4882642017-12-04 21:45:19265base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner>>::
266 DestructorAtExit g_main_task_runner = LAZY_INSTANCE_INITIALIZER;
267
hong.zhengb28b5332016-05-11 02:33:39268// v8::MemoryPressureLevel should correspond to base::MemoryPressureListener.
269static_assert(static_cast<v8::MemoryPressureLevel>(
270 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) ==
271 v8::MemoryPressureLevel::kNone, "none level not align");
272static_assert(static_cast<v8::MemoryPressureLevel>(
273 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) ==
274 v8::MemoryPressureLevel::kModerate, "moderate level not align");
275static_assert(static_cast<v8::MemoryPressureLevel>(
276 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) ==
277 v8::MemoryPressureLevel::kCritical, "critical level not align");
278
hong.zheng2e296f822016-06-29 02:47:44279// WebMemoryPressureLevel should correspond to base::MemoryPressureListener.
280static_assert(static_cast<blink::WebMemoryPressureLevel>(
Blink Reformat1c4d759e2017-04-09 16:34:54281 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) ==
282 blink::kWebMemoryPressureLevelNone,
283 "blink::WebMemoryPressureLevelNone not align");
284static_assert(
285 static_cast<blink::WebMemoryPressureLevel>(
286 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) ==
287 blink::kWebMemoryPressureLevelModerate,
288 "blink::WebMemoryPressureLevelModerate not align");
289static_assert(
290 static_cast<blink::WebMemoryPressureLevel>(
291 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) ==
292 blink::kWebMemoryPressureLevelCritical,
293 "blink::WebMemoryPressureLevelCritical not align");
hong.zheng2e296f822016-06-29 02:47:44294
[email protected]b2d98762012-09-03 17:04:06295void* CreateHistogram(
[email protected]d1b8fccc2011-08-03 01:20:13296 const char *name, int min, int max, size_t buckets) {
297 if (min <= 0)
298 min = 1;
[email protected]b2d98762012-09-03 17:04:06299 std::string histogram_name;
300 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
301 if (render_thread_impl) { // Can be null in tests.
302 histogram_name = render_thread_impl->
303 histogram_customizer()->ConvertToCustomHistogramName(name);
304 } else {
305 histogram_name = std::string(name);
306 }
[email protected]de415552013-01-23 04:12:17307 base::HistogramBase* histogram = base::Histogram::FactoryGet(
[email protected]b2d98762012-09-03 17:04:06308 histogram_name, min, max, buckets,
309 base::Histogram::kUmaTargetedHistogramFlag);
[email protected]d1b8fccc2011-08-03 01:20:13310 return histogram;
311}
312
[email protected]b2d98762012-09-03 17:04:06313void AddHistogramSample(void* hist, int sample) {
[email protected]d1b8fccc2011-08-03 01:20:13314 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
315 histogram->Add(sample);
316}
317
ben76f52b242016-06-18 05:42:48318class FrameFactoryImpl : public mojom::FrameFactory {
rockotf8fdd9b2015-12-16 22:22:35319 public:
benf28ce882017-05-02 16:15:49320 explicit FrameFactoryImpl(const service_manager::BindSourceInfo& source_info)
321 : source_info_(source_info), routing_id_highmark_(-1) {}
rockotf8fdd9b2015-12-16 22:22:35322
ben76f52b242016-06-18 05:42:48323 private:
324 // mojom::FrameFactory:
Balazs Engedyba034e72017-10-27 22:26:28325 void CreateFrame(int32_t frame_routing_id,
326 mojom::FrameRequest frame_request) override {
rockotf8fdd9b2015-12-16 22:22:35327 // TODO(morrita): This is for investigating http://crbug.com/415059 and
328 // should be removed once it is fixed.
329 CHECK_LT(routing_id_highmark_, frame_routing_id);
330 routing_id_highmark_ = frame_routing_id;
331
332 RenderFrameImpl* frame = RenderFrameImpl::FromRoutingID(frame_routing_id);
333 // We can receive a GetServiceProviderForFrame message for a frame not yet
rockot067ca55f2016-09-30 22:00:15334 // created due to a race between the message and a
335 // mojom::Renderer::CreateView IPC that triggers creation of the RenderFrame
336 // we want.
rockotf8fdd9b2015-12-16 22:22:35337 if (!frame) {
ben76f52b242016-06-18 05:42:48338 RenderThreadImpl::current()->RegisterPendingFrameCreate(
Balazs Engedyba034e72017-10-27 22:26:28339 source_info_, frame_routing_id, std::move(frame_request));
rockotf8fdd9b2015-12-16 22:22:35340 return;
341 }
342
Balazs Engedyba034e72017-10-27 22:26:28343 frame->BindFrame(source_info_, std::move(frame_request));
rockotf8fdd9b2015-12-16 22:22:35344 }
345
346 private:
benf28ce882017-05-02 16:15:49347 service_manager::BindSourceInfo source_info_;
rockotf8fdd9b2015-12-16 22:22:35348 int32_t routing_id_highmark_;
rockotf8fdd9b2015-12-16 22:22:35349};
350
Ben Goodger21ada1e2017-07-19 14:53:01351void CreateFrameFactory(mojom::FrameFactoryRequest request,
352 const service_manager::BindSourceInfo& source_info) {
Jeremy Roman04f27c372017-10-27 15:20:55353 mojo::MakeStrongBinding(std::make_unique<FrameFactoryImpl>(source_info),
rockot8e66a08d2016-09-13 00:48:21354 std::move(request));
rockotf8fdd9b2015-12-16 22:22:35355}
356
sadrul85cc5d82016-12-20 03:37:41357scoped_refptr<ui::ContextProviderCommandBuffer> CreateOffscreenContext(
danakjc3983552016-05-03 00:04:35358 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host,
Antoine Labour5ac65db2017-12-19 18:02:58359 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
danakj0dd9e1e2016-05-11 22:15:09360 const gpu::SharedMemoryLimits& limits,
danakj9a04adc2016-05-16 22:45:07361 bool support_locking,
Victor Miura3a4ad4f82017-12-13 06:03:45362 bool support_gles2_interface,
363 bool support_raster_interface,
Adrienne Walker436a7752017-08-28 23:33:09364 bool support_oop_rasterization,
sadrul85cc5d82016-12-20 03:37:41365 ui::command_buffer_metrics::ContextType type,
sunnypsd8ce1c22016-05-10 18:02:40366 int32_t stream_id,
sunnyps8f9139e2017-05-12 17:53:25367 gpu::SchedulingPriority stream_priority) {
danakje8ec797e2016-04-19 04:08:43368 DCHECK(gpu_channel_host);
369 // This is used to create a few different offscreen contexts:
Victor Miura3a4ad4f82017-12-13 06:03:45370 // - The shared main thread context, used by blink for 2D Canvas.
371 // - The compositor worker context, used for GPU raster.
372 // - The media context, used for accelerated video decoding.
danakje8ec797e2016-04-19 04:08:43373 // This is for an offscreen context, so the default framebuffer doesn't need
374 // alpha, depth, stencil, antialiasing.
Antoine Labourfeab2392017-12-21 20:28:39375 gpu::ContextCreationAttribs attributes;
danakje8ec797e2016-04-19 04:08:43376 attributes.alpha_size = -1;
377 attributes.depth_size = 0;
378 attributes.stencil_size = 0;
379 attributes.samples = 0;
380 attributes.sample_buffers = 0;
381 attributes.bind_generates_resource = false;
382 attributes.lose_context_when_out_of_memory = true;
Victor Miura3a4ad4f82017-12-13 06:03:45383 attributes.enable_gles2_interface = support_gles2_interface;
384 attributes.enable_raster_interface = support_raster_interface;
Adrienne Walker436a7752017-08-28 23:33:09385 attributes.enable_oop_rasterization = support_oop_rasterization;
386
sunnypsd8ce1c22016-05-10 18:02:40387 const bool automatic_flushes = false;
kylechar96f3eba2017-09-25 20:23:56388 return base::MakeRefCounted<ui::ContextProviderCommandBuffer>(
Antoine Labour5ac65db2017-12-19 18:02:58389 std::move(gpu_channel_host), gpu_memory_buffer_manager, stream_id,
390 stream_priority, gpu::kNullSurfaceHandle,
zmo09ea813b2017-01-20 23:38:36391 GURL("chrome://gpu/RenderThreadImpl::CreateOffscreenContext/" +
392 ui::command_buffer_metrics::ContextTypeToString(type)),
Victor Miuraff6488612017-12-21 04:16:15393 automatic_flushes, support_locking, limits, attributes,
394 nullptr /* share_context */, type);
danakje8ec797e2016-04-19 04:08:43395}
396
dalecurtis4a9839a2017-05-04 23:40:47397// Hook that allows single-sample metric code from //components/metrics to
398// connect from the renderer process to the browser process.
399void CreateSingleSampleMetricsProvider(
400 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
401 service_manager::Connector* connector,
402 metrics::mojom::SingleSampleMetricsProviderRequest request) {
403 if (task_runner->BelongsToCurrentThread()) {
404 connector->BindInterface(mojom::kBrowserServiceName, std::move(request));
405 return;
406 }
407
408 task_runner->PostTask(
409 FROM_HERE,
tzikff2a81a2017-09-15 05:10:06410 base::BindOnce(&CreateSingleSampleMetricsProvider, std::move(task_runner),
411 connector, base::Passed(&request)));
dalecurtis4a9839a2017-05-04 23:40:47412}
413
samans7e6675cc2017-06-05 20:16:08414class RendererLocalSurfaceIdProvider : public viz::LocalSurfaceIdProvider {
415 public:
Fady Samueld5c26182017-07-12 02:43:33416 const viz::LocalSurfaceId& GetLocalSurfaceIdForFrame(
danakj5e0a12b2017-09-25 17:26:49417 const viz::CompositorFrame& frame) override {
samans7e6675cc2017-06-05 20:16:08418 auto new_surface_properties =
419 RenderWidgetSurfaceProperties::FromCompositorFrame(frame);
420 if (!local_surface_id_.is_valid() ||
421 new_surface_properties != surface_properties_) {
Chris Blumeefd8f242017-12-08 00:24:27422 local_surface_id_ = parent_local_surface_id_allocator_.GenerateId();
samans7e6675cc2017-06-05 20:16:08423 surface_properties_ = new_surface_properties;
424 }
425 return local_surface_id_;
426 }
427
428 private:
Chris Blumeefd8f242017-12-08 00:24:27429 viz::ParentLocalSurfaceIdAllocator parent_local_surface_id_allocator_;
Fady Samueld5c26182017-07-12 02:43:33430 viz::LocalSurfaceId local_surface_id_;
samans7e6675cc2017-06-05 20:16:08431 RenderWidgetSurfaceProperties surface_properties_;
432};
433
Khushal3e96e662017-10-30 23:16:50434// This factory is used to defer binding of the InterfacePtr to the compositor
435// thread.
436class UkmRecorderFactoryImpl : public cc::UkmRecorderFactory {
437 public:
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:46438 explicit UkmRecorderFactoryImpl(
439 std::unique_ptr<service_manager::Connector> connector)
Khushalda50f8152017-12-01 09:04:40440 : connector_(std::move(connector)) {
441 DCHECK(connector_);
Khushal3e96e662017-10-30 23:16:50442 }
443 ~UkmRecorderFactoryImpl() override = default;
444
445 std::unique_ptr<ukm::UkmRecorder> CreateRecorder() override {
Khushalda50f8152017-12-01 09:04:40446 return ukm::MojoUkmRecorder::Create(connector_.get());
Khushal3e96e662017-10-30 23:16:50447 }
448
449 private:
Khushalda50f8152017-12-01 09:04:40450 std::unique_ptr<service_manager::Connector> connector_;
Khushal3e96e662017-10-30 23:16:50451};
452
[email protected]b2d98762012-09-03 17:04:06453} // namespace
454
[email protected]b2d98762012-09-03 17:04:06455RenderThreadImpl::HistogramCustomizer::HistogramCustomizer() {
456 custom_histograms_.insert("V8.MemoryExternalFragmentationTotal");
457 custom_histograms_.insert("V8.MemoryHeapSampleTotalCommitted");
458 custom_histograms_.insert("V8.MemoryHeapSampleTotalUsed");
hablich4598ddc2015-11-07 18:37:28459 custom_histograms_.insert("V8.MemoryHeapUsed");
460 custom_histograms_.insert("V8.MemoryHeapCommitted");
[email protected]b2d98762012-09-03 17:04:06461}
462
463RenderThreadImpl::HistogramCustomizer::~HistogramCustomizer() {}
464
465void RenderThreadImpl::HistogramCustomizer::RenderViewNavigatedToHost(
466 const std::string& host, size_t view_count) {
avi83883c82014-12-23 00:08:49467 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
468 switches::kDisableHistogramCustomizer)) {
[email protected]9baee832012-12-10 11:07:15469 return;
470 }
[email protected]b2d98762012-09-03 17:04:06471 // Check if all RenderViews are displaying a page from the same host. If there
472 // is only one RenderView, the common host is this view's host. If there are
473 // many, check if this one shares the common host of the other
474 // RenderViews. It's ok to not detect some cases where the RenderViews share a
475 // common host. This information is only used for producing custom histograms.
476 if (view_count == 1)
477 SetCommonHost(host);
478 else if (host != common_host_)
479 SetCommonHost(std::string());
480}
481
482std::string RenderThreadImpl::HistogramCustomizer::ConvertToCustomHistogramName(
483 const char* histogram_name) const {
484 std::string name(histogram_name);
485 if (!common_host_histogram_suffix_.empty() &&
486 custom_histograms_.find(name) != custom_histograms_.end())
487 name += common_host_histogram_suffix_;
488 return name;
489}
490
491void RenderThreadImpl::HistogramCustomizer::SetCommonHost(
492 const std::string& host) {
493 if (host != common_host_) {
494 common_host_ = host;
495 common_host_histogram_suffix_ = HostToCustomHistogramSuffix(host);
Blink Reformat1c4d759e2017-04-09 16:34:54496 blink::MainThreadIsolate()->SetCreateHistogramFunction(CreateHistogram);
[email protected]b2d98762012-09-03 17:04:06497 }
498}
499
hablichd6a4f122015-10-28 11:34:49500std::string RenderThreadImpl::HistogramCustomizer::HostToCustomHistogramSuffix(
501 const std::string& host) {
502 if (host == "mail.google.com")
503 return ".gmail";
504 if (host == "docs.google.com" || host == "drive.google.com")
505 return ".docs";
506 if (host == "plus.google.com")
507 return ".plus";
508 if (host == "inbox.google.com")
509 return ".inbox";
hpayerdd4def052016-02-06 19:06:10510 if (host == "calendar.google.com")
511 return ".calendar";
hablichd6a4f122015-10-28 11:34:49512 if (host == "www.youtube.com")
513 return ".youtube";
514 if (IsAlexaTop10NonGoogleSite(host))
515 return ".top10";
516
517 return std::string();
518}
519
520bool RenderThreadImpl::HistogramCustomizer::IsAlexaTop10NonGoogleSite(
521 const std::string& host) {
522 // The Top10 sites have different TLD and/or subdomains depending on the
523 // localization.
524 if (host == "sina.com.cn")
525 return true;
526
527 std::string sanitized_host =
528 net::registry_controlled_domains::GetDomainAndRegistry(
529 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
530
531 if (sanitized_host == "facebook.com")
532 return true;
533 if (sanitized_host == "baidu.com")
534 return true;
535 if (sanitized_host == "qq.com")
536 return true;
537 if (sanitized_host == "twitter.com")
538 return true;
539 if (sanitized_host == "taobao.com")
540 return true;
541 if (sanitized_host == "live.com")
542 return true;
543
544 if (!sanitized_host.empty()) {
545 std::vector<base::StringPiece> host_tokens = base::SplitStringPiece(
546 sanitized_host, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
547
548 if (host_tokens.size() >= 2) {
549 if ((host_tokens[0] == "yahoo") || (host_tokens[0] == "amazon") ||
550 (host_tokens[0] == "wikipedia")) {
551 return true;
552 }
553 }
554 }
555 return false;
556}
557
leon.han2c0f9f12015-07-11 02:01:19558// static
559RenderThreadImpl* RenderThreadImpl::Create(
560 const InProcessChildThreadParams& params) {
Dmitry Skibaa2476cb92017-12-15 23:44:39561 TRACE_EVENT0("startup", "RenderThreadImpl::Create");
skyostil529caa292016-08-10 17:44:51562 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler =
563 blink::scheduler::RendererScheduler::Create();
jam75c44222016-03-23 05:34:24564 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter;
565 return new RenderThreadImpl(
566 params, std::move(renderer_scheduler), test_task_counter);
leon.han2c0f9f12015-07-11 02:01:19567}
568
569// static
570RenderThreadImpl* RenderThreadImpl::Create(
dchengcedca5612016-04-09 01:40:15571 std::unique_ptr<base::MessageLoop> main_message_loop,
skyostil529caa292016-08-10 17:44:51572 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler) {
Dmitry Skibaa2476cb92017-12-15 23:44:39573 TRACE_EVENT0("startup", "RenderThreadImpl::Create");
dcheng07945f632015-12-26 07:59:32574 return new RenderThreadImpl(std::move(main_message_loop),
575 std::move(renderer_scheduler));
leon.han2c0f9f12015-07-11 02:01:19576}
577
rockot5c478a72016-09-28 23:14:18578// static
[email protected]f1a29a02011-10-06 23:08:44579RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06580 return lazy_tls.Pointer()->Get();
581}
582
rockot5c478a72016-09-28 23:14:18583// static
584mojom::RenderMessageFilter* RenderThreadImpl::current_render_message_filter() {
585 if (g_render_message_filter_for_testing)
586 return g_render_message_filter_for_testing;
587 DCHECK(current());
588 return current()->render_message_filter();
589}
590
591// static
olegmax045f7fb12017-05-19 07:58:55592RendererBlinkPlatformImpl* RenderThreadImpl::current_blink_platform_impl() {
593 if (g_current_blink_platform_impl_for_testing)
594 return g_current_blink_platform_impl_for_testing;
595 DCHECK(current());
596 return current()->blink_platform_impl();
597}
598
599// static
rockot5c478a72016-09-28 23:14:18600void RenderThreadImpl::SetRenderMessageFilterForTesting(
601 mojom::RenderMessageFilter* render_message_filter) {
602 g_render_message_filter_for_testing = render_message_filter;
603}
604
olegmax045f7fb12017-05-19 07:58:55605// static
606void RenderThreadImpl::SetRendererBlinkPlatformImplForTesting(
607 RendererBlinkPlatformImpl* blink_platform_impl) {
608 g_current_blink_platform_impl_for_testing = blink_platform_impl;
609}
610
John Abd-El-Malekd4882642017-12-04 21:45:19611// static
612scoped_refptr<base::SingleThreadTaskRunner>
Hajime Hoshi20578c62018-01-12 09:43:26613RenderThreadImpl::DeprecatedGetMainTaskRunner() {
John Abd-El-Malekd4882642017-12-04 21:45:19614 return g_main_task_runner.Get();
615}
616
staraz067f58242016-11-07 21:06:41617// In single-process mode used for debugging, we don't pass a renderer client
618// ID via command line because RenderThreadImpl lives in the same process as
619// the browser
leon.han2c0f9f12015-07-11 02:01:19620RenderThreadImpl::RenderThreadImpl(
621 const InProcessChildThreadParams& params,
skyostil529caa292016-08-10 17:44:51622 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler,
rockot97a86832016-12-10 04:53:06623 const scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue)
Hajime Hoshife11b132017-11-13 10:16:07624 : ChildThreadImpl(
625 Options::Builder()
626 .InBrowserProcess(params)
627 .AutoStartServiceManagerConnection(false)
628 .ConnectToBrowser(true)
629 .IPCTaskRunner(scheduler ? scheduler->IPCTaskRunner() : nullptr)
630 .Build()),
dcheng07945f632015-12-26 07:59:32631 renderer_scheduler_(std::move(scheduler)),
rockot067ca55f2016-09-30 22:00:15632 categorized_worker_pool_(new CategorizedWorkerPool()),
staraz067f58242016-11-07 21:06:41633 renderer_binding_(this),
danakjab9ef4d2017-10-31 23:38:33634 client_id_(1),
635 compositing_mode_watcher_binding_(this) {
jam75c44222016-03-23 05:34:24636 Init(resource_task_queue);
morritac6238ab2015-03-18 01:48:29637}
638
[email protected]42f1d7822009-07-23 18:17:55639// When we run plugins in process, we actually run them on the render thread,
640// which means that we need to make the render thread pump UI events.
haraken53f081d2014-11-11 01:03:40641RenderThreadImpl::RenderThreadImpl(
dchengcedca5612016-04-09 01:40:15642 std::unique_ptr<base::MessageLoop> main_message_loop,
skyostil529caa292016-08-10 17:44:51643 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler)
Hajime Hoshia1626932017-11-30 09:50:59644 : ChildThreadImpl(
645 Options::Builder()
646 .AutoStartServiceManagerConnection(false)
647 .ConnectToBrowser(true)
648 .IPCTaskRunner(scheduler ? scheduler->IPCTaskRunner() : nullptr)
649 .Build()),
dcheng07945f632015-12-26 07:59:32650 renderer_scheduler_(std::move(scheduler)),
651 main_message_loop_(std::move(main_message_loop)),
rockot067ca55f2016-09-30 22:00:15652 categorized_worker_pool_(new CategorizedWorkerPool()),
wjmaclean1d970622017-01-21 22:28:24653 is_scroll_animator_enabled_(false),
danakjab9ef4d2017-10-31 23:38:33654 renderer_binding_(this),
655 compositing_mode_watcher_binding_(this) {
jam75c44222016-03-23 05:34:24656 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter;
staraz067f58242016-11-07 21:06:41657 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
658 switches::kRendererClientId));
659 base::StringToInt(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
660 switches::kRendererClientId),
661 &client_id_);
jam75c44222016-03-23 05:34:24662 Init(test_task_counter);
haraken53f081d2014-11-11 01:03:40663}
664
jam75c44222016-03-23 05:34:24665void RenderThreadImpl::Init(
rockot97a86832016-12-10 04:53:06666 const scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue) {
fdorayf6d86242015-10-08 16:49:53667 TRACE_EVENT0("startup", "RenderThreadImpl::Init");
[email protected]a872ea1f2010-08-11 04:45:33668
ssidb2e3ece2015-02-09 16:02:20669 base::trace_event::TraceLog::GetInstance()->SetThreadSortIndex(
[email protected]91a2aea2013-07-08 23:14:39670 base::PlatformThread::CurrentId(),
671 kTraceEventRendererMainThreadSortIndex);
672
thakis18e426412017-03-15 12:06:37673#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
sievers9dff72052015-11-16 18:35:57674 // On Mac and Android Java UI, the select popups are rendered by the browser.
Blink Reformat1c4d759e2017-04-09 16:34:54675 blink::WebView::SetUseExternalPopupMenus(true);
[email protected]53c607c2011-03-21 23:19:04676#endif
677
[email protected]94f9a0f682009-06-15 18:30:30678 lazy_tls.Pointer()->Set(this);
John Abd-El-Malekd4882642017-12-04 21:45:19679 g_main_task_runner.Get() = base::MessageLoop::current()->task_runner();
[email protected]b3e83de2012-02-07 03:33:28680
[email protected]06c694d2012-02-01 22:26:16681 // Register this object as the main thread.
682 ChildProcess::current()->set_main_thread(this);
683
dalecurtis4a9839a2017-05-04 23:40:47684 metrics::InitializeSingleSampleMetricsFactory(
685 base::BindRepeating(&CreateSingleSampleMetricsProvider,
686 message_loop()->task_runner(), GetConnector()));
687
Sadrul Habib Chowdhuryeeb5a942017-11-28 21:17:23688 gpu_ =
689 ui::Gpu::Create(GetConnector(),
690 switches::IsMusHostingViz() ? ui::mojom::kServiceName
691 : mojom::kBrowserServiceName,
692 GetIOTaskRunner());
penghuange1d86512016-07-08 18:15:00693
Gary Klassenb78686272017-08-11 22:38:13694 viz::mojom::SharedBitmapAllocationNotifierPtr
danakj92429852017-06-29 00:19:03695 shared_bitmap_allocation_notifier_ptr;
Saman Sami9b768542017-07-14 20:42:34696 GetConnector()->BindInterface(
697 mojom::kBrowserServiceName,
danakj92429852017-06-29 00:19:03698 mojo::MakeRequest(&shared_bitmap_allocation_notifier_ptr));
Jeremy Roman04f27c372017-10-27 15:20:55699 shared_bitmap_manager_ = std::make_unique<viz::ClientSharedBitmapManager>(
Gary Klassenb78686272017-08-11 22:38:13700 viz::mojom::ThreadSafeSharedBitmapAllocationNotifierPtr::Create(
danakj92429852017-06-29 00:19:03701 shared_bitmap_allocation_notifier_ptr.PassInterface(),
702 GetChannel()->ipc_task_runner_refptr()));
jcivelli0f21bd52016-12-07 21:21:33703
John Abd-El-Malekf9f86c12017-10-21 07:22:23704 notification_dispatcher_ =
705 new NotificationDispatcher(thread_safe_sender());
706 AddFilter(notification_dispatcher_->GetFilter());
707
Yutaka Hirano99ab0e0a32017-12-25 00:40:32708 resource_dispatcher_.reset(
709 new ResourceDispatcher(message_loop()->task_runner()));
Sasha Bermeister9a3f9da2017-11-29 03:30:28710 quota_dispatcher_.reset(new QuotaDispatcher(message_loop()->task_runner()));
Yuzhu Shenfe5f3a52018-01-11 21:24:13711 url_loader_throttle_provider_ =
712 GetContentClient()->renderer()->CreateURLLoaderThrottleProvider(
713 URLLoaderThrottleProviderType::kFrame);
John Abd-El-Malek6b56ef712017-10-21 22:52:46714
Stuart Langley7b8dc772017-10-31 03:55:20715 auto registry = std::make_unique<service_manager::BinderRegistry>();
Ken Rockot510c5972017-12-05 16:37:04716 InitializeWebKit(resource_task_queue, registry.get());
Makoto Shimazu7af43ee2017-08-25 10:20:40717 blink_initialized_time_ = base::TimeTicks::Now();
jam75c44222016-03-23 05:34:24718
[email protected]31f87132010-04-21 23:36:21719 // In single process the single process is all there is.
Alexander Timin4b6152232017-10-17 20:34:06720 webkit_shared_timer_suspended_ = false;
[email protected]bee16aab2009-08-26 15:55:03721 widget_count_ = 0;
722 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44723 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48724 idle_notifications_to_skip_ = 0;
[email protected]8d86fce2009-02-26 23:37:55725
[email protected]d5b2fdf2013-06-05 09:36:55726 appcache_dispatcher_.reset(
Nicholas Verne07e9c6452017-12-13 04:12:55727 new AppCacheDispatcher(new AppCacheFrontendImpl()));
728 registry->AddInterface(
729 base::BindRepeating(&AppCacheDispatcher::Bind,
730 base::Unretained(appcache_dispatcher())),
Hajime Hoshi20578c62018-01-12 09:43:26731 GetRendererScheduler()->IPCTaskRunner());
[email protected]1910fe82012-05-10 00:04:10732 dom_storage_dispatcher_.reset(new DomStorageDispatcher());
reillyg39fb4662016-11-22 20:27:17733 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
jsbellabadb9b2015-03-23 21:03:44734 main_thread_cache_storage_dispatcher_.reset(
jsbell279efb42015-03-31 17:02:46735 new CacheStorageDispatcher(thread_safe_sender()));
John Abd-El-Malekdcf1d1372017-10-22 06:39:41736 file_system_dispatcher_.reset(new FileSystemDispatcher());
[email protected]dd9241932010-02-24 19:23:13737
[email protected]45048072014-01-14 13:51:29738 vc_manager_.reset(new VideoCaptureImplManager());
[email protected]c9c43a02013-12-17 08:59:54739
fsamuel6c1dfeb2014-12-18 19:21:33740 browser_plugin_manager_.reset(new BrowserPluginManager());
741 AddObserver(browser_plugin_manager_.get());
742
Brett Wilson0748bf412016-11-22 17:55:46743#if BUILDFLAG(ENABLE_WEBRTC)
[email protected]af089972013-01-10 04:04:40744 peer_connection_tracker_.reset(new PeerConnectionTracker());
745 AddObserver(peer_connection_tracker_.get());
746
thestig529ad8a2016-07-08 20:30:12747 p2p_socket_dispatcher_ = new P2PSocketDispatcher(GetIOTaskRunner().get());
[email protected]fc72bb12013-06-02 21:13:46748 AddFilter(p2p_socket_dispatcher_.get());
[email protected]921480f62013-07-20 03:42:57749
ivocadd54f0d2015-12-18 23:17:05750 peer_connection_factory_.reset(
751 new PeerConnectionDependencyFactory(p2p_socket_dispatcher_.get()));
752
skyostil2d3b5bd2015-05-27 15:40:59753 aec_dump_message_filter_ = new AecDumpMessageFilter(
thestig529ad8a2016-07-08 20:30:12754 GetIOTaskRunner(), message_loop()->task_runner());
ivocadd54f0d2015-12-18 23:17:05755
[email protected]da9f30a2014-06-18 19:39:04756 AddFilter(aec_dump_message_filter_.get());
757
Brett Wilson0748bf412016-11-22 17:55:46758#endif // BUILDFLAG(ENABLE_WEBRTC)
Max Morin3d464a42018-01-15 11:17:00759 {
760 scoped_refptr<AudioInputMessageFilter> audio_input_message_filter;
761 if (!base::FeatureList::IsEnabled(
762 features::kUseMojoAudioInputStreamFactory)) {
763 // In case we shouldn't use mojo factories, we need to create an
764 // AudioInputMessageFilter which |audio_input_ipc_factory_| can use for
765 // IPC.
766 audio_input_message_filter =
767 base::MakeRefCounted<AudioInputMessageFilter>(GetIOTaskRunner());
768 AddFilter(audio_input_message_filter.get());
769 }
[email protected]e25f4d72011-06-08 20:58:46770
Max Morin3d464a42018-01-15 11:17:00771 audio_input_ipc_factory_.emplace(std::move(audio_input_message_filter),
772 message_loop()->task_runner(),
773 GetIOTaskRunner());
Max Morind4c4887d2017-06-08 07:41:34774 }
775
Max Morin3d464a42018-01-15 11:17:00776 {
777 scoped_refptr<AudioMessageFilter> audio_output_message_filter;
778 if (!base::FeatureList::IsEnabled(
779 features::kUseMojoAudioOutputStreamFactory)) {
780 // In case we shouldn't use mojo factories, we need to create an
781 // AudioMessageFilter which |audio_output_ipc_factory_| can use for IPC.
782 audio_output_message_filter =
783 base::MakeRefCounted<AudioMessageFilter>(GetIOTaskRunner());
784 AddFilter(audio_output_message_filter.get());
785 }
786
787 audio_output_ipc_factory_.emplace(std::move(audio_output_message_filter),
788 GetIOTaskRunner());
789 }
[email protected]f7eb0a392011-07-12 10:19:51790
thestig529ad8a2016-07-08 20:30:12791 midi_message_filter_ = new MidiMessageFilter(GetIOTaskRunner());
[email protected]a9875152013-06-22 04:03:03792 AddFilter(midi_message_filter_.get());
793
jsbellabadb9b2015-03-23 21:03:44794 AddFilter((new CacheStorageMessageFilter(thread_safe_sender()))->GetFilter());
795
kinuko5af4ffe2015-06-09 03:38:46796 AddFilter((new ServiceWorkerContextMessageFilter())->GetFilter());
[email protected]ddbb53342014-01-06 10:59:47797
Darin Fisherbcc33632017-09-26 00:57:16798// Register exported services:
799
sadrul943e3b32016-08-04 18:22:59800#if defined(USE_AURA)
Scott Violet30daff62017-12-01 07:04:52801 if (IsRunningWithMus())
bend32292b2016-10-07 00:21:58802 CreateRenderWidgetWindowTreeClientFactory(GetServiceManagerConnection());
rockotcef38272016-07-15 22:47:47803#endif
804
Stuart Langley7b8dc772017-10-31 03:55:20805 registry->AddInterface(base::Bind(&SharedWorkerFactoryImpl::Create),
806 base::ThreadTaskRunnerHandle::Get());
807 GetServiceManagerConnection()->AddConnectionFilter(
808 std::make_unique<SimpleConnectionFilter>(std::move(registry)));
Stuart Langley6771c902017-09-01 11:23:30809
Darin Fisherbcc33632017-09-26 00:57:16810 {
811 auto registry_with_source_info =
Jeremy Roman04f27c372017-10-27 15:20:55812 std::make_unique<service_manager::BinderRegistryWithArgs<
Darin Fisherbcc33632017-09-26 00:57:16813 const service_manager::BindSourceInfo&>>();
814 registry_with_source_info->AddInterface(
815 base::Bind(&CreateFrameFactory), base::ThreadTaskRunnerHandle::Get());
816 GetServiceManagerConnection()->AddConnectionFilter(
Jeremy Roman04f27c372017-10-27 15:20:55817 std::make_unique<SimpleConnectionFilterWithSourceInfo>(
Darin Fisherbcc33632017-09-26 00:57:16818 std::move(registry_with_source_info)));
819 }
rockotcef38272016-07-15 22:47:47820
[email protected]e9ff79c2012-10-19 21:31:26821 GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24822
benbd9dc802017-04-19 01:37:43823 StartServiceManagerConnection();
824
rockot067ca55f2016-09-30 22:00:15825 GetAssociatedInterfaceRegistry()->AddInterface(
826 base::Bind(&RenderThreadImpl::OnRendererInterfaceRequest,
827 base::Unretained(this)));
828
[email protected]b48c53ad2014-02-05 21:59:18829 InitSkiaEventTracer();
ssid59c969162015-07-28 13:02:58830 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40831 skia::SkiaMemoryDumpProvider::GetInstance(), "Skia", nullptr);
[email protected]b48c53ad2014-02-05 21:59:18832
avi83883c82014-12-23 00:08:49833 const base::CommandLine& command_line =
834 *base::CommandLine::ForCurrentProcess();
[email protected]e54ab492012-06-12 19:40:01835
ochangdd89a1e2016-06-08 16:39:01836#if defined(ENABLE_IPC_FUZZER)
837 if (command_line.HasSwitch(switches::kIpcDumpDirectory)) {
838 base::FilePath dump_directory =
839 command_line.GetSwitchValuePath(switches::kIpcDumpDirectory);
840 IPC::ChannelProxy::OutgoingMessageFilter* filter =
841 LoadExternalIPCDumper(dump_directory);
842 GetChannel()->set_outgoing_message_filter(filter);
843 }
844#endif
845
jbroman0d302162015-09-05 05:46:23846 cc::SetClientNameForMetrics("Renderer");
loysoa6edaaff2015-05-25 03:26:44847
loyso65c93c602015-08-11 05:15:57848 is_threaded_animation_enabled_ =
849 !command_line.HasSwitch(cc::switches::kDisableThreadedAnimation);
850
reveman91a0a872014-11-04 03:40:32851 is_zero_copy_enabled_ = command_line.HasSwitch(switches::kEnableZeroCopy);
ericrk1d17f752015-10-20 03:03:07852 is_partial_raster_enabled_ =
dongseong.hwang23db47f2016-03-08 07:50:02853 !command_line.HasSwitch(switches::kDisablePartialRaster);
ccameronc7fcd132015-11-03 20:14:31854 is_gpu_memory_buffer_compositor_resources_enabled_ = command_line.HasSwitch(
855 switches::kEnableGpuMemoryBufferCompositorResources);
[email protected]a23530d2014-03-11 06:04:14856
Greg Kerra7b943b2017-07-24 23:17:17857// On macOS this value is adjusted in `UpdateScrollbarTheme()`,
858// but the system default is true.
859#if defined(OS_MACOSX)
860 is_elastic_overscroll_enabled_ = true;
ccamerona7644752014-12-30 01:16:31861#else
862 is_elastic_overscroll_enabled_ = false;
863#endif
864
[email protected]a23530d2014-03-11 06:04:14865 if (command_line.HasSwitch(switches::kDisableLCDText)) {
866 is_lcd_text_enabled_ = false;
867 } else if (command_line.HasSwitch(switches::kEnableLCDText)) {
868 is_lcd_text_enabled_ = true;
869 } else {
870#if defined(OS_ANDROID)
871 is_lcd_text_enabled_ = false;
872#else
873 is_lcd_text_enabled_ = true;
874#endif
875 }
876
danakjab9ef4d2017-10-31 23:38:33877 if (command_line.HasSwitch(switches::kDisableGpuCompositing))
878 is_gpu_compositing_disabled_ = true;
879
[email protected]b8d82c22014-03-31 20:12:46880 is_gpu_rasterization_forced_ =
881 command_line.HasSwitch(switches::kForceGpuRasterization);
[email protected]a23530d2014-03-11 06:04:14882
senorblancob60ba952015-01-27 19:12:36883 if (command_line.HasSwitch(switches::kGpuRasterizationMSAASampleCount)) {
884 std::string string_value = command_line.GetSwitchValueASCII(
885 switches::kGpuRasterizationMSAASampleCount);
886 bool parsed_msaa_sample_count =
887 base::StringToInt(string_value, &gpu_rasterization_msaa_sample_count_);
888 DCHECK(parsed_msaa_sample_count) << string_value;
889 DCHECK_GE(gpu_rasterization_msaa_sample_count_, 0);
890 } else {
senorblanco2a5b0e12015-08-14 21:55:37891 gpu_rasterization_msaa_sample_count_ = -1;
senorblancob60ba952015-01-27 19:12:36892 }
893
[email protected]b242b142014-05-07 14:48:49894 if (command_line.HasSwitch(switches::kDisableDistanceFieldText)) {
895 is_distance_field_text_enabled_ = false;
896 } else if (command_line.HasSwitch(switches::kEnableDistanceFieldText)) {
897 is_distance_field_text_enabled_ = true;
898 } else {
899 is_distance_field_text_enabled_ = false;
900 }
901
Khushalfdacdc92017-09-22 22:40:52902 WebRuntimeFeatures::EnableCompositorImageAnimations(
903 command_line.HasSwitch(switches::kEnableCompositorImageAnimations));
904
[email protected]433df472012-03-07 20:33:39905 // Note that under Linux, the media library will normally already have
906 // been initialized by the Zygote before this instance became a Renderer.
chcunninghamfd11b3c2015-06-09 02:09:42907 media::InitializeMediaLibrary();
[email protected]433df472012-03-07 20:33:39908
dalecurtis88af3932016-02-20 00:12:20909#if defined(OS_ANDROID)
910 if (!command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode) &&
911 media::MediaCodecUtil::IsMediaCodecAvailable()) {
912 media::EnablePlatformDecoderSupport();
913 }
914#endif
915
bashi296ebda2017-03-28 03:27:09916 memory_pressure_listener_.reset(new base::MemoryPressureListener(
917 base::Bind(&RenderThreadImpl::OnMemoryPressure, base::Unretained(this)),
918 base::Bind(&RenderThreadImpl::OnSyncMemoryPressure,
919 base::Unretained(this))));
920
921 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) {
922 // Disable MemoryPressureListener when memory coordinator is enabled.
923 base::MemoryPressureListener::SetNotificationsSuppressed(true);
924
925 // TODO(bashi): Revisit how to manage the lifetime of
926 // ChildMemoryCoordinatorImpl.
927 // https://codereview.chromium.org/2094583002/#msg52
928 mojom::MemoryCoordinatorHandlePtr parent_coordinator;
929 GetConnector()->BindInterface(mojom::kBrowserServiceName,
930 mojo::MakeRequest(&parent_coordinator));
931 memory_coordinator_ = CreateChildMemoryCoordinator(
932 std::move(parent_coordinator), this);
933 }
934
danakj16275d4c2015-06-11 19:23:51935 int num_raster_threads = 0;
936 std::string string_value =
937 command_line.GetSwitchValueASCII(switches::kNumRasterThreads);
938 bool parsed_num_raster_threads =
939 base::StringToInt(string_value, &num_raster_threads);
940 DCHECK(parsed_num_raster_threads) << string_value;
941 DCHECK_GT(num_raster_threads, 0);
vmiura78b69282015-02-14 00:01:17942
prashant.nfad657e2016-06-01 07:52:17943 categorized_worker_pool_->Start(num_raster_threads);
[email protected]1e1d1e12014-01-17 16:14:29944
penghuang342762b2016-12-02 21:04:58945 discardable_memory::mojom::DiscardableSharedMemoryManagerPtr manager_ptr;
Sadrul Habib Chowdhury1dbe3b42017-11-18 02:27:43946 if (IsRunningWithMus()) {
penghuangd6843e42016-12-17 13:57:20947#if defined(USE_AURA)
benbd3c2482017-01-07 05:48:21948 GetServiceManagerConnection()->GetConnector()->BindInterface(
penghuangd6843e42016-12-17 13:57:20949 ui::mojom::kServiceName, &manager_ptr);
950#else
951 NOTREACHED();
952#endif
953 } else {
ben649b3edd2017-03-23 00:32:02954 ChildThread::Get()->GetConnector()->BindInterface(
955 mojom::kBrowserServiceName, mojo::MakeRequest(&manager_ptr));
penghuangd6843e42016-12-17 13:57:20956 }
957
Jeremy Roman04f27c372017-10-27 15:20:55958 discardable_shared_memory_manager_ = std::make_unique<
penghuang342762b2016-12-02 21:04:58959 discardable_memory::ClientDiscardableSharedMemoryManager>(
960 std::move(manager_ptr), GetIOTaskRunner());
961
boliu9760e212015-06-23 22:49:06962 // TODO(boliu): In single process, browser main loop should set up the
963 // discardable memory manager, and should skip this if kSingleProcess.
964 // See crbug.com/503724.
965 base::DiscardableMemoryAllocator::SetInstance(
penghuang342762b2016-12-02 21:04:58966 discardable_shared_memory_manager_.get());
revemancb5a66af2014-10-25 00:34:39967
ben649b3edd2017-03-23 00:32:02968 GetConnector()->BindInterface(mojom::kBrowserServiceName,
969 mojo::MakeRequest(&storage_partition_service_));
hajimehoshi69093272016-05-13 08:30:58970
reveman7b97c322016-09-20 02:10:58971#if defined(OS_LINUX)
Nicholas Verne84dc55e2017-11-23 01:02:38972 render_message_filter()->SetThreadPriority(
973 ChildProcess::current()->io_thread_id(), base::ThreadPriority::DISPLAY);
974 render_message_filter()->SetThreadPriority(
lukasza6710dbd2017-04-07 20:58:03975 categorized_worker_pool_->background_worker_thread_id(),
reveman7b97c322016-09-20 02:10:58976 base::ThreadPriority::BACKGROUND);
977#endif
978
tasakbb0640b2017-05-15 09:02:26979 process_foregrounded_count_ = 0;
tasakb95dbb50c2017-02-08 18:07:50980 needs_to_record_first_active_paint_ = false;
Takashi SAKAMOTO870f6262017-08-22 04:08:27981 was_backgrounded_time_ = base::TimeTicks::Min();
hajimehoshi7bb39582016-10-12 04:30:57982
983 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this);
fdoray50a38342016-11-21 20:46:04984
985 // If this renderer doesn't run inside the browser process, enable
986 // SequencedWorkerPool. Otherwise, it should already have been enabled.
987 // TODO(fdoray): Remove this once the SequencedWorkerPool to TaskScheduler
988 // redirection experiment concludes https://crbug.com/622400.
989 if (!command_line.HasSwitch(switches::kSingleProcess))
990 base::SequencedWorkerPool::EnableForProcess();
samans2040988b2017-04-11 23:58:35991
992 GetConnector()->BindInterface(mojom::kBrowserServiceName,
993 mojo::MakeRequest(&frame_sink_provider_));
danakjab9ef4d2017-10-31 23:38:33994
995 if (!is_gpu_compositing_disabled_) {
996 GetConnector()->BindInterface(
997 mojom::kBrowserServiceName,
998 mojo::MakeRequest(&compositing_mode_reporter_));
999
1000 // Make |this| a CompositingModeWatcher for the
1001 // |compositing_mode_reporter_|.
1002 viz::mojom::CompositingModeWatcherPtr watcher_ptr;
1003 compositing_mode_watcher_binding_.Bind(mojo::MakeRequest(&watcher_ptr));
1004 compositing_mode_reporter_->AddCompositingModeWatcher(
1005 std::move(watcher_ptr));
1006 }
initial.commit09911bf2008-07-26 23:55:291007}
1008
John Abd-El-Malekd4882642017-12-04 21:45:191009RenderThreadImpl::~RenderThreadImpl() {
1010 g_main_task_runner.Get() = nullptr;
1011}
[email protected]ce79d8512013-04-22 22:44:411012
Nicholas Verne5420564d2017-10-27 07:37:541013void RenderThreadImpl::Shutdown() {
John Abd-El-Malekdcf1d1372017-10-22 06:39:411014 ChildThreadImpl::Shutdown();
John Abd-El-Malekf75a3192017-10-22 14:23:201015 quota_dispatcher_.reset();
John Abd-El-Malekdcf1d1372017-10-22 06:39:411016 file_system_dispatcher_.reset();
1017 WebFileSystemImpl::DeleteThreadSpecificInstance();
harakenbbfdd9f02017-01-12 07:14:041018 // In a multi-process mode, we immediately exit the renderer.
1019 // Historically we had a graceful shutdown sequence here but it was
1020 // 1) a waste of performance and 2) a source of lots of complicated
1021 // crashes caused by shutdown ordering. Immediate exit eliminates
1022 // those problems.
cbrunia3f655b2017-03-20 11:36:411023
1024 // Give the V8 isolate a chance to dump internal stats useful for performance
1025 // evaluation and debugging.
Blink Reformat1c4d759e2017-04-09 16:34:541026 blink::MainThreadIsolate()->DumpAndResetStats();
cbrunia3f655b2017-03-20 11:36:411027
Adithya Srinivasandf421e82017-06-01 14:36:111028 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1029 switches::kDumpBlinkRuntimeCallStats))
1030 blink::LogRuntimeCallStats();
1031
harakenbbfdd9f02017-01-12 07:14:041032 // In a single-process mode, we cannot call _exit(0) in Shutdown() because
1033 // it will exit the process before the browser side is ready to exit.
1034 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
1035 switches::kSingleProcess))
haraken940efb92017-02-08 05:58:151036 base::Process::TerminateCurrentProcessImmediately(0);
harakenbbfdd9f02017-01-12 07:14:041037}
[email protected]1223d6ef2011-03-28 16:47:501038
harakenbbfdd9f02017-01-12 07:14:041039bool RenderThreadImpl::ShouldBeDestroyed() {
1040 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
1041 switches::kSingleProcess));
1042 // In a single-process mode, it is unsafe to destruct this renderer thread
1043 // because we haven't run the shutdown sequence. Hence we leak the render
1044 // thread.
1045 //
1046 // In this case, we also need to disable at-exit callbacks because some of
1047 // the at-exit callbacks are expected to run after the renderer thread
1048 // has been destructed.
1049 base::AtExitManager::DisableAllAtExitManagers();
1050 return false;
initial.commit09911bf2008-07-26 23:55:291051}
1052
[email protected]f1a29a02011-10-06 23:08:441053bool RenderThreadImpl::Send(IPC::Message* msg) {
changwan6ed4d432016-05-19 22:03:541054 // There are cases where we want to pump asynchronous messages while waiting
1055 // synchronously for the replies to the message to be sent here. However, this
1056 // may create an opportunity for re-entrancy into WebKit and other subsystems,
1057 // so we need to take care to disable callbacks, timers, and pending network
1058 // loads that could trigger such callbacks.
[email protected]38b592902011-04-16 02:08:421059 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:501060 if (msg->is_sync()) {
1061 if (msg->is_caller_pumping_messages()) {
1062 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:501063 }
[email protected]c1f50aa2010-02-18 03:46:571064 }
1065
Yoland Yan25439ba2017-10-11 19:45:551066 std::unique_ptr<blink::scheduler::RendererScheduler::RendererPauseHandle>
1067 renderer_paused_handle;
1068
[email protected]c1f50aa2010-02-18 03:46:571069 if (pumping_events) {
Yoland Yan25439ba2017-10-11 19:45:551070 renderer_paused_handle = renderer_scheduler_->PauseRenderer();
Blink Reformat1c4d759e2017-04-09 16:34:541071 WebView::WillEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:571072 }
1073
jam8a021512a2015-02-03 18:16:081074 bool rv = ChildThreadImpl::Send(msg);
[email protected]c1f50aa2010-02-18 03:46:571075
Yoland Yan25439ba2017-10-11 19:45:551076 if (pumping_events)
Blink Reformat1c4d759e2017-04-09 16:34:541077 WebView::DidExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:571078
1079 return rv;
1080}
1081
[email protected]f1a29a02011-10-06 23:08:441082IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:061083 return channel();
1084}
1085
[email protected]f1a29a02011-10-06 23:08:441086std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:061087 // The browser process should have passed the locale to the renderer via the
[email protected]dfd53652012-10-25 00:20:021088 // --lang command line flag.
avi83883c82014-12-23 00:08:491089 const base::CommandLine& parsed_command_line =
1090 *base::CommandLine::ForCurrentProcess();
[email protected]526476902011-10-06 20:34:061091 const std::string& lang =
1092 parsed_command_line.GetSwitchValueASCII(switches::kLang);
[email protected]dfd53652012-10-25 00:20:021093 DCHECK(!lang.empty());
[email protected]526476902011-10-06 20:34:061094 return lang;
1095}
1096
[email protected]07bb6332012-01-21 01:07:571097IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
1098 return sync_message_filter();
1099}
1100
avi1023d012015-12-25 02:39:141101void RenderThreadImpl::AddRoute(int32_t routing_id, IPC::Listener* listener) {
jam8a021512a2015-02-03 18:16:081102 ChildThreadImpl::GetRouter()->AddRoute(routing_id, listener);
ben76f52b242016-06-18 05:42:481103 auto it = pending_frame_creates_.find(routing_id);
1104 if (it == pending_frame_creates_.end())
rockotf8fdd9b2015-12-16 22:22:351105 return;
1106
1107 RenderFrameImpl* frame = RenderFrameImpl::FromRoutingID(routing_id);
1108 if (!frame)
1109 return;
1110
ben76f52b242016-06-18 05:42:481111 scoped_refptr<PendingFrameCreate> create(it->second);
Balazs Engedyba034e72017-10-27 22:26:281112 frame->BindFrame(it->second->browser_info(), it->second->TakeFrameRequest());
ben76f52b242016-06-18 05:42:481113 pending_frame_creates_.erase(it);
[email protected]c1f50aa2010-02-18 03:46:571114}
1115
avi1023d012015-12-25 02:39:141116void RenderThreadImpl::RemoveRoute(int32_t routing_id) {
jam8a021512a2015-02-03 18:16:081117 ChildThreadImpl::GetRouter()->RemoveRoute(routing_id);
[email protected]c1f50aa2010-02-18 03:46:571118}
[email protected]0ec90d522014-03-12 16:28:191119
ben76f52b242016-06-18 05:42:481120void RenderThreadImpl::RegisterPendingFrameCreate(
benf28ce882017-05-02 16:15:491121 const service_manager::BindSourceInfo& browser_info,
rockotf8fdd9b2015-12-16 22:22:351122 int routing_id,
Balazs Engedyba034e72017-10-27 22:26:281123 mojom::FrameRequest frame_request) {
ben76f52b242016-06-18 05:42:481124 std::pair<PendingFrameCreateMap::iterator, bool> result =
1125 pending_frame_creates_.insert(std::make_pair(
kylechar96f3eba2017-09-25 20:23:561126 routing_id, base::MakeRefCounted<PendingFrameCreate>(
Balazs Engedyba034e72017-10-27 22:26:281127 browser_info, routing_id, std::move(frame_request))));
rockotf8fdd9b2015-12-16 22:22:351128 CHECK(result.second) << "Inserting a duplicate item.";
1129}
1130
leon.han06e55662016-03-26 17:19:421131mojom::StoragePartitionService* RenderThreadImpl::GetStoragePartitionService() {
jamc912ca32016-02-24 20:17:311132 return storage_partition_service_.get();
1133}
1134
jam188f19f2017-06-07 03:56:241135mojom::RendererHost* RenderThreadImpl::GetRendererHost() {
Nicholas Verne06de8122017-11-16 04:47:381136 if (!renderer_host_) {
Joel Hockey3146d142017-11-15 22:27:401137 GetChannel()->GetRemoteAssociatedInterface(&renderer_host_);
Nicholas Verne06de8122017-11-16 04:47:381138 }
jam188f19f2017-06-07 03:56:241139 return renderer_host_.get();
1140}
1141
[email protected]77fc9b92011-10-15 16:20:371142int RenderThreadImpl::GenerateRoutingID() {
rockote261d2112016-09-21 22:22:231143 int32_t routing_id = MSG_ROUTING_NONE;
1144 render_message_filter()->GenerateRoutingID(&routing_id);
[email protected]77fc9b92011-10-15 16:20:371145 return routing_id;
1146}
1147
[email protected]74122042014-04-25 00:07:301148void RenderThreadImpl::AddFilter(IPC::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:551149 channel()->AddFilter(filter);
1150}
1151
[email protected]74122042014-04-25 00:07:301152void RenderThreadImpl::RemoveFilter(IPC::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:551153 channel()->RemoveFilter(filter);
1154}
1155
tyoshino832a58a2016-04-18 08:14:081156void RenderThreadImpl::AddObserver(RenderThreadObserver* observer) {
[email protected]526476902011-10-06 20:34:061157 observers_.AddObserver(observer);
nigeltao7cd8d5582016-12-12 06:05:281158 observer->RegisterMojoInterfaces(&associated_interfaces_);
[email protected]526476902011-10-06 20:34:061159}
1160
tyoshino832a58a2016-04-18 08:14:081161void RenderThreadImpl::RemoveObserver(RenderThreadObserver* observer) {
nigeltao7cd8d5582016-12-12 06:05:281162 observer->UnregisterMojoInterfaces(&associated_interfaces_);
[email protected]526476902011-10-06 20:34:061163 observers_.RemoveObserver(observer);
1164}
1165
[email protected]359dfa32011-10-12 01:10:151166void RenderThreadImpl::SetResourceDispatcherDelegate(
[email protected]e9ff79c2012-10-19 21:31:261167 ResourceDispatcherDelegate* delegate) {
John Abd-El-Malek6b56ef712017-10-21 22:52:461168 resource_dispatcher_->set_delegate(delegate);
[email protected]359dfa32011-10-12 01:10:151169}
1170
vollick1050cc62015-12-03 07:04:541171void RenderThreadImpl::InitializeCompositorThread() {
boliu66024c62016-04-20 04:00:411172 base::Thread::Options options;
vollick1050cc62015-12-03 07:04:541173#if defined(OS_ANDROID)
boliu66024c62016-04-20 04:00:411174 options.priority = base::ThreadPriority::DISPLAY;
vollick1050cc62015-12-03 07:04:541175#endif
altiminc7369bf2017-04-11 14:29:151176 compositor_thread_ =
1177 blink::scheduler::WebThreadBase::CreateCompositorThread(options);
boliu66024c62016-04-20 04:00:411178 blink_platform_impl_->SetCompositorThread(compositor_thread_.get());
Siddhartha5ac59e62018-01-03 00:08:141179 compositor_task_runner_ = compositor_thread_->GetSingleThreadTaskRunner();
boliu66024c62016-04-20 04:00:411180 compositor_task_runner_->PostTask(
1181 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061182 base::BindOnce(base::IgnoreResult(&ThreadRestrictions::SetIOAllowed),
1183 false));
reveman7b97c322016-09-20 02:10:581184#if defined(OS_LINUX)
Nicholas Verne84dc55e2017-11-23 01:02:381185 render_message_filter()->SetThreadPriority(compositor_thread_->ThreadId(),
1186 base::ThreadPriority::DISPLAY);
reveman7b97c322016-09-20 02:10:581187#endif
vollick1050cc62015-12-03 07:04:541188
Dave Tapuska525eb15e2017-08-17 21:05:501189 if (!base::FeatureList::IsEnabled(features::kMojoInputMessages)) {
1190 SynchronousInputHandlerProxyClient* synchronous_input_handler_proxy_client =
1191 nullptr;
vollick1050cc62015-12-03 07:04:541192#if defined(OS_ANDROID)
Dave Tapuska525eb15e2017-08-17 21:05:501193 if (GetContentClient()->UsingSynchronousCompositing()) {
1194 sync_compositor_message_filter_ =
1195 new SynchronousCompositorFilter(compositor_task_runner_);
1196 AddFilter(sync_compositor_message_filter_.get());
1197 synchronous_input_handler_proxy_client =
1198 sync_compositor_message_filter_.get();
1199 }
vollick1050cc62015-12-03 07:04:541200#endif
Dave Tapuska525eb15e2017-08-17 21:05:501201 scoped_refptr<InputEventFilter> compositor_input_event_filter(
1202 new InputEventFilter(main_input_callback_.callback(),
1203 main_thread_compositor_task_runner_,
1204 compositor_task_runner_));
1205 InputHandlerManagerClient* input_handler_manager_client =
1206 compositor_input_event_filter.get();
1207 input_event_filter_ = compositor_input_event_filter;
1208 input_handler_manager_.reset(new InputHandlerManager(
1209 compositor_task_runner_, input_handler_manager_client,
1210 synchronous_input_handler_proxy_client, renderer_scheduler_.get()));
1211 } else {
1212#if defined(OS_ANDROID)
1213 // TODO(dtapuska): Implement a mojo channel for the synchronous
1214 // compositor android webview uses.
1215 DCHECK(!GetContentClient()->UsingSynchronousCompositing());
1216#endif
1217 }
vollick1050cc62015-12-03 07:04:541218}
1219
jam75c44222016-03-23 05:34:241220void RenderThreadImpl::InitializeWebKit(
Stuart Langley7b8dc772017-10-31 03:55:201221 const scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue,
Ken Rockot510c5972017-12-05 16:37:041222 service_manager::BinderRegistry* registry) {
jam75c44222016-03-23 05:34:241223 DCHECK(!blink_platform_impl_);
[email protected]d1b8fccc2011-08-03 01:20:131224
avi83883c82014-12-23 00:08:491225 const base::CommandLine& command_line =
1226 *base::CommandLine::ForCurrentProcess();
chunyang.daibe874c52014-11-14 06:45:051227
1228#ifdef ENABLE_VTUNE_JIT_INTERFACE
1229 if (command_line.HasSwitch(switches::kEnableVtune))
1230 gin::Debug::SetJitCodeEventHandler(vTune::GetVtuneCodeEventHandler());
1231#endif
1232
Reilly Grant9944f162017-08-29 01:22:411233 blink_platform_impl_.reset(
1234 new RendererBlinkPlatformImpl(renderer_scheduler_.get()));
rmcilroy4073ae12015-01-08 13:08:101235 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
hlopko852ffcf2016-09-16 16:50:591236 GetContentClient()
1237 ->renderer()
1238 ->SetRuntimeFeaturesDefaultsBeforeBlinkInitialization();
Ken Rockot510c5972017-12-05 16:37:041239 blink::Initialize(blink_platform_impl_.get(), registry);
[email protected]d1b8fccc2011-08-03 01:20:131240
Blink Reformat1c4d759e2017-04-09 16:34:541241 v8::Isolate* isolate = blink::MainThreadIsolate();
[email protected]4b5340282014-07-08 11:37:341242 isolate->SetCreateHistogramFunction(CreateHistogram);
1243 isolate->SetAddHistogramSampleFunction(AddHistogramSample);
skyostila37c2a72016-06-29 17:30:071244 renderer_scheduler_->SetRAILModeObserver(this);
[email protected]4b5340282014-07-08 11:37:341245
rmcilroy321f924d2014-11-06 00:56:001246 main_thread_compositor_task_runner_ =
danakj6e3bf8012014-12-16 18:27:531247 renderer_scheduler_->CompositorTaskRunner();
skyostilc30aa402014-10-10 13:49:091248
jdduke691dd572014-12-02 20:47:521249 main_input_callback_.Reset(
1250 base::Bind(base::IgnoreResult(&RenderThreadImpl::OnMessageReceived),
1251 base::Unretained(this)));
1252
jam75c44222016-03-23 05:34:241253 scoped_refptr<base::SingleThreadTaskRunner> resource_task_queue2;
1254 if (resource_task_queue) {
1255 resource_task_queue2 = resource_task_queue;
1256 } else {
1257 resource_task_queue2 = renderer_scheduler_->LoadingTaskRunner();
1258 }
Yutaka Hirano99ab0e0a32017-12-25 00:40:321259 // The ResourceDispatcher needs to use the same queue to ensure tasks are
1260 // executed in the expected order.
John Abd-El-Malek6b56ef712017-10-21 22:52:461261 resource_dispatcher_->SetThreadTaskRunner(resource_task_queue2);
jam75c44222016-03-23 05:34:241262
khushalsagard7178e42017-01-20 01:31:491263 if (!command_line.HasSwitch(switches::kDisableThreadedCompositing))
vollick1050cc62015-12-03 07:04:541264 InitializeCompositorThread();
[email protected]a9fb30aa2011-10-06 06:58:461265
jdduke691dd572014-12-02 20:47:521266 if (!input_event_filter_.get()) {
1267 // Always provide an input event filter implementation to ensure consistent
1268 // input event scheduling and prioritization.
1269 // TODO(jdduke): Merge InputEventFilter, InputHandlerManager and
1270 // MainThreadInputEventFilter, crbug.com/436057.
1271 input_event_filter_ = new MainThreadInputEventFilter(
1272 main_input_callback_.callback(), main_thread_compositor_task_runner_);
1273 }
1274 AddFilter(input_event_filter_.get());
1275
skyostil2d3b5bd2015-05-27 15:40:591276 scoped_refptr<base::SingleThreadTaskRunner> compositor_impl_side_task_runner;
vollick1050cc62015-12-03 07:04:541277 if (compositor_task_runner_)
skyostil2d3b5bd2015-05-27 15:40:591278 compositor_impl_side_task_runner = compositor_task_runner_;
[email protected]7f1f63f2013-03-07 06:07:291279 else
skyostil2d3b5bd2015-05-27 15:40:591280 compositor_impl_side_task_runner = base::ThreadTaskRunnerHandle::Get();
[email protected]1842fe22012-08-13 23:24:351281
simonhonga7e3ac42014-11-11 20:50:221282 compositor_message_filter_ = new CompositorForwardingMessageFilter(
skyostil2d3b5bd2015-05-27 15:40:591283 compositor_impl_side_task_runner.get());
simonhonga7e3ac42014-11-11 20:50:221284 AddFilter(compositor_message_filter_.get());
[email protected]1842fe22012-08-13 23:24:351285
[email protected]58436a12012-03-21 17:10:261286 RenderThreadImpl::RegisterSchemes();
1287
xhwang194acae2014-11-12 22:46:331288 RenderMediaClient::Initialize();
1289
ulan4a385192015-11-11 10:59:181290 if (GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden()) {
[email protected]1784b2f2011-11-24 10:53:481291 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
ulan4a385192015-11-11 10:59:181292 } else {
1293 // If we do not track widget visibility, then assume conservatively that
1294 // the isolate is in background. This reduces memory usage.
1295 isolate->IsolateInBackgroundNotification();
1296 }
[email protected]2541d1a2013-07-10 07:33:271297
John Abd-El-Maleke1d4de32017-10-21 16:14:291298 service_worker_message_filter_ =
1299 new ServiceWorkerMessageFilter(thread_safe_sender());
1300 AddFilter(service_worker_message_filter_->GetFilter());
1301
Shubhie Panicker11a8e6a2017-09-19 09:29:231302 renderer_scheduler_->SetStoppingWhenBackgroundedEnabled(
1303 GetContentClient()->renderer()->AllowStoppingWhenProcessBackgrounded());
jdduke73220f02015-09-04 17:03:511304
reedccf98c52014-10-03 16:40:381305 SkGraphics::SetResourceCacheSingleAllocationByteLimit(
[email protected]dd2c1022014-07-22 23:13:571306 kImageCacheSingleAllocationByteLimit);
1307
reed6e5a72222015-08-06 20:37:161308 // Hook up blink's codecs so skia can call them
reed08cd16372017-02-21 22:11:411309 SkGraphics::SetImageGeneratorFromEncodedDataFactory(
Vladimir Levin325831d2017-08-30 15:03:221310 blink::WebImageGenerator::CreateAsSkImageGenerator);
reed6e5a72222015-08-06 20:37:161311
paritosh.in7e30c902015-04-15 17:04:071312 if (command_line.HasSwitch(switches::kExplicitlyAllowedPorts)) {
1313 std::string allowed_ports =
1314 command_line.GetSwitchValueASCII(switches::kExplicitlyAllowedPorts);
1315 net::SetExplicitlyAllowedPorts(allowed_ports);
1316 }
[email protected]d1b8fccc2011-08-03 01:20:131317}
1318
[email protected]58436a12012-03-21 17:10:261319void RenderThreadImpl::RegisterSchemes() {
mkwst8e94fb32015-05-20 05:05:141320 // chrome:
Blink Reformat1c4d759e2017-04-09 16:34:541321 WebString chrome_scheme(WebString::FromASCII(kChromeUIScheme));
1322 WebSecurityPolicy::RegisterURLSchemeAsDisplayIsolated(chrome_scheme);
1323 WebSecurityPolicy::RegisterURLSchemeAsNotAllowingJavascriptURLs(
mkwst8e94fb32015-05-20 05:05:141324 chrome_scheme);
mkwst8e94fb32015-05-20 05:05:141325
1326 // chrome-devtools:
Blink Reformat1c4d759e2017-04-09 16:34:541327 WebString devtools_scheme(WebString::FromASCII(kChromeDevToolsScheme));
1328 WebSecurityPolicy::RegisterURLSchemeAsDisplayIsolated(devtools_scheme);
meacerce6b66032016-06-02 20:56:051329
1330 // view-source:
Blink Reformat1c4d759e2017-04-09 16:34:541331 WebString view_source_scheme(WebString::FromASCII(kViewSourceScheme));
1332 WebSecurityPolicy::RegisterURLSchemeAsDisplayIsolated(view_source_scheme);
Alex Moshchuk71f485592017-08-16 16:20:001333
1334 // chrome-error:
1335 WebString error_scheme(WebString::FromASCII(kChromeErrorScheme));
1336 WebSecurityPolicy::RegisterURLSchemeAsDisplayIsolated(error_scheme);
1337 WebSecurityPolicy::RegisterURLSchemeAsNotAllowingJavascriptURLs(error_scheme);
[email protected]58436a12012-03-21 17:10:261338}
1339
[email protected]e6e30ac2014-01-13 21:24:391340void RenderThreadImpl::RecordAction(const base::UserMetricsAction& action) {
[email protected]97880c82013-12-04 07:09:211341 Send(new ViewHostMsg_UserMetricsRecordAction(action.str_));
1342}
1343
1344void RenderThreadImpl::RecordComputedAction(const std::string& action) {
[email protected]526476902011-10-06 20:34:061345 Send(new ViewHostMsg_UserMetricsRecordAction(action));
1346}
1347
dchengcedca5612016-04-09 01:40:151348std::unique_ptr<base::SharedMemory>
1349RenderThreadImpl::HostAllocateSharedMemoryBuffer(size_t size) {
sadrulee3ff1f2016-12-09 04:22:011350 return ChildThreadImpl::AllocateSharedMemory(size);
[email protected]00614a82011-10-07 22:39:311351}
1352
Fady Samuel4b5f9862017-07-11 05:27:201353viz::SharedBitmapManager* RenderThreadImpl::GetSharedBitmapManager() {
jbauman2f5c1942014-12-06 03:28:241354 return shared_bitmap_manager();
1355}
1356
[email protected]f1a29a02011-10-06 23:08:441357void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
Blink Reformat1c4d759e2017-04-09 16:34:541358 WebScriptController::RegisterExtension(extension);
[email protected]526476902011-10-06 20:34:061359}
1360
avi1023d012015-12-25 02:39:141361void RenderThreadImpl::ScheduleIdleHandler(int64_t initial_delay_ms) {
[email protected]6593ae12011-11-14 12:09:441362 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:061363 idle_timer_.Stop();
1364 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:441365 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:441366 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:061367}
1368
[email protected]f1a29a02011-10-06 23:08:441369void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:481370 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
[email protected]e9ff79c2012-10-19 21:31:261371 GetContentClient()->renderer()->
[email protected]1784b2f2011-11-24 10:53:481372 RunIdleHandlerWhenWidgetsHidden();
1373 if (run_in_foreground_tab) {
jochen5a32aaf2014-09-26 20:37:431374 if (idle_notifications_to_skip_ > 0) {
1375 --idle_notifications_to_skip_;
1376 } else {
ssid0603ca9f2015-06-09 16:48:081377 ReleaseFreeMemory();
jochen5a32aaf2014-09-26 20:37:431378 }
1379 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]1784b2f2011-11-24 10:53:481380 return;
1381 }
[email protected]237a14852012-04-28 02:56:381382
ssid0603ca9f2015-06-09 16:48:081383 ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:061384
[email protected]26e82322014-01-20 14:18:221385 // Continue the idle timer if the webkit shared timer is not suspended or
1386 // something is left to do.
Alexander Timin4b6152232017-10-17 20:34:061387 bool continue_timer = !webkit_shared_timer_suspended_;
[email protected]26e82322014-01-20 14:18:221388
sullivancd45a3e2014-09-19 14:39:181389 // Schedule next invocation. When the tab is originally hidden, an invocation
1390 // is scheduled for kInitialIdleHandlerDelayMs in
1391 // RenderThreadImpl::WidgetHidden in order to race to a minimal heap.
1392 // After that, idle calls can be much less frequent, so run at a maximum of
1393 // once every kLongIdleHandlerDelayMs.
[email protected]6593ae12011-11-14 12:09:441394 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:061395 // delay = delay + 1 / (delay + 2)
1396 // Using floor(delay) has a dampening effect such as:
sullivancd45a3e2014-09-19 14:39:181397 // 30s, 30, 30, 31, 31, 31, 31, 32, 32, ...
[email protected]6593ae12011-11-14 12:09:441398 // If the delay is in milliseconds, the above formula is equivalent to:
1399 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
1400 // which is equivalent to
1401 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
[email protected]26e82322014-01-20 14:18:221402 if (continue_timer) {
sullivancd45a3e2014-09-19 14:39:181403 ScheduleIdleHandler(
1404 std::max(kLongIdleHandlerDelayMs,
1405 idle_notification_delay_in_ms_ +
1406 1000000 / (idle_notification_delay_in_ms_ + 2000)));
[email protected]26e82322014-01-20 14:18:221407
1408 } else {
1409 idle_timer_.Stop();
1410 }
[email protected]526476902011-10-06 20:34:061411
ericwilligers88e69742016-10-17 19:29:551412 for (auto& observer : observers_)
1413 observer.IdleNotification();
[email protected]526476902011-10-06 20:34:061414}
1415
avi1023d012015-12-25 02:39:141416int64_t RenderThreadImpl::GetIdleNotificationDelayInMs() const {
[email protected]6593ae12011-11-14 12:09:441417 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:061418}
1419
[email protected]6593ae12011-11-14 12:09:441420void RenderThreadImpl::SetIdleNotificationDelayInMs(
avi1023d012015-12-25 02:39:141421 int64_t idle_notification_delay_in_ms) {
[email protected]6593ae12011-11-14 12:09:441422 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:081423}
1424
[email protected]5b18406362013-06-18 18:46:431425int RenderThreadImpl::PostTaskToAllWebWorkers(const base::Closure& closure) {
kinukoef647412015-12-23 06:10:431426 return WorkerThreadRegistry::Instance()->PostTaskToAllThreads(closure);
[email protected]a9bd323d2013-06-17 20:27:561427}
1428
[email protected]b02f5902012-12-19 07:33:001429bool RenderThreadImpl::ResolveProxy(const GURL& url, std::string* proxy_list) {
1430 bool result = false;
1431 Send(new ViewHostMsg_ResolveProxy(url, &result, proxy_list));
1432 return result;
1433}
1434
[email protected]1784b2f2011-11-24 10:53:481435void RenderThreadImpl::PostponeIdleNotification() {
1436 idle_notifications_to_skip_ = 2;
1437}
1438
dcastagna7f45dada2015-10-19 20:17:351439media::GpuVideoAcceleratorFactories* RenderThreadImpl::GetGpuFactories() {
[email protected]3bb8bb32013-07-11 13:13:031440 DCHECK(IsMainThread());
1441
dcastagna09bd6b32016-02-01 21:54:281442 if (!gpu_factories_.empty()) {
sadrul85cc5d82016-12-20 03:37:411443 scoped_refptr<ui::ContextProviderCommandBuffer> shared_context_provider =
dcastagna09bd6b32016-02-01 21:54:281444 gpu_factories_.back()->ContextProviderMainThread();
1445 if (shared_context_provider) {
Xu Xing32549162017-07-17 22:25:431446 viz::ContextProvider::ScopedContextLock lock(
dcastagna09bd6b32016-02-01 21:54:281447 shared_context_provider.get());
Miguel Casas5e1018052018-01-09 19:17:361448 if (lock.ContextGL()->GetGraphicsResetStatusKHR() == GL_NO_ERROR)
leon.han21e0e482017-02-23 04:13:321449 return gpu_factories_.back().get();
Miguel Casas5e1018052018-01-09 19:17:361450
1451 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner =
1452 GetMediaThreadTaskRunner();
1453 media_task_runner->PostTask(
1454 FROM_HERE,
1455 base::BindOnce(
1456 base::IgnoreResult(
1457 &GpuVideoAcceleratorFactoriesImpl::CheckContextLost),
1458 base::Unretained(gpu_factories_.back().get())));
dcastagna09bd6b32016-02-01 21:54:281459 }
1460 }
dcastagna7f45dada2015-10-19 20:17:351461
tobiasjsca238b3b2015-06-24 22:53:541462 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1463
danakj0b4b94e32016-05-10 22:33:011464 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host =
sadrul6d310fa2016-08-04 02:12:161465 EstablishGpuChannelSync();
danakj0b4b94e32016-05-10 22:33:011466 if (!gpu_channel_host)
1467 return nullptr;
danakj0dd9e1e2016-05-11 22:15:091468 // This context is only used to create textures and mailbox them, so
1469 // use lower limits than the default.
1470 gpu::SharedMemoryLimits limits = gpu::SharedMemoryLimits::ForMailboxContext();
danakj9a04adc2016-05-16 22:45:071471 bool support_locking = true;
Victor Miura3a4ad4f82017-12-13 06:03:451472 bool support_gles2_interface = true;
1473 bool support_raster_interface = false;
Adrienne Walker436a7752017-08-28 23:33:091474 bool support_oop_rasterization = false;
sadrul85cc5d82016-12-20 03:37:411475 scoped_refptr<ui::ContextProviderCommandBuffer> media_context_provider =
Antoine Labour5ac65db2017-12-19 18:02:581476 CreateOffscreenContext(
1477 gpu_channel_host, GetGpuMemoryBufferManager(), limits,
1478 support_locking, support_gles2_interface, support_raster_interface,
1479 support_oop_rasterization, ui::command_buffer_metrics::MEDIA_CONTEXT,
1480 kGpuStreamIdDefault, kGpuStreamPriorityDefault);
danakj45cfd232017-10-18 19:31:311481 auto result = media_context_provider->BindToCurrentThread();
1482 if (result != gpu::ContextResult::kSuccess)
danakj0b4b94e32016-05-10 22:33:011483 return nullptr;
danakj0b4b94e32016-05-10 22:33:011484
acolwellb4034942014-08-28 15:42:431485 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner =
1486 GetMediaThreadTaskRunner();
danakj0b4b94e32016-05-10 22:33:011487 const bool enable_video_accelerator =
Zhenyao Mo16d39b82017-12-15 22:48:041488 !cmd_line->HasSwitch(switches::kDisableAcceleratedVideoDecode) &&
1489 (gpu_channel_host->gpu_feature_info()
1490 .status_values[gpu::GPU_FEATURE_TYPE_ACCELERATED_VIDEO_DECODE] ==
1491 gpu::kGpuFeatureStatusEnabled);
danakj0b4b94e32016-05-10 22:33:011492 const bool enable_gpu_memory_buffer_video_frames =
danakjab9ef4d2017-10-31 23:38:331493 !is_gpu_compositing_disabled_ &&
dcastagna43c3a86a2016-02-02 21:16:381494#if defined(OS_MACOSX) || defined(OS_LINUX)
danakjab9ef4d2017-10-31 23:38:331495 !cmd_line->HasSwitch(switches::kDisableGpuMemoryBufferVideoFrames);
jbaumana0bb8982017-05-05 19:50:531496#elif defined(OS_WIN)
1497 !cmd_line->HasSwitch(switches::kDisableGpuMemoryBufferVideoFrames) &&
jbaumana0bb8982017-05-05 19:50:531498 (cmd_line->HasSwitch(switches::kEnableGpuMemoryBufferVideoFrames) ||
1499 gpu_channel_host->gpu_info().supports_overlays);
dcastagnab65e6072015-09-05 07:18:421500#else
danakj0b4b94e32016-05-10 22:33:011501 cmd_line->HasSwitch(switches::kEnableGpuMemoryBufferVideoFrames);
dcastagnab65e6072015-09-05 07:18:421502#endif
dcastagna5077d6d2016-01-27 00:07:051503
Miguel Casas-Sanchez7d464702017-07-29 14:19:211504 media::mojom::VideoEncodeAcceleratorProviderPtr vea_provider;
1505 gpu_->CreateVideoEncodeAcceleratorProvider(mojo::MakeRequest(&vea_provider));
Miguel Casas-Sanchezdaca32562017-07-11 08:05:451506
Miguel Casas-Sanchezcfcca5d52017-07-07 02:32:591507 gpu_factories_.push_back(GpuVideoAcceleratorFactoriesImpl::Create(
danakj0b4b94e32016-05-10 22:33:011508 std::move(gpu_channel_host), base::ThreadTaskRunnerHandle::Get(),
1509 media_task_runner, std::move(media_context_provider),
Victor Miuracabdfac2018-01-05 02:18:181510 enable_gpu_memory_buffer_video_frames, enable_video_accelerator,
1511 vea_provider.PassInterface()));
Miguel Casas5e1018052018-01-09 19:17:361512 gpu_factories_.back()->SetRenderingColorSpace(rendering_color_space_);
leon.han21e0e482017-02-23 04:13:321513 return gpu_factories_.back().get();
[email protected]3bb8bb32013-07-11 13:13:031514}
1515
sadrul85cc5d82016-12-20 03:37:411516scoped_refptr<ui::ContextProviderCommandBuffer>
[email protected]f10dc472013-09-27 03:31:591517RenderThreadImpl::SharedMainThreadContextProvider() {
[email protected]e06e1122013-03-15 17:12:381518 DCHECK(IsMainThread());
danakje8ec797e2016-04-19 04:08:431519 if (shared_main_thread_contexts_ &&
1520 shared_main_thread_contexts_->ContextGL()->GetGraphicsResetStatusKHR() ==
1521 GL_NO_ERROR)
1522 return shared_main_thread_contexts_;
1523
sadrul6d310fa2016-08-04 02:12:161524 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(
1525 EstablishGpuChannelSync());
danakje8ec797e2016-04-19 04:08:431526 if (!gpu_channel_host) {
1527 shared_main_thread_contexts_ = nullptr;
1528 return nullptr;
[email protected]c29b7ff2013-03-06 03:51:041529 }
danakje8ec797e2016-04-19 04:08:431530
danakj9a04adc2016-05-16 22:45:071531 bool support_locking = false;
Victor Miura3a4ad4f82017-12-13 06:03:451532 bool support_gles2_interface = true;
1533 bool support_raster_interface = false;
Adrienne Walker436a7752017-08-28 23:33:091534 bool support_oop_rasterization = false;
danakjc3983552016-05-03 00:04:351535 shared_main_thread_contexts_ = CreateOffscreenContext(
Antoine Labour5ac65db2017-12-19 18:02:581536 std::move(gpu_channel_host), GetGpuMemoryBufferManager(),
1537 gpu::SharedMemoryLimits(), support_locking, support_gles2_interface,
1538 support_raster_interface, support_oop_rasterization,
sadrul85cc5d82016-12-20 03:37:411539 ui::command_buffer_metrics::RENDERER_MAINTHREAD_CONTEXT,
sunnyps8f9139e2017-05-12 17:53:251540 kGpuStreamIdDefault, kGpuStreamPriorityDefault);
danakj45cfd232017-10-18 19:31:311541 auto result = shared_main_thread_contexts_->BindToCurrentThread();
1542 if (result != gpu::ContextResult::kSuccess)
danakje8ec797e2016-04-19 04:08:431543 shared_main_thread_contexts_ = nullptr;
[email protected]f10dc472013-09-27 03:31:591544 return shared_main_thread_contexts_;
[email protected]c29b7ff2013-03-06 03:51:041545}
1546
siva.gunturi5d4feb052015-11-15 16:15:311547#if defined(OS_ANDROID)
boliuf8753bf62016-02-11 20:09:421548
siva.gunturi5d4feb052015-11-15 16:15:311549scoped_refptr<StreamTextureFactory> RenderThreadImpl::GetStreamTexureFactory() {
1550 DCHECK(IsMainThread());
boliu30f1b262016-04-19 00:12:331551 if (!stream_texture_factory_.get() ||
1552 stream_texture_factory_->ContextGL()->GetGraphicsResetStatusKHR() !=
1553 GL_NO_ERROR) {
sadrul85cc5d82016-12-20 03:37:411554 scoped_refptr<ui::ContextProviderCommandBuffer> shared_context_provider =
piman9fc22f32016-05-02 22:21:221555 SharedMainThreadContextProvider();
1556 if (!shared_context_provider) {
penghuange1d86512016-07-08 18:15:001557 stream_texture_factory_ = nullptr;
1558 return nullptr;
siva.gunturi5d4feb052015-11-15 16:15:311559 }
piman9fc22f32016-05-02 22:21:221560 DCHECK(shared_context_provider->GetCommandBufferProxy());
1561 DCHECK(shared_context_provider->GetCommandBufferProxy()->channel());
1562 stream_texture_factory_ =
1563 StreamTextureFactory::Create(std::move(shared_context_provider));
siva.gunturi5d4feb052015-11-15 16:15:311564 }
1565 return stream_texture_factory_;
1566}
boliuf8753bf62016-02-11 20:09:421567
1568bool RenderThreadImpl::EnableStreamTextureCopy() {
boliu30f1b262016-04-19 00:12:331569 return sync_compositor_message_filter_.get();
boliuf8753bf62016-02-11 20:09:421570}
1571
siva.gunturi5d4feb052015-11-15 16:15:311572#endif
1573
[email protected]e9ff79c2012-10-19 21:31:261574AudioRendererMixerManager* RenderThreadImpl::GetAudioRendererMixerManager() {
[email protected]59383c782013-04-17 16:43:271575 if (!audio_renderer_mixer_manager_) {
olka7a4679392016-05-27 15:32:581576 audio_renderer_mixer_manager_ = AudioRendererMixerManager::Create();
[email protected]3958e972012-07-17 00:25:411577 }
1578
1579 return audio_renderer_mixer_manager_.get();
1580}
1581
[email protected]73429ca2014-03-06 06:07:471582base::WaitableEvent* RenderThreadImpl::GetShutdownEvent() {
1583 return ChildProcess::current()->GetShutDownEvent();
1584}
1585
staraz067f58242016-11-07 21:06:411586int32_t RenderThreadImpl::GetClientId() {
1587 return client_id_;
1588}
1589
Karan Bhatiac89897f2017-08-10 01:06:401590void RenderThreadImpl::SetRendererProcessType(
1591 blink::scheduler::RendererProcessType type) {
1592 renderer_scheduler_->SetRendererProcessType(type);
1593}
1594
John Abd-El-Malek6b56ef712017-10-21 22:52:461595bool RenderThreadImpl::OnMessageReceived(const IPC::Message& msg) {
John Abd-El-Malekdcf1d1372017-10-22 06:39:411596 if (file_system_dispatcher_->OnMessageReceived(msg))
1597 return true;
John Abd-El-Malek6b56ef712017-10-21 22:52:461598 return ChildThreadImpl::OnMessageReceived(msg);
1599}
1600
rockot067ca55f2016-09-30 22:00:151601void RenderThreadImpl::OnAssociatedInterfaceRequest(
1602 const std::string& name,
1603 mojo::ScopedInterfaceEndpointHandle handle) {
rockot70bbb59492017-01-25 00:56:511604 if (associated_interfaces_.CanBindRequest(name))
1605 associated_interfaces_.BindRequest(name, std::move(handle));
1606 else
1607 ChildThreadImpl::OnAssociatedInterfaceRequest(name, std::move(handle));
rockot067ca55f2016-09-30 22:00:151608}
1609
weilifabbf7572017-05-22 19:05:161610scoped_refptr<base::SingleThreadTaskRunner>
1611RenderThreadImpl::GetIOTaskRunner() {
1612 return ChildProcess::current()->io_task_runner();
1613}
1614
danakj6e3bf8012014-12-16 18:27:531615bool RenderThreadImpl::IsGpuRasterizationForced() {
1616 return is_gpu_rasterization_forced_;
1617}
1618
senorblancob60ba952015-01-27 19:12:361619int RenderThreadImpl::GetGpuRasterizationMSAASampleCount() {
1620 return gpu_rasterization_msaa_sample_count_;
1621}
1622
danakj6e3bf8012014-12-16 18:27:531623bool RenderThreadImpl::IsLcdTextEnabled() {
1624 return is_lcd_text_enabled_;
1625}
1626
1627bool RenderThreadImpl::IsDistanceFieldTextEnabled() {
1628 return is_distance_field_text_enabled_;
1629}
1630
1631bool RenderThreadImpl::IsZeroCopyEnabled() {
1632 return is_zero_copy_enabled_;
1633}
1634
ericrk1d17f752015-10-20 03:03:071635bool RenderThreadImpl::IsPartialRasterEnabled() {
1636 return is_partial_raster_enabled_;
jbroman5f7f71932015-08-18 16:24:461637}
1638
ccameronc7fcd132015-11-03 20:14:311639bool RenderThreadImpl::IsGpuMemoryBufferCompositorResourcesEnabled() {
1640 return is_gpu_memory_buffer_compositor_resources_enabled_;
1641}
1642
ccamerona7644752014-12-30 01:16:311643bool RenderThreadImpl::IsElasticOverscrollEnabled() {
1644 return is_elastic_overscroll_enabled_;
1645}
1646
danakj6e3bf8012014-12-16 18:27:531647scoped_refptr<base::SingleThreadTaskRunner>
1648RenderThreadImpl::GetCompositorMainThreadTaskRunner() {
1649 return main_thread_compositor_task_runner_;
1650}
1651
1652scoped_refptr<base::SingleThreadTaskRunner>
1653RenderThreadImpl::GetCompositorImplThreadTaskRunner() {
skyostil2d3b5bd2015-05-27 15:40:591654 return compositor_task_runner_;
danakj6e3bf8012014-12-16 18:27:531655}
1656
1657gpu::GpuMemoryBufferManager* RenderThreadImpl::GetGpuMemoryBufferManager() {
sadrul53546592016-12-17 01:44:211658 return gpu_->gpu_memory_buffer_manager();
danakj6e3bf8012014-12-16 18:27:531659}
1660
skyostil529caa292016-08-10 17:44:511661blink::scheduler::RendererScheduler* RenderThreadImpl::GetRendererScheduler() {
danakj6e3bf8012014-12-16 18:27:531662 return renderer_scheduler_.get();
1663}
1664
Fady Samuelc645ffe2017-07-24 17:28:201665std::unique_ptr<viz::BeginFrameSource>
danakj6e3bf8012014-12-16 18:27:531666RenderThreadImpl::CreateExternalBeginFrameSource(int routing_id) {
Jeremy Roman04f27c372017-10-27 15:20:551667 return std::make_unique<CompositorExternalBeginFrameSource>(
ricea29649b92016-08-31 09:30:211668 compositor_message_filter_.get(), sync_message_filter(), routing_id);
danakj6e3bf8012014-12-16 18:27:531669}
1670
Fady Samuelc645ffe2017-07-24 17:28:201671std::unique_ptr<viz::SyntheticBeginFrameSource>
samans09812d32017-03-27 19:51:501672RenderThreadImpl::CreateSyntheticBeginFrameSource() {
1673 base::SingleThreadTaskRunner* compositor_impl_side_task_runner =
1674 compositor_task_runner_ ? compositor_task_runner_.get()
1675 : base::ThreadTaskRunnerHandle::Get().get();
Jeremy Roman04f27c372017-10-27 15:20:551676 return std::make_unique<viz::BackToBackBeginFrameSource>(
1677 std::make_unique<viz::DelayBasedTimeSource>(
samans09812d32017-03-27 19:51:501678 compositor_impl_side_task_runner));
1679}
1680
reveman34b7a1522015-03-23 20:27:471681cc::TaskGraphRunner* RenderThreadImpl::GetTaskGraphRunner() {
prashant.nfad657e2016-06-01 07:52:171682 return categorized_worker_pool_->GetTaskGraphRunner();
reveman34b7a1522015-03-23 20:27:471683}
1684
loyso65c93c602015-08-11 05:15:571685bool RenderThreadImpl::IsThreadedAnimationEnabled() {
1686 return is_threaded_animation_enabled_;
1687}
1688
wjmaclean1d970622017-01-21 22:28:241689bool RenderThreadImpl::IsScrollAnimatorEnabled() {
1690 return is_scroll_animator_enabled_;
1691}
1692
Khushal3e96e662017-10-30 23:16:501693std::unique_ptr<cc::UkmRecorderFactory>
1694RenderThreadImpl::CreateUkmRecorderFactory() {
Khushalda50f8152017-12-01 09:04:401695 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1696 switches::kDisableCompositorUkmForTests)) {
1697 return nullptr;
1698 }
1699
1700 return std::make_unique<UkmRecorderFactoryImpl>(GetConnector()->Clone());
Khushal3e96e662017-10-30 23:16:501701}
1702
skyostila37c2a72016-06-29 17:30:071703void RenderThreadImpl::OnRAILModeChanged(v8::RAILMode rail_mode) {
Blink Reformat1c4d759e2017-04-09 16:34:541704 blink::MainThreadIsolate()->SetRAILMode(rail_mode);
1705 blink::SetRAILModeOnWorkerThreadIsolates(rail_mode);
skyostila37c2a72016-06-29 17:30:071706}
1707
[email protected]b3e83de2012-02-07 03:33:281708bool RenderThreadImpl::IsMainThread() {
1709 return !!current();
1710}
1711
torne88b66452016-05-03 13:22:141712void RenderThreadImpl::OnChannelError() {
1713 // In single-process mode, the renderer can't be restarted after shutdown.
1714 // So, if we get a channel error, crash the whole process right now to get a
1715 // more informative stack, since we will otherwise just crash later when we
1716 // try to restart it.
1717 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
1718 switches::kSingleProcess));
1719 ChildThreadImpl::OnChannelError();
1720}
1721
Nicholas Verne06de8122017-11-16 04:47:381722void RenderThreadImpl::OnProcessFinalRelease() {
1723 if (on_channel_error_called())
1724 return;
1725 // The child process shutdown sequence is a request response based mechanism,
1726 // where we send out an initial feeler request to the child process host
1727 // instance in the browser to verify if it's ok to shutdown the child process.
1728 // The browser then sends back a response if it's ok to shutdown. This avoids
1729 // race conditions if the process refcount is 0 but there's an IPC message
1730 // inflight that would addref it.
1731 GetRendererHost()->ShutdownRequest();
1732}
1733
[email protected]f1a29a02011-10-06 23:08:441734bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
dchenga2d442c22016-10-13 15:39:211735 for (auto& observer : observers_) {
1736 if (observer.OnControlMessageReceived(msg))
[email protected]1223d6ef2011-03-28 16:47:501737 return true;
1738 }
1739
[email protected]70c19a932010-05-14 12:59:111740 // Some messages are handled by delegates.
Nicholas Verne07e9c6452017-12-13 04:12:551741 if (dom_storage_dispatcher_->OnMessageReceived(msg)) {
[email protected]a95986a82010-12-24 06:19:281742 return true;
[email protected]1910fe82012-05-10 00:04:101743 }
Darin Fisherbcc33632017-09-26 00:57:161744 return false;
initial.commit09911bf2008-07-26 23:55:291745}
1746
Nicholas Verne5420564d2017-10-27 07:37:541747void RenderThreadImpl::SetProcessBackgrounded(bool backgrounded) {
1748 // Set timer slack to maximum on main thread when in background.
1749 base::TimerSlack timer_slack = base::TIMER_SLACK_NONE;
1750 if (backgrounded)
1751 timer_slack = base::TIMER_SLACK_MAXIMUM;
1752 base::MessageLoop::current()->SetTimerSlack(timer_slack);
jdduke73220f02015-09-04 17:03:511753
Alexander Timin771b3372017-06-15 13:26:471754 renderer_scheduler_->SetRendererBackgrounded(backgrounded);
hajimehoshi69093272016-05-13 08:30:581755 if (backgrounded) {
tasakb95dbb50c2017-02-08 18:07:501756 needs_to_record_first_active_paint_ = false;
tasak335a1872017-06-14 09:50:231757 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1758 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061759 base::BindOnce(&RenderThreadImpl::RecordMemoryUsageAfterBackgrounded,
1760 base::Unretained(this), "5min",
1761 process_foregrounded_count_),
tasak335a1872017-06-14 09:50:231762 base::TimeDelta::FromMinutes(5));
1763 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1764 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061765 base::BindOnce(&RenderThreadImpl::RecordMemoryUsageAfterBackgrounded,
1766 base::Unretained(this), "10min",
1767 process_foregrounded_count_),
tasak335a1872017-06-14 09:50:231768 base::TimeDelta::FromMinutes(10));
1769 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1770 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061771 base::BindOnce(&RenderThreadImpl::RecordMemoryUsageAfterBackgrounded,
1772 base::Unretained(this), "15min",
1773 process_foregrounded_count_),
tasak335a1872017-06-14 09:50:231774 base::TimeDelta::FromMinutes(15));
Takashi SAKAMOTO870f6262017-08-22 04:08:271775 was_backgrounded_time_ = base::TimeTicks::Now();
hajimehoshi69093272016-05-13 08:30:581776 } else {
tasakbb0640b2017-05-15 09:02:261777 process_foregrounded_count_++;
hajimehoshi69093272016-05-13 08:30:581778 }
1779}
1780
Nicholas Verne5420564d2017-10-27 07:37:541781void RenderThreadImpl::ProcessPurgeAndSuspend() {
tasak6ff44032016-11-07 06:45:251782 if (!RendererIsHidden())
hajimehoshi69093272016-05-13 08:30:581783 return;
tasakff1e62422016-12-22 04:53:551784
tasakc4de4672017-04-26 02:37:361785 if (!base::FeatureList::IsEnabled(features::kPurgeAndSuspend))
1786 return;
1787
1788 base::MemoryCoordinatorClientRegistry::GetInstance()->PurgeMemory();
tasakb95dbb50c2017-02-08 18:07:501789 needs_to_record_first_active_paint_ = true;
tasakc4de4672017-04-26 02:37:361790
1791 RendererMemoryMetrics memory_metrics;
1792 if (!GetRendererMemoryMetrics(&memory_metrics))
1793 return;
1794
1795 purge_and_suspend_memory_metrics_ = memory_metrics;
tasaka27961a2017-05-24 07:33:251796 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1797 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061798 base::BindOnce(
1799 &RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics,
1800 base::Unretained(this), "30min", process_foregrounded_count_),
tasaka27961a2017-05-24 07:33:251801 base::TimeDelta::FromMinutes(30));
1802 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1803 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061804 base::BindOnce(
1805 &RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics,
1806 base::Unretained(this), "60min", process_foregrounded_count_),
tasaka27961a2017-05-24 07:33:251807 base::TimeDelta::FromMinutes(60));
1808 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
1809 FROM_HERE,
tzikff2a81a2017-09-15 05:10:061810 base::BindOnce(
1811 &RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics,
1812 base::Unretained(this), "90min", process_foregrounded_count_),
tasaka27961a2017-05-24 07:33:251813 base::TimeDelta::FromMinutes(90));
tasakb46626a2016-10-18 05:54:441814}
1815
bashia0ba0d42017-04-03 08:11:061816bool RenderThreadImpl::GetRendererMemoryMetrics(
keishi51ed0d52017-01-12 10:04:461817 RendererMemoryMetrics* memory_metrics) const {
1818 DCHECK(memory_metrics);
1819
chrisha05e94f22017-04-05 22:06:041820 // Cache this result, as it can change while this code is running, and is used
1821 // as a divisor below.
1822 size_t render_view_count = RenderView::GetRenderViewCount();
1823
1824 // If there are no render views it doesn't make sense to calculate metrics
1825 // right now.
1826 if (render_view_count == 0)
bashia0ba0d42017-04-03 08:11:061827 return false;
1828
keishi51ed0d52017-01-12 10:04:461829 blink::WebMemoryStatistics blink_stats = blink::WebMemoryStatistics::Get();
1830 memory_metrics->partition_alloc_kb =
Blink Reformat1c4d759e2017-04-09 16:34:541831 blink_stats.partition_alloc_total_allocated_bytes / 1024;
1832 memory_metrics->blink_gc_kb =
1833 blink_stats.blink_gc_total_allocated_bytes / 1024;
keishi12b598b92017-06-20 10:25:261834 std::unique_ptr<base::ProcessMetrics> metric(
1835 base::ProcessMetrics::CreateCurrentProcessMetrics());
1836 size_t malloc_usage = metric->GetMallocUsage();
keishi51ed0d52017-01-12 10:04:461837 memory_metrics->malloc_mb = malloc_usage / 1024 / 1024;
1838
1839 discardable_memory::ClientDiscardableSharedMemoryManager::Statistics
1840 discardable_stats = discardable_shared_memory_manager_->GetStatistics();
1841 size_t discardable_usage =
1842 discardable_stats.total_size - discardable_stats.freelist_size;
1843 memory_metrics->discardable_kb = discardable_usage / 1024;
1844
1845 size_t v8_usage = 0;
Blink Reformat1c4d759e2017-04-09 16:34:541846 if (v8::Isolate* isolate = blink::MainThreadIsolate()) {
keishi51ed0d52017-01-12 10:04:461847 v8::HeapStatistics v8_heap_statistics;
1848 isolate->GetHeapStatistics(&v8_heap_statistics);
1849 v8_usage = v8_heap_statistics.total_heap_size();
1850 }
1851 // TODO(tasak): Currently only memory usage of mainThreadIsolate() is
1852 // reported. We should collect memory usages of all isolates using
1853 // memory-infra.
1854 memory_metrics->v8_main_thread_isolate_mb = v8_usage / 1024 / 1024;
Blink Reformat1c4d759e2017-04-09 16:34:541855 size_t total_allocated = blink_stats.partition_alloc_total_allocated_bytes +
1856 blink_stats.blink_gc_total_allocated_bytes +
keishi51ed0d52017-01-12 10:04:461857 malloc_usage + v8_usage + discardable_usage;
1858 memory_metrics->total_allocated_mb = total_allocated / 1024 / 1024;
1859 memory_metrics->non_discardable_total_allocated_mb =
1860 (total_allocated - discardable_usage) / 1024 / 1024;
1861 memory_metrics->total_allocated_per_render_view_mb =
chrisha05e94f22017-04-05 22:06:041862 total_allocated / render_view_count / 1024 / 1024;
bashia0ba0d42017-04-03 08:11:061863
1864 return true;
keishi51ed0d52017-01-12 10:04:461865}
1866
tasak335a1872017-06-14 09:50:231867static void RecordMemoryUsageAfterBackgroundedMB(const char* basename,
1868 const char* suffix,
1869 int memory_usage) {
1870 std::string histogram_name = base::StringPrintf("%s.%s", basename, suffix);
1871 base::UmaHistogramMemoryLargeMB(histogram_name, memory_usage);
1872}
1873
1874void RenderThreadImpl::RecordMemoryUsageAfterBackgrounded(
1875 const char* suffix,
1876 int foregrounded_count) {
1877 // If this renderer is resumed, we should not update UMA.
1878 if (!RendererIsHidden())
1879 return;
1880 // If this renderer was not kept backgrounded for 5/10/15 minutes,
1881 // we should not record current memory usage.
1882 if (foregrounded_count != process_foregrounded_count_)
1883 return;
1884
1885 RendererMemoryMetrics memory_metrics;
1886 if (!GetRendererMemoryMetrics(&memory_metrics))
1887 return;
1888 RecordMemoryUsageAfterBackgroundedMB(
1889 "Memory.Experimental.Renderer.PartitionAlloc.AfterBackgrounded", suffix,
1890 memory_metrics.partition_alloc_kb / 1024);
1891 RecordMemoryUsageAfterBackgroundedMB(
1892 "Memory.Experimental.Renderer.BlinkGC.AfterBackgrounded", suffix,
1893 memory_metrics.blink_gc_kb / 1024);
1894 RecordMemoryUsageAfterBackgroundedMB(
1895 "Memory.Experimental.Renderer.Malloc.AfterBackgrounded", suffix,
1896 memory_metrics.malloc_mb);
1897 RecordMemoryUsageAfterBackgroundedMB(
1898 "Memory.Experimental.Renderer.Discardable.AfterBackgrounded", suffix,
1899 memory_metrics.discardable_kb / 1024);
1900 RecordMemoryUsageAfterBackgroundedMB(
1901 "Memory.Experimental.Renderer.V8MainThreaIsolate.AfterBackgrounded",
1902 suffix, memory_metrics.v8_main_thread_isolate_mb);
1903 RecordMemoryUsageAfterBackgroundedMB(
1904 "Memory.Experimental.Renderer.TotalAllocated.AfterBackgrounded", suffix,
1905 memory_metrics.total_allocated_mb);
1906}
1907
tasak72f64042017-01-19 14:02:531908#define GET_MEMORY_GROWTH(current, previous, allocator) \
1909 (current.allocator > previous.allocator \
1910 ? current.allocator - previous.allocator \
1911 : 0)
1912
tasaka27961a2017-05-24 07:33:251913static void RecordPurgeAndSuspendMemoryGrowthKB(const char* basename,
1914 const char* suffix,
1915 int memory_usage) {
1916 std::string histogram_name = base::StringPrintf("%s.%s", basename, suffix);
1917 base::UmaHistogramMemoryKB(histogram_name, memory_usage);
1918}
tasakbb0640b2017-05-15 09:02:261919
1920void RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics(
1921 const char* suffix,
1922 int foregrounded_count_when_purged) {
tasak72f64042017-01-19 14:02:531923 // If this renderer is resumed, we should not update UMA.
1924 if (!RendererIsHidden())
1925 return;
tasakbb0640b2017-05-15 09:02:261926 if (foregrounded_count_when_purged != process_foregrounded_count_)
1927 return;
tasak72f64042017-01-19 14:02:531928
1929 RendererMemoryMetrics memory_metrics;
bashia0ba0d42017-04-03 08:11:061930 if (!GetRendererMemoryMetrics(&memory_metrics))
1931 return;
1932
tasaka27961a2017-05-24 07:33:251933 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261934 "PurgeAndSuspend.Experimental.MemoryGrowth.PartitionAllocKB", suffix,
tasak72f64042017-01-19 14:02:531935 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
1936 partition_alloc_kb));
tasaka27961a2017-05-24 07:33:251937 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261938 "PurgeAndSuspend.Experimental.MemoryGrowth.BlinkGCKB", suffix,
tasak72f64042017-01-19 14:02:531939 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
1940 blink_gc_kb));
tasaka27961a2017-05-24 07:33:251941 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261942 "PurgeAndSuspend.Experimental.MemoryGrowth.MallocKB", suffix,
tasak72f64042017-01-19 14:02:531943 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
tasakbb0640b2017-05-15 09:02:261944 malloc_mb) *
1945 1024);
tasaka27961a2017-05-24 07:33:251946 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261947 "PurgeAndSuspend.Experimental.MemoryGrowth.DiscardableKB", suffix,
tasak72f64042017-01-19 14:02:531948 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
1949 discardable_kb));
tasaka27961a2017-05-24 07:33:251950 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261951 "PurgeAndSuspend.Experimental.MemoryGrowth.V8MainThreadIsolateKB", suffix,
tasak72f64042017-01-19 14:02:531952 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
tasakbb0640b2017-05-15 09:02:261953 v8_main_thread_isolate_mb) *
1954 1024);
tasaka27961a2017-05-24 07:33:251955 RecordPurgeAndSuspendMemoryGrowthKB(
tasakbb0640b2017-05-15 09:02:261956 "PurgeAndSuspend.Experimental.MemoryGrowth.TotalAllocatedKB", suffix,
tasak72f64042017-01-19 14:02:531957 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_,
tasakbb0640b2017-05-15 09:02:261958 total_allocated_mb) *
1959 1024);
jdduke73220f02015-09-04 17:03:511960}
1961
danakjab9ef4d2017-10-31 23:38:331962void RenderThreadImpl::CompositingModeFallbackToSoftware() {
Antoine Labour7fcbc782017-11-14 16:59:581963 gpu_->LoseChannel();
danakjab9ef4d2017-10-31 23:38:331964 is_gpu_compositing_disabled_ = true;
1965}
1966
Bo Liud1ef1c32017-12-08 06:15:501967scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() {
[email protected]d13f35d2012-05-18 02:28:151968 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
1969
Antoine Labour7fcbc782017-11-14 16:59:581970 scoped_refptr<gpu::GpuChannelHost> gpu_channel =
Bo Liud1ef1c32017-12-08 06:15:501971 gpu_->EstablishGpuChannelSync();
Antoine Labour7fcbc782017-11-14 16:59:581972 if (gpu_channel)
1973 GetContentClient()->SetGpuInfo(gpu_channel->gpu_info());
1974 return gpu_channel;
[email protected]3bf4d532010-03-27 00:23:341975}
1976
danakjc7afae52017-06-20 21:12:411977void RenderThreadImpl::RequestNewLayerTreeFrameSink(
danakj83066a32016-06-21 02:34:491978 int routing_id,
1979 scoped_refptr<FrameSwapMessageQueue> frame_swap_message_queue,
fsamuelcf3002e2017-03-20 23:13:471980 const GURL& url,
danakjc7afae52017-06-20 21:12:411981 const LayerTreeFrameSinkCallback& callback) {
danakj946e3172017-11-06 23:46:511982 // Misconfigured bots (eg. crbug.com/780757) could run layout tests on a
1983 // machine where gpu compositing doesn't work. Don't crash in that case.
1984 if (layout_test_mode() && is_gpu_compositing_disabled_) {
1985 LOG(FATAL) << "Layout tests require gpu compositing, but it is disabled.";
1986 return;
1987 }
1988
danakj83066a32016-06-21 02:34:491989 const base::CommandLine& command_line =
1990 *base::CommandLine::ForCurrentProcess();
Xu Xing48e1b322017-09-25 10:48:011991 viz::ClientLayerTreeFrameSink::InitParams params;
Sunny Sachanandani158d26d2017-12-12 19:44:101992 params.compositor_task_runner = compositor_task_runner_;
Xu Xing48e1b322017-09-25 10:48:011993 params.enable_surface_synchronization =
Fady Samuel343a74952017-11-30 14:19:201994 features::IsSurfaceSynchronizationEnabled();
Xu Xing48e1b322017-09-25 10:48:011995 params.local_surface_id_provider =
Jeremy Roman04f27c372017-10-27 15:20:551996 std::make_unique<RendererLocalSurfaceIdProvider>();
Fady Samuel0c4b4bf2017-08-22 00:06:461997
Eric Seckler8af8c0e52018-01-17 23:45:531998 // The renderer runs animations and layout for animate_only BeginFrames.
1999 params.wants_animate_only_begin_frames = true;
2000
samans09812d32017-03-27 19:51:502001 // In disable gpu vsync mode, also let the renderer tick as fast as it
2002 // can. The top level begin frame source will also be running as a back
2003 // to back begin frame source, but using a synthetic begin frame source
2004 // here reduces latency when in this mode (at least for frames
2005 // starting--it potentially increases it for input on the other hand.)
samans09812d32017-03-27 19:51:502006 if (command_line.HasSwitch(switches::kDisableGpuVsync) &&
2007 command_line.GetSwitchValueASCII(switches::kDisableGpuVsync) != "gpu") {
Xu Xing48e1b322017-09-25 10:48:012008 params.synthetic_begin_frame_source = CreateSyntheticBeginFrameSource();
samans09812d32017-03-27 19:51:502009 }
2010
sadrul943e3b32016-08-04 18:22:592011#if defined(USE_AURA)
Sadrul Habib Chowdhuryeeb5a942017-11-28 21:17:232012 if (switches::IsMusHostingViz()) {
Scott Violet986ac472017-08-10 16:38:342013 if (!RendererWindowTreeClient::Get(routing_id)) {
2014 callback.Run(nullptr);
2015 return;
2016 }
Bo Liud1ef1c32017-12-08 06:15:502017 scoped_refptr<gpu::GpuChannelHost> channel = EstablishGpuChannelSync();
sadrulbe22c4f2017-03-14 06:59:252018 // If the channel could not be established correctly, then return null. This
2019 // would cause the compositor to wait and try again at a later time.
fsamuelcf3002e2017-03-20 23:13:472020 if (!channel) {
2021 callback.Run(nullptr);
2022 return;
2023 }
fsamuel95c974eb2017-03-22 10:00:252024 RendererWindowTreeClient::Get(routing_id)
danakjc7afae52017-06-20 21:12:412025 ->RequestLayerTreeFrameSink(
fsamuel95c974eb2017-03-22 10:00:252026 gpu_->CreateContextProvider(std::move(channel)),
2027 GetGpuMemoryBufferManager(), callback);
fsamuelcf3002e2017-03-20 23:13:472028 return;
danakj83066a32016-06-21 02:34:492029 }
2030#endif
2031
Gary Klassen4d0f0232017-07-27 23:23:122032 viz::mojom::CompositorFrameSinkRequest sink_request =
kylecharb82991b2017-10-04 22:40:082033 mojo::MakeRequest(&params.pipes.compositor_frame_sink_info);
Gary Klassen4d0f0232017-07-27 23:23:122034 viz::mojom::CompositorFrameSinkClientPtr client;
kylecharb82991b2017-10-04 22:40:082035 params.pipes.client_request = mojo::MakeRequest(&client);
samansa61ab212017-05-24 12:11:432036
danakj83066a32016-06-21 02:34:492037 if (command_line.HasSwitch(switches::kEnableVulkan)) {
Alex Zhang1f159572017-08-23 20:14:202038 scoped_refptr<viz::VulkanContextProvider> vulkan_context_provider =
2039 viz::VulkanInProcessContextProvider::Create();
danakj83066a32016-06-21 02:34:492040 if (vulkan_context_provider) {
2041 DCHECK(!layout_test_mode());
samansa61ab212017-05-24 12:11:432042 frame_sink_provider_->CreateForWidget(routing_id, std::move(sink_request),
2043 std::move(client));
Jeremy Roman04f27c372017-10-27 15:20:552044 callback.Run(std::make_unique<viz::ClientLayerTreeFrameSink>(
Xu Xing48e1b322017-09-25 10:48:012045 std::move(vulkan_context_provider), &params));
fsamuelcf3002e2017-03-20 23:13:472046 return;
danakj83066a32016-06-21 02:34:492047 }
2048 }
2049
danakjab9ef4d2017-10-31 23:38:332050 if (is_gpu_compositing_disabled_) {
danakj83066a32016-06-21 02:34:492051 DCHECK(!layout_test_mode());
samansa61ab212017-05-24 12:11:432052 frame_sink_provider_->CreateForWidget(routing_id, std::move(sink_request),
2053 std::move(client));
Xu Xing48e1b322017-09-25 10:48:012054 params.shared_bitmap_manager = shared_bitmap_manager();
Jeremy Roman04f27c372017-10-27 15:20:552055 callback.Run(std::make_unique<viz::ClientLayerTreeFrameSink>(
Xu Xing48e1b322017-09-25 10:48:012056 nullptr, nullptr, &params));
fsamuelcf3002e2017-03-20 23:13:472057 return;
danakj83066a32016-06-21 02:34:492058 }
2059
danakjab9ef4d2017-10-31 23:38:332060 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host =
Bo Liud1ef1c32017-12-08 06:15:502061 EstablishGpuChannelSync();
danakjab9ef4d2017-10-31 23:38:332062 if (!gpu_channel_host) {
2063 // Wait and try again. We may hear that the compositing mode has switched
2064 // to software in the meantime.
2065 callback.Run(nullptr);
2066 return;
2067 }
2068
Victor Miuraff6488612017-12-21 04:16:152069 scoped_refptr<viz::RasterContextProvider> worker_context_provider =
danakj83066a32016-06-21 02:34:492070 SharedCompositorWorkerContextProvider();
2071 if (!worker_context_provider) {
2072 // Cause the compositor to wait and try again.
fsamuelcf3002e2017-03-20 23:13:472073 callback.Run(nullptr);
2074 return;
danakj83066a32016-06-21 02:34:492075 }
2076
2077 // The renderer compositor context doesn't do a lot of stuff, so we don't
2078 // expect it to need a lot of space for commands or transfer. Raster and
2079 // uploads happen on the worker context instead.
2080 gpu::SharedMemoryLimits limits = gpu::SharedMemoryLimits::ForMailboxContext();
2081
2082 // This is for an offscreen context for the compositor. So the default
2083 // framebuffer doesn't need alpha, depth, stencil, antialiasing.
Antoine Labourfeab2392017-12-21 20:28:392084 gpu::ContextCreationAttribs attributes;
danakj83066a32016-06-21 02:34:492085 attributes.alpha_size = -1;
2086 attributes.depth_size = 0;
2087 attributes.stencil_size = 0;
2088 attributes.samples = 0;
2089 attributes.sample_buffers = 0;
2090 attributes.bind_generates_resource = false;
2091 attributes.lose_context_when_out_of_memory = true;
Victor Miura3a4ad4f82017-12-13 06:03:452092 attributes.enable_gles2_interface = true;
2093 attributes.enable_raster_interface = false;
2094 attributes.enable_oop_rasterization = false;
danakj83066a32016-06-21 02:34:492095
2096 constexpr bool automatic_flushes = false;
2097 constexpr bool support_locking = false;
2098
sadrul85cc5d82016-12-20 03:37:412099 scoped_refptr<ui::ContextProviderCommandBuffer> context_provider(
2100 new ui::ContextProviderCommandBuffer(
Antoine Labour5ac65db2017-12-19 18:02:582101 gpu_channel_host, GetGpuMemoryBufferManager(), kGpuStreamIdDefault,
2102 kGpuStreamPriorityDefault, gpu::kNullSurfaceHandle, url,
Victor Miuraff6488612017-12-21 04:16:152103 automatic_flushes, support_locking, limits, attributes,
2104 nullptr /* share_context */,
sadrul85cc5d82016-12-20 03:37:412105 ui::command_buffer_metrics::RENDER_COMPOSITOR_CONTEXT));
danakj83066a32016-06-21 02:34:492106
danakj83066a32016-06-21 02:34:492107 if (layout_test_deps_) {
danakjc7afae52017-06-20 21:12:412108 callback.Run(layout_test_deps_->CreateLayerTreeFrameSink(
jbroman6ccbc7d472016-07-27 04:45:412109 routing_id, std::move(gpu_channel_host), std::move(context_provider),
fsamuelcf3002e2017-03-20 23:13:472110 std::move(worker_context_provider), GetGpuMemoryBufferManager(), this));
2111 return;
danakj83066a32016-06-21 02:34:492112 }
2113
2114#if defined(OS_ANDROID)
2115 if (sync_compositor_message_filter_) {
Fady Samuelc645ffe2017-07-24 17:28:202116 std::unique_ptr<viz::BeginFrameSource> begin_frame_source =
Xu Xing48e1b322017-09-25 10:48:012117 params.synthetic_begin_frame_source
2118 ? std::move(params.synthetic_begin_frame_source)
samans09812d32017-03-27 19:51:502119 : CreateExternalBeginFrameSource(routing_id);
Jeremy Roman04f27c372017-10-27 15:20:552120 callback.Run(std::make_unique<SynchronousLayerTreeFrameSink>(
danakja40dd4482016-06-28 01:14:102121 std::move(context_provider), std::move(worker_context_provider),
danakja2b002952017-12-14 22:36:222122 compositor_task_runner_, GetGpuMemoryBufferManager(), routing_id,
2123 g_next_layer_tree_frame_sink_id++, std::move(begin_frame_source),
2124 sync_compositor_message_filter_.get(),
fsamuelcf3002e2017-03-20 23:13:472125 std::move(frame_swap_message_queue)));
2126 return;
danakj83066a32016-06-21 02:34:492127 }
2128#endif
samansa61ab212017-05-24 12:11:432129 frame_sink_provider_->CreateForWidget(routing_id, std::move(sink_request),
2130 std::move(client));
Xu Xing48e1b322017-09-25 10:48:012131 params.gpu_memory_buffer_manager = GetGpuMemoryBufferManager();
Jeremy Roman04f27c372017-10-27 15:20:552132 callback.Run(std::make_unique<viz::ClientLayerTreeFrameSink>(
samans2040988b2017-04-11 23:58:352133 std::move(context_provider), std::move(worker_context_provider),
Xu Xing48e1b322017-09-25 10:48:012134 &params));
danakj83066a32016-06-21 02:34:492135}
2136
Sam McNally52e50282017-11-29 00:54:022137blink::AssociatedInterfaceRegistry*
rockot067ca55f2016-09-30 22:00:152138RenderThreadImpl::GetAssociatedInterfaceRegistry() {
2139 return &associated_interfaces_;
2140}
2141
jbroman6ccbc7d472016-07-27 04:45:412142std::unique_ptr<cc::SwapPromise>
2143RenderThreadImpl::RequestCopyOfOutputForLayoutTest(
2144 int32_t routing_id,
Fady Samueldfecb7d2017-07-26 11:41:042145 std::unique_ptr<viz::CopyOutputRequest> request) {
jbroman6ccbc7d472016-07-27 04:45:412146 DCHECK(layout_test_deps_);
2147 return layout_test_deps_->RequestCopyOfOutput(routing_id, std::move(request));
2148}
2149
altimineb6bd1962017-05-03 14:52:382150std::unique_ptr<blink::WebMediaStreamCenter>
2151RenderThreadImpl::CreateMediaStreamCenter(
[email protected]180ef242013-11-07 06:50:462152 blink::WebMediaStreamCenterClient* client) {
altimineb6bd1962017-05-03 14:52:382153 std::unique_ptr<blink::WebMediaStreamCenter> media_stream_center;
Brett Wilson0748bf412016-11-22 17:55:462154#if BUILDFLAG(ENABLE_WEBRTC)
altimineb6bd1962017-05-03 14:52:382155 if (!media_stream_center) {
Harald Alvestrand4931caa2017-11-08 16:53:352156 media_stream_center = std::make_unique<MediaStreamCenter>(
2157 client, GetPeerConnectionDependencyFactory());
[email protected]68e5fee2013-02-18 10:04:222158 }
[email protected]d8cd8372012-03-09 10:49:512159#endif
altimineb6bd1962017-05-03 14:52:382160 return media_stream_center;
[email protected]d8cd8372012-03-09 10:49:512161}
2162
Brett Wilson0748bf412016-11-22 17:55:462163#if BUILDFLAG(ENABLE_WEBRTC)
[email protected]0107d8a2014-05-16 10:20:342164PeerConnectionDependencyFactory*
2165RenderThreadImpl::GetPeerConnectionDependencyFactory() {
[email protected]83e0a482014-05-22 18:07:182166 return peer_connection_factory_.get();
[email protected]6ee10bd2012-09-13 09:01:532167}
[email protected]22fe91d2014-08-12 17:07:122168#endif
[email protected]6ee10bd2012-09-13 09:01:532169
rockot1587e332016-07-27 17:44:142170mojom::RenderFrameMessageFilter*
2171RenderThreadImpl::render_frame_message_filter() {
2172 if (!render_frame_message_filter_)
2173 GetChannel()->GetRemoteAssociatedInterface(&render_frame_message_filter_);
2174 return render_frame_message_filter_.get();
2175}
2176
rockote261d2112016-09-21 22:22:232177mojom::RenderMessageFilter* RenderThreadImpl::render_message_filter() {
2178 if (!render_message_filter_)
2179 GetChannel()->GetRemoteAssociatedInterface(&render_message_filter_);
2180 return render_message_filter_.get();
2181}
2182
penghuang346a46f92016-03-31 21:37:522183gpu::GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
Antoine Labour7fcbc782017-11-14 16:59:582184 return gpu_->GetGpuChannel().get();
[email protected]6217d392010-03-25 22:08:352185}
2186
rockot067ca55f2016-09-30 22:00:152187void RenderThreadImpl::CreateView(mojom::CreateViewParamsPtr params) {
2188 CompositorDependencies* compositor_deps = this;
wjmaclean1d970622017-01-21 22:28:242189 is_scroll_animator_enabled_ = params->web_preferences.enable_scroll_animator;
rockot067ca55f2016-09-30 22:00:152190 // When bringing in render_view, also bring in webkit's glue and jsbindings.
Balazs Engedyba034e72017-10-27 22:26:282191 RenderViewImpl::Create(compositor_deps, std::move(params),
Hajime Hoshiabb3c8f2017-12-04 18:41:392192 RenderWidget::ShowCallback(),
2193 GetRendererScheduler()->DefaultTaskRunner());
rockot067ca55f2016-09-30 22:00:152194}
2195
rockot53be7caf2016-10-04 20:17:082196void RenderThreadImpl::CreateFrame(mojom::CreateFrameParamsPtr params) {
rockot53be7caf2016-10-04 20:17:082197 CompositorDependencies* compositor_deps = this;
Ken Rockot26efbd62017-11-16 04:39:492198 service_manager::mojom::InterfaceProviderPtr interface_provider(
2199 std::move(params->interface_provider));
rockot53be7caf2016-10-04 20:17:082200 RenderFrameImpl::CreateFrame(
Ken Rockot26efbd62017-11-16 04:39:492201 params->routing_id, std::move(interface_provider),
Balazs Engedyba034e72017-10-27 22:26:282202 params->proxy_routing_id, params->opener_routing_id,
rockot53be7caf2016-10-04 20:17:082203 params->parent_routing_id, params->previous_sibling_routing_id,
Pavel Feldman25234722017-10-11 02:49:062204 params->devtools_frame_token, params->replication_state, compositor_deps,
2205 *params->widget_params, params->frame_owner_properties);
rockot53be7caf2016-10-04 20:17:082206}
2207
Makoto Shimazu7af43ee2017-08-25 10:20:402208void RenderThreadImpl::SetUpEmbeddedWorkerChannelForServiceWorker(
2209 mojom::EmbeddedWorkerInstanceClientAssociatedRequest client_request) {
2210 EmbeddedWorkerInstanceClientImpl::Create(
2211 blink_initialized_time_, GetIOTaskRunner(), std::move(client_request));
2212}
2213
rockot53be7caf2016-10-04 20:17:082214void RenderThreadImpl::CreateFrameProxy(
2215 int32_t routing_id,
2216 int32_t render_view_routing_id,
2217 int32_t opener_routing_id,
2218 int32_t parent_routing_id,
2219 const FrameReplicationState& replicated_state) {
nick3b5a21f2016-11-22 23:07:112220 RenderFrameProxy::CreateFrameProxy(
2221 routing_id, render_view_routing_id,
2222 RenderFrameImpl::ResolveOpener(opener_routing_id), parent_routing_id,
2223 replicated_state);
rockot53be7caf2016-10-04 20:17:082224}
2225
rockota2db0da2016-10-18 17:39:112226void RenderThreadImpl::OnNetworkConnectionChanged(
2227 net::NetworkChangeNotifier::ConnectionType type,
2228 double max_bandwidth_mbps) {
2229 bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE;
Blink Reformat1c4d759e2017-04-09 16:34:542230 WebNetworkStateNotifier::SetOnLine(online);
rockota2db0da2016-10-18 17:39:112231 for (auto& observer : observers_)
2232 observer.NetworkStateChanged(online);
Blink Reformat1c4d759e2017-04-09 16:34:542233 WebNetworkStateNotifier::SetWebConnection(
rockota2db0da2016-10-18 17:39:112234 NetConnectionTypeToWebConnectionType(type), max_bandwidth_mbps);
2235}
2236
tbansal15973c32017-05-10 18:40:442237void RenderThreadImpl::OnNetworkQualityChanged(
tbansalb612c5d2017-05-25 18:53:062238 net::EffectiveConnectionType type,
tbansal99d8aeb2017-05-22 19:12:582239 base::TimeDelta http_rtt,
2240 base::TimeDelta transport_rtt,
tbansal15973c32017-05-10 18:40:442241 double downlink_throughput_kbps) {
2242 UMA_HISTOGRAM_BOOLEAN("NQE.RenderThreadNotified", true);
tbansalb612c5d2017-05-25 18:53:062243 WebNetworkStateNotifier::SetNetworkQuality(
2244 EffectiveConnectionTypeToWebEffectiveConnectionType(type), http_rtt,
2245 transport_rtt, downlink_throughput_kbps);
tbansal15973c32017-05-10 18:40:442246}
2247
rockota2db0da2016-10-18 17:39:112248void RenderThreadImpl::SetWebKitSharedTimersSuspended(bool suspend) {
2249#if defined(OS_ANDROID)
2250 if (suspend) {
Alexander Timin4b6152232017-10-17 20:34:062251 renderer_scheduler_->PauseTimersForAndroidWebView();
rockota2db0da2016-10-18 17:39:112252 } else {
Alexander Timin4b6152232017-10-17 20:34:062253 renderer_scheduler_->ResumeTimersForAndroidWebView();
rockota2db0da2016-10-18 17:39:112254 }
Alexander Timin4b6152232017-10-17 20:34:062255 webkit_shared_timer_suspended_ = suspend;
rockota2db0da2016-10-18 17:39:112256#else
2257 NOTREACHED();
2258#endif
2259}
2260
2261void RenderThreadImpl::UpdateScrollbarTheme(
2262 mojom::UpdateScrollbarThemeParamsPtr params) {
2263#if defined(OS_MACOSX)
2264 static_cast<WebScrollbarBehaviorImpl*>(
Blink Reformat1c4d759e2017-04-09 16:34:542265 blink_platform_impl_->ScrollbarBehavior())
rockota2db0da2016-10-18 17:39:112266 ->set_jump_on_track_click(params->jump_on_track_click);
2267
Blink Reformat1c4d759e2017-04-09 16:34:542268 blink::WebScrollbarTheme::UpdateScrollbarsWithNSDefaults(
rockota2db0da2016-10-18 17:39:112269 params->initial_button_delay, params->autoscroll_button_delay,
2270 params->preferred_scroller_style, params->redraw,
2271 params->button_placement);
Greg Kerra7b943b2017-07-24 23:17:172272
2273 is_elastic_overscroll_enabled_ = params->scroll_view_rubber_banding;
rockota2db0da2016-10-18 17:39:112274#else
2275 NOTREACHED();
2276#endif
2277}
2278
2279void RenderThreadImpl::OnSystemColorsChanged(
2280 int32_t aqua_color_variant,
2281 const std::string& highlight_text_color,
2282 const std::string& highlight_color) {
2283#if defined(OS_MACOSX)
2284 SystemColorsDidChange(aqua_color_variant, highlight_text_color,
2285 highlight_color);
2286#else
2287 NOTREACHED();
2288#endif
2289}
2290
2291void RenderThreadImpl::PurgePluginListCache(bool reload_pages) {
brettw4b461082016-11-19 18:55:162292#if BUILDFLAG(ENABLE_PLUGINS)
rockota2db0da2016-10-18 17:39:112293 // The call below will cause a GetPlugins call with refresh=true, but at this
2294 // point we already know that the browser has refreshed its list, so disable
2295 // refresh temporarily to prevent each renderer process causing the list to be
2296 // regenerated.
2297 blink_platform_impl_->set_plugin_refresh_allowed(false);
Blink Reformat1c4d759e2017-04-09 16:34:542298 blink::ResetPluginCache(reload_pages);
rockota2db0da2016-10-18 17:39:112299 blink_platform_impl_->set_plugin_refresh_allowed(true);
2300
2301 for (auto& observer : observers_)
2302 observer.PluginListChanged();
2303#else
2304 NOTREACHED();
2305#endif
2306}
2307
[email protected]5071cb22013-07-10 02:19:062308void RenderThreadImpl::OnMemoryPressure(
2309 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
kylecharb82991b2017-10-04 22:40:082310 TRACE_EVENT0("memory", "RenderThreadImpl::OnMemoryPressure");
rmcilroy7fbb3bd52015-02-17 19:02:142311 if (blink_platform_impl_) {
Blink Reformat1c4d759e2017-04-09 16:34:542312 blink::WebMemoryCoordinator::OnMemoryPressure(
bashic577bfc2016-01-08 03:42:352313 static_cast<blink::WebMemoryPressureLevel>(memory_pressure_level));
hajimehoshi7bb39582016-10-12 04:30:572314 }
2315 if (memory_pressure_level ==
2316 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
2317 ReleaseFreeMemory();
2318 ClearMemory();
2319 }
2320}
rmcilroyab967c972015-02-17 23:15:002321
hajimehoshi7bb39582016-10-12 04:30:572322void RenderThreadImpl::OnMemoryStateChange(base::MemoryState state) {
hajimehoshi7bb39582016-10-12 04:30:572323 if (blink_platform_impl_) {
Blink Reformat1c4d759e2017-04-09 16:34:542324 blink::WebMemoryCoordinator::OnMemoryStateChange(
hajimehoshi7bb39582016-10-12 04:30:572325 static_cast<blink::MemoryState>(state));
2326 }
bashi233f65e2017-02-09 08:36:032327}
2328
2329void RenderThreadImpl::OnPurgeMemory() {
bashib9058f282017-03-30 01:55:242330 // Record amount of purged memory after 2 seconds. 2 seconds is arbitrary
2331 // but it works most cases.
2332 RendererMemoryMetrics metrics;
bashia0ba0d42017-04-03 08:11:062333 if (!GetRendererMemoryMetrics(&metrics))
2334 return;
2335
bashib9058f282017-03-30 01:55:242336 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask(
2337 FROM_HERE,
tzikff2a81a2017-09-15 05:10:062338 base::BindOnce(&RenderThreadImpl::RecordPurgeMemory,
2339 base::Unretained(this), std::move(metrics)),
bashib9058f282017-03-30 01:55:242340 base::TimeDelta::FromSeconds(2));
2341
bashi233f65e2017-02-09 08:36:032342 OnTrimMemoryImmediately();
2343 ReleaseFreeMemory();
2344 ClearMemory();
2345 if (blink_platform_impl_)
Blink Reformat1c4d759e2017-04-09 16:34:542346 blink::WebMemoryCoordinator::OnPurgeMemory();
hajimehoshi7bb39582016-10-12 04:30:572347}
2348
bashib9058f282017-03-30 01:55:242349void RenderThreadImpl::RecordPurgeMemory(RendererMemoryMetrics before) {
2350 RendererMemoryMetrics after;
bashia0ba0d42017-04-03 08:11:062351 if (!GetRendererMemoryMetrics(&after))
2352 return;
bashib9058f282017-03-30 01:55:242353 int64_t mbytes = static_cast<int64_t>(before.total_allocated_mb) -
2354 static_cast<int64_t>(after.total_allocated_mb);
2355 if (mbytes < 0)
2356 mbytes = 0;
2357 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Experimental.Renderer.PurgedMemory",
2358 mbytes);
2359}
2360
hajimehoshi7bb39582016-10-12 04:30:572361void RenderThreadImpl::ClearMemory() {
2362 // Do not call into blink if it is not initialized.
2363 if (blink_platform_impl_) {
2364 // Purge Skia font cache, by setting it to 0 and then again to the
2365 // previous limit.
2366 size_t font_cache_limit = SkGraphics::SetFontCacheLimit(0);
2367 SkGraphics::SetFontCacheLimit(font_cache_limit);
rmcilroy7fbb3bd52015-02-17 19:02:142368 }
[email protected]5071cb22013-07-10 02:19:062369}
2370
Marijn Kruisselbrink8c276412017-07-29 06:11:212371scoped_refptr<base::TaskRunner> RenderThreadImpl::GetFileThreadTaskRunner() {
2372 return blink_platform_impl_->BaseFileTaskRunner();
[email protected]c6a7b862010-08-20 22:19:382373}
[email protected]e9ff79c2012-10-19 21:31:262374
acolwellb4034942014-08-28 15:42:432375scoped_refptr<base::SingleThreadTaskRunner>
2376RenderThreadImpl::GetMediaThreadTaskRunner() {
fdoraye94d8642016-07-07 19:19:012377 DCHECK(message_loop()->task_runner()->BelongsToCurrentThread());
[email protected]c1330c82013-06-06 02:23:252378 if (!media_thread_) {
2379 media_thread_.reset(new base::Thread("Media"));
2380 media_thread_->Start();
2381 }
skyostil2d3b5bd2015-05-27 15:40:592382 return media_thread_->task_runner();
[email protected]c1330c82013-06-06 02:23:252383}
2384
dcastagna4517a182015-08-05 19:51:032385base::TaskRunner* RenderThreadImpl::GetWorkerTaskRunner() {
prashant.nfad657e2016-06-01 07:52:172386 return categorized_worker_pool_.get();
dcastagnab880e8f2015-06-30 20:16:062387}
2388
Victor Miuraff6488612017-12-21 04:16:152389scoped_refptr<viz::RasterContextProvider>
danakj0b4b94e32016-05-10 22:33:012390RenderThreadImpl::SharedCompositorWorkerContextProvider() {
revemand180dfc32015-09-24 00:19:432391 DCHECK(IsMainThread());
2392 // Try to reuse existing shared worker context provider.
revemand180dfc32015-09-24 00:19:432393 if (shared_worker_context_provider_) {
2394 // Note: If context is lost, delete reference after releasing the lock.
Victor Miura29b7ea3d2017-12-19 20:23:592395 viz::RasterContextProvider::ScopedRasterContextLock lock(
boliu11afa7e2016-04-18 18:04:142396 shared_worker_context_provider_.get());
Victor Miura29b7ea3d2017-12-19 20:23:592397 if (lock.RasterInterface()->GetGraphicsResetStatusKHR() == GL_NO_ERROR)
danakje8ec797e2016-04-19 04:08:432398 return shared_worker_context_provider_;
revemand180dfc32015-09-24 00:19:432399 }
danakje8ec797e2016-04-19 04:08:432400
sadrul6d310fa2016-08-04 02:12:162401 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(
2402 EstablishGpuChannelSync());
danakje8ec797e2016-04-19 04:08:432403 if (!gpu_channel_host) {
2404 shared_worker_context_provider_ = nullptr;
2405 return shared_worker_context_provider_;
revemand180dfc32015-09-24 00:19:432406 }
danakje8ec797e2016-04-19 04:08:432407
danakj9a04adc2016-05-16 22:45:072408 bool support_locking = true;
Adrienne Walker436a7752017-08-28 23:33:092409 bool support_oop_rasterization =
2410 base::CommandLine::ForCurrentProcess()->HasSwitch(
2411 switches::kEnableOOPRasterization);
Victor Miuraff6488612017-12-21 04:16:152412 bool support_gles2_interface = false;
Victor Miura3a4ad4f82017-12-13 06:03:452413 bool support_raster_interface = true;
danakj0dd9e1e2016-05-11 22:15:092414 shared_worker_context_provider_ = CreateOffscreenContext(
Antoine Labour5ac65db2017-12-19 18:02:582415 std::move(gpu_channel_host), GetGpuMemoryBufferManager(),
2416 gpu::SharedMemoryLimits(), support_locking, support_gles2_interface,
2417 support_raster_interface, support_oop_rasterization,
Victor Miuraff6488612017-12-21 04:16:152418 ui::command_buffer_metrics::RENDER_WORKER_CONTEXT, kGpuStreamIdWorker,
2419 kGpuStreamPriorityWorker);
danakj45cfd232017-10-18 19:31:312420 auto result = shared_worker_context_provider_->BindToCurrentThread();
2421 if (result != gpu::ContextResult::kSuccess)
danakje8ec797e2016-04-19 04:08:432422 shared_worker_context_provider_ = nullptr;
revemand180dfc32015-09-24 00:19:432423 return shared_worker_context_provider_;
2424}
2425
juncai2f298a82017-04-18 03:51:392426void RenderThreadImpl::SampleGamepads(device::Gamepads* data) {
Blink Reformat1c4d759e2017-04-09 16:34:542427 blink_platform_impl_->SampleGamepads(*data);
[email protected]0ff736562014-05-09 09:09:472428}
2429
rmcilroyaa296052015-04-14 15:35:272430bool RenderThreadImpl::RendererIsHidden() const {
2431 return widget_count_ > 0 && hidden_widget_count_ == widget_count_;
2432}
2433
[email protected]b2db9272014-01-10 17:42:002434void RenderThreadImpl::WidgetCreated() {
rmcilroyaa296052015-04-14 15:35:272435 bool renderer_was_hidden = RendererIsHidden();
[email protected]b2db9272014-01-10 17:42:002436 widget_count_++;
rmcilroyaa296052015-04-14 15:35:272437 if (renderer_was_hidden)
2438 OnRendererVisible();
[email protected]b2db9272014-01-10 17:42:002439}
2440
2441void RenderThreadImpl::WidgetDestroyed() {
rmcilroyaa296052015-04-14 15:35:272442 // TODO(rmcilroy): Remove the restriction that destroyed widgets must be
2443 // unhidden before WidgetDestroyed is called.
2444 DCHECK_GT(widget_count_, 0);
2445 DCHECK_GT(widget_count_, hidden_widget_count_);
[email protected]b2db9272014-01-10 17:42:002446 widget_count_--;
rmcilroyaa296052015-04-14 15:35:272447 if (RendererIsHidden())
2448 OnRendererHidden();
[email protected]b2db9272014-01-10 17:42:002449}
2450
2451void RenderThreadImpl::WidgetHidden() {
2452 DCHECK_LT(hidden_widget_count_, widget_count_);
2453 hidden_widget_count_++;
rmcilroyaa296052015-04-14 15:35:272454 if (RendererIsHidden())
2455 OnRendererHidden();
[email protected]b2db9272014-01-10 17:42:002456}
2457
2458void RenderThreadImpl::WidgetRestored() {
rmcilroyaa296052015-04-14 15:35:272459 bool renderer_was_hidden = RendererIsHidden();
[email protected]b2db9272014-01-10 17:42:002460 DCHECK_GT(hidden_widget_count_, 0);
2461 hidden_widget_count_--;
rmcilroyaa296052015-04-14 15:35:272462 if (renderer_was_hidden)
2463 OnRendererVisible();
2464}
[email protected]b2db9272014-01-10 17:42:002465
rmcilroyaa296052015-04-14 15:35:272466void RenderThreadImpl::OnRendererHidden() {
Blink Reformat1c4d759e2017-04-09 16:34:542467 blink::MainThreadIsolate()->IsolateInBackgroundNotification();
rmcilroyaa296052015-04-14 15:35:272468 // TODO(rmcilroy): Remove IdleHandler and replace it with an IdleTask
2469 // scheduled by the RendererScheduler - http://crbug.com/469210.
ulan4a385192015-11-11 10:59:182470 if (!GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
2471 return;
Alexander Timin771b3372017-06-15 13:26:472472 renderer_scheduler_->SetRendererHidden(true);
ulan4a385192015-11-11 10:59:182473 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
rmcilroyaa296052015-04-14 15:35:272474}
2475
2476void RenderThreadImpl::OnRendererVisible() {
Blink Reformat1c4d759e2017-04-09 16:34:542477 blink::MainThreadIsolate()->IsolateInForegroundNotification();
rmcilroyaa296052015-04-14 15:35:272478 if (!GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
[email protected]b2db9272014-01-10 17:42:002479 return;
Alexander Timin771b3372017-06-15 13:26:472480 renderer_scheduler_->SetRendererHidden(false);
[email protected]b2db9272014-01-10 17:42:002481 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
2482}
2483
ssid0603ca9f2015-06-09 16:48:082484void RenderThreadImpl::ReleaseFreeMemory() {
2485 base::allocator::ReleaseFreeMemory();
penghuang342762b2016-12-02 21:04:582486 discardable_shared_memory_manager_->ReleaseFreeMemory();
ssid0603ca9f2015-06-09 16:48:082487
2488 if (blink_platform_impl_)
Blink Reformat1c4d759e2017-04-09 16:34:542489 blink::DecommitFreeableMemory();
ssid0603ca9f2015-06-09 16:48:082490}
2491
ben76f52b242016-06-18 05:42:482492RenderThreadImpl::PendingFrameCreate::PendingFrameCreate(
benf28ce882017-05-02 16:15:492493 const service_manager::BindSourceInfo& browser_info,
rockotf8fdd9b2015-12-16 22:22:352494 int routing_id,
Balazs Engedyba034e72017-10-27 22:26:282495 mojom::FrameRequest frame_request)
benf28ce882017-05-02 16:15:492496 : browser_info_(browser_info),
2497 routing_id_(routing_id),
Balazs Engedyba034e72017-10-27 22:26:282498 frame_request_(std::move(frame_request)) {}
rockotf8fdd9b2015-12-16 22:22:352499
ben76f52b242016-06-18 05:42:482500RenderThreadImpl::PendingFrameCreate::~PendingFrameCreate() {
rockotf8fdd9b2015-12-16 22:22:352501}
2502
ben76f52b242016-06-18 05:42:482503void RenderThreadImpl::PendingFrameCreate::OnConnectionError() {
rockotf8fdd9b2015-12-16 22:22:352504 size_t erased =
ben76f52b242016-06-18 05:42:482505 RenderThreadImpl::current()->pending_frame_creates_.erase(routing_id_);
rockotf8fdd9b2015-12-16 22:22:352506 DCHECK_EQ(1u, erased);
2507}
2508
hong.zhengb28b5332016-05-11 02:33:392509void RenderThreadImpl::OnSyncMemoryPressure(
2510 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
Blink Reformat1c4d759e2017-04-09 16:34:542511 if (!blink::MainThreadIsolate())
hong.zhengb28b5332016-05-11 02:33:392512 return;
2513
2514 v8::MemoryPressureLevel v8_memory_pressure_level =
2515 static_cast<v8::MemoryPressureLevel>(memory_pressure_level);
2516
Luke Halliwell16136de2017-07-05 21:15:562517#if !BUILDFLAG(ALLOW_CRITICAL_MEMORY_PRESSURE_HANDLING_IN_FOREGROUND)
hong.zhengb28b5332016-05-11 02:33:392518 // In order to reduce performance impact, translate critical level to
Luke Halliwell16136de2017-07-05 21:15:562519 // moderate level for foreground renderer.
hong.zhengb28b5332016-05-11 02:33:392520 if (!RendererIsHidden() &&
2521 v8_memory_pressure_level == v8::MemoryPressureLevel::kCritical)
2522 v8_memory_pressure_level = v8::MemoryPressureLevel::kModerate;
Luke Halliwell16136de2017-07-05 21:15:562523#endif // !BUILDFLAG(ALLOW_CRITICAL_MEMORY_PRESSURE_HANDLING_IN_FOREGROUND)
hong.zhengb28b5332016-05-11 02:33:392524
Blink Reformat1c4d759e2017-04-09 16:34:542525 blink::MainThreadIsolate()->MemoryPressureNotification(
hong.zhengb28b5332016-05-11 02:33:392526 v8_memory_pressure_level);
2527 blink::MemoryPressureNotificationToWorkerThreadIsolates(
2528 v8_memory_pressure_level);
2529}
2530
bashic4b4afcb2016-08-23 06:37:522531// Note that this would be called only when memory_coordinator is enabled.
2532// OnSyncMemoryPressure() is never called in that case.
2533void RenderThreadImpl::OnTrimMemoryImmediately() {
Blink Reformat1c4d759e2017-04-09 16:34:542534 if (blink::MainThreadIsolate()) {
2535 blink::MainThreadIsolate()->MemoryPressureNotification(
bashic4b4afcb2016-08-23 06:37:522536 v8::MemoryPressureLevel::kCritical);
2537 blink::MemoryPressureNotificationToWorkerThreadIsolates(
2538 v8::MemoryPressureLevel::kCritical);
2539 }
2540}
2541
rockot067ca55f2016-09-30 22:00:152542void RenderThreadImpl::OnRendererInterfaceRequest(
2543 mojom::RendererAssociatedRequest request) {
2544 DCHECK(!renderer_binding_.is_bound());
2545 renderer_binding_.Bind(std::move(request));
2546}
bashic4b4afcb2016-08-23 06:37:522547
Takashi SAKAMOTO870f6262017-08-22 04:08:272548bool RenderThreadImpl::NeedsToRecordFirstActivePaint(
2549 int ttfap_metric_type) const {
2550 if (ttfap_metric_type == RenderWidget::TTFAP_AFTER_PURGED)
2551 return needs_to_record_first_active_paint_;
2552
2553 if (was_backgrounded_time_.is_min())
2554 return false;
2555 base::TimeDelta passed = base::TimeTicks::Now() - was_backgrounded_time_;
2556 return passed.InMinutes() >= 5;
2557}
2558
Miguel Casas5e1018052018-01-09 19:17:362559void RenderThreadImpl::SetRenderingColorSpace(
2560 const gfx::ColorSpace& color_space) {
2561 DCHECK(IsMainThread());
2562 rendering_color_space_ = color_space;
2563
2564 for (const auto& factories : gpu_factories_) {
2565 if (factories)
2566 factories->SetRenderingColorSpace(color_space);
2567 }
2568}
2569
[email protected]e9ff79c2012-10-19 21:31:262570} // namespace content