mobile/android/base/gfx/GeckoLayerClient.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import org.mozilla.gecko.GeckoAppShell;
michael@0 9 import org.mozilla.gecko.GeckoEvent;
michael@0 10 import org.mozilla.gecko.gfx.LayerView.DrawListener;
michael@0 11 import org.mozilla.gecko.Tab;
michael@0 12 import org.mozilla.gecko.Tabs;
michael@0 13 import org.mozilla.gecko.ZoomConstraints;
michael@0 14 import org.mozilla.gecko.mozglue.RobocopTarget;
michael@0 15 import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI;
michael@0 16 import org.mozilla.gecko.EventDispatcher;
michael@0 17 import org.mozilla.gecko.util.FloatUtils;
michael@0 18
michael@0 19 import android.content.Context;
michael@0 20 import android.graphics.PointF;
michael@0 21 import android.graphics.RectF;
michael@0 22 import android.os.SystemClock;
michael@0 23 import android.util.DisplayMetrics;
michael@0 24 import android.util.Log;
michael@0 25
michael@0 26 import java.util.ArrayList;
michael@0 27 import java.util.List;
michael@0 28
michael@0 29 class GeckoLayerClient implements LayerView.Listener, PanZoomTarget
michael@0 30 {
michael@0 31 private static final String LOGTAG = "GeckoLayerClient";
michael@0 32
michael@0 33 private LayerRenderer mLayerRenderer;
michael@0 34 private boolean mLayerRendererInitialized;
michael@0 35
michael@0 36 private Context mContext;
michael@0 37 private IntSize mScreenSize;
michael@0 38 private IntSize mWindowSize;
michael@0 39 private DisplayPortMetrics mDisplayPort;
michael@0 40
michael@0 41 private boolean mRecordDrawTimes;
michael@0 42 private final DrawTimingQueue mDrawTimingQueue;
michael@0 43
michael@0 44 private VirtualLayer mRootLayer;
michael@0 45
michael@0 46 /* The Gecko viewport as per the UI thread. Must be touched only on the UI thread.
michael@0 47 * If any events being sent to Gecko that are relative to the Gecko viewport position,
michael@0 48 * they must (a) be relative to this viewport, and (b) be sent on the UI thread to
michael@0 49 * avoid races. As long as these two conditions are satisfied, and the events being
michael@0 50 * sent to Gecko are processed in FIFO order, the events will properly be relative
michael@0 51 * to the Gecko viewport position. Note that if Gecko updates its viewport independently,
michael@0 52 * we get notified synchronously and also update this on the UI thread.
michael@0 53 */
michael@0 54 private ImmutableViewportMetrics mGeckoViewport;
michael@0 55
michael@0 56 /*
michael@0 57 * The viewport metrics being used to draw the current frame. This is only
michael@0 58 * accessed by the compositor thread, and so needs no synchronisation.
michael@0 59 */
michael@0 60 private ImmutableViewportMetrics mFrameMetrics;
michael@0 61
michael@0 62 private List<DrawListener> mDrawListeners;
michael@0 63
michael@0 64 /* Used as temporaries by syncViewportInfo */
michael@0 65 private final ViewTransform mCurrentViewTransform;
michael@0 66 private final RectF mCurrentViewTransformMargins;
michael@0 67
michael@0 68 /* Used as the return value of progressiveUpdateCallback */
michael@0 69 private final ProgressiveUpdateData mProgressiveUpdateData;
michael@0 70 private DisplayPortMetrics mProgressiveUpdateDisplayPort;
michael@0 71 private boolean mLastProgressiveUpdateWasLowPrecision;
michael@0 72 private boolean mProgressiveUpdateWasInDanger;
michael@0 73
michael@0 74 private boolean mForceRedraw;
michael@0 75
michael@0 76 /* The current viewport metrics.
michael@0 77 * This is volatile so that we can read and write to it from different threads.
michael@0 78 * We avoid synchronization to make getting the viewport metrics from
michael@0 79 * the compositor as cheap as possible. The viewport is immutable so
michael@0 80 * we don't need to worry about anyone mutating it while we're reading from it.
michael@0 81 * Specifically:
michael@0 82 * 1) reading mViewportMetrics from any thread is fine without synchronization
michael@0 83 * 2) writing to mViewportMetrics requires synchronizing on the layer controller object
michael@0 84 * 3) whenver reading multiple fields from mViewportMetrics without synchronization (i.e. in
michael@0 85 * case 1 above) you should always frist grab a local copy of the reference, and then use
michael@0 86 * that because mViewportMetrics might get reassigned in between reading the different
michael@0 87 * fields. */
michael@0 88 private volatile ImmutableViewportMetrics mViewportMetrics;
michael@0 89 private LayerView.OnMetricsChangedListener mViewportChangeListener;
michael@0 90
michael@0 91 private ZoomConstraints mZoomConstraints;
michael@0 92
michael@0 93 private boolean mGeckoIsReady;
michael@0 94
michael@0 95 private final PanZoomController mPanZoomController;
michael@0 96 private final LayerMarginsAnimator mMarginsAnimator;
michael@0 97 private LayerView mView;
michael@0 98
michael@0 99 /* This flag is true from the time that browser.js detects a first-paint is about to start,
michael@0 100 * to the time that we receive the first-paint composite notification from the compositor.
michael@0 101 * Note that there is a small race condition with this; if there are two paints that both
michael@0 102 * have the first-paint flag set, and the second paint happens concurrently with the
michael@0 103 * composite for the first paint, then this flag may be set to true prematurely. Fixing this
michael@0 104 * is possible but risky; see https://bugzilla.mozilla.org/show_bug.cgi?id=797615#c751
michael@0 105 */
michael@0 106 private volatile boolean mContentDocumentIsDisplayed;
michael@0 107
michael@0 108 public GeckoLayerClient(Context context, LayerView view, EventDispatcher eventDispatcher) {
michael@0 109 // we can fill these in with dummy values because they are always written
michael@0 110 // to before being read
michael@0 111 mContext = context;
michael@0 112 mScreenSize = new IntSize(0, 0);
michael@0 113 mWindowSize = new IntSize(0, 0);
michael@0 114 mDisplayPort = new DisplayPortMetrics();
michael@0 115 mRecordDrawTimes = true;
michael@0 116 mDrawTimingQueue = new DrawTimingQueue();
michael@0 117 mCurrentViewTransform = new ViewTransform(0, 0, 1);
michael@0 118 mCurrentViewTransformMargins = new RectF();
michael@0 119 mProgressiveUpdateData = new ProgressiveUpdateData();
michael@0 120 mProgressiveUpdateDisplayPort = new DisplayPortMetrics();
michael@0 121 mLastProgressiveUpdateWasLowPrecision = false;
michael@0 122 mProgressiveUpdateWasInDanger = false;
michael@0 123
michael@0 124 mForceRedraw = true;
michael@0 125 DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
michael@0 126 mViewportMetrics = new ImmutableViewportMetrics(displayMetrics)
michael@0 127 .setViewportSize(view.getWidth(), view.getHeight());
michael@0 128 mZoomConstraints = new ZoomConstraints(false);
michael@0 129
michael@0 130 Tab tab = Tabs.getInstance().getSelectedTab();
michael@0 131 if (tab != null) {
michael@0 132 mZoomConstraints = tab.getZoomConstraints();
michael@0 133 mViewportMetrics = mViewportMetrics.setIsRTL(tab.getIsRTL());
michael@0 134 }
michael@0 135
michael@0 136 mFrameMetrics = mViewportMetrics;
michael@0 137
michael@0 138 mDrawListeners = new ArrayList<DrawListener>();
michael@0 139 mPanZoomController = PanZoomController.Factory.create(this, view, eventDispatcher);
michael@0 140 mMarginsAnimator = new LayerMarginsAnimator(this, view);
michael@0 141 mView = view;
michael@0 142 mView.setListener(this);
michael@0 143 mContentDocumentIsDisplayed = true;
michael@0 144 }
michael@0 145
michael@0 146 public void setOverscrollHandler(final Overscroll listener) {
michael@0 147 mPanZoomController.setOverscrollHandler(listener);
michael@0 148 }
michael@0 149
michael@0 150 /** Attaches to root layer so that Gecko appears. */
michael@0 151 public void notifyGeckoReady() {
michael@0 152 mGeckoIsReady = true;
michael@0 153
michael@0 154 mRootLayer = new VirtualLayer(new IntSize(mView.getWidth(), mView.getHeight()));
michael@0 155 mLayerRenderer = mView.getRenderer();
michael@0 156
michael@0 157 sendResizeEventIfNecessary(true);
michael@0 158
michael@0 159 DisplayPortCalculator.initPrefs();
michael@0 160
michael@0 161 // Gecko being ready is one of the two conditions (along with having an available
michael@0 162 // surface) that cause us to create the compositor. So here, now that we know gecko
michael@0 163 // is ready, call updateCompositor() to see if we can actually do the creation.
michael@0 164 // This needs to run on the UI thread so that the surface validity can't change on
michael@0 165 // us while we're in the middle of creating the compositor.
michael@0 166 mView.post(new Runnable() {
michael@0 167 @Override
michael@0 168 public void run() {
michael@0 169 mView.getGLController().updateCompositor();
michael@0 170 }
michael@0 171 });
michael@0 172 }
michael@0 173
michael@0 174 public void destroy() {
michael@0 175 mPanZoomController.destroy();
michael@0 176 mMarginsAnimator.destroy();
michael@0 177 mDrawListeners.clear();
michael@0 178 }
michael@0 179
michael@0 180 /**
michael@0 181 * Returns true if this client is fine with performing a redraw operation or false if it
michael@0 182 * would prefer that the action didn't take place.
michael@0 183 */
michael@0 184 private boolean getRedrawHint() {
michael@0 185 if (mForceRedraw) {
michael@0 186 mForceRedraw = false;
michael@0 187 return true;
michael@0 188 }
michael@0 189
michael@0 190 if (!mPanZoomController.getRedrawHint()) {
michael@0 191 return false;
michael@0 192 }
michael@0 193
michael@0 194 return DisplayPortCalculator.aboutToCheckerboard(mViewportMetrics,
michael@0 195 mPanZoomController.getVelocityVector(), mDisplayPort);
michael@0 196 }
michael@0 197
michael@0 198 Layer getRoot() {
michael@0 199 return mGeckoIsReady ? mRootLayer : null;
michael@0 200 }
michael@0 201
michael@0 202 public LayerView getView() {
michael@0 203 return mView;
michael@0 204 }
michael@0 205
michael@0 206 public FloatSize getViewportSize() {
michael@0 207 return mViewportMetrics.getSize();
michael@0 208 }
michael@0 209
michael@0 210 /**
michael@0 211 * The view calls this function to indicate that the viewport changed size. It must hold the
michael@0 212 * monitor while calling it.
michael@0 213 *
michael@0 214 * TODO: Refactor this to use an interface. Expose that interface only to the view and not
michael@0 215 * to the layer client. That way, the layer client won't be tempted to call this, which might
michael@0 216 * result in an infinite loop.
michael@0 217 */
michael@0 218 void setViewportSize(int width, int height) {
michael@0 219 mViewportMetrics = mViewportMetrics.setViewportSize(width, height);
michael@0 220
michael@0 221 if (mGeckoIsReady) {
michael@0 222 // here we send gecko a resize message. The code in browser.js is responsible for
michael@0 223 // picking up on that resize event, modifying the viewport as necessary, and informing
michael@0 224 // us of the new viewport.
michael@0 225 sendResizeEventIfNecessary(true);
michael@0 226 // the following call also sends gecko a message, which will be processed after the resize
michael@0 227 // message above has updated the viewport. this message ensures that if we have just put
michael@0 228 // focus in a text field, we scroll the content so that the text field is in view.
michael@0 229 GeckoAppShell.viewSizeChanged();
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 PanZoomController getPanZoomController() {
michael@0 234 return mPanZoomController;
michael@0 235 }
michael@0 236
michael@0 237 LayerMarginsAnimator getLayerMarginsAnimator() {
michael@0 238 return mMarginsAnimator;
michael@0 239 }
michael@0 240
michael@0 241 /* Informs Gecko that the screen size has changed. */
michael@0 242 private void sendResizeEventIfNecessary(boolean force) {
michael@0 243 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
michael@0 244
michael@0 245 IntSize newScreenSize = new IntSize(metrics.widthPixels, metrics.heightPixels);
michael@0 246 IntSize newWindowSize = new IntSize(mView.getWidth(), mView.getHeight());
michael@0 247
michael@0 248 boolean screenSizeChanged = !mScreenSize.equals(newScreenSize);
michael@0 249 boolean windowSizeChanged = !mWindowSize.equals(newWindowSize);
michael@0 250
michael@0 251 if (!force && !screenSizeChanged && !windowSizeChanged) {
michael@0 252 return;
michael@0 253 }
michael@0 254
michael@0 255 mScreenSize = newScreenSize;
michael@0 256 mWindowSize = newWindowSize;
michael@0 257
michael@0 258 if (screenSizeChanged) {
michael@0 259 Log.d(LOGTAG, "Screen-size changed to " + mScreenSize);
michael@0 260 }
michael@0 261
michael@0 262 if (windowSizeChanged) {
michael@0 263 Log.d(LOGTAG, "Window-size changed to " + mWindowSize);
michael@0 264 }
michael@0 265
michael@0 266 GeckoEvent event = GeckoEvent.createSizeChangedEvent(mWindowSize.width, mWindowSize.height,
michael@0 267 mScreenSize.width, mScreenSize.height);
michael@0 268 GeckoAppShell.sendEventToGecko(event);
michael@0 269 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Window:Resize", ""));
michael@0 270 }
michael@0 271
michael@0 272 /** Sets the current page rect. You must hold the monitor while calling this. */
michael@0 273 private void setPageRect(RectF rect, RectF cssRect) {
michael@0 274 // Since the "rect" is always just a multiple of "cssRect" we don't need to
michael@0 275 // check both; this function assumes that both "rect" and "cssRect" are relative
michael@0 276 // the zoom factor in mViewportMetrics.
michael@0 277 if (mViewportMetrics.getCssPageRect().equals(cssRect))
michael@0 278 return;
michael@0 279
michael@0 280 mViewportMetrics = mViewportMetrics.setPageRect(rect, cssRect);
michael@0 281
michael@0 282 // Page size is owned by the layer client, so no need to notify it of
michael@0 283 // this change.
michael@0 284
michael@0 285 post(new Runnable() {
michael@0 286 @Override
michael@0 287 public void run() {
michael@0 288 mPanZoomController.pageRectUpdated();
michael@0 289 mView.requestRender();
michael@0 290 }
michael@0 291 });
michael@0 292 }
michael@0 293
michael@0 294 /**
michael@0 295 * Derives content document fixed position margins/fixed layer margins from
michael@0 296 * the view margins in the given metrics object.
michael@0 297 */
michael@0 298 private void getFixedMargins(ImmutableViewportMetrics metrics, RectF fixedMargins) {
michael@0 299 fixedMargins.left = 0;
michael@0 300 fixedMargins.top = 0;
michael@0 301 fixedMargins.right = 0;
michael@0 302 fixedMargins.bottom = 0;
michael@0 303
michael@0 304 // The maximum margins are determined by the scrollable area of the page.
michael@0 305 float maxMarginWidth = Math.max(0, metrics.getPageWidth() - metrics.getWidthWithoutMargins());
michael@0 306 float maxMarginHeight = Math.max(0, metrics.getPageHeight() - metrics.getHeightWithoutMargins());
michael@0 307
michael@0 308 // If the margins can't fully hide, they're pinned on - in which case,
michael@0 309 // fixed margins should always be zero.
michael@0 310 if (maxMarginWidth < metrics.marginLeft + metrics.marginRight) {
michael@0 311 maxMarginWidth = 0;
michael@0 312 }
michael@0 313 if (maxMarginHeight < metrics.marginTop + metrics.marginBottom) {
michael@0 314 maxMarginHeight = 0;
michael@0 315 }
michael@0 316
michael@0 317 PointF offset = metrics.getMarginOffset();
michael@0 318 RectF overscroll = metrics.getOverscroll();
michael@0 319 if (offset.x >= 0) {
michael@0 320 fixedMargins.right = Math.max(0, Math.min(offset.x - overscroll.right, maxMarginWidth));
michael@0 321 } else {
michael@0 322 fixedMargins.left = Math.max(0, Math.min(-offset.x - overscroll.left, maxMarginWidth));
michael@0 323 }
michael@0 324 if (offset.y >= 0) {
michael@0 325 fixedMargins.bottom = Math.max(0, Math.min(offset.y - overscroll.bottom, maxMarginHeight));
michael@0 326 } else {
michael@0 327 fixedMargins.top = Math.max(0, Math.min(-offset.y - overscroll.top, maxMarginHeight));
michael@0 328 }
michael@0 329
michael@0 330 // Adjust for overscroll. If we're overscrolled on one side, add that
michael@0 331 // distance to the margins of the other side (limiting to the maximum
michael@0 332 // margin size calculated above).
michael@0 333 if (overscroll.left > 0) {
michael@0 334 fixedMargins.right = Math.min(maxMarginWidth - fixedMargins.left,
michael@0 335 fixedMargins.right + overscroll.left);
michael@0 336 } else if (overscroll.right > 0) {
michael@0 337 fixedMargins.left = Math.min(maxMarginWidth - fixedMargins.right,
michael@0 338 fixedMargins.left + overscroll.right);
michael@0 339 }
michael@0 340 if (overscroll.top > 0) {
michael@0 341 fixedMargins.bottom = Math.min(maxMarginHeight - fixedMargins.top,
michael@0 342 fixedMargins.bottom + overscroll.top);
michael@0 343 } else if (overscroll.bottom > 0) {
michael@0 344 fixedMargins.top = Math.min(maxMarginHeight - fixedMargins.bottom,
michael@0 345 fixedMargins.top + overscroll.bottom);
michael@0 346 }
michael@0 347 }
michael@0 348
michael@0 349 private void adjustViewport(DisplayPortMetrics displayPort) {
michael@0 350 ImmutableViewportMetrics metrics = getViewportMetrics();
michael@0 351 ImmutableViewportMetrics clampedMetrics = metrics.clamp();
michael@0 352
michael@0 353 RectF margins = new RectF();
michael@0 354 getFixedMargins(metrics, margins);
michael@0 355 clampedMetrics = clampedMetrics.setMargins(
michael@0 356 margins.left, margins.top, margins.right, margins.bottom);
michael@0 357
michael@0 358 if (displayPort == null) {
michael@0 359 displayPort = DisplayPortCalculator.calculate(metrics, mPanZoomController.getVelocityVector());
michael@0 360 }
michael@0 361
michael@0 362 mDisplayPort = displayPort;
michael@0 363 mGeckoViewport = clampedMetrics;
michael@0 364
michael@0 365 if (mRecordDrawTimes) {
michael@0 366 mDrawTimingQueue.add(displayPort);
michael@0 367 }
michael@0 368
michael@0 369 GeckoAppShell.sendEventToGecko(GeckoEvent.createViewportEvent(clampedMetrics, displayPort));
michael@0 370 }
michael@0 371
michael@0 372 /** Aborts any pan/zoom animation that is currently in progress. */
michael@0 373 private void abortPanZoomAnimation() {
michael@0 374 if (mPanZoomController != null) {
michael@0 375 post(new Runnable() {
michael@0 376 @Override
michael@0 377 public void run() {
michael@0 378 mPanZoomController.abortAnimation();
michael@0 379 }
michael@0 380 });
michael@0 381 }
michael@0 382 }
michael@0 383
michael@0 384 /**
michael@0 385 * The different types of Viewport messages handled. All viewport events
michael@0 386 * expect a display-port to be returned, but can handle one not being
michael@0 387 * returned.
michael@0 388 */
michael@0 389 private enum ViewportMessageType {
michael@0 390 UPDATE, // The viewport has changed and should be entirely updated
michael@0 391 PAGE_SIZE // The viewport's page-size has changed
michael@0 392 }
michael@0 393
michael@0 394 /** Viewport message handler. */
michael@0 395 private DisplayPortMetrics handleViewportMessage(ImmutableViewportMetrics messageMetrics, ViewportMessageType type) {
michael@0 396 synchronized (getLock()) {
michael@0 397 ImmutableViewportMetrics newMetrics;
michael@0 398 ImmutableViewportMetrics oldMetrics = getViewportMetrics();
michael@0 399
michael@0 400 switch (type) {
michael@0 401 default:
michael@0 402 case UPDATE:
michael@0 403 // Keep the old viewport size
michael@0 404 newMetrics = messageMetrics.setViewportSize(oldMetrics.getWidth(), oldMetrics.getHeight());
michael@0 405 if (!oldMetrics.fuzzyEquals(newMetrics)) {
michael@0 406 abortPanZoomAnimation();
michael@0 407 }
michael@0 408 break;
michael@0 409 case PAGE_SIZE:
michael@0 410 // adjust the page dimensions to account for differences in zoom
michael@0 411 // between the rendered content (which is what Gecko tells us)
michael@0 412 // and our zoom level (which may have diverged).
michael@0 413 float scaleFactor = oldMetrics.zoomFactor / messageMetrics.zoomFactor;
michael@0 414 newMetrics = oldMetrics.setPageRect(RectUtils.scale(messageMetrics.getPageRect(), scaleFactor), messageMetrics.getCssPageRect());
michael@0 415 break;
michael@0 416 }
michael@0 417
michael@0 418 // Update the Gecko-side viewport metrics. Make sure to do this
michael@0 419 // before modifying the metrics below.
michael@0 420 final ImmutableViewportMetrics geckoMetrics = newMetrics.clamp();
michael@0 421 post(new Runnable() {
michael@0 422 @Override
michael@0 423 public void run() {
michael@0 424 mGeckoViewport = geckoMetrics;
michael@0 425 }
michael@0 426 });
michael@0 427
michael@0 428 setViewportMetrics(newMetrics, type == ViewportMessageType.UPDATE);
michael@0 429 mDisplayPort = DisplayPortCalculator.calculate(getViewportMetrics(), null);
michael@0 430 }
michael@0 431 return mDisplayPort;
michael@0 432 }
michael@0 433
michael@0 434 @WrapElementForJNI
michael@0 435 DisplayPortMetrics getDisplayPort(boolean pageSizeUpdate, boolean isBrowserContentDisplayed, int tabId, ImmutableViewportMetrics metrics) {
michael@0 436 Tabs tabs = Tabs.getInstance();
michael@0 437 if (isBrowserContentDisplayed && tabs.isSelectedTabId(tabId)) {
michael@0 438 // for foreground tabs, send the viewport update unless the document
michael@0 439 // displayed is different from the content document. In that case, just
michael@0 440 // calculate the display port.
michael@0 441 return handleViewportMessage(metrics, pageSizeUpdate ? ViewportMessageType.PAGE_SIZE : ViewportMessageType.UPDATE);
michael@0 442 } else {
michael@0 443 // for background tabs, request a new display port calculation, so that
michael@0 444 // when we do switch to that tab, we have the correct display port and
michael@0 445 // don't need to draw twice (once to allow the first-paint viewport to
michael@0 446 // get to java, and again once java figures out the display port).
michael@0 447 return DisplayPortCalculator.calculate(metrics, null);
michael@0 448 }
michael@0 449 }
michael@0 450
michael@0 451 @WrapElementForJNI
michael@0 452 void contentDocumentChanged() {
michael@0 453 mContentDocumentIsDisplayed = false;
michael@0 454 }
michael@0 455
michael@0 456 @WrapElementForJNI
michael@0 457 boolean isContentDocumentDisplayed() {
michael@0 458 return mContentDocumentIsDisplayed;
michael@0 459 }
michael@0 460
michael@0 461 // This is called on the Gecko thread to determine if we're still interested
michael@0 462 // in the update of this display-port to continue. We can return true here
michael@0 463 // to abort the current update and continue with any subsequent ones. This
michael@0 464 // is useful for slow-to-render pages when the display-port starts lagging
michael@0 465 // behind enough that continuing to draw it is wasted effort.
michael@0 466 @WrapElementForJNI(allowMultithread = true)
michael@0 467 public ProgressiveUpdateData progressiveUpdateCallback(boolean aHasPendingNewThebesContent,
michael@0 468 float x, float y, float width, float height,
michael@0 469 float resolution, boolean lowPrecision) {
michael@0 470 // Reset the checkerboard risk flag when switching to low precision
michael@0 471 // rendering.
michael@0 472 if (lowPrecision && !mLastProgressiveUpdateWasLowPrecision) {
michael@0 473 // Skip low precision rendering until we're at risk of checkerboarding.
michael@0 474 if (!mProgressiveUpdateWasInDanger) {
michael@0 475 mProgressiveUpdateData.abort = true;
michael@0 476 return mProgressiveUpdateData;
michael@0 477 }
michael@0 478 mProgressiveUpdateWasInDanger = false;
michael@0 479 }
michael@0 480 mLastProgressiveUpdateWasLowPrecision = lowPrecision;
michael@0 481
michael@0 482 // Grab a local copy of the last display-port sent to Gecko and the
michael@0 483 // current viewport metrics to avoid races when accessing them.
michael@0 484 DisplayPortMetrics displayPort = mDisplayPort;
michael@0 485 ImmutableViewportMetrics viewportMetrics = mViewportMetrics;
michael@0 486 mProgressiveUpdateData.setViewport(viewportMetrics);
michael@0 487 mProgressiveUpdateData.abort = false;
michael@0 488
michael@0 489 // Always abort updates if the resolution has changed. There's no use
michael@0 490 // in drawing at the incorrect resolution.
michael@0 491 if (!FloatUtils.fuzzyEquals(resolution, viewportMetrics.zoomFactor)) {
michael@0 492 Log.d(LOGTAG, "Aborting draw due to resolution change: " + resolution + " != " + viewportMetrics.zoomFactor);
michael@0 493 mProgressiveUpdateData.abort = true;
michael@0 494 return mProgressiveUpdateData;
michael@0 495 }
michael@0 496
michael@0 497 // Store the high precision displayport for comparison when doing low
michael@0 498 // precision updates.
michael@0 499 if (!lowPrecision) {
michael@0 500 if (!FloatUtils.fuzzyEquals(resolution, mProgressiveUpdateDisplayPort.resolution) ||
michael@0 501 !FloatUtils.fuzzyEquals(x, mProgressiveUpdateDisplayPort.getLeft()) ||
michael@0 502 !FloatUtils.fuzzyEquals(y, mProgressiveUpdateDisplayPort.getTop()) ||
michael@0 503 !FloatUtils.fuzzyEquals(x + width, mProgressiveUpdateDisplayPort.getRight()) ||
michael@0 504 !FloatUtils.fuzzyEquals(y + height, mProgressiveUpdateDisplayPort.getBottom())) {
michael@0 505 mProgressiveUpdateDisplayPort =
michael@0 506 new DisplayPortMetrics(x, y, x+width, y+height, resolution);
michael@0 507 }
michael@0 508 }
michael@0 509
michael@0 510 // If we're not doing low precision draws and we're about to
michael@0 511 // checkerboard, enable low precision drawing.
michael@0 512 if (!lowPrecision && !mProgressiveUpdateWasInDanger) {
michael@0 513 if (DisplayPortCalculator.aboutToCheckerboard(viewportMetrics,
michael@0 514 mPanZoomController.getVelocityVector(), mProgressiveUpdateDisplayPort)) {
michael@0 515 mProgressiveUpdateWasInDanger = true;
michael@0 516 }
michael@0 517 }
michael@0 518
michael@0 519 // XXX All sorts of rounding happens inside Gecko that becomes hard to
michael@0 520 // account exactly for. Given we align the display-port to tile
michael@0 521 // boundaries (and so they rarely vary by sub-pixel amounts), just
michael@0 522 // check that values are within a couple of pixels of the
michael@0 523 // display-port bounds.
michael@0 524
michael@0 525 // Never abort drawing if we can't be sure we've sent a more recent
michael@0 526 // display-port. If we abort updating when we shouldn't, we can end up
michael@0 527 // with blank regions on the screen and we open up the risk of entering
michael@0 528 // an endless updating cycle.
michael@0 529 if (Math.abs(displayPort.getLeft() - mProgressiveUpdateDisplayPort.getLeft()) <= 2 &&
michael@0 530 Math.abs(displayPort.getTop() - mProgressiveUpdateDisplayPort.getTop()) <= 2 &&
michael@0 531 Math.abs(displayPort.getBottom() - mProgressiveUpdateDisplayPort.getBottom()) <= 2 &&
michael@0 532 Math.abs(displayPort.getRight() - mProgressiveUpdateDisplayPort.getRight()) <= 2) {
michael@0 533 return mProgressiveUpdateData;
michael@0 534 }
michael@0 535
michael@0 536 // Abort updates when the display-port no longer contains the visible
michael@0 537 // area of the page (that is, the viewport cropped by the page
michael@0 538 // boundaries).
michael@0 539 // XXX This makes the assumption that we never let the visible area of
michael@0 540 // the page fall outside of the display-port.
michael@0 541 if (Math.max(viewportMetrics.viewportRectLeft, viewportMetrics.pageRectLeft) + 1 < x ||
michael@0 542 Math.max(viewportMetrics.viewportRectTop, viewportMetrics.pageRectTop) + 1 < y ||
michael@0 543 Math.min(viewportMetrics.viewportRectRight, viewportMetrics.pageRectRight) - 1 > x + width ||
michael@0 544 Math.min(viewportMetrics.viewportRectBottom, viewportMetrics.pageRectBottom) - 1 > y + height) {
michael@0 545 Log.d(LOGTAG, "Aborting update due to viewport not in display-port");
michael@0 546 mProgressiveUpdateData.abort = true;
michael@0 547
michael@0 548 // Enable low-precision drawing, as we're likely to be in danger if
michael@0 549 // this situation has been encountered.
michael@0 550 mProgressiveUpdateWasInDanger = true;
michael@0 551
michael@0 552 return mProgressiveUpdateData;
michael@0 553 }
michael@0 554
michael@0 555 // Abort drawing stale low-precision content if there's a more recent
michael@0 556 // display-port in the pipeline.
michael@0 557 if (lowPrecision && !aHasPendingNewThebesContent) {
michael@0 558 mProgressiveUpdateData.abort = true;
michael@0 559 }
michael@0 560 return mProgressiveUpdateData;
michael@0 561 }
michael@0 562
michael@0 563 void setZoomConstraints(ZoomConstraints constraints) {
michael@0 564 mZoomConstraints = constraints;
michael@0 565 }
michael@0 566
michael@0 567 void setIsRTL(boolean aIsRTL) {
michael@0 568 synchronized (getLock()) {
michael@0 569 ImmutableViewportMetrics newMetrics = getViewportMetrics().setIsRTL(aIsRTL);
michael@0 570 setViewportMetrics(newMetrics, false);
michael@0 571 }
michael@0 572 }
michael@0 573
michael@0 574 /** The compositor invokes this function just before compositing a frame where the document
michael@0 575 * is different from the document composited on the last frame. In these cases, the viewport
michael@0 576 * information we have in Java is no longer valid and needs to be replaced with the new
michael@0 577 * viewport information provided. setPageRect will never be invoked on the same frame that
michael@0 578 * this function is invoked on; and this function will always be called prior to syncViewportInfo.
michael@0 579 */
michael@0 580 @WrapElementForJNI(allowMultithread = true)
michael@0 581 public void setFirstPaintViewport(float offsetX, float offsetY, float zoom,
michael@0 582 float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom) {
michael@0 583 synchronized (getLock()) {
michael@0 584 ImmutableViewportMetrics currentMetrics = getViewportMetrics();
michael@0 585
michael@0 586 Tab tab = Tabs.getInstance().getSelectedTab();
michael@0 587
michael@0 588 RectF cssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
michael@0 589 RectF pageRect = RectUtils.scaleAndRound(cssPageRect, zoom);
michael@0 590
michael@0 591 final ImmutableViewportMetrics newMetrics = currentMetrics
michael@0 592 .setViewportOrigin(offsetX, offsetY)
michael@0 593 .setZoomFactor(zoom)
michael@0 594 .setPageRect(pageRect, cssPageRect)
michael@0 595 .setIsRTL(tab.getIsRTL());
michael@0 596 // Since we have switched to displaying a different document, we need to update any
michael@0 597 // viewport-related state we have lying around. This includes mGeckoViewport and
michael@0 598 // mViewportMetrics. Usually this information is updated via handleViewportMessage
michael@0 599 // while we remain on the same document.
michael@0 600 post(new Runnable() {
michael@0 601 @Override
michael@0 602 public void run() {
michael@0 603 mGeckoViewport = newMetrics;
michael@0 604 }
michael@0 605 });
michael@0 606
michael@0 607 setViewportMetrics(newMetrics);
michael@0 608
michael@0 609 mView.setBackgroundColor(tab.getBackgroundColor());
michael@0 610 setZoomConstraints(tab.getZoomConstraints());
michael@0 611
michael@0 612 // At this point, we have just switched to displaying a different document than we
michael@0 613 // we previously displaying. This means we need to abort any panning/zooming animations
michael@0 614 // that are in progress and send an updated display port request to browser.js as soon
michael@0 615 // as possible. The call to PanZoomController.abortAnimation accomplishes this by calling the
michael@0 616 // forceRedraw function, which sends the viewport to gecko. The display port request is
michael@0 617 // actually a full viewport update, which is fine because if browser.js has somehow moved to
michael@0 618 // be out of sync with this first-paint viewport, then we force them back in sync.
michael@0 619 abortPanZoomAnimation();
michael@0 620
michael@0 621 // Indicate that the document is about to be composited so the
michael@0 622 // LayerView background can be removed.
michael@0 623 if (mView.getPaintState() == LayerView.PAINT_START) {
michael@0 624 mView.setPaintState(LayerView.PAINT_BEFORE_FIRST);
michael@0 625 }
michael@0 626 }
michael@0 627 DisplayPortCalculator.resetPageState();
michael@0 628 mDrawTimingQueue.reset();
michael@0 629
michael@0 630 mContentDocumentIsDisplayed = true;
michael@0 631 }
michael@0 632
michael@0 633 /** The compositor invokes this function whenever it determines that the page rect
michael@0 634 * has changed (based on the information it gets from layout). If setFirstPaintViewport
michael@0 635 * is invoked on a frame, then this function will not be. For any given frame, this
michael@0 636 * function will be invoked before syncViewportInfo.
michael@0 637 */
michael@0 638 @WrapElementForJNI(allowMultithread = true)
michael@0 639 public void setPageRect(float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom) {
michael@0 640 synchronized (getLock()) {
michael@0 641 RectF cssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
michael@0 642 float ourZoom = getViewportMetrics().zoomFactor;
michael@0 643 setPageRect(RectUtils.scale(cssPageRect, ourZoom), cssPageRect);
michael@0 644 // Here the page size of the document has changed, but the document being displayed
michael@0 645 // is still the same. Therefore, we don't need to send anything to browser.js; any
michael@0 646 // changes we need to make to the display port will get sent the next time we call
michael@0 647 // adjustViewport().
michael@0 648 }
michael@0 649 }
michael@0 650
michael@0 651 /** The compositor invokes this function on every frame to figure out what part of the
michael@0 652 * page to display, and to inform Java of the current display port. Since it is called
michael@0 653 * on every frame, it needs to be ultra-fast.
michael@0 654 * It avoids taking any locks or allocating any objects. We keep around a
michael@0 655 * mCurrentViewTransform so we don't need to allocate a new ViewTransform
michael@0 656 * everytime we're called. NOTE: we might be able to return a ImmutableViewportMetrics
michael@0 657 * which would avoid the copy into mCurrentViewTransform.
michael@0 658 */
michael@0 659 @WrapElementForJNI(allowMultithread = true)
michael@0 660 public ViewTransform syncViewportInfo(int x, int y, int width, int height, float resolution, boolean layersUpdated) {
michael@0 661 // getViewportMetrics is thread safe so we don't need to synchronize.
michael@0 662 // We save the viewport metrics here, so we later use it later in
michael@0 663 // createFrame (which will be called by nsWindow::DrawWindowUnderlay on
michael@0 664 // the native side, by the compositor). The viewport
michael@0 665 // metrics can change between here and there, as it's accessed outside
michael@0 666 // of the compositor thread.
michael@0 667 mFrameMetrics = getViewportMetrics();
michael@0 668
michael@0 669 mCurrentViewTransform.x = mFrameMetrics.viewportRectLeft;
michael@0 670 mCurrentViewTransform.y = mFrameMetrics.viewportRectTop;
michael@0 671 mCurrentViewTransform.scale = mFrameMetrics.zoomFactor;
michael@0 672
michael@0 673 // Adjust the fixed layer margins so that overscroll subtracts from them.
michael@0 674 getFixedMargins(mFrameMetrics, mCurrentViewTransformMargins);
michael@0 675 mCurrentViewTransform.fixedLayerMarginLeft = mCurrentViewTransformMargins.left;
michael@0 676 mCurrentViewTransform.fixedLayerMarginTop = mCurrentViewTransformMargins.top;
michael@0 677 mCurrentViewTransform.fixedLayerMarginRight = mCurrentViewTransformMargins.right;
michael@0 678 mCurrentViewTransform.fixedLayerMarginBottom = mCurrentViewTransformMargins.bottom;
michael@0 679
michael@0 680 // Offset the view transform so that it renders in the correct place.
michael@0 681 PointF offset = mFrameMetrics.getMarginOffset();
michael@0 682 mCurrentViewTransform.offsetX = offset.x;
michael@0 683 mCurrentViewTransform.offsetY = offset.y;
michael@0 684
michael@0 685 mRootLayer.setPositionAndResolution(
michael@0 686 Math.round(x + mCurrentViewTransform.offsetX),
michael@0 687 Math.round(y + mCurrentViewTransform.offsetY),
michael@0 688 Math.round(x + width + mCurrentViewTransform.offsetX),
michael@0 689 Math.round(y + height + mCurrentViewTransform.offsetY),
michael@0 690 resolution);
michael@0 691
michael@0 692 if (layersUpdated && mRecordDrawTimes) {
michael@0 693 // If we got a layers update, that means a draw finished. Check to see if the area drawn matches
michael@0 694 // one of our requested displayports; if it does calculate the draw time and notify the
michael@0 695 // DisplayPortCalculator
michael@0 696 DisplayPortMetrics drawn = new DisplayPortMetrics(x, y, x + width, y + height, resolution);
michael@0 697 long time = mDrawTimingQueue.findTimeFor(drawn);
michael@0 698 if (time >= 0) {
michael@0 699 long now = SystemClock.uptimeMillis();
michael@0 700 time = now - time;
michael@0 701 mRecordDrawTimes = DisplayPortCalculator.drawTimeUpdate(time, width * height);
michael@0 702 }
michael@0 703 }
michael@0 704
michael@0 705 if (layersUpdated) {
michael@0 706 for (DrawListener listener : mDrawListeners) {
michael@0 707 listener.drawFinished();
michael@0 708 }
michael@0 709 }
michael@0 710
michael@0 711 return mCurrentViewTransform;
michael@0 712 }
michael@0 713
michael@0 714 @WrapElementForJNI(allowMultithread = true)
michael@0 715 public ViewTransform syncFrameMetrics(float offsetX, float offsetY, float zoom,
michael@0 716 float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom,
michael@0 717 boolean layersUpdated, int x, int y, int width, int height, float resolution,
michael@0 718 boolean isFirstPaint)
michael@0 719 {
michael@0 720 if (isFirstPaint) {
michael@0 721 setFirstPaintViewport(offsetX, offsetY, zoom,
michael@0 722 cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
michael@0 723 }
michael@0 724
michael@0 725 return syncViewportInfo(x, y, width, height, resolution, layersUpdated);
michael@0 726 }
michael@0 727
michael@0 728 @WrapElementForJNI(allowMultithread = true)
michael@0 729 public LayerRenderer.Frame createFrame() {
michael@0 730 // Create the shaders and textures if necessary.
michael@0 731 if (!mLayerRendererInitialized) {
michael@0 732 mLayerRenderer.checkMonitoringEnabled();
michael@0 733 mLayerRenderer.createDefaultProgram();
michael@0 734 mLayerRendererInitialized = true;
michael@0 735 }
michael@0 736
michael@0 737 try {
michael@0 738 return mLayerRenderer.createFrame(mFrameMetrics);
michael@0 739 } catch (Exception e) {
michael@0 740 Log.w(LOGTAG, e);
michael@0 741 return null;
michael@0 742 }
michael@0 743 }
michael@0 744
michael@0 745 @WrapElementForJNI(allowMultithread = true)
michael@0 746 public void activateProgram() {
michael@0 747 mLayerRenderer.activateDefaultProgram();
michael@0 748 }
michael@0 749
michael@0 750 @WrapElementForJNI(allowMultithread = true)
michael@0 751 public void deactivateProgram() {
michael@0 752 mLayerRenderer.deactivateDefaultProgram();
michael@0 753 }
michael@0 754
michael@0 755 private void geometryChanged(DisplayPortMetrics displayPort) {
michael@0 756 /* Let Gecko know if the screensize has changed */
michael@0 757 sendResizeEventIfNecessary(false);
michael@0 758 if (getRedrawHint()) {
michael@0 759 adjustViewport(displayPort);
michael@0 760 }
michael@0 761 }
michael@0 762
michael@0 763 /** Implementation of LayerView.Listener */
michael@0 764 @Override
michael@0 765 public void renderRequested() {
michael@0 766 try {
michael@0 767 GeckoAppShell.scheduleComposite();
michael@0 768 } catch (UnsupportedOperationException uoe) {
michael@0 769 // In some very rare cases this gets called before libxul is loaded,
michael@0 770 // so catch and ignore the exception that will throw. See bug 837821
michael@0 771 Log.d(LOGTAG, "Dropping renderRequested call before libxul load.");
michael@0 772 }
michael@0 773 }
michael@0 774
michael@0 775 /** Implementation of LayerView.Listener */
michael@0 776 @Override
michael@0 777 public void sizeChanged(int width, int height) {
michael@0 778 // We need to make sure a draw happens synchronously at this point,
michael@0 779 // but resizing the surface before the SurfaceView has resized will
michael@0 780 // cause a visible jump.
michael@0 781 mView.getGLController().resumeCompositor(mWindowSize.width, mWindowSize.height);
michael@0 782 }
michael@0 783
michael@0 784 /** Implementation of LayerView.Listener */
michael@0 785 @Override
michael@0 786 public void surfaceChanged(int width, int height) {
michael@0 787 setViewportSize(width, height);
michael@0 788 }
michael@0 789
michael@0 790 /** Implementation of PanZoomTarget */
michael@0 791 @Override
michael@0 792 public ImmutableViewportMetrics getViewportMetrics() {
michael@0 793 return mViewportMetrics;
michael@0 794 }
michael@0 795
michael@0 796 /** Implementation of PanZoomTarget */
michael@0 797 @Override
michael@0 798 public ZoomConstraints getZoomConstraints() {
michael@0 799 return mZoomConstraints;
michael@0 800 }
michael@0 801
michael@0 802 /** Implementation of PanZoomTarget */
michael@0 803 @Override
michael@0 804 public boolean isFullScreen() {
michael@0 805 return mView.isFullScreen();
michael@0 806 }
michael@0 807
michael@0 808 /** Implementation of PanZoomTarget */
michael@0 809 @Override
michael@0 810 public RectF getMaxMargins() {
michael@0 811 return mMarginsAnimator.getMaxMargins();
michael@0 812 }
michael@0 813
michael@0 814 /** Implementation of PanZoomTarget */
michael@0 815 @Override
michael@0 816 public void setAnimationTarget(ImmutableViewportMetrics metrics) {
michael@0 817 if (mGeckoIsReady) {
michael@0 818 // We know what the final viewport of the animation is going to be, so
michael@0 819 // immediately request a draw of that area by setting the display port
michael@0 820 // accordingly. This way we should have the content pre-rendered by the
michael@0 821 // time the animation is done.
michael@0 822 DisplayPortMetrics displayPort = DisplayPortCalculator.calculate(metrics, null);
michael@0 823 adjustViewport(displayPort);
michael@0 824 }
michael@0 825 }
michael@0 826
michael@0 827 /** Implementation of PanZoomTarget
michael@0 828 * You must hold the monitor while calling this.
michael@0 829 */
michael@0 830 @Override
michael@0 831 public void setViewportMetrics(ImmutableViewportMetrics metrics) {
michael@0 832 setViewportMetrics(metrics, true);
michael@0 833 }
michael@0 834
michael@0 835 /*
michael@0 836 * You must hold the monitor while calling this.
michael@0 837 */
michael@0 838 private void setViewportMetrics(ImmutableViewportMetrics metrics, boolean notifyGecko) {
michael@0 839 // This class owns the viewport size and the fixed layer margins; don't let other pieces
michael@0 840 // of code clobber either of them. The only place the viewport size should ever be
michael@0 841 // updated is in GeckoLayerClient.setViewportSize, and the only place the margins should
michael@0 842 // ever be updated is in GeckoLayerClient.setFixedLayerMargins; both of these assign to
michael@0 843 // mViewportMetrics directly.
michael@0 844 metrics = metrics.setViewportSize(mViewportMetrics.getWidth(), mViewportMetrics.getHeight());
michael@0 845 metrics = metrics.setMarginsFrom(mViewportMetrics);
michael@0 846 mViewportMetrics = metrics;
michael@0 847
michael@0 848 viewportMetricsChanged(notifyGecko);
michael@0 849 }
michael@0 850
michael@0 851 /*
michael@0 852 * You must hold the monitor while calling this.
michael@0 853 */
michael@0 854 private void viewportMetricsChanged(boolean notifyGecko) {
michael@0 855 if (mViewportChangeListener != null) {
michael@0 856 mViewportChangeListener.onMetricsChanged(mViewportMetrics);
michael@0 857 }
michael@0 858
michael@0 859 mView.requestRender();
michael@0 860 if (notifyGecko && mGeckoIsReady) {
michael@0 861 geometryChanged(null);
michael@0 862 }
michael@0 863 }
michael@0 864
michael@0 865 /*
michael@0 866 * Updates the viewport metrics, overriding the viewport size and margins
michael@0 867 * which are normally retained when calling setViewportMetrics.
michael@0 868 * You must hold the monitor while calling this.
michael@0 869 */
michael@0 870 void forceViewportMetrics(ImmutableViewportMetrics metrics, boolean notifyGecko, boolean forceRedraw) {
michael@0 871 if (forceRedraw) {
michael@0 872 mForceRedraw = true;
michael@0 873 }
michael@0 874 mViewportMetrics = metrics;
michael@0 875 viewportMetricsChanged(notifyGecko);
michael@0 876 }
michael@0 877
michael@0 878 /** Implementation of PanZoomTarget
michael@0 879 * Scroll the viewport by a certain amount. This will take viewport margins
michael@0 880 * and margin animation into account. If margins are currently animating,
michael@0 881 * this will just go ahead and modify the viewport origin, otherwise the
michael@0 882 * delta will be applied to the margins and the remainder will be applied to
michael@0 883 * the viewport origin.
michael@0 884 *
michael@0 885 * You must hold the monitor while calling this.
michael@0 886 */
michael@0 887 @Override
michael@0 888 public void scrollBy(float dx, float dy) {
michael@0 889 // Set mViewportMetrics manually so the margin changes take.
michael@0 890 mViewportMetrics = mMarginsAnimator.scrollBy(mViewportMetrics, dx, dy);
michael@0 891 viewportMetricsChanged(true);
michael@0 892 }
michael@0 893
michael@0 894 /** Implementation of PanZoomTarget
michael@0 895 * Notification that a subdocument has been scrolled by a certain amount.
michael@0 896 * This is used here to make sure that the margins are still accessible
michael@0 897 * during subdocument scrolling.
michael@0 898 *
michael@0 899 * You must hold the monitor while calling this.
michael@0 900 */
michael@0 901 @Override
michael@0 902 public void scrollMarginsBy(float dx, float dy) {
michael@0 903 ImmutableViewportMetrics newMarginsMetrics =
michael@0 904 mMarginsAnimator.scrollBy(mViewportMetrics, dx, dy);
michael@0 905 mViewportMetrics = mViewportMetrics.setMarginsFrom(newMarginsMetrics);
michael@0 906 viewportMetricsChanged(true);
michael@0 907 }
michael@0 908
michael@0 909 /** Implementation of PanZoomTarget */
michael@0 910 @Override
michael@0 911 public void panZoomStopped() {
michael@0 912 if (mViewportChangeListener != null) {
michael@0 913 mViewportChangeListener.onPanZoomStopped();
michael@0 914 }
michael@0 915 }
michael@0 916
michael@0 917 /** Implementation of PanZoomTarget */
michael@0 918 @Override
michael@0 919 public void forceRedraw(DisplayPortMetrics displayPort) {
michael@0 920 mForceRedraw = true;
michael@0 921 if (mGeckoIsReady) {
michael@0 922 geometryChanged(displayPort);
michael@0 923 }
michael@0 924 }
michael@0 925
michael@0 926 /** Implementation of PanZoomTarget */
michael@0 927 @Override
michael@0 928 public boolean post(Runnable action) {
michael@0 929 return mView.post(action);
michael@0 930 }
michael@0 931
michael@0 932 /** Implementation of PanZoomTarget */
michael@0 933 @Override
michael@0 934 public void postRenderTask(RenderTask task) {
michael@0 935 mView.postRenderTask(task);
michael@0 936 }
michael@0 937
michael@0 938 /** Implementation of PanZoomTarget */
michael@0 939 @Override
michael@0 940 public void removeRenderTask(RenderTask task) {
michael@0 941 mView.removeRenderTask(task);
michael@0 942 }
michael@0 943
michael@0 944
michael@0 945 /** Implementation of PanZoomTarget */
michael@0 946 @Override
michael@0 947 public boolean postDelayed(Runnable action, long delayMillis) {
michael@0 948 return mView.postDelayed(action, delayMillis);
michael@0 949 }
michael@0 950
michael@0 951 /** Implementation of PanZoomTarget */
michael@0 952 @Override
michael@0 953 public Object getLock() {
michael@0 954 return this;
michael@0 955 }
michael@0 956
michael@0 957 /** Implementation of PanZoomTarget
michael@0 958 * Converts a point from layer view coordinates to layer coordinates. In other words, given a
michael@0 959 * point measured in pixels from the top left corner of the layer view, returns the point in
michael@0 960 * pixels measured from the last scroll position we sent to Gecko, in CSS pixels. Assuming the
michael@0 961 * events being sent to Gecko are processed in FIFO order, this calculation should always be
michael@0 962 * correct.
michael@0 963 */
michael@0 964 @Override
michael@0 965 public PointF convertViewPointToLayerPoint(PointF viewPoint) {
michael@0 966 if (!mGeckoIsReady) {
michael@0 967 return null;
michael@0 968 }
michael@0 969
michael@0 970 ImmutableViewportMetrics viewportMetrics = mViewportMetrics;
michael@0 971 PointF origin = viewportMetrics.getOrigin();
michael@0 972 PointF offset = viewportMetrics.getMarginOffset();
michael@0 973 origin.offset(-offset.x, -offset.y);
michael@0 974 float zoom = viewportMetrics.zoomFactor;
michael@0 975 ImmutableViewportMetrics geckoViewport = mGeckoViewport;
michael@0 976 PointF geckoOrigin = geckoViewport.getOrigin();
michael@0 977 float geckoZoom = geckoViewport.zoomFactor;
michael@0 978
michael@0 979 // viewPoint + origin - offset gives the coordinate in device pixels from the top-left corner of the page.
michael@0 980 // Divided by zoom, this gives us the coordinate in CSS pixels from the top-left corner of the page.
michael@0 981 // geckoOrigin / geckoZoom is where Gecko thinks it is (scrollTo position) in CSS pixels from
michael@0 982 // the top-left corner of the page. Subtracting the two gives us the offset of the viewPoint from
michael@0 983 // the current Gecko coordinate in CSS pixels.
michael@0 984 PointF layerPoint = new PointF(
michael@0 985 ((viewPoint.x + origin.x) / zoom) - (geckoOrigin.x / geckoZoom),
michael@0 986 ((viewPoint.y + origin.y) / zoom) - (geckoOrigin.y / geckoZoom));
michael@0 987
michael@0 988 return layerPoint;
michael@0 989 }
michael@0 990
michael@0 991 void setOnMetricsChangedListener(LayerView.OnMetricsChangedListener listener) {
michael@0 992 mViewportChangeListener = listener;
michael@0 993 }
michael@0 994
michael@0 995 public void addDrawListener(DrawListener listener) {
michael@0 996 mDrawListeners.add(listener);
michael@0 997 }
michael@0 998
michael@0 999 public void removeDrawListener(DrawListener listener) {
michael@0 1000 mDrawListeners.remove(listener);
michael@0 1001 }
michael@0 1002 }

mercurial