Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/apz/src/DesktopFlingPhysics.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_layers_DesktopFlingPhysics_h_
8
#define mozilla_layers_DesktopFlingPhysics_h_
9
10
#include "AsyncPanZoomController.h"
11
#include "Units.h"
12
#include "gfxPrefs.h"
13
#include "mozilla/Assertions.h"
14
15
#define FLING_PHYS_LOG(...)
16
// #define FLING_PHYS_LOG(...) printf_stderr("FLING: " __VA_ARGS__)
17
18
namespace mozilla {
19
namespace layers {
20
21
class DesktopFlingPhysics {
22
public:
23
  void Init(const ParentLayerPoint& aStartingVelocity, float aPLPPI /* unused */)
24
0
  {
25
0
    mVelocity = aStartingVelocity;
26
0
  }
27
  void Sample(const TimeDuration& aDelta,
28
              ParentLayerPoint* aOutVelocity,
29
              ParentLayerPoint* aOutOffset)
30
0
  {
31
0
    float friction = gfxPrefs::APZFlingFriction();
32
0
    float threshold = gfxPrefs::APZFlingStoppedThreshold();
33
0
34
0
    mVelocity = ParentLayerPoint(
35
0
        ApplyFrictionOrCancel(mVelocity.x, aDelta, friction, threshold),
36
0
        ApplyFrictionOrCancel(mVelocity.y, aDelta, friction, threshold));
37
0
38
0
    *aOutVelocity = mVelocity;
39
0
    *aOutOffset = mVelocity * aDelta.ToMilliseconds();
40
0
  }
41
private:
42
  /**
43
   * Applies friction to the given velocity and returns the result, or
44
   * returns zero if the velocity is too low.
45
   * |aVelocity| is the incoming velocity.
46
   * |aDelta| is the amount of time that has passed since the last time
47
   * friction was applied.
48
   * |aFriction| is the amount of friction to apply.
49
   * |aThreshold| is the velocity below which the fling is cancelled.
50
   */
51
  static float ApplyFrictionOrCancel(float aVelocity, const TimeDuration& aDelta,
52
                                     float aFriction, float aThreshold)
53
0
  {
54
0
    if (fabsf(aVelocity) <= aThreshold) {
55
0
      // If the velocity is very low, just set it to 0 and stop the fling,
56
0
      // otherwise we'll just asymptotically approach 0 and the user won't
57
0
      // actually see any changes.
58
0
      return 0.0f;
59
0
    }
60
0
61
0
    aVelocity *= pow(1.0f - aFriction, float(aDelta.ToMilliseconds()));
62
0
    return aVelocity;
63
0
  }
64
65
  ParentLayerPoint mVelocity;
66
};
67
68
} // namespace layers
69
} // namespace mozilla
70
71
#endif // mozilla_layers_DesktopFlingPhysics_h_