Skip to content

Commit

Permalink
Pass gfx structs by const ref (gfx::Vector2dF)
Browse files Browse the repository at this point in the history
Avoid unneccessary copy of structures gfx::Vector2dF
by passing them by const ref rather than value.

Any struct of size > 4 bytes should be passed by const ref.
Passing by ref for these structs is faster than passing
by value, especially when invoking function has multiple parameters.


Pass gfx structs by const ref (gfx::Vector2dF)

BUG=159273

Review URL: https://codereview.chromium.org/130443005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@246563 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
ajay.berwal@samsung.com committed Jan 23, 2014
1 parent 8aaf0f5 commit 3244c91
Show file tree
Hide file tree
Showing 29 changed files with 90 additions and 75 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Aaron Randolph <aaron.randolph@gmail.com>
Adam Treat <adam.treat@samsung.com>
Adenilson Cavalcanti <a.cavalcanti@partner.samsung.com>
Aditya Bhargava <heuristicist@gmail.com>
Ajay Berwal <ajay.berwal@samsung.com>
Alex Gartrell <agartrell@cmu.edu>
Alex Scheele <alexscheele@gmail.com>
Alexander Sulfrian <alexander@sulfrian.net>
Expand Down
2 changes: 1 addition & 1 deletion cc/animation/layer_animation_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ void LayerAnimationController::NotifyObserversFilterAnimated(
}

void LayerAnimationController::NotifyObserversScrollOffsetAnimated(
gfx::Vector2dF scroll_offset) {
const gfx::Vector2dF& scroll_offset) {
FOR_EACH_OBSERVER(LayerAnimationValueObserver,
value_observers_,
OnScrollOffsetAnimated(scroll_offset));
Expand Down
2 changes: 1 addition & 1 deletion cc/animation/layer_animation_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class CC_EXPORT LayerAnimationController
void NotifyObserversOpacityAnimated(float opacity);
void NotifyObserversTransformAnimated(const gfx::Transform& transform);
void NotifyObserversFilterAnimated(const FilterOperations& filter);
void NotifyObserversScrollOffsetAnimated(gfx::Vector2dF scroll_offset);
void NotifyObserversScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset);

void NotifyObserversAnimationWaitingForDeletion();

Expand Down
2 changes: 1 addition & 1 deletion cc/animation/layer_animation_value_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CC_EXPORT LayerAnimationValueObserver {
virtual void OnFilterAnimated(const FilterOperations& filters) = 0;
virtual void OnOpacityAnimated(float opacity) = 0;
virtual void OnTransformAnimated(const gfx::Transform& transform) = 0;
virtual void OnScrollOffsetAnimated(gfx::Vector2dF scroll_offset) = 0;
virtual void OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) = 0;
virtual void OnAnimationWaitingForDeletion() = 0;
virtual bool IsActive() const = 0;
};
Expand Down
7 changes: 4 additions & 3 deletions cc/animation/scroll_offset_animation_curve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ const double kDurationDivisor = 60.0;
namespace cc {

scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create(
gfx::Vector2dF target_value,
const gfx::Vector2dF& target_value,
scoped_ptr<TimingFunction> timing_function) {
return make_scoped_ptr(
new ScrollOffsetAnimationCurve(target_value, timing_function.Pass()));
}

ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
gfx::Vector2dF target_value,
const gfx::Vector2dF& target_value,
scoped_ptr<TimingFunction> timing_function)
: target_value_(target_value),
duration_(0.0),
timing_function_(timing_function.Pass()) {}

ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}

void ScrollOffsetAnimationCurve::SetInitialValue(gfx::Vector2dF initial_value) {
void ScrollOffsetAnimationCurve::SetInitialValue(
const gfx::Vector2dF& initial_value) {
initial_value_ = initial_value;

// The duration of a scroll animation depends on the size of the scroll.
Expand Down
6 changes: 3 additions & 3 deletions cc/animation/scroll_offset_animation_curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class TimingFunction;
class CC_EXPORT ScrollOffsetAnimationCurve : public AnimationCurve {
public:
static scoped_ptr<ScrollOffsetAnimationCurve> Create(
gfx::Vector2dF target_value,
const gfx::Vector2dF& target_value,
scoped_ptr<TimingFunction> timing_function);

virtual ~ScrollOffsetAnimationCurve();

void SetInitialValue(gfx::Vector2dF initial_value);
void SetInitialValue(const gfx::Vector2dF& initial_value);
gfx::Vector2dF GetValue(double t) const;

// AnimationCurve implementation
Expand All @@ -30,7 +30,7 @@ class CC_EXPORT ScrollOffsetAnimationCurve : public AnimationCurve {
virtual scoped_ptr<AnimationCurve> Clone() const OVERRIDE;

private:
ScrollOffsetAnimationCurve(gfx::Vector2dF target_value,
ScrollOffsetAnimationCurve(const gfx::Vector2dF& target_value,
scoped_ptr <TimingFunction> timing_function);

gfx::Vector2dF initial_value_;
Expand Down
8 changes: 4 additions & 4 deletions cc/base/math_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,16 +492,16 @@ gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents(
return gfx::Vector2dF(x_scale, y_scale);
}

float MathUtil::SmallestAngleBetweenVectors(gfx::Vector2dF v1,
gfx::Vector2dF v2) {
float MathUtil::SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
const 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) {
gfx::Vector2dF MathUtil::ProjectVector(const gfx::Vector2dF& source,
const gfx::Vector2dF& destination) {
float projected_length =
gfx::DotProduct(source, destination) / destination.LengthSquared();
return gfx::Vector2dF(projected_length * destination.x(),
Expand Down
8 changes: 4 additions & 4 deletions cc/base/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ class CC_EXPORT MathUtil {

// 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);
static float SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
const 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);
static gfx::Vector2dF ProjectVector(const gfx::Vector2dF& source,
const gfx::Vector2dF& destination);

// Conversion to value.
static scoped_ptr<base::Value> AsValue(gfx::Size s);
Expand Down
4 changes: 2 additions & 2 deletions cc/input/input_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CC_EXPORT InputHandler {
// to the client.
// Should only be called if ScrollBegin() returned ScrollStarted.
virtual bool ScrollBy(gfx::Point viewport_point,
gfx::Vector2dF scroll_delta) = 0;
const gfx::Vector2dF& scroll_delta) = 0;

virtual bool ScrollVerticallyByPage(
gfx::Point viewport_point,
Expand All @@ -95,7 +95,7 @@ class CC_EXPORT InputHandler {
// ScrollIgnored if not.
virtual ScrollStatus FlingScrollBegin() = 0;

virtual void NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) = 0;
virtual void NotifyCurrentFlingVelocity(const gfx::Vector2dF& velocity) = 0;

virtual void MouseMoveAt(gfx::Point mouse_position) = 0;

Expand Down
4 changes: 2 additions & 2 deletions cc/input/layer_scroll_offset_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class LayerScrollOffsetDelegate {
public:
// This is called by the compositor to notify the delegate what is the upper
// total scroll offset bound.
virtual void SetMaxScrollOffset(gfx::Vector2dF max_scroll_offset) = 0;
virtual void SetMaxScrollOffset(const gfx::Vector2dF& max_scroll_offset) = 0;

// This is called by the compositor when the scroll offset of the layer would
// have otherwise changed.
virtual void SetTotalScrollOffset(gfx::Vector2dF new_value) = 0;
virtual void SetTotalScrollOffset(const gfx::Vector2dF& new_value) = 0;

// This is called by the compositor to query the current scroll offset of the
// layer.
Expand Down
16 changes: 8 additions & 8 deletions cc/input/page_scale_animation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ namespace {

// This takes a viewport-relative vector and returns a vector whose values are
// between 0 and 1, representing the percentage position within the viewport.
gfx::Vector2dF NormalizeFromViewport(gfx::Vector2dF denormalized,
gfx::Vector2dF NormalizeFromViewport(const gfx::Vector2dF& denormalized,
const gfx::SizeF& viewport_size) {
return gfx::ScaleVector2d(denormalized,
1.f / viewport_size.width(),
1.f / viewport_size.height());
}

gfx::Vector2dF DenormalizeToViewport(gfx::Vector2dF normalized,
gfx::Vector2dF DenormalizeToViewport(const gfx::Vector2dF& normalized,
const gfx::SizeF& viewport_size) {
return gfx::ScaleVector2d(normalized,
viewport_size.width(),
viewport_size.height());
}

gfx::Vector2dF InterpolateBetween(gfx::Vector2dF start,
gfx::Vector2dF end,
gfx::Vector2dF InterpolateBetween(const gfx::Vector2dF& start,
const gfx::Vector2dF& end,
float interp) {
return start + gfx::ScaleVector2d(end - start, interp);
}
Expand All @@ -41,7 +41,7 @@ gfx::Vector2dF InterpolateBetween(gfx::Vector2dF start,
namespace cc {

scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create(
gfx::Vector2dF start_scroll_offset,
const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
Expand All @@ -54,7 +54,7 @@ scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create(
}

PageScaleAnimation::PageScaleAnimation(
gfx::Vector2dF start_scroll_offset,
const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
Expand All @@ -72,7 +72,7 @@ PageScaleAnimation::PageScaleAnimation(

PageScaleAnimation::~PageScaleAnimation() {}

void PageScaleAnimation::ZoomTo(gfx::Vector2dF target_scroll_offset,
void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset,
float target_page_scale_factor,
double duration) {
target_page_scale_factor_ = target_page_scale_factor;
Expand All @@ -92,7 +92,7 @@ void PageScaleAnimation::ZoomTo(gfx::Vector2dF target_scroll_offset,
start_anchor_ = target_anchor_;
}

void PageScaleAnimation::ZoomWithAnchor(gfx::Vector2dF anchor,
void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor,
float target_page_scale_factor,
double duration) {
start_anchor_ = anchor;
Expand Down
8 changes: 4 additions & 4 deletions cc/input/page_scale_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PageScaleAnimation {
public:
// Construct with the state at the beginning of the animation.
static scoped_ptr<PageScaleAnimation> Create(
gfx::Vector2dF start_scroll_offset,
const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
Expand All @@ -37,15 +37,15 @@ class PageScaleAnimation {
// immediately after construction to set the final scroll and page scale.

// Zoom while explicitly specifying the top-left scroll position.
void ZoomTo(gfx::Vector2dF target_scroll_offset,
void ZoomTo(const gfx::Vector2dF& target_scroll_offset,
float target_page_scale_factor,
double duration);

// Zoom based on a specified anchor. The animator will attempt to keep it
// at the same position on the physical display throughout the animation,
// unless the edges of the root layer are hit. The anchor is specified
// as an offset from the content layer.
void ZoomWithAnchor(gfx::Vector2dF anchor,
void ZoomWithAnchor(const gfx::Vector2dF& anchor,
float target_page_scale_factor,
double duration);

Expand All @@ -69,7 +69,7 @@ class PageScaleAnimation {
float target_page_scale_factor() const { return target_page_scale_factor_; }

protected:
PageScaleAnimation(gfx::Vector2dF start_scroll_offset,
PageScaleAnimation(const gfx::Vector2dF& start_scroll_offset,
float start_page_scale_factor,
const gfx::SizeF& viewport_size,
const gfx::SizeF& root_layer_size,
Expand Down
2 changes: 1 addition & 1 deletion cc/input/top_controls_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void TopControlsManager::ScrollBegin() {
}

gfx::Vector2dF TopControlsManager::ScrollBy(
const gfx::Vector2dF pending_delta) {
const gfx::Vector2dF& pending_delta) {
if (pinch_gesture_active_)
return pending_delta;

Expand Down
2 changes: 1 addition & 1 deletion cc/input/top_controls_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CC_EXPORT TopControlsManager
bool animate);

void ScrollBegin();
gfx::Vector2dF ScrollBy(const gfx::Vector2dF pending_delta);
gfx::Vector2dF ScrollBy(const gfx::Vector2dF& pending_delta);
void ScrollEnd();

// The caller should ensure that |Pinch{Begin,End}| are called within
Expand Down
2 changes: 1 addition & 1 deletion cc/layers/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void Layer::OnTransformAnimated(const gfx::Transform& transform) {
transform_ = transform;
}

void Layer::OnScrollOffsetAnimated(gfx::Vector2dF scroll_offset) {
void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
// Do nothing. Scroll deltas will be sent from the compositor thread back
// to the main thread in the same manner as during non-animated
// compositor-driven scrolling.
Expand Down
3 changes: 2 additions & 1 deletion cc/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ class CC_EXPORT Layer : public base::RefCounted<Layer>,
virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE;
virtual void OnOpacityAnimated(float opacity) OVERRIDE;
virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
virtual void OnScrollOffsetAnimated(gfx::Vector2dF scroll_offset) OVERRIDE;
virtual void OnScrollOffsetAnimated(
const gfx::Vector2dF& scroll_offset) OVERRIDE;
virtual void OnAnimationWaitingForDeletion() OVERRIDE;
virtual bool IsActive() const OVERRIDE;

Expand Down
8 changes: 4 additions & 4 deletions cc/layers/layer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void LayerImpl::SetSentScrollDelta(gfx::Vector2d sent_scroll_delta) {
sent_scroll_delta_ = sent_scroll_delta;
}

gfx::Vector2dF LayerImpl::ScrollBy(gfx::Vector2dF scroll) {
gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
DCHECK(scrollable());
gfx::Vector2dF min_delta = -scroll_offset_;
gfx::Vector2dF max_delta = max_scroll_offset_ - scroll_offset_;
Expand Down Expand Up @@ -731,7 +731,7 @@ void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
SetTransform(transform);
}

void LayerImpl::OnScrollOffsetAnimated(gfx::Vector2dF scroll_offset) {
void LayerImpl::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
// Only layers in the active tree should need to do anything here, since
// layers in the pending tree will find out about these changes as a
// result of the call to SetScrollDelta.
Expand Down Expand Up @@ -1092,7 +1092,7 @@ void LayerImpl::SetScrollOffset(gfx::Vector2d scroll_offset) {
}

void LayerImpl::SetScrollOffsetAndDelta(gfx::Vector2d scroll_offset,
gfx::Vector2dF scroll_delta) {
const gfx::Vector2dF& scroll_delta) {
bool changed = false;

if (scroll_offset_ != scroll_offset) {
Expand Down Expand Up @@ -1139,7 +1139,7 @@ gfx::Vector2dF LayerImpl::ScrollDelta() const {
return scroll_delta_;
}

void LayerImpl::SetScrollDelta(gfx::Vector2dF scroll_delta) {
void LayerImpl::SetScrollDelta(const gfx::Vector2dF& scroll_delta) {
SetScrollOffsetAndDelta(scroll_offset_, scroll_delta);
}

Expand Down
11 changes: 6 additions & 5 deletions cc/layers/layer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class CC_EXPORT LayerImpl : public LayerAnimationValueObserver,
virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE;
virtual void OnOpacityAnimated(float opacity) OVERRIDE;
virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
virtual void OnScrollOffsetAnimated(gfx::Vector2dF scroll_offset) OVERRIDE;
virtual void OnScrollOffsetAnimated(
const gfx::Vector2dF& scroll_offset) OVERRIDE;
virtual void OnAnimationWaitingForDeletion() OVERRIDE;
virtual bool IsActive() const OVERRIDE;

Expand Down Expand Up @@ -244,7 +245,7 @@ class CC_EXPORT LayerImpl : public LayerAnimationValueObserver,
return is_container_for_fixed_position_layers_;
}

void SetFixedContainerSizeDelta(gfx::Vector2dF delta) {
void SetFixedContainerSizeDelta(const gfx::Vector2dF& delta) {
fixed_container_size_delta_ = delta;
}
gfx::Vector2dF fixed_container_size_delta() const {
Expand Down Expand Up @@ -369,13 +370,13 @@ class CC_EXPORT LayerImpl : public LayerAnimationValueObserver,

void SetScrollOffset(gfx::Vector2d scroll_offset);
void SetScrollOffsetAndDelta(gfx::Vector2d scroll_offset,
gfx::Vector2dF scroll_delta);
const gfx::Vector2dF& scroll_delta);
gfx::Vector2d scroll_offset() const { return scroll_offset_; }

void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset);
gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; }

void SetScrollDelta(gfx::Vector2dF scroll_delta);
void SetScrollDelta(const gfx::Vector2dF& scroll_delta);
gfx::Vector2dF ScrollDelta() const;

gfx::Vector2dF TotalScrollOffset() const;
Expand All @@ -385,7 +386,7 @@ class CC_EXPORT LayerImpl : public LayerAnimationValueObserver,

// Returns the delta of the scroll that was outside of the bounds of the
// initial scroll
gfx::Vector2dF ScrollBy(gfx::Vector2dF scroll);
gfx::Vector2dF ScrollBy(const gfx::Vector2dF& scroll);

void SetScrollable(bool scrollable) { scrollable_ = scrollable; }
bool scrollable() const { return scrollable_; }
Expand Down
Loading

0 comments on commit 3244c91

Please sign in to comment.