Commit a17997b9 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

Media Controls: Avoid style recalculation

When generating the media controls DOM we were recalculating
styles each time we called 'AppendChild'. This moves us
to using 'ParserAppendChild' which does not do this.

According to manual testing this reduces the time calculating
styles from 230ms to 139ms.

BUG=821961,821414

Change-Id: I0d673ddc6e12ff81a311315466be735a7673c1bd
Reviewed-on: https://chromium-review.googlesource.com/998187
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548872}
parent 95f066e0
...@@ -155,10 +155,10 @@ bool ShouldShowFullscreenButton(const HTMLMediaElement& media_element) { ...@@ -155,10 +155,10 @@ bool ShouldShowFullscreenButton(const HTMLMediaElement& media_element) {
return true; return true;
} }
void MaybeAppendChild(Element* parent, Element* child) { void MaybeParserAppendChild(Element* parent, Element* child) {
DCHECK(parent); DCHECK(parent);
if (child) if (child)
parent->AppendChild(child); parent->ParserAppendChild(child);
} }
bool ShouldShowPictureInPictureButton(HTMLMediaElement& media_element) { bool ShouldShowPictureInPictureButton(HTMLMediaElement& media_element) {
...@@ -410,7 +410,7 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element, ...@@ -410,7 +410,7 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element,
MediaControlsResourceLoader::InjectMediaControlsUAStyleSheet(); MediaControlsResourceLoader::InjectMediaControlsUAStyleSheet();
shadow_root.AppendChild(controls); shadow_root.ParserAppendChild(controls);
return controls; return controls;
} }
...@@ -487,7 +487,7 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element, ...@@ -487,7 +487,7 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element,
void MediaControlsImpl::InitializeControls() { void MediaControlsImpl::InitializeControls() {
if (IsModern() && MediaElement().IsHTMLVideoElement()) { if (IsModern() && MediaElement().IsHTMLVideoElement()) {
loading_panel_ = new MediaControlLoadingPanelElement(*this); loading_panel_ = new MediaControlLoadingPanelElement(*this);
AppendChild(loading_panel_); ParserAppendChild(loading_panel_);
} }
overlay_enclosure_ = new MediaControlOverlayEnclosureElement(*this); overlay_enclosure_ = new MediaControlOverlayEnclosureElement(*this);
...@@ -497,13 +497,13 @@ void MediaControlsImpl::InitializeControls() { ...@@ -497,13 +497,13 @@ void MediaControlsImpl::InitializeControls() {
overlay_play_button_ = new MediaControlOverlayPlayButtonElement(*this); overlay_play_button_ = new MediaControlOverlayPlayButtonElement(*this);
if (!IsModern()) if (!IsModern())
overlay_enclosure_->AppendChild(overlay_play_button_); overlay_enclosure_->ParserAppendChild(overlay_play_button_);
} }
overlay_cast_button_ = new MediaControlCastButtonElement(*this, true); overlay_cast_button_ = new MediaControlCastButtonElement(*this, true);
overlay_enclosure_->AppendChild(overlay_cast_button_); overlay_enclosure_->ParserAppendChild(overlay_cast_button_);
AppendChild(overlay_enclosure_); ParserAppendChild(overlay_enclosure_);
// Create an enclosing element for the panel so we can visually offset the // Create an enclosing element for the panel so we can visually offset the
// controls correctly. // controls correctly.
...@@ -549,36 +549,35 @@ void MediaControlsImpl::InitializeControls() { ...@@ -549,36 +549,35 @@ void MediaControlsImpl::InitializeControls() {
overflow_menu_ = new MediaControlOverflowMenuButtonElement(*this); overflow_menu_ = new MediaControlOverflowMenuButtonElement(*this);
PopulatePanel(); PopulatePanel();
enclosure_->AppendChild(panel_); enclosure_->ParserAppendChild(panel_);
AppendChild(enclosure_); ParserAppendChild(enclosure_);
text_track_list_ = new MediaControlTextTrackListElement(*this); text_track_list_ = new MediaControlTextTrackListElement(*this);
AppendChild(text_track_list_); ParserAppendChild(text_track_list_);
overflow_list_ = new MediaControlOverflowMenuListElement(*this); overflow_list_ = new MediaControlOverflowMenuListElement(*this);
AppendChild(overflow_list_); ParserAppendChild(overflow_list_);
// The order in which we append elements to the overflow list is significant // The order in which we append elements to the overflow list is significant
// because it determines how the elements show up in the overflow menu // because it determines how the elements show up in the overflow menu
// relative to each other. The first item appended appears at the top of the // relative to each other. The first item appended appears at the top of the
// overflow menu. // overflow menu.
overflow_list_->AppendChild(play_button_->CreateOverflowElement( overflow_list_->ParserAppendChild(play_button_->CreateOverflowElement(
new MediaControlPlayButtonElement(*this))); new MediaControlPlayButtonElement(*this)));
overflow_list_->AppendChild(fullscreen_button_->CreateOverflowElement( overflow_list_->ParserAppendChild(fullscreen_button_->CreateOverflowElement(
new MediaControlFullscreenButtonElement(*this))); new MediaControlFullscreenButtonElement(*this)));
overflow_list_->AppendChild(download_button_->CreateOverflowElement( overflow_list_->ParserAppendChild(download_button_->CreateOverflowElement(
new MediaControlDownloadButtonElement(*this))); new MediaControlDownloadButtonElement(*this)));
overflow_list_->AppendChild(mute_button_->CreateOverflowElement( overflow_list_->ParserAppendChild(mute_button_->CreateOverflowElement(
new MediaControlMuteButtonElement(*this))); new MediaControlMuteButtonElement(*this)));
overflow_list_->AppendChild(cast_button_->CreateOverflowElement( overflow_list_->ParserAppendChild(cast_button_->CreateOverflowElement(
new MediaControlCastButtonElement(*this, false))); new MediaControlCastButtonElement(*this, false)));
overflow_list_->AppendChild( overflow_list_->ParserAppendChild(
toggle_closed_captions_button_->CreateOverflowElement( toggle_closed_captions_button_->CreateOverflowElement(
new MediaControlToggleClosedCaptionsButtonElement(*this))); new MediaControlToggleClosedCaptionsButtonElement(*this)));
if (RuntimeEnabledFeatures::PictureInPictureEnabled()) { if (RuntimeEnabledFeatures::PictureInPictureEnabled()) {
overflow_list_->AppendChild( overflow_list_->ParserAppendChild(
picture_in_picture_button_->CreateOverflowElement( picture_in_picture_button_->CreateOverflowElement(
new MediaControlPictureInPictureButtonElement(*this))); new MediaControlPictureInPictureButtonElement(*this)));
} }
...@@ -596,9 +595,9 @@ void MediaControlsImpl::PopulatePanel() { ...@@ -596,9 +595,9 @@ void MediaControlsImpl::PopulatePanel() {
Element* button_panel = panel_; Element* button_panel = panel_;
if (IsModern() && MediaElement().IsHTMLVideoElement() && if (IsModern() && MediaElement().IsHTMLVideoElement() &&
!is_acting_as_audio_controls_) { !is_acting_as_audio_controls_) {
MaybeAppendChild(panel_, scrubbing_message_); MaybeParserAppendChild(panel_, scrubbing_message_);
panel_->AppendChild(overlay_play_button_); panel_->ParserAppendChild(overlay_play_button_);
panel_->AppendChild(media_button_panel_); panel_->ParserAppendChild(media_button_panel_);
button_panel = media_button_panel_; button_panel = media_button_panel_;
} }
...@@ -606,35 +605,35 @@ void MediaControlsImpl::PopulatePanel() { ...@@ -606,35 +605,35 @@ void MediaControlsImpl::PopulatePanel() {
// only or are using the legacy controls. // only or are using the legacy controls.
if (!IsModern() || is_acting_as_audio_controls_ || if (!IsModern() || is_acting_as_audio_controls_ ||
MediaElement().IsHTMLAudioElement()) { MediaElement().IsHTMLAudioElement()) {
button_panel->AppendChild(play_button_); button_panel->ParserAppendChild(play_button_);
} }
button_panel->AppendChild(current_time_display_); button_panel->ParserAppendChild(current_time_display_);
button_panel->AppendChild(duration_display_); button_panel->ParserAppendChild(duration_display_);
if (IsModern() && MediaElement().IsHTMLVideoElement()) { if (IsModern() && MediaElement().IsHTMLVideoElement()) {
MediaControlElementsHelper::CreateDiv( MediaControlElementsHelper::CreateDiv(
"-internal-media-controls-button-spacer", button_panel); "-internal-media-controls-button-spacer", button_panel);
} }
panel_->AppendChild(timeline_); panel_->ParserAppendChild(timeline_);
button_panel->AppendChild(mute_button_); button_panel->ParserAppendChild(mute_button_);
MaybeAppendChild(button_panel, volume_slider_); MaybeParserAppendChild(button_panel, volume_slider_);
MaybeAppendChild(button_panel, picture_in_picture_button_); MaybeParserAppendChild(button_panel, picture_in_picture_button_);
button_panel->AppendChild(fullscreen_button_); button_panel->ParserAppendChild(fullscreen_button_);
// The download, cast and captions buttons should not be present on the modern // The download, cast and captions buttons should not be present on the modern
// controls button panel. // controls button panel.
if (!IsModern()) { if (!IsModern()) {
button_panel->AppendChild(download_button_); button_panel->ParserAppendChild(download_button_);
button_panel->AppendChild(cast_button_); button_panel->ParserAppendChild(cast_button_);
button_panel->AppendChild(toggle_closed_captions_button_); button_panel->ParserAppendChild(toggle_closed_captions_button_);
} }
button_panel->AppendChild(overflow_menu_); button_panel->ParserAppendChild(overflow_menu_);
} }
Node::InsertionNotificationRequest MediaControlsImpl::InsertedInto( Node::InsertionNotificationRequest MediaControlsImpl::InsertedInto(
......
...@@ -82,7 +82,7 @@ HTMLDivElement* MediaControlElementsHelper::CreateDiv(const AtomicString& id, ...@@ -82,7 +82,7 @@ HTMLDivElement* MediaControlElementsHelper::CreateDiv(const AtomicString& id,
DCHECK(parent); DCHECK(parent);
HTMLDivElement* element = HTMLDivElement::Create(parent->GetDocument()); HTMLDivElement* element = HTMLDivElement::Create(parent->GetDocument());
element->SetShadowPseudoId(id); element->SetShadowPseudoId(id);
parent->AppendChild(element); parent->ParserAppendChild(element);
return element; return element;
} }
...@@ -112,7 +112,7 @@ HTMLDivElement* MediaControlElementsHelper::CreateDivWithId( ...@@ -112,7 +112,7 @@ HTMLDivElement* MediaControlElementsHelper::CreateDivWithId(
DCHECK(parent); DCHECK(parent);
HTMLDivElement* element = HTMLDivElement::Create(parent->GetDocument()); HTMLDivElement* element = HTMLDivElement::Create(parent->GetDocument());
element->setAttribute("id", id); element->setAttribute("id", id);
parent->AppendChild(element); parent->ParserAppendChild(element);
return element; return element;
} }
......
...@@ -66,15 +66,15 @@ HTMLElement* MediaControlInputElement::CreateOverflowElement( ...@@ -66,15 +66,15 @@ HTMLElement* MediaControlInputElement::CreateOverflowElement(
AtomicString("-internal-media-controls-overflow-menu-list-item")); AtomicString("-internal-media-controls-overflow-menu-list-item"));
// Appending a button to a label element ensures that clicks on the label // Appending a button to a label element ensures that clicks on the label
// are passed down to the button, performing the action we'd expect. // are passed down to the button, performing the action we'd expect.
element->AppendChild(button); element->ParserAppendChild(button);
if (MediaControlsImpl::IsModern()) { if (MediaControlsImpl::IsModern()) {
overflow_menu_container_ = HTMLDivElement::Create(GetDocument()); overflow_menu_container_ = HTMLDivElement::Create(GetDocument());
overflow_menu_container_->AppendChild(overflow_menu_text_); overflow_menu_container_->ParserAppendChild(overflow_menu_text_);
UpdateOverflowSubtitleElement(button->GetOverflowMenuSubtitleString()); UpdateOverflowSubtitleElement(button->GetOverflowMenuSubtitleString());
element->AppendChild(overflow_menu_container_); element->ParserAppendChild(overflow_menu_container_);
} else { } else {
element->AppendChild(overflow_menu_text_); element->ParserAppendChild(overflow_menu_text_);
} }
// Initialize the internal states of the main element and the overflow one. // Initialize the internal states of the main element and the overflow one.
...@@ -108,7 +108,7 @@ void MediaControlInputElement::UpdateOverflowSubtitleElement(String text) { ...@@ -108,7 +108,7 @@ void MediaControlInputElement::UpdateOverflowSubtitleElement(String text) {
overflow_menu_subtitle_->setInnerText(text, ASSERT_NO_EXCEPTION); overflow_menu_subtitle_->setInnerText(text, ASSERT_NO_EXCEPTION);
overflow_menu_subtitle_->setAttribute("class", kOverflowSubtitleCSSClass); overflow_menu_subtitle_->setAttribute("class", kOverflowSubtitleCSSClass);
overflow_menu_container_->AppendChild(overflow_menu_subtitle_); overflow_menu_container_->ParserAppendChild(overflow_menu_subtitle_);
overflow_menu_container_->setAttribute( overflow_menu_container_->setAttribute(
"class", kOverflowContainerWithSubtitleCSSClass); "class", kOverflowContainerWithSubtitleCSSClass);
} }
......
...@@ -63,7 +63,7 @@ void MediaControlLoadingPanelElement::PopulateShadowDOM() { ...@@ -63,7 +63,7 @@ void MediaControlLoadingPanelElement::PopulateShadowDOM() {
auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags()); auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags());
style->setTextContent( style->setTextContent(
MediaControlsResourceLoader::GetShadowLoadingStyleSheet()); MediaControlsResourceLoader::GetShadowLoadingStyleSheet());
shadow_root->AppendChild(style); shadow_root->ParserAppendChild(style);
// The spinner frame is centers the spinner in the middle of the element and // The spinner frame is centers the spinner in the middle of the element and
// cuts off any overflowing content. It also contains a SVG mask which will // cuts off any overflowing content. It also contains a SVG mask which will
......
...@@ -182,17 +182,18 @@ void MediaControlOverlayPlayButtonElement::MaybeJump(int seconds) { ...@@ -182,17 +182,18 @@ void MediaControlOverlayPlayButtonElement::MaybeJump(int seconds) {
auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags()); auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags());
style->setTextContent( style->setTextContent(
MediaControlsResourceLoader::GetOverlayPlayStyleSheet()); MediaControlsResourceLoader::GetOverlayPlayStyleSheet());
shadow_root->AppendChild(style); shadow_root->ParserAppendChild(style);
// Insert the left jump arrow to the left of the play button. // Insert the left jump arrow to the left of the play button.
left_jump_arrow_ = new MediaControlOverlayPlayButtonElement::AnimatedArrow( left_jump_arrow_ = new MediaControlOverlayPlayButtonElement::AnimatedArrow(
"left-arrow", GetDocument()); "left-arrow", GetDocument());
shadow_root->InsertBefore(left_jump_arrow_, shadow_root->firstChild()); shadow_root->ParserInsertBefore(left_jump_arrow_,
*shadow_root->firstChild());
// Insert the right jump arrow to the right of the play button. // Insert the right jump arrow to the right of the play button.
right_jump_arrow_ = new MediaControlOverlayPlayButtonElement::AnimatedArrow( right_jump_arrow_ = new MediaControlOverlayPlayButtonElement::AnimatedArrow(
"right-arrow", GetDocument()); "right-arrow", GetDocument());
shadow_root->AppendChild(right_jump_arrow_); shadow_root->ParserAppendChild(right_jump_arrow_);
} }
DCHECK(left_jump_arrow_ && right_jump_arrow_); DCHECK(left_jump_arrow_ && right_jump_arrow_);
......
...@@ -32,7 +32,7 @@ void MediaControlScrubbingMessageElement::PopulateChildren() { ...@@ -32,7 +32,7 @@ void MediaControlScrubbingMessageElement::PopulateChildren() {
HTMLStyleElement::Create(GetDocument(), CreateElementFlags()); HTMLStyleElement::Create(GetDocument(), CreateElementFlags());
style->setTextContent( style->setTextContent(
MediaControlsResourceLoader::GetScrubbingMessageStyleSheet()); MediaControlsResourceLoader::GetScrubbingMessageStyleSheet());
shadow_root->AppendChild(style); shadow_root->ParserAppendChild(style);
HTMLDivElement* arrow_left_div1 = HTMLDivElement* arrow_left_div1 =
MediaControlElementsHelper::CreateDivWithId("arrow-left1", shadow_root); MediaControlElementsHelper::CreateDivWithId("arrow-left1", shadow_root);
......
...@@ -120,11 +120,11 @@ Element* MediaControlTextTrackListElement::CreateTextTrackListItem( ...@@ -120,11 +120,11 @@ Element* MediaControlTextTrackListElement::CreateTextTrackListItem(
// Modern media controls should have the checkbox after the text instead of // Modern media controls should have the checkbox after the text instead of
// the other way around. // the other way around.
if (!MediaControlsImpl::IsModern()) if (!MediaControlsImpl::IsModern())
track_item->AppendChild(track_item_input); track_item->ParserAppendChild(track_item_input);
String track_label = GetMediaControls().GetTextTrackLabel(track); String track_label = GetMediaControls().GetTextTrackLabel(track);
track_item->AppendChild(Text::Create(GetDocument(), track_label)); track_item->ParserAppendChild(Text::Create(GetDocument(), track_label));
if (MediaControlsImpl::IsModern()) if (MediaControlsImpl::IsModern())
track_item->AppendChild(track_item_input); track_item->ParserAppendChild(track_item_input);
// Add a track kind marker icon if there are multiple tracks with the same // Add a track kind marker icon if there are multiple tracks with the same
// label or if the track has no label. // label or if the track has no label.
...@@ -138,7 +138,7 @@ Element* MediaControlTextTrackListElement::CreateTextTrackListItem( ...@@ -138,7 +138,7 @@ Element* MediaControlTextTrackListElement::CreateTextTrackListItem(
track_kind_marker->SetShadowPseudoId(AtomicString( track_kind_marker->SetShadowPseudoId(AtomicString(
"-internal-media-controls-text-track-list-kind-subtitles")); "-internal-media-controls-text-track-list-kind-subtitles"));
} }
track_item->AppendChild(track_kind_marker); track_item->ParserAppendChild(track_kind_marker);
} }
return track_item; return track_item;
} }
...@@ -147,7 +147,7 @@ Element* MediaControlTextTrackListElement::CreateTextTrackHeaderItem() { ...@@ -147,7 +147,7 @@ Element* MediaControlTextTrackListElement::CreateTextTrackHeaderItem() {
HTMLLabelElement* header_item = HTMLLabelElement::Create(GetDocument()); HTMLLabelElement* header_item = HTMLLabelElement::Create(GetDocument());
header_item->SetShadowPseudoId( header_item->SetShadowPseudoId(
"-internal-media-controls-text-track-list-header"); "-internal-media-controls-text-track-list-header");
header_item->AppendChild( header_item->ParserAppendChild(
Text::Create(GetDocument(), Text::Create(GetDocument(),
GetLocale().QueryString( GetLocale().QueryString(
WebLocalizedString::kOverflowMenuCaptionsSubmenuTitle))); WebLocalizedString::kOverflowMenuCaptionsSubmenuTitle)));
...@@ -164,18 +164,18 @@ void MediaControlTextTrackListElement::RefreshTextTrackListMenu() { ...@@ -164,18 +164,18 @@ void MediaControlTextTrackListElement::RefreshTextTrackListMenu() {
RemoveChildren(kOmitSubtreeModifiedEvent); RemoveChildren(kOmitSubtreeModifiedEvent);
if (MediaControlsImpl::IsModern()) if (MediaControlsImpl::IsModern())
AppendChild(CreateTextTrackHeaderItem()); ParserAppendChild(CreateTextTrackHeaderItem());
// Construct a menu for subtitles and captions. Pass in a nullptr to // Construct a menu for subtitles and captions. Pass in a nullptr to
// createTextTrackListItem to create the "Off" track item. // createTextTrackListItem to create the "Off" track item.
AppendChild(CreateTextTrackListItem(nullptr)); ParserAppendChild(CreateTextTrackListItem(nullptr));
TextTrackList* track_list = MediaElement().textTracks(); TextTrackList* track_list = MediaElement().textTracks();
for (unsigned i = 0; i < track_list->length(); i++) { for (unsigned i = 0; i < track_list->length(); i++) {
TextTrack* track = track_list->AnonymousIndexedGetter(i); TextTrack* track = track_list->AnonymousIndexedGetter(i);
if (!track->CanBeRendered()) if (!track->CanBeRendered())
continue; continue;
AppendChild(CreateTextTrackListItem(track)); ParserAppendChild(CreateTextTrackListItem(track));
} }
} }
......
...@@ -77,7 +77,7 @@ MediaControlTimelineElement::MediaControlTimelineElement( ...@@ -77,7 +77,7 @@ MediaControlTimelineElement::MediaControlTimelineElement(
auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags()); auto* style = HTMLStyleElement::Create(GetDocument(), CreateElementFlags());
style->setTextContent( style->setTextContent(
MediaControlsResourceLoader::GetShadowTimelineStyleSheet()); MediaControlsResourceLoader::GetShadowTimelineStyleSheet());
track.AppendChild(style); track.ParserAppendChild(style);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment