gfx/skia/trunk/src/gpu/FlingState.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial