1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/libui/VelocityControl.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright (C) 2012 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#define LOG_TAG "VelocityControl" 1.21 +//#define LOG_NDEBUG 0 1.22 + 1.23 +// Log debug messages about acceleration. 1.24 +#define DEBUG_ACCELERATION 0 1.25 + 1.26 +#include <math.h> 1.27 +#include <limits.h> 1.28 + 1.29 +#include "VelocityControl.h" 1.30 +#include <utils/BitSet.h> 1.31 +#include <utils/Timers.h> 1.32 + 1.33 +namespace android { 1.34 + 1.35 +// --- VelocityControl --- 1.36 + 1.37 +const nsecs_t VelocityControl::STOP_TIME; 1.38 + 1.39 +VelocityControl::VelocityControl() { 1.40 + reset(); 1.41 +} 1.42 + 1.43 +void VelocityControl::setParameters(const VelocityControlParameters& parameters) { 1.44 + mParameters = parameters; 1.45 + reset(); 1.46 +} 1.47 + 1.48 +void VelocityControl::reset() { 1.49 + mLastMovementTime = LLONG_MIN; 1.50 + mRawPosition.x = 0; 1.51 + mRawPosition.y = 0; 1.52 + mVelocityTracker.clear(); 1.53 +} 1.54 + 1.55 +void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) { 1.56 + if ((deltaX && *deltaX) || (deltaY && *deltaY)) { 1.57 + if (eventTime >= mLastMovementTime + STOP_TIME) { 1.58 +#if DEBUG_ACCELERATION 1.59 + ALOGD("VelocityControl: stopped, last movement was %0.3fms ago", 1.60 + (eventTime - mLastMovementTime) * 0.000001f); 1.61 +#endif 1.62 + reset(); 1.63 + } 1.64 + 1.65 + mLastMovementTime = eventTime; 1.66 + if (deltaX) { 1.67 + mRawPosition.x += *deltaX; 1.68 + } 1.69 + if (deltaY) { 1.70 + mRawPosition.y += *deltaY; 1.71 + } 1.72 + mVelocityTracker.addMovement(eventTime, BitSet32(BitSet32::valueForBit(0)), &mRawPosition); 1.73 + 1.74 + float vx, vy; 1.75 + float scale = mParameters.scale; 1.76 + if (mVelocityTracker.getVelocity(0, &vx, &vy)) { 1.77 + float speed = hypotf(vx, vy) * scale; 1.78 + if (speed >= mParameters.highThreshold) { 1.79 + // Apply full acceleration above the high speed threshold. 1.80 + scale *= mParameters.acceleration; 1.81 + } else if (speed > mParameters.lowThreshold) { 1.82 + // Linearly interpolate the acceleration to apply between the low and high 1.83 + // speed thresholds. 1.84 + scale *= 1 + (speed - mParameters.lowThreshold) 1.85 + / (mParameters.highThreshold - mParameters.lowThreshold) 1.86 + * (mParameters.acceleration - 1); 1.87 + } 1.88 + 1.89 +#if DEBUG_ACCELERATION 1.90 + ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): " 1.91 + "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f", 1.92 + mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold, 1.93 + mParameters.acceleration, 1.94 + vx, vy, speed, scale / mParameters.scale); 1.95 +#endif 1.96 + } else { 1.97 +#if DEBUG_ACCELERATION 1.98 + ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity", 1.99 + mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold, 1.100 + mParameters.acceleration); 1.101 +#endif 1.102 + } 1.103 + 1.104 + if (deltaX) { 1.105 + *deltaX *= scale; 1.106 + } 1.107 + if (deltaY) { 1.108 + *deltaY *= scale; 1.109 + } 1.110 + } 1.111 +} 1.112 + 1.113 +} // namespace android