/
ServoView.java
327 lines (269 loc) · 8.97 KB
/
ServoView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* 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/. */
package com.mozilla.servoview;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.opengl.GLES31;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Choreographer;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.OverScroller;
import com.mozilla.servoview.Servo.Client;
import com.mozilla.servoview.Servo.GfxCallbacks;
import com.mozilla.servoview.Servo.RunCallback;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class ServoView extends GLSurfaceView
implements
GestureDetector.OnGestureListener,
ScaleGestureDetector.OnScaleGestureListener,
Choreographer.FrameCallback,
GfxCallbacks,
RunCallback {
private static final String LOGTAG = "ServoView";
private Activity mActivity;
private Servo mServo;
private Client mClient = null;
private Uri mInitialUri = null;
private boolean mAnimating;
private String mServoArgs = "";
private GestureDetector mGestureDetector;
private ScaleGestureDetector mScaleGestureDetector;
private OverScroller mScroller;
private int mLastX = 0;
private int mCurX = 0;
private int mLastY = 0;
private int mCurY = 0;
private boolean mFlinging;
private boolean mScrolling;
private boolean mZooming;
private float mZoomFactor = 1;
public ServoView(Context context, AttributeSet attrs) {
super(context, attrs);
mActivity = (Activity) context;
setFocusable(true);
setFocusableInTouchMode(true);
setWillNotCacheDrawing(false);
setEGLContextClientVersion(3);
setEGLConfigChooser(8, 8, 8, 8, 24, 0);
setPreserveEGLContextOnPause(true);
ServoGLRenderer mRenderer = new ServoGLRenderer(this);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
initGestures(context);
}
public void setServoArgs(String args) {
mServoArgs = args != null ? args : "";
}
public void reload() {
mServo.reload();
}
public void goBack() {
mServo.goBack();
}
public void goForward() {
mServo.goForward();
}
public void stop() {
mServo.stop();
}
public void onSurfaceInvalidated(int width, int height) {
if (mServo != null) {
mServo.resize(width, height);
mServo.refresh();
}
}
public void loadUri(Uri uri) {
if (mServo != null) {
mServo.loadUri(uri.toString());
} else {
mInitialUri = uri;
}
}
public void flushGLBuffers() {
inUIThread(() -> {
requestRender();
});
}
// Scroll and click
public void animationStateChanged(boolean animating) {
if (!mAnimating && animating) {
post(() -> Choreographer.getInstance().postFrameCallback(ServoView.this));
}
mAnimating = animating;
}
public void makeCurrent() {
}
public void inGLThread(Runnable f) {
queueEvent(f);
}
public void inUIThread(Runnable f) {
post(f);
}
public void onGLReady() {
final boolean showLogs = true;
int width = getWidth();
int height = getHeight();
inGLThread(() -> {
String uri = mInitialUri == null ? null : mInitialUri.toString();
mServo = new Servo(this, this, mClient, mActivity, mServoArgs, uri, width, height, showLogs);
});
}
public void setClient(Client client) {
mClient = client;
}
private void initGestures(Context context) {
mGestureDetector = new GestureDetector(context, this);
mScaleGestureDetector = new ScaleGestureDetector(context, this);
mScroller = new OverScroller(context);
}
public void doFrame(long frameTimeNanos) {
// 3 reasons to be here: animating or scrolling/flinging or pinching
if (mFlinging && mScroller.isFinished()) {
mFlinging = false;
mScrolling = false;
mServo.scrollEnd(0, 0, mCurX, mCurY);
}
if (mFlinging) {
mScroller.computeScrollOffset();
mCurX = mScroller.getCurrX();
mCurY = mScroller.getCurrY();
}
int dx = mCurX - mLastX;
int dy = mCurY - mLastY;
mLastX = mCurX;
mLastY = mCurY;
boolean scrollNecessary = mScrolling && (dx != 0 || dy != 0);
boolean zoomNecessary = mZooming && mZoomFactor != 1;
if (scrollNecessary) {
mServo.scroll(dx, dy, mCurX, mCurY);
}
if (zoomNecessary) {
mServo.pinchZoom(mZoomFactor, 0, 0);
mZoomFactor = 1;
}
if (!zoomNecessary && !scrollNecessary && mAnimating) {
requestRender();
}
if (mZooming || mScrolling || mAnimating) {
Choreographer.getInstance().postFrameCallback(this);
}
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mFlinging = true;
// FIXME: magic values
// https://github.com/servo/servo/issues/20361
int mPageWidth = 80000;
int mPageHeight = 80000;
mCurX = velocityX < 0 ? mPageWidth : 0;
mLastX = mCurX;
mCurY = velocityY < 0 ? mPageHeight : 0;
mLastY = mCurY;
mScroller.fling(mCurX, mCurY, (int) velocityX, (int) velocityY, 0, mPageWidth, 0, mPageHeight);
return true;
}
public boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);
return true;
}
public boolean onTouchEvent(final MotionEvent e) {
mGestureDetector.onTouchEvent(e);
mScaleGestureDetector.onTouchEvent(e);
int action = e.getActionMasked();
switch (action) {
case (MotionEvent.ACTION_DOWN):
mCurX = (int) e.getX();
mLastX = mCurX;
mCurY = (int) e.getY();
mLastY = mCurY;
mScroller.forceFinished(true);
mServo.scrollStart(0, 0, mCurX, mCurY);
mScrolling = true;
Choreographer.getInstance().postFrameCallback(this);
return true;
case (MotionEvent.ACTION_MOVE):
mCurX = (int) e.getX();
mCurY = (int) e.getY();
return true;
case (MotionEvent.ACTION_UP):
case (MotionEvent.ACTION_CANCEL):
if (!mFlinging) {
mScrolling = false;
mServo.scrollEnd(0, 0, mCurX, mCurY);
}
return true;
default:
return true;
}
}
public boolean onSingleTapUp(MotionEvent e) {
mServo.click((int) e.getX(), (int) e.getY());
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return true;
}
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
if (mScroller.isFinished()) {
mZoomFactor = detector.getScaleFactor();
mZooming = true;
mServo.pinchZoomStart(mZoomFactor, 0, 0);
Choreographer.getInstance().postFrameCallback(this);
return true;
} else {
return false;
}
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
mZoomFactor *= detector.getScaleFactor();
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
mZoomFactor = detector.getScaleFactor();
mZooming = false;
mServo.pinchZoomEnd(mZoomFactor, 0, 0);
}
@Override
public void onPause() {
super.onPause();
if (mServo != null) {
mServo.suspend(true);
}
}
@Override
public void onResume() {
super.onResume();
if (mServo != null) {
mServo.suspend(false);
}
}
static class ServoGLRenderer implements Renderer {
private final ServoView mView;
ServoGLRenderer(ServoView view) {
mView = view;
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
mView.onGLReady();
}
public void onDrawFrame(GL10 unused) {
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES31.glViewport(0, 0, width, height);
mView.onSurfaceInvalidated(width, height);
}
}
}