diff options
author | jamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-18 04:24:21 +0000 |
---|---|---|
committer | jamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-18 04:24:21 +0000 |
commit | fa816c6222fb6fd6ade423aa3c8b3bf378e4a92b (patch) | |
tree | 9c28aa5d36dc738da31df9f5e43caf432a4ef9bc /cc | |
parent | 2d803aeb45055fc663c6872ecd3dced582781543 (diff) | |
download | chromium_src-fa816c6222fb6fd6ade423aa3c8b3bf378e4a92b.zip chromium_src-fa816c6222fb6fd6ade423aa3c8b3bf378e4a92b.tar.gz chromium_src-fa816c6222fb6fd6ade423aa3c8b3bf378e4a92b.tar.bz2 |
Chromify MathUtil and FloatQuadUnittest
R=enne@chromium.org
Review URL: https://chromiumcodereview.appspot.com/12457028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@188670 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cc')
-rw-r--r-- | cc/damage_tracker.cc | 10 | ||||
-rw-r--r-- | cc/damage_tracker_unittest.cc | 2 | ||||
-rw-r--r-- | cc/debug_rect_history.cc | 10 | ||||
-rw-r--r-- | cc/delegated_renderer_layer_impl.cc | 6 | ||||
-rw-r--r-- | cc/direct_renderer.cc | 2 | ||||
-rw-r--r-- | cc/float_quad_unittest.cc | 83 | ||||
-rw-r--r-- | cc/gl_renderer.cc | 27 | ||||
-rw-r--r-- | cc/layer_impl.cc | 12 | ||||
-rw-r--r-- | cc/layer_sorter.cc | 10 | ||||
-rw-r--r-- | cc/layer_sorter_unittest.cc | 2 | ||||
-rw-r--r-- | cc/layer_tree_host_common.cc | 18 | ||||
-rw-r--r-- | cc/layer_tree_host_common_unittest.cc | 34 | ||||
-rw-r--r-- | cc/layer_tree_host_impl.cc | 20 | ||||
-rw-r--r-- | cc/managed_tile_state.cc | 4 | ||||
-rw-r--r-- | cc/math_util.cc | 791 | ||||
-rw-r--r-- | cc/math_util.h | 227 | ||||
-rw-r--r-- | cc/math_util_unittest.cc | 164 | ||||
-rw-r--r-- | cc/occlusion_tracker.cc | 36 | ||||
-rw-r--r-- | cc/occlusion_tracker_unittest.cc | 2 | ||||
-rw-r--r-- | cc/overdraw_metrics.cc | 2 | ||||
-rw-r--r-- | cc/picture_layer_impl.cc | 6 | ||||
-rw-r--r-- | cc/picture_layer_tiling.cc | 10 | ||||
-rw-r--r-- | cc/quad_culler_unittest.cc | 4 | ||||
-rw-r--r-- | cc/render_surface.cc | 4 | ||||
-rw-r--r-- | cc/render_surface_impl.cc | 6 | ||||
-rw-r--r-- | cc/test/layer_test_common.cc | 2 | ||||
-rw-r--r-- | cc/tile_priority.cc | 6 |
27 files changed, 832 insertions, 668 deletions
diff --git a/cc/damage_tracker.cc b/cc/damage_tracker.cc index 9fbaf18..b9fe5e4 100644 --- a/cc/damage_tracker.cc +++ b/cc/damage_tracker.cc @@ -271,7 +271,7 @@ void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, gfx::RectF old_rect_in_target_space = RemoveRectFromCurrentFrame(layer->id(), &layer_is_new); - gfx::RectF rect_in_target_space = MathUtil::mapClippedRect( + gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( layer->draw_transform(), gfx::RectF(gfx::PointF(), layer->content_bounds())); SaveRectForNextFrame(layer->id(), rect_in_target_space); @@ -290,7 +290,7 @@ void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, gfx::RectF update_content_rect = layer->LayerRectToContentRect(layer->update_rect()); gfx::RectF update_rect_in_target_space = - MathUtil::mapClippedRect(layer->draw_transform(), update_content_rect); + MathUtil::MapClippedRect(layer->draw_transform(), update_content_rect); target_damage_rect->Union(update_rect_in_target_space); } } @@ -343,13 +343,13 @@ void DamageTracker::ExtendDamageForRenderSurface( if (!damage_rect_in_local_space.IsEmpty()) { const gfx::Transform& draw_transform = render_surface->draw_transform(); gfx::RectF damage_rect_in_target_space = - MathUtil::mapClippedRect(draw_transform, damage_rect_in_local_space); + MathUtil::MapClippedRect(draw_transform, damage_rect_in_local_space); target_damage_rect->Union(damage_rect_in_target_space); if (layer->replica_layer()) { const gfx::Transform& replica_draw_transform = render_surface->replica_draw_transform(); - target_damage_rect->Union(MathUtil::mapClippedRect( + target_damage_rect->Union(MathUtil::MapClippedRect( replica_draw_transform, damage_rect_in_local_space)); } } @@ -364,7 +364,7 @@ void DamageTracker::ExtendDamageForRenderSurface( const gfx::Transform& replica_draw_transform = render_surface->replica_draw_transform(); - gfx::RectF replica_mask_layer_rect = MathUtil::mapClippedRect( + gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( replica_draw_transform, gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); SaveRectForNextFrame(replica_mask_layer->id(), replica_mask_layer_rect); diff --git a/cc/damage_tracker_unittest.cc b/cc/damage_tracker_unittest.cc index abf8da4..75013b3 100644 --- a/cc/damage_tracker_unittest.cc +++ b/cc/damage_tracker_unittest.cc @@ -355,7 +355,7 @@ TEST_F(DamageTrackerTest, verifyDamageForPerspectiveClippedLayer) // otherwise this test is not actually testing the intended scenario. gfx::QuadF testQuad(gfx::RectF(gfx::PointF(), gfx::SizeF(100, 100))); bool clipped = false; - MathUtil::mapQuad(transform, testQuad, clipped); + MathUtil::MapQuad(transform, testQuad, &clipped); EXPECT_TRUE(clipped); // Damage the child without moving it. diff --git a/cc/debug_rect_history.cc b/cc/debug_rect_history.cc index 6ad7cc6..68adc6f 100644 --- a/cc/debug_rect_history.cc +++ b/cc/debug_rect_history.cc @@ -64,7 +64,7 @@ void DebugRectHistory::SavePaintRects(LayerImpl* layer) { gfx::ScaleRect(layer->update_rect(), width_scale, height_scale); debug_rects_.push_back( DebugRect(PAINT_RECT_TYPE, - MathUtil::mapClippedRect(layer->screen_space_transform(), + MathUtil::MapClippedRect(layer->screen_space_transform(), update_content_rect))); } @@ -98,7 +98,7 @@ void DebugRectHistory::SavePropertyChangedRects( layer->LayerSurfacePropertyChanged()) { debug_rects_.push_back( DebugRect(PROPERTY_CHANGED_RECT_TYPE, - MathUtil::mapClippedRect( + MathUtil::MapClippedRect( layer->screen_space_transform(), gfx::RectF(gfx::PointF(), layer->content_bounds())))); } @@ -117,7 +117,7 @@ void DebugRectHistory::SaveSurfaceDamageRects( debug_rects_.push_back(DebugRect( SURFACE_DAMAGE_RECT_TYPE, - MathUtil::mapClippedRect( + MathUtil::MapClippedRect( render_surface->screen_space_transform(), render_surface->damage_tracker()->current_damage_rect()))); } @@ -134,13 +134,13 @@ void DebugRectHistory::SaveScreenSpaceRects( debug_rects_.push_back(DebugRect( SCREEN_SPACE_RECT_TYPE, - MathUtil::mapClippedRect(render_surface->screen_space_transform(), + MathUtil::MapClippedRect(render_surface->screen_space_transform(), render_surface->content_rect()))); if (render_surface_layer->replica_layer()) { debug_rects_.push_back( DebugRect(REPLICA_SCREEN_SPACE_RECT_TYPE, - MathUtil::mapClippedRect( + MathUtil::MapClippedRect( render_surface->replica_screen_space_transform(), render_surface->content_rect()))); } diff --git a/cc/delegated_renderer_layer_impl.cc b/cc/delegated_renderer_layer_impl.cc index adf20cf..e673745 100644 --- a/cc/delegated_renderer_layer_impl.cc +++ b/cc/delegated_renderer_layer_impl.cc @@ -76,7 +76,7 @@ void DelegatedRendererLayerImpl::SetFrameData( // will be in layer space. if (!frame_data->render_pass_list.empty()) { RenderPass* new_root_pass = frame_data->render_pass_list.back(); - gfx::RectF damage_in_layer = MathUtil::mapClippedRect( + gfx::RectF damage_in_layer = MathUtil::MapClippedRect( DelegatedFrameToLayerSpaceTransform( new_root_pass->output_rect.size()), damage_in_frame); @@ -286,13 +286,13 @@ void DelegatedRendererLayerImpl::AppendRenderPassQuads( if (render_target() == this) { DCHECK(!is_clipped()); DCHECK(render_surface()); - output_shared_quad_state->clip_rect = MathUtil::mapClippedRect( + output_shared_quad_state->clip_rect = MathUtil::MapClippedRect( delegated_frame_to_target_transform, output_shared_quad_state->clip_rect); } else { gfx::Rect clip_rect = drawable_content_rect(); if (output_shared_quad_state->is_clipped) { - clip_rect.Intersect(MathUtil::mapClippedRect( + clip_rect.Intersect(MathUtil::MapClippedRect( delegated_frame_to_target_transform, output_shared_quad_state->clip_rect)); } diff --git a/cc/direct_renderer.cc b/cc/direct_renderer.cc index aebbece..9ef2fab 100644 --- a/cc/direct_renderer.cc +++ b/cc/direct_renderer.cc @@ -211,7 +211,7 @@ gfx::RectF DirectRenderer::ComputeScissorRectForRenderPass( &inverseTransform)) { // Only intersect inverse-projected damage if the transform is invertible. gfx::RectF damage_rect_in_render_pass_space = - MathUtil::projectClippedRect(inverseTransform, frame.root_damage_rect); + MathUtil::ProjectClippedRect(inverseTransform, frame.root_damage_rect); render_pass_scissor.Intersect(damage_rect_in_render_pass_space); } diff --git a/cc/float_quad_unittest.cc b/cc/float_quad_unittest.cc index a13c582..82378c3 100644 --- a/cc/float_quad_unittest.cc +++ b/cc/float_quad_unittest.cc @@ -11,47 +11,54 @@ namespace cc { namespace { -// TODO(danakj) Move this test to ui/gfx/ when we don't need MathUtil::mapQuad. -TEST(FloatQuadTest, IsRectilinearTest) -{ - const int numRectilinear = 8; - gfx::Transform rectilinearTrans[numRectilinear]; - rectilinearTrans[1].Rotate(90); - rectilinearTrans[2].Rotate(180); - rectilinearTrans[3].Rotate(270); - rectilinearTrans[4].SkewX(0.00000000001); - rectilinearTrans[5].SkewY(0.00000000001); - rectilinearTrans[6].Scale(0.00001, 0.00001); - rectilinearTrans[6].Rotate(180); - rectilinearTrans[7].Scale(100000, 100000); - rectilinearTrans[7].Rotate(180); +// TODO(danakj) Move this test to ui/gfx/ when we don't need MathUtil::MapQuad. +TEST(FloatQuadTest, IsRectilinearTest) { + const int kNumRectilinear = 8; + gfx::Transform rectilinear_trans[kNumRectilinear]; + rectilinear_trans[1].Rotate(90.0); + rectilinear_trans[2].Rotate(180.0); + rectilinear_trans[3].Rotate(270.0); + rectilinear_trans[4].SkewX(0.00000000001); + rectilinear_trans[5].SkewY(0.00000000001); + rectilinear_trans[6].Scale(0.00001, 0.00001); + rectilinear_trans[6].Rotate(180.0); + rectilinear_trans[7].Scale(100000, 100000); + rectilinear_trans[7].Rotate(180.0); - for (int i = 0; i < numRectilinear; ++i) { - bool clipped = false; - gfx::QuadF quad = MathUtil::mapQuad(rectilinearTrans[i], gfx::QuadF(gfx::RectF(0.01010101f, 0.01010101f, 100.01010101f, 100.01010101f)), clipped); - ASSERT_TRUE(!clipped); - EXPECT_TRUE(quad.IsRectilinear()); - } + for (int i = 0; i < kNumRectilinear; ++i) { + bool clipped = false; + gfx::QuadF quad = MathUtil::MapQuad( + rectilinear_trans[i], + gfx::QuadF( + gfx::RectF(0.01010101f, 0.01010101f, 100.01010101f, 100.01010101f)), + &clipped); + ASSERT_TRUE(!clipped); + EXPECT_TRUE(quad.IsRectilinear()); + } - const int numNonRectilinear = 10; - gfx::Transform nonRectilinearTrans[numNonRectilinear]; - nonRectilinearTrans[0].Rotate(359.999); - nonRectilinearTrans[1].Rotate(0.0000001); - nonRectilinearTrans[2].Rotate(89.999999); - nonRectilinearTrans[3].Rotate(90.0000001); - nonRectilinearTrans[4].Rotate(179.999999); - nonRectilinearTrans[5].Rotate(180.0000001); - nonRectilinearTrans[6].Rotate(269.999999); - nonRectilinearTrans[7].Rotate(270.0000001); - nonRectilinearTrans[8].SkewX(0.00001); - nonRectilinearTrans[9].SkewY(0.00001); + const int kNumNonRectilinear = 10; + gfx::Transform non_rectilinear_trans[kNumNonRectilinear]; + non_rectilinear_trans[0].Rotate(359.999); + non_rectilinear_trans[1].Rotate(0.0000001); + non_rectilinear_trans[2].Rotate(89.999999); + non_rectilinear_trans[3].Rotate(90.0000001); + non_rectilinear_trans[4].Rotate(179.999999); + non_rectilinear_trans[5].Rotate(180.0000001); + non_rectilinear_trans[6].Rotate(269.999999); + non_rectilinear_trans[7].Rotate(270.0000001); + non_rectilinear_trans[8].SkewX(0.00001); + non_rectilinear_trans[9].SkewY(0.00001); - for (int i = 0; i < numNonRectilinear; ++i) { - bool clipped = false; - gfx::QuadF quad = MathUtil::mapQuad(nonRectilinearTrans[i], gfx::QuadF(gfx::RectF(0.01010101f, 0.01010101f, 100.01010101f, 100.01010101f)), clipped); - ASSERT_TRUE(!clipped); - EXPECT_FALSE(quad.IsRectilinear()); - } + for (int i = 0; i < kNumNonRectilinear; ++i) { + bool clipped = false; + gfx::QuadF quad = MathUtil::MapQuad( + non_rectilinear_trans[i], + gfx::QuadF( + gfx::RectF(0.01010101f, 0.01010101f, 100.01010101f, 100.01010101f)), + &clipped); + ASSERT_TRUE(!clipped); + EXPECT_FALSE(quad.IsRectilinear()); + } } } // namespace diff --git a/cc/gl_renderer.cc b/cc/gl_renderer.cc index 0c30385..a4d3af9 100644 --- a/cc/gl_renderer.cc +++ b/cc/gl_renderer.cc @@ -566,7 +566,7 @@ scoped_ptr<ScopedResource> GLRenderer::DrawBackgroundFilters( // FIXME: Do a single readback for both the surface and replica and cache the // filtered results (once filter textures are not reused). - gfx::Rect device_rect = gfx::ToEnclosingRect(MathUtil::mapClippedRect( + gfx::Rect device_rect = gfx::ToEnclosingRect(MathUtil::MapClippedRect( contents_device_transform, SharedGeometryQuad().BoundingBox())); int top, right, bottom, left; @@ -677,8 +677,8 @@ void GLRenderer::DrawRenderPassQuad(DrawingFrame& frame, } bool clipped = false; - gfx::QuadF device_quad = MathUtil::mapQuad( - contents_device_transform, SharedGeometryQuad(), clipped); + gfx::QuadF device_quad = MathUtil::MapQuad( + contents_device_transform, SharedGeometryQuad(), &clipped); DCHECK(!clipped); LayerQuad deviceLayerBounds(gfx::QuadF(device_quad.BoundingBox())); LayerQuad device_layer_edges(device_quad); @@ -829,8 +829,9 @@ void GLRenderer::DrawRenderPassQuad(DrawingFrame& frame, // Map device space quad to surface space. contents_device_transform has no 3d // component since it was flattened, so we don't need to project. - gfx::QuadF surface_quad = MathUtil::mapQuad( - contents_device_transform_inverse, device_layer_edges.ToQuadF(), clipped); + gfx::QuadF surface_quad = MathUtil::MapQuad(contents_device_transform_inverse, + device_layer_edges.ToQuadF(), + &clipped); DCHECK(!clipped); SetShaderOpacity(quad->opacity(), shader_alpha_location); @@ -872,8 +873,8 @@ bool GLRenderer::SetupQuadForAntialiasing( gfx::Rect tile_rect = quad->visible_rect; bool clipped = false; - gfx::QuadF device_layer_quad = MathUtil::mapQuad( - device_transform, gfx::QuadF(quad->visibleContentRect()), clipped); + gfx::QuadF device_layer_quad = MathUtil::MapQuad( + device_transform, gfx::QuadF(quad->visibleContentRect()), &clipped); DCHECK(!clipped); // TODO(reveman): Axis-aligned is not enough to avoid anti-aliasing. @@ -900,13 +901,13 @@ bool GLRenderer::SetupQuadForAntialiasing( gfx::PointF top_right = tile_rect.top_right(); // Map points to device space. - bottom_right = MathUtil::mapPoint(device_transform, bottom_right, clipped); + bottom_right = MathUtil::MapPoint(device_transform, bottom_right, &clipped); DCHECK(!clipped); - bottom_left = MathUtil::mapPoint(device_transform, bottom_left, clipped); + bottom_left = MathUtil::MapPoint(device_transform, bottom_left, &clipped); DCHECK(!clipped); - top_left = MathUtil::mapPoint(device_transform, top_left, clipped); + top_left = MathUtil::MapPoint(device_transform, top_left, &clipped); DCHECK(!clipped); - top_right = MathUtil::mapPoint(device_transform, top_right, clipped); + top_right = MathUtil::MapPoint(device_transform, top_right, &clipped); DCHECK(!clipped); LayerQuad::Edge bottom_edge(bottom_right, bottom_left); @@ -940,8 +941,8 @@ bool GLRenderer::SetupQuadForAntialiasing( gfx::Transform::kSkipInitialization); bool did_invert = device_transform.GetInverse(&inverse_device_transform); DCHECK(did_invert); - *local_quad = MathUtil::mapQuad( - inverse_device_transform, device_quad.ToQuadF(), clipped); + *local_quad = MathUtil::MapQuad( + inverse_device_transform, device_quad.ToQuadF(), &clipped); // We should not DCHECK(!clipped) here, because anti-aliasing inflation may // cause deviceQuad to become clipped. To our knowledge this scenario does // not need to be handled differently than the unclipped case. diff --git a/cc/layer_impl.cc b/cc/layer_impl.cc index ecfbdc6..eaaa07e 100644 --- a/cc/layer_impl.cc +++ b/cc/layer_impl.cc @@ -244,9 +244,9 @@ InputHandlerClient::ScrollStatus LayerImpl::TryScroll( } gfx::PointF hit_test_point_in_content_space = - MathUtil::projectPoint(inverse_screen_space_transform, + MathUtil::ProjectPoint(inverse_screen_space_transform, screen_space_point, - clipped); + &clipped); gfx::PointF hit_test_point_in_layer_space = gfx::ScalePoint(hit_test_point_in_content_space, 1.f / contents_scale_x(), @@ -944,15 +944,15 @@ void LayerImpl::SetVerticalScrollbarLayer(ScrollbarLayerImpl* scrollbar_layer) { void LayerImpl::AsValueInto(base::DictionaryValue* dict) const { dict->SetInteger("id", id()); - dict->Set("bounds", MathUtil::asValue(bounds()).release()); + dict->Set("bounds", MathUtil::AsValue(bounds()).release()); dict->SetInteger("draws_content", DrawsContent()); bool clipped; - gfx::QuadF layer_quad = MathUtil::mapQuad( + gfx::QuadF layer_quad = MathUtil::MapQuad( screen_space_transform(), gfx::QuadF(gfx::Rect(content_bounds())), - clipped); - dict->Set("layer_quad", MathUtil::asValue(layer_quad).release()); + &clipped); + dict->Set("layer_quad", MathUtil::AsValue(layer_quad).release()); } diff --git a/cc/layer_sorter.cc b/cc/layer_sorter.cc index ef8bbca..f567219 100644 --- a/cc/layer_sorter.cc +++ b/cc/layer_sorter.cc @@ -190,7 +190,7 @@ LayerShape::LayerShape(float width, gfx::PointF clippedQuad[8]; int num_vertices_in_clipped_quad; - MathUtil::mapClippedQuad(draw_transform, + MathUtil::MapClippedQuad(draw_transform, layer_quad, clippedQuad, num_vertices_in_clipped_quad); @@ -201,7 +201,7 @@ LayerShape::LayerShape(float width, } projected_bounds = - MathUtil::computeEnclosingRectOfVertices(clippedQuad, + MathUtil::ComputeEnclosingRectOfVertices(clippedQuad, num_vertices_in_clipped_quad); // NOTE: it will require very significant refactoring and overhead to deal @@ -222,11 +222,11 @@ LayerShape::LayerShape(float width, // Compute the normal of the layer's plane. bool clipped = false; gfx::Point3F c1 = - MathUtil::mapPoint(draw_transform, gfx::Point3F(0.f, 0.f, 0.f), clipped); + 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); + 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); + MathUtil::MapPoint(draw_transform, gfx::Point3F(1.f, 0.f, 0.f), &clipped); // FIXME: Deal with clipping. gfx::Vector3dF c12 = c2 - c1; gfx::Vector3dF c13 = c3 - c1; diff --git a/cc/layer_sorter_unittest.cc b/cc/layer_sorter_unittest.cc index 58f653b..38227ea 100644 --- a/cc/layer_sorter_unittest.cc +++ b/cc/layer_sorter_unittest.cc @@ -186,7 +186,7 @@ TEST(LayerSorterTest, LayersUnderPathologicalPerspectiveTransform) { // where part of layer B go behind the w = 0 plane. gfx::QuadF test_quad = gfx::QuadF(gfx::RectF(-0.5f, -0.5f, 1.f, 1.f)); bool clipped = false; - MathUtil::mapQuad(perspective_matrix * transform_b, test_quad, clipped); + MathUtil::MapQuad(perspective_matrix * transform_b, test_quad, &clipped); ASSERT_TRUE(clipped); overlap_result = diff --git a/cc/layer_tree_host_common.cc b/cc/layer_tree_host_common.cc index b59e2a9..6e3ad49 100644 --- a/cc/layer_tree_host_common.cc +++ b/cc/layer_tree_host_common.cc @@ -64,14 +64,14 @@ inline gfx::Rect calculateVisibleRectWithCachedLayerRect(const gfx::Rect& target // TODO(shawnsingh): Either we need to handle uninvertible transforms // here, or DCHECK that the transform is invertible. } - gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect))); + gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect))); layerRect.Intersect(layerBoundRect); return layerRect; } gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Transform& transform) { - gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect); + gfx::Rect layerInSurfaceSpace = MathUtil::MapClippedRect(transform, layerBoundRect); return calculateVisibleRectWithCachedLayerRect(targetSurfaceRect, layerBoundRect, layerInSurfaceSpace, transform); } @@ -445,7 +445,7 @@ static inline void CalculateContentsScale(LayerType* layer, float contentsScale, static inline void updateLayerContentsScale(LayerImpl* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animating_transform_to_screen) { - gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor); + gfx::Vector2dF transformScale = MathUtil::ComputeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor); float contentsScale = std::max(transformScale.x(), transformScale.y()); CalculateContentsScale(layer, contentsScale, animating_transform_to_screen); } @@ -455,7 +455,7 @@ static inline void updateLayerContentsScale(Layer* layer, const gfx::Transform& float rasterScale = layer->raster_scale(); if (layer->automatically_compute_raster_scale()) { - gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, 0.f); + gfx::Vector2dF transformScale = MathUtil::ComputeTransform2dScaleComponents(combinedTransform, 0.f); float combinedScale = std::max(transformScale.x(), transformScale.y()); float idealRasterScale = combinedScale / deviceScaleFactor; if (!layer->bounds_contain_page_scale()) @@ -743,7 +743,7 @@ static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transfo gfx::Transform nextHierarchyMatrix = fullHierarchyMatrix; gfx::Transform sublayerMatrix; - gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor); + gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::ComputeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor); if (subtreeShouldRenderToSeparateSurface(layer, combinedTransform.IsScaleOrTranslation())) { // Check back-face visibility before continuing with this surface and its subtree @@ -825,7 +825,7 @@ static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transfo // TODO(shawnsingh): Either we need to handle uninvertible transforms // here, or DCHECK that the transform is invertible. } - clipRectForSubtreeInDescendantSpace = gfx::ToEnclosingRect(MathUtil::projectClippedRect(inverseSurfaceDrawTransform, renderSurface->clip_rect())); + clipRectForSubtreeInDescendantSpace = gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(inverseSurfaceDrawTransform, renderSurface->clip_rect())); } else { renderSurface->SetClipRect(gfx::Rect()); clipRectForSubtreeInDescendantSpace = clipRectFromAncestorInDescendantSpace; @@ -867,7 +867,7 @@ static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transfo if (adjustTextAA) layerDrawProperties.can_use_lcd_text = layerCanUseLCDText; - gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->draw_transform(), contentRect)); + gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::MapClippedRect(layer->draw_transform(), contentRect)); if (layerClipsSubtree(layer)) { subtreeShouldBeClipped = true; @@ -1097,7 +1097,7 @@ static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const gfx::Transf // Transform the hit test point from screen space to the local space of the given rect. bool clipped = false; - gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(inverseLocalSpaceToScreenSpace, screenSpacePoint, clipped); + gfx::PointF hitTestPointInLocalSpace = MathUtil::ProjectPoint(inverseLocalSpaceToScreenSpace, screenSpacePoint, &clipped); // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect. if (clipped) @@ -1115,7 +1115,7 @@ static bool pointHitsRegion(gfx::PointF screenSpacePoint, const gfx::Transform& // Transform the hit test point from screen space to the local space of the given region. bool clipped = false; - gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, clipped); + gfx::PointF hitTestPointInContentSpace = MathUtil::ProjectPoint(inverseScreenSpaceTransform, screenSpacePoint, &clipped); gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY); // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region. diff --git a/cc/layer_tree_host_common_unittest.cc b/cc/layer_tree_host_common_unittest.cc index cb228b3..7739afd 100644 --- a/cc/layer_tree_host_common_unittest.cc +++ b/cc/layer_tree_host_common_unittest.cc @@ -420,7 +420,7 @@ TEST(LayerTreeHostCommonTest, verifyTransformsForSingleRenderSurface) gfx::Transform parentCompositeTransform = parentTranslationToAnchor * parentLayerTransform * inverse(parentTranslationToAnchor) * parentTranslationToAnchor * parentSublayerMatrix * inverse(parentTranslationToAnchor); - gfx::Vector2dF parentCompositeScale = MathUtil::computeTransform2dScaleComponents(parentCompositeTransform, 1.0f); + gfx::Vector2dF parentCompositeScale = MathUtil::ComputeTransform2dScaleComponents(parentCompositeTransform, 1.0f); gfx::Transform surfaceSublayerTransform; surfaceSublayerTransform.Scale(parentCompositeScale.x(), parentCompositeScale.y()); gfx::Transform surfaceSublayerCompositeTransform = parentCompositeTransform * inverse(surfaceSublayerTransform); @@ -593,7 +593,7 @@ TEST(LayerTreeHostCommonTest, verifyTransformsForReplica) * parentTranslationToAnchor * parentSublayerMatrix * inverse(parentTranslationToAnchor); gfx::Transform replicaLayerTransform; replicaLayerTransform.Scale3d(3, 3, 1); - gfx::Vector2dF parentCompositeScale = MathUtil::computeTransform2dScaleComponents(parentCompositeTransform, 1.f); + gfx::Vector2dF parentCompositeScale = MathUtil::ComputeTransform2dScaleComponents(parentCompositeTransform, 1.f); gfx::Transform surfaceSublayerTransform; surfaceSublayerTransform.Scale(parentCompositeScale.x(), parentCompositeScale.y()); gfx::Transform replicaCompositeTransform = parentCompositeTransform * replicaLayerTransform * inverse(surfaceSublayerTransform); @@ -676,7 +676,7 @@ TEST(LayerTreeHostCommonTest, verifyTransformsForRenderSurfaceHierarchy) gfx::Transform B = translationToAnchor * sublayerTransform * inverse(translationToAnchor); gfx::Transform R = A * translationToAnchor * replicaLayerTransform * inverse(translationToAnchor); - gfx::Vector2dF surface1ParentTransformScale = MathUtil::computeTransform2dScaleComponents(A * B, 1.f); + gfx::Vector2dF surface1ParentTransformScale = MathUtil::ComputeTransform2dScaleComponents(A * B, 1.f); gfx::Transform surface1SublayerTransform; surface1SublayerTransform.Scale(surface1ParentTransformScale.x(), surface1ParentTransformScale.y()); @@ -685,7 +685,7 @@ TEST(LayerTreeHostCommonTest, verifyTransformsForRenderSurfaceHierarchy) // S1 = transform to move from renderSurface1 pixels to the layer space of the owning layer gfx::Transform S1 = inverse(surface1SublayerTransform); - gfx::Vector2dF surface2ParentTransformScale = MathUtil::computeTransform2dScaleComponents(SS1 * A * B, 1.f); + gfx::Vector2dF surface2ParentTransformScale = MathUtil::ComputeTransform2dScaleComponents(SS1 * A * B, 1.f); gfx::Transform surface2SublayerTransform; surface2SublayerTransform.Scale(surface2ParentTransformScale.x(), surface2ParentTransformScale.y()); @@ -2226,7 +2226,7 @@ TEST(LayerTreeHostCommonTest, verifyVisibleRectFor3dPerspectiveWhenClippedByW) // Sanity check that this transform does indeed cause w < 0 when applying the // transform, otherwise this code is not testing the intended scenario. bool clipped = false; - MathUtil::mapQuad(layerToSurfaceTransform, gfx::QuadF(gfx::RectF(layerContentRect)), clipped); + MathUtil::MapQuad(layerToSurfaceTransform, gfx::QuadF(gfx::RectF(layerContentRect)), &clipped); ASSERT_TRUE(clipped); int expectedXPosition = 0; @@ -2256,8 +2256,8 @@ TEST(LayerTreeHostCommonTest, verifyVisibleRectForPerspectiveUnprojection) // Sanity check that un-projection does indeed cause w < 0, otherwise this code is not // testing the intended scenario. bool clipped = false; - gfx::RectF clippedRect = MathUtil::mapClippedRect(layerToSurfaceTransform, layerContentRect); - MathUtil::projectQuad(inverse(layerToSurfaceTransform), gfx::QuadF(clippedRect), clipped); + gfx::RectF clippedRect = MathUtil::MapClippedRect(layerToSurfaceTransform, layerContentRect); + MathUtil::ProjectQuad(inverse(layerToSurfaceTransform), gfx::QuadF(clippedRect), &clipped); ASSERT_TRUE(clipped); // Only the corner of the layer is not visible on the surface because of being @@ -4287,8 +4287,8 @@ TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPI) // Verify results of transformed parent rects gfx::RectF parentContentBounds(gfx::PointF(), gfx::SizeF(parent->content_bounds())); - gfx::RectF parentDrawRect = MathUtil::mapClippedRect(parent->draw_transform(), parentContentBounds); - gfx::RectF parentScreenSpaceRect = MathUtil::mapClippedRect(parent->screen_space_transform(), parentContentBounds); + gfx::RectF parentDrawRect = MathUtil::MapClippedRect(parent->draw_transform(), parentContentBounds); + gfx::RectF parentScreenSpaceRect = MathUtil::MapClippedRect(parent->screen_space_transform(), parentContentBounds); gfx::RectF expectedParentDrawRect(gfx::PointF(), parent->bounds()); expectedParentDrawRect.Scale(deviceScaleFactor); @@ -4306,11 +4306,11 @@ TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPI) // Verify results of transformed child and childEmpty rects. They should match. gfx::RectF childContentBounds(gfx::PointF(), gfx::SizeF(child->content_bounds())); - gfx::RectF childDrawRect = MathUtil::mapClippedRect(child->draw_transform(), childContentBounds); - gfx::RectF childScreenSpaceRect = MathUtil::mapClippedRect(child->screen_space_transform(), childContentBounds); + gfx::RectF childDrawRect = MathUtil::MapClippedRect(child->draw_transform(), childContentBounds); + gfx::RectF childScreenSpaceRect = MathUtil::MapClippedRect(child->screen_space_transform(), childContentBounds); - gfx::RectF childEmptyDrawRect = MathUtil::mapClippedRect(childEmpty->draw_transform(), childContentBounds); - gfx::RectF childEmptyScreenSpaceRect = MathUtil::mapClippedRect(childEmpty->screen_space_transform(), childContentBounds); + gfx::RectF childEmptyDrawRect = MathUtil::MapClippedRect(childEmpty->draw_transform(), childContentBounds); + gfx::RectF childEmptyScreenSpaceRect = MathUtil::MapClippedRect(childEmpty->screen_space_transform(), childContentBounds); gfx::RectF expectedChildDrawRect(child->position(), child->bounds()); expectedChildDrawRect.Scale(deviceScaleFactor); @@ -4440,8 +4440,8 @@ TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPIAccurateScaleZeroChi // Verify results of transformed parent rects gfx::RectF parentContentBounds(gfx::PointF(), gfx::SizeF(parent->content_bounds())); - gfx::RectF parentDrawRect = MathUtil::mapClippedRect(parent->draw_transform(), parentContentBounds); - gfx::RectF parentScreenSpaceRect = MathUtil::mapClippedRect(parent->screen_space_transform(), parentContentBounds); + gfx::RectF parentDrawRect = MathUtil::MapClippedRect(parent->draw_transform(), parentContentBounds); + gfx::RectF parentScreenSpaceRect = MathUtil::MapClippedRect(parent->screen_space_transform(), parentContentBounds); gfx::RectF expectedParentDrawRect(gfx::PointF(), parent->bounds()); expectedParentDrawRect.Scale(deviceScaleFactor); @@ -4458,8 +4458,8 @@ TEST(LayerTreeHostCommonTest, verifyLayerTransformsInHighDPIAccurateScaleZeroChi // Verify results of transformed child rects gfx::RectF childContentBounds(gfx::PointF(), gfx::SizeF(child->content_bounds())); - gfx::RectF childDrawRect = MathUtil::mapClippedRect(child->draw_transform(), childContentBounds); - gfx::RectF childScreenSpaceRect = MathUtil::mapClippedRect(child->screen_space_transform(), childContentBounds); + gfx::RectF childDrawRect = MathUtil::MapClippedRect(child->draw_transform(), childContentBounds); + gfx::RectF childScreenSpaceRect = MathUtil::MapClippedRect(child->screen_space_transform(), childContentBounds); gfx::RectF expectedChildDrawRect(gfx::PointF(), child->bounds()); expectedChildDrawRect.Scale(deviceScaleFactor); diff --git a/cc/layer_tree_host_impl.cc b/cc/layer_tree_host_impl.cc index 667b19b..b5d82f3 100644 --- a/cc/layer_tree_host_impl.cc +++ b/cc/layer_tree_host_impl.cc @@ -470,7 +470,7 @@ static void AppendQuadsToFillScreen( // The root layer transform is composed of translations and scales only, // no perspective, so mapping is sufficient (as opposed to projecting). gfx::Rect layer_rect = - MathUtil::mapClippedRect(transform_to_layer_space, fill_rects.rect()); + MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect()); // Skip the quad culler and just append the quads directly to avoid // occlusion checks. scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create(); @@ -1432,13 +1432,13 @@ gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta( bool start_clipped, end_clipped; gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta; gfx::PointF local_start_point = - MathUtil::projectPoint(inverse_screen_space_transform, + MathUtil::ProjectPoint(inverse_screen_space_transform, screen_space_point, - start_clipped); + &start_clipped); gfx::PointF local_end_point = - MathUtil::projectPoint(inverse_screen_space_transform, + MathUtil::ProjectPoint(inverse_screen_space_transform, screen_space_end_point, - end_clipped); + &end_clipped); // In general scroll point coordinates should not get clipped. DCHECK(!start_clipped); @@ -1469,9 +1469,9 @@ gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta( // Calculate the applied scroll delta in viewport space coordinates. gfx::PointF actual_screen_space_end_point = - MathUtil::mapPoint(layer_impl->screen_space_transform(), + MathUtil::MapPoint(layer_impl->screen_space_transform(), actual_local_content_end_point, - end_clipped); + &end_clipped); DCHECK(!end_clipped); if (end_clipped) return gfx::Vector2dF(); @@ -1542,7 +1542,7 @@ bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point, // make it easier to scroll just one layer in one direction without // affecting any of its parents. float angle_threshold = 45; - if (MathUtil::smallestAngleBetweenVectors( + if (MathUtil::SmallestAngleBetweenVectors( applied_delta, pending_delta) < angle_threshold) { pending_delta = gfx::Vector2d(); break; @@ -1551,7 +1551,7 @@ bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point, // Allow further movement only on an axis perpendicular to the direction in // which the layer moved. gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x()); - pending_delta = MathUtil::projectVector(pending_delta, perpendicular_axis); + pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis); if (gfx::ToFlooredVector2d(pending_delta).IsZero()) break; @@ -1904,7 +1904,7 @@ scoped_ptr<base::Value> LayerTreeHostImpl::FrameStateAsValue() const { scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); state->SetString("lthi_id", StringPrintf("%p", this)); state->Set("device_viewport_size", - MathUtil::asValue(device_viewport_size_).release()); + MathUtil::AsValue(device_viewport_size_).release()); if (tile_manager_) state->Set("tiles", tile_manager_->AllTilesAsValue().release()); state->Set("active_tree", active_tree_->AsValue().release()); diff --git a/cc/managed_tile_state.cc b/cc/managed_tile_state.cc index 480be48..b416045 100644 --- a/cc/managed_tile_state.cc +++ b/cc/managed_tile_state.cc @@ -68,9 +68,9 @@ scoped_ptr<base::Value> ManagedTileState::AsValue() const { TileManagerBinAsValue(bin[ACTIVE_TREE]).release()); state->Set("resolution", TileResolutionAsValue(resolution).release()); state->Set("time_to_needed_in_seconds", - MathUtil::asValueSafely(time_to_needed_in_seconds).release()); + MathUtil::AsValueSafely(time_to_needed_in_seconds).release()); state->Set("distance_to_visible_in_pixels", - MathUtil::asValueSafely(distance_to_visible_in_pixels).release()); + MathUtil::AsValueSafely(distance_to_visible_in_pixels).release()); state->SetBoolean("is_picture_pile_analyzed", picture_pile_analyzed); state->SetBoolean("is_cheap_to_raster", picture_pile_analysis.is_cheap_to_raster); diff --git a/cc/math_util.cc b/cc/math_util.cc index f5c0cb4..f177480 100644 --- a/cc/math_util.cc +++ b/cc/math_util.cc @@ -4,6 +4,7 @@ #include "cc/math_util.h" +#include <algorithm> #include <cmath> #include <limits> @@ -17,406 +18,538 @@ namespace cc { -const double MathUtil::PI_DOUBLE = 3.14159265358979323846; -const float MathUtil::PI_FLOAT = 3.14159265358979323846f; -const double MathUtil::EPSILON = 1e-9; - -static HomogeneousCoordinate projectHomogeneousPoint(const gfx::Transform& transform, const gfx::PointF& p) -{ - // In this case, the layer we are trying to project onto is perpendicular to ray - // (point p and z-axis direction) that we are trying to project. This happens when the - // layer is rotated so that it is infinitesimally thin, or when it is co-planar with - // the camera origin -- i.e. when the layer is invisible anyway. - if (!transform.matrix().getDouble(2, 2)) - return HomogeneousCoordinate(0, 0, 0, 1); - - double x = p.x(); - double y = p.y(); - double z = -(transform.matrix().getDouble(2, 0) * x + transform.matrix().getDouble(2, 1) * y + transform.matrix().getDouble(2, 3)) / transform.matrix().getDouble(2, 2); - // implicit definition of w = 1; - - double outX = x * transform.matrix().getDouble(0, 0) + y * transform.matrix().getDouble(0, 1) + z * transform.matrix().getDouble(0, 2) + transform.matrix().getDouble(0, 3); - double outY = x * transform.matrix().getDouble(1, 0) + y * transform.matrix().getDouble(1, 1) + z * transform.matrix().getDouble(1, 2) + transform.matrix().getDouble(1, 3); - double outZ = x * transform.matrix().getDouble(2, 0) + y * transform.matrix().getDouble(2, 1) + z * transform.matrix().getDouble(2, 2) + transform.matrix().getDouble(2, 3); - double outW = x * transform.matrix().getDouble(3, 0) + y * transform.matrix().getDouble(3, 1) + z * transform.matrix().getDouble(3, 2) + transform.matrix().getDouble(3, 3); - - return HomogeneousCoordinate(outX, outY, outZ, outW); +const double MathUtil::kPiDouble = 3.14159265358979323846; +const float MathUtil::kPiFloat = 3.14159265358979323846f; + +static HomogeneousCoordinate ProjectHomogeneousPoint( + const gfx::Transform& transform, + gfx::PointF p) { + // In this case, the layer we are trying to project onto is perpendicular to + // ray (point p and z-axis direction) that we are trying to project. This + // happens when the layer is rotated so that it is infinitesimally thin, or + // when it is co-planar with the camera origin -- i.e. when the layer is + // invisible anyway. + if (!transform.matrix().getDouble(2, 2)) + return HomogeneousCoordinate(0.0, 0.0, 0.0, 1.0); + + double x = p.x(); + double y = p.y(); + double z = -(transform.matrix().getDouble(2, 0) * x + + transform.matrix().getDouble(2, 1) * y + + transform.matrix().getDouble(2, 3)) / + transform.matrix().getDouble(2, 2); + // implicit definition of w = 1; + + double out_x = x * transform.matrix().getDouble(0, 0) + + y * transform.matrix().getDouble(0, 1) + + z * transform.matrix().getDouble(0, 2) + + transform.matrix().getDouble(0, 3); + double out_y = x * transform.matrix().getDouble(1, 0) + + y * transform.matrix().getDouble(1, 1) + + z * transform.matrix().getDouble(1, 2) + + transform.matrix().getDouble(1, 3); + double out_z = x * transform.matrix().getDouble(2, 0) + + y * transform.matrix().getDouble(2, 1) + + z * transform.matrix().getDouble(2, 2) + + transform.matrix().getDouble(2, 3); + double out_w = x * transform.matrix().getDouble(3, 0) + + y * transform.matrix().getDouble(3, 1) + + z * transform.matrix().getDouble(3, 2) + + transform.matrix().getDouble(3, 3); + + return HomogeneousCoordinate(out_x, out_y, out_z, out_w); } -static HomogeneousCoordinate mapHomogeneousPoint(const gfx::Transform& transform, const gfx::Point3F& p) -{ - double x = p.x(); - double y = p.y(); - double z = p.z(); - // implicit definition of w = 1; - - double outX = x * transform.matrix().getDouble(0, 0) + y * transform.matrix().getDouble(0, 1) + z * transform.matrix().getDouble(0, 2) + transform.matrix().getDouble(0, 3); - double outY = x * transform.matrix().getDouble(1, 0) + y * transform.matrix().getDouble(1, 1) + z * transform.matrix().getDouble(1, 2) + transform.matrix().getDouble(1, 3); - double outZ = x * transform.matrix().getDouble(2, 0) + y * transform.matrix().getDouble(2, 1) + z * transform.matrix().getDouble(2, 2) + transform.matrix().getDouble(2, 3); - double outW = x * transform.matrix().getDouble(3, 0) + y * transform.matrix().getDouble(3, 1) + z * transform.matrix().getDouble(3, 2) + transform.matrix().getDouble(3, 3); - - return HomogeneousCoordinate(outX, outY, outZ, outW); +static HomogeneousCoordinate MapHomogeneousPoint( + const gfx::Transform& transform, + const gfx::Point3F& p) { + double x = p.x(); + double y = p.y(); + double z = p.z(); + // implicit definition of w = 1; + + double out_x = x * transform.matrix().getDouble(0, 0) + + y * transform.matrix().getDouble(0, 1) + + z * transform.matrix().getDouble(0, 2) + + transform.matrix().getDouble(0, 3); + double out_y = x * transform.matrix().getDouble(1, 0) + + y * transform.matrix().getDouble(1, 1) + + z * transform.matrix().getDouble(1, 2) + + transform.matrix().getDouble(1, 3); + double out_z = x * transform.matrix().getDouble(2, 0) + + y * transform.matrix().getDouble(2, 1) + + z * transform.matrix().getDouble(2, 2) + + transform.matrix().getDouble(2, 3); + double out_w = x * transform.matrix().getDouble(3, 0) + + y * transform.matrix().getDouble(3, 1) + + z * transform.matrix().getDouble(3, 2) + + transform.matrix().getDouble(3, 3); + + return HomogeneousCoordinate(out_x, out_y, out_z, out_w); } -static HomogeneousCoordinate computeClippedPointForEdge(const HomogeneousCoordinate& h1, const HomogeneousCoordinate& h2) -{ - // Points h1 and h2 form a line in 4d, and any point on that line can be represented - // as an interpolation between h1 and h2: - // p = (1-t) h1 + (t) h2 - // - // We want to compute point p such that p.w == epsilon, where epsilon is a small - // non-zero number. (but the smaller the number is, the higher the risk of overflow) - // To do this, we solve for t in the following equation: - // p.w = epsilon = (1-t) * h1.w + (t) * h2.w - // - // Once paramter t is known, the rest of p can be computed via p = (1-t) h1 + (t) h2. - - // Technically this is a special case of the following assertion, but its a good idea to keep it an explicit sanity check here. - DCHECK(h2.w != h1.w); - // Exactly one of h1 or h2 (but not both) must be on the negative side of the w plane when this is called. - DCHECK(h1.shouldBeClipped() ^ h2.shouldBeClipped()); - - double w = 0.00001; // or any positive non-zero small epsilon - - double t = (w - h1.w) / (h2.w - h1.w); - - double x = (1-t) * h1.x + t * h2.x; - double y = (1-t) * h1.y + t * h2.y; - double z = (1-t) * h1.z + t * h2.z; - - return HomogeneousCoordinate(x, y, z, w); +static HomogeneousCoordinate ComputeClippedPointForEdge( + const HomogeneousCoordinate& h1, + const HomogeneousCoordinate& h2) { + // Points h1 and h2 form a line in 4d, and any point on that line can be + // represented as an interpolation between h1 and h2: + // p = (1-t) h1 + (t) h2 + // + // We want to compute point p such that p.w == epsilon, where epsilon is a + // small non-zero number. (but the smaller the number is, the higher the risk + // of overflow) + // To do this, we solve for t in the following equation: + // p.w = epsilon = (1-t) * h1.w + (t) * h2.w + // + // Once paramter t is known, the rest of p can be computed via + // p = (1-t) h1 + (t) h2. + + // Technically this is a special case of the following assertion, but its a + // good idea to keep it an explicit sanity check here. + DCHECK_NE(h2.w, h1.w); + // Exactly one of h1 or h2 (but not both) must be on the negative side of the + // w plane when this is called. + DCHECK(h1.ShouldBeClipped() ^ h2.ShouldBeClipped()); + + double w = 0.00001; // or any positive non-zero small epsilon + + double t = (w - h1.w) / (h2.w - h1.w); + + double x = (1 - t) * h1.x + t * h2.x; + double y = (1 - t) * h1.y + t * h2.y; + double z = (1 - t) * h1.z + t * h2.z; + + return HomogeneousCoordinate(x, y, z, w); } -static inline void expandBoundsToIncludePoint(float& xmin, float& xmax, float& ymin, float& ymax, const gfx::PointF& p) -{ - xmin = std::min(p.x(), xmin); - xmax = std::max(p.x(), xmax); - ymin = std::min(p.y(), ymin); - ymax = std::max(p.y(), ymax); +static inline void ExpandBoundsToIncludePoint(float* xmin, + float* xmax, + float* ymin, + float* ymax, + gfx::PointF p) { + *xmin = std::min(p.x(), *xmin); + *xmax = std::max(p.x(), *xmax); + *ymin = std::min(p.y(), *ymin); + *ymax = std::max(p.y(), *ymax); } -static inline void addVertexToClippedQuad(const gfx::PointF& newVertex, gfx::PointF clippedQuad[8], int& numVerticesInClippedQuad) -{ - clippedQuad[numVerticesInClippedQuad] = newVertex; - numVerticesInClippedQuad++; +static inline void AddVertexToClippedQuad(gfx::PointF new_vertex, + gfx::PointF clipped_quad[8], + int& num_vertices_in_clipped_quad) { + clipped_quad[num_vertices_in_clipped_quad] = new_vertex; + num_vertices_in_clipped_quad++; } -gfx::Rect MathUtil::mapClippedRect(const gfx::Transform& transform, const gfx::Rect& srcRect) -{ - return gfx::ToEnclosingRect(mapClippedRect(transform, gfx::RectF(srcRect))); +gfx::Rect MathUtil::MapClippedRect(const gfx::Transform& transform, + gfx::Rect src_rect) { + return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect))); } -gfx::RectF MathUtil::mapClippedRect(const gfx::Transform& transform, const gfx::RectF& srcRect) -{ - if (transform.IsIdentityOrTranslation()) - return srcRect + gfx::Vector2dF(static_cast<float>(transform.matrix().getDouble(0, 3)), static_cast<float>(transform.matrix().getDouble(1, 3))); - - // Apply the transform, but retain the result in homogeneous coordinates. - - double quad[4 * 2]; // input: 4 x 2D points - quad[0] = srcRect.x(); - quad[1] = srcRect.y(); - quad[2] = srcRect.right(); - quad[3] = srcRect.y(); - quad[4] = srcRect.right(); - quad[5] = srcRect.bottom(); - quad[6] = srcRect.x(); - quad[7] = srcRect.bottom(); - - double result[4 * 4]; // output: 4 x 4D homogeneous points - transform.matrix().map2(quad, 4, result); - - HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); - HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); - HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]); - HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]); - return computeEnclosingClippedRect(hc0, hc1, hc2, hc3); +gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform, + const gfx::RectF& src_rect) { + if (transform.IsIdentityOrTranslation()) + return src_rect + + gfx::Vector2dF( + static_cast<float>(transform.matrix().getDouble(0, 3)), + static_cast<float>(transform.matrix().getDouble(1, 3))); + + // Apply the transform, but retain the result in homogeneous coordinates. + + double quad[4 * 2]; // input: 4 x 2D points + quad[0] = src_rect.x(); + quad[1] = src_rect.y(); + quad[2] = src_rect.right(); + quad[3] = src_rect.y(); + quad[4] = src_rect.right(); + quad[5] = src_rect.bottom(); + quad[6] = src_rect.x(); + quad[7] = src_rect.bottom(); + + double result[4 * 4]; // output: 4 x 4D homogeneous points + transform.matrix().map2(quad, 4, result); + + HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); + HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); + HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]); + HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]); + return ComputeEnclosingClippedRect(hc0, hc1, hc2, hc3); } -gfx::RectF MathUtil::projectClippedRect(const gfx::Transform& transform, const gfx::RectF& srcRect) -{ - if (transform.IsIdentityOrTranslation()) - return srcRect + gfx::Vector2dF(static_cast<float>(transform.matrix().getDouble(0, 3)), static_cast<float>(transform.matrix().getDouble(1, 3))); - - // Perform the projection, but retain the result in homogeneous coordinates. - gfx::QuadF q = gfx::QuadF(srcRect); - HomogeneousCoordinate h1 = projectHomogeneousPoint(transform, q.p1()); - HomogeneousCoordinate h2 = projectHomogeneousPoint(transform, q.p2()); - HomogeneousCoordinate h3 = projectHomogeneousPoint(transform, q.p3()); - HomogeneousCoordinate h4 = projectHomogeneousPoint(transform, q.p4()); - - return computeEnclosingClippedRect(h1, h2, h3, h4); +gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform, + const gfx::RectF& src_rect) { + if (transform.IsIdentityOrTranslation()) { + return src_rect + + gfx::Vector2dF( + static_cast<float>(transform.matrix().getDouble(0, 3)), + static_cast<float>(transform.matrix().getDouble(1, 3))); + } + + // Perform the projection, but retain the result in homogeneous coordinates. + gfx::QuadF q = gfx::QuadF(src_rect); + HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1()); + HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2()); + HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3()); + HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4()); + + return ComputeEnclosingClippedRect(h1, h2, h3, h4); } -void MathUtil::mapClippedQuad(const gfx::Transform& transform, const gfx::QuadF& srcQuad, gfx::PointF clippedQuad[8], int& numVerticesInClippedQuad) -{ - HomogeneousCoordinate h1 = mapHomogeneousPoint(transform, gfx::Point3F(srcQuad.p1())); - HomogeneousCoordinate h2 = mapHomogeneousPoint(transform, gfx::Point3F(srcQuad.p2())); - HomogeneousCoordinate h3 = mapHomogeneousPoint(transform, gfx::Point3F(srcQuad.p3())); - HomogeneousCoordinate h4 = mapHomogeneousPoint(transform, gfx::Point3F(srcQuad.p4())); - - // The order of adding the vertices to the array is chosen so that clockwise / counter-clockwise orientation is retained. - - numVerticesInClippedQuad = 0; - - if (!h1.shouldBeClipped()) - addVertexToClippedQuad(h1.cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (h1.shouldBeClipped() ^ h2.shouldBeClipped()) - addVertexToClippedQuad(computeClippedPointForEdge(h1, h2).cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (!h2.shouldBeClipped()) - addVertexToClippedQuad(h2.cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (h2.shouldBeClipped() ^ h3.shouldBeClipped()) - addVertexToClippedQuad(computeClippedPointForEdge(h2, h3).cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (!h3.shouldBeClipped()) - addVertexToClippedQuad(h3.cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (h3.shouldBeClipped() ^ h4.shouldBeClipped()) - addVertexToClippedQuad(computeClippedPointForEdge(h3, h4).cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (!h4.shouldBeClipped()) - addVertexToClippedQuad(h4.cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - if (h4.shouldBeClipped() ^ h1.shouldBeClipped()) - addVertexToClippedQuad(computeClippedPointForEdge(h4, h1).cartesianPoint2d(), clippedQuad, numVerticesInClippedQuad); - - DCHECK(numVerticesInClippedQuad <= 8); +void MathUtil::MapClippedQuad(const gfx::Transform& transform, + const gfx::QuadF& src_quad, + gfx::PointF clipped_quad[8], + int& num_vertices_in_clipped_quad) { + HomogeneousCoordinate h1 = + MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); + HomogeneousCoordinate h2 = + MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); + HomogeneousCoordinate h3 = + MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); + HomogeneousCoordinate h4 = + MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4())); + + // The order of adding the vertices to the array is chosen so that + // clockwise / counter-clockwise orientation is retained. + + num_vertices_in_clipped_quad = 0; + + if (!h1.ShouldBeClipped()) { + AddVertexToClippedQuad( + h1.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); + } + + if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped()) { + AddVertexToClippedQuad( + ComputeClippedPointForEdge(h1, h2).CartesianPoint2d(), + clipped_quad, + num_vertices_in_clipped_quad); + } + + if (!h2.ShouldBeClipped()) { + AddVertexToClippedQuad( + h2.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); + } + + if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped()) { + AddVertexToClippedQuad( + ComputeClippedPointForEdge(h2, h3).CartesianPoint2d(), + clipped_quad, + num_vertices_in_clipped_quad); + } + + if (!h3.ShouldBeClipped()) { + AddVertexToClippedQuad( + h3.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); + } + + if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped()) { + AddVertexToClippedQuad( + ComputeClippedPointForEdge(h3, h4).CartesianPoint2d(), + clipped_quad, + num_vertices_in_clipped_quad); + } + + if (!h4.ShouldBeClipped()) { + AddVertexToClippedQuad( + h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); + } + + if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { + AddVertexToClippedQuad( + ComputeClippedPointForEdge(h4, h1).CartesianPoint2d(), + clipped_quad, + num_vertices_in_clipped_quad); + } + + DCHECK_LE(num_vertices_in_clipped_quad, 8); } -gfx::RectF MathUtil::computeEnclosingRectOfVertices(gfx::PointF vertices[], int numVertices) -{ - if (numVertices < 2) - return gfx::RectF(); +gfx::RectF MathUtil::ComputeEnclosingRectOfVertices(gfx::PointF vertices[], + int num_vertices) { + if (num_vertices < 2) + return gfx::RectF(); - float xmin = std::numeric_limits<float>::max(); - float xmax = -std::numeric_limits<float>::max(); - float ymin = std::numeric_limits<float>::max(); - float ymax = -std::numeric_limits<float>::max(); + float xmin = std::numeric_limits<float>::max(); + float xmax = -std::numeric_limits<float>::max(); + float ymin = std::numeric_limits<float>::max(); + float ymax = -std::numeric_limits<float>::max(); - for (int i = 0; i < numVertices; ++i) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, vertices[i]); + for (int i = 0; i < num_vertices; ++i) + ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, vertices[i]); - return gfx::RectF(gfx::PointF(xmin, ymin), gfx::SizeF(xmax - xmin, ymax - ymin)); + return gfx::RectF(gfx::PointF(xmin, ymin), + gfx::SizeF(xmax - xmin, ymax - ymin)); } -gfx::RectF MathUtil::computeEnclosingClippedRect(const HomogeneousCoordinate& h1, const HomogeneousCoordinate& h2, const HomogeneousCoordinate& h3, const HomogeneousCoordinate& h4) -{ - // This function performs clipping as necessary and computes the enclosing 2d - // gfx::RectF of the vertices. Doing these two steps simultaneously allows us to avoid - // the overhead of storing an unknown number of clipped vertices. - - // If no vertices on the quad are clipped, then we can simply return the enclosing rect directly. - bool somethingClipped = h1.shouldBeClipped() || h2.shouldBeClipped() || h3.shouldBeClipped() || h4.shouldBeClipped(); - if (!somethingClipped) { - gfx::QuadF mappedQuad = gfx::QuadF(h1.cartesianPoint2d(), h2.cartesianPoint2d(), h3.cartesianPoint2d(), h4.cartesianPoint2d()); - return mappedQuad.BoundingBox(); - } - - bool everythingClipped = h1.shouldBeClipped() && h2.shouldBeClipped() && h3.shouldBeClipped() && h4.shouldBeClipped(); - if (everythingClipped) - return gfx::RectF(); - - - float xmin = std::numeric_limits<float>::max(); - float xmax = -std::numeric_limits<float>::max(); - float ymin = std::numeric_limits<float>::max(); - float ymax = -std::numeric_limits<float>::max(); - - if (!h1.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, h1.cartesianPoint2d()); - - if (h1.shouldBeClipped() ^ h2.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, computeClippedPointForEdge(h1, h2).cartesianPoint2d()); - - if (!h2.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, h2.cartesianPoint2d()); - - if (h2.shouldBeClipped() ^ h3.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, computeClippedPointForEdge(h2, h3).cartesianPoint2d()); - - if (!h3.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, h3.cartesianPoint2d()); - - if (h3.shouldBeClipped() ^ h4.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, computeClippedPointForEdge(h3, h4).cartesianPoint2d()); - - if (!h4.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, h4.cartesianPoint2d()); - - if (h4.shouldBeClipped() ^ h1.shouldBeClipped()) - expandBoundsToIncludePoint(xmin, xmax, ymin, ymax, computeClippedPointForEdge(h4, h1).cartesianPoint2d()); - - return gfx::RectF(gfx::PointF(xmin, ymin), gfx::SizeF(xmax - xmin, ymax - ymin)); +gfx::RectF MathUtil::ComputeEnclosingClippedRect( + const HomogeneousCoordinate& h1, + const HomogeneousCoordinate& h2, + const HomogeneousCoordinate& h3, + const HomogeneousCoordinate& h4) { + // This function performs clipping as necessary and computes the enclosing 2d + // gfx::RectF of the vertices. Doing these two steps simultaneously allows us + // to avoid the overhead of storing an unknown number of clipped vertices. + + // If no vertices on the quad are clipped, then we can simply return the + // enclosing rect directly. + bool something_clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() || + h3.ShouldBeClipped() || h4.ShouldBeClipped(); + if (!something_clipped) { + gfx::QuadF mapped_quad = gfx::QuadF(h1.CartesianPoint2d(), + h2.CartesianPoint2d(), + h3.CartesianPoint2d(), + h4.CartesianPoint2d()); + return mapped_quad.BoundingBox(); + } + + bool everything_clipped = h1.ShouldBeClipped() && h2.ShouldBeClipped() && + h3.ShouldBeClipped() && h4.ShouldBeClipped(); + if (everything_clipped) + return gfx::RectF(); + + float xmin = std::numeric_limits<float>::max(); + float xmax = -std::numeric_limits<float>::max(); + float ymin = std::numeric_limits<float>::max(); + float ymax = -std::numeric_limits<float>::max(); + + if (!h1.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, + h1.CartesianPoint2d()); + + if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, + &xmax, + &ymin, + &ymax, + ComputeClippedPointForEdge(h1, h2) + .CartesianPoint2d()); + + if (!h2.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, + h2.CartesianPoint2d()); + + if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, + &xmax, + &ymin, + &ymax, + ComputeClippedPointForEdge(h2, h3) + .CartesianPoint2d()); + + if (!h3.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, + h3.CartesianPoint2d()); + + if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, + &xmax, + &ymin, + &ymax, + ComputeClippedPointForEdge(h3, h4) + .CartesianPoint2d()); + + if (!h4.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, + h4.CartesianPoint2d()); + + if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) + ExpandBoundsToIncludePoint(&xmin, + &xmax, + &ymin, + &ymax, + ComputeClippedPointForEdge(h4, h1) + .CartesianPoint2d()); + + return gfx::RectF(gfx::PointF(xmin, ymin), + gfx::SizeF(xmax - xmin, ymax - ymin)); } -gfx::QuadF MathUtil::mapQuad(const gfx::Transform& transform, const gfx::QuadF& q, bool& clipped) -{ - if (transform.IsIdentityOrTranslation()) { - gfx::QuadF mappedQuad(q); - mappedQuad += gfx::Vector2dF(static_cast<float>(transform.matrix().getDouble(0, 3)), static_cast<float>(transform.matrix().getDouble(1, 3))); - clipped = false; - return mappedQuad; - } - - HomogeneousCoordinate h1 = mapHomogeneousPoint(transform, gfx::Point3F(q.p1())); - HomogeneousCoordinate h2 = mapHomogeneousPoint(transform, gfx::Point3F(q.p2())); - HomogeneousCoordinate h3 = mapHomogeneousPoint(transform, gfx::Point3F(q.p3())); - HomogeneousCoordinate h4 = mapHomogeneousPoint(transform, gfx::Point3F(q.p4())); - - clipped = h1.shouldBeClipped() || h2.shouldBeClipped() || h3.shouldBeClipped() || h4.shouldBeClipped(); - - // Result will be invalid if clipped == true. But, compute it anyway just in case, to emulate existing behavior. - return gfx::QuadF(h1.cartesianPoint2d(), h2.cartesianPoint2d(), h3.cartesianPoint2d(), h4.cartesianPoint2d()); +gfx::QuadF MathUtil::MapQuad(const gfx::Transform& transform, + const gfx::QuadF& q, + bool* clipped) { + if (transform.IsIdentityOrTranslation()) { + gfx::QuadF mapped_quad(q); + mapped_quad += + gfx::Vector2dF(static_cast<float>(transform.matrix().getDouble(0, 3)), + static_cast<float>(transform.matrix().getDouble(1, 3))); + *clipped = false; + return mapped_quad; + } + + HomogeneousCoordinate h1 = + MapHomogeneousPoint(transform, gfx::Point3F(q.p1())); + HomogeneousCoordinate h2 = + MapHomogeneousPoint(transform, gfx::Point3F(q.p2())); + HomogeneousCoordinate h3 = + MapHomogeneousPoint(transform, gfx::Point3F(q.p3())); + HomogeneousCoordinate h4 = + MapHomogeneousPoint(transform, gfx::Point3F(q.p4())); + + *clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() || + h3.ShouldBeClipped() || h4.ShouldBeClipped(); + + // Result will be invalid if clipped == true. But, compute it anyway just in + // case, to emulate existing behavior. + return gfx::QuadF(h1.CartesianPoint2d(), + h2.CartesianPoint2d(), + h3.CartesianPoint2d(), + h4.CartesianPoint2d()); } -gfx::PointF MathUtil::mapPoint(const gfx::Transform& transform, const gfx::PointF& p, bool& clipped) -{ - HomogeneousCoordinate h = mapHomogeneousPoint(transform, gfx::Point3F(p)); +gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform, + gfx::PointF p, + bool* clipped) { + HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p)); - if (h.w > 0) { - clipped = false; - return h.cartesianPoint2d(); - } + if (h.w > 0) { + *clipped = false; + return h.CartesianPoint2d(); + } - // The cartesian coordinates will be invalid after dividing by w. - clipped = true; + // The cartesian coordinates will be invalid after dividing by w. + *clipped = true; - // Avoid dividing by w if w == 0. - if (!h.w) - return gfx::PointF(); + // Avoid dividing by w if w == 0. + if (!h.w) + return gfx::PointF(); - // This return value will be invalid because clipped == true, but (1) users of this - // code should be ignoring the return value when clipped == true anyway, and (2) this - // behavior is more consistent with existing behavior of WebKit transforms if the user - // really does not ignore the return value. - return h.cartesianPoint2d(); + // This return value will be invalid because clipped == true, but (1) users of + // this code should be ignoring the return value when clipped == true anyway, + // and (2) this behavior is more consistent with existing behavior of WebKit + // transforms if the user really does not ignore the return value. + return h.CartesianPoint2d(); } -gfx::Point3F MathUtil::mapPoint(const gfx::Transform& transform, const gfx::Point3F& p, bool& clipped) -{ - HomogeneousCoordinate h = mapHomogeneousPoint(transform, p); +gfx::Point3F MathUtil::MapPoint(const gfx::Transform& transform, + const gfx::Point3F& p, + bool* clipped) { + HomogeneousCoordinate h = MapHomogeneousPoint(transform, p); - if (h.w > 0) { - clipped = false; - return h.cartesianPoint3d(); - } + if (h.w > 0) { + *clipped = false; + return h.CartesianPoint3d(); + } - // The cartesian coordinates will be invalid after dividing by w. - clipped = true; + // The cartesian coordinates will be invalid after dividing by w. + *clipped = true; - // Avoid dividing by w if w == 0. - if (!h.w) - return gfx::Point3F(); + // Avoid dividing by w if w == 0. + if (!h.w) + return gfx::Point3F(); - // This return value will be invalid because clipped == true, but (1) users of this - // code should be ignoring the return value when clipped == true anyway, and (2) this - // behavior is more consistent with existing behavior of WebKit transforms if the user - // really does not ignore the return value. - return h.cartesianPoint3d(); + // This return value will be invalid because clipped == true, but (1) users of + // this code should be ignoring the return value when clipped == true anyway, + // and (2) this behavior is more consistent with existing behavior of WebKit + // transforms if the user really does not ignore the return value. + return h.CartesianPoint3d(); } -gfx::QuadF MathUtil::projectQuad(const gfx::Transform& transform, const gfx::QuadF& q, bool& clipped) -{ - gfx::QuadF projectedQuad; - bool clippedPoint; - projectedQuad.set_p1(projectPoint(transform, q.p1(), clippedPoint)); - clipped = clippedPoint; - projectedQuad.set_p2(projectPoint(transform, q.p2(), clippedPoint)); - clipped |= clippedPoint; - projectedQuad.set_p3(projectPoint(transform, q.p3(), clippedPoint)); - clipped |= clippedPoint; - projectedQuad.set_p4(projectPoint(transform, q.p4(), clippedPoint)); - clipped |= clippedPoint; - - return projectedQuad; +gfx::QuadF MathUtil::ProjectQuad(const gfx::Transform& transform, + const gfx::QuadF& q, + bool* clipped) { + gfx::QuadF projected_quad; + bool clipped_point; + projected_quad.set_p1(ProjectPoint(transform, q.p1(), &clipped_point)); + *clipped = clipped_point; + projected_quad.set_p2(ProjectPoint(transform, q.p2(), &clipped_point)); + *clipped |= clipped_point; + projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point)); + *clipped |= clipped_point; + projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point)); + *clipped |= clipped_point; + + return projected_quad; } -gfx::PointF MathUtil::projectPoint(const gfx::Transform& transform, const gfx::PointF& p, bool& clipped) -{ - HomogeneousCoordinate h = projectHomogeneousPoint(transform, p); - - if (h.w > 0) { - // The cartesian coordinates will be valid in this case. - clipped = false; - return h.cartesianPoint2d(); - } - - // The cartesian coordinates will be invalid after dividing by w. - clipped = true; - - // Avoid dividing by w if w == 0. - if (!h.w) - return gfx::PointF(); - - // This return value will be invalid because clipped == true, but (1) users of this - // code should be ignoring the return value when clipped == true anyway, and (2) this - // behavior is more consistent with existing behavior of WebKit transforms if the user - // really does not ignore the return value. - return h.cartesianPoint2d(); +gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform, + gfx::PointF p, + bool* clipped) { + HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); + + if (h.w > 0) { + // The cartesian coordinates will be valid in this case. + *clipped = false; + return h.CartesianPoint2d(); + } + + // The cartesian coordinates will be invalid after dividing by w. + *clipped = true; + + // Avoid dividing by w if w == 0. + if (!h.w) + return gfx::PointF(); + + // This return value will be invalid because clipped == true, but (1) users of + // this code should be ignoring the return value when clipped == true anyway, + // and (2) this behavior is more consistent with existing behavior of WebKit + // transforms if the user really does not ignore the return value. + return h.CartesianPoint2d(); } -static inline float scaleOnAxis(double a, double b, double c) -{ - return std::sqrt(a * a + b * b + c * c); +static inline float ScaleOnAxis(double a, double b, double c) { + return std::sqrt(a * a + b * b + c * c); } -gfx::Vector2dF MathUtil::computeTransform2dScaleComponents(const gfx::Transform& transform, float fallbackValue) -{ - if (transform.HasPerspective()) - return gfx::Vector2dF(fallbackValue, fallbackValue); - float xScale = scaleOnAxis(transform.matrix().getDouble(0, 0), transform.matrix().getDouble(1, 0), transform.matrix().getDouble(2, 0)); - float yScale = scaleOnAxis(transform.matrix().getDouble(0, 1), transform.matrix().getDouble(1, 1), transform.matrix().getDouble(2, 1)); - return gfx::Vector2dF(xScale, yScale); +gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents( + const gfx::Transform& transform, + float fallback_value) { + if (transform.HasPerspective()) + return gfx::Vector2dF(fallback_value, fallback_value); + float x_scale = ScaleOnAxis(transform.matrix().getDouble(0, 0), + transform.matrix().getDouble(1, 0), + transform.matrix().getDouble(2, 0)); + float y_scale = ScaleOnAxis(transform.matrix().getDouble(0, 1), + transform.matrix().getDouble(1, 1), + transform.matrix().getDouble(2, 1)); + return gfx::Vector2dF(x_scale, y_scale); } -float MathUtil::smallestAngleBetweenVectors(gfx::Vector2dF v1, gfx::Vector2dF v2) -{ - double dotProduct = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length(); - // Clamp to compensate for rounding errors. - dotProduct = std::max(-1.0, std::min(1.0, dotProduct)); - return static_cast<float>(Rad2Deg(std::acos(dotProduct))); +float MathUtil::SmallestAngleBetweenVectors(gfx::Vector2dF v1, + gfx::Vector2dF v2) { + double dot_product = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length(); + // Clamp to compensate for rounding errors. + dot_product = std::max(-1.0, std::min(1.0, dot_product)); + return static_cast<float>(Rad2Deg(std::acos(dot_product))); } -gfx::Vector2dF MathUtil::projectVector(gfx::Vector2dF source, gfx::Vector2dF destination) -{ - float projectedLength = gfx::DotProduct(source, destination) / destination.LengthSquared(); - return gfx::Vector2dF(projectedLength * destination.x(), projectedLength * destination.y()); +gfx::Vector2dF MathUtil::ProjectVector(gfx::Vector2dF source, + gfx::Vector2dF destination) { + float projected_length = + gfx::DotProduct(source, destination) / destination.LengthSquared(); + return gfx::Vector2dF(projected_length * destination.x(), + projected_length * destination.y()); } -scoped_ptr<base::Value> MathUtil::asValue(gfx::Size s) { +scoped_ptr<base::Value> MathUtil::AsValue(gfx::Size s) { scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); res->SetDouble("width", s.width()); res->SetDouble("height", s.height()); return res.PassAs<base::Value>(); } -scoped_ptr<base::Value> MathUtil::asValue(gfx::PointF pt) { +scoped_ptr<base::Value> MathUtil::AsValue(gfx::PointF pt) { scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); res->SetDouble("x", pt.x()); res->SetDouble("y", pt.y()); return res.PassAs<base::Value>(); } -scoped_ptr<base::Value> MathUtil::asValue(gfx::QuadF q) { +scoped_ptr<base::Value> MathUtil::AsValue(gfx::QuadF q) { scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); - res->Set("p1", asValue(q.p1()).release()); - res->Set("p2", asValue(q.p2()).release()); - res->Set("p3", asValue(q.p3()).release()); - res->Set("p4", asValue(q.p4()).release()); + res->Set("p1", AsValue(q.p1()).release()); + res->Set("p2", AsValue(q.p2()).release()); + res->Set("p3", AsValue(q.p3()).release()); + res->Set("p4", AsValue(q.p4()).release()); return res.PassAs<base::Value>(); } -scoped_ptr<base::Value> MathUtil::asValueSafely(double value) { +scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) { return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( - std::min(value, std::numeric_limits<double>::max()))); + std::min(value, std::numeric_limits<double>::max()))); } -scoped_ptr<base::Value> MathUtil::asValueSafely(float value) { +scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( - std::min(value, std::numeric_limits<float>::max()))); + std::min(value, std::numeric_limits<float>::max()))); } } // namespace cc diff --git a/cc/math_util.h b/cc/math_util.h index 24a155d..d170b26 100644 --- a/cc/math_util.h +++ b/cc/math_util.h @@ -15,9 +15,7 @@ #include "ui/gfx/size.h" #include "ui/gfx/transform.h" -namespace base { -class Value; -} +namespace base { class Value; } namespace gfx { class QuadF; @@ -30,112 +28,133 @@ class Vector2dF; namespace cc { struct HomogeneousCoordinate { - HomogeneousCoordinate(double newX, double newY, double newZ, double newW) - : x(newX) - , y(newY) - , z(newZ) - , w(newW) - { - } - - bool shouldBeClipped() const - { - return w <= 0; - } - - gfx::PointF cartesianPoint2d() const - { - if (w == 1) - return gfx::PointF(x, y); - - // For now, because this code is used privately only by MathUtil, it should never be called when w == 0, and we do not yet need to handle that case. - DCHECK(w); - double invW = 1.0 / w; - return gfx::PointF(x * invW, y * invW); - } - - gfx::Point3F cartesianPoint3d() const - { - if (w == 1) - return gfx::Point3F(x, y, z); - - // For now, because this code is used privately only by MathUtil, it should never be called when w == 0, and we do not yet need to handle that case. - DCHECK(w); - double invW = 1.0 / w; - return gfx::Point3F(x * invW, y * invW, z * invW); - } - - double x; - double y; - double z; - double w; + HomogeneousCoordinate(double new_x, double new_y, double new_z, double new_w) + : x(new_x), y(new_y), z(new_z), w(new_w) {} + + bool ShouldBeClipped() const { return w <= 0.0; } + + gfx::PointF CartesianPoint2d() const { + if (w == 1.0) + return gfx::PointF(x, y); + + // For now, because this code is used privately only by MathUtil, it should + // never be called when w == 0, and we do not yet need to handle that case. + DCHECK(w); + double inv_w = 1.0 / w; + return gfx::PointF(x * inv_w, y * inv_w); + } + + gfx::Point3F CartesianPoint3d() const { + if (w == 1) + return gfx::Point3F(x, y, z); + + // For now, because this code is used privately only by MathUtil, it should + // never be called when w == 0, and we do not yet need to handle that case. + DCHECK(w); + double inv_w = 1.0 / w; + return gfx::Point3F(x * inv_w, y * inv_w, z * inv_w); + } + + double x; + double y; + double z; + double w; }; class CC_EXPORT MathUtil { -public: - static const double PI_DOUBLE; - static const float PI_FLOAT; - static const double EPSILON; - - static double Deg2Rad(double deg) { return deg * PI_DOUBLE / 180; } - static double Rad2Deg(double rad) { return rad * 180 / PI_DOUBLE; } - - static float Deg2Rad(float deg) { return deg * PI_FLOAT / 180; } - static float Rad2Deg(float rad) { return rad * 180 / PI_FLOAT; } - - static float Round(float f) { return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); } - static double Round(double d) { return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); } - - // Background: Existing transform code does not do the right thing in - // mapRect / mapQuad / projectQuad when there is a perspective projection that causes - // one of the transformed vertices to go to w < 0. In those cases, it is necessary to - // perform clipping in homogeneous coordinates, after applying the transform, before - // dividing-by-w to convert to cartesian coordinates. - // - // These functions return the axis-aligned rect that encloses the correctly clipped, - // transformed polygon. - static gfx::Rect mapClippedRect(const gfx::Transform&, const gfx::Rect&); - static gfx::RectF mapClippedRect(const gfx::Transform&, const gfx::RectF&); - static gfx::RectF projectClippedRect(const gfx::Transform&, const gfx::RectF&); - - // Returns an array of vertices that represent the clipped polygon. After returning, indexes from - // 0 to numVerticesInClippedQuad are valid in the clippedQuad array. Note that - // numVerticesInClippedQuad may be zero, which means the entire quad was clipped, and - // none of the vertices in the array are valid. - static void mapClippedQuad(const gfx::Transform&, const gfx::QuadF& srcQuad, gfx::PointF clippedQuad[8], int& numVerticesInClippedQuad); - - static gfx::RectF computeEnclosingRectOfVertices(gfx::PointF vertices[], int numVertices); - static gfx::RectF computeEnclosingClippedRect(const HomogeneousCoordinate& h1, const HomogeneousCoordinate& h2, const HomogeneousCoordinate& h3, const HomogeneousCoordinate& h4); - - // NOTE: These functions do not do correct clipping against w = 0 plane, but they - // correctly detect the clipped condition via the boolean clipped. - static gfx::QuadF mapQuad(const gfx::Transform&, const gfx::QuadF&, bool& clipped); - static gfx::PointF mapPoint(const gfx::Transform&, const gfx::PointF&, bool& clipped); - static gfx::Point3F mapPoint(const gfx::Transform&, const gfx::Point3F&, bool& clipped); - static gfx::QuadF projectQuad(const gfx::Transform&, const gfx::QuadF&, bool& clipped); - static gfx::PointF projectPoint(const gfx::Transform&, const gfx::PointF&, bool& clipped); - - static gfx::Vector2dF computeTransform2dScaleComponents(const gfx::Transform&, float fallbackValue); - - // Returns the smallest angle between the given two vectors in degrees. Neither vector is - // assumed to be normalized. - static float smallestAngleBetweenVectors(gfx::Vector2dF, gfx::Vector2dF); - - // Projects the |source| vector onto |destination|. Neither vector is assumed to be normalized. - static gfx::Vector2dF projectVector(gfx::Vector2dF source, gfx::Vector2dF destination); - - // Conversion to value. - static scoped_ptr<base::Value> asValue(gfx::Size s); - static scoped_ptr<base::Value> asValue(gfx::PointF q); - static scoped_ptr<base::Value> asValue(gfx::QuadF q); - - // Returns a base::Value representation of the floating point value. - // If the value is inf, returns max double/float representation. - static scoped_ptr<base::Value> asValueSafely(double value); - static scoped_ptr<base::Value> asValueSafely(float value); + public: + static const double kPiDouble; + static const float kPiFloat; + + static double Deg2Rad(double deg) { return deg * kPiDouble / 180.0; } + static double Rad2Deg(double rad) { return rad * 180.0 / kPiDouble; } + + static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } + static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } + + static float Round(float f) { + return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); + } + static double Round(double d) { + return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); + } + + // Background: Existing transform code does not do the right thing in + // mapRect / mapQuad / projectQuad when there is a perspective projection that + // causes one of the transformed vertices to go to w < 0. In those cases, it + // is necessary to perform clipping in homogeneous coordinates, after applying + // the transform, before dividing-by-w to convert to cartesian coordinates. + // + // These functions return the axis-aligned rect that encloses the correctly + // clipped, transformed polygon. + static gfx::Rect MapClippedRect(const gfx::Transform& transform, + gfx::Rect rect); + static gfx::RectF MapClippedRect(const gfx::Transform& transform, + const gfx::RectF& rect); + static gfx::RectF ProjectClippedRect(const gfx::Transform& transform, + const gfx::RectF& rect); + + // Returns an array of vertices that represent the clipped polygon. After + // returning, indexes from 0 to numVerticesInClippedQuad are valid in the + // clippedQuad array. Note that numVerticesInClippedQuad may be zero, which + // means the entire quad was clipped, and none of the vertices in the array + // are valid. + static void MapClippedQuad(const gfx::Transform& transform, + const gfx::QuadF& src_quad, + gfx::PointF clippedQuad[8], + int& numVerticesInClippedQuad); + + static gfx::RectF ComputeEnclosingRectOfVertices(gfx::PointF vertices[], + int num_vertices); + static gfx::RectF ComputeEnclosingClippedRect( + const HomogeneousCoordinate& h1, + const HomogeneousCoordinate& h2, + const HomogeneousCoordinate& h3, + const HomogeneousCoordinate& h4); + + // NOTE: These functions do not do correct clipping against w = 0 plane, but + // they correctly detect the clipped condition via the boolean clipped. + static gfx::QuadF MapQuad(const gfx::Transform& transform, + const gfx::QuadF& quad, + bool* clipped); + static gfx::PointF MapPoint(const gfx::Transform& transform, + gfx::PointF point, + bool* clipped); + static gfx::Point3F MapPoint(const gfx::Transform&, + const gfx::Point3F&, + bool* clipped); + static gfx::QuadF ProjectQuad(const gfx::Transform& transform, + const gfx::QuadF& quad, + bool* clipped); + static gfx::PointF ProjectPoint(const gfx::Transform& transform, + gfx::PointF point, + bool* clipped); + + static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&, + float fallbackValue); + + // Returns the smallest angle between the given two vectors in degrees. + // Neither vector is assumed to be normalized. + static float SmallestAngleBetweenVectors(gfx::Vector2dF v1, + gfx::Vector2dF v2); + + // Projects the |source| vector onto |destination|. Neither vector is assumed + // to be normalized. + static gfx::Vector2dF ProjectVector(gfx::Vector2dF source, + gfx::Vector2dF destination); + + // Conversion to value. + static scoped_ptr<base::Value> AsValue(gfx::Size s); + static scoped_ptr<base::Value> AsValue(gfx::PointF q); + static scoped_ptr<base::Value> AsValue(gfx::QuadF q); + + // Returns a base::Value representation of the floating point value. + // If the value is inf, returns max double/float representation. + static scoped_ptr<base::Value> AsValueSafely(double value); + static scoped_ptr<base::Value> AsValueSafely(float value); }; -} // namespace cc +} // namespace cc #endif // CC_MATH_UTIL_H_ diff --git a/cc/math_util_unittest.cc b/cc/math_util_unittest.cc index d00603b..1430a88 100644 --- a/cc/math_util_unittest.cc +++ b/cc/math_util_unittest.cc @@ -16,102 +16,106 @@ namespace cc { namespace { -TEST(MathUtilTest, verifyProjectionOfPerpendicularPlane) -{ - // In this case, the m33() element of the transform becomes zero, which could cause a - // divide-by-zero when projecting points/quads. +TEST(MathUtilTest, ProjectionOfPerpendicularPlane) { + // In this case, the m33() element of the transform becomes zero, which could + // cause a divide-by-zero when projecting points/quads. - gfx::Transform transform; - transform.MakeIdentity(); - transform.matrix().setDouble(2, 2, 0); + gfx::Transform transform; + transform.MakeIdentity(); + transform.matrix().setDouble(2, 2, 0); - gfx::RectF rect = gfx::RectF(0, 0, 1, 1); - gfx::RectF projectedRect = MathUtil::projectClippedRect(transform, rect); + gfx::RectF rect = gfx::RectF(0, 0, 1, 1); + gfx::RectF projected_rect = MathUtil::ProjectClippedRect(transform, rect); - EXPECT_EQ(0, projectedRect.x()); - EXPECT_EQ(0, projectedRect.y()); - EXPECT_TRUE(projectedRect.IsEmpty()); + EXPECT_EQ(0, projected_rect.x()); + EXPECT_EQ(0, projected_rect.y()); + EXPECT_TRUE(projected_rect.IsEmpty()); } -TEST(MathUtilTest, verifyEnclosingClippedRectUsesCorrectInitialBounds) -{ - HomogeneousCoordinate h1(-100, -100, 0, 1); - HomogeneousCoordinate h2(-10, -10, 0, 1); - HomogeneousCoordinate h3(10, 10, 0, -1); - HomogeneousCoordinate h4(100, 100, 0, -1); +TEST(MathUtilTest, EnclosingClippedRectUsesCorrectInitialBounds) { + HomogeneousCoordinate h1(-100, -100, 0, 1); + HomogeneousCoordinate h2(-10, -10, 0, 1); + HomogeneousCoordinate h3(10, 10, 0, -1); + HomogeneousCoordinate h4(100, 100, 0, -1); - // The bounds of the enclosing clipped rect should be -100 to -10 for both x and y. - // However, if there is a bug where the initial xmin/xmax/ymin/ymax are initialized to - // numeric_limits<float>::min() (which is zero, not -flt_max) then the enclosing - // clipped rect will be computed incorrectly. - gfx::RectF result = MathUtil::computeEnclosingClippedRect(h1, h2, h3, h4); + // The bounds of the enclosing clipped rect should be -100 to -10 for both x + // and y. However, if there is a bug where the initial xmin/xmax/ymin/ymax are + // initialized to numeric_limits<float>::min() (which is zero, not -flt_max) + // then the enclosing clipped rect will be computed incorrectly. + gfx::RectF result = MathUtil::ComputeEnclosingClippedRect(h1, h2, h3, h4); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), result); + EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), + result); } -TEST(MathUtilTest, verifyEnclosingRectOfVerticesUsesCorrectInitialBounds) -{ - gfx::PointF vertices[3]; - int numVertices = 3; +TEST(MathUtilTest, EnclosingRectOfVerticesUsesCorrectInitialBounds) { + gfx::PointF vertices[3]; + int num_vertices = 3; - vertices[0] = gfx::PointF(-10, -100); - vertices[1] = gfx::PointF(-100, -10); - vertices[2] = gfx::PointF(-30, -30); + vertices[0] = gfx::PointF(-10, -100); + vertices[1] = gfx::PointF(-100, -10); + vertices[2] = gfx::PointF(-30, -30); - // The bounds of the enclosing rect should be -100 to -10 for both x and y. However, - // if there is a bug where the initial xmin/xmax/ymin/ymax are initialized to - // numeric_limits<float>::min() (which is zero, not -flt_max) then the enclosing - // clipped rect will be computed incorrectly. - gfx::RectF result = MathUtil::computeEnclosingRectOfVertices(vertices, numVertices); + // The bounds of the enclosing rect should be -100 to -10 for both x and y. + // However, if there is a bug where the initial xmin/xmax/ymin/ymax are + // initialized to numeric_limits<float>::min() (which is zero, not -flt_max) + // then the enclosing clipped rect will be computed incorrectly. + gfx::RectF result = + MathUtil::ComputeEnclosingRectOfVertices(vertices, num_vertices); - EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), result); + EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), + result); } -TEST(MathUtilTest, smallestAngleBetweenVectors) -{ - gfx::Vector2dF x(1, 0); - gfx::Vector2dF y(0, 1); - gfx::Vector2dF testVector(0.5, 0.5); - - // Orthogonal vectors are at an angle of 90 degress. - EXPECT_EQ(90, MathUtil::smallestAngleBetweenVectors(x, y)); - - // A vector makes a zero angle with itself. - EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(x, x)); - EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(y, y)); - EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(testVector, testVector)); - - // Parallel but reversed vectors are at 180 degrees. - EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(x, -x)); - EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(y, -y)); - EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(testVector, -testVector)); - - // The test vector is at a known angle. - EXPECT_FLOAT_EQ(45, std::floor(MathUtil::smallestAngleBetweenVectors(testVector, x))); - EXPECT_FLOAT_EQ(45, std::floor(MathUtil::smallestAngleBetweenVectors(testVector, y))); +TEST(MathUtilTest, SmallestAngleBetweenVectors) { + gfx::Vector2dF x(1, 0); + gfx::Vector2dF y(0, 1); + gfx::Vector2dF test_vector(0.5, 0.5); + + // Orthogonal vectors are at an angle of 90 degress. + EXPECT_EQ(90, MathUtil::SmallestAngleBetweenVectors(x, y)); + + // A vector makes a zero angle with itself. + EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(x, x)); + EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(y, y)); + EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(test_vector, test_vector)); + + // Parallel but reversed vectors are at 180 degrees. + EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(x, -x)); + EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(y, -y)); + EXPECT_FLOAT_EQ( + 180, MathUtil::SmallestAngleBetweenVectors(test_vector, -test_vector)); + + // The test vector is at a known angle. + EXPECT_FLOAT_EQ( + 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, x))); + EXPECT_FLOAT_EQ( + 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, y))); } -TEST(MathUtilTest, vectorProjection) -{ - gfx::Vector2dF x(1, 0); - gfx::Vector2dF y(0, 1); - gfx::Vector2dF testVector(0.3f, 0.7f); - - // Orthogonal vectors project to a zero vector. - EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::projectVector(x, y)); - EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::projectVector(y, x)); - - // Projecting a vector onto the orthonormal basis gives the corresponding component of the - // vector. - EXPECT_VECTOR_EQ(gfx::Vector2dF(testVector.x(), 0), MathUtil::projectVector(testVector, x)); - EXPECT_VECTOR_EQ(gfx::Vector2dF(0, testVector.y()), MathUtil::projectVector(testVector, y)); - - // Finally check than an arbitrary vector projected to another one gives a vector parallel to - // the second vector. - gfx::Vector2dF targetVector(0.5, 0.2f); - gfx::Vector2dF projectedVector = MathUtil::projectVector(testVector, targetVector); - EXPECT_EQ(projectedVector.x() / targetVector.x(), - projectedVector.y() / targetVector.y()); +TEST(MathUtilTest, VectorProjection) { + gfx::Vector2dF x(1, 0); + gfx::Vector2dF y(0, 1); + gfx::Vector2dF test_vector(0.3f, 0.7f); + + // Orthogonal vectors project to a zero vector. + EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(x, y)); + EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(y, x)); + + // Projecting a vector onto the orthonormal basis gives the corresponding + // component of the vector. + EXPECT_VECTOR_EQ(gfx::Vector2dF(test_vector.x(), 0), + MathUtil::ProjectVector(test_vector, x)); + EXPECT_VECTOR_EQ(gfx::Vector2dF(0, test_vector.y()), + MathUtil::ProjectVector(test_vector, y)); + + // Finally check than an arbitrary vector projected to another one gives a + // vector parallel to the second vector. + gfx::Vector2dF target_vector(0.5, 0.2f); + gfx::Vector2dF projected_vector = + MathUtil::ProjectVector(test_vector, target_vector); + EXPECT_EQ(projected_vector.x() / target_vector.x(), + projected_vector.y() / target_vector.y()); } } // namespace diff --git a/cc/occlusion_tracker.cc b/cc/occlusion_tracker.cc index 970cdc5..45be89e 100644 --- a/cc/occlusion_tracker.cc +++ b/cc/occlusion_tracker.cc @@ -59,7 +59,7 @@ static gfx::Rect ScreenSpaceClipRectInTargetSurface( &inverse_screen_space_transform)) return target_surface->content_rect(); - return gfx::ToEnclosingRect(MathUtil::projectClippedRect( + return gfx::ToEnclosingRect(MathUtil::ProjectClippedRect( inverse_screen_space_transform, screen_space_clip_rect)); } @@ -77,7 +77,7 @@ static Region TransformSurfaceOpaqueRegion(const Region& region, bool clipped; gfx::QuadF transformed_bounds_quad = - MathUtil::mapQuad(transform, gfx::QuadF(region.bounds()), clipped); + MathUtil::MapQuad(transform, gfx::QuadF(region.bounds()), &clipped); // FIXME: Find a rect interior to each transformed quad. if (clipped || !transformed_bounds_quad.IsRectilinear()) return Region(); @@ -87,7 +87,7 @@ static Region TransformSurfaceOpaqueRegion(const Region& region, Region transformed_region; for (Region::Iterator rects(region); rects.has_rect(); rects.next()) { gfx::QuadF transformed_quad = - MathUtil::mapQuad(transform, gfx::QuadF(rects.rect()), clipped); + MathUtil::MapQuad(transform, gfx::QuadF(rects.rect()), &clipped); gfx::Rect transformed_rect = gfx::ToEnclosedRect(transformed_quad.BoundingBox()); DCHECK(!clipped); // We only map if the transform preserves axis alignment. @@ -241,7 +241,7 @@ static void ReduceOcclusionBelowSurface(LayerType* contributing_layer, return; gfx::Rect affected_area_in_target = gfx::ToEnclosingRect( - MathUtil::mapClippedRect(surface_transform, gfx::RectF(surface_rect))); + MathUtil::MapClippedRect(surface_transform, gfx::RectF(surface_rect))); if (contributing_layer->render_surface()->is_clipped()) { affected_area_in_target.Intersect( contributing_layer->render_surface()->clip_rect()); @@ -415,8 +415,8 @@ void OcclusionTrackerBase<LayerType, RenderSurfaceType>:: DCHECK(layer->visible_content_rect().Contains(opaque_contents.bounds())); bool clipped; - gfx::QuadF visible_transformed_quad = MathUtil::mapQuad( - layer->draw_transform(), gfx::QuadF(opaque_contents.bounds()), clipped); + gfx::QuadF visible_transformed_quad = MathUtil::MapQuad( + layer->draw_transform(), gfx::QuadF(opaque_contents.bounds()), &clipped); // FIXME: Find a rect interior to each transformed quad. if (clipped || !visible_transformed_quad.IsRectilinear()) return; @@ -429,10 +429,10 @@ void OcclusionTrackerBase<LayerType, RenderSurfaceType>:: for (Region::Iterator opaqueContentRects(opaque_contents); opaqueContentRects.has_rect(); opaqueContentRects.next()) { - gfx::QuadF transformed_quad = MathUtil::mapQuad( + gfx::QuadF transformed_quad = MathUtil::MapQuad( layer->draw_transform(), gfx::QuadF(opaqueContentRects.rect()), - clipped); + &clipped); gfx::Rect transformed_rect = gfx::ToEnclosedRect(transformed_quad.BoundingBox()); DCHECK(!clipped); // We only map if the transform preserves axis alignment. @@ -446,9 +446,9 @@ void OcclusionTrackerBase<LayerType, RenderSurfaceType>:: continue; // Save the occluding area in screen space for debug visualization. - gfx::QuadF screen_space_quad = MathUtil::mapQuad( + gfx::QuadF screen_space_quad = MathUtil::MapQuad( layer->render_target()->render_surface()->screen_space_transform(), - gfx::QuadF(transformed_rect), clipped); + gfx::QuadF(transformed_rect), &clipped); // TODO(danakj): Store the quad in the debug info instead of the bounding // box. gfx::Rect screen_space_rect = @@ -467,16 +467,16 @@ void OcclusionTrackerBase<LayerType, RenderSurfaceType>:: // We've already checked for clipping in the mapQuad call above, these calls // should not clip anything further. gfx::Rect transformed_rect = gfx::ToEnclosedRect( - MathUtil::mapClippedRect(layer->draw_transform(), + MathUtil::MapClippedRect(layer->draw_transform(), gfx::RectF(nonOpaqueContentRects.rect()))); transformed_rect.Intersect(clip_rect_in_target); if (transformed_rect.IsEmpty()) continue; - gfx::QuadF screen_space_quad = MathUtil::mapQuad( + gfx::QuadF screen_space_quad = MathUtil::MapQuad( layer->render_target()->render_surface()->screen_space_transform(), gfx::QuadF(transformed_rect), - clipped); + &clipped); // TODO(danakj): Store the quad in the debug info instead of the bounding // box. gfx::Rect screen_space_rect = @@ -520,7 +520,7 @@ bool OcclusionTrackerBase<LayerType, RenderSurfaceType>::Occluded( // Take the ToEnclosingRect at each step, as we want to contain any unoccluded // partial pixels in the resulting Rect. Region unoccluded_region_in_target_surface = gfx::ToEnclosingRect( - MathUtil::mapClippedRect(draw_transform, gfx::RectF(content_rect))); + MathUtil::MapClippedRect(draw_transform, gfx::RectF(content_rect))); // Layers can't clip across surfaces, so count this as internal occlusion. if (is_clipped) unoccluded_region_in_target_surface.Intersect(clip_rect_in_target); @@ -588,7 +588,7 @@ gfx::Rect OcclusionTrackerBase<LayerType, RenderSurfaceType>:: // Take the ToEnclosingRect at each step, as we want to contain any unoccluded // partial pixels in the resulting Rect. Region unoccluded_region_in_target_surface = gfx::ToEnclosingRect( - MathUtil::mapClippedRect(draw_transform, gfx::RectF(content_rect))); + MathUtil::MapClippedRect(draw_transform, gfx::RectF(content_rect))); // Layers can't clip across surfaces, so count this as internal occlusion. if (is_clipped) unoccluded_region_in_target_surface.Intersect(clip_rect_in_target); @@ -610,7 +610,7 @@ gfx::Rect OcclusionTrackerBase<LayerType, RenderSurfaceType>:: gfx::RectF unoccluded_rect_in_target_surface = unoccluded_region_in_target_surface.bounds(); gfx::Rect unoccluded_rect = gfx::ToEnclosingRect( - MathUtil::projectClippedRect(inverse_draw_transform, + MathUtil::ProjectClippedRect(inverse_draw_transform, unoccluded_rect_in_target_surface)); unoccluded_rect.Intersect(content_rect); @@ -669,7 +669,7 @@ gfx::Rect OcclusionTrackerBase<LayerType, RenderSurfaceType>:: // Take the ToEnclosingRect at each step, as we want to contain any unoccluded // partial pixels in the resulting Rect. Region unoccluded_region_in_target_surface = gfx::ToEnclosingRect( - MathUtil::mapClippedRect(draw_transform, gfx::RectF(content_rect))); + MathUtil::MapClippedRect(draw_transform, gfx::RectF(content_rect))); // Layers can't clip across surfaces, so count this as internal occlusion. if (surface->is_clipped()) unoccluded_region_in_target_surface.Intersect(surface->clip_rect()); @@ -697,7 +697,7 @@ gfx::Rect OcclusionTrackerBase<LayerType, RenderSurfaceType>:: gfx::RectF unoccluded_rect_in_target_surface = unoccluded_region_in_target_surface.bounds(); gfx::Rect unoccluded_rect = gfx::ToEnclosingRect( - MathUtil::projectClippedRect(inverse_draw_transform, + MathUtil::ProjectClippedRect(inverse_draw_transform, unoccluded_rect_in_target_surface)); unoccluded_rect.Intersect(content_rect); diff --git a/cc/occlusion_tracker_unittest.cc b/cc/occlusion_tracker_unittest.cc index d779370..a48aa5d 100644 --- a/cc/occlusion_tracker_unittest.cc +++ b/cc/occlusion_tracker_unittest.cc @@ -859,7 +859,7 @@ protected: TestOcclusionTrackerWithClip<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(gfx::Rect(0, 0, 1000, 1000)); - gfx::Rect clippedLayerInChild = MathUtil::mapClippedRect(layerTransform, layer->visible_content_rect()); + gfx::Rect clippedLayerInChild = MathUtil::MapClippedRect(layerTransform, layer->visible_content_rect()); this->visitLayer(layer, occlusion); this->enterContributingSurface(child, occlusion); diff --git a/cc/overdraw_metrics.cc b/cc/overdraw_metrics.cc index 3b0244f..cff06cb 100644 --- a/cc/overdraw_metrics.cc +++ b/cc/overdraw_metrics.cc @@ -48,7 +48,7 @@ static inline float AreaOfMappedQuad(const gfx::Transform& transform, const gfx::QuadF& quad) { gfx::PointF clippedQuad[8]; int num_vertices_in_clipped_quad = 0; - MathUtil::mapClippedQuad(transform, + MathUtil::MapClippedQuad(transform, quad, clippedQuad, num_vertices_in_clipped_quad); diff --git a/cc/picture_layer_impl.cc b/cc/picture_layer_impl.cc index 049fe95..729c77e 100644 --- a/cc/picture_layer_impl.cc +++ b/cc/picture_layer_impl.cc @@ -92,10 +92,10 @@ void PictureLayerImpl::AppendQuads(QuadSink* quadSink, AppendDebugBorderQuad(quadSink, sharedQuadState, appendQuadsData); bool clipped = false; - gfx::QuadF target_quad = MathUtil::mapQuad( + gfx::QuadF target_quad = MathUtil::MapQuad( draw_transform(), gfx::QuadF(rect), - clipped); + &clipped); bool is_axis_aligned_in_target = !clipped && target_quad.IsRectilinear(); bool is_pixel_aligned = is_axis_aligned_in_target && @@ -242,7 +242,7 @@ void PictureLayerImpl::UpdateTilePriorities() { if (screen_space_transform().GetInverse(&screen_to_layer)) { gfx::Rect device_viewport(layer_tree_impl()->device_viewport_size()); viewport_in_content_space = gfx::ToEnclosingRect( - MathUtil::projectClippedRect(screen_to_layer, device_viewport)); + MathUtil::ProjectClippedRect(screen_to_layer, device_viewport)); } WhichTree tree = diff --git a/cc/picture_layer_tiling.cc b/cc/picture_layer_tiling.cc index 955dfb7..a413310 100644 --- a/cc/picture_layer_tiling.cc +++ b/cc/picture_layer_tiling.cc @@ -452,13 +452,13 @@ void PictureLayerTiling::UpdateTilePriorities( tile_bounds, current_scale, current_scale); - gfx::RectF current_screen_rect = MathUtil::mapClippedRect( + gfx::RectF current_screen_rect = MathUtil::MapClippedRect( current_screen_transform, current_layer_content_rect); gfx::RectF last_layer_content_rect = gfx::ScaleRect( tile_bounds, last_scale, last_scale); - gfx::RectF last_screen_rect = MathUtil::mapClippedRect( + gfx::RectF last_screen_rect = MathUtil::MapClippedRect( last_screen_transform, last_layer_content_rect); float distance_to_visible_in_pixels = @@ -475,9 +475,9 @@ void PictureLayerTiling::UpdateTilePriorities( if (store_screen_space_quads_on_tiles) { bool clipped; priority.set_current_screen_quad( - MathUtil::mapQuad(current_screen_transform, + MathUtil::MapQuad(current_screen_transform, gfx::QuadF(current_layer_content_rect), - clipped)); + &clipped)); } tile->SetPriority(tree, priority); } @@ -507,7 +507,7 @@ scoped_ptr<base::Value> PictureLayerTiling::AsValue() const { state->SetInteger("num_tiles", tiles_.size()); state->SetDouble("content_scale", contents_scale_); state->Set("content_bounds", - MathUtil::asValue(ContentRect().size()).release()); + MathUtil::AsValue(ContentRect().size()).release()); return state.PassAs<base::Value>(); } diff --git a/cc/quad_culler_unittest.cc b/cc/quad_culler_unittest.cc index 153a52e..99d2ad1 100644 --- a/cc/quad_culler_unittest.cc +++ b/cc/quad_culler_unittest.cc @@ -85,7 +85,7 @@ class QuadCullerTest : public testing::Test { } } - gfx::Rect rect_in_target = MathUtil::mapClippedRect( + gfx::Rect rect_in_target = MathUtil::MapClippedRect( layer->draw_transform(), layer->visible_content_rect()); if (!parent) { layer->CreateRenderSurface(); @@ -95,7 +95,7 @@ class QuadCullerTest : public testing::Test { } else { layer->draw_properties().render_target = parent->render_target(); parent->render_surface()->layer_list().push_back(layer.get()); - rect_in_target.Union(MathUtil::mapClippedRect( + rect_in_target.Union(MathUtil::MapClippedRect( parent->draw_transform(), parent->visible_content_rect())); parent->render_surface()->SetContentRect(rect_in_target); } diff --git a/cc/render_surface.cc b/cc/render_surface.cc index 77d6fae..06aada5 100644 --- a/cc/render_surface.cc +++ b/cc/render_surface.cc @@ -23,10 +23,10 @@ RenderSurface::~RenderSurface() {} gfx::RectF RenderSurface::DrawableContentRect() const { gfx::RectF drawable_content_rect = - MathUtil::mapClippedRect(draw_transform_, content_rect_); + MathUtil::MapClippedRect(draw_transform_, content_rect_); if (owning_layer_->has_replica()) drawable_content_rect.Union( - MathUtil::mapClippedRect(replica_draw_transform_, content_rect_)); + MathUtil::MapClippedRect(replica_draw_transform_, content_rect_)); return drawable_content_rect; } diff --git a/cc/render_surface_impl.cc b/cc/render_surface_impl.cc index d667994..0b1183f 100644 --- a/cc/render_surface_impl.cc +++ b/cc/render_surface_impl.cc @@ -43,10 +43,10 @@ RenderSurfaceImpl::~RenderSurfaceImpl() {} gfx::RectF RenderSurfaceImpl::DrawableContentRect() const { gfx::RectF drawable_content_rect = - MathUtil::mapClippedRect(draw_transform_, content_rect_); + MathUtil::MapClippedRect(draw_transform_, content_rect_); if (owning_layer_->has_replica()) { drawable_content_rect.Union( - MathUtil::mapClippedRect(replica_draw_transform_, content_rect_)); + MathUtil::MapClippedRect(replica_draw_transform_, content_rect_)); } return drawable_content_rect; @@ -248,7 +248,7 @@ void RenderSurfaceImpl::AppendQuads(QuadSink* quad_sink, gfx::RectF mask_uv_rect(0.f, 0.f, 1.f, 1.f); if (mask_layer) { gfx::Vector2dF owning_layer_draw_scale = - MathUtil::computeTransform2dScaleComponents( + MathUtil::ComputeTransform2dScaleComponents( owning_layer_->draw_transform(), 1.f); gfx::SizeF unclipped_surface_size = gfx::ScaleSize( owning_layer_->content_bounds(), diff --git a/cc/test/layer_test_common.cc b/cc/test/layer_test_common.cc index 8d8d842..34d7c1b 100644 --- a/cc/test/layer_test_common.cc +++ b/cc/test/layer_test_common.cc @@ -39,7 +39,7 @@ void verifyQuadsExactlyCoverRect(const cc::QuadList& quads, for (size_t i = 0; i < quads.size(); ++i) { cc::DrawQuad* quad = quads[i]; - gfx::RectF quadRectF = cc::MathUtil::mapClippedRect(quad->quadTransform(), gfx::RectF(quad->rect)); + gfx::RectF quadRectF = cc::MathUtil::MapClippedRect(quad->quadTransform(), gfx::RectF(quad->rect)); // Before testing for exact coverage in the integer world, assert that rounding // will not round the rect incorrectly. diff --git a/cc/tile_priority.cc b/cc/tile_priority.cc index 8c3d178..ec4f817 100644 --- a/cc/tile_priority.cc +++ b/cc/tile_priority.cc @@ -98,9 +98,9 @@ scoped_ptr<base::Value> TilePriority::AsValue() const { scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); state->SetBoolean("is_live", is_live); state->Set("resolution", TileResolutionAsValue(resolution).release()); - state->Set("time_to_visible_in_seconds", MathUtil::asValueSafely(time_to_visible_in_seconds).release()); - state->Set("distance_to_visible_in_pixels", MathUtil::asValueSafely(distance_to_visible_in_pixels).release()); - state->Set("current_screen_quad", MathUtil::asValue(current_screen_quad).release()); + state->Set("time_to_visible_in_seconds", MathUtil::AsValueSafely(time_to_visible_in_seconds).release()); + state->Set("distance_to_visible_in_pixels", MathUtil::AsValueSafely(distance_to_visible_in_pixels).release()); + state->Set("current_screen_quad", MathUtil::AsValue(current_screen_quad).release()); return state.PassAs<base::Value>(); } |