Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2010 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | #include "FlingState.h" |
michael@0 | 12 | #include "SkMatrix.h" |
michael@0 | 13 | #include "SkTime.h" |
michael@0 | 14 | |
michael@0 | 15 | #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true |
michael@0 | 16 | |
michael@0 | 17 | static const float MAX_FLING_SPEED = 1500; |
michael@0 | 18 | |
michael@0 | 19 | static float pin_max_fling(float speed) { |
michael@0 | 20 | if (speed > MAX_FLING_SPEED) { |
michael@0 | 21 | speed = MAX_FLING_SPEED; |
michael@0 | 22 | } |
michael@0 | 23 | return speed; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | static double getseconds() { |
michael@0 | 27 | return SkTime::GetMSecs() * 0.001; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | // returns +1 or -1, depending on the sign of x |
michael@0 | 31 | // returns +1 if x is zero |
michael@0 | 32 | static SkScalar SkScalarSign(SkScalar x) { |
michael@0 | 33 | SkScalar sign = SK_Scalar1; |
michael@0 | 34 | if (x < 0) { |
michael@0 | 35 | sign = -sign; |
michael@0 | 36 | } |
michael@0 | 37 | return sign; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | static void unit_axis_align(SkVector* unit) { |
michael@0 | 41 | const SkScalar TOLERANCE = SkDoubleToScalar(0.15); |
michael@0 | 42 | if (SkScalarAbs(unit->fX) < TOLERANCE) { |
michael@0 | 43 | unit->fX = 0; |
michael@0 | 44 | unit->fY = SkScalarSign(unit->fY); |
michael@0 | 45 | } else if (SkScalarAbs(unit->fY) < TOLERANCE) { |
michael@0 | 46 | unit->fX = SkScalarSign(unit->fX); |
michael@0 | 47 | unit->fY = 0; |
michael@0 | 48 | } |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void FlingState::reset(float sx, float sy) { |
michael@0 | 52 | fActive = true; |
michael@0 | 53 | fDirection.set(sx, sy); |
michael@0 | 54 | fSpeed0 = SkPoint::Normalize(&fDirection); |
michael@0 | 55 | fSpeed0 = pin_max_fling(fSpeed0); |
michael@0 | 56 | fTime0 = getseconds(); |
michael@0 | 57 | |
michael@0 | 58 | unit_axis_align(&fDirection); |
michael@0 | 59 | // printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | bool FlingState::evaluateMatrix(SkMatrix* matrix) { |
michael@0 | 63 | if (!fActive) { |
michael@0 | 64 | return false; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | const float t = getseconds() - fTime0; |
michael@0 | 68 | const float MIN_SPEED = 2; |
michael@0 | 69 | const float K0 = 5.0; |
michael@0 | 70 | const float K1 = 0.02; |
michael@0 | 71 | const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1); |
michael@0 | 72 | if (speed <= MIN_SPEED) { |
michael@0 | 73 | fActive = false; |
michael@0 | 74 | return false; |
michael@0 | 75 | } |
michael@0 | 76 | float dist = (fSpeed0 - speed) / K0; |
michael@0 | 77 | |
michael@0 | 78 | // printf("---- time %g speed %g dist %g\n", t, speed, dist); |
michael@0 | 79 | float tx = fDirection.fX * dist; |
michael@0 | 80 | float ty = fDirection.fY * dist; |
michael@0 | 81 | if (DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER) { |
michael@0 | 82 | tx = sk_float_round2int(tx); |
michael@0 | 83 | ty = sk_float_round2int(ty); |
michael@0 | 84 | } |
michael@0 | 85 | matrix->setTranslate(tx, ty); |
michael@0 | 86 | // printf("---- evaluate (%g %g)\n", tx, ty); |
michael@0 | 87 | |
michael@0 | 88 | return true; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | //////////////////////////////////////// |
michael@0 | 92 | |
michael@0 | 93 | GrAnimateFloat::GrAnimateFloat() : fTime0(0) {} |
michael@0 | 94 | |
michael@0 | 95 | void GrAnimateFloat::start(float v0, float v1, float duration) { |
michael@0 | 96 | fValue0 = v0; |
michael@0 | 97 | fValue1 = v1; |
michael@0 | 98 | fDuration = duration; |
michael@0 | 99 | if (duration > 0) { |
michael@0 | 100 | fTime0 = SkTime::GetMSecs(); |
michael@0 | 101 | if (!fTime0) { |
michael@0 | 102 | fTime0 = 1; // time0 is our sentinel |
michael@0 | 103 | } |
michael@0 | 104 | } else { |
michael@0 | 105 | fTime0 = 0; |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | float GrAnimateFloat::evaluate() { |
michael@0 | 110 | if (!fTime0) { |
michael@0 | 111 | return fValue1; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | double elapsed = (SkTime::GetMSecs() - fTime0) * 0.001; |
michael@0 | 115 | if (elapsed >= fDuration) { |
michael@0 | 116 | fTime0 = 0; |
michael@0 | 117 | return fValue1; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | double t = elapsed / fDuration; |
michael@0 | 121 | if (true) { |
michael@0 | 122 | t = (3 - 2 * t) * t * t; |
michael@0 | 123 | } |
michael@0 | 124 | return fValue0 + t * (fValue1 - fValue0); |
michael@0 | 125 | } |