#include "cc/trees/layer_tree_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <iterator>
#include <limits>
#include <memory>
#include <set>
#include <unordered_set>
#include <utility>
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/stack_allocated.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "cc/base/devtools_instrumentation.h"
#include "cc/base/features.h"
#include "cc/base/histograms.h"
#include "cc/base/math_util.h"
#include "cc/base/synced_property.h"
#include "cc/input/browser_controls_offset_manager.h"
#include "cc/input/page_scale_animation.h"
#include "cc/input/scrollbar_animation_controller.h"
#include "cc/layers/effect_tree_layer_list_iterator.h"
#include "cc/layers/heads_up_display_layer_impl.h"
#include "cc/layers/layer.h"
#include "cc/layers/render_surface_impl.h"
#include "cc/layers/scrollbar_layer_impl_base.h"
#include "cc/resources/ui_resource_request.h"
#include "cc/trees/clip_node.h"
#include "cc/trees/draw_property_utils.h"
#include "cc/trees/effect_node.h"
#include "cc/trees/layer_tree_frame_sink.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "cc/trees/mutator_host.h"
#include "cc/trees/occlusion_tracker.h"
#include "cc/trees/property_tree.h"
#include "cc/trees/scroll_node.h"
#include "cc/trees/transform_node.h"
#include "cc/trees/tree_synchronizer.h"
#include "cc/view_transition/view_transition_request.h"
#include "components/viz/common/traced_value.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
namespace cc {
namespace {
class ViewportAnchor { … };
std::pair<gfx::PointF, gfx::PointF> GetVisibleSelectionEndPoints(
const gfx::RectF& rect,
const gfx::PointF& top,
const gfx::PointF& bottom) { … }
}
void LayerTreeLifecycle::AdvanceTo(LifecycleState next_state) { … }
LayerTreeImpl::LayerTreeImpl(
LayerTreeHostImpl& host_impl,
scoped_refptr<SyncedScale> page_scale_factor,
scoped_refptr<SyncedBrowserControls> top_controls_shown_ratio,
scoped_refptr<SyncedBrowserControls> bottom_controls_shown_ratio,
scoped_refptr<SyncedElasticOverscroll> elastic_overscroll)
: … { … }
LayerTreeImpl::~LayerTreeImpl() { … }
void LayerTreeImpl::Shutdown() { … }
void LayerTreeImpl::ReleaseResources() { … }
void LayerTreeImpl::OnPurgeMemory() { … }
void LayerTreeImpl::ReleaseTileResources() { … }
void LayerTreeImpl::RecreateTileResources() { … }
void LayerTreeImpl::SetVisible(bool visible) { … }
void LayerTreeImpl::DidUpdateScrollOffset(
ElementId id,
bool pushed_from_main_or_pending_tree) { … }
void LayerTreeImpl::UpdateAllScrollbarGeometries() { … }
void LayerTreeImpl::UpdateViewportScrollbarGeometries() { … }
void LayerTreeImpl::UpdateScrollbarGeometries(const ScrollNode& scroll_node) { … }
const RenderSurfaceImpl* LayerTreeImpl::RootRenderSurface() const { … }
bool LayerTreeImpl::LayerListIsEmpty() const { … }
void LayerTreeImpl::SetRootLayerForTesting(std::unique_ptr<LayerImpl> layer) { … }
void LayerTreeImpl::OnCanDrawStateChangedForTree() { … }
void LayerTreeImpl::InvalidateRegionForImages(
const PaintImageIdFlatSet& images_to_invalidate) { … }
void LayerTreeImpl::InvalidateRasterInducingScrolls(
const base::flat_set<ElementId>& scrolls_to_invalidate) { … }
void LayerTreeImpl::UpdateViewportContainerSizes() { … }
bool LayerTreeImpl::IsRootLayer(const LayerImpl* layer) const { … }
gfx::PointF LayerTreeImpl::TotalScrollOffset() const { … }
gfx::PointF LayerTreeImpl::TotalMaxScrollOffset() const { … }
OwnedLayerImplList LayerTreeImpl::DetachLayers() { … }
OwnedLayerImplList LayerTreeImpl::DetachLayersKeepingRootLayerForTesting() { … }
void LayerTreeImpl::SetPropertyTrees(PropertyTrees& property_trees,
bool preserve_change_tracking) { … }
void LayerTreeImpl::SetPropertyTrees(const PropertyTrees& property_trees,
PropertyTreesChangeState& change_state,
bool preserve_change_tracking) { … }
void LayerTreeImpl::PullPropertiesFrom(
CommitState& commit_state,
const ThreadUnsafeCommitState& unsafe_state) { … }
void LayerTreeImpl::PullPropertyTreesFrom(
CommitState& commit_state,
const ThreadUnsafeCommitState& unsafe_state) { … }
void LayerTreeImpl::PullLayerTreePropertiesFrom(CommitState& commit_state) { … }
void LayerTreeImpl::PushPropertyTreesTo(LayerTreeImpl* target_tree) { … }
void LayerTreeImpl::PushSurfaceRangesTo(LayerTreeImpl* target_tree) { … }
void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl* target_tree) { … }
void LayerTreeImpl::HandleTickmarksVisibilityChange() { … }
void LayerTreeImpl::RequestShowScrollbars(ElementId scroll_element_id) { … }
void LayerTreeImpl::HandleScrollbarShowRequests() { … }
void LayerTreeImpl::MoveChangeTrackingToLayers() { … }
void LayerTreeImpl::ForceRecalculateRasterScales() { … }
bool LayerTreeImpl::IsElementInPropertyTree(ElementId element_id) const { … }
ElementListType LayerTreeImpl::GetElementTypeForAnimation() const { … }
void LayerTreeImpl::SetTransformMutated(ElementId element_id,
const gfx::Transform& transform) { … }
void LayerTreeImpl::SetOpacityMutated(ElementId element_id, float opacity) { … }
void LayerTreeImpl::SetFilterMutated(ElementId element_id,
const FilterOperations& filters) { … }
void LayerTreeImpl::SetBackdropFilterMutated(
ElementId element_id,
const FilterOperations& backdrop_filters) { … }
void LayerTreeImpl::AddPresentationCallbacks(
std::vector<PresentationTimeCallbackBuffer::Callback> callbacks) { … }
std::vector<PresentationTimeCallbackBuffer::Callback>
LayerTreeImpl::TakePresentationCallbacks() { … }
void LayerTreeImpl::AddSuccessfulPresentationCallbacks(
std::vector<PresentationTimeCallbackBuffer::SuccessfulCallbackWithDetails>
callbacks) { … }
std::vector<PresentationTimeCallbackBuffer::SuccessfulCallbackWithDetails>
LayerTreeImpl::TakeSuccessfulPresentationCallbacks() { … }
LayerImpl* LayerTreeImpl::InnerViewportScrollLayerForTesting() const { … }
LayerImpl* LayerTreeImpl::OuterViewportScrollLayerForTesting() const { … }
ScrollNode* LayerTreeImpl::CurrentlyScrollingNode() { … }
const ScrollNode* LayerTreeImpl::CurrentlyScrollingNode() const { … }
int LayerTreeImpl::LastScrolledScrollNodeIndex() const { … }
void LayerTreeImpl::SetCurrentlyScrollingNode(const ScrollNode* node) { … }
void LayerTreeImpl::ClearCurrentlyScrollingNode() { … }
float LayerTreeImpl::ClampPageScaleFactorToLimits(
float page_scale_factor) const { … }
void LayerTreeImpl::UpdatePropertyTreeAnimationFromMainThread() { … }
void LayerTreeImpl::UpdateTransformAnimation(ElementId element_id,
int transform_node_index) { … }
void LayerTreeImpl::UpdatePageScaleNode() { … }
void LayerTreeImpl::SetPageScaleOnActiveTree(float active_page_scale) { … }
void LayerTreeImpl::PushPageScaleFromMainThread(float page_scale_factor,
float min_page_scale_factor,
float max_page_scale_factor) { … }
void LayerTreeImpl::PushPageScaleFactorAndLimits(const float* page_scale_factor,
float min_page_scale_factor,
float max_page_scale_factor) { … }
void LayerTreeImpl::SetBrowserControlsParams(
const BrowserControlsParams& params) { … }
void LayerTreeImpl::set_overscroll_behavior(
const OverscrollBehavior& behavior) { … }
bool LayerTreeImpl::ClampTopControlsShownRatio() { … }
bool LayerTreeImpl::ClampBottomControlsShownRatio() { … }
bool LayerTreeImpl::SetCurrentBrowserControlsShownRatio(float top_ratio,
float bottom_ratio) { … }
void LayerTreeImpl::PushBrowserControlsFromMainThread(
float top_controls_shown_ratio,
float bottom_controls_shown_ratio) { … }
void LayerTreeImpl::PushBrowserControls(
const float* top_controls_shown_ratio,
const float* bottom_controls_shown_ratio) { … }
bool LayerTreeImpl::SetPageScaleFactorLimits(float min_page_scale_factor,
float max_page_scale_factor) { … }
void LayerTreeImpl::DidUpdatePageScale() { … }
void LayerTreeImpl::SetDeviceScaleFactor(float device_scale_factor) { … }
void LayerTreeImpl::SetLocalSurfaceIdFromParent(
const viz::LocalSurfaceId& local_surface_id_from_parent) { … }
void LayerTreeImpl::RequestNewLocalSurfaceId() { … }
bool LayerTreeImpl::TakeNewLocalSurfaceIdRequest() { … }
void LayerTreeImpl::SetScreenshotDestinationToken(
base::UnguessableToken destination_token) { … }
base::UnguessableToken LayerTreeImpl::TakeScreenshotDestinationToken() { … }
void LayerTreeImpl::SetDeviceViewportRect(
const gfx::Rect& device_viewport_rect) { … }
gfx::Rect LayerTreeImpl::GetDeviceViewport() const { … }
void LayerTreeImpl::SetDisplayColorSpaces(
const gfx::DisplayColorSpaces& display_color_spaces) { … }
void LayerTreeImpl::SetExternalPageScaleFactor(
float external_page_scale_factor) { … }
SyncedScale* LayerTreeImpl::page_scale_factor() { … }
const SyncedScale* LayerTreeImpl::page_scale_factor() const { … }
gfx::SizeF LayerTreeImpl::ScrollableViewportSize() const { … }
gfx::Rect LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const { … }
void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit(
bool next_bmf,
bool main_frame_applied_deltas) { … }
void LayerTreeImpl::SetViewportPropertyIds(const ViewportPropertyIds& ids) { … }
const TransformNode* LayerTreeImpl::OverscrollElasticityTransformNode() const { … }
const TransformNode* LayerTreeImpl::PageScaleTransformNode() const { … }
const ScrollNode* LayerTreeImpl::InnerViewportScrollNode() const { … }
const ClipNode* LayerTreeImpl::OuterViewportClipNode() const { … }
const ScrollNode* LayerTreeImpl::OuterViewportScrollNode() const { … }
void LayerTreeImpl::SetElementIdsForTesting() { … }
bool LayerTreeImpl::UpdateDrawProperties(
bool update_tiles,
bool update_image_animation_controller,
LayerImplList* output_update_layer_list_for_testing) { … }
bool LayerTreeImpl::UpdateTiles() { … }
const RenderSurfaceList& LayerTreeImpl::GetRenderSurfaceList() const { … }
const Region& LayerTreeImpl::UnoccludedScreenSpaceRegion() const { … }
gfx::SizeF LayerTreeImpl::ScrollableSize() const { … }
LayerImpl* LayerTreeImpl::LayerById(int id) const { … }
LayerImpl* LayerTreeImpl::LayerByElementId(ElementId element_id) const { … }
void LayerTreeImpl::SetSurfaceRanges(
const base::flat_set<viz::SurfaceRange> surface_ranges) { … }
const base::flat_set<viz::SurfaceRange>& LayerTreeImpl::SurfaceRanges() const { … }
void LayerTreeImpl::ClearSurfaceRanges() { … }
void LayerTreeImpl::AddLayerShouldPushProperties(LayerImpl* layer) { … }
void LayerTreeImpl::ClearLayersThatShouldPushProperties() { … }
void LayerTreeImpl::RegisterLayer(LayerImpl* layer) { … }
void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) { … }
void LayerTreeImpl::AddLayer(std::unique_ptr<LayerImpl> layer) { … }
size_t LayerTreeImpl::NumLayers() { … }
void LayerTreeImpl::DidBecomeActive() { … }
bool LayerTreeImpl::RequiresHighResToDraw() const { … }
TaskRunnerProvider* LayerTreeImpl::task_runner_provider() const { … }
LayerTreeFrameSink* LayerTreeImpl::layer_tree_frame_sink() { … }
int LayerTreeImpl::max_texture_size() const { … }
const LayerTreeSettings& LayerTreeImpl::settings() const { … }
const LayerTreeDebugState& LayerTreeImpl::debug_state() const { … }
viz::RasterContextProvider* LayerTreeImpl::context_provider() const { … }
viz::ClientResourceProvider* LayerTreeImpl::resource_provider() const { … }
TileManager* LayerTreeImpl::tile_manager() const { … }
ImageDecodeCache* LayerTreeImpl::image_decode_cache() const { … }
ImageAnimationController* LayerTreeImpl::image_animation_controller() const { … }
DroppedFrameCounter* LayerTreeImpl::dropped_frame_counter() const { … }
MemoryHistory* LayerTreeImpl::memory_history() const { … }
DebugRectHistory* LayerTreeImpl::debug_rect_history() const { … }
bool LayerTreeImpl::IsActiveTree() const { … }
bool LayerTreeImpl::IsPendingTree() const { … }
bool LayerTreeImpl::IsRecycleTree() const { … }
bool LayerTreeImpl::IsSyncTree() const { … }
bool LayerTreeImpl::HasPendingTree() const { … }
LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) { … }
LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) { … }
bool LayerTreeImpl::PinchGestureActive() const { … }
const viz::BeginFrameArgs& LayerTreeImpl::CurrentBeginFrameArgs() const { … }
base::TimeDelta LayerTreeImpl::CurrentBeginFrameInterval() const { … }
const gfx::Rect LayerTreeImpl::ViewportRectForTilePriority() const { … }
std::unique_ptr<ScrollbarAnimationController>
LayerTreeImpl::CreateScrollbarAnimationController(ElementId scroll_element_id,
float initial_opacity) { … }
void LayerTreeImpl::DidAnimateScrollOffset() { … }
bool LayerTreeImpl::use_gpu_rasterization() const { … }
bool LayerTreeImpl::create_low_res_tiling() const { … }
void LayerTreeImpl::SetNeedsRedraw() { … }
void LayerTreeImpl::GetAllPrioritizedTilesForTracing(
std::vector<PrioritizedTile>* prioritized_tiles) const { … }
void LayerTreeImpl::AsValueInto(base::trace_event::TracedValue* state) const { … }
void LayerTreeImpl::QueueSwapPromise(
std::unique_ptr<SwapPromise> swap_promise) { … }
void LayerTreeImpl::QueuePinnedSwapPromise(
std::unique_ptr<SwapPromise> swap_promise) { … }
void LayerTreeImpl::PassSwapPromises(
std::vector<std::unique_ptr<SwapPromise>> new_swap_promises) { … }
void LayerTreeImpl::AppendSwapPromises(
std::vector<std::unique_ptr<SwapPromise>> new_swap_promises) { … }
void LayerTreeImpl::FinishSwapPromises(viz::CompositorFrameMetadata* metadata) { … }
void LayerTreeImpl::ClearSwapPromises() { … }
void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason) { … }
void LayerTreeImpl::DidModifyTilePriorities(bool pending_update_tiles) { … }
void LayerTreeImpl::set_ui_resource_request_queue(
UIResourceRequestQueue queue) { … }
viz::ResourceId LayerTreeImpl::ResourceIdForUIResource(UIResourceId uid) const { … }
bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid) const { … }
void LayerTreeImpl::ProcessUIResourceRequestQueue() { … }
void LayerTreeImpl::RegisterPictureLayerImpl(PictureLayerImpl* layer) { … }
void LayerTreeImpl::UnregisterPictureLayerImpl(PictureLayerImpl* layer) { … }
void LayerTreeImpl::NotifyLayerHasPaintWorkletsChanged(PictureLayerImpl* layer,
bool has_worklets) { … }
void LayerTreeImpl::RegisterScrollbar(ScrollbarLayerImplBase* scrollbar_layer) { … }
void LayerTreeImpl::UnregisterScrollbar(
ScrollbarLayerImplBase* scrollbar_layer) { … }
ScrollbarSet LayerTreeImpl::ScrollbarsFor(ElementId scroll_element_id) const { … }
static bool PointHitsRect(
const gfx::PointF& screen_space_point,
const gfx::Transform& local_space_to_screen_space_transform,
const gfx::Rect& local_space_rect,
float* distance_to_camera) { … }
static bool PointIsClippedByAncestorClipNode(
const gfx::PointF& screen_space_point,
const LayerImpl* layer) { … }
static bool PointIsClippedBySurfaceOrClipRect(
const gfx::PointF& screen_space_point,
const LayerImpl* layer) { … }
static bool PointHitsRegion(const gfx::PointF& screen_space_point,
const gfx::Transform& screen_space_transform,
const Region& layer_space_region,
const LayerImpl* layer_impl) { … }
static bool PointHitsLayer(const LayerImpl* layer,
const gfx::PointF& screen_space_point,
float* distance_to_intersection) { … }
struct FindClosestMatchingLayerState { … };
template <typename Functor>
static void FindClosestMatchingLayer(const gfx::PointF& screen_space_point,
LayerImpl* root_layer,
const Functor& func,
FindClosestMatchingLayerState* state) { … }
struct HitTestVisibleScrollableOrTouchableFunctor { … };
LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPoint(
const gfx::PointF& screen_space_point) { … }
struct FindTouchEventLayerFunctor { … };
struct FindWheelEventHandlerLayerFunctor { … };
template <typename Functor>
LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInEventHandlerRegion(
const gfx::PointF& screen_space_point,
const Functor& func) { … }
LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion(
const gfx::PointF& screen_space_point) { … }
LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInWheelEventHandlerRegion(
const gfx::PointF& screen_space_point) { … }
std::vector<const LayerImpl*>
LayerTreeImpl::FindLayersUpToFirstScrollableOrOpaqueToHitTest(
const gfx::PointF& screen_space_point) { … }
bool LayerTreeImpl::PointHitsMainThreadScrollHitTestRegion(
const gfx::PointF& screen_space_point,
const LayerImpl& layer) const { … }
ElementId LayerTreeImpl::PointHitsNonCompositedScroll(
const gfx::PointF& screen_space_point,
const LayerImpl& layer) const { … }
static ElementId GetFrameElementIdForLayer(const LayerImpl* layer) { … }
static void FindClosestMatchingLayerForAttribution(
const gfx::PointF& screen_space_point,
const LayerImpl* root_layer,
FindClosestMatchingLayerState* state) { … }
ElementId LayerTreeImpl::FindFrameElementIdAtPoint(
const gfx::PointF& screen_space_point) { … }
void LayerTreeImpl::RegisterSelection(const LayerSelection& selection) { … }
void LayerTreeImpl::ResetHandleVisibilityChanged() { … }
static gfx::SelectionBound ComputeViewportSelectionBound(
const LayerSelectionBound& layer_bound,
LayerImpl* layer,
float device_scale_factor) { … }
void LayerTreeImpl::GetViewportSelection(
viz::Selection<gfx::SelectionBound>* selection) { … }
bool LayerTreeImpl::SmoothnessTakesPriority() const { … }
VideoFrameControllerClient* LayerTreeImpl::GetVideoFrameControllerClient()
const { … }
void LayerTreeImpl::UpdateImageDecodingHints(
base::flat_map<PaintImage::Id, PaintImage::DecodingMode>
decoding_mode_map) { … }
int LayerTreeImpl::GetMSAASampleCountForRaster(
const DisplayItemList& display_list) const { … }
TargetColorParams LayerTreeImpl::GetTargetColorParams(
gfx::ContentColorUsage content_color_usage) const { … }
void LayerTreeImpl::SetPendingPageScaleAnimation(
std::unique_ptr<PendingPageScaleAnimation> pending_animation) { … }
std::unique_ptr<PendingPageScaleAnimation>
LayerTreeImpl::TakePendingPageScaleAnimation() { … }
void LayerTreeImpl::AppendEventsMetricsFromMainThread(
EventMetrics::List events_metrics) { … }
EventMetrics::List LayerTreeImpl::TakeEventsMetrics() { … }
bool LayerTreeImpl::TakeForceSendMetadataRequest() { … }
void LayerTreeImpl::ResetAllChangeTracking() { … }
std::string LayerTreeImpl::LayerListAsJson() const { … }
void LayerTreeImpl::AddViewTransitionRequest(
std::unique_ptr<ViewTransitionRequest> request) { … }
std::vector<std::unique_ptr<ViewTransitionRequest>>
LayerTreeImpl::TakeViewTransitionRequests() { … }
bool LayerTreeImpl::HasViewTransitionRequests() const { … }
bool LayerTreeImpl::HasViewTransitionSaveRequest() const { … }
bool LayerTreeImpl::IsReadyToActivate() const { … }
void LayerTreeImpl::RequestImplSideInvalidationForRerasterTiling() { … }
}