build/mobile/robocop/FennecNativeDriver.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko;
michael@0 6
michael@0 7 import java.io.BufferedOutputStream;
michael@0 8 import java.io.BufferedReader;
michael@0 9 import java.io.DataOutputStream;
michael@0 10 import java.io.File;
michael@0 11 import java.io.FileOutputStream;
michael@0 12 import java.io.FileReader;
michael@0 13 import java.io.FileWriter;
michael@0 14 import java.io.IOException;
michael@0 15 import java.io.PrintWriter;
michael@0 16 import java.nio.IntBuffer;
michael@0 17 import java.util.HashMap;
michael@0 18 import java.util.List;
michael@0 19 import java.util.Map;
michael@0 20
michael@0 21 import org.json.JSONException;
michael@0 22 import org.json.JSONObject;
michael@0 23 import org.mozilla.gecko.gfx.LayerView;
michael@0 24 import org.mozilla.gecko.gfx.PanningPerfAPI;
michael@0 25 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 26
michael@0 27 import android.app.Activity;
michael@0 28 import android.util.Log;
michael@0 29 import android.view.View;
michael@0 30
michael@0 31 import com.jayway.android.robotium.solo.Solo;
michael@0 32
michael@0 33 public class FennecNativeDriver implements Driver {
michael@0 34 private static final int FRAME_TIME_THRESHOLD = 25; // allow 25ms per frame (40fps)
michael@0 35
michael@0 36 private Activity mActivity;
michael@0 37 private Solo mSolo;
michael@0 38 private String mRootPath;
michael@0 39
michael@0 40 private static String mLogFile = null;
michael@0 41 private static LogLevel mLogLevel = LogLevel.INFO;
michael@0 42
michael@0 43 public enum LogLevel {
michael@0 44 DEBUG(1),
michael@0 45 INFO(2),
michael@0 46 WARN(3),
michael@0 47 ERROR(4);
michael@0 48
michael@0 49 private int mValue;
michael@0 50 LogLevel(int value) {
michael@0 51 mValue = value;
michael@0 52 }
michael@0 53 public boolean isEnabled(LogLevel configuredLevel) {
michael@0 54 return mValue >= configuredLevel.getValue();
michael@0 55 }
michael@0 56 private int getValue() {
michael@0 57 return mValue;
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 public FennecNativeDriver(Activity activity, Solo robocop, String rootPath) {
michael@0 62 mActivity = activity;
michael@0 63 mSolo = robocop;
michael@0 64 mRootPath = rootPath;
michael@0 65 }
michael@0 66
michael@0 67 //Information on the location of the Gecko Frame.
michael@0 68 private boolean mGeckoInfo = false;
michael@0 69 private int mGeckoTop = 100;
michael@0 70 private int mGeckoLeft = 0;
michael@0 71 private int mGeckoHeight= 700;
michael@0 72 private int mGeckoWidth = 1024;
michael@0 73
michael@0 74 private void getGeckoInfo() {
michael@0 75 View geckoLayout = mActivity.findViewById(R.id.gecko_layout);
michael@0 76 if (geckoLayout != null) {
michael@0 77 int[] pos = new int[2];
michael@0 78 geckoLayout.getLocationOnScreen(pos);
michael@0 79 mGeckoTop = pos[1];
michael@0 80 mGeckoLeft = pos[0];
michael@0 81 mGeckoWidth = geckoLayout.getWidth();
michael@0 82 mGeckoHeight = geckoLayout.getHeight();
michael@0 83 mGeckoInfo = true;
michael@0 84 } else {
michael@0 85 throw new RoboCopException("Unable to find view gecko_layout");
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 public int getGeckoTop() {
michael@0 90 if (!mGeckoInfo) {
michael@0 91 getGeckoInfo();
michael@0 92 }
michael@0 93 return mGeckoTop;
michael@0 94 }
michael@0 95
michael@0 96 public int getGeckoLeft() {
michael@0 97 if (!mGeckoInfo) {
michael@0 98 getGeckoInfo();
michael@0 99 }
michael@0 100 return mGeckoLeft;
michael@0 101 }
michael@0 102
michael@0 103 public int getGeckoHeight() {
michael@0 104 if (!mGeckoInfo) {
michael@0 105 getGeckoInfo();
michael@0 106 }
michael@0 107 return mGeckoHeight;
michael@0 108 }
michael@0 109
michael@0 110 public int getGeckoWidth() {
michael@0 111 if (!mGeckoInfo) {
michael@0 112 getGeckoInfo();
michael@0 113 }
michael@0 114 return mGeckoWidth;
michael@0 115 }
michael@0 116
michael@0 117 /** Find the element with given id.
michael@0 118 *
michael@0 119 * @return An Element representing the view, or null if the view is not found.
michael@0 120 */
michael@0 121 public Element findElement(Activity activity, int id) {
michael@0 122 return new FennecNativeElement(id, activity);
michael@0 123 }
michael@0 124
michael@0 125 public void startFrameRecording() {
michael@0 126 PanningPerfAPI.startFrameTimeRecording();
michael@0 127 }
michael@0 128
michael@0 129 public int stopFrameRecording() {
michael@0 130 final List<Long> frames = PanningPerfAPI.stopFrameTimeRecording();
michael@0 131 int badness = 0;
michael@0 132 for (int i = 1; i < frames.size(); i++) {
michael@0 133 long frameTime = frames.get(i) - frames.get(i - 1);
michael@0 134 int delay = (int)(frameTime - FRAME_TIME_THRESHOLD);
michael@0 135 // for each frame we miss, add the square of the delay. This
michael@0 136 // makes large delays much worse than small delays.
michael@0 137 if (delay > 0) {
michael@0 138 badness += delay * delay;
michael@0 139 }
michael@0 140 }
michael@0 141
michael@0 142 // Don't do any averaging of the numbers because really we want to
michael@0 143 // know how bad the jank was at its worst
michael@0 144 return badness;
michael@0 145 }
michael@0 146
michael@0 147 public void startCheckerboardRecording() {
michael@0 148 PanningPerfAPI.startCheckerboardRecording();
michael@0 149 }
michael@0 150
michael@0 151 public float stopCheckerboardRecording() {
michael@0 152 final List<Float> checkerboard = PanningPerfAPI.stopCheckerboardRecording();
michael@0 153 float total = 0;
michael@0 154 for (float val : checkerboard) {
michael@0 155 total += val;
michael@0 156 }
michael@0 157 return total * 100.0f;
michael@0 158 }
michael@0 159
michael@0 160 private LayerView getSurfaceView() {
michael@0 161 final LayerView layerView = mSolo.getView(LayerView.class, 0);
michael@0 162
michael@0 163 if (layerView == null) {
michael@0 164 log(LogLevel.WARN, "getSurfaceView could not find LayerView");
michael@0 165 for (final View v : mSolo.getViews()) {
michael@0 166 log(LogLevel.WARN, " View: " + v);
michael@0 167 }
michael@0 168 }
michael@0 169 return layerView;
michael@0 170 }
michael@0 171
michael@0 172 public PaintedSurface getPaintedSurface() {
michael@0 173 final LayerView view = getSurfaceView();
michael@0 174 if (view == null) {
michael@0 175 return null;
michael@0 176 }
michael@0 177
michael@0 178 final IntBuffer pixelBuffer = view.getPixels();
michael@0 179
michael@0 180 // now we need to (1) flip the image, because GL likes to do things up-side-down,
michael@0 181 // and (2) rearrange the bits from AGBR-8888 to ARGB-8888.
michael@0 182 int w = view.getWidth();
michael@0 183 int h = view.getHeight();
michael@0 184 pixelBuffer.position(0);
michael@0 185 String mapFile = mRootPath + "/pixels.map";
michael@0 186
michael@0 187 FileOutputStream fos = null;
michael@0 188 BufferedOutputStream bos = null;
michael@0 189 DataOutputStream dos = null;
michael@0 190 try {
michael@0 191 fos = new FileOutputStream(mapFile);
michael@0 192 bos = new BufferedOutputStream(fos);
michael@0 193 dos = new DataOutputStream(bos);
michael@0 194
michael@0 195 for (int y = h - 1; y >= 0; y--) {
michael@0 196 for (int x = 0; x < w; x++) {
michael@0 197 int agbr = pixelBuffer.get();
michael@0 198 dos.writeInt((agbr & 0xFF00FF00) | ((agbr >> 16) & 0x000000FF) | ((agbr << 16) & 0x00FF0000));
michael@0 199 }
michael@0 200 }
michael@0 201 } catch (IOException e) {
michael@0 202 throw new RoboCopException("exception with pixel writer on file: " + mapFile);
michael@0 203 } finally {
michael@0 204 try {
michael@0 205 if (dos != null) {
michael@0 206 dos.flush();
michael@0 207 dos.close();
michael@0 208 }
michael@0 209 // closing dos automatically closes bos
michael@0 210 if (fos != null) {
michael@0 211 fos.flush();
michael@0 212 fos.close();
michael@0 213 }
michael@0 214 } catch (IOException e) {
michael@0 215 log(LogLevel.ERROR, e);
michael@0 216 throw new RoboCopException("exception closing pixel writer on file: " + mapFile);
michael@0 217 }
michael@0 218 }
michael@0 219 return new PaintedSurface(mapFile, w, h);
michael@0 220 }
michael@0 221
michael@0 222 public int mHeight=0;
michael@0 223 public int mScrollHeight=0;
michael@0 224 public int mPageHeight=10;
michael@0 225
michael@0 226 public int getScrollHeight() {
michael@0 227 return mScrollHeight;
michael@0 228 }
michael@0 229 public int getPageHeight() {
michael@0 230 return mPageHeight;
michael@0 231 }
michael@0 232 public int getHeight() {
michael@0 233 return mHeight;
michael@0 234 }
michael@0 235
michael@0 236 public void setupScrollHandling() {
michael@0 237 GeckoAppShell.registerEventListener("robocop:scroll", new GeckoEventListener() {
michael@0 238 @Override
michael@0 239 public void handleMessage(final String event, final JSONObject message) {
michael@0 240 try {
michael@0 241 mScrollHeight = message.getInt("y");
michael@0 242 mHeight = message.getInt("cheight");
michael@0 243 // We don't want a height of 0. That means it's a bad response.
michael@0 244 if (mHeight > 0) {
michael@0 245 mPageHeight = message.getInt("height");
michael@0 246 }
michael@0 247 } catch (JSONException e) {
michael@0 248 FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
michael@0 249 "WARNING: ScrollReceived, but message does not contain " +
michael@0 250 "expected fields: " + e);
michael@0 251 }
michael@0 252 }
michael@0 253 });
michael@0 254 }
michael@0 255
michael@0 256 /**
michael@0 257 * Takes a filename, loads the file, and returns a string version of the entire file.
michael@0 258 */
michael@0 259 public static String getFile(String filename)
michael@0 260 {
michael@0 261 StringBuilder text = new StringBuilder();
michael@0 262
michael@0 263 BufferedReader br = null;
michael@0 264 try {
michael@0 265 br = new BufferedReader(new FileReader(filename));
michael@0 266 String line;
michael@0 267
michael@0 268 while ((line = br.readLine()) != null) {
michael@0 269 text.append(line);
michael@0 270 text.append('\n');
michael@0 271 }
michael@0 272 } catch (IOException e) {
michael@0 273 log(LogLevel.ERROR, e);
michael@0 274 } finally {
michael@0 275 try {
michael@0 276 br.close();
michael@0 277 } catch (IOException e) {
michael@0 278 }
michael@0 279 }
michael@0 280 return text.toString();
michael@0 281 }
michael@0 282
michael@0 283 /**
michael@0 284 * Takes a string of "key=value" pairs split by \n and creates a hash table.
michael@0 285 */
michael@0 286 public static Map<String, String> convertTextToTable(String data)
michael@0 287 {
michael@0 288 HashMap<String, String> retVal = new HashMap<String, String>();
michael@0 289
michael@0 290 String[] lines = data.split("\n");
michael@0 291 for (int i = 0; i < lines.length; i++) {
michael@0 292 String[] parts = lines[i].split("=", 2);
michael@0 293 retVal.put(parts[0].trim(), parts[1].trim());
michael@0 294 }
michael@0 295 return retVal;
michael@0 296 }
michael@0 297
michael@0 298 public static void logAllStackTraces(LogLevel level) {
michael@0 299 StringBuffer sb = new StringBuffer();
michael@0 300 sb.append("Dumping ALL the threads!\n");
michael@0 301 Map<Thread, StackTraceElement[]> allStacks = Thread.getAllStackTraces();
michael@0 302 for (Thread t : allStacks.keySet()) {
michael@0 303 sb.append(t.toString()).append('\n');
michael@0 304 for (StackTraceElement ste : allStacks.get(t)) {
michael@0 305 sb.append(ste.toString()).append('\n');
michael@0 306 }
michael@0 307 sb.append('\n');
michael@0 308 }
michael@0 309 log(level, sb.toString());
michael@0 310 }
michael@0 311
michael@0 312 /**
michael@0 313 * Set the filename used for logging. If the file already exists, delete it
michael@0 314 * as a safe-guard against accidentally appending to an old log file.
michael@0 315 */
michael@0 316 public static void setLogFile(String filename) {
michael@0 317 mLogFile = filename;
michael@0 318 File file = new File(mLogFile);
michael@0 319 if (file.exists()) {
michael@0 320 file.delete();
michael@0 321 }
michael@0 322 }
michael@0 323
michael@0 324 public static void setLogLevel(LogLevel level) {
michael@0 325 mLogLevel = level;
michael@0 326 }
michael@0 327
michael@0 328 public static void log(LogLevel level, String message) {
michael@0 329 log(level, message, null);
michael@0 330 }
michael@0 331
michael@0 332 public static void log(LogLevel level, Throwable t) {
michael@0 333 log(level, null, t);
michael@0 334 }
michael@0 335
michael@0 336 public static void log(LogLevel level, String message, Throwable t) {
michael@0 337 if (mLogFile == null) {
michael@0 338 assert(false);
michael@0 339 }
michael@0 340
michael@0 341 if (level.isEnabled(mLogLevel)) {
michael@0 342 PrintWriter pw = null;
michael@0 343 try {
michael@0 344 pw = new PrintWriter(new FileWriter(mLogFile, true));
michael@0 345 if (message != null) {
michael@0 346 pw.println(message);
michael@0 347 }
michael@0 348 if (t != null) {
michael@0 349 t.printStackTrace(pw);
michael@0 350 }
michael@0 351 } catch (IOException ioe) {
michael@0 352 Log.e("Robocop", "exception with file writer on: " + mLogFile);
michael@0 353 } finally {
michael@0 354 pw.close();
michael@0 355 }
michael@0 356 // PrintWriter doesn't throw IOE but sets an error flag instead,
michael@0 357 // so check for that
michael@0 358 if (pw.checkError()) {
michael@0 359 Log.e("Robocop", "exception with file writer on: " + mLogFile);
michael@0 360 }
michael@0 361 }
michael@0 362
michael@0 363 if (level == LogLevel.INFO) {
michael@0 364 Log.i("Robocop", message, t);
michael@0 365 } else if (level == LogLevel.DEBUG) {
michael@0 366 Log.d("Robocop", message, t);
michael@0 367 } else if (level == LogLevel.WARN) {
michael@0 368 Log.w("Robocop", message, t);
michael@0 369 } else if (level == LogLevel.ERROR) {
michael@0 370 Log.e("Robocop", message, t);
michael@0 371 }
michael@0 372 }
michael@0 373 }

mercurial