LibreOffice Module android (master) 1
GeckoLayerClient.java
Go to the documentation of this file.
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
6package org.mozilla.gecko.gfx;
7
9import android.graphics.PointF;
10import android.graphics.RectF;
11import android.util.DisplayMetrics;
12
15
16import java.util.List;
17
18public class GeckoLayerClient implements PanZoomTarget {
19 private static final String LOGTAG = GeckoLayerClient.class.getSimpleName();
20
22
26
29
30 private boolean mForceRedraw;
31
32 /* The current viewport metrics.
33 * This is volatile so that we can read and write to it from different threads.
34 * We avoid synchronization to make getting the viewport metrics from
35 * the compositor as cheap as possible. The viewport is immutable so
36 * we don't need to worry about anyone mutating it while we're reading from it.
37 * Specifically:
38 * 1) reading mViewportMetrics from any thread is fine without synchronization
39 * 2) writing to mViewportMetrics requires synchronizing on the layer controller object
40 * 3) whenever reading multiple fields from mViewportMetrics without synchronization (i.e. in
41 * case 1 above) you should always first grab a local copy of the reference, and then use
42 * that because mViewportMetrics might get reassigned in between reading the different
43 * fields. */
45
47
48 private boolean mIsReady;
49
50 private PanZoomController mPanZoomController;
52 private final DisplayPortCalculator mDisplayPortCalculator;
53
55 // we can fill these in with dummy values because they are always written
56 // to before being read
57 mContext = context;
58 mScreenSize = new IntSize(0, 0);
60 mDisplayPortCalculator = new DisplayPortCalculator(mContext);
61
62 mForceRedraw = true;
63 DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
64 mViewportMetrics = new ImmutableViewportMetrics(displayMetrics);
65 }
66
67 public void setView(LayerView view) {
68 mView = view;
69 mPanZoomController = PanZoomController.Factory.create(mContext, this, view);
70 mView.connect(this);
71 }
72
73 public void notifyReady() {
74 mIsReady = true;
75
78
80
82
85 }
86
87 public void destroy() {
88 mPanZoomController.destroy();
89 }
90
91 Layer getRoot() {
92 return mIsReady ? mRootLayer : null;
93 }
94
95 Layer getLowResLayer() {
96 return mIsReady ? mLowResLayer : null;
97 }
98
99 public LayerView getView() {
100 return mView;
101 }
102
107 private boolean getRedrawHint() {
108 if (mForceRedraw) {
109 mForceRedraw = false;
110 return true;
111 }
112
113 if (!mPanZoomController.getRedrawHint()) {
114 return false;
115 }
116 return mDisplayPortCalculator.aboutToCheckerboard(mViewportMetrics, mPanZoomController.getVelocityVector(), getDisplayPort());
117 }
118
127 void setViewportSize(FloatSize size, boolean forceResizeEvent) {
129 sendResizeEventIfNecessary(forceResizeEvent);
130 }
131
132 PanZoomController getPanZoomController() {
133 return mPanZoomController;
134 }
135
136 /* Informs Gecko that the screen size has changed.
137 * @param force: If true, a resize event will always be sent, otherwise
138 * it is only sent if size has changed. */
139 private void sendResizeEventIfNecessary(boolean force) {
140 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
141 IntSize newScreenSize = new IntSize(metrics.widthPixels, metrics.heightPixels);
142
143 if (!force && mScreenSize.equals(newScreenSize)) {
144 return;
145 }
146
147 mScreenSize = newScreenSize;
148
150 }
151
155 private void setPageRect(RectF rect, RectF cssRect) {
156 // Since the "rect" is always just a multiple of "cssRect" we don't need to
157 // check both; this function assumes that both "rect" and "cssRect" are relative
158 // the zoom factor in mViewportMetrics.
159 if (mViewportMetrics.getCssPageRect().equals(cssRect))
160 return;
161
163
164 // Page size is owned by the layer client, so no need to notify it of
165 // this change.
166
167 post(new Runnable() {
168 public void run() {
169 mPanZoomController.pageRectUpdated();
171 }
172 });
173 }
174
175 private void adjustViewport(DisplayPortMetrics displayPort) {
177
178 ImmutableViewportMetrics clampedMetrics = metrics.clamp();
179
180 if (displayPort == null) {
181 displayPort = mDisplayPortCalculator.calculate(metrics, mPanZoomController.getVelocityVector());
182 }
183
184 mDisplayPort = displayPort;
185
187 }
188
192 public void abortPanZoomAnimation() {
193 if (mPanZoomController != null) {
194 mView.post(new Runnable() {
195 public void run() {
196 mPanZoomController.abortAnimation();
197 }
198 });
199 }
200 }
201
202 public void setZoomConstraints(ZoomConstraints constraints) {
203 mZoomConstraints = constraints;
204 }
205
211 public void setPageRect(float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom) {
212 synchronized (getLock()) {
213 RectF cssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
214 float ourZoom = getViewportMetrics().zoomFactor;
215 setPageRect(RectUtils.scale(cssPageRect, ourZoom), cssPageRect);
216 // Here the page size of the document has changed, but the document being displayed
217 // is still the same. Therefore, we don't need to send anything to browser.js; any
218 // changes we need to make to the display port will get sent the next time we call
219 // adjustViewport().
220 }
221 }
222
224 return mDisplayPort;
225 }
226
227 public void beginDrawing() {
230 }
231
232 public void endDrawing() {
235 }
236
237 private void geometryChanged() {
239 if (getRedrawHint()) {
240 adjustViewport(null);
241 }
242 }
243
245 @Override
247 return mViewportMetrics;
248 }
249
251 @Override
253 return mZoomConstraints;
254 }
255
257 @Override
259 if (mIsReady) {
260 // We know what the final viewport of the animation is going to be, so
261 // immediately request a draw of that area by setting the display port
262 // accordingly. This way we should have the content pre-rendered by the
263 // time the animation is done.
264 DisplayPortMetrics displayPort = mDisplayPortCalculator.calculate(viewport, null);
265 adjustViewport(displayPort);
266 }
267 }
268
272 @Override
274 mViewportMetrics = viewport;
276 if (mIsReady) {
278 }
279 }
280
282 @Override
283 public void forceRedraw() {
284 mForceRedraw = true;
285 if (mIsReady) {
287 }
288 }
289
291 @Override
292 public boolean post(Runnable action) {
293 return mView.post(action);
294 }
295
297 @Override
298 public Object getLock() {
299 return this;
300 }
301
302 public PointF convertViewPointToLayerPoint(PointF viewPoint) {
304 PointF origin = viewportMetrics.getOrigin();
305 float zoom = viewportMetrics.zoomFactor;
306
307 return new PointF(
308 ((viewPoint.x + origin.x) / zoom),
309 ((viewPoint.y + origin.y) / zoom));
310 }
311
313 @Override
314 public boolean isFullScreen() {
315 return false;
316 }
317
318 public void zoomTo(RectF rect) {
319 if (mPanZoomController instanceof JavaPanZoomController) {
320 ((JavaPanZoomController) mPanZoomController).animatedZoomTo(rect);
321 }
322 }
323
327 public void moveTo(PointF point, Float zoom) {
328 if (mPanZoomController instanceof JavaPanZoomController) {
329 ((JavaPanZoomController) mPanZoomController).animatedMove(point, zoom);
330 }
331 }
332
333 public void zoomTo(float pageWidth, float pageHeight) {
334 zoomTo(new RectF(0, 0, pageWidth, pageHeight));
335 }
336
337 public void forceRender() {
339 }
340
341 /* Root Layer Access */
342 private void reevaluateTiles() {
345 }
346
347 public void clearAndResetlayers() {
350 }
351
352 public void invalidateTiles(List<SubTile> tilesToInvalidate, RectF rect) {
353 mLowResLayer.invalidateTiles(tilesToInvalidate, rect);
354 mRootLayer.invalidateTiles(tilesToInvalidate, rect);
355 }
356}
Common static LOKit functions, functions to send events.
Definition: LOKitShell.java:26
static void sendSizeChangedEvent(int width, int height)
Definition: LOKitShell.java:94
Main activity of the LibreOffice App.
void beginTransaction()
Call this before modifying the layer.
void reevaluateTiles(ImmutableViewportMetrics viewportMetrics, DisplayPortMetrics mDisplayPort)
void endTransaction()
Call this when you're done modifying the layer.
void invalidateTiles(List< SubTile > tilesToInvalidate, RectF cssRect)
Invalidate tiles which intersect the input rect.
void zoomTo(float pageWidth, float pageHeight)
void setZoomConstraints(ZoomConstraints constraints)
final DisplayPortCalculator mDisplayPortCalculator
void setAnimationTarget(ImmutableViewportMetrics viewport)
Implementation of PanZoomTarget.
volatile ImmutableViewportMetrics mViewportMetrics
void invalidateTiles(List< SubTile > tilesToInvalidate, RectF rect)
void abortPanZoomAnimation()
Aborts any pan/zoom animation that is currently in progress.
void adjustViewport(DisplayPortMetrics displayPort)
boolean getRedrawHint()
Returns true if this controller is fine with performing a redraw operation or false if it would prefe...
void setViewportMetrics(ImmutableViewportMetrics viewport)
Implementation of PanZoomTarget You must hold the monitor while calling this.
void setPageRect(RectF rect, RectF cssRect)
Sets the current page rect.
void setPageRect(float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom)
The compositor invokes this function whenever it determines that the page rect has changed (based on ...
Object getLock()
Implementation of PanZoomTarget.
PointF convertViewPointToLayerPoint(PointF viewPoint)
void moveTo(PointF point, Float zoom)
Move the viewport to the desired point, and change the zoom level.
void forceRedraw()
Implementation of PanZoomTarget.
GeckoLayerClient(LibreOfficeMainActivity context)
boolean isFullScreen()
Implementation of PanZoomTarget.
ZoomConstraints getZoomConstraints()
Implementation of PanZoomTarget.
ImmutableViewportMetrics getViewportMetrics()
Implementation of PanZoomTarget.
boolean post(Runnable action)
Implementation of PanZoomTarget.
ImmutableViewportMetrics are used to store the viewport metrics in way that we can access a version o...
ImmutableViewportMetrics clamp()
Clamps the viewport to remain within the page rect.
ImmutableViewportMetrics setViewportSize(float width, float height)
ImmutableViewportMetrics setPageRect(RectF pageRect, RectF cssPageRect)
boolean equals(IntSize size)
Definition: IntSize.java:35
The layer renderer implements the rendering logic for a layer view.
A view rendered by the layer compositor.
Definition: LayerView.java:41
void setLayerRenderer(LayerRenderer renderer)
Definition: LayerView.java:212
static RectF scale(RectF rect, float scale)
Definition: RectUtils.java:44
def run(arg=None, arg2=-1)
def point()
size
action