#include "cc/layers/picture_layer_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <set>
#include <utility>
#include "arkweb/build/features/features.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/system/sys_info.h"
#include "base/time/time.h"
#include "base/trace_event/traced_value.h"
#include "build/build_config.h"
#include "cc/base/features.h"
#include "cc/base/math_util.h"
#include "cc/benchmarks/micro_benchmark_impl.h"
#include "cc/debug/debug_colors.h"
#include "cc/layers/append_quads_context.h"
#include "cc/layers/append_quads_data.h"
#include "cc/paint/display_item_list.h"
#include "cc/tiles/tile_manager.h"
#include "cc/tiles/tiling_set_raster_queue_all.h"
#include "cc/trees/draw_property_utils.h"
#include "cc/trees/effect_node.h"
#include "cc/trees/layer_tree_impl.h"
#include "cc/trees/occlusion.h"
#include "cc/trees/transform_node.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/quads/debug_border_draw_quad.h"
#include "components/viz/common/quads/picture_draw_quad.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/tile_draw_quad.h"
#include "components/viz/common/traced_value.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
namespace cc {
namespace {
const float kMaxScaleRatioDuringPinch = 2.0f;
const float kSnapToExistingTilingRatio = 1.2f;
const float kMaxIdealContentsScale = 10000.f;
const float kMinScaleRatioForWillChangeTransform = 0.25f;
const float kRatioToAdjustRasterScaleForTransformAnimation = 1.5f;
gfx::Rect SafeIntersectRects(const gfx::Rect& one, const gfx::Rect& two) {
if (one.IsEmpty() || two.IsEmpty())
return gfx::Rect();
int rx = std::max(one.x(), two.x());
int ry = std::max(one.y(), two.y());
int64_t rr = std::min(static_cast<int64_t>(one.x()) + one.width(),
static_cast<int64_t>(two.x()) + two.width());
int64_t rb = std::min(static_cast<int64_t>(one.y()) + one.height(),
static_cast<int64_t>(two.y()) + two.height());
if (rx > rr || ry > rb)
return gfx::Rect();
return gfx::Rect(rx, ry, static_cast<int>(rr - rx),
static_cast<int>(rb - ry));
}
}
PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
: TileBasedLayerImpl(tree_impl, id) {
layer_tree_impl()->RegisterPictureLayerImpl(this);
}
PictureLayerImpl::~PictureLayerImpl() {
if (twin_layer_)
twin_layer_->twin_layer_ = nullptr;
if (!paint_worklet_records_.empty() && !layer_tree_impl()->IsActiveTree())
layer_tree_impl()->NotifyLayerHasPaintWorkletsChanged(this, false);
if (!layer_tree_impl()->IsActiveTree()) {
layer_tree_impl()
->paint_worklet_tracker()
.UpdatePaintWorkletInputProperties({}, this);
}
layer_tree_impl()->UnregisterPictureLayerImpl(this);
UnregisterAnimatedImages();
}
mojom::LayerType PictureLayerImpl::GetLayerType() const {
return mojom::LayerType::kPicture;
}
std::unique_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
LayerTreeImpl* tree_impl) const {
return PictureLayerImpl::Create(tree_impl, id());
}
void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
layer_impl->has_animated_image_update_rect_ = has_animated_image_update_rect_;
layer_impl->has_non_animated_image_update_rect_ =
has_non_animated_image_update_rect_;
bool changed_other_props = GetChangeFlag(kChangedGeneralProperty);
LayerImpl::PushPropertiesTo(base_layer);
DCHECK(!twin_layer_ || twin_layer_ == layer_impl);
DCHECK(!twin_layer_ || layer_impl->twin_layer_ == this);
twin_layer_ = layer_impl;
layer_impl->twin_layer_ = this;
if (layer_tree_impl()->settings().TreesInVizInClientProcess()) {
{
auto& dst = layer_impl->updated_tiles_;
auto& src = updated_tiles_;
for (auto& [scale, set_src] : src) {
auto it = dst.find(scale);
if (it == dst.end()) {
dst.emplace(scale, std::move(set_src));
} else {
it->second.merge(set_src);
}
}
src.clear();
}
layer_impl->should_batch_updated_tiles_ = true;
}
if (changed_other_props) {
layer_impl->SetIsBackdropFilterMask(is_backdrop_filter_mask());
DCHECK(!solid_color() || tilings_->num_tilings() == 0);
DCHECK_LE(tilings_->num_tilings(), 1u);
layer_impl->set_gpu_raster_max_texture_size(gpu_raster_max_texture_size_);
layer_impl->UpdateRasterSourceInternal(
raster_source_, &invalidation_, tilings_.get(), &paint_worklet_records_,
discardable_image_map_.get());
DCHECK(invalidation_.IsEmpty());
DCHECK(!solid_color() || layer_impl->tilings_->num_tilings() == 0);
layer_impl->raster_page_scale_ = raster_page_scale_;
layer_impl->raster_device_scale_ = raster_device_scale_;
layer_impl->raster_source_scale_ = raster_source_scale_;
layer_impl->raster_contents_scale_ = raster_contents_scale_;
layer_impl->lcd_text_disallowed_reason_ = lcd_text_disallowed_reason_;
}
layer_impl->SanityCheckTilingState();
}
void PictureLayerImpl::AppendQuadsForResourcelessSoftwareDraw(
const AppendQuadsContext& context,
viz::CompositorRenderPass* render_pass,
AppendQuadsData* append_quads_data,
viz::SharedQuadState* shared_quad_state,
const Occlusion& scaled_occlusion) {
DCHECK(shared_quad_state->quad_layer_rect.origin() == gfx::Point(0, 0));
float max_contents_scale = GetMaximumContentsScaleForUseInAppendQuads();
float device_scale_factor = layer_tree_impl()->device_scale_factor();
AppendDebugBorderQuad(
render_pass, shared_quad_state->quad_layer_rect, shared_quad_state,
append_quads_data, DebugColors::DirectPictureBorderColor(),
DebugColors::DirectPictureBorderWidth(device_scale_factor));
gfx::Rect geometry_rect = shared_quad_state->visible_quad_layer_rect;
gfx::Rect visible_geometry_rect =
scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
bool needs_blending = !contents_opaque();
gfx::Rect scaled_recorded_bounds = gfx::ScaleToEnclosingRect(
raster_source_->recorded_bounds(), max_contents_scale);
geometry_rect.Intersect(scaled_recorded_bounds);
visible_geometry_rect.Intersect(scaled_recorded_bounds);
if (visible_geometry_rect.IsEmpty()) {
return;
}
DCHECK(raster_source_->HasRecordings());
gfx::Rect quad_content_rect = shared_quad_state->visible_quad_layer_rect;
gfx::Size texture_size = quad_content_rect.size();
gfx::RectF texture_rect = gfx::RectF(gfx::SizeF(texture_size));
viz::PictureDrawQuad::ImageAnimationMap image_animation_map;
const auto* controller = layer_tree_impl()->image_animation_controller();
WhichTree tree = layer_tree_impl()->IsPendingTree() ? WhichTree::PENDING_TREE
: WhichTree::ACTIVE_TREE;
for (const auto& image_data :
discardable_image_map_->animated_images_metadata()) {
image_animation_map[image_data.paint_image_id] =
controller->GetFrameIndexForImage(image_data.paint_image_id, tree);
}
auto* quad = render_pass->CreateAndAppendDrawQuad<viz::PictureDrawQuad>();
quad->SetNew(
shared_quad_state, geometry_rect, visible_geometry_rect, needs_blending,
texture_rect, nearest_neighbor_, quad_content_rect, max_contents_scale,
std::move(image_animation_map), raster_source_->GetDisplayItemList(),
GetRasterInducingScrollOffsets());
ValidateQuadResources(quad);
}
void PictureLayerImpl::AppendQuadsSpecialization(
const AppendQuadsContext& context,
viz::CompositorRenderPass* render_pass,
AppendQuadsData* append_quads_data,
viz::SharedQuadState* shared_quad_state,
const Occlusion& scaled_occlusion,
const gfx::Vector2d& quad_offset) {
float max_contents_scale = GetMaximumContentsScaleForUseInAppendQuads();
last_append_quads_tilings_.clear();
gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
viewport_rect_for_tile_priority_in_content_space_, max_contents_scale);
std::optional<gfx::Rect> scaled_cull_rect;
const ScrollTree& scroll_tree =
layer_tree_impl()->property_trees()->scroll_tree();
if (const ScrollNode* scroll_node = scroll_tree.Node(scroll_tree_index())) {
if (transform_tree_index() == scroll_node->transform_id) {
if (const gfx::Rect* cull_rect =
scroll_tree.ScrollingContentsCullRect(scroll_node->element_id)) {
scaled_cull_rect = gfx::ToEnclosingRect(gfx::ScaleRect(
gfx::RectF(*cull_rect) - offset_to_transform_parent(),
max_contents_scale));
}
}
}
if (const auto& display_list = raster_source_->GetDisplayItemList()) {
for (auto& [element_id, info] : display_list->raster_inducing_scrolls()) {
if (!info.visual_rect.Intersects(visible_layer_rect())) {
continue;
}
if (const gfx::Rect* cull_rect =
scroll_tree.ScrollingContentsCullRect(element_id)) {
if (const auto* scroll_node =
scroll_tree.FindNodeFromElementId(element_id)) {
if (!scroll_tree.CanRealizeScrollsOnPendingTree(*scroll_node)) {
continue;
}
gfx::RectF visible_rect(
gfx::Rect(scroll_node->container_origin,
scroll_tree.container_bounds(scroll_node->id)));
visible_rect.Offset(
scroll_tree.current_scroll_offset(element_id).OffsetFromOrigin());
if (!cull_rect->Contains(gfx::ToEnclosedRect(visible_rect))) {
append_quads_data->checkerboarded_needs_record = true;
break;
}
}
}
}
}
int missing_tile_count = 0;
produced_tile_last_append_quads_ = false;
gfx::Rect scaled_recorded_bounds = gfx::ScaleToEnclosingRect(
raster_source_->recorded_bounds(), max_contents_scale);
for (auto iter = Cover(shared_quad_state->visible_quad_layer_rect,
max_contents_scale, GetIdealContentsScaleKey());
iter; ++iter) {
gfx::Rect geometry_rect = iter.geometry_rect();
if (!scaled_recorded_bounds.Intersects(geometry_rect)) {
continue;
}
gfx::Rect visible_geometry_rect =
scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
gfx::Rect offset_geometry_rect = geometry_rect;
offset_geometry_rect.Offset(quad_offset);
gfx::Rect offset_visible_geometry_rect = visible_geometry_rect;
offset_visible_geometry_rect.Offset(quad_offset);
bool needs_blending = !contents_opaque();
if (visible_geometry_rect.IsEmpty())
continue;
uint64_t visible_geometry_area = visible_geometry_rect.size().Area64();
append_quads_data->visible_layer_area += visible_geometry_area;
bool has_draw_quad = false;
if (*iter && iter->draw_info().IsReadyToDraw()) {
const TileDrawInfo& draw_info = iter->draw_info();
iter->mark_used();
switch (draw_info.mode()) {
case TileDrawInfo::RESOURCE_MODE: {
gfx::RectF texture_rect = iter.texture_rect();
if (iter->contents_scale_key() != raster_contents_scale_key() &&
iter->contents_scale_key() < GetIdealContentsScaleKey() &&
geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
append_quads_data->checkerboarded_needs_raster = true;
}
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::TileDrawQuad>();
quad->SetNew(
shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, needs_blending,
draw_info.resource_id_for_export(), texture_rect,
nearest_neighbor_,
!layer_tree_impl()->settings().enable_edge_anti_aliasing);
ValidateQuadResources(quad);
has_draw_quad = true;
break;
}
case TileDrawInfo::SOLID_COLOR_MODE: {
float alpha = draw_info.solid_color().fA * shared_quad_state->opacity;
if (alpha >= std::numeric_limits<float>::epsilon()) {
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
quad->SetNew(
shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, draw_info.solid_color(),
!layer_tree_impl()->settings().enable_edge_anti_aliasing);
ValidateQuadResources(quad);
}
has_draw_quad = true;
break;
}
case TileDrawInfo::OOM_MODE:
break;
}
}
if (!append_quads_data->checkerboarded_needs_record && scaled_cull_rect &&
!scaled_cull_rect->Contains(visible_geometry_rect)) {
append_quads_data->checkerboarded_needs_record = true;
}
if (!has_draw_quad) {
SkColor4f color = safe_opaque_background_color();
if (ShowDebugBorders(DebugBorderType::LAYER)) {
color = DebugColors::DefaultCheckerboardColor();
}
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
quad->SetNew(shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, color, false);
ValidateQuadResources(quad);
if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
++missing_tile_count;
}
if (*iter) {
UMA_HISTOGRAM_BOOLEAN(
"Compositing.DecodeLCPCandidateImage.MissedDeadline",
iter->HasMissingLCPCandidateImages());
}
continue;
}
if (iter.resolution() != HIGH_RESOLUTION) {
append_quads_data->approximated_visible_content_area +=
visible_geometry_area;
}
produced_tile_last_append_quads_ = true;
if (last_append_quads_tilings_.empty() ||
last_append_quads_tilings_.back() != iter.CurrentTiling()) {
last_append_quads_tilings_.push_back(iter.CurrentTiling());
}
}
if (missing_tile_count) {
append_quads_data->num_missing_tiles += missing_tile_count;
append_quads_data->checkerboarded_needs_raster = true;
TRACE_EVENT_INSTANT1("cc", "PictureLayerImpl::AppendQuads checkerboard",
TRACE_EVENT_SCOPE_THREAD, "missing_tile_count",
missing_tile_count);
}
CleanUpTilingsOnActiveLayer();
SanityCheckTilingState();
}
bool PictureLayerImpl::UpdateTiles() {
if (!CanHaveTilings()) {
ideal_page_scale_ = 0.f;
ideal_device_scale_ = 0.f;
ideal_contents_scale_ = gfx::Vector2dF(0.f, 0.f);
ideal_source_scale_ = gfx::Vector2dF(0.f, 0.f);
SanityCheckTilingState();
return false;
}
if (layer_tree_impl()->IsActiveTree()) {
CleanUpTilingsOnActiveLayer();
}
UpdateIdealScales();
const bool should_adjust_raster_scale = ShouldAdjustRasterScale();
if (should_adjust_raster_scale)
RecalculateRasterScales();
UpdateTilingsForRasterScaleAndTranslation(should_adjust_raster_scale);
raster_source_size_changed_ = false;
DCHECK(raster_page_scale_);
DCHECK(raster_device_scale_);
DCHECK(raster_source_scale_.x());
DCHECK(raster_source_scale_.y());
DCHECK(raster_contents_scale_.x());
DCHECK(raster_contents_scale_.y());
was_screen_space_transform_animating_ =
draw_properties().screen_space_transform_is_animating;
double current_frame_time_in_seconds =
(layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
base::TimeTicks()).InSecondsF();
UpdateViewportRectForTilePriorityInContentSpace();
bool can_require_tiles_for_activation = false;
if (contributes_to_drawn_render_surface()) {
can_require_tiles_for_activation =
produced_tile_last_append_quads_ || RequiresHighResToDraw() ||
!layer_tree_impl()->SmoothnessTakesPriority();
}
static const base::NoDestructor<Occlusion> kEmptyOcclusion;
const Occlusion& occlusion_in_content_space =
layer_tree_impl()->settings().use_occlusion_for_tile_prioritization
? draw_properties().occlusion_in_content_space
: *kEmptyOcclusion;
bool updated = tilings_->UpdateTilePriorities(
viewport_rect_for_tile_priority_in_content_space_,
GetIdealContentsScaleKey(), current_frame_time_in_seconds,
occlusion_in_content_space, can_require_tiles_for_activation);
DCHECK_GT(tilings_->num_tilings(), 0u);
SanityCheckTilingState();
return updated;
}
void PictureLayerImpl::UpdateViewportRectForTilePriorityInContentSpace() {
gfx::Rect visible_rect_in_content_space = visible_layer_rect();
gfx::Rect viewport_rect_for_tile_priority =
layer_tree_impl()->ViewportRectForTilePriority();
if (visible_rect_in_content_space.IsEmpty() ||
layer_tree_impl()->GetDeviceViewport() !=
viewport_rect_for_tile_priority) {
gfx::Transform view_to_layer;
if (ScreenSpaceTransform().GetInverse(&view_to_layer)) {
visible_rect_in_content_space = MathUtil::ProjectEnclosingClippedRect(
view_to_layer, viewport_rect_for_tile_priority);
gfx::Rect padded_bounds(bounds());
int padding_amount = layer_tree_impl()
->settings()
.skewport_extrapolation_limit_in_screen_pixels *
MaximumTilingContentsScale();
padded_bounds.Inset(-padding_amount);
visible_rect_in_content_space =
SafeIntersectRects(visible_rect_in_content_space, padded_bounds);
}
}
viewport_rect_for_tile_priority_in_content_space_ =
visible_rect_in_content_space;
}
PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() const {
if (!twin_layer_ || !twin_layer_->IsOnActiveOrPendingTree())
return nullptr;
return twin_layer_;
}
void PictureLayerImpl::UpdateRasterSource(
scoped_refptr<RasterSource> raster_source,
Region* new_invalidation) {
CHECK(layer_tree_impl()->IsSyncTree());
UpdateRasterSourceInternal(
std::move(raster_source), new_invalidation,
nullptr, nullptr, nullptr);
}
void PictureLayerImpl::UpdateRasterSourceInternal(
scoped_refptr<RasterSource> raster_source,
Region* new_invalidation,
const PictureLayerTilingSet* pending_set,
const PaintWorkletRecordMap* pending_paint_worklet_records,
const DiscardableImageMap* pending_discardable_image_map) {
CHECK(raster_source);
DCHECK(raster_source->size().IsEmpty() || bounds() == raster_source->size())
<< " layer bounds " << bounds().ToString() << " raster_source size "
<< raster_source->size().ToString();
SetNeedsPushProperties();
if (!raster_source_ || raster_source_->size() != raster_source->size()) {
raster_source_size_changed_ = true;
}
const bool recording_updated =
!raster_source_ || raster_source_->GetDisplayItemList() !=
raster_source->GetDisplayItemList();
if (recording_updated && raster_source_) {
const auto& current_display_item_list =
raster_source_->GetDisplayItemList();
const auto& new_display_item_list = raster_source->GetDisplayItemList();
if (current_display_item_list && new_display_item_list) {
bool needs_full_invalidation =
layer_tree_impl()->GetMSAASampleCountForRaster(
*current_display_item_list) !=
layer_tree_impl()->GetMSAASampleCountForRaster(
*new_display_item_list);
needs_full_invalidation |=
layer_tree_impl()->GetTargetColorParams(
current_display_item_list->content_color_usage()) !=
layer_tree_impl()->GetTargetColorParams(
new_display_item_list->content_color_usage());
if (needs_full_invalidation) {
new_invalidation->Union(gfx::Rect(raster_source->size()));
}
}
}
bool could_have_tilings = CanHaveTilings();
raster_source_ = std::move(raster_source);
SetSolidColor(std::nullopt);
if (raster_source_->IsSolidColor()) {
SetSolidColor(raster_source_->GetSolidColor());
}
raster_source_->set_debug_name(DebugName());
UpdateDirectlyCompositedImageFromRasterSource();
if (pending_set) {
if (pending_discardable_image_map != discardable_image_map_) {
CHECK(pending_paint_worklet_records);
paint_worklet_records_ = *pending_paint_worklet_records;
UnregisterAnimatedImages();
discardable_image_map_ = pending_discardable_image_map;
RegisterAnimatedImages();
}
} else if (recording_updated) {
layer_tree_impl()->AddLayerNeedingUpdateDiscardableImageMap(this);
}
invalidation_.Clear();
invalidation_.Swap(new_invalidation);
bool can_have_tilings = CanHaveTilings();
DCHECK(!pending_set ||
can_have_tilings == GetPendingOrActiveTwinLayer()->CanHaveTilings());
if (could_have_tilings != can_have_tilings)
layer_tree_impl()->set_needs_update_draw_properties();
if (!can_have_tilings) {
RemoveAllTilings();
return;
}
if (pending_set) {
tilings_->UpdateTilingsToCurrentRasterSourceForActivation(
raster_source_, pending_set, invalidation_, MinimumContentsScale(),
MaximumContentsScale());
} else {
tilings_->UpdateTilingsToCurrentRasterSourceForCommit(
raster_source_, invalidation_, MinimumContentsScale(),
MaximumContentsScale());
}
}
void PictureLayerImpl::SetRasterSourceForTesting(
scoped_refptr<RasterSource> raster_source,
const Region& invalidation) {
LayerTreeImpl::DiscardableImageMapUpdater updater(layer_tree_impl());
Region invalidation_temp = invalidation;
UpdateRasterSource(std::move(raster_source), &invalidation_temp);
}
void PictureLayerImpl::RegenerateDiscardableImageMap() {
CHECK(layer_tree_impl()->IsSyncTree());
UnregisterAnimatedImages();
if (const auto* display_list = raster_source_->GetDisplayItemList().get()) {
DiscardableImageMap::DecodingModeMap decoding_mode_map;
DiscardableImageMap::PaintWorkletInputs paint_worklet_inputs;
discardable_image_map_ = display_list->GenerateDiscardableImageMap(
GetRasterInducingScrollOffsets(), &decoding_mode_map,
&paint_worklet_inputs);
SetPaintWorkletInputs(paint_worklet_inputs);
layer_tree_impl()->UpdateImageDecodingHints(decoding_mode_map);
} else {
SetPaintWorkletInputs({});
discardable_image_map_ = nullptr;
}
RegisterAnimatedImages();
}
void PictureLayerImpl::UpdateCanUseLCDText(
bool raster_translation_aligns_pixels) {
if (!layer_tree_impl()->IsSyncTree())
return;
lcd_text_disallowed_reason_ =
ComputeLCDTextDisallowedReason(raster_translation_aligns_pixels);
}
bool PictureLayerImpl::AffectedByWillChangeTransformHint() const {
TransformNode* transform_node =
GetTransformTree().Node(transform_tree_index());
return transform_node &&
transform_node->node_or_ancestors_will_change_transform;
}
LCDTextDisallowedReason PictureLayerImpl::ComputeLCDTextDisallowedReason(
bool raster_translation_aligns_pixels) const {
if (!raster_source_ || !raster_source_->GetDisplayItemList() ||
!raster_source_->GetDisplayItemList()->has_draw_text_ops()) {
return LCDTextDisallowedReason::kNoText;
}
if (layer_tree_impl()->settings().layers_always_allowed_lcd_text) {
return LCDTextDisallowedReason::kNone;
}
if (!layer_tree_impl()->settings().can_use_lcd_text) {
return LCDTextDisallowedReason::kSetting;
}
TransformNode* transform_node =
GetTransformTree().Node(transform_tree_index());
if (transform_node->node_or_ancestors_will_change_transform) {
return LCDTextDisallowedReason::kWillChangeTransform;
}
if (screen_space_transform_is_animating()) {
return LCDTextDisallowedReason::kTransformAnimation;
}
EffectNode* effect_node = GetEffectTree().Node(effect_tree_index());
if (effect_node->lcd_text_disallowed_by_filter ||
effect_node->lcd_text_disallowed_by_backdrop_filter) {
return LCDTextDisallowedReason::kPixelOrColorEffect;
}
if (!raster_translation_aligns_pixels) {
if (static_cast<int>(offset_to_transform_parent().x()) !=
offset_to_transform_parent().x()) {
return LCDTextDisallowedReason::kNonIntegralXOffset;
}
if (static_cast<int>(offset_to_transform_parent().y()) !=
offset_to_transform_parent().y()) {
return LCDTextDisallowedReason::kNonIntegralYOffset;
}
return LCDTextDisallowedReason::kNonIntegralTranslation;
}
if (!contents_opaque_for_text()) {
if (!background_color().isOpaque()) {
return LCDTextDisallowedReason::kBackgroundColorNotOpaque;
}
return LCDTextDisallowedReason::kContentsNotOpaque;
}
return LCDTextDisallowedReason::kNone;
}
LCDTextDisallowedReason
PictureLayerImpl::ComputeLCDTextDisallowedReasonForTesting() const {
gfx::Vector2dF raster_translation;
return ComputeLCDTextDisallowedReason(
CalculateRasterTranslation(raster_translation));
}
void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile,
bool update_damage) {
if (update_damage) {
if (layer_tree_impl()->IsActiveTree()) {
damage_rect_.Union(tile->enclosing_layer_rect());
}
if (tile->draw_info().NeedsRaster()) {
PictureLayerTiling* tiling =
tilings_->FindTilingWithScaleKey(tile->contents_scale_key());
if (tiling) {
tiling->set_all_tiles_done(false);
tilings_->set_all_tiles_done(false);
}
}
}
if (layer_tree_impl()->settings().TreesInVizInClientProcess() &&
should_batch_updated_tiles_) {
updated_tiles_[tile->contents_scale_key()].emplace(tile->tiling_i_index(),
tile->tiling_j_index());
}
}
gfx::Rect PictureLayerImpl::GetDamageRect() const {
return damage_rect_;
}
void PictureLayerImpl::ResetChangeTracking() {
LayerImpl::ResetChangeTracking();
damage_rect_.SetRect(0, 0, 0, 0);
has_animated_image_update_rect_ = false;
has_non_animated_image_update_rect_ = false;
}
void PictureLayerImpl::DidBeginTracing() {
raster_source_->DidBeginTracing();
}
void PictureLayerImpl::ReleaseResources() {
tilings_->ReleaseAllResources();
ResetRasterScale();
}
void PictureLayerImpl::ReleaseTileResources() {
ReleaseResources();
}
void PictureLayerImpl::RecreateTileResources() {
tilings_ = CreatePictureLayerTilingSet();
}
Region PictureLayerImpl::GetInvalidationRegionForDebugging() {
return IntersectRegions(invalidation_, update_rect());
}
std::unique_ptr<Tile> PictureLayerImpl::CreateTile(
const Tile::CreateInfo& info) {
SetNeedsPushProperties();
tilings_->set_all_tiles_done(false);
int flags = 0;
if (!is_backdrop_filter_mask()) {
flags = Tile::USE_PICTURE_ANALYSIS;
}
if (contents_opaque())
flags |= Tile::IS_OPAQUE;
return layer_tree_impl()->tile_manager()->CreateTile(
info, id(), layer_tree_impl()->source_frame_number(), flags);
}
const Region* PictureLayerImpl::GetPendingInvalidation() {
if (layer_tree_impl()->IsPendingTree())
return &invalidation_;
if (layer_tree_impl()->IsRecycleTree())
return nullptr;
DCHECK(layer_tree_impl()->IsActiveTree());
if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
return &twin_layer->invalidation_;
return nullptr;
}
const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling(
const PictureLayerTiling* tiling) const {
PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
if (!twin_layer)
return nullptr;
const PictureLayerTiling* twin_tiling =
twin_layer->tilings_->FindTilingWithScaleKey(
tiling->contents_scale_key());
if (twin_tiling &&
twin_tiling->raster_transform() == tiling->raster_transform())
return twin_tiling;
return nullptr;
}
bool PictureLayerImpl::RequiresHighResToDraw() const {
return layer_tree_impl()->RequiresHighResToDraw();
}
const PaintWorkletRecordMap& PictureLayerImpl::GetPaintWorkletRecords() const {
return paint_worklet_records_;
}
bool PictureLayerImpl::IsDirectlyCompositedImage() const {
return directly_composited_image_default_raster_scale_ > 0.f;
}
std::vector<const DrawImage*> PictureLayerImpl::GetDiscardableImagesInRect(
const gfx::Rect& rect) const {
return discardable_image_map_->GetDiscardableImagesInRect(rect);
}
ScrollOffsetMap PictureLayerImpl::GetRasterInducingScrollOffsets() const {
ScrollOffsetMap map;
if (raster_source_) {
const ScrollTree& scroll_tree =
layer_tree_impl()->property_trees()->scroll_tree();
const TransformTree& transform_tree =
layer_tree_impl()->property_trees()->transform_tree();
for (auto [element_id, _] :
raster_source_->GetDisplayItemList()->raster_inducing_scrolls()) {
const auto* scroll_node = scroll_tree.FindNodeFromElementId(element_id);
const auto* transform =
scroll_node ? transform_tree.Node(scroll_node->transform_id)
: nullptr;
if (transform) {
map[element_id] = gfx::PointAtOffsetFromOrigin(
-transform->to_parent.To2dTranslation());
} else {
map[element_id] = scroll_tree.current_scroll_offset(element_id);
}
}
}
return map;
}
const GlobalStateThatImpactsTilePriority& PictureLayerImpl::global_tile_state()
const {
return layer_tree_impl()->global_tile_state();
}
gfx::Rect PictureLayerImpl::GetEnclosingVisibleRectInTargetSpace() const {
return GetScaledEnclosingVisibleRectInTargetSpace(
MaximumTilingContentsScale());
}
bool PictureLayerImpl::ShouldAnimate(PaintImage::Id paint_image_id) const {
CHECK(discardable_image_map_);
CHECK(!discardable_image_map_->empty());
if (!HasValidTilePriorities())
return false;
const auto& rects = discardable_image_map_->GetRectsForImage(paint_image_id);
for (const auto& r : rects) {
if (r.Intersects(visible_layer_rect()))
return true;
}
return false;
}
gfx::Size PictureLayerImpl::CalculateTileSize(const gfx::Size& content_bounds) {
return tile_size_calculator_.CalculateTileSize(content_bounds);
}
void PictureLayerImpl::GetContentsResourceId(
viz::ResourceId* resource_id,
gfx::Size* resource_size,
gfx::SizeF* resource_uv_size) const {
if (!is_backdrop_filter_mask()) {
*resource_id = viz::kInvalidResourceId;
return;
}
float dest_scale = MaximumTilingContentsScale();
gfx::Rect content_rect =
gfx::ScaleToEnclosingRect(gfx::Rect(bounds()), dest_scale);
auto iter =
tilings_->Cover(content_rect, dest_scale, GetIdealContentsScaleKey());
if (!iter || !*iter) {
*resource_id = viz::kInvalidResourceId;
return;
}
DCHECK(iter.geometry_rect() == content_rect)
<< "iter rect " << iter.geometry_rect().ToString() << " content rect "
<< content_rect.ToString();
const TileDrawInfo& draw_info = iter->draw_info();
if (!draw_info.IsReadyToDraw() ||
draw_info.mode() != TileDrawInfo::RESOURCE_MODE) {
*resource_id = viz::kInvalidResourceId;
return;
}
*resource_id = draw_info.resource_id_for_export();
*resource_size = draw_info.resource_size();
gfx::SizeF requested_tile_size =
gfx::SizeF(iter->tiling()->tiling_data()->tiling_rect().size());
DCHECK_LE(requested_tile_size.width(), draw_info.resource_size().width());
DCHECK_LE(requested_tile_size.height(), draw_info.resource_size().height());
*resource_uv_size = gfx::SizeF(
requested_tile_size.width() / draw_info.resource_size().width(),
requested_tile_size.height() / draw_info.resource_size().height());
}
void PictureLayerImpl::UpdateDirectlyCompositedImageFromRasterSource() {
float new_default_raster_scale = 0;
bool new_nearest_neighbor = false;
if (const auto& info = raster_source_->directly_composited_image_info()) {
new_default_raster_scale =
GetPreferredRasterScale(info->default_raster_scale);
new_nearest_neighbor = info->nearest_neighbor;
}
directly_composited_image_default_raster_scale_changed_ =
new_default_raster_scale !=
directly_composited_image_default_raster_scale_;
if (new_nearest_neighbor != nearest_neighbor_ ||
directly_composited_image_default_raster_scale_changed_) {
directly_composited_image_default_raster_scale_ = new_default_raster_scale;
nearest_neighbor_ = new_nearest_neighbor;
NoteLayerPropertyChanged();
}
}
bool PictureLayerImpl::ShouldDirectlyCompositeImage(float raster_scale) const {
if (raster_scale < 0.1f)
return true;
gfx::SizeF layer_bounds(bounds());
gfx::RectF scaled_bounds_rect(layer_bounds);
scaled_bounds_rect.Scale(raster_scale);
gfx::RectF content_rect(gfx::ToEnclosingRect(scaled_bounds_rect));
content_rect.InvScale(raster_scale);
return std::abs(layer_bounds.width() - content_rect.width()) < 1.f &&
std::abs(layer_bounds.height() - content_rect.height()) < 1.f;
}
float PictureLayerImpl::CalculateDirectlyCompositedImageRasterScale() const {
DCHECK(IsDirectlyCompositedImage());
float adjusted_raster_scale =
directly_composited_image_default_raster_scale_changed_
? directly_composited_image_default_raster_scale_
: raster_source_scale_key();
float max_scale = std::max(directly_composited_image_default_raster_scale_,
MinimumContentsScale());
float min_scale = MinimumContentsScale();
float clamped_ideal_source_scale =
std::clamp(ideal_source_scale_key(), min_scale, max_scale);
constexpr float kFarAwayFactor = 32.f;
if (adjusted_raster_scale < clamped_ideal_source_scale / kFarAwayFactor) {
adjusted_raster_scale = clamped_ideal_source_scale;
} else if (adjusted_raster_scale >
clamped_ideal_source_scale * kFarAwayFactor) {
adjusted_raster_scale = clamped_ideal_source_scale;
} else {
while (adjusted_raster_scale < clamped_ideal_source_scale)
adjusted_raster_scale *= 2.f;
while (adjusted_raster_scale >= 2 * clamped_ideal_source_scale)
adjusted_raster_scale /= 2.f;
}
adjusted_raster_scale =
std::clamp(adjusted_raster_scale, min_scale, max_scale);
return adjusted_raster_scale;
}
PictureLayerTiling* PictureLayerImpl::AddTiling(
const gfx::AxisTransform2d& raster_transform) {
DCHECK(CanHaveTilings());
DCHECK_GE(raster_transform.scale().x(), MinimumContentsScale());
DCHECK_GE(raster_transform.scale().y(), MinimumContentsScale());
DCHECK_LE(raster_transform.scale().x(), MaximumContentsScale());
DCHECK_LE(raster_transform.scale().y(), MaximumContentsScale());
DCHECK(raster_source_->HasRecordings());
bool tiling_can_use_lcd_text =
can_use_lcd_text() && raster_transform.scale() == raster_contents_scale_;
return tilings_->AddTiling(raster_transform, raster_source_,
tiling_can_use_lcd_text);
}
void PictureLayerImpl::RemoveAllTilings() {
tilings_->RemoveAllTilings();
ResetRasterScale();
}
bool PictureLayerImpl::CanRecreateHighResTilingForLCDTextAndRasterTransform(
const PictureLayerTiling& high_res) const {
if (high_res.can_use_lcd_text() &&
(lcd_text_disallowed_reason_ ==
LCDTextDisallowedReason::kBackgroundColorNotOpaque ||
lcd_text_disallowed_reason_ ==
LCDTextDisallowedReason::kContentsNotOpaque)) {
DCHECK(!layer_tree_impl()->IsSyncTree() ||
!layer_tree_impl()->IsReadyToActivate());
return true;
}
if (draw_properties().screen_space_transform_is_animating ||
AffectedByWillChangeTransformHint())
return false;
if (layer_tree_impl()->PinchGestureActive())
return false;
if (lcd_text_disallowed_reason_ == LCDTextDisallowedReason::kNoText &&
high_res.raster_transform().scale() == raster_contents_scale_)
return false;
if (layer_tree_impl()->IsSyncTree() && layer_tree_impl()->IsReadyToActivate())
return false;
if (layer_tree_impl()->GetActivelyScrollingType() !=
ActivelyScrollingType::kNone) {
return false;
}
return true;
}
void PictureLayerImpl::UpdateTilingsForRasterScaleAndTranslation(
bool has_adjusted_raster_scale) {
PictureLayerTiling* high_res =
tilings_->FindTilingWithScaleKey(raster_contents_scale_key());
gfx::Vector2dF raster_translation;
bool raster_translation_aligns_pixels =
CalculateRasterTranslation(raster_translation);
UpdateCanUseLCDText(raster_translation_aligns_pixels);
if (high_res) {
bool raster_transform_is_not_ideal =
high_res->raster_transform().scale() != raster_contents_scale_ ||
high_res->raster_transform().translation() != raster_translation;
bool can_use_lcd_text_changed =
high_res->can_use_lcd_text() != can_use_lcd_text();
bool can_recreate_highres_tiling =
CanRecreateHighResTilingForLCDTextAndRasterTransform(*high_res);
bool should_recreate_high_res =
(raster_transform_is_not_ideal || can_use_lcd_text_changed) &&
layer_tree_impl()->IsSyncTree() && can_recreate_highres_tiling;
bool can_request_invalidation_for_high_res =
(raster_transform_is_not_ideal || can_use_lcd_text_changed) &&
!layer_tree_impl()->settings().commit_to_active_tree &&
layer_tree_impl()->IsActiveTree() && can_recreate_highres_tiling &&
!layer_tree_impl()->HasPendingTree();
if (should_recreate_high_res) {
tilings_->Remove(high_res);
high_res = nullptr;
} else if (can_request_invalidation_for_high_res) {
layer_tree_impl()->RequestImplSideInvalidationForRerasterTiling();
} else if (!has_adjusted_raster_scale) {
DCHECK_EQ(HIGH_RESOLUTION, high_res->resolution());
SanityCheckTilingState();
return;
}
}
tilings_->MarkAllTilingsNonIdeal();
if (!high_res) {
high_res = AddTiling(gfx::AxisTransform2d::FromScaleAndTranslation(
raster_contents_scale_, raster_translation));
}
high_res->set_resolution(HIGH_RESOLUTION);
if (layer_tree_impl()->IsPendingTree() ||
(layer_tree_impl()->settings().commit_to_active_tree &&
IsDirectlyCompositedImage())) {
tilings_->RemoveNonIdealTilings();
}
SanityCheckTilingState();
}
bool PictureLayerImpl::ShouldAdjustRasterScale() const {
if (!raster_contents_scale_.x() || !raster_contents_scale_.y())
return true;
if (raster_source_size_changed_)
return true;
if (IsDirectlyCompositedImage()) {
if (directly_composited_image_default_raster_scale_changed_)
return true;
float max_scale = std::max(directly_composited_image_default_raster_scale_,
MinimumContentsScale());
if (raster_source_scale_key() <
std::min(ideal_source_scale_key(), max_scale))
return true;
if (raster_source_scale_key() > 4 * ideal_source_scale_key())
return true;
return false;
}
if (was_screen_space_transform_animating_ !=
draw_properties().screen_space_transform_is_animating) {
if (draw_properties().screen_space_transform_is_animating) {
float maximum_animation_scale =
layer_tree_impl()->property_trees()->MaximumAnimationToScreenScale(
transform_tree_index());
if ((maximum_animation_scale != raster_contents_scale_.x() ||
maximum_animation_scale != raster_contents_scale_.y())) {
return true;
}
} else {
if (!AffectedByWillChangeTransformHint())
return true;
}
}
bool is_pinching = layer_tree_impl()->PinchGestureActive();
if (is_pinching && raster_page_scale_) {
float ratio = ideal_page_scale_ / raster_page_scale_;
if (raster_page_scale_ > ideal_page_scale_ ||
ratio > kMaxScaleRatioDuringPinch)
return true;
}
if (!is_pinching) {
if (raster_page_scale_ != ideal_page_scale_)
return true;
}
if (raster_device_scale_ != ideal_device_scale_)
return true;
float max_scale = MaximumContentsScale();
if (raster_contents_scale_.x() > max_scale ||
raster_contents_scale_.y() > max_scale)
return true;
float min_scale = MinimumContentsScale();
if (raster_contents_scale_.x() < min_scale ||
raster_contents_scale_.y() < min_scale)
return true;
if (draw_properties().screen_space_transform_is_animating) {
if (layer_tree_impl()->device_viewport_rect_changed()) {
return true;
}
if (ideal_contents_scale_.x() >
raster_contents_scale_.x() *
kRatioToAdjustRasterScaleForTransformAnimation ||
ideal_contents_scale_.y() >
raster_contents_scale_.y() *
kRatioToAdjustRasterScaleForTransformAnimation) {
auto* property_trees = layer_tree_impl()->property_trees();
int transform_id = transform_tree_index();
if (property_trees->AnimationScaleCacheIsInvalid(transform_id) ||
!property_trees->AnimationAffectedByInvalidScale(transform_id)) {
return true;
}
}
return false;
}
if (raster_source_scale_ == ideal_source_scale_)
return false;
if (AffectedByWillChangeTransformHint()) {
float min_raster_scale = MinimumRasterContentsScaleForWillChangeTransform();
if (raster_contents_scale_.x() >= min_raster_scale &&
raster_contents_scale_.y() >= min_raster_scale)
return false;
}
return true;
}
void PictureLayerImpl::RecalculateRasterScales() {
if (IsDirectlyCompositedImage()) {
float used_raster_scale = CalculateDirectlyCompositedImageRasterScale();
directly_composited_image_default_raster_scale_changed_ = false;
if (ShouldDirectlyCompositeImage(used_raster_scale)) {
raster_source_scale_ =
gfx::Vector2dF(used_raster_scale, used_raster_scale);
raster_page_scale_ = 1.f;
raster_device_scale_ = 1.f;
raster_contents_scale_ = raster_source_scale_;
return;
}
directly_composited_image_default_raster_scale_ = 0.f;
}
gfx::Vector2dF old_raster_contents_scale = raster_contents_scale_;
float old_raster_page_scale = raster_page_scale_;
gfx::Vector2dF preserved_raster_contents_scale = old_raster_contents_scale;
raster_device_scale_ = ideal_device_scale_;
raster_page_scale_ = ideal_page_scale_;
raster_source_scale_ = ideal_source_scale_;
raster_contents_scale_ = ideal_contents_scale_;
bool is_pinching = layer_tree_impl()->PinchGestureActive();
if (is_pinching && !old_raster_contents_scale.IsZero()) {
bool zooming_out = old_raster_page_scale > ideal_page_scale_;
float desired_contents_scale =
std::max(old_raster_contents_scale.x(), old_raster_contents_scale.y());
float ideal_scale = GetIdealContentsScaleKey();
if (zooming_out) {
while (desired_contents_scale > ideal_scale)
desired_contents_scale /= kMaxScaleRatioDuringPinch;
} else {
while (desired_contents_scale < ideal_scale)
desired_contents_scale *= kMaxScaleRatioDuringPinch;
}
if (const auto* snapped_to_tiling = tilings_->FindTilingWithNearestScaleKey(
desired_contents_scale, kSnapToExistingTilingRatio)) {
raster_contents_scale_ = snapped_to_tiling->raster_transform().scale();
} else {
raster_contents_scale_ = old_raster_contents_scale;
raster_contents_scale_.Scale(desired_contents_scale /
raster_contents_scale_key());
}
preserved_raster_contents_scale = raster_contents_scale_;
raster_page_scale_ =
std::max(raster_contents_scale_.x() / raster_source_scale_.x(),
raster_contents_scale_.y() / raster_source_scale_.y()) /
raster_device_scale_;
}
if (draw_properties().screen_space_transform_is_animating)
AdjustRasterScaleForTransformAnimation(preserved_raster_contents_scale);
if (AffectedByWillChangeTransformHint()) {
float min_scale = MinimumRasterContentsScaleForWillChangeTransform();
raster_contents_scale_.SetToMax(gfx::Vector2dF(min_scale, min_scale));
}
float min_scale = MinimumContentsScale();
float max_scale = MaximumContentsScale();
raster_contents_scale_.SetToMax(gfx::Vector2dF(min_scale, min_scale));
raster_contents_scale_.SetToMin(gfx::Vector2dF(max_scale, max_scale));
DCHECK_GE(raster_contents_scale_.x(), min_scale);
DCHECK_GE(raster_contents_scale_.y(), min_scale);
DCHECK_LE(raster_contents_scale_.x(), max_scale);
DCHECK_LE(raster_contents_scale_.y(), max_scale);
}
void PictureLayerImpl::AdjustRasterScaleForTransformAnimation(
const gfx::Vector2dF& preserved_raster_contents_scale) {
DCHECK(draw_properties().screen_space_transform_is_animating);
float maximum_animation_scale =
layer_tree_impl()->property_trees()->MaximumAnimationToScreenScale(
transform_tree_index());
raster_contents_scale_.SetToMax(
gfx::Vector2dF(maximum_animation_scale, maximum_animation_scale));
if (AffectedByWillChangeTransformHint()) {
raster_contents_scale_.SetToMax(preserved_raster_contents_scale);
}
gfx::Size viewport = layer_tree_impl()->GetDeviceViewport().size();
constexpr int kMinViewportDimension = 500;
float max_viewport_dimension =
std::max({viewport.width(), viewport.height(), kMinViewportDimension});
DCHECK(max_viewport_dimension);
float squared_viewport_area = max_viewport_dimension * max_viewport_dimension;
gfx::SizeF max_visible_bounds(raster_source_->recorded_bounds().size());
max_visible_bounds.SetToMin(
gfx::SizeF(max_viewport_dimension, max_viewport_dimension));
gfx::SizeF max_visible_bounds_at_max_scale =
gfx::ScaleSize(max_visible_bounds, raster_contents_scale_.x(),
raster_contents_scale_.y());
float maximum_area = max_visible_bounds_at_max_scale.width() *
max_visible_bounds_at_max_scale.height();
if (maximum_area > squared_viewport_area) [[unlikely]] {
raster_contents_scale_.Scale(
1.f / std::sqrt(maximum_area / squared_viewport_area));
}
}
void PictureLayerImpl::CleanUpTilingsOnActiveLayer() {
DCHECK(layer_tree_impl()->IsActiveTree());
if (tilings_->num_tilings() == 0) {
return;
}
float min_acceptable_high_res_scale =
std::min(raster_contents_scale_key(), GetIdealContentsScaleKey());
float max_acceptable_high_res_scale =
std::max(raster_contents_scale_key(), GetIdealContentsScaleKey());
PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
if (twin && twin->CanHaveTilings()) {
min_acceptable_high_res_scale = std::min(
{min_acceptable_high_res_scale, twin->raster_contents_scale_key(),
twin->GetIdealContentsScaleKey()});
max_acceptable_high_res_scale = std::max(
{max_acceptable_high_res_scale, twin->raster_contents_scale_key(),
twin->GetIdealContentsScaleKey()});
}
std::vector<PictureLayerTiling*> to_remove;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = tilings_->tiling_at(i);
if (tiling->contents_scale_key() >= min_acceptable_high_res_scale &&
tiling->contents_scale_key() <= max_acceptable_high_res_scale) {
continue;
}
if (base::Contains(last_append_quads_tilings_, tiling)) {
continue;
}
if (layer_tree_impl()->settings().TreesInVizInClientProcess()) {
proposed_tiling_scales_for_deletion_.insert(tiling->contents_scale_key());
} else {
to_remove.push_back(tiling);
}
}
if (layer_tree_impl()->settings().TreesInVizInClientProcess()) {
return;
}
for (auto* tiling : to_remove) {
DCHECK_NE(HIGH_RESOLUTION, tiling->resolution());
tilings_->Remove(tiling);
}
}
float PictureLayerImpl::MinimumRasterContentsScaleForWillChangeTransform()
const {
DCHECK(AffectedByWillChangeTransformHint());
float native_scale = ideal_device_scale_ * ideal_page_scale_;
float ideal_scale = GetIdealContentsScaleKey();
if (ideal_scale < native_scale * kMinScaleRatioForWillChangeTransform) {
return ideal_scale * kMinScaleRatioForWillChangeTransform;
}
return native_scale;
}
void PictureLayerImpl::CleanUpTilings(
const std::vector<float>& tiling_scales_to_clean_up) {
for (float scale : tiling_scales_to_clean_up) {
if (auto* tiling = tilings_->FindTilingWithScaleKey(scale)) {
tilings_->Remove(tiling);
}
}
}
bool PictureLayerImpl::CalculateRasterTranslation(
gfx::Vector2dF& raster_translation) const {
if (layer_tree_impl()->settings().layers_always_allowed_lcd_text)
return true;
if (!raster_source_ || !raster_source_->GetDisplayItemList() ||
!raster_source_->GetDisplayItemList()->has_draw_text_ops()) {
return false;
}
const float external_page_scale_factor =
(base::FeatureList::IsEnabled(
features::kComputeRasterTranslateForExternalScale) &&
layer_tree_impl())
? layer_tree_impl()->external_page_scale_factor()
: 1.f;
gfx::Transform scaled_draw_transform = DrawTransform();
scaled_draw_transform.PostScale(external_page_scale_factor);
if (!draw_property_utils::RasterScalesApproximatelyEqual(
scaled_draw_transform.To2dScale(), raster_contents_scale_)) {
return false;
}
gfx::Transform scaled_screen_space_transform = ScreenSpaceTransform();
scaled_screen_space_transform.PostScale(external_page_scale_factor);
if (auto offset = draw_property_utils::PixelAlignmentOffset(
scaled_screen_space_transform, scaled_draw_transform)) {
raster_translation = *offset;
return true;
}
return false;
}
float PictureLayerImpl::MinimumContentsScale() const {
gfx::Size recorded_size = raster_source_->recorded_bounds().size();
int min_dimension = std::min(recorded_size.width(), recorded_size.height());
return min_dimension ? 1.f / min_dimension : 1.f;
}
float PictureLayerImpl::MaximumContentsScale() const {
if (bounds().IsEmpty())
return 0;
float max_dimension = static_cast<float>(
is_backdrop_filter_mask() ? layer_tree_impl()->max_texture_size()
: std::numeric_limits<int>::max());
int higher_dimension = std::max(bounds().width(), bounds().height());
float max_scale = max_dimension / higher_dimension;
return nextafterf(max_scale, 0.f);
}
void PictureLayerImpl::ResetRasterScale() {
raster_page_scale_ = 0.f;
raster_device_scale_ = 0.f;
raster_source_scale_ = gfx::Vector2dF(0.f, 0.f);
raster_contents_scale_ = gfx::Vector2dF(0.f, 0.f);
directly_composited_image_default_raster_scale_ = 0.f;
}
bool PictureLayerImpl::CanHaveTilings() const {
if (!raster_source_)
return false;
if (solid_color()) {
return false;
}
if (!draws_content())
return false;
if (!raster_source_->HasRecordings())
return false;
DCHECK(!raster_source_->size().IsEmpty());
if (MaximumContentsScale() < MinimumContentsScale())
return false;
return true;
}
void PictureLayerImpl::SanityCheckTilingState() const {
#if DCHECK_IS_ON()
if (!CanHaveTilings()) {
DCHECK_EQ(0u, tilings_->num_tilings());
return;
}
if (tilings_->num_tilings() == 0)
return;
if (layer_tree_impl()->settings().TreesInVizInClientProcess()) {
return;
}
DCHECK_EQ(1, tilings_->NumHighResTilings());
#endif
}
float PictureLayerImpl::MaximumTilingContentsScale() const {
float max_contents_scale = tilings_->GetMaximumContentsScale();
return std::max(max_contents_scale, MinimumContentsScale());
}
std::unique_ptr<PictureLayerTilingSet>
PictureLayerImpl::CreatePictureLayerTilingSet() {
const LayerTreeSettings& settings = layer_tree_impl()->settings();
return PictureLayerTilingSet::Create(
IsActive() ? ACTIVE_TREE : PENDING_TREE, this,
settings.tiling_interest_area_padding,
layer_tree_impl()->raster_caps().use_gpu_rasterization
? settings.gpu_rasterization_skewport_target_time_in_seconds
: settings.skewport_target_time_in_seconds,
settings.skewport_extrapolation_limit_in_screen_pixels,
settings.max_preraster_distance_in_screen_pixels);
}
void PictureLayerImpl::UpdateIdealScales() {
DCHECK(CanHaveTilings());
float min_contents_scale = MinimumContentsScale();
DCHECK_GT(min_contents_scale, 0.f);
ideal_device_scale_ = layer_tree_impl()->device_scale_factor();
ideal_page_scale_ = 1.f;
ideal_contents_scale_ = GetIdealContentsScale();
if (layer_tree_impl()->PageScaleTransformNode()) {
DCHECK(layer_tree_impl()->settings().is_for_scalable_page);
ideal_page_scale_ = IsAffectedByPageScale()
? layer_tree_impl()->current_page_scale_factor()
: 1.f;
}
float external_page_scale_factor =
layer_tree_impl() ? layer_tree_impl()->external_page_scale_factor() : 1.f;
DCHECK(!layer_tree_impl() ||
layer_tree_impl()->settings().is_for_scalable_page ||
external_page_scale_factor == 1.f ||
layer_tree_impl()->current_page_scale_factor() == 1.f);
ideal_page_scale_ *= external_page_scale_factor;
ideal_contents_scale_.Scale(external_page_scale_factor);
ideal_contents_scale_.SetToMax(
gfx::Vector2dF(min_contents_scale, min_contents_scale));
ideal_contents_scale_.SetToMin(
gfx::Vector2dF(kMaxIdealContentsScale, kMaxIdealContentsScale));
ideal_source_scale_ = {ideal_contents_scale_.x() / ideal_page_scale_,
ideal_contents_scale_.y() / ideal_page_scale_};
}
void PictureLayerImpl::GetDebugBorderProperties(SkColor4f* color,
float* width) const {
float device_scale_factor =
layer_tree_impl() ? layer_tree_impl()->device_scale_factor() : 1;
if (IsDirectlyCompositedImage()) {
*color = DebugColors::ImageLayerBorderColor();
*width = DebugColors::ImageLayerBorderWidth(device_scale_factor);
} else {
*color = DebugColors::TiledContentLayerBorderColor();
*width = DebugColors::TiledContentLayerBorderWidth(device_scale_factor);
}
}
void PictureLayerImpl::GetAllPrioritizedTilesForTracing(
std::vector<PrioritizedTile>* prioritized_tiles) const {
if (!tilings_)
return;
tilings_->GetAllPrioritizedTilesForTracing(prioritized_tiles);
}
void PictureLayerImpl::AsValueInto(
base::trace_event::TracedValue* state) const {
LayerImpl::AsValueInto(state);
state->SetDouble("ideal_contents_scale", GetIdealContentsScaleKey());
state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
state->BeginArray("tilings");
tilings_->AsValueInto(state);
state->EndArray();
MathUtil::AddToTracedValue("tile_priority_rect",
viewport_rect_for_tile_priority_in_content_space_,
state);
MathUtil::AddToTracedValue("visible_rect", visible_layer_rect(), state);
state->SetString(
"lcd_text_disallowed_reason",
LCDTextDisallowedReasonToString(lcd_text_disallowed_reason_));
state->BeginArray("pictures");
raster_source_->AsValueInto(state);
state->EndArray();
state->BeginArray("invalidation");
invalidation_.AsValueInto(state);
state->EndArray();
state->BeginArray("coverage_tiles");
for (auto iter =
tilings_->Cover(gfx::Rect(bounds()), MaximumTilingContentsScale(),
GetIdealContentsScaleKey());
iter; ++iter) {
state->BeginDictionary();
MathUtil::AddToTracedValue("geometry_rect", iter.geometry_rect(), state);
if (*iter)
viz::TracedValue::SetIDRef(viz::TracedValue::Id(*iter), state, "tile");
state->EndDictionary();
}
state->EndArray();
state->BeginDictionary("can_have_tilings_state");
state->SetBoolean("can_have_tilings", CanHaveTilings());
state->SetBoolean("raster_source_solid_color", !!solid_color());
state->SetBoolean("draws_content", draws_content());
state->SetBoolean("raster_source_has_recordings",
raster_source_->HasRecordings());
state->SetDouble("max_contents_scale", MaximumTilingContentsScale());
state->SetDouble("min_contents_scale", MinimumContentsScale());
state->EndDictionary();
state->BeginDictionary("raster_scales");
state->SetDouble("page_scale", raster_page_scale_);
state->SetDouble("device_scale", raster_device_scale_);
state->BeginArray("source_scale");
state->AppendDouble(raster_source_scale_.x());
state->AppendDouble(raster_source_scale_.y());
state->EndArray();
state->BeginArray("contents_scale");
state->AppendDouble(raster_contents_scale_.x());
state->AppendDouble(raster_contents_scale_.y());
state->EndArray();
state->EndDictionary();
state->BeginDictionary("ideal_scales");
state->SetDouble("page_scale", ideal_page_scale_);
state->SetDouble("device_scale", ideal_device_scale_);
state->BeginArray("source_scale");
state->AppendDouble(ideal_source_scale_.x());
state->AppendDouble(ideal_source_scale_.y());
state->EndArray();
state->BeginArray("contents_scale");
state->AppendDouble(ideal_contents_scale_.x());
state->AppendDouble(ideal_contents_scale_.y());
state->EndArray();
state->EndDictionary();
}
size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
return tilings_->GPUMemoryUsageInBytes();
}
void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
benchmark->RunOnLayer(this);
}
bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
return !layer_tree_impl()->IsRecycleTree();
}
bool PictureLayerImpl::HasValidTilePriorities() const {
return IsOnActiveOrPendingTree() &&
(contributes_to_drawn_render_surface() || raster_even_if_not_drawn());
}
PictureLayerImpl::ImageInvalidationResult
PictureLayerImpl::InvalidateRegionForImages(
const PaintImageIdFlatSet& images_to_invalidate) {
if (!discardable_image_map_ || discardable_image_map_->empty()) {
return ImageInvalidationResult::kNoImages;
}
bool all_animated_image = true;
auto* controller = layer_tree_impl()->image_animation_controller();
InvalidationRegion image_invalidation;
for (auto image_id : images_to_invalidate) {
all_animated_image &= controller->IsRegistered(image_id);
const auto& rects = discardable_image_map_->GetRectsForImage(image_id);
for (const auto& r : rects) {
image_invalidation.Union(r);
}
}
Region invalidation;
image_invalidation.Swap(&invalidation);
if (invalidation.IsEmpty())
return ImageInvalidationResult::kNoInvalidation;
UnionUpdateRect(invalidation.bounds());
if (all_animated_image) {
has_animated_image_update_rect_ = true;
} else {
has_non_animated_image_update_rect_ = true;
}
invalidation_.Union(invalidation);
tilings_->Invalidate(invalidation);
return ImageInvalidationResult::kInvalidated;
}
void PictureLayerImpl::InvalidateRasterInducingScrolls(
const base::flat_set<ElementId>& scrolls_to_invalidate) {
if (!raster_source_ || !raster_source_->GetDisplayItemList()) {
return;
}
const DisplayItemList::RasterInducingScrollMap& raster_inducing_scrolls =
raster_source_->GetDisplayItemList()->raster_inducing_scrolls();
Region invalidation;
bool needs_update_discardable_image_map = false;
for (ElementId element_id : scrolls_to_invalidate) {
auto it = raster_inducing_scrolls.find(element_id);
if (it != raster_inducing_scrolls.end()) {
UnionUpdateRect(it->second.visual_rect);
has_non_animated_image_update_rect_ = true;
invalidation.Union(it->second.visual_rect);
needs_update_discardable_image_map |= it->second.has_discardable_images;
}
}
if (!invalidation.IsEmpty()) {
if (needs_update_discardable_image_map) {
discardable_image_map_ =
raster_source_->GetDisplayItemList()->GenerateDiscardableImageMap(
GetRasterInducingScrollOffsets());
}
invalidation_.Union(invalidation);
tilings_->Invalidate(invalidation);
}
}
void PictureLayerImpl::SetPaintWorkletRecord(
scoped_refptr<const PaintWorkletInput> input,
PaintRecord record) {
DCHECK(base::Contains(paint_worklet_records_, input));
paint_worklet_records_[input].second = std::move(record);
}
void PictureLayerImpl::RegisterAnimatedImages() {
if (!discardable_image_map_) {
return;
}
auto* controller = layer_tree_impl()->image_animation_controller();
for (const auto& data : discardable_image_map_->animated_images_metadata()) {
if (layer_tree_impl()->IsSyncTree())
controller->UpdateAnimatedImage(data);
controller->RegisterAnimationDriver(data.paint_image_id, this);
}
}
void PictureLayerImpl::UnregisterAnimatedImages() {
if (!discardable_image_map_) {
return;
}
auto* controller = layer_tree_impl()->image_animation_controller();
for (const auto& data : discardable_image_map_->animated_images_metadata()) {
controller->UnregisterAnimationDriver(data.paint_image_id, this);
}
}
void PictureLayerImpl::SetPaintWorkletInputs(
const DiscardableImageMap::PaintWorkletInputs& inputs) {
DCHECK(layer_tree_impl()->IsPendingTree() || inputs.empty());
bool had_paint_worklets = !paint_worklet_records_.empty();
PaintWorkletRecordMap new_records;
for (const auto& input_with_id : inputs) {
const auto& input = input_with_id.first;
const auto& paint_image_id = input_with_id.second;
auto it = new_records.find(input);
DCHECK(it == new_records.end() || it->second.first == paint_image_id);
new_records[input] = std::make_pair(
paint_image_id, std::move(paint_worklet_records_[input].second));
paint_worklet_records_[input].second = std::nullopt;
}
paint_worklet_records_.swap(new_records);
bool has_paint_worklets = !paint_worklet_records_.empty();
if ((has_paint_worklets != had_paint_worklets) &&
layer_tree_impl()->IsPendingTree()) {
layer_tree_impl()->NotifyLayerHasPaintWorkletsChanged(this,
has_paint_worklets);
}
if (layer_tree_impl()->IsPendingTree()) {
layer_tree_impl()
->paint_worklet_tracker()
.UpdatePaintWorkletInputProperties(inputs, this);
}
}
void PictureLayerImpl::InvalidatePaintWorklets(
const PaintWorkletInput::PropertyKey& key,
const PaintWorkletInput::PropertyValue& prev,
const PaintWorkletInput::PropertyValue& next) {
for (auto& entry : paint_worklet_records_) {
const std::vector<PaintWorkletInput::PropertyKey>& prop_ids =
entry.first->GetPropertyKeys();
if (base::Contains(prop_ids, key) &&
entry.first->ValueChangeShouldCauseRepaint(prev, next)) {
entry.second.second = std::nullopt;
}
}
}
PictureLayerImpl::TileUpdateSet PictureLayerImpl::TakeUpdatedTiles() {
TileUpdateSet updates;
updates.swap(updated_tiles_);
should_batch_updated_tiles_ = false;
return updates;
}
std::vector<float> PictureLayerImpl::TakeProposedTilingScalesForDeletion() {
std::vector<float> updates(proposed_tiling_scales_for_deletion_.begin(),
proposed_tiling_scales_for_deletion_.end());
proposed_tiling_scales_for_deletion_.clear();
return updates;
}
PictureLayerImpl::TileUpdateSet PictureLayerImpl::TakeAllTiles() {
DCHECK(layer_tree_impl()->settings().TreesInVizInClientProcess());
DCHECK(layer_tree_impl()->IsActiveTree());
updated_tiles_.clear();
TileUpdateSet updates;
for (size_t ii = 0; ii < tilings_->num_tilings(); ++ii) {
PictureLayerTiling::TileIterator iter(tilings_->tiling_at(ii));
for (; !iter.AtEnd(); iter.Next()) {
Tile* tile = iter.GetCurrent();
updates[tile->contents_scale_key()].emplace(tile->tiling_i_index(),
tile->tiling_j_index());
}
}
should_batch_updated_tiles_ = false;
return updates;
}
gfx::ContentColorUsage PictureLayerImpl::GetContentColorUsage() const {
auto display_item_list = raster_source_->GetDisplayItemList();
if (!display_item_list)
return gfx::ContentColorUsage::kSRGB;
return display_item_list->content_color_usage();
}
DamageReasonSet PictureLayerImpl::GetDamageReasons() const {
DamageReasonSet reasons = GetDamageReasonsFromLayerPropertyChange();
if (has_animated_image_update_rect_) {
reasons.Put(DamageReason::kAnimatedImage);
}
if (has_non_animated_image_update_rect_ || !damage_rect_.IsEmpty()) {
reasons.Put(DamageReason::kUntracked);
}
return reasons;
}
float PictureLayerImpl::GetMaximumContentsScaleForUseInAppendQuads() {
return tilings_->num_tilings() ? MaximumTilingContentsScale() : 1.f;
}
TileBasedLayerImpl<PictureLayerTiling>::TilingResolution
PictureLayerImpl::GetTilingResolutionForDebugBorders(
const PictureLayerTiling* tiling) const {
if (tiling->resolution() == HIGH_RESOLUTION) {
return TilingResolution::kHigh;
}
if (tiling->contents_scale_key() > raster_contents_scale_key()) {
return TilingResolution::kAboveHigh;
}
return TilingResolution::kBelowHigh;
}
TilingSetCoverageIterator<PictureLayerTiling> PictureLayerImpl::Cover(
const gfx::Rect& coverage_rect,
float coverage_scale,
float ideal_contents_scale) {
return tilings_->Cover(coverage_rect, coverage_scale, ideal_contents_scale);
}
float PictureLayerImpl::GetIdealContentsScaleKey() const {
return std::max(ideal_contents_scale_.x(), ideal_contents_scale_.y());
}
}