2012-07-19 23:48:25 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et tw=80 : */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "CompositorParent.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2012-08-08 21:39:02 -07:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-08-06 13:32:11 -07:00
|
|
|
#include "mozilla/Constants.h"
|
2012-07-19 23:48:25 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
#include "mozilla/XPCOM.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
2012-08-08 21:39:02 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-07-19 23:48:25 -07:00
|
|
|
#include "AsyncPanZoomController.h"
|
|
|
|
#include "GestureEventListener.h"
|
|
|
|
#include "nsIThreadManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-07-31 16:19:25 -07:00
|
|
|
#include "Layers.h"
|
2012-08-08 21:39:02 -07:00
|
|
|
#include "AnimationCommon.h"
|
|
|
|
|
|
|
|
using namespace mozilla::css;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2012-08-08 21:39:11 -07:00
|
|
|
const float AsyncPanZoomController::TOUCH_START_TOLERANCE = 1.0f/16.0f;
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
static const float EPSILON = 0.0001;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum amount of time while panning before sending a viewport change. This
|
|
|
|
* will asynchronously repaint the page. It is also forced when panning stops.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t PAN_REPAINT_INTERVAL = 250;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum amount of time flinging before sending a viewport change. This will
|
|
|
|
* asynchronously repaint the page.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t FLING_REPAINT_INTERVAL = 75;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-07-22 18:51:23 -07:00
|
|
|
/**
|
|
|
|
* Minimum amount of speed along an axis before we begin painting far ahead by
|
|
|
|
* adjusting the displayport.
|
|
|
|
*/
|
2012-09-28 21:02:45 -07:00
|
|
|
static const float MIN_SKATE_SPEED = 0.7f;
|
2012-07-22 18:51:23 -07:00
|
|
|
|
2012-08-08 21:39:02 -07:00
|
|
|
/**
|
|
|
|
* Duration of a zoom to animation.
|
|
|
|
*/
|
|
|
|
static const TimeDuration ZOOM_TO_DURATION = TimeDuration::FromSeconds(0.25);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computed time function used for sampling frames of a zoom to animation.
|
|
|
|
*/
|
|
|
|
StaticAutoPtr<ComputedTimingFunction> gComputedTimingFunction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum zoom amount, always used, even if a page asks for higher.
|
|
|
|
*/
|
|
|
|
static const double MAX_ZOOM = 8.0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum zoom amount, always used, even if a page asks for lower.
|
|
|
|
*/
|
|
|
|
static const double MIN_ZOOM = 0.125;
|
|
|
|
|
2012-08-21 21:37:06 -07:00
|
|
|
/**
|
|
|
|
* Amount of time before we timeout touch event listeners. For example, if
|
|
|
|
* content is being unruly/slow and we don't get a response back within this
|
|
|
|
* time, we will just pretend that content did not preventDefault any touch
|
|
|
|
* events we dispatched to it.
|
|
|
|
*/
|
|
|
|
static const int TOUCH_LISTENER_TIMEOUT = 300;
|
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
/**
|
|
|
|
* Number of samples to store of how long it took to paint after the previous
|
|
|
|
* requests.
|
|
|
|
*/
|
|
|
|
static const int NUM_PAINT_DURATION_SAMPLES = 3;
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
AsyncPanZoomController::AsyncPanZoomController(GeckoContentController* aGeckoContentController,
|
|
|
|
GestureBehavior aGestures)
|
|
|
|
: mGeckoContentController(aGeckoContentController),
|
2012-08-21 21:37:06 -07:00
|
|
|
mTouchListenerTimeoutTask(nullptr),
|
2012-07-19 23:48:25 -07:00
|
|
|
mX(this),
|
|
|
|
mY(this),
|
2012-09-28 19:18:18 -07:00
|
|
|
mAllowZoom(true),
|
|
|
|
mMinZoom(MIN_ZOOM),
|
|
|
|
mMaxZoom(MAX_ZOOM),
|
2012-07-19 23:48:25 -07:00
|
|
|
mMonitor("AsyncPanZoomController"),
|
|
|
|
mLastSampleTime(TimeStamp::Now()),
|
|
|
|
mState(NOTHING),
|
2012-07-27 00:33:48 -07:00
|
|
|
mDPI(72),
|
2012-09-28 21:02:41 -07:00
|
|
|
mWaitingForContentToPaint(false),
|
2012-08-21 21:37:06 -07:00
|
|
|
mDisableNextTouchBatch(false),
|
|
|
|
mHandlingTouchQueue(false)
|
2012-07-19 23:48:25 -07:00
|
|
|
{
|
|
|
|
if (aGestures == USE_GESTURE_DETECTOR) {
|
|
|
|
mGestureEventListener = new GestureEventListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDPI(mDPI);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
if (!gComputedTimingFunction) {
|
|
|
|
gComputedTimingFunction = new ComputedTimingFunction();
|
|
|
|
gComputedTimingFunction->Init(
|
|
|
|
nsTimingFunction(NS_STYLE_TRANSITION_TIMING_FUNCTION_EASE));
|
|
|
|
ClearOnShutdown(&gComputedTimingFunction);
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AsyncPanZoomController::~AsyncPanZoomController() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static gfx::Point
|
|
|
|
WidgetSpaceToCompensatedViewportSpace(const gfx::Point& aPoint,
|
|
|
|
gfxFloat aCurrentZoom)
|
|
|
|
{
|
|
|
|
// Transform the input point from local widget space to the content document
|
|
|
|
// space that the user is seeing, from last composite.
|
|
|
|
gfx::Point pt(aPoint);
|
|
|
|
pt = pt / aCurrentZoom;
|
|
|
|
|
|
|
|
// FIXME/bug 775451: this doesn't attempt to compensate for content transforms
|
|
|
|
// in effect on the compositor. The problem is that it's very hard for us to
|
|
|
|
// know what content CSS pixel is at widget point 0,0 based on information
|
|
|
|
// available here. So we use this hacky implementation for now, which works
|
|
|
|
// in quiescent states.
|
|
|
|
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus
|
2012-08-21 21:37:06 -07:00
|
|
|
AsyncPanZoomController::ReceiveInputEvent(const nsInputEvent& aEvent,
|
|
|
|
nsInputEvent* aOutEvent)
|
2012-07-19 23:48:25 -07:00
|
|
|
{
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat currentResolution;
|
2012-07-19 23:48:25 -07:00
|
|
|
gfx::Point currentScrollOffset, lastScrollOffset;
|
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
2012-10-11 22:46:24 -07:00
|
|
|
currentResolution = CalculateResolution(mFrameMetrics).width;
|
2012-09-28 19:16:34 -07:00
|
|
|
currentScrollOffset = gfx::Point(mFrameMetrics.mScrollOffset.x,
|
|
|
|
mFrameMetrics.mScrollOffset.y);
|
|
|
|
lastScrollOffset = gfx::Point(mLastContentPaintMetrics.mScrollOffset.x,
|
|
|
|
mLastContentPaintMetrics.mScrollOffset.y);
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status;
|
|
|
|
switch (aEvent.eventStructType) {
|
|
|
|
case NS_TOUCH_EVENT: {
|
|
|
|
MultiTouchInput event(static_cast<const nsTouchEvent&>(aEvent));
|
2012-08-21 21:37:06 -07:00
|
|
|
status = ReceiveInputEvent(event);
|
2012-07-19 23:48:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NS_MOUSE_EVENT: {
|
|
|
|
MultiTouchInput event(static_cast<const nsMouseEvent&>(aEvent));
|
2012-08-21 21:37:06 -07:00
|
|
|
status = ReceiveInputEvent(event);
|
2012-07-19 23:48:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aEvent.eventStructType) {
|
|
|
|
case NS_TOUCH_EVENT: {
|
|
|
|
nsTouchEvent* touchEvent = static_cast<nsTouchEvent*>(aOutEvent);
|
|
|
|
const nsTArray<nsCOMPtr<nsIDOMTouch> >& touches = touchEvent->touches;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < touches.Length(); ++i) {
|
2012-07-19 23:48:25 -07:00
|
|
|
nsIDOMTouch* touch = touches[i];
|
|
|
|
if (touch) {
|
|
|
|
gfx::Point refPoint = WidgetSpaceToCompensatedViewportSpace(
|
|
|
|
gfx::Point(touch->mRefPoint.x, touch->mRefPoint.y),
|
2012-10-11 22:46:24 -07:00
|
|
|
currentResolution);
|
2012-07-19 23:48:25 -07:00
|
|
|
touch->mRefPoint = nsIntPoint(refPoint.x, refPoint.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
gfx::Point refPoint = WidgetSpaceToCompensatedViewportSpace(
|
|
|
|
gfx::Point(aOutEvent->refPoint.x, aOutEvent->refPoint.y),
|
2012-10-11 22:46:24 -07:00
|
|
|
currentResolution);
|
2012-07-19 23:48:25 -07:00
|
|
|
aOutEvent->refPoint = nsIntPoint(refPoint.x, refPoint.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-08-21 21:37:06 -07:00
|
|
|
nsEventStatus AsyncPanZoomController::ReceiveInputEvent(const InputData& aEvent) {
|
2012-08-21 21:37:09 -07:00
|
|
|
// If we may have touch listeners, we enable the machinery that allows touch
|
|
|
|
// listeners to preventDefault any touch inputs. This should not happen unless
|
|
|
|
// there are actually touch listeners as it introduces potentially unbounded
|
|
|
|
// lag because it causes a round-trip through content. Usually, if content is
|
|
|
|
// responding in a timely fashion, this only introduces a nearly constant few
|
|
|
|
// hundred ms of lag.
|
2012-08-21 21:37:06 -07:00
|
|
|
if (mFrameMetrics.mMayHaveTouchListeners && aEvent.mInputType == MULTITOUCH_INPUT &&
|
|
|
|
(mState == NOTHING || mState == TOUCHING || mState == PANNING)) {
|
|
|
|
const MultiTouchInput& multiTouchInput = aEvent.AsMultiTouchInput();
|
|
|
|
if (multiTouchInput.mType == MultiTouchInput::MULTITOUCH_START) {
|
|
|
|
SetState(WAITING_LISTENERS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == WAITING_LISTENERS || mHandlingTouchQueue) {
|
|
|
|
if (aEvent.mInputType == MULTITOUCH_INPUT) {
|
|
|
|
const MultiTouchInput& multiTouchInput = aEvent.AsMultiTouchInput();
|
|
|
|
mTouchQueue.AppendElement(multiTouchInput);
|
|
|
|
|
|
|
|
if (!mTouchListenerTimeoutTask) {
|
|
|
|
mTouchListenerTimeoutTask =
|
|
|
|
NewRunnableMethod(this, &AsyncPanZoomController::TimeoutTouchListeners);
|
|
|
|
|
|
|
|
MessageLoop::current()->PostDelayedTask(
|
|
|
|
FROM_HERE,
|
|
|
|
mTouchListenerTimeoutTask,
|
|
|
|
TOUCH_LISTENER_TIMEOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HandleInputEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
nsEventStatus AsyncPanZoomController::HandleInputEvent(const InputData& aEvent) {
|
|
|
|
nsEventStatus rv = nsEventStatus_eIgnore;
|
|
|
|
|
2012-08-08 13:38:10 -07:00
|
|
|
if (mGestureEventListener && !mDisableNextTouchBatch) {
|
2012-08-21 21:37:06 -07:00
|
|
|
rv = mGestureEventListener->HandleInputEvent(aEvent);
|
2012-07-19 23:48:25 -07:00
|
|
|
if (rv == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aEvent.mInputType) {
|
|
|
|
case MULTITOUCH_INPUT: {
|
|
|
|
const MultiTouchInput& multiTouchInput = aEvent.AsMultiTouchInput();
|
|
|
|
switch (multiTouchInput.mType) {
|
|
|
|
case MultiTouchInput::MULTITOUCH_START: rv = OnTouchStart(multiTouchInput); break;
|
|
|
|
case MultiTouchInput::MULTITOUCH_MOVE: rv = OnTouchMove(multiTouchInput); break;
|
|
|
|
case MultiTouchInput::MULTITOUCH_END: rv = OnTouchEnd(multiTouchInput); break;
|
|
|
|
case MultiTouchInput::MULTITOUCH_CANCEL: rv = OnTouchCancel(multiTouchInput); break;
|
|
|
|
default: NS_WARNING("Unhandled multitouch"); break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PINCHGESTURE_INPUT: {
|
|
|
|
const PinchGestureInput& pinchGestureInput = aEvent.AsPinchGestureInput();
|
|
|
|
switch (pinchGestureInput.mType) {
|
|
|
|
case PinchGestureInput::PINCHGESTURE_START: rv = OnScaleBegin(pinchGestureInput); break;
|
|
|
|
case PinchGestureInput::PINCHGESTURE_SCALE: rv = OnScale(pinchGestureInput); break;
|
|
|
|
case PinchGestureInput::PINCHGESTURE_END: rv = OnScaleEnd(pinchGestureInput); break;
|
|
|
|
default: NS_WARNING("Unhandled pinch gesture"); break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TAPGESTURE_INPUT: {
|
|
|
|
const TapGestureInput& tapGestureInput = aEvent.AsTapGestureInput();
|
|
|
|
switch (tapGestureInput.mType) {
|
|
|
|
case TapGestureInput::TAPGESTURE_LONG: rv = OnLongPress(tapGestureInput); break;
|
|
|
|
case TapGestureInput::TAPGESTURE_UP: rv = OnSingleTapUp(tapGestureInput); break;
|
|
|
|
case TapGestureInput::TAPGESTURE_CONFIRMED: rv = OnSingleTapConfirmed(tapGestureInput); break;
|
|
|
|
case TapGestureInput::TAPGESTURE_DOUBLE: rv = OnDoubleTap(tapGestureInput); break;
|
|
|
|
case TapGestureInput::TAPGESTURE_CANCEL: rv = OnCancelTap(tapGestureInput); break;
|
|
|
|
default: NS_WARNING("Unhandled tap gesture"); break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: NS_WARNING("Unhandled input event"); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastEventTime = aEvent.mTime;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnTouchStart(const MultiTouchInput& aEvent) {
|
|
|
|
SingleTouchData& touch = GetFirstSingleTouch(aEvent);
|
|
|
|
|
|
|
|
nsIntPoint point = touch.mScreenPoint;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t xPos = point.x, yPos = point.y;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
switch (mState) {
|
2012-08-08 21:39:02 -07:00
|
|
|
case ANIMATING_ZOOM:
|
|
|
|
// We just interrupted a double-tap animation, so force a redraw in case
|
|
|
|
// this touchstart is just a tap that doesn't end up triggering a redraw.
|
2012-09-28 19:16:38 -07:00
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
// Bring the resolution back in sync with the zoom.
|
|
|
|
SetZoomAndResolution(mFrameMetrics.mZoom.width);
|
|
|
|
RequestContentRepaint();
|
|
|
|
ScheduleComposite();
|
|
|
|
}
|
2012-08-08 21:39:02 -07:00
|
|
|
// Fall through.
|
2012-07-19 23:48:25 -07:00
|
|
|
case FLING:
|
2012-08-07 20:40:48 -07:00
|
|
|
CancelAnimation();
|
2012-07-19 23:48:25 -07:00
|
|
|
// Fall through.
|
|
|
|
case NOTHING:
|
|
|
|
mX.StartTouch(xPos);
|
|
|
|
mY.StartTouch(yPos);
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(TOUCHING);
|
2012-07-19 23:48:25 -07:00
|
|
|
break;
|
|
|
|
case TOUCHING:
|
|
|
|
case PANNING:
|
|
|
|
case PINCHING:
|
2012-08-21 21:37:06 -07:00
|
|
|
case WAITING_LISTENERS:
|
2012-07-19 23:48:25 -07:00
|
|
|
NS_WARNING("Received impossible touch in OnTouchStart");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unhandled case in OnTouchStart");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnTouchMove(const MultiTouchInput& aEvent) {
|
2012-08-08 13:38:06 -07:00
|
|
|
if (mDisableNextTouchBatch) {
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
|
|
|
case NOTHING:
|
2012-08-08 21:39:02 -07:00
|
|
|
case ANIMATING_ZOOM:
|
2012-07-19 23:48:25 -07:00
|
|
|
// May happen if the user double-taps and drags without lifting after the
|
|
|
|
// second tap. Ignore the move if this happens.
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
|
|
|
|
case TOUCHING: {
|
2012-08-08 21:39:11 -07:00
|
|
|
float panThreshold = TOUCH_START_TOLERANCE * mDPI;
|
2012-08-07 18:51:11 -07:00
|
|
|
UpdateWithTouchAtDevicePoint(aEvent);
|
2012-08-08 13:38:16 -07:00
|
|
|
|
2012-07-22 20:49:07 -07:00
|
|
|
if (PanDistance() < panThreshold) {
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
2012-08-08 13:38:16 -07:00
|
|
|
|
2012-07-22 20:49:07 -07:00
|
|
|
StartPanning(aEvent);
|
2012-08-08 13:38:16 -07:00
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PANNING:
|
|
|
|
TrackTouch(aEvent);
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
|
|
|
|
case PINCHING:
|
|
|
|
// The scale gesture listener should have handled this.
|
|
|
|
NS_WARNING("Gesture listener should have handled pinching in OnTouchMove.");
|
|
|
|
return nsEventStatus_eIgnore;
|
2012-08-21 21:37:06 -07:00
|
|
|
|
|
|
|
case WAITING_LISTENERS:
|
|
|
|
NS_WARNING("Received impossible touch in OnTouchMove");
|
|
|
|
break;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnTouchEnd(const MultiTouchInput& aEvent) {
|
2012-08-08 13:38:06 -07:00
|
|
|
if (mDisableNextTouchBatch) {
|
|
|
|
mDisableNextTouchBatch = false;
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
switch (mState) {
|
|
|
|
case FLING:
|
|
|
|
// Should never happen.
|
|
|
|
NS_WARNING("Received impossible touch end in OnTouchEnd.");
|
|
|
|
// Fall through.
|
2012-08-08 21:39:02 -07:00
|
|
|
case ANIMATING_ZOOM:
|
2012-07-19 23:48:25 -07:00
|
|
|
case NOTHING:
|
|
|
|
// May happen if the user double-taps and drags without lifting after the
|
|
|
|
// second tap. Ignore if this happens.
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
|
|
|
|
case TOUCHING:
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(NOTHING);
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
|
|
|
|
case PANNING:
|
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
ScheduleComposite();
|
|
|
|
RequestContentRepaint();
|
|
|
|
}
|
2012-08-13 21:08:38 -07:00
|
|
|
mX.EndTouch();
|
|
|
|
mY.EndTouch();
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(FLING);
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2012-08-21 21:37:06 -07:00
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
case PINCHING:
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(NOTHING);
|
2012-07-19 23:48:25 -07:00
|
|
|
// Scale gesture listener should have handled this.
|
|
|
|
NS_WARNING("Gesture listener should have handled pinching in OnTouchEnd.");
|
|
|
|
return nsEventStatus_eIgnore;
|
2012-08-21 21:37:06 -07:00
|
|
|
|
|
|
|
case WAITING_LISTENERS:
|
|
|
|
NS_WARNING("Received impossible touch in OnTouchEnd");
|
|
|
|
break;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnTouchCancel(const MultiTouchInput& aEvent) {
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(NOTHING);
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnScaleBegin(const PinchGestureInput& aEvent) {
|
2012-09-28 19:18:18 -07:00
|
|
|
if (!mAllowZoom) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(PINCHING);
|
2012-07-19 23:48:25 -07:00
|
|
|
mLastZoomFocus = aEvent.mFocusPoint;
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnScale(const PinchGestureInput& aEvent) {
|
2012-09-28 19:18:18 -07:00
|
|
|
if (mState != PINCHING) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
float prevSpan = aEvent.mPreviousSpan;
|
|
|
|
if (fabsf(prevSpan) <= EPSILON || fabsf(aEvent.mCurrentSpan) <= EPSILON) {
|
|
|
|
// We're still handling it; we've just decided to throw this event away.
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
float spanRatio = aEvent.mCurrentSpan / aEvent.mPreviousSpan;
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
|
|
|
gfxFloat userZoom = mFrameMetrics.mZoom.width;
|
2012-07-19 23:48:25 -07:00
|
|
|
nsIntPoint focusPoint = aEvent.mFocusPoint;
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat xFocusChange = (mLastZoomFocus.x - focusPoint.x) / resolution;
|
|
|
|
gfxFloat yFocusChange = (mLastZoomFocus.y - focusPoint.y) / resolution;
|
2012-07-19 23:48:25 -07:00
|
|
|
// If displacing by the change in focus point will take us off page bounds,
|
|
|
|
// then reduce the displacement such that it doesn't.
|
|
|
|
if (mX.DisplacementWillOverscroll(xFocusChange) != Axis::OVERSCROLL_NONE) {
|
|
|
|
xFocusChange -= mX.DisplacementWillOverscrollAmount(xFocusChange);
|
|
|
|
}
|
|
|
|
if (mY.DisplacementWillOverscroll(yFocusChange) != Axis::OVERSCROLL_NONE) {
|
|
|
|
yFocusChange -= mY.DisplacementWillOverscrollAmount(yFocusChange);
|
|
|
|
}
|
2012-08-21 21:37:15 -07:00
|
|
|
ScrollBy(gfx::Point(xFocusChange, yFocusChange));
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// When we zoom in with focus, we can zoom too much towards the boundaries
|
|
|
|
// that we actually go over them. These are the needed displacements along
|
|
|
|
// either axis such that we don't overscroll the boundaries when zooming.
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat neededDisplacementX = 0, neededDisplacementY = 0;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// Only do the scaling if we won't go over 8x zoom in or out.
|
2012-10-11 22:46:24 -07:00
|
|
|
bool doScale = (spanRatio > 1.0 && userZoom < mMaxZoom) ||
|
|
|
|
(spanRatio < 1.0 && userZoom > mMinZoom);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// If this zoom will take it over 8x zoom in either direction, but it's not
|
|
|
|
// already there, then normalize it.
|
2012-10-11 22:46:24 -07:00
|
|
|
if (userZoom * spanRatio > mMaxZoom) {
|
|
|
|
spanRatio = userZoom / mMaxZoom;
|
|
|
|
} else if (userZoom * spanRatio < mMinZoom) {
|
|
|
|
spanRatio = userZoom / mMinZoom;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doScale) {
|
|
|
|
switch (mX.ScaleWillOverscroll(spanRatio, focusPoint.x))
|
|
|
|
{
|
|
|
|
case Axis::OVERSCROLL_NONE:
|
|
|
|
break;
|
|
|
|
case Axis::OVERSCROLL_MINUS:
|
|
|
|
case Axis::OVERSCROLL_PLUS:
|
|
|
|
neededDisplacementX = -mX.ScaleWillOverscrollAmount(spanRatio, focusPoint.x);
|
|
|
|
break;
|
|
|
|
case Axis::OVERSCROLL_BOTH:
|
|
|
|
// If scaling this way will make us overscroll in both directions, then
|
|
|
|
// we must already be at the maximum zoomed out amount. In this case, we
|
|
|
|
// don't want to allow this scaling to go through and instead clamp it
|
|
|
|
// here.
|
|
|
|
doScale = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doScale) {
|
|
|
|
switch (mY.ScaleWillOverscroll(spanRatio, focusPoint.y))
|
|
|
|
{
|
|
|
|
case Axis::OVERSCROLL_NONE:
|
|
|
|
break;
|
|
|
|
case Axis::OVERSCROLL_MINUS:
|
|
|
|
case Axis::OVERSCROLL_PLUS:
|
|
|
|
neededDisplacementY = -mY.ScaleWillOverscrollAmount(spanRatio, focusPoint.y);
|
|
|
|
break;
|
|
|
|
case Axis::OVERSCROLL_BOTH:
|
|
|
|
doScale = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doScale) {
|
2012-10-11 22:46:24 -07:00
|
|
|
ScaleWithFocus(userZoom * spanRatio, focusPoint);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
if (neededDisplacementX != 0 || neededDisplacementY != 0) {
|
2012-08-21 21:37:15 -07:00
|
|
|
ScrollBy(gfx::Point(neededDisplacementX, neededDisplacementY));
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ScheduleComposite();
|
|
|
|
// We don't want to redraw on every scale, so don't use
|
|
|
|
// RequestContentRepaint()
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastZoomFocus = focusPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnScaleEnd(const PinchGestureInput& aEvent) {
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(PANNING);
|
2012-07-19 23:48:25 -07:00
|
|
|
mX.StartTouch(aEvent.mFocusPoint.x);
|
|
|
|
mY.StartTouch(aEvent.mFocusPoint.y);
|
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
ScheduleComposite();
|
|
|
|
RequestContentRepaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnLongPress(const TapGestureInput& aEvent) {
|
2012-11-08 11:35:02 -08:00
|
|
|
if (mGeckoContentController) {
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
|
|
|
gfx::Point point = WidgetSpaceToCompensatedViewportSpace(
|
|
|
|
gfx::Point(aEvent.mPoint.x, aEvent.mPoint.y),
|
|
|
|
resolution);
|
|
|
|
mGeckoContentController->HandleLongTap(nsIntPoint(NS_lround(point.x),
|
|
|
|
NS_lround(point.y)));
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnSingleTapUp(const TapGestureInput& aEvent) {
|
2012-09-14 18:16:32 -07:00
|
|
|
if (mGeckoContentController) {
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
2012-09-14 18:16:32 -07:00
|
|
|
gfx::Point point = WidgetSpaceToCompensatedViewportSpace(
|
|
|
|
gfx::Point(aEvent.mPoint.x, aEvent.mPoint.y),
|
2012-10-11 22:46:24 -07:00
|
|
|
resolution);
|
|
|
|
mGeckoContentController->HandleSingleTap(nsIntPoint(NS_lround(point.x),
|
|
|
|
NS_lround(point.y)));
|
2012-09-14 18:16:32 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnSingleTapConfirmed(const TapGestureInput& aEvent) {
|
|
|
|
// XXX: Implement this.
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnDoubleTap(const TapGestureInput& aEvent) {
|
2012-08-08 21:39:02 -07:00
|
|
|
if (mGeckoContentController) {
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
2012-09-28 19:18:18 -07:00
|
|
|
if (mAllowZoom) {
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
2012-09-28 19:18:18 -07:00
|
|
|
gfx::Point point = WidgetSpaceToCompensatedViewportSpace(
|
|
|
|
gfx::Point(aEvent.mPoint.x, aEvent.mPoint.y),
|
2012-10-11 22:46:24 -07:00
|
|
|
resolution);
|
|
|
|
mGeckoContentController->HandleDoubleTap(nsIntPoint(NS_lround(point.x),
|
|
|
|
NS_lround(point.y)));
|
2012-09-28 19:18:18 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:39:02 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus AsyncPanZoomController::OnCancelTap(const TapGestureInput& aEvent) {
|
|
|
|
// XXX: Implement this.
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:49:07 -07:00
|
|
|
float AsyncPanZoomController::PanDistance() {
|
2012-08-08 13:38:16 -07:00
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
return NS_hypot(mX.PanDistance(), mY.PanDistance());
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 13:38:23 -07:00
|
|
|
const gfx::Point AsyncPanZoomController::GetVelocityVector() {
|
|
|
|
return gfx::Point(mX.GetVelocity(), mY.GetVelocity());
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
const gfx::Point AsyncPanZoomController::GetAccelerationVector() {
|
|
|
|
return gfx::Point(mX.GetAccelerationFactor(), mY.GetAccelerationFactor());
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:49:07 -07:00
|
|
|
void AsyncPanZoomController::StartPanning(const MultiTouchInput& aEvent) {
|
|
|
|
float dx = mX.PanDistance(),
|
|
|
|
dy = mY.PanDistance();
|
|
|
|
|
|
|
|
double angle = atan2(dy, dx); // range [-pi, pi]
|
|
|
|
angle = fabs(angle); // range [0, pi]
|
|
|
|
|
2012-08-08 13:38:20 -07:00
|
|
|
SetState(PANNING);
|
2012-07-22 20:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::UpdateWithTouchAtDevicePoint(const MultiTouchInput& aEvent) {
|
2012-07-19 23:48:25 -07:00
|
|
|
SingleTouchData& touch = GetFirstSingleTouch(aEvent);
|
|
|
|
nsIntPoint point = touch.mScreenPoint;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t xPos = point.x, yPos = point.y;
|
2012-07-22 18:43:37 -07:00
|
|
|
TimeDuration timeDelta = TimeDuration().FromMilliseconds(aEvent.mTime - mLastEventTime);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// Probably a duplicate event, just throw it away.
|
2012-07-22 18:43:37 -07:00
|
|
|
if (timeDelta.ToMilliseconds() <= EPSILON) {
|
2012-07-19 23:48:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:49:07 -07:00
|
|
|
mX.UpdateWithTouchAtDevicePoint(xPos, timeDelta);
|
|
|
|
mY.UpdateWithTouchAtDevicePoint(yPos, timeDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::TrackTouch(const MultiTouchInput& aEvent) {
|
|
|
|
TimeDuration timeDelta = TimeDuration().FromMilliseconds(aEvent.mTime - mLastEventTime);
|
|
|
|
|
|
|
|
// Probably a duplicate event, just throw it away.
|
|
|
|
if (timeDelta.ToMilliseconds() <= EPSILON) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateWithTouchAtDevicePoint(aEvent);
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
{
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
|
|
|
// We want to inversely scale it because when you're zoomed further in, a
|
|
|
|
// larger swipe should move you a shorter distance.
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat inverseResolution = 1 / CalculateResolution(mFrameMetrics).width;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
int32_t xDisplacement = mX.GetDisplacementForDuration(inverseResolution,
|
|
|
|
timeDelta);
|
|
|
|
int32_t yDisplacement = mY.GetDisplacementForDuration(inverseResolution,
|
|
|
|
timeDelta);
|
2012-07-19 23:48:25 -07:00
|
|
|
if (!xDisplacement && !yDisplacement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-21 21:37:15 -07:00
|
|
|
ScrollBy(gfx::Point(xDisplacement, yDisplacement));
|
2012-07-19 23:48:25 -07:00
|
|
|
ScheduleComposite();
|
|
|
|
|
2012-07-27 00:33:57 -07:00
|
|
|
RequestContentRepaint();
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SingleTouchData& AsyncPanZoomController::GetFirstSingleTouch(const MultiTouchInput& aEvent) {
|
|
|
|
return (SingleTouchData&)aEvent.mTouches[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncPanZoomController::DoFling(const TimeDuration& aDelta) {
|
|
|
|
if (mState != FLING) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-22 18:50:41 -07:00
|
|
|
bool shouldContinueFlingX = mX.FlingApplyFrictionOrCancel(aDelta),
|
|
|
|
shouldContinueFlingY = mY.FlingApplyFrictionOrCancel(aDelta);
|
|
|
|
// If we shouldn't continue the fling, let's just stop and repaint.
|
|
|
|
if (!shouldContinueFlingX && !shouldContinueFlingY) {
|
2012-09-28 21:02:45 -07:00
|
|
|
// Bring the resolution back in sync with the zoom, in case we scaled down
|
|
|
|
// the zoom while accelerating.
|
|
|
|
SetZoomAndResolution(mFrameMetrics.mZoom.width);
|
2012-07-19 23:48:25 -07:00
|
|
|
RequestContentRepaint();
|
|
|
|
mState = NOTHING;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to inversely scale it because when you're zoomed further in, a
|
|
|
|
// larger swipe should move you a shorter distance.
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat inverseResolution = 1 / CalculateResolution(mFrameMetrics).width;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-08-21 21:37:15 -07:00
|
|
|
ScrollBy(gfx::Point(
|
2012-10-11 22:46:24 -07:00
|
|
|
mX.GetDisplacementForDuration(inverseResolution, aDelta),
|
|
|
|
mY.GetDisplacementForDuration(inverseResolution, aDelta)
|
2012-07-19 23:48:25 -07:00
|
|
|
));
|
|
|
|
RequestContentRepaint();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::CancelAnimation() {
|
|
|
|
mState = NOTHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::SetCompositorParent(CompositorParent* aCompositorParent) {
|
|
|
|
mCompositorParent = aCompositorParent;
|
|
|
|
}
|
|
|
|
|
2012-08-21 21:37:15 -07:00
|
|
|
void AsyncPanZoomController::ScrollBy(const gfx::Point& aOffset) {
|
2012-09-28 19:16:34 -07:00
|
|
|
gfx::Point newOffset(mFrameMetrics.mScrollOffset.x + aOffset.x,
|
|
|
|
mFrameMetrics.mScrollOffset.y + aOffset.y);
|
2012-07-19 23:48:25 -07:00
|
|
|
FrameMetrics metrics(mFrameMetrics);
|
2012-09-28 19:16:34 -07:00
|
|
|
metrics.mScrollOffset = newOffset;
|
2012-07-19 23:48:25 -07:00
|
|
|
mFrameMetrics = metrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::SetPageRect(const gfx::Rect& aCSSPageRect) {
|
|
|
|
FrameMetrics metrics = mFrameMetrics;
|
|
|
|
gfx::Rect pageSize = aCSSPageRect;
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// The page rect is the css page rect scaled by the current zoom.
|
2012-10-11 22:46:24 -07:00
|
|
|
pageSize.ScaleInverseRoundOut(resolution);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// Round the page rect so we don't get any truncation, then get the nsIntRect
|
|
|
|
// from this.
|
2012-10-11 22:46:24 -07:00
|
|
|
metrics.mContentRect = nsIntRect(pageSize.x, pageSize.y,
|
|
|
|
pageSize.width, pageSize.height);
|
2012-09-28 19:16:34 -07:00
|
|
|
metrics.mScrollableRect = aCSSPageRect;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
mFrameMetrics = metrics;
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
void AsyncPanZoomController::ScaleWithFocus(float aZoom,
|
|
|
|
const nsIntPoint& aFocus) {
|
|
|
|
float zoomFactor = aZoom / mFrameMetrics.mZoom.width;
|
|
|
|
gfxFloat resolution = CalculateResolution(mFrameMetrics).width;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
SetZoomAndResolution(aZoom);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
// Force a recalculation of the page rect based on the new zoom and the
|
|
|
|
// current CSS page rect (which is unchanged since it's not affected by zoom).
|
2012-09-28 19:16:34 -07:00
|
|
|
SetPageRect(mFrameMetrics.mScrollableRect);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
// If the new scale is very small, we risk multiplying in huge rounding
|
|
|
|
// errors, so don't bother adjusting the scroll offset.
|
2012-10-11 22:46:24 -07:00
|
|
|
if (resolution >= 0.01f) {
|
|
|
|
mFrameMetrics.mScrollOffset.x +=
|
|
|
|
gfxFloat(aFocus.x) * (zoomFactor - 1.0) / resolution;
|
|
|
|
mFrameMetrics.mScrollOffset.y +=
|
|
|
|
gfxFloat(aFocus.y) * (zoomFactor - 1.0) / resolution;
|
2012-09-28 21:02:45 -07:00
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
bool AsyncPanZoomController::EnlargeDisplayPortAlongAxis(float aSkateSizeMultiplier,
|
|
|
|
double aEstimatedPaintDuration,
|
|
|
|
float aCompositionBounds,
|
2012-08-08 13:38:23 -07:00
|
|
|
float aVelocity,
|
2012-09-28 21:02:45 -07:00
|
|
|
float aAcceleration,
|
2012-08-08 13:38:23 -07:00
|
|
|
float* aDisplayPortOffset,
|
|
|
|
float* aDisplayPortLength)
|
|
|
|
{
|
|
|
|
if (fabsf(aVelocity) > MIN_SKATE_SPEED) {
|
2012-09-28 21:02:45 -07:00
|
|
|
// Enlarge the area we paint.
|
|
|
|
*aDisplayPortLength = aCompositionBounds * aSkateSizeMultiplier;
|
|
|
|
// Position the area we paint such that all of the excess that extends past
|
|
|
|
// the screen is on the side towards the velocity.
|
2012-09-28 19:16:34 -07:00
|
|
|
*aDisplayPortOffset = aVelocity > 0 ? 0 : aCompositionBounds - *aDisplayPortLength;
|
2012-09-28 21:02:45 -07:00
|
|
|
|
|
|
|
// Only compensate for acceleration when we actually have any. Otherwise
|
|
|
|
// we'll overcompensate when a user is just panning around without flinging.
|
|
|
|
if (aAcceleration > 1.01f) {
|
|
|
|
// Compensate for acceleration and how long we expect a paint to take. We
|
|
|
|
// try to predict where the viewport will be when painting has finished.
|
|
|
|
*aDisplayPortOffset +=
|
|
|
|
fabsf(aAcceleration) * aVelocity * aCompositionBounds * aEstimatedPaintDuration;
|
|
|
|
// If our velocity is in the negative direction of the axis, we have to
|
|
|
|
// compensate for the fact that our scroll offset is the top-left position
|
|
|
|
// of the viewport. In this case, let's make it relative to the
|
|
|
|
// bottom-right. That way, we'll always be growing the displayport upwards
|
|
|
|
// and to the left when skating negatively.
|
|
|
|
*aDisplayPortOffset -= aVelocity < 0 ? aCompositionBounds : 0;
|
|
|
|
}
|
2012-08-08 13:38:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-28 19:18:18 -07:00
|
|
|
const gfx::Rect AsyncPanZoomController::CalculatePendingDisplayPort(
|
|
|
|
const FrameMetrics& aFrameMetrics,
|
2012-09-28 21:02:45 -07:00
|
|
|
const gfx::Point& aVelocity,
|
|
|
|
const gfx::Point& aAcceleration,
|
|
|
|
double aEstimatedPaintDuration)
|
2012-09-28 19:18:18 -07:00
|
|
|
{
|
2012-09-28 21:02:45 -07:00
|
|
|
// The multiplier we apply to a dimension's length if it is skating. That is,
|
|
|
|
// if it's going above MIN_SKATE_SPEED. We prefer to increase the size of the
|
|
|
|
// Y axis because it is more natural in the case that a user is reading a page
|
|
|
|
// that scrolls up/down. Note that one, both or neither of these may be used
|
|
|
|
// at any instant.
|
|
|
|
const float X_SKATE_SIZE_MULTIPLIER = 3.0f;
|
|
|
|
const float Y_SKATE_SIZE_MULTIPLIER = 3.5f;
|
|
|
|
|
|
|
|
// The multiplier we apply to a dimension's length if it is stationary. We
|
|
|
|
// prefer to increase the size of the Y axis because it is more natural in the
|
|
|
|
// case that a user is reading a page that scrolls up/down. Note that one,
|
|
|
|
// both or neither of these may be used at any instant.
|
|
|
|
const float X_STATIONARY_SIZE_MULTIPLIER = 1.5f;
|
|
|
|
const float Y_STATIONARY_SIZE_MULTIPLIER = 2.5f;
|
|
|
|
|
|
|
|
// If we don't get an estimated paint duration, we probably don't have any
|
|
|
|
// data. In this case, we're dealing with either a stationary frame or a first
|
|
|
|
// paint. In either of these cases, we can just assume it'll take 1 second to
|
|
|
|
// paint. Getting this correct is not important anyways since it's only really
|
|
|
|
// useful when accelerating, which can't be happening at this point.
|
|
|
|
double estimatedPaintDuration =
|
|
|
|
aEstimatedPaintDuration > EPSILON ? aEstimatedPaintDuration : 1.0;
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat resolution = CalculateResolution(aFrameMetrics).width;
|
2012-09-28 19:18:18 -07:00
|
|
|
nsIntRect compositionBounds = aFrameMetrics.mCompositionBounds;
|
2012-10-11 22:46:24 -07:00
|
|
|
compositionBounds.ScaleInverseRoundIn(resolution);
|
2012-09-28 21:02:45 -07:00
|
|
|
const gfx::Rect& scrollableRect = aFrameMetrics.mScrollableRect;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-09-28 19:18:18 -07:00
|
|
|
gfx::Point scrollOffset = aFrameMetrics.mScrollOffset;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-07-22 18:51:23 -07:00
|
|
|
gfx::Rect displayPort(0, 0,
|
2012-09-28 21:02:45 -07:00
|
|
|
compositionBounds.width * X_STATIONARY_SIZE_MULTIPLIER,
|
|
|
|
compositionBounds.height * Y_STATIONARY_SIZE_MULTIPLIER);
|
2012-07-22 18:51:23 -07:00
|
|
|
|
2012-08-08 13:38:23 -07:00
|
|
|
// If there's motion along an axis of movement, and it's above a threshold,
|
|
|
|
// then we want to paint a larger area in the direction of that motion so that
|
|
|
|
// it's less likely to checkerboard.
|
|
|
|
bool enlargedX = EnlargeDisplayPortAlongAxis(
|
2012-09-28 21:02:45 -07:00
|
|
|
X_SKATE_SIZE_MULTIPLIER, estimatedPaintDuration,
|
|
|
|
compositionBounds.width, aVelocity.x, aAcceleration.x,
|
|
|
|
&displayPort.x, &displayPort.width);
|
2012-08-08 13:38:23 -07:00
|
|
|
bool enlargedY = EnlargeDisplayPortAlongAxis(
|
2012-09-28 21:02:45 -07:00
|
|
|
Y_SKATE_SIZE_MULTIPLIER, estimatedPaintDuration,
|
|
|
|
compositionBounds.height, aVelocity.y, aAcceleration.y,
|
|
|
|
&displayPort.y, &displayPort.height);
|
2012-08-08 13:38:23 -07:00
|
|
|
|
|
|
|
if (!enlargedX && !enlargedY) {
|
2012-09-28 21:02:45 -07:00
|
|
|
// Position the x and y such that the screen falls in the middle of the displayport.
|
|
|
|
displayPort.x = -(displayPort.width - compositionBounds.width) / 2;
|
|
|
|
displayPort.y = -(displayPort.height - compositionBounds.height) / 2;
|
2012-08-08 13:38:23 -07:00
|
|
|
} else if (!enlargedX) {
|
2012-09-28 19:16:34 -07:00
|
|
|
displayPort.width = compositionBounds.width;
|
2012-08-08 13:38:23 -07:00
|
|
|
} else if (!enlargedY) {
|
2012-09-28 19:16:34 -07:00
|
|
|
displayPort.height = compositionBounds.height;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
// If we go over the bounds when trying to predict where we will be when this
|
|
|
|
// paint finishes, move it back into the range of the CSS content rect.
|
|
|
|
// FIXME/bug 780395: Generalize this. This code is pretty hacky as it will
|
|
|
|
// probably not work at all for RTL content. This is not intended to be
|
|
|
|
// incredibly accurate; it'll just prevent the entire displayport from being
|
|
|
|
// outside the content rect (which causes bad things to happen).
|
|
|
|
if (enlargedX || enlargedY) {
|
|
|
|
if (scrollOffset.x + compositionBounds.width > scrollableRect.width) {
|
|
|
|
scrollOffset.x -= compositionBounds.width + scrollOffset.x - scrollableRect.width;
|
|
|
|
} else if (scrollOffset.x < scrollableRect.x) {
|
|
|
|
scrollOffset.x = scrollableRect.x;
|
|
|
|
}
|
|
|
|
if (scrollOffset.y + compositionBounds.height > scrollableRect.height) {
|
|
|
|
scrollOffset.y -= compositionBounds.height + scrollOffset.y - scrollableRect.height;
|
|
|
|
} else if (scrollOffset.y < scrollableRect.y) {
|
|
|
|
scrollOffset.y = scrollableRect.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-22 18:51:23 -07:00
|
|
|
gfx::Rect shiftedDisplayPort = displayPort;
|
2012-07-27 00:33:53 -07:00
|
|
|
shiftedDisplayPort.MoveBy(scrollOffset.x, scrollOffset.y);
|
2012-09-28 19:18:18 -07:00
|
|
|
displayPort = shiftedDisplayPort.Intersect(aFrameMetrics.mScrollableRect);
|
2012-07-27 00:33:53 -07:00
|
|
|
displayPort.MoveBy(-scrollOffset.x, -scrollOffset.y);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
return displayPort;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
/*static*/ gfxSize
|
|
|
|
AsyncPanZoomController::CalculateIntrinsicScale(const FrameMetrics& aMetrics)
|
|
|
|
{
|
|
|
|
gfxFloat intrinsicScale = (gfxFloat(aMetrics.mCompositionBounds.width) /
|
|
|
|
gfxFloat(aMetrics.mViewport.width));
|
|
|
|
return gfxSize(intrinsicScale, intrinsicScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ gfxSize
|
|
|
|
AsyncPanZoomController::CalculateResolution(const FrameMetrics& aMetrics)
|
|
|
|
{
|
|
|
|
gfxSize intrinsicScale = CalculateIntrinsicScale(aMetrics);
|
|
|
|
gfxSize userZoom = aMetrics.mZoom;
|
|
|
|
return gfxSize(intrinsicScale.width * userZoom.width,
|
|
|
|
intrinsicScale.height * userZoom.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ gfx::Rect
|
|
|
|
AsyncPanZoomController::CalculateCompositedRectInCssPixels(const FrameMetrics& aMetrics)
|
|
|
|
{
|
|
|
|
gfxSize resolution = CalculateResolution(aMetrics);
|
|
|
|
gfx::Rect rect(aMetrics.mCompositionBounds.x,
|
|
|
|
aMetrics.mCompositionBounds.y,
|
|
|
|
aMetrics.mCompositionBounds.width,
|
|
|
|
aMetrics.mCompositionBounds.height);
|
|
|
|
rect.ScaleInverseRoundIn(resolution.width, resolution.height);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
void AsyncPanZoomController::SetDPI(int aDPI) {
|
|
|
|
mDPI = aDPI;
|
|
|
|
}
|
|
|
|
|
2012-08-08 21:39:11 -07:00
|
|
|
int AsyncPanZoomController::GetDPI() {
|
|
|
|
return mDPI;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
void AsyncPanZoomController::ScheduleComposite() {
|
|
|
|
if (mCompositorParent) {
|
|
|
|
mCompositorParent->ScheduleRenderOnCompositorThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::RequestContentRepaint() {
|
2012-09-28 21:02:45 -07:00
|
|
|
mPreviousPaintStartTime = TimeStamp::Now();
|
|
|
|
|
|
|
|
double estimatedPaintSum = 0.0;
|
|
|
|
for (uint32_t i = 0; i < mPreviousPaintDurations.Length(); i++) {
|
|
|
|
estimatedPaintSum += mPreviousPaintDurations[i].ToSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
double estimatedPaintDuration = 0.0;
|
|
|
|
if (estimatedPaintSum > EPSILON) {
|
|
|
|
estimatedPaintDuration = estimatedPaintSum / mPreviousPaintDurations.Length();
|
|
|
|
}
|
|
|
|
|
2012-09-28 19:18:18 -07:00
|
|
|
mFrameMetrics.mDisplayPort =
|
2012-09-28 21:02:45 -07:00
|
|
|
CalculatePendingDisplayPort(mFrameMetrics,
|
|
|
|
GetVelocityVector(),
|
|
|
|
GetAccelerationVector(),
|
|
|
|
estimatedPaintDuration);
|
2012-07-27 00:33:48 -07:00
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
gfx::Point oldScrollOffset = mLastPaintRequestMetrics.mScrollOffset,
|
|
|
|
newScrollOffset = mFrameMetrics.mScrollOffset;
|
2012-08-21 21:37:15 -07:00
|
|
|
|
2012-07-27 00:33:48 -07:00
|
|
|
// If we're trying to paint what we already think is painted, discard this
|
|
|
|
// request since it's a pointless paint.
|
2012-09-28 19:16:34 -07:00
|
|
|
gfx::Rect oldDisplayPort = mLastPaintRequestMetrics.mDisplayPort;
|
|
|
|
gfx::Rect newDisplayPort = mFrameMetrics.mDisplayPort;
|
2012-08-21 21:37:15 -07:00
|
|
|
|
|
|
|
oldDisplayPort.MoveBy(oldScrollOffset.x, oldScrollOffset.y);
|
|
|
|
newDisplayPort.MoveBy(newScrollOffset.x, newScrollOffset.y);
|
|
|
|
|
|
|
|
if (fabsf(oldDisplayPort.x - newDisplayPort.x) < EPSILON &&
|
|
|
|
fabsf(oldDisplayPort.y - newDisplayPort.y) < EPSILON &&
|
|
|
|
fabsf(oldDisplayPort.width - newDisplayPort.width) < EPSILON &&
|
|
|
|
fabsf(oldDisplayPort.height - newDisplayPort.height) < EPSILON &&
|
2012-07-27 00:33:48 -07:00
|
|
|
mFrameMetrics.mResolution.width == mLastPaintRequestMetrics.mResolution.width) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
// Cache the zoom since we're temporarily changing it for
|
|
|
|
// acceleration-scaled painting.
|
|
|
|
gfxFloat actualZoom = mFrameMetrics.mZoom.width;
|
2012-09-28 21:02:45 -07:00
|
|
|
// Calculate the factor of acceleration based on the faster of the two axes.
|
|
|
|
float accelerationFactor =
|
|
|
|
clamped(NS_MAX(mX.GetAccelerationFactor(), mY.GetAccelerationFactor()),
|
|
|
|
float(MIN_ZOOM) / 2.0f, float(MAX_ZOOM));
|
|
|
|
// Scale down the resolution a bit based on acceleration.
|
2012-10-11 22:46:24 -07:00
|
|
|
mFrameMetrics.mZoom.width = mFrameMetrics.mZoom.height =
|
|
|
|
actualZoom / accelerationFactor;
|
2012-09-28 21:02:45 -07:00
|
|
|
|
2012-09-28 21:02:41 -07:00
|
|
|
// This message is compressed, so fire whether or not we already have a paint
|
|
|
|
// queued up. We need to know whether or not a paint was requested anyways,
|
2012-10-11 22:46:24 -07:00
|
|
|
// for the purposes of content calling window.scrollTo().
|
2012-09-28 21:02:41 -07:00
|
|
|
mGeckoContentController->RequestContentRepaint(mFrameMetrics);
|
|
|
|
mLastPaintRequestMetrics = mFrameMetrics;
|
|
|
|
mWaitingForContentToPaint = true;
|
2012-09-28 21:02:45 -07:00
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
// Set the zoom back to what it was for the purpose of logic control.
|
|
|
|
mFrameMetrics.mZoom = gfxSize(actualZoom, actualZoom);
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncPanZoomController::SampleContentTransformForFrame(const TimeStamp& aSampleTime,
|
2012-08-03 14:29:22 -07:00
|
|
|
ContainerLayer* aLayer,
|
2012-07-19 23:48:25 -07:00
|
|
|
gfx3DMatrix* aNewTransform) {
|
|
|
|
// The eventual return value of this function. The compositor needs to know
|
|
|
|
// whether or not to advance by a frame as soon as it can. For example, if a
|
|
|
|
// fling is happening, it has to keep compositing so that the animation is
|
|
|
|
// smooth. If an animation frame is requested, it is the compositor's
|
|
|
|
// responsibility to schedule a composite.
|
|
|
|
bool requestAnimationFrame = false;
|
|
|
|
|
2012-07-31 16:19:25 -07:00
|
|
|
const gfx3DMatrix& currentTransform = aLayer->GetTransform();
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
// Scales on the root layer, on what's currently painted.
|
2012-07-31 16:19:25 -07:00
|
|
|
float rootScaleX = currentTransform.GetXScale(),
|
|
|
|
rootScaleY = currentTransform.GetYScale();
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-08-21 21:37:15 -07:00
|
|
|
gfx::Point metricsScrollOffset(0, 0);
|
|
|
|
gfx::Point scrollOffset;
|
2012-07-19 23:48:25 -07:00
|
|
|
float localScaleX, localScaleY;
|
2012-08-03 14:29:22 -07:00
|
|
|
const FrameMetrics& frame = aLayer->GetFrameMetrics();
|
2012-07-19 23:48:25 -07:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
switch (mState) {
|
2012-08-08 21:39:02 -07:00
|
|
|
case FLING:
|
2012-10-11 22:46:24 -07:00
|
|
|
// If a fling is currently happening, apply it now. We can pull
|
|
|
|
// the updated metrics afterwards.
|
2012-08-08 21:39:02 -07:00
|
|
|
requestAnimationFrame |= DoFling(aSampleTime - mLastSampleTime);
|
|
|
|
break;
|
|
|
|
case ANIMATING_ZOOM: {
|
|
|
|
double animPosition = (aSampleTime - mAnimationStartTime) / ZOOM_TO_DURATION;
|
|
|
|
if (animPosition > 1.0) {
|
|
|
|
animPosition = 1.0;
|
|
|
|
}
|
2012-10-11 22:46:24 -07:00
|
|
|
// Sample the zoom at the current time point. The sampled zoom
|
|
|
|
// will affect the final computed resolution.
|
2012-08-08 21:39:02 -07:00
|
|
|
double sampledPosition = gComputedTimingFunction->GetValue(animPosition);
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat startZoom = mStartZoomToMetrics.mZoom.width;
|
|
|
|
gfxFloat endZoom = mEndZoomToMetrics.mZoom.width;
|
|
|
|
gfxFloat sampledZoom = (endZoom * sampledPosition +
|
|
|
|
startZoom * (1 - sampledPosition));
|
|
|
|
mFrameMetrics.mZoom = gfxSize(sampledZoom, sampledZoom);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
mFrameMetrics.mScrollOffset = gfx::Point(
|
|
|
|
mEndZoomToMetrics.mScrollOffset.x * sampledPosition +
|
|
|
|
mStartZoomToMetrics.mScrollOffset.x * (1 - sampledPosition),
|
|
|
|
mEndZoomToMetrics.mScrollOffset.y * sampledPosition +
|
|
|
|
mStartZoomToMetrics.mScrollOffset.y * (1 - sampledPosition)
|
2012-08-08 21:39:02 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
requestAnimationFrame = true;
|
|
|
|
|
|
|
|
if (aSampleTime - mAnimationStartTime >= ZOOM_TO_DURATION) {
|
2012-09-28 19:16:38 -07:00
|
|
|
// Bring the resolution in sync with the zoom.
|
|
|
|
SetZoomAndResolution(mFrameMetrics.mZoom.width);
|
2012-08-08 21:39:02 -07:00
|
|
|
mState = NOTHING;
|
|
|
|
RequestContentRepaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
// Current local transform; this is not what's painted but rather
|
|
|
|
// what PZC has transformed due to touches like panning or
|
|
|
|
// pinching. Eventually, the root layer transform will become this
|
|
|
|
// during runtime, but we must wait for Gecko to repaint.
|
|
|
|
gfxSize localScale = CalculateResolution(mFrameMetrics);
|
|
|
|
localScaleX = localScale.width;
|
|
|
|
localScaleY = localScale.height;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-08-03 14:29:22 -07:00
|
|
|
if (frame.IsScrollable()) {
|
2012-09-28 19:16:34 -07:00
|
|
|
metricsScrollOffset = frame.GetScrollOffsetInLayerPixels();
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
scrollOffset = mFrameMetrics.mScrollOffset;
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIntPoint scrollCompensation(
|
2012-08-21 21:37:15 -07:00
|
|
|
NS_lround((scrollOffset.x / rootScaleX - metricsScrollOffset.x) * localScaleX),
|
|
|
|
NS_lround((scrollOffset.y / rootScaleY - metricsScrollOffset.y) * localScaleY));
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
ViewTransform treeTransform(-scrollCompensation, localScaleX, localScaleY);
|
2012-07-31 16:19:25 -07:00
|
|
|
*aNewTransform = gfx3DMatrix(treeTransform) * currentTransform;
|
|
|
|
|
|
|
|
// The transform already takes the resolution scale into account. Since we
|
|
|
|
// will apply the resolution scale again when computing the effective
|
|
|
|
// transform, we must apply the inverse resolution scale here.
|
2012-08-03 14:29:22 -07:00
|
|
|
aNewTransform->Scale(1.0f/aLayer->GetPreXScale(),
|
|
|
|
1.0f/aLayer->GetPreYScale(),
|
2012-07-31 16:19:25 -07:00
|
|
|
1);
|
2012-08-03 14:29:22 -07:00
|
|
|
aNewTransform->ScalePost(1.0f/aLayer->GetPostXScale(),
|
|
|
|
1.0f/aLayer->GetPostYScale(),
|
|
|
|
1);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
mLastSampleTime = aSampleTime;
|
|
|
|
|
|
|
|
return requestAnimationFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncPanZoomController::NotifyLayersUpdated(const FrameMetrics& aViewportFrame, bool aIsFirstPaint) {
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
|
|
|
|
mLastContentPaintMetrics = aViewportFrame;
|
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
if (mWaitingForContentToPaint) {
|
|
|
|
// Remove the oldest sample we have if adding a new sample takes us over our
|
|
|
|
// desired number of samples.
|
|
|
|
if (mPreviousPaintDurations.Length() >= NUM_PAINT_DURATION_SAMPLES) {
|
|
|
|
mPreviousPaintDurations.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mPreviousPaintDurations.AppendElement(
|
|
|
|
TimeStamp::Now() - mPreviousPaintStartTime);
|
|
|
|
} else {
|
2012-09-14 14:57:35 -07:00
|
|
|
// No paint was requested, but we got one anyways. One possible cause of this
|
|
|
|
// is that content could have fired a scrollTo(). In this case, we should take
|
|
|
|
// the new scroll offset. Document/viewport changes are handled elsewhere.
|
|
|
|
// Also note that, since NotifyLayersUpdated() is called whenever there's a
|
|
|
|
// layers update, we didn't necessarily get a new scroll offset, but we're
|
|
|
|
// updating our local copy of it anyways just in case.
|
|
|
|
switch (mState) {
|
|
|
|
case NOTHING:
|
|
|
|
case FLING:
|
|
|
|
case TOUCHING:
|
|
|
|
case WAITING_LISTENERS:
|
2012-09-28 19:16:38 -07:00
|
|
|
mFrameMetrics.mScrollOffset = aViewportFrame.mScrollOffset;
|
2012-09-14 14:57:35 -07:00
|
|
|
break;
|
|
|
|
// Don't clobber if we're in other states.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-07-27 00:33:48 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 21:02:41 -07:00
|
|
|
mWaitingForContentToPaint = false;
|
2012-10-11 22:46:24 -07:00
|
|
|
bool needContentRepaint = false;
|
|
|
|
if (aViewportFrame.mCompositionBounds.width == mFrameMetrics.mCompositionBounds.width &&
|
|
|
|
aViewportFrame.mCompositionBounds.height == mFrameMetrics.mCompositionBounds.height) {
|
|
|
|
// Remote content has sync'd up to the composition geometry
|
|
|
|
// change, so we can accept the viewport it's calculated.
|
|
|
|
gfxSize previousResolution = CalculateResolution(mFrameMetrics);
|
|
|
|
mFrameMetrics.mViewport = aViewportFrame.mViewport;
|
|
|
|
gfxSize newResolution = CalculateResolution(mFrameMetrics);
|
|
|
|
needContentRepaint |= (previousResolution != newResolution);
|
|
|
|
}
|
2012-07-27 00:33:48 -07:00
|
|
|
|
2012-09-28 21:02:41 -07:00
|
|
|
if (aIsFirstPaint || mFrameMetrics.IsDefault()) {
|
2012-09-28 21:02:45 -07:00
|
|
|
mPreviousPaintDurations.Clear();
|
|
|
|
|
2012-08-13 21:08:38 -07:00
|
|
|
mX.CancelTouch();
|
|
|
|
mY.CancelTouch();
|
2012-09-28 19:16:34 -07:00
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
mFrameMetrics = aViewportFrame;
|
2012-09-28 19:16:38 -07:00
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
SetPageRect(mFrameMetrics.mScrollableRect);
|
2012-07-22 18:51:38 -07:00
|
|
|
|
2012-09-14 18:16:32 -07:00
|
|
|
mState = NOTHING;
|
2012-09-28 19:16:34 -07:00
|
|
|
} else if (!mFrameMetrics.mScrollableRect.IsEqualEdges(aViewportFrame.mScrollableRect)) {
|
|
|
|
mFrameMetrics.mScrollableRect = aViewportFrame.mScrollableRect;
|
|
|
|
SetPageRect(mFrameMetrics.mScrollableRect);
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
2012-10-11 22:46:24 -07:00
|
|
|
|
|
|
|
if (needContentRepaint) {
|
|
|
|
RequestContentRepaint();
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const FrameMetrics& AsyncPanZoomController::GetFrameMetrics() {
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
return mFrameMetrics;
|
|
|
|
}
|
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
void AsyncPanZoomController::UpdateCompositionBounds(const nsIntRect& aCompositionBounds) {
|
2012-07-19 23:48:25 -07:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2012-09-28 19:18:18 -07:00
|
|
|
|
|
|
|
nsIntRect oldCompositionBounds = mFrameMetrics.mCompositionBounds;
|
|
|
|
mFrameMetrics.mCompositionBounds = aCompositionBounds;
|
|
|
|
|
|
|
|
// If the window had 0 dimensions before, or does now, we don't want to
|
|
|
|
// repaint or update the zoom since we'll run into rendering issues and/or
|
|
|
|
// divide-by-zero. This manifests itself as the screen flashing. If the page
|
|
|
|
// has gone out of view, the buffer will be cleared elsewhere anyways.
|
|
|
|
if (aCompositionBounds.width && aCompositionBounds.height &&
|
|
|
|
oldCompositionBounds.width && oldCompositionBounds.height) {
|
2012-10-11 22:46:24 -07:00
|
|
|
SetZoomAndResolution(mFrameMetrics.mZoom.width);
|
2012-09-28 19:18:18 -07:00
|
|
|
|
|
|
|
// Repaint on a rotation so that our new resolution gets properly updated.
|
|
|
|
RequestContentRepaint();
|
|
|
|
}
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 13:38:06 -07:00
|
|
|
void AsyncPanZoomController::CancelDefaultPanZoom() {
|
|
|
|
mDisableNextTouchBatch = true;
|
2012-08-08 13:38:10 -07:00
|
|
|
if (mGestureEventListener) {
|
|
|
|
mGestureEventListener->CancelGesture();
|
|
|
|
}
|
2012-08-08 13:38:06 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:39:02 -07:00
|
|
|
void AsyncPanZoomController::ZoomToRect(const gfxRect& aRect) {
|
|
|
|
gfx::Rect zoomToRect(gfx::Rect(aRect.x, aRect.y, aRect.width, aRect.height));
|
|
|
|
|
|
|
|
SetState(ANIMATING_ZOOM);
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
nsIntRect compositionBounds = mFrameMetrics.mCompositionBounds;
|
|
|
|
gfx::Rect cssPageRect = mFrameMetrics.mScrollableRect;
|
|
|
|
gfx::Point scrollOffset = mFrameMetrics.mScrollOffset;
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxSize resolution = CalculateResolution(mFrameMetrics);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
// If the rect is empty, treat it as a request to zoom out to the full page
|
|
|
|
// size.
|
|
|
|
if (zoomToRect.IsEmpty()) {
|
2012-09-28 19:16:34 -07:00
|
|
|
// composition bounds in CSS coordinates
|
|
|
|
nsIntRect cssCompositionBounds = compositionBounds;
|
2012-10-11 22:46:24 -07:00
|
|
|
cssCompositionBounds.ScaleInverseRoundIn(resolution.width,
|
|
|
|
resolution.height);
|
2012-09-28 19:16:34 -07:00
|
|
|
cssCompositionBounds.MoveBy(scrollOffset.x, scrollOffset.y);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
2012-09-28 19:16:34 -07:00
|
|
|
float y = mFrameMetrics.mScrollOffset.y;
|
|
|
|
float newHeight =
|
|
|
|
cssCompositionBounds.height * cssPageRect.width / cssCompositionBounds.width;
|
|
|
|
float dh = cssCompositionBounds.height - newHeight;
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
zoomToRect = gfx::Rect(0.0f,
|
|
|
|
y + dh/2,
|
|
|
|
cssPageRect.width,
|
|
|
|
y + dh/2 + newHeight);
|
|
|
|
} else {
|
2012-09-28 19:16:34 -07:00
|
|
|
float targetRatio = float(compositionBounds.width) / float(compositionBounds.height);
|
2012-08-08 21:39:02 -07:00
|
|
|
float rectRatio = zoomToRect.width / zoomToRect.height;
|
|
|
|
|
|
|
|
if (fabsf(targetRatio - rectRatio) < EPSILON) {
|
|
|
|
// All good, do nothing.
|
|
|
|
} else if (targetRatio < rectRatio) {
|
|
|
|
// Need to increase zoomToRect height.
|
|
|
|
float newHeight = zoomToRect.height / targetRatio;
|
|
|
|
zoomToRect.y -= (newHeight - zoomToRect.height) / 2;
|
|
|
|
zoomToRect.height = newHeight;
|
|
|
|
} else { // (targetRatio > rectRatio) {
|
|
|
|
// Need to increase zoomToRect width.
|
|
|
|
float newWidth = targetRatio * zoomToRect.width;
|
|
|
|
zoomToRect.x -= (newWidth - zoomToRect.width) / 2;
|
|
|
|
zoomToRect.width = newWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
zoomToRect = zoomToRect.Intersect(cssPageRect);
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
gfxFloat targetResolution =
|
|
|
|
NS_MIN(compositionBounds.width / zoomToRect.width,
|
|
|
|
compositionBounds.height / zoomToRect.height);
|
|
|
|
gfxFloat targetZoom = clamped(float(targetResolution / resolution.width),
|
|
|
|
mMinZoom, mMaxZoom);
|
|
|
|
mEndZoomToMetrics.mZoom = gfxSize(targetZoom, targetZoom);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
// Recalculate the zoom to rect using the new dimensions.
|
2012-10-11 22:46:24 -07:00
|
|
|
zoomToRect.width = compositionBounds.width / targetResolution;
|
|
|
|
zoomToRect.height = compositionBounds.height / targetResolution;
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
// Clamp the zoom to rect to the CSS rect to make sure it fits.
|
|
|
|
zoomToRect = zoomToRect.Intersect(cssPageRect);
|
|
|
|
|
|
|
|
// Do one final recalculation to get the resolution.
|
2012-10-11 22:46:24 -07:00
|
|
|
targetResolution = NS_MAX(compositionBounds.width / zoomToRect.width,
|
|
|
|
compositionBounds.height / zoomToRect.height);
|
|
|
|
targetZoom = targetResolution / resolution.width;
|
|
|
|
mEndZoomToMetrics.mZoom = gfxSize(targetZoom, targetZoom);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
mStartZoomToMetrics = mFrameMetrics;
|
2012-09-28 19:16:34 -07:00
|
|
|
mEndZoomToMetrics.mScrollOffset =
|
2012-08-21 21:37:15 -07:00
|
|
|
gfx::Point(zoomToRect.x, zoomToRect.y);
|
2012-08-08 21:39:02 -07:00
|
|
|
|
|
|
|
mAnimationStartTime = TimeStamp::Now();
|
|
|
|
|
|
|
|
ScheduleComposite();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 21:37:06 -07:00
|
|
|
void AsyncPanZoomController::ContentReceivedTouch(bool aPreventDefault) {
|
|
|
|
if (!mFrameMetrics.mMayHaveTouchListeners) {
|
|
|
|
mTouchQueue.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTouchListenerTimeoutTask) {
|
|
|
|
mTouchListenerTimeoutTask->Cancel();
|
|
|
|
mTouchListenerTimeoutTask = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == WAITING_LISTENERS) {
|
|
|
|
if (!aPreventDefault) {
|
|
|
|
SetState(NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHandlingTouchQueue = true;
|
|
|
|
|
|
|
|
while (!mTouchQueue.IsEmpty()) {
|
|
|
|
if (!aPreventDefault) {
|
|
|
|
HandleInputEvent(mTouchQueue[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTouchQueue[0].mType == MultiTouchInput::MULTITOUCH_END ||
|
|
|
|
mTouchQueue[0].mType == MultiTouchInput::MULTITOUCH_CANCEL) {
|
|
|
|
mTouchQueue.RemoveElementAt(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTouchQueue.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHandlingTouchQueue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 13:38:20 -07:00
|
|
|
void AsyncPanZoomController::SetState(PanZoomState aState) {
|
|
|
|
MonitorAutoLock monitor(mMonitor);
|
|
|
|
mState = aState;
|
|
|
|
}
|
|
|
|
|
2012-08-21 21:37:06 -07:00
|
|
|
void AsyncPanZoomController::TimeoutTouchListeners() {
|
|
|
|
ContentReceivedTouch(false);
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:46:24 -07:00
|
|
|
void AsyncPanZoomController::SetZoomAndResolution(float aZoom) {
|
2012-09-28 19:16:38 -07:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-10-11 22:46:24 -07:00
|
|
|
mFrameMetrics.mZoom = gfxSize(aZoom, aZoom);
|
|
|
|
mFrameMetrics.mResolution = CalculateResolution(mFrameMetrics);
|
2012-09-28 19:16:38 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 19:18:18 -07:00
|
|
|
void AsyncPanZoomController::UpdateZoomConstraints(bool aAllowZoom,
|
|
|
|
float aMinZoom,
|
|
|
|
float aMaxZoom) {
|
|
|
|
mAllowZoom = aAllowZoom;
|
|
|
|
mMinZoom = aMinZoom;
|
|
|
|
mMaxZoom = aMaxZoom;
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
}
|
|
|
|
}
|