blob: 10802b449d03ab163036e567ed99b1bba4119912 [file] [log] [blame]
Chris Craik5888ec22011-07-15 16:24:37 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.test.tilebenchmark;
18
19import android.content.Context;
Chris Craik55ad2ef2011-08-19 17:35:20 -070020import android.os.CountDownTimer;
Chris Craik5888ec22011-07-15 16:24:37 -070021import android.util.AttributeSet;
Chris Craik555c55e2011-07-28 15:39:43 -070022import android.util.Log;
Chris Craik5888ec22011-07-15 16:24:37 -070023import android.webkit.WebView;
24
25import com.test.tilebenchmark.ProfileActivity.ProfileCallback;
Chris Craik555c55e2011-07-28 15:39:43 -070026import com.test.tilebenchmark.RunData.TileData;
Chris Craik5888ec22011-07-15 16:24:37 -070027
28public class ProfiledWebView extends WebView {
29 private int mSpeed;
30
Chris Craik55ad2ef2011-08-19 17:35:20 -070031 private boolean mIsTesting = false;
32 private boolean mIsScrolling = false;
Chris Craik5888ec22011-07-15 16:24:37 -070033 private ProfileCallback mCallback;
Chris Craik555c55e2011-07-28 15:39:43 -070034 private long mContentInvalMillis;
Chris Craik55ad2ef2011-08-19 17:35:20 -070035 private boolean mHadToBeForced = false;
36 private int mTestCount = 0;
37 private static final int LOAD_STALL_MILLIS = 5000; // nr of millis after load,
38 // before test is forced
Chris Craik5888ec22011-07-15 16:24:37 -070039
40 public ProfiledWebView(Context context) {
41 super(context);
42 }
43
44 public ProfiledWebView(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 }
47
48 public ProfiledWebView(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50 }
51
52 public ProfiledWebView(Context context, AttributeSet attrs, int defStyle,
53 boolean privateBrowsing) {
54 super(context, attrs, defStyle, privateBrowsing);
55 }
56
57 @Override
58 protected void onDraw(android.graphics.Canvas canvas) {
Chris Craik55ad2ef2011-08-19 17:35:20 -070059 if (mIsTesting && mIsScrolling) {
Chris Craik5888ec22011-07-15 16:24:37 -070060 if (canScrollVertically(1)) {
61 scrollBy(0, mSpeed);
62 } else {
63 stopScrollTest();
Chris Craik55ad2ef2011-08-19 17:35:20 -070064 mIsScrolling = false;
Chris Craik5888ec22011-07-15 16:24:37 -070065 }
66 }
67 super.onDraw(canvas);
68 }
69
70 /*
Chris Craik21555ab2011-07-21 11:52:19 -070071 * Called once the page is loaded to start scrolling for evaluating tiles.
Chris Craik555c55e2011-07-28 15:39:43 -070072 * If autoScrolling isn't set, stop must be called manually. Before
73 * scrolling, invalidate all content and redraw it, measuring time taken.
Chris Craik5888ec22011-07-15 16:24:37 -070074 */
Chris Craik21555ab2011-07-21 11:52:19 -070075 public void startScrollTest(ProfileCallback callback, boolean autoScrolling) {
Chris Craik55ad2ef2011-08-19 17:35:20 -070076 mIsScrolling = autoScrolling;
Chris Craik5888ec22011-07-15 16:24:37 -070077 mCallback = callback;
Chris Craik55ad2ef2011-08-19 17:35:20 -070078 mIsTesting = false;
Chris Craik555c55e2011-07-28 15:39:43 -070079 mContentInvalMillis = System.currentTimeMillis();
80 registerPageSwapCallback();
81 contentInvalidateAll();
Chris Craik5888ec22011-07-15 16:24:37 -070082 invalidate();
Chris Craik55ad2ef2011-08-19 17:35:20 -070083
84 mTestCount++;
85 final int testCount = mTestCount;
86
87 if (autoScrolling) {
88 // after a while, force it to start even if the pages haven't swapped
89 new CountDownTimer(LOAD_STALL_MILLIS, LOAD_STALL_MILLIS) {
90 @Override
91 public void onTick(long millisUntilFinished) {
92 }
93
94 @Override
95 public void onFinish() {
96 if (testCount == mTestCount && !mIsTesting) {
97 mHadToBeForced = true;
98 Log.d("ProfiledWebView", "num " + testCount
99 + " forcing a page swap with a scroll...");
100 scrollBy(0, 1);
101 invalidate(); // ensure a redraw so that auto-scrolling can occur
102 }
103 }
104 }.start();
105 }
Chris Craik5888ec22011-07-15 16:24:37 -0700106 }
107
108 /*
Chris Craik555c55e2011-07-28 15:39:43 -0700109 * Called after the manual contentInvalidateAll, after the tiles have all
110 * been redrawn.
111 */
112 @Override
Chris Craikd0051c02011-11-29 10:26:10 -0800113 protected void pageSwapCallback(boolean startAnim) {
Chris Craik555c55e2011-07-28 15:39:43 -0700114 mContentInvalMillis = System.currentTimeMillis() - mContentInvalMillis;
Chris Craikd0051c02011-11-29 10:26:10 -0800115 super.pageSwapCallback(startAnim);
Chris Craik555c55e2011-07-28 15:39:43 -0700116 Log.d("ProfiledWebView", "REDRAW TOOK " + mContentInvalMillis
117 + "millis");
Chris Craik55ad2ef2011-08-19 17:35:20 -0700118 mIsTesting = true;
Chris Craik555c55e2011-07-28 15:39:43 -0700119 invalidate(); // ensure a redraw so that auto-scrolling can occur
120 tileProfilingStart();
121 }
122
123 /*
Chris Craik5888ec22011-07-15 16:24:37 -0700124 * Called once the page has stopped scrolling
125 */
126 public void stopScrollTest() {
Chris Craik555c55e2011-07-28 15:39:43 -0700127 tileProfilingStop();
Chris Craik55ad2ef2011-08-19 17:35:20 -0700128 mIsTesting = false;
Chris Craik21555ab2011-07-21 11:52:19 -0700129
130 if (mCallback == null) {
131 tileProfilingClear();
132 return;
133 }
Chris Craik5888ec22011-07-15 16:24:37 -0700134
Chris Craik555c55e2011-07-28 15:39:43 -0700135 RunData data = new RunData(super.tileProfilingNumFrames());
Chris Craik55ad2ef2011-08-19 17:35:20 -0700136 // record the time spent (before scrolling) rendering the page
Chris Craik555c55e2011-07-28 15:39:43 -0700137 data.singleStats.put(getResources().getString(R.string.render_millis),
138 (double)mContentInvalMillis);
Chris Craik55ad2ef2011-08-19 17:35:20 -0700139 // record if the page render timed out
140 Log.d("ProfiledWebView", "hadtobeforced = " + mHadToBeForced);
141 data.singleStats.put(getResources().getString(R.string.render_stalls),
142 mHadToBeForced ? 1.0 : 0.0);
143 mHadToBeForced = false;
144
Chris Craik555c55e2011-07-28 15:39:43 -0700145 for (int frame = 0; frame < data.frames.length; frame++) {
146 data.frames[frame] = new TileData[
Chris Craik21555ab2011-07-21 11:52:19 -0700147 tileProfilingNumTilesInFrame(frame)];
Chris Craik555c55e2011-07-28 15:39:43 -0700148 for (int tile = 0; tile < data.frames[frame].length; tile++) {
Chris Craik21555ab2011-07-21 11:52:19 -0700149 int left = tileProfilingGetInt(frame, tile, "left");
150 int top = tileProfilingGetInt(frame, tile, "top");
151 int right = tileProfilingGetInt(frame, tile, "right");
152 int bottom = tileProfilingGetInt(frame, tile, "bottom");
Chris Craik5888ec22011-07-15 16:24:37 -0700153
Chris Craik21555ab2011-07-21 11:52:19 -0700154 boolean isReady = super.tileProfilingGetInt(
155 frame, tile, "isReady") == 1;
156 int level = tileProfilingGetInt(frame, tile, "level");
157
158 float scale = tileProfilingGetFloat(frame, tile, "scale");
159
Chris Craik555c55e2011-07-28 15:39:43 -0700160 data.frames[frame][tile] = data.new TileData(left, top, right, bottom,
Chris Craik21555ab2011-07-21 11:52:19 -0700161 isReady, level, scale);
Chris Craik5888ec22011-07-15 16:24:37 -0700162 }
163 }
Chris Craik555c55e2011-07-28 15:39:43 -0700164 tileProfilingClear();
Chris Craik5888ec22011-07-15 16:24:37 -0700165
166 mCallback.profileCallback(data);
167 }
168
169 @Override
170 public void loadUrl(String url) {
Chris Craik555c55e2011-07-28 15:39:43 -0700171 if (!url.startsWith("http://") && !url.startsWith("file://")) {
Chris Craik5888ec22011-07-15 16:24:37 -0700172 url = "http://" + url;
173 }
174 super.loadUrl(url);
175 }
176
177 public void setAutoScrollSpeed(int speedInt) {
178 mSpeed = speedInt;
179 }
180}