| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import org.libreoffice.LibreOfficeMainActivity; |
| 9 |
import android.graphics.PointF; |
| 10 |
import android.graphics.RectF; |
| 11 |
import android.util.DisplayMetrics; |
| 12 |
|
| 13 |
import org.libreoffice.LOKitShell; |
| 14 |
import org.mozilla.gecko.ZoomConstraints; |
| 15 |
|
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
public class GeckoLayerClient implements PanZoomTarget { |
| 19 |
private static final String LOGTAG = GeckoLayerClient.class.getSimpleName(); |
| 20 |
|
| 21 |
private final LibreOfficeMainActivity mContext; |
| 22 |
private IntSize mScreenSize; |
| 23 |
private DisplayPortMetrics mDisplayPort; |
| 24 |
|
| 25 |
private final ComposedTileLayer mLowResLayer; |
| 26 |
private final ComposedTileLayer mRootLayer; |
| 27 |
|
| 28 |
private boolean mForceRedraw; |
| 29 |
|
| 30 |
/* The current viewport metrics. |
| 31 |
* This is volatile so that we can read and write to it from different threads. |
| 32 |
* We avoid synchronization to make getting the viewport metrics from |
| 33 |
* the compositor as cheap as possible. The viewport is immutable so |
| 34 |
* we don't need to worry about anyone mutating it while we're reading from it. |
| 35 |
* Specifically: |
| 36 |
* 1) reading mViewportMetrics from any thread is fine without synchronization |
| 37 |
* 2) writing to mViewportMetrics requires synchronizing on the layer controller object |
| 38 |
* 3) whenever reading multiple fields from mViewportMetrics without synchronization (i.e. in |
| 39 |
* case 1 above) you should always first grab a local copy of the reference, and then use |
| 40 |
* that because mViewportMetrics might get reassigned in between reading the different |
| 41 |
* fields. */ |
| 42 |
private volatile ImmutableViewportMetrics mViewportMetrics; |
| 43 |
|
| 44 |
private ZoomConstraints mZoomConstraints; |
| 45 |
|
| 46 |
private final JavaPanZoomController mPanZoomController; |
| 47 |
private final LayerView mView; |
| 48 |
private final DisplayPortCalculator mDisplayPortCalculator; |
| 49 |
|
| 50 |
public GeckoLayerClient(LibreOfficeMainActivity context, LayerView view ) { |
| 51 |
// we can fill these in with dummy values because they are always written |
| 52 |
// to before being read |
| 53 |
mContext = context; |
| 54 |
mScreenSize = new IntSize(0, 0); |
| 55 |
mDisplayPort = new DisplayPortMetrics(); |
| 56 |
mDisplayPortCalculator = new DisplayPortCalculator(mContext); |
| 57 |
|
| 58 |
mForceRedraw = true; |
| 59 |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); |
| 60 |
mViewportMetrics = new ImmutableViewportMetrics(displayMetrics); |
| 61 |
|
| 62 |
mView = view; |
| 63 |
mPanZoomController = new JavaPanZoomController(mContext, this, mView); |
| 64 |
mView.connect(this); |
| 65 |
|
| 66 |
mRootLayer = new DynamicTileLayer(mContext); |
| 67 |
mLowResLayer = new FixedZoomTileLayer(mContext); |
| 68 |
|
| 69 |
final LayerRenderer layerRenderer = new LayerRenderer(mView); |
| 70 |
mView.setLayerRenderer(layerRenderer); |
| 71 |
|
| 72 |
sendResizeEventIfNecessary(false); |
| 73 |
mView.requestRender(); |
| 74 |
} |
| 75 |
|
| 76 |
Layer getRoot() { |
| 77 |
return mRootLayer; |
| 78 |
} |
| 79 |
|
| 80 |
Layer getLowResLayer() { |
| 81 |
return mLowResLayer; |
| 82 |
} |
| 83 |
|
| 84 |
public LayerView getView() { |
| 85 |
return mView; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns true if this controller is fine with performing a redraw operation or false if it |
| 90 |
* would prefer that the action didn't take place. |
| 91 |
*/ |
| 92 |
private boolean getRedrawHint() { |
| 93 |
if (mForceRedraw) { |
| 94 |
mForceRedraw = false; |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
if (!mPanZoomController.getRedrawHint()) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
return mDisplayPortCalculator.aboutToCheckerboard(mViewportMetrics, mPanZoomController.getVelocityVector(), getDisplayPort()); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* The view calls this function to indicate that the viewport changed size. It must hold the |
| 106 |
* monitor while calling it. |
| 107 |
* |
| 108 |
* TODO: Refactor this to use an interface. Expose that interface only to the view and not |
| 109 |
* to the layer client. That way, the layer client won't be tempted to call this, which might |
| 110 |
* result in an infinite loop. |
| 111 |
*/ |
| 112 |
void setViewportSize(FloatSize size, boolean forceResizeEvent) { |
| 113 |
mViewportMetrics = mViewportMetrics.setViewportSize(size.width, size.height); |
| 114 |
sendResizeEventIfNecessary(forceResizeEvent); |
| 115 |
} |
| 116 |
|
| 117 |
JavaPanZoomController getPanZoomController() { |
| 118 |
return mPanZoomController; |
| 119 |
} |
| 120 |
|
| 121 |
/* Informs Gecko that the screen size has changed. |
| 122 |
* @param force: If true, a resize event will always be sent, otherwise |
| 123 |
* it is only sent if size has changed. */ |
| 124 |
private void sendResizeEventIfNecessary(boolean force) { |
| 125 |
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); |
| 126 |
IntSize newScreenSize = new IntSize(metrics.widthPixels, metrics.heightPixels); |
| 127 |
|
| 128 |
if (!force && mScreenSize.equals(newScreenSize)) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
mScreenSize = newScreenSize; |
| 133 |
|
| 134 |
LOKitShell.sendSizeChangedEvent(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Sets the current page rect. You must hold the monitor while calling this. |
| 139 |
*/ |
| 140 |
private void setPageRect(RectF rect, RectF cssRect) { |
| 141 |
// Since the "rect" is always just a multiple of "cssRect" we don't need to |
| 142 |
// check both; this function assumes that both "rect" and "cssRect" are relative |
| 143 |
// the zoom factor in mViewportMetrics. |
| 144 |
if (mViewportMetrics.getCssPageRect().equals(cssRect)) |
| 145 |
return; |
| 146 |
|
| 147 |
mViewportMetrics = mViewportMetrics.setPageRect(rect, cssRect); |
| 148 |
|
| 149 |
// Page size is owned by the layer client, so no need to notify it of |
| 150 |
// this change. |
| 151 |
|
| 152 |
post(new Runnable() { |
| 153 |
public void run() { |
| 154 |
mPanZoomController.pageRectUpdated(); |
| 155 |
mView.requestRender(); |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
private void adjustViewport(DisplayPortMetrics displayPort) { |
| 161 |
ImmutableViewportMetrics metrics = getViewportMetrics(); |
| 162 |
|
| 163 |
if (displayPort == null) { |
| 164 |
displayPort = mDisplayPortCalculator.calculate(metrics, mPanZoomController.getVelocityVector()); |
| 165 |
} |
| 166 |
|
| 167 |
mDisplayPort = displayPort; |
| 168 |
|
| 169 |
reevaluateTiles(); |
| 170 |
} |
| 171 |
|
| 172 |
public void setZoomConstraints(ZoomConstraints constraints) { |
| 173 |
mZoomConstraints = constraints; |
| 174 |
} |
| 175 |
|
| 176 |
/** The compositor invokes this function whenever it determines that the page rect |
| 177 |
* has changed (based on the information it gets from layout). If setFirstPaintViewport |
| 178 |
* is invoked on a frame, then this function will not be. For any given frame, this |
| 179 |
* function will be invoked before syncViewportInfo. |
| 180 |
*/ |
| 181 |
public void setPageRect(float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom) { |
| 182 |
synchronized (getLock()) { |
| 183 |
RectF cssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom); |
| 184 |
float ourZoom = getViewportMetrics().zoomFactor; |
| 185 |
setPageRect(RectUtils.scale(cssPageRect, ourZoom), cssPageRect); |
| 186 |
// Here the page size of the document has changed, but the document being displayed |
| 187 |
// is still the same. Therefore, we don't need to send anything to browser.js; any |
| 188 |
// changes we need to make to the display port will get sent the next time we call |
| 189 |
// adjustViewport(). |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
private DisplayPortMetrics getDisplayPort() { |
| 194 |
return mDisplayPort; |
| 195 |
} |
| 196 |
|
| 197 |
public void beginDrawing() { |
| 198 |
mLowResLayer.beginTransaction(); |
| 199 |
mRootLayer.beginTransaction(); |
| 200 |
} |
| 201 |
|
| 202 |
public void endDrawing() { |
| 203 |
mLowResLayer.endTransaction(); |
| 204 |
mRootLayer.endTransaction(); |
| 205 |
} |
| 206 |
|
| 207 |
private void geometryChanged() { |
| 208 |
sendResizeEventIfNecessary(false); |
| 209 |
if (getRedrawHint()) { |
| 210 |
adjustViewport(null); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/** Implementation of PanZoomTarget */ |
| 215 |
@Override |
| 216 |
public ImmutableViewportMetrics getViewportMetrics() { |
| 217 |
return mViewportMetrics; |
| 218 |
} |
| 219 |
|
| 220 |
/** Implementation of PanZoomTarget */ |
| 221 |
@Override |
| 222 |
public ZoomConstraints getZoomConstraints() { |
| 223 |
return mZoomConstraints; |
| 224 |
} |
| 225 |
|
| 226 |
/** Implementation of PanZoomTarget */ |
| 227 |
@Override |
| 228 |
public void setAnimationTarget(ImmutableViewportMetrics viewport) { |
| 229 |
// We know what the final viewport of the animation is going to be, so |
| 230 |
// immediately request a draw of that area by setting the display port |
| 231 |
// accordingly. This way we should have the content pre-rendered by the |
| 232 |
// time the animation is done. |
| 233 |
DisplayPortMetrics displayPort = mDisplayPortCalculator.calculate(viewport, null); |
| 234 |
adjustViewport(displayPort); |
| 235 |
} |
| 236 |
|
| 237 |
/** Implementation of PanZoomTarget |
| 238 |
* You must hold the monitor while calling this. |
| 239 |
*/ |
| 240 |
@Override |
| 241 |
public void setViewportMetrics(ImmutableViewportMetrics viewport) { |
| 242 |
mViewportMetrics = viewport; |
| 243 |
mView.requestRender(); |
| 244 |
geometryChanged(); |
| 245 |
} |
| 246 |
|
| 247 |
/** Implementation of PanZoomTarget */ |
| 248 |
@Override |
| 249 |
public void forceRedraw() { |
| 250 |
mForceRedraw = true; |
| 251 |
geometryChanged(); |
| 252 |
} |
| 253 |
|
| 254 |
/** Implementation of PanZoomTarget */ |
| 255 |
@Override |
| 256 |
public boolean post(Runnable action) { |
| 257 |
return mView.post(action); |
| 258 |
} |
| 259 |
|
| 260 |
/** Implementation of PanZoomTarget */ |
| 261 |
@Override |
| 262 |
public Object getLock() { |
| 263 |
return this; |
| 264 |
} |
| 265 |
|
| 266 |
public PointF convertViewPointToLayerPoint(PointF viewPoint) { |
| 267 |
ImmutableViewportMetrics viewportMetrics = mViewportMetrics; |
| 268 |
PointF origin = viewportMetrics.getOrigin(); |
| 269 |
float zoom = viewportMetrics.zoomFactor; |
| 270 |
|
| 271 |
return new PointF( |
| 272 |
((viewPoint.x + origin.x) / zoom), |
| 273 |
((viewPoint.y + origin.y) / zoom)); |
| 274 |
} |
| 275 |
|
| 276 |
/** Implementation of PanZoomTarget */ |
| 277 |
@Override |
| 278 |
public boolean isFullScreen() { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
public void zoomTo(RectF rect) { |
| 283 |
mPanZoomController.animatedZoomTo(rect); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Move the viewport to the desired point, and change the zoom level. |
| 288 |
*/ |
| 289 |
public void moveTo(PointF point, Float zoom) { |
| 290 |
mPanZoomController.animatedMove(point, zoom); |
| 291 |
} |
| 292 |
|
| 293 |
public void zoomTo(float pageWidth, float pageHeight) { |
| 294 |
zoomTo(new RectF(0, 0, pageWidth, pageHeight)); |
| 295 |
} |
| 296 |
|
| 297 |
public void forceRender() { |
| 298 |
mView.requestRender(); |
| 299 |
} |
| 300 |
|
| 301 |
/* Root Layer Access */ |
| 302 |
private void reevaluateTiles() { |
| 303 |
mLowResLayer.reevaluateTiles(mViewportMetrics); |
| 304 |
mRootLayer.reevaluateTiles(mViewportMetrics); |
| 305 |
} |
| 306 |
|
| 307 |
public void clearAndResetlayers() { |
| 308 |
mLowResLayer.clearAndReset(); |
| 309 |
mRootLayer.clearAndReset(); |
| 310 |
} |
| 311 |
|
| 312 |
public void invalidateTiles(List<SubTile> tilesToInvalidate, RectF rect) { |
| 313 |
mLowResLayer.invalidateTiles(tilesToInvalidate, rect); |
| 314 |
mRootLayer.invalidateTiles(tilesToInvalidate, rect); |
| 315 |
} |
| 316 |
} |