This source file includes following definitions.
- SortLayers
- SortLayers
- GetEffectiveScrollDelta
- GetEffectiveTotalScrollOffset
- CalculateVisibleRectWithCachedLayerRect
- CalculateVisibleRect
- NextTargetSurface
- ComputeChangeOfBasisTranslation
- TranslateRectToTargetSpace
- UpdateClipRectsForClipChild
- UpdateAccumulatedSurfaceState
- IsRootLayer
- LayerIsInExisting3DRenderingContext
- IsRootLayerOfNewRenderingContext
- IsLayerBackFaceVisible
- IsSurfaceBackFaceVisible
- LayerClipsSubtree
- CalculateVisibleContentRect
- TransformToParentIsKnown
- TransformToParentIsKnown
- TransformToScreenIsKnown
- TransformToScreenIsKnown
- LayerShouldBeSkipped
- SubtreeShouldBeSkipped
- SubtreeShouldBeSkipped
- SavePaintPropertiesLayer
- SavePaintPropertiesLayer
- SubtreeShouldRenderToSeparateSurface
- ComputeSizeDeltaCompensation
- ApplyPositionAdjustment
- ApplyPositionAdjustment
- ComputeScrollCompensationForThisLayer
- ComputeScrollCompensationMatrixForChildren
- ComputeScrollCompensationMatrixForChildren
- CalculateContentsScale
- UpdateLayerContentsScale
- UpdateLayerContentsScale
- CreateOrReuseRenderSurface
- CreateOrReuseRenderSurface
- RemoveSurfaceForEarlyExit
- PreCalculateMetaInformation
- RoundTranslationComponents
- GetChildContainingLayer
- AddScrollParentChain
- SortChildrenForRecursion
- GetNewDescendantsStartIndexAndCount
- GetNewRenderSurfacesStartIndexAndCount
- SortLayerListContributions
- CalculateDrawPropertiesInternal
- CalculateDrawProperties
- CalculateDrawProperties
- PointHitsRect
- PointHitsRegion
- PointIsClippedBySurfaceOrClipRect
- FindLayerThatIsHitByPoint
- FindLayerThatIsHitByPointInTouchHandlerRegion
- LayerHasTouchEventHandlersAt
#include "cc/trees/layer_tree_host_common.h"
#include <algorithm>
#include "base/debug/trace_event.h"
#include "cc/base/math_util.h"
#include "cc/layers/heads_up_display_layer_impl.h"
#include "cc/layers/layer.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/layer_iterator.h"
#include "cc/layers/render_surface.h"
#include "cc/layers/render_surface_impl.h"
#include "cc/trees/layer_sorter.h"
#include "cc/trees/layer_tree_impl.h"
#include "ui/gfx/point_conversions.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/transform.h"
namespace cc {
ScrollAndScaleSet::ScrollAndScaleSet() {}
ScrollAndScaleSet::~ScrollAndScaleSet() {}
static void SortLayers(LayerList::iterator forst,
LayerList::iterator end,
void* layer_sorter) {
NOTREACHED();
}
static void SortLayers(LayerImplList::iterator first,
LayerImplList::iterator end,
LayerSorter* layer_sorter) {
DCHECK(layer_sorter);
TRACE_EVENT0("cc", "LayerTreeHostCommon::SortLayers");
layer_sorter->Sort(first, end);
}
template <typename LayerType>
static gfx::Vector2dF GetEffectiveScrollDelta(LayerType* layer) {
gfx::Vector2dF scroll_delta = layer->ScrollDelta();
if (layer->scroll_parent())
scroll_delta += layer->scroll_parent()->ScrollDelta();
return scroll_delta;
}
template <typename LayerType>
static gfx::Vector2dF GetEffectiveTotalScrollOffset(LayerType* layer) {
gfx::Vector2dF offset = layer->TotalScrollOffset();
if (layer->scroll_parent())
offset += layer->scroll_parent()->ScrollDelta();
return offset;
}
inline gfx::Rect CalculateVisibleRectWithCachedLayerRect(
const gfx::Rect& target_surface_rect,
const gfx::Rect& layer_bound_rect,
const gfx::Rect& layer_rect_in_target_space,
const gfx::Transform& transform) {
if (layer_rect_in_target_space.IsEmpty())
return gfx::Rect();
if (target_surface_rect.Contains(layer_rect_in_target_space))
return layer_bound_rect;
gfx::Rect minimal_surface_rect = target_surface_rect;
minimal_surface_rect.Intersect(layer_rect_in_target_space);
if (minimal_surface_rect.IsEmpty())
return gfx::Rect();
gfx::Transform surface_to_layer(gfx::Transform::kSkipInitialization);
if (!transform.GetInverse(&surface_to_layer)) {
return layer_bound_rect;
}
gfx::Rect layer_rect = MathUtil::ProjectEnclosingClippedRect(
surface_to_layer, minimal_surface_rect);
layer_rect.Intersect(layer_bound_rect);
return layer_rect;
}
gfx::Rect LayerTreeHostCommon::CalculateVisibleRect(
const gfx::Rect& target_surface_rect,
const gfx::Rect& layer_bound_rect,
const gfx::Transform& transform) {
gfx::Rect layer_in_surface_space =
MathUtil::MapEnclosingClippedRect(transform, layer_bound_rect);
return CalculateVisibleRectWithCachedLayerRect(
target_surface_rect, layer_bound_rect, layer_in_surface_space, transform);
}
template <typename LayerType>
static LayerType* NextTargetSurface(LayerType* layer) {
return layer->parent() ? layer->parent()->render_target() : 0;
}
template <typename LayerType>
static gfx::Vector2dF ComputeChangeOfBasisTranslation(
const LayerType& ancestor_layer,
const LayerType& descendant_layer) {
DCHECK(descendant_layer.HasAncestor(&ancestor_layer));
const LayerType* descendant_target = descendant_layer.render_target();
DCHECK(descendant_target);
const LayerType* ancestor_target = ancestor_layer.render_target();
DCHECK(ancestor_target);
gfx::Vector2dF translation;
for (const LayerType* target = descendant_target; target != ancestor_target;
target = NextTargetSurface(target)) {
const gfx::Transform& trans = target->render_surface()->draw_transform();
DCHECK(trans.IsIdentityOrTranslation());
DCHECK_EQ(0.f, trans.matrix().get(2, 3));
translation += trans.To2dTranslation();
}
return translation;
}
enum TranslateRectDirection {
TranslateRectDirectionToAncestor,
TranslateRectDirectionToDescendant
};
template <typename LayerType>
static gfx::Rect TranslateRectToTargetSpace(const LayerType& ancestor_layer,
const LayerType& descendant_layer,
const gfx::Rect& rect,
TranslateRectDirection direction) {
gfx::Vector2dF translation = ComputeChangeOfBasisTranslation<LayerType>(
ancestor_layer, descendant_layer);
if (direction == TranslateRectDirectionToDescendant)
translation.Scale(-1.f);
return gfx::ToEnclosingRect(
gfx::RectF(rect.origin() + translation, rect.size()));
}
template <typename LayerType>
static void UpdateClipRectsForClipChild(
const LayerType* layer,
gfx::Rect* clip_rect_in_parent_target_space,
bool* subtree_should_be_clipped) {
const LayerType* clip_parent = layer->scroll_parent();
if (!clip_parent)
clip_parent = layer->clip_parent();
if (!clip_parent || clip_parent == layer->parent())
return;
DCHECK(layer->parent());
*clip_rect_in_parent_target_space = clip_parent->clip_rect();
*subtree_should_be_clipped = clip_parent->is_clipped();
if (clip_parent == layer->clip_parent()) {
*clip_rect_in_parent_target_space = TranslateRectToTargetSpace<LayerType>(
*clip_parent,
*layer->parent(),
*clip_rect_in_parent_target_space,
TranslateRectDirectionToDescendant);
} else {
*clip_rect_in_parent_target_space =
TranslateRectToTargetSpace<LayerType>(*layer->parent(),
*clip_parent,
*clip_rect_in_parent_target_space,
TranslateRectDirectionToAncestor);
}
}
template <typename LayerType>
struct AccumulatedSurfaceState {
explicit AccumulatedSurfaceState(LayerType* render_target)
: render_target(render_target) {}
gfx::Rect drawable_content_rect;
LayerType* render_target;
};
template <typename LayerType>
void UpdateAccumulatedSurfaceState(
LayerType* layer,
const gfx::Rect& drawable_content_rect,
std::vector<AccumulatedSurfaceState<LayerType> >*
accumulated_surface_state) {
if (IsRootLayer(layer))
return;
LayerType* render_target = layer->clip_parent()
? layer->clip_parent()->render_target()
: layer->parent()->render_target();
gfx::Rect target_rect = drawable_content_rect;
if (layer->render_surface()) {
target_rect =
gfx::ToEnclosedRect(layer->render_surface()->DrawableContentRect());
}
if (render_target->is_clipped()) {
gfx::Rect clip_rect = render_target->clip_rect();
if (layer->clip_parent()) {
clip_rect = TranslateRectToTargetSpace<LayerType>(
*layer->clip_parent(),
*layer,
clip_rect,
TranslateRectDirectionToDescendant);
}
target_rect.Intersect(clip_rect);
}
DCHECK_LT(0ul, accumulated_surface_state->size());
typedef typename std::vector<AccumulatedSurfaceState<LayerType> >
AccumulatedSurfaceStateVector;
typedef typename AccumulatedSurfaceStateVector::reverse_iterator
AccumulatedSurfaceStateIterator;
AccumulatedSurfaceStateIterator current_state =
accumulated_surface_state->rbegin();
bool found_render_target = false;
for (; current_state != accumulated_surface_state->rend(); ++current_state) {
current_state->drawable_content_rect.Union(target_rect);
if (current_state->render_target == render_target) {
found_render_target = true;
break;
}
LayerType* current_target = current_state->render_target;
DCHECK(current_target->render_surface());
const gfx::Transform& current_draw_transform =
current_target->render_surface()->draw_transform();
DCHECK(current_target->num_unclipped_descendants() == 0 ||
current_draw_transform.IsIdentityOrTranslation());
target_rect = gfx::ToEnclosingRect(
MathUtil::MapClippedRect(current_draw_transform, target_rect));
}
DCHECK(found_render_target);
}
template <typename LayerType> static inline bool IsRootLayer(LayerType* layer) {
return !layer->parent();
}
template <typename LayerType>
static inline bool LayerIsInExisting3DRenderingContext(LayerType* layer) {
return layer->is_3d_sorted() && layer->parent() &&
layer->parent()->is_3d_sorted();
}
template <typename LayerType>
static bool IsRootLayerOfNewRenderingContext(LayerType* layer) {
if (layer->parent())
return !layer->parent()->is_3d_sorted() && layer->is_3d_sorted();
return layer->is_3d_sorted();
}
template <typename LayerType>
static bool IsLayerBackFaceVisible(LayerType* layer) {
if (LayerIsInExisting3DRenderingContext(layer))
return layer->draw_transform().IsBackFaceVisible();
return layer->transform().IsBackFaceVisible();
}
template <typename LayerType>
static bool IsSurfaceBackFaceVisible(LayerType* layer,
const gfx::Transform& draw_transform) {
if (LayerIsInExisting3DRenderingContext(layer))
return draw_transform.IsBackFaceVisible();
if (IsRootLayerOfNewRenderingContext(layer))
return layer->transform().IsBackFaceVisible();
return false;
}
template <typename LayerType>
static inline bool LayerClipsSubtree(LayerType* layer) {
return layer->masks_to_bounds() || layer->mask_layer();
}
template <typename LayerType>
static gfx::Rect CalculateVisibleContentRect(
LayerType* layer,
const gfx::Rect& clip_rect_of_target_surface_in_target_space,
const gfx::Rect& layer_rect_in_target_space) {
DCHECK(layer->render_target());
if (!layer->DrawsContent() || layer->content_bounds().IsEmpty() ||
layer->drawable_content_rect().IsEmpty())
return gfx::Rect();
gfx::Rect visible_rect_in_target_surface_space =
layer->drawable_content_rect();
if (!layer->render_target()->render_surface()->clip_rect().IsEmpty()) {
visible_rect_in_target_surface_space.Intersect(
clip_rect_of_target_surface_in_target_space);
}
if (visible_rect_in_target_surface_space.IsEmpty())
return gfx::Rect();
return CalculateVisibleRectWithCachedLayerRect(
visible_rect_in_target_surface_space,
gfx::Rect(layer->content_bounds()),
layer_rect_in_target_space,
layer->draw_transform());
}
static inline bool TransformToParentIsKnown(LayerImpl* layer) { return true; }
static inline bool TransformToParentIsKnown(Layer* layer) {
return !layer->TransformIsAnimating();
}
static inline bool TransformToScreenIsKnown(LayerImpl* layer) { return true; }
static inline bool TransformToScreenIsKnown(Layer* layer) {
return !layer->screen_space_transform_is_animating();
}
template <typename LayerType>
static bool LayerShouldBeSkipped(LayerType* layer, bool layer_is_drawn) {
if (!layer_is_drawn)
return true;
if (layer->bounds().IsEmpty())
return true;
LayerType* backface_test_layer = layer;
if (layer->use_parent_backface_visibility()) {
DCHECK(layer->parent());
DCHECK(!layer->parent()->use_parent_backface_visibility());
backface_test_layer = layer->parent();
}
if (!backface_test_layer->double_sided() &&
TransformToScreenIsKnown(backface_test_layer) &&
IsLayerBackFaceVisible(backface_test_layer))
return true;
bool can_accept_input = !layer->touch_event_handler_region().IsEmpty() ||
layer->have_wheel_event_handlers();
if (!layer->DrawsContent() && !can_accept_input)
return true;
return false;
}
static inline bool SubtreeShouldBeSkipped(LayerImpl* layer,
bool layer_is_drawn) {
if (layer->draw_properties().layer_or_descendant_has_copy_request)
return false;
if (!layer_is_drawn)
return true;
if (layer->layer_tree_impl()->IsPendingTree() && layer->OpacityIsAnimating())
return false;
return !layer->opacity();
}
static inline bool SubtreeShouldBeSkipped(Layer* layer, bool layer_is_drawn) {
if (layer->draw_properties().layer_or_descendant_has_copy_request)
return false;
if (!layer_is_drawn)
return true;
return !layer->opacity() && !layer->OpacityIsAnimating() &&
!layer->OpacityCanAnimateOnImplThread();
}
static inline void SavePaintPropertiesLayer(LayerImpl* layer) {}
static inline void SavePaintPropertiesLayer(Layer* layer) {
layer->SavePaintProperties();
if (layer->mask_layer())
layer->mask_layer()->SavePaintProperties();
if (layer->replica_layer() && layer->replica_layer()->mask_layer())
layer->replica_layer()->mask_layer()->SavePaintProperties();
}
template <typename LayerType>
static bool SubtreeShouldRenderToSeparateSurface(
LayerType* layer,
bool axis_aligned_with_respect_to_parent) {
bool is_root = IsRootLayer(layer);
if (layer->mask_layer()) {
DCHECK(!is_root);
return true;
}
if (layer->replica_layer()) {
DCHECK(!is_root);
return true;
}
if (!layer->filters().IsEmpty() || !layer->background_filters().IsEmpty()) {
DCHECK(!is_root);
return true;
}
int num_descendants_that_draw_content =
layer->draw_properties().num_descendants_that_draw_content;
if (LayerIsInExisting3DRenderingContext(layer) &&
layer->should_flatten_transform() &&
num_descendants_that_draw_content > 0) {
TRACE_EVENT_INSTANT0(
"cc",
"LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening",
TRACE_EVENT_SCOPE_THREAD);
DCHECK(!is_root);
return true;
}
if (!layer->uses_default_blend_mode()) {
TRACE_EVENT_INSTANT0(
"cc",
"LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface blending",
TRACE_EVENT_SCOPE_THREAD);
DCHECK(!is_root);
return true;
}
bool layer_clips_external_content =
LayerClipsSubtree(layer) || layer->HasDelegatedContent();
if (layer_clips_external_content && !axis_aligned_with_respect_to_parent &&
num_descendants_that_draw_content > 0) {
TRACE_EVENT_INSTANT0(
"cc",
"LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping",
TRACE_EVENT_SCOPE_THREAD);
DCHECK(!is_root);
return true;
}
bool at_least_two_layers_in_subtree_draw_content =
num_descendants_that_draw_content > 0 &&
(layer->DrawsContent() || num_descendants_that_draw_content > 1);
if (layer->opacity() != 1.f && layer->should_flatten_transform() &&
at_least_two_layers_in_subtree_draw_content) {
TRACE_EVENT_INSTANT0(
"cc",
"LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity",
TRACE_EVENT_SCOPE_THREAD);
DCHECK(!is_root);
return true;
}
if (is_root)
return true;
if (layer->is_root_for_isolated_group()) {
TRACE_EVENT_INSTANT0(
"cc",
"LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface isolation",
TRACE_EVENT_SCOPE_THREAD);
return true;
}
if (layer->force_render_surface())
return true;
if (layer->HasCopyRequest())
return true;
return false;
}
gfx::Transform ComputeSizeDeltaCompensation(
LayerImpl* layer,
LayerImpl* container,
const gfx::Vector2dF& position_offset) {
gfx::Transform result_transform;
gfx::Transform target_surface_space_to_container_layer_space;
LayerImpl* container_target_surface = container->render_target();
for (LayerImpl* current_target_surface = NextTargetSurface(layer);
current_target_surface &&
current_target_surface != container_target_surface;
current_target_surface = NextTargetSurface(current_target_surface)) {
target_surface_space_to_container_layer_space.ConcatTransform(
current_target_surface->render_surface()->draw_transform());
}
gfx::Transform container_layer_space_to_container_target_surface_space =
container->draw_transform();
container_layer_space_to_container_target_surface_space.Scale(
container->contents_scale_x(), container->contents_scale_y());
gfx::Transform container_target_surface_space_to_container_layer_space;
if (container_layer_space_to_container_target_surface_space.GetInverse(
&container_target_surface_space_to_container_layer_space)) {
target_surface_space_to_container_layer_space.ConcatTransform(
container_target_surface_space_to_container_layer_space);
}
gfx::Transform container_layer_space_to_target_surface_space;
if (target_surface_space_to_container_layer_space.GetInverse(
&container_layer_space_to_target_surface_space)) {
result_transform.PreconcatTransform(
container_layer_space_to_target_surface_space);
} else {
return gfx::Transform();
}
result_transform.Translate(position_offset.x(), position_offset.y());
result_transform.PreconcatTransform(
target_surface_space_to_container_layer_space);
return result_transform;
}
void ApplyPositionAdjustment(
Layer* layer,
Layer* container,
const gfx::Transform& scroll_compensation,
gfx::Transform* combined_transform) {}
void ApplyPositionAdjustment(
LayerImpl* layer,
LayerImpl* container,
const gfx::Transform& scroll_compensation,
gfx::Transform* combined_transform) {
if (!layer->position_constraint().is_fixed_position())
return;
combined_transform->ConcatTransform(scroll_compensation);
bool fixed_to_right_edge =
layer->position_constraint().is_fixed_to_right_edge();
bool fixed_to_bottom_edge =
layer->position_constraint().is_fixed_to_bottom_edge();
gfx::Vector2dF position_offset = container->FixedContainerSizeDelta();
position_offset.set_x(fixed_to_right_edge ? position_offset.x() : 0);
position_offset.set_y(fixed_to_bottom_edge ? position_offset.y() : 0);
if (position_offset.IsZero())
return;
combined_transform->ConcatTransform(
ComputeSizeDeltaCompensation(layer, container, position_offset));
}
gfx::Transform ComputeScrollCompensationForThisLayer(
LayerImpl* scrolling_layer,
const gfx::Transform& parent_matrix,
const gfx::Vector2dF& scroll_delta) {
gfx::Transform scroll_compensation_for_this_layer = parent_matrix;
scroll_compensation_for_this_layer.Translate(
scroll_delta.x(),
scroll_delta.y());
gfx::Transform inverse_parent_matrix(gfx::Transform::kSkipInitialization);
if (!parent_matrix.GetInverse(&inverse_parent_matrix)) {
}
scroll_compensation_for_this_layer.PreconcatTransform(
inverse_parent_matrix);
return scroll_compensation_for_this_layer;
}
gfx::Transform ComputeScrollCompensationMatrixForChildren(
Layer* current_layer,
const gfx::Transform& current_parent_matrix,
const gfx::Transform& current_scroll_compensation,
const gfx::Vector2dF& scroll_delta) {
return gfx::Transform();
}
gfx::Transform ComputeScrollCompensationMatrixForChildren(
LayerImpl* layer,
const gfx::Transform& parent_matrix,
const gfx::Transform& current_scroll_compensation_matrix,
const gfx::Vector2dF& scroll_delta) {
bool current_layer_resets_scroll_compensation_for_descendants =
layer->IsContainerForFixedPositionLayers() ||
layer->position_constraint().is_fixed_position();
if (!current_layer_resets_scroll_compensation_for_descendants &&
scroll_delta.IsZero() && !layer->render_surface())
return current_scroll_compensation_matrix;
gfx::Transform next_scroll_compensation_matrix;
if (!current_layer_resets_scroll_compensation_for_descendants)
next_scroll_compensation_matrix = current_scroll_compensation_matrix;
if (!scroll_delta.IsZero()) {
gfx::Transform scroll_compensation_for_this_layer =
ComputeScrollCompensationForThisLayer(
layer, parent_matrix, scroll_delta);
next_scroll_compensation_matrix.PreconcatTransform(
scroll_compensation_for_this_layer);
}
if (layer->render_surface() &&
!next_scroll_compensation_matrix.IsIdentity()) {
gfx::Transform inverse_surface_draw_transform(
gfx::Transform::kSkipInitialization);
if (!layer->render_surface()->draw_transform().GetInverse(
&inverse_surface_draw_transform)) {
}
next_scroll_compensation_matrix =
inverse_surface_draw_transform * next_scroll_compensation_matrix *
layer->render_surface()->draw_transform();
}
return next_scroll_compensation_matrix;
}
template <typename LayerType>
static inline void CalculateContentsScale(LayerType* layer,
float contents_scale,
float device_scale_factor,
float page_scale_factor,
bool animating_transform_to_screen) {
layer->CalculateContentsScale(contents_scale,
device_scale_factor,
page_scale_factor,
animating_transform_to_screen,
&layer->draw_properties().contents_scale_x,
&layer->draw_properties().contents_scale_y,
&layer->draw_properties().content_bounds);
LayerType* mask_layer = layer->mask_layer();
if (mask_layer) {
mask_layer->CalculateContentsScale(
contents_scale,
device_scale_factor,
page_scale_factor,
animating_transform_to_screen,
&mask_layer->draw_properties().contents_scale_x,
&mask_layer->draw_properties().contents_scale_y,
&mask_layer->draw_properties().content_bounds);
}
LayerType* replica_mask_layer =
layer->replica_layer() ? layer->replica_layer()->mask_layer() : NULL;
if (replica_mask_layer) {
replica_mask_layer->CalculateContentsScale(
contents_scale,
device_scale_factor,
page_scale_factor,
animating_transform_to_screen,
&replica_mask_layer->draw_properties().contents_scale_x,
&replica_mask_layer->draw_properties().contents_scale_y,
&replica_mask_layer->draw_properties().content_bounds);
}
}
static inline void UpdateLayerContentsScale(
LayerImpl* layer,
bool can_adjust_raster_scale,
float ideal_contents_scale,
float device_scale_factor,
float page_scale_factor,
bool animating_transform_to_screen) {
CalculateContentsScale(layer,
ideal_contents_scale,
device_scale_factor,
page_scale_factor,
animating_transform_to_screen);
}
static inline void UpdateLayerContentsScale(
Layer* layer,
bool can_adjust_raster_scale,
float ideal_contents_scale,
float device_scale_factor,
float page_scale_factor,
bool animating_transform_to_screen) {
if (can_adjust_raster_scale) {
float ideal_raster_scale =
ideal_contents_scale / (device_scale_factor * page_scale_factor);
bool need_to_set_raster_scale = layer->raster_scale_is_unknown();
if (!need_to_set_raster_scale && layer->raster_scale() != 1.f &&
ideal_raster_scale != layer->raster_scale()) {
ideal_raster_scale = 1.f;
need_to_set_raster_scale = true;
}
if (need_to_set_raster_scale) {
bool use_and_save_ideal_scale =
ideal_raster_scale >= 1.f && !animating_transform_to_screen;
if (use_and_save_ideal_scale)
layer->set_raster_scale(ideal_raster_scale);
}
}
float raster_scale = 1.f;
if (!layer->raster_scale_is_unknown())
raster_scale = layer->raster_scale();
gfx::Size old_content_bounds = layer->content_bounds();
float old_contents_scale_x = layer->contents_scale_x();
float old_contents_scale_y = layer->contents_scale_y();
float contents_scale = raster_scale * device_scale_factor * page_scale_factor;
CalculateContentsScale(layer,
contents_scale,
device_scale_factor,
page_scale_factor,
animating_transform_to_screen);
if (layer->content_bounds() != old_content_bounds ||
layer->contents_scale_x() != old_contents_scale_x ||
layer->contents_scale_y() != old_contents_scale_y)
layer->SetNeedsPushProperties();
}
static inline RenderSurface* CreateOrReuseRenderSurface(Layer* layer) {
DCHECK(!layer->render_surface());
layer->CreateRenderSurface();
return layer->render_surface();
}
static inline RenderSurfaceImpl* CreateOrReuseRenderSurface(LayerImpl* layer) {
if (!layer->render_surface()) {
layer->CreateRenderSurface();
return layer->render_surface();
}
layer->render_surface()->ClearLayerLists();
return layer->render_surface();
}
template <typename LayerType>
static inline void RemoveSurfaceForEarlyExit(
LayerType* layer_to_remove,
typename LayerType::RenderSurfaceListType* render_surface_layer_list) {
DCHECK(layer_to_remove->render_surface());
while (render_surface_layer_list->back() != layer_to_remove) {
render_surface_layer_list->back()->ClearRenderSurface();
render_surface_layer_list->pop_back();
}
DCHECK_EQ(render_surface_layer_list->back(), layer_to_remove);
render_surface_layer_list->pop_back();
layer_to_remove->ClearRenderSurface();
}
struct PreCalculateMetaInformationRecursiveData {
bool layer_or_descendant_has_copy_request;
int num_unclipped_descendants;
PreCalculateMetaInformationRecursiveData()
: layer_or_descendant_has_copy_request(false),
num_unclipped_descendants(0) {}
void Merge(const PreCalculateMetaInformationRecursiveData& data) {
layer_or_descendant_has_copy_request |=
data.layer_or_descendant_has_copy_request;
num_unclipped_descendants +=
data.num_unclipped_descendants;
}
};
template <typename LayerType>
static void PreCalculateMetaInformation(
LayerType* layer,
PreCalculateMetaInformationRecursiveData* recursive_data) {
bool has_delegated_content = layer->HasDelegatedContent();
int num_descendants_that_draw_content = 0;
if (has_delegated_content) {
num_descendants_that_draw_content = 1000;
}
layer->draw_properties().sorted_for_recursion = false;
layer->draw_properties().has_child_with_a_scroll_parent = false;
if (layer->clip_parent())
recursive_data->num_unclipped_descendants++;
for (size_t i = 0; i < layer->children().size(); ++i) {
LayerType* child_layer =
LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i);
PreCalculateMetaInformationRecursiveData data_for_child;
PreCalculateMetaInformation(child_layer, &data_for_child);
num_descendants_that_draw_content += child_layer->DrawsContent() ? 1 : 0;
num_descendants_that_draw_content +=
child_layer->draw_properties().num_descendants_that_draw_content;
if (child_layer->scroll_parent())
layer->draw_properties().has_child_with_a_scroll_parent = true;
recursive_data->Merge(data_for_child);
}
if (layer->clip_children()) {
int num_clip_children = layer->clip_children()->size();
DCHECK_GE(recursive_data->num_unclipped_descendants, num_clip_children);
recursive_data->num_unclipped_descendants -= num_clip_children;
}
if (layer->HasCopyRequest())
recursive_data->layer_or_descendant_has_copy_request = true;
layer->draw_properties().num_descendants_that_draw_content =
num_descendants_that_draw_content;
layer->draw_properties().num_unclipped_descendants =
recursive_data->num_unclipped_descendants;
layer->draw_properties().layer_or_descendant_has_copy_request =
recursive_data->layer_or_descendant_has_copy_request;
}
static void RoundTranslationComponents(gfx::Transform* transform) {
transform->matrix().set(0, 3, MathUtil::Round(transform->matrix().get(0, 3)));
transform->matrix().set(1, 3, MathUtil::Round(transform->matrix().get(1, 3)));
}
template <typename LayerType>
struct SubtreeGlobals {
LayerSorter* layer_sorter;
int max_texture_size;
float device_scale_factor;
float page_scale_factor;
const LayerType* page_scale_application_layer;
bool can_adjust_raster_scales;
bool can_render_to_separate_surface;
};
template<typename LayerType>
struct DataForRecursion {
gfx::Transform parent_matrix;
gfx::Transform full_hierarchy_matrix;
gfx::Transform scroll_compensation_matrix;
LayerType* fixed_container;
gfx::Rect clip_rect_in_target_space;
gfx::Rect clip_rect_of_target_surface_in_target_space;
bool ancestor_clips_subtree;
typename LayerType::RenderSurfaceType*
nearest_occlusion_immune_ancestor_surface;
bool in_subtree_of_page_scale_application_layer;
bool subtree_can_use_lcd_text;
bool subtree_is_visible_from_ancestor;
};
template <typename LayerType>
static LayerType* GetChildContainingLayer(const LayerType& parent,
LayerType* layer) {
for (LayerType* ancestor = layer; ancestor; ancestor = ancestor->parent()) {
if (ancestor->parent() == &parent)
return ancestor;
}
NOTREACHED();
return 0;
}
template <typename LayerType>
static void AddScrollParentChain(std::vector<LayerType*>* out,
const LayerType& parent,
LayerType* layer) {
LayerType* child = GetChildContainingLayer(parent, layer);
if (child->draw_properties().sorted_for_recursion)
return;
if (LayerType* scroll_parent = child->scroll_parent())
AddScrollParentChain(out, parent, scroll_parent);
out->push_back(child);
child->draw_properties().sorted_for_recursion = true;
}
template <typename LayerType>
static bool SortChildrenForRecursion(std::vector<LayerType*>* out,
const LayerType& parent) {
out->reserve(parent.children().size());
bool order_changed = false;
for (size_t i = 0; i < parent.children().size(); ++i) {
LayerType* current =
LayerTreeHostCommon::get_child_as_raw_ptr(parent.children(), i);
if (current->draw_properties().sorted_for_recursion) {
order_changed = true;
continue;
}
AddScrollParentChain(out, parent, current);
}
DCHECK_EQ(parent.children().size(), out->size());
return order_changed;
}
template <typename LayerType>
static void GetNewDescendantsStartIndexAndCount(LayerType* layer,
size_t* start_index,
size_t* count) {
*start_index = layer->draw_properties().index_of_first_descendants_addition;
*count = layer->draw_properties().num_descendants_added;
}
template <typename LayerType>
static void GetNewRenderSurfacesStartIndexAndCount(LayerType* layer,
size_t* start_index,
size_t* count) {
*start_index = layer->draw_properties()
.index_of_first_render_surface_layer_list_addition;
*count = layer->draw_properties().num_render_surfaces_added;
}
template <typename LayerType,
typename GetIndexAndCountType>
static void SortLayerListContributions(
const LayerType& parent,
typename LayerType::RenderSurfaceListType* unsorted,
size_t start_index_for_all_contributions,
GetIndexAndCountType get_index_and_count) {
typename LayerType::LayerListType buffer;
for (size_t i = 0; i < parent.children().size(); ++i) {
LayerType* child =
LayerTreeHostCommon::get_child_as_raw_ptr(parent.children(), i);
size_t start_index = 0;
size_t count = 0;
get_index_and_count(child, &start_index, &count);
for (size_t j = start_index; j < start_index + count; ++j)
buffer.push_back(unsorted->at(j));
}
DCHECK_EQ(buffer.size(),
unsorted->size() - start_index_for_all_contributions);
for (size_t i = 0; i < buffer.size(); ++i)
(*unsorted)[i + start_index_for_all_contributions] = buffer[i];
}
template <typename LayerType>
static void CalculateDrawPropertiesInternal(
LayerType* layer,
const SubtreeGlobals<LayerType>& globals,
const DataForRecursion<LayerType>& data_from_ancestor,
typename LayerType::RenderSurfaceListType* render_surface_layer_list,
typename LayerType::RenderSurfaceListType* layer_list,
std::vector<AccumulatedSurfaceState<LayerType> >*
accumulated_surface_state) {
DCHECK(globals.page_scale_application_layer ||
(globals.page_scale_factor == 1.f));
DataForRecursion<LayerType> data_for_children;
typename LayerType::RenderSurfaceType*
nearest_occlusion_immune_ancestor_surface =
data_from_ancestor.nearest_occlusion_immune_ancestor_surface;
data_for_children.in_subtree_of_page_scale_application_layer =
data_from_ancestor.in_subtree_of_page_scale_application_layer;
data_for_children.subtree_can_use_lcd_text =
data_from_ancestor.subtree_can_use_lcd_text;
const bool layer_is_visible =
data_from_ancestor.subtree_is_visible_from_ancestor &&
!layer->hide_layer_and_subtree();
const bool layer_is_drawn = layer_is_visible || layer->HasCopyRequest();
if (!IsRootLayer(layer) && SubtreeShouldBeSkipped(layer, layer_is_drawn)) {
if (layer->render_surface())
layer->ClearRenderSurface();
return;
}
bool ancestor_clips_subtree = data_from_ancestor.ancestor_clips_subtree;
gfx::Rect ancestor_clip_rect_in_target_space =
data_from_ancestor.clip_rect_in_target_space;
UpdateClipRectsForClipChild<LayerType>(
layer, &ancestor_clip_rect_in_target_space, &ancestor_clips_subtree);
DrawProperties<LayerType>& layer_draw_properties = layer->draw_properties();
gfx::Rect clip_rect_in_target_space;
bool layer_or_ancestor_clips_descendants = false;
gfx::Rect clip_rect_of_target_surface_in_target_space;
float accumulated_draw_opacity = layer->opacity();
bool animating_opacity_to_target = layer->OpacityIsAnimating();
bool animating_opacity_to_screen = animating_opacity_to_target;
if (layer->parent()) {
accumulated_draw_opacity *= layer->parent()->draw_opacity();
animating_opacity_to_target |= layer->parent()->draw_opacity_is_animating();
animating_opacity_to_screen |=
layer->parent()->screen_space_opacity_is_animating();
}
bool animating_transform_to_target = layer->TransformIsAnimating();
bool animating_transform_to_screen = animating_transform_to_target;
if (layer->parent()) {
animating_transform_to_target |=
layer->parent()->draw_transform_is_animating();
animating_transform_to_screen |=
layer->parent()->screen_space_transform_is_animating();
}
gfx::Size bounds = layer->bounds();
gfx::PointF anchor_point = layer->anchor_point();
gfx::Vector2dF scroll_offset = GetEffectiveTotalScrollOffset(layer);
gfx::PointF position = layer->position() - scroll_offset;
gfx::Transform combined_transform = data_from_ancestor.parent_matrix;
if (!layer->transform().IsIdentity()) {
combined_transform.Translate3d(
position.x() + anchor_point.x() * bounds.width(),
position.y() + anchor_point.y() * bounds.height(),
layer->anchor_point_z());
combined_transform.PreconcatTransform(layer->transform());
combined_transform.Translate3d(-anchor_point.x() * bounds.width(),
-anchor_point.y() * bounds.height(),
-layer->anchor_point_z());
} else {
combined_transform.Translate(position.x(), position.y());
}
gfx::Vector2dF effective_scroll_delta = GetEffectiveScrollDelta(layer);
if (!animating_transform_to_target && layer->scrollable() &&
combined_transform.IsScaleOrTranslation()) {
gfx::Vector2dF previous_translation = combined_transform.To2dTranslation();
RoundTranslationComponents(&combined_transform);
gfx::Vector2dF current_translation = combined_transform.To2dTranslation();
gfx::Vector2dF parent_scales = MathUtil::ComputeTransform2dScaleComponents(
data_from_ancestor.parent_matrix, 1.f);
effective_scroll_delta -=
gfx::ScaleVector2d(current_translation - previous_translation,
1.f / parent_scales.x(),
1.f / parent_scales.y());
}
ApplyPositionAdjustment(layer, data_from_ancestor.fixed_container,
data_from_ancestor.scroll_compensation_matrix, &combined_transform);
float layer_scale_factors = globals.device_scale_factor;
if (data_from_ancestor.in_subtree_of_page_scale_application_layer)
layer_scale_factors *= globals.page_scale_factor;
gfx::Vector2dF combined_transform_scales =
MathUtil::ComputeTransform2dScaleComponents(
combined_transform,
layer_scale_factors);
float ideal_contents_scale =
globals.can_adjust_raster_scales
? std::max(combined_transform_scales.x(),
combined_transform_scales.y())
: layer_scale_factors;
UpdateLayerContentsScale(
layer,
globals.can_adjust_raster_scales,
ideal_contents_scale,
globals.device_scale_factor,
data_from_ancestor.in_subtree_of_page_scale_application_layer ?
globals.page_scale_factor : 1.f,
animating_transform_to_screen);
layer_draw_properties.target_space_transform = combined_transform;
layer_draw_properties.target_space_transform.Scale(
SK_MScalar1 / layer->contents_scale_x(),
SK_MScalar1 / layer->contents_scale_y());
layer_draw_properties.screen_space_transform =
data_from_ancestor.full_hierarchy_matrix;
if (layer->should_flatten_transform())
layer_draw_properties.screen_space_transform.FlattenTo2d();
layer_draw_properties.screen_space_transform.PreconcatTransform
(layer_draw_properties.target_space_transform);
bool adjust_text_aa =
!animating_opacity_to_screen && !animating_transform_to_screen;
bool layer_can_use_lcd_text =
data_from_ancestor.subtree_can_use_lcd_text &&
accumulated_draw_opacity == 1.f &&
layer_draw_properties.target_space_transform.
IsIdentityOrIntegerTranslation();
gfx::RectF content_rect(layer->content_bounds());
data_for_children.full_hierarchy_matrix =
data_from_ancestor.full_hierarchy_matrix;
gfx::Vector2dF render_surface_sublayer_scale =
globals.can_adjust_raster_scales
? combined_transform_scales
: gfx::Vector2dF(layer_scale_factors, layer_scale_factors);
bool render_to_separate_surface;
if (globals.can_render_to_separate_surface) {
render_to_separate_surface = SubtreeShouldRenderToSeparateSurface(
layer, combined_transform.Preserves2dAxisAlignment());
} else {
render_to_separate_surface = IsRootLayer(layer);
}
if (render_to_separate_surface) {
if (!layer->double_sided() && TransformToParentIsKnown(layer) &&
IsSurfaceBackFaceVisible(layer, combined_transform)) {
layer->ClearRenderSurface();
return;
}
typename LayerType::RenderSurfaceType* render_surface =
CreateOrReuseRenderSurface(layer);
if (IsRootLayer(layer)) {
data_for_children.parent_matrix = combined_transform;
layer->render_surface()->set_contributes_to_drawn_surface(false);
} else {
combined_transform.Scale(1.0 / render_surface_sublayer_scale.x(),
1.0 / render_surface_sublayer_scale.y());
render_surface->SetDrawTransform(combined_transform);
layer_draw_properties.target_space_transform.MakeIdentity();
layer_draw_properties.target_space_transform.
Scale(render_surface_sublayer_scale.x() / layer->contents_scale_x(),
render_surface_sublayer_scale.y() / layer->contents_scale_y());
DCHECK(data_for_children.parent_matrix.IsIdentity());
data_for_children.parent_matrix.Scale(render_surface_sublayer_scale.x(),
render_surface_sublayer_scale.y());
layer->render_surface()->set_contributes_to_drawn_surface(
layer_is_visible);
}
render_surface->SetDrawOpacity(accumulated_draw_opacity);
render_surface->SetDrawOpacityIsAnimating(animating_opacity_to_target);
animating_opacity_to_target = false;
layer_draw_properties.opacity = 1.f;
layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
layer_draw_properties.screen_space_opacity_is_animating =
animating_opacity_to_screen;
render_surface->SetTargetSurfaceTransformsAreAnimating(
animating_transform_to_target);
render_surface->SetScreenSpaceTransformsAreAnimating(
animating_transform_to_screen);
animating_transform_to_target = false;
layer_draw_properties.target_space_transform_is_animating =
animating_transform_to_target;
layer_draw_properties.screen_space_transform_is_animating =
animating_transform_to_screen;
data_for_children.full_hierarchy_matrix.PreconcatTransform(
render_surface->draw_transform());
if (layer->mask_layer()) {
DrawProperties<LayerType>& mask_layer_draw_properties =
layer->mask_layer()->draw_properties();
mask_layer_draw_properties.render_target = layer;
mask_layer_draw_properties.visible_content_rect =
gfx::Rect(layer->content_bounds());
}
if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
DrawProperties<LayerType>& replica_mask_draw_properties =
layer->replica_layer()->mask_layer()->draw_properties();
replica_mask_draw_properties.render_target = layer;
replica_mask_draw_properties.visible_content_rect =
gfx::Rect(layer->content_bounds());
}
if (layer->HasCopyRequest() ||
layer->has_replica() ||
layer->filters().HasReferenceFilter() ||
layer->filters().HasFilterThatMovesPixels()) {
nearest_occlusion_immune_ancestor_surface = render_surface;
}
render_surface->SetNearestOcclusionImmuneAncestor(
nearest_occlusion_immune_ancestor_surface);
layer_or_ancestor_clips_descendants = false;
bool subtree_is_clipped_by_surface_bounds = false;
if (ancestor_clips_subtree) {
gfx::Transform inverse_surface_draw_transform(
gfx::Transform::kSkipInitialization);
if (!render_surface->draw_transform().GetInverse(
&inverse_surface_draw_transform)) {
}
gfx::Rect projected_surface_rect = MathUtil::ProjectEnclosingClippedRect(
inverse_surface_draw_transform, ancestor_clip_rect_in_target_space);
if (layer_draw_properties.num_unclipped_descendants > 0) {
layer_or_ancestor_clips_descendants = true;
clip_rect_in_target_space = projected_surface_rect;
} else {
render_surface->SetClipRect(ancestor_clip_rect_in_target_space);
clip_rect_of_target_surface_in_target_space = projected_surface_rect;
subtree_is_clipped_by_surface_bounds = true;
}
}
DCHECK(layer->render_surface());
DCHECK(!layer->parent() || layer->parent()->render_target() ==
accumulated_surface_state->back().render_target);
accumulated_surface_state->push_back(
AccumulatedSurfaceState<LayerType>(layer));
render_surface->SetIsClipped(subtree_is_clipped_by_surface_bounds);
if (!subtree_is_clipped_by_surface_bounds) {
render_surface->SetClipRect(gfx::Rect());
clip_rect_of_target_surface_in_target_space =
data_from_ancestor.clip_rect_of_target_surface_in_target_space;
}
data_for_children.subtree_can_use_lcd_text = layer_can_use_lcd_text;
render_surface_layer_list->push_back(layer);
} else {
DCHECK(layer->parent());
layer_draw_properties.target_space_transform_is_animating =
animating_transform_to_target;
layer_draw_properties.screen_space_transform_is_animating =
animating_transform_to_screen;
layer_draw_properties.opacity = accumulated_draw_opacity;
layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
layer_draw_properties.screen_space_opacity_is_animating =
animating_opacity_to_screen;
data_for_children.parent_matrix = combined_transform;
layer->ClearRenderSurface();
layer_or_ancestor_clips_descendants = ancestor_clips_subtree;
if (ancestor_clips_subtree) {
clip_rect_in_target_space =
ancestor_clip_rect_in_target_space;
}
clip_rect_of_target_surface_in_target_space =
data_from_ancestor.clip_rect_of_target_surface_in_target_space;
layer_draw_properties.render_target = layer->parent()->render_target();
}
if (adjust_text_aa)
layer_draw_properties.can_use_lcd_text = layer_can_use_lcd_text;
gfx::Rect rect_in_target_space = ToEnclosingRect(
MathUtil::MapClippedRect(layer->draw_transform(), content_rect));
if (LayerClipsSubtree(layer)) {
layer_or_ancestor_clips_descendants = true;
if (ancestor_clips_subtree && !layer->render_surface()) {
clip_rect_in_target_space =
ancestor_clip_rect_in_target_space;
clip_rect_in_target_space.Intersect(rect_in_target_space);
} else {
clip_rect_in_target_space = rect_in_target_space;
}
}
layer_draw_properties.is_clipped = layer_or_ancestor_clips_descendants;
if (layer_or_ancestor_clips_descendants) {
layer_draw_properties.clip_rect = clip_rect_in_target_space;
} else {
layer_draw_properties.clip_rect = rect_in_target_space;
}
typename LayerType::RenderSurfaceListType& descendants =
(layer->render_surface() ? layer->render_surface()->layer_list()
: *layer_list);
size_t sorting_start_index = descendants.size();
if (!LayerShouldBeSkipped(layer, layer_is_drawn))
descendants.push_back(layer);
size_t render_surface_layer_list_child_sorting_start_index =
render_surface_layer_list->size();
size_t layer_list_child_sorting_start_index = descendants.size();
if (!layer->children().empty()) {
if (layer == globals.page_scale_application_layer) {
data_for_children.parent_matrix.Scale(
globals.page_scale_factor,
globals.page_scale_factor);
data_for_children.in_subtree_of_page_scale_application_layer = true;
}
if (layer->should_flatten_transform())
data_for_children.parent_matrix.FlattenTo2d();
data_for_children.scroll_compensation_matrix =
ComputeScrollCompensationMatrixForChildren(
layer,
data_from_ancestor.parent_matrix,
data_from_ancestor.scroll_compensation_matrix,
effective_scroll_delta);
data_for_children.fixed_container =
layer->IsContainerForFixedPositionLayers() ?
layer : data_from_ancestor.fixed_container;
data_for_children.clip_rect_in_target_space = clip_rect_in_target_space;
data_for_children.clip_rect_of_target_surface_in_target_space =
clip_rect_of_target_surface_in_target_space;
data_for_children.ancestor_clips_subtree =
layer_or_ancestor_clips_descendants;
data_for_children.nearest_occlusion_immune_ancestor_surface =
nearest_occlusion_immune_ancestor_surface;
data_for_children.subtree_is_visible_from_ancestor = layer_is_drawn;
}
std::vector<LayerType*> sorted_children;
bool child_order_changed = false;
if (layer_draw_properties.has_child_with_a_scroll_parent)
child_order_changed = SortChildrenForRecursion(&sorted_children, *layer);
for (size_t i = 0; i < layer->children().size(); ++i) {
LayerType* child =
layer_draw_properties.has_child_with_a_scroll_parent
? sorted_children[i]
: LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i);
child->draw_properties().index_of_first_descendants_addition =
descendants.size();
child->draw_properties().index_of_first_render_surface_layer_list_addition =
render_surface_layer_list->size();
CalculateDrawPropertiesInternal<LayerType>(child,
globals,
data_for_children,
render_surface_layer_list,
&descendants,
accumulated_surface_state);
if (child->render_surface() &&
!child->render_surface()->content_rect().IsEmpty()) {
descendants.push_back(child);
}
child->draw_properties().num_descendants_added =
descendants.size() -
child->draw_properties().index_of_first_descendants_addition;
child->draw_properties().num_render_surfaces_added =
render_surface_layer_list->size() -
child->draw_properties()
.index_of_first_render_surface_layer_list_addition;
}
if (child_order_changed) {
SortLayerListContributions(
*layer,
render_surface_layer_list,
render_surface_layer_list_child_sorting_start_index,
&GetNewRenderSurfacesStartIndexAndCount<LayerType>);
SortLayerListContributions(
*layer,
&descendants,
layer_list_child_sorting_start_index,
&GetNewDescendantsStartIndexAndCount<LayerType>);
}
gfx::Rect local_drawable_content_rect_of_subtree =
accumulated_surface_state->back().drawable_content_rect;
if (layer->render_surface()) {
DCHECK(accumulated_surface_state->back().render_target == layer);
accumulated_surface_state->pop_back();
}
if (layer->render_surface() && !IsRootLayer(layer) &&
layer->render_surface()->layer_list().empty()) {
RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
return;
}
layer_draw_properties.drawable_content_rect = rect_in_target_space;
if (layer_or_ancestor_clips_descendants) {
layer_draw_properties.drawable_content_rect.Intersect(
clip_rect_in_target_space);
}
if (layer->DrawsContent()) {
local_drawable_content_rect_of_subtree.Union(
layer_draw_properties.drawable_content_rect);
}
layer_draw_properties.visible_content_rect = CalculateVisibleContentRect(
layer, clip_rect_of_target_surface_in_target_space, rect_in_target_space);
if (IsRootLayer(layer)) {
DCHECK(layer->render_surface());
layer->render_surface()->SetContentRect(
ancestor_clip_rect_in_target_space);
} else if (layer->render_surface()) {
typename LayerType::RenderSurfaceType* render_surface =
layer->render_surface();
gfx::Rect clipped_content_rect = local_drawable_content_rect_of_subtree;
if (!layer->replica_layer() && TransformToParentIsKnown(layer)) {
if (render_surface->is_clipped() && !clipped_content_rect.IsEmpty()) {
gfx::Rect surface_clip_rect = LayerTreeHostCommon::CalculateVisibleRect(
render_surface->clip_rect(),
clipped_content_rect,
render_surface->draw_transform());
clipped_content_rect.Intersect(surface_clip_rect);
}
}
clipped_content_rect.set_width(
std::min(clipped_content_rect.width(), globals.max_texture_size));
clipped_content_rect.set_height(
std::min(clipped_content_rect.height(), globals.max_texture_size));
if (clipped_content_rect.IsEmpty()) {
RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
return;
}
DCHECK(layer->uses_default_blend_mode() || IsRootLayer(layer) ||
!layer->parent()->render_target() ||
IsRootLayer(layer->parent()->render_target()) ||
layer->parent()->render_target()->is_root_for_isolated_group());
render_surface->SetContentRect(clipped_content_rect);
gfx::Transform screen_space_transform = layer->screen_space_transform();
screen_space_transform.Scale(
layer->contents_scale_x() / render_surface_sublayer_scale.x(),
layer->contents_scale_y() / render_surface_sublayer_scale.y());
render_surface->SetScreenSpaceTransform(screen_space_transform);
if (layer->replica_layer()) {
gfx::Transform surface_origin_to_replica_origin_transform;
surface_origin_to_replica_origin_transform.Scale(
render_surface_sublayer_scale.x(), render_surface_sublayer_scale.y());
surface_origin_to_replica_origin_transform.Translate(
layer->replica_layer()->position().x() +
layer->replica_layer()->anchor_point().x() * bounds.width(),
layer->replica_layer()->position().y() +
layer->replica_layer()->anchor_point().y() * bounds.height());
surface_origin_to_replica_origin_transform.PreconcatTransform(
layer->replica_layer()->transform());
surface_origin_to_replica_origin_transform.Translate(
-layer->replica_layer()->anchor_point().x() * bounds.width(),
-layer->replica_layer()->anchor_point().y() * bounds.height());
surface_origin_to_replica_origin_transform.Scale(
1.0 / render_surface_sublayer_scale.x(),
1.0 / render_surface_sublayer_scale.y());
gfx::Transform replica_origin_transform =
layer->render_surface()->draw_transform() *
surface_origin_to_replica_origin_transform;
render_surface->SetReplicaDrawTransform(replica_origin_transform);
gfx::Transform replica_screen_space_transform =
layer->render_surface()->screen_space_transform() *
surface_origin_to_replica_origin_transform;
render_surface->SetReplicaScreenSpaceTransform(
replica_screen_space_transform);
}
}
SavePaintPropertiesLayer(layer);
if (sorting_start_index == descendants.size()) {
DCHECK(!layer->render_surface() || IsRootLayer(layer));
return;
}
if (globals.layer_sorter && descendants.size() && layer->is_3d_sorted() &&
!LayerIsInExisting3DRenderingContext(layer)) {
SortLayers(descendants.begin() + sorting_start_index,
descendants.end(),
globals.layer_sorter);
}
UpdateAccumulatedSurfaceState<LayerType>(
layer, local_drawable_content_rect_of_subtree, accumulated_surface_state);
if (layer->HasContributingDelegatedRenderPasses()) {
layer->render_target()->render_surface()->
AddContributingDelegatedRenderPassLayer(layer);
}
}
void LayerTreeHostCommon::CalculateDrawProperties(
CalcDrawPropsMainInputs* inputs) {
DCHECK(inputs->root_layer);
DCHECK(IsRootLayer(inputs->root_layer));
DCHECK(inputs->render_surface_layer_list);
gfx::Transform identity_matrix;
gfx::Transform scaled_device_transform = inputs->device_transform;
scaled_device_transform.Scale(inputs->device_scale_factor,
inputs->device_scale_factor);
RenderSurfaceLayerList dummy_layer_list;
gfx::Rect device_viewport_rect(inputs->device_viewport_size);
SubtreeGlobals<Layer> globals;
globals.layer_sorter = NULL;
globals.max_texture_size = inputs->max_texture_size;
globals.device_scale_factor = inputs->device_scale_factor;
globals.page_scale_factor = inputs->page_scale_factor;
globals.page_scale_application_layer = inputs->page_scale_application_layer;
globals.can_render_to_separate_surface =
inputs->can_render_to_separate_surface;
globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales;
DataForRecursion<Layer> data_for_recursion;
data_for_recursion.parent_matrix = scaled_device_transform;
data_for_recursion.full_hierarchy_matrix = identity_matrix;
data_for_recursion.scroll_compensation_matrix = identity_matrix;
data_for_recursion.fixed_container = inputs->root_layer;
data_for_recursion.clip_rect_in_target_space = device_viewport_rect;
data_for_recursion.clip_rect_of_target_surface_in_target_space =
device_viewport_rect;
data_for_recursion.ancestor_clips_subtree = true;
data_for_recursion.nearest_occlusion_immune_ancestor_surface = NULL;
data_for_recursion.in_subtree_of_page_scale_application_layer = false;
data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text;
data_for_recursion.subtree_is_visible_from_ancestor = true;
PreCalculateMetaInformationRecursiveData recursive_data;
PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
std::vector<AccumulatedSurfaceState<Layer> > accumulated_surface_state;
CalculateDrawPropertiesInternal<Layer>(inputs->root_layer,
globals,
data_for_recursion,
inputs->render_surface_layer_list,
&dummy_layer_list,
&accumulated_surface_state);
DCHECK_EQ(0u, dummy_layer_list.size());
DCHECK(inputs->root_layer->render_surface());
}
void LayerTreeHostCommon::CalculateDrawProperties(
CalcDrawPropsImplInputs* inputs) {
DCHECK(inputs->root_layer);
DCHECK(IsRootLayer(inputs->root_layer));
DCHECK(inputs->render_surface_layer_list);
gfx::Transform identity_matrix;
gfx::Transform scaled_device_transform = inputs->device_transform;
scaled_device_transform.Scale(inputs->device_scale_factor,
inputs->device_scale_factor);
LayerImplList dummy_layer_list;
LayerSorter layer_sorter;
gfx::Rect device_viewport_rect(inputs->device_viewport_size);
SubtreeGlobals<LayerImpl> globals;
globals.layer_sorter = &layer_sorter;
globals.max_texture_size = inputs->max_texture_size;
globals.device_scale_factor = inputs->device_scale_factor;
globals.page_scale_factor = inputs->page_scale_factor;
globals.page_scale_application_layer = inputs->page_scale_application_layer;
globals.can_render_to_separate_surface =
inputs->can_render_to_separate_surface;
globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales;
DataForRecursion<LayerImpl> data_for_recursion;
data_for_recursion.parent_matrix = scaled_device_transform;
data_for_recursion.full_hierarchy_matrix = identity_matrix;
data_for_recursion.scroll_compensation_matrix = identity_matrix;
data_for_recursion.fixed_container = inputs->root_layer;
data_for_recursion.clip_rect_in_target_space = device_viewport_rect;
data_for_recursion.clip_rect_of_target_surface_in_target_space =
device_viewport_rect;
data_for_recursion.ancestor_clips_subtree = true;
data_for_recursion.nearest_occlusion_immune_ancestor_surface = NULL;
data_for_recursion.in_subtree_of_page_scale_application_layer = false;
data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text;
data_for_recursion.subtree_is_visible_from_ancestor = true;
PreCalculateMetaInformationRecursiveData recursive_data;
PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
std::vector<AccumulatedSurfaceState<LayerImpl> >
accumulated_surface_state;
CalculateDrawPropertiesInternal<LayerImpl>(inputs->root_layer,
globals,
data_for_recursion,
inputs->render_surface_layer_list,
&dummy_layer_list,
&accumulated_surface_state);
DCHECK_EQ(0u, dummy_layer_list.size());
DCHECK(inputs->root_layer->render_surface());
}
static bool PointHitsRect(
const gfx::PointF& screen_space_point,
const gfx::Transform& local_space_to_screen_space_transform,
const gfx::RectF& local_space_rect) {
gfx::Transform inverse_local_space_to_screen_space(
gfx::Transform::kSkipInitialization);
if (!local_space_to_screen_space_transform.GetInverse(
&inverse_local_space_to_screen_space))
return false;
bool clipped = false;
gfx::PointF hit_test_point_in_local_space = MathUtil::ProjectPoint(
inverse_local_space_to_screen_space, screen_space_point, &clipped);
if (clipped)
return false;
return local_space_rect.Contains(hit_test_point_in_local_space);
}
static bool PointHitsRegion(const gfx::PointF& screen_space_point,
const gfx::Transform& screen_space_transform,
const Region& layer_space_region,
float layer_content_scale_x,
float layer_content_scale_y) {
gfx::Transform inverse_screen_space_transform(
gfx::Transform::kSkipInitialization);
if (!screen_space_transform.GetInverse(&inverse_screen_space_transform))
return false;
bool clipped = false;
gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint(
inverse_screen_space_transform, screen_space_point, &clipped);
gfx::PointF hit_test_point_in_layer_space =
gfx::ScalePoint(hit_test_point_in_content_space,
1.f / layer_content_scale_x,
1.f / layer_content_scale_y);
if (clipped)
return false;
return layer_space_region.Contains(
gfx::ToRoundedPoint(hit_test_point_in_layer_space));
}
static bool PointIsClippedBySurfaceOrClipRect(
const gfx::PointF& screen_space_point,
LayerImpl* layer) {
LayerImpl* current_layer = layer;
while (current_layer) {
if (current_layer->render_surface() &&
!PointHitsRect(
screen_space_point,
current_layer->render_surface()->screen_space_transform(),
current_layer->render_surface()->content_rect()))
return true;
LayerImpl* render_target = current_layer->render_target();
if (LayerClipsSubtree(current_layer) &&
!PointHitsRect(
screen_space_point,
render_target->render_surface()->screen_space_transform(),
current_layer->drawable_content_rect()))
return true;
current_layer = current_layer->parent();
}
return false;
}
LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPoint(
const gfx::PointF& screen_space_point,
const LayerImplList& render_surface_layer_list) {
LayerImpl* found_layer = NULL;
typedef LayerIterator<LayerImpl> LayerIteratorType;
LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list);
for (LayerIteratorType
it = LayerIteratorType::Begin(&render_surface_layer_list);
it != end;
++it) {
if (!it.represents_itself())
continue;
LayerImpl* current_layer = (*it);
gfx::RectF content_rect(current_layer->content_bounds());
if (!PointHitsRect(screen_space_point,
current_layer->screen_space_transform(),
content_rect))
continue;
if (PointIsClippedBySurfaceOrClipRect(screen_space_point, current_layer))
continue;
if (current_layer == current_layer->layer_tree_impl()->hud_layer())
continue;
found_layer = current_layer;
break;
}
return found_layer;
}
LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
const gfx::PointF& screen_space_point,
const LayerImplList& render_surface_layer_list) {
LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
screen_space_point,
render_surface_layer_list);
for (; layer_impl; layer_impl = layer_impl->parent()) {
if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(screen_space_point,
layer_impl))
break;
}
return layer_impl;
}
bool LayerTreeHostCommon::LayerHasTouchEventHandlersAt(
const gfx::PointF& screen_space_point,
LayerImpl* layer_impl) {
if (layer_impl->touch_event_handler_region().IsEmpty())
return false;
if (!PointHitsRegion(screen_space_point,
layer_impl->screen_space_transform(),
layer_impl->touch_event_handler_region(),
layer_impl->contents_scale_x(),
layer_impl->contents_scale_y()))
return false;
if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl))
return false;
return true;
}
}