gfx/skia/trunk/src/views/SkTouchGesture.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 "SkTouchGesture.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 SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
michael@0 18
michael@0 19 static SkScalar pin_max_fling(SkScalar 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 z is zero
michael@0 32 static SkScalar SkScalarSignNonZero(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 = SkScalarSignNonZero(unit->fY);
michael@0 45 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
michael@0 46 unit->fX = SkScalarSignNonZero(unit->fX);
michael@0 47 unit->fY = 0;
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 void SkFlingState::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 SkFlingState::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 = (float)(getseconds() - fTime0);
michael@0 68 const float MIN_SPEED = 2;
michael@0 69 const float K0 = 5;
michael@0 70 const float K1 = 0.02f;
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 = (float)sk_float_round2int(tx);
michael@0 83 ty = (float)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 static const SkMSec MAX_DBL_TAP_INTERVAL = 300;
michael@0 94 static const float MAX_DBL_TAP_DISTANCE = 100;
michael@0 95 static const float MAX_JITTER_RADIUS = 2;
michael@0 96
michael@0 97 // if true, then ignore the touch-move, 'cause its probably just jitter
michael@0 98 static bool close_enough_for_jitter(float x0, float y0, float x1, float y1) {
michael@0 99 return sk_float_abs(x0 - x1) <= MAX_JITTER_RADIUS &&
michael@0 100 sk_float_abs(y0 - y1) <= MAX_JITTER_RADIUS;
michael@0 101 }
michael@0 102
michael@0 103 ///////////////////////////////////////////////////////////////////////////////
michael@0 104
michael@0 105 SkTouchGesture::SkTouchGesture() {
michael@0 106 this->reset();
michael@0 107 }
michael@0 108
michael@0 109 SkTouchGesture::~SkTouchGesture() {
michael@0 110 }
michael@0 111
michael@0 112 void SkTouchGesture::reset() {
michael@0 113 fTouches.reset();
michael@0 114 fState = kEmpty_State;
michael@0 115 fLocalM.reset();
michael@0 116 fGlobalM.reset();
michael@0 117
michael@0 118 fLastUpT = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
michael@0 119 fLastUpP.set(0, 0);
michael@0 120 }
michael@0 121
michael@0 122 void SkTouchGesture::flushLocalM() {
michael@0 123 fGlobalM.postConcat(fLocalM);
michael@0 124 fLocalM.reset();
michael@0 125 }
michael@0 126
michael@0 127 const SkMatrix& SkTouchGesture::localM() {
michael@0 128 if (fFlinger.isActive()) {
michael@0 129 if (!fFlinger.evaluateMatrix(&fLocalM)) {
michael@0 130 this->flushLocalM();
michael@0 131 }
michael@0 132 }
michael@0 133 return fLocalM;
michael@0 134 }
michael@0 135
michael@0 136 void SkTouchGesture::appendNewRec(void* owner, float x, float y) {
michael@0 137 Rec* rec = fTouches.append();
michael@0 138 rec->fOwner = owner;
michael@0 139 rec->fStartX = rec->fPrevX = rec->fLastX = x;
michael@0 140 rec->fStartY = rec->fPrevY = rec->fLastY = y;
michael@0 141 rec->fLastT = rec->fPrevT = SkTime::GetMSecs();
michael@0 142 }
michael@0 143
michael@0 144 void SkTouchGesture::touchBegin(void* owner, float x, float y) {
michael@0 145 // GrPrintf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y);
michael@0 146
michael@0 147 int index = this->findRec(owner);
michael@0 148 if (index >= 0) {
michael@0 149 this->flushLocalM();
michael@0 150 fTouches.removeShuffle(index);
michael@0 151 SkDebugf("---- already exists, removing\n");
michael@0 152 }
michael@0 153
michael@0 154 if (fTouches.count() == 2) {
michael@0 155 return;
michael@0 156 }
michael@0 157
michael@0 158 this->flushLocalM();
michael@0 159 fFlinger.stop();
michael@0 160
michael@0 161 this->appendNewRec(owner, x, y);
michael@0 162
michael@0 163 switch (fTouches.count()) {
michael@0 164 case 1:
michael@0 165 fState = kTranslate_State;
michael@0 166 break;
michael@0 167 case 2:
michael@0 168 fState = kZoom_State;
michael@0 169 break;
michael@0 170 default:
michael@0 171 break;
michael@0 172 }
michael@0 173 }
michael@0 174
michael@0 175 int SkTouchGesture::findRec(void* owner) const {
michael@0 176 for (int i = 0; i < fTouches.count(); i++) {
michael@0 177 if (owner == fTouches[i].fOwner) {
michael@0 178 return i;
michael@0 179 }
michael@0 180 }
michael@0 181 return -1;
michael@0 182 }
michael@0 183
michael@0 184 static SkScalar center(float pos0, float pos1) {
michael@0 185 return (pos0 + pos1) * 0.5f;
michael@0 186 }
michael@0 187
michael@0 188 static const float MAX_ZOOM_SCALE = 4;
michael@0 189 static const float MIN_ZOOM_SCALE = 0.25f;
michael@0 190
michael@0 191 float SkTouchGesture::limitTotalZoom(float scale) const {
michael@0 192 // this query works 'cause we know that we're square-scale w/ no skew/rotation
michael@0 193 const float curr = SkScalarToFloat(fGlobalM[0]);
michael@0 194
michael@0 195 if (scale > 1 && curr * scale > MAX_ZOOM_SCALE) {
michael@0 196 scale = MAX_ZOOM_SCALE / curr;
michael@0 197 } else if (scale < 1 && curr * scale < MIN_ZOOM_SCALE) {
michael@0 198 scale = MIN_ZOOM_SCALE / curr;
michael@0 199 }
michael@0 200 return scale;
michael@0 201 }
michael@0 202
michael@0 203 void SkTouchGesture::touchMoved(void* owner, float x, float y) {
michael@0 204 // GrPrintf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
michael@0 205
michael@0 206 SkASSERT(kEmpty_State != fState);
michael@0 207
michael@0 208 int index = this->findRec(owner);
michael@0 209 if (index < 0) {
michael@0 210 // not found, so I guess we should add it...
michael@0 211 SkDebugf("---- add missing begin\n");
michael@0 212 this->appendNewRec(owner, x, y);
michael@0 213 index = fTouches.count() - 1;
michael@0 214 }
michael@0 215
michael@0 216 Rec& rec = fTouches[index];
michael@0 217
michael@0 218 // not sure how valuable this is
michael@0 219 if (fTouches.count() == 2) {
michael@0 220 if (close_enough_for_jitter(rec.fLastX, rec.fLastY, x, y)) {
michael@0 221 // GrPrintf("--- drop touchMove, withing jitter tolerance %g %g\n", rec.fLastX - x, rec.fLastY - y);
michael@0 222 return;
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 rec.fPrevX = rec.fLastX; rec.fLastX = x;
michael@0 227 rec.fPrevY = rec.fLastY; rec.fLastY = y;
michael@0 228 rec.fPrevT = rec.fLastT; rec.fLastT = SkTime::GetMSecs();
michael@0 229
michael@0 230 switch (fTouches.count()) {
michael@0 231 case 1: {
michael@0 232 float dx = rec.fLastX - rec.fStartX;
michael@0 233 float dy = rec.fLastY - rec.fStartY;
michael@0 234 dx = (float)sk_float_round2int(dx);
michael@0 235 dy = (float)sk_float_round2int(dy);
michael@0 236 fLocalM.setTranslate(dx, dy);
michael@0 237 } break;
michael@0 238 case 2: {
michael@0 239 SkASSERT(kZoom_State == fState);
michael@0 240 const Rec& rec0 = fTouches[0];
michael@0 241 const Rec& rec1 = fTouches[1];
michael@0 242
michael@0 243 float scale = this->computePinch(rec0, rec1);
michael@0 244 scale = this->limitTotalZoom(scale);
michael@0 245
michael@0 246 fLocalM.setTranslate(-center(rec0.fStartX, rec1.fStartX),
michael@0 247 -center(rec0.fStartY, rec1.fStartY));
michael@0 248 fLocalM.postScale(scale, scale);
michael@0 249 fLocalM.postTranslate(center(rec0.fLastX, rec1.fLastX),
michael@0 250 center(rec0.fLastY, rec1.fLastY));
michael@0 251 } break;
michael@0 252 default:
michael@0 253 break;
michael@0 254 }
michael@0 255 }
michael@0 256
michael@0 257 void SkTouchGesture::touchEnd(void* owner) {
michael@0 258 // GrPrintf("--- %d touchEnd %p\n", fTouches.count(), owner);
michael@0 259
michael@0 260 int index = this->findRec(owner);
michael@0 261 if (index < 0) {
michael@0 262 SkDebugf("--- not found\n");
michael@0 263 return;
michael@0 264 }
michael@0 265
michael@0 266 const Rec& rec = fTouches[index];
michael@0 267 if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
michael@0 268 return;
michael@0 269 }
michael@0 270
michael@0 271 // count() reflects the number before we removed the owner
michael@0 272 switch (fTouches.count()) {
michael@0 273 case 1: {
michael@0 274 this->flushLocalM();
michael@0 275 float dx = rec.fLastX - rec.fPrevX;
michael@0 276 float dy = rec.fLastY - rec.fPrevY;
michael@0 277 float dur = (rec.fLastT - rec.fPrevT) * 0.001f;
michael@0 278 if (dur > 0) {
michael@0 279 fFlinger.reset(dx / dur, dy / dur);
michael@0 280 }
michael@0 281 fState = kEmpty_State;
michael@0 282 } break;
michael@0 283 case 2:
michael@0 284 this->flushLocalM();
michael@0 285 SkASSERT(kZoom_State == fState);
michael@0 286 fState = kEmpty_State;
michael@0 287 break;
michael@0 288 default:
michael@0 289 SkASSERT(kZoom_State == fState);
michael@0 290 break;
michael@0 291 }
michael@0 292
michael@0 293 fTouches.removeShuffle(index);
michael@0 294 }
michael@0 295
michael@0 296 float SkTouchGesture::computePinch(const Rec& rec0, const Rec& rec1) {
michael@0 297 double dx = rec0.fStartX - rec1.fStartX;
michael@0 298 double dy = rec0.fStartY - rec1.fStartY;
michael@0 299 double dist0 = sqrt(dx*dx + dy*dy);
michael@0 300
michael@0 301 dx = rec0.fLastX - rec1.fLastX;
michael@0 302 dy = rec0.fLastY - rec1.fLastY;
michael@0 303 double dist1 = sqrt(dx*dx + dy*dy);
michael@0 304
michael@0 305 double scale = dist1 / dist0;
michael@0 306 return (float)scale;
michael@0 307 }
michael@0 308
michael@0 309 bool SkTouchGesture::handleDblTap(float x, float y) {
michael@0 310 bool found = false;
michael@0 311 SkMSec now = SkTime::GetMSecs();
michael@0 312 if (now - fLastUpT <= MAX_DBL_TAP_INTERVAL) {
michael@0 313 if (SkPoint::Length(fLastUpP.fX - x,
michael@0 314 fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) {
michael@0 315 fFlinger.stop();
michael@0 316 fLocalM.reset();
michael@0 317 fGlobalM.reset();
michael@0 318 fTouches.reset();
michael@0 319 fState = kEmpty_State;
michael@0 320 found = true;
michael@0 321 }
michael@0 322 }
michael@0 323
michael@0 324 fLastUpT = now;
michael@0 325 fLastUpP.set(x, y);
michael@0 326 return found;
michael@0 327 }

mercurial