This source file includes following definitions.
- PerpProduct
- EdgeEdgeTest
- incoming_edge_weight
- CheckFloatingPointNumericAccuracy
- CheckOverlap
- LayerZFromProjectedPoint
- CreateGraphNodes
- CreateGraphEdges
- RemoveEdgeFromList
#include "cc/trees/layer_sorter.h"
#include <algorithm>
#include <deque>
#include <limits>
#include <vector>
#include "base/logging.h"
#include "cc/base/math_util.h"
#include "cc/layers/render_surface_impl.h"
#include "ui/gfx/transform.h"
namespace cc {
const float k_layer_epsilon = 1e-4f;
inline static float PerpProduct(const gfx::Vector2dF& u,
const gfx::Vector2dF& v) {
return u.x() * v.y() - u.y() * v.x();
}
static bool EdgeEdgeTest(const gfx::PointF& a,
const gfx::PointF& b,
const gfx::PointF& c,
const gfx::PointF& d,
gfx::PointF* r) {
gfx::Vector2dF u = b - a;
gfx::Vector2dF v = d - c;
gfx::Vector2dF w = a - c;
float denom = PerpProduct(u, v);
if (!denom)
return false;
float s = PerpProduct(v, w) / denom;
if (s < 0.f || s > 1.f)
return false;
float t = PerpProduct(u, w) / denom;
if (t < 0.f || t > 1.f)
return false;
u.Scale(s);
*r = a + u;
return true;
}
GraphNode::GraphNode(LayerImpl* layer_impl)
: layer(layer_impl),
incoming_edge_weight(0.f) {}
GraphNode::~GraphNode() {}
LayerSorter::LayerSorter()
: z_range_(0.f) {}
LayerSorter::~LayerSorter() {}
static float CheckFloatingPointNumericAccuracy(float a, float b) {
float abs_dif = std::abs(b - a);
float abs_max = std::max(std::abs(b), std::abs(a));
return abs_dif / abs_max;
}
LayerSorter::ABCompareResult LayerSorter::CheckOverlap(LayerShape* a,
LayerShape* b,
float z_threshold,
float* weight) {
*weight = 0.f;
if (!a->projected_bounds.Intersects(b->projected_bounds))
return None;
gfx::PointF aPoints[4] = { a->projected_quad.p1(),
a->projected_quad.p2(),
a->projected_quad.p3(),
a->projected_quad.p4() };
gfx::PointF bPoints[4] = { b->projected_quad.p1(),
b->projected_quad.p2(),
b->projected_quad.p3(),
b->projected_quad.p4() };
std::vector<gfx::PointF> overlap_points;
for (int i = 0; i < 4; ++i) {
if (a->projected_quad.Contains(bPoints[i]))
overlap_points.push_back(bPoints[i]);
if (b->projected_quad.Contains(aPoints[i]))
overlap_points.push_back(aPoints[i]);
}
gfx::PointF r;
for (int ea = 0; ea < 4; ++ea)
for (int eb = 0; eb < 4; ++eb)
if (EdgeEdgeTest(aPoints[ea], aPoints[(ea + 1) % 4],
bPoints[eb], bPoints[(eb + 1) % 4],
&r))
overlap_points.push_back(r);
if (overlap_points.empty())
return None;
float max_positive = 0.f;
float max_negative = 0.f;
bool accurate = false;
for (size_t o = 0; o < overlap_points.size(); o++) {
float za = a->LayerZFromProjectedPoint(overlap_points[o]);
float zb = b->LayerZFromProjectedPoint(overlap_points[o]);
if (CheckFloatingPointNumericAccuracy(za, zb) > k_layer_epsilon)
accurate = true;
float diff = za - zb;
if (diff > max_positive)
max_positive = diff;
if (diff < max_negative)
max_negative = diff;
}
if (!accurate)
return ABeforeB;
float max_diff =
std::abs(max_positive) > std::abs(max_negative) ?
max_positive : max_negative;
if (max_positive > z_threshold && max_negative < -z_threshold)
*weight = 0.f;
else
*weight = std::abs(max_diff);
if (max_diff <= 0.f)
return ABeforeB;
return BBeforeA;
}
LayerShape::LayerShape() {}
LayerShape::LayerShape(float width,
float height,
const gfx::Transform& draw_transform) {
gfx::QuadF layer_quad(gfx::RectF(0.f, 0.f, width, height));
gfx::PointF clipped_quad[8];
int num_vertices_in_clipped_quad;
MathUtil::MapClippedQuad(draw_transform,
layer_quad,
clipped_quad,
&num_vertices_in_clipped_quad);
if (num_vertices_in_clipped_quad < 3) {
projected_bounds = gfx::RectF();
return;
}
projected_bounds =
MathUtil::ComputeEnclosingRectOfVertices(clipped_quad,
num_vertices_in_clipped_quad);
projected_quad.set_p1(clipped_quad[0]);
projected_quad.set_p2(clipped_quad[1]);
projected_quad.set_p3(clipped_quad[2]);
if (num_vertices_in_clipped_quad >= 4) {
projected_quad.set_p4(clipped_quad[3]);
} else {
projected_quad.set_p4(clipped_quad[2]);
}
bool clipped = false;
gfx::Point3F c1 =
MathUtil::MapPoint(draw_transform, gfx::Point3F(0.f, 0.f, 0.f), &clipped);
gfx::Point3F c2 =
MathUtil::MapPoint(draw_transform, gfx::Point3F(0.f, 1.f, 0.f), &clipped);
gfx::Point3F c3 =
MathUtil::MapPoint(draw_transform, gfx::Point3F(1.f, 0.f, 0.f), &clipped);
gfx::Vector3dF c12 = c2 - c1;
gfx::Vector3dF c13 = c3 - c1;
layer_normal = gfx::CrossProduct(c13, c12);
transform_origin = c1;
}
LayerShape::~LayerShape() {}
float LayerShape::LayerZFromProjectedPoint(const gfx::PointF& p) const {
gfx::Vector3dF z_axis(0.f, 0.f, 1.f);
gfx::Vector3dF w = gfx::Point3F(p) - transform_origin;
float d = gfx::DotProduct(layer_normal, z_axis);
float n = -gfx::DotProduct(layer_normal, w);
if (!d)
return 0.f;
return n / d;
}
void LayerSorter::CreateGraphNodes(LayerImplList::iterator first,
LayerImplList::iterator last) {
DVLOG(2) << "Creating graph nodes:";
float min_z = FLT_MAX;
float max_z = -FLT_MAX;
for (LayerImplList::const_iterator it = first; it < last; it++) {
nodes_.push_back(GraphNode(*it));
GraphNode& node = nodes_.at(nodes_.size() - 1);
RenderSurfaceImpl* render_surface = node.layer->render_surface();
if (!node.layer->DrawsContent() && !render_surface)
continue;
DVLOG(2) << "Layer " << node.layer->id() <<
" (" << node.layer->bounds().width() <<
" x " << node.layer->bounds().height() << ")";
gfx::Transform draw_transform;
float layer_width, layer_height;
if (render_surface) {
draw_transform = render_surface->draw_transform();
layer_width = render_surface->content_rect().width();
layer_height = render_surface->content_rect().height();
} else {
draw_transform = node.layer->draw_transform();
layer_width = node.layer->content_bounds().width();
layer_height = node.layer->content_bounds().height();
}
node.shape = LayerShape(layer_width, layer_height, draw_transform);
max_z = std::max(max_z, node.shape.transform_origin.z());
min_z = std::min(min_z, node.shape.transform_origin.z());
}
z_range_ = std::abs(max_z - min_z);
}
void LayerSorter::CreateGraphEdges() {
DVLOG(2) << "Edges:";
const float z_threshold_factor = 0.01f;
float z_threshold = z_range_ * z_threshold_factor;
for (size_t na = 0; na < nodes_.size(); na++) {
GraphNode& node_a = nodes_[na];
if (!node_a.layer->DrawsContent() && !node_a.layer->render_surface())
continue;
for (size_t nb = na + 1; nb < nodes_.size(); nb++) {
GraphNode& node_b = nodes_[nb];
if (!node_b.layer->DrawsContent() && !node_b.layer->render_surface())
continue;
float weight = 0.f;
ABCompareResult overlap_result = CheckOverlap(&node_a.shape,
&node_b.shape,
z_threshold,
&weight);
GraphNode* start_node = NULL;
GraphNode* end_node = NULL;
if (overlap_result == ABeforeB) {
start_node = &node_a;
end_node = &node_b;
} else if (overlap_result == BBeforeA) {
start_node = &node_b;
end_node = &node_a;
}
if (start_node) {
DVLOG(2) << start_node->layer->id() << " -> " << end_node->layer->id();
edges_.push_back(GraphEdge(start_node, end_node, weight));
}
}
}
for (size_t i = 0; i < edges_.size(); i++) {
GraphEdge& edge = edges_[i];
active_edges_[&edge] = &edge;
edge.from->outgoing.push_back(&edge);
edge.to->incoming.push_back(&edge);
edge.to->incoming_edge_weight += edge.weight;
}
}
void LayerSorter::RemoveEdgeFromList(GraphEdge* edge,
std::vector<GraphEdge*>* list) {
std::vector<GraphEdge*>::iterator iter =
std::find(list->begin(), list->end(), edge);
DCHECK(iter != list->end());
list->erase(iter);
}
void LayerSorter::Sort(LayerImplList::iterator first,
LayerImplList::iterator last) {
DVLOG(2) << "Sorting start ----";
CreateGraphNodes(first, last);
CreateGraphEdges();
std::vector<GraphNode*> sorted_list;
std::deque<GraphNode*> no_incoming_edge_node_list;
for (NodeList::iterator la = nodes_.begin(); la < nodes_.end(); la++) {
if (!la->incoming.size())
no_incoming_edge_node_list.push_back(&(*la));
}
DVLOG(2) << "Sorted list: ";
while (active_edges_.size() || no_incoming_edge_node_list.size()) {
while (no_incoming_edge_node_list.size()) {
GraphNode* from_node = no_incoming_edge_node_list.front();
no_incoming_edge_node_list.pop_front();
sorted_list.push_back(from_node);
DVLOG(2) << from_node->layer->id() << ", ";
for (size_t i = 0; i < from_node->outgoing.size(); i++) {
GraphEdge* outgoing_edge = from_node->outgoing[i];
active_edges_.erase(outgoing_edge);
RemoveEdgeFromList(outgoing_edge, &outgoing_edge->to->incoming);
outgoing_edge->to->incoming_edge_weight -= outgoing_edge->weight;
if (!outgoing_edge->to->incoming.size())
no_incoming_edge_node_list.push_back(outgoing_edge->to);
}
from_node->outgoing.clear();
}
if (!active_edges_.size())
break;
float min_incoming_edge_weight = FLT_MAX;
GraphNode* next_node = NULL;
for (size_t i = 0; i < nodes_.size(); i++) {
if (nodes_[i].incoming.size() &&
nodes_[i].incoming_edge_weight < min_incoming_edge_weight) {
min_incoming_edge_weight = nodes_[i].incoming_edge_weight;
next_node = &nodes_[i];
}
}
DCHECK(next_node);
for (size_t e = 0; e < next_node->incoming.size(); e++) {
GraphEdge* incoming_edge = next_node->incoming[e];
active_edges_.erase(incoming_edge);
RemoveEdgeFromList(incoming_edge, &incoming_edge->from->outgoing);
}
next_node->incoming.clear();
next_node->incoming_edge_weight = 0.f;
no_incoming_edge_node_list.push_back(next_node);
DVLOG(2) << "Breaking cycle by cleaning up incoming edges from " <<
next_node->layer->id() <<
" (weight = " << min_incoming_edge_weight << ")";
}
int count = 0;
for (LayerImplList::iterator it = first; it < last; it++)
*it = sorted_list[count++]->layer;
DVLOG(2) << "Sorting end ----";
nodes_.clear();
edges_.clear();
active_edges_.clear();
}
}